leds-ns2.c 10.0 KB

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