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/mfd/mc13xxx.h>
25 struct mc13xxx_led_devtype
{
33 struct led_classdev cdev
;
35 struct mc13xxx_leds
*leds
;
39 struct mc13xxx
*master
;
40 struct mc13xxx_led_devtype
*devtype
;
42 struct mc13xxx_led
*led
;
45 static unsigned int mc13xxx_max_brightness(int id
)
47 if (id
>= MC13783_LED_MD
&& id
<= MC13783_LED_KP
)
49 else if (id
>= MC13783_LED_R1
&& id
<= MC13783_LED_B3
)
55 static int mc13xxx_led_set(struct led_classdev
*led_cdev
,
56 enum led_brightness value
)
58 struct mc13xxx_led
*led
=
59 container_of(led_cdev
, struct mc13xxx_led
, cdev
);
60 struct mc13xxx_leds
*leds
= led
->leds
;
61 unsigned int reg
, bank
, off
, shift
;
68 shift
= 9 + (led
->id
- MC13783_LED_MD
) * 4;
79 off
= led
->id
- MC13783_LED_R1
;
82 shift
= (off
- bank
* 3) * 5 + 6;
87 reg
= (led
->id
- MC13892_LED_MD
) / 2;
88 shift
= 3 + (led
->id
- MC13892_LED_MD
) * 12;
93 off
= led
->id
- MC13892_LED_R
;
96 shift
= (off
- bank
* 2) * 12 + 3;
101 shift
= 3 + (led
->id
- MC34708_LED_R
) * 12;
107 return mc13xxx_reg_rmw(leds
->master
, leds
->devtype
->ledctrl_base
+ reg
,
108 mc13xxx_max_brightness(led
->id
) << shift
,
113 static struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
114 struct platform_device
*pdev
)
116 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
117 struct mc13xxx_leds_platform_data
*pdata
;
118 struct device_node
*parent
, *child
;
119 struct device
*dev
= &pdev
->dev
;
120 int i
= 0, ret
= -ENODATA
;
122 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
124 return ERR_PTR(-ENOMEM
);
126 parent
= of_get_child_by_name(dev
->parent
->of_node
, "leds");
130 ret
= of_property_read_u32_array(parent
, "led-control",
132 leds
->devtype
->num_regs
);
136 pdata
->num_leds
= of_get_child_count(parent
);
138 pdata
->led
= devm_kzalloc(dev
, pdata
->num_leds
* sizeof(*pdata
->led
),
145 for_each_child_of_node(parent
, child
) {
149 if (of_property_read_u32(child
, "reg", &tmp
))
151 pdata
->led
[i
].id
= leds
->devtype
->led_min
+ tmp
;
153 if (!of_property_read_string(child
, "label", &str
))
154 pdata
->led
[i
].name
= str
;
155 if (!of_property_read_string(child
, "linux,default-trigger",
157 pdata
->led
[i
].default_trigger
= str
;
163 ret
= i
> 0 ? 0 : -ENODATA
;
168 return ret
? ERR_PTR(ret
) : pdata
;
171 static inline struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
172 struct platform_device
*pdev
)
174 return ERR_PTR(-ENOSYS
);
178 static int __init
mc13xxx_led_probe(struct platform_device
*pdev
)
180 struct device
*dev
= &pdev
->dev
;
181 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(dev
);
182 struct mc13xxx
*mcdev
= dev_get_drvdata(dev
->parent
);
183 struct mc13xxx_led_devtype
*devtype
=
184 (struct mc13xxx_led_devtype
*)pdev
->id_entry
->driver_data
;
185 struct mc13xxx_leds
*leds
;
186 int i
, id
, ret
= -ENODATA
;
189 leds
= devm_kzalloc(dev
, sizeof(*leds
), GFP_KERNEL
);
193 leds
->devtype
= devtype
;
194 leds
->master
= mcdev
;
195 platform_set_drvdata(pdev
, leds
);
197 if (dev
->parent
->of_node
) {
198 pdata
= mc13xxx_led_probe_dt(pdev
);
200 return PTR_ERR(pdata
);
204 leds
->num_leds
= pdata
->num_leds
;
206 if ((leds
->num_leds
< 1) ||
207 (leds
->num_leds
> (devtype
->led_max
- devtype
->led_min
+ 1))) {
208 dev_err(dev
, "Invalid LED count %d\n", leds
->num_leds
);
212 leds
->led
= devm_kzalloc(dev
, leds
->num_leds
* sizeof(*leds
->led
),
217 for (i
= 0; i
< devtype
->num_regs
; i
++) {
218 ret
= mc13xxx_reg_write(mcdev
, leds
->devtype
->ledctrl_base
+ i
,
219 pdata
->led_control
[i
]);
224 for (i
= 0; i
< leds
->num_leds
; i
++) {
225 const char *name
, *trig
;
229 id
= pdata
->led
[i
].id
;
230 name
= pdata
->led
[i
].name
;
231 trig
= pdata
->led
[i
].default_trigger
;
233 if ((id
> devtype
->led_max
) || (id
< devtype
->led_min
)) {
234 dev_err(dev
, "Invalid ID %i\n", id
);
238 if (init_led
& (1 << id
)) {
239 dev_warn(dev
, "LED %i already initialized\n", id
);
244 leds
->led
[i
].id
= id
;
245 leds
->led
[i
].leds
= leds
;
246 leds
->led
[i
].cdev
.name
= name
;
247 leds
->led
[i
].cdev
.default_trigger
= trig
;
248 leds
->led
[i
].cdev
.flags
= LED_CORE_SUSPENDRESUME
;
249 leds
->led
[i
].cdev
.brightness_set_blocking
= mc13xxx_led_set
;
250 leds
->led
[i
].cdev
.max_brightness
= mc13xxx_max_brightness(id
);
252 ret
= led_classdev_register(dev
->parent
, &leds
->led
[i
].cdev
);
254 dev_err(dev
, "Failed to register LED %i\n", id
);
261 led_classdev_unregister(&leds
->led
[i
].cdev
);
266 static int mc13xxx_led_remove(struct platform_device
*pdev
)
268 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
271 for (i
= 0; i
< leds
->num_leds
; i
++)
272 led_classdev_unregister(&leds
->led
[i
].cdev
);
277 static const struct mc13xxx_led_devtype mc13783_led_devtype
= {
278 .led_min
= MC13783_LED_MD
,
279 .led_max
= MC13783_LED_B3
,
284 static const struct mc13xxx_led_devtype mc13892_led_devtype
= {
285 .led_min
= MC13892_LED_MD
,
286 .led_max
= MC13892_LED_B
,
291 static const struct mc13xxx_led_devtype mc34708_led_devtype
= {
292 .led_min
= MC34708_LED_R
,
293 .led_max
= MC34708_LED_G
,
298 static const struct platform_device_id mc13xxx_led_id_table
[] = {
299 { "mc13783-led", (kernel_ulong_t
)&mc13783_led_devtype
, },
300 { "mc13892-led", (kernel_ulong_t
)&mc13892_led_devtype
, },
301 { "mc34708-led", (kernel_ulong_t
)&mc34708_led_devtype
, },
304 MODULE_DEVICE_TABLE(platform
, mc13xxx_led_id_table
);
306 static struct platform_driver mc13xxx_led_driver
= {
308 .name
= "mc13xxx-led",
310 .remove
= mc13xxx_led_remove
,
311 .id_table
= mc13xxx_led_id_table
,
313 module_platform_driver_probe(mc13xxx_led_driver
, mc13xxx_led_probe
);
315 MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
316 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
317 MODULE_LICENSE("GPL");