blob: da8ec244a64128d6097cb271e5fe12224b4efd39 [file] [log] [blame]
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +02001/*
2 * LEDs driver for Freescale MC13783
3 *
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>
20#include <linux/init.h>
21#include <linux/platform_device.h>
22#include <linux/leds.h>
23#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 -070026#define MC13XXX_REG_LED_CONTROL(x) (51 + (x))
27
28struct mc13xxx_led_devtype {
29 int led_min;
30 int led_max;
31 int num_regs;
32};
33
34struct mc13xxx_led {
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020035 struct led_classdev cdev;
36 struct work_struct work;
David Janderf3ca0782011-08-24 15:28:20 +020037 struct mc13xxx *master;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020038 enum led_brightness new_brightness;
39 int id;
40};
41
Alexander Shiyan9d2638132013-06-10 09:59:30 -070042struct mc13xxx_leds {
43 struct mc13xxx_led_devtype *devtype;
44 int num_leds;
45 struct mc13xxx_led led[0];
46};
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020047
Alexander Shiyan9d2638132013-06-10 09:59:30 -070048static void mc13xxx_led_work(struct work_struct *work)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020049{
Alexander Shiyan9d2638132013-06-10 09:59:30 -070050 struct mc13xxx_led *led = container_of(work, struct mc13xxx_led, work);
51 int reg, mask, value, bank, off, shift;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020052
53 switch (led->id) {
54 case MC13783_LED_MD:
Alexander Shiyan9d2638132013-06-10 09:59:30 -070055 reg = MC13XXX_REG_LED_CONTROL(2);
56 shift = 9;
57 mask = 0x0f;
58 value = led->new_brightness >> 4;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020059 break;
60 case MC13783_LED_AD:
Alexander Shiyan9d2638132013-06-10 09:59:30 -070061 reg = MC13XXX_REG_LED_CONTROL(2);
62 shift = 13;
63 mask = 0x0f;
64 value = led->new_brightness >> 4;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020065 break;
66 case MC13783_LED_KP:
Alexander Shiyan9d2638132013-06-10 09:59:30 -070067 reg = MC13XXX_REG_LED_CONTROL(2);
68 shift = 17;
69 mask = 0x0f;
70 value = led->new_brightness >> 4;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020071 break;
72 case MC13783_LED_R1:
73 case MC13783_LED_G1:
74 case MC13783_LED_B1:
75 case MC13783_LED_R2:
76 case MC13783_LED_G2:
77 case MC13783_LED_B2:
78 case MC13783_LED_R3:
79 case MC13783_LED_G3:
80 case MC13783_LED_B3:
81 off = led->id - MC13783_LED_R1;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070082 bank = off / 3;
83 reg = MC13XXX_REG_LED_CONTROL(3) + bank;
84 shift = (off - bank * 3) * 5 + 6;
85 value = led->new_brightness >> 3;
86 mask = 0x1f;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020087 break;
Alexander Shiyan9d2638132013-06-10 09:59:30 -070088 default:
89 BUG();
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020090 }
91
David Janderf3ca0782011-08-24 15:28:20 +020092 mc13xxx_lock(led->master);
Alexander Shiyan9d2638132013-06-10 09:59:30 -070093 mc13xxx_reg_rmw(led->master, reg, mask << shift, value << shift);
David Janderf3ca0782011-08-24 15:28:20 +020094 mc13xxx_unlock(led->master);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020095}
96
Alexander Shiyan9d2638132013-06-10 09:59:30 -070097static void mc13xxx_led_set(struct led_classdev *led_cdev,
98 enum led_brightness value)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +020099{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700100 struct mc13xxx_led *led =
101 container_of(led_cdev, struct mc13xxx_led, cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200102
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200103 led->new_brightness = value;
104 schedule_work(&led->work);
105}
106
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700107static int __init mc13xxx_led_setup(struct mc13xxx_led *led, int max_current)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200108{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700109 int shift, mask, reg, ret, bank;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200110
111 switch (led->id) {
112 case MC13783_LED_MD:
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700113 reg = MC13XXX_REG_LED_CONTROL(2);
114 shift = 0;
115 mask = 0x07;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200116 break;
117 case MC13783_LED_AD:
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700118 reg = MC13XXX_REG_LED_CONTROL(2);
119 shift = 3;
120 mask = 0x07;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200121 break;
122 case MC13783_LED_KP:
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700123 reg = MC13XXX_REG_LED_CONTROL(2);
124 shift = 6;
125 mask = 0x07;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200126 break;
127 case MC13783_LED_R1:
128 case MC13783_LED_G1:
129 case MC13783_LED_B1:
130 case MC13783_LED_R2:
131 case MC13783_LED_G2:
132 case MC13783_LED_B2:
133 case MC13783_LED_R3:
134 case MC13783_LED_G3:
135 case MC13783_LED_B3:
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700136 bank = (led->id - MC13783_LED_R1) / 3;
137 reg = MC13XXX_REG_LED_CONTROL(3) + bank;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200138 shift = ((led->id - MC13783_LED_R1) - bank * 3) * 2;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700139 mask = 0x03;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200140 break;
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700141 default:
142 BUG();
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200143 }
144
David Janderf3ca0782011-08-24 15:28:20 +0200145 mc13xxx_lock(led->master);
David Janderf3ca0782011-08-24 15:28:20 +0200146 ret = mc13xxx_reg_rmw(led->master, reg, mask << shift,
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700147 max_current << shift);
David Janderf3ca0782011-08-24 15:28:20 +0200148 mc13xxx_unlock(led->master);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700149
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200150 return ret;
151}
152
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700153static int __init mc13xxx_led_probe(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200154{
Samuel Ortiz52b7ad32011-09-18 19:12:34 +0200155 struct mc13xxx_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700156 struct mc13xxx *mcdev = dev_get_drvdata(pdev->dev.parent);
157 struct mc13xxx_led_devtype *devtype =
158 (struct mc13xxx_led_devtype *)pdev->id_entry->driver_data;
159 struct mc13xxx_leds *leds;
160 int i, id, num_leds, ret;
161 u32 reg, init_led = 0;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200162
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700163 if (!pdata) {
164 dev_err(&pdev->dev, "Missing platform data\n");
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200165 return -ENODEV;
166 }
167
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700168 num_leds = pdata->num_leds;
169
170 if ((num_leds < 1) ||
171 (num_leds > (devtype->led_max - devtype->led_min + 1))) {
172 dev_err(&pdev->dev, "Invalid LED count %d\n", num_leds);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200173 return -EINVAL;
174 }
175
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700176 leds = devm_kzalloc(&pdev->dev, num_leds * sizeof(struct mc13xxx_led) +
177 sizeof(struct mc13xxx_leds), GFP_KERNEL);
178 if (!leds)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200179 return -ENOMEM;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200180
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700181 leds->devtype = devtype;
182 leds->num_leds = num_leds;
183 platform_set_drvdata(pdev, leds);
184
185 mc13xxx_lock(mcdev);
186 for (i = 0; i < devtype->num_regs; i++) {
187 reg = pdata->led_control[i];
188 WARN_ON(reg >= (1 << 24));
189 ret = mc13xxx_reg_write(mcdev, MC13XXX_REG_LED_CONTROL(i), reg);
190 if (ret)
191 break;
192 }
193 mc13xxx_unlock(mcdev);
194
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200195 if (ret) {
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700196 dev_err(&pdev->dev, "Unable to init LED driver\n");
Bryan Wua44cdd22012-07-04 12:18:35 +0800197 return ret;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200198 }
199
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700200 for (i = 0; i < num_leds; i++) {
201 const char *name, *trig;
202 char max_current;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200203
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700204 ret = -EINVAL;
205
206 id = pdata->led[i].id;
207 name = pdata->led[i].name;
208 trig = pdata->led[i].default_trigger;
209 max_current = pdata->led[i].max_current;
210
211 if ((id > devtype->led_max) || (id < devtype->led_min)) {
212 dev_err(&pdev->dev, "Invalid ID %i\n", id);
213 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200214 }
215
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700216 if (init_led & (1 << id)) {
217 dev_warn(&pdev->dev,
218 "LED %i already initialized\n", id);
219 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200220 }
221
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700222 init_led |= 1 << id;
223 leds->led[i].id = id;
224 leds->led[i].master = mcdev;
225 leds->led[i].cdev.name = name;
226 leds->led[i].cdev.default_trigger = trig;
227 leds->led[i].cdev.brightness_set = mc13xxx_led_set;
228 leds->led[i].cdev.brightness = LED_OFF;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200229
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700230 INIT_WORK(&leds->led[i].work, mc13xxx_led_work);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200231
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700232 ret = mc13xxx_led_setup(&leds->led[i], max_current);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200233 if (ret) {
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700234 dev_err(&pdev->dev, "Unable to setup LED %i\n", id);
235 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200236 }
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700237 ret = led_classdev_register(pdev->dev.parent,
238 &leds->led[i].cdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200239 if (ret) {
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700240 dev_err(&pdev->dev, "Failed to register LED %i\n", id);
241 break;
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200242 }
243 }
244
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700245 if (ret)
246 while (--i >= 0) {
247 led_classdev_unregister(&leds->led[i].cdev);
248 cancel_work_sync(&leds->led[i].work);
249 }
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200250
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200251 return ret;
252}
253
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700254static int mc13xxx_led_remove(struct platform_device *pdev)
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200255{
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700256 struct mc13xxx *mcdev = dev_get_drvdata(pdev->dev.parent);
257 struct mc13xxx_leds *leds = platform_get_drvdata(pdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200258 int i;
259
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700260 for (i = 0; i < leds->num_leds; i++) {
261 led_classdev_unregister(&leds->led[i].cdev);
262 cancel_work_sync(&leds->led[i].work);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200263 }
264
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700265 mc13xxx_lock(mcdev);
266 for (i = 0; i < leds->devtype->num_regs; i++)
267 mc13xxx_reg_write(mcdev, MC13XXX_REG_LED_CONTROL(i), 0);
268 mc13xxx_unlock(mcdev);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200269
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200270 return 0;
271}
272
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700273static const struct mc13xxx_led_devtype mc13783_led_devtype = {
274 .led_min = MC13783_LED_MD,
275 .led_max = MC13783_LED_B3,
276 .num_regs = 6,
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200277};
278
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700279static const struct platform_device_id mc13xxx_led_id_table[] = {
280 { "mc13783-led", (kernel_ulong_t)&mc13783_led_devtype, },
281 { }
282};
283MODULE_DEVICE_TABLE(platform, mc13xxx_led_id_table);
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200284
Alexander Shiyan9d2638132013-06-10 09:59:30 -0700285static struct platform_driver mc13xxx_led_driver = {
286 .driver = {
287 .name = "mc13xxx-led",
288 .owner = THIS_MODULE,
289 },
290 .remove = mc13xxx_led_remove,
291 .id_table = mc13xxx_led_id_table,
292};
293module_platform_driver_probe(mc13xxx_led_driver, mc13xxx_led_probe);
294
295MODULE_DESCRIPTION("LEDs driver for Freescale MC13XXX PMIC");
Philippe Rétornaz7fdcef82010-05-19 09:24:31 +0200296MODULE_AUTHOR("Philippe Retornaz <philippe.retornaz@epfl.ch>");
297MODULE_LICENSE("GPL");