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