leds-tca6507.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. #ifdef CONFIG_GPIOLIB
  92. int gpio_base;
  93. #endif
  94. };
  95. #define TCA6507_MAKE_GPIO 1
  96. enum {
  97. BANK0,
  98. BANK1,
  99. MASTER,
  100. };
  101. static int bank_source[3] = {
  102. TCA6507_LS_LED_PWM0,
  103. TCA6507_LS_LED_PWM1,
  104. TCA6507_LS_LED_MIR,
  105. };
  106. static int blink_source[2] = {
  107. TCA6507_LS_BLINK0,
  108. TCA6507_LS_BLINK1,
  109. };
  110. /* PWM registers */
  111. #define TCA6507_REG_CNT 11
  112. /*
  113. * 0x00, 0x01, 0x02 encode the TCA6507_LS_* values, each output
  114. * owns one bit in each register
  115. */
  116. #define TCA6507_FADE_ON 0x03
  117. #define TCA6507_FULL_ON 0x04
  118. #define TCA6507_FADE_OFF 0x05
  119. #define TCA6507_FIRST_OFF 0x06
  120. #define TCA6507_SECOND_OFF 0x07
  121. #define TCA6507_MAX_INTENSITY 0x08
  122. #define TCA6507_MASTER_INTENSITY 0x09
  123. #define TCA6507_INITIALIZE 0x0A
  124. #define INIT_CODE 0x8
  125. #define TIMECODES 16
  126. static int time_codes[TIMECODES] = {
  127. 0, 64, 128, 192, 256, 384, 512, 768,
  128. 1024, 1536, 2048, 3072, 4096, 5760, 8128, 16320
  129. };
  130. /* Convert an led.brightness level (0..255) to a TCA6507 level (0..15) */
  131. static inline int TO_LEVEL(int brightness)
  132. {
  133. return brightness >> 4;
  134. }
  135. /* ...and convert back */
  136. static inline int TO_BRIGHT(int level)
  137. {
  138. if (level)
  139. return (level << 4) | 0xf;
  140. return 0;
  141. }
  142. #define NUM_LEDS 7
  143. struct tca6507_chip {
  144. int reg_set; /* One bit per register where
  145. * a '1' means the register
  146. * should be written */
  147. u8 reg_file[TCA6507_REG_CNT];
  148. /* Bank 2 is Master Intensity and doesn't use times */
  149. struct bank {
  150. int level;
  151. int ontime, offtime;
  152. int on_dflt, off_dflt;
  153. int time_use, level_use;
  154. } bank[3];
  155. struct i2c_client *client;
  156. struct work_struct work;
  157. spinlock_t lock;
  158. struct tca6507_led {
  159. struct tca6507_chip *chip;
  160. struct led_classdev led_cdev;
  161. int num;
  162. int ontime, offtime;
  163. int on_dflt, off_dflt;
  164. int bank; /* Bank used, or -1 */
  165. int blink; /* Set if hardware-blinking */
  166. } leds[NUM_LEDS];
  167. #ifdef CONFIG_GPIOLIB
  168. struct gpio_chip gpio;
  169. int gpio_map[NUM_LEDS];
  170. #endif
  171. };
  172. static const struct i2c_device_id tca6507_id[] = {
  173. { "tca6507" },
  174. { }
  175. };
  176. MODULE_DEVICE_TABLE(i2c, tca6507_id);
  177. static int choose_times(int msec, int *c1p, int *c2p)
  178. {
  179. /*
  180. * Choose two timecodes which add to 'msec' as near as
  181. * possible. The first returned is the 'on' or 'off' time.
  182. * The second is to be used as a 'fade-on' or 'fade-off' time.
  183. * If 'msec' is even, the first will not be smaller than the
  184. * second. If 'msec' is odd, the first will not be larger
  185. * than the second.
  186. * If we cannot get a sum within 1/8 of 'msec' fail with
  187. * -EINVAL, otherwise return the sum that was achieved, plus 1
  188. * if the first is smaller.
  189. * If two possibilities are equally good (e.g. 512+0,
  190. * 256+256), choose the first pair so there is more
  191. * change-time visible (i.e. it is softer).
  192. */
  193. int c1, c2;
  194. int tmax = msec * 9 / 8;
  195. int tmin = msec * 7 / 8;
  196. int diff = 65536;
  197. /* We start at '1' to ensure we never even think of choosing a
  198. * total time of '0'.
  199. */
  200. for (c1 = 1; c1 < TIMECODES; c1++) {
  201. int t = time_codes[c1];
  202. if (t*2 < tmin)
  203. continue;
  204. if (t > tmax)
  205. break;
  206. for (c2 = 0; c2 <= c1; c2++) {
  207. int tt = t + time_codes[c2];
  208. int d;
  209. if (tt < tmin)
  210. continue;
  211. if (tt > tmax)
  212. break;
  213. /* This works! */
  214. d = abs(msec - tt);
  215. if (d >= diff)
  216. continue;
  217. /* Best yet */
  218. *c1p = c1;
  219. *c2p = c2;
  220. diff = d;
  221. if (d == 0)
  222. return msec;
  223. }
  224. }
  225. if (diff < 65536) {
  226. int actual;
  227. if (msec & 1) {
  228. swap(*c2p, *c1p);
  229. }
  230. actual = time_codes[*c1p] + time_codes[*c2p];
  231. if (*c1p < *c2p)
  232. return actual + 1;
  233. else
  234. return actual;
  235. }
  236. /* No close match */
  237. return -EINVAL;
  238. }
  239. /*
  240. * Update the register file with the appropriate 3-bit state for the
  241. * given led.
  242. */
  243. static void set_select(struct tca6507_chip *tca, int led, int val)
  244. {
  245. int mask = (1 << led);
  246. int bit;
  247. for (bit = 0; bit < 3; bit++) {
  248. int n = tca->reg_file[bit] & ~mask;
  249. if (val & (1 << bit))
  250. n |= mask;
  251. if (tca->reg_file[bit] != n) {
  252. tca->reg_file[bit] = n;
  253. tca->reg_set |= (1 << bit);
  254. }
  255. }
  256. }
  257. /* Update the register file with the appropriate 4-bit code for one
  258. * bank or other. This can be used for timers, for levels, or for
  259. * initialization.
  260. */
  261. static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
  262. {
  263. int mask = 0xF;
  264. int n;
  265. if (bank) {
  266. mask <<= 4;
  267. new <<= 4;
  268. }
  269. n = tca->reg_file[reg] & ~mask;
  270. n |= new;
  271. if (tca->reg_file[reg] != n) {
  272. tca->reg_file[reg] = n;
  273. tca->reg_set |= 1 << reg;
  274. }
  275. }
  276. /* Update brightness level. */
  277. static void set_level(struct tca6507_chip *tca, int bank, int level)
  278. {
  279. switch (bank) {
  280. case BANK0:
  281. case BANK1:
  282. set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
  283. break;
  284. case MASTER:
  285. set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
  286. break;
  287. }
  288. tca->bank[bank].level = level;
  289. }
  290. /* Record all relevant time codes for a given bank */
  291. static void set_times(struct tca6507_chip *tca, int bank)
  292. {
  293. int c1, c2;
  294. int result;
  295. result = choose_times(tca->bank[bank].ontime, &c1, &c2);
  296. if (result < 0)
  297. return;
  298. dev_dbg(&tca->client->dev,
  299. "Chose on times %d(%d) %d(%d) for %dms\n",
  300. c1, time_codes[c1],
  301. c2, time_codes[c2], tca->bank[bank].ontime);
  302. set_code(tca, TCA6507_FADE_ON, bank, c2);
  303. set_code(tca, TCA6507_FULL_ON, bank, c1);
  304. tca->bank[bank].ontime = result;
  305. result = choose_times(tca->bank[bank].offtime, &c1, &c2);
  306. dev_dbg(&tca->client->dev,
  307. "Chose off times %d(%d) %d(%d) for %dms\n",
  308. c1, time_codes[c1],
  309. c2, time_codes[c2], tca->bank[bank].offtime);
  310. set_code(tca, TCA6507_FADE_OFF, bank, c2);
  311. set_code(tca, TCA6507_FIRST_OFF, bank, c1);
  312. set_code(tca, TCA6507_SECOND_OFF, bank, c1);
  313. tca->bank[bank].offtime = result;
  314. set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
  315. }
  316. /* Write all needed register of tca6507 */
  317. static void tca6507_work(struct work_struct *work)
  318. {
  319. struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
  320. work);
  321. struct i2c_client *cl = tca->client;
  322. int set;
  323. u8 file[TCA6507_REG_CNT];
  324. int r;
  325. spin_lock_irq(&tca->lock);
  326. set = tca->reg_set;
  327. memcpy(file, tca->reg_file, TCA6507_REG_CNT);
  328. tca->reg_set = 0;
  329. spin_unlock_irq(&tca->lock);
  330. for (r = 0; r < TCA6507_REG_CNT; r++)
  331. if (set & (1<<r))
  332. i2c_smbus_write_byte_data(cl, r, file[r]);
  333. }
  334. static void led_release(struct tca6507_led *led)
  335. {
  336. /* If led owns any resource, release it. */
  337. struct tca6507_chip *tca = led->chip;
  338. if (led->bank >= 0) {
  339. struct bank *b = tca->bank + led->bank;
  340. if (led->blink)
  341. b->time_use--;
  342. b->level_use--;
  343. }
  344. led->blink = 0;
  345. led->bank = -1;
  346. }
  347. static int led_prepare(struct tca6507_led *led)
  348. {
  349. /* Assign this led to a bank, configuring that bank if
  350. * necessary. */
  351. int level = TO_LEVEL(led->led_cdev.brightness);
  352. struct tca6507_chip *tca = led->chip;
  353. int c1, c2;
  354. int i;
  355. struct bank *b;
  356. int need_init = 0;
  357. led->led_cdev.brightness = TO_BRIGHT(level);
  358. if (level == 0) {
  359. set_select(tca, led->num, TCA6507_LS_LED_OFF);
  360. return 0;
  361. }
  362. if (led->ontime == 0 || led->offtime == 0) {
  363. /*
  364. * Just set the brightness, choosing first usable
  365. * bank. If none perfect, choose best. Count
  366. * backwards so we check MASTER bank first to avoid
  367. * wasting a timer.
  368. */
  369. int best = -1;/* full-on */
  370. int diff = 15-level;
  371. if (level == 15) {
  372. set_select(tca, led->num, TCA6507_LS_LED_ON);
  373. return 0;
  374. }
  375. for (i = MASTER; i >= BANK0; i--) {
  376. int d;
  377. if (tca->bank[i].level == level ||
  378. tca->bank[i].level_use == 0) {
  379. best = i;
  380. break;
  381. }
  382. d = abs(level - tca->bank[i].level);
  383. if (d < diff) {
  384. diff = d;
  385. best = i;
  386. }
  387. }
  388. if (best == -1) {
  389. /* Best brightness is full-on */
  390. set_select(tca, led->num, TCA6507_LS_LED_ON);
  391. led->led_cdev.brightness = LED_FULL;
  392. return 0;
  393. }
  394. if (!tca->bank[best].level_use)
  395. set_level(tca, best, level);
  396. tca->bank[best].level_use++;
  397. led->bank = best;
  398. set_select(tca, led->num, bank_source[best]);
  399. led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
  400. return 0;
  401. }
  402. /*
  403. * We have on/off time so we need to try to allocate a timing
  404. * bank. First check if times are compatible with hardware
  405. * and give up if not.
  406. */
  407. if (choose_times(led->ontime, &c1, &c2) < 0)
  408. return -EINVAL;
  409. if (choose_times(led->offtime, &c1, &c2) < 0)
  410. return -EINVAL;
  411. for (i = BANK0; i <= BANK1; i++) {
  412. if (tca->bank[i].level_use == 0)
  413. /* not in use - it is ours! */
  414. break;
  415. if (tca->bank[i].level != level)
  416. /* Incompatible level - skip */
  417. /* FIX: if timer matches we maybe should consider
  418. * this anyway...
  419. */
  420. continue;
  421. if (tca->bank[i].time_use == 0)
  422. /* Timer not in use, and level matches - use it */
  423. break;
  424. if (!(tca->bank[i].on_dflt ||
  425. led->on_dflt ||
  426. tca->bank[i].ontime == led->ontime))
  427. /* on time is incompatible */
  428. continue;
  429. if (!(tca->bank[i].off_dflt ||
  430. led->off_dflt ||
  431. tca->bank[i].offtime == led->offtime))
  432. /* off time is incompatible */
  433. continue;
  434. /* looks like a suitable match */
  435. break;
  436. }
  437. if (i > BANK1)
  438. /* Nothing matches - how sad */
  439. return -EINVAL;
  440. b = &tca->bank[i];
  441. if (b->level_use == 0)
  442. set_level(tca, i, level);
  443. b->level_use++;
  444. led->bank = i;
  445. if (b->on_dflt ||
  446. !led->on_dflt ||
  447. b->time_use == 0) {
  448. b->ontime = led->ontime;
  449. b->on_dflt = led->on_dflt;
  450. need_init = 1;
  451. }
  452. if (b->off_dflt ||
  453. !led->off_dflt ||
  454. b->time_use == 0) {
  455. b->offtime = led->offtime;
  456. b->off_dflt = led->off_dflt;
  457. need_init = 1;
  458. }
  459. if (need_init)
  460. set_times(tca, i);
  461. led->ontime = b->ontime;
  462. led->offtime = b->offtime;
  463. b->time_use++;
  464. led->blink = 1;
  465. led->led_cdev.brightness = TO_BRIGHT(b->level);
  466. set_select(tca, led->num, blink_source[i]);
  467. return 0;
  468. }
  469. static int led_assign(struct tca6507_led *led)
  470. {
  471. struct tca6507_chip *tca = led->chip;
  472. int err;
  473. unsigned long flags;
  474. spin_lock_irqsave(&tca->lock, flags);
  475. led_release(led);
  476. err = led_prepare(led);
  477. if (err) {
  478. /*
  479. * Can only fail on timer setup. In that case we need
  480. * to re-establish as steady level.
  481. */
  482. led->ontime = 0;
  483. led->offtime = 0;
  484. led_prepare(led);
  485. }
  486. spin_unlock_irqrestore(&tca->lock, flags);
  487. if (tca->reg_set)
  488. schedule_work(&tca->work);
  489. return err;
  490. }
  491. static void tca6507_brightness_set(struct led_classdev *led_cdev,
  492. enum led_brightness brightness)
  493. {
  494. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  495. led_cdev);
  496. led->led_cdev.brightness = brightness;
  497. led->ontime = 0;
  498. led->offtime = 0;
  499. led_assign(led);
  500. }
  501. static int tca6507_blink_set(struct led_classdev *led_cdev,
  502. unsigned long *delay_on,
  503. unsigned long *delay_off)
  504. {
  505. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  506. led_cdev);
  507. if (*delay_on == 0)
  508. led->on_dflt = 1;
  509. else if (delay_on != &led_cdev->blink_delay_on)
  510. led->on_dflt = 0;
  511. led->ontime = *delay_on;
  512. if (*delay_off == 0)
  513. led->off_dflt = 1;
  514. else if (delay_off != &led_cdev->blink_delay_off)
  515. led->off_dflt = 0;
  516. led->offtime = *delay_off;
  517. if (led->ontime == 0)
  518. led->ontime = 512;
  519. if (led->offtime == 0)
  520. led->offtime = 512;
  521. if (led->led_cdev.brightness == LED_OFF)
  522. led->led_cdev.brightness = LED_FULL;
  523. if (led_assign(led) < 0) {
  524. led->ontime = 0;
  525. led->offtime = 0;
  526. led->led_cdev.brightness = LED_OFF;
  527. return -EINVAL;
  528. }
  529. *delay_on = led->ontime;
  530. *delay_off = led->offtime;
  531. return 0;
  532. }
  533. #ifdef CONFIG_GPIOLIB
  534. static void tca6507_gpio_set_value(struct gpio_chip *gc,
  535. unsigned offset, int val)
  536. {
  537. struct tca6507_chip *tca = gpiochip_get_data(gc);
  538. unsigned long flags;
  539. spin_lock_irqsave(&tca->lock, flags);
  540. /*
  541. * 'OFF' is floating high, and 'ON' is pulled down, so it has
  542. * the inverse sense of 'val'.
  543. */
  544. set_select(tca, tca->gpio_map[offset],
  545. val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
  546. spin_unlock_irqrestore(&tca->lock, flags);
  547. if (tca->reg_set)
  548. schedule_work(&tca->work);
  549. }
  550. static int tca6507_gpio_direction_output(struct gpio_chip *gc,
  551. unsigned offset, int val)
  552. {
  553. tca6507_gpio_set_value(gc, offset, val);
  554. return 0;
  555. }
  556. static int tca6507_probe_gpios(struct device *dev,
  557. struct tca6507_chip *tca,
  558. struct tca6507_platform_data *pdata)
  559. {
  560. int err;
  561. int i = 0;
  562. int gpios = 0;
  563. for (i = 0; i < NUM_LEDS; i++)
  564. if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
  565. /* Configure as a gpio */
  566. tca->gpio_map[gpios] = i;
  567. gpios++;
  568. }
  569. if (!gpios)
  570. return 0;
  571. tca->gpio.label = "gpio-tca6507";
  572. tca->gpio.ngpio = gpios;
  573. tca->gpio.base = pdata->gpio_base;
  574. tca->gpio.owner = THIS_MODULE;
  575. tca->gpio.direction_output = tca6507_gpio_direction_output;
  576. tca->gpio.set = tca6507_gpio_set_value;
  577. tca->gpio.parent = dev;
  578. err = gpiochip_add_data(&tca->gpio, tca);
  579. if (err) {
  580. tca->gpio.ngpio = 0;
  581. return err;
  582. }
  583. return 0;
  584. }
  585. static void tca6507_remove_gpio(struct tca6507_chip *tca)
  586. {
  587. if (tca->gpio.ngpio)
  588. gpiochip_remove(&tca->gpio);
  589. }
  590. #else /* CONFIG_GPIOLIB */
  591. static int tca6507_probe_gpios(struct device *dev,
  592. struct tca6507_chip *tca,
  593. struct tca6507_platform_data *pdata)
  594. {
  595. return 0;
  596. }
  597. static void tca6507_remove_gpio(struct tca6507_chip *tca)
  598. {
  599. }
  600. #endif /* CONFIG_GPIOLIB */
  601. static struct tca6507_platform_data *
  602. tca6507_led_dt_init(struct device *dev)
  603. {
  604. struct tca6507_platform_data *pdata;
  605. struct fwnode_handle *child;
  606. struct led_info *tca_leds;
  607. int count;
  608. count = device_get_child_node_count(dev);
  609. if (!count || count > NUM_LEDS)
  610. return ERR_PTR(-ENODEV);
  611. tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
  612. GFP_KERNEL);
  613. if (!tca_leds)
  614. return ERR_PTR(-ENOMEM);
  615. device_for_each_child_node(dev, child) {
  616. struct led_info led;
  617. u32 reg;
  618. int ret;
  619. if (fwnode_property_read_string(child, "label", &led.name))
  620. led.name = fwnode_get_name(child);
  621. if (fwnode_property_read_string(child, "linux,default-trigger",
  622. &led.default_trigger))
  623. led.default_trigger = NULL;
  624. led.flags = 0;
  625. if (fwnode_property_match_string(child, "compatible",
  626. "gpio") >= 0)
  627. led.flags |= TCA6507_MAKE_GPIO;
  628. ret = fwnode_property_read_u32(child, "reg", &reg);
  629. if (ret || reg >= NUM_LEDS) {
  630. fwnode_handle_put(child);
  631. return ERR_PTR(ret ? : -EINVAL);
  632. }
  633. tca_leds[reg] = led;
  634. }
  635. pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
  636. GFP_KERNEL);
  637. if (!pdata)
  638. return ERR_PTR(-ENOMEM);
  639. pdata->leds.leds = tca_leds;
  640. pdata->leds.num_leds = NUM_LEDS;
  641. #ifdef CONFIG_GPIOLIB
  642. pdata->gpio_base = -1;
  643. #endif
  644. return pdata;
  645. }
  646. static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
  647. { .compatible = "ti,tca6507", },
  648. {},
  649. };
  650. MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
  651. static int tca6507_probe(struct i2c_client *client,
  652. const struct i2c_device_id *id)
  653. {
  654. struct device *dev = &client->dev;
  655. struct i2c_adapter *adapter;
  656. struct tca6507_chip *tca;
  657. struct tca6507_platform_data *pdata;
  658. int err;
  659. int i = 0;
  660. adapter = client->adapter;
  661. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  662. return -EIO;
  663. pdata = tca6507_led_dt_init(dev);
  664. if (IS_ERR(pdata)) {
  665. dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
  666. return PTR_ERR(pdata);
  667. }
  668. tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
  669. if (!tca)
  670. return -ENOMEM;
  671. tca->client = client;
  672. INIT_WORK(&tca->work, tca6507_work);
  673. spin_lock_init(&tca->lock);
  674. i2c_set_clientdata(client, tca);
  675. for (i = 0; i < NUM_LEDS; i++) {
  676. struct tca6507_led *l = tca->leds + i;
  677. l->chip = tca;
  678. l->num = i;
  679. if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
  680. l->led_cdev.name = pdata->leds.leds[i].name;
  681. l->led_cdev.default_trigger
  682. = pdata->leds.leds[i].default_trigger;
  683. l->led_cdev.brightness_set = tca6507_brightness_set;
  684. l->led_cdev.blink_set = tca6507_blink_set;
  685. l->bank = -1;
  686. err = led_classdev_register(dev, &l->led_cdev);
  687. if (err < 0)
  688. goto exit;
  689. }
  690. }
  691. err = tca6507_probe_gpios(dev, tca, pdata);
  692. if (err)
  693. goto exit;
  694. /* set all registers to known state - zero */
  695. tca->reg_set = 0x7f;
  696. schedule_work(&tca->work);
  697. return 0;
  698. exit:
  699. while (i--) {
  700. if (tca->leds[i].led_cdev.name)
  701. led_classdev_unregister(&tca->leds[i].led_cdev);
  702. }
  703. return err;
  704. }
  705. static void tca6507_remove(struct i2c_client *client)
  706. {
  707. int i;
  708. struct tca6507_chip *tca = i2c_get_clientdata(client);
  709. struct tca6507_led *tca_leds = tca->leds;
  710. for (i = 0; i < NUM_LEDS; i++) {
  711. if (tca_leds[i].led_cdev.name)
  712. led_classdev_unregister(&tca_leds[i].led_cdev);
  713. }
  714. tca6507_remove_gpio(tca);
  715. cancel_work_sync(&tca->work);
  716. }
  717. static struct i2c_driver tca6507_driver = {
  718. .driver = {
  719. .name = "leds-tca6507",
  720. .of_match_table = of_match_ptr(of_tca6507_leds_match),
  721. },
  722. .probe = tca6507_probe,
  723. .remove = tca6507_remove,
  724. .id_table = tca6507_id,
  725. };
  726. module_i2c_driver(tca6507_driver);
  727. MODULE_AUTHOR("NeilBrown <[email protected]>");
  728. MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
  729. MODULE_LICENSE("GPL v2");