blob: 2421cf1049915a6ef52fa55d31b30d0c2e88f406 [file] [log] [blame]
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +02001/*
Alexander Shiyana59ce652014-01-31 22:36:28 -08002 * LEDs driver for Freescale MC13783/MC13892/MC34708
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +02003 *
4 * Copyright (C) 2010 Philippe Rétornaz
5 *
6 * Based on leds-da903x:
7 * Copyright (C) 2008 Compulab, Ltd.
8 * Mike Rapoport <mike@compulab.co.il>
9 *
10 * Copyright (C) 2006-2008 Marvell International Ltd.
11 * Eric Miao <eric.miao@marvell.com>
12 *
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.
16 */
17
18#include <linux/module.h>
19#include <linux/kernel.h>
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020020#include <linux/platform_device.h>
21#include <linux/leds.h>
Alexander Shiyan25c65792014-01-31 22:37:23 -080022#include <linux/of.h>
David Janderf3ca0782011-08-24 15:28:20 +020023#include <linux/mfd/mc13xxx.h>
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020024
Alexander Shiyan9d2638132013-06-10 09:59:30 -070025struct mc13xxx_led_devtype {
26 int led_min;
27 int led_max;
28 int num_regs;
Alexander Shiyana59ce652014-01-31 22:36:28 -080029 u32 ledctrl_base;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070030};
31
32struct mc13xxx_led {
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020033 struct led_classdev cdev;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020034 int id;
Alexander Shiyana59ce652014-01-31 22:36:28 -080035 struct mc13xxx_leds *leds;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020036};
37
Alexander Shiyan9d2638132013-06-10 09:59:30 -070038struct mc13xxx_leds {
Alexander Shiyana59ce652014-01-31 22:36:28 -080039 struct mc13xxx *master;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070040 struct mc13xxx_led_devtype *devtype;
41 int num_leds;
Alexander Shiyan25c65792014-01-31 22:37:23 -080042 struct mc13xxx_led *led;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070043};
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020044
Alexander Shiyan677d13f2014-01-31 22:36:30 -080045static unsigned int mc13xxx_max_brightness(int id)
46{
47 if (id >= MC13783_LED_MD && id <= MC13783_LED_KP)
48 return 0x0f;
49 else if (id >= MC13783_LED_R1 && id <= MC13783_LED_B3)
50 return 0x1f;
51
52 return 0x3f;
53}
54
Andrew Lunn4330f2f2015-08-20 12:52:08 +020055static int mc13xxx_led_set(struct led_classdev *led_cdev,
56 enum led_brightness value)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020057{
Andrew Lunn4330f2f2015-08-20 12:52:08 +020058 struct mc13xxx_led *led =
59 container_of(led_cdev, struct mc13xxx_led, cdev);
Alexander Shiyana59ce652014-01-31 22:36:28 -080060 struct mc13xxx_leds *leds = led->leds;
Alexander Shiyan677d13f2014-01-31 22:36:30 -080061 unsigned int reg, bank, off, shift;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020062
63 switch (led->id) {
64 case MC13783_LED_MD:
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020065 case MC13783_LED_AD:
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020066 case MC13783_LED_KP:
Alexander Shiyana59ce652014-01-31 22:36:28 -080067 reg = 2;
68 shift = 9 + (led->id - MC13783_LED_MD) * 4;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020069 break;
70 case MC13783_LED_R1:
71 case MC13783_LED_G1:
72 case MC13783_LED_B1:
73 case MC13783_LED_R2:
74 case MC13783_LED_G2:
75 case MC13783_LED_B2:
76 case MC13783_LED_R3:
77 case MC13783_LED_G3:
78 case MC13783_LED_B3:
79 off = led->id - MC13783_LED_R1;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070080 bank = off / 3;
Alexander Shiyana59ce652014-01-31 22:36:28 -080081 reg = 3 + bank;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070082 shift = (off - bank * 3) * 5 + 6;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020083 break;
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070084 case MC13892_LED_MD:
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070085 case MC13892_LED_AD:
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070086 case MC13892_LED_KP:
Alexander Kurzcce35f32016-10-09 15:37:47 +020087 off = led->id - MC13892_LED_MD;
88 reg = off / 2;
89 shift = 3 + (off - reg * 2) * 12;
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070090 break;
91 case MC13892_LED_R:
92 case MC13892_LED_G:
93 case MC13892_LED_B:
94 off = led->id - MC13892_LED_R;
95 bank = off / 2;
Alexander Shiyana59ce652014-01-31 22:36:28 -080096 reg = 2 + bank;
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070097 shift = (off - bank * 2) * 12 + 3;
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070098 break;
Alexander Shiyana59ce652014-01-31 22:36:28 -080099 case MC34708_LED_R:
100 case MC34708_LED_G:
101 reg = 0;
102 shift = 3 + (led->id - MC34708_LED_R) * 12;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800103 break;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700104 default:
105 BUG();
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200106 }
107
Andrew Lunn4330f2f2015-08-20 12:52:08 +0200108 return mc13xxx_reg_rmw(leds->master, leds->devtype->ledctrl_base + reg,
Alexander Shiyan677d13f2014-01-31 22:36:30 -0800109 mc13xxx_max_brightness(led->id) << shift,
Andrew Lunn4330f2f2015-08-20 12:52:08 +0200110 value << shift);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200111}
112
Alexander Shiyan25c65792014-01-31 22:37:23 -0800113#ifdef CONFIG_OF
114static struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
115 struct platform_device *pdev)
116{
117 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
118 struct mc13xxx_leds_platform_data *pdata;
119 struct device_node *parent, *child;
120 struct device *dev = &pdev->dev;
121 int i = 0, ret = -ENODATA;
122
123 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
124 if (!pdata)
125 return ERR_PTR(-ENOMEM);
126
Geert Uytterhoeven452bc142015-01-14 05:51:58 -0800127 parent = of_get_child_by_name(dev->parent->of_node, "leds");
Alexander Shiyan25c65792014-01-31 22:37:23 -0800128 if (!parent)
129 goto out_node_put;
130
131 ret = of_property_read_u32_array(parent, "led-control",
132 pdata->led_control,
133 leds->devtype->num_regs);
134 if (ret)
135 goto out_node_put;
136
137 pdata->num_leds = of_get_child_count(parent);
138
139 pdata->led = devm_kzalloc(dev, pdata->num_leds * sizeof(*pdata->led),
140 GFP_KERNEL);
141 if (!pdata->led) {
142 ret = -ENOMEM;
143 goto out_node_put;
144 }
145
146 for_each_child_of_node(parent, child) {
147 const char *str;
148 u32 tmp;
149
150 if (of_property_read_u32(child, "reg", &tmp))
151 continue;
152 pdata->led[i].id = leds->devtype->led_min + tmp;
153
154 if (!of_property_read_string(child, "label", &str))
155 pdata->led[i].name = str;
156 if (!of_property_read_string(child, "linux,default-trigger",
157 &str))
158 pdata->led[i].default_trigger = str;
159
160 i++;
161 }
162
163 pdata->num_leds = i;
164 ret = i > 0 ? 0 : -ENODATA;
165
166out_node_put:
167 of_node_put(parent);
168
169 return ret ? ERR_PTR(ret) : pdata;
170}
171#else
172static inline struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
173 struct platform_device *pdev)
174{
175 return ERR_PTR(-ENOSYS);
176}
177#endif
178
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700179static int __init mc13xxx_led_probe(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200180{
Alexander Shiyana59ce652014-01-31 22:36:28 -0800181 struct device *dev = &pdev->dev;
182 struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(dev);
183 struct mc13xxx *mcdev = dev_get_drvdata(dev->parent);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700184 struct mc13xxx_led_devtype *devtype =
185 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
186 struct mc13xxx_leds *leds;
Alexander Shiyan25c65792014-01-31 22:37:23 -0800187 int i, id, ret = -ENODATA;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800188 u32 init_led = 0;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200189
Alexander Shiyan25c65792014-01-31 22:37:23 -0800190 leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700191 if (!leds)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200192 return -ENOMEM;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200193
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700194 leds->devtype = devtype;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800195 leds->master = mcdev;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700196 platform_set_drvdata(pdev, leds);
197
Alexander Shiyan25c65792014-01-31 22:37:23 -0800198 if (dev->parent->of_node) {
199 pdata = mc13xxx_led_probe_dt(pdev);
200 if (IS_ERR(pdata))
201 return PTR_ERR(pdata);
202 } else if (!pdata)
203 return -ENODATA;
204
205 leds->num_leds = pdata->num_leds;
206
207 if ((leds->num_leds < 1) ||
208 (leds->num_leds > (devtype->led_max - devtype->led_min + 1))) {
209 dev_err(dev, "Invalid LED count %d\n", leds->num_leds);
210 return -EINVAL;
211 }
212
213 leds->led = devm_kzalloc(dev, leds->num_leds * sizeof(*leds->led),
214 GFP_KERNEL);
215 if (!leds->led)
216 return -ENOMEM;
217
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700218 for (i = 0; i < devtype->num_regs; i++) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800219 ret = mc13xxx_reg_write(mcdev, leds->devtype->ledctrl_base + i,
220 pdata->led_control[i]);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700221 if (ret)
Alexander Shiyan3df22c02013-12-06 22:22:19 -0800222 return ret;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200223 }
224
Alexander Shiyan25c65792014-01-31 22:37:23 -0800225 for (i = 0; i < leds->num_leds; i++) {
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700226 const char *name, *trig;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200227
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700228 ret = -EINVAL;
229
230 id = pdata->led[i].id;
231 name = pdata->led[i].name;
232 trig = pdata->led[i].default_trigger;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700233
234 if ((id > devtype->led_max) || (id < devtype->led_min)) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800235 dev_err(dev, "Invalid ID %i\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700236 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200237 }
238
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700239 if (init_led & (1 << id)) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800240 dev_warn(dev, "LED %i already initialized\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700241 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200242 }
243
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700244 init_led |= 1 << id;
245 leds->led[i].id = id;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800246 leds->led[i].leds = leds;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700247 leds->led[i].cdev.name = name;
248 leds->led[i].cdev.default_trigger = trig;
Alexander Shiyan02e9e112014-01-31 22:36:29 -0800249 leds->led[i].cdev.flags = LED_CORE_SUSPENDRESUME;
Andrew Lunn4330f2f2015-08-20 12:52:08 +0200250 leds->led[i].cdev.brightness_set_blocking = mc13xxx_led_set;
Alexander Shiyan677d13f2014-01-31 22:36:30 -0800251 leds->led[i].cdev.max_brightness = mc13xxx_max_brightness(id);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200252
Alexander Shiyana59ce652014-01-31 22:36:28 -0800253 ret = led_classdev_register(dev->parent, &leds->led[i].cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200254 if (ret) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800255 dev_err(dev, "Failed to register LED %i\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700256 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200257 }
258 }
259
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700260 if (ret)
Andrew Lunn4330f2f2015-08-20 12:52:08 +0200261 while (--i >= 0)
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700262 led_classdev_unregister(&leds->led[i].cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200263
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200264 return ret;
265}
266
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700267static int mc13xxx_led_remove(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200268{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700269 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200270 int i;
271
Andrew Lunn4330f2f2015-08-20 12:52:08 +0200272 for (i = 0; i < leds->num_leds; i++)
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700273 led_classdev_unregister(&leds->led[i].cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200274
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200275 return 0;
276}
277
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700278static const struct mc13xxx_led_devtype mc13783_led_devtype = {
279 .led_min = MC13783_LED_MD,
280 .led_max = MC13783_LED_B3,
281 .num_regs = 6,
Alexander Shiyana59ce652014-01-31 22:36:28 -0800282 .ledctrl_base = 51,
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200283};
284
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700285static const struct mc13xxx_led_devtype mc13892_led_devtype = {
286 .led_min = MC13892_LED_MD,
287 .led_max = MC13892_LED_B,
288 .num_regs = 4,
Alexander Shiyana59ce652014-01-31 22:36:28 -0800289 .ledctrl_base = 51,
290};
291
292static const struct mc13xxx_led_devtype mc34708_led_devtype = {
293 .led_min = MC34708_LED_R,
294 .led_max = MC34708_LED_G,
295 .num_regs = 1,
296 .ledctrl_base = 54,
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700297};
298
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700299static const struct platform_device_id mc13xxx_led_id_table[] = {
300 { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700301 { "mc13892-led", (kernel_ulong_t)&mc13892_led_devtype, },
Alexander Shiyana59ce652014-01-31 22:36:28 -0800302 { "mc34708-led", (kernel_ulong_t)&mc34708_led_devtype, },
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700303 { }
304};
305MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200306
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700307static struct platform_driver mc13xxx_led_driver = {
308 .driver = {
309 .name = "mc13xxx-led",
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700310 },
311 .remove = mc13xxx_led_remove,
312 .id_table = mc13xxx_led_id_table,
313};
314module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
315
316MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200317MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
318MODULE_LICENSE("GPL");