leds-ns2.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
  3. *
  4. * Copyright (C) 2010 LaCie
  5. *
  6. * Author: Simon Guinot <sguinot@lacie.com>
  7. *
  8. * Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/slab.h>
  28. #include <linux/gpio.h>
  29. #include <linux/leds.h>
  30. #include <linux/module.h>
  31. #include <mach/leds-ns2.h>
  32. /*
  33. * The Network Space v2 dual-GPIO LED is wired to a CPLD and can blink in
  34. * relation with the SATA activity. This capability is exposed through the
  35. * "sata" sysfs attribute.
  36. *
  37. * The following array detail the different LED registers and the combination
  38. * of their possible values:
  39. *
  40. * cmd_led | slow_led | /SATA active | LED state
  41. * | | |
  42. * 1 | 0 | x | off
  43. * - | 1 | x | on
  44. * 0 | 0 | 1 | on
  45. * 0 | 0 | 0 | blink (rate 300ms)
  46. */
  47. enum ns2_led_modes {
  48. NS_V2_LED_OFF,
  49. NS_V2_LED_ON,
  50. NS_V2_LED_SATA,
  51. };
  52. struct ns2_led_mode_value {
  53. enum ns2_led_modes mode;
  54. int cmd_level;
  55. int slow_level;
  56. };
  57. static struct ns2_led_mode_value ns2_led_modval[] = {
  58. { NS_V2_LED_OFF , 1, 0 },
  59. { NS_V2_LED_ON , 0, 1 },
  60. { NS_V2_LED_ON , 1, 1 },
  61. { NS_V2_LED_SATA, 0, 0 },
  62. };
  63. struct ns2_led_data {
  64. struct led_classdev cdev;
  65. unsigned cmd;
  66. unsigned slow;
  67. unsigned char sata; /* True when SATA mode active. */
  68. rwlock_t rw_lock; /* Lock GPIOs. */
  69. };
  70. static int ns2_led_get_mode(struct ns2_led_data *led_dat,
  71. enum ns2_led_modes *mode)
  72. {
  73. int i;
  74. int ret = -EINVAL;
  75. int cmd_level;
  76. int slow_level;
  77. read_lock_irq(&led_dat->rw_lock);
  78. cmd_level = gpio_get_value(led_dat->cmd);
  79. slow_level = gpio_get_value(led_dat->slow);
  80. for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
  81. if (cmd_level == ns2_led_modval[i].cmd_level &&
  82. slow_level == ns2_led_modval[i].slow_level) {
  83. *mode = ns2_led_modval[i].mode;
  84. ret = 0;
  85. break;
  86. }
  87. }
  88. read_unlock_irq(&led_dat->rw_lock);
  89. return ret;
  90. }
  91. static void ns2_led_set_mode(struct ns2_led_data *led_dat,
  92. enum ns2_led_modes mode)
  93. {
  94. int i;
  95. unsigned long flags;
  96. write_lock_irqsave(&led_dat->rw_lock, flags);
  97. for (i = 0; i < ARRAY_SIZE(ns2_led_modval); i++) {
  98. if (mode == ns2_led_modval[i].mode) {
  99. gpio_set_value(led_dat->cmd,
  100. ns2_led_modval[i].cmd_level);
  101. gpio_set_value(led_dat->slow,
  102. ns2_led_modval[i].slow_level);
  103. }
  104. }
  105. write_unlock_irqrestore(&led_dat->rw_lock, flags);
  106. }
  107. static void ns2_led_set(struct led_classdev *led_cdev,
  108. enum led_brightness value)
  109. {
  110. struct ns2_led_data *led_dat =
  111. container_of(led_cdev, struct ns2_led_data, cdev);
  112. enum ns2_led_modes mode;
  113. if (value == LED_OFF)
  114. mode = NS_V2_LED_OFF;
  115. else if (led_dat->sata)
  116. mode = NS_V2_LED_SATA;
  117. else
  118. mode = NS_V2_LED_ON;
  119. ns2_led_set_mode(led_dat, mode);
  120. }
  121. static ssize_t ns2_led_sata_store(struct device *dev,
  122. struct device_attribute *attr,
  123. const char *buff, size_t count)
  124. {
  125. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  126. struct ns2_led_data *led_dat =
  127. container_of(led_cdev, struct ns2_led_data, cdev);
  128. int ret;
  129. unsigned long enable;
  130. enum ns2_led_modes mode;
  131. ret = strict_strtoul(buff, 10, &enable);
  132. if (ret < 0)
  133. return ret;
  134. enable = !!enable;
  135. if (led_dat->sata == enable)
  136. return count;
  137. ret = ns2_led_get_mode(led_dat, &mode);
  138. if (ret < 0)
  139. return ret;
  140. if (enable && mode == NS_V2_LED_ON)
  141. ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
  142. if (!enable && mode == NS_V2_LED_SATA)
  143. ns2_led_set_mode(led_dat, NS_V2_LED_ON);
  144. led_dat->sata = enable;
  145. return count;
  146. }
  147. static ssize_t ns2_led_sata_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  151. struct ns2_led_data *led_dat =
  152. container_of(led_cdev, struct ns2_led_data, cdev);
  153. return sprintf(buf, "%d\n", led_dat->sata);
  154. }
  155. static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
  156. static int __devinit
  157. create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
  158. const struct ns2_led *template)
  159. {
  160. int ret;
  161. enum ns2_led_modes mode;
  162. ret = gpio_request(template->cmd, template->name);
  163. if (ret == 0) {
  164. ret = gpio_direction_output(template->cmd,
  165. gpio_get_value(template->cmd));
  166. if (ret)
  167. gpio_free(template->cmd);
  168. }
  169. if (ret) {
  170. dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
  171. template->name);
  172. }
  173. ret = gpio_request(template->slow, template->name);
  174. if (ret == 0) {
  175. ret = gpio_direction_output(template->slow,
  176. gpio_get_value(template->slow));
  177. if (ret)
  178. gpio_free(template->slow);
  179. }
  180. if (ret) {
  181. dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
  182. template->name);
  183. goto err_free_cmd;
  184. }
  185. rwlock_init(&led_dat->rw_lock);
  186. led_dat->cdev.name = template->name;
  187. led_dat->cdev.default_trigger = template->default_trigger;
  188. led_dat->cdev.blink_set = NULL;
  189. led_dat->cdev.brightness_set = ns2_led_set;
  190. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  191. led_dat->cmd = template->cmd;
  192. led_dat->slow = template->slow;
  193. ret = ns2_led_get_mode(led_dat, &mode);
  194. if (ret < 0)
  195. goto err_free_slow;
  196. /* Set LED initial state. */
  197. led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
  198. led_dat->cdev.brightness =
  199. (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
  200. ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
  201. if (ret < 0)
  202. goto err_free_slow;
  203. ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
  204. if (ret < 0)
  205. goto err_free_cdev;
  206. return 0;
  207. err_free_cdev:
  208. led_classdev_unregister(&led_dat->cdev);
  209. err_free_slow:
  210. gpio_free(led_dat->slow);
  211. err_free_cmd:
  212. gpio_free(led_dat->cmd);
  213. return ret;
  214. }
  215. static void delete_ns2_led(struct ns2_led_data *led_dat)
  216. {
  217. device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
  218. led_classdev_unregister(&led_dat->cdev);
  219. gpio_free(led_dat->cmd);
  220. gpio_free(led_dat->slow);
  221. }
  222. static int __devinit ns2_led_probe(struct platform_device *pdev)
  223. {
  224. struct ns2_led_platform_data *pdata = pdev->dev.platform_data;
  225. struct ns2_led_data *leds_data;
  226. int i;
  227. int ret;
  228. if (!pdata)
  229. return -EINVAL;
  230. leds_data = kzalloc(sizeof(struct ns2_led_data) *
  231. pdata->num_leds, GFP_KERNEL);
  232. if (!leds_data)
  233. return -ENOMEM;
  234. for (i = 0; i < pdata->num_leds; i++) {
  235. ret = create_ns2_led(pdev, &leds_data[i], &pdata->leds[i]);
  236. if (ret < 0)
  237. goto err;
  238. }
  239. platform_set_drvdata(pdev, leds_data);
  240. return 0;
  241. err:
  242. for (i = i - 1; i >= 0; i--)
  243. delete_ns2_led(&leds_data[i]);
  244. kfree(leds_data);
  245. return ret;
  246. }
  247. static int __devexit ns2_led_remove(struct platform_device *pdev)
  248. {
  249. int i;
  250. struct ns2_led_platform_data *pdata = pdev->dev.platform_data;
  251. struct ns2_led_data *leds_data;
  252. leds_data = platform_get_drvdata(pdev);
  253. for (i = 0; i < pdata->num_leds; i++)
  254. delete_ns2_led(&leds_data[i]);
  255. kfree(leds_data);
  256. platform_set_drvdata(pdev, NULL);
  257. return 0;
  258. }
  259. static struct platform_driver ns2_led_driver = {
  260. .probe = ns2_led_probe,
  261. .remove = __devexit_p(ns2_led_remove),
  262. .driver = {
  263. .name = "leds-ns2",
  264. .owner = THIS_MODULE,
  265. },
  266. };
  267. module_platform_driver(ns2_led_driver);
  268. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  269. MODULE_DESCRIPTION("Network Space v2 LED driver");
  270. MODULE_LICENSE("GPL");
  271. MODULE_ALIAS("platform:leds-ns2");