892-leds-Add-LED1202-I2C-driver.patch 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. From: Vicentiu Galanopulo <[email protected]>
  2. To: Pavel Machek <[email protected]>, Lee Jones <[email protected]>,
  3. Rob Herring <[email protected]>,
  4. Krzysztof Kozlowski <[email protected]>,
  5. Conor Dooley <[email protected]>,
  6. Jonathan Corbet <[email protected]>,
  7. Vicentiu Galanopulo <[email protected]>,
  8. [email protected], [email protected],
  9. [email protected], [email protected]
  10. Subject: [PATCH v11 3/3] leds: Add LED1202 I2C driver
  11. Date: Wed, 18 Dec 2024 18:33:59 +0000 [thread overview]
  12. Message-ID: <[email protected]> (raw)
  13. In-Reply-To: <[email protected]>
  14. The output current can be adjusted separately for each channel by 8-bit
  15. analog (current sink input) and 12-bit digital (PWM) dimming control. The
  16. LED1202 implements 12 low-side current generators with independent dimming
  17. control.
  18. Internal volatile memory allows the user to store up to 8 different patterns,
  19. each pattern is a particular output configuration in terms of PWM
  20. duty-cycle (on 4096 steps). Analog dimming (on 256 steps) is per channel but
  21. common to all patterns. Each device tree LED node will have a corresponding
  22. entry in /sys/class/leds with the label name. The brightness property
  23. corresponds to the per channel analog dimming, while the patterns[1-8] to the
  24. PWM dimming control.
  25. Signed-off-by: Vicentiu Galanopulo <[email protected]>
  26. ---
  27. Changes in v10:
  28. - update description help in Kconfig
  29. - move st1202_led and st1202_chip into one line, declaration and definition
  30. Changes in v9:
  31. - log errors directly in st1202_write_reg and st1202_read_reg
  32. - use mutex guards instead of lock/unlock
  33. - remove i2c_set_clientdata
  34. Changes in v7:
  35. - fix st1202_brightness_get() error: uninitialized symbol 'value'
  36. Changes in v6:
  37. - fix build error
  38. Changes in v5:
  39. - remove unused macros
  40. - switch to using devm_led_classdev_register_ext (struct st1202_led update)
  41. - add prescalar_to_milliseconds (convert [22..5660]ms to [0..255] reg value)
  42. - remove register range check in dt_init (range protected by yaml)
  43. - address all review comments in v4
  44. Changes in v4:
  45. - Remove attributes/extended attributes implementation
  46. - Use /sys/class/leds/<led>/hw_pattern (Pavel suggestion)
  47. - Implement review findings of Christophe JAILLET
  48. Changes in v3:
  49. - Rename all ll1202 to st1202, including driver file name
  50. - Convert all magic numbers to defines
  51. - Refactor the show/store callbacks as per Lee's and Thomas's review
  52. - Remove ll1202_get_channel and use dev_ext_attributes instead
  53. - Log all error values for all the functions
  54. - Use sysfs_emit for show callbacks
  55. Changes in v2:
  56. - Fix build error for device_attribute modes
  57. drivers/leds/Kconfig | 10 +
  58. drivers/leds/Makefile | 1 +
  59. drivers/leds/leds-st1202.c | 416 +++++++++++++++++++++++++++++++++++++
  60. 3 files changed, 427 insertions(+)
  61. create mode 100644 drivers/leds/leds-st1202.c
  62. --- a/drivers/leds/Kconfig
  63. +++ b/drivers/leds/Kconfig
  64. @@ -865,6 +865,16 @@ config LEDS_LM36274
  65. Say Y to enable the LM36274 LED driver for TI LMU devices.
  66. This supports the LED device LM36274.
  67. +config LEDS_ST1202
  68. + tristate "LED Support for STMicroelectronics LED1202 I2C chips"
  69. + depends on LEDS_CLASS
  70. + depends on I2C
  71. + depends on OF
  72. + select LEDS_TRIGGERS
  73. + help
  74. + Say Y to enable support for LEDs connected to LED1202
  75. + LED driver chips accessed via the I2C bus.
  76. +
  77. config LEDS_TPS6105X
  78. tristate "LED support for TI TPS6105X"
  79. depends on LEDS_CLASS
  80. --- a/drivers/leds/Makefile
  81. +++ b/drivers/leds/Makefile
  82. @@ -78,6 +78,7 @@ obj-$(CONFIG_LEDS_POWERNV) += leds-powe
  83. obj-$(CONFIG_LEDS_PWM) += leds-pwm.o
  84. obj-$(CONFIG_LEDS_REGULATOR) += leds-regulator.o
  85. obj-$(CONFIG_LEDS_SC27XX_BLTC) += leds-sc27xx-bltc.o
  86. +obj-$(CONFIG_LEDS_ST1202) += leds-st1202.o
  87. obj-$(CONFIG_LEDS_SUNFIRE) += leds-sunfire.o
  88. obj-$(CONFIG_LEDS_SYSCON) += leds-syscon.o
  89. obj-$(CONFIG_LEDS_TCA6507) += leds-tca6507.o
  90. --- /dev/null
  91. +++ b/drivers/leds/leds-st1202.c
  92. @@ -0,0 +1,416 @@
  93. +// SPDX-License-Identifier: GPL-2.0-only
  94. +/*
  95. + * LED driver for STMicroelectronics LED1202 chip
  96. + *
  97. + * Copyright (C) 2024 Remote-Tech Ltd. UK
  98. + */
  99. +
  100. +#include <linux/cleanup.h>
  101. +#include <linux/ctype.h>
  102. +#include <linux/delay.h>
  103. +#include <linux/err.h>
  104. +#include <linux/gpio.h>
  105. +#include <linux/i2c.h>
  106. +#include <linux/leds.h>
  107. +#include <linux/module.h>
  108. +#include <linux/slab.h>
  109. +#include <linux/string.h>
  110. +
  111. +#define ST1202_CHAN_DISABLE_ALL 0x00
  112. +#define ST1202_CHAN_ENABLE_HIGH 0x03
  113. +#define ST1202_CHAN_ENABLE_LOW 0x02
  114. +#define ST1202_CONFIG_REG 0x04
  115. +/* PATS: Pattern sequence feature enable */
  116. +#define ST1202_CONFIG_REG_PATS BIT(7)
  117. +/* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
  118. +#define ST1202_CONFIG_REG_PATSR BIT(6)
  119. +#define ST1202_CONFIG_REG_SHFT BIT(3)
  120. +#define ST1202_DEV_ENABLE 0x01
  121. +#define ST1202_DEV_ENABLE_ON BIT(0)
  122. +#define ST1202_DEV_ENABLE_RESET BIT(7)
  123. +#define ST1202_DEVICE_ID 0x00
  124. +#define ST1202_ILED_REG0 0x09
  125. +#define ST1202_MAX_LEDS 12
  126. +#define ST1202_MAX_PATTERNS 8
  127. +#define ST1202_MILLIS_PATTERN_DUR_MAX 5660
  128. +#define ST1202_MILLIS_PATTERN_DUR_MIN 22
  129. +#define ST1202_PATTERN_DUR 0x16
  130. +#define ST1202_PATTERN_PWM 0x1E
  131. +#define ST1202_PATTERN_REP 0x15
  132. +
  133. +struct st1202_led {
  134. + struct fwnode_handle *fwnode;
  135. + struct led_classdev led_cdev;
  136. + struct st1202_chip *chip;
  137. + bool is_active;
  138. + int led_num;
  139. +};
  140. +
  141. +struct st1202_chip {
  142. + struct i2c_client *client;
  143. + struct mutex lock;
  144. + struct st1202_led leds[ST1202_MAX_LEDS];
  145. +};
  146. +
  147. +static struct st1202_led *cdev_to_st1202_led(struct led_classdev *cdev)
  148. +{
  149. + return container_of(cdev, struct st1202_led, led_cdev);
  150. +}
  151. +
  152. +static int st1202_read_reg(struct st1202_chip *chip, int reg, uint8_t *val)
  153. +{
  154. + struct device *dev = &chip->client->dev;
  155. + int ret;
  156. +
  157. + ret = i2c_smbus_read_byte_data(chip->client, reg);
  158. + if (ret < 0) {
  159. + dev_err(dev, "Failed to read register [0x%x]: %d\n", reg, ret);
  160. + return ret;
  161. + }
  162. +
  163. + *val = (uint8_t)ret;
  164. + return 0;
  165. +}
  166. +
  167. +static int st1202_write_reg(struct st1202_chip *chip, int reg, uint8_t val)
  168. +{
  169. + struct device *dev = &chip->client->dev;
  170. + int ret;
  171. +
  172. + ret = i2c_smbus_write_byte_data(chip->client, reg, val);
  173. + if (ret != 0)
  174. + dev_err(dev, "Failed to write %d to register [0x%x]: %d\n", val, reg, ret);
  175. +
  176. + return ret;
  177. +}
  178. +
  179. +static uint8_t st1202_prescalar_to_miliseconds(unsigned int value)
  180. +{
  181. + return value / ST1202_MILLIS_PATTERN_DUR_MIN - 1;
  182. +}
  183. +
  184. +static int st1202_pwm_pattern_write(struct st1202_chip *chip, int led_num,
  185. + int pattern, unsigned int value)
  186. +{
  187. + u8 value_l, value_h;
  188. + int ret;
  189. +
  190. + value_l = (u8)value;
  191. + value_h = (u8)(value >> 8);
  192. +
  193. + /*
  194. + * Datasheet: Register address low = 1Eh + 2*(xh) + 18h*(yh),
  195. + * where x is the channel number (led number) in hexadecimal (x = 00h .. 0Bh)
  196. + * and y is the pattern number in hexadecimal (y = 00h .. 07h)
  197. + */
  198. + ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + (led_num * 2) + 0x18 * pattern),
  199. + value_l);
  200. + if (ret != 0)
  201. + return ret;
  202. +
  203. + /*
  204. + * Datasheet: Register address high = 1Eh + 01h + 2(xh) +18h*(yh),
  205. + * where x is the channel number in hexadecimal (x = 00h .. 0Bh)
  206. + * and y is the pattern number in hexadecimal (y = 00h .. 07h)
  207. + */
  208. + ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + 0x1 + (led_num * 2) + 0x18 * pattern),
  209. + value_h);
  210. + if (ret != 0)
  211. + return ret;
  212. +
  213. + return 0;
  214. +}
  215. +
  216. +static int st1202_duration_pattern_write(struct st1202_chip *chip, int pattern,
  217. + unsigned int value)
  218. +{
  219. + return st1202_write_reg(chip, (ST1202_PATTERN_DUR + pattern),
  220. + st1202_prescalar_to_miliseconds(value));
  221. +}
  222. +
  223. +static void st1202_brightness_set(struct led_classdev *led_cdev,
  224. + enum led_brightness value)
  225. +{
  226. + struct st1202_led *led = cdev_to_st1202_led(led_cdev);
  227. + struct st1202_chip *chip = led->chip;
  228. +
  229. + guard(mutex)(&chip->lock);
  230. +
  231. + st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, value);
  232. +}
  233. +
  234. +static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
  235. +{
  236. + struct st1202_led *led = cdev_to_st1202_led(led_cdev);
  237. + struct st1202_chip *chip = led->chip;
  238. + u8 value = 0;
  239. +
  240. + guard(mutex)(&chip->lock);
  241. +
  242. + st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
  243. +
  244. + return value;
  245. +}
  246. +
  247. +static int st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
  248. +{
  249. + u8 chan_low, chan_high;
  250. + int ret;
  251. +
  252. + guard(mutex)(&chip->lock);
  253. +
  254. + if (led_num <= 7) {
  255. + ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
  256. + if (ret < 0)
  257. + return ret;
  258. +
  259. + chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
  260. +
  261. + ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
  262. + if (ret < 0)
  263. + return ret;
  264. +
  265. + } else {
  266. + ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
  267. + if (ret < 0)
  268. + return ret;
  269. +
  270. + chan_high = active ? chan_high | (BIT(led_num) >> 8) :
  271. + chan_high & ~(BIT(led_num) >> 8);
  272. +
  273. + ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
  274. + if (ret < 0)
  275. + return ret;
  276. + }
  277. +
  278. + return 0;
  279. +}
  280. +
  281. +static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
  282. +{
  283. + struct st1202_led *led = cdev_to_st1202_led(ldev);
  284. + struct st1202_chip *chip = led->chip;
  285. +
  286. + return st1202_channel_set(chip, led->led_num, value == LED_OFF ? false : true);
  287. +}
  288. +
  289. +static int st1202_led_pattern_clear(struct led_classdev *ldev)
  290. +{
  291. + struct st1202_led *led = cdev_to_st1202_led(ldev);
  292. + struct st1202_chip *chip = led->chip;
  293. + int ret;
  294. +
  295. + guard(mutex)(&chip->lock);
  296. +
  297. + for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++) {
  298. + ret = st1202_pwm_pattern_write(chip, led->led_num, patt, LED_OFF);
  299. + if (ret != 0)
  300. + return ret;
  301. +
  302. + ret = st1202_duration_pattern_write(chip, patt, ST1202_MILLIS_PATTERN_DUR_MIN);
  303. + if (ret != 0)
  304. + return ret;
  305. + }
  306. +
  307. + return 0;
  308. +}
  309. +
  310. +static int st1202_led_pattern_set(struct led_classdev *ldev,
  311. + struct led_pattern *pattern,
  312. + u32 len, int repeat)
  313. +{
  314. + struct st1202_led *led = cdev_to_st1202_led(ldev);
  315. + struct st1202_chip *chip = led->chip;
  316. + int ret;
  317. +
  318. + if (len > ST1202_MAX_PATTERNS)
  319. + return -EINVAL;
  320. +
  321. + guard(mutex)(&chip->lock);
  322. +
  323. + for (int patt = 0; patt < len; patt++) {
  324. + if (pattern[patt].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
  325. + pattern[patt].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
  326. + return -EINVAL;
  327. +
  328. + ret = st1202_pwm_pattern_write(chip, led->led_num, patt, pattern[patt].brightness);
  329. + if (ret != 0)
  330. + return ret;
  331. +
  332. + ret = st1202_duration_pattern_write(chip, patt, pattern[patt].delta_t);
  333. + if (ret != 0)
  334. + return ret;
  335. + }
  336. +
  337. + ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
  338. + if (ret != 0)
  339. + return ret;
  340. +
  341. + ret = st1202_write_reg(chip, ST1202_CONFIG_REG, (ST1202_CONFIG_REG_PATSR |
  342. + ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_SHFT));
  343. + if (ret != 0)
  344. + return ret;
  345. +
  346. + return 0;
  347. +}
  348. +
  349. +static int st1202_dt_init(struct st1202_chip *chip)
  350. +{
  351. + struct device *dev = &chip->client->dev;
  352. + struct st1202_led *led;
  353. + int err, reg;
  354. +
  355. + for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
  356. + struct led_init_data init_data = {};
  357. +
  358. + err = of_property_read_u32(child, "reg", &reg);
  359. + if (err)
  360. + return dev_err_probe(dev, err, "Invalid register\n");
  361. +
  362. + led = &chip->leds[reg];
  363. + led->is_active = true;
  364. + led->fwnode = of_fwnode_handle(child);
  365. +
  366. + led->led_cdev.max_brightness = U8_MAX;
  367. + led->led_cdev.brightness_set_blocking = st1202_led_set;
  368. + led->led_cdev.pattern_set = st1202_led_pattern_set;
  369. + led->led_cdev.pattern_clear = st1202_led_pattern_clear;
  370. + led->led_cdev.default_trigger = "pattern";
  371. +
  372. + init_data.fwnode = led->fwnode;
  373. + init_data.devicename = "st1202";
  374. + init_data.default_label = ":";
  375. +
  376. + err = devm_led_classdev_register_ext(dev, &led->led_cdev, &init_data);
  377. + if (err < 0)
  378. + return dev_err_probe(dev, err, "Failed to register LED class device\n");
  379. +
  380. + led->led_cdev.brightness_set = st1202_brightness_set;
  381. + led->led_cdev.brightness_get = st1202_brightness_get;
  382. + }
  383. +
  384. + return 0;
  385. +}
  386. +
  387. +static int st1202_setup(struct st1202_chip *chip)
  388. +{
  389. + int ret;
  390. +
  391. + guard(mutex)(&chip->lock);
  392. +
  393. + /*
  394. + * Once the supply voltage is applied, the LED1202 executes some internal checks,
  395. + * afterwords it stops the oscillator and puts the internal LDO in quiescent mode.
  396. + * To start the device, EN bit must be set inside the “Device Enable” register at
  397. + * address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
  398. + * from the internal non-volatile memory and performs an auto-calibration procedure
  399. + * in order to increase the output current precision.
  400. + * Such initialization lasts about 6.5 ms.
  401. + */
  402. +
  403. + /* Reset the chip during setup */
  404. + ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
  405. + if (ret < 0)
  406. + return ret;
  407. +
  408. + /* Enable phase-shift delay feature */
  409. + ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
  410. + if (ret < 0)
  411. + return ret;
  412. +
  413. + /* Enable the device */
  414. + ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
  415. + if (ret < 0)
  416. + return ret;
  417. +
  418. + /* Duration of initialization */
  419. + usleep_range(6500, 10000);
  420. +
  421. + /* Deactivate all LEDS (channels) and activate only the ones found in Device Tree */
  422. + ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
  423. + if (ret < 0)
  424. + return ret;
  425. +
  426. + ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
  427. + if (ret < 0)
  428. + return ret;
  429. +
  430. + ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
  431. + ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_PATSR);
  432. + if (ret < 0)
  433. + return ret;
  434. +
  435. + return 0;
  436. +}
  437. +
  438. +static int st1202_probe(struct i2c_client *client)
  439. +{
  440. + struct st1202_chip *chip;
  441. + struct st1202_led *led;
  442. + int ret;
  443. +
  444. + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  445. + return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
  446. +
  447. + chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  448. + if (!chip)
  449. + return -ENOMEM;
  450. +
  451. + devm_mutex_init(&client->dev, &chip->lock);
  452. + chip->client = client;
  453. +
  454. + ret = st1202_dt_init(chip);
  455. + if (ret < 0)
  456. + return ret;
  457. +
  458. + ret = st1202_setup(chip);
  459. + if (ret < 0)
  460. + return ret;
  461. +
  462. + for (int i = 0; i < ST1202_MAX_LEDS; i++) {
  463. + led = &chip->leds[i];
  464. + led->chip = chip;
  465. + led->led_num = i;
  466. +
  467. + if (!led->is_active)
  468. + continue;
  469. +
  470. + ret = st1202_channel_set(led->chip, led->led_num, true);
  471. + if (ret < 0)
  472. + return dev_err_probe(&client->dev, ret,
  473. + "Failed to activate LED channel\n");
  474. +
  475. + ret = st1202_led_pattern_clear(&led->led_cdev);
  476. + if (ret < 0)
  477. + return dev_err_probe(&client->dev, ret,
  478. + "Failed to clear LED pattern\n");
  479. + }
  480. +
  481. + return 0;
  482. +}
  483. +
  484. +static const struct i2c_device_id st1202_id[] = {
  485. + { "st1202-i2c" },
  486. + { /* sentinel */ }
  487. +};
  488. +MODULE_DEVICE_TABLE(i2c, st1202_id);
  489. +
  490. +static const struct of_device_id st1202_dt_ids[] = {
  491. + { .compatible = "st,led1202" },
  492. + { /* sentinel */ }
  493. +};
  494. +MODULE_DEVICE_TABLE(of, st1202_dt_ids);
  495. +
  496. +static struct i2c_driver st1202_driver = {
  497. + .driver = {
  498. + .name = "leds-st1202",
  499. + .of_match_table = of_match_ptr(st1202_dt_ids),
  500. + },
  501. + .probe = st1202_probe,
  502. + .id_table = st1202_id,
  503. +};
  504. +module_i2c_driver(st1202_driver);
  505. +
  506. +MODULE_AUTHOR("Remote Tech LTD");
  507. +MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
  508. +MODULE_LICENSE("GPL");