leds-tca6507.c 20 KB

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