leds-sunfire.c 6.0 KB

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