2 * LED driver for WM831x status LEDs
4 * Copyright(C) 2009 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/leds.h>
16 #include <linux/err.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/pdata.h>
19 #include <linux/mfd/wm831x/status.h>
20 #include <linux/module.h>
23 struct wm831x_status
{
24 struct led_classdev cdev
;
25 struct wm831x
*wm831x
;
28 spinlock_t value_lock
;
29 int reg
; /* Control register */
30 int reg_val
; /* Control register value */
36 enum led_brightness brightness
;
39 #define to_wm831x_status(led_cdev) \
40 container_of(led_cdev, struct wm831x_status, cdev)
42 static void wm831x_status_set(struct wm831x_status
*led
)
46 mutex_lock(&led
->mutex
);
48 led
->reg_val
&= ~(WM831X_LED_SRC_MASK
| WM831X_LED_MODE_MASK
|
49 WM831X_LED_DUTY_CYC_MASK
| WM831X_LED_DUR_MASK
);
51 spin_lock_irqsave(&led
->value_lock
, flags
);
53 led
->reg_val
|= led
->src
<< WM831X_LED_SRC_SHIFT
;
55 led
->reg_val
|= 2 << WM831X_LED_MODE_SHIFT
;
56 led
->reg_val
|= led
->blink_time
<< WM831X_LED_DUR_SHIFT
;
57 led
->reg_val
|= led
->blink_cyc
;
59 if (led
->brightness
!= LED_OFF
)
60 led
->reg_val
|= 1 << WM831X_LED_MODE_SHIFT
;
63 spin_unlock_irqrestore(&led
->value_lock
, flags
);
65 wm831x_reg_write(led
->wm831x
, led
->reg
, led
->reg_val
);
67 mutex_unlock(&led
->mutex
);
70 static int wm831x_status_brightness_set(struct led_classdev
*led_cdev
,
71 enum led_brightness value
)
73 struct wm831x_status
*led
= to_wm831x_status(led_cdev
);
76 spin_lock_irqsave(&led
->value_lock
, flags
);
77 led
->brightness
= value
;
80 spin_unlock_irqrestore(&led
->value_lock
, flags
);
81 wm831x_status_set(led
);
86 static int wm831x_status_blink_set(struct led_classdev
*led_cdev
,
87 unsigned long *delay_on
,
88 unsigned long *delay_off
)
90 struct wm831x_status
*led
= to_wm831x_status(led_cdev
);
94 /* Pick some defaults if we've not been given times */
95 if (*delay_on
== 0 && *delay_off
== 0) {
100 spin_lock_irqsave(&led
->value_lock
, flags
);
102 /* We only have a limited selection of settings, see if we can
103 * support the configuration we're being given */
116 /* Actually 62.5ms */
125 switch (*delay_off
/ *delay_on
) {
149 spin_unlock_irqrestore(&led
->value_lock
, flags
);
150 wm831x_status_set(led
);
155 static const char * const led_src_texts
[] = {
162 static ssize_t
wm831x_status_src_show(struct device
*dev
,
163 struct device_attribute
*attr
, char *buf
)
165 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
166 struct wm831x_status
*led
= to_wm831x_status(led_cdev
);
170 mutex_lock(&led
->mutex
);
172 for (i
= 0; i
< ARRAY_SIZE(led_src_texts
); i
++)
174 ret
+= sprintf(&buf
[ret
], "[%s] ", led_src_texts
[i
]);
176 ret
+= sprintf(&buf
[ret
], "%s ", led_src_texts
[i
]);
178 mutex_unlock(&led
->mutex
);
180 ret
+= sprintf(&buf
[ret
], "\n");
185 static ssize_t
wm831x_status_src_store(struct device
*dev
,
186 struct device_attribute
*attr
,
187 const char *buf
, size_t size
)
189 struct led_classdev
*led_cdev
= dev_get_drvdata(dev
);
190 struct wm831x_status
*led
= to_wm831x_status(led_cdev
);
193 i
= sysfs_match_string(led_src_texts
, buf
);
195 mutex_lock(&led
->mutex
);
197 mutex_unlock(&led
->mutex
);
198 wm831x_status_set(led
);
204 static DEVICE_ATTR(src
, 0644, wm831x_status_src_show
, wm831x_status_src_store
);
206 static struct attribute
*wm831x_status_attrs
[] = {
210 ATTRIBUTE_GROUPS(wm831x_status
);
212 static int wm831x_status_probe(struct platform_device
*pdev
)
214 struct wm831x
*wm831x
= dev_get_drvdata(pdev
->dev
.parent
);
215 struct wm831x_pdata
*chip_pdata
;
216 struct wm831x_status_pdata pdata
;
217 struct wm831x_status
*drvdata
;
218 struct resource
*res
;
219 int id
= pdev
->id
% ARRAY_SIZE(chip_pdata
->status
);
222 res
= platform_get_resource(pdev
, IORESOURCE_REG
, 0);
224 dev_err(&pdev
->dev
, "No register resource\n");
228 drvdata
= devm_kzalloc(&pdev
->dev
, sizeof(struct wm831x_status
),
233 drvdata
->wm831x
= wm831x
;
234 drvdata
->reg
= res
->start
;
236 if (dev_get_platdata(wm831x
->dev
))
237 chip_pdata
= dev_get_platdata(wm831x
->dev
);
241 memset(&pdata
, 0, sizeof(pdata
));
242 if (chip_pdata
&& chip_pdata
->status
[id
])
243 memcpy(&pdata
, chip_pdata
->status
[id
], sizeof(pdata
));
245 pdata
.name
= dev_name(&pdev
->dev
);
247 mutex_init(&drvdata
->mutex
);
248 spin_lock_init(&drvdata
->value_lock
);
250 /* We cache the configuration register and read startup values
252 drvdata
->reg_val
= wm831x_reg_read(wm831x
, drvdata
->reg
);
254 if (drvdata
->reg_val
& WM831X_LED_MODE_MASK
)
255 drvdata
->brightness
= LED_FULL
;
257 drvdata
->brightness
= LED_OFF
;
259 /* Set a default source if configured, otherwise leave the
260 * current hardware setting.
262 if (pdata
.default_src
== WM831X_STATUS_PRESERVE
) {
263 drvdata
->src
= drvdata
->reg_val
;
264 drvdata
->src
&= WM831X_LED_SRC_MASK
;
265 drvdata
->src
>>= WM831X_LED_SRC_SHIFT
;
267 drvdata
->src
= pdata
.default_src
- 1;
270 drvdata
->cdev
.name
= pdata
.name
;
271 drvdata
->cdev
.default_trigger
= pdata
.default_trigger
;
272 drvdata
->cdev
.brightness_set_blocking
= wm831x_status_brightness_set
;
273 drvdata
->cdev
.blink_set
= wm831x_status_blink_set
;
274 drvdata
->cdev
.groups
= wm831x_status_groups
;
276 ret
= devm_led_classdev_register(wm831x
->dev
, &drvdata
->cdev
);
278 dev_err(&pdev
->dev
, "Failed to register LED: %d\n", ret
);
285 static struct platform_driver wm831x_status_driver
= {
287 .name
= "wm831x-status",
289 .probe
= wm831x_status_probe
,
292 module_platform_driver(wm831x_status_driver
);
294 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
295 MODULE_DESCRIPTION("WM831x status LED driver");
296 MODULE_LICENSE("GPL");
297 MODULE_ALIAS("platform:wm831x-status");