leds-88pm860x.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/of.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_MASK (0x1F)
  22. #define LED_CURRENT_MASK (0x07 << 5)
  23. #define LED_BLINK_MASK (0x7F)
  24. #define LED_ON_CONTINUOUS (0x0F << 3)
  25. #define LED1_BLINK_EN (1 << 1)
  26. #define LED2_BLINK_EN (1 << 2)
  27. struct pm860x_led {
  28. struct led_classdev cdev;
  29. struct i2c_client *i2c;
  30. struct work_struct work;
  31. struct pm860x_chip *chip;
  32. struct mutex lock;
  33. char name[MFD_NAME_SIZE];
  34. int port;
  35. int iset;
  36. unsigned char brightness;
  37. unsigned char current_brightness;
  38. int reg_control;
  39. int reg_blink;
  40. int blink_mask;
  41. };
  42. static int led_power_set(struct pm860x_chip *chip, int port, int on)
  43. {
  44. int ret = -EINVAL;
  45. switch (port) {
  46. case 0:
  47. case 1:
  48. case 2:
  49. ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
  50. pm8606_osc_disable(chip, RGB1_ENABLE);
  51. break;
  52. case 3:
  53. case 4:
  54. case 5:
  55. ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
  56. pm8606_osc_disable(chip, RGB2_ENABLE);
  57. break;
  58. }
  59. return ret;
  60. }
  61. static void pm860x_led_work(struct work_struct *work)
  62. {
  63. struct pm860x_led *led;
  64. struct pm860x_chip *chip;
  65. unsigned char buf[3];
  66. int ret;
  67. led = container_of(work, struct pm860x_led, work);
  68. chip = led->chip;
  69. mutex_lock(&led->lock);
  70. if ((led->current_brightness == 0) && led->brightness) {
  71. led_power_set(chip, led->port, 1);
  72. if (led->iset) {
  73. pm860x_set_bits(led->i2c, led->reg_control,
  74. LED_CURRENT_MASK, led->iset);
  75. }
  76. pm860x_set_bits(led->i2c, led->reg_blink,
  77. LED_BLINK_MASK, LED_ON_CONTINUOUS);
  78. pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
  79. led->blink_mask);
  80. }
  81. pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
  82. led->brightness);
  83. if (led->brightness == 0) {
  84. pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
  85. ret = buf[0] & LED_PWM_MASK;
  86. ret |= buf[1] & LED_PWM_MASK;
  87. ret |= buf[2] & LED_PWM_MASK;
  88. if (ret == 0) {
  89. /* unset current since no led is lighting */
  90. pm860x_set_bits(led->i2c, led->reg_control,
  91. LED_CURRENT_MASK, 0);
  92. pm860x_set_bits(led->i2c, PM8606_WLED3B,
  93. led->blink_mask, 0);
  94. led_power_set(chip, led->port, 0);
  95. }
  96. }
  97. led->current_brightness = led->brightness;
  98. dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
  99. led->reg_control, led->brightness);
  100. mutex_unlock(&led->lock);
  101. }
  102. static void pm860x_led_set(struct led_classdev *cdev,
  103. enum led_brightness value)
  104. {
  105. struct pm860x_led *data = container_of(cdev, struct pm860x_led, cdev);
  106. data->brightness = value >> 3;
  107. schedule_work(&data->work);
  108. }
  109. #ifdef CONFIG_OF
  110. static int pm860x_led_dt_init(struct platform_device *pdev,
  111. struct pm860x_led *data)
  112. {
  113. struct device_node *nproot, *np;
  114. int iset = 0;
  115. if (!pdev->dev.parent->of_node)
  116. return -ENODEV;
  117. nproot = of_get_child_by_name(pdev->dev.parent->of_node, "leds");
  118. if (!nproot) {
  119. dev_err(&pdev->dev, "failed to find leds node\n");
  120. return -ENODEV;
  121. }
  122. for_each_child_of_node(nproot, np) {
  123. if (!of_node_cmp(np->name, data->name)) {
  124. of_property_read_u32(np, "marvell,88pm860x-iset",
  125. &iset);
  126. data->iset = PM8606_LED_CURRENT(iset);
  127. break;
  128. }
  129. }
  130. of_node_put(nproot);
  131. return 0;
  132. }
  133. #else
  134. #define pm860x_led_dt_init(x, y) (-1)
  135. #endif
  136. static int pm860x_led_probe(struct platform_device *pdev)
  137. {
  138. struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
  139. struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev);
  140. struct pm860x_led *data;
  141. struct resource *res;
  142. int ret = 0;
  143. data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
  144. if (data == NULL)
  145. return -ENOMEM;
  146. res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
  147. if (!res) {
  148. dev_err(&pdev->dev, "No REG resource for control\n");
  149. return -ENXIO;
  150. }
  151. data->reg_control = res->start;
  152. res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink");
  153. if (!res) {
  154. dev_err(&pdev->dev, "No REG resource for blink\n");
  155. return -ENXIO;
  156. }
  157. data->reg_blink = res->start;
  158. memset(data->name, 0, MFD_NAME_SIZE);
  159. switch (pdev->id) {
  160. case 0:
  161. data->blink_mask = LED1_BLINK_EN;
  162. sprintf(data->name, "led0-red");
  163. break;
  164. case 1:
  165. data->blink_mask = LED1_BLINK_EN;
  166. sprintf(data->name, "led0-green");
  167. break;
  168. case 2:
  169. data->blink_mask = LED1_BLINK_EN;
  170. sprintf(data->name, "led0-blue");
  171. break;
  172. case 3:
  173. data->blink_mask = LED2_BLINK_EN;
  174. sprintf(data->name, "led1-red");
  175. break;
  176. case 4:
  177. data->blink_mask = LED2_BLINK_EN;
  178. sprintf(data->name, "led1-green");
  179. break;
  180. case 5:
  181. data->blink_mask = LED2_BLINK_EN;
  182. sprintf(data->name, "led1-blue");
  183. break;
  184. }
  185. platform_set_drvdata(pdev, data);
  186. data->chip = chip;
  187. data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
  188. data->port = pdev->id;
  189. if (pm860x_led_dt_init(pdev, data))
  190. if (pdata)
  191. data->iset = pdata->iset;
  192. data->current_brightness = 0;
  193. data->cdev.name = data->name;
  194. data->cdev.brightness_set = pm860x_led_set;
  195. mutex_init(&data->lock);
  196. INIT_WORK(&data->work, pm860x_led_work);
  197. ret = led_classdev_register(chip->dev, &data->cdev);
  198. if (ret < 0) {
  199. dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
  200. return ret;
  201. }
  202. pm860x_led_set(&data->cdev, 0);
  203. return 0;
  204. }
  205. static int pm860x_led_remove(struct platform_device *pdev)
  206. {
  207. struct pm860x_led *data = platform_get_drvdata(pdev);
  208. led_classdev_unregister(&data->cdev);
  209. return 0;
  210. }
  211. static struct platform_driver pm860x_led_driver = {
  212. .driver = {
  213. .name = "88pm860x-led",
  214. },
  215. .probe = pm860x_led_probe,
  216. .remove = pm860x_led_remove,
  217. };
  218. module_platform_driver(pm860x_led_driver);
  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");