leds-tca6507.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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. c1 = *c2p;
  229. *c2p = *c1p;
  230. *c1p = c1;
  231. }
  232. actual = time_codes[*c1p] + time_codes[*c2p];
  233. if (*c1p < *c2p)
  234. return actual + 1;
  235. else
  236. return actual;
  237. }
  238. /* No close match */
  239. return -EINVAL;
  240. }
  241. /*
  242. * Update the register file with the appropriate 3-bit state for the
  243. * given led.
  244. */
  245. static void set_select(struct tca6507_chip *tca, int led, int val)
  246. {
  247. int mask = (1 << led);
  248. int bit;
  249. for (bit = 0; bit < 3; bit++) {
  250. int n = tca->reg_file[bit] & ~mask;
  251. if (val & (1 << bit))
  252. n |= mask;
  253. if (tca->reg_file[bit] != n) {
  254. tca->reg_file[bit] = n;
  255. tca->reg_set |= (1 << bit);
  256. }
  257. }
  258. }
  259. /* Update the register file with the appropriate 4-bit code for one
  260. * bank or other. This can be used for timers, for levels, or for
  261. * initialization.
  262. */
  263. static void set_code(struct tca6507_chip *tca, int reg, int bank, int new)
  264. {
  265. int mask = 0xF;
  266. int n;
  267. if (bank) {
  268. mask <<= 4;
  269. new <<= 4;
  270. }
  271. n = tca->reg_file[reg] & ~mask;
  272. n |= new;
  273. if (tca->reg_file[reg] != n) {
  274. tca->reg_file[reg] = n;
  275. tca->reg_set |= 1 << reg;
  276. }
  277. }
  278. /* Update brightness level. */
  279. static void set_level(struct tca6507_chip *tca, int bank, int level)
  280. {
  281. switch (bank) {
  282. case BANK0:
  283. case BANK1:
  284. set_code(tca, TCA6507_MAX_INTENSITY, bank, level);
  285. break;
  286. case MASTER:
  287. set_code(tca, TCA6507_MASTER_INTENSITY, 0, level);
  288. break;
  289. }
  290. tca->bank[bank].level = level;
  291. }
  292. /* Record all relevant time codes for a given bank */
  293. static void set_times(struct tca6507_chip *tca, int bank)
  294. {
  295. int c1, c2;
  296. int result;
  297. result = choose_times(tca->bank[bank].ontime, &c1, &c2);
  298. if (result < 0)
  299. return;
  300. dev_dbg(&tca->client->dev,
  301. "Chose on times %d(%d) %d(%d) for %dms\n",
  302. c1, time_codes[c1],
  303. c2, time_codes[c2], tca->bank[bank].ontime);
  304. set_code(tca, TCA6507_FADE_ON, bank, c2);
  305. set_code(tca, TCA6507_FULL_ON, bank, c1);
  306. tca->bank[bank].ontime = result;
  307. result = choose_times(tca->bank[bank].offtime, &c1, &c2);
  308. dev_dbg(&tca->client->dev,
  309. "Chose off times %d(%d) %d(%d) for %dms\n",
  310. c1, time_codes[c1],
  311. c2, time_codes[c2], tca->bank[bank].offtime);
  312. set_code(tca, TCA6507_FADE_OFF, bank, c2);
  313. set_code(tca, TCA6507_FIRST_OFF, bank, c1);
  314. set_code(tca, TCA6507_SECOND_OFF, bank, c1);
  315. tca->bank[bank].offtime = result;
  316. set_code(tca, TCA6507_INITIALIZE, bank, INIT_CODE);
  317. }
  318. /* Write all needed register of tca6507 */
  319. static void tca6507_work(struct work_struct *work)
  320. {
  321. struct tca6507_chip *tca = container_of(work, struct tca6507_chip,
  322. work);
  323. struct i2c_client *cl = tca->client;
  324. int set;
  325. u8 file[TCA6507_REG_CNT];
  326. int r;
  327. spin_lock_irq(&tca->lock);
  328. set = tca->reg_set;
  329. memcpy(file, tca->reg_file, TCA6507_REG_CNT);
  330. tca->reg_set = 0;
  331. spin_unlock_irq(&tca->lock);
  332. for (r = 0; r < TCA6507_REG_CNT; r++)
  333. if (set & (1<<r))
  334. i2c_smbus_write_byte_data(cl, r, file[r]);
  335. }
  336. static void led_release(struct tca6507_led *led)
  337. {
  338. /* If led owns any resource, release it. */
  339. struct tca6507_chip *tca = led->chip;
  340. if (led->bank >= 0) {
  341. struct bank *b = tca->bank + led->bank;
  342. if (led->blink)
  343. b->time_use--;
  344. b->level_use--;
  345. }
  346. led->blink = 0;
  347. led->bank = -1;
  348. }
  349. static int led_prepare(struct tca6507_led *led)
  350. {
  351. /* Assign this led to a bank, configuring that bank if
  352. * necessary. */
  353. int level = TO_LEVEL(led->led_cdev.brightness);
  354. struct tca6507_chip *tca = led->chip;
  355. int c1, c2;
  356. int i;
  357. struct bank *b;
  358. int need_init = 0;
  359. led->led_cdev.brightness = TO_BRIGHT(level);
  360. if (level == 0) {
  361. set_select(tca, led->num, TCA6507_LS_LED_OFF);
  362. return 0;
  363. }
  364. if (led->ontime == 0 || led->offtime == 0) {
  365. /*
  366. * Just set the brightness, choosing first usable
  367. * bank. If none perfect, choose best. Count
  368. * backwards so we check MASTER bank first to avoid
  369. * wasting a timer.
  370. */
  371. int best = -1;/* full-on */
  372. int diff = 15-level;
  373. if (level == 15) {
  374. set_select(tca, led->num, TCA6507_LS_LED_ON);
  375. return 0;
  376. }
  377. for (i = MASTER; i >= BANK0; i--) {
  378. int d;
  379. if (tca->bank[i].level == level ||
  380. tca->bank[i].level_use == 0) {
  381. best = i;
  382. break;
  383. }
  384. d = abs(level - tca->bank[i].level);
  385. if (d < diff) {
  386. diff = d;
  387. best = i;
  388. }
  389. }
  390. if (best == -1) {
  391. /* Best brightness is full-on */
  392. set_select(tca, led->num, TCA6507_LS_LED_ON);
  393. led->led_cdev.brightness = LED_FULL;
  394. return 0;
  395. }
  396. if (!tca->bank[best].level_use)
  397. set_level(tca, best, level);
  398. tca->bank[best].level_use++;
  399. led->bank = best;
  400. set_select(tca, led->num, bank_source[best]);
  401. led->led_cdev.brightness = TO_BRIGHT(tca->bank[best].level);
  402. return 0;
  403. }
  404. /*
  405. * We have on/off time so we need to try to allocate a timing
  406. * bank. First check if times are compatible with hardware
  407. * and give up if not.
  408. */
  409. if (choose_times(led->ontime, &c1, &c2) < 0)
  410. return -EINVAL;
  411. if (choose_times(led->offtime, &c1, &c2) < 0)
  412. return -EINVAL;
  413. for (i = BANK0; i <= BANK1; i++) {
  414. if (tca->bank[i].level_use == 0)
  415. /* not in use - it is ours! */
  416. break;
  417. if (tca->bank[i].level != level)
  418. /* Incompatible level - skip */
  419. /* FIX: if timer matches we maybe should consider
  420. * this anyway...
  421. */
  422. continue;
  423. if (tca->bank[i].time_use == 0)
  424. /* Timer not in use, and level matches - use it */
  425. break;
  426. if (!(tca->bank[i].on_dflt ||
  427. led->on_dflt ||
  428. tca->bank[i].ontime == led->ontime))
  429. /* on time is incompatible */
  430. continue;
  431. if (!(tca->bank[i].off_dflt ||
  432. led->off_dflt ||
  433. tca->bank[i].offtime == led->offtime))
  434. /* off time is incompatible */
  435. continue;
  436. /* looks like a suitable match */
  437. break;
  438. }
  439. if (i > BANK1)
  440. /* Nothing matches - how sad */
  441. return -EINVAL;
  442. b = &tca->bank[i];
  443. if (b->level_use == 0)
  444. set_level(tca, i, level);
  445. b->level_use++;
  446. led->bank = i;
  447. if (b->on_dflt ||
  448. !led->on_dflt ||
  449. b->time_use == 0) {
  450. b->ontime = led->ontime;
  451. b->on_dflt = led->on_dflt;
  452. need_init = 1;
  453. }
  454. if (b->off_dflt ||
  455. !led->off_dflt ||
  456. b->time_use == 0) {
  457. b->offtime = led->offtime;
  458. b->off_dflt = led->off_dflt;
  459. need_init = 1;
  460. }
  461. if (need_init)
  462. set_times(tca, i);
  463. led->ontime = b->ontime;
  464. led->offtime = b->offtime;
  465. b->time_use++;
  466. led->blink = 1;
  467. led->led_cdev.brightness = TO_BRIGHT(b->level);
  468. set_select(tca, led->num, blink_source[i]);
  469. return 0;
  470. }
  471. static int led_assign(struct tca6507_led *led)
  472. {
  473. struct tca6507_chip *tca = led->chip;
  474. int err;
  475. unsigned long flags;
  476. spin_lock_irqsave(&tca->lock, flags);
  477. led_release(led);
  478. err = led_prepare(led);
  479. if (err) {
  480. /*
  481. * Can only fail on timer setup. In that case we need
  482. * to re-establish as steady level.
  483. */
  484. led->ontime = 0;
  485. led->offtime = 0;
  486. led_prepare(led);
  487. }
  488. spin_unlock_irqrestore(&tca->lock, flags);
  489. if (tca->reg_set)
  490. schedule_work(&tca->work);
  491. return err;
  492. }
  493. static void tca6507_brightness_set(struct led_classdev *led_cdev,
  494. enum led_brightness brightness)
  495. {
  496. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  497. led_cdev);
  498. led->led_cdev.brightness = brightness;
  499. led->ontime = 0;
  500. led->offtime = 0;
  501. led_assign(led);
  502. }
  503. static int tca6507_blink_set(struct led_classdev *led_cdev,
  504. unsigned long *delay_on,
  505. unsigned long *delay_off)
  506. {
  507. struct tca6507_led *led = container_of(led_cdev, struct tca6507_led,
  508. led_cdev);
  509. if (*delay_on == 0)
  510. led->on_dflt = 1;
  511. else if (delay_on != &led_cdev->blink_delay_on)
  512. led->on_dflt = 0;
  513. led->ontime = *delay_on;
  514. if (*delay_off == 0)
  515. led->off_dflt = 1;
  516. else if (delay_off != &led_cdev->blink_delay_off)
  517. led->off_dflt = 0;
  518. led->offtime = *delay_off;
  519. if (led->ontime == 0)
  520. led->ontime = 512;
  521. if (led->offtime == 0)
  522. led->offtime = 512;
  523. if (led->led_cdev.brightness == LED_OFF)
  524. led->led_cdev.brightness = LED_FULL;
  525. if (led_assign(led) < 0) {
  526. led->ontime = 0;
  527. led->offtime = 0;
  528. led->led_cdev.brightness = LED_OFF;
  529. return -EINVAL;
  530. }
  531. *delay_on = led->ontime;
  532. *delay_off = led->offtime;
  533. return 0;
  534. }
  535. #ifdef CONFIG_GPIOLIB
  536. static void tca6507_gpio_set_value(struct gpio_chip *gc,
  537. unsigned offset, int val)
  538. {
  539. struct tca6507_chip *tca = gpiochip_get_data(gc);
  540. unsigned long flags;
  541. spin_lock_irqsave(&tca->lock, flags);
  542. /*
  543. * 'OFF' is floating high, and 'ON' is pulled down, so it has
  544. * the inverse sense of 'val'.
  545. */
  546. set_select(tca, tca->gpio_map[offset],
  547. val ? TCA6507_LS_LED_OFF : TCA6507_LS_LED_ON);
  548. spin_unlock_irqrestore(&tca->lock, flags);
  549. if (tca->reg_set)
  550. schedule_work(&tca->work);
  551. }
  552. static int tca6507_gpio_direction_output(struct gpio_chip *gc,
  553. unsigned offset, int val)
  554. {
  555. tca6507_gpio_set_value(gc, offset, val);
  556. return 0;
  557. }
  558. static int tca6507_probe_gpios(struct device *dev,
  559. struct tca6507_chip *tca,
  560. struct tca6507_platform_data *pdata)
  561. {
  562. int err;
  563. int i = 0;
  564. int gpios = 0;
  565. for (i = 0; i < NUM_LEDS; i++)
  566. if (pdata->leds.leds[i].name && pdata->leds.leds[i].flags) {
  567. /* Configure as a gpio */
  568. tca->gpio_map[gpios] = i;
  569. gpios++;
  570. }
  571. if (!gpios)
  572. return 0;
  573. tca->gpio.label = "gpio-tca6507";
  574. tca->gpio.ngpio = gpios;
  575. tca->gpio.base = pdata->gpio_base;
  576. tca->gpio.owner = THIS_MODULE;
  577. tca->gpio.direction_output = tca6507_gpio_direction_output;
  578. tca->gpio.set = tca6507_gpio_set_value;
  579. tca->gpio.parent = dev;
  580. #ifdef CONFIG_OF_GPIO
  581. tca->gpio.of_node = of_node_get(dev_of_node(dev));
  582. #endif
  583. err = gpiochip_add_data(&tca->gpio, tca);
  584. if (err) {
  585. tca->gpio.ngpio = 0;
  586. return err;
  587. }
  588. return 0;
  589. }
  590. static void tca6507_remove_gpio(struct tca6507_chip *tca)
  591. {
  592. if (tca->gpio.ngpio)
  593. gpiochip_remove(&tca->gpio);
  594. }
  595. #else /* CONFIG_GPIOLIB */
  596. static int tca6507_probe_gpios(struct device *dev,
  597. struct tca6507_chip *tca,
  598. struct tca6507_platform_data *pdata)
  599. {
  600. return 0;
  601. }
  602. static void tca6507_remove_gpio(struct tca6507_chip *tca)
  603. {
  604. }
  605. #endif /* CONFIG_GPIOLIB */
  606. static struct tca6507_platform_data *
  607. tca6507_led_dt_init(struct device *dev)
  608. {
  609. struct tca6507_platform_data *pdata;
  610. struct fwnode_handle *child;
  611. struct led_info *tca_leds;
  612. int count;
  613. count = device_get_child_node_count(dev);
  614. if (!count || count > NUM_LEDS)
  615. return ERR_PTR(-ENODEV);
  616. tca_leds = devm_kcalloc(dev, NUM_LEDS, sizeof(struct led_info),
  617. GFP_KERNEL);
  618. if (!tca_leds)
  619. return ERR_PTR(-ENOMEM);
  620. device_for_each_child_node(dev, child) {
  621. struct led_info led;
  622. u32 reg;
  623. int ret;
  624. if (fwnode_property_read_string(child, "label", &led.name))
  625. led.name = fwnode_get_name(child);
  626. fwnode_property_read_string(child, "linux,default-trigger",
  627. &led.default_trigger);
  628. led.flags = 0;
  629. if (fwnode_property_match_string(child, "compatible",
  630. "gpio") >= 0)
  631. led.flags |= TCA6507_MAKE_GPIO;
  632. ret = fwnode_property_read_u32(child, "reg", &reg);
  633. if (ret || reg >= NUM_LEDS) {
  634. fwnode_handle_put(child);
  635. return ERR_PTR(ret ? : -EINVAL);
  636. }
  637. tca_leds[reg] = led;
  638. }
  639. pdata = devm_kzalloc(dev, sizeof(struct tca6507_platform_data),
  640. GFP_KERNEL);
  641. if (!pdata)
  642. return ERR_PTR(-ENOMEM);
  643. pdata->leds.leds = tca_leds;
  644. pdata->leds.num_leds = NUM_LEDS;
  645. #ifdef CONFIG_GPIOLIB
  646. pdata->gpio_base = -1;
  647. #endif
  648. return pdata;
  649. }
  650. static const struct of_device_id __maybe_unused of_tca6507_leds_match[] = {
  651. { .compatible = "ti,tca6507", },
  652. {},
  653. };
  654. MODULE_DEVICE_TABLE(of, of_tca6507_leds_match);
  655. static int tca6507_probe(struct i2c_client *client,
  656. const struct i2c_device_id *id)
  657. {
  658. struct device *dev = &client->dev;
  659. struct i2c_adapter *adapter;
  660. struct tca6507_chip *tca;
  661. struct tca6507_platform_data *pdata;
  662. int err;
  663. int i = 0;
  664. adapter = client->adapter;
  665. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  666. return -EIO;
  667. pdata = tca6507_led_dt_init(dev);
  668. if (IS_ERR(pdata)) {
  669. dev_err(dev, "Need %d entries in platform-data list\n", NUM_LEDS);
  670. return PTR_ERR(pdata);
  671. }
  672. tca = devm_kzalloc(dev, sizeof(*tca), GFP_KERNEL);
  673. if (!tca)
  674. return -ENOMEM;
  675. tca->client = client;
  676. INIT_WORK(&tca->work, tca6507_work);
  677. spin_lock_init(&tca->lock);
  678. i2c_set_clientdata(client, tca);
  679. for (i = 0; i < NUM_LEDS; i++) {
  680. struct tca6507_led *l = tca->leds + i;
  681. l->chip = tca;
  682. l->num = i;
  683. if (pdata->leds.leds[i].name && !pdata->leds.leds[i].flags) {
  684. l->led_cdev.name = pdata->leds.leds[i].name;
  685. l->led_cdev.default_trigger
  686. = pdata->leds.leds[i].default_trigger;
  687. l->led_cdev.brightness_set = tca6507_brightness_set;
  688. l->led_cdev.blink_set = tca6507_blink_set;
  689. l->bank = -1;
  690. err = led_classdev_register(dev, &l->led_cdev);
  691. if (err < 0)
  692. goto exit;
  693. }
  694. }
  695. err = tca6507_probe_gpios(dev, tca, pdata);
  696. if (err)
  697. goto exit;
  698. /* set all registers to known state - zero */
  699. tca->reg_set = 0x7f;
  700. schedule_work(&tca->work);
  701. return 0;
  702. exit:
  703. while (i--) {
  704. if (tca->leds[i].led_cdev.name)
  705. led_classdev_unregister(&tca->leds[i].led_cdev);
  706. }
  707. return err;
  708. }
  709. static int tca6507_remove(struct i2c_client *client)
  710. {
  711. int i;
  712. struct tca6507_chip *tca = i2c_get_clientdata(client);
  713. struct tca6507_led *tca_leds = tca->leds;
  714. for (i = 0; i < NUM_LEDS; i++) {
  715. if (tca_leds[i].led_cdev.name)
  716. led_classdev_unregister(&tca_leds[i].led_cdev);
  717. }
  718. tca6507_remove_gpio(tca);
  719. cancel_work_sync(&tca->work);
  720. return 0;
  721. }
  722. static struct i2c_driver tca6507_driver = {
  723. .driver = {
  724. .name = "leds-tca6507",
  725. .of_match_table = of_match_ptr(of_tca6507_leds_match),
  726. },
  727. .probe = tca6507_probe,
  728. .remove = tca6507_remove,
  729. .id_table = tca6507_id,
  730. };
  731. module_i2c_driver(tca6507_driver);
  732. MODULE_AUTHOR("NeilBrown <neilb@suse.de>");
  733. MODULE_DESCRIPTION("TCA6507 LED/GPO driver");
  734. MODULE_LICENSE("GPL v2");