leds-88pm860x.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * LED driver for Marvell 88PM860x
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/i2c.h>
  16. #include <linux/leds.h>
  17. #include <linux/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/mfd/88pm860x.h>
  20. #define LED_PWM_SHIFT (3)
  21. #define LED_PWM_MASK (0x1F)
  22. #define LED_CURRENT_MASK (0x07 << 5)
  23. #define LED_BLINK_ON_MASK (0x07)
  24. #define LED_BLINK_MASK (0x7F)
  25. #define LED_BLINK_ON(x) ((x & 0x7) * 66 + 66)
  26. #define LED_BLINK_ON_MIN LED_BLINK_ON(0)
  27. #define LED_BLINK_ON_MAX LED_BLINK_ON(0x7)
  28. #define LED_ON_CONTINUOUS (0x0F << 3)
  29. #define LED_TO_ON(x) ((x - 66) / 66)
  30. #define LED1_BLINK_EN (1 << 1)
  31. #define LED2_BLINK_EN (1 << 2)
  32. struct pm860x_led {
  33. struct led_classdev cdev;
  34. struct i2c_client *i2c;
  35. struct work_struct work;
  36. struct pm860x_chip *chip;
  37. struct mutex lock;
  38. char name[MFD_NAME_SIZE];
  39. int port;
  40. int iset;
  41. unsigned char brightness;
  42. unsigned char current_brightness;
  43. int blink_data;
  44. int blink_time;
  45. int blink_on;
  46. int blink_off;
  47. };
  48. /* return offset of color register */
  49. static inline int __led_off(int port)
  50. {
  51. int ret = -EINVAL;
  52. switch (port) {
  53. case PM8606_LED1_RED:
  54. case PM8606_LED1_GREEN:
  55. case PM8606_LED1_BLUE:
  56. ret = port - PM8606_LED1_RED + PM8606_RGB1B;
  57. break;
  58. case PM8606_LED2_RED:
  59. case PM8606_LED2_GREEN:
  60. case PM8606_LED2_BLUE:
  61. ret = port - PM8606_LED2_RED + PM8606_RGB2B;
  62. break;
  63. }
  64. return ret;
  65. }
  66. /* return offset of blink register */
  67. static inline int __blink_off(int port)
  68. {
  69. int ret = -EINVAL;
  70. switch (port) {
  71. case PM8606_LED1_RED:
  72. case PM8606_LED1_GREEN:
  73. case PM8606_LED1_BLUE:
  74. ret = PM8606_RGB1A;
  75. break;
  76. case PM8606_LED2_RED:
  77. case PM8606_LED2_GREEN:
  78. case PM8606_LED2_BLUE:
  79. ret = PM8606_RGB2A;
  80. break;
  81. }
  82. return ret;
  83. }
  84. static inline int __blink_ctl_mask(int port)
  85. {
  86. int ret = -EINVAL;
  87. switch (port) {
  88. case PM8606_LED1_RED:
  89. case PM8606_LED1_GREEN:
  90. case PM8606_LED1_BLUE:
  91. ret = LED1_BLINK_EN;
  92. break;
  93. case PM8606_LED2_RED:
  94. case PM8606_LED2_GREEN:
  95. case PM8606_LED2_BLUE:
  96. ret = LED2_BLINK_EN;
  97. break;
  98. }
  99. return ret;
  100. }
  101. static void pm860x_led_work(struct work_struct *work)
  102. {
  103. struct pm860x_led *led;
  104. struct pm860x_chip *chip;
  105. unsigned char buf[3];
  106. int mask, ret;
  107. led = container_of(work, struct pm860x_led, work);
  108. chip = led->chip;
  109. mutex_lock(&led->lock);
  110. if ((led->current_brightness == 0) && led->brightness) {
  111. if (led->iset) {
  112. pm860x_set_bits(led->i2c, __led_off(led->port),
  113. LED_CURRENT_MASK, led->iset);
  114. }
  115. pm860x_set_bits(led->i2c, __blink_off(led->port),
  116. LED_BLINK_MASK, LED_ON_CONTINUOUS);
  117. mask = __blink_ctl_mask(led->port);
  118. pm860x_set_bits(led->i2c, PM8606_WLED3B, mask, mask);
  119. }
  120. pm860x_set_bits(led->i2c, __led_off(led->port), LED_PWM_MASK,
  121. led->brightness);
  122. if (led->brightness == 0) {
  123. pm860x_bulk_read(led->i2c, __led_off(led->port), 3, buf);
  124. ret = buf[0] & LED_PWM_MASK;
  125. ret |= buf[1] & LED_PWM_MASK;
  126. ret |= buf[2] & LED_PWM_MASK;
  127. if (ret == 0) {
  128. /* unset current since no led is lighting */
  129. pm860x_set_bits(led->i2c, __led_off(led->port),
  130. LED_CURRENT_MASK, 0);
  131. mask = __blink_ctl_mask(led->port);
  132. pm860x_set_bits(led->i2c, PM8606_WLED3B, mask, 0);
  133. }
  134. }
  135. led->current_brightness = led->brightness;
  136. dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
  137. __led_off(led->port), led->brightness);
  138. mutex_unlock(&led->lock);
  139. }
  140. static void pm860x_led_set(struct led_classdev *cdev,
  141. enum led_brightness value)
  142. {
  143. struct pm860x_led *data = container_of(cdev, struct pm860x_led, cdev);
  144. data->brightness = value >> 3;
  145. schedule_work(&data->work);
  146. }
  147. static int pm860x_led_probe(struct platform_device *pdev)
  148. {
  149. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  150. struct pm860x_led_pdata *pdata;
  151. struct pm860x_led *data;
  152. struct resource *res;
  153. int ret;
  154. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  155. if (res == NULL) {
  156. dev_err(&pdev->dev, "No I/O resource!\n");
  157. return -EINVAL;
  158. }
  159. pdata = pdev->dev.platform_data;
  160. if (pdata == NULL) {
  161. dev_err(&pdev->dev, "No platform data!\n");
  162. return -EINVAL;
  163. }
  164. data = kzalloc(sizeof(struct pm860x_led), GFP_KERNEL);
  165. if (data == NULL)
  166. return -ENOMEM;
  167. strncpy(data->name, res->name, MFD_NAME_SIZE - 1);
  168. dev_set_drvdata(&pdev->dev, data);
  169. data->chip = chip;
  170. data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
  171. data->iset = pdata->iset;
  172. data->port = pdata->flags;
  173. if (data->port < 0) {
  174. dev_err(&pdev->dev, "check device failed\n");
  175. kfree(data);
  176. return -EINVAL;
  177. }
  178. data->current_brightness = 0;
  179. data->cdev.name = data->name;
  180. data->cdev.brightness_set = pm860x_led_set;
  181. mutex_init(&data->lock);
  182. INIT_WORK(&data->work, pm860x_led_work);
  183. ret = led_classdev_register(chip->dev, &data->cdev);
  184. if (ret < 0) {
  185. dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
  186. goto out;
  187. }
  188. pm860x_led_set(&data->cdev, 0);
  189. return 0;
  190. out:
  191. kfree(data);
  192. return ret;
  193. }
  194. static int pm860x_led_remove(struct platform_device *pdev)
  195. {
  196. struct pm860x_led *data = platform_get_drvdata(pdev);
  197. led_classdev_unregister(&data->cdev);
  198. kfree(data);
  199. return 0;
  200. }
  201. static struct platform_driver pm860x_led_driver = {
  202. .driver = {
  203. .name = "88pm860x-led",
  204. .owner = THIS_MODULE,
  205. },
  206. .probe = pm860x_led_probe,
  207. .remove = pm860x_led_remove,
  208. };
  209. static int __devinit pm860x_led_init(void)
  210. {
  211. return platform_driver_register(&pm860x_led_driver);
  212. }
  213. module_init(pm860x_led_init);
  214. static void __devexit pm860x_led_exit(void)
  215. {
  216. platform_driver_unregister(&pm860x_led_driver);
  217. }
  218. module_exit(pm860x_led_exit);
  219. MODULE_DESCRIPTION("LED driver for Marvell PM860x");
  220. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  221. MODULE_LICENSE("GPL");
  222. MODULE_ALIAS("platform:88pm860x-led");