]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/leds/leds-88pm860x.c
ipv6: AF_INET6 link address family
[net-next-2.6.git] / drivers / leds / leds-88pm860x.c
CommitLineData
0a2f915b
HZ
1/*
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>
5a0e3ad6 18#include <linux/slab.h>
0a2f915b
HZ
19#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)
0a2f915b
HZ
27#define LED_BLINK_MASK (0x7F)
28
29#define LED_BLINK_ON(x) ((x & 0x7) * 66 + 66)
0a2f915b
HZ
30#define LED_BLINK_ON_MIN LED_BLINK_ON(0)
31#define LED_BLINK_ON_MAX LED_BLINK_ON(0x7)
f5d59fc5 32#define LED_ON_CONTINUOUS (0x0F << 3)
0a2f915b 33#define LED_TO_ON(x) ((x - 66) / 66)
0a2f915b
HZ
34
35#define LED1_BLINK_EN (1 << 1)
36#define LED2_BLINK_EN (1 << 2)
37
0a2f915b
HZ
38struct pm860x_led {
39 struct led_classdev cdev;
40 struct i2c_client *i2c;
41 struct work_struct work;
42 struct pm860x_chip *chip;
43 struct mutex lock;
44 char name[MFD_NAME_SIZE];
45
46 int port;
47 int iset;
0a2f915b
HZ
48 unsigned char brightness;
49 unsigned char current_brightness;
50
51 int blink_data;
52 int blink_time;
53 int blink_on;
54 int blink_off;
55};
56
57/* return offset of color register */
58static inline int __led_off(int port)
59{
60 int ret = -EINVAL;
61
62 switch (port) {
63 case PM8606_LED1_RED:
64 case PM8606_LED1_GREEN:
65 case PM8606_LED1_BLUE:
66 ret = port - PM8606_LED1_RED + PM8606_RGB1B;
67 break;
68 case PM8606_LED2_RED:
69 case PM8606_LED2_GREEN:
70 case PM8606_LED2_BLUE:
71 ret = port - PM8606_LED2_RED + PM8606_RGB2B;
72 break;
73 }
74 return ret;
75}
76
77/* return offset of blink register */
78static inline int __blink_off(int port)
79{
80 int ret = -EINVAL;
81
82 switch (port) {
83 case PM8606_LED1_RED:
84 case PM8606_LED1_GREEN:
85 case PM8606_LED1_BLUE:
86 ret = PM8606_RGB1A;
f5d59fc5 87 break;
0a2f915b
HZ
88 case PM8606_LED2_RED:
89 case PM8606_LED2_GREEN:
90 case PM8606_LED2_BLUE:
91 ret = PM8606_RGB2A;
f5d59fc5 92 break;
0a2f915b
HZ
93 }
94 return ret;
95}
96
97static inline int __blink_ctl_mask(int port)
98{
99 int ret = -EINVAL;
100
101 switch (port) {
102 case PM8606_LED1_RED:
103 case PM8606_LED1_GREEN:
104 case PM8606_LED1_BLUE:
105 ret = LED1_BLINK_EN;
106 break;
107 case PM8606_LED2_RED:
108 case PM8606_LED2_GREEN:
109 case PM8606_LED2_BLUE:
110 ret = LED2_BLINK_EN;
111 break;
112 }
113 return ret;
114}
115
f5d59fc5 116static void pm860x_led_work(struct work_struct *work)
0a2f915b 117{
0a2f915b 118
f5d59fc5
HZ
119 struct pm860x_led *led;
120 struct pm860x_chip *chip;
121 int mask;
122
123 led = container_of(work, struct pm860x_led, work);
124 chip = led->chip;
0a2f915b 125 mutex_lock(&led->lock);
f5d59fc5
HZ
126 if ((led->current_brightness == 0) && led->brightness) {
127 if (led->iset) {
128 pm860x_set_bits(led->i2c, __led_off(led->port),
129 LED_CURRENT_MASK, led->iset);
0a2f915b 130 }
0a2f915b 131 mask = __blink_ctl_mask(led->port);
f5d59fc5
HZ
132 pm860x_set_bits(led->i2c, PM8606_WLED3B, mask, mask);
133 } else if (led->brightness == 0) {
134 pm860x_set_bits(led->i2c, __led_off(led->port),
135 LED_CURRENT_MASK, 0);
136 mask = __blink_ctl_mask(led->port);
137 pm860x_set_bits(led->i2c, PM8606_WLED3B, mask, 0);
0a2f915b 138 }
f5d59fc5
HZ
139 pm860x_set_bits(led->i2c, __led_off(led->port), LED_PWM_MASK,
140 led->brightness);
141 led->current_brightness = led->brightness;
142 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
143 __led_off(led->port), led->brightness);
0a2f915b 144 mutex_unlock(&led->lock);
0a2f915b
HZ
145}
146
147static void pm860x_led_set(struct led_classdev *cdev,
148 enum led_brightness value)
149{
150 struct pm860x_led *data = container_of(cdev, struct pm860x_led, cdev);
151
0a2f915b 152 data->brightness = value >> 3;
0a2f915b 153 schedule_work(&data->work);
0a2f915b
HZ
154}
155
156static int __check_device(struct pm860x_led_pdata *pdata, char *name)
157{
158 struct pm860x_led_pdata *p = pdata;
159 int ret = -EINVAL;
160
161 while (p && p->id) {
162 if ((p->id != PM8606_ID_LED) || (p->flags < 0))
163 break;
164
165 if (!strncmp(name, pm860x_led_name[p->flags],
166 MFD_NAME_SIZE)) {
167 ret = (int)p->flags;
168 break;
169 }
170 p++;
171 }
172 return ret;
173}
174
175static int pm860x_led_probe(struct platform_device *pdev)
176{
177 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
178 struct pm860x_platform_data *pm860x_pdata;
179 struct pm860x_led_pdata *pdata;
180 struct pm860x_led *data;
181 struct resource *res;
182 int ret;
183
184 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
185 if (res == NULL) {
186 dev_err(&pdev->dev, "No I/O resource!\n");
187 return -EINVAL;
188 }
189
190 if (pdev->dev.parent->platform_data) {
191 pm860x_pdata = pdev->dev.parent->platform_data;
192 pdata = pm860x_pdata->led;
98652efc 193 } else {
f5d59fc5 194 dev_err(&pdev->dev, "No platform data!\n");
98652efc
CF
195 return -EINVAL;
196 }
0a2f915b
HZ
197
198 data = kzalloc(sizeof(struct pm860x_led), GFP_KERNEL);
199 if (data == NULL)
200 return -ENOMEM;
201 strncpy(data->name, res->name, MFD_NAME_SIZE);
202 dev_set_drvdata(&pdev->dev, data);
203 data->chip = chip;
204 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
205 data->iset = pdata->iset;
206 data->port = __check_device(pdata, data->name);
98652efc
CF
207 if (data->port < 0) {
208 dev_err(&pdev->dev, "check device failed\n");
209 kfree(data);
0a2f915b 210 return -EINVAL;
98652efc 211 }
0a2f915b
HZ
212
213 data->current_brightness = 0;
214 data->cdev.name = data->name;
215 data->cdev.brightness_set = pm860x_led_set;
0a2f915b
HZ
216 mutex_init(&data->lock);
217 INIT_WORK(&data->work, pm860x_led_work);
218
219 ret = led_classdev_register(chip->dev, &data->cdev);
220 if (ret < 0) {
221 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
222 goto out;
223 }
224 return 0;
225out:
226 kfree(data);
227 return ret;
228}
229
230static int pm860x_led_remove(struct platform_device *pdev)
231{
232 struct pm860x_led *data = platform_get_drvdata(pdev);
233
234 led_classdev_unregister(&data->cdev);
235 kfree(data);
236
237 return 0;
238}
239
240static struct platform_driver pm860x_led_driver = {
241 .driver = {
242 .name = "88pm860x-led",
243 .owner = THIS_MODULE,
244 },
245 .probe = pm860x_led_probe,
246 .remove = pm860x_led_remove,
247};
248
249static int __devinit pm860x_led_init(void)
250{
251 return platform_driver_register(&pm860x_led_driver);
252}
253module_init(pm860x_led_init);
254
255static void __devexit pm860x_led_exit(void)
256{
257 platform_driver_unregister(&pm860x_led_driver);
258}
259module_exit(pm860x_led_exit);
260
261MODULE_DESCRIPTION("LED driver for Marvell PM860x");
262MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
263MODULE_LICENSE("GPL");
264MODULE_ALIAS("platform:88pm860x-led");