blob: 5036d7b4f82e812f60fce7322288339162077cb0 [file] [log] [blame]
Michael Henneriched4a10b2009-10-08 04:00:19 -04001/*
2 * LEDs driver for Analog Devices ADP5520/ADP5501 MFD PMICs
3 *
4 * Copyright 2009 Analog Devices Inc.
5 *
6 * Loosely derived from leds-da903x:
7 * Copyright (C) 2008 Compulab, Ltd.
Sachin Kamatef734162012-11-25 11:05:41 +05308 * Mike Rapoport <mike@compulab.co.il>
Michael Henneriched4a10b2009-10-08 04:00:19 -04009 *
10 * Copyright (C) 2006-2008 Marvell International Ltd.
Sachin Kamatef734162012-11-25 11:05:41 +053011 * Eric Miao <eric.miao@marvell.com>
Michael Henneriched4a10b2009-10-08 04:00:19 -040012 *
13 * Licensed under the GPL-2 or later.
14 */
15
16#include <linux/module.h>
17#include <linux/kernel.h>
Michael Henneriched4a10b2009-10-08 04:00:19 -040018#include <linux/platform_device.h>
19#include <linux/leds.h>
20#include <linux/workqueue.h>
21#include <linux/mfd/adp5520.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Michael Henneriched4a10b2009-10-08 04:00:19 -040023
24struct adp5520_led {
25 struct led_classdev cdev;
26 struct work_struct work;
27 struct device *master;
28 enum led_brightness new_brightness;
29 int id;
30 int flags;
31};
32
33static void adp5520_led_work(struct work_struct *work)
34{
35 struct adp5520_led *led = container_of(work, struct adp5520_led, work);
36 adp5520_write(led->master, ADP5520_LED1_CURRENT + led->id - 1,
37 led->new_brightness >> 2);
38}
39
40static void adp5520_led_set(struct led_classdev *led_cdev,
41 enum led_brightness value)
42{
43 struct adp5520_led *led;
44
45 led = container_of(led_cdev, struct adp5520_led, cdev);
46 led->new_brightness = value;
47 schedule_work(&led->work);
48}
49
50static int adp5520_led_setup(struct adp5520_led *led)
51{
52 struct device *dev = led->master;
53 int flags = led->flags;
54 int ret = 0;
55
56 switch (led->id) {
57 case FLAG_ID_ADP5520_LED1_ADP5501_LED0:
58 ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
59 (flags >> ADP5520_FLAG_OFFT_SHIFT) &
60 ADP5520_FLAG_OFFT_MASK);
61 ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
62 ADP5520_LED1_EN);
63 break;
64 case FLAG_ID_ADP5520_LED2_ADP5501_LED1:
65 ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
66 ((flags >> ADP5520_FLAG_OFFT_SHIFT) &
67 ADP5520_FLAG_OFFT_MASK) << 2);
68 ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
69 ADP5520_R3_MODE);
70 ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
71 ADP5520_LED2_EN);
72 break;
73 case FLAG_ID_ADP5520_LED3_ADP5501_LED2:
74 ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
75 ((flags >> ADP5520_FLAG_OFFT_SHIFT) &
76 ADP5520_FLAG_OFFT_MASK) << 4);
77 ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
78 ADP5520_C3_MODE);
79 ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
80 ADP5520_LED3_EN);
81 break;
82 }
83
84 return ret;
85}
86
Bill Pemberton98ea1ea2012-11-19 13:23:02 -050087static int adp5520_led_prepare(struct platform_device *pdev)
Michael Henneriched4a10b2009-10-08 04:00:19 -040088{
Jingoo Han87aae1e2013-07-30 01:07:35 -070089 struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Henneriched4a10b2009-10-08 04:00:19 -040090 struct device *dev = pdev->dev.parent;
91 int ret = 0;
92
93 ret |= adp5520_write(dev, ADP5520_LED1_CURRENT, 0);
94 ret |= adp5520_write(dev, ADP5520_LED2_CURRENT, 0);
95 ret |= adp5520_write(dev, ADP5520_LED3_CURRENT, 0);
96 ret |= adp5520_write(dev, ADP5520_LED_TIME, pdata->led_on_time << 6);
97 ret |= adp5520_write(dev, ADP5520_LED_FADE, FADE_VAL(pdata->fade_in,
98 pdata->fade_out));
99
100 return ret;
101}
102
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500103static int adp5520_led_probe(struct platform_device *pdev)
Michael Henneriched4a10b2009-10-08 04:00:19 -0400104{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700105 struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Henneriched4a10b2009-10-08 04:00:19 -0400106 struct adp5520_led *led, *led_dat;
107 struct led_info *cur_led;
108 int ret, i;
109
110 if (pdata == NULL) {
111 dev_err(&pdev->dev, "missing platform data\n");
112 return -ENODEV;
113 }
114
115 if (pdata->num_leds > ADP5520_01_MAXLEDS) {
116 dev_err(&pdev->dev, "can't handle more than %d LEDS\n",
117 ADP5520_01_MAXLEDS);
118 return -EFAULT;
119 }
120
Bryan Wubcbf0162012-07-04 12:11:40 +0800121 led = devm_kzalloc(&pdev->dev, sizeof(*led) * pdata->num_leds,
122 GFP_KERNEL);
Xiubo Li0c9a03b2014-05-07 20:25:52 -0700123 if (!led)
Michael Henneriched4a10b2009-10-08 04:00:19 -0400124 return -ENOMEM;
Michael Henneriched4a10b2009-10-08 04:00:19 -0400125
126 ret = adp5520_led_prepare(pdev);
Michael Henneriched4a10b2009-10-08 04:00:19 -0400127 if (ret) {
128 dev_err(&pdev->dev, "failed to write\n");
Bryan Wubcbf0162012-07-04 12:11:40 +0800129 return ret;
Michael Henneriched4a10b2009-10-08 04:00:19 -0400130 }
131
132 for (i = 0; i < pdata->num_leds; ++i) {
133 cur_led = &pdata->leds[i];
134 led_dat = &led[i];
135
136 led_dat->cdev.name = cur_led->name;
137 led_dat->cdev.default_trigger = cur_led->default_trigger;
138 led_dat->cdev.brightness_set = adp5520_led_set;
139 led_dat->cdev.brightness = LED_OFF;
140
141 if (cur_led->flags & ADP5520_FLAG_LED_MASK)
142 led_dat->flags = cur_led->flags;
143 else
144 led_dat->flags = i + 1;
145
146 led_dat->id = led_dat->flags & ADP5520_FLAG_LED_MASK;
147
148 led_dat->master = pdev->dev.parent;
149 led_dat->new_brightness = LED_OFF;
150
151 INIT_WORK(&led_dat->work, adp5520_led_work);
152
153 ret = led_classdev_register(led_dat->master, &led_dat->cdev);
154 if (ret) {
155 dev_err(&pdev->dev, "failed to register LED %d\n",
156 led_dat->id);
157 goto err;
158 }
159
160 ret = adp5520_led_setup(led_dat);
161 if (ret) {
162 dev_err(&pdev->dev, "failed to write\n");
163 i++;
164 goto err;
165 }
166 }
167
168 platform_set_drvdata(pdev, led);
169 return 0;
170
171err:
172 if (i > 0) {
173 for (i = i - 1; i >= 0; i--) {
174 led_classdev_unregister(&led[i].cdev);
175 cancel_work_sync(&led[i].work);
176 }
177 }
178
Michael Henneriched4a10b2009-10-08 04:00:19 -0400179 return ret;
180}
181
Bill Pemberton678e8a62012-11-19 13:26:00 -0500182static int adp5520_led_remove(struct platform_device *pdev)
Michael Henneriched4a10b2009-10-08 04:00:19 -0400183{
Jingoo Han87aae1e2013-07-30 01:07:35 -0700184 struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
Michael Henneriched4a10b2009-10-08 04:00:19 -0400185 struct adp5520_led *led;
186 int i;
187
188 led = platform_get_drvdata(pdev);
189
190 adp5520_clr_bits(led->master, ADP5520_LED_CONTROL,
191 ADP5520_LED1_EN | ADP5520_LED2_EN | ADP5520_LED3_EN);
192
193 for (i = 0; i < pdata->num_leds; i++) {
194 led_classdev_unregister(&led[i].cdev);
195 cancel_work_sync(&led[i].work);
196 }
197
Michael Henneriched4a10b2009-10-08 04:00:19 -0400198 return 0;
199}
200
201static struct platform_driver adp5520_led_driver = {
202 .driver = {
203 .name = "adp5520-led",
204 .owner = THIS_MODULE,
205 },
206 .probe = adp5520_led_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500207 .remove = adp5520_led_remove,
Michael Henneriched4a10b2009-10-08 04:00:19 -0400208};
209
Axel Lin892a8842012-01-10 15:09:24 -0800210module_platform_driver(adp5520_led_driver);
Michael Henneriched4a10b2009-10-08 04:00:19 -0400211
212MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
213MODULE_DESCRIPTION("LEDS ADP5520(01) Driver");
214MODULE_LICENSE("GPL");
215MODULE_ALIAS("platform:adp5520-led");