blob: b7677106cff8041035be46fdc03442acd727f0cb [file] [log] [blame]
Haojian Zhuang0a2f9152010-01-06 17:10:09 -05001/*
2 * LED driver for Marvell 88PM860x
3 *
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/platform_device.h>
16#include <linux/i2c.h>
17#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050019#include <linux/workqueue.h>
20#include <linux/mfd/88pm860x.h>
21
22#define LED_PWM_SHIFT (3)
23#define LED_PWM_MASK (0x1F)
24#define LED_CURRENT_MASK (0x07 << 5)
25
26#define LED_BLINK_ON_MASK (0x07)
27#define LED_BLINK_PERIOD_MASK (0x0F << 3)
28#define LED_BLINK_MASK (0x7F)
29
30#define LED_BLINK_ON(x) ((x & 0x7) * 66 + 66)
31#define LED_BLINK_PERIOD(x) ((x & 0xF) * 530 + 930)
32#define LED_BLINK_ON_MIN LED_BLINK_ON(0)
33#define LED_BLINK_ON_MAX LED_BLINK_ON(0x7)
34#define LED_BLINK_PERIOD_MIN LED_BLINK_PERIOD(0)
35#define LED_BLINK_PERIOD_MAX LED_BLINK_PERIOD(0xE)
36#define LED_TO_ON(x) ((x - 66) / 66)
37#define LED_TO_PERIOD(x) ((x - 930) / 530)
38
39#define LED1_BLINK_EN (1 << 1)
40#define LED2_BLINK_EN (1 << 2)
41
42enum {
43 SET_BRIGHTNESS,
44 SET_BLINK,
45};
46
47struct pm860x_led {
48 struct led_classdev cdev;
49 struct i2c_client *i2c;
50 struct work_struct work;
51 struct pm860x_chip *chip;
52 struct mutex lock;
53 char name[MFD_NAME_SIZE];
54
55 int port;
56 int iset;
57 int command;
58 int offset;
59 unsigned char brightness;
60 unsigned char current_brightness;
61
62 int blink_data;
63 int blink_time;
64 int blink_on;
65 int blink_off;
66};
67
68/* return offset of color register */
69static inline int __led_off(int port)
70{
71 int ret = -EINVAL;
72
73 switch (port) {
74 case PM8606_LED1_RED:
75 case PM8606_LED1_GREEN:
76 case PM8606_LED1_BLUE:
77 ret = port - PM8606_LED1_RED + PM8606_RGB1B;
78 break;
79 case PM8606_LED2_RED:
80 case PM8606_LED2_GREEN:
81 case PM8606_LED2_BLUE:
82 ret = port - PM8606_LED2_RED + PM8606_RGB2B;
83 break;
84 }
85 return ret;
86}
87
88/* return offset of blink register */
89static inline int __blink_off(int port)
90{
91 int ret = -EINVAL;
92
93 switch (port) {
94 case PM8606_LED1_RED:
95 case PM8606_LED1_GREEN:
96 case PM8606_LED1_BLUE:
97 ret = PM8606_RGB1A;
98 case PM8606_LED2_RED:
99 case PM8606_LED2_GREEN:
100 case PM8606_LED2_BLUE:
101 ret = PM8606_RGB2A;
102 }
103 return ret;
104}
105
106static inline int __blink_ctl_mask(int port)
107{
108 int ret = -EINVAL;
109
110 switch (port) {
111 case PM8606_LED1_RED:
112 case PM8606_LED1_GREEN:
113 case PM8606_LED1_BLUE:
114 ret = LED1_BLINK_EN;
115 break;
116 case PM8606_LED2_RED:
117 case PM8606_LED2_GREEN:
118 case PM8606_LED2_BLUE:
119 ret = LED2_BLINK_EN;
120 break;
121 }
122 return ret;
123}
124
125static int __led_set(struct pm860x_led *led, int command)
126{
127 struct pm860x_chip *chip = led->chip;
128 int mask, ret;
129
130 mutex_lock(&led->lock);
131 switch (command) {
132 case SET_BRIGHTNESS:
133 if ((led->current_brightness == 0) && led->brightness) {
134 if (led->iset) {
135 ret = pm860x_set_bits(led->i2c, led->offset,
136 LED_CURRENT_MASK, led->iset);
137 if (ret < 0)
138 goto out;
139 }
140 } else if (led->brightness == 0) {
141 ret = pm860x_set_bits(led->i2c, led->offset,
142 LED_CURRENT_MASK, 0);
143 if (ret < 0)
144 goto out;
145 }
146 ret = pm860x_set_bits(led->i2c, led->offset, LED_PWM_MASK,
147 led->brightness);
148 if (ret < 0)
149 goto out;
150 led->current_brightness = led->brightness;
151 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
152 led->offset, led->brightness);
153 break;
154 case SET_BLINK:
155 ret = pm860x_set_bits(led->i2c, led->offset,
156 LED_BLINK_MASK, led->blink_data);
157 if (ret < 0)
158 goto out;
159
160 mask = __blink_ctl_mask(led->port);
161 ret = pm860x_set_bits(led->i2c, PM8606_WLED3B, mask, mask);
162 if (ret < 0)
163 goto out;
164 dev_dbg(chip->dev, "LED blink delay on:%dms, delay off:%dms\n",
165 led->blink_on, led->blink_off);
166 break;
167 }
168out:
169 mutex_unlock(&led->lock);
170 return 0;
171}
172
173static void pm860x_led_work(struct work_struct *work)
174{
175 struct pm860x_led *led;
176
177 led = container_of(work, struct pm860x_led, work);
178 __led_set(led, led->command);
179}
180
181static void pm860x_led_set(struct led_classdev *cdev,
182 enum led_brightness value)
183{
184 struct pm860x_led *data = container_of(cdev, struct pm860x_led, cdev);
185
186 data->offset = __led_off(data->port);
187 data->brightness = value >> 3;
188 data->command = SET_BRIGHTNESS;
189 schedule_work(&data->work);
190}
191
192static int pm860x_led_blink(struct led_classdev *cdev,
193 unsigned long *delay_on,
194 unsigned long *delay_off)
195{
196 struct pm860x_led *data = container_of(cdev, struct pm860x_led, cdev);
197 int period, on;
198
199 on = *delay_on;
200 if ((on < LED_BLINK_ON_MIN) || (on > LED_BLINK_ON_MAX))
201 return -EINVAL;
202
203 on = LED_TO_ON(on);
204 on = LED_BLINK_ON(on);
205
206 period = on + *delay_off;
207 if ((period < LED_BLINK_PERIOD_MIN) || (period > LED_BLINK_PERIOD_MAX))
208 return -EINVAL;
209 period = LED_TO_PERIOD(period);
210 period = LED_BLINK_PERIOD(period);
211
212 data->offset = __blink_off(data->port);
213 data->blink_on = on;
214 data->blink_off = period - data->blink_on;
215 data->blink_data = (period << 3) | data->blink_on;
216 data->command = SET_BLINK;
217 schedule_work(&data->work);
218
219 return 0;
220}
221
222static int __check_device(struct pm860x_led_pdata *pdata, char *name)
223{
224 struct pm860x_led_pdata *p = pdata;
225 int ret = -EINVAL;
226
227 while (p && p->id) {
228 if ((p->id != PM8606_ID_LED) || (p->flags < 0))
229 break;
230
231 if (!strncmp(name, pm860x_led_name[p->flags],
232 MFD_NAME_SIZE)) {
233 ret = (int)p->flags;
234 break;
235 }
236 p++;
237 }
238 return ret;
239}
240
241static int pm860x_led_probe(struct platform_device *pdev)
242{
243 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
244 struct pm860x_platform_data *pm860x_pdata;
245 struct pm860x_led_pdata *pdata;
246 struct pm860x_led *data;
247 struct resource *res;
248 int ret;
249
250 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
251 if (res == NULL) {
252 dev_err(&pdev->dev, "No I/O resource!\n");
253 return -EINVAL;
254 }
255
256 if (pdev->dev.parent->platform_data) {
257 pm860x_pdata = pdev->dev.parent->platform_data;
258 pdata = pm860x_pdata->led;
Christoph Fritz98652ef2010-04-17 13:57:17 +0200259 } else {
260 dev_err(&pdev->dev, "missing platform data\n");
261 return -EINVAL;
262 }
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500263
264 data = kzalloc(sizeof(struct pm860x_led), GFP_KERNEL);
265 if (data == NULL)
266 return -ENOMEM;
267 strncpy(data->name, res->name, MFD_NAME_SIZE);
268 dev_set_drvdata(&pdev->dev, data);
269 data->chip = chip;
270 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
271 data->iset = pdata->iset;
272 data->port = __check_device(pdata, data->name);
Christoph Fritz98652ef2010-04-17 13:57:17 +0200273 if (data->port < 0) {
274 dev_err(&pdev->dev, "check device failed\n");
275 kfree(data);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500276 return -EINVAL;
Christoph Fritz98652ef2010-04-17 13:57:17 +0200277 }
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500278
279 data->current_brightness = 0;
280 data->cdev.name = data->name;
281 data->cdev.brightness_set = pm860x_led_set;
282 data->cdev.blink_set = pm860x_led_blink;
283 mutex_init(&data->lock);
284 INIT_WORK(&data->work, pm860x_led_work);
285
286 ret = led_classdev_register(chip->dev, &data->cdev);
287 if (ret < 0) {
288 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
289 goto out;
290 }
291 return 0;
292out:
293 kfree(data);
294 return ret;
295}
296
297static int pm860x_led_remove(struct platform_device *pdev)
298{
299 struct pm860x_led *data = platform_get_drvdata(pdev);
300
301 led_classdev_unregister(&data->cdev);
302 kfree(data);
303
304 return 0;
305}
306
307static struct platform_driver pm860x_led_driver = {
308 .driver = {
309 .name = "88pm860x-led",
310 .owner = THIS_MODULE,
311 },
312 .probe = pm860x_led_probe,
313 .remove = pm860x_led_remove,
314};
315
316static int __devinit pm860x_led_init(void)
317{
318 return platform_driver_register(&pm860x_led_driver);
319}
320module_init(pm860x_led_init);
321
322static void __devexit pm860x_led_exit(void)
323{
324 platform_driver_unregister(&pm860x_led_driver);
325}
326module_exit(pm860x_led_exit);
327
328MODULE_DESCRIPTION("LED driver for Marvell PM860x");
329MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
330MODULE_LICENSE("GPL");
331MODULE_ALIAS("platform:88pm860x-led");