leds-tca6507.c 22 KB

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