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