blob: be93b20e792a7aa36dc81a0cb60de7bb8c66654d [file] [log] [blame]
Mark Brownc746b552009-09-05 14:09:21 +01001/*
2 * LED driver for WM831x status LEDs
3 *
4 * Copyright(C) 2009 Wolfson Microelectronics PLC.
5 *
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.
9 *
10 */
11
12#include <linux/kernel.h>
Mark Brownc746b552009-09-05 14:09:21 +010013#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Mark Brownc746b552009-09-05 14:09:21 +010015#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>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040020#include <linux/module.h>
Mark Brownc746b552009-09-05 14:09:21 +010021
22
23struct wm831x_status {
24 struct led_classdev cdev;
25 struct wm831x *wm831x;
Mark Brownc746b552009-09-05 14:09:21 +010026 struct mutex mutex;
27
28 spinlock_t value_lock;
29 int reg; /* Control register */
30 int reg_val; /* Control register value */
31
32 int blink;
33 int blink_time;
34 int blink_cyc;
35 int src;
36 enum led_brightness brightness;
37};
38
39#define to_wm831x_status(led_cdev) \
40 container_of(led_cdev, struct wm831x_status, cdev)
41
Andrew Lunnf633a512015-08-20 12:40:45 +020042static void wm831x_status_set(struct wm831x_status *led)
Mark Brownc746b552009-09-05 14:09:21 +010043{
Mark Brownc746b552009-09-05 14:09:21 +010044 unsigned long flags;
45
46 mutex_lock(&led->mutex);
47
48 led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK |
49 WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK);
50
51 spin_lock_irqsave(&led->value_lock, flags);
52
53 led->reg_val |= led->src << WM831X_LED_SRC_SHIFT;
54 if (led->blink) {
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;
58 } else {
59 if (led->brightness != LED_OFF)
60 led->reg_val |= 1 << WM831X_LED_MODE_SHIFT;
61 }
62
63 spin_unlock_irqrestore(&led->value_lock, flags);
64
65 wm831x_reg_write(led->wm831x, led->reg, led->reg_val);
66
67 mutex_unlock(&led->mutex);
68}
69
Andrew Lunnf633a512015-08-20 12:40:45 +020070static int wm831x_status_brightness_set(struct led_classdev *led_cdev,
71 enum led_brightness value)
Mark Brownc746b552009-09-05 14:09:21 +010072{
73 struct wm831x_status *led = to_wm831x_status(led_cdev);
74 unsigned long flags;
75
76 spin_lock_irqsave(&led->value_lock, flags);
77 led->brightness = value;
78 if (value == LED_OFF)
79 led->blink = 0;
Mark Brownc746b552009-09-05 14:09:21 +010080 spin_unlock_irqrestore(&led->value_lock, flags);
Andrew Lunnf633a512015-08-20 12:40:45 +020081 wm831x_status_set(led);
82
83 return 0;
Mark Brownc746b552009-09-05 14:09:21 +010084}
85
86static int wm831x_status_blink_set(struct led_classdev *led_cdev,
87 unsigned long *delay_on,
88 unsigned long *delay_off)
89{
90 struct wm831x_status *led = to_wm831x_status(led_cdev);
91 unsigned long flags;
92 int ret = 0;
93
94 /* Pick some defaults if we've not been given times */
95 if (*delay_on == 0 && *delay_off == 0) {
96 *delay_on = 250;
97 *delay_off = 250;
98 }
99
100 spin_lock_irqsave(&led->value_lock, flags);
101
102 /* We only have a limited selection of settings, see if we can
103 * support the configuration we're being given */
104 switch (*delay_on) {
105 case 1000:
106 led->blink_time = 0;
107 break;
108 case 250:
109 led->blink_time = 1;
110 break;
111 case 125:
112 led->blink_time = 2;
113 break;
114 case 62:
115 case 63:
116 /* Actually 62.5ms */
117 led->blink_time = 3;
118 break;
119 default:
120 ret = -EINVAL;
121 break;
122 }
123
124 if (ret == 0) {
125 switch (*delay_off / *delay_on) {
126 case 1:
127 led->blink_cyc = 0;
128 break;
129 case 3:
130 led->blink_cyc = 1;
131 break;
132 case 4:
133 led->blink_cyc = 2;
134 break;
135 case 8:
136 led->blink_cyc = 3;
137 break;
138 default:
139 ret = -EINVAL;
140 break;
141 }
142 }
143
144 if (ret == 0)
145 led->blink = 1;
146 else
147 led->blink = 0;
148
Mark Brownc746b552009-09-05 14:09:21 +0100149 spin_unlock_irqrestore(&led->value_lock, flags);
Andrew Lunnf633a512015-08-20 12:40:45 +0200150 wm831x_status_set(led);
Mark Brownc746b552009-09-05 14:09:21 +0100151
152 return ret;
153}
154
Jingoo Han8a4529a2013-01-21 21:59:29 -0800155static const char * const led_src_texts[] = {
Mark Brownc746b552009-09-05 14:09:21 +0100156 "otp",
157 "power",
158 "charger",
159 "soft",
160};
161
162static ssize_t wm831x_status_src_show(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 struct led_classdev *led_cdev = dev_get_drvdata(dev);
166 struct wm831x_status *led = to_wm831x_status(led_cdev);
167 int i;
168 ssize_t ret = 0;
169
170 mutex_lock(&led->mutex);
171
172 for (i = 0; i < ARRAY_SIZE(led_src_texts); i++)
173 if (i == led->src)
174 ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]);
175 else
176 ret += sprintf(&buf[ret], "%s ", led_src_texts[i]);
177
178 mutex_unlock(&led->mutex);
179
180 ret += sprintf(&buf[ret], "\n");
181
182 return ret;
183}
184
185static ssize_t wm831x_status_src_store(struct device *dev,
186 struct device_attribute *attr,
187 const char *buf, size_t size)
188{
189 struct led_classdev *led_cdev = dev_get_drvdata(dev);
190 struct wm831x_status *led = to_wm831x_status(led_cdev);
191 char name[20];
192 int i;
193 size_t len;
194
195 name[sizeof(name) - 1] = '\0';
196 strncpy(name, buf, sizeof(name) - 1);
197 len = strlen(name);
198
199 if (len && name[len - 1] == '\n')
200 name[len - 1] = '\0';
201
202 for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) {
203 if (!strcmp(name, led_src_texts[i])) {
204 mutex_lock(&led->mutex);
Mark Brownc746b552009-09-05 14:09:21 +0100205 led->src = i;
Mark Brownc746b552009-09-05 14:09:21 +0100206 mutex_unlock(&led->mutex);
Andrew Lunnf633a512015-08-20 12:40:45 +0200207 wm831x_status_set(led);
Mark Brownc746b552009-09-05 14:09:21 +0100208 }
209 }
210
211 return size;
212}
213
214static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store);
215
Johan Hovoldb2015ed2014-06-25 10:08:53 -0700216static struct attribute *wm831x_status_attrs[] = {
217 &dev_attr_src.attr,
218 NULL
219};
220ATTRIBUTE_GROUPS(wm831x_status);
221
Mark Brownc746b552009-09-05 14:09:21 +0100222static int wm831x_status_probe(struct platform_device *pdev)
223{
224 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
225 struct wm831x_pdata *chip_pdata;
226 struct wm831x_status_pdata pdata;
227 struct wm831x_status *drvdata;
228 struct resource *res;
229 int id = pdev->id % ARRAY_SIZE(chip_pdata->status);
230 int ret;
231
Mark Brown61abeba2013-08-29 07:18:14 -0700232 res = platform_get_resource(pdev, IORESOURCE_REG, 0);
Mark Brownc746b552009-09-05 14:09:21 +0100233 if (res == NULL) {
Mark Brown61abeba2013-08-29 07:18:14 -0700234 dev_err(&pdev->dev, "No register resource\n");
Johan Hovoldb2015ed2014-06-25 10:08:53 -0700235 return -EINVAL;
Mark Brownc746b552009-09-05 14:09:21 +0100236 }
237
Mark Brown1713cb92012-01-10 15:09:45 -0800238 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_status),
239 GFP_KERNEL);
Mark Brownc746b552009-09-05 14:09:21 +0100240 if (!drvdata)
241 return -ENOMEM;
Mark Brownc746b552009-09-05 14:09:21 +0100242
243 drvdata->wm831x = wm831x;
244 drvdata->reg = res->start;
245
Jingoo Han87aae1e2013-07-30 01:07:35 -0700246 if (dev_get_platdata(wm831x->dev))
247 chip_pdata = dev_get_platdata(wm831x->dev);
Mark Brownc746b552009-09-05 14:09:21 +0100248 else
249 chip_pdata = NULL;
250
251 memset(&pdata, 0, sizeof(pdata));
252 if (chip_pdata && chip_pdata->status[id])
253 memcpy(&pdata, chip_pdata->status[id], sizeof(pdata));
254 else
255 pdata.name = dev_name(&pdev->dev);
256
257 mutex_init(&drvdata->mutex);
Mark Brownc746b552009-09-05 14:09:21 +0100258 spin_lock_init(&drvdata->value_lock);
259
260 /* We cache the configuration register and read startup values
261 * from it. */
262 drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg);
263
264 if (drvdata->reg_val & WM831X_LED_MODE_MASK)
265 drvdata->brightness = LED_FULL;
266 else
267 drvdata->brightness = LED_OFF;
268
269 /* Set a default source if configured, otherwise leave the
270 * current hardware setting.
271 */
272 if (pdata.default_src == WM831X_STATUS_PRESERVE) {
273 drvdata->src = drvdata->reg_val;
274 drvdata->src &= WM831X_LED_SRC_MASK;
275 drvdata->src >>= WM831X_LED_SRC_SHIFT;
276 } else {
277 drvdata->src = pdata.default_src - 1;
278 }
279
280 drvdata->cdev.name = pdata.name;
281 drvdata->cdev.default_trigger = pdata.default_trigger;
Andrew Lunnf633a512015-08-20 12:40:45 +0200282 drvdata->cdev.brightness_set_blocking = wm831x_status_brightness_set;
Mark Brownc746b552009-09-05 14:09:21 +0100283 drvdata->cdev.blink_set = wm831x_status_blink_set;
Johan Hovoldb2015ed2014-06-25 10:08:53 -0700284 drvdata->cdev.groups = wm831x_status_groups;
Mark Brownc746b552009-09-05 14:09:21 +0100285
Amitoj Kaur Chawla8d3b6a42016-03-09 08:53:47 +0530286 ret = devm_led_classdev_register(wm831x->dev, &drvdata->cdev);
Mark Brownc746b552009-09-05 14:09:21 +0100287 if (ret < 0) {
288 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
Johan Hovoldb2015ed2014-06-25 10:08:53 -0700289 return ret;
Mark Brownc746b552009-09-05 14:09:21 +0100290 }
291
Mark Brownc746b552009-09-05 14:09:21 +0100292 return 0;
Mark Brownc746b552009-09-05 14:09:21 +0100293}
294
Mark Brownc746b552009-09-05 14:09:21 +0100295static struct platform_driver wm831x_status_driver = {
296 .driver = {
297 .name = "wm831x-status",
Mark Brownc746b552009-09-05 14:09:21 +0100298 },
299 .probe = wm831x_status_probe,
Mark Brownc746b552009-09-05 14:09:21 +0100300};
301
Axel Lin892a8842012-01-10 15:09:24 -0800302module_platform_driver(wm831x_status_driver);
Mark Brownc746b552009-09-05 14:09:21 +0100303
304MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
305MODULE_DESCRIPTION("WM831x status LED driver");
306MODULE_LICENSE("GPL");
307MODULE_ALIAS("platform:wm831x-status");