leds-pca9633.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright 2011 bct electronic GmbH
  3. *
  4. * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  5. *
  6. * Based on leds-pca955x.c
  7. *
  8. * This file is subject to the terms and conditions of version 2 of
  9. * the GNU General Public License. See the file COPYING in the main
  10. * directory of this archive for more details.
  11. *
  12. * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/string.h>
  18. #include <linux/ctype.h>
  19. #include <linux/leds.h>
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_data/leds-pca9633.h>
  25. /* LED select registers determine the source that drives LED outputs */
  26. #define PCA9633_LED_OFF 0x0 /* LED driver off */
  27. #define PCA9633_LED_ON 0x1 /* LED driver on */
  28. #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */
  29. #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */
  30. #define PCA9633_MODE1 0x00
  31. #define PCA9633_MODE2 0x01
  32. #define PCA9633_PWM_BASE 0x02
  33. #define PCA9633_LEDOUT 0x08
  34. static const struct i2c_device_id pca9633_id[] = {
  35. { "pca9633", 0 },
  36. { }
  37. };
  38. MODULE_DEVICE_TABLE(i2c, pca9633_id);
  39. struct pca9633_led {
  40. struct i2c_client *client;
  41. struct work_struct work;
  42. enum led_brightness brightness;
  43. struct led_classdev led_cdev;
  44. int led_num; /* 0 .. 3 potentially */
  45. char name[32];
  46. };
  47. static void pca9633_led_work(struct work_struct *work)
  48. {
  49. struct pca9633_led *pca9633 = container_of(work,
  50. struct pca9633_led, work);
  51. u8 ledout = i2c_smbus_read_byte_data(pca9633->client, PCA9633_LEDOUT);
  52. int shift = 2 * pca9633->led_num;
  53. u8 mask = 0x3 << shift;
  54. switch (pca9633->brightness) {
  55. case LED_FULL:
  56. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  57. (ledout & ~mask) | (PCA9633_LED_ON << shift));
  58. break;
  59. case LED_OFF:
  60. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  61. ledout & ~mask);
  62. break;
  63. default:
  64. i2c_smbus_write_byte_data(pca9633->client,
  65. PCA9633_PWM_BASE + pca9633->led_num,
  66. pca9633->brightness);
  67. i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT,
  68. (ledout & ~mask) | (PCA9633_LED_PWM << shift));
  69. break;
  70. }
  71. }
  72. static void pca9633_led_set(struct led_classdev *led_cdev,
  73. enum led_brightness value)
  74. {
  75. struct pca9633_led *pca9633;
  76. pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev);
  77. pca9633->brightness = value;
  78. /*
  79. * Must use workqueue for the actual I/O since I2C operations
  80. * can sleep.
  81. */
  82. schedule_work(&pca9633->work);
  83. }
  84. static int pca9633_probe(struct i2c_client *client,
  85. const struct i2c_device_id *id)
  86. {
  87. struct pca9633_led *pca9633;
  88. struct pca9633_platform_data *pdata;
  89. int i, err;
  90. pdata = client->dev.platform_data;
  91. if (pdata) {
  92. if (pdata->leds.num_leds <= 0 || pdata->leds.num_leds > 4) {
  93. dev_err(&client->dev, "board info must claim at most 4 LEDs");
  94. return -EINVAL;
  95. }
  96. }
  97. pca9633 = devm_kzalloc(&client->dev, 4 * sizeof(*pca9633), GFP_KERNEL);
  98. if (!pca9633)
  99. return -ENOMEM;
  100. i2c_set_clientdata(client, pca9633);
  101. for (i = 0; i < 4; i++) {
  102. pca9633[i].client = client;
  103. pca9633[i].led_num = i;
  104. /* Platform data can specify LED names and default triggers */
  105. if (pdata && i < pdata->leds.num_leds) {
  106. if (pdata->leds.leds[i].name)
  107. snprintf(pca9633[i].name,
  108. sizeof(pca9633[i].name), "pca9633:%s",
  109. pdata->leds.leds[i].name);
  110. if (pdata->leds.leds[i].default_trigger)
  111. pca9633[i].led_cdev.default_trigger =
  112. pdata->leds.leds[i].default_trigger;
  113. } else {
  114. snprintf(pca9633[i].name, sizeof(pca9633[i].name),
  115. "pca9633:%d", i);
  116. }
  117. pca9633[i].led_cdev.name = pca9633[i].name;
  118. pca9633[i].led_cdev.brightness_set = pca9633_led_set;
  119. INIT_WORK(&pca9633[i].work, pca9633_led_work);
  120. err = led_classdev_register(&client->dev, &pca9633[i].led_cdev);
  121. if (err < 0)
  122. goto exit;
  123. }
  124. /* Disable LED all-call address and set normal mode */
  125. i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00);
  126. /* Configure output: open-drain or totem pole (push-pull) */
  127. if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN)
  128. i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01);
  129. /* Turn off LEDs */
  130. i2c_smbus_write_byte_data(client, PCA9633_LEDOUT, 0x00);
  131. return 0;
  132. exit:
  133. while (i--) {
  134. led_classdev_unregister(&pca9633[i].led_cdev);
  135. cancel_work_sync(&pca9633[i].work);
  136. }
  137. return err;
  138. }
  139. static int pca9633_remove(struct i2c_client *client)
  140. {
  141. struct pca9633_led *pca9633 = i2c_get_clientdata(client);
  142. int i;
  143. for (i = 0; i < 4; i++) {
  144. led_classdev_unregister(&pca9633[i].led_cdev);
  145. cancel_work_sync(&pca9633[i].work);
  146. }
  147. return 0;
  148. }
  149. static struct i2c_driver pca9633_driver = {
  150. .driver = {
  151. .name = "leds-pca9633",
  152. .owner = THIS_MODULE,
  153. },
  154. .probe = pca9633_probe,
  155. .remove = pca9633_remove,
  156. .id_table = pca9633_id,
  157. };
  158. module_i2c_driver(pca9633_driver);
  159. MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
  160. MODULE_DESCRIPTION("PCA9633 LED driver");
  161. MODULE_LICENSE("GPL v2");