leds-max77693.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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), *child_node;
  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(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. of_node_put(child_node);
  513. return -EINVAL;
  514. }
  515. if (i == 2) {
  516. fled_id = FLED1;
  517. led->fled_mask = FLED1_IOUT | FLED2_IOUT;
  518. } else if (led_sources[0] == FLED1) {
  519. fled_id = FLED1;
  520. led->fled_mask |= FLED1_IOUT;
  521. } else if (led_sources[0] == FLED2) {
  522. fled_id = FLED2;
  523. led->fled_mask |= FLED2_IOUT;
  524. } else {
  525. dev_err(dev,
  526. "Wrong led-sources DT property value.\n");
  527. of_node_put(child_node);
  528. return -EINVAL;
  529. }
  530. if (sub_nodes[fled_id]) {
  531. dev_err(dev,
  532. "Conflicting \"led-sources\" DT properties\n");
  533. of_node_put(child_node);
  534. return -EINVAL;
  535. }
  536. sub_nodes[fled_id] = child_node;
  537. sub_leds[fled_id].fled_id = fled_id;
  538. cfg->label[fled_id] =
  539. of_get_property(child_node, "label", NULL) ? :
  540. child_node->name;
  541. ret = of_property_read_u32(child_node, "led-max-microamp",
  542. &cfg->iout_torch_max[fled_id]);
  543. if (ret < 0) {
  544. cfg->iout_torch_max[fled_id] = TORCH_IOUT_MIN;
  545. dev_warn(dev, "led-max-microamp DT property missing\n");
  546. }
  547. ret = of_property_read_u32(child_node, "flash-max-microamp",
  548. &cfg->iout_flash_max[fled_id]);
  549. if (ret < 0) {
  550. cfg->iout_flash_max[fled_id] = FLASH_IOUT_MIN;
  551. dev_warn(dev,
  552. "flash-max-microamp DT property missing\n");
  553. }
  554. ret = of_property_read_u32(child_node, "flash-max-timeout-us",
  555. &cfg->flash_timeout_max[fled_id]);
  556. if (ret < 0) {
  557. cfg->flash_timeout_max[fled_id] = FLASH_TIMEOUT_MIN;
  558. dev_warn(dev,
  559. "flash-max-timeout-us DT property missing\n");
  560. }
  561. if (++cfg->num_leds == 2 ||
  562. (max77693_fled_used(led, FLED1) &&
  563. max77693_fled_used(led, FLED2))) {
  564. of_node_put(child_node);
  565. break;
  566. }
  567. }
  568. if (cfg->num_leds == 0) {
  569. dev_err(dev, "No DT child node found for connected LED(s).\n");
  570. return -EINVAL;
  571. }
  572. return 0;
  573. }
  574. static void clamp_align(u32 *v, u32 min, u32 max, u32 step)
  575. {
  576. *v = clamp_val(*v, min, max);
  577. if (step > 1)
  578. *v = (*v - min) / step * step + min;
  579. }
  580. static void max77693_align_iout_current(struct max77693_led_device *led,
  581. u32 *iout, u32 min, u32 max, u32 step)
  582. {
  583. int i;
  584. if (led->iout_joint) {
  585. if (iout[FLED1] > min) {
  586. iout[FLED1] /= 2;
  587. iout[FLED2] = iout[FLED1];
  588. } else {
  589. iout[FLED1] = min;
  590. iout[FLED2] = 0;
  591. return;
  592. }
  593. }
  594. for (i = FLED1; i <= FLED2; ++i)
  595. if (max77693_fled_used(led, i))
  596. clamp_align(&iout[i], min, max, step);
  597. else
  598. iout[i] = 0;
  599. }
  600. static void max77693_led_validate_configuration(struct max77693_led_device *led,
  601. struct max77693_led_config_data *cfg)
  602. {
  603. u32 flash_iout_max = cfg->boost_mode ? FLASH_IOUT_MAX_2LEDS :
  604. FLASH_IOUT_MAX_1LED;
  605. int i;
  606. if (cfg->num_leds == 1 &&
  607. max77693_fled_used(led, FLED1) && max77693_fled_used(led, FLED2))
  608. led->iout_joint = true;
  609. cfg->boost_mode = clamp_val(cfg->boost_mode, MAX77693_LED_BOOST_NONE,
  610. MAX77693_LED_BOOST_FIXED);
  611. /* Boost must be enabled if both current outputs are used */
  612. if ((cfg->boost_mode == MAX77693_LED_BOOST_NONE) && led->iout_joint)
  613. cfg->boost_mode = MAX77693_LED_BOOST_FIXED;
  614. max77693_align_iout_current(led, cfg->iout_torch_max,
  615. TORCH_IOUT_MIN, TORCH_IOUT_MAX, TORCH_IOUT_STEP);
  616. max77693_align_iout_current(led, cfg->iout_flash_max,
  617. FLASH_IOUT_MIN, flash_iout_max, FLASH_IOUT_STEP);
  618. for (i = 0; i < ARRAY_SIZE(cfg->flash_timeout_max); ++i)
  619. clamp_align(&cfg->flash_timeout_max[i], FLASH_TIMEOUT_MIN,
  620. FLASH_TIMEOUT_MAX, FLASH_TIMEOUT_STEP);
  621. clamp_align(&cfg->boost_vout, FLASH_VOUT_MIN, FLASH_VOUT_MAX,
  622. FLASH_VOUT_STEP);
  623. if (cfg->low_vsys)
  624. clamp_align(&cfg->low_vsys, MAX_FLASH1_VSYS_MIN,
  625. MAX_FLASH1_VSYS_MAX, MAX_FLASH1_VSYS_STEP);
  626. }
  627. static int max77693_led_get_configuration(struct max77693_led_device *led,
  628. struct max77693_led_config_data *cfg,
  629. struct device_node **sub_nodes)
  630. {
  631. int ret;
  632. ret = max77693_led_parse_dt(led, cfg, sub_nodes);
  633. if (ret < 0)
  634. return ret;
  635. max77693_led_validate_configuration(led, cfg);
  636. memcpy(led->iout_torch_max, cfg->iout_torch_max,
  637. sizeof(led->iout_torch_max));
  638. memcpy(led->iout_flash_max, cfg->iout_flash_max,
  639. sizeof(led->iout_flash_max));
  640. return 0;
  641. }
  642. static const struct led_flash_ops flash_ops = {
  643. .flash_brightness_set = max77693_led_flash_brightness_set,
  644. .strobe_set = max77693_led_flash_strobe_set,
  645. .strobe_get = max77693_led_flash_strobe_get,
  646. .timeout_set = max77693_led_flash_timeout_set,
  647. .fault_get = max77693_led_flash_fault_get,
  648. };
  649. static void max77693_init_flash_settings(struct max77693_sub_led *sub_led,
  650. struct max77693_led_config_data *led_cfg)
  651. {
  652. struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
  653. struct max77693_led_device *led = sub_led_to_led(sub_led);
  654. int fled_id = sub_led->fled_id;
  655. struct led_flash_setting *setting;
  656. /* Init flash intensity setting */
  657. setting = &fled_cdev->brightness;
  658. setting->min = FLASH_IOUT_MIN;
  659. setting->max = led->iout_joint ?
  660. led_cfg->iout_flash_max[FLED1] +
  661. led_cfg->iout_flash_max[FLED2] :
  662. led_cfg->iout_flash_max[fled_id];
  663. setting->step = FLASH_IOUT_STEP;
  664. setting->val = setting->max;
  665. /* Init flash timeout setting */
  666. setting = &fled_cdev->timeout;
  667. setting->min = FLASH_TIMEOUT_MIN;
  668. setting->max = led_cfg->flash_timeout_max[fled_id];
  669. setting->step = FLASH_TIMEOUT_STEP;
  670. setting->val = setting->max;
  671. }
  672. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  673. static int max77693_led_external_strobe_set(
  674. struct v4l2_flash *v4l2_flash,
  675. bool enable)
  676. {
  677. struct max77693_sub_led *sub_led =
  678. flcdev_to_sub_led(v4l2_flash->fled_cdev);
  679. struct max77693_led_device *led = sub_led_to_led(sub_led);
  680. int fled_id = sub_led->fled_id;
  681. int ret;
  682. mutex_lock(&led->lock);
  683. if (enable)
  684. ret = max77693_add_mode(led, MODE_FLASH_EXTERNAL(fled_id));
  685. else
  686. ret = max77693_clear_mode(led, MODE_FLASH_EXTERNAL(fled_id));
  687. mutex_unlock(&led->lock);
  688. return ret;
  689. }
  690. static void max77693_init_v4l2_flash_config(struct max77693_sub_led *sub_led,
  691. struct max77693_led_config_data *led_cfg,
  692. struct v4l2_flash_config *v4l2_sd_cfg)
  693. {
  694. struct max77693_led_device *led = sub_led_to_led(sub_led);
  695. struct device *dev = &led->pdev->dev;
  696. struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
  697. struct i2c_client *i2c = iodev->i2c;
  698. struct led_flash_setting *s;
  699. snprintf(v4l2_sd_cfg->dev_name, sizeof(v4l2_sd_cfg->dev_name),
  700. "%s %d-%04x", sub_led->fled_cdev.led_cdev.name,
  701. i2c_adapter_id(i2c->adapter), i2c->addr);
  702. s = &v4l2_sd_cfg->intensity;
  703. s->min = TORCH_IOUT_MIN;
  704. s->max = sub_led->fled_cdev.led_cdev.max_brightness * TORCH_IOUT_STEP;
  705. s->step = TORCH_IOUT_STEP;
  706. s->val = s->max;
  707. /* Init flash faults config */
  708. v4l2_sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE |
  709. LED_FAULT_SHORT_CIRCUIT |
  710. LED_FAULT_OVER_CURRENT;
  711. v4l2_sd_cfg->has_external_strobe = true;
  712. }
  713. static const struct v4l2_flash_ops v4l2_flash_ops = {
  714. .external_strobe_set = max77693_led_external_strobe_set,
  715. };
  716. #else
  717. static inline void max77693_init_v4l2_flash_config(
  718. struct max77693_sub_led *sub_led,
  719. struct max77693_led_config_data *led_cfg,
  720. struct v4l2_flash_config *v4l2_sd_cfg)
  721. {
  722. }
  723. static const struct v4l2_flash_ops v4l2_flash_ops;
  724. #endif
  725. static void max77693_init_fled_cdev(struct max77693_sub_led *sub_led,
  726. struct max77693_led_config_data *led_cfg)
  727. {
  728. struct max77693_led_device *led = sub_led_to_led(sub_led);
  729. int fled_id = sub_led->fled_id;
  730. struct led_classdev_flash *fled_cdev;
  731. struct led_classdev *led_cdev;
  732. /* Initialize LED Flash class device */
  733. fled_cdev = &sub_led->fled_cdev;
  734. fled_cdev->ops = &flash_ops;
  735. led_cdev = &fled_cdev->led_cdev;
  736. led_cdev->name = led_cfg->label[fled_id];
  737. led_cdev->brightness_set_blocking = max77693_led_brightness_set;
  738. led_cdev->max_brightness = (led->iout_joint ?
  739. led_cfg->iout_torch_max[FLED1] +
  740. led_cfg->iout_torch_max[FLED2] :
  741. led_cfg->iout_torch_max[fled_id]) /
  742. TORCH_IOUT_STEP;
  743. led_cdev->flags |= LED_DEV_CAP_FLASH;
  744. max77693_init_flash_settings(sub_led, led_cfg);
  745. /* Init flash timeout cache */
  746. sub_led->flash_timeout = fled_cdev->timeout.val;
  747. }
  748. static int max77693_register_led(struct max77693_sub_led *sub_led,
  749. struct max77693_led_config_data *led_cfg,
  750. struct device_node *sub_node)
  751. {
  752. struct max77693_led_device *led = sub_led_to_led(sub_led);
  753. struct led_classdev_flash *fled_cdev = &sub_led->fled_cdev;
  754. struct device *dev = &led->pdev->dev;
  755. struct v4l2_flash_config v4l2_sd_cfg = {};
  756. int ret;
  757. /* Register in the LED subsystem */
  758. ret = led_classdev_flash_register(dev, fled_cdev);
  759. if (ret < 0)
  760. return ret;
  761. max77693_init_v4l2_flash_config(sub_led, led_cfg, &v4l2_sd_cfg);
  762. /* Register in the V4L2 subsystem. */
  763. sub_led->v4l2_flash = v4l2_flash_init(dev, of_fwnode_handle(sub_node),
  764. fled_cdev, &v4l2_flash_ops,
  765. &v4l2_sd_cfg);
  766. if (IS_ERR(sub_led->v4l2_flash)) {
  767. ret = PTR_ERR(sub_led->v4l2_flash);
  768. goto err_v4l2_flash_init;
  769. }
  770. return 0;
  771. err_v4l2_flash_init:
  772. led_classdev_flash_unregister(fled_cdev);
  773. return ret;
  774. }
  775. static int max77693_led_probe(struct platform_device *pdev)
  776. {
  777. struct device *dev = &pdev->dev;
  778. struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
  779. struct max77693_led_device *led;
  780. struct max77693_sub_led *sub_leds;
  781. struct device_node *sub_nodes[2] = {};
  782. struct max77693_led_config_data led_cfg = {};
  783. int init_fled_cdev[2], i, ret;
  784. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  785. if (!led)
  786. return -ENOMEM;
  787. led->pdev = pdev;
  788. led->regmap = iodev->regmap;
  789. led->allowed_modes = MODE_FLASH_MASK;
  790. sub_leds = led->sub_leds;
  791. platform_set_drvdata(pdev, led);
  792. ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
  793. if (ret < 0)
  794. return ret;
  795. ret = max77693_setup(led, &led_cfg);
  796. if (ret < 0)
  797. return ret;
  798. mutex_init(&led->lock);
  799. init_fled_cdev[FLED1] =
  800. led->iout_joint || max77693_fled_used(led, FLED1);
  801. init_fled_cdev[FLED2] =
  802. !led->iout_joint && max77693_fled_used(led, FLED2);
  803. for (i = FLED1; i <= FLED2; ++i) {
  804. if (!init_fled_cdev[i])
  805. continue;
  806. /* Initialize LED Flash class device */
  807. max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
  808. /*
  809. * Register LED Flash class device and corresponding
  810. * V4L2 Flash device.
  811. */
  812. ret = max77693_register_led(&sub_leds[i], &led_cfg,
  813. sub_nodes[i]);
  814. if (ret < 0) {
  815. /*
  816. * At this moment FLED1 might have been already
  817. * registered and it needs to be released.
  818. */
  819. if (i == FLED2)
  820. goto err_register_led2;
  821. else
  822. goto err_register_led1;
  823. }
  824. }
  825. return 0;
  826. err_register_led2:
  827. /* It is possible than only FLED2 was to be registered */
  828. if (!init_fled_cdev[FLED1])
  829. goto err_register_led1;
  830. v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
  831. led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
  832. err_register_led1:
  833. mutex_destroy(&led->lock);
  834. return ret;
  835. }
  836. static int 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. return 0;
  850. }
  851. static const struct of_device_id max77693_led_dt_match[] = {
  852. { .compatible = "maxim,max77693-led" },
  853. {},
  854. };
  855. MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
  856. static struct platform_driver max77693_led_driver = {
  857. .probe = max77693_led_probe,
  858. .remove = max77693_led_remove,
  859. .driver = {
  860. .name = "max77693-led",
  861. .of_match_table = max77693_led_dt_match,
  862. },
  863. };
  864. module_platform_driver(max77693_led_driver);
  865. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  866. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  867. MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
  868. MODULE_LICENSE("GPL v2");