leds-lp55xx-common.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * LP5521/LP5523/LP55231/LP5562 Common Driver
  4. *
  5. * Copyright 2012 Texas Instruments
  6. *
  7. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  8. *
  9. * Derived from leds-lp5521.c, leds-lp5523.c
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/cleanup.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/firmware.h>
  16. #include <linux/i2c.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/leds.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_data/leds-lp55xx.h>
  21. #include <linux/slab.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <dt-bindings/leds/leds-lp55xx.h>
  24. #include "leds-lp55xx-common.h"
  25. /* OP MODE require at least 153 us to clear regs */
  26. #define LP55XX_CMD_SLEEP 200
  27. #define LP55xx_PROGRAM_PAGES 16
  28. #define LP55xx_MAX_PROGRAM_LENGTH (LP55xx_BYTES_PER_PAGE * 4) /* 128 bytes (4 pages) */
  29. /*
  30. * Program Memory Operations
  31. * Same Mask for each engine for both mode and exec
  32. * ENG1 GENMASK(3, 2)
  33. * ENG2 GENMASK(5, 4)
  34. * ENG3 GENMASK(7, 6)
  35. */
  36. #define LP55xx_MODE_DISABLE_ALL_ENG 0x0
  37. #define LP55xx_MODE_ENG_MASK GENMASK(1, 0)
  38. #define LP55xx_MODE_DISABLE_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x0)
  39. #define LP55xx_MODE_LOAD_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x1)
  40. #define LP55xx_MODE_RUN_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x2)
  41. #define LP55xx_MODE_HALT_ENG FIELD_PREP_CONST(LP55xx_MODE_ENG_MASK, 0x3)
  42. #define LP55xx_MODE_ENGn_SHIFT(n, shift) ((shift) + (2 * (3 - (n))))
  43. #define LP55xx_MODE_ENGn_MASK(n, shift) (LP55xx_MODE_ENG_MASK << LP55xx_MODE_ENGn_SHIFT(n, shift))
  44. #define LP55xx_MODE_ENGn_GET(n, mode, shift) \
  45. (((mode) >> LP55xx_MODE_ENGn_SHIFT(n, shift)) & LP55xx_MODE_ENG_MASK)
  46. #define LP55xx_EXEC_ENG_MASK GENMASK(1, 0)
  47. #define LP55xx_EXEC_HOLD_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x0)
  48. #define LP55xx_EXEC_STEP_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x1)
  49. #define LP55xx_EXEC_RUN_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x2)
  50. #define LP55xx_EXEC_ONCE_ENG FIELD_PREP_CONST(LP55xx_EXEC_ENG_MASK, 0x3)
  51. #define LP55xx_EXEC_ENGn_SHIFT(n, shift) ((shift) + (2 * (3 - (n))))
  52. #define LP55xx_EXEC_ENGn_MASK(n, shift) (LP55xx_EXEC_ENG_MASK << LP55xx_EXEC_ENGn_SHIFT(n, shift))
  53. /* Memory Page Selection */
  54. #define LP55xx_REG_PROG_PAGE_SEL 0x4f
  55. /* If supported, each ENGINE have an equal amount of pages offset from page 0 */
  56. #define LP55xx_PAGE_OFFSET(n, pages) (((n) - 1) * (pages))
  57. #define LED_ACTIVE(mux, led) (!!((mux) & (0x0001 << (led))))
  58. /* MASTER FADER common property */
  59. #define LP55xx_FADER_MAPPING_MASK GENMASK(7, 6)
  60. /* External clock rate */
  61. #define LP55XX_CLK_32K 32768
  62. static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev)
  63. {
  64. return container_of(cdev, struct lp55xx_led, cdev);
  65. }
  66. static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev)
  67. {
  68. return cdev_to_lp55xx_led(dev_get_drvdata(dev));
  69. }
  70. static struct lp55xx_led *mcled_cdev_to_led(struct led_classdev_mc *mc_cdev)
  71. {
  72. return container_of(mc_cdev, struct lp55xx_led, mc_cdev);
  73. }
  74. static void lp55xx_wait_opmode_done(struct lp55xx_chip *chip)
  75. {
  76. const struct lp55xx_device_config *cfg = chip->cfg;
  77. int __always_unused ret;
  78. u8 val;
  79. /*
  80. * Recent chip supports BUSY bit for engine.
  81. * Check support by checking if val is not 0.
  82. * For legacy device, sleep at least 153 us.
  83. */
  84. if (cfg->engine_busy.val) {
  85. read_poll_timeout(lp55xx_read, ret, !(val & cfg->engine_busy.mask),
  86. LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 10, false,
  87. chip, cfg->engine_busy.addr, &val);
  88. } else {
  89. usleep_range(LP55XX_CMD_SLEEP, LP55XX_CMD_SLEEP * 2);
  90. }
  91. }
  92. void lp55xx_stop_all_engine(struct lp55xx_chip *chip)
  93. {
  94. const struct lp55xx_device_config *cfg = chip->cfg;
  95. lp55xx_write(chip, cfg->reg_op_mode.addr, LP55xx_MODE_DISABLE_ALL_ENG);
  96. lp55xx_wait_opmode_done(chip);
  97. }
  98. EXPORT_SYMBOL_GPL(lp55xx_stop_all_engine);
  99. void lp55xx_load_engine(struct lp55xx_chip *chip)
  100. {
  101. enum lp55xx_engine_index idx = chip->engine_idx;
  102. const struct lp55xx_device_config *cfg = chip->cfg;
  103. u8 mask, val;
  104. mask = LP55xx_MODE_ENGn_MASK(idx, cfg->reg_op_mode.shift);
  105. val = LP55xx_MODE_LOAD_ENG << LP55xx_MODE_ENGn_SHIFT(idx, cfg->reg_op_mode.shift);
  106. lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, val);
  107. lp55xx_wait_opmode_done(chip);
  108. /* Setup PAGE if supported (pages_per_engine not 0)*/
  109. if (cfg->pages_per_engine)
  110. lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL,
  111. LP55xx_PAGE_OFFSET(idx, cfg->pages_per_engine));
  112. }
  113. EXPORT_SYMBOL_GPL(lp55xx_load_engine);
  114. int lp55xx_run_engine_common(struct lp55xx_chip *chip)
  115. {
  116. const struct lp55xx_device_config *cfg = chip->cfg;
  117. u8 mode, exec;
  118. int i, ret;
  119. /* To run the engine, both OP MODE and EXEC needs to be put in RUN mode */
  120. ret = lp55xx_read(chip, cfg->reg_op_mode.addr, &mode);
  121. if (ret)
  122. return ret;
  123. ret = lp55xx_read(chip, cfg->reg_exec.addr, &exec);
  124. if (ret)
  125. return ret;
  126. /* Switch to RUN only for engine that were put in LOAD previously */
  127. for (i = LP55XX_ENGINE_1; i <= LP55XX_ENGINE_3; i++) {
  128. if (LP55xx_MODE_ENGn_GET(i, mode, cfg->reg_op_mode.shift) != LP55xx_MODE_LOAD_ENG)
  129. continue;
  130. mode &= ~LP55xx_MODE_ENGn_MASK(i, cfg->reg_op_mode.shift);
  131. mode |= LP55xx_MODE_RUN_ENG << LP55xx_MODE_ENGn_SHIFT(i, cfg->reg_op_mode.shift);
  132. exec &= ~LP55xx_EXEC_ENGn_MASK(i, cfg->reg_exec.shift);
  133. exec |= LP55xx_EXEC_RUN_ENG << LP55xx_EXEC_ENGn_SHIFT(i, cfg->reg_exec.shift);
  134. }
  135. lp55xx_write(chip, cfg->reg_op_mode.addr, mode);
  136. lp55xx_wait_opmode_done(chip);
  137. lp55xx_write(chip, cfg->reg_exec.addr, exec);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL_GPL(lp55xx_run_engine_common);
  141. int lp55xx_update_program_memory(struct lp55xx_chip *chip,
  142. const u8 *data, size_t size)
  143. {
  144. enum lp55xx_engine_index idx = chip->engine_idx;
  145. const struct lp55xx_device_config *cfg = chip->cfg;
  146. u8 pattern[LP55xx_MAX_PROGRAM_LENGTH] = { };
  147. u8 start_addr = cfg->prog_mem_base.addr;
  148. int page, i = 0, offset = 0;
  149. int program_length, ret;
  150. program_length = LP55xx_BYTES_PER_PAGE;
  151. if (cfg->pages_per_engine)
  152. program_length *= cfg->pages_per_engine;
  153. while ((offset < size - 1) && (i < program_length)) {
  154. unsigned int cmd;
  155. int nrchars;
  156. char c[3];
  157. /* separate sscanfs because length is working only for %s */
  158. ret = sscanf(data + offset, "%2s%n ", c, &nrchars);
  159. if (ret != 1)
  160. goto err;
  161. ret = sscanf(c, "%2x", &cmd);
  162. if (ret != 1)
  163. goto err;
  164. pattern[i] = (u8)cmd;
  165. offset += nrchars;
  166. i++;
  167. }
  168. /* Each instruction is 16bit long. Check that length is even */
  169. if (i % 2)
  170. goto err;
  171. /*
  172. * For legacy LED chip with no page support, engine base address are
  173. * one after another at offset of 32.
  174. * For LED chip that support page, PAGE is already set in load_engine.
  175. */
  176. if (!cfg->pages_per_engine)
  177. start_addr += LP55xx_BYTES_PER_PAGE * (idx - 1);
  178. for (page = 0; page < program_length / LP55xx_BYTES_PER_PAGE; page++) {
  179. /* Write to the next page each 32 bytes (if supported) */
  180. if (cfg->pages_per_engine)
  181. lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL,
  182. LP55xx_PAGE_OFFSET(idx, cfg->pages_per_engine) + page);
  183. for (i = 0; i < LP55xx_BYTES_PER_PAGE; i++) {
  184. ret = lp55xx_write(chip, start_addr + i,
  185. pattern[i + (page * LP55xx_BYTES_PER_PAGE)]);
  186. if (ret)
  187. return -EINVAL;
  188. }
  189. }
  190. return size;
  191. err:
  192. dev_err(&chip->cl->dev, "wrong pattern format\n");
  193. return -EINVAL;
  194. }
  195. EXPORT_SYMBOL_GPL(lp55xx_update_program_memory);
  196. void lp55xx_firmware_loaded_cb(struct lp55xx_chip *chip)
  197. {
  198. const struct lp55xx_device_config *cfg = chip->cfg;
  199. const struct firmware *fw = chip->fw;
  200. int program_length;
  201. program_length = LP55xx_BYTES_PER_PAGE;
  202. if (cfg->pages_per_engine)
  203. program_length *= cfg->pages_per_engine;
  204. /*
  205. * the firmware is encoded in ascii hex character, with 2 chars
  206. * per byte
  207. */
  208. if (fw->size > program_length * 2) {
  209. dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n",
  210. fw->size);
  211. return;
  212. }
  213. /*
  214. * Program memory sequence
  215. * 1) set engine mode to "LOAD"
  216. * 2) write firmware data into program memory
  217. */
  218. lp55xx_load_engine(chip);
  219. lp55xx_update_program_memory(chip, fw->data, fw->size);
  220. }
  221. EXPORT_SYMBOL_GPL(lp55xx_firmware_loaded_cb);
  222. int lp55xx_led_brightness(struct lp55xx_led *led)
  223. {
  224. struct lp55xx_chip *chip = led->chip;
  225. const struct lp55xx_device_config *cfg = chip->cfg;
  226. int ret;
  227. guard(mutex)(&chip->lock);
  228. ret = lp55xx_write(chip, cfg->reg_led_pwm_base.addr + led->chan_nr,
  229. led->brightness);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(lp55xx_led_brightness);
  233. int lp55xx_multicolor_brightness(struct lp55xx_led *led)
  234. {
  235. struct lp55xx_chip *chip = led->chip;
  236. const struct lp55xx_device_config *cfg = chip->cfg;
  237. int ret;
  238. int i;
  239. guard(mutex)(&chip->lock);
  240. for (i = 0; i < led->mc_cdev.num_colors; i++) {
  241. ret = lp55xx_write(chip,
  242. cfg->reg_led_pwm_base.addr +
  243. led->mc_cdev.subled_info[i].channel,
  244. led->mc_cdev.subled_info[i].brightness);
  245. if (ret)
  246. break;
  247. }
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(lp55xx_multicolor_brightness);
  251. void lp55xx_set_led_current(struct lp55xx_led *led, u8 led_current)
  252. {
  253. struct lp55xx_chip *chip = led->chip;
  254. const struct lp55xx_device_config *cfg = chip->cfg;
  255. led->led_current = led_current;
  256. lp55xx_write(led->chip, cfg->reg_led_current_base.addr + led->chan_nr,
  257. led_current);
  258. }
  259. EXPORT_SYMBOL_GPL(lp55xx_set_led_current);
  260. void lp55xx_turn_off_channels(struct lp55xx_chip *chip)
  261. {
  262. const struct lp55xx_device_config *cfg = chip->cfg;
  263. int i;
  264. for (i = 0; i < cfg->max_channel; i++)
  265. lp55xx_write(chip, cfg->reg_led_pwm_base.addr + i, 0);
  266. }
  267. EXPORT_SYMBOL_GPL(lp55xx_turn_off_channels);
  268. void lp55xx_stop_engine(struct lp55xx_chip *chip)
  269. {
  270. enum lp55xx_engine_index idx = chip->engine_idx;
  271. const struct lp55xx_device_config *cfg = chip->cfg;
  272. u8 mask;
  273. mask = LP55xx_MODE_ENGn_MASK(idx, cfg->reg_op_mode.shift);
  274. lp55xx_update_bits(chip, cfg->reg_op_mode.addr, mask, 0);
  275. lp55xx_wait_opmode_done(chip);
  276. }
  277. EXPORT_SYMBOL_GPL(lp55xx_stop_engine);
  278. static void lp55xx_reset_device(struct lp55xx_chip *chip)
  279. {
  280. const struct lp55xx_device_config *cfg = chip->cfg;
  281. u8 addr = cfg->reset.addr;
  282. u8 val = cfg->reset.val;
  283. /* no error checking here because no ACK from the device after reset */
  284. lp55xx_write(chip, addr, val);
  285. }
  286. static int lp55xx_detect_device(struct lp55xx_chip *chip)
  287. {
  288. const struct lp55xx_device_config *cfg = chip->cfg;
  289. u8 addr = cfg->enable.addr;
  290. u8 val = cfg->enable.val;
  291. int ret;
  292. ret = lp55xx_write(chip, addr, val);
  293. if (ret)
  294. return ret;
  295. usleep_range(1000, 2000);
  296. ret = lp55xx_read(chip, addr, &val);
  297. if (ret)
  298. return ret;
  299. if (val != cfg->enable.val)
  300. return -ENODEV;
  301. return 0;
  302. }
  303. static int lp55xx_post_init_device(struct lp55xx_chip *chip)
  304. {
  305. const struct lp55xx_device_config *cfg = chip->cfg;
  306. if (!cfg->post_init_device)
  307. return 0;
  308. return cfg->post_init_device(chip);
  309. }
  310. static ssize_t led_current_show(struct device *dev,
  311. struct device_attribute *attr,
  312. char *buf)
  313. {
  314. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  315. return sysfs_emit(buf, "%d\n", led->led_current);
  316. }
  317. static ssize_t led_current_store(struct device *dev,
  318. struct device_attribute *attr,
  319. const char *buf, size_t len)
  320. {
  321. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  322. struct lp55xx_chip *chip = led->chip;
  323. unsigned long curr;
  324. if (kstrtoul(buf, 0, &curr))
  325. return -EINVAL;
  326. if (curr > led->max_current)
  327. return -EINVAL;
  328. if (!chip->cfg->set_led_current)
  329. return len;
  330. guard(mutex)(&chip->lock);
  331. chip->cfg->set_led_current(led, (u8)curr);
  332. return len;
  333. }
  334. static ssize_t max_current_show(struct device *dev,
  335. struct device_attribute *attr,
  336. char *buf)
  337. {
  338. struct lp55xx_led *led = dev_to_lp55xx_led(dev);
  339. return sysfs_emit(buf, "%d\n", led->max_current);
  340. }
  341. static DEVICE_ATTR_RW(led_current);
  342. static DEVICE_ATTR_RO(max_current);
  343. static struct attribute *lp55xx_led_attrs[] = {
  344. &dev_attr_led_current.attr,
  345. &dev_attr_max_current.attr,
  346. NULL,
  347. };
  348. ATTRIBUTE_GROUPS(lp55xx_led);
  349. static int lp55xx_set_mc_brightness(struct led_classdev *cdev,
  350. enum led_brightness brightness)
  351. {
  352. struct led_classdev_mc *mc_dev = lcdev_to_mccdev(cdev);
  353. struct lp55xx_led *led = mcled_cdev_to_led(mc_dev);
  354. const struct lp55xx_device_config *cfg = led->chip->cfg;
  355. led_mc_calc_color_components(&led->mc_cdev, brightness);
  356. return cfg->multicolor_brightness_fn(led);
  357. }
  358. static int lp55xx_set_brightness(struct led_classdev *cdev,
  359. enum led_brightness brightness)
  360. {
  361. struct lp55xx_led *led = cdev_to_lp55xx_led(cdev);
  362. const struct lp55xx_device_config *cfg = led->chip->cfg;
  363. led->brightness = (u8)brightness;
  364. return cfg->brightness_fn(led);
  365. }
  366. static int lp55xx_init_led(struct lp55xx_led *led,
  367. struct lp55xx_chip *chip, int chan)
  368. {
  369. struct lp55xx_platform_data *pdata = chip->pdata;
  370. const struct lp55xx_device_config *cfg = chip->cfg;
  371. struct device *dev = &chip->cl->dev;
  372. int max_channel = cfg->max_channel;
  373. struct mc_subled *mc_led_info;
  374. struct led_classdev *led_cdev;
  375. char name[32];
  376. int i;
  377. int ret;
  378. if (chan >= max_channel) {
  379. dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel);
  380. return -EINVAL;
  381. }
  382. if (pdata->led_config[chan].led_current == 0)
  383. return 0;
  384. if (pdata->led_config[chan].name) {
  385. led->cdev.name = pdata->led_config[chan].name;
  386. } else {
  387. snprintf(name, sizeof(name), "%s:channel%d",
  388. pdata->label ? : chip->cl->name, chan);
  389. led->cdev.name = name;
  390. }
  391. if (pdata->led_config[chan].num_colors > 1) {
  392. mc_led_info = devm_kcalloc(dev,
  393. pdata->led_config[chan].num_colors,
  394. sizeof(*mc_led_info), GFP_KERNEL);
  395. if (!mc_led_info)
  396. return -ENOMEM;
  397. led_cdev = &led->mc_cdev.led_cdev;
  398. led_cdev->name = led->cdev.name;
  399. led_cdev->brightness_set_blocking = lp55xx_set_mc_brightness;
  400. led->mc_cdev.num_colors = pdata->led_config[chan].num_colors;
  401. for (i = 0; i < led->mc_cdev.num_colors; i++) {
  402. mc_led_info[i].color_index =
  403. pdata->led_config[chan].color_id[i];
  404. mc_led_info[i].channel =
  405. pdata->led_config[chan].output_num[i];
  406. }
  407. led->mc_cdev.subled_info = mc_led_info;
  408. } else {
  409. led->cdev.brightness_set_blocking = lp55xx_set_brightness;
  410. }
  411. led->cdev.groups = lp55xx_led_groups;
  412. led->cdev.default_trigger = pdata->led_config[chan].default_trigger;
  413. led->led_current = pdata->led_config[chan].led_current;
  414. led->max_current = pdata->led_config[chan].max_current;
  415. led->chan_nr = pdata->led_config[chan].chan_nr;
  416. if (led->chan_nr >= max_channel) {
  417. dev_err(dev, "Use channel numbers between 0 and %d\n",
  418. max_channel - 1);
  419. return -EINVAL;
  420. }
  421. if (pdata->led_config[chan].num_colors > 1)
  422. ret = devm_led_classdev_multicolor_register(dev, &led->mc_cdev);
  423. else
  424. ret = devm_led_classdev_register(dev, &led->cdev);
  425. if (ret) {
  426. dev_err(dev, "led register err: %d\n", ret);
  427. return ret;
  428. }
  429. return 0;
  430. }
  431. static void lp55xx_firmware_loaded(const struct firmware *fw, void *context)
  432. {
  433. struct lp55xx_chip *chip = context;
  434. struct device *dev = &chip->cl->dev;
  435. enum lp55xx_engine_index idx = chip->engine_idx;
  436. if (!fw) {
  437. dev_err(dev, "firmware request failed\n");
  438. return;
  439. }
  440. /* handling firmware data is chip dependent */
  441. scoped_guard(mutex, &chip->lock) {
  442. chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD;
  443. chip->fw = fw;
  444. if (chip->cfg->firmware_cb)
  445. chip->cfg->firmware_cb(chip);
  446. }
  447. /* firmware should be released for other channel use */
  448. release_firmware(chip->fw);
  449. chip->fw = NULL;
  450. }
  451. static int lp55xx_request_firmware(struct lp55xx_chip *chip)
  452. {
  453. const char *name = chip->cl->name;
  454. struct device *dev = &chip->cl->dev;
  455. return request_firmware_nowait(THIS_MODULE, false, name, dev,
  456. GFP_KERNEL, chip, lp55xx_firmware_loaded);
  457. }
  458. static ssize_t select_engine_show(struct device *dev,
  459. struct device_attribute *attr,
  460. char *buf)
  461. {
  462. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  463. struct lp55xx_chip *chip = led->chip;
  464. return sprintf(buf, "%d\n", chip->engine_idx);
  465. }
  466. static ssize_t select_engine_store(struct device *dev,
  467. struct device_attribute *attr,
  468. const char *buf, size_t len)
  469. {
  470. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  471. struct lp55xx_chip *chip = led->chip;
  472. unsigned long val;
  473. int ret;
  474. if (kstrtoul(buf, 0, &val))
  475. return -EINVAL;
  476. /* select the engine to be run */
  477. switch (val) {
  478. case LP55XX_ENGINE_1:
  479. case LP55XX_ENGINE_2:
  480. case LP55XX_ENGINE_3:
  481. scoped_guard(mutex, &chip->lock) {
  482. chip->engine_idx = val;
  483. ret = lp55xx_request_firmware(chip);
  484. }
  485. break;
  486. default:
  487. dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val);
  488. return -EINVAL;
  489. }
  490. if (ret) {
  491. dev_err(dev, "request firmware err: %d\n", ret);
  492. return ret;
  493. }
  494. return len;
  495. }
  496. static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start)
  497. {
  498. if (chip->cfg->run_engine)
  499. chip->cfg->run_engine(chip, start);
  500. }
  501. static ssize_t run_engine_store(struct device *dev,
  502. struct device_attribute *attr,
  503. const char *buf, size_t len)
  504. {
  505. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  506. struct lp55xx_chip *chip = led->chip;
  507. unsigned long val;
  508. if (kstrtoul(buf, 0, &val))
  509. return -EINVAL;
  510. /* run or stop the selected engine */
  511. if (val <= 0) {
  512. lp55xx_run_engine(chip, false);
  513. return len;
  514. }
  515. guard(mutex)(&chip->lock);
  516. lp55xx_run_engine(chip, true);
  517. return len;
  518. }
  519. static DEVICE_ATTR_RW(select_engine);
  520. static DEVICE_ATTR_WO(run_engine);
  521. ssize_t lp55xx_show_engine_mode(struct device *dev,
  522. struct device_attribute *attr,
  523. char *buf, int nr)
  524. {
  525. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  526. struct lp55xx_chip *chip = led->chip;
  527. enum lp55xx_engine_mode mode = chip->engines[nr - 1].mode;
  528. switch (mode) {
  529. case LP55XX_ENGINE_RUN:
  530. return sysfs_emit(buf, "run\n");
  531. case LP55XX_ENGINE_LOAD:
  532. return sysfs_emit(buf, "load\n");
  533. case LP55XX_ENGINE_DISABLED:
  534. default:
  535. return sysfs_emit(buf, "disabled\n");
  536. }
  537. }
  538. EXPORT_SYMBOL_GPL(lp55xx_show_engine_mode);
  539. ssize_t lp55xx_store_engine_mode(struct device *dev,
  540. struct device_attribute *attr,
  541. const char *buf, size_t len, int nr)
  542. {
  543. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  544. struct lp55xx_chip *chip = led->chip;
  545. const struct lp55xx_device_config *cfg = chip->cfg;
  546. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  547. guard(mutex)(&chip->lock);
  548. chip->engine_idx = nr;
  549. if (!strncmp(buf, "run", 3)) {
  550. cfg->run_engine(chip, true);
  551. engine->mode = LP55XX_ENGINE_RUN;
  552. } else if (!strncmp(buf, "load", 4)) {
  553. lp55xx_stop_engine(chip);
  554. lp55xx_load_engine(chip);
  555. engine->mode = LP55XX_ENGINE_LOAD;
  556. } else if (!strncmp(buf, "disabled", 8)) {
  557. lp55xx_stop_engine(chip);
  558. engine->mode = LP55XX_ENGINE_DISABLED;
  559. }
  560. return len;
  561. }
  562. EXPORT_SYMBOL_GPL(lp55xx_store_engine_mode);
  563. ssize_t lp55xx_store_engine_load(struct device *dev,
  564. struct device_attribute *attr,
  565. const char *buf, size_t len, int nr)
  566. {
  567. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  568. struct lp55xx_chip *chip = led->chip;
  569. int ret;
  570. guard(mutex)(&chip->lock);
  571. chip->engine_idx = nr;
  572. lp55xx_load_engine(chip);
  573. ret = lp55xx_update_program_memory(chip, buf, len);
  574. return ret;
  575. }
  576. EXPORT_SYMBOL_GPL(lp55xx_store_engine_load);
  577. static int lp55xx_mux_parse(struct lp55xx_chip *chip, const char *buf,
  578. u16 *mux, size_t len)
  579. {
  580. const struct lp55xx_device_config *cfg = chip->cfg;
  581. u16 tmp_mux = 0;
  582. int i;
  583. len = min_t(int, len, cfg->max_channel);
  584. for (i = 0; i < len; i++) {
  585. switch (buf[i]) {
  586. case '1':
  587. tmp_mux |= (1 << i);
  588. break;
  589. case '0':
  590. break;
  591. case '\n':
  592. i = len;
  593. break;
  594. default:
  595. return -1;
  596. }
  597. }
  598. *mux = tmp_mux;
  599. return 0;
  600. }
  601. ssize_t lp55xx_show_engine_leds(struct device *dev,
  602. struct device_attribute *attr,
  603. char *buf, int nr)
  604. {
  605. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  606. struct lp55xx_chip *chip = led->chip;
  607. const struct lp55xx_device_config *cfg = chip->cfg;
  608. unsigned int led_active;
  609. int i, pos = 0;
  610. for (i = 0; i < cfg->max_channel; i++) {
  611. led_active = LED_ACTIVE(chip->engines[nr - 1].led_mux, i);
  612. pos += sysfs_emit_at(buf, pos, "%x", led_active);
  613. }
  614. pos += sysfs_emit_at(buf, pos, "\n");
  615. return pos;
  616. }
  617. EXPORT_SYMBOL_GPL(lp55xx_show_engine_leds);
  618. static int lp55xx_load_mux(struct lp55xx_chip *chip, u16 mux, int nr)
  619. {
  620. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  621. const struct lp55xx_device_config *cfg = chip->cfg;
  622. u8 mux_page;
  623. int ret;
  624. lp55xx_load_engine(chip);
  625. /* Derive the MUX page offset by starting at the end of the ENGINE pages */
  626. mux_page = cfg->pages_per_engine * LP55XX_ENGINE_MAX + (nr - 1);
  627. ret = lp55xx_write(chip, LP55xx_REG_PROG_PAGE_SEL, mux_page);
  628. if (ret)
  629. return ret;
  630. ret = lp55xx_write(chip, cfg->prog_mem_base.addr, (u8)(mux >> 8));
  631. if (ret)
  632. return ret;
  633. ret = lp55xx_write(chip, cfg->prog_mem_base.addr + 1, (u8)(mux));
  634. if (ret)
  635. return ret;
  636. engine->led_mux = mux;
  637. return 0;
  638. }
  639. ssize_t lp55xx_store_engine_leds(struct device *dev,
  640. struct device_attribute *attr,
  641. const char *buf, size_t len, int nr)
  642. {
  643. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  644. struct lp55xx_chip *chip = led->chip;
  645. struct lp55xx_engine *engine = &chip->engines[nr - 1];
  646. u16 mux = 0;
  647. if (lp55xx_mux_parse(chip, buf, &mux, len))
  648. return -EINVAL;
  649. guard(mutex)(&chip->lock);
  650. chip->engine_idx = nr;
  651. if (engine->mode != LP55XX_ENGINE_LOAD)
  652. return -EINVAL;
  653. if (lp55xx_load_mux(chip, mux, nr))
  654. return -EINVAL;
  655. return len;
  656. }
  657. EXPORT_SYMBOL_GPL(lp55xx_store_engine_leds);
  658. ssize_t lp55xx_show_master_fader(struct device *dev,
  659. struct device_attribute *attr,
  660. char *buf, int nr)
  661. {
  662. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  663. struct lp55xx_chip *chip = led->chip;
  664. const struct lp55xx_device_config *cfg = chip->cfg;
  665. int ret;
  666. u8 val;
  667. guard(mutex)(&chip->lock);
  668. ret = lp55xx_read(chip, cfg->reg_master_fader_base.addr + nr - 1, &val);
  669. return ret ? ret : sysfs_emit(buf, "%u\n", val);
  670. }
  671. EXPORT_SYMBOL_GPL(lp55xx_show_master_fader);
  672. ssize_t lp55xx_store_master_fader(struct device *dev,
  673. struct device_attribute *attr,
  674. const char *buf, size_t len, int nr)
  675. {
  676. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  677. struct lp55xx_chip *chip = led->chip;
  678. const struct lp55xx_device_config *cfg = chip->cfg;
  679. int ret;
  680. unsigned long val;
  681. if (kstrtoul(buf, 0, &val))
  682. return -EINVAL;
  683. if (val > 0xff)
  684. return -EINVAL;
  685. guard(mutex)(&chip->lock);
  686. ret = lp55xx_write(chip, cfg->reg_master_fader_base.addr + nr - 1,
  687. (u8)val);
  688. return ret ? ret : len;
  689. }
  690. EXPORT_SYMBOL_GPL(lp55xx_store_master_fader);
  691. ssize_t lp55xx_show_master_fader_leds(struct device *dev,
  692. struct device_attribute *attr,
  693. char *buf)
  694. {
  695. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  696. struct lp55xx_chip *chip = led->chip;
  697. const struct lp55xx_device_config *cfg = chip->cfg;
  698. int i, ret, pos = 0;
  699. u8 val;
  700. guard(mutex)(&chip->lock);
  701. for (i = 0; i < cfg->max_channel; i++) {
  702. ret = lp55xx_read(chip, cfg->reg_led_ctrl_base.addr + i, &val);
  703. if (ret)
  704. return ret;
  705. val = FIELD_GET(LP55xx_FADER_MAPPING_MASK, val);
  706. if (val > FIELD_MAX(LP55xx_FADER_MAPPING_MASK)) {
  707. return -EINVAL;
  708. }
  709. buf[pos++] = val + '0';
  710. }
  711. buf[pos++] = '\n';
  712. return pos;
  713. }
  714. EXPORT_SYMBOL_GPL(lp55xx_show_master_fader_leds);
  715. ssize_t lp55xx_store_master_fader_leds(struct device *dev,
  716. struct device_attribute *attr,
  717. const char *buf, size_t len)
  718. {
  719. struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev));
  720. struct lp55xx_chip *chip = led->chip;
  721. const struct lp55xx_device_config *cfg = chip->cfg;
  722. int i, n, ret;
  723. u8 val;
  724. n = min_t(int, len, cfg->max_channel);
  725. guard(mutex)(&chip->lock);
  726. for (i = 0; i < n; i++) {
  727. if (buf[i] >= '0' && buf[i] <= '3') {
  728. val = (buf[i] - '0') << __bf_shf(LP55xx_FADER_MAPPING_MASK);
  729. ret = lp55xx_update_bits(chip,
  730. cfg->reg_led_ctrl_base.addr + i,
  731. LP55xx_FADER_MAPPING_MASK,
  732. val);
  733. if (ret)
  734. return ret;
  735. } else {
  736. return -EINVAL;
  737. }
  738. }
  739. return len;
  740. }
  741. EXPORT_SYMBOL_GPL(lp55xx_store_master_fader_leds);
  742. static struct attribute *lp55xx_engine_attributes[] = {
  743. &dev_attr_select_engine.attr,
  744. &dev_attr_run_engine.attr,
  745. NULL,
  746. };
  747. static const struct attribute_group lp55xx_engine_attr_group = {
  748. .attrs = lp55xx_engine_attributes,
  749. };
  750. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  751. {
  752. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  753. }
  754. EXPORT_SYMBOL_GPL(lp55xx_write);
  755. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  756. {
  757. s32 ret;
  758. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  759. if (ret < 0)
  760. return ret;
  761. *val = ret;
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(lp55xx_read);
  765. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  766. {
  767. int ret;
  768. u8 tmp;
  769. ret = lp55xx_read(chip, reg, &tmp);
  770. if (ret)
  771. return ret;
  772. tmp &= ~mask;
  773. tmp |= val & mask;
  774. return lp55xx_write(chip, reg, tmp);
  775. }
  776. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  777. bool lp55xx_is_extclk_used(struct lp55xx_chip *chip)
  778. {
  779. struct clk *clk;
  780. clk = devm_clk_get_enabled(&chip->cl->dev, "32k_clk");
  781. if (IS_ERR(clk))
  782. goto use_internal_clk;
  783. if (clk_get_rate(clk) != LP55XX_CLK_32K)
  784. goto use_internal_clk;
  785. dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K);
  786. return true;
  787. use_internal_clk:
  788. dev_info(&chip->cl->dev, "internal clock used\n");
  789. return false;
  790. }
  791. EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used);
  792. static void lp55xx_deinit_device(struct lp55xx_chip *chip)
  793. {
  794. struct lp55xx_platform_data *pdata = chip->pdata;
  795. if (pdata->enable_gpiod)
  796. gpiod_set_value(pdata->enable_gpiod, 0);
  797. }
  798. static int lp55xx_init_device(struct lp55xx_chip *chip)
  799. {
  800. struct lp55xx_platform_data *pdata;
  801. const struct lp55xx_device_config *cfg;
  802. struct device *dev = &chip->cl->dev;
  803. int ret = 0;
  804. WARN_ON(!chip);
  805. pdata = chip->pdata;
  806. cfg = chip->cfg;
  807. if (!pdata || !cfg)
  808. return -EINVAL;
  809. if (pdata->enable_gpiod) {
  810. gpiod_direction_output(pdata->enable_gpiod, 0);
  811. gpiod_set_consumer_name(pdata->enable_gpiod, "LP55xx enable");
  812. gpiod_set_value_cansleep(pdata->enable_gpiod, 0);
  813. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  814. gpiod_set_value_cansleep(pdata->enable_gpiod, 1);
  815. usleep_range(1000, 2000); /* 500us abs min. */
  816. }
  817. lp55xx_reset_device(chip);
  818. /*
  819. * Exact value is not available. 10 - 20ms
  820. * appears to be enough for reset.
  821. */
  822. usleep_range(10000, 20000);
  823. ret = lp55xx_detect_device(chip);
  824. if (ret) {
  825. dev_err(dev, "device detection err: %d\n", ret);
  826. goto err;
  827. }
  828. /* chip specific initialization */
  829. ret = lp55xx_post_init_device(chip);
  830. if (ret) {
  831. dev_err(dev, "post init device err: %d\n", ret);
  832. goto err_post_init;
  833. }
  834. return 0;
  835. err_post_init:
  836. lp55xx_deinit_device(chip);
  837. err:
  838. return ret;
  839. }
  840. static int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip)
  841. {
  842. struct lp55xx_platform_data *pdata = chip->pdata;
  843. const struct lp55xx_device_config *cfg = chip->cfg;
  844. int num_channels = pdata->num_channels;
  845. struct lp55xx_led *each;
  846. u8 led_current;
  847. int ret;
  848. int i;
  849. if (!cfg->brightness_fn) {
  850. dev_err(&chip->cl->dev, "empty brightness configuration\n");
  851. return -EINVAL;
  852. }
  853. for (i = 0; i < num_channels; i++) {
  854. /* do not initialize channels that are not connected */
  855. if (pdata->led_config[i].led_current == 0)
  856. continue;
  857. led_current = pdata->led_config[i].led_current;
  858. each = led + i;
  859. ret = lp55xx_init_led(each, chip, i);
  860. if (ret)
  861. goto err_init_led;
  862. chip->num_leds++;
  863. each->chip = chip;
  864. /* setting led current at each channel */
  865. if (cfg->set_led_current)
  866. cfg->set_led_current(each, led_current);
  867. }
  868. return 0;
  869. err_init_led:
  870. return ret;
  871. }
  872. static int lp55xx_register_sysfs(struct lp55xx_chip *chip)
  873. {
  874. struct device *dev = &chip->cl->dev;
  875. const struct lp55xx_device_config *cfg = chip->cfg;
  876. int ret;
  877. if (!cfg->run_engine || !cfg->firmware_cb)
  878. goto dev_specific_attrs;
  879. ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group);
  880. if (ret)
  881. return ret;
  882. dev_specific_attrs:
  883. return cfg->dev_attr_group ?
  884. sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0;
  885. }
  886. static void lp55xx_unregister_sysfs(struct lp55xx_chip *chip)
  887. {
  888. struct device *dev = &chip->cl->dev;
  889. const struct lp55xx_device_config *cfg = chip->cfg;
  890. if (cfg->dev_attr_group)
  891. sysfs_remove_group(&dev->kobj, cfg->dev_attr_group);
  892. sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group);
  893. }
  894. static int lp55xx_parse_common_child(struct device_node *np,
  895. struct lp55xx_led_config *cfg,
  896. int led_number, int *chan_nr)
  897. {
  898. int ret;
  899. of_property_read_string(np, "chan-name",
  900. &cfg[led_number].name);
  901. of_property_read_u8(np, "led-cur",
  902. &cfg[led_number].led_current);
  903. of_property_read_u8(np, "max-cur",
  904. &cfg[led_number].max_current);
  905. ret = of_property_read_u32(np, "reg", chan_nr);
  906. if (ret)
  907. return ret;
  908. return 0;
  909. }
  910. static int lp55xx_parse_multi_led_child(struct device_node *child,
  911. struct lp55xx_led_config *cfg,
  912. int child_number, int color_number)
  913. {
  914. int chan_nr, color_id, ret;
  915. ret = lp55xx_parse_common_child(child, cfg, child_number, &chan_nr);
  916. if (ret)
  917. return ret;
  918. ret = of_property_read_u32(child, "color", &color_id);
  919. if (ret)
  920. return ret;
  921. cfg[child_number].color_id[color_number] = color_id;
  922. cfg[child_number].output_num[color_number] = chan_nr;
  923. return 0;
  924. }
  925. static int lp55xx_parse_multi_led(struct device_node *np,
  926. struct lp55xx_led_config *cfg,
  927. int child_number)
  928. {
  929. int num_colors = 0, ret;
  930. for_each_available_child_of_node_scoped(np, child) {
  931. ret = lp55xx_parse_multi_led_child(child, cfg, child_number,
  932. num_colors);
  933. if (ret)
  934. return ret;
  935. num_colors++;
  936. }
  937. cfg[child_number].num_colors = num_colors;
  938. return 0;
  939. }
  940. static int lp55xx_parse_logical_led(struct device_node *np,
  941. struct lp55xx_led_config *cfg,
  942. int child_number)
  943. {
  944. int led_color, ret;
  945. int chan_nr = 0;
  946. cfg[child_number].default_trigger =
  947. of_get_property(np, "linux,default-trigger", NULL);
  948. ret = of_property_read_u32(np, "color", &led_color);
  949. if (ret)
  950. return ret;
  951. if (led_color == LED_COLOR_ID_RGB)
  952. return lp55xx_parse_multi_led(np, cfg, child_number);
  953. ret = lp55xx_parse_common_child(np, cfg, child_number, &chan_nr);
  954. if (ret < 0)
  955. return ret;
  956. cfg[child_number].chan_nr = chan_nr;
  957. return ret;
  958. }
  959. static struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev,
  960. struct device_node *np,
  961. struct lp55xx_chip *chip)
  962. {
  963. struct device_node *child;
  964. struct lp55xx_platform_data *pdata;
  965. struct lp55xx_led_config *cfg;
  966. int num_channels;
  967. int i = 0;
  968. int ret;
  969. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  970. if (!pdata)
  971. return ERR_PTR(-ENOMEM);
  972. num_channels = of_get_available_child_count(np);
  973. if (num_channels == 0) {
  974. dev_err(dev, "no LED channels\n");
  975. return ERR_PTR(-EINVAL);
  976. }
  977. cfg = devm_kcalloc(dev, num_channels, sizeof(*cfg), GFP_KERNEL);
  978. if (!cfg)
  979. return ERR_PTR(-ENOMEM);
  980. pdata->led_config = &cfg[0];
  981. pdata->num_channels = num_channels;
  982. cfg->max_channel = chip->cfg->max_channel;
  983. for_each_available_child_of_node(np, child) {
  984. ret = lp55xx_parse_logical_led(child, cfg, i);
  985. if (ret) {
  986. of_node_put(child);
  987. return ERR_PTR(-EINVAL);
  988. }
  989. i++;
  990. }
  991. if (of_property_read_u32(np, "ti,charge-pump-mode", &pdata->charge_pump_mode))
  992. pdata->charge_pump_mode = LP55XX_CP_AUTO;
  993. if (pdata->charge_pump_mode > LP55XX_CP_AUTO) {
  994. dev_err(dev, "invalid charge pump mode %d\n", pdata->charge_pump_mode);
  995. return ERR_PTR(-EINVAL);
  996. }
  997. of_property_read_string(np, "label", &pdata->label);
  998. of_property_read_u8(np, "clock-mode", &pdata->clock_mode);
  999. pdata->enable_gpiod = devm_gpiod_get_optional(dev, "enable",
  1000. GPIOD_ASIS);
  1001. if (IS_ERR(pdata->enable_gpiod))
  1002. return ERR_CAST(pdata->enable_gpiod);
  1003. /* LP8501 specific */
  1004. of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel);
  1005. return pdata;
  1006. }
  1007. int lp55xx_probe(struct i2c_client *client)
  1008. {
  1009. const struct i2c_device_id *id = i2c_client_get_device_id(client);
  1010. int program_length, ret;
  1011. struct lp55xx_chip *chip;
  1012. struct lp55xx_led *led;
  1013. struct lp55xx_platform_data *pdata = dev_get_platdata(&client->dev);
  1014. struct device_node *np = dev_of_node(&client->dev);
  1015. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  1016. if (!chip)
  1017. return -ENOMEM;
  1018. chip->cfg = i2c_get_match_data(client);
  1019. if (!pdata) {
  1020. if (np) {
  1021. pdata = lp55xx_of_populate_pdata(&client->dev, np,
  1022. chip);
  1023. if (IS_ERR(pdata))
  1024. return PTR_ERR(pdata);
  1025. } else {
  1026. dev_err(&client->dev, "no platform data\n");
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. /* Validate max program page */
  1031. program_length = LP55xx_BYTES_PER_PAGE;
  1032. if (chip->cfg->pages_per_engine)
  1033. program_length *= chip->cfg->pages_per_engine;
  1034. /* support a max of 128bytes */
  1035. if (program_length > LP55xx_MAX_PROGRAM_LENGTH) {
  1036. dev_err(&client->dev, "invalid pages_per_engine configured\n");
  1037. return -EINVAL;
  1038. }
  1039. led = devm_kcalloc(&client->dev,
  1040. pdata->num_channels, sizeof(*led), GFP_KERNEL);
  1041. if (!led)
  1042. return -ENOMEM;
  1043. chip->cl = client;
  1044. chip->pdata = pdata;
  1045. mutex_init(&chip->lock);
  1046. i2c_set_clientdata(client, led);
  1047. ret = lp55xx_init_device(chip);
  1048. if (ret)
  1049. goto err_init;
  1050. dev_info(&client->dev, "%s Programmable led chip found\n", id->name);
  1051. ret = lp55xx_register_leds(led, chip);
  1052. if (ret)
  1053. goto err_out;
  1054. ret = lp55xx_register_sysfs(chip);
  1055. if (ret) {
  1056. dev_err(&client->dev, "registering sysfs failed\n");
  1057. goto err_out;
  1058. }
  1059. return 0;
  1060. err_out:
  1061. lp55xx_deinit_device(chip);
  1062. err_init:
  1063. return ret;
  1064. }
  1065. EXPORT_SYMBOL_GPL(lp55xx_probe);
  1066. void lp55xx_remove(struct i2c_client *client)
  1067. {
  1068. struct lp55xx_led *led = i2c_get_clientdata(client);
  1069. struct lp55xx_chip *chip = led->chip;
  1070. lp55xx_stop_all_engine(chip);
  1071. lp55xx_unregister_sysfs(chip);
  1072. lp55xx_deinit_device(chip);
  1073. }
  1074. EXPORT_SYMBOL_GPL(lp55xx_remove);
  1075. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  1076. MODULE_DESCRIPTION("LP55xx Common Driver");
  1077. MODULE_LICENSE("GPL");