leds-wm831x-status.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/leds.h>
  16. #include <linux/err.h>
  17. #include <linux/mfd/wm831x/core.h>
  18. #include <linux/mfd/wm831x/pdata.h>
  19. #include <linux/mfd/wm831x/status.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 *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 int wm831x_status_probe(struct platform_device *pdev)
  182. {
  183. struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
  184. struct wm831x_pdata *chip_pdata;
  185. struct wm831x_status_pdata pdata;
  186. struct wm831x_status *drvdata;
  187. struct resource *res;
  188. int id = pdev->id % ARRAY_SIZE(chip_pdata->status);
  189. int ret;
  190. res = platform_get_resource(pdev, IORESOURCE_IO, 0);
  191. if (res == NULL) {
  192. dev_err(&pdev->dev, "No I/O resource\n");
  193. ret = -EINVAL;
  194. goto err;
  195. }
  196. drvdata = kzalloc(sizeof(struct wm831x_status), GFP_KERNEL);
  197. if (!drvdata)
  198. return -ENOMEM;
  199. dev_set_drvdata(&pdev->dev, drvdata);
  200. drvdata->wm831x = wm831x;
  201. drvdata->reg = res->start;
  202. if (wm831x->dev->platform_data)
  203. chip_pdata = wm831x->dev->platform_data;
  204. else
  205. chip_pdata = NULL;
  206. memset(&pdata, 0, sizeof(pdata));
  207. if (chip_pdata && chip_pdata->status[id])
  208. memcpy(&pdata, chip_pdata->status[id], sizeof(pdata));
  209. else
  210. pdata.name = dev_name(&pdev->dev);
  211. mutex_init(&drvdata->mutex);
  212. INIT_WORK(&drvdata->work, wm831x_status_work);
  213. spin_lock_init(&drvdata->value_lock);
  214. /* We cache the configuration register and read startup values
  215. * from it. */
  216. drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg);
  217. if (drvdata->reg_val & WM831X_LED_MODE_MASK)
  218. drvdata->brightness = LED_FULL;
  219. else
  220. drvdata->brightness = LED_OFF;
  221. /* Set a default source if configured, otherwise leave the
  222. * current hardware setting.
  223. */
  224. if (pdata.default_src == WM831X_STATUS_PRESERVE) {
  225. drvdata->src = drvdata->reg_val;
  226. drvdata->src &= WM831X_LED_SRC_MASK;
  227. drvdata->src >>= WM831X_LED_SRC_SHIFT;
  228. } else {
  229. drvdata->src = pdata.default_src - 1;
  230. }
  231. drvdata->cdev.name = pdata.name;
  232. drvdata->cdev.default_trigger = pdata.default_trigger;
  233. drvdata->cdev.brightness_set = wm831x_status_set;
  234. drvdata->cdev.blink_set = wm831x_status_blink_set;
  235. ret = led_classdev_register(wm831x->dev, &drvdata->cdev);
  236. if (ret < 0) {
  237. dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
  238. goto err_led;
  239. }
  240. ret = device_create_file(drvdata->cdev.dev, &dev_attr_src);
  241. if (ret != 0)
  242. dev_err(&pdev->dev,
  243. "No source control for LED: %d\n", ret);
  244. return 0;
  245. err_led:
  246. led_classdev_unregister(&drvdata->cdev);
  247. kfree(drvdata);
  248. err:
  249. return ret;
  250. }
  251. static int wm831x_status_remove(struct platform_device *pdev)
  252. {
  253. struct wm831x_status *drvdata = platform_get_drvdata(pdev);
  254. device_remove_file(drvdata->cdev.dev, &dev_attr_src);
  255. led_classdev_unregister(&drvdata->cdev);
  256. kfree(drvdata);
  257. return 0;
  258. }
  259. static struct platform_driver wm831x_status_driver = {
  260. .driver = {
  261. .name = "wm831x-status",
  262. .owner = THIS_MODULE,
  263. },
  264. .probe = wm831x_status_probe,
  265. .remove = wm831x_status_remove,
  266. };
  267. static int __devinit wm831x_status_init(void)
  268. {
  269. return platform_driver_register(&wm831x_status_driver);
  270. }
  271. module_init(wm831x_status_init);
  272. static void wm831x_status_exit(void)
  273. {
  274. platform_driver_unregister(&wm831x_status_driver);
  275. }
  276. module_exit(wm831x_status_exit);
  277. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  278. MODULE_DESCRIPTION("WM831x status LED driver");
  279. MODULE_LICENSE("GPL");
  280. MODULE_ALIAS("platform:wm831x-status");