leds-tca6507.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * leds-tca6507
  4. *
  5. * The TCA6507 is a programmable LED controller that can drive 7
  6. * separate lines either by holding them low, or by pulsing them
  7. * with modulated width.
  8. * The modulation can be varied in a simple pattern to produce a
  9. * blink or double-blink.
  10. *
  11. * This driver can configure each line either as a 'GPIO' which is
  12. * out-only (pull-up resistor required) or as an LED with variable
  13. * brightness and hardware-assisted blinking.
  14. *
  15. * Apart from OFF and ON there are three programmable brightness
  16. * levels which can be programmed from 0 to 15 and indicate how many
  17. * 500usec intervals in each 8msec that the led is 'on'. The levels
  18. * are named MASTER, BANK0 and BANK1.
  19. *
  20. * There are two different blink rates that can be programmed, each
  21. * with separate time for rise, on, fall, off and second-off. Thus if
  22. * 3 or more different non-trivial rates are required, software must
  23. * be used for the extra rates. The two different blink rates must
  24. * align with the two levels BANK0 and BANK1. This driver does not
  25. * support double-blink so 'second-off' always matches 'off'.
  26. *
  27. * Only 16 different times can be programmed in a roughly logarithmic
  28. * scale from 64ms to 16320ms. To be precise the possible times are:
  29. * 0, 64, 128, 192, 256, 384, 512, 768,
  30. * 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
  31. *
  32. * Times that cannot be closely matched with these must be handled in
  33. * software. This driver allows 12.5% error in matching.
  34. *
  35. * This driver does not allow rise/fall rates to be set explicitly.
  36. * When trying to match a given 'on' or 'off' period, an appropriate
  37. * pair of 'change' and 'hold' times are chosen to get a close match.
  38. * If the target delay is even, the 'change' number will be the
  39. * smaller; if odd, the 'hold' number will be the smaller.
  40. * Choosing pairs of delays with 12.5% errors allows us to match
  41. * delays in the ranges: 56-72, 112-144, 168-216, 224-27504,
  42. * 28560-36720.
  43. * 26% of the achievable sums can be matched by multiple pairings.
  44. * For example 1536 == 1536+0, 1024+512, or 768+768.
  45. * This driver will always choose the pairing with the least
  46. * maximum - 768+768 in this case. Other pairings are not available.
  47. *
  48. * Access to the 3 levels and 2 blinks are on a first-come,
  49. * first-served basis. Access can be shared by multiple leds if they
  50. * have the same level and either same blink rates, or some don't
  51. * blink. When a led changes, it relinquishes access and tries again,
  52. * so it might lose access to hardware blink.
  53. *
  54. * If a blink engine cannot be allocated, software blink is used. If
  55. * the desired brightness cannot be allocated, the closest available
  56. * non-zero brightness is used. As 'full' is always available, the
  57. * worst case would be to have two different blink rates at '1', with
  58. * Max at '2', then other leds will have to choose between '2' and
  59. * '16'. Hopefully this is not likely.
  60. *
  61. * Each bank (BANK0 and BANK1) has two usage counts - LEDs using the
  62. * brightness and LEDs using the blink. It can only be reprogrammed
  63. * when the appropriate counter is zero. The MASTER level has a
  64. * single usage count.
  65. *
  66. * Each LED has programmable 'on' and 'off' time as milliseconds.
  67. * With each there is a flag saying if it was explicitly requested or
  68. * defaulted. Similarly the banks know if each time was explicit or a
  69. * default. Defaults are permitted to be changed freely - they are
  70. * not recognised when matching.
  71. */
  72. #include <linux/module.h>
  73. #include <linux/slab.h>
  74. #include <linux/leds.h>
  75. #include <linux/err.h>
  76. #include <linux/i2c.h>
  77. #include <linux/gpio/driver.h>
  78. #include <linux/property.h>
  79. #include <linux/workqueue.h>
  80. /* LED select registers determine the source that drives LED outputs */
  81. #define TCA6507_LS_LED_OFF 0x0 /* Output HI-Z (off) */
  82. #define TCA6507_LS_LED_OFF1 0x1 /* Output HI-Z (off) - not used */
  83. #define TCA6507_LS_LED_PWM0 0x2 /* Output LOW with Bank0 rate */
  84. #define TCA6507_LS_LED_PWM1 0x3 /* Output LOW with Bank1 rate */
  85. #define TCA6507_LS_LED_ON 0x4 /* Output LOW (on) */
  86. #define TCA6507_LS_LED_MIR 0x5 /* Output LOW with Master Intensity */
  87. #define TCA6507_LS_BLINK0 0x6 /* Blink at Bank0 rate */
  88. #define TCA6507_LS_BLINK1 0x7 /* Blink at Bank1 rate */
  89. struct tca6507_platform_data {
  90. struct led_platform_data leds;
  91. };
  92. #define TCA6507_MAKE_GPIO 1
  93. enum {
  94. BANK0,
  95. BANK1,
  96. MASTER,
  97. };
  98. static int bank_source[3] = {
  99. TCA6507_LS_LED_PWM0,
  100. TCA6507_LS_LED_PWM1,
  101. TCA6507_LS_LED_MIR,
  102. };
  103. static int blink_source[2] = {
  104. TCA6507_LS_BLINK0,
  105. TCA6507_LS_BLINK1,
  106. };
  107. /* PWM registers */
  108. #define TCA6507_REG_CNT 11
  109. /*
  110. * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
  111. * owns one bit in each register
  112. */
  113. #define TCA6507_FADE_ON 0x03
  114. #define TCA6507_FULL_ON 0x04
  115. #define TCA6507_FADE_OFF 0x05
  116. #define TCA6507_FIRST_OFF 0x06
  117. #define TCA6507_SECOND_OFF 0x07
  118. #define TCA6507_MAX_INTENSITY 0x08
  119. #define TCA6507_MASTER_INTENSITY 0x09
  120. #define TCA6507_INITIALIZE 0x0A
  121. #define INIT_CODE 0x8
  122. #define TIMECODES 16
  123. static int time_codes[TIMECODES] = {
  124. 0, 64, 128, 192, 256, 384, 512, 768,
  125. 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
  126. };
  127. /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
  128. static inline int TO_LEVEL(int brightness)
  129. {
  130. return brightness >> 4;
  131. }
  132. /* ...and convert back */
  133. static inline int TO_BRIGHT(int level)
  134. {
  135. if (level)
  136. return (level << 4) | 0xf;
  137. return 0;
  138. }
  139. #define NUM_LEDS 7
  140. struct tca6507_chip {
  141. int reg_set; /* One bit per register where
  142. * a '1' means the register
  143. * should be written */
  144. u8 reg_file[TCA6507_REG_CNT];
  145. /* Bank 2 is Master Intensity and doesn't use times */
  146. struct bank {
  147. int level;
  148. int ontime, offtime;
  149. int on_dflt, off_dflt;
  150. int time_use, level_use;
  151. } bank[3];
  152. struct i2c_client *client;
  153. struct work_struct work;
  154. spinlock_t lock;
  155. struct tca6507_led {
  156. struct tca6507_chip *chip;
  157. struct led_classdev led_cdev;
  158. int num;
  159. int ontime, offtime;
  160. int on_dflt, off_dflt;
  161. int bank; /* Bank used, or -1 */
  162. int blink; /* Set if hardware-blinking */
  163. } leds[NUM_LEDS];
  164. #ifdef CONFIG_GPIOLIB
  165. struct gpio_chip gpio;
  166. int gpio_map[NUM_LEDS];
  167. #endif
  168. };
  169. static const struct i2c_device_id tca6507_id[] = {
  170. { "tca6507" },
  171. { }
  172. };
  173. MODULE_DEVICE_TABLE(i2c, tca6507_id);
  174. static int choose_times(int msec, int *c1p, int *c2p)
  175. {
  176. /*
  177. * Choose two timecodes which add to 'msec' as near as
  178. * possible. The first returned is the 'on' or 'off' time.
  179. * The second is to be used as a 'fade-on' or 'fade-off' time.
  180. * If 'msec' is even, the first will not be smaller than the
  181. * second. If 'msec' is odd, the first will not be larger
  182. * than the second.
  183. * If we cannot get a sum within 1/8 of 'msec' fail with
  184. * -EINVAL, otherwise return the sum that was achieved, plus 1
  185. * if the first is smaller.
  186. * If two possibilities are equally good (e.g. 512+0,
  187. * 256+256), choose the first pair so there is more
  188. * change-time visible (i.e. it is softer).
  189. */
  190. int c1, c2;
  191. int tmax = msec * 9 / 8;
  192. int tmin = msec * 7 / 8;
  193. int diff = 65536;
  194. /* We start at '1' to ensure we never even think of choosing a
  195. * total time of '0'.
  196. */
  197. for (c1 = 1; c1 < TIMECODES; c1++) {
  198. int t = time_codes[c1];
  199. if (t*2 < tmin)
  200. continue;
  201. if (t > tmax)
  202. break;
  203. for (c2 = 0; c2 <= c1; c2++) {
  204. int tt = t + time_codes[c2];
  205. int d;
  206. if (tt < tmin)
  207. continue;
  208. if (tt > tmax)
  209. break;
  210. /* This works! */
  211. d = abs(msec - tt);
  212. if (d >= diff)
  213. continue;
  214. /* Best yet */
  215. *c1p = c1;
  216. *c2p = c2;
  217. diff = d;
  218. if (d == 0)
  219. return msec;
  220. }
  221. }
  222. if (diff < 65536) {
  223. int actual;
  224. if (msec & 1) {
  225. swap(*c2p, *c1p);
  226. }
  227. actual = time_codes[*c1p] + time_codes[*c2p];
  228. if (*c1p < *c2p)
  229. return actual + 1;
  230. else
  231. return actual;
  232. }
  233. /* No close match */
  234. return -EINVAL;
  235. }
  236. /*
  237. * Update the register file with the appropriate 3-bit state for the
  238. * given led.
  239. */
  240. static void set_select(struct tca6507_chip *tca, int led, int val)
  241. {
  242. int mask = (1 << led);
  243. int bit;
  244. for (bit = 0; bit < 3; bit++) {
  245. int n = tca->reg_file[bit] & ~mask;
  246. if (val & (1 << bit))
  247. n |= mask;
  248. if (tca->reg_file[bit] != n) {
  249. tca->reg_file[bit] = n;
  250. tca->reg_set |= (1 << bit);
  251. }
  252. }
  253. }
  254. /* Update the register file with the appropriate 4-bit code for one
  255. * bank or other. This can be used for timers, for levels, or for
  256. * initialization.
  257. */
  258. static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
  259. {
  260. int mask = 0xF;
  261. int n;
  262. if (bank) {
  263. mask <<= 4;
  264. new <<= 4;
  265. }
  266. n = tca->reg_file[reg] & ~mask;
  267. n |= new;
  268. if (tca->reg_file[reg] != n) {
  269. tca->reg_file[reg] = n;
  270. tca->reg_set |= 1 << reg;
  271. }
  272. }
  273. /* Update brightness level. */
  274. static void set_level(struct tca6507_chip *tca, int bank, int level)
  275. {
  276. switch (bank) {
  277. case BANK0:
  278. case BANK1:
  279. set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
  280. break;
  281. case MASTER:
  282. set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
  283. break;
  284. }
  285. tca->bank[bank].level = level;
  286. }
  287. /* Record all relevant time codes for a given bank */
  288. static void set_times(struct tca6507_chip *tca, int bank)
  289. {
  290. int c1, c2;
  291. int result;
  292. result = choose_times(tca->bank[bank].ontime, &c1, &c2);
  293. if (result < 0)
  294. return;
  295. dev_dbg(&tca->client->dev,
  296. "Chose on times %d(%d) %d(%d) for %dms\n",
  297. c1, time_codes[c1],
  298. c2, time_codes[c2], tca->bank[bank].ontime);
  299. set_code(tca, TCA6507_FADE_ON, bank, c2);
  300. set_code(tca, TCA6507_FULL_ON, bank, c1);
  301. tca->bank[bank].ontime = result;
  302. result = choose_times(tca->bank[bank].offtime, &c1, &c2);
  303. dev_dbg(&tca->client->dev,
  304. "Chose off times %d(%d) %d(%d) for %dms\n",
  305. c1, time_codes[c1],
  306. c2, time_codes[c2], tca->bank[bank].offtime);
  307. set_code(tca, TCA6507_FADE_OFF, bank, c2);
  308. set_code(tca, TCA6507_FIRST_OFF, bank, c1);
  309. set_code(tca, TCA6507_SECOND_OFF, bank, c1);
  310. tca->bank[bank].offtime = result;
  311. set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
  312. }
  313. /* Write all needed register of tca6507 */
  314. static void tca6507_work(struct work_struct *work)
  315. {
  316. struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
  317. work);
  318. struct i2c_client *cl = tca->client;
  319. int set;
  320. u8 file[TCA6507_REG_CNT];
  321. int r;
  322. spin_lock_irq(&tca->lock);
  323. set = tca->reg_set;
  324. memcpy(file, tca->reg_file, TCA6507_REG_CNT);
  325. tca->reg_set = 0;
  326. spin_unlock_irq(&tca->lock);
  327. for (r = 0; r < TCA6507_REG_CNT; r++)
  328. if (set & (1<<r))
  329. i2c_smbus_write_byte_data(cl, r, file[r]);
  330. }
  331. static void led_release(struct tca6507_led *led)
  332. {
  333. /* If led owns any resource, release it. */
  334. struct tca6507_chip *tca = led->chip;
  335. if (led->bank >= 0) {
  336. struct bank *b = tca->bank + led->bank;
  337. if (led->blink)
  338. b->time_use--;
  339. b->level_use--;
  340. }
  341. led->blink = 0;
  342. led->bank = -1;
  343. }
  344. static int led_prepare(struct tca6507_led *led)
  345. {
  346. /* Assign this led to a bank, configuring that bank if
  347. * necessary. */
  348. int level = TO_LEVEL(led->led_cdev.brightness);
  349. struct tca6507_chip *tca = led->chip;
  350. int c1, c2;
  351. int i;
  352. struct bank *b;
  353. int need_init = 0;
  354. led->led_cdev.brightness = TO_BRIGHT(level);
  355. if (level == 0) {
  356. set_select(tca, led->num, TCA6507_LS_LED_OFF);
  357. return 0;
  358. }
  359. if (led->ontime == 0 || led->offtime == 0) {
  360. /*
  361. * Just set the brightness, choosing first usable
  362. * bank. If none perfect, choose best. Count
  363. * backwards so we check MASTER bank first to avoid
  364. * wasting a timer.
  365. */
  366. int best = -1;/* full-on */
  367. int diff = 15-level;
  368. if (level == 15) {
  369. set_select(tca, led->num, TCA6507_LS_LED_ON);
  370. return 0;
  371. }
  372. for (i = MASTER; i >= BANK0; i--) {
  373. int d;
  374. if (tca->bank[i].level == level ||
  375. tca->bank[i].level_use == 0) {
  376. best = i;
  377. break;
  378. }
  379. d = abs(level - tca->bank[i].level);
  380. if (d < diff) {
  381. diff = d;
  382. best = i;
  383. }
  384. }
  385. if (best == -1) {
  386. /* Best brightness is full-on */
  387. set_select(tca, led->num, TCA6507_LS_LED_ON);
  388. led->led_cdev.brightness = LED_FULL;
  389. return 0;
  390. }
  391. if (!tca->bank[best].level_use)
  392. set_level(tca, best, level);
  393. tca->bank[best].level_use++;
  394. led->bank = best;
  395. set_select(tca, led->num, bank_source[best]);
  396. led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
  397. return 0;
  398. }
  399. /*
  400. * We have on/off time so we need to try to allocate a timing
  401. * bank. First check if times are compatible with hardware
  402. * and give up if not.
  403. */
  404. if (choose_times(led->ontime, &c1, &c2) < 0)
  405. return -EINVAL;
  406. if (choose_times(led->offtime, &c1, &c2) < 0)
  407. return -EINVAL;
  408. for (i = BANK0; i <= BANK1; i++) {
  409. if (tca->bank[i].level_use == 0)
  410. /* not in use - it is ours! */
  411. break;
  412. if (tca->bank[i].level != level)
  413. /* Incompatible level - skip */
  414. /* FIX: if timer matches we maybe should consider
  415. * this anyway...
  416. */
  417. continue;
  418. if (tca->bank[i].time_use == 0)
  419. /* Timer not in use, and level matches - use it */
  420. break;
  421. if (!(tca->bank[i].on_dflt ||
  422. led->on_dflt ||
  423. tca->bank[i].ontime == led->ontime))
  424. /* on time is incompatible */
  425. continue;
  426. if (!(tca->bank[i].off_dflt ||
  427. led->off_dflt ||
  428. tca->bank[i].offtime == led->offtime))
  429. /* off time is incompatible */
  430. continue;
  431. /* looks like a suitable match */
  432. break;
  433. }
  434. if (i > BANK1)
  435. /* Nothing matches - how sad */
  436. return -EINVAL;
  437. b = &tca->bank[i];
  438. if (b->level_use == 0)
  439. set_level(tca, i, level);
  440. b->level_use++;
  441. led->bank = i;
  442. if (b->on_dflt ||
  443. !led->on_dflt ||
  444. b->time_use == 0) {
  445. b->ontime = led->ontime;
  446. b->on_dflt = led->on_dflt;
  447. need_init = 1;
  448. }
  449. if (b->off_dflt ||
  450. !led->off_dflt ||
  451. b->time_use == 0) {
  452. b->offtime = led->offtime;
  453. b->off_dflt = led->off_dflt;
  454. need_init = 1;
  455. }
  456. if (need_init)
  457. set_times(tca, i);
  458. led->ontime = b->ontime;
  459. led->offtime = b->offtime;
  460. b->time_use++;
  461. led->blink = 1;
  462. led->led_cdev.brightness = TO_BRIGHT(b->level);
  463. set_select(tca, led->num, blink_source[i]);
  464. return 0;
  465. }
  466. static int led_assign(struct tca6507_led *led)
  467. {
  468. struct tca6507_chip *tca = led->chip;
  469. int err;
  470. unsigned long flags;
  471. spin_lock_irqsave(&tca->lock, flags);
  472. led_release(led);
  473. err = led_prepare(led);
  474. if (err) {
  475. /*
  476. * Can only fail on timer setup. In that case we need
  477. * to re-establish as steady level.
  478. */
  479. led->ontime = 0;
  480. led->offtime = 0;
  481. led_prepare(led);
  482. }
  483. spin_unlock_irqrestore(&tca->lock, flags);
  484. if (tca->reg_set)
  485. schedule_work(&tca->work);
  486. return err;
  487. }
  488. static void tca6507_brightness_set(struct led_classdev *led_cdev,
  489. enum led_brightness brightness)
  490. {
  491. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  492. led_cdev);
  493. led->led_cdev.brightness = brightness;
  494. led->ontime = 0;
  495. led->offtime = 0;
  496. led_assign(led);
  497. }
  498. static int tca6507_blink_set(struct led_classdev *led_cdev,
  499. unsigned long *delay_on,
  500. unsigned long *delay_off)
  501. {
  502. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  503. led_cdev);
  504. if (*delay_on == 0)
  505. led->on_dflt = 1;
  506. else if (delay_on != &led_cdev->blink_delay_on)
  507. led->on_dflt = 0;
  508. led->ontime = *delay_on;
  509. if (*delay_off == 0)
  510. led->off_dflt = 1;
  511. else if (delay_off != &led_cdev->blink_delay_off)
  512. led->off_dflt = 0;
  513. led->offtime = *delay_off;
  514. if (led->ontime == 0)
  515. led->ontime = 512;
  516. if (led->offtime == 0)
  517. led->offtime = 512;
  518. if (led->led_cdev.brightness == LED_OFF)
  519. led->led_cdev.brightness = LED_FULL;
  520. if (led_assign(led) < 0) {
  521. led->ontime = 0;
  522. led->offtime = 0;
  523. led->led_cdev.brightness = LED_OFF;
  524. return -EINVAL;
  525. }
  526. *delay_on = led->ontime;
  527. *delay_off = led->offtime;
  528. return 0;
  529. }
  530. #ifdef CONFIG_GPIOLIB
  531. static void tca6507_gpio_set_value(struct gpio_chip *gc,
  532. unsigned offset, int val)
  533. {
  534. struct tca6507_chip *tca = gpiochip_get_data(gc);
  535. unsigned long flags;
  536. spin_lock_irqsave(&tca->lock, flags);
  537. /*
  538. * 'OFF' is floating high, and 'ON' is pulled down, so it has
  539. * the inverse sense of 'val'.
  540. */
  541. set_select(tca, tca->gpio_map[offset],
  542. val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
  543. spin_unlock_irqrestore(&tca->lock, flags);
  544. if (tca->reg_set)
  545. schedule_work(&tca->work);
  546. }
  547. static int tca6507_gpio_direction_output(struct gpio_chip *gc,
  548. unsigned offset, int val)
  549. {
  550. tca6507_gpio_set_value(gc, offset, val);
  551. return 0;
  552. }
  553. static int tca6507_probe_gpios(struct device *dev,
  554. struct tca6507_chip *tca,
  555. struct tca6507_platform_data *pdata)
  556. {
  557. int err;
  558. int i = 0;
  559. int gpios = 0;
  560. for (i = 0; i < NUM_LEDS; i++)
  561. if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
  562. /* Configure as a gpio */
  563. tca->gpio_map[gpios] = i;
  564. gpios++;
  565. }
  566. if (!gpios)
  567. return 0;
  568. tca->gpio.label = "gpio-tca6507";
  569. tca->gpio.ngpio = gpios;
  570. tca->gpio.base = -1;
  571. tca->gpio.owner = THIS_MODULE;
  572. tca->gpio.direction_output = tca6507_gpio_direction_output;
  573. tca->gpio.set = tca6507_gpio_set_value;
  574. tca->gpio.parent = dev;
  575. err = devm_gpiochip_add_data(dev, &tca->gpio, tca);
  576. if (err) {
  577. tca->gpio.ngpio = 0;
  578. return err;
  579. }
  580. return 0;
  581. }
  582. #else /* CONFIG_GPIOLIB */
  583. static int tca6507_probe_gpios(struct device *dev,
  584. struct tca6507_chip *tca,
  585. struct tca6507_platform_data *pdata)
  586. {
  587. return 0;
  588. }
  589. #endif /* CONFIG_GPIOLIB */
  590. static struct tca6507_platform_data *
  591. tca6507_led_dt_init(struct device *dev)
  592. {
  593. struct tca6507_platform_data *pdata;
  594. struct fwnode_handle *child;
  595. struct led_info *tca_leds;
  596. int count;
  597. count = device_get_child_node_count(dev);
  598. if (!count || count > NUM_LEDS)
  599. return ERR_PTR(-ENODEV);
  600. tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
  601. GFP_KERNEL);
  602. if (!tca_leds)
  603. return ERR_PTR(-ENOMEM);
  604. device_for_each_child_node(dev, child) {
  605. struct led_info led;
  606. u32 reg;
  607. int ret;
  608. if (fwnode_property_read_string(child, "label", &led.name))
  609. led.name = fwnode_get_name(child);
  610. if (fwnode_property_read_string(child, "linux,default-trigger",
  611. &led.default_trigger))
  612. led.default_trigger = NULL;
  613. led.flags = 0;
  614. if (fwnode_device_is_compatible(child, "gpio"))
  615. led.flags |= TCA6507_MAKE_GPIO;
  616. ret = fwnode_property_read_u32(child, "reg", &reg);
  617. if (ret || reg >= NUM_LEDS) {
  618. fwnode_handle_put(child);
  619. return ERR_PTR(ret ? : -EINVAL);
  620. }
  621. tca_leds[reg] = led;
  622. }
  623. pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
  624. GFP_KERNEL);
  625. if (!pdata)
  626. return ERR_PTR(-ENOMEM);
  627. pdata->leds.leds = tca_leds;
  628. pdata->leds.num_leds = NUM_LEDS;
  629. return pdata;
  630. }
  631. static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
  632. { .compatible = "ti,tca6507", },
  633. {},
  634. };
  635. MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
  636. static int tca6507_probe(struct i2c_client *client)
  637. {
  638. struct device *dev = &client->dev;
  639. struct i2c_adapter *adapter;
  640. struct tca6507_chip *tca;
  641. struct tca6507_platform_data *pdata;
  642. int err;
  643. int i = 0;
  644. adapter = client->adapter;
  645. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  646. return -EIO;
  647. pdata = tca6507_led_dt_init(dev);
  648. if (IS_ERR(pdata)) {
  649. dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
  650. return PTR_ERR(pdata);
  651. }
  652. tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
  653. if (!tca)
  654. return -ENOMEM;
  655. tca->client = client;
  656. INIT_WORK(&tca->work, tca6507_work);
  657. spin_lock_init(&tca->lock);
  658. i2c_set_clientdata(client, tca);
  659. for (i = 0; i < NUM_LEDS; i++) {
  660. struct tca6507_led *l = tca->leds + i;
  661. l->chip = tca;
  662. l->num = i;
  663. if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
  664. l->led_cdev.name = pdata->leds.leds[i].name;
  665. l->led_cdev.default_trigger
  666. = pdata->leds.leds[i].default_trigger;
  667. l->led_cdev.brightness_set = tca6507_brightness_set;
  668. l->led_cdev.blink_set = tca6507_blink_set;
  669. l->bank = -1;
  670. err = devm_led_classdev_register(dev, &l->led_cdev);
  671. if (err < 0)
  672. return err;
  673. }
  674. }
  675. err = tca6507_probe_gpios(dev, tca, pdata);
  676. if (err)
  677. return err;
  678. /* set all registers to known state - zero */
  679. tca->reg_set = 0x7f;
  680. schedule_work(&tca->work);
  681. return 0;
  682. }
  683. static void tca6507_remove(struct i2c_client *client)
  684. {
  685. struct tca6507_chip *tca = i2c_get_clientdata(client);
  686. cancel_work_sync(&tca->work);
  687. }
  688. static struct i2c_driver tca6507_driver = {
  689. .driver = {
  690. .name = "leds-tca6507",
  691. .of_match_table = of_match_ptr(of_tca6507_leds_match),
  692. },
  693. .probe = tca6507_probe,
  694. .remove = tca6507_remove,
  695. .id_table = tca6507_id,
  696. };
  697. module_i2c_driver(tca6507_driver);
  698. MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
  699. MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
  700. MODULE_LICENSE("GPL v2");