leds-pca955x.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
  3. *
  4. * Author: Nate Case <ncase@xes-inc.com>
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * LED driver for various PCA955x I2C LED drivers
  11. *
  12. * Supported devices:
  13. *
  14. * Device Description 7-bit slave address
  15. * ------ ----------- -------------------
  16. * PCA9550 2-bit driver 0x60 .. 0x61
  17. * PCA9551 8-bit driver 0x60 .. 0x67
  18. * PCA9552 16-bit driver 0x60 .. 0x67
  19. * PCA9553/01 4-bit driver 0x62
  20. * PCA9553/02 4-bit driver 0x63
  21. *
  22. * Philips PCA955x LED driver chips follow a register map as shown below:
  23. *
  24. * Control Register Description
  25. * ---------------- -----------
  26. * 0x0 Input register 0
  27. * ..
  28. * NUM_INPUT_REGS - 1 Last Input register X
  29. *
  30. * NUM_INPUT_REGS Frequency prescaler 0
  31. * NUM_INPUT_REGS + 1 PWM register 0
  32. * NUM_INPUT_REGS + 2 Frequency prescaler 1
  33. * NUM_INPUT_REGS + 3 PWM register 1
  34. *
  35. * NUM_INPUT_REGS + 4 LED selector 0
  36. * NUM_INPUT_REGS + 4
  37. * + NUM_LED_REGS - 1 Last LED selector
  38. *
  39. * where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
  40. * bits the chip supports.
  41. */
  42. #include <linux/module.h>
  43. #include <linux/delay.h>
  44. #include <linux/string.h>
  45. #include <linux/ctype.h>
  46. #include <linux/leds.h>
  47. #include <linux/err.h>
  48. #include <linux/i2c.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/slab.h>
  51. /* LED select registers determine the source that drives LED outputs */
  52. #define PCA955X_LS_LED_ON 0x0 /* Output LOW */
  53. #define PCA955X_LS_LED_OFF 0x1 /* Output HI-Z */
  54. #define PCA955X_LS_BLINK0 0x2 /* Blink at PWM0 rate */
  55. #define PCA955X_LS_BLINK1 0x3 /* Blink at PWM1 rate */
  56. enum pca955x_type {
  57. pca9550,
  58. pca9551,
  59. pca9552,
  60. pca9553,
  61. };
  62. struct pca955x_chipdef {
  63. int bits;
  64. u8 slv_addr; /* 7-bit slave address mask */
  65. int slv_addr_shift; /* Number of bits to ignore */
  66. };
  67. static struct pca955x_chipdef pca955x_chipdefs[] = {
  68. [pca9550] = {
  69. .bits = 2,
  70. .slv_addr = /* 110000x */ 0x60,
  71. .slv_addr_shift = 1,
  72. },
  73. [pca9551] = {
  74. .bits = 8,
  75. .slv_addr = /* 1100xxx */ 0x60,
  76. .slv_addr_shift = 3,
  77. },
  78. [pca9552] = {
  79. .bits = 16,
  80. .slv_addr = /* 1100xxx */ 0x60,
  81. .slv_addr_shift = 3,
  82. },
  83. [pca9553] = {
  84. .bits = 4,
  85. .slv_addr = /* 110001x */ 0x62,
  86. .slv_addr_shift = 1,
  87. },
  88. };
  89. static const struct i2c_device_id pca955x_id[] = {
  90. { "pca9550", pca9550 },
  91. { "pca9551", pca9551 },
  92. { "pca9552", pca9552 },
  93. { "pca9553", pca9553 },
  94. { }
  95. };
  96. MODULE_DEVICE_TABLE(i2c, pca955x_id);
  97. struct pca955x_led {
  98. struct pca955x_chipdef *chipdef;
  99. struct i2c_client *client;
  100. struct work_struct work;
  101. spinlock_t lock;
  102. enum led_brightness brightness;
  103. struct led_classdev led_cdev;
  104. int led_num; /* 0 .. 15 potentially */
  105. char name[32];
  106. };
  107. /* 8 bits per input register */
  108. static inline int pca95xx_num_input_regs(int bits)
  109. {
  110. return (bits + 7) / 8;
  111. }
  112. /* 4 bits per LED selector register */
  113. static inline int pca95xx_num_led_regs(int bits)
  114. {
  115. return (bits + 3) / 4;
  116. }
  117. /*
  118. * Return an LED selector register value based on an existing one, with
  119. * the appropriate 2-bit state value set for the given LED number (0-3).
  120. */
  121. static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
  122. {
  123. return (oldval & (~(0x3 << (led_num << 1)))) |
  124. ((state & 0x3) << (led_num << 1));
  125. }
  126. /*
  127. * Write to frequency prescaler register, used to program the
  128. * period of the PWM output. period = (PSCx + 1) / 38
  129. */
  130. static void pca955x_write_psc(struct i2c_client *client, int n, u8 val)
  131. {
  132. struct pca955x_led *pca955x = i2c_get_clientdata(client);
  133. i2c_smbus_write_byte_data(client,
  134. pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
  135. val);
  136. }
  137. /*
  138. * Write to PWM register, which determines the duty cycle of the
  139. * output. LED is OFF when the count is less than the value of this
  140. * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
  141. *
  142. * Duty cycle is (256 - PWMx) / 256
  143. */
  144. static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
  145. {
  146. struct pca955x_led *pca955x = i2c_get_clientdata(client);
  147. i2c_smbus_write_byte_data(client,
  148. pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
  149. val);
  150. }
  151. /*
  152. * Write to LED selector register, which determines the source that
  153. * drives the LED output.
  154. */
  155. static void pca955x_write_ls(struct i2c_client *client, int n, u8 val)
  156. {
  157. struct pca955x_led *pca955x = i2c_get_clientdata(client);
  158. i2c_smbus_write_byte_data(client,
  159. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
  160. val);
  161. }
  162. /*
  163. * Read the LED selector register, which determines the source that
  164. * drives the LED output.
  165. */
  166. static u8 pca955x_read_ls(struct i2c_client *client, int n)
  167. {
  168. struct pca955x_led *pca955x = i2c_get_clientdata(client);
  169. return (u8) i2c_smbus_read_byte_data(client,
  170. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
  171. }
  172. static void pca955x_led_work(struct work_struct *work)
  173. {
  174. struct pca955x_led *pca955x;
  175. u8 ls;
  176. int chip_ls; /* which LSx to use (0-3 potentially) */
  177. int ls_led; /* which set of bits within LSx to use (0-3) */
  178. pca955x = container_of(work, struct pca955x_led, work);
  179. chip_ls = pca955x->led_num / 4;
  180. ls_led = pca955x->led_num % 4;
  181. ls = pca955x_read_ls(pca955x->client, chip_ls);
  182. switch (pca955x->brightness) {
  183. case LED_FULL:
  184. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
  185. break;
  186. case LED_OFF:
  187. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
  188. break;
  189. case LED_HALF:
  190. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
  191. break;
  192. default:
  193. /*
  194. * Use PWM1 for all other values. This has the unwanted
  195. * side effect of making all LEDs on the chip share the
  196. * same brightness level if set to a value other than
  197. * OFF, HALF, or FULL. But, this is probably better than
  198. * just turning off for all other values.
  199. */
  200. pca955x_write_pwm(pca955x->client, 1, 255-pca955x->brightness);
  201. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
  202. break;
  203. }
  204. pca955x_write_ls(pca955x->client, chip_ls, ls);
  205. }
  206. static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness value)
  207. {
  208. struct pca955x_led *pca955x;
  209. pca955x = container_of(led_cdev, struct pca955x_led, led_cdev);
  210. spin_lock(&pca955x->lock);
  211. pca955x->brightness = value;
  212. /*
  213. * Must use workqueue for the actual I/O since I2C operations
  214. * can sleep.
  215. */
  216. schedule_work(&pca955x->work);
  217. spin_unlock(&pca955x->lock);
  218. }
  219. static int __devinit pca955x_probe(struct i2c_client *client,
  220. const struct i2c_device_id *id)
  221. {
  222. struct pca955x_led *pca955x;
  223. struct pca955x_chipdef *chip;
  224. struct i2c_adapter *adapter;
  225. struct led_platform_data *pdata;
  226. int i, err;
  227. chip = &pca955x_chipdefs[id->driver_data];
  228. adapter = to_i2c_adapter(client->dev.parent);
  229. pdata = client->dev.platform_data;
  230. /* Make sure the slave address / chip type combo given is possible */
  231. if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
  232. chip->slv_addr) {
  233. dev_err(&client->dev, "invalid slave address %02x\n",
  234. client->addr);
  235. return -ENODEV;
  236. }
  237. printk(KERN_INFO "leds-pca955x: Using %s %d-bit LED driver at "
  238. "slave address 0x%02x\n",
  239. id->name, chip->bits, client->addr);
  240. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  241. return -EIO;
  242. if (pdata) {
  243. if (pdata->num_leds != chip->bits) {
  244. dev_err(&client->dev, "board info claims %d LEDs"
  245. " on a %d-bit chip\n",
  246. pdata->num_leds, chip->bits);
  247. return -ENODEV;
  248. }
  249. }
  250. pca955x = kzalloc(sizeof(*pca955x) * chip->bits, GFP_KERNEL);
  251. if (!pca955x)
  252. return -ENOMEM;
  253. i2c_set_clientdata(client, pca955x);
  254. for (i = 0; i < chip->bits; i++) {
  255. pca955x[i].chipdef = chip;
  256. pca955x[i].client = client;
  257. pca955x[i].led_num = i;
  258. /* Platform data can specify LED names and default triggers */
  259. if (pdata) {
  260. if (pdata->leds[i].name)
  261. snprintf(pca955x[i].name,
  262. sizeof(pca955x[i].name), "pca955x:%s",
  263. pdata->leds[i].name);
  264. if (pdata->leds[i].default_trigger)
  265. pca955x[i].led_cdev.default_trigger =
  266. pdata->leds[i].default_trigger;
  267. } else {
  268. snprintf(pca955x[i].name, sizeof(pca955x[i].name),
  269. "pca955x:%d", i);
  270. }
  271. spin_lock_init(&pca955x[i].lock);
  272. pca955x[i].led_cdev.name = pca955x[i].name;
  273. pca955x[i].led_cdev.brightness_set = pca955x_led_set;
  274. INIT_WORK(&pca955x[i].work, pca955x_led_work);
  275. err = led_classdev_register(&client->dev, &pca955x[i].led_cdev);
  276. if (err < 0)
  277. goto exit;
  278. }
  279. /* Turn off LEDs */
  280. for (i = 0; i < pca95xx_num_led_regs(chip->bits); i++)
  281. pca955x_write_ls(client, i, 0x55);
  282. /* PWM0 is used for half brightness or 50% duty cycle */
  283. pca955x_write_pwm(client, 0, 255-LED_HALF);
  284. /* PWM1 is used for variable brightness, default to OFF */
  285. pca955x_write_pwm(client, 1, 0);
  286. /* Set to fast frequency so we do not see flashing */
  287. pca955x_write_psc(client, 0, 0);
  288. pca955x_write_psc(client, 1, 0);
  289. return 0;
  290. exit:
  291. while (i--) {
  292. led_classdev_unregister(&pca955x[i].led_cdev);
  293. cancel_work_sync(&pca955x[i].work);
  294. }
  295. kfree(pca955x);
  296. return err;
  297. }
  298. static int __devexit pca955x_remove(struct i2c_client *client)
  299. {
  300. struct pca955x_led *pca955x = i2c_get_clientdata(client);
  301. int i;
  302. for (i = 0; i < pca955x->chipdef->bits; i++) {
  303. led_classdev_unregister(&pca955x[i].led_cdev);
  304. cancel_work_sync(&pca955x[i].work);
  305. }
  306. kfree(pca955x);
  307. return 0;
  308. }
  309. static struct i2c_driver pca955x_driver = {
  310. .driver = {
  311. .name = "leds-pca955x",
  312. .owner = THIS_MODULE,
  313. },
  314. .probe = pca955x_probe,
  315. .remove = __devexit_p(pca955x_remove),
  316. .id_table = pca955x_id,
  317. };
  318. static int __init pca955x_leds_init(void)
  319. {
  320. return i2c_add_driver(&pca955x_driver);
  321. }
  322. static void __exit pca955x_leds_exit(void)
  323. {
  324. i2c_del_driver(&pca955x_driver);
  325. }
  326. module_init(pca955x_leds_init);
  327. module_exit(pca955x_leds_exit);
  328. MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
  329. MODULE_DESCRIPTION("PCA955x LED driver");
  330. MODULE_LICENSE("GPL v2");