aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/leds/leds-sunfire.c
blob: 388632d23d447130ca3a8368cd48bae9a9b410eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* leds-sunfire.c: SUNW,Ultra-Enterprise LED driver.
 *
 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/leds.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/slab.h>

#include <asm/fhc.h>
#include <asm/upa.h>

MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
MODULE_DESCRIPTION("Sun Fire LED driver");
MODULE_LICENSE("GPL");

struct sunfire_led {
	struct led_classdev	led_cdev;
	void __iomem		*reg;
};
#define	to_sunfire_led(d) container_of(d, struct sunfire_led, led_cdev)

static void __clockboard_set(struct led_classdev *led_cdev,
			     enum led_brightness led_val, u8 bit)
{
	struct sunfire_led *p = to_sunfire_led(led_cdev);
	u8 reg = upa_readb(p->reg);

	switch (bit) {
	case CLOCK_CTRL_LLED:
		if (led_val)
			reg &= ~bit;
		else
			reg |= bit;
		break;

	default:
		if (led_val)
			reg |= bit;
		else
			reg &= ~bit;
		break;
	}
	upa_writeb(reg, p->reg);
}

static void clockboard_left_set(struct led_classdev *led_cdev,
				enum led_brightness led_val)
{
	__clockboard_set(led_cdev, led_val, CLOCK_CTRL_LLED);
}

static void clockboard_middle_set(struct led_classdev *led_cdev,
				  enum led_brightness led_val)
{
	__clockboard_set(led_cdev, led_val, CLOCK_CTRL_MLED);
}

static void clockboard_right_set(struct led_classdev *led_cdev,
				 enum led_brightness led_val)
{
	__clockboard_set(led_cdev, led_val, CLOCK_CTRL_RLED);
}

static void __fhc_set(struct led_classdev *led_cdev,
			     enum led_brightness led_val, u32 bit)
{
	struct sunfire_led *p = to_sunfire_led(led_cdev);
	u32 reg = upa_readl(p->reg);

	switch (bit) {
	case FHC_CONTROL_LLED:
		if (led_val)
			reg &= ~bit;
		else
			reg |= bit;
		break;

	default:
		if (led_val)
			reg |= bit;
		else
			reg &= ~bit;
		break;
	}
	upa_writel(reg, p->reg);
}

static void fhc_left_set(struct led_classdev *led_cdev,
			 enum led_brightness led_val)
{
	__fhc_set(led_cdev, led_val, FHC_CONTROL_LLED);
}

static void fhc_middle_set(struct led_classdev *led_cdev,
			   enum led_brightness led_val)
{
	__fhc_set(led_cdev, led_val, FHC_CONTROL_MLED);
}

static void fhc_right_set(struct led_classdev *led_cdev,
			  enum led_brightness led_val)
{
	__fhc_set(led_cdev, led_val, FHC_CONTROL_RLED);
}

typedef void (*set_handler)(struct led_classdev *, enum led_brightness);
struct led_type {
	const char	*name;
	set_handler	handler;
	const char	*default_trigger;
};

#define NUM_LEDS_PER_BOARD	3
struct sunfire_drvdata {
	struct sunfire_led	leds[NUM_LEDS_PER_BOARD];
};

static int sunfire_led_generic_probe(struct platform_device *pdev,
					       struct led_type *types)
{
	struct sunfire_drvdata *p;
	int i, err;

	if (pdev->num_resources != 1) {
		dev_err(&pdev->dev, "Wrong number of resources %d, should be 1\n",
		       pdev->num_resources);
		return -EINVAL;
	}

	p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
	if (!p) {
		dev_err(&pdev->dev, "Could not allocate struct sunfire_drvdata\n");
		return -ENOMEM;
	}

	for (i = 0; i < NUM_LEDS_PER_BOARD; i++) {
		struct led_classdev *lp = &p->leds[i].led_cdev;

		p->leds[i].reg = (void __iomem *) pdev->resource[0].start;
		lp->name = types[i].name;
		lp->brightness = LED_FULL;
		lp->brightness_set = types[i].handler;
		lp->default_trigger = types[i].default_trigger;

		err = led_classdev_register(&pdev->dev, lp);
		if (err) {
			dev_err(&pdev->dev, "Could not register %s LED\n",
			       lp->name);
			for (i--; i >= 0; i--)
				led_classdev_unregister(&p->leds[i].led_cdev);
			return err;
		}
	}

	platform_set_drvdata(pdev, p);

	return 0;
}

static int sunfire_led_generic_remove(struct platform_device *pdev)
{
	struct sunfire_drvdata *p = platform_get_drvdata(pdev);
	int i;

	for (i = 0; i < NUM_LEDS_PER_BOARD; i++)
		led_classdev_unregister(&p->leds[i].led_cdev);

	return 0;
}

static struct led_type clockboard_led_types[NUM_LEDS_PER_BOARD] = {
	{
		.name		= "clockboard-left",
		.handler	= clockboard_left_set,
	},
	{
		.name		= "clockboard-middle",
		.handler	= clockboard_middle_set,
	},
	{
		.name		= "clockboard-right",
		.handler	= clockboard_right_set,
		.default_trigger = "heartbeat",
	},
};

static int sunfire_clockboard_led_probe(struct platform_device *pdev)
{
	return sunfire_led_generic_probe(pdev, clockboard_led_types);
}

static struct led_type fhc_led_types[NUM_LEDS_PER_BOARD] = {
	{
		.name		= "fhc-left",
		.handler	= fhc_left_set,
	},
	{
		.name		= "fhc-middle",
		.handler	= fhc_middle_set,
	},
	{
		.name		= "fhc-right",
		.handler	= fhc_right_set,
		.default_trigger = "heartbeat",
	},
};

static int sunfire_fhc_led_probe(struct platform_device *pdev)
{
	return sunfire_led_generic_probe(pdev, fhc_led_types);
}

MODULE_ALIAS("platform:sunfire-clockboard-leds");
MODULE_ALIAS("platform:sunfire-fhc-leds");

static struct platform_driver sunfire_clockboard_led_driver = {
	.probe		= sunfire_clockboard_led_probe,
	.remove		= sunfire_led_generic_remove,
	.driver		= {
		.name	= "sunfire-clockboard-leds",
		.owner	= THIS_MODULE,
	},
};

static struct platform_driver sunfire_fhc_led_driver = {
	.probe		= sunfire_fhc_led_probe,
	.remove		= sunfire_led_generic_remove,
	.driver		= {
		.name	= "sunfire-fhc-leds",
		.owner	= THIS_MODULE,
	},
};

static int __init sunfire_leds_init(void)
{
	int err = platform_driver_register(&sunfire_clockboard_led_driver);

	if (err) {
		pr_err("Could not register clock board LED driver\n");
		return err;
	}

	err = platform_driver_register(&sunfire_fhc_led_driver);
	if (err) {
		pr_err("Could not register FHC LED driver\n");
		platform_driver_unregister(&sunfire_clockboard_led_driver);
	}

	return err;
}

static void __exit sunfire_leds_exit(void)
{
	platform_driver_unregister(&sunfire_clockboard_led_driver);
	platform_driver_unregister(&sunfire_fhc_led_driver);
}

module_init(sunfire_leds_init);
module_exit(sunfire_leds_exit);