leds: leds-mc13783: Prepare driver to support MC13892 LEDs
[linux-2.6-block.git] / drivers / leds / leds-mc13783.c
CommitLineData
7fdcef8a
PR
1/*
2 * LEDs driver for Freescale MC13783
3 *
4 * Copyright (C) 2010 Philippe Rétornaz
5 *
6 * Based on leds-da903x:
7 * Copyright (C) 2008 Compulab, Ltd.
8 * Mike Rapoport <mike@compulab.co.il>
9 *
10 * Copyright (C) 2006-2008 Marvell International Ltd.
11 * Eric Miao <eric.miao@marvell.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/leds.h>
23#include <linux/workqueue.h>
f3ca0782 24#include <linux/mfd/mc13xxx.h>
7fdcef8a 25
9d263813
AS
26#define MC13XXX_REG_LED_CONTROL(x) (51 + (x))
27
28struct mc13xxx_led_devtype {
29 int led_min;
30 int led_max;
31 int num_regs;
32};
33
34struct mc13xxx_led {
7fdcef8a
PR
35 struct led_classdev cdev;
36 struct work_struct work;
f3ca0782 37 struct mc13xxx *master;
7fdcef8a
PR
38 enum led_brightness new_brightness;
39 int id;
40};
41
9d263813
AS
42struct mc13xxx_leds {
43 struct mc13xxx_led_devtype *devtype;
44 int num_leds;
45 struct mc13xxx_led led[0];
46};
47
48static void mc13xxx_led_work(struct work_struct *work)
7fdcef8a 49{
9d263813
AS
50 struct mc13xxx_led *led = container_of(work, struct mc13xxx_led, work);
51 int reg, mask, value, bank, off, shift;
7fdcef8a
PR
52
53 switch (led->id) {
54 case MC13783_LED_MD:
9d263813
AS
55 reg = MC13XXX_REG_LED_CONTROL(2);
56 shift = 9;
57 mask = 0x0f;
58 value = led->new_brightness >> 4;
7fdcef8a
PR
59 break;
60 case MC13783_LED_AD:
9d263813
AS
61 reg = MC13XXX_REG_LED_CONTROL(2);
62 shift = 13;
63 mask = 0x0f;
64 value = led->new_brightness >> 4;
7fdcef8a
PR
65 break;
66 case MC13783_LED_KP:
9d263813
AS
67 reg = MC13XXX_REG_LED_CONTROL(2);
68 shift = 17;
69 mask = 0x0f;
70 value = led->new_brightness >> 4;
7fdcef8a
PR
71 break;
72 case MC13783_LED_R1:
73 case MC13783_LED_G1:
74 case MC13783_LED_B1:
75 case MC13783_LED_R2:
76 case MC13783_LED_G2:
77 case MC13783_LED_B2:
78 case MC13783_LED_R3:
79 case MC13783_LED_G3:
80 case MC13783_LED_B3:
81 off = led->id - MC13783_LED_R1;
9d263813
AS
82 bank = off / 3;
83 reg = MC13XXX_REG_LED_CONTROL(3) + bank;
84 shift = (off - bank * 3) * 5 + 6;
85 value = led->new_brightness >> 3;
86 mask = 0x1f;
7fdcef8a 87 break;
9d263813
AS
88 default:
89 BUG();
7fdcef8a
PR
90 }
91
f3ca0782 92 mc13xxx_lock(led->master);
9d263813 93 mc13xxx_reg_rmw(led->master, reg, mask << shift, value << shift);
f3ca0782 94 mc13xxx_unlock(led->master);
7fdcef8a
PR
95}
96
9d263813
AS
97static void mc13xxx_led_set(struct led_classdev *led_cdev,
98 enum led_brightness value)
7fdcef8a 99{
9d263813
AS
100 struct mc13xxx_led *led =
101 container_of(led_cdev, struct mc13xxx_led, cdev);
7fdcef8a 102
7fdcef8a
PR
103 led->new_brightness = value;
104 schedule_work(&led->work);
105}
106
9d263813 107static int __init mc13xxx_led_setup(struct mc13xxx_led *led, int max_current)
7fdcef8a 108{
9d263813 109 int shift, mask, reg, ret, bank;
7fdcef8a
PR
110
111 switch (led->id) {
112 case MC13783_LED_MD:
9d263813
AS
113 reg = MC13XXX_REG_LED_CONTROL(2);
114 shift = 0;
115 mask = 0x07;
7fdcef8a
PR
116 break;
117 case MC13783_LED_AD:
9d263813
AS
118 reg = MC13XXX_REG_LED_CONTROL(2);
119 shift = 3;
120 mask = 0x07;
7fdcef8a
PR
121 break;
122 case MC13783_LED_KP:
9d263813
AS
123 reg = MC13XXX_REG_LED_CONTROL(2);
124 shift = 6;
125 mask = 0x07;
7fdcef8a
PR
126 break;
127 case MC13783_LED_R1:
128 case MC13783_LED_G1:
129 case MC13783_LED_B1:
130 case MC13783_LED_R2:
131 case MC13783_LED_G2:
132 case MC13783_LED_B2:
133 case MC13783_LED_R3:
134 case MC13783_LED_G3:
135 case MC13783_LED_B3:
9d263813
AS
136 bank = (led->id - MC13783_LED_R1) / 3;
137 reg = MC13XXX_REG_LED_CONTROL(3) + bank;
7fdcef8a 138 shift = ((led->id - MC13783_LED_R1) - bank * 3) * 2;
9d263813 139 mask = 0x03;
7fdcef8a 140 break;
9d263813
AS
141 default:
142 BUG();
7fdcef8a
PR
143 }
144
f3ca0782 145 mc13xxx_lock(led->master);
f3ca0782 146 ret = mc13xxx_reg_rmw(led->master, reg, mask << shift,
9d263813 147 max_current << shift);
f3ca0782 148 mc13xxx_unlock(led->master);
7fdcef8a 149
7fdcef8a
PR
150 return ret;
151}
152
9d263813 153static int __init mc13xxx_led_probe(struct platform_device *pdev)
7fdcef8a 154{
52b7ad3a 155 struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
9d263813
AS
156 struct mc13xxx *mcdev = dev_get_drvdata(pdev->dev.parent);
157 struct mc13xxx_led_devtype *devtype =
158 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
159 struct mc13xxx_leds *leds;
160 int i, id, num_leds, ret;
161 u32 reg, init_led = 0;
162
163 if (!pdata) {
164 dev_err(&pdev->dev, "Missing platform data\n");
7fdcef8a
PR
165 return -ENODEV;
166 }
167
9d263813
AS
168 num_leds = pdata->num_leds;
169
170 if ((num_leds < 1) ||
171 (num_leds > (devtype->led_max - devtype->led_min + 1))) {
172 dev_err(&pdev->dev, "Invalid LED count %d\n", num_leds);
7fdcef8a
PR
173 return -EINVAL;
174 }
175
9d263813
AS
176 leds = devm_kzalloc(&pdev->dev, num_leds * sizeof(struct mc13xxx_led) +
177 sizeof(struct mc13xxx_leds), GFP_KERNEL);
178 if (!leds)
7fdcef8a 179 return -ENOMEM;
9d263813
AS
180
181 leds->devtype = devtype;
182 leds->num_leds = num_leds;
183 platform_set_drvdata(pdev, leds);
184
185 mc13xxx_lock(mcdev);
186 for (i = 0; i < devtype->num_regs; i++) {
187 reg = pdata->led_control[i];
188 WARN_ON(reg >= (1 << 24));
189 ret = mc13xxx_reg_write(mcdev, MC13XXX_REG_LED_CONTROL(i), reg);
190 if (ret)
191 break;
7fdcef8a 192 }
9d263813 193 mc13xxx_unlock(mcdev);
7fdcef8a 194
7fdcef8a 195 if (ret) {
9d263813 196 dev_err(&pdev->dev, "Unable to init LED driver\n");
a44cdd2c 197 return ret;
7fdcef8a
PR
198 }
199
9d263813
AS
200 for (i = 0; i < num_leds; i++) {
201 const char *name, *trig;
202 char max_current;
7fdcef8a 203
9d263813
AS
204 ret = -EINVAL;
205
206 id = pdata->led[i].id;
207 name = pdata->led[i].name;
208 trig = pdata->led[i].default_trigger;
209 max_current = pdata->led[i].max_current;
210
211 if ((id > devtype->led_max) || (id < devtype->led_min)) {
212 dev_err(&pdev->dev, "Invalid ID %i\n", id);
213 break;
7fdcef8a
PR
214 }
215
9d263813
AS
216 if (init_led & (1 << id)) {
217 dev_warn(&pdev->dev,
218 "LED %i already initialized\n", id);
219 break;
7fdcef8a
PR
220 }
221
9d263813
AS
222 init_led |= 1 << id;
223 leds->led[i].id = id;
224 leds->led[i].master = mcdev;
225 leds->led[i].cdev.name = name;
226 leds->led[i].cdev.default_trigger = trig;
227 leds->led[i].cdev.brightness_set = mc13xxx_led_set;
228 leds->led[i].cdev.brightness = LED_OFF;
7fdcef8a 229
9d263813 230 INIT_WORK(&leds->led[i].work, mc13xxx_led_work);
7fdcef8a 231
9d263813 232 ret = mc13xxx_led_setup(&leds->led[i], max_current);
7fdcef8a 233 if (ret) {
9d263813
AS
234 dev_err(&pdev->dev, "Unable to setup LED %i\n", id);
235 break;
7fdcef8a 236 }
9d263813
AS
237 ret = led_classdev_register(pdev->dev.parent,
238 &leds->led[i].cdev);
7fdcef8a 239 if (ret) {
9d263813
AS
240 dev_err(&pdev->dev, "Failed to register LED %i\n", id);
241 break;
7fdcef8a
PR
242 }
243 }
244
9d263813
AS
245 if (ret)
246 while (--i >= 0) {
247 led_classdev_unregister(&leds->led[i].cdev);
248 cancel_work_sync(&leds->led[i].work);
249 }
7fdcef8a 250
7fdcef8a
PR
251 return ret;
252}
253
9d263813 254static int mc13xxx_led_remove(struct platform_device *pdev)
7fdcef8a 255{
9d263813
AS
256 struct mc13xxx *mcdev = dev_get_drvdata(pdev->dev.parent);
257 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
7fdcef8a
PR
258 int i;
259
9d263813
AS
260 for (i = 0; i < leds->num_leds; i++) {
261 led_classdev_unregister(&leds->led[i].cdev);
262 cancel_work_sync(&leds->led[i].work);
7fdcef8a
PR
263 }
264
9d263813
AS
265 mc13xxx_lock(mcdev);
266 for (i = 0; i < leds->devtype->num_regs; i++)
267 mc13xxx_reg_write(mcdev, MC13XXX_REG_LED_CONTROL(i), 0);
268 mc13xxx_unlock(mcdev);
7fdcef8a 269
7fdcef8a
PR
270 return 0;
271}
272
9d263813
AS
273static const struct mc13xxx_led_devtype mc13783_led_devtype = {
274 .led_min = MC13783_LED_MD,
275 .led_max = MC13783_LED_B3,
276 .num_regs = 6,
277};
278
279static const struct platform_device_id mc13xxx_led_id_table[] = {
280 { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
281 { }
282};
283MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
284
285static struct platform_driver mc13xxx_led_driver = {
7fdcef8a 286 .driver = {
9d263813 287 .name = "mc13xxx-led",
7fdcef8a
PR
288 .owner = THIS_MODULE,
289 },
9d263813
AS
290 .remove = mc13xxx_led_remove,
291 .id_table = mc13xxx_led_id_table,
7fdcef8a 292};
9d263813 293module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
7fdcef8a 294
9d263813 295MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
7fdcef8a
PR
296MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
297MODULE_LICENSE("GPL");