leds-netxbig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * leds-netxbig.c - Driver for the 2Big and 5Big Network series LEDs
  3. *
  4. * Copyright (C) 2010 LaCie
  5. *
  6. * Author: Simon Guinot <sguinot@lacie.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/irq.h>
  25. #include <linux/slab.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/gpio.h>
  29. #include <linux/leds.h>
  30. #include <mach/leds-netxbig.h>
  31. /*
  32. * GPIO extension bus.
  33. */
  34. static DEFINE_SPINLOCK(gpio_ext_lock);
  35. static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
  36. {
  37. int pin;
  38. for (pin = 0; pin < gpio_ext->num_addr; pin++)
  39. gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
  40. }
  41. static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
  42. {
  43. int pin;
  44. for (pin = 0; pin < gpio_ext->num_data; pin++)
  45. gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
  46. }
  47. static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
  48. {
  49. /* Enable select is done on the raising edge. */
  50. gpio_set_value(gpio_ext->enable, 0);
  51. gpio_set_value(gpio_ext->enable, 1);
  52. }
  53. static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
  54. int addr, int value)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(&gpio_ext_lock, flags);
  58. gpio_ext_set_addr(gpio_ext, addr);
  59. gpio_ext_set_data(gpio_ext, value);
  60. gpio_ext_enable_select(gpio_ext);
  61. spin_unlock_irqrestore(&gpio_ext_lock, flags);
  62. }
  63. static int __devinit gpio_ext_init(struct netxbig_gpio_ext *gpio_ext)
  64. {
  65. int err;
  66. int i;
  67. if (unlikely(!gpio_ext))
  68. return -EINVAL;
  69. /* Configure address GPIOs. */
  70. for (i = 0; i < gpio_ext->num_addr; i++) {
  71. err = gpio_request(gpio_ext->addr[i], "GPIO extension addr");
  72. if (err)
  73. goto err_free_addr;
  74. err = gpio_direction_output(gpio_ext->addr[i], 0);
  75. if (err) {
  76. gpio_free(gpio_ext->addr[i]);
  77. goto err_free_addr;
  78. }
  79. }
  80. /* Configure data GPIOs. */
  81. for (i = 0; i < gpio_ext->num_data; i++) {
  82. err = gpio_request(gpio_ext->data[i], "GPIO extension data");
  83. if (err)
  84. goto err_free_data;
  85. err = gpio_direction_output(gpio_ext->data[i], 0);
  86. if (err) {
  87. gpio_free(gpio_ext->data[i]);
  88. goto err_free_data;
  89. }
  90. }
  91. /* Configure "enable select" GPIO. */
  92. err = gpio_request(gpio_ext->enable, "GPIO extension enable");
  93. if (err)
  94. goto err_free_data;
  95. err = gpio_direction_output(gpio_ext->enable, 0);
  96. if (err) {
  97. gpio_free(gpio_ext->enable);
  98. goto err_free_data;
  99. }
  100. return 0;
  101. err_free_data:
  102. for (i = i - 1; i >= 0; i--)
  103. gpio_free(gpio_ext->data[i]);
  104. i = gpio_ext->num_addr;
  105. err_free_addr:
  106. for (i = i - 1; i >= 0; i--)
  107. gpio_free(gpio_ext->addr[i]);
  108. return err;
  109. }
  110. static void __devexit gpio_ext_free(struct netxbig_gpio_ext *gpio_ext)
  111. {
  112. int i;
  113. gpio_free(gpio_ext->enable);
  114. for (i = gpio_ext->num_addr - 1; i >= 0; i--)
  115. gpio_free(gpio_ext->addr[i]);
  116. for (i = gpio_ext->num_data - 1; i >= 0; i--)
  117. gpio_free(gpio_ext->data[i]);
  118. }
  119. /*
  120. * Class LED driver.
  121. */
  122. struct netxbig_led_data {
  123. struct netxbig_gpio_ext *gpio_ext;
  124. struct led_classdev cdev;
  125. int mode_addr;
  126. int *mode_val;
  127. int bright_addr;
  128. int bright_max;
  129. struct netxbig_led_timer *timer;
  130. int num_timer;
  131. enum netxbig_led_mode mode;
  132. int sata;
  133. spinlock_t lock;
  134. };
  135. static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
  136. unsigned long delay_on,
  137. unsigned long delay_off,
  138. struct netxbig_led_timer *timer,
  139. int num_timer)
  140. {
  141. int i;
  142. for (i = 0; i < num_timer; i++) {
  143. if (timer[i].delay_on == delay_on &&
  144. timer[i].delay_off == delay_off) {
  145. *mode = timer[i].mode;
  146. return 0;
  147. }
  148. }
  149. return -EINVAL;
  150. }
  151. static int netxbig_led_blink_set(struct led_classdev *led_cdev,
  152. unsigned long *delay_on,
  153. unsigned long *delay_off)
  154. {
  155. struct netxbig_led_data *led_dat =
  156. container_of(led_cdev, struct netxbig_led_data, cdev);
  157. enum netxbig_led_mode mode;
  158. int mode_val;
  159. int ret;
  160. /* Look for a LED mode with the requested timer frequency. */
  161. ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
  162. led_dat->timer, led_dat->num_timer);
  163. if (ret < 0)
  164. return ret;
  165. mode_val = led_dat->mode_val[mode];
  166. if (mode_val == NETXBIG_LED_INVALID_MODE)
  167. return -EINVAL;
  168. spin_lock_irq(&led_dat->lock);
  169. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  170. led_dat->mode = mode;
  171. spin_unlock_irq(&led_dat->lock);
  172. return 0;
  173. }
  174. static void netxbig_led_set(struct led_classdev *led_cdev,
  175. enum led_brightness value)
  176. {
  177. struct netxbig_led_data *led_dat =
  178. container_of(led_cdev, struct netxbig_led_data, cdev);
  179. enum netxbig_led_mode mode;
  180. int mode_val, bright_val;
  181. int set_brightness = 1;
  182. unsigned long flags;
  183. spin_lock_irqsave(&led_dat->lock, flags);
  184. if (value == LED_OFF) {
  185. mode = NETXBIG_LED_OFF;
  186. set_brightness = 0;
  187. } else {
  188. if (led_dat->sata)
  189. mode = NETXBIG_LED_SATA;
  190. else if (led_dat->mode == NETXBIG_LED_OFF)
  191. mode = NETXBIG_LED_ON;
  192. else /* Keep 'timer' mode. */
  193. mode = led_dat->mode;
  194. }
  195. mode_val = led_dat->mode_val[mode];
  196. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  197. led_dat->mode = mode;
  198. /*
  199. * Note that the brightness register is shared between all the
  200. * SATA LEDs. So, change the brightness setting for a single
  201. * SATA LED will affect all the others.
  202. */
  203. if (set_brightness) {
  204. bright_val = DIV_ROUND_UP(value * led_dat->bright_max,
  205. LED_FULL);
  206. gpio_ext_set_value(led_dat->gpio_ext,
  207. led_dat->bright_addr, bright_val);
  208. }
  209. spin_unlock_irqrestore(&led_dat->lock, flags);
  210. }
  211. static ssize_t netxbig_led_sata_store(struct device *dev,
  212. struct device_attribute *attr,
  213. const char *buff, size_t count)
  214. {
  215. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  216. struct netxbig_led_data *led_dat =
  217. container_of(led_cdev, struct netxbig_led_data, cdev);
  218. unsigned long enable;
  219. enum netxbig_led_mode mode;
  220. int mode_val;
  221. int ret;
  222. ret = strict_strtoul(buff, 10, &enable);
  223. if (ret < 0)
  224. return ret;
  225. enable = !!enable;
  226. spin_lock_irq(&led_dat->lock);
  227. if (led_dat->sata == enable) {
  228. ret = count;
  229. goto exit_unlock;
  230. }
  231. if (led_dat->mode != NETXBIG_LED_ON &&
  232. led_dat->mode != NETXBIG_LED_SATA)
  233. mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
  234. else if (enable)
  235. mode = NETXBIG_LED_SATA;
  236. else
  237. mode = NETXBIG_LED_ON;
  238. mode_val = led_dat->mode_val[mode];
  239. if (mode_val == NETXBIG_LED_INVALID_MODE) {
  240. ret = -EINVAL;
  241. goto exit_unlock;
  242. }
  243. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  244. led_dat->mode = mode;
  245. led_dat->sata = enable;
  246. ret = count;
  247. exit_unlock:
  248. spin_unlock_irq(&led_dat->lock);
  249. return ret;
  250. }
  251. static ssize_t netxbig_led_sata_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  255. struct netxbig_led_data *led_dat =
  256. container_of(led_cdev, struct netxbig_led_data, cdev);
  257. return sprintf(buf, "%d\n", led_dat->sata);
  258. }
  259. static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
  260. static void __devexit delete_netxbig_led(struct netxbig_led_data *led_dat)
  261. {
  262. if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
  263. device_remove_file(led_dat->cdev.dev, &dev_attr_sata);
  264. led_classdev_unregister(&led_dat->cdev);
  265. }
  266. static int __devinit
  267. create_netxbig_led(struct platform_device *pdev,
  268. struct netxbig_led_data *led_dat,
  269. const struct netxbig_led *template)
  270. {
  271. struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
  272. int ret;
  273. spin_lock_init(&led_dat->lock);
  274. led_dat->gpio_ext = pdata->gpio_ext;
  275. led_dat->cdev.name = template->name;
  276. led_dat->cdev.default_trigger = template->default_trigger;
  277. led_dat->cdev.blink_set = netxbig_led_blink_set;
  278. led_dat->cdev.brightness_set = netxbig_led_set;
  279. /*
  280. * Because the GPIO extension bus don't allow to read registers
  281. * value, there is no way to probe the LED initial state.
  282. * So, the initial sysfs LED value for the "brightness" and "sata"
  283. * attributes are inconsistent.
  284. *
  285. * Note that the initial LED state can't be reconfigured.
  286. * The reason is that the LED behaviour must stay uniform during
  287. * the whole boot process (bootloader+linux).
  288. */
  289. led_dat->sata = 0;
  290. led_dat->cdev.brightness = LED_OFF;
  291. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  292. led_dat->mode_addr = template->mode_addr;
  293. led_dat->mode_val = template->mode_val;
  294. led_dat->bright_addr = template->bright_addr;
  295. led_dat->bright_max = (1 << pdata->gpio_ext->num_data) - 1;
  296. led_dat->timer = pdata->timer;
  297. led_dat->num_timer = pdata->num_timer;
  298. ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
  299. if (ret < 0)
  300. return ret;
  301. /*
  302. * If available, expose the SATA activity blink capability through
  303. * a "sata" sysfs attribute.
  304. */
  305. if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE) {
  306. ret = device_create_file(led_dat->cdev.dev, &dev_attr_sata);
  307. if (ret)
  308. led_classdev_unregister(&led_dat->cdev);
  309. }
  310. return ret;
  311. }
  312. static int __devinit netxbig_led_probe(struct platform_device *pdev)
  313. {
  314. struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
  315. struct netxbig_led_data *leds_data;
  316. int i;
  317. int ret;
  318. if (!pdata)
  319. return -EINVAL;
  320. leds_data = kzalloc(sizeof(struct netxbig_led_data) * pdata->num_leds,
  321. GFP_KERNEL);
  322. if (!leds_data)
  323. return -ENOMEM;
  324. ret = gpio_ext_init(pdata->gpio_ext);
  325. if (ret < 0)
  326. goto err_free_data;
  327. for (i = 0; i < pdata->num_leds; i++) {
  328. ret = create_netxbig_led(pdev, &leds_data[i], &pdata->leds[i]);
  329. if (ret < 0)
  330. goto err_free_leds;
  331. }
  332. platform_set_drvdata(pdev, leds_data);
  333. return 0;
  334. err_free_leds:
  335. for (i = i - 1; i >= 0; i--)
  336. delete_netxbig_led(&leds_data[i]);
  337. gpio_ext_free(pdata->gpio_ext);
  338. err_free_data:
  339. kfree(leds_data);
  340. return ret;
  341. }
  342. static int __devexit netxbig_led_remove(struct platform_device *pdev)
  343. {
  344. struct netxbig_led_platform_data *pdata = pdev->dev.platform_data;
  345. struct netxbig_led_data *leds_data;
  346. int i;
  347. leds_data = platform_get_drvdata(pdev);
  348. for (i = 0; i < pdata->num_leds; i++)
  349. delete_netxbig_led(&leds_data[i]);
  350. gpio_ext_free(pdata->gpio_ext);
  351. kfree(leds_data);
  352. return 0;
  353. }
  354. static struct platform_driver netxbig_led_driver = {
  355. .probe = netxbig_led_probe,
  356. .remove = __devexit_p(netxbig_led_remove),
  357. .driver = {
  358. .name = "leds-netxbig",
  359. .owner = THIS_MODULE,
  360. },
  361. };
  362. MODULE_ALIAS("platform:leds-netxbig");
  363. static int __init netxbig_led_init(void)
  364. {
  365. return platform_driver_register(&netxbig_led_driver);
  366. }
  367. static void __exit netxbig_led_exit(void)
  368. {
  369. platform_driver_unregister(&netxbig_led_driver);
  370. }
  371. module_init(netxbig_led_init);
  372. module_exit(netxbig_led_exit);
  373. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  374. MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
  375. MODULE_LICENSE("GPL");