leds-rt8515.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * LED driver for Richtek RT8515 flash/torch white LEDs
  4. * found on some Samsung mobile phones.
  5. *
  6. * This is a 1.5A Boost dual channel driver produced around 2011.
  7. *
  8. * The component lacks a datasheet, but in the schematic picture
  9. * from the LG P970 service manual you can see the connections
  10. * from the RT8515 to the LED, with two resistors connected
  11. * from the pins "RFS" and "RTS" to ground.
  12. *
  13. * On the LG P970:
  14. * RFS (resistance flash setting?) is 20 kOhm
  15. * RTS (resistance torch setting?) is 39 kOhm
  16. *
  17. * Some sleuthing finds us the RT9387A which we have a datasheet for:
  18. * https://static5.arrow.com/pdfs/2014/7/27/8/21/12/794/rtt_/manual/94download_ds.jspprt9387a.jspprt9387a.pdf
  19. * This apparently works the same way so in theory this driver
  20. * should cover RT9387A as well. This has not been tested, please
  21. * update the compatibles if you add RT9387A support.
  22. *
  23. * Linus Walleij <[email protected]>
  24. */
  25. #include <linux/delay.h>
  26. #include <linux/err.h>
  27. #include <linux/gpio/consumer.h>
  28. #include <linux/led-class-flash.h>
  29. #include <linux/mod_devicetable.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/property.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <media/v4l2-flash-led-class.h>
  35. /* We can provide 15-700 mA out to the LED */
  36. #define RT8515_MIN_IOUT_MA 15
  37. #define RT8515_MAX_IOUT_MA 700
  38. /* The maximum intensity is 1-16 for flash and 1-100 for torch */
  39. #define RT8515_FLASH_MAX 16
  40. #define RT8515_TORCH_MAX 100
  41. #define RT8515_TIMEOUT_US 250000U
  42. #define RT8515_MAX_TIMEOUT_US 300000U
  43. struct rt8515 {
  44. struct led_classdev_flash fled;
  45. struct device *dev;
  46. struct v4l2_flash *v4l2_flash;
  47. struct mutex lock;
  48. struct regulator *reg;
  49. struct gpio_desc *enable_torch;
  50. struct gpio_desc *enable_flash;
  51. struct timer_list powerdown_timer;
  52. u32 max_timeout; /* Flash max timeout */
  53. int flash_max_intensity;
  54. int torch_max_intensity;
  55. };
  56. static struct rt8515 *to_rt8515(struct led_classdev_flash *fled)
  57. {
  58. return container_of(fled, struct rt8515, fled);
  59. }
  60. static void rt8515_gpio_led_off(struct rt8515 *rt)
  61. {
  62. gpiod_set_value(rt->enable_flash, 0);
  63. gpiod_set_value(rt->enable_torch, 0);
  64. }
  65. static void rt8515_gpio_brightness_commit(struct gpio_desc *gpiod,
  66. int brightness)
  67. {
  68. int i;
  69. /*
  70. * Toggling a GPIO line with a small delay increases the
  71. * brightness one step at a time.
  72. */
  73. for (i = 0; i < brightness; i++) {
  74. gpiod_set_value(gpiod, 0);
  75. udelay(1);
  76. gpiod_set_value(gpiod, 1);
  77. udelay(1);
  78. }
  79. }
  80. /* This is setting the torch light level */
  81. static int rt8515_led_brightness_set(struct led_classdev *led,
  82. enum led_brightness brightness)
  83. {
  84. struct led_classdev_flash *fled = lcdev_to_flcdev(led);
  85. struct rt8515 *rt = to_rt8515(fled);
  86. mutex_lock(&rt->lock);
  87. if (brightness == LED_OFF) {
  88. /* Off */
  89. rt8515_gpio_led_off(rt);
  90. } else if (brightness < RT8515_TORCH_MAX) {
  91. /* Step it up to movie mode brightness using the flash pin */
  92. rt8515_gpio_brightness_commit(rt->enable_torch, brightness);
  93. } else {
  94. /* Max torch brightness requested */
  95. gpiod_set_value(rt->enable_torch, 1);
  96. }
  97. mutex_unlock(&rt->lock);
  98. return 0;
  99. }
  100. static int rt8515_led_flash_strobe_set(struct led_classdev_flash *fled,
  101. bool state)
  102. {
  103. struct rt8515 *rt = to_rt8515(fled);
  104. struct led_flash_setting *timeout = &fled->timeout;
  105. int brightness = rt->flash_max_intensity;
  106. mutex_lock(&rt->lock);
  107. if (state) {
  108. /* Enable LED flash mode and set brightness */
  109. rt8515_gpio_brightness_commit(rt->enable_flash, brightness);
  110. /* Set timeout */
  111. mod_timer(&rt->powerdown_timer,
  112. jiffies + usecs_to_jiffies(timeout->val));
  113. } else {
  114. del_timer_sync(&rt->powerdown_timer);
  115. /* Turn the LED off */
  116. rt8515_gpio_led_off(rt);
  117. }
  118. fled->led_cdev.brightness = LED_OFF;
  119. /* After this the torch LED will be disabled */
  120. mutex_unlock(&rt->lock);
  121. return 0;
  122. }
  123. static int rt8515_led_flash_strobe_get(struct led_classdev_flash *fled,
  124. bool *state)
  125. {
  126. struct rt8515 *rt = to_rt8515(fled);
  127. *state = timer_pending(&rt->powerdown_timer);
  128. return 0;
  129. }
  130. static int rt8515_led_flash_timeout_set(struct led_classdev_flash *fled,
  131. u32 timeout)
  132. {
  133. /* The timeout is stored in the led-class-flash core */
  134. return 0;
  135. }
  136. static const struct led_flash_ops rt8515_flash_ops = {
  137. .strobe_set = rt8515_led_flash_strobe_set,
  138. .strobe_get = rt8515_led_flash_strobe_get,
  139. .timeout_set = rt8515_led_flash_timeout_set,
  140. };
  141. static void rt8515_powerdown_timer(struct timer_list *t)
  142. {
  143. struct rt8515 *rt = from_timer(rt, t, powerdown_timer);
  144. /* Turn the LED off */
  145. rt8515_gpio_led_off(rt);
  146. }
  147. static void rt8515_init_flash_timeout(struct rt8515 *rt)
  148. {
  149. struct led_classdev_flash *fled = &rt->fled;
  150. struct led_flash_setting *s;
  151. /* Init flash timeout setting */
  152. s = &fled->timeout;
  153. s->min = 1;
  154. s->max = rt->max_timeout;
  155. s->step = 1;
  156. /*
  157. * Set default timeout to RT8515_TIMEOUT_US except if
  158. * max_timeout from DT is lower.
  159. */
  160. s->val = min(rt->max_timeout, RT8515_TIMEOUT_US);
  161. }
  162. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  163. /* Configure the V2L2 flash subdevice */
  164. static void rt8515_init_v4l2_flash_config(struct rt8515 *rt,
  165. struct v4l2_flash_config *v4l2_sd_cfg)
  166. {
  167. struct led_classdev *led = &rt->fled.led_cdev;
  168. struct led_flash_setting *s;
  169. strscpy(v4l2_sd_cfg->dev_name, led->dev->kobj.name,
  170. sizeof(v4l2_sd_cfg->dev_name));
  171. /*
  172. * Init flash intensity setting: this is a linear scale
  173. * capped from the device tree max intensity setting
  174. * 1..flash_max_intensity
  175. */
  176. s = &v4l2_sd_cfg->intensity;
  177. s->min = 1;
  178. s->max = rt->flash_max_intensity;
  179. s->step = 1;
  180. s->val = s->max;
  181. }
  182. static void rt8515_v4l2_flash_release(struct rt8515 *rt)
  183. {
  184. v4l2_flash_release(rt->v4l2_flash);
  185. }
  186. #else
  187. static void rt8515_init_v4l2_flash_config(struct rt8515 *rt,
  188. struct v4l2_flash_config *v4l2_sd_cfg)
  189. {
  190. }
  191. static void rt8515_v4l2_flash_release(struct rt8515 *rt)
  192. {
  193. }
  194. #endif
  195. static void rt8515_determine_max_intensity(struct rt8515 *rt,
  196. struct fwnode_handle *led,
  197. const char *resistance,
  198. const char *max_ua_prop, int hw_max,
  199. int *max_intensity_setting)
  200. {
  201. u32 res = 0; /* Can't be 0 so 0 is undefined */
  202. u32 ua;
  203. u32 max_ma;
  204. int max_intensity;
  205. int ret;
  206. fwnode_property_read_u32(rt->dev->fwnode, resistance, &res);
  207. ret = fwnode_property_read_u32(led, max_ua_prop, &ua);
  208. /* Missing info in DT, OK go with hardware maxima */
  209. if (ret || res == 0) {
  210. dev_err(rt->dev,
  211. "either %s or %s missing from DT, using HW max\n",
  212. resistance, max_ua_prop);
  213. max_ma = RT8515_MAX_IOUT_MA;
  214. max_intensity = hw_max;
  215. goto out_assign_max;
  216. }
  217. /*
  218. * Formula from the datasheet, this is the maximum current
  219. * defined by the hardware.
  220. */
  221. max_ma = (5500 * 1000) / res;
  222. /*
  223. * Calculate max intensity (linear scaling)
  224. * Formula is ((ua / 1000) / max_ma) * 100, then simplified
  225. */
  226. max_intensity = (ua / 10) / max_ma;
  227. dev_info(rt->dev,
  228. "current restricted from %u to %u mA, max intensity %d/100\n",
  229. max_ma, (ua / 1000), max_intensity);
  230. out_assign_max:
  231. dev_info(rt->dev, "max intensity %d/%d = %d mA\n",
  232. max_intensity, hw_max, max_ma);
  233. *max_intensity_setting = max_intensity;
  234. }
  235. static int rt8515_probe(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct fwnode_handle *child;
  239. struct rt8515 *rt;
  240. struct led_classdev *led;
  241. struct led_classdev_flash *fled;
  242. struct led_init_data init_data = {};
  243. struct v4l2_flash_config v4l2_sd_cfg = {};
  244. int ret;
  245. rt = devm_kzalloc(dev, sizeof(*rt), GFP_KERNEL);
  246. if (!rt)
  247. return -ENOMEM;
  248. rt->dev = dev;
  249. fled = &rt->fled;
  250. led = &fled->led_cdev;
  251. /* ENF - Enable Flash line */
  252. rt->enable_flash = devm_gpiod_get(dev, "enf", GPIOD_OUT_LOW);
  253. if (IS_ERR(rt->enable_flash))
  254. return dev_err_probe(dev, PTR_ERR(rt->enable_flash),
  255. "cannot get ENF (enable flash) GPIO\n");
  256. /* ENT - Enable Torch line */
  257. rt->enable_torch = devm_gpiod_get(dev, "ent", GPIOD_OUT_LOW);
  258. if (IS_ERR(rt->enable_torch))
  259. return dev_err_probe(dev, PTR_ERR(rt->enable_torch),
  260. "cannot get ENT (enable torch) GPIO\n");
  261. child = fwnode_get_next_available_child_node(dev->fwnode, NULL);
  262. if (!child) {
  263. dev_err(dev,
  264. "No fwnode child node found for connected LED.\n");
  265. return -EINVAL;
  266. }
  267. init_data.fwnode = child;
  268. rt8515_determine_max_intensity(rt, child, "richtek,rfs-ohms",
  269. "flash-max-microamp",
  270. RT8515_FLASH_MAX,
  271. &rt->flash_max_intensity);
  272. rt8515_determine_max_intensity(rt, child, "richtek,rts-ohms",
  273. "led-max-microamp",
  274. RT8515_TORCH_MAX,
  275. &rt->torch_max_intensity);
  276. ret = fwnode_property_read_u32(child, "flash-max-timeout-us",
  277. &rt->max_timeout);
  278. if (ret) {
  279. rt->max_timeout = RT8515_MAX_TIMEOUT_US;
  280. dev_warn(dev,
  281. "flash-max-timeout-us property missing\n");
  282. }
  283. timer_setup(&rt->powerdown_timer, rt8515_powerdown_timer, 0);
  284. rt8515_init_flash_timeout(rt);
  285. fled->ops = &rt8515_flash_ops;
  286. led->max_brightness = rt->torch_max_intensity;
  287. led->brightness_set_blocking = rt8515_led_brightness_set;
  288. led->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH;
  289. mutex_init(&rt->lock);
  290. platform_set_drvdata(pdev, rt);
  291. ret = devm_led_classdev_flash_register_ext(dev, fled, &init_data);
  292. if (ret) {
  293. fwnode_handle_put(child);
  294. mutex_destroy(&rt->lock);
  295. dev_err(dev, "can't register LED %s\n", led->name);
  296. return ret;
  297. }
  298. rt8515_init_v4l2_flash_config(rt, &v4l2_sd_cfg);
  299. /* Create a V4L2 Flash device if V4L2 flash is enabled */
  300. rt->v4l2_flash = v4l2_flash_init(dev, child, fled, NULL, &v4l2_sd_cfg);
  301. if (IS_ERR(rt->v4l2_flash)) {
  302. ret = PTR_ERR(rt->v4l2_flash);
  303. dev_err(dev, "failed to register V4L2 flash device (%d)\n",
  304. ret);
  305. /*
  306. * Continue without the V4L2 flash
  307. * (we still have the classdev)
  308. */
  309. }
  310. fwnode_handle_put(child);
  311. return 0;
  312. }
  313. static int rt8515_remove(struct platform_device *pdev)
  314. {
  315. struct rt8515 *rt = platform_get_drvdata(pdev);
  316. rt8515_v4l2_flash_release(rt);
  317. del_timer_sync(&rt->powerdown_timer);
  318. mutex_destroy(&rt->lock);
  319. return 0;
  320. }
  321. static const struct of_device_id rt8515_match[] = {
  322. { .compatible = "richtek,rt8515", },
  323. { /* sentinel */ }
  324. };
  325. MODULE_DEVICE_TABLE(of, rt8515_match);
  326. static struct platform_driver rt8515_driver = {
  327. .driver = {
  328. .name = "rt8515",
  329. .of_match_table = rt8515_match,
  330. },
  331. .probe = rt8515_probe,
  332. .remove = rt8515_remove,
  333. };
  334. module_platform_driver(rt8515_driver);
  335. MODULE_AUTHOR("Linus Walleij <[email protected]>");
  336. MODULE_DESCRIPTION("Richtek RT8515 LED driver");
  337. MODULE_LICENSE("GPL");