leds-ktd2692.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * LED driver : leds-ktd2692.c
  3. *
  4. * Copyright (C) 2015 Samsung Electronics
  5. * Ingi Kim <ingi2.kim@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/led-class-flash.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regulator/consumer.h>
  20. /* Value related the movie mode */
  21. #define KTD2692_MOVIE_MODE_CURRENT_LEVELS 16
  22. #define KTD2692_MM_TO_FL_RATIO(x) ((x) / 3)
  23. #define KTD2962_MM_MIN_CURR_THRESHOLD_SCALE 8
  24. /* Value related the flash mode */
  25. #define KTD2692_FLASH_MODE_TIMEOUT_LEVELS 8
  26. #define KTD2692_FLASH_MODE_TIMEOUT_DISABLE 0
  27. #define KTD2692_FLASH_MODE_CURR_PERCENT(x) (((x) * 16) / 100)
  28. /* Macro for getting offset of flash timeout */
  29. #define GET_TIMEOUT_OFFSET(timeout, step) ((timeout) / (step))
  30. /* Base register address */
  31. #define KTD2692_REG_LVP_BASE 0x00
  32. #define KTD2692_REG_FLASH_TIMEOUT_BASE 0x20
  33. #define KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE 0x40
  34. #define KTD2692_REG_MOVIE_CURRENT_BASE 0x60
  35. #define KTD2692_REG_FLASH_CURRENT_BASE 0x80
  36. #define KTD2692_REG_MODE_BASE 0xA0
  37. /* Set bit coding time for expresswire interface */
  38. #define KTD2692_TIME_RESET_US 700
  39. #define KTD2692_TIME_DATA_START_TIME_US 10
  40. #define KTD2692_TIME_HIGH_END_OF_DATA_US 350
  41. #define KTD2692_TIME_LOW_END_OF_DATA_US 10
  42. #define KTD2692_TIME_SHORT_BITSET_US 4
  43. #define KTD2692_TIME_LONG_BITSET_US 12
  44. /* KTD2692 default length of name */
  45. #define KTD2692_NAME_LENGTH 20
  46. enum ktd2692_bitset {
  47. KTD2692_LOW = 0,
  48. KTD2692_HIGH,
  49. };
  50. /* Movie / Flash Mode Control */
  51. enum ktd2692_led_mode {
  52. KTD2692_MODE_DISABLE = 0, /* default */
  53. KTD2692_MODE_MOVIE,
  54. KTD2692_MODE_FLASH,
  55. };
  56. struct ktd2692_led_config_data {
  57. /* maximum LED current in movie mode */
  58. u32 movie_max_microamp;
  59. /* maximum LED current in flash mode */
  60. u32 flash_max_microamp;
  61. /* maximum flash timeout */
  62. u32 flash_max_timeout;
  63. /* max LED brightness level */
  64. enum led_brightness max_brightness;
  65. };
  66. struct ktd2692_context {
  67. /* Related LED Flash class device */
  68. struct led_classdev_flash fled_cdev;
  69. /* secures access to the device */
  70. struct mutex lock;
  71. struct regulator *regulator;
  72. struct gpio_desc *aux_gpio;
  73. struct gpio_desc *ctrl_gpio;
  74. enum ktd2692_led_mode mode;
  75. enum led_brightness torch_brightness;
  76. };
  77. static struct ktd2692_context *fled_cdev_to_led(
  78. struct led_classdev_flash *fled_cdev)
  79. {
  80. return container_of(fled_cdev, struct ktd2692_context, fled_cdev);
  81. }
  82. static void ktd2692_expresswire_start(struct ktd2692_context *led)
  83. {
  84. gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
  85. udelay(KTD2692_TIME_DATA_START_TIME_US);
  86. }
  87. static void ktd2692_expresswire_reset(struct ktd2692_context *led)
  88. {
  89. gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
  90. udelay(KTD2692_TIME_RESET_US);
  91. }
  92. static void ktd2692_expresswire_end(struct ktd2692_context *led)
  93. {
  94. gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
  95. udelay(KTD2692_TIME_LOW_END_OF_DATA_US);
  96. gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
  97. udelay(KTD2692_TIME_HIGH_END_OF_DATA_US);
  98. }
  99. static void ktd2692_expresswire_set_bit(struct ktd2692_context *led, bool bit)
  100. {
  101. /*
  102. * The Low Bit(0) and High Bit(1) is based on a time detection
  103. * algorithm between time low and time high
  104. * Time_(L_LB) : Low time of the Low Bit(0)
  105. * Time_(H_LB) : High time of the LOW Bit(0)
  106. * Time_(L_HB) : Low time of the High Bit(1)
  107. * Time_(H_HB) : High time of the High Bit(1)
  108. *
  109. * It can be simplified to:
  110. * Low Bit(0) : 2 * Time_(H_LB) < Time_(L_LB)
  111. * High Bit(1) : 2 * Time_(L_HB) < Time_(H_HB)
  112. * HIGH ___ ____ _.. _________ ___
  113. * |_________| |_.. |____| |__|
  114. * LOW <L_LB> <H_LB> <L_HB> <H_HB>
  115. * [ Low Bit (0) ] [ High Bit(1) ]
  116. */
  117. if (bit) {
  118. gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
  119. udelay(KTD2692_TIME_SHORT_BITSET_US);
  120. gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
  121. udelay(KTD2692_TIME_LONG_BITSET_US);
  122. } else {
  123. gpiod_direction_output(led->ctrl_gpio, KTD2692_LOW);
  124. udelay(KTD2692_TIME_LONG_BITSET_US);
  125. gpiod_direction_output(led->ctrl_gpio, KTD2692_HIGH);
  126. udelay(KTD2692_TIME_SHORT_BITSET_US);
  127. }
  128. }
  129. static void ktd2692_expresswire_write(struct ktd2692_context *led, u8 value)
  130. {
  131. int i;
  132. ktd2692_expresswire_start(led);
  133. for (i = 7; i >= 0; i--)
  134. ktd2692_expresswire_set_bit(led, value & BIT(i));
  135. ktd2692_expresswire_end(led);
  136. }
  137. static int ktd2692_led_brightness_set(struct led_classdev *led_cdev,
  138. enum led_brightness brightness)
  139. {
  140. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  141. struct ktd2692_context *led = fled_cdev_to_led(fled_cdev);
  142. mutex_lock(&led->lock);
  143. if (brightness == LED_OFF) {
  144. led->mode = KTD2692_MODE_DISABLE;
  145. gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
  146. } else {
  147. ktd2692_expresswire_write(led, brightness |
  148. KTD2692_REG_MOVIE_CURRENT_BASE);
  149. led->mode = KTD2692_MODE_MOVIE;
  150. }
  151. ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE);
  152. mutex_unlock(&led->lock);
  153. return 0;
  154. }
  155. static int ktd2692_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
  156. bool state)
  157. {
  158. struct ktd2692_context *led = fled_cdev_to_led(fled_cdev);
  159. struct led_flash_setting *timeout = &fled_cdev->timeout;
  160. u32 flash_tm_reg;
  161. mutex_lock(&led->lock);
  162. if (state) {
  163. flash_tm_reg = GET_TIMEOUT_OFFSET(timeout->val, timeout->step);
  164. ktd2692_expresswire_write(led, flash_tm_reg
  165. | KTD2692_REG_FLASH_TIMEOUT_BASE);
  166. led->mode = KTD2692_MODE_FLASH;
  167. gpiod_direction_output(led->aux_gpio, KTD2692_HIGH);
  168. } else {
  169. led->mode = KTD2692_MODE_DISABLE;
  170. gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
  171. }
  172. ktd2692_expresswire_write(led, led->mode | KTD2692_REG_MODE_BASE);
  173. fled_cdev->led_cdev.brightness = LED_OFF;
  174. led->mode = KTD2692_MODE_DISABLE;
  175. mutex_unlock(&led->lock);
  176. return 0;
  177. }
  178. static int ktd2692_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
  179. u32 timeout)
  180. {
  181. return 0;
  182. }
  183. static void ktd2692_init_movie_current_max(struct ktd2692_led_config_data *cfg)
  184. {
  185. u32 offset, step;
  186. u32 movie_current_microamp;
  187. offset = KTD2692_MOVIE_MODE_CURRENT_LEVELS;
  188. step = KTD2692_MM_TO_FL_RATIO(cfg->flash_max_microamp)
  189. / KTD2692_MOVIE_MODE_CURRENT_LEVELS;
  190. do {
  191. movie_current_microamp = step * offset;
  192. offset--;
  193. } while ((movie_current_microamp > cfg->movie_max_microamp) &&
  194. (offset > 0));
  195. cfg->max_brightness = offset;
  196. }
  197. static void ktd2692_init_flash_timeout(struct led_classdev_flash *fled_cdev,
  198. struct ktd2692_led_config_data *cfg)
  199. {
  200. struct led_flash_setting *setting;
  201. setting = &fled_cdev->timeout;
  202. setting->min = KTD2692_FLASH_MODE_TIMEOUT_DISABLE;
  203. setting->max = cfg->flash_max_timeout;
  204. setting->step = cfg->flash_max_timeout
  205. / (KTD2692_FLASH_MODE_TIMEOUT_LEVELS - 1);
  206. setting->val = cfg->flash_max_timeout;
  207. }
  208. static void ktd2692_setup(struct ktd2692_context *led)
  209. {
  210. led->mode = KTD2692_MODE_DISABLE;
  211. ktd2692_expresswire_reset(led);
  212. gpiod_direction_output(led->aux_gpio, KTD2692_LOW);
  213. ktd2692_expresswire_write(led, (KTD2962_MM_MIN_CURR_THRESHOLD_SCALE - 1)
  214. | KTD2692_REG_MM_MIN_CURR_THRESHOLD_BASE);
  215. ktd2692_expresswire_write(led, KTD2692_FLASH_MODE_CURR_PERCENT(45)
  216. | KTD2692_REG_FLASH_CURRENT_BASE);
  217. }
  218. static int ktd2692_parse_dt(struct ktd2692_context *led, struct device *dev,
  219. struct ktd2692_led_config_data *cfg)
  220. {
  221. struct device_node *np = dev->of_node;
  222. struct device_node *child_node;
  223. int ret;
  224. if (!dev->of_node)
  225. return -ENXIO;
  226. led->ctrl_gpio = devm_gpiod_get(dev, "ctrl", GPIOD_ASIS);
  227. ret = PTR_ERR_OR_ZERO(led->ctrl_gpio);
  228. if (ret) {
  229. dev_err(dev, "cannot get ctrl-gpios %d\n", ret);
  230. return ret;
  231. }
  232. led->aux_gpio = devm_gpiod_get(dev, "aux", GPIOD_ASIS);
  233. ret = PTR_ERR_OR_ZERO(led->aux_gpio);
  234. if (ret) {
  235. dev_err(dev, "cannot get aux-gpios %d\n", ret);
  236. return ret;
  237. }
  238. led->regulator = devm_regulator_get(dev, "vin");
  239. if (IS_ERR(led->regulator))
  240. led->regulator = NULL;
  241. if (led->regulator) {
  242. ret = regulator_enable(led->regulator);
  243. if (ret)
  244. dev_err(dev, "Failed to enable supply: %d\n", ret);
  245. }
  246. child_node = of_get_next_available_child(np, NULL);
  247. if (!child_node) {
  248. dev_err(dev, "No DT child node found for connected LED.\n");
  249. return -EINVAL;
  250. }
  251. led->fled_cdev.led_cdev.name =
  252. of_get_property(child_node, "label", NULL) ? : child_node->name;
  253. ret = of_property_read_u32(child_node, "led-max-microamp",
  254. &cfg->movie_max_microamp);
  255. if (ret) {
  256. dev_err(dev, "failed to parse led-max-microamp\n");
  257. goto err_parse_dt;
  258. }
  259. ret = of_property_read_u32(child_node, "flash-max-microamp",
  260. &cfg->flash_max_microamp);
  261. if (ret) {
  262. dev_err(dev, "failed to parse flash-max-microamp\n");
  263. goto err_parse_dt;
  264. }
  265. ret = of_property_read_u32(child_node, "flash-max-timeout-us",
  266. &cfg->flash_max_timeout);
  267. if (ret) {
  268. dev_err(dev, "failed to parse flash-max-timeout-us\n");
  269. goto err_parse_dt;
  270. }
  271. err_parse_dt:
  272. of_node_put(child_node);
  273. return ret;
  274. }
  275. static const struct led_flash_ops flash_ops = {
  276. .strobe_set = ktd2692_led_flash_strobe_set,
  277. .timeout_set = ktd2692_led_flash_timeout_set,
  278. };
  279. static int ktd2692_probe(struct platform_device *pdev)
  280. {
  281. struct ktd2692_context *led;
  282. struct led_classdev *led_cdev;
  283. struct led_classdev_flash *fled_cdev;
  284. struct ktd2692_led_config_data led_cfg;
  285. int ret;
  286. led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
  287. if (!led)
  288. return -ENOMEM;
  289. fled_cdev = &led->fled_cdev;
  290. led_cdev = &fled_cdev->led_cdev;
  291. ret = ktd2692_parse_dt(led, &pdev->dev, &led_cfg);
  292. if (ret)
  293. return ret;
  294. ktd2692_init_flash_timeout(fled_cdev, &led_cfg);
  295. ktd2692_init_movie_current_max(&led_cfg);
  296. fled_cdev->ops = &flash_ops;
  297. led_cdev->max_brightness = led_cfg.max_brightness;
  298. led_cdev->brightness_set_blocking = ktd2692_led_brightness_set;
  299. led_cdev->flags |= LED_CORE_SUSPENDRESUME | LED_DEV_CAP_FLASH;
  300. mutex_init(&led->lock);
  301. platform_set_drvdata(pdev, led);
  302. ret = led_classdev_flash_register(&pdev->dev, fled_cdev);
  303. if (ret) {
  304. dev_err(&pdev->dev, "can't register LED %s\n", led_cdev->name);
  305. mutex_destroy(&led->lock);
  306. return ret;
  307. }
  308. ktd2692_setup(led);
  309. return 0;
  310. }
  311. static int ktd2692_remove(struct platform_device *pdev)
  312. {
  313. struct ktd2692_context *led = platform_get_drvdata(pdev);
  314. int ret;
  315. led_classdev_flash_unregister(&led->fled_cdev);
  316. if (led->regulator) {
  317. ret = regulator_disable(led->regulator);
  318. if (ret)
  319. dev_err(&pdev->dev,
  320. "Failed to disable supply: %d\n", ret);
  321. }
  322. mutex_destroy(&led->lock);
  323. return 0;
  324. }
  325. static const struct of_device_id ktd2692_match[] = {
  326. { .compatible = "kinetic,ktd2692", },
  327. { /* sentinel */ },
  328. };
  329. MODULE_DEVICE_TABLE(of, ktd2692_match);
  330. static struct platform_driver ktd2692_driver = {
  331. .driver = {
  332. .name = "ktd2692",
  333. .of_match_table = ktd2692_match,
  334. },
  335. .probe = ktd2692_probe,
  336. .remove = ktd2692_remove,
  337. };
  338. module_platform_driver(ktd2692_driver);
  339. MODULE_AUTHOR("Ingi Kim <ingi2.kim@samsung.com>");
  340. MODULE_DESCRIPTION("Kinetic KTD2692 LED driver");
  341. MODULE_LICENSE("GPL v2");