rt4831-backlight.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <dt-bindings/leds/rt4831-backlight.h>
  3. #include <linux/backlight.h>
  4. #include <linux/bitops.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mod_devicetable.h>
  7. #include <linux/module.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/property.h>
  10. #include <linux/regmap.h>
  11. #define RT4831_REG_BLCFG 0x02
  12. #define RT4831_REG_BLDIML 0x04
  13. #define RT4831_REG_ENABLE 0x08
  14. #define RT4831_REG_BLOPT2 0x11
  15. #define RT4831_BLMAX_BRIGHTNESS 2048
  16. #define RT4831_BLOVP_MASK GENMASK(7, 5)
  17. #define RT4831_BLOVP_SHIFT 5
  18. #define RT4831_BLPWMEN_MASK BIT(0)
  19. #define RT4831_BLEN_MASK BIT(4)
  20. #define RT4831_BLCH_MASK GENMASK(3, 0)
  21. #define RT4831_BLDIML_MASK GENMASK(2, 0)
  22. #define RT4831_BLDIMH_MASK GENMASK(10, 3)
  23. #define RT4831_BLDIMH_SHIFT 3
  24. #define RT4831_BLOCP_MASK GENMASK(1, 0)
  25. #define RT4831_BLOCP_MINUA 900000
  26. #define RT4831_BLOCP_MAXUA 1800000
  27. #define RT4831_BLOCP_STEPUA 300000
  28. struct rt4831_priv {
  29. struct device *dev;
  30. struct regmap *regmap;
  31. struct backlight_device *bl;
  32. };
  33. static int rt4831_bl_update_status(struct backlight_device *bl_dev)
  34. {
  35. struct rt4831_priv *priv = bl_get_data(bl_dev);
  36. int brightness = backlight_get_brightness(bl_dev);
  37. unsigned int enable = brightness ? RT4831_BLEN_MASK : 0;
  38. u8 v[2];
  39. int ret;
  40. if (brightness) {
  41. v[0] = (brightness - 1) & RT4831_BLDIML_MASK;
  42. v[1] = ((brightness - 1) & RT4831_BLDIMH_MASK) >> RT4831_BLDIMH_SHIFT;
  43. ret = regmap_raw_write(priv->regmap, RT4831_REG_BLDIML, v, sizeof(v));
  44. if (ret)
  45. return ret;
  46. }
  47. return regmap_update_bits(priv->regmap, RT4831_REG_ENABLE, RT4831_BLEN_MASK, enable);
  48. }
  49. static int rt4831_bl_get_brightness(struct backlight_device *bl_dev)
  50. {
  51. struct rt4831_priv *priv = bl_get_data(bl_dev);
  52. unsigned int val;
  53. u8 v[2];
  54. int ret;
  55. ret = regmap_read(priv->regmap, RT4831_REG_ENABLE, &val);
  56. if (ret)
  57. return ret;
  58. if (!(val & RT4831_BLEN_MASK))
  59. return 0;
  60. ret = regmap_raw_read(priv->regmap, RT4831_REG_BLDIML, v, sizeof(v));
  61. if (ret)
  62. return ret;
  63. ret = (v[1] << RT4831_BLDIMH_SHIFT) + (v[0] & RT4831_BLDIML_MASK) + 1;
  64. return ret;
  65. }
  66. static const struct backlight_ops rt4831_bl_ops = {
  67. .options = BL_CORE_SUSPENDRESUME,
  68. .update_status = rt4831_bl_update_status,
  69. .get_brightness = rt4831_bl_get_brightness,
  70. };
  71. static int rt4831_parse_backlight_properties(struct rt4831_priv *priv,
  72. struct backlight_properties *bl_props)
  73. {
  74. struct device *dev = priv->dev;
  75. u8 propval;
  76. u32 brightness, ocp_uA;
  77. unsigned int val = 0;
  78. int ret;
  79. /* common properties */
  80. ret = device_property_read_u32(dev, "max-brightness", &brightness);
  81. if (ret)
  82. brightness = RT4831_BLMAX_BRIGHTNESS;
  83. bl_props->max_brightness = min_t(u32, brightness, RT4831_BLMAX_BRIGHTNESS);
  84. ret = device_property_read_u32(dev, "default-brightness", &brightness);
  85. if (ret)
  86. brightness = bl_props->max_brightness;
  87. bl_props->brightness = min_t(u32, brightness, bl_props->max_brightness);
  88. /* vendor properties */
  89. if (device_property_read_bool(dev, "richtek,pwm-enable"))
  90. val = RT4831_BLPWMEN_MASK;
  91. ret = regmap_update_bits(priv->regmap, RT4831_REG_BLCFG, RT4831_BLPWMEN_MASK, val);
  92. if (ret)
  93. return ret;
  94. ret = device_property_read_u8(dev, "richtek,bled-ovp-sel", &propval);
  95. if (ret)
  96. propval = RT4831_BLOVPLVL_21V;
  97. propval = min_t(u8, propval, RT4831_BLOVPLVL_29V);
  98. ret = regmap_update_bits(priv->regmap, RT4831_REG_BLCFG, RT4831_BLOVP_MASK,
  99. propval << RT4831_BLOVP_SHIFT);
  100. if (ret)
  101. return ret;
  102. /*
  103. * This OCP level is used to protect and limit the inductor current.
  104. * If inductor peak current reach the level, low-side MOSFET will be
  105. * turned off. Meanwhile, the output channel current may be limited.
  106. * To match the configured channel current, the inductor chosen must
  107. * be higher than the OCP level.
  108. *
  109. * Not like the OVP level, the default 21V can be used in the most
  110. * application. But if the chosen OCP level is smaller than needed,
  111. * it will also affect the backlight channel output current to be
  112. * smaller than the register setting.
  113. */
  114. ret = device_property_read_u32(dev, "richtek,bled-ocp-microamp",
  115. &ocp_uA);
  116. if (!ret) {
  117. ocp_uA = clamp_val(ocp_uA, RT4831_BLOCP_MINUA,
  118. RT4831_BLOCP_MAXUA);
  119. val = DIV_ROUND_UP(ocp_uA - RT4831_BLOCP_MINUA,
  120. RT4831_BLOCP_STEPUA);
  121. ret = regmap_update_bits(priv->regmap, RT4831_REG_BLOPT2,
  122. RT4831_BLOCP_MASK, val);
  123. if (ret)
  124. return ret;
  125. }
  126. ret = device_property_read_u8(dev, "richtek,channel-use", &propval);
  127. if (ret) {
  128. dev_err(dev, "richtek,channel-use DT property missing\n");
  129. return ret;
  130. }
  131. if (!(propval & RT4831_BLCH_MASK)) {
  132. dev_err(dev, "No channel specified\n");
  133. return -EINVAL;
  134. }
  135. return regmap_update_bits(priv->regmap, RT4831_REG_ENABLE, RT4831_BLCH_MASK, propval);
  136. }
  137. static int rt4831_bl_probe(struct platform_device *pdev)
  138. {
  139. struct rt4831_priv *priv;
  140. struct backlight_properties bl_props = { .type = BACKLIGHT_RAW,
  141. .scale = BACKLIGHT_SCALE_LINEAR };
  142. int ret;
  143. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  144. if (!priv)
  145. return -ENOMEM;
  146. priv->dev = &pdev->dev;
  147. priv->regmap = dev_get_regmap(pdev->dev.parent, NULL);
  148. if (!priv->regmap) {
  149. dev_err(&pdev->dev, "Failed to init regmap\n");
  150. return -ENODEV;
  151. }
  152. ret = rt4831_parse_backlight_properties(priv, &bl_props);
  153. if (ret) {
  154. dev_err(&pdev->dev, "Failed to parse backlight properties\n");
  155. return ret;
  156. }
  157. priv->bl = devm_backlight_device_register(&pdev->dev, pdev->name, &pdev->dev, priv,
  158. &rt4831_bl_ops, &bl_props);
  159. if (IS_ERR(priv->bl)) {
  160. dev_err(&pdev->dev, "Failed to register backlight\n");
  161. return PTR_ERR(priv->bl);
  162. }
  163. backlight_update_status(priv->bl);
  164. platform_set_drvdata(pdev, priv);
  165. return 0;
  166. }
  167. static void rt4831_bl_remove(struct platform_device *pdev)
  168. {
  169. struct rt4831_priv *priv = platform_get_drvdata(pdev);
  170. struct backlight_device *bl_dev = priv->bl;
  171. bl_dev->props.brightness = 0;
  172. backlight_update_status(priv->bl);
  173. }
  174. static const struct of_device_id __maybe_unused rt4831_bl_of_match[] = {
  175. { .compatible = "richtek,rt4831-backlight", },
  176. {}
  177. };
  178. MODULE_DEVICE_TABLE(of, rt4831_bl_of_match);
  179. static struct platform_driver rt4831_bl_driver = {
  180. .driver = {
  181. .name = "rt4831-backlight",
  182. .of_match_table = rt4831_bl_of_match,
  183. },
  184. .probe = rt4831_bl_probe,
  185. .remove = rt4831_bl_remove,
  186. };
  187. module_platform_driver(rt4831_bl_driver);
  188. MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
  189. MODULE_DESCRIPTION("Richtek RT4831 Backlight Driver");
  190. MODULE_LICENSE("GPL v2");