leds-max77693.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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->torch_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, sub_node, fled_cdev, NULL,
  767. &v4l2_flash_ops, &v4l2_sd_cfg);
  768. if (IS_ERR(sub_led->v4l2_flash)) {
  769. ret = PTR_ERR(sub_led->v4l2_flash);
  770. goto err_v4l2_flash_init;
  771. }
  772. return 0;
  773. err_v4l2_flash_init:
  774. led_classdev_flash_unregister(fled_cdev);
  775. return ret;
  776. }
  777. static int max77693_led_probe(struct platform_device *pdev)
  778. {
  779. struct device *dev = &pdev->dev;
  780. struct max77693_dev *iodev = dev_get_drvdata(dev->parent);
  781. struct max77693_led_device *led;
  782. struct max77693_sub_led *sub_leds;
  783. struct device_node *sub_nodes[2] = {};
  784. struct max77693_led_config_data led_cfg = {};
  785. int init_fled_cdev[2], i, ret;
  786. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  787. if (!led)
  788. return -ENOMEM;
  789. led->pdev = pdev;
  790. led->regmap = iodev->regmap;
  791. led->allowed_modes = MODE_FLASH_MASK;
  792. sub_leds = led->sub_leds;
  793. platform_set_drvdata(pdev, led);
  794. ret = max77693_led_get_configuration(led, &led_cfg, sub_nodes);
  795. if (ret < 0)
  796. return ret;
  797. ret = max77693_setup(led, &led_cfg);
  798. if (ret < 0)
  799. return ret;
  800. mutex_init(&led->lock);
  801. init_fled_cdev[FLED1] =
  802. led->iout_joint || max77693_fled_used(led, FLED1);
  803. init_fled_cdev[FLED2] =
  804. !led->iout_joint && max77693_fled_used(led, FLED2);
  805. for (i = FLED1; i <= FLED2; ++i) {
  806. if (!init_fled_cdev[i])
  807. continue;
  808. /* Initialize LED Flash class device */
  809. max77693_init_fled_cdev(&sub_leds[i], &led_cfg);
  810. /*
  811. * Register LED Flash class device and corresponding
  812. * V4L2 Flash device.
  813. */
  814. ret = max77693_register_led(&sub_leds[i], &led_cfg,
  815. sub_nodes[i]);
  816. if (ret < 0) {
  817. /*
  818. * At this moment FLED1 might have been already
  819. * registered and it needs to be released.
  820. */
  821. if (i == FLED2)
  822. goto err_register_led2;
  823. else
  824. goto err_register_led1;
  825. }
  826. }
  827. return 0;
  828. err_register_led2:
  829. /* It is possible than only FLED2 was to be registered */
  830. if (!init_fled_cdev[FLED1])
  831. goto err_register_led1;
  832. v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
  833. led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
  834. err_register_led1:
  835. mutex_destroy(&led->lock);
  836. return ret;
  837. }
  838. static int max77693_led_remove(struct platform_device *pdev)
  839. {
  840. struct max77693_led_device *led = platform_get_drvdata(pdev);
  841. struct max77693_sub_led *sub_leds = led->sub_leds;
  842. if (led->iout_joint || max77693_fled_used(led, FLED1)) {
  843. v4l2_flash_release(sub_leds[FLED1].v4l2_flash);
  844. led_classdev_flash_unregister(&sub_leds[FLED1].fled_cdev);
  845. }
  846. if (!led->iout_joint && max77693_fled_used(led, FLED2)) {
  847. v4l2_flash_release(sub_leds[FLED2].v4l2_flash);
  848. led_classdev_flash_unregister(&sub_leds[FLED2].fled_cdev);
  849. }
  850. mutex_destroy(&led->lock);
  851. return 0;
  852. }
  853. static const struct of_device_id max77693_led_dt_match[] = {
  854. { .compatible = "maxim,max77693-led" },
  855. {},
  856. };
  857. MODULE_DEVICE_TABLE(of, max77693_led_dt_match);
  858. static struct platform_driver max77693_led_driver = {
  859. .probe = max77693_led_probe,
  860. .remove = max77693_led_remove,
  861. .driver = {
  862. .name = "max77693-led",
  863. .of_match_table = max77693_led_dt_match,
  864. },
  865. };
  866. module_platform_driver(max77693_led_driver);
  867. MODULE_AUTHOR("Jacek Anaszewski <j.anaszewski@samsung.com>");
  868. MODULE_AUTHOR("Andrzej Hajda <a.hajda@samsung.com>");
  869. MODULE_DESCRIPTION("Maxim MAX77693 led flash driver");
  870. MODULE_LICENSE("GPL v2");