1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * LEDs driver for Freescale MC13783/MC13892/MC34708
4 *
5 * Copyright (C) 2010 Philippe Rétornaz
6 *
7 * Based on leds-da903x:
8 * Copyright (C) 2008 Compulab, Ltd.
9 * Mike Rapoport <mike@compulab.co.il>
10 *
11 * Copyright (C) 2006-2008 Marvell International Ltd.
12 * Eric Miao <eric.miao@marvell.com>
13 */
14
15 #include <linux/cleanup.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/leds.h>
20 #include <linux/of.h>
21 #include <linux/mfd/mc13xxx.h>
22
23 struct mc13xxx_led_devtype {
24 int led_min;
25 int led_max;
26 int num_regs;
27 u32 ledctrl_base;
28 };
29
30 struct mc13xxx_led {
31 struct led_classdev cdev;
32 int id;
33 struct mc13xxx_leds *leds;
34 };
35
36 struct mc13xxx_leds {
37 struct mc13xxx *master;
38 struct mc13xxx_led_devtype *devtype;
39 int num_leds;
40 struct mc13xxx_led *led;
41 };
42
mc13xxx_max_brightness(int id)43 static unsigned int mc13xxx_max_brightness(int id)
44 {
45 if (id >= MC13783_LED_MD && id <= MC13783_LED_KP)
46 return 0x0f;
47 else if (id >= MC13783_LED_R1 && id <= MC13783_LED_B3)
48 return 0x1f;
49
50 return 0x3f;
51 }
52
mc13xxx_led_set(struct led_classdev * led_cdev,enum led_brightness value)53 static int mc13xxx_led_set(struct led_classdev *led_cdev,
54 enum led_brightness value)
55 {
56 struct mc13xxx_led *led =
57 container_of(led_cdev, struct mc13xxx_led, cdev);
58 struct mc13xxx_leds *leds = led->leds;
59 unsigned int reg, bank, off, shift;
60
61 switch (led->id) {
62 case MC13783_LED_MD:
63 case MC13783_LED_AD:
64 case MC13783_LED_KP:
65 reg = 2;
66 shift = 9 + (led->id - MC13783_LED_MD) * 4;
67 break;
68 case MC13783_LED_R1:
69 case MC13783_LED_G1:
70 case MC13783_LED_B1:
71 case MC13783_LED_R2:
72 case MC13783_LED_G2:
73 case MC13783_LED_B2:
74 case MC13783_LED_R3:
75 case MC13783_LED_G3:
76 case MC13783_LED_B3:
77 off = led->id - MC13783_LED_R1;
78 bank = off / 3;
79 reg = 3 + bank;
80 shift = (off - bank * 3) * 5 + 6;
81 break;
82 case MC13892_LED_MD:
83 case MC13892_LED_AD:
84 case MC13892_LED_KP:
85 off = led->id - MC13892_LED_MD;
86 reg = off / 2;
87 shift = 3 + (off - reg * 2) * 12;
88 break;
89 case MC13892_LED_R:
90 case MC13892_LED_G:
91 case MC13892_LED_B:
92 off = led->id - MC13892_LED_R;
93 bank = off / 2;
94 reg = 2 + bank;
95 shift = (off - bank * 2) * 12 + 3;
96 break;
97 case MC34708_LED_R:
98 case MC34708_LED_G:
99 reg = 0;
100 shift = 3 + (led->id - MC34708_LED_R) * 12;
101 break;
102 default:
103 BUG();
104 }
105
106 return mc13xxx_reg_rmw(leds->master, leds->devtype->ledctrl_base + reg,
107 mc13xxx_max_brightness(led->id) << shift,
108 value << shift);
109 }
110
111 #ifdef CONFIG_OF
mc13xxx_led_probe_dt(struct platform_device * pdev)112 static struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
113 struct platform_device *pdev)
114 {
115 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
116 struct mc13xxx_leds_platform_data *pdata;
117 struct device_node *child;
118 struct device *dev = &pdev->dev;
119 int i = 0, ret = -ENODATA;
120
121 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
122 if (!pdata)
123 return ERR_PTR(-ENOMEM);
124
125 struct device_node *parent __free(device_node) =
126 of_get_child_by_name(dev_of_node(dev->parent), "leds");
127 if (!parent)
128 return ERR_PTR(-ENODATA);
129
130 ret = of_property_read_u32_array(parent, "led-control",
131 pdata->led_control,
132 leds->devtype->num_regs);
133 if (ret)
134 return ERR_PTR(ret);
135
136 pdata->num_leds = of_get_available_child_count(parent);
137
138 pdata->led = devm_kcalloc(dev, pdata->num_leds, sizeof(*pdata->led),
139 GFP_KERNEL);
140 if (!pdata->led)
141 return ERR_PTR(-ENOMEM);
142
143 for_each_available_child_of_node(parent, child) {
144 const char *str;
145 u32 tmp;
146
147 if (of_property_read_u32(child, "reg", &tmp))
148 continue;
149 pdata->led[i].id = leds->devtype->led_min + tmp;
150
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",
154 &str))
155 pdata->led[i].default_trigger = str;
156
157 i++;
158 }
159
160 pdata->num_leds = i;
161 if (i <= 0)
162 return ERR_PTR(-ENODATA);
163
164 return pdata;
165 }
166 #else
mc13xxx_led_probe_dt(struct platform_device * pdev)167 static inline struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
168 struct platform_device *pdev)
169 {
170 return ERR_PTR(-ENOSYS);
171 }
172 #endif
173
mc13xxx_led_probe(struct platform_device * pdev)174 static int __init mc13xxx_led_probe(struct platform_device *pdev)
175 {
176 struct device *dev = &pdev->dev;
177 struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(dev);
178 struct mc13xxx *mcdev = dev_get_drvdata(dev->parent);
179 struct mc13xxx_led_devtype *devtype =
180 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
181 struct mc13xxx_leds *leds;
182 int i, id, ret = -ENODATA;
183 u32 init_led = 0;
184
185 leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
186 if (!leds)
187 return -ENOMEM;
188
189 leds->devtype = devtype;
190 leds->master = mcdev;
191 platform_set_drvdata(pdev, leds);
192
193 if (dev_of_node(dev->parent)) {
194 pdata = mc13xxx_led_probe_dt(pdev);
195 if (IS_ERR(pdata))
196 return PTR_ERR(pdata);
197 } else if (!pdata)
198 return -ENODATA;
199
200 leds->num_leds = pdata->num_leds;
201
202 if ((leds->num_leds < 1) ||
203 (leds->num_leds > (devtype->led_max - devtype->led_min + 1))) {
204 dev_err(dev, "Invalid LED count %d\n", leds->num_leds);
205 return -EINVAL;
206 }
207
208 leds->led = devm_kcalloc(dev, leds->num_leds, sizeof(*leds->led),
209 GFP_KERNEL);
210 if (!leds->led)
211 return -ENOMEM;
212
213 for (i = 0; i < devtype->num_regs; i++) {
214 ret = mc13xxx_reg_write(mcdev, leds->devtype->ledctrl_base + i,
215 pdata->led_control[i]);
216 if (ret)
217 return ret;
218 }
219
220 for (i = 0; i < leds->num_leds; i++) {
221 const char *name, *trig;
222
223 ret = -EINVAL;
224
225 id = pdata->led[i].id;
226 name = pdata->led[i].name;
227 trig = pdata->led[i].default_trigger;
228
229 if ((id > devtype->led_max) || (id < devtype->led_min)) {
230 dev_err(dev, "Invalid ID %i\n", id);
231 break;
232 }
233
234 if (init_led & (1 << id)) {
235 dev_warn(dev, "LED %i already initialized\n", id);
236 break;
237 }
238
239 init_led |= 1 << id;
240 leds->led[i].id = id;
241 leds->led[i].leds = leds;
242 leds->led[i].cdev.name = name;
243 leds->led[i].cdev.default_trigger = trig;
244 leds->led[i].cdev.flags = LED_CORE_SUSPENDRESUME;
245 leds->led[i].cdev.brightness_set_blocking = mc13xxx_led_set;
246 leds->led[i].cdev.max_brightness = mc13xxx_max_brightness(id);
247
248 ret = led_classdev_register(dev->parent, &leds->led[i].cdev);
249 if (ret) {
250 dev_err(dev, "Failed to register LED %i\n", id);
251 break;
252 }
253 }
254
255 if (ret)
256 while (--i >= 0)
257 led_classdev_unregister(&leds->led[i].cdev);
258
259 return ret;
260 }
261
mc13xxx_led_remove(struct platform_device * pdev)262 static void mc13xxx_led_remove(struct platform_device *pdev)
263 {
264 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
265 int i;
266
267 for (i = 0; i < leds->num_leds; i++)
268 led_classdev_unregister(&leds->led[i].cdev);
269 }
270
271 static const struct mc13xxx_led_devtype mc13783_led_devtype = {
272 .led_min = MC13783_LED_MD,
273 .led_max = MC13783_LED_B3,
274 .num_regs = 6,
275 .ledctrl_base = 51,
276 };
277
278 static const struct mc13xxx_led_devtype mc13892_led_devtype = {
279 .led_min = MC13892_LED_MD,
280 .led_max = MC13892_LED_B,
281 .num_regs = 4,
282 .ledctrl_base = 51,
283 };
284
285 static const struct mc13xxx_led_devtype mc34708_led_devtype = {
286 .led_min = MC34708_LED_R,
287 .led_max = MC34708_LED_G,
288 .num_regs = 1,
289 .ledctrl_base = 54,
290 };
291
292 static const struct platform_device_id mc13xxx_led_id_table[] = {
293 { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
294 { "mc13892-led", (kernel_ulong_t)&mc13892_led_devtype, },
295 { "mc34708-led", (kernel_ulong_t)&mc34708_led_devtype, },
296 { }
297 };
298 MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
299
300 static struct platform_driver mc13xxx_led_driver = {
301 .driver = {
302 .name = "mc13xxx-led",
303 },
304 .remove_new = mc13xxx_led_remove,
305 .id_table = mc13xxx_led_id_table,
306 };
307 module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
308
309 MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
310 MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
311 MODULE_LICENSE("GPL");
312