leds-max77693.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * LED Flash class driver for the flash cell of max77693 mfd.
  3. *
  4. * Copyright (C) 2015, Samsung Electronics Co., Ltd.
  5. *
  6. * Authors: Jacek Anaszewski <j.anaszewski@samsung.com>
  7. * Andrzej Hajda <a.hajda@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. */
  13. #include <linux/led-class-flash.h>
  14. #include <linux/mfd/max77693.h>
  15. #include <linux/mfd/max77693-common.h>
  16. #include <linux/mfd/max77693-private.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. #include <media/v4l2-flash-led-class.h>
  24. #define MODE_OFF 0
  25. #define MODE_FLASH(a) (1 << (a))
  26. #define MODE_TORCH(a) (1 << (2 + (a)))
  27. #define MODE_FLASH_EXTERNAL(a) (1 << (4 + (a)))
  28. #define MODE_FLASH_MASK (MODE_FLASH(FLED1) | MODE_FLASH(FLED2) | \
  29. MODE_FLASH_EXTERNAL(FLED1) | \
  30. MODE_FLASH_EXTERNAL(FLED2))
  31. #define MODE_TORCH_MASK (MODE_TORCH(FLED1) | MODE_TORCH(FLED2))
  32. #define FLED1_IOUT (1 << 0)
  33. #define FLED2_IOUT (1 << 1)
  34. enum max77693_fled {
  35. FLED1,
  36. FLED2,
  37. };
  38. enum max77693_led_mode {
  39. FLASH,
  40. TORCH,
  41. };
  42. struct max77693_led_config_data {
  43. const char *label[2];
  44. u32 iout_torch_max[2];
  45. u32 iout_flash_max[2];
  46. u32 flash_timeout_max[2];
  47. u32 num_leds;
  48. u32 boost_mode;
  49. u32 boost_vout;
  50. u32 low_vsys;
  51. };
  52. struct max77693_sub_led {
  53. /* corresponding FLED output identifier */
  54. int fled_id;
  55. /* corresponding LED Flash class device */
  56. struct led_classdev_flash fled_cdev;
  57. /* assures led-triggers compatibility */
  58. struct work_struct work_brightness_set;
  59. /* V4L2 Flash device */
  60. struct v4l2_flash *v4l2_flash;
  61. /* brightness cache */
  62. unsigned int torch_brightness;
  63. /* flash timeout cache */
  64. unsigned int flash_timeout;
  65. /* flash faults that may have occurred */
  66. u32 flash_faults;
  67. };
  68. struct max77693_led_device {
  69. /* parent mfd regmap */
  70. struct regmap *regmap;
  71. /* platform device data */
  72. struct platform_device *pdev;
  73. /* secures access to the device */
  74. struct mutex lock;
  75. /* sub led data */
  76. struct max77693_sub_led sub_leds[2];
  77. /* maximum torch current values for FLED outputs */
  78. u32 iout_torch_max[2];
  79. /* maximum flash current values for FLED outputs */
  80. u32 iout_flash_max[2];
  81. /* current flash timeout cache */
  82. unsigned int current_flash_timeout;
  83. /* ITORCH register cache */
  84. u8 torch_iout_reg;
  85. /* mode of fled outputs */
  86. unsigned int mode_flags;
  87. /* recently strobed fled */
  88. int strobing_sub_led_id;
  89. /* bitmask of FLED outputs use state (bit 0. - FLED1, bit 1. - FLED2) */
  90. u8 fled_mask;
  91. /* FLED modes that can be set */
  92. u8 allowed_modes;
  93. /* arrangement of current outputs */
  94. bool iout_joint;
  95. };
  96. static u8 max77693_led_iout_to_reg(u32 ua)
  97. {
  98. if (ua < FLASH_IOUT_MIN)
  99. ua = FLASH_IOUT_MIN;
  100. return (ua - FLASH_IOUT_MIN) / FLASH_IOUT_STEP;
  101. }
  102. static u8 max77693_flash_timeout_to_reg(u32 us)
  103. {
  104. return (us - FLASH_TIMEOUT_MIN) / FLASH_TIMEOUT_STEP;
  105. }
  106. static inline struct max77693_sub_led *flcdev_to_sub_led(
  107. struct led_classdev_flash *fled_cdev)
  108. {
  109. return container_of(fled_cdev, struct max77693_sub_led, fled_cdev);
  110. }
  111. static inline struct max77693_led_device *sub_led_to_led(
  112. struct max77693_sub_led *sub_led)
  113. {
  114. return container_of(sub_led, struct max77693_led_device,
  115. sub_leds[sub_led->fled_id]);
  116. }
  117. static inline u8 max77693_led_vsys_to_reg(u32 mv)
  118. {
  119. return ((mv - MAX_FLASH1_VSYS_MIN) / MAX_FLASH1_VSYS_STEP) << 2;
  120. }
  121. static inline u8 max77693_led_vout_to_reg(u32 mv)
  122. {
  123. return (mv - FLASH_VOUT_MIN) / FLASH_VOUT_STEP + FLASH_VOUT_RMIN;
  124. }
  125. static inline bool max77693_fled_used(struct max77693_led_device *led,
  126. int fled_id)
  127. {
  128. u8 fled_bit = (fled_id == FLED1) ? FLED1_IOUT : FLED2_IOUT;
  129. return led->fled_mask & fled_bit;
  130. }
  131. static int max77693_set_mode_reg(struct max77693_led_device *led, u8 mode)
  132. {
  133. struct regmap *rmap = led->regmap;
  134. int ret, v = 0, i;
  135. for (i = FLED1; i <= FLED2; ++i) {
  136. if (mode & MODE_TORCH(i))
  137. v |= FLASH_EN_ON << TORCH_EN_SHIFT(i);
  138. if (mode & MODE_FLASH(i)) {
  139. v |= FLASH_EN_ON << FLASH_EN_SHIFT(i);
  140. } else if (mode & MODE_FLASH_EXTERNAL(i)) {
  141. v |= FLASH_EN_FLASH << FLASH_EN_SHIFT(i);
  142. /*
  143. * Enable hw triggering also for torch mode, as some
  144. * camera sensors use torch led to fathom ambient light
  145. * conditions before strobing the flash.
  146. */
  147. v |= FLASH_EN_TORCH << TORCH_EN_SHIFT(i);
  148. }
  149. }
  150. /* Reset the register only prior setting flash modes */
  151. if (mode & ~(MODE_TORCH(FLED1) | MODE_TORCH(FLED2))) {
  152. ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, 0);
  153. if (ret < 0)
  154. return ret;
  155. }
  156. return regmap_write(rmap, MAX77693_LED_REG_FLASH_EN, v);
  157. }
  158. static int max77693_add_mode(struct max77693_led_device *led, u8 mode)
  159. {
  160. u8 new_mode_flags;
  161. int i, ret;
  162. if (led->iout_joint)
  163. /* Span the mode on FLED2 for joint iouts case */
  164. mode |= (mode << 1);
  165. /*
  166. * FLASH_EXTERNAL mode activates FLASHEN and TORCHEN pins in the device.
  167. * Corresponding register bit fields interfere with SW triggered modes,
  168. * thus clear them to ensure proper device configuration.
  169. */
  170. for (i = FLED1; i <= FLED2; ++i)
  171. if (mode & MODE_FLASH_EXTERNAL(i))
  172. led->mode_flags &= (~MODE_TORCH(i) & ~MODE_FLASH(i));
  173. new_mode_flags = mode | led->mode_flags;
  174. new_mode_flags &= led->allowed_modes;
  175. if (new_mode_flags ^ led->mode_flags)
  176. led->mode_flags = new_mode_flags;
  177. else
  178. return 0;
  179. ret = max77693_set_mode_reg(led, led->mode_flags);
  180. if (ret < 0)
  181. return ret;
  182. /*
  183. * Clear flash mode flag after setting the mode to avoid spurious flash
  184. * strobing on each subsequent torch mode setting.
  185. */
  186. if (mode & MODE_FLASH_MASK)
  187. led->mode_flags &= ~mode;
  188. return ret;
  189. }
  190. static int max77693_clear_mode(struct max77693_led_device *led,
  191. u8 mode)
  192. {
  193. if (led->iout_joint)
  194. /* Clear mode also on FLED2 for joint iouts case */
  195. mode |= (mode << 1);
  196. led->mode_flags &= ~mode;
  197. return max77693_set_mode_reg(led, led->mode_flags);
  198. }
  199. static void max77693_add_allowed_modes(struct max77693_led_device *led,
  200. int fled_id, enum max77693_led_mode mode)
  201. {
  202. if (mode == FLASH)
  203. led->allowed_modes |= (MODE_FLASH(fled_id) |
  204. MODE_FLASH_EXTERNAL(fled_id));
  205. else
  206. led->allowed_modes |= MODE_TORCH(fled_id);
  207. }
  208. static void max77693_distribute_currents(struct max77693_led_device *led,
  209. int fled_id, enum max77693_led_mode mode,
  210. u32 micro_amp, u32 iout_max[2], u32 iout[2])
  211. {
  212. if (!led->iout_joint) {
  213. iout[fled_id] = micro_amp;
  214. max77693_add_allowed_modes(led, fled_id, mode);
  215. return;
  216. }
  217. iout[FLED1] = min(micro_amp, iout_max[FLED1]);
  218. iout[FLED2] = micro_amp - iout[FLED1];
  219. if (mode == FLASH)
  220. led->allowed_modes &= ~MODE_FLASH_MASK;
  221. else
  222. led->allowed_modes &= ~MODE_TORCH_MASK;
  223. max77693_add_allowed_modes(led, FLED1, mode);
  224. if (iout[FLED2])
  225. max77693_add_allowed_modes(led, FLED2, mode);
  226. }
  227. static int max77693_set_torch_current(struct max77693_led_device *led,
  228. int fled_id, u32 micro_amp)
  229. {
  230. struct regmap *rmap = led->regmap;
  231. u8 iout1_reg = 0, iout2_reg = 0;
  232. u32 iout[2];
  233. max77693_distribute_currents(led, fled_id, TORCH, micro_amp,
  234. led->iout_torch_max, iout);
  235. if (fled_id == FLED1 || led->iout_joint) {
  236. iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
  237. led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT2_SHIFT);
  238. }
  239. if (fled_id == FLED2 || led->iout_joint) {
  240. iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
  241. led->torch_iout_reg &= TORCH_IOUT_MASK(TORCH_IOUT1_SHIFT);
  242. }
  243. led->torch_iout_reg |= ((iout1_reg << TORCH_IOUT1_SHIFT) |
  244. (iout2_reg << TORCH_IOUT2_SHIFT));
  245. return regmap_write(rmap, MAX77693_LED_REG_ITORCH,
  246. led->torch_iout_reg);
  247. }
  248. static int max77693_set_flash_current(struct max77693_led_device *led,
  249. int fled_id,
  250. u32 micro_amp)
  251. {
  252. struct regmap *rmap = led->regmap;
  253. u8 iout1_reg, iout2_reg;
  254. u32 iout[2];
  255. int ret = -EINVAL;
  256. max77693_distribute_currents(led, fled_id, FLASH, micro_amp,
  257. led->iout_flash_max, iout);
  258. if (fled_id == FLED1 || led->iout_joint) {
  259. iout1_reg = max77693_led_iout_to_reg(iout[FLED1]);
  260. ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH1,
  261. iout1_reg);
  262. if (ret < 0)
  263. return ret;
  264. }
  265. if (fled_id == FLED2 || led->iout_joint) {
  266. iout2_reg = max77693_led_iout_to_reg(iout[FLED2]);
  267. ret = regmap_write(rmap, MAX77693_LED_REG_IFLASH2,
  268. iout2_reg);
  269. }
  270. return ret;
  271. }
  272. static int max77693_set_timeout(struct max77693_led_device *led, u32 microsec)
  273. {
  274. struct regmap *rmap = led->regmap;
  275. u8 v;
  276. int ret;
  277. v = max77693_flash_timeout_to_reg(microsec) | FLASH_TMR_LEVEL;
  278. ret = regmap_write(rmap, MAX77693_LED_REG_FLASH_TIMER, v);
  279. if (ret < 0)
  280. return ret;
  281. led->current_flash_timeout = microsec;
  282. return 0;
  283. }
  284. static int max77693_get_strobe_status(struct max77693_led_device *led,
  285. bool *state)
  286. {
  287. struct regmap *rmap = led->regmap;
  288. unsigned int v;
  289. int ret;
  290. ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_STATUS, &v);
  291. if (ret < 0)
  292. return ret;
  293. *state = v & FLASH_STATUS_FLASH_ON;
  294. return ret;
  295. }
  296. static int max77693_get_flash_faults(struct max77693_sub_led *sub_led)
  297. {
  298. struct max77693_led_device *led = sub_led_to_led(sub_led);
  299. struct regmap *rmap = led->regmap;
  300. unsigned int v;
  301. u8 fault_open_mask, fault_short_mask;
  302. int ret;
  303. sub_led->flash_faults = 0;
  304. if (led->iout_joint) {
  305. fault_open_mask = FLASH_INT_FLED1_OPEN | FLASH_INT_FLED2_OPEN;
  306. fault_short_mask = FLASH_INT_FLED1_SHORT |
  307. FLASH_INT_FLED2_SHORT;
  308. } else {
  309. fault_open_mask = (sub_led->fled_id == FLED1) ?
  310. FLASH_INT_FLED1_OPEN :
  311. FLASH_INT_FLED2_OPEN;
  312. fault_short_mask = (sub_led->fled_id == FLED1) ?
  313. FLASH_INT_FLED1_SHORT :
  314. FLASH_INT_FLED2_SHORT;
  315. }
  316. ret = regmap_read(rmap, MAX77693_LED_REG_FLASH_INT, &v);
  317. if (ret < 0)
  318. return ret;
  319. if (v & fault_open_mask)
  320. sub_led->flash_faults |= LED_FAULT_OVER_VOLTAGE;
  321. if (v & fault_short_mask)
  322. sub_led->flash_faults |= LED_FAULT_SHORT_CIRCUIT;
  323. if (v & FLASH_INT_OVER_CURRENT)
  324. sub_led->flash_faults |= LED_FAULT_OVER_CURRENT;
  325. return 0;
  326. }
  327. static int max77693_setup(struct max77693_led_device *led,
  328. struct max77693_led_config_data *led_cfg)
  329. {
  330. struct regmap *rmap = led->regmap;
  331. int i, first_led, last_led, ret;
  332. u32 max_flash_curr[2];
  333. u8 v;
  334. /*
  335. * Initialize only flash current. Torch current doesn't
  336. * require initialization as ITORCH register is written with
  337. * new value each time brightness_set op is called.
  338. */
  339. if (led->iout_joint) {
  340. first_led = FLED1;
  341. last_led = FLED1;
  342. max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1] +
  343. led_cfg->iout_flash_max[FLED2];
  344. } else {
  345. first_led = max77693_fled_used(led, FLED1) ? FLED1 : FLED2;
  346. last_led = max77693_fled_used(led, FLED2) ? FLED2 : FLED1;
  347. max_flash_curr[FLED1] = led_cfg->iout_flash_max[FLED1];
  348. max_flash_curr[FLED2] = led_cfg->iout_flash_max[FLED2];
  349. }
  350. for (i = first_led; i <= last_led; ++i) {
  351. ret = max77693_set_flash_current(led, i,
  352. max_flash_curr[i]);
  353. if (ret < 0)
  354. return ret;
  355. }
  356. v = TORCH_TMR_NO_TIMER | MAX77693_LED_TRIG_TYPE_LEVEL;
  357. ret = regmap_write(rmap, MAX77693_LED_REG_ITORCHTIMER, v);
  358. if (ret < 0)
  359. return ret;
  360. if (led_cfg->low_vsys > 0)
  361. v = max77693_led_vsys_to_reg(led_cfg->low_vsys) |
  362. MAX_FLASH1_MAX_FL_EN;
  363. else
  364. v = 0;
  365. ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH1, v);
  366. if (ret < 0)
  367. return ret;
  368. ret = regmap_write(rmap, MAX77693_LED_REG_MAX_FLASH2, 0);
  369. if (ret < 0)
  370. return ret;
  371. if (led_cfg->boost_mode == MAX77693_LED_BOOST_FIXED)
  372. v = FLASH_BOOST_FIXED;
  373. else
  374. v = led_cfg->boost_mode | led_cfg->boost_mode << 1;
  375. if (max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
  376. v |= FLASH_BOOST_LEDNUM_2;
  377. ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_CNTL, v);
  378. if (ret < 0)
  379. return ret;
  380. v = max77693_led_vout_to_reg(led_cfg->boost_vout);
  381. ret = regmap_write(rmap, MAX77693_LED_REG_VOUT_FLASH1, v);
  382. if (ret < 0)
  383. return ret;
  384. return max77693_set_mode_reg(led, MODE_OFF);
  385. }
  386. static int __max77693_led_brightness_set(struct max77693_led_device *led,
  387. int fled_id, enum led_brightness value)
  388. {
  389. int ret;
  390. mutex_lock(&led->lock);
  391. if (value == 0) {
  392. ret = max77693_clear_mode(led, MODE_TORCH(fled_id));
  393. if (ret < 0)
  394. dev_dbg(&led->pdev->dev,
  395. "Failed to clear torch mode (%d)\n",
  396. ret);
  397. goto unlock;
  398. }
  399. ret = max77693_set_torch_current(led, fled_id, value * TORCH_IOUT_STEP);
  400. if (ret < 0) {
  401. dev_dbg(&led->pdev->dev,
  402. "Failed to set torch current (%d)\n",
  403. ret);
  404. goto unlock;
  405. }
  406. ret = max77693_add_mode(led, MODE_TORCH(fled_id));
  407. if (ret < 0)
  408. dev_dbg(&led->pdev->dev,
  409. "Failed to set torch mode (%d)\n",
  410. ret);
  411. unlock:
  412. mutex_unlock(&led->lock);
  413. return ret;
  414. }
  415. static void max77693_led_brightness_set_work(
  416. struct work_struct *work)
  417. {
  418. struct max77693_sub_led *sub_led =
  419. container_of(work, struct max77693_sub_led,
  420. work_brightness_set);
  421. struct max77693_led_device *led = sub_led_to_led(sub_led);
  422. __max77693_led_brightness_set(led, sub_led->fled_id,
  423. sub_led->torch_brightness);
  424. }
  425. /* LED subsystem callbacks */
  426. static int max77693_led_brightness_set_sync(
  427. struct led_classdev *led_cdev,
  428. enum led_brightness value)
  429. {
  430. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  431. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  432. struct max77693_led_device *led = sub_led_to_led(sub_led);
  433. return __max77693_led_brightness_set(led, sub_led->fled_id, value);
  434. }
  435. static void max77693_led_brightness_set(
  436. struct led_classdev *led_cdev,
  437. enum led_brightness value)
  438. {
  439. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  440. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  441. sub_led->torch_brightness = value;
  442. schedule_work(&sub_led->work_brightness_set);
  443. }
  444. static int max77693_led_flash_brightness_set(
  445. struct led_classdev_flash *fled_cdev,
  446. u32 brightness)
  447. {
  448. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  449. struct max77693_led_device *led = sub_led_to_led(sub_led);
  450. int ret;
  451. mutex_lock(&led->lock);
  452. ret = max77693_set_flash_current(led, sub_led->fled_id, brightness);
  453. mutex_unlock(&led->lock);
  454. return ret;
  455. }
  456. static int max77693_led_flash_strobe_set(
  457. struct led_classdev_flash *fled_cdev,
  458. bool state)
  459. {
  460. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  461. struct max77693_led_device *led = sub_led_to_led(sub_led);
  462. int fled_id = sub_led->fled_id;
  463. int ret;
  464. mutex_lock(&led->lock);
  465. if (!state) {
  466. ret = max77693_clear_mode(led, MODE_FLASH(fled_id));
  467. goto unlock;
  468. }
  469. if (sub_led->flash_timeout != led->current_flash_timeout) {
  470. ret = max77693_set_timeout(led, sub_led->flash_timeout);
  471. if (ret < 0)
  472. goto unlock;
  473. }
  474. led->strobing_sub_led_id = fled_id;
  475. ret = max77693_add_mode(led, MODE_FLASH(fled_id));
  476. if (ret < 0)
  477. goto unlock;
  478. ret = max77693_get_flash_faults(sub_led);
  479. unlock:
  480. mutex_unlock(&led->lock);
  481. return ret;
  482. }
  483. static int max77693_led_flash_fault_get(
  484. struct led_classdev_flash *fled_cdev,
  485. u32 *fault)
  486. {
  487. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  488. *fault = sub_led->flash_faults;
  489. return 0;
  490. }
  491. static int max77693_led_flash_strobe_get(
  492. struct led_classdev_flash *fled_cdev,
  493. bool *state)
  494. {
  495. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  496. struct max77693_led_device *led = sub_led_to_led(sub_led);
  497. int ret;
  498. if (!state)
  499. return -EINVAL;
  500. mutex_lock(&led->lock);
  501. ret = max77693_get_strobe_status(led, state);
  502. *state = !!(*state && (led->strobing_sub_led_id == sub_led->fled_id));
  503. mutex_unlock(&led->lock);
  504. return ret;
  505. }
  506. static int max77693_led_flash_timeout_set(
  507. struct led_classdev_flash *fled_cdev,
  508. u32 timeout)
  509. {
  510. struct max77693_sub_led *sub_led = flcdev_to_sub_led(fled_cdev);
  511. struct max77693_led_device *led = sub_led_to_led(sub_led);
  512. mutex_lock(&led->lock);
  513. sub_led->flash_timeout = timeout;
  514. mutex_unlock(&led->lock);
  515. return 0;
  516. }
  517. static int max77693_led_parse_dt(struct max77693_led_device *led,
  518. struct max77693_led_config_data *cfg,
  519. struct device_node **sub_nodes)
  520. {
  521. struct device *dev = &led->pdev->dev;
  522. struct max77693_sub_led *sub_leds = led->sub_leds;
  523. struct device_node *node = dev->of_node, *child_node;
  524. struct property *prop;
  525. u32 led_sources[2];
  526. int i, ret, fled_id;
  527. of_property_read_u32(node, "maxim,boost-mode", &cfg->boost_mode);
  528. of_property_read_u32(node, "maxim,boost-mvout", &cfg->boost_vout);
  529. of_property_read_u32(node, "maxim,mvsys-min", &cfg->low_vsys);
  530. for_each_available_child_of_node(node, child_node) {
  531. prop = of_find_property(child_node, "led-sources", NULL);
  532. if (prop) {
  533. const __be32 *srcs = NULL;
  534. for (i = 0; i < ARRAY_SIZE(led_sources); ++i) {
  535. srcs = of_prop_next_u32(prop, srcs,
  536. &led_sources[i]);
  537. if (!srcs)
  538. break;
  539. }
  540. } else {
  541. dev_err(dev,
  542. "led-sources DT property missing\n");
  543. of_node_put(child_node);
  544. return -EINVAL;
  545. }
  546. if (i == 2) {
  547. fled_id = FLED1;
  548. led->fled_mask = FLED1_IOUT | FLED2_IOUT;
  549. } else if (led_sources[0] == FLED1) {
  550. fled_id = FLED1;
  551. led->fled_mask |= FLED1_IOUT;
  552. } else if (led_sources[0] == FLED2) {
  553. fled_id = FLED2;
  554. led->fled_mask |= FLED2_IOUT;
  555. } else {
  556. dev_err(dev,
  557. "Wrong led-sources DT property value.\n");
  558. of_node_put(child_node);
  559. return -EINVAL;
  560. }
  561. if (sub_nodes[fled_id]) {
  562. dev_err(dev,
  563. "Conflicting \"led-sources\" DT properties\n");
  564. return -EINVAL;
  565. }
  566. sub_nodes[fled_id] = child_node;
  567. sub_leds[fled_id].fled_id = fled_id;
  568. cfg->label[fled_id] =
  569. of_get_property(child_node, "label", NULL) ? :
  570. child_node->name;
  571. ret = of_property_read_u32(child_node, "led-max-microamp",
  572. &cfg->iout_torch_max[fled_id]);
  573. if (ret < 0) {
  574. cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
  575. dev_warn(dev, "led-max-microamp DT property missing\n");
  576. }
  577. ret = of_property_read_u32(child_node, "flash-max-microamp",
  578. &cfg->iout_flash_max[fled_id]);
  579. if (ret < 0) {
  580. cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
  581. dev_warn(dev,
  582. "flash-max-microamp DT property missing\n");
  583. }
  584. ret = of_property_read_u32(child_node, "flash-max-timeout-us",
  585. &cfg->flash_timeout_max[fled_id]);
  586. if (ret < 0) {
  587. cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
  588. dev_warn(dev,
  589. "flash-max-timeout-us DT property missing\n");
  590. }
  591. if (++cfg->num_leds == 2 ||
  592. (max77693_fled_used(led, FLED1) &&
  593. max77693_fled_used(led, FLED2))) {
  594. of_node_put(child_node);
  595. break;
  596. }
  597. }
  598. if (cfg->num_leds == 0) {
  599. dev_err(dev, "No DT child node found for connected LED(s).\n");
  600. return -EINVAL;
  601. }
  602. return 0;
  603. }
  604. static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
  605. {
  606. *v = clamp_val(*v, min, max);
  607. if (step > 1)
  608. *v = (*v - min) / step * step + min;
  609. }
  610. static void max77693_align_iout_current(struct max77693_led_device *led,
  611. u32 *iout, u32 min, u32 max, u32 step)
  612. {
  613. int i;
  614. if (led->iout_joint) {
  615. if (iout[FLED1] > min) {
  616. iout[FLED1] /= 2;
  617. iout[FLED2] = iout[FLED1];
  618. } else {
  619. iout[FLED1] = min;
  620. iout[FLED2] = 0;
  621. return;
  622. }
  623. }
  624. for (i = FLED1; i <= FLED2; ++i)
  625. if (max77693_fled_used(led, i))
  626. clamp_align(&iout[i], min, max, step);
  627. else
  628. iout[i] = 0;
  629. }
  630. static void max77693_led_validate_configuration(struct max77693_led_device *led,
  631. struct max77693_led_config_data *cfg)
  632. {
  633. u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
  634. FLASH_IOUT_MAX_1LED;
  635. int i;
  636. if (cfg->num_leds == 1 &&
  637. max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
  638. led->iout_joint = true;
  639. cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
  640. MAX77693_LED_BOOST_FIXED);
  641. /* Boost must be enabled if both current outputs are used */
  642. if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
  643. cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
  644. max77693_align_iout_current(led, cfg->iout_torch_max,
  645. TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
  646. max77693_align_iout_current(led, cfg->iout_flash_max,
  647. FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
  648. for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
  649. clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
  650. FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
  651. clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
  652. FLASH_VOUT_STEP);
  653. if (cfg->low_vsys)
  654. clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
  655. MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
  656. }
  657. static int max77693_led_get_configuration(struct max77693_led_device *led,
  658. struct max77693_led_config_data *cfg,
  659. struct device_node **sub_nodes)
  660. {
  661. int ret;
  662. ret = max77693_led_parse_dt(led, cfg, sub_nodes);
  663. if (ret < 0)
  664. return ret;
  665. max77693_led_validate_configuration(led, cfg);
  666. memcpy(led->iout_torch_max, cfg->iout_torch_max,
  667. sizeof(led->iout_torch_max));
  668. memcpy(led->iout_flash_max, cfg->iout_flash_max,
  669. sizeof(led->iout_flash_max));
  670. return 0;
  671. }
  672. static const struct led_flash_ops flash_ops = {
  673. .flash_brightness_set = max77693_led_flash_brightness_set,
  674. .strobe_set = max77693_led_flash_strobe_set,
  675. .strobe_get = max77693_led_flash_strobe_get,
  676. .timeout_set = max77693_led_flash_timeout_set,
  677. .fault_get = max77693_led_flash_fault_get,
  678. };
  679. static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
  680. struct max77693_led_config_data *led_cfg)
  681. {
  682. struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
  683. struct max77693_led_device *led = sub_led_to_led(sub_led);
  684. int fled_id = sub_led->fled_id;
  685. struct led_flash_setting *setting;
  686. /* Init flash intensity setting */
  687. setting = &fled_cdev->brightness;
  688. setting->min = FLASH_IOUT_MIN;
  689. setting->max = led->iout_joint ?
  690. led_cfg->iout_flash_max[FLED1] +
  691. led_cfg->iout_flash_max[FLED2] :
  692. led_cfg->iout_flash_max[fled_id];
  693. setting->step = FLASH_IOUT_STEP;
  694. setting->val = setting->max;
  695. /* Init flash timeout setting */
  696. setting = &fled_cdev->timeout;
  697. setting->min = FLASH_TIMEOUT_MIN;
  698. setting->max = led_cfg->flash_timeout_max[fled_id];
  699. setting->step = FLASH_TIMEOUT_STEP;
  700. setting->val = setting->max;
  701. }
  702. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  703. static int max77693_led_external_strobe_set(
  704. struct v4l2_flash *v4l2_flash,
  705. bool enable)
  706. {
  707. struct max77693_sub_led *sub_led =
  708. flcdev_to_sub_led(v4l2_flash->fled_cdev);
  709. struct max77693_led_device *led = sub_led_to_led(sub_led);
  710. int fled_id = sub_led->fled_id;
  711. int ret;
  712. mutex_lock(&led->lock);
  713. if (enable)
  714. ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
  715. else
  716. ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
  717. mutex_unlock(&led->lock);
  718. return ret;
  719. }
  720. static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
  721. struct max77693_led_config_data *led_cfg,
  722. struct v4l2_flash_config *v4l2_sd_cfg)
  723. {
  724. struct max77693_led_device *led = sub_led_to_led(sub_led);
  725. struct device *dev = &led->pdev->dev;
  726. struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
  727. struct i2c_client *i2c = iodev->i2c;
  728. struct led_flash_setting *s;
  729. snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
  730. "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
  731. i2c_adapter_id(i2c->adapter), i2c->addr);
  732. s = &v4l2_sd_cfg->torch_intensity;
  733. s->min = TORCH_IOUT_MIN;
  734. s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
  735. s->step = TORCH_IOUT_STEP;
  736. s->val = s->max;
  737. /* Init flash faults config */
  738. v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
  739. LED_FAULT_SHORT_CIRCUIT |
  740. LED_FAULT_OVER_CURRENT;
  741. v4l2_sd_cfg->has_external_strobe = true;
  742. }
  743. static const struct v4l2_flash_ops v4l2_flash_ops = {
  744. .external_strobe_set = max77693_led_external_strobe_set,
  745. };
  746. #else
  747. static inline void max77693_init_v4l2_flash_config(
  748. struct max77693_sub_led *sub_led,
  749. struct max77693_led_config_data *led_cfg,
  750. struct v4l2_flash_config *v4l2_sd_cfg)
  751. {
  752. }
  753. static const struct v4l2_flash_ops v4l2_flash_ops;
  754. #endif
  755. static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
  756. struct max77693_led_config_data *led_cfg)
  757. {
  758. struct max77693_led_device *led = sub_led_to_led(sub_led);
  759. int fled_id = sub_led->fled_id;
  760. struct led_classdev_flash *fled_cdev;
  761. struct led_classdev *led_cdev;
  762. /* Initialize LED Flash class device */
  763. fled_cdev = &sub_led->fled_cdev;
  764. fled_cdev->ops = &flash_ops;
  765. led_cdev = &fled_cdev->led_cdev;
  766. led_cdev->name = led_cfg->label[fled_id];
  767. led_cdev->brightness_set = max77693_led_brightness_set;
  768. led_cdev->brightness_set_sync = max77693_led_brightness_set_sync;
  769. led_cdev->max_brightness = (led->iout_joint ?
  770. led_cfg->iout_torch_max[FLED1] +
  771. led_cfg->iout_torch_max[FLED2] :
  772. led_cfg->iout_torch_max[fled_id]) /
  773. TORCH_IOUT_STEP;
  774. led_cdev->flags |= LED_DEV_CAP_FLASH;
  775. INIT_WORK(&sub_led->work_brightness_set,
  776. max77693_led_brightness_set_work);
  777. max77693_init_flash_settings(sub_led, led_cfg);
  778. /* Init flash timeout cache */
  779. sub_led->flash_timeout = fled_cdev->timeout.val;
  780. }
  781. static int max77693_register_led(struct max77693_sub_led *sub_led,
  782. struct max77693_led_config_data *led_cfg,
  783. struct device_node *sub_node)
  784. {
  785. struct max77693_led_device *led = sub_led_to_led(sub_led);
  786. struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
  787. struct device *dev = &led->pdev->dev;
  788. struct v4l2_flash_config v4l2_sd_cfg = {};
  789. int ret;
  790. /* Register in the LED subsystem */
  791. ret = led_classdev_flash_register(dev, fled_cdev);
  792. if (ret < 0)
  793. return ret;
  794. max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
  795. /* Register in the V4L2 subsystem. */
  796. sub_led->v4l2_flash = v4l2_flash_init(dev, sub_node, fled_cdev, NULL,
  797. &v4l2_flash_ops, &v4l2_sd_cfg);
  798. if (IS_ERR(sub_led->v4l2_flash)) {
  799. ret = PTR_ERR(sub_led->v4l2_flash);
  800. goto err_v4l2_flash_init;
  801. }
  802. return 0;
  803. err_v4l2_flash_init:
  804. led_classdev_flash_unregister(fled_cdev);
  805. return ret;
  806. }
  807. static int max77693_led_probe(struct platform_device *pdev)
  808. {
  809. struct device *dev = &pdev->dev;
  810. struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
  811. struct max77693_led_device *led;
  812. struct max77693_sub_led *sub_leds;
  813. struct device_node *sub_nodes[2] = {};
  814. struct max77693_led_config_data led_cfg = {};
  815. int init_fled_cdev[2], i, ret;
  816. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  817. if (!led)
  818. return -ENOMEM;
  819. led->pdev = pdev;
  820. led->regmap = iodev->regmap;
  821. led->allowed_modes = MODE_FLASH_MASK;
  822. sub_leds = led->sub_leds;
  823. platform_set_drvdata(pdev, led);
  824. ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
  825. if (ret < 0)
  826. return ret;
  827. ret = max77693_setup(led, &led_cfg);
  828. if (ret < 0)
  829. return ret;
  830. mutex_init(&led->lock);
  831. init_fled_cdev[FLED1] =
  832. led->iout_joint || max77693_fled_used(led, FLED1);
  833. init_fled_cdev[FLED2] =
  834. !led->iout_joint && max77693_fled_used(led, FLED2);
  835. for (i = FLED1; i <= FLED2; ++i) {
  836. if (!init_fled_cdev[i])
  837. continue;
  838. /* Initialize LED Flash class device */
  839. max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
  840. /*
  841. * Register LED Flash class device and corresponding
  842. * V4L2 Flash device.
  843. */
  844. ret = max77693_register_led(&sub_leds[i], &led_cfg,
  845. sub_nodes[i]);
  846. if (ret < 0) {
  847. /*
  848. * At this moment FLED1 might have been already
  849. * registered and it needs to be released.
  850. */
  851. if (i == FLED2)
  852. goto err_register_led2;
  853. else
  854. goto err_register_led1;
  855. }
  856. }
  857. return 0;
  858. err_register_led2:
  859. /* It is possible than only FLED2 was to be registered */
  860. if (!init_fled_cdev[FLED1])
  861. goto err_register_led1;
  862. v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
  863. led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
  864. err_register_led1:
  865. mutex_destroy(&led->lock);
  866. return ret;
  867. }
  868. static int max77693_led_remove(struct platform_device *pdev)
  869. {
  870. struct max77693_led_device *led = platform_get_drvdata(pdev);
  871. struct max77693_sub_led *sub_leds = led->sub_leds;
  872. if (led->iout_joint || max77693_fled_used(led, FLED1)) {
  873. v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
  874. led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
  875. cancel_work_sync(&sub_leds[FLED1].work_brightness_set);
  876. }
  877. if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
  878. v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
  879. led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
  880. cancel_work_sync(&sub_leds[FLED2].work_brightness_set);
  881. }
  882. mutex_destroy(&led->lock);
  883. return 0;
  884. }
  885. static const struct of_device_id max77693_led_dt_match[] = {
  886. { .compatible = "maxim,max77693-led" },
  887. {},
  888. };
  889. MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
  890. static struct platform_driver max77693_led_driver = {
  891. .probe = max77693_led_probe,
  892. .remove = max77693_led_remove,
  893. .driver = {
  894. .name = "max77693-led",
  895. .of_match_table = max77693_led_dt_match,
  896. },
  897. };
  898. module_platform_driver(max77693_led_driver);
  899. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  900. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  901. MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
  902. MODULE_LICENSE("GPL v2");