leds-netxbig.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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/of_gpio.h>
  29. #include <linux/leds.h>
  30. #include <linux/platform_data/leds-kirkwood-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 gpio_ext_init(struct platform_device *pdev,
  64. struct netxbig_gpio_ext *gpio_ext)
  65. {
  66. int err;
  67. int i;
  68. if (unlikely(!gpio_ext))
  69. return -EINVAL;
  70. /* Configure address GPIOs. */
  71. for (i = 0; i < gpio_ext->num_addr; i++) {
  72. err = devm_gpio_request_one(&pdev->dev, gpio_ext->addr[i],
  73. GPIOF_OUT_INIT_LOW,
  74. "GPIO extension addr");
  75. if (err)
  76. return err;
  77. }
  78. /* Configure data GPIOs. */
  79. for (i = 0; i < gpio_ext->num_data; i++) {
  80. err = devm_gpio_request_one(&pdev->dev, gpio_ext->data[i],
  81. GPIOF_OUT_INIT_LOW,
  82. "GPIO extension data");
  83. if (err)
  84. return err;
  85. }
  86. /* Configure "enable select" GPIO. */
  87. err = devm_gpio_request_one(&pdev->dev, gpio_ext->enable,
  88. GPIOF_OUT_INIT_LOW,
  89. "GPIO extension enable");
  90. if (err)
  91. return err;
  92. return 0;
  93. }
  94. /*
  95. * Class LED driver.
  96. */
  97. struct netxbig_led_data {
  98. struct netxbig_gpio_ext *gpio_ext;
  99. struct led_classdev cdev;
  100. int mode_addr;
  101. int *mode_val;
  102. int bright_addr;
  103. struct netxbig_led_timer *timer;
  104. int num_timer;
  105. enum netxbig_led_mode mode;
  106. int sata;
  107. spinlock_t lock;
  108. };
  109. static int netxbig_led_get_timer_mode(enum netxbig_led_mode *mode,
  110. unsigned long delay_on,
  111. unsigned long delay_off,
  112. struct netxbig_led_timer *timer,
  113. int num_timer)
  114. {
  115. int i;
  116. for (i = 0; i < num_timer; i++) {
  117. if (timer[i].delay_on == delay_on &&
  118. timer[i].delay_off == delay_off) {
  119. *mode = timer[i].mode;
  120. return 0;
  121. }
  122. }
  123. return -EINVAL;
  124. }
  125. static int netxbig_led_blink_set(struct led_classdev *led_cdev,
  126. unsigned long *delay_on,
  127. unsigned long *delay_off)
  128. {
  129. struct netxbig_led_data *led_dat =
  130. container_of(led_cdev, struct netxbig_led_data, cdev);
  131. enum netxbig_led_mode mode;
  132. int mode_val;
  133. int ret;
  134. /* Look for a LED mode with the requested timer frequency. */
  135. ret = netxbig_led_get_timer_mode(&mode, *delay_on, *delay_off,
  136. led_dat->timer, led_dat->num_timer);
  137. if (ret < 0)
  138. return ret;
  139. mode_val = led_dat->mode_val[mode];
  140. if (mode_val == NETXBIG_LED_INVALID_MODE)
  141. return -EINVAL;
  142. spin_lock_irq(&led_dat->lock);
  143. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  144. led_dat->mode = mode;
  145. spin_unlock_irq(&led_dat->lock);
  146. return 0;
  147. }
  148. static void netxbig_led_set(struct led_classdev *led_cdev,
  149. enum led_brightness value)
  150. {
  151. struct netxbig_led_data *led_dat =
  152. container_of(led_cdev, struct netxbig_led_data, cdev);
  153. enum netxbig_led_mode mode;
  154. int mode_val;
  155. int set_brightness = 1;
  156. unsigned long flags;
  157. spin_lock_irqsave(&led_dat->lock, flags);
  158. if (value == LED_OFF) {
  159. mode = NETXBIG_LED_OFF;
  160. set_brightness = 0;
  161. } else {
  162. if (led_dat->sata)
  163. mode = NETXBIG_LED_SATA;
  164. else if (led_dat->mode == NETXBIG_LED_OFF)
  165. mode = NETXBIG_LED_ON;
  166. else /* Keep 'timer' mode. */
  167. mode = led_dat->mode;
  168. }
  169. mode_val = led_dat->mode_val[mode];
  170. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  171. led_dat->mode = mode;
  172. /*
  173. * Note that the brightness register is shared between all the
  174. * SATA LEDs. So, change the brightness setting for a single
  175. * SATA LED will affect all the others.
  176. */
  177. if (set_brightness)
  178. gpio_ext_set_value(led_dat->gpio_ext,
  179. led_dat->bright_addr, value);
  180. spin_unlock_irqrestore(&led_dat->lock, flags);
  181. }
  182. static ssize_t netxbig_led_sata_store(struct device *dev,
  183. struct device_attribute *attr,
  184. const char *buff, size_t count)
  185. {
  186. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  187. struct netxbig_led_data *led_dat =
  188. container_of(led_cdev, struct netxbig_led_data, cdev);
  189. unsigned long enable;
  190. enum netxbig_led_mode mode;
  191. int mode_val;
  192. int ret;
  193. ret = kstrtoul(buff, 10, &enable);
  194. if (ret < 0)
  195. return ret;
  196. enable = !!enable;
  197. spin_lock_irq(&led_dat->lock);
  198. if (led_dat->sata == enable) {
  199. ret = count;
  200. goto exit_unlock;
  201. }
  202. if (led_dat->mode != NETXBIG_LED_ON &&
  203. led_dat->mode != NETXBIG_LED_SATA)
  204. mode = led_dat->mode; /* Keep modes 'off' and 'timer'. */
  205. else if (enable)
  206. mode = NETXBIG_LED_SATA;
  207. else
  208. mode = NETXBIG_LED_ON;
  209. mode_val = led_dat->mode_val[mode];
  210. if (mode_val == NETXBIG_LED_INVALID_MODE) {
  211. ret = -EINVAL;
  212. goto exit_unlock;
  213. }
  214. gpio_ext_set_value(led_dat->gpio_ext, led_dat->mode_addr, mode_val);
  215. led_dat->mode = mode;
  216. led_dat->sata = enable;
  217. ret = count;
  218. exit_unlock:
  219. spin_unlock_irq(&led_dat->lock);
  220. return ret;
  221. }
  222. static ssize_t netxbig_led_sata_show(struct device *dev,
  223. struct device_attribute *attr, char *buf)
  224. {
  225. struct led_classdev *led_cdev = dev_get_drvdata(dev);
  226. struct netxbig_led_data *led_dat =
  227. container_of(led_cdev, struct netxbig_led_data, cdev);
  228. return sprintf(buf, "%d\n", led_dat->sata);
  229. }
  230. static DEVICE_ATTR(sata, 0644, netxbig_led_sata_show, netxbig_led_sata_store);
  231. static struct attribute *netxbig_led_attrs[] = {
  232. &dev_attr_sata.attr,
  233. NULL
  234. };
  235. ATTRIBUTE_GROUPS(netxbig_led);
  236. static int create_netxbig_led(struct platform_device *pdev,
  237. struct netxbig_led_platform_data *pdata,
  238. struct netxbig_led_data *led_dat,
  239. const struct netxbig_led *template)
  240. {
  241. spin_lock_init(&led_dat->lock);
  242. led_dat->gpio_ext = pdata->gpio_ext;
  243. led_dat->cdev.name = template->name;
  244. led_dat->cdev.default_trigger = template->default_trigger;
  245. led_dat->cdev.blink_set = netxbig_led_blink_set;
  246. led_dat->cdev.brightness_set = netxbig_led_set;
  247. /*
  248. * Because the GPIO extension bus don't allow to read registers
  249. * value, there is no way to probe the LED initial state.
  250. * So, the initial sysfs LED value for the "brightness" and "sata"
  251. * attributes are inconsistent.
  252. *
  253. * Note that the initial LED state can't be reconfigured.
  254. * The reason is that the LED behaviour must stay uniform during
  255. * the whole boot process (bootloader+linux).
  256. */
  257. led_dat->sata = 0;
  258. led_dat->cdev.brightness = LED_OFF;
  259. led_dat->cdev.max_brightness = template->bright_max;
  260. led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
  261. led_dat->mode_addr = template->mode_addr;
  262. led_dat->mode_val = template->mode_val;
  263. led_dat->bright_addr = template->bright_addr;
  264. led_dat->timer = pdata->timer;
  265. led_dat->num_timer = pdata->num_timer;
  266. /*
  267. * If available, expose the SATA activity blink capability through
  268. * a "sata" sysfs attribute.
  269. */
  270. if (led_dat->mode_val[NETXBIG_LED_SATA] != NETXBIG_LED_INVALID_MODE)
  271. led_dat->cdev.groups = netxbig_led_groups;
  272. return devm_led_classdev_register(&pdev->dev, &led_dat->cdev);
  273. }
  274. #ifdef CONFIG_OF_GPIO
  275. static int gpio_ext_get_of_pdata(struct device *dev, struct device_node *np,
  276. struct netxbig_gpio_ext *gpio_ext)
  277. {
  278. int *addr, *data;
  279. int num_addr, num_data;
  280. int ret;
  281. int i;
  282. ret = of_gpio_named_count(np, "addr-gpios");
  283. if (ret < 0) {
  284. dev_err(dev,
  285. "Failed to count GPIOs in DT property addr-gpios\n");
  286. return ret;
  287. }
  288. num_addr = ret;
  289. addr = devm_kzalloc(dev, num_addr * sizeof(*addr), GFP_KERNEL);
  290. if (!addr)
  291. return -ENOMEM;
  292. for (i = 0; i < num_addr; i++) {
  293. ret = of_get_named_gpio(np, "addr-gpios", i);
  294. if (ret < 0)
  295. return ret;
  296. addr[i] = ret;
  297. }
  298. gpio_ext->addr = addr;
  299. gpio_ext->num_addr = num_addr;
  300. ret = of_gpio_named_count(np, "data-gpios");
  301. if (ret < 0) {
  302. dev_err(dev,
  303. "Failed to count GPIOs in DT property data-gpios\n");
  304. return ret;
  305. }
  306. num_data = ret;
  307. data = devm_kzalloc(dev, num_data * sizeof(*data), GFP_KERNEL);
  308. if (!data)
  309. return -ENOMEM;
  310. for (i = 0; i < num_data; i++) {
  311. ret = of_get_named_gpio(np, "data-gpios", i);
  312. if (ret < 0)
  313. return ret;
  314. data[i] = ret;
  315. }
  316. gpio_ext->data = data;
  317. gpio_ext->num_data = num_data;
  318. ret = of_get_named_gpio(np, "enable-gpio", 0);
  319. if (ret < 0) {
  320. dev_err(dev,
  321. "Failed to get GPIO from DT property enable-gpio\n");
  322. return ret;
  323. }
  324. gpio_ext->enable = ret;
  325. return 0;
  326. }
  327. static int netxbig_leds_get_of_pdata(struct device *dev,
  328. struct netxbig_led_platform_data *pdata)
  329. {
  330. struct device_node *np = dev->of_node;
  331. struct device_node *gpio_ext_np;
  332. struct device_node *child;
  333. struct netxbig_gpio_ext *gpio_ext;
  334. struct netxbig_led_timer *timers;
  335. struct netxbig_led *leds, *led;
  336. int num_timers;
  337. int num_leds = 0;
  338. int ret;
  339. int i;
  340. /* GPIO extension */
  341. gpio_ext_np = of_parse_phandle(np, "gpio-ext", 0);
  342. if (!gpio_ext_np) {
  343. dev_err(dev, "Failed to get DT handle gpio-ext\n");
  344. return -EINVAL;
  345. }
  346. gpio_ext = devm_kzalloc(dev, sizeof(*gpio_ext), GFP_KERNEL);
  347. if (!gpio_ext)
  348. return -ENOMEM;
  349. ret = gpio_ext_get_of_pdata(dev, gpio_ext_np, gpio_ext);
  350. if (ret)
  351. return ret;
  352. of_node_put(gpio_ext_np);
  353. pdata->gpio_ext = gpio_ext;
  354. /* Timers (optional) */
  355. ret = of_property_count_u32_elems(np, "timers");
  356. if (ret > 0) {
  357. if (ret % 3)
  358. return -EINVAL;
  359. num_timers = ret / 3;
  360. timers = devm_kzalloc(dev, num_timers * sizeof(*timers),
  361. GFP_KERNEL);
  362. if (!timers)
  363. return -ENOMEM;
  364. for (i = 0; i < num_timers; i++) {
  365. u32 tmp;
  366. of_property_read_u32_index(np, "timers", 3 * i,
  367. &timers[i].mode);
  368. if (timers[i].mode >= NETXBIG_LED_MODE_NUM)
  369. return -EINVAL;
  370. of_property_read_u32_index(np, "timers",
  371. 3 * i + 1, &tmp);
  372. timers[i].delay_on = tmp;
  373. of_property_read_u32_index(np, "timers",
  374. 3 * i + 2, &tmp);
  375. timers[i].delay_off = tmp;
  376. }
  377. pdata->timer = timers;
  378. pdata->num_timer = num_timers;
  379. }
  380. /* LEDs */
  381. num_leds = of_get_child_count(np);
  382. if (!num_leds) {
  383. dev_err(dev, "No LED subnodes found in DT\n");
  384. return -ENODEV;
  385. }
  386. leds = devm_kzalloc(dev, num_leds * sizeof(*leds), GFP_KERNEL);
  387. if (!leds)
  388. return -ENOMEM;
  389. led = leds;
  390. for_each_child_of_node(np, child) {
  391. const char *string;
  392. int *mode_val;
  393. int num_modes;
  394. ret = of_property_read_u32(child, "mode-addr",
  395. &led->mode_addr);
  396. if (ret)
  397. goto err_node_put;
  398. ret = of_property_read_u32(child, "bright-addr",
  399. &led->bright_addr);
  400. if (ret)
  401. goto err_node_put;
  402. ret = of_property_read_u32(child, "max-brightness",
  403. &led->bright_max);
  404. if (ret)
  405. goto err_node_put;
  406. mode_val =
  407. devm_kzalloc(dev,
  408. NETXBIG_LED_MODE_NUM * sizeof(*mode_val),
  409. GFP_KERNEL);
  410. if (!mode_val) {
  411. ret = -ENOMEM;
  412. goto err_node_put;
  413. }
  414. for (i = 0; i < NETXBIG_LED_MODE_NUM; i++)
  415. mode_val[i] = NETXBIG_LED_INVALID_MODE;
  416. ret = of_property_count_u32_elems(child, "mode-val");
  417. if (ret < 0 || ret % 2) {
  418. ret = -EINVAL;
  419. goto err_node_put;
  420. }
  421. num_modes = ret / 2;
  422. if (num_modes > NETXBIG_LED_MODE_NUM) {
  423. ret = -EINVAL;
  424. goto err_node_put;
  425. }
  426. for (i = 0; i < num_modes; i++) {
  427. int mode;
  428. int val;
  429. of_property_read_u32_index(child,
  430. "mode-val", 2 * i, &mode);
  431. of_property_read_u32_index(child,
  432. "mode-val", 2 * i + 1, &val);
  433. if (mode >= NETXBIG_LED_MODE_NUM) {
  434. ret = -EINVAL;
  435. goto err_node_put;
  436. }
  437. mode_val[mode] = val;
  438. }
  439. led->mode_val = mode_val;
  440. if (!of_property_read_string(child, "label", &string))
  441. led->name = string;
  442. else
  443. led->name = child->name;
  444. if (!of_property_read_string(child,
  445. "linux,default-trigger", &string))
  446. led->default_trigger = string;
  447. led++;
  448. }
  449. pdata->leds = leds;
  450. pdata->num_leds = num_leds;
  451. return 0;
  452. err_node_put:
  453. of_node_put(child);
  454. return ret;
  455. }
  456. static const struct of_device_id of_netxbig_leds_match[] = {
  457. { .compatible = "lacie,netxbig-leds", },
  458. {},
  459. };
  460. #else
  461. static inline int
  462. netxbig_leds_get_of_pdata(struct device *dev,
  463. struct netxbig_led_platform_data *pdata)
  464. {
  465. return -ENODEV;
  466. }
  467. #endif /* CONFIG_OF_GPIO */
  468. static int netxbig_led_probe(struct platform_device *pdev)
  469. {
  470. struct netxbig_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
  471. struct netxbig_led_data *leds_data;
  472. int i;
  473. int ret;
  474. if (!pdata) {
  475. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  476. if (!pdata)
  477. return -ENOMEM;
  478. ret = netxbig_leds_get_of_pdata(&pdev->dev, pdata);
  479. if (ret)
  480. return ret;
  481. }
  482. leds_data = devm_kzalloc(&pdev->dev,
  483. pdata->num_leds * sizeof(*leds_data),
  484. GFP_KERNEL);
  485. if (!leds_data)
  486. return -ENOMEM;
  487. ret = gpio_ext_init(pdev, pdata->gpio_ext);
  488. if (ret < 0)
  489. return ret;
  490. for (i = 0; i < pdata->num_leds; i++) {
  491. ret = create_netxbig_led(pdev, pdata,
  492. &leds_data[i], &pdata->leds[i]);
  493. if (ret < 0)
  494. return ret;
  495. }
  496. return 0;
  497. }
  498. static struct platform_driver netxbig_led_driver = {
  499. .probe = netxbig_led_probe,
  500. .driver = {
  501. .name = "leds-netxbig",
  502. .of_match_table = of_match_ptr(of_netxbig_leds_match),
  503. },
  504. };
  505. module_platform_driver(netxbig_led_driver);
  506. MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
  507. MODULE_DESCRIPTION("LED driver for LaCie xBig Network boards");
  508. MODULE_LICENSE("GPL");
  509. MODULE_ALIAS("platform:leds-netxbig");