leds-aat1290.c 14 KB

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