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