leds-pca955x.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 {
  98. struct mutex lock;
  99. struct pca955x_led *leds;
  100. struct pca955x_chipdef *chipdef;
  101. struct i2c_client *client;
  102. };
  103. struct pca955x_led {
  104. struct pca955x *pca955x;
  105. struct work_struct work;
  106. enum led_brightness brightness;
  107. struct led_classdev led_cdev;
  108. int led_num; /* 0 .. 15 potentially */
  109. char name[32];
  110. };
  111. /* 8 bits per input register */
  112. static inline int pca95xx_num_input_regs(int bits)
  113. {
  114. return (bits + 7) / 8;
  115. }
  116. /* 4 bits per LED selector register */
  117. static inline int pca95xx_num_led_regs(int bits)
  118. {
  119. return (bits + 3) / 4;
  120. }
  121. /*
  122. * Return an LED selector register value based on an existing one, with
  123. * the appropriate 2-bit state value set for the given LED number (0-3).
  124. */
  125. static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
  126. {
  127. return (oldval & (~(0x3 << (led_num << 1)))) |
  128. ((state & 0x3) << (led_num << 1));
  129. }
  130. /*
  131. * Write to frequency prescaler register, used to program the
  132. * period of the PWM output. period = (PSCx + 1) / 38
  133. */
  134. static void pca955x_write_psc(struct i2c_client *client, int n, u8 val)
  135. {
  136. struct pca955x *pca955x = i2c_get_clientdata(client);
  137. i2c_smbus_write_byte_data(client,
  138. pca95xx_num_input_regs(pca955x->chipdef->bits) + 2*n,
  139. val);
  140. }
  141. /*
  142. * Write to PWM register, which determines the duty cycle of the
  143. * output. LED is OFF when the count is less than the value of this
  144. * register, and ON when it is greater. If PWMx == 0, LED is always OFF.
  145. *
  146. * Duty cycle is (256 - PWMx) / 256
  147. */
  148. static void pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
  149. {
  150. struct pca955x *pca955x = i2c_get_clientdata(client);
  151. i2c_smbus_write_byte_data(client,
  152. pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + 2*n,
  153. val);
  154. }
  155. /*
  156. * Write to LED selector register, which determines the source that
  157. * drives the LED output.
  158. */
  159. static void pca955x_write_ls(struct i2c_client *client, int n, u8 val)
  160. {
  161. struct pca955x *pca955x = i2c_get_clientdata(client);
  162. i2c_smbus_write_byte_data(client,
  163. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n,
  164. val);
  165. }
  166. /*
  167. * Read the LED selector register, which determines the source that
  168. * drives the LED output.
  169. */
  170. static u8 pca955x_read_ls(struct i2c_client *client, int n)
  171. {
  172. struct pca955x *pca955x = i2c_get_clientdata(client);
  173. return (u8) i2c_smbus_read_byte_data(client,
  174. pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n);
  175. }
  176. static void pca955x_led_work(struct work_struct *work)
  177. {
  178. struct pca955x_led *pca955x_led;
  179. struct pca955x *pca955x;
  180. u8 ls;
  181. int chip_ls; /* which LSx to use (0-3 potentially) */
  182. int ls_led; /* which set of bits within LSx to use (0-3) */
  183. pca955x_led = container_of(work, struct pca955x_led, work);
  184. pca955x = pca955x_led->pca955x;
  185. chip_ls = pca955x_led->led_num / 4;
  186. ls_led = pca955x_led->led_num % 4;
  187. mutex_lock(&pca955x->lock);
  188. ls = pca955x_read_ls(pca955x->client, chip_ls);
  189. switch (pca955x_led->brightness) {
  190. case LED_FULL:
  191. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
  192. break;
  193. case LED_OFF:
  194. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
  195. break;
  196. case LED_HALF:
  197. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
  198. break;
  199. default:
  200. /*
  201. * Use PWM1 for all other values. This has the unwanted
  202. * side effect of making all LEDs on the chip share the
  203. * same brightness level if set to a value other than
  204. * OFF, HALF, or FULL. But, this is probably better than
  205. * just turning off for all other values.
  206. */
  207. pca955x_write_pwm(pca955x->client, 1,
  208. 255 - pca955x_led->brightness);
  209. ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
  210. break;
  211. }
  212. pca955x_write_ls(pca955x->client, chip_ls, ls);
  213. mutex_unlock(&pca955x->lock);
  214. }
  215. static void pca955x_led_set(struct led_classdev *led_cdev, enum led_brightness value)
  216. {
  217. struct pca955x_led *pca955x;
  218. pca955x = container_of(led_cdev, struct pca955x_led, led_cdev);
  219. pca955x->brightness = value;
  220. /*
  221. * Must use workqueue for the actual I/O since I2C operations
  222. * can sleep.
  223. */
  224. schedule_work(&pca955x->work);
  225. }
  226. static int pca955x_probe(struct i2c_client *client,
  227. const struct i2c_device_id *id)
  228. {
  229. struct pca955x *pca955x;
  230. struct pca955x_led *pca955x_led;
  231. struct pca955x_chipdef *chip;
  232. struct i2c_adapter *adapter;
  233. struct led_platform_data *pdata;
  234. int i, err;
  235. chip = &pca955x_chipdefs[id->driver_data];
  236. adapter = to_i2c_adapter(client->dev.parent);
  237. pdata = dev_get_platdata(&client->dev);
  238. /* Make sure the slave address / chip type combo given is possible */
  239. if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
  240. chip->slv_addr) {
  241. dev_err(&client->dev, "invalid slave address %02x\n",
  242. client->addr);
  243. return -ENODEV;
  244. }
  245. dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
  246. "slave address 0x%02x\n",
  247. id->name, chip->bits, client->addr);
  248. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  249. return -EIO;
  250. if (pdata) {
  251. if (pdata->num_leds != chip->bits) {
  252. dev_err(&client->dev, "board info claims %d LEDs"
  253. " on a %d-bit chip\n",
  254. pdata->num_leds, chip->bits);
  255. return -ENODEV;
  256. }
  257. }
  258. pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
  259. if (!pca955x)
  260. return -ENOMEM;
  261. pca955x->leds = devm_kzalloc(&client->dev,
  262. sizeof(*pca955x_led) * chip->bits, GFP_KERNEL);
  263. if (!pca955x->leds)
  264. return -ENOMEM;
  265. i2c_set_clientdata(client, pca955x);
  266. mutex_init(&pca955x->lock);
  267. pca955x->client = client;
  268. pca955x->chipdef = chip;
  269. for (i = 0; i < chip->bits; i++) {
  270. pca955x_led = &pca955x->leds[i];
  271. pca955x_led->led_num = i;
  272. pca955x_led->pca955x = pca955x;
  273. /* Platform data can specify LED names and default triggers */
  274. if (pdata) {
  275. if (pdata->leds[i].name)
  276. snprintf(pca955x_led->name,
  277. sizeof(pca955x_led->name), "pca955x:%s",
  278. pdata->leds[i].name);
  279. if (pdata->leds[i].default_trigger)
  280. pca955x_led->led_cdev.default_trigger =
  281. pdata->leds[i].default_trigger;
  282. } else {
  283. snprintf(pca955x_led->name, sizeof(pca955x_led->name),
  284. "pca955x:%d", i);
  285. }
  286. pca955x_led->led_cdev.name = pca955x_led->name;
  287. pca955x_led->led_cdev.brightness_set = pca955x_led_set;
  288. INIT_WORK(&pca955x_led->work, pca955x_led_work);
  289. err = led_classdev_register(&client->dev,
  290. &pca955x_led->led_cdev);
  291. if (err < 0)
  292. goto exit;
  293. }
  294. /* Turn off LEDs */
  295. for (i = 0; i < pca95xx_num_led_regs(chip->bits); i++)
  296. pca955x_write_ls(client, i, 0x55);
  297. /* PWM0 is used for half brightness or 50% duty cycle */
  298. pca955x_write_pwm(client, 0, 255-LED_HALF);
  299. /* PWM1 is used for variable brightness, default to OFF */
  300. pca955x_write_pwm(client, 1, 0);
  301. /* Set to fast frequency so we do not see flashing */
  302. pca955x_write_psc(client, 0, 0);
  303. pca955x_write_psc(client, 1, 0);
  304. return 0;
  305. exit:
  306. while (i--) {
  307. led_classdev_unregister(&pca955x->leds[i].led_cdev);
  308. cancel_work_sync(&pca955x->leds[i].work);
  309. }
  310. return err;
  311. }
  312. static int pca955x_remove(struct i2c_client *client)
  313. {
  314. struct pca955x *pca955x = i2c_get_clientdata(client);
  315. int i;
  316. for (i = 0; i < pca955x->chipdef->bits; i++) {
  317. led_classdev_unregister(&pca955x->leds[i].led_cdev);
  318. cancel_work_sync(&pca955x->leds[i].work);
  319. }
  320. return 0;
  321. }
  322. static struct i2c_driver pca955x_driver = {
  323. .driver = {
  324. .name = "leds-pca955x",
  325. },
  326. .probe = pca955x_probe,
  327. .remove = pca955x_remove,
  328. .id_table = pca955x_id,
  329. };
  330. module_i2c_driver(pca955x_driver);
  331. MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
  332. MODULE_DESCRIPTION("PCA955x LED driver");
  333. MODULE_LICENSE("GPL v2");