leds-tca6507.c 22 KB

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