leds-netxbig.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/irq.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/gpio.h>
  28. #include <linux/leds.h>
  29. #include <linux/platform_data/leds-kirkwood-netxbig.h>
  30. /*
  31. * GPIO extension bus.
  32. */
  33. static DEFINE_SPINLOCK(gpio_ext_lock);
  34. static void gpio_ext_set_addr(struct netxbig_gpio_ext *gpio_ext, int addr)
  35. {
  36. int pin;
  37. for (pin = 0; pin < gpio_ext->num_addr; pin++)
  38. gpio_set_value(gpio_ext->addr[pin], (addr >> pin) & 1);
  39. }
  40. static void gpio_ext_set_data(struct netxbig_gpio_ext *gpio_ext, int data)
  41. {
  42. int pin;
  43. for (pin = 0; pin < gpio_ext->num_data; pin++)
  44. gpio_set_value(gpio_ext->data[pin], (data >> pin) & 1);
  45. }
  46. static void gpio_ext_enable_select(struct netxbig_gpio_ext *gpio_ext)
  47. {
  48. /* Enable select is done on the raising edge. */
  49. gpio_set_value(gpio_ext->enable, 0);
  50. gpio_set_value(gpio_ext->enable, 1);
  51. }
  52. static void gpio_ext_set_value(struct netxbig_gpio_ext *gpio_ext,
  53. int addr, int value)
  54. {
  55. unsigned long flags;
  56. spin_lock_irqsave(&gpio_ext_lock, flags);
  57. gpio_ext_set_addr(gpio_ext, addr);
  58. gpio_ext_set_data(gpio_ext, value);
  59. gpio_ext_enable_select(gpio_ext);
  60. spin_unlock_irqrestore(&gpio_ext_lock, flags);
  61. }
  62. static int gpio_ext_init(struct netxbig_gpio_ext *gpio_ext)
  63. {
  64. int err;
  65. int i;
  66. if (unlikely(!gpio_ext))
  67. return -EINVAL;
  68. /* Configure address GPIOs. */
  69. for (i = 0; i < gpio_ext->num_addr; i++) {
  70. err = gpio_request_one(gpio_ext->addr[i], GPIOF_OUT_INIT_LOW,
  71. "GPIO extension addr");
  72. if (err)
  73. goto err_free_addr;
  74. }
  75. /* Configure data GPIOs. */
  76. for (i = 0; i < gpio_ext->num_data; i++) {
  77. err = gpio_request_one(gpio_ext->data[i], GPIOF_OUT_INIT_LOW,
  78. "GPIO extension data");
  79. if (err)
  80. goto err_free_data;
  81. }
  82. /* Configure "enable select" GPIO. */
  83. err = gpio_request_one(gpio_ext->enable, GPIOF_OUT_INIT_LOW,
  84. "GPIO extension enable");
  85. if (err)
  86. goto err_free_data;
  87. return 0;
  88. err_free_data:
  89. for (i = i - 1; i >= 0; i--)
  90. gpio_free(gpio_ext->data[i]);
  91. i = gpio_ext->num_addr;
  92. err_free_addr:
  93. for (i = i - 1; i >= 0; i--)
  94. gpio_free(gpio_ext->addr[i]);
  95. return err;
  96. }
  97. static void gpio_ext_free(struct netxbig_gpio_ext *gpio_ext)
  98. {
  99. int i;
  100. gpio_free(gpio_ext->enable);
  101. for (i = gpio_ext->num_addr - 1; i >= 0; i--)
  102. gpio_free(gpio_ext->addr[i]);
  103. for (i = gpio_ext->num_data - 1; i >= 0; i--)
  104. gpio_free(gpio_ext->data[i]);
  105. }
  106. /*
  107. * Class LED driver.
  108. */
  109. struct netxbig_led_data {
  110. struct netxbig_gpio_ext *gpio_ext;
  111. struct led_classdev cdev;
  112. int mode_addr;
  113. int *mode_val;
  114. int bright_addr;
  115. int bright_max;
  116. struct netxbig_led_timer *timer;
  117. int num_timer;
  118. enum netxbig_led_mode mode;
  119. int sata;
  120. spinlock_t lock;
  121. };
  122. static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
  123. unsigned long delay_on,
  124. unsigned long delay_off,
  125. struct netxbig_led_timer *timer,
  126. int num_timer)
  127. {
  128. int i;
  129. for (i = 0; i < num_timer; i++) {
  130. if (timer[i].delay_on == delay_on &&
  131. timer[i].delay_off == delay_off) {
  132. *mode = timer[i].mode;
  133. return 0;
  134. }
  135. }
  136. return -EINVAL;
  137. }
  138. static int netxbig_led_blink_set(struct led_classdev *led_cdev,
  139. unsigned long *delay_on,
  140. unsigned long *delay_off)
  141. {
  142. struct netxbig_led_data *led_dat =
  143. container_of(led_cdev, struct netxbig_led_data, cdev);
  144. enum netxbig_led_mode mode;
  145. int mode_val;
  146. int ret;
  147. /* Look for a LED mode with the requested timer frequency. */
  148. ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
  149. led_dat->timer, led_dat->num_timer);
  150. if (ret < 0)
  151. return ret;
  152. mode_val = led_dat->mode_val[mode];
  153. if (mode_val == NETXBIG_LED_INVALID_MODE)
  154. return -EINVAL;
  155. spin_lock_irq(&led_dat->lock);
  156. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  157. led_dat->mode = mode;
  158. spin_unlock_irq(&led_dat->lock);
  159. return 0;
  160. }
  161. static void netxbig_led_set(struct led_classdev *led_cdev,
  162. enum led_brightness value)
  163. {
  164. struct netxbig_led_data *led_dat =
  165. container_of(led_cdev, struct netxbig_led_data, cdev);
  166. enum netxbig_led_mode mode;
  167. int mode_val, bright_val;
  168. int set_brightness = 1;
  169. unsigned long flags;
  170. spin_lock_irqsave(&led_dat->lock, flags);
  171. if (value == LED_OFF) {
  172. mode = NETXBIG_LED_OFF;
  173. set_brightness = 0;
  174. } else {
  175. if (led_dat->sata)
  176. mode = NETXBIG_LED_SATA;
  177. else if (led_dat->mode == NETXBIG_LED_OFF)
  178. mode = NETXBIG_LED_ON;
  179. else /* Keep 'timer' mode. */
  180. mode = led_dat->mode;
  181. }
  182. mode_val = led_dat->mode_val[mode];
  183. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  184. led_dat->mode = mode;
  185. /*
  186. * Note that the brightness register is shared between all the
  187. * SATA LEDs. So, change the brightness setting for a single
  188. * SATA LED will affect all the others.
  189. */
  190. if (set_brightness) {
  191. bright_val = DIV_ROUND_UP(value * led_dat->bright_max,
  192. LED_FULL);
  193. gpio_ext_set_value(led_dat->gpio_ext,
  194. led_dat->bright_addr, bright_val);
  195. }
  196. spin_unlock_irqrestore(&led_dat->lock, flags);
  197. }
  198. static ssize_t netxbig_led_sata_store(struct device *dev,
  199. struct device_attribute *attr,
  200. const char *buff, size_t count)
  201. {
  202. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  203. struct netxbig_led_data *led_dat =
  204. container_of(led_cdev, struct netxbig_led_data, cdev);
  205. unsigned long enable;
  206. enum netxbig_led_mode mode;
  207. int mode_val;
  208. int ret;
  209. ret = kstrtoul(buff, 10, &enable);
  210. if (ret < 0)
  211. return ret;
  212. enable = !!enable;
  213. spin_lock_irq(&led_dat->lock);
  214. if (led_dat->sata == enable) {
  215. ret = count;
  216. goto exit_unlock;
  217. }
  218. if (led_dat->mode != NETXBIG_LED_ON &&
  219. led_dat->mode != NETXBIG_LED_SATA)
  220. mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
  221. else if (enable)
  222. mode = NETXBIG_LED_SATA;
  223. else
  224. mode = NETXBIG_LED_ON;
  225. mode_val = led_dat->mode_val[mode];
  226. if (mode_val == NETXBIG_LED_INVALID_MODE) {
  227. ret = -EINVAL;
  228. goto exit_unlock;
  229. }
  230. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  231. led_dat->mode = mode;
  232. led_dat->sata = enable;
  233. ret = count;
  234. exit_unlock:
  235. spin_unlock_irq(&led_dat->lock);
  236. return ret;
  237. }
  238. static ssize_t netxbig_led_sata_show(struct device *dev,
  239. struct device_attribute *attr, char *buf)
  240. {
  241. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  242. struct netxbig_led_data *led_dat =
  243. container_of(led_cdev, struct netxbig_led_data, cdev);
  244. return sprintf(buf, "%d\n", led_dat->sata);
  245. }
  246. static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
  247. static struct attribute *netxbig_led_attrs[] = {
  248. &dev_attr_sata.attr,
  249. NULL
  250. };
  251. ATTRIBUTE_GROUPS(netxbig_led);
  252. static void delete_netxbig_led(struct netxbig_led_data *led_dat)
  253. {
  254. led_classdev_unregister(&led_dat->cdev);
  255. }
  256. static int
  257. create_netxbig_led(struct platform_device *pdev,
  258. struct netxbig_led_data *led_dat,
  259. const struct netxbig_led *template)
  260. {
  261. struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  262. spin_lock_init(&led_dat->lock);
  263. led_dat->gpio_ext = pdata->gpio_ext;
  264. led_dat->cdev.name = template->name;
  265. led_dat->cdev.default_trigger = template->default_trigger;
  266. led_dat->cdev.blink_set = netxbig_led_blink_set;
  267. led_dat->cdev.brightness_set = netxbig_led_set;
  268. /*
  269. * Because the GPIO extension bus don't allow to read registers
  270. * value, there is no way to probe the LED initial state.
  271. * So, the initial sysfs LED value for the "brightness" and "sata"
  272. * attributes are inconsistent.
  273. *
  274. * Note that the initial LED state can't be reconfigured.
  275. * The reason is that the LED behaviour must stay uniform during
  276. * the whole boot process (bootloader+linux).
  277. */
  278. led_dat->sata = 0;
  279. led_dat->cdev.brightness = LED_OFF;
  280. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  281. led_dat->mode_addr = template->mode_addr;
  282. led_dat->mode_val = template->mode_val;
  283. led_dat->bright_addr = template->bright_addr;
  284. led_dat->bright_max = (1 << pdata->gpio_ext->num_data) - 1;
  285. led_dat->timer = pdata->timer;
  286. led_dat->num_timer = pdata->num_timer;
  287. /*
  288. * If available, expose the SATA activity blink capability through
  289. * a "sata" sysfs attribute.
  290. */
  291. if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
  292. led_dat->cdev.groups = netxbig_led_groups;
  293. return led_classdev_register(&pdev->dev, &led_dat->cdev);
  294. }
  295. static int netxbig_led_probe(struct platform_device *pdev)
  296. {
  297. struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  298. struct netxbig_led_data *leds_data;
  299. int i;
  300. int ret;
  301. if (!pdata)
  302. return -EINVAL;
  303. leds_data = devm_kzalloc(&pdev->dev,
  304. sizeof(struct netxbig_led_data) * pdata->num_leds, GFP_KERNEL);
  305. if (!leds_data)
  306. return -ENOMEM;
  307. ret = gpio_ext_init(pdata->gpio_ext);
  308. if (ret < 0)
  309. return ret;
  310. for (i = 0; i < pdata->num_leds; i++) {
  311. ret = create_netxbig_led(pdev, &leds_data[i], &pdata->leds[i]);
  312. if (ret < 0)
  313. goto err_free_leds;
  314. }
  315. platform_set_drvdata(pdev, leds_data);
  316. return 0;
  317. err_free_leds:
  318. for (i = i - 1; i >= 0; i--)
  319. delete_netxbig_led(&leds_data[i]);
  320. gpio_ext_free(pdata->gpio_ext);
  321. return ret;
  322. }
  323. static int netxbig_led_remove(struct platform_device *pdev)
  324. {
  325. struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  326. struct netxbig_led_data *leds_data;
  327. int i;
  328. leds_data = platform_get_drvdata(pdev);
  329. for (i = 0; i < pdata->num_leds; i++)
  330. delete_netxbig_led(&leds_data[i]);
  331. gpio_ext_free(pdata->gpio_ext);
  332. return 0;
  333. }
  334. static struct platform_driver netxbig_led_driver = {
  335. .probe = netxbig_led_probe,
  336. .remove = netxbig_led_remove,
  337. .driver = {
  338. .name = "leds-netxbig",
  339. },
  340. };
  341. module_platform_driver(netxbig_led_driver);
  342. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  343. MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
  344. MODULE_LICENSE("GPL");
  345. MODULE_ALIAS("platform:leds-netxbig");