1 // SPDX-License-Identifier: GPL-2.0-only
3 * LEDs driver for Freescale MC13783/MC13892/MC34708
5 * Copyright (C) 2010 Philippe Rétornaz
7 * Based on leds-da903x:
8 * Copyright (C) 2008 Compulab, Ltd.
9 * Mike Rapoport <mike@compulab.co.il>
11 * Copyright (C) 2006-2008 Marvell International Ltd.
12 * Eric Miao <eric.miao@marvell.com>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/leds.h>
20 #include <linux/mfd/mc13xxx.h>
22 struct mc13xxx_led_devtype
{
30 struct led_classdev cdev
;
32 struct mc13xxx_leds
*leds
;
36 struct mc13xxx
*master
;
37 struct mc13xxx_led_devtype
*devtype
;
39 struct mc13xxx_led
*led
;
42 static unsigned int mc13xxx_max_brightness(int id
)
44 if (id
>= MC13783_LED_MD
&& id
<= MC13783_LED_KP
)
46 else if (id
>= MC13783_LED_R1
&& id
<= MC13783_LED_B3
)
52 static int mc13xxx_led_set(struct led_classdev
*led_cdev
,
53 enum led_brightness value
)
55 struct mc13xxx_led
*led
=
56 container_of(led_cdev
, struct mc13xxx_led
, cdev
);
57 struct mc13xxx_leds
*leds
= led
->leds
;
58 unsigned int reg
, bank
, off
, shift
;
65 shift
= 9 + (led
->id
- MC13783_LED_MD
) * 4;
76 off
= led
->id
- MC13783_LED_R1
;
79 shift
= (off
- bank
* 3) * 5 + 6;
84 off
= led
->id
- MC13892_LED_MD
;
86 shift
= 3 + (off
- reg
* 2) * 12;
91 off
= led
->id
- MC13892_LED_R
;
94 shift
= (off
- bank
* 2) * 12 + 3;
99 shift
= 3 + (led
->id
- MC34708_LED_R
) * 12;
105 return mc13xxx_reg_rmw(leds
->master
, leds
->devtype
->ledctrl_base
+ reg
,
106 mc13xxx_max_brightness(led
->id
) << shift
,
111 static struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
112 struct platform_device
*pdev
)
114 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
115 struct mc13xxx_leds_platform_data
*pdata
;
116 struct device_node
*parent
, *child
;
117 struct device
*dev
= &pdev
->dev
;
118 int i
= 0, ret
= -ENODATA
;
120 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
122 return ERR_PTR(-ENOMEM
);
124 parent
= of_get_child_by_name(dev
->parent
->of_node
, "leds");
128 ret
= of_property_read_u32_array(parent
, "led-control",
130 leds
->devtype
->num_regs
);
134 pdata
->num_leds
= of_get_child_count(parent
);
136 pdata
->led
= devm_kcalloc(dev
, pdata
->num_leds
, sizeof(*pdata
->led
),
143 for_each_child_of_node(parent
, child
) {
147 if (of_property_read_u32(child
, "reg", &tmp
))
149 pdata
->led
[i
].id
= leds
->devtype
->led_min
+ tmp
;
151 if (!of_property_read_string(child
, "label", &str
))
152 pdata
->led
[i
].name
= str
;
153 if (!of_property_read_string(child
, "linux,default-trigger",
155 pdata
->led
[i
].default_trigger
= str
;
161 ret
= i
> 0 ? 0 : -ENODATA
;
166 return ret
? ERR_PTR(ret
) : pdata
;
169 static inline struct mc13xxx_leds_platform_data __init
*mc13xxx_led_probe_dt(
170 struct platform_device
*pdev
)
172 return ERR_PTR(-ENOSYS
);
176 static int __init
mc13xxx_led_probe(struct platform_device
*pdev
)
178 struct device
*dev
= &pdev
->dev
;
179 struct mc13xxx_leds_platform_data
*pdata
= dev_get_platdata(dev
);
180 struct mc13xxx
*mcdev
= dev_get_drvdata(dev
->parent
);
181 struct mc13xxx_led_devtype
*devtype
=
182 (struct mc13xxx_led_devtype
*)pdev
->id_entry
->driver_data
;
183 struct mc13xxx_leds
*leds
;
184 int i
, id
, ret
= -ENODATA
;
187 leds
= devm_kzalloc(dev
, sizeof(*leds
), GFP_KERNEL
);
191 leds
->devtype
= devtype
;
192 leds
->master
= mcdev
;
193 platform_set_drvdata(pdev
, leds
);
195 if (dev
->parent
->of_node
) {
196 pdata
= mc13xxx_led_probe_dt(pdev
);
198 return PTR_ERR(pdata
);
202 leds
->num_leds
= pdata
->num_leds
;
204 if ((leds
->num_leds
< 1) ||
205 (leds
->num_leds
> (devtype
->led_max
- devtype
->led_min
+ 1))) {
206 dev_err(dev
, "Invalid LED count %d\n", leds
->num_leds
);
210 leds
->led
= devm_kcalloc(dev
, leds
->num_leds
, sizeof(*leds
->led
),
215 for (i
= 0; i
< devtype
->num_regs
; i
++) {
216 ret
= mc13xxx_reg_write(mcdev
, leds
->devtype
->ledctrl_base
+ i
,
217 pdata
->led_control
[i
]);
222 for (i
= 0; i
< leds
->num_leds
; i
++) {
223 const char *name
, *trig
;
227 id
= pdata
->led
[i
].id
;
228 name
= pdata
->led
[i
].name
;
229 trig
= pdata
->led
[i
].default_trigger
;
231 if ((id
> devtype
->led_max
) || (id
< devtype
->led_min
)) {
232 dev_err(dev
, "Invalid ID %i\n", id
);
236 if (init_led
& (1 << id
)) {
237 dev_warn(dev
, "LED %i already initialized\n", id
);
242 leds
->led
[i
].id
= id
;
243 leds
->led
[i
].leds
= leds
;
244 leds
->led
[i
].cdev
.name
= name
;
245 leds
->led
[i
].cdev
.default_trigger
= trig
;
246 leds
->led
[i
].cdev
.flags
= LED_CORE_SUSPENDRESUME
;
247 leds
->led
[i
].cdev
.brightness_set_blocking
= mc13xxx_led_set
;
248 leds
->led
[i
].cdev
.max_brightness
= mc13xxx_max_brightness(id
);
250 ret
= led_classdev_register(dev
->parent
, &leds
->led
[i
].cdev
);
252 dev_err(dev
, "Failed to register LED %i\n", id
);
259 led_classdev_unregister(&leds
->led
[i
].cdev
);
264 static int mc13xxx_led_remove(struct platform_device
*pdev
)
266 struct mc13xxx_leds
*leds
= platform_get_drvdata(pdev
);
269 for (i
= 0; i
< leds
->num_leds
; i
++)
270 led_classdev_unregister(&leds
->led
[i
].cdev
);
275 static const struct mc13xxx_led_devtype mc13783_led_devtype
= {
276 .led_min
= MC13783_LED_MD
,
277 .led_max
= MC13783_LED_B3
,
282 static const struct mc13xxx_led_devtype mc13892_led_devtype
= {
283 .led_min
= MC13892_LED_MD
,
284 .led_max
= MC13892_LED_B
,
289 static const struct mc13xxx_led_devtype mc34708_led_devtype
= {
290 .led_min
= MC34708_LED_R
,
291 .led_max
= MC34708_LED_G
,
296 static const struct platform_device_id mc13xxx_led_id_table
[] = {
297 { "mc13783-led", (kernel_ulong_t
)&mc13783_led_devtype
, },
298 { "mc13892-led", (kernel_ulong_t
)&mc13892_led_devtype
, },
299 { "mc34708-led", (kernel_ulong_t
)&mc34708_led_devtype
, },
302 MODULE_DEVICE_TABLE(platform
, mc13xxx_led_id_table
);
304 static struct platform_driver mc13xxx_led_driver
= {
306 .name
= "mc13xxx-led",
308 .remove
= mc13xxx_led_remove
,
309 .id_table
= mc13xxx_led_id_table
,
311 module_platform_driver_probe(mc13xxx_led_driver
, mc13xxx_led_probe
);
313 MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
314 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
315 MODULE_LICENSE("GPL");