leds-aat1290.c 14 KB

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