leds-aat1290.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * LED Flash class driver for the AAT1290
  3. * 1.5A Step-Up Current Regulator for Flash LEDs
  4. *
  5. * Copyright (C) 2015, Samsung Electronics Co., Ltd.
  6. * Author: Jacek Anaszewski <j.anaszewski@samsung.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/led-class-flash.h>
  15. #include <linux/leds.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/pinctrl/consumer.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. #include <media/v4l2-flash-led-class.h>
  24. #define AAT1290_MOVIE_MODE_CURRENT_ADDR 17
  25. #define AAT1290_MAX_MM_CURR_PERCENT_0 16
  26. #define AAT1290_MAX_MM_CURR_PERCENT_100 1
  27. #define AAT1290_FLASH_SAFETY_TIMER_ADDR 18
  28. #define AAT1290_MOVIE_MODE_CONFIG_ADDR 19
  29. #define AAT1290_MOVIE_MODE_OFF 1
  30. #define AAT1290_MOVIE_MODE_ON 3
  31. #define AAT1290_MM_CURRENT_RATIO_ADDR 20
  32. #define AAT1290_MM_TO_FL_1_92 1
  33. #define AAT1290_MM_TO_FL_RATIO 1000 / 1920
  34. #define AAT1290_MAX_MM_CURRENT(fl_max) (fl_max * AAT1290_MM_TO_FL_RATIO)
  35. #define AAT1290_LATCH_TIME_MIN_US 500
  36. #define AAT1290_LATCH_TIME_MAX_US 1000
  37. #define AAT1290_EN_SET_TICK_TIME_US 1
  38. #define AAT1290_FLEN_OFF_DELAY_TIME_US 10
  39. #define AAT1290_FLASH_TM_NUM_LEVELS 16
  40. #define AAT1290_MM_CURRENT_SCALE_SIZE 15
  41. struct aat1290_led_config_data {
  42. /* maximum LED current in movie mode */
  43. u32 max_mm_current;
  44. /* maximum LED current in flash mode */
  45. u32 max_flash_current;
  46. /* maximum flash timeout */
  47. u32 max_flash_tm;
  48. /* external strobe capability */
  49. bool has_external_strobe;
  50. /* max LED brightness level */
  51. enum led_brightness max_brightness;
  52. };
  53. struct aat1290_led {
  54. /* platform device data */
  55. struct platform_device *pdev;
  56. /* secures access to the device */
  57. struct mutex lock;
  58. /* corresponding LED Flash class device */
  59. struct led_classdev_flash fled_cdev;
  60. /* V4L2 Flash device */
  61. struct v4l2_flash *v4l2_flash;
  62. /* FLEN pin */
  63. struct gpio_desc *gpio_fl_en;
  64. /* EN|SET pin */
  65. struct gpio_desc *gpio_en_set;
  66. /* movie mode current scale */
  67. int *mm_current_scale;
  68. /* device mode */
  69. bool movie_mode;
  70. /* brightness cache */
  71. unsigned int torch_brightness;
  72. /* assures led-triggers compatibility */
  73. struct work_struct work_brightness_set;
  74. };
  75. static struct aat1290_led *fled_cdev_to_led(
  76. struct led_classdev_flash *fled_cdev)
  77. {
  78. return container_of(fled_cdev, struct aat1290_led, fled_cdev);
  79. }
  80. static void aat1290_as2cwire_write(struct aat1290_led *led, int addr, int value)
  81. {
  82. int i;
  83. gpiod_direction_output(led->gpio_fl_en, 0);
  84. gpiod_direction_output(led->gpio_en_set, 0);
  85. udelay(AAT1290_FLEN_OFF_DELAY_TIME_US);
  86. /* write address */
  87. for (i = 0; i < addr; ++i) {
  88. udelay(AAT1290_EN_SET_TICK_TIME_US);
  89. gpiod_direction_output(led->gpio_en_set, 0);
  90. udelay(AAT1290_EN_SET_TICK_TIME_US);
  91. gpiod_direction_output(led->gpio_en_set, 1);
  92. }
  93. usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
  94. /* write data */
  95. for (i = 0; i < value; ++i) {
  96. udelay(AAT1290_EN_SET_TICK_TIME_US);
  97. gpiod_direction_output(led->gpio_en_set, 0);
  98. udelay(AAT1290_EN_SET_TICK_TIME_US);
  99. gpiod_direction_output(led->gpio_en_set, 1);
  100. }
  101. usleep_range(AAT1290_LATCH_TIME_MIN_US, AAT1290_LATCH_TIME_MAX_US);
  102. }
  103. static void aat1290_set_flash_safety_timer(struct aat1290_led *led,
  104. unsigned int micro_sec)
  105. {
  106. struct led_classdev_flash *fled_cdev = &led->fled_cdev;
  107. struct led_flash_setting *flash_tm = &fled_cdev->timeout;
  108. int flash_tm_reg = AAT1290_FLASH_TM_NUM_LEVELS -
  109. (micro_sec / flash_tm->step) + 1;
  110. aat1290_as2cwire_write(led, AAT1290_FLASH_SAFETY_TIMER_ADDR,
  111. flash_tm_reg);
  112. }
  113. static void aat1290_brightness_set(struct aat1290_led *led,
  114. enum led_brightness brightness)
  115. {
  116. mutex_lock(&led->lock);
  117. if (brightness == 0) {
  118. gpiod_direction_output(led->gpio_fl_en, 0);
  119. gpiod_direction_output(led->gpio_en_set, 0);
  120. led->movie_mode = false;
  121. } else {
  122. if (!led->movie_mode) {
  123. aat1290_as2cwire_write(led,
  124. AAT1290_MM_CURRENT_RATIO_ADDR,
  125. AAT1290_MM_TO_FL_1_92);
  126. led->movie_mode = true;
  127. }
  128. aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CURRENT_ADDR,
  129. AAT1290_MAX_MM_CURR_PERCENT_0 - brightness);
  130. aat1290_as2cwire_write(led, AAT1290_MOVIE_MODE_CONFIG_ADDR,
  131. AAT1290_MOVIE_MODE_ON);
  132. }
  133. mutex_unlock(&led->lock);
  134. }
  135. /* LED subsystem callbacks */
  136. static void aat1290_brightness_set_work(struct work_struct *work)
  137. {
  138. struct aat1290_led *led =
  139. container_of(work, struct aat1290_led, work_brightness_set);
  140. aat1290_brightness_set(led, led->torch_brightness);
  141. }
  142. static void aat1290_led_brightness_set(struct led_classdev *led_cdev,
  143. enum led_brightness brightness)
  144. {
  145. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  146. struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
  147. led->torch_brightness = brightness;
  148. schedule_work(&led->work_brightness_set);
  149. }
  150. static int aat1290_led_brightness_set_sync(struct led_classdev *led_cdev,
  151. enum led_brightness brightness)
  152. {
  153. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  154. struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
  155. aat1290_brightness_set(led, brightness);
  156. return 0;
  157. }
  158. static int aat1290_led_flash_strobe_set(struct led_classdev_flash *fled_cdev,
  159. bool state)
  160. {
  161. struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
  162. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  163. struct led_flash_setting *timeout = &fled_cdev->timeout;
  164. mutex_lock(&led->lock);
  165. if (state) {
  166. aat1290_set_flash_safety_timer(led, timeout->val);
  167. gpiod_direction_output(led->gpio_fl_en, 1);
  168. } else {
  169. gpiod_direction_output(led->gpio_fl_en, 0);
  170. gpiod_direction_output(led->gpio_en_set, 0);
  171. }
  172. /*
  173. * To reenter movie mode after a flash event the part must be cycled
  174. * off and back on to reset the movie mode and reprogrammed via the
  175. * AS2Cwire. Therefore the brightness and movie_mode properties needs
  176. * to be updated here to reflect the actual state.
  177. */
  178. led_cdev->brightness = 0;
  179. led->movie_mode = false;
  180. mutex_unlock(&led->lock);
  181. return 0;
  182. }
  183. static int aat1290_led_flash_timeout_set(struct led_classdev_flash *fled_cdev,
  184. u32 timeout)
  185. {
  186. /*
  187. * Don't do anything - flash timeout is cached in the led-class-flash
  188. * core and will be applied in the strobe_set op, as writing the
  189. * safety timer register spuriously turns the torch mode on.
  190. */
  191. return 0;
  192. }
  193. static int aat1290_led_parse_dt(struct aat1290_led *led,
  194. struct aat1290_led_config_data *cfg,
  195. struct device_node **sub_node)
  196. {
  197. struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
  198. struct device *dev = &led->pdev->dev;
  199. struct device_node *child_node;
  200. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  201. struct pinctrl *pinctrl;
  202. #endif
  203. int ret = 0;
  204. led->gpio_fl_en = devm_gpiod_get(dev, "flen", GPIOD_ASIS);
  205. if (IS_ERR(led->gpio_fl_en)) {
  206. ret = PTR_ERR(led->gpio_fl_en);
  207. dev_err(dev, "Unable to claim gpio \"flen\".\n");
  208. return ret;
  209. }
  210. led->gpio_en_set = devm_gpiod_get(dev, "enset", GPIOD_ASIS);
  211. if (IS_ERR(led->gpio_en_set)) {
  212. ret = PTR_ERR(led->gpio_en_set);
  213. dev_err(dev, "Unable to claim gpio \"enset\".\n");
  214. return ret;
  215. }
  216. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  217. pinctrl = devm_pinctrl_get_select_default(&led->pdev->dev);
  218. if (IS_ERR(pinctrl)) {
  219. cfg->has_external_strobe = false;
  220. dev_info(dev,
  221. "No support for external strobe detected.\n");
  222. } else {
  223. cfg->has_external_strobe = true;
  224. }
  225. #endif
  226. child_node = of_get_next_available_child(dev->of_node, NULL);
  227. if (!child_node) {
  228. dev_err(dev, "No DT child node found for connected LED.\n");
  229. return -EINVAL;
  230. }
  231. led_cdev->name = of_get_property(child_node, "label", NULL) ? :
  232. child_node->name;
  233. ret = of_property_read_u32(child_node, "led-max-microamp",
  234. &cfg->max_mm_current);
  235. /*
  236. * led-max-microamp will default to 1/20 of flash-max-microamp
  237. * in case it is missing.
  238. */
  239. if (ret < 0)
  240. dev_warn(dev,
  241. "led-max-microamp DT property missing\n");
  242. ret = of_property_read_u32(child_node, "flash-max-microamp",
  243. &cfg->max_flash_current);
  244. if (ret < 0) {
  245. dev_err(dev,
  246. "flash-max-microamp DT property missing\n");
  247. return ret;
  248. }
  249. ret = of_property_read_u32(child_node, "flash-max-timeout-us",
  250. &cfg->max_flash_tm);
  251. if (ret < 0) {
  252. dev_err(dev,
  253. "flash-max-timeout-us DT property missing\n");
  254. return ret;
  255. }
  256. of_node_put(child_node);
  257. *sub_node = child_node;
  258. return ret;
  259. }
  260. static void aat1290_led_validate_mm_current(struct aat1290_led *led,
  261. struct aat1290_led_config_data *cfg)
  262. {
  263. int i, b = 0, e = AAT1290_MM_CURRENT_SCALE_SIZE;
  264. while (e - b > 1) {
  265. i = b + (e - b) / 2;
  266. if (cfg->max_mm_current < led->mm_current_scale[i])
  267. e = i;
  268. else
  269. b = i;
  270. }
  271. cfg->max_mm_current = led->mm_current_scale[b];
  272. cfg->max_brightness = b + 1;
  273. }
  274. static int init_mm_current_scale(struct aat1290_led *led,
  275. struct aat1290_led_config_data *cfg)
  276. {
  277. int max_mm_current_percent[] = { 20, 22, 25, 28, 32, 36, 40, 45, 50, 56,
  278. 63, 71, 79, 89, 100 };
  279. int i, max_mm_current =
  280. AAT1290_MAX_MM_CURRENT(cfg->max_flash_current);
  281. led->mm_current_scale = devm_kzalloc(&led->pdev->dev,
  282. sizeof(max_mm_current_percent),
  283. GFP_KERNEL);
  284. if (!led->mm_current_scale)
  285. return -ENOMEM;
  286. for (i = 0; i < AAT1290_MM_CURRENT_SCALE_SIZE; ++i)
  287. led->mm_current_scale[i] = max_mm_current *
  288. max_mm_current_percent[i] / 100;
  289. return 0;
  290. }
  291. static int aat1290_led_get_configuration(struct aat1290_led *led,
  292. struct aat1290_led_config_data *cfg,
  293. struct device_node **sub_node)
  294. {
  295. int ret;
  296. ret = aat1290_led_parse_dt(led, cfg, sub_node);
  297. if (ret < 0)
  298. return ret;
  299. /*
  300. * Init non-linear movie mode current scale basing
  301. * on the max flash current from led configuration.
  302. */
  303. ret = init_mm_current_scale(led, cfg);
  304. if (ret < 0)
  305. return ret;
  306. aat1290_led_validate_mm_current(led, cfg);
  307. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  308. #else
  309. devm_kfree(&led->pdev->dev, led->mm_current_scale);
  310. #endif
  311. return 0;
  312. }
  313. static void aat1290_init_flash_timeout(struct aat1290_led *led,
  314. struct aat1290_led_config_data *cfg)
  315. {
  316. struct led_classdev_flash *fled_cdev = &led->fled_cdev;
  317. struct led_flash_setting *setting;
  318. /* Init flash timeout setting */
  319. setting = &fled_cdev->timeout;
  320. setting->min = cfg->max_flash_tm / AAT1290_FLASH_TM_NUM_LEVELS;
  321. setting->max = cfg->max_flash_tm;
  322. setting->step = setting->min;
  323. setting->val = setting->max;
  324. }
  325. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  326. static enum led_brightness aat1290_intensity_to_brightness(
  327. struct v4l2_flash *v4l2_flash,
  328. s32 intensity)
  329. {
  330. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  331. struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
  332. int i;
  333. for (i = AAT1290_MM_CURRENT_SCALE_SIZE - 1; i >= 0; --i)
  334. if (intensity >= led->mm_current_scale[i])
  335. return i + 1;
  336. return 1;
  337. }
  338. static s32 aat1290_brightness_to_intensity(struct v4l2_flash *v4l2_flash,
  339. enum led_brightness brightness)
  340. {
  341. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  342. struct aat1290_led *led = fled_cdev_to_led(fled_cdev);
  343. return led->mm_current_scale[brightness - 1];
  344. }
  345. static int aat1290_led_external_strobe_set(struct v4l2_flash *v4l2_flash,
  346. bool enable)
  347. {
  348. struct aat1290_led *led = fled_cdev_to_led(v4l2_flash->fled_cdev);
  349. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  350. struct led_classdev *led_cdev = &fled_cdev->led_cdev;
  351. struct pinctrl *pinctrl;
  352. gpiod_direction_output(led->gpio_fl_en, 0);
  353. gpiod_direction_output(led->gpio_en_set, 0);
  354. led->movie_mode = false;
  355. led_cdev->brightness = 0;
  356. pinctrl = devm_pinctrl_get_select(&led->pdev->dev,
  357. enable ? "isp" : "host");
  358. if (IS_ERR(pinctrl)) {
  359. dev_warn(&led->pdev->dev, "Unable to switch strobe source.\n");
  360. return PTR_ERR(pinctrl);
  361. }
  362. return 0;
  363. }
  364. static void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
  365. struct aat1290_led_config_data *led_cfg,
  366. struct v4l2_flash_config *v4l2_sd_cfg)
  367. {
  368. struct led_classdev *led_cdev = &led->fled_cdev.led_cdev;
  369. struct led_flash_setting *s;
  370. strlcpy(v4l2_sd_cfg->dev_name, led_cdev->name,
  371. sizeof(v4l2_sd_cfg->dev_name));
  372. s = &v4l2_sd_cfg->torch_intensity;
  373. s->min = led->mm_current_scale[0];
  374. s->max = led_cfg->max_mm_current;
  375. s->step = 1;
  376. s->val = s->max;
  377. v4l2_sd_cfg->has_external_strobe = led_cfg->has_external_strobe;
  378. }
  379. static const struct v4l2_flash_ops v4l2_flash_ops = {
  380. .external_strobe_set = aat1290_led_external_strobe_set,
  381. .intensity_to_led_brightness = aat1290_intensity_to_brightness,
  382. .led_brightness_to_intensity = aat1290_brightness_to_intensity,
  383. };
  384. #else
  385. static inline void aat1290_init_v4l2_flash_config(struct aat1290_led *led,
  386. struct aat1290_led_config_data *led_cfg,
  387. struct v4l2_flash_config *v4l2_sd_cfg)
  388. {
  389. }
  390. static const struct v4l2_flash_ops v4l2_flash_ops;
  391. #endif
  392. static const struct led_flash_ops flash_ops = {
  393. .strobe_set = aat1290_led_flash_strobe_set,
  394. .timeout_set = aat1290_led_flash_timeout_set,
  395. };
  396. static int aat1290_led_probe(struct platform_device *pdev)
  397. {
  398. struct device *dev = &pdev->dev;
  399. struct device_node *sub_node = NULL;
  400. struct aat1290_led *led;
  401. struct led_classdev *led_cdev;
  402. struct led_classdev_flash *fled_cdev;
  403. struct aat1290_led_config_data led_cfg = {};
  404. struct v4l2_flash_config v4l2_sd_cfg = {};
  405. int ret;
  406. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  407. if (!led)
  408. return -ENOMEM;
  409. led->pdev = pdev;
  410. platform_set_drvdata(pdev, led);
  411. fled_cdev = &led->fled_cdev;
  412. fled_cdev->ops = &flash_ops;
  413. led_cdev = &fled_cdev->led_cdev;
  414. ret = aat1290_led_get_configuration(led, &led_cfg, &sub_node);
  415. if (ret < 0)
  416. return ret;
  417. mutex_init(&led->lock);
  418. /* Initialize LED Flash class device */
  419. led_cdev->brightness_set = aat1290_led_brightness_set;
  420. led_cdev->brightness_set_sync = aat1290_led_brightness_set_sync;
  421. led_cdev->max_brightness = led_cfg.max_brightness;
  422. led_cdev->flags |= LED_DEV_CAP_FLASH;
  423. INIT_WORK(&led->work_brightness_set, aat1290_brightness_set_work);
  424. aat1290_init_flash_timeout(led, &led_cfg);
  425. /* Register LED Flash class device */
  426. ret = led_classdev_flash_register(&pdev->dev, fled_cdev);
  427. if (ret < 0)
  428. goto err_flash_register;
  429. aat1290_init_v4l2_flash_config(led, &led_cfg, &v4l2_sd_cfg);
  430. /* Create V4L2 Flash subdev. */
  431. led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
  432. &v4l2_flash_ops, &v4l2_sd_cfg);
  433. if (IS_ERR(led->v4l2_flash)) {
  434. ret = PTR_ERR(led->v4l2_flash);
  435. goto error_v4l2_flash_init;
  436. }
  437. return 0;
  438. error_v4l2_flash_init:
  439. led_classdev_flash_unregister(fled_cdev);
  440. err_flash_register:
  441. mutex_destroy(&led->lock);
  442. return ret;
  443. }
  444. static int aat1290_led_remove(struct platform_device *pdev)
  445. {
  446. struct aat1290_led *led = platform_get_drvdata(pdev);
  447. v4l2_flash_release(led->v4l2_flash);
  448. led_classdev_flash_unregister(&led->fled_cdev);
  449. cancel_work_sync(&led->work_brightness_set);
  450. mutex_destroy(&led->lock);
  451. return 0;
  452. }
  453. static const struct of_device_id aat1290_led_dt_match[] = {
  454. { .compatible = "skyworks,aat1290" },
  455. {},
  456. };
  457. MODULE_DEVICE_TABLE(of, aat1290_led_dt_match);
  458. static struct platform_driver aat1290_led_driver = {
  459. .probe = aat1290_led_probe,
  460. .remove = aat1290_led_remove,
  461. .driver = {
  462. .name = "aat1290",
  463. .of_match_table = aat1290_led_dt_match,
  464. },
  465. };
  466. module_platform_driver(aat1290_led_driver);
  467. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  468. MODULE_DESCRIPTION("Skyworks Current Regulator for Flash LEDs");
  469. MODULE_LICENSE("GPL v2");