2 * LEDs driver for Freescale MC13783/MC13892/MC34708
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/platform_device.h>
21 #include <linux/leds.h>
23 #include <linux/workqueue.h>
24 #include <linux/mfd/mc13xxx.h>
26 struct mc13xxx_led_devtype
{
34 struct led_classdev cdev
;
35 struct work_struct work
;
36 enum led_brightness new_brightness
;
38 struct mc13xxx_leds
*leds
;
42 struct mc13xxx
*master
;
43 struct mc13xxx_led_devtype
*devtype
;
45 struct mc13xxx_led
*led
;
48 static unsigned int mc13xxx_max_brightness(int id
)
50 if (id
>= MC13783_LED_MD
&& id
<= MC13783_LED_KP
)
52 else if (id
>= MC13783_LED_R1
&& id
<= MC13783_LED_B3
)
58 static void mc13xxx_led_work(struct work_struct
*work
)
60 struct mc13xxx_led
*led
= container_of(work
, struct mc13xxx_led
, work
);
61 struct mc13xxx_leds
*leds
= led
->leds
;
62 unsigned int reg
, bank
, off
, shift
;
69 shift
= 9 + (led
->id
- MC13783_LED_MD
) * 4;
80 off
= led
->id
- MC13783_LED_R1
;
83 shift
= (off
- bank
* 3) * 5 + 6;
88 reg
= (led
->id
- MC13892_LED_MD
) / 2;
89 shift
= 3 + (led
->id
- MC13892_LED_MD
) * 12;
94 off
= led
->id
- MC13892_LED_R
;
97 shift
= (off
- bank
* 2) * 12 + 3;
102 shift
= 3 + (led
->id
- MC34708_LED_R
) * 12;
108 mc13xxx_reg_rmw(leds
->master
, leds
->devtype
->ledctrl_base
+ reg
,
109 mc13xxx_max_brightness(led
->id
) << shift
,
110 led
->new_brightness
<< shift
);
113 static void mc13xxx_led_set(struct led_classdev
*led_cdev
,
114 enum led_brightness value
)
116 struct mc13xxx_led
*led
=
117 container_of(led_cdev
, struct mc13xxx_led
, cdev
);
119 led
->new_brightness
= value
;
120 schedule_work(&led
->work
);
124 static struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
125 struct platform_device
*pdev
)
127 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
128 struct mc13xxx_leds_platform_data
*pdata
;
129 struct device_node
*parent
, *child
;
130 struct device
*dev
= &pdev
->dev
;
131 int i
= 0, ret
= -ENODATA
;
133 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
135 return ERR_PTR(-ENOMEM
);
137 of_node_get(dev
->parent
->of_node
);
139 parent
= of_find_node_by_name(dev
->parent
->of_node
, "leds");
143 ret
= of_property_read_u32_array(parent
, "led-control",
145 leds
->devtype
->num_regs
);
149 pdata
->num_leds
= of_get_child_count(parent
);
151 pdata
->led
= devm_kzalloc(dev
, pdata
->num_leds
* sizeof(*pdata
->led
),
158 for_each_child_of_node(parent
, child
) {
162 if (of_property_read_u32(child
, "reg", &tmp
))
164 pdata
->led
[i
].id
= leds
->devtype
->led_min
+ tmp
;
166 if (!of_property_read_string(child
, "label", &str
))
167 pdata
->led
[i
].name
= str
;
168 if (!of_property_read_string(child
, "linux,default-trigger",
170 pdata
->led
[i
].default_trigger
= str
;
176 ret
= i
> 0 ? 0 : -ENODATA
;
181 return ret
? ERR_PTR(ret
) : pdata
;
184 static inline struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
185 struct platform_device
*pdev
)
187 return ERR_PTR(-ENOSYS
);
191 static int __init
mc13xxx_led_probe(struct platform_device
*pdev
)
193 struct device
*dev
= &pdev
->dev
;
194 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(dev
);
195 struct mc13xxx
*mcdev
= dev_get_drvdata(dev
->parent
);
196 struct mc13xxx_led_devtype
*devtype
=
197 (struct mc13xxx_led_devtype
*)pdev
->id_entry
->driver_data
;
198 struct mc13xxx_leds
*leds
;
199 int i
, id
, ret
= -ENODATA
;
202 leds
= devm_kzalloc(dev
, sizeof(*leds
), GFP_KERNEL
);
206 leds
->devtype
= devtype
;
207 leds
->master
= mcdev
;
208 platform_set_drvdata(pdev
, leds
);
210 if (dev
->parent
->of_node
) {
211 pdata
= mc13xxx_led_probe_dt(pdev
);
213 return PTR_ERR(pdata
);
217 leds
->num_leds
= pdata
->num_leds
;
219 if ((leds
->num_leds
< 1) ||
220 (leds
->num_leds
> (devtype
->led_max
- devtype
->led_min
+ 1))) {
221 dev_err(dev
, "Invalid LED count %d\n", leds
->num_leds
);
225 leds
->led
= devm_kzalloc(dev
, leds
->num_leds
* sizeof(*leds
->led
),
230 for (i
= 0; i
< devtype
->num_regs
; i
++) {
231 ret
= mc13xxx_reg_write(mcdev
, leds
->devtype
->ledctrl_base
+ i
,
232 pdata
->led_control
[i
]);
237 for (i
= 0; i
< leds
->num_leds
; i
++) {
238 const char *name
, *trig
;
242 id
= pdata
->led
[i
].id
;
243 name
= pdata
->led
[i
].name
;
244 trig
= pdata
->led
[i
].default_trigger
;
246 if ((id
> devtype
->led_max
) || (id
< devtype
->led_min
)) {
247 dev_err(dev
, "Invalid ID %i\n", id
);
251 if (init_led
& (1 << id
)) {
252 dev_warn(dev
, "LED %i already initialized\n", id
);
257 leds
->led
[i
].id
= id
;
258 leds
->led
[i
].leds
= leds
;
259 leds
->led
[i
].cdev
.name
= name
;
260 leds
->led
[i
].cdev
.default_trigger
= trig
;
261 leds
->led
[i
].cdev
.flags
= LED_CORE_SUSPENDRESUME
;
262 leds
->led
[i
].cdev
.brightness_set
= mc13xxx_led_set
;
263 leds
->led
[i
].cdev
.max_brightness
= mc13xxx_max_brightness(id
);
265 INIT_WORK(&leds
->led
[i
].work
, mc13xxx_led_work
);
267 ret
= led_classdev_register(dev
->parent
, &leds
->led
[i
].cdev
);
269 dev_err(dev
, "Failed to register LED %i\n", id
);
276 led_classdev_unregister(&leds
->led
[i
].cdev
);
277 cancel_work_sync(&leds
->led
[i
].work
);
283 static int mc13xxx_led_remove(struct platform_device
*pdev
)
285 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
288 for (i
= 0; i
< leds
->num_leds
; i
++) {
289 led_classdev_unregister(&leds
->led
[i
].cdev
);
290 cancel_work_sync(&leds
->led
[i
].work
);
296 static const struct mc13xxx_led_devtype mc13783_led_devtype
= {
297 .led_min
= MC13783_LED_MD
,
298 .led_max
= MC13783_LED_B3
,
303 static const struct mc13xxx_led_devtype mc13892_led_devtype
= {
304 .led_min
= MC13892_LED_MD
,
305 .led_max
= MC13892_LED_B
,
310 static const struct mc13xxx_led_devtype mc34708_led_devtype
= {
311 .led_min
= MC34708_LED_R
,
312 .led_max
= MC34708_LED_G
,
317 static const struct platform_device_id mc13xxx_led_id_table
[] = {
318 { "mc13783-led", (kernel_ulong_t
)&mc13783_led_devtype
, },
319 { "mc13892-led", (kernel_ulong_t
)&mc13892_led_devtype
, },
320 { "mc34708-led", (kernel_ulong_t
)&mc34708_led_devtype
, },
323 MODULE_DEVICE_TABLE(platform
, mc13xxx_led_id_table
);
325 static struct platform_driver mc13xxx_led_driver
= {
327 .name
= "mc13xxx-led",
328 .owner
= THIS_MODULE
,
330 .remove
= mc13xxx_led_remove
,
331 .id_table
= mc13xxx_led_id_table
,
333 module_platform_driver_probe(mc13xxx_led_driver
, mc13xxx_led_probe
);
335 MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
336 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
337 MODULE_LICENSE("GPL");