leds-wm831x-status.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * LED driver for WM831x status LEDs
  3. *
  4. * Copyright(C) 2009 Wolfson Microelectronics PLC.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/slab.h>
  14. #include <linux/leds.h>
  15. #include <linux/err.h>
  16. #include <linux/mfd/wm831x/core.h>
  17. #include <linux/mfd/wm831x/pdata.h>
  18. #include <linux/mfd/wm831x/status.h>
  19. #include <linux/module.h>
  20. struct wm831x_status {
  21. struct led_classdev cdev;
  22. struct wm831x *wm831x;
  23. struct work_struct work;
  24. struct mutex mutex;
  25. spinlock_t value_lock;
  26. int reg; /* Control register */
  27. int reg_val; /* Control register value */
  28. int blink;
  29. int blink_time;
  30. int blink_cyc;
  31. int src;
  32. enum led_brightness brightness;
  33. };
  34. #define to_wm831x_status(led_cdev) \
  35. container_of(led_cdev, struct wm831x_status, cdev)
  36. static void wm831x_status_work(struct work_struct *work)
  37. {
  38. struct wm831x_status *led = container_of(work, struct wm831x_status,
  39. work);
  40. unsigned long flags;
  41. mutex_lock(&led->mutex);
  42. led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK |
  43. WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK);
  44. spin_lock_irqsave(&led->value_lock, flags);
  45. led->reg_val |= led->src << WM831X_LED_SRC_SHIFT;
  46. if (led->blink) {
  47. led->reg_val |= 2 << WM831X_LED_MODE_SHIFT;
  48. led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT;
  49. led->reg_val |= led->blink_cyc;
  50. } else {
  51. if (led->brightness != LED_OFF)
  52. led->reg_val |= 1 << WM831X_LED_MODE_SHIFT;
  53. }
  54. spin_unlock_irqrestore(&led->value_lock, flags);
  55. wm831x_reg_write(led->wm831x, led->reg, led->reg_val);
  56. mutex_unlock(&led->mutex);
  57. }
  58. static void wm831x_status_set(struct led_classdev *led_cdev,
  59. enum led_brightness value)
  60. {
  61. struct wm831x_status *led = to_wm831x_status(led_cdev);
  62. unsigned long flags;
  63. spin_lock_irqsave(&led->value_lock, flags);
  64. led->brightness = value;
  65. if (value == LED_OFF)
  66. led->blink = 0;
  67. schedule_work(&led->work);
  68. spin_unlock_irqrestore(&led->value_lock, flags);
  69. }
  70. static int wm831x_status_blink_set(struct led_classdev *led_cdev,
  71. unsigned long *delay_on,
  72. unsigned long *delay_off)
  73. {
  74. struct wm831x_status *led = to_wm831x_status(led_cdev);
  75. unsigned long flags;
  76. int ret = 0;
  77. /* Pick some defaults if we've not been given times */
  78. if (*delay_on == 0 && *delay_off == 0) {
  79. *delay_on = 250;
  80. *delay_off = 250;
  81. }
  82. spin_lock_irqsave(&led->value_lock, flags);
  83. /* We only have a limited selection of settings, see if we can
  84. * support the configuration we're being given */
  85. switch (*delay_on) {
  86. case 1000:
  87. led->blink_time = 0;
  88. break;
  89. case 250:
  90. led->blink_time = 1;
  91. break;
  92. case 125:
  93. led->blink_time = 2;
  94. break;
  95. case 62:
  96. case 63:
  97. /* Actually 62.5ms */
  98. led->blink_time = 3;
  99. break;
  100. default:
  101. ret = -EINVAL;
  102. break;
  103. }
  104. if (ret == 0) {
  105. switch (*delay_off / *delay_on) {
  106. case 1:
  107. led->blink_cyc = 0;
  108. break;
  109. case 3:
  110. led->blink_cyc = 1;
  111. break;
  112. case 4:
  113. led->blink_cyc = 2;
  114. break;
  115. case 8:
  116. led->blink_cyc = 3;
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. break;
  121. }
  122. }
  123. if (ret == 0)
  124. led->blink = 1;
  125. else
  126. led->blink = 0;
  127. /* Always update; if we fail turn off blinking since we expect
  128. * a software fallback. */
  129. schedule_work(&led->work);
  130. spin_unlock_irqrestore(&led->value_lock, flags);
  131. return ret;
  132. }
  133. static const char * const led_src_texts[] = {
  134. "otp",
  135. "power",
  136. "charger",
  137. "soft",
  138. };
  139. static ssize_t wm831x_status_src_show(struct device *dev,
  140. struct device_attribute *attr, char *buf)
  141. {
  142. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  143. struct wm831x_status *led = to_wm831x_status(led_cdev);
  144. int i;
  145. ssize_t ret = 0;
  146. mutex_lock(&led->mutex);
  147. for (i = 0; i < ARRAY_SIZE(led_src_texts); i++)
  148. if (i == led->src)
  149. ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]);
  150. else
  151. ret += sprintf(&buf[ret], "%s ", led_src_texts[i]);
  152. mutex_unlock(&led->mutex);
  153. ret += sprintf(&buf[ret], "\n");
  154. return ret;
  155. }
  156. static ssize_t wm831x_status_src_store(struct device *dev,
  157. struct device_attribute *attr,
  158. const char *buf, size_t size)
  159. {
  160. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  161. struct wm831x_status *led = to_wm831x_status(led_cdev);
  162. char name[20];
  163. int i;
  164. size_t len;
  165. name[sizeof(name) - 1] = '\0';
  166. strncpy(name, buf, sizeof(name) - 1);
  167. len = strlen(name);
  168. if (len && name[len - 1] == '\n')
  169. name[len - 1] = '\0';
  170. for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) {
  171. if (!strcmp(name, led_src_texts[i])) {
  172. mutex_lock(&led->mutex);
  173. led->src = i;
  174. schedule_work(&led->work);
  175. mutex_unlock(&led->mutex);
  176. }
  177. }
  178. return size;
  179. }
  180. static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store);
  181. static struct attribute *wm831x_status_attrs[] = {
  182. &dev_attr_src.attr,
  183. NULL
  184. };
  185. ATTRIBUTE_GROUPS(wm831x_status);
  186. static int wm831x_status_probe(struct platform_device *pdev)
  187. {
  188. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  189. struct wm831x_pdata *chip_pdata;
  190. struct wm831x_status_pdata pdata;
  191. struct wm831x_status *drvdata;
  192. struct resource *res;
  193. int id = pdev->id % ARRAY_SIZE(chip_pdata->status);
  194. int ret;
  195. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  196. if (res == NULL) {
  197. dev_err(&pdev->dev, "No register resource\n");
  198. return -EINVAL;
  199. }
  200. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_status),
  201. GFP_KERNEL);
  202. if (!drvdata)
  203. return -ENOMEM;
  204. platform_set_drvdata(pdev, drvdata);
  205. drvdata->wm831x = wm831x;
  206. drvdata->reg = res->start;
  207. if (dev_get_platdata(wm831x->dev))
  208. chip_pdata = dev_get_platdata(wm831x->dev);
  209. else
  210. chip_pdata = NULL;
  211. memset(&pdata, 0, sizeof(pdata));
  212. if (chip_pdata && chip_pdata->status[id])
  213. memcpy(&pdata, chip_pdata->status[id], sizeof(pdata));
  214. else
  215. pdata.name = dev_name(&pdev->dev);
  216. mutex_init(&drvdata->mutex);
  217. INIT_WORK(&drvdata->work, wm831x_status_work);
  218. spin_lock_init(&drvdata->value_lock);
  219. /* We cache the configuration register and read startup values
  220. * from it. */
  221. drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg);
  222. if (drvdata->reg_val & WM831X_LED_MODE_MASK)
  223. drvdata->brightness = LED_FULL;
  224. else
  225. drvdata->brightness = LED_OFF;
  226. /* Set a default source if configured, otherwise leave the
  227. * current hardware setting.
  228. */
  229. if (pdata.default_src == WM831X_STATUS_PRESERVE) {
  230. drvdata->src = drvdata->reg_val;
  231. drvdata->src &= WM831X_LED_SRC_MASK;
  232. drvdata->src >>= WM831X_LED_SRC_SHIFT;
  233. } else {
  234. drvdata->src = pdata.default_src - 1;
  235. }
  236. drvdata->cdev.name = pdata.name;
  237. drvdata->cdev.default_trigger = pdata.default_trigger;
  238. drvdata->cdev.brightness_set = wm831x_status_set;
  239. drvdata->cdev.blink_set = wm831x_status_blink_set;
  240. drvdata->cdev.groups = wm831x_status_groups;
  241. ret = led_classdev_register(wm831x->dev, &drvdata->cdev);
  242. if (ret < 0) {
  243. dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
  244. return ret;
  245. }
  246. return 0;
  247. }
  248. static int wm831x_status_remove(struct platform_device *pdev)
  249. {
  250. struct wm831x_status *drvdata = platform_get_drvdata(pdev);
  251. led_classdev_unregister(&drvdata->cdev);
  252. return 0;
  253. }
  254. static struct platform_driver wm831x_status_driver = {
  255. .driver = {
  256. .name = "wm831x-status",
  257. },
  258. .probe = wm831x_status_probe,
  259. .remove = wm831x_status_remove,
  260. };
  261. module_platform_driver(wm831x_status_driver);
  262. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  263. MODULE_DESCRIPTION("WM831x status LED driver");
  264. MODULE_LICENSE("GPL");
  265. MODULE_ALIAS("platform:wm831x-status");