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/mc13xxx.h>
25 #include <linux/slab.h>
28 struct led_classdev cdev
;
29 struct work_struct work
;
30 struct mc13xxx
*master
;
31 enum led_brightness new_brightness
;
35 #define MC13783_REG_LED_CONTROL_0 51
36 #define MC13783_LED_C0_ENABLE_BIT (1 << 0)
37 #define MC13783_LED_C0_TRIODE_MD_BIT (1 << 7)
38 #define MC13783_LED_C0_TRIODE_AD_BIT (1 << 8)
39 #define MC13783_LED_C0_TRIODE_KP_BIT (1 << 9)
40 #define MC13783_LED_C0_BOOST_BIT (1 << 10)
41 #define MC13783_LED_C0_ABMODE_MASK 0x7
42 #define MC13783_LED_C0_ABMODE 11
43 #define MC13783_LED_C0_ABREF_MASK 0x3
44 #define MC13783_LED_C0_ABREF 14
46 #define MC13783_REG_LED_CONTROL_1 52
47 #define MC13783_LED_C1_TC1HALF_BIT (1 << 18)
49 #define MC13783_REG_LED_CONTROL_2 53
50 #define MC13783_LED_C2_BL_P_MASK 0xf
51 #define MC13783_LED_C2_MD_P 9
52 #define MC13783_LED_C2_AD_P 13
53 #define MC13783_LED_C2_KP_P 17
54 #define MC13783_LED_C2_BL_C_MASK 0x7
55 #define MC13783_LED_C2_MD_C 0
56 #define MC13783_LED_C2_AD_C 3
57 #define MC13783_LED_C2_KP_C 6
59 #define MC13783_REG_LED_CONTROL_3 54
60 #define MC13783_LED_C3_TC_P 6
61 #define MC13783_LED_C3_TC_P_MASK 0x1f
63 #define MC13783_REG_LED_CONTROL_4 55
64 #define MC13783_REG_LED_CONTROL_5 56
66 #define MC13783_LED_Cx_PERIOD 21
67 #define MC13783_LED_Cx_PERIOD_MASK 0x3
68 #define MC13783_LED_Cx_SLEWLIM_BIT (1 << 23)
69 #define MC13783_LED_Cx_TRIODE_TC_BIT (1 << 23)
70 #define MC13783_LED_Cx_TC_C_MASK 0x3
72 static void mc13783_led_work(struct work_struct
*work
)
74 struct mc13783_led
*led
= container_of(work
, struct mc13783_led
, work
);
82 reg
= MC13783_REG_LED_CONTROL_2
;
83 mask
= MC13783_LED_C2_BL_P_MASK
<< MC13783_LED_C2_MD_P
;
84 value
= (led
->new_brightness
>> 4) << MC13783_LED_C2_MD_P
;
87 reg
= MC13783_REG_LED_CONTROL_2
;
88 mask
= MC13783_LED_C2_BL_P_MASK
<< MC13783_LED_C2_AD_P
;
89 value
= (led
->new_brightness
>> 4) << MC13783_LED_C2_AD_P
;
92 reg
= MC13783_REG_LED_CONTROL_2
;
93 mask
= MC13783_LED_C2_BL_P_MASK
<< MC13783_LED_C2_KP_P
;
94 value
= (led
->new_brightness
>> 4) << MC13783_LED_C2_KP_P
;
105 off
= led
->id
- MC13783_LED_R1
;
107 reg
= MC13783_REG_LED_CONTROL_3
+ off
/3;
108 shift
= (off
- bank
* 3) * 5 + MC13783_LED_C3_TC_P
;
109 value
= (led
->new_brightness
>> 3) << shift
;
110 mask
= MC13783_LED_C3_TC_P_MASK
<< shift
;
114 mc13xxx_lock(led
->master
);
116 mc13xxx_reg_rmw(led
->master
, reg
, mask
, value
);
118 mc13xxx_unlock(led
->master
);
121 static void mc13783_led_set(struct led_classdev
*led_cdev
,
122 enum led_brightness value
)
124 struct mc13783_led
*led
;
126 led
= container_of(led_cdev
, struct mc13783_led
, cdev
);
127 led
->new_brightness
= value
;
128 schedule_work(&led
->work
);
131 static int mc13783_led_setup(struct mc13783_led
*led
, int max_current
)
141 shift
= MC13783_LED_C2_MD_C
;
142 mask
= MC13783_LED_C2_BL_C_MASK
;
143 value
= max_current
& MC13783_LED_C2_BL_C_MASK
;
144 reg
= MC13783_REG_LED_CONTROL_2
;
147 shift
= MC13783_LED_C2_AD_C
;
148 mask
= MC13783_LED_C2_BL_C_MASK
;
149 value
= max_current
& MC13783_LED_C2_BL_C_MASK
;
150 reg
= MC13783_REG_LED_CONTROL_2
;
153 shift
= MC13783_LED_C2_KP_C
;
154 mask
= MC13783_LED_C2_BL_C_MASK
;
155 value
= max_current
& MC13783_LED_C2_BL_C_MASK
;
156 reg
= MC13783_REG_LED_CONTROL_2
;
167 bank
= (led
->id
- MC13783_LED_R1
)/3;
168 reg
= MC13783_REG_LED_CONTROL_3
+ bank
;
169 shift
= ((led
->id
- MC13783_LED_R1
) - bank
* 3) * 2;
170 mask
= MC13783_LED_Cx_TC_C_MASK
;
171 value
= max_current
& MC13783_LED_Cx_TC_C_MASK
;
175 mc13xxx_lock(led
->master
);
177 ret
= mc13xxx_reg_rmw(led
->master
, reg
, mask
<< shift
,
180 mc13xxx_unlock(led
->master
);
184 static int mc13783_leds_prepare(struct platform_device
*pdev
)
186 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
187 struct mc13xxx
*dev
= dev_get_drvdata(pdev
->dev
.parent
);
193 if (pdata
->flags
& MC13783_LED_TC1HALF
)
194 reg
|= MC13783_LED_C1_TC1HALF_BIT
;
196 if (pdata
->flags
& MC13783_LED_SLEWLIMTC
)
197 reg
|= MC13783_LED_Cx_SLEWLIM_BIT
;
199 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_1
, reg
);
203 reg
= (pdata
->bl_period
& MC13783_LED_Cx_PERIOD_MASK
) <<
204 MC13783_LED_Cx_PERIOD
;
206 if (pdata
->flags
& MC13783_LED_SLEWLIMBL
)
207 reg
|= MC13783_LED_Cx_SLEWLIM_BIT
;
209 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_2
, reg
);
213 reg
= (pdata
->tc1_period
& MC13783_LED_Cx_PERIOD_MASK
) <<
214 MC13783_LED_Cx_PERIOD
;
216 if (pdata
->flags
& MC13783_LED_TRIODE_TC1
)
217 reg
|= MC13783_LED_Cx_TRIODE_TC_BIT
;
219 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_3
, reg
);
223 reg
= (pdata
->tc2_period
& MC13783_LED_Cx_PERIOD_MASK
) <<
224 MC13783_LED_Cx_PERIOD
;
226 if (pdata
->flags
& MC13783_LED_TRIODE_TC2
)
227 reg
|= MC13783_LED_Cx_TRIODE_TC_BIT
;
229 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_4
, reg
);
233 reg
= (pdata
->tc3_period
& MC13783_LED_Cx_PERIOD_MASK
) <<
234 MC13783_LED_Cx_PERIOD
;
236 if (pdata
->flags
& MC13783_LED_TRIODE_TC3
)
237 reg
|= MC13783_LED_Cx_TRIODE_TC_BIT
;
239 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_5
, reg
);
243 reg
= MC13783_LED_C0_ENABLE_BIT
;
244 if (pdata
->flags
& MC13783_LED_TRIODE_MD
)
245 reg
|= MC13783_LED_C0_TRIODE_MD_BIT
;
246 if (pdata
->flags
& MC13783_LED_TRIODE_AD
)
247 reg
|= MC13783_LED_C0_TRIODE_AD_BIT
;
248 if (pdata
->flags
& MC13783_LED_TRIODE_KP
)
249 reg
|= MC13783_LED_C0_TRIODE_KP_BIT
;
250 if (pdata
->flags
& MC13783_LED_BOOST_EN
)
251 reg
|= MC13783_LED_C0_BOOST_BIT
;
253 reg
|= (pdata
->abmode
& MC13783_LED_C0_ABMODE_MASK
) <<
254 MC13783_LED_C0_ABMODE
;
255 reg
|= (pdata
->abref
& MC13783_LED_C0_ABREF_MASK
) <<
256 MC13783_LED_C0_ABREF
;
258 ret
= mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_0
, reg
);
265 static int mc13783_led_probe(struct platform_device
*pdev
)
267 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
268 struct mc13xxx_led_platform_data
*led_cur
;
269 struct mc13783_led
*led
, *led_dat
;
274 dev_err(&pdev
->dev
, "missing platform data\n");
278 if (pdata
->num_leds
< 1 || pdata
->num_leds
> (MC13783_LED_MAX
+ 1)) {
279 dev_err(&pdev
->dev
, "Invalid led count %d\n", pdata
->num_leds
);
283 led
= devm_kzalloc(&pdev
->dev
, pdata
->num_leds
* sizeof(*led
),
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
);
351 static int mc13783_led_remove(struct platform_device
*pdev
)
353 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
354 struct mc13783_led
*led
= platform_get_drvdata(pdev
);
355 struct mc13xxx
*dev
= dev_get_drvdata(pdev
->dev
.parent
);
358 for (i
= 0; i
< pdata
->num_leds
; i
++) {
359 led_classdev_unregister(&led
[i
].cdev
);
360 cancel_work_sync(&led
[i
].work
);
365 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_0
, 0);
366 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_1
, 0);
367 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_2
, 0);
368 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_3
, 0);
369 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_4
, 0);
370 mc13xxx_reg_write(dev
, MC13783_REG_LED_CONTROL_5
, 0);
374 platform_set_drvdata(pdev
, NULL
);
378 static struct platform_driver mc13783_led_driver
= {
380 .name
= "mc13783-led",
381 .owner
= THIS_MODULE
,
383 .probe
= mc13783_led_probe
,
384 .remove
= mc13783_led_remove
,
387 module_platform_driver(mc13783_led_driver
);
389 MODULE_DESCRIPTION("LEDs driver for Freescale MC13783 PMIC");
390 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
391 MODULE_LICENSE("GPL");
392 MODULE_ALIAS("platform:mc13783-led");