leds-max77693.c 27 KB

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