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>
29 struct led_classdev cdev
;
30 struct work_struct work
;
31 struct mc13783
*master
;
32 enum led_brightness new_brightness
;
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
);
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
;
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
;
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
;
106 off
= led
->id
- MC13783_LED_R1
;
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
;
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
)
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
;
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
;
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
;
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
;
176 mc13783_lock(led
->master
);
178 ret
= mc13783_reg_rmw(led
->master
, reg
, mask
<< shift
,
181 mc13783_unlock(led
->master
);
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
;
275 dev_err(&pdev
->dev
, "missing platform data\n");
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
);
284 led
= kzalloc(sizeof(*led
) * pdata
->num_leds
, GFP_KERNEL
);
286 dev_err(&pdev
->dev
, "failed to alloc memory\n");
290 ret
= mc13783_leds_prepare(pdev
);
292 dev_err(&pdev
->dev
, "unable to init led driver\n");
296 for (i
= 0; i
< pdata
->num_leds
; 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
);
306 if (init_led
& (1 << led_cur
->id
)) {
307 dev_err(&pdev
->dev
, "led %d already initialized\n",
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
);
325 dev_err(&pdev
->dev
, "failed to register led %d\n",
330 ret
= mc13783_led_setup(led_dat
, led_cur
->max_current
);
332 dev_err(&pdev
->dev
, "unable to init led %d\n",
339 platform_set_drvdata(pdev
, led
);
343 for (i
= i
- 1; i
>= 0; i
--) {
344 led_classdev_unregister(&led
[i
].cdev
);
345 cancel_work_sync(&led
[i
].work
);
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
);
360 for (i
= 0; i
< pdata
->num_leds
; i
++) {
361 led_classdev_unregister(&led
[i
].cdev
);
362 cancel_work_sync(&led
[i
].work
);
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);
380 static struct platform_driver mc13783_led_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");