leds-pca955x.c 9.3 KB

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