mach-ux500: update the DB8500 register file
[linux-2.6/libata-dev.git] / drivers / leds / leds-mc13783.c
blob126ca7955f6e16f4f43cd72ec387d4a8a0fd3637
1 /*
2 * LEDs driver for Freescale MC13783
4 * Copyright (C) 2010 Philippe Rétornaz
6 * Based on leds-da903x:
7 * Copyright (C) 2008 Compulab, Ltd.
8 * Mike Rapoport <mike@compulab.co.il>
10 * Copyright (C) 2006-2008 Marvell International Ltd.
11 * Eric Miao <eric.miao@marvell.com>
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.
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>
24 #include <linux/mfd/mc13783.h>
25 #include <linux/mfd/core.h>
26 #include <linux/slab.h>
28 struct mc13783_led {
29 struct led_classdev cdev;
30 struct work_struct work;
31 struct mc13783 *master;
32 enum led_brightness new_brightness;
33 int id;
36 #define MC13783_REG_LED_CONTROL_0 51
37 #define MC13783_LED_C0_ENABLE_BIT (1 << 0)
38 #define MC13783_LED_C0_TRIODE_MD_BIT (1 << 7)
39 #define MC13783_LED_C0_TRIODE_AD_BIT (1 << 8)
40 #define MC13783_LED_C0_TRIODE_KP_BIT (1 << 9)
41 #define MC13783_LED_C0_BOOST_BIT (1 << 10)
42 #define MC13783_LED_C0_ABMODE_MASK 0x7
43 #define MC13783_LED_C0_ABMODE 11
44 #define MC13783_LED_C0_ABREF_MASK 0x3
45 #define MC13783_LED_C0_ABREF 14
47 #define MC13783_REG_LED_CONTROL_1 52
48 #define MC13783_LED_C1_TC1HALF_BIT (1 << 18)
50 #define MC13783_REG_LED_CONTROL_2 53
51 #define MC13783_LED_C2_BL_P_MASK 0xf
52 #define MC13783_LED_C2_MD_P 9
53 #define MC13783_LED_C2_AD_P 13
54 #define MC13783_LED_C2_KP_P 17
55 #define MC13783_LED_C2_BL_C_MASK 0x7
56 #define MC13783_LED_C2_MD_C 0
57 #define MC13783_LED_C2_AD_C 3
58 #define MC13783_LED_C2_KP_C 6
60 #define MC13783_REG_LED_CONTROL_3 54
61 #define MC13783_LED_C3_TC_P 6
62 #define MC13783_LED_C3_TC_P_MASK 0x1f
64 #define MC13783_REG_LED_CONTROL_4 55
65 #define MC13783_REG_LED_CONTROL_5 56
67 #define MC13783_LED_Cx_PERIOD 21
68 #define MC13783_LED_Cx_PERIOD_MASK 0x3
69 #define MC13783_LED_Cx_SLEWLIM_BIT (1 << 23)
70 #define MC13783_LED_Cx_TRIODE_TC_BIT (1 << 23)
71 #define MC13783_LED_Cx_TC_C_MASK 0x3
73 static void mc13783_led_work(struct work_struct *work)
75 struct mc13783_led *led = container_of(work, struct mc13783_led, work);
76 int reg = 0;
77 int mask = 0;
78 int value = 0;
79 int bank, off, shift;
81 switch (led->id) {
82 case MC13783_LED_MD:
83 reg = MC13783_REG_LED_CONTROL_2;
84 mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_MD_P;
85 value = (led->new_brightness >> 4) << MC13783_LED_C2_MD_P;
86 break;
87 case MC13783_LED_AD:
88 reg = MC13783_REG_LED_CONTROL_2;
89 mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_AD_P;
90 value = (led->new_brightness >> 4) << MC13783_LED_C2_AD_P;
91 break;
92 case MC13783_LED_KP:
93 reg = MC13783_REG_LED_CONTROL_2;
94 mask = MC13783_LED_C2_BL_P_MASK << MC13783_LED_C2_KP_P;
95 value = (led->new_brightness >> 4) << MC13783_LED_C2_KP_P;
96 break;
97 case MC13783_LED_R1:
98 case MC13783_LED_G1:
99 case MC13783_LED_B1:
100 case MC13783_LED_R2:
101 case MC13783_LED_G2:
102 case MC13783_LED_B2:
103 case MC13783_LED_R3:
104 case MC13783_LED_G3:
105 case MC13783_LED_B3:
106 off = led->id - MC13783_LED_R1;
107 bank = off/3;
108 reg = MC13783_REG_LED_CONTROL_3 + off/3;
109 shift = (off - bank * 3) * 5 + MC13783_LED_C3_TC_P;
110 value = (led->new_brightness >> 3) << shift;
111 mask = MC13783_LED_C3_TC_P_MASK << shift;
112 break;
115 mc13783_lock(led->master);
117 mc13783_reg_rmw(led->master, reg, mask, value);
119 mc13783_unlock(led->master);
122 static void mc13783_led_set(struct led_classdev *led_cdev,
123 enum led_brightness value)
125 struct mc13783_led *led;
127 led = container_of(led_cdev, struct mc13783_led, cdev);
128 led->new_brightness = value;
129 schedule_work(&led->work);
132 static int __devinit mc13783_led_setup(struct mc13783_led *led, int max_current)
134 int shift = 0;
135 int mask = 0;
136 int value = 0;
137 int reg = 0;
138 int ret, bank;
140 switch (led->id) {
141 case MC13783_LED_MD:
142 shift = MC13783_LED_C2_MD_C;
143 mask = MC13783_LED_C2_BL_C_MASK;
144 value = max_current & MC13783_LED_C2_BL_C_MASK;
145 reg = MC13783_REG_LED_CONTROL_2;
146 break;
147 case MC13783_LED_AD:
148 shift = MC13783_LED_C2_AD_C;
149 mask = MC13783_LED_C2_BL_C_MASK;
150 value = max_current & MC13783_LED_C2_BL_C_MASK;
151 reg = MC13783_REG_LED_CONTROL_2;
152 break;
153 case MC13783_LED_KP:
154 shift = MC13783_LED_C2_KP_C;
155 mask = MC13783_LED_C2_BL_C_MASK;
156 value = max_current & MC13783_LED_C2_BL_C_MASK;
157 reg = MC13783_REG_LED_CONTROL_2;
158 break;
159 case MC13783_LED_R1:
160 case MC13783_LED_G1:
161 case MC13783_LED_B1:
162 case MC13783_LED_R2:
163 case MC13783_LED_G2:
164 case MC13783_LED_B2:
165 case MC13783_LED_R3:
166 case MC13783_LED_G3:
167 case MC13783_LED_B3:
168 bank = (led->id - MC13783_LED_R1)/3;
169 reg = MC13783_REG_LED_CONTROL_3 + bank;
170 shift = ((led->id - MC13783_LED_R1) - bank * 3) * 2;
171 mask = MC13783_LED_Cx_TC_C_MASK;
172 value = max_current & MC13783_LED_Cx_TC_C_MASK;
173 break;
176 mc13783_lock(led->master);
178 ret = mc13783_reg_rmw(led->master, reg, mask << shift,
179 value << shift);
181 mc13783_unlock(led->master);
182 return ret;
185 static int __devinit mc13783_leds_prepare(struct platform_device *pdev)
187 struct mc13783_leds_platform_data *pdata = mfd_get_data(pdev);
188 struct mc13783 *dev = dev_get_drvdata(pdev->dev.parent);
189 int ret = 0;
190 int reg = 0;
192 mc13783_lock(dev);
194 if (pdata->flags & MC13783_LED_TC1HALF)
195 reg |= MC13783_LED_C1_TC1HALF_BIT;
197 if (pdata->flags & MC13783_LED_SLEWLIMTC)
198 reg |= MC13783_LED_Cx_SLEWLIM_BIT;
200 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_1, reg);
201 if (ret)
202 goto out;
204 reg = (pdata->bl_period & MC13783_LED_Cx_PERIOD_MASK) <<
205 MC13783_LED_Cx_PERIOD;
207 if (pdata->flags & MC13783_LED_SLEWLIMBL)
208 reg |= MC13783_LED_Cx_SLEWLIM_BIT;
210 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_2, reg);
211 if (ret)
212 goto out;
214 reg = (pdata->tc1_period & MC13783_LED_Cx_PERIOD_MASK) <<
215 MC13783_LED_Cx_PERIOD;
217 if (pdata->flags & MC13783_LED_TRIODE_TC1)
218 reg |= MC13783_LED_Cx_TRIODE_TC_BIT;
220 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_3, reg);
221 if (ret)
222 goto out;
224 reg = (pdata->tc2_period & MC13783_LED_Cx_PERIOD_MASK) <<
225 MC13783_LED_Cx_PERIOD;
227 if (pdata->flags & MC13783_LED_TRIODE_TC2)
228 reg |= MC13783_LED_Cx_TRIODE_TC_BIT;
230 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_4, reg);
231 if (ret)
232 goto out;
234 reg = (pdata->tc3_period & MC13783_LED_Cx_PERIOD_MASK) <<
235 MC13783_LED_Cx_PERIOD;
237 if (pdata->flags & MC13783_LED_TRIODE_TC3)
238 reg |= MC13783_LED_Cx_TRIODE_TC_BIT;
240 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_5, reg);
241 if (ret)
242 goto out;
244 reg = MC13783_LED_C0_ENABLE_BIT;
245 if (pdata->flags & MC13783_LED_TRIODE_MD)
246 reg |= MC13783_LED_C0_TRIODE_MD_BIT;
247 if (pdata->flags & MC13783_LED_TRIODE_AD)
248 reg |= MC13783_LED_C0_TRIODE_AD_BIT;
249 if (pdata->flags & MC13783_LED_TRIODE_KP)
250 reg |= MC13783_LED_C0_TRIODE_KP_BIT;
251 if (pdata->flags & MC13783_LED_BOOST_EN)
252 reg |= MC13783_LED_C0_BOOST_BIT;
254 reg |= (pdata->abmode & MC13783_LED_C0_ABMODE_MASK) <<
255 MC13783_LED_C0_ABMODE;
256 reg |= (pdata->abref & MC13783_LED_C0_ABREF_MASK) <<
257 MC13783_LED_C0_ABREF;
259 ret = mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_0, reg);
261 out:
262 mc13783_unlock(dev);
263 return ret;
266 static int __devinit mc13783_led_probe(struct platform_device *pdev)
268 struct mc13783_leds_platform_data *pdata = mfd_get_data(pdev);
269 struct mc13783_led_platform_data *led_cur;
270 struct mc13783_led *led, *led_dat;
271 int ret, i;
272 int init_led = 0;
274 if (pdata == NULL) {
275 dev_err(&pdev->dev, "missing platform data\n");
276 return -ENODEV;
279 if (pdata->num_leds < 1 || pdata->num_leds > MC13783_LED_MAX) {
280 dev_err(&pdev->dev, "Invalid led count %d\n", pdata->num_leds);
281 return -EINVAL;
284 led = kzalloc(sizeof(*led) * pdata->num_leds, GFP_KERNEL);
285 if (led == NULL) {
286 dev_err(&pdev->dev, "failed to alloc memory\n");
287 return -ENOMEM;
290 ret = mc13783_leds_prepare(pdev);
291 if (ret) {
292 dev_err(&pdev->dev, "unable to init led driver\n");
293 goto err_free;
296 for (i = 0; i < pdata->num_leds; i++) {
297 led_dat = &led[i];
298 led_cur = &pdata->led[i];
300 if (led_cur->id > MC13783_LED_MAX || led_cur->id < 0) {
301 dev_err(&pdev->dev, "invalid id %d\n", led_cur->id);
302 ret = -EINVAL;
303 goto err_register;
306 if (init_led & (1 << led_cur->id)) {
307 dev_err(&pdev->dev, "led %d already initialized\n",
308 led_cur->id);
309 ret = -EINVAL;
310 goto err_register;
313 init_led |= 1 << led_cur->id;
314 led_dat->cdev.name = led_cur->name;
315 led_dat->cdev.default_trigger = led_cur->default_trigger;
316 led_dat->cdev.brightness_set = mc13783_led_set;
317 led_dat->cdev.brightness = LED_OFF;
318 led_dat->id = led_cur->id;
319 led_dat->master = dev_get_drvdata(pdev->dev.parent);
321 INIT_WORK(&led_dat->work, mc13783_led_work);
323 ret = led_classdev_register(pdev->dev.parent, &led_dat->cdev);
324 if (ret) {
325 dev_err(&pdev->dev, "failed to register led %d\n",
326 led_dat->id);
327 goto err_register;
330 ret = mc13783_led_setup(led_dat, led_cur->max_current);
331 if (ret) {
332 dev_err(&pdev->dev, "unable to init led %d\n",
333 led_dat->id);
334 i++;
335 goto err_register;
339 platform_set_drvdata(pdev, led);
340 return 0;
342 err_register:
343 for (i = i - 1; i >= 0; i--) {
344 led_classdev_unregister(&led[i].cdev);
345 cancel_work_sync(&led[i].work);
348 err_free:
349 kfree(led);
350 return ret;
353 static int __devexit mc13783_led_remove(struct platform_device *pdev)
355 struct mc13783_leds_platform_data *pdata = mfd_get_data(pdev);
356 struct mc13783_led *led = platform_get_drvdata(pdev);
357 struct mc13783 *dev = dev_get_drvdata(pdev->dev.parent);
358 int i;
360 for (i = 0; i < pdata->num_leds; i++) {
361 led_classdev_unregister(&led[i].cdev);
362 cancel_work_sync(&led[i].work);
365 mc13783_lock(dev);
367 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_0, 0);
368 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_1, 0);
369 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_2, 0);
370 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_3, 0);
371 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_4, 0);
372 mc13783_reg_write(dev, MC13783_REG_LED_CONTROL_5, 0);
374 mc13783_unlock(dev);
376 kfree(led);
377 return 0;
380 static struct platform_driver mc13783_led_driver = {
381 .driver = {
382 .name = "mc13783-led",
383 .owner = THIS_MODULE,
385 .probe = mc13783_led_probe,
386 .remove = __devexit_p(mc13783_led_remove),
389 static int __init mc13783_led_init(void)
391 return platform_driver_register(&mc13783_led_driver);
393 module_init(mc13783_led_init);
395 static void __exit mc13783_led_exit(void)
397 platform_driver_unregister(&mc13783_led_driver);
399 module_exit(mc13783_led_exit);
401 MODULE_DESCRIPTION("LEDs driver for Freescale MC13783 PMIC");
402 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
403 MODULE_LICENSE("GPL");
404 MODULE_ALIAS("platform:mc13783-led");