leds-qcom-flash.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022, 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bits.h>
  7. #include <linux/leds.h>
  8. #include <linux/led-class-flash.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/property.h>
  12. #include <linux/regmap.h>
  13. #include <media/v4l2-flash-led-class.h>
  14. /* registers definitions */
  15. #define FLASH_REVISION_REG 0x00
  16. #define FLASH_4CH_REVISION_V0P1 0x01
  17. #define FLASH_TYPE_REG 0x04
  18. #define FLASH_TYPE_VAL 0x18
  19. #define FLASH_SUBTYPE_REG 0x05
  20. #define FLASH_SUBTYPE_3CH_PM8150_VAL 0x04
  21. #define FLASH_SUBTYPE_3CH_PMI8998_VAL 0x03
  22. #define FLASH_SUBTYPE_4CH_VAL 0x07
  23. #define FLASH_STS_3CH_OTST1 BIT(0)
  24. #define FLASH_STS_3CH_OTST2 BIT(1)
  25. #define FLASH_STS_3CH_OTST3 BIT(2)
  26. #define FLASH_STS_3CH_BOB_THM_OVERLOAD BIT(3)
  27. #define FLASH_STS_3CH_VPH_DROOP BIT(4)
  28. #define FLASH_STS_3CH_BOB_ILIM_S1 BIT(5)
  29. #define FLASH_STS_3CH_BOB_ILIM_S2 BIT(6)
  30. #define FLASH_STS_3CH_BCL_IBAT BIT(7)
  31. #define FLASH_STS_4CH_VPH_LOW BIT(0)
  32. #define FLASH_STS_4CH_BCL_IBAT BIT(1)
  33. #define FLASH_STS_4CH_BOB_ILIM_S1 BIT(2)
  34. #define FLASH_STS_4CH_BOB_ILIM_S2 BIT(3)
  35. #define FLASH_STS_4CH_OTST2 BIT(4)
  36. #define FLASH_STS_4CH_OTST1 BIT(5)
  37. #define FLASH_STS_4CHG_BOB_THM_OVERLOAD BIT(6)
  38. #define FLASH_TIMER_EN_BIT BIT(7)
  39. #define FLASH_TIMER_VAL_MASK GENMASK(6, 0)
  40. #define FLASH_TIMER_STEP_MS 10
  41. #define FLASH_STROBE_HW_SW_SEL_BIT BIT(2)
  42. #define SW_STROBE_VAL 0
  43. #define HW_STROBE_VAL 1
  44. #define FLASH_HW_STROBE_TRIGGER_SEL_BIT BIT(1)
  45. #define STROBE_LEVEL_TRIGGER_VAL 0
  46. #define STROBE_EDGE_TRIGGER_VAL 1
  47. #define FLASH_STROBE_POLARITY_BIT BIT(0)
  48. #define STROBE_ACTIVE_HIGH_VAL 1
  49. #define FLASH_IRES_MASK_4CH BIT(0)
  50. #define FLASH_IRES_MASK_3CH GENMASK(1, 0)
  51. #define FLASH_IRES_12P5MA_VAL 0
  52. #define FLASH_IRES_5MA_VAL_4CH 1
  53. #define FLASH_IRES_5MA_VAL_3CH 3
  54. /* constants */
  55. #define FLASH_CURRENT_MAX_UA 1500000
  56. #define TORCH_CURRENT_MAX_UA 500000
  57. #define FLASH_TOTAL_CURRENT_MAX_UA 2000000
  58. #define FLASH_CURRENT_DEFAULT_UA 1000000
  59. #define TORCH_CURRENT_DEFAULT_UA 200000
  60. #define TORCH_IRES_UA 5000
  61. #define FLASH_IRES_UA 12500
  62. #define FLASH_TIMEOUT_MAX_US 1280000
  63. #define FLASH_TIMEOUT_STEP_US 10000
  64. #define UA_PER_MA 1000
  65. /* thermal threshold constants */
  66. #define OTST_3CH_MIN_VAL 3
  67. #define OTST1_4CH_MIN_VAL 0
  68. #define OTST1_4CH_V0P1_MIN_VAL 3
  69. #define OTST2_4CH_MIN_VAL 0
  70. #define OTST1_MAX_CURRENT_MA 1000
  71. #define OTST2_MAX_CURRENT_MA 500
  72. #define OTST3_MAX_CURRENT_MA 200
  73. enum hw_type {
  74. QCOM_MVFLASH_3CH,
  75. QCOM_MVFLASH_4CH,
  76. };
  77. enum led_mode {
  78. FLASH_MODE,
  79. TORCH_MODE,
  80. };
  81. enum led_strobe {
  82. SW_STROBE,
  83. HW_STROBE,
  84. };
  85. enum {
  86. REG_STATUS1,
  87. REG_STATUS2,
  88. REG_STATUS3,
  89. REG_CHAN_TIMER,
  90. REG_ITARGET,
  91. REG_MODULE_EN,
  92. REG_IRESOLUTION,
  93. REG_CHAN_STROBE,
  94. REG_CHAN_EN,
  95. REG_THERM_THRSH1,
  96. REG_THERM_THRSH2,
  97. REG_THERM_THRSH3,
  98. REG_TORCH_CLAMP,
  99. REG_MAX_COUNT,
  100. };
  101. static const struct reg_field mvflash_3ch_regs[REG_MAX_COUNT] = {
  102. [REG_STATUS1] = REG_FIELD(0x08, 0, 7),
  103. [REG_STATUS2] = REG_FIELD(0x09, 0, 7),
  104. [REG_STATUS3] = REG_FIELD(0x0a, 0, 7),
  105. [REG_CHAN_TIMER] = REG_FIELD_ID(0x40, 0, 7, 3, 1),
  106. [REG_ITARGET] = REG_FIELD_ID(0x43, 0, 6, 3, 1),
  107. [REG_MODULE_EN] = REG_FIELD(0x46, 7, 7),
  108. [REG_IRESOLUTION] = REG_FIELD(0x47, 0, 5),
  109. [REG_CHAN_STROBE] = REG_FIELD_ID(0x49, 0, 2, 3, 1),
  110. [REG_CHAN_EN] = REG_FIELD(0x4c, 0, 2),
  111. [REG_THERM_THRSH1] = REG_FIELD(0x56, 0, 2),
  112. [REG_THERM_THRSH2] = REG_FIELD(0x57, 0, 2),
  113. [REG_THERM_THRSH3] = REG_FIELD(0x58, 0, 2),
  114. [REG_TORCH_CLAMP] = REG_FIELD(0xec, 0, 6),
  115. };
  116. static const struct reg_field mvflash_4ch_regs[REG_MAX_COUNT] = {
  117. [REG_STATUS1] = REG_FIELD(0x06, 0, 7),
  118. [REG_STATUS2] = REG_FIELD(0x07, 0, 6),
  119. [REG_STATUS3] = REG_FIELD(0x09, 0, 7),
  120. [REG_CHAN_TIMER] = REG_FIELD_ID(0x3e, 0, 7, 4, 1),
  121. [REG_ITARGET] = REG_FIELD_ID(0x42, 0, 6, 4, 1),
  122. [REG_MODULE_EN] = REG_FIELD(0x46, 7, 7),
  123. [REG_IRESOLUTION] = REG_FIELD(0x49, 0, 3),
  124. [REG_CHAN_STROBE] = REG_FIELD_ID(0x4a, 0, 6, 4, 1),
  125. [REG_CHAN_EN] = REG_FIELD(0x4e, 0, 3),
  126. [REG_THERM_THRSH1] = REG_FIELD(0x7a, 0, 2),
  127. [REG_THERM_THRSH2] = REG_FIELD(0x78, 0, 2),
  128. [REG_TORCH_CLAMP] = REG_FIELD(0xed, 0, 6),
  129. };
  130. struct qcom_flash_data {
  131. struct v4l2_flash **v4l2_flash;
  132. struct regmap_field *r_fields[REG_MAX_COUNT];
  133. struct mutex lock;
  134. enum hw_type hw_type;
  135. u32 total_ma;
  136. u8 leds_count;
  137. u8 max_channels;
  138. u8 chan_en_bits;
  139. u8 revision;
  140. u8 torch_clamp;
  141. };
  142. struct qcom_flash_led {
  143. struct qcom_flash_data *flash_data;
  144. struct led_classdev_flash flash;
  145. u32 max_flash_current_ma;
  146. u32 max_torch_current_ma;
  147. u32 max_timeout_ms;
  148. u32 flash_current_ma;
  149. u32 flash_timeout_ms;
  150. u32 current_in_use_ma;
  151. u8 *chan_id;
  152. u8 chan_count;
  153. bool enabled;
  154. };
  155. static int set_flash_module_en(struct qcom_flash_led *led, bool en)
  156. {
  157. struct qcom_flash_data *flash_data = led->flash_data;
  158. u8 led_mask = 0, enable;
  159. int i, rc;
  160. for (i = 0; i < led->chan_count; i++)
  161. led_mask |= BIT(led->chan_id[i]);
  162. mutex_lock(&flash_data->lock);
  163. if (en)
  164. flash_data->chan_en_bits |= led_mask;
  165. else
  166. flash_data->chan_en_bits &= ~led_mask;
  167. enable = !!flash_data->chan_en_bits;
  168. rc = regmap_field_write(flash_data->r_fields[REG_MODULE_EN], enable);
  169. if (rc)
  170. dev_err(led->flash.led_cdev.dev, "write module_en failed, rc=%d\n", rc);
  171. mutex_unlock(&flash_data->lock);
  172. return rc;
  173. }
  174. static int update_allowed_flash_current(struct qcom_flash_led *led, u32 *current_ma, bool strobe)
  175. {
  176. struct qcom_flash_data *flash_data = led->flash_data;
  177. u32 therm_ma, avail_ma, thrsh[3], min_thrsh, sts;
  178. int rc = 0;
  179. mutex_lock(&flash_data->lock);
  180. /*
  181. * Put previously allocated current into allowed budget in either of these two cases:
  182. * 1) LED is disabled;
  183. * 2) LED is enabled repeatedly
  184. */
  185. if (!strobe || led->current_in_use_ma != 0) {
  186. if (flash_data->total_ma >= led->current_in_use_ma)
  187. flash_data->total_ma -= led->current_in_use_ma;
  188. else
  189. flash_data->total_ma = 0;
  190. led->current_in_use_ma = 0;
  191. if (!strobe)
  192. goto unlock;
  193. }
  194. /*
  195. * Cache the default thermal threshold settings, and set them to the lowest levels before
  196. * reading over-temp real time status. If over-temp has been triggered at the lowest
  197. * threshold, it's very likely that it would be triggered at a higher (default) threshold
  198. * when more flash current is requested. Prevent device from triggering over-temp condition
  199. * by limiting the flash current for the new request.
  200. */
  201. rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH1], &thrsh[0]);
  202. if (rc < 0)
  203. goto unlock;
  204. rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH2], &thrsh[1]);
  205. if (rc < 0)
  206. goto unlock;
  207. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  208. rc = regmap_field_read(flash_data->r_fields[REG_THERM_THRSH3], &thrsh[2]);
  209. if (rc < 0)
  210. goto unlock;
  211. }
  212. min_thrsh = OTST_3CH_MIN_VAL;
  213. if (flash_data->hw_type == QCOM_MVFLASH_4CH)
  214. min_thrsh = (flash_data->revision == FLASH_4CH_REVISION_V0P1) ?
  215. OTST1_4CH_V0P1_MIN_VAL : OTST1_4CH_MIN_VAL;
  216. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH1], min_thrsh);
  217. if (rc < 0)
  218. goto unlock;
  219. if (flash_data->hw_type == QCOM_MVFLASH_4CH)
  220. min_thrsh = OTST2_4CH_MIN_VAL;
  221. /*
  222. * The default thermal threshold settings have been updated hence
  223. * restore them if any fault happens starting from here.
  224. */
  225. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH2], min_thrsh);
  226. if (rc < 0)
  227. goto restore;
  228. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  229. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH3], min_thrsh);
  230. if (rc < 0)
  231. goto restore;
  232. }
  233. /* Read thermal level status to get corresponding derating flash current */
  234. rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &sts);
  235. if (rc)
  236. goto restore;
  237. therm_ma = FLASH_TOTAL_CURRENT_MAX_UA / 1000;
  238. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  239. if (sts & FLASH_STS_3CH_OTST3)
  240. therm_ma = OTST3_MAX_CURRENT_MA;
  241. else if (sts & FLASH_STS_3CH_OTST2)
  242. therm_ma = OTST2_MAX_CURRENT_MA;
  243. else if (sts & FLASH_STS_3CH_OTST1)
  244. therm_ma = OTST1_MAX_CURRENT_MA;
  245. } else {
  246. if (sts & FLASH_STS_4CH_OTST2)
  247. therm_ma = OTST2_MAX_CURRENT_MA;
  248. else if (sts & FLASH_STS_4CH_OTST1)
  249. therm_ma = OTST1_MAX_CURRENT_MA;
  250. }
  251. /* Calculate the allowed flash current for the request */
  252. if (therm_ma <= flash_data->total_ma)
  253. avail_ma = 0;
  254. else
  255. avail_ma = therm_ma - flash_data->total_ma;
  256. *current_ma = min_t(u32, *current_ma, avail_ma);
  257. led->current_in_use_ma = *current_ma;
  258. flash_data->total_ma += led->current_in_use_ma;
  259. dev_dbg(led->flash.led_cdev.dev, "allowed flash current: %dmA, total current: %dmA\n",
  260. led->current_in_use_ma, flash_data->total_ma);
  261. restore:
  262. /* Restore to default thermal threshold settings */
  263. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH1], thrsh[0]);
  264. if (rc < 0)
  265. goto unlock;
  266. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH2], thrsh[1]);
  267. if (rc < 0)
  268. goto unlock;
  269. if (flash_data->hw_type == QCOM_MVFLASH_3CH)
  270. rc = regmap_field_write(flash_data->r_fields[REG_THERM_THRSH3], thrsh[2]);
  271. unlock:
  272. mutex_unlock(&flash_data->lock);
  273. return rc;
  274. }
  275. static int set_flash_current(struct qcom_flash_led *led, u32 current_ma, enum led_mode mode)
  276. {
  277. struct qcom_flash_data *flash_data = led->flash_data;
  278. u32 itarg_ua, ires_ua;
  279. u8 shift, ires_mask = 0, ires_val = 0, chan_id;
  280. int i, rc;
  281. /*
  282. * Split the current across the channels and set the
  283. * IRESOLUTION and ITARGET registers accordingly.
  284. */
  285. itarg_ua = (current_ma * UA_PER_MA) / led->chan_count + 1;
  286. ires_ua = (mode == FLASH_MODE) ? FLASH_IRES_UA : TORCH_IRES_UA;
  287. for (i = 0; i < led->chan_count; i++) {
  288. u8 itarget = 0;
  289. if (itarg_ua > ires_ua)
  290. itarget = itarg_ua / ires_ua - 1;
  291. chan_id = led->chan_id[i];
  292. rc = regmap_fields_write(flash_data->r_fields[REG_ITARGET], chan_id, itarget);
  293. if (rc)
  294. return rc;
  295. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  296. shift = chan_id * 2;
  297. ires_mask |= FLASH_IRES_MASK_3CH << shift;
  298. ires_val |= ((mode == FLASH_MODE) ?
  299. (FLASH_IRES_12P5MA_VAL << shift) :
  300. (FLASH_IRES_5MA_VAL_3CH << shift));
  301. } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
  302. shift = chan_id;
  303. ires_mask |= FLASH_IRES_MASK_4CH << shift;
  304. ires_val |= ((mode == FLASH_MODE) ?
  305. (FLASH_IRES_12P5MA_VAL << shift) :
  306. (FLASH_IRES_5MA_VAL_4CH << shift));
  307. } else {
  308. dev_err(led->flash.led_cdev.dev,
  309. "HW type %d is not supported\n", flash_data->hw_type);
  310. return -EOPNOTSUPP;
  311. }
  312. }
  313. return regmap_field_update_bits(flash_data->r_fields[REG_IRESOLUTION], ires_mask, ires_val);
  314. }
  315. static int set_flash_timeout(struct qcom_flash_led *led, u32 timeout_ms)
  316. {
  317. struct qcom_flash_data *flash_data = led->flash_data;
  318. u8 timer, chan_id;
  319. int rc, i;
  320. /* set SAFETY_TIMER for all the channels connected to the same LED */
  321. timeout_ms = min_t(u32, timeout_ms, led->max_timeout_ms);
  322. for (i = 0; i < led->chan_count; i++) {
  323. chan_id = led->chan_id[i];
  324. timer = timeout_ms / FLASH_TIMER_STEP_MS;
  325. timer = clamp_t(u8, timer, 0, FLASH_TIMER_VAL_MASK);
  326. if (timeout_ms)
  327. timer |= FLASH_TIMER_EN_BIT;
  328. rc = regmap_fields_write(flash_data->r_fields[REG_CHAN_TIMER], chan_id, timer);
  329. if (rc)
  330. return rc;
  331. }
  332. return 0;
  333. }
  334. static int set_flash_strobe(struct qcom_flash_led *led, enum led_strobe strobe, bool state)
  335. {
  336. struct qcom_flash_data *flash_data = led->flash_data;
  337. u8 strobe_sel, chan_en, chan_id, chan_mask = 0;
  338. int rc, i;
  339. /* Set SW strobe config for all channels connected to the LED */
  340. for (i = 0; i < led->chan_count; i++) {
  341. chan_id = led->chan_id[i];
  342. if (strobe == SW_STROBE)
  343. strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, SW_STROBE_VAL);
  344. else
  345. strobe_sel = FIELD_PREP(FLASH_STROBE_HW_SW_SEL_BIT, HW_STROBE_VAL);
  346. strobe_sel |=
  347. FIELD_PREP(FLASH_HW_STROBE_TRIGGER_SEL_BIT, STROBE_LEVEL_TRIGGER_VAL) |
  348. FIELD_PREP(FLASH_STROBE_POLARITY_BIT, STROBE_ACTIVE_HIGH_VAL);
  349. rc = regmap_fields_write(
  350. flash_data->r_fields[REG_CHAN_STROBE], chan_id, strobe_sel);
  351. if (rc)
  352. return rc;
  353. chan_mask |= BIT(chan_id);
  354. }
  355. /* Enable/disable flash channels */
  356. chan_en = state ? chan_mask : 0;
  357. rc = regmap_field_update_bits(flash_data->r_fields[REG_CHAN_EN], chan_mask, chan_en);
  358. if (rc)
  359. return rc;
  360. led->enabled = state;
  361. return 0;
  362. }
  363. static inline struct qcom_flash_led *flcdev_to_qcom_fled(struct led_classdev_flash *flcdev)
  364. {
  365. return container_of(flcdev, struct qcom_flash_led, flash);
  366. }
  367. static int qcom_flash_brightness_set(struct led_classdev_flash *fled_cdev, u32 brightness)
  368. {
  369. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  370. led->flash_current_ma = min_t(u32, led->max_flash_current_ma, brightness / UA_PER_MA);
  371. return 0;
  372. }
  373. static int qcom_flash_timeout_set(struct led_classdev_flash *fled_cdev, u32 timeout)
  374. {
  375. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  376. led->flash_timeout_ms = timeout / USEC_PER_MSEC;
  377. return 0;
  378. }
  379. static int qcom_flash_strobe_set(struct led_classdev_flash *fled_cdev, bool state)
  380. {
  381. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  382. int rc;
  383. rc = set_flash_strobe(led, SW_STROBE, false);
  384. if (rc)
  385. return rc;
  386. rc = update_allowed_flash_current(led, &led->flash_current_ma, state);
  387. if (rc < 0)
  388. return rc;
  389. rc = set_flash_current(led, led->flash_current_ma, FLASH_MODE);
  390. if (rc)
  391. return rc;
  392. rc = set_flash_timeout(led, led->flash_timeout_ms);
  393. if (rc)
  394. return rc;
  395. rc = set_flash_module_en(led, state);
  396. if (rc)
  397. return rc;
  398. return set_flash_strobe(led, SW_STROBE, state);
  399. }
  400. static int qcom_flash_strobe_get(struct led_classdev_flash *fled_cdev, bool *state)
  401. {
  402. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  403. *state = led->enabled;
  404. return 0;
  405. }
  406. static int qcom_flash_fault_get(struct led_classdev_flash *fled_cdev, u32 *fault)
  407. {
  408. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  409. struct qcom_flash_data *flash_data = led->flash_data;
  410. u8 shift, chan_id, chan_mask = 0;
  411. u8 ot_mask = 0, oc_mask = 0, uv_mask = 0;
  412. u32 val, fault_sts = 0;
  413. int i, rc;
  414. rc = regmap_field_read(flash_data->r_fields[REG_STATUS1], &val);
  415. if (rc)
  416. return rc;
  417. for (i = 0; i < led->chan_count; i++) {
  418. chan_id = led->chan_id[i];
  419. shift = chan_id * 2;
  420. if (val & BIT(shift))
  421. fault_sts |= LED_FAULT_SHORT_CIRCUIT;
  422. chan_mask |= BIT(chan_id);
  423. }
  424. rc = regmap_field_read(flash_data->r_fields[REG_STATUS2], &val);
  425. if (rc)
  426. return rc;
  427. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  428. ot_mask = FLASH_STS_3CH_OTST1 |
  429. FLASH_STS_3CH_OTST2 |
  430. FLASH_STS_3CH_OTST3 |
  431. FLASH_STS_3CH_BOB_THM_OVERLOAD;
  432. oc_mask = FLASH_STS_3CH_BOB_ILIM_S1 |
  433. FLASH_STS_3CH_BOB_ILIM_S2 |
  434. FLASH_STS_3CH_BCL_IBAT;
  435. uv_mask = FLASH_STS_3CH_VPH_DROOP;
  436. } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
  437. ot_mask = FLASH_STS_4CH_OTST2 |
  438. FLASH_STS_4CH_OTST1 |
  439. FLASH_STS_4CHG_BOB_THM_OVERLOAD;
  440. oc_mask = FLASH_STS_4CH_BCL_IBAT |
  441. FLASH_STS_4CH_BOB_ILIM_S1 |
  442. FLASH_STS_4CH_BOB_ILIM_S2;
  443. uv_mask = FLASH_STS_4CH_VPH_LOW;
  444. }
  445. if (val & ot_mask)
  446. fault_sts |= LED_FAULT_OVER_TEMPERATURE;
  447. if (val & oc_mask)
  448. fault_sts |= LED_FAULT_OVER_CURRENT;
  449. if (val & uv_mask)
  450. fault_sts |= LED_FAULT_INPUT_VOLTAGE;
  451. rc = regmap_field_read(flash_data->r_fields[REG_STATUS3], &val);
  452. if (rc)
  453. return rc;
  454. if (flash_data->hw_type == QCOM_MVFLASH_3CH) {
  455. if (val & chan_mask)
  456. fault_sts |= LED_FAULT_TIMEOUT;
  457. } else if (flash_data->hw_type == QCOM_MVFLASH_4CH) {
  458. for (i = 0; i < led->chan_count; i++) {
  459. chan_id = led->chan_id[i];
  460. shift = chan_id * 2;
  461. if (val & BIT(shift))
  462. fault_sts |= LED_FAULT_TIMEOUT;
  463. }
  464. }
  465. *fault = fault_sts;
  466. return 0;
  467. }
  468. static int qcom_flash_led_brightness_set(struct led_classdev *led_cdev,
  469. enum led_brightness brightness)
  470. {
  471. struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(led_cdev);
  472. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  473. u32 current_ma = brightness * led->max_torch_current_ma / LED_FULL;
  474. bool enable = !!brightness;
  475. int rc;
  476. rc = set_flash_strobe(led, SW_STROBE, false);
  477. if (rc)
  478. return rc;
  479. rc = set_flash_module_en(led, false);
  480. if (rc)
  481. return rc;
  482. rc = update_allowed_flash_current(led, &current_ma, enable);
  483. if (rc < 0)
  484. return rc;
  485. rc = set_flash_current(led, current_ma, TORCH_MODE);
  486. if (rc)
  487. return rc;
  488. /* Disable flash timeout for torch LED */
  489. rc = set_flash_timeout(led, 0);
  490. if (rc)
  491. return rc;
  492. rc = set_flash_module_en(led, enable);
  493. if (rc)
  494. return rc;
  495. return set_flash_strobe(led, SW_STROBE, enable);
  496. }
  497. static const struct led_flash_ops qcom_flash_ops = {
  498. .flash_brightness_set = qcom_flash_brightness_set,
  499. .strobe_set = qcom_flash_strobe_set,
  500. .strobe_get = qcom_flash_strobe_get,
  501. .timeout_set = qcom_flash_timeout_set,
  502. .fault_get = qcom_flash_fault_get,
  503. };
  504. #if IS_ENABLED(CONFIG_V4L2_FLASH_LED_CLASS)
  505. static int qcom_flash_external_strobe_set(struct v4l2_flash *v4l2_flash, bool enable)
  506. {
  507. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  508. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  509. int rc;
  510. rc = set_flash_module_en(led, enable);
  511. if (rc)
  512. return rc;
  513. if (enable)
  514. return set_flash_strobe(led, HW_STROBE, true);
  515. else
  516. return set_flash_strobe(led, SW_STROBE, false);
  517. }
  518. static enum led_brightness
  519. qcom_flash_intensity_to_led_brightness(struct v4l2_flash *v4l2_flash, s32 intensity)
  520. {
  521. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  522. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  523. u32 current_ma = intensity / UA_PER_MA;
  524. current_ma = min_t(u32, current_ma, led->max_torch_current_ma);
  525. if (!current_ma)
  526. return LED_OFF;
  527. return (current_ma * LED_FULL) / led->max_torch_current_ma;
  528. }
  529. static s32 qcom_flash_brightness_to_led_intensity(struct v4l2_flash *v4l2_flash,
  530. enum led_brightness brightness)
  531. {
  532. struct led_classdev_flash *fled_cdev = v4l2_flash->fled_cdev;
  533. struct qcom_flash_led *led = flcdev_to_qcom_fled(fled_cdev);
  534. return (brightness * led->max_torch_current_ma * UA_PER_MA) / LED_FULL;
  535. }
  536. static const struct v4l2_flash_ops qcom_v4l2_flash_ops = {
  537. .external_strobe_set = qcom_flash_external_strobe_set,
  538. .intensity_to_led_brightness = qcom_flash_intensity_to_led_brightness,
  539. .led_brightness_to_intensity = qcom_flash_brightness_to_led_intensity,
  540. };
  541. static int
  542. qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode)
  543. {
  544. struct qcom_flash_data *flash_data = led->flash_data;
  545. struct v4l2_flash_config v4l2_cfg = { 0 };
  546. struct led_flash_setting *intensity = &v4l2_cfg.intensity;
  547. struct v4l2_flash *v4l2_flash;
  548. if (!(led->flash.led_cdev.flags & LED_DEV_CAP_FLASH))
  549. return 0;
  550. intensity->min = intensity->step = TORCH_IRES_UA * led->chan_count;
  551. intensity->max = led->max_torch_current_ma * UA_PER_MA;
  552. intensity->val = min_t(u32, intensity->max, TORCH_CURRENT_DEFAULT_UA);
  553. strscpy(v4l2_cfg.dev_name, led->flash.led_cdev.dev->kobj.name,
  554. sizeof(v4l2_cfg.dev_name));
  555. v4l2_cfg.has_external_strobe = true;
  556. v4l2_cfg.flash_faults = LED_FAULT_INPUT_VOLTAGE |
  557. LED_FAULT_OVER_CURRENT |
  558. LED_FAULT_SHORT_CIRCUIT |
  559. LED_FAULT_OVER_TEMPERATURE |
  560. LED_FAULT_TIMEOUT;
  561. v4l2_flash = v4l2_flash_init(dev, fwnode, &led->flash, &qcom_v4l2_flash_ops, &v4l2_cfg);
  562. if (IS_ERR(v4l2_flash))
  563. return PTR_ERR(v4l2_flash);
  564. flash_data->v4l2_flash[flash_data->leds_count] = v4l2_flash;
  565. return 0;
  566. }
  567. # else
  568. static int
  569. qcom_flash_v4l2_init(struct device *dev, struct qcom_flash_led *led, struct fwnode_handle *fwnode)
  570. {
  571. return 0;
  572. }
  573. #endif
  574. static int qcom_flash_register_led_device(struct device *dev,
  575. struct fwnode_handle *node, struct qcom_flash_led *led)
  576. {
  577. struct qcom_flash_data *flash_data = led->flash_data;
  578. struct led_init_data init_data;
  579. struct led_classdev_flash *flash = &led->flash;
  580. struct led_flash_setting *brightness, *timeout;
  581. u32 current_ua, timeout_us;
  582. u32 channels[4];
  583. int i, rc, count;
  584. u8 torch_clamp;
  585. count = fwnode_property_count_u32(node, "led-sources");
  586. if (count <= 0) {
  587. dev_err(dev, "No led-sources specified\n");
  588. return -ENODEV;
  589. }
  590. if (count > flash_data->max_channels) {
  591. dev_err(dev, "led-sources count %u exceeds maximum channel count %u\n",
  592. count, flash_data->max_channels);
  593. return -EINVAL;
  594. }
  595. rc = fwnode_property_read_u32_array(node, "led-sources", channels, count);
  596. if (rc < 0) {
  597. dev_err(dev, "Failed to read led-sources property, rc=%d\n", rc);
  598. return rc;
  599. }
  600. led->chan_count = count;
  601. led->chan_id = devm_kcalloc(dev, count, sizeof(u8), GFP_KERNEL);
  602. if (!led->chan_id)
  603. return -ENOMEM;
  604. for (i = 0; i < count; i++) {
  605. if ((channels[i] == 0) || (channels[i] > flash_data->max_channels)) {
  606. dev_err(dev, "led-source out of HW support range [1-%u]\n",
  607. flash_data->max_channels);
  608. return -EINVAL;
  609. }
  610. /* Make chan_id indexing from 0 */
  611. led->chan_id[i] = channels[i] - 1;
  612. }
  613. rc = fwnode_property_read_u32(node, "led-max-microamp", &current_ua);
  614. if (rc < 0) {
  615. dev_err(dev, "Failed to read led-max-microamp property, rc=%d\n", rc);
  616. return rc;
  617. }
  618. if (current_ua == 0) {
  619. dev_err(dev, "led-max-microamp shouldn't be 0\n");
  620. return -EINVAL;
  621. }
  622. current_ua = min_t(u32, current_ua, TORCH_CURRENT_MAX_UA * led->chan_count);
  623. led->max_torch_current_ma = current_ua / UA_PER_MA;
  624. torch_clamp = (current_ua / led->chan_count) / TORCH_IRES_UA;
  625. if (torch_clamp != 0)
  626. torch_clamp--;
  627. flash_data->torch_clamp = max_t(u8, flash_data->torch_clamp, torch_clamp);
  628. if (fwnode_property_present(node, "flash-max-microamp")) {
  629. flash->led_cdev.flags |= LED_DEV_CAP_FLASH;
  630. rc = fwnode_property_read_u32(node, "flash-max-microamp", &current_ua);
  631. if (rc < 0) {
  632. dev_err(dev, "Failed to read flash-max-microamp property, rc=%d\n",
  633. rc);
  634. return rc;
  635. }
  636. current_ua = min_t(u32, current_ua, FLASH_CURRENT_MAX_UA * led->chan_count);
  637. current_ua = min_t(u32, current_ua, FLASH_TOTAL_CURRENT_MAX_UA);
  638. /* Initialize flash class LED device brightness settings */
  639. brightness = &flash->brightness;
  640. brightness->min = brightness->step = FLASH_IRES_UA * led->chan_count;
  641. brightness->max = current_ua;
  642. brightness->val = min_t(u32, current_ua, FLASH_CURRENT_DEFAULT_UA);
  643. led->max_flash_current_ma = current_ua / UA_PER_MA;
  644. led->flash_current_ma = brightness->val / UA_PER_MA;
  645. rc = fwnode_property_read_u32(node, "flash-max-timeout-us", &timeout_us);
  646. if (rc < 0) {
  647. dev_err(dev, "Failed to read flash-max-timeout-us property, rc=%d\n",
  648. rc);
  649. return rc;
  650. }
  651. timeout_us = min_t(u32, timeout_us, FLASH_TIMEOUT_MAX_US);
  652. /* Initialize flash class LED device timeout settings */
  653. timeout = &flash->timeout;
  654. timeout->min = timeout->step = FLASH_TIMEOUT_STEP_US;
  655. timeout->val = timeout->max = timeout_us;
  656. led->max_timeout_ms = led->flash_timeout_ms = timeout_us / USEC_PER_MSEC;
  657. flash->ops = &qcom_flash_ops;
  658. }
  659. flash->led_cdev.brightness_set_blocking = qcom_flash_led_brightness_set;
  660. init_data.fwnode = node;
  661. init_data.devicename = NULL;
  662. init_data.default_label = NULL;
  663. init_data.devname_mandatory = false;
  664. rc = devm_led_classdev_flash_register_ext(dev, flash, &init_data);
  665. if (rc < 0) {
  666. dev_err(dev, "Register flash LED classdev failed, rc=%d\n", rc);
  667. return rc;
  668. }
  669. return qcom_flash_v4l2_init(dev, led, node);
  670. }
  671. static int qcom_flash_led_probe(struct platform_device *pdev)
  672. {
  673. struct qcom_flash_data *flash_data;
  674. struct qcom_flash_led *led;
  675. struct fwnode_handle *child;
  676. struct device *dev = &pdev->dev;
  677. struct regmap *regmap;
  678. struct reg_field *regs;
  679. int count, i, rc;
  680. u32 val, reg_base;
  681. flash_data = devm_kzalloc(dev, sizeof(*flash_data), GFP_KERNEL);
  682. if (!flash_data)
  683. return -ENOMEM;
  684. regmap = dev_get_regmap(dev->parent, NULL);
  685. if (!regmap) {
  686. dev_err(dev, "Failed to get parent regmap\n");
  687. return -EINVAL;
  688. }
  689. rc = fwnode_property_read_u32(dev->fwnode, "reg", &reg_base);
  690. if (rc < 0) {
  691. dev_err(dev, "Failed to get register base address, rc=%d\n", rc);
  692. return rc;
  693. }
  694. rc = regmap_read(regmap, reg_base + FLASH_TYPE_REG, &val);
  695. if (rc < 0) {
  696. dev_err(dev, "Read flash LED module type failed, rc=%d\n", rc);
  697. return rc;
  698. }
  699. if (val != FLASH_TYPE_VAL) {
  700. dev_err(dev, "type %#x is not a flash LED module\n", val);
  701. return -ENODEV;
  702. }
  703. rc = regmap_read(regmap, reg_base + FLASH_SUBTYPE_REG, &val);
  704. if (rc < 0) {
  705. dev_err(dev, "Read flash LED module subtype failed, rc=%d\n", rc);
  706. return rc;
  707. }
  708. if (val == FLASH_SUBTYPE_3CH_PM8150_VAL || val == FLASH_SUBTYPE_3CH_PMI8998_VAL) {
  709. flash_data->hw_type = QCOM_MVFLASH_3CH;
  710. flash_data->max_channels = 3;
  711. regs = devm_kmemdup(dev, mvflash_3ch_regs, sizeof(mvflash_3ch_regs),
  712. GFP_KERNEL);
  713. if (!regs)
  714. return -ENOMEM;
  715. } else if (val == FLASH_SUBTYPE_4CH_VAL) {
  716. flash_data->hw_type = QCOM_MVFLASH_4CH;
  717. flash_data->max_channels = 4;
  718. regs = devm_kmemdup(dev, mvflash_4ch_regs, sizeof(mvflash_4ch_regs),
  719. GFP_KERNEL);
  720. if (!regs)
  721. return -ENOMEM;
  722. rc = regmap_read(regmap, reg_base + FLASH_REVISION_REG, &val);
  723. if (rc < 0) {
  724. dev_err(dev, "Failed to read flash LED module revision, rc=%d\n", rc);
  725. return rc;
  726. }
  727. flash_data->revision = val;
  728. } else {
  729. dev_err(dev, "flash LED subtype %#x is not yet supported\n", val);
  730. return -ENODEV;
  731. }
  732. for (i = 0; i < REG_MAX_COUNT; i++)
  733. regs[i].reg += reg_base;
  734. rc = devm_regmap_field_bulk_alloc(dev, regmap, flash_data->r_fields, regs, REG_MAX_COUNT);
  735. if (rc < 0) {
  736. dev_err(dev, "Failed to allocate regmap field, rc=%d\n", rc);
  737. return rc;
  738. }
  739. devm_kfree(dev, regs); /* devm_regmap_field_bulk_alloc() makes copies */
  740. platform_set_drvdata(pdev, flash_data);
  741. mutex_init(&flash_data->lock);
  742. count = device_get_child_node_count(dev);
  743. if (count == 0 || count > flash_data->max_channels) {
  744. dev_err(dev, "No child or child count exceeds %d\n", flash_data->max_channels);
  745. return -EINVAL;
  746. }
  747. flash_data->v4l2_flash = devm_kcalloc(dev, count,
  748. sizeof(*flash_data->v4l2_flash), GFP_KERNEL);
  749. if (!flash_data->v4l2_flash)
  750. return -ENOMEM;
  751. device_for_each_child_node(dev, child) {
  752. led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
  753. if (!led) {
  754. rc = -ENOMEM;
  755. goto release;
  756. }
  757. led->flash_data = flash_data;
  758. rc = qcom_flash_register_led_device(dev, child, led);
  759. if (rc < 0)
  760. goto release;
  761. flash_data->leds_count++;
  762. }
  763. return regmap_field_write(flash_data->r_fields[REG_TORCH_CLAMP], flash_data->torch_clamp);
  764. release:
  765. fwnode_handle_put(child);
  766. while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count)
  767. v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]);
  768. return rc;
  769. }
  770. static void qcom_flash_led_remove(struct platform_device *pdev)
  771. {
  772. struct qcom_flash_data *flash_data = platform_get_drvdata(pdev);
  773. while (flash_data->v4l2_flash[flash_data->leds_count] && flash_data->leds_count)
  774. v4l2_flash_release(flash_data->v4l2_flash[flash_data->leds_count--]);
  775. mutex_destroy(&flash_data->lock);
  776. }
  777. static const struct of_device_id qcom_flash_led_match_table[] = {
  778. { .compatible = "qcom,spmi-flash-led" },
  779. { }
  780. };
  781. MODULE_DEVICE_TABLE(of, qcom_flash_led_match_table);
  782. static struct platform_driver qcom_flash_led_driver = {
  783. .driver = {
  784. .name = "leds-qcom-flash",
  785. .of_match_table = qcom_flash_led_match_table,
  786. },
  787. .probe = qcom_flash_led_probe,
  788. .remove_new = qcom_flash_led_remove,
  789. };
  790. module_platform_driver(qcom_flash_led_driver);
  791. MODULE_DESCRIPTION("QCOM Flash LED driver");
  792. MODULE_LICENSE("GPL");