blob: e2b847fe22a1c9343d1bb37020000b470ccd7ff7 [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>
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020023#include <linux/workqueue.h>
David Janderf3ca0782011-08-24 15:28:20 +020024#include <linux/mfd/mc13xxx.h>
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020025
Alexander Shiyan9d2638132013-06-10 09:59:30 -070026struct mc13xxx_led_devtype {
27 int led_min;
28 int led_max;
29 int num_regs;
Alexander Shiyana59ce652014-01-31 22:36:28 -080030 u32 ledctrl_base;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070031};
32
33struct mc13xxx_led {
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020034 struct led_classdev cdev;
35 struct work_struct work;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020036 enum led_brightness new_brightness;
37 int id;
Alexander Shiyana59ce652014-01-31 22:36:28 -080038 struct mc13xxx_leds *leds;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020039};
40
Alexander Shiyan9d2638132013-06-10 09:59:30 -070041struct mc13xxx_leds {
Alexander Shiyana59ce652014-01-31 22:36:28 -080042 struct mc13xxx *master;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070043 struct mc13xxx_led_devtype *devtype;
44 int num_leds;
Alexander Shiyan25c65792014-01-31 22:37:23 -080045 struct mc13xxx_led *led;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070046};
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020047
Alexander Shiyan677d13f2014-01-31 22:36:30 -080048static unsigned int mc13xxx_max_brightness(int id)
49{
50 if (id >= MC13783_LED_MD && id <= MC13783_LED_KP)
51 return 0x0f;
52 else if (id >= MC13783_LED_R1 && id <= MC13783_LED_B3)
53 return 0x1f;
54
55 return 0x3f;
56}
57
Alexander Shiyan9d2638132013-06-10 09:59:30 -070058static void mc13xxx_led_work(struct work_struct *work)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020059{
Alexander Shiyan9d2638132013-06-10 09:59:30 -070060 struct mc13xxx_led *led = container_of(work, struct mc13xxx_led, work);
Alexander Shiyana59ce652014-01-31 22:36:28 -080061 struct mc13xxx_leds *leds = led->leds;
Alexander Shiyan677d13f2014-01-31 22:36:30 -080062 unsigned int reg, bank, off, shift;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020063
64 switch (led->id) {
65 case MC13783_LED_MD:
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020066 case MC13783_LED_AD:
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020067 case MC13783_LED_KP:
Alexander Shiyana59ce652014-01-31 22:36:28 -080068 reg = 2;
69 shift = 9 + (led->id - MC13783_LED_MD) * 4;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020070 break;
71 case MC13783_LED_R1:
72 case MC13783_LED_G1:
73 case MC13783_LED_B1:
74 case MC13783_LED_R2:
75 case MC13783_LED_G2:
76 case MC13783_LED_B2:
77 case MC13783_LED_R3:
78 case MC13783_LED_G3:
79 case MC13783_LED_B3:
80 off = led->id - MC13783_LED_R1;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070081 bank = off / 3;
Alexander Shiyana59ce652014-01-31 22:36:28 -080082 reg = 3 + bank;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070083 shift = (off - bank * 3) * 5 + 6;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020084 break;
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070085 case MC13892_LED_MD:
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070086 case MC13892_LED_AD:
Alexander Shiyanae6cdb02013-06-10 09:59:31 -070087 case MC13892_LED_KP:
Alexander Shiyana59ce652014-01-31 22:36:28 -080088 reg = (led->id - MC13892_LED_MD) / 2;
89 shift = 3 + (led->id - MC13892_LED_MD) * 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
Alexander Shiyana59ce652014-01-31 22:36:28 -0800108 mc13xxx_reg_rmw(leds->master, leds->devtype->ledctrl_base + reg,
Alexander Shiyan677d13f2014-01-31 22:36:30 -0800109 mc13xxx_max_brightness(led->id) << shift,
110 led->new_brightness << shift);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200111}
112
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700113static void mc13xxx_led_set(struct led_classdev *led_cdev,
114 enum led_brightness value)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200115{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700116 struct mc13xxx_led *led =
117 container_of(led_cdev, struct mc13xxx_led, cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200118
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200119 led->new_brightness = value;
120 schedule_work(&led->work);
121}
122
Alexander Shiyan25c65792014-01-31 22:37:23 -0800123#ifdef CONFIG_OF
124static struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
125 struct platform_device *pdev)
126{
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;
132
133 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
134 if (!pdata)
135 return ERR_PTR(-ENOMEM);
136
Geert Uytterhoeven452bc142015-01-14 05:51:58 -0800137 parent = of_get_child_by_name(dev->parent->of_node, "leds");
Alexander Shiyan25c65792014-01-31 22:37:23 -0800138 if (!parent)
139 goto out_node_put;
140
141 ret = of_property_read_u32_array(parent, "led-control",
142 pdata->led_control,
143 leds->devtype->num_regs);
144 if (ret)
145 goto out_node_put;
146
147 pdata->num_leds = of_get_child_count(parent);
148
149 pdata->led = devm_kzalloc(dev, pdata->num_leds * sizeof(*pdata->led),
150 GFP_KERNEL);
151 if (!pdata->led) {
152 ret = -ENOMEM;
153 goto out_node_put;
154 }
155
156 for_each_child_of_node(parent, child) {
157 const char *str;
158 u32 tmp;
159
160 if (of_property_read_u32(child, "reg", &tmp))
161 continue;
162 pdata->led[i].id = leds->devtype->led_min + tmp;
163
164 if (!of_property_read_string(child, "label", &str))
165 pdata->led[i].name = str;
166 if (!of_property_read_string(child, "linux,default-trigger",
167 &str))
168 pdata->led[i].default_trigger = str;
169
170 i++;
171 }
172
173 pdata->num_leds = i;
174 ret = i > 0 ? 0 : -ENODATA;
175
176out_node_put:
177 of_node_put(parent);
178
179 return ret ? ERR_PTR(ret) : pdata;
180}
181#else
182static inline struct mc13xxx_leds_platform_data __init *mc13xxx_led_probe_dt(
183 struct platform_device *pdev)
184{
185 return ERR_PTR(-ENOSYS);
186}
187#endif
188
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700189static int __init mc13xxx_led_probe(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200190{
Alexander Shiyana59ce652014-01-31 22:36:28 -0800191 struct device *dev = &pdev->dev;
192 struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(dev);
193 struct mc13xxx *mcdev = dev_get_drvdata(dev->parent);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700194 struct mc13xxx_led_devtype *devtype =
195 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
196 struct mc13xxx_leds *leds;
Alexander Shiyan25c65792014-01-31 22:37:23 -0800197 int i, id, ret = -ENODATA;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800198 u32 init_led = 0;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200199
Alexander Shiyan25c65792014-01-31 22:37:23 -0800200 leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700201 if (!leds)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200202 return -ENOMEM;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200203
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700204 leds->devtype = devtype;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800205 leds->master = mcdev;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700206 platform_set_drvdata(pdev, leds);
207
Alexander Shiyan25c65792014-01-31 22:37:23 -0800208 if (dev->parent->of_node) {
209 pdata = mc13xxx_led_probe_dt(pdev);
210 if (IS_ERR(pdata))
211 return PTR_ERR(pdata);
212 } else if (!pdata)
213 return -ENODATA;
214
215 leds->num_leds = pdata->num_leds;
216
217 if ((leds->num_leds < 1) ||
218 (leds->num_leds > (devtype->led_max - devtype->led_min + 1))) {
219 dev_err(dev, "Invalid LED count %d\n", leds->num_leds);
220 return -EINVAL;
221 }
222
223 leds->led = devm_kzalloc(dev, leds->num_leds * sizeof(*leds->led),
224 GFP_KERNEL);
225 if (!leds->led)
226 return -ENOMEM;
227
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700228 for (i = 0; i < devtype->num_regs; i++) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800229 ret = mc13xxx_reg_write(mcdev, leds->devtype->ledctrl_base + i,
230 pdata->led_control[i]);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700231 if (ret)
Alexander Shiyan3df22c02013-12-06 22:22:19 -0800232 return ret;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200233 }
234
Alexander Shiyan25c65792014-01-31 22:37:23 -0800235 for (i = 0; i < leds->num_leds; i++) {
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700236 const char *name, *trig;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200237
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700238 ret = -EINVAL;
239
240 id = pdata->led[i].id;
241 name = pdata->led[i].name;
242 trig = pdata->led[i].default_trigger;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700243
244 if ((id > devtype->led_max) || (id < devtype->led_min)) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800245 dev_err(dev, "Invalid ID %i\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700246 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200247 }
248
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700249 if (init_led & (1 << id)) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800250 dev_warn(dev, "LED %i already initialized\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700251 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200252 }
253
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700254 init_led |= 1 << id;
255 leds->led[i].id = id;
Alexander Shiyana59ce652014-01-31 22:36:28 -0800256 leds->led[i].leds = leds;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700257 leds->led[i].cdev.name = name;
258 leds->led[i].cdev.default_trigger = trig;
Alexander Shiyan02e9e112014-01-31 22:36:29 -0800259 leds->led[i].cdev.flags = LED_CORE_SUSPENDRESUME;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700260 leds->led[i].cdev.brightness_set = mc13xxx_led_set;
Alexander Shiyan677d13f2014-01-31 22:36:30 -0800261 leds->led[i].cdev.max_brightness = mc13xxx_max_brightness(id);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200262
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700263 INIT_WORK(&leds->led[i].work, mc13xxx_led_work);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200264
Alexander Shiyana59ce652014-01-31 22:36:28 -0800265 ret = led_classdev_register(dev->parent, &leds->led[i].cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200266 if (ret) {
Alexander Shiyana59ce652014-01-31 22:36:28 -0800267 dev_err(dev, "Failed to register LED %i\n", id);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700268 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200269 }
270 }
271
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700272 if (ret)
273 while (--i >= 0) {
274 led_classdev_unregister(&leds->led[i].cdev);
275 cancel_work_sync(&leds->led[i].work);
276 }
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200277
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200278 return ret;
279}
280
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700281static int mc13xxx_led_remove(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200282{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700283 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200284 int i;
285
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700286 for (i = 0; i < leds->num_leds; i++) {
287 led_classdev_unregister(&leds->led[i].cdev);
288 cancel_work_sync(&leds->led[i].work);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200289 }
290
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200291 return 0;
292}
293
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700294static const struct mc13xxx_led_devtype mc13783_led_devtype = {
295 .led_min = MC13783_LED_MD,
296 .led_max = MC13783_LED_B3,
297 .num_regs = 6,
Alexander Shiyana59ce652014-01-31 22:36:28 -0800298 .ledctrl_base = 51,
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200299};
300
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700301static const struct mc13xxx_led_devtype mc13892_led_devtype = {
302 .led_min = MC13892_LED_MD,
303 .led_max = MC13892_LED_B,
304 .num_regs = 4,
Alexander Shiyana59ce652014-01-31 22:36:28 -0800305 .ledctrl_base = 51,
306};
307
308static const struct mc13xxx_led_devtype mc34708_led_devtype = {
309 .led_min = MC34708_LED_R,
310 .led_max = MC34708_LED_G,
311 .num_regs = 1,
312 .ledctrl_base = 54,
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700313};
314
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700315static const struct platform_device_id mc13xxx_led_id_table[] = {
316 { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
Alexander Shiyanae6cdb02013-06-10 09:59:31 -0700317 { "mc13892-led", (kernel_ulong_t)&mc13892_led_devtype, },
Alexander Shiyana59ce652014-01-31 22:36:28 -0800318 { "mc34708-led", (kernel_ulong_t)&mc34708_led_devtype, },
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700319 { }
320};
321MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200322
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700323static struct platform_driver mc13xxx_led_driver = {
324 .driver = {
325 .name = "mc13xxx-led",
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700326 },
327 .remove = mc13xxx_led_remove,
328 .id_table = mc13xxx_led_id_table,
329};
330module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
331
332MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200333MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
334MODULE_LICENSE("GPL");