leds-pca9685.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 2013 Maximilian Güntner <maximilian.guentner@gmail.com>
  3. *
  4. * This file is subject to the terms and conditions of version 2 of
  5. * the GNU General Public License. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Based on leds-pca963x.c driver by
  9. * Peter Meerwald <p.meerwald@bct-electronic.com>
  10. *
  11. * Driver for the NXP PCA9685 12-Bit PWM LED driver chip.
  12. *
  13. */
  14. #include <linux/ctype.h>
  15. #include <linux/delay.h>
  16. #include <linux/err.h>
  17. #include <linux/i2c.h>
  18. #include <linux/leds.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/string.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/platform_data/leds-pca9685.h>
  24. /* Register Addresses */
  25. #define PCA9685_MODE1 0x00
  26. #define PCA9685_MODE2 0x01
  27. #define PCA9685_LED0_ON_L 0x06
  28. #define PCA9685_ALL_LED_ON_L 0xFA
  29. /* MODE1 Register */
  30. #define PCA9685_ALLCALL 0x00
  31. #define PCA9685_SLEEP 0x04
  32. #define PCA9685_AI 0x05
  33. /* MODE2 Register */
  34. #define PCA9685_INVRT 0x04
  35. #define PCA9685_OUTDRV 0x02
  36. static const struct i2c_device_id pca9685_id[] = {
  37. { "pca9685", 0 },
  38. { }
  39. };
  40. MODULE_DEVICE_TABLE(i2c, pca9685_id);
  41. struct pca9685_led {
  42. struct i2c_client *client;
  43. struct work_struct work;
  44. u16 brightness;
  45. struct led_classdev led_cdev;
  46. int led_num; /* 0-15 */
  47. char name[32];
  48. };
  49. static void pca9685_write_msg(struct i2c_client *client, u8 *buf, u8 len)
  50. {
  51. struct i2c_msg msg = {
  52. .addr = client->addr,
  53. .flags = 0x00,
  54. .len = len,
  55. .buf = buf
  56. };
  57. i2c_transfer(client->adapter, &msg, 1);
  58. }
  59. static void pca9685_all_off(struct i2c_client *client)
  60. {
  61. u8 i2c_buffer[5] = {PCA9685_ALL_LED_ON_L, 0x00, 0x00, 0x00, 0x10};
  62. pca9685_write_msg(client, i2c_buffer, 5);
  63. }
  64. static void pca9685_led_work(struct work_struct *work)
  65. {
  66. struct pca9685_led *pca9685;
  67. u8 i2c_buffer[5];
  68. pca9685 = container_of(work, struct pca9685_led, work);
  69. i2c_buffer[0] = PCA9685_LED0_ON_L + 4 * pca9685->led_num;
  70. /*
  71. * 4095 is the maximum brightness, so we set the ON time to 0x1000
  72. * which disables the PWM generator for that LED
  73. */
  74. if (pca9685->brightness == 4095)
  75. *((__le16 *)(i2c_buffer+1)) = cpu_to_le16(0x1000);
  76. else
  77. *((__le16 *)(i2c_buffer+1)) = 0x0000;
  78. if (pca9685->brightness == 0)
  79. *((__le16 *)(i2c_buffer+3)) = cpu_to_le16(0x1000);
  80. else if (pca9685->brightness == 4095)
  81. *((__le16 *)(i2c_buffer+3)) = 0x0000;
  82. else
  83. *((__le16 *)(i2c_buffer+3)) = cpu_to_le16(pca9685->brightness);
  84. pca9685_write_msg(pca9685->client, i2c_buffer, 5);
  85. }
  86. static void pca9685_led_set(struct led_classdev *led_cdev,
  87. enum led_brightness value)
  88. {
  89. struct pca9685_led *pca9685;
  90. pca9685 = container_of(led_cdev, struct pca9685_led, led_cdev);
  91. pca9685->brightness = value;
  92. schedule_work(&pca9685->work);
  93. }
  94. static int pca9685_probe(struct i2c_client *client,
  95. const struct i2c_device_id *id)
  96. {
  97. struct pca9685_led *pca9685;
  98. struct pca9685_platform_data *pdata;
  99. int err;
  100. u8 i;
  101. pdata = dev_get_platdata(&client->dev);
  102. if (pdata) {
  103. if (pdata->leds.num_leds < 1 || pdata->leds.num_leds > 15) {
  104. dev_err(&client->dev, "board info must claim 1-16 LEDs");
  105. return -EINVAL;
  106. }
  107. }
  108. pca9685 = devm_kzalloc(&client->dev, 16 * sizeof(*pca9685), GFP_KERNEL);
  109. if (!pca9685)
  110. return -ENOMEM;
  111. i2c_set_clientdata(client, pca9685);
  112. pca9685_all_off(client);
  113. for (i = 0; i < 16; i++) {
  114. pca9685[i].client = client;
  115. pca9685[i].led_num = i;
  116. pca9685[i].name[0] = '\0';
  117. if (pdata && i < pdata->leds.num_leds) {
  118. if (pdata->leds.leds[i].name)
  119. strncpy(pca9685[i].name,
  120. pdata->leds.leds[i].name,
  121. sizeof(pca9685[i].name)-1);
  122. if (pdata->leds.leds[i].default_trigger)
  123. pca9685[i].led_cdev.default_trigger =
  124. pdata->leds.leds[i].default_trigger;
  125. }
  126. if (strlen(pca9685[i].name) == 0) {
  127. /*
  128. * Write adapter and address to the name as well.
  129. * Otherwise multiple chips attached to one host would
  130. * not work.
  131. */
  132. snprintf(pca9685[i].name, sizeof(pca9685[i].name),
  133. "pca9685:%d:x%.2x:%d",
  134. client->adapter->nr, client->addr, i);
  135. }
  136. pca9685[i].led_cdev.name = pca9685[i].name;
  137. pca9685[i].led_cdev.max_brightness = 0xfff;
  138. pca9685[i].led_cdev.brightness_set = pca9685_led_set;
  139. INIT_WORK(&pca9685[i].work, pca9685_led_work);
  140. err = led_classdev_register(&client->dev, &pca9685[i].led_cdev);
  141. if (err < 0)
  142. goto exit;
  143. }
  144. if (pdata)
  145. i2c_smbus_write_byte_data(client, PCA9685_MODE2,
  146. pdata->outdrv << PCA9685_OUTDRV |
  147. pdata->inverted << PCA9685_INVRT);
  148. else
  149. i2c_smbus_write_byte_data(client, PCA9685_MODE2,
  150. PCA9685_TOTEM_POLE << PCA9685_OUTDRV);
  151. /* Enable Auto-Increment, enable oscillator, ALLCALL/SUBADDR disabled */
  152. i2c_smbus_write_byte_data(client, PCA9685_MODE1, BIT(PCA9685_AI));
  153. return 0;
  154. exit:
  155. while (i--) {
  156. led_classdev_unregister(&pca9685[i].led_cdev);
  157. cancel_work_sync(&pca9685[i].work);
  158. }
  159. return err;
  160. }
  161. static int pca9685_remove(struct i2c_client *client)
  162. {
  163. struct pca9685_led *pca9685 = i2c_get_clientdata(client);
  164. u8 i;
  165. for (i = 0; i < 16; i++) {
  166. led_classdev_unregister(&pca9685[i].led_cdev);
  167. cancel_work_sync(&pca9685[i].work);
  168. }
  169. pca9685_all_off(client);
  170. return 0;
  171. }
  172. static struct i2c_driver pca9685_driver = {
  173. .driver = {
  174. .name = "leds-pca9685",
  175. .owner = THIS_MODULE,
  176. },
  177. .probe = pca9685_probe,
  178. .remove = pca9685_remove,
  179. .id_table = pca9685_id,
  180. };
  181. module_i2c_driver(pca9685_driver);
  182. MODULE_AUTHOR("Maximilian Güntner <maximilian.guentner@gmail.com>");
  183. MODULE_DESCRIPTION("PCA9685 LED Driver");
  184. MODULE_LICENSE("GPL v2");