rt4831-backlight.c 6.3 KB

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