leds-88pm860x.c 6.2 KB

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