leds-88pm860x.c 5.6 KB

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