leds-sunfire.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* leds-sunfire.c: SUNW,Ultra-Enterprise LED driver.
  2. *
  3. * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/leds.h>
  10. #include <linux/io.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <asm/fhc.h>
  14. #include <asm/upa.h>
  15. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  16. MODULE_DESCRIPTION("Sun Fire LED driver");
  17. MODULE_LICENSE("GPL");
  18. struct sunfire_led {
  19. struct led_classdev led_cdev;
  20. void __iomem *reg;
  21. };
  22. #define to_sunfire_led(d) container_of(d, struct sunfire_led, led_cdev)
  23. static void __clockboard_set(struct led_classdev *led_cdev,
  24. enum led_brightness led_val, u8 bit)
  25. {
  26. struct sunfire_led *p = to_sunfire_led(led_cdev);
  27. u8 reg = upa_readb(p->reg);
  28. switch (bit) {
  29. case CLOCK_CTRL_LLED:
  30. if (led_val)
  31. reg &= ~bit;
  32. else
  33. reg |= bit;
  34. break;
  35. default:
  36. if (led_val)
  37. reg |= bit;
  38. else
  39. reg &= ~bit;
  40. break;
  41. }
  42. upa_writeb(reg, p->reg);
  43. }
  44. static void clockboard_left_set(struct led_classdev *led_cdev,
  45. enum led_brightness led_val)
  46. {
  47. __clockboard_set(led_cdev, led_val, CLOCK_CTRL_LLED);
  48. }
  49. static void clockboard_middle_set(struct led_classdev *led_cdev,
  50. enum led_brightness led_val)
  51. {
  52. __clockboard_set(led_cdev, led_val, CLOCK_CTRL_MLED);
  53. }
  54. static void clockboard_right_set(struct led_classdev *led_cdev,
  55. enum led_brightness led_val)
  56. {
  57. __clockboard_set(led_cdev, led_val, CLOCK_CTRL_RLED);
  58. }
  59. static void __fhc_set(struct led_classdev *led_cdev,
  60. enum led_brightness led_val, u32 bit)
  61. {
  62. struct sunfire_led *p = to_sunfire_led(led_cdev);
  63. u32 reg = upa_readl(p->reg);
  64. switch (bit) {
  65. case FHC_CONTROL_LLED:
  66. if (led_val)
  67. reg &= ~bit;
  68. else
  69. reg |= bit;
  70. break;
  71. default:
  72. if (led_val)
  73. reg |= bit;
  74. else
  75. reg &= ~bit;
  76. break;
  77. }
  78. upa_writel(reg, p->reg);
  79. }
  80. static void fhc_left_set(struct led_classdev *led_cdev,
  81. enum led_brightness led_val)
  82. {
  83. __fhc_set(led_cdev, led_val, FHC_CONTROL_LLED);
  84. }
  85. static void fhc_middle_set(struct led_classdev *led_cdev,
  86. enum led_brightness led_val)
  87. {
  88. __fhc_set(led_cdev, led_val, FHC_CONTROL_MLED);
  89. }
  90. static void fhc_right_set(struct led_classdev *led_cdev,
  91. enum led_brightness led_val)
  92. {
  93. __fhc_set(led_cdev, led_val, FHC_CONTROL_RLED);
  94. }
  95. typedef void (*set_handler)(struct led_classdev *, enum led_brightness);
  96. struct led_type {
  97. const char *name;
  98. set_handler handler;
  99. const char *default_trigger;
  100. };
  101. #define NUM_LEDS_PER_BOARD 3
  102. struct sunfire_drvdata {
  103. struct sunfire_led leds[NUM_LEDS_PER_BOARD];
  104. };
  105. static int sunfire_led_generic_probe(struct platform_device *pdev,
  106. struct led_type *types)
  107. {
  108. struct sunfire_drvdata *p;
  109. int i, err;
  110. if (pdev->num_resources != 1) {
  111. dev_err(&pdev->dev, "Wrong number of resources %d, should be 1\n",
  112. pdev->num_resources);
  113. return -EINVAL;
  114. }
  115. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  116. if (!p)
  117. return -ENOMEM;
  118. for (i = 0; i < NUM_LEDS_PER_BOARD; i++) {
  119. struct led_classdev *lp = &p->leds[i].led_cdev;
  120. p->leds[i].reg = (void __iomem *) pdev->resource[0].start;
  121. lp->name = types[i].name;
  122. lp->brightness = LED_FULL;
  123. lp->brightness_set = types[i].handler;
  124. lp->default_trigger = types[i].default_trigger;
  125. err = led_classdev_register(&pdev->dev, lp);
  126. if (err) {
  127. dev_err(&pdev->dev, "Could not register %s LED\n",
  128. lp->name);
  129. for (i--; i >= 0; i--)
  130. led_classdev_unregister(&p->leds[i].led_cdev);
  131. return err;
  132. }
  133. }
  134. platform_set_drvdata(pdev, p);
  135. return 0;
  136. }
  137. static int sunfire_led_generic_remove(struct platform_device *pdev)
  138. {
  139. struct sunfire_drvdata *p = platform_get_drvdata(pdev);
  140. int i;
  141. for (i = 0; i < NUM_LEDS_PER_BOARD; i++)
  142. led_classdev_unregister(&p->leds[i].led_cdev);
  143. return 0;
  144. }
  145. static struct led_type clockboard_led_types[NUM_LEDS_PER_BOARD] = {
  146. {
  147. .name = "clockboard-left",
  148. .handler = clockboard_left_set,
  149. },
  150. {
  151. .name = "clockboard-middle",
  152. .handler = clockboard_middle_set,
  153. },
  154. {
  155. .name = "clockboard-right",
  156. .handler = clockboard_right_set,
  157. .default_trigger = "heartbeat",
  158. },
  159. };
  160. static int sunfire_clockboard_led_probe(struct platform_device *pdev)
  161. {
  162. return sunfire_led_generic_probe(pdev, clockboard_led_types);
  163. }
  164. static struct led_type fhc_led_types[NUM_LEDS_PER_BOARD] = {
  165. {
  166. .name = "fhc-left",
  167. .handler = fhc_left_set,
  168. },
  169. {
  170. .name = "fhc-middle",
  171. .handler = fhc_middle_set,
  172. },
  173. {
  174. .name = "fhc-right",
  175. .handler = fhc_right_set,
  176. .default_trigger = "heartbeat",
  177. },
  178. };
  179. static int sunfire_fhc_led_probe(struct platform_device *pdev)
  180. {
  181. return sunfire_led_generic_probe(pdev, fhc_led_types);
  182. }
  183. MODULE_ALIAS("platform:sunfire-clockboard-leds");
  184. MODULE_ALIAS("platform:sunfire-fhc-leds");
  185. static struct platform_driver sunfire_clockboard_led_driver = {
  186. .probe = sunfire_clockboard_led_probe,
  187. .remove = sunfire_led_generic_remove,
  188. .driver = {
  189. .name = "sunfire-clockboard-leds",
  190. },
  191. };
  192. static struct platform_driver sunfire_fhc_led_driver = {
  193. .probe = sunfire_fhc_led_probe,
  194. .remove = sunfire_led_generic_remove,
  195. .driver = {
  196. .name = "sunfire-fhc-leds",
  197. },
  198. };
  199. static struct platform_driver * const drivers[] = {
  200. &sunfire_clockboard_led_driver,
  201. &sunfire_fhc_led_driver,
  202. };
  203. static int __init sunfire_leds_init(void)
  204. {
  205. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  206. }
  207. static void __exit sunfire_leds_exit(void)
  208. {
  209. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  210. }
  211. module_init(sunfire_leds_init);
  212. module_exit(sunfire_leds_exit);