1c746b551SMark Brown /*
2c746b551SMark Brown  * LED driver for WM831x status LEDs
3c746b551SMark Brown  *
4c746b551SMark Brown  * Copyright(C) 2009 Wolfson Microelectronics PLC.
5c746b551SMark Brown  *
6c746b551SMark Brown  * This program is free software; you can redistribute it and/or modify
7c746b551SMark Brown  * it under the terms of the GNU General Public License version 2 as
8c746b551SMark Brown  * published by the Free Software Foundation.
9c746b551SMark Brown  *
10c746b551SMark Brown  */
11c746b551SMark Brown 
12c746b551SMark Brown #include <linux/kernel.h>
13c746b551SMark Brown #include <linux/init.h>
14c746b551SMark Brown #include <linux/platform_device.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
16c746b551SMark Brown #include <linux/leds.h>
17c746b551SMark Brown #include <linux/err.h>
18c746b551SMark Brown #include <linux/mfd/wm831x/core.h>
19c746b551SMark Brown #include <linux/mfd/wm831x/pdata.h>
20c746b551SMark Brown #include <linux/mfd/wm831x/status.h>
21c746b551SMark Brown 
22c746b551SMark Brown 
23c746b551SMark Brown struct wm831x_status {
24c746b551SMark Brown 	struct led_classdev cdev;
25c746b551SMark Brown 	struct wm831x *wm831x;
26c746b551SMark Brown 	struct work_struct work;
27c746b551SMark Brown 	struct mutex mutex;
28c746b551SMark Brown 
29c746b551SMark Brown 	spinlock_t value_lock;
30c746b551SMark Brown 	int reg;     /* Control register */
31c746b551SMark Brown 	int reg_val; /* Control register value */
32c746b551SMark Brown 
33c746b551SMark Brown 	int blink;
34c746b551SMark Brown 	int blink_time;
35c746b551SMark Brown 	int blink_cyc;
36c746b551SMark Brown 	int src;
37c746b551SMark Brown 	enum led_brightness brightness;
38c746b551SMark Brown };
39c746b551SMark Brown 
40c746b551SMark Brown #define to_wm831x_status(led_cdev) \
41c746b551SMark Brown 	container_of(led_cdev, struct wm831x_status, cdev)
42c746b551SMark Brown 
43c746b551SMark Brown static void wm831x_status_work(struct work_struct *work)
44c746b551SMark Brown {
45c746b551SMark Brown 	struct wm831x_status *led = container_of(work, struct wm831x_status,
46c746b551SMark Brown 						 work);
47c746b551SMark Brown 	unsigned long flags;
48c746b551SMark Brown 
49c746b551SMark Brown 	mutex_lock(&led->mutex);
50c746b551SMark Brown 
51c746b551SMark Brown 	led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK |
52c746b551SMark Brown 			  WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK);
53c746b551SMark Brown 
54c746b551SMark Brown 	spin_lock_irqsave(&led->value_lock, flags);
55c746b551SMark Brown 
56c746b551SMark Brown 	led->reg_val |= led->src << WM831X_LED_SRC_SHIFT;
57c746b551SMark Brown 	if (led->blink) {
58c746b551SMark Brown 		led->reg_val |= 2 << WM831X_LED_MODE_SHIFT;
59c746b551SMark Brown 		led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT;
60c746b551SMark Brown 		led->reg_val |= led->blink_cyc;
61c746b551SMark Brown 	} else {
62c746b551SMark Brown 		if (led->brightness != LED_OFF)
63c746b551SMark Brown 			led->reg_val |= 1 << WM831X_LED_MODE_SHIFT;
64c746b551SMark Brown 	}
65c746b551SMark Brown 
66c746b551SMark Brown 	spin_unlock_irqrestore(&led->value_lock, flags);
67c746b551SMark Brown 
68c746b551SMark Brown 	wm831x_reg_write(led->wm831x, led->reg, led->reg_val);
69c746b551SMark Brown 
70c746b551SMark Brown 	mutex_unlock(&led->mutex);
71c746b551SMark Brown }
72c746b551SMark Brown 
73c746b551SMark Brown static void wm831x_status_set(struct led_classdev *led_cdev,
74c746b551SMark Brown 			   enum led_brightness value)
75c746b551SMark Brown {
76c746b551SMark Brown 	struct wm831x_status *led = to_wm831x_status(led_cdev);
77c746b551SMark Brown 	unsigned long flags;
78c746b551SMark Brown 
79c746b551SMark Brown 	spin_lock_irqsave(&led->value_lock, flags);
80c746b551SMark Brown 	led->brightness = value;
81c746b551SMark Brown 	if (value == LED_OFF)
82c746b551SMark Brown 		led->blink = 0;
83c746b551SMark Brown 	schedule_work(&led->work);
84c746b551SMark Brown 	spin_unlock_irqrestore(&led->value_lock, flags);
85c746b551SMark Brown }
86c746b551SMark Brown 
87c746b551SMark Brown static int wm831x_status_blink_set(struct led_classdev *led_cdev,
88c746b551SMark Brown 				   unsigned long *delay_on,
89c746b551SMark Brown 				   unsigned long *delay_off)
90c746b551SMark Brown {
91c746b551SMark Brown 	struct wm831x_status *led = to_wm831x_status(led_cdev);
92c746b551SMark Brown 	unsigned long flags;
93c746b551SMark Brown 	int ret = 0;
94c746b551SMark Brown 
95c746b551SMark Brown 	/* Pick some defaults if we've not been given times */
96c746b551SMark Brown 	if (*delay_on == 0 && *delay_off == 0) {
97c746b551SMark Brown 		*delay_on = 250;
98c746b551SMark Brown 		*delay_off = 250;
99c746b551SMark Brown 	}
100c746b551SMark Brown 
101c746b551SMark Brown 	spin_lock_irqsave(&led->value_lock, flags);
102c746b551SMark Brown 
103c746b551SMark Brown 	/* We only have a limited selection of settings, see if we can
104c746b551SMark Brown 	 * support the configuration we're being given */
105c746b551SMark Brown 	switch (*delay_on) {
106c746b551SMark Brown 	case 1000:
107c746b551SMark Brown 		led->blink_time = 0;
108c746b551SMark Brown 		break;
109c746b551SMark Brown 	case 250:
110c746b551SMark Brown 		led->blink_time = 1;
111c746b551SMark Brown 		break;
112c746b551SMark Brown 	case 125:
113c746b551SMark Brown 		led->blink_time = 2;
114c746b551SMark Brown 		break;
115c746b551SMark Brown 	case 62:
116c746b551SMark Brown 	case 63:
117c746b551SMark Brown 		/* Actually 62.5ms */
118c746b551SMark Brown 		led->blink_time = 3;
119c746b551SMark Brown 		break;
120c746b551SMark Brown 	default:
121c746b551SMark Brown 		ret = -EINVAL;
122c746b551SMark Brown 		break;
123c746b551SMark Brown 	}
124c746b551SMark Brown 
125c746b551SMark Brown 	if (ret == 0) {
126c746b551SMark Brown 		switch (*delay_off / *delay_on) {
127c746b551SMark Brown 		case 1:
128c746b551SMark Brown 			led->blink_cyc = 0;
129c746b551SMark Brown 			break;
130c746b551SMark Brown 		case 3:
131c746b551SMark Brown 			led->blink_cyc = 1;
132c746b551SMark Brown 			break;
133c746b551SMark Brown 		case 4:
134c746b551SMark Brown 			led->blink_cyc = 2;
135c746b551SMark Brown 			break;
136c746b551SMark Brown 		case 8:
137c746b551SMark Brown 			led->blink_cyc = 3;
138c746b551SMark Brown 			break;
139c746b551SMark Brown 		default:
140c746b551SMark Brown 			ret = -EINVAL;
141c746b551SMark Brown 			break;
142c746b551SMark Brown 		}
143c746b551SMark Brown 	}
144c746b551SMark Brown 
145c746b551SMark Brown 	if (ret == 0)
146c746b551SMark Brown 		led->blink = 1;
147c746b551SMark Brown 	else
148c746b551SMark Brown 		led->blink = 0;
149c746b551SMark Brown 
150c746b551SMark Brown 	/* Always update; if we fail turn off blinking since we expect
151c746b551SMark Brown 	 * a software fallback. */
152c746b551SMark Brown 	schedule_work(&led->work);
153c746b551SMark Brown 
154c746b551SMark Brown 	spin_unlock_irqrestore(&led->value_lock, flags);
155c746b551SMark Brown 
156c746b551SMark Brown 	return ret;
157c746b551SMark Brown }
158c746b551SMark Brown 
159c746b551SMark Brown static const char *led_src_texts[] = {
160c746b551SMark Brown 	"otp",
161c746b551SMark Brown 	"power",
162c746b551SMark Brown 	"charger",
163c746b551SMark Brown 	"soft",
164c746b551SMark Brown };
165c746b551SMark Brown 
166c746b551SMark Brown static ssize_t wm831x_status_src_show(struct device *dev,
167c746b551SMark Brown 				      struct device_attribute *attr, char *buf)
168c746b551SMark Brown {
169c746b551SMark Brown 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
170c746b551SMark Brown 	struct wm831x_status *led = to_wm831x_status(led_cdev);
171c746b551SMark Brown 	int i;
172c746b551SMark Brown 	ssize_t ret = 0;
173c746b551SMark Brown 
174c746b551SMark Brown 	mutex_lock(&led->mutex);
175c746b551SMark Brown 
176c746b551SMark Brown 	for (i = 0; i < ARRAY_SIZE(led_src_texts); i++)
177c746b551SMark Brown 		if (i == led->src)
178c746b551SMark Brown 			ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]);
179c746b551SMark Brown 		else
180c746b551SMark Brown 			ret += sprintf(&buf[ret], "%s ", led_src_texts[i]);
181c746b551SMark Brown 
182c746b551SMark Brown 	mutex_unlock(&led->mutex);
183c746b551SMark Brown 
184c746b551SMark Brown 	ret += sprintf(&buf[ret], "\n");
185c746b551SMark Brown 
186c746b551SMark Brown 	return ret;
187c746b551SMark Brown }
188c746b551SMark Brown 
189c746b551SMark Brown static ssize_t wm831x_status_src_store(struct device *dev,
190c746b551SMark Brown 				       struct device_attribute *attr,
191c746b551SMark Brown 				       const char *buf, size_t size)
192c746b551SMark Brown {
193c746b551SMark Brown 	struct led_classdev *led_cdev = dev_get_drvdata(dev);
194c746b551SMark Brown 	struct wm831x_status *led = to_wm831x_status(led_cdev);
195c746b551SMark Brown 	char name[20];
196c746b551SMark Brown 	int i;
197c746b551SMark Brown 	size_t len;
198c746b551SMark Brown 
199c746b551SMark Brown 	name[sizeof(name) - 1] = '\0';
200c746b551SMark Brown 	strncpy(name, buf, sizeof(name) - 1);
201c746b551SMark Brown 	len = strlen(name);
202c746b551SMark Brown 
203c746b551SMark Brown 	if (len && name[len - 1] == '\n')
204c746b551SMark Brown 		name[len - 1] = '\0';
205c746b551SMark Brown 
206c746b551SMark Brown 	for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) {
207c746b551SMark Brown 		if (!strcmp(name, led_src_texts[i])) {
208c746b551SMark Brown 			mutex_lock(&led->mutex);
209c746b551SMark Brown 
210c746b551SMark Brown 			led->src = i;
211c746b551SMark Brown 			schedule_work(&led->work);
212c746b551SMark Brown 
213c746b551SMark Brown 			mutex_unlock(&led->mutex);
214c746b551SMark Brown 		}
215c746b551SMark Brown 	}
216c746b551SMark Brown 
217c746b551SMark Brown 	return size;
218c746b551SMark Brown }
219c746b551SMark Brown 
220c746b551SMark Brown static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store);
221c746b551SMark Brown 
222c746b551SMark Brown static int wm831x_status_probe(struct platform_device *pdev)
223c746b551SMark Brown {
224c746b551SMark Brown 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
225c746b551SMark Brown 	struct wm831x_pdata *chip_pdata;
226c746b551SMark Brown 	struct wm831x_status_pdata pdata;
227c746b551SMark Brown 	struct wm831x_status *drvdata;
228c746b551SMark Brown 	struct resource *res;
229c746b551SMark Brown 	int id = pdev->id % ARRAY_SIZE(chip_pdata->status);
230c746b551SMark Brown 	int ret;
231c746b551SMark Brown 
232c746b551SMark Brown 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
233c746b551SMark Brown 	if (res == NULL) {
234c746b551SMark Brown 		dev_err(&pdev->dev, "No I/O resource\n");
235c746b551SMark Brown 		ret = -EINVAL;
236c746b551SMark Brown 		goto err;
237c746b551SMark Brown 	}
238c746b551SMark Brown 
239c746b551SMark Brown 	drvdata = kzalloc(sizeof(struct wm831x_status), GFP_KERNEL);
240c746b551SMark Brown 	if (!drvdata)
241c746b551SMark Brown 		return -ENOMEM;
242c746b551SMark Brown 	dev_set_drvdata(&pdev->dev, drvdata);
243c746b551SMark Brown 
244c746b551SMark Brown 	drvdata->wm831x = wm831x;
245c746b551SMark Brown 	drvdata->reg = res->start;
246c746b551SMark Brown 
247c746b551SMark Brown 	if (wm831x->dev->platform_data)
248c746b551SMark Brown 		chip_pdata = wm831x->dev->platform_data;
249c746b551SMark Brown 	else
250c746b551SMark Brown 		chip_pdata = NULL;
251c746b551SMark Brown 
252c746b551SMark Brown 	memset(&pdata, 0, sizeof(pdata));
253c746b551SMark Brown 	if (chip_pdata && chip_pdata->status[id])
254c746b551SMark Brown 		memcpy(&pdata, chip_pdata->status[id], sizeof(pdata));
255c746b551SMark Brown 	else
256c746b551SMark Brown 		pdata.name = dev_name(&pdev->dev);
257c746b551SMark Brown 
258c746b551SMark Brown 	mutex_init(&drvdata->mutex);
259c746b551SMark Brown 	INIT_WORK(&drvdata->work, wm831x_status_work);
260c746b551SMark Brown 	spin_lock_init(&drvdata->value_lock);
261c746b551SMark Brown 
262c746b551SMark Brown 	/* We cache the configuration register and read startup values
263c746b551SMark Brown 	 * from it. */
264c746b551SMark Brown 	drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg);
265c746b551SMark Brown 
266c746b551SMark Brown 	if (drvdata->reg_val & WM831X_LED_MODE_MASK)
267c746b551SMark Brown 		drvdata->brightness = LED_FULL;
268c746b551SMark Brown 	else
269c746b551SMark Brown 		drvdata->brightness = LED_OFF;
270c746b551SMark Brown 
271c746b551SMark Brown 	/* Set a default source if configured, otherwise leave the
272c746b551SMark Brown 	 * current hardware setting.
273c746b551SMark Brown 	 */
274c746b551SMark Brown 	if (pdata.default_src == WM831X_STATUS_PRESERVE) {
275c746b551SMark Brown 		drvdata->src = drvdata->reg_val;
276c746b551SMark Brown 		drvdata->src &= WM831X_LED_SRC_MASK;
277c746b551SMark Brown 		drvdata->src >>= WM831X_LED_SRC_SHIFT;
278c746b551SMark Brown 	} else {
279c746b551SMark Brown 		drvdata->src = pdata.default_src - 1;
280c746b551SMark Brown 	}
281c746b551SMark Brown 
282c746b551SMark Brown 	drvdata->cdev.name = pdata.name;
283c746b551SMark Brown 	drvdata->cdev.default_trigger = pdata.default_trigger;
284c746b551SMark Brown 	drvdata->cdev.brightness_set = wm831x_status_set;
285c746b551SMark Brown 	drvdata->cdev.blink_set = wm831x_status_blink_set;
286c746b551SMark Brown 
287c746b551SMark Brown 	ret = led_classdev_register(wm831x->dev, &drvdata->cdev);
288c746b551SMark Brown 	if (ret < 0) {
289c746b551SMark Brown 		dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
290c746b551SMark Brown 		goto err_led;
291c746b551SMark Brown 	}
292c746b551SMark Brown 
293c746b551SMark Brown 	ret = device_create_file(drvdata->cdev.dev, &dev_attr_src);
294c746b551SMark Brown 	if (ret != 0)
295c746b551SMark Brown 		dev_err(&pdev->dev,
296c746b551SMark Brown 			"No source control for LED: %d\n", ret);
297c746b551SMark Brown 
298c746b551SMark Brown 	return 0;
299c746b551SMark Brown 
300c746b551SMark Brown err_led:
301c746b551SMark Brown 	led_classdev_unregister(&drvdata->cdev);
302c746b551SMark Brown 	kfree(drvdata);
303c746b551SMark Brown err:
304c746b551SMark Brown 	return ret;
305c746b551SMark Brown }
306c746b551SMark Brown 
307c746b551SMark Brown static int wm831x_status_remove(struct platform_device *pdev)
308c746b551SMark Brown {
309c746b551SMark Brown 	struct wm831x_status *drvdata = platform_get_drvdata(pdev);
310c746b551SMark Brown 
311c746b551SMark Brown 	device_remove_file(drvdata->cdev.dev, &dev_attr_src);
312c746b551SMark Brown 	led_classdev_unregister(&drvdata->cdev);
313c746b551SMark Brown 	kfree(drvdata);
314c746b551SMark Brown 
315c746b551SMark Brown 	return 0;
316c746b551SMark Brown }
317c746b551SMark Brown 
318c746b551SMark Brown static struct platform_driver wm831x_status_driver = {
319c746b551SMark Brown 	.driver = {
320c746b551SMark Brown 		   .name = "wm831x-status",
321c746b551SMark Brown 		   .owner = THIS_MODULE,
322c746b551SMark Brown 		   },
323c746b551SMark Brown 	.probe = wm831x_status_probe,
324c746b551SMark Brown 	.remove = wm831x_status_remove,
325c746b551SMark Brown };
326c746b551SMark Brown 
327c746b551SMark Brown static int __devinit wm831x_status_init(void)
328c746b551SMark Brown {
329c746b551SMark Brown 	return platform_driver_register(&wm831x_status_driver);
330c746b551SMark Brown }
331c746b551SMark Brown module_init(wm831x_status_init);
332c746b551SMark Brown 
333c746b551SMark Brown static void wm831x_status_exit(void)
334c746b551SMark Brown {
335c746b551SMark Brown 	platform_driver_unregister(&wm831x_status_driver);
336c746b551SMark Brown }
337c746b551SMark Brown module_exit(wm831x_status_exit);
338c746b551SMark Brown 
339c746b551SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
340c746b551SMark Brown MODULE_DESCRIPTION("WM831x status LED driver");
341c746b551SMark Brown MODULE_LICENSE("GPL");
342c746b551SMark Brown MODULE_ALIAS("platform:wm831x-status");
343