leds-max77693.c 27 KB

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