leds-ns2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/platform_device.h>
  26. #include <linux/slab.h>
  27. #include <linux/gpio.h>
  28. #include <linux/leds.h>
  29. #include <linux/module.h>
  30. #include <linux/platform_data/leds-kirkwood-ns2.h>
  31. #include <linux/of.h>
  32. #include <linux/of_gpio.h>
  33. #include "leds.h"
  34. /*
  35. * The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
  36. * modes are available: off, on and SATA activity blinking. The LED modes are
  37. * controlled through two GPIOs (command and slow): each combination of values
  38. * for the command/slow GPIOs corresponds to a LED mode.
  39. */
  40. struct ns2_led_data {
  41. struct led_classdev cdev;
  42. unsigned cmd;
  43. unsigned slow;
  44. bool can_sleep;
  45. int mode_index;
  46. unsigned char sata; /* True when SATA mode active. */
  47. rwlock_t rw_lock; /* Lock GPIOs. */
  48. struct work_struct work;
  49. int num_modes;
  50. struct ns2_led_modval *modval;
  51. };
  52. static void ns2_led_work(struct work_struct *work)
  53. {
  54. struct ns2_led_data *led_dat =
  55. container_of(work, struct ns2_led_data, work);
  56. int i = led_dat->mode_index;
  57. gpio_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
  58. gpio_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
  59. }
  60. static int ns2_led_get_mode(struct ns2_led_data *led_dat,
  61. enum ns2_led_modes *mode)
  62. {
  63. int i;
  64. int ret = -EINVAL;
  65. int cmd_level;
  66. int slow_level;
  67. cmd_level = gpio_get_value_cansleep(led_dat->cmd);
  68. slow_level = gpio_get_value_cansleep(led_dat->slow);
  69. for (i = 0; i < led_dat->num_modes; i++) {
  70. if (cmd_level == led_dat->modval[i].cmd_level &&
  71. slow_level == led_dat->modval[i].slow_level) {
  72. *mode = led_dat->modval[i].mode;
  73. ret = 0;
  74. break;
  75. }
  76. }
  77. return ret;
  78. }
  79. static void ns2_led_set_mode(struct ns2_led_data *led_dat,
  80. enum ns2_led_modes mode)
  81. {
  82. int i;
  83. bool found = false;
  84. unsigned long flags;
  85. for (i = 0; i < led_dat->num_modes; i++)
  86. if (mode == led_dat->modval[i].mode) {
  87. found = true;
  88. break;
  89. }
  90. if (!found)
  91. return;
  92. write_lock_irqsave(&led_dat->rw_lock, flags);
  93. if (!led_dat->can_sleep) {
  94. gpio_set_value(led_dat->cmd,
  95. led_dat->modval[i].cmd_level);
  96. gpio_set_value(led_dat->slow,
  97. led_dat->modval[i].slow_level);
  98. goto exit_unlock;
  99. }
  100. led_dat->mode_index = i;
  101. schedule_work(&led_dat->work);
  102. exit_unlock:
  103. write_unlock_irqrestore(&led_dat->rw_lock, flags);
  104. }
  105. static void ns2_led_set(struct led_classdev *led_cdev,
  106. enum led_brightness value)
  107. {
  108. struct ns2_led_data *led_dat =
  109. container_of(led_cdev, struct ns2_led_data, cdev);
  110. enum ns2_led_modes mode;
  111. if (value == LED_OFF)
  112. mode = NS_V2_LED_OFF;
  113. else if (led_dat->sata)
  114. mode = NS_V2_LED_SATA;
  115. else
  116. mode = NS_V2_LED_ON;
  117. ns2_led_set_mode(led_dat, mode);
  118. }
  119. static ssize_t ns2_led_sata_store(struct device *dev,
  120. struct device_attribute *attr,
  121. const char *buff, size_t count)
  122. {
  123. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  124. struct ns2_led_data *led_dat =
  125. container_of(led_cdev, struct ns2_led_data, cdev);
  126. int ret;
  127. unsigned long enable;
  128. ret = kstrtoul(buff, 10, &enable);
  129. if (ret < 0)
  130. return ret;
  131. enable = !!enable;
  132. if (led_dat->sata == enable)
  133. goto exit;
  134. led_dat->sata = enable;
  135. if (!led_get_brightness(led_cdev))
  136. goto exit;
  137. if (enable)
  138. ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
  139. else
  140. ns2_led_set_mode(led_dat, NS_V2_LED_ON);
  141. exit:
  142. return count;
  143. }
  144. static ssize_t ns2_led_sata_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  148. struct ns2_led_data *led_dat =
  149. container_of(led_cdev, struct ns2_led_data, cdev);
  150. return sprintf(buf, "%d\n", led_dat->sata);
  151. }
  152. static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
  153. static struct attribute *ns2_led_attrs[] = {
  154. &dev_attr_sata.attr,
  155. NULL
  156. };
  157. ATTRIBUTE_GROUPS(ns2_led);
  158. static int
  159. create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
  160. const struct ns2_led *template)
  161. {
  162. int ret;
  163. enum ns2_led_modes mode;
  164. ret = devm_gpio_request_one(&pdev->dev, template->cmd,
  165. gpio_get_value_cansleep(template->cmd) ?
  166. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  167. template->name);
  168. if (ret) {
  169. dev_err(&pdev->dev, "%s: failed to setup command GPIO\n",
  170. template->name);
  171. return ret;
  172. }
  173. ret = devm_gpio_request_one(&pdev->dev, template->slow,
  174. gpio_get_value_cansleep(template->slow) ?
  175. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW,
  176. template->name);
  177. if (ret) {
  178. dev_err(&pdev->dev, "%s: failed to setup slow GPIO\n",
  179. template->name);
  180. return ret;
  181. }
  182. rwlock_init(&led_dat->rw_lock);
  183. led_dat->cdev.name = template->name;
  184. led_dat->cdev.default_trigger = template->default_trigger;
  185. led_dat->cdev.blink_set = NULL;
  186. led_dat->cdev.brightness_set = ns2_led_set;
  187. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  188. led_dat->cdev.groups = ns2_led_groups;
  189. led_dat->cmd = template->cmd;
  190. led_dat->slow = template->slow;
  191. led_dat->can_sleep = gpio_cansleep(led_dat->cmd) |
  192. gpio_cansleep(led_dat->slow);
  193. led_dat->modval = template->modval;
  194. led_dat->num_modes = template->num_modes;
  195. ret = ns2_led_get_mode(led_dat, &mode);
  196. if (ret < 0)
  197. return ret;
  198. /* Set LED initial state. */
  199. led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
  200. led_dat->cdev.brightness =
  201. (mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
  202. INIT_WORK(&led_dat->work, ns2_led_work);
  203. ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
  204. if (ret < 0)
  205. return ret;
  206. return 0;
  207. }
  208. static void delete_ns2_led(struct ns2_led_data *led_dat)
  209. {
  210. led_classdev_unregister(&led_dat->cdev);
  211. cancel_work_sync(&led_dat->work);
  212. }
  213. #ifdef CONFIG_OF_GPIO
  214. /*
  215. * Translate OpenFirmware node properties into platform_data.
  216. */
  217. static int
  218. ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
  219. {
  220. struct device_node *np = dev->of_node;
  221. struct device_node *child;
  222. struct ns2_led *led, *leds;
  223. int num_leds = 0;
  224. num_leds = of_get_child_count(np);
  225. if (!num_leds)
  226. return -ENODEV;
  227. leds = devm_kzalloc(dev, num_leds * sizeof(struct ns2_led),
  228. GFP_KERNEL);
  229. if (!leds)
  230. return -ENOMEM;
  231. led = leds;
  232. for_each_child_of_node(np, child) {
  233. const char *string;
  234. int ret, i, num_modes;
  235. struct ns2_led_modval *modval;
  236. ret = of_get_named_gpio(child, "cmd-gpio", 0);
  237. if (ret < 0)
  238. return ret;
  239. led->cmd = ret;
  240. ret = of_get_named_gpio(child, "slow-gpio", 0);
  241. if (ret < 0)
  242. return ret;
  243. led->slow = ret;
  244. ret = of_property_read_string(child, "label", &string);
  245. led->name = (ret == 0) ? string : child->name;
  246. ret = of_property_read_string(child, "linux,default-trigger",
  247. &string);
  248. if (ret == 0)
  249. led->default_trigger = string;
  250. ret = of_property_count_u32_elems(child, "modes-map");
  251. if (ret < 0 || ret % 3) {
  252. dev_err(dev,
  253. "Missing or malformed modes-map property\n");
  254. return -EINVAL;
  255. }
  256. num_modes = ret / 3;
  257. modval = devm_kzalloc(dev,
  258. num_modes * sizeof(struct ns2_led_modval),
  259. GFP_KERNEL);
  260. if (!modval)
  261. return -ENOMEM;
  262. for (i = 0; i < num_modes; i++) {
  263. of_property_read_u32_index(child,
  264. "modes-map", 3 * i,
  265. (u32 *) &modval[i].mode);
  266. of_property_read_u32_index(child,
  267. "modes-map", 3 * i + 1,
  268. (u32 *) &modval[i].cmd_level);
  269. of_property_read_u32_index(child,
  270. "modes-map", 3 * i + 2,
  271. (u32 *) &modval[i].slow_level);
  272. }
  273. led->num_modes = num_modes;
  274. led->modval = modval;
  275. led++;
  276. }
  277. pdata->leds = leds;
  278. pdata->num_leds = num_leds;
  279. return 0;
  280. }
  281. static const struct of_device_id of_ns2_leds_match[] = {
  282. { .compatible = "lacie,ns2-leds", },
  283. {},
  284. };
  285. MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
  286. #endif /* CONFIG_OF_GPIO */
  287. struct ns2_led_priv {
  288. int num_leds;
  289. struct ns2_led_data leds_data[];
  290. };
  291. static inline int sizeof_ns2_led_priv(int num_leds)
  292. {
  293. return sizeof(struct ns2_led_priv) +
  294. (sizeof(struct ns2_led_data) * num_leds);
  295. }
  296. static int ns2_led_probe(struct platform_device *pdev)
  297. {
  298. struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  299. struct ns2_led_priv *priv;
  300. int i;
  301. int ret;
  302. #ifdef CONFIG_OF_GPIO
  303. if (!pdata) {
  304. pdata = devm_kzalloc(&pdev->dev,
  305. sizeof(struct ns2_led_platform_data),
  306. GFP_KERNEL);
  307. if (!pdata)
  308. return -ENOMEM;
  309. ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
  310. if (ret)
  311. return ret;
  312. }
  313. #else
  314. if (!pdata)
  315. return -EINVAL;
  316. #endif /* CONFIG_OF_GPIO */
  317. priv = devm_kzalloc(&pdev->dev,
  318. sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
  319. if (!priv)
  320. return -ENOMEM;
  321. priv->num_leds = pdata->num_leds;
  322. for (i = 0; i < priv->num_leds; i++) {
  323. ret = create_ns2_led(pdev, &priv->leds_data[i],
  324. &pdata->leds[i]);
  325. if (ret < 0) {
  326. for (i = i - 1; i >= 0; i--)
  327. delete_ns2_led(&priv->leds_data[i]);
  328. return ret;
  329. }
  330. }
  331. platform_set_drvdata(pdev, priv);
  332. return 0;
  333. }
  334. static int ns2_led_remove(struct platform_device *pdev)
  335. {
  336. int i;
  337. struct ns2_led_priv *priv;
  338. priv = platform_get_drvdata(pdev);
  339. for (i = 0; i < priv->num_leds; i++)
  340. delete_ns2_led(&priv->leds_data[i]);
  341. return 0;
  342. }
  343. static struct platform_driver ns2_led_driver = {
  344. .probe = ns2_led_probe,
  345. .remove = ns2_led_remove,
  346. .driver = {
  347. .name = "leds-ns2",
  348. .of_match_table = of_match_ptr(of_ns2_leds_match),
  349. },
  350. };
  351. module_platform_driver(ns2_led_driver);
  352. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  353. MODULE_DESCRIPTION("Network Space v2 LED driver");
  354. MODULE_LICENSE("GPL");
  355. MODULE_ALIAS("platform:leds-ns2");