xref: /openbmc/linux/drivers/leds/leds-adp5520.c (revision 98ea1ea2)
1ed4a10b4SMichael Hennerich /*
2ed4a10b4SMichael Hennerich  * LEDs driver for Analog Devices ADP5520/ADP5501 MFD PMICs
3ed4a10b4SMichael Hennerich  *
4ed4a10b4SMichael Hennerich  * Copyright 2009 Analog Devices Inc.
5ed4a10b4SMichael Hennerich  *
6ed4a10b4SMichael Hennerich  * Loosely derived from leds-da903x:
7ed4a10b4SMichael Hennerich  * Copyright (C) 2008 Compulab, Ltd.
8ed4a10b4SMichael Hennerich  * 	Mike Rapoport <mike@compulab.co.il>
9ed4a10b4SMichael Hennerich  *
10ed4a10b4SMichael Hennerich  * Copyright (C) 2006-2008 Marvell International Ltd.
11ed4a10b4SMichael Hennerich  * 	Eric Miao <eric.miao@marvell.com>
12ed4a10b4SMichael Hennerich  *
13ed4a10b4SMichael Hennerich  * Licensed under the GPL-2 or later.
14ed4a10b4SMichael Hennerich  */
15ed4a10b4SMichael Hennerich 
16ed4a10b4SMichael Hennerich #include <linux/module.h>
17ed4a10b4SMichael Hennerich #include <linux/kernel.h>
18ed4a10b4SMichael Hennerich #include <linux/init.h>
19ed4a10b4SMichael Hennerich #include <linux/platform_device.h>
20ed4a10b4SMichael Hennerich #include <linux/leds.h>
21ed4a10b4SMichael Hennerich #include <linux/workqueue.h>
22ed4a10b4SMichael Hennerich #include <linux/mfd/adp5520.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24ed4a10b4SMichael Hennerich 
25ed4a10b4SMichael Hennerich struct adp5520_led {
26ed4a10b4SMichael Hennerich 	struct led_classdev	cdev;
27ed4a10b4SMichael Hennerich 	struct work_struct	work;
28ed4a10b4SMichael Hennerich 	struct device		*master;
29ed4a10b4SMichael Hennerich 	enum led_brightness	new_brightness;
30ed4a10b4SMichael Hennerich 	int			id;
31ed4a10b4SMichael Hennerich 	int			flags;
32ed4a10b4SMichael Hennerich };
33ed4a10b4SMichael Hennerich 
34ed4a10b4SMichael Hennerich static void adp5520_led_work(struct work_struct *work)
35ed4a10b4SMichael Hennerich {
36ed4a10b4SMichael Hennerich 	struct adp5520_led *led = container_of(work, struct adp5520_led, work);
37ed4a10b4SMichael Hennerich 	adp5520_write(led->master, ADP5520_LED1_CURRENT + led->id - 1,
38ed4a10b4SMichael Hennerich 			 led->new_brightness >> 2);
39ed4a10b4SMichael Hennerich }
40ed4a10b4SMichael Hennerich 
41ed4a10b4SMichael Hennerich static void adp5520_led_set(struct led_classdev *led_cdev,
42ed4a10b4SMichael Hennerich 			   enum led_brightness value)
43ed4a10b4SMichael Hennerich {
44ed4a10b4SMichael Hennerich 	struct adp5520_led *led;
45ed4a10b4SMichael Hennerich 
46ed4a10b4SMichael Hennerich 	led = container_of(led_cdev, struct adp5520_led, cdev);
47ed4a10b4SMichael Hennerich 	led->new_brightness = value;
48ed4a10b4SMichael Hennerich 	schedule_work(&led->work);
49ed4a10b4SMichael Hennerich }
50ed4a10b4SMichael Hennerich 
51ed4a10b4SMichael Hennerich static int adp5520_led_setup(struct adp5520_led *led)
52ed4a10b4SMichael Hennerich {
53ed4a10b4SMichael Hennerich 	struct device *dev = led->master;
54ed4a10b4SMichael Hennerich 	int flags = led->flags;
55ed4a10b4SMichael Hennerich 	int ret = 0;
56ed4a10b4SMichael Hennerich 
57ed4a10b4SMichael Hennerich 	switch (led->id) {
58ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED1_ADP5501_LED0:
59ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
60ed4a10b4SMichael Hennerich 					(flags >> ADP5520_FLAG_OFFT_SHIFT) &
61ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK);
62ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
63ed4a10b4SMichael Hennerich 					ADP5520_LED1_EN);
64ed4a10b4SMichael Hennerich 		break;
65ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED2_ADP5501_LED1:
66ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev,  ADP5520_LED_TIME,
67ed4a10b4SMichael Hennerich 					((flags >> ADP5520_FLAG_OFFT_SHIFT) &
68ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK) << 2);
69ed4a10b4SMichael Hennerich 		ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
70ed4a10b4SMichael Hennerich 					 ADP5520_R3_MODE);
71ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
72ed4a10b4SMichael Hennerich 					ADP5520_LED2_EN);
73ed4a10b4SMichael Hennerich 		break;
74ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED3_ADP5501_LED2:
75ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev,  ADP5520_LED_TIME,
76ed4a10b4SMichael Hennerich 					((flags >> ADP5520_FLAG_OFFT_SHIFT) &
77ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK) << 4);
78ed4a10b4SMichael Hennerich 		ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
79ed4a10b4SMichael Hennerich 					ADP5520_C3_MODE);
80ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
81ed4a10b4SMichael Hennerich 					ADP5520_LED3_EN);
82ed4a10b4SMichael Hennerich 		break;
83ed4a10b4SMichael Hennerich 	}
84ed4a10b4SMichael Hennerich 
85ed4a10b4SMichael Hennerich 	return ret;
86ed4a10b4SMichael Hennerich }
87ed4a10b4SMichael Hennerich 
8898ea1ea2SBill Pemberton static int adp5520_led_prepare(struct platform_device *pdev)
89ed4a10b4SMichael Hennerich {
90ed4a10b4SMichael Hennerich 	struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
91ed4a10b4SMichael Hennerich 	struct device *dev = pdev->dev.parent;
92ed4a10b4SMichael Hennerich 	int ret = 0;
93ed4a10b4SMichael Hennerich 
94ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED1_CURRENT, 0);
95ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED2_CURRENT, 0);
96ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED3_CURRENT, 0);
97ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED_TIME, pdata->led_on_time << 6);
98ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED_FADE, FADE_VAL(pdata->fade_in,
99ed4a10b4SMichael Hennerich 		 pdata->fade_out));
100ed4a10b4SMichael Hennerich 
101ed4a10b4SMichael Hennerich 	return ret;
102ed4a10b4SMichael Hennerich }
103ed4a10b4SMichael Hennerich 
10498ea1ea2SBill Pemberton static int adp5520_led_probe(struct platform_device *pdev)
105ed4a10b4SMichael Hennerich {
106ed4a10b4SMichael Hennerich 	struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
107ed4a10b4SMichael Hennerich 	struct adp5520_led *led, *led_dat;
108ed4a10b4SMichael Hennerich 	struct led_info *cur_led;
109ed4a10b4SMichael Hennerich 	int ret, i;
110ed4a10b4SMichael Hennerich 
111ed4a10b4SMichael Hennerich 	if (pdata == NULL) {
112ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "missing platform data\n");
113ed4a10b4SMichael Hennerich 		return -ENODEV;
114ed4a10b4SMichael Hennerich 	}
115ed4a10b4SMichael Hennerich 
116ed4a10b4SMichael Hennerich 	if (pdata->num_leds > ADP5520_01_MAXLEDS) {
117ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "can't handle more than %d LEDS\n",
118ed4a10b4SMichael Hennerich 				 ADP5520_01_MAXLEDS);
119ed4a10b4SMichael Hennerich 		return -EFAULT;
120ed4a10b4SMichael Hennerich 	}
121ed4a10b4SMichael Hennerich 
122bcbf0162SBryan Wu 	led = devm_kzalloc(&pdev->dev, sizeof(*led) * pdata->num_leds,
123bcbf0162SBryan Wu 				GFP_KERNEL);
124ed4a10b4SMichael Hennerich 	if (led == NULL) {
125ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "failed to alloc memory\n");
126ed4a10b4SMichael Hennerich 		return -ENOMEM;
127ed4a10b4SMichael Hennerich 	}
128ed4a10b4SMichael Hennerich 
129ed4a10b4SMichael Hennerich 	ret = adp5520_led_prepare(pdev);
130ed4a10b4SMichael Hennerich 
131ed4a10b4SMichael Hennerich 	if (ret) {
132ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "failed to write\n");
133bcbf0162SBryan Wu 		return ret;
134ed4a10b4SMichael Hennerich 	}
135ed4a10b4SMichael Hennerich 
136ed4a10b4SMichael Hennerich 	for (i = 0; i < pdata->num_leds; ++i) {
137ed4a10b4SMichael Hennerich 		cur_led = &pdata->leds[i];
138ed4a10b4SMichael Hennerich 		led_dat = &led[i];
139ed4a10b4SMichael Hennerich 
140ed4a10b4SMichael Hennerich 		led_dat->cdev.name = cur_led->name;
141ed4a10b4SMichael Hennerich 		led_dat->cdev.default_trigger = cur_led->default_trigger;
142ed4a10b4SMichael Hennerich 		led_dat->cdev.brightness_set = adp5520_led_set;
143ed4a10b4SMichael Hennerich 		led_dat->cdev.brightness = LED_OFF;
144ed4a10b4SMichael Hennerich 
145ed4a10b4SMichael Hennerich 		if (cur_led->flags & ADP5520_FLAG_LED_MASK)
146ed4a10b4SMichael Hennerich 			led_dat->flags = cur_led->flags;
147ed4a10b4SMichael Hennerich 		else
148ed4a10b4SMichael Hennerich 			led_dat->flags = i + 1;
149ed4a10b4SMichael Hennerich 
150ed4a10b4SMichael Hennerich 		led_dat->id = led_dat->flags & ADP5520_FLAG_LED_MASK;
151ed4a10b4SMichael Hennerich 
152ed4a10b4SMichael Hennerich 		led_dat->master = pdev->dev.parent;
153ed4a10b4SMichael Hennerich 		led_dat->new_brightness = LED_OFF;
154ed4a10b4SMichael Hennerich 
155ed4a10b4SMichael Hennerich 		INIT_WORK(&led_dat->work, adp5520_led_work);
156ed4a10b4SMichael Hennerich 
157ed4a10b4SMichael Hennerich 		ret = led_classdev_register(led_dat->master, &led_dat->cdev);
158ed4a10b4SMichael Hennerich 		if (ret) {
159ed4a10b4SMichael Hennerich 			dev_err(&pdev->dev, "failed to register LED %d\n",
160ed4a10b4SMichael Hennerich 				led_dat->id);
161ed4a10b4SMichael Hennerich 			goto err;
162ed4a10b4SMichael Hennerich 		}
163ed4a10b4SMichael Hennerich 
164ed4a10b4SMichael Hennerich 		ret = adp5520_led_setup(led_dat);
165ed4a10b4SMichael Hennerich 		if (ret) {
166ed4a10b4SMichael Hennerich 			dev_err(&pdev->dev, "failed to write\n");
167ed4a10b4SMichael Hennerich 			i++;
168ed4a10b4SMichael Hennerich 			goto err;
169ed4a10b4SMichael Hennerich 		}
170ed4a10b4SMichael Hennerich 	}
171ed4a10b4SMichael Hennerich 
172ed4a10b4SMichael Hennerich 	platform_set_drvdata(pdev, led);
173ed4a10b4SMichael Hennerich 	return 0;
174ed4a10b4SMichael Hennerich 
175ed4a10b4SMichael Hennerich err:
176ed4a10b4SMichael Hennerich 	if (i > 0) {
177ed4a10b4SMichael Hennerich 		for (i = i - 1; i >= 0; i--) {
178ed4a10b4SMichael Hennerich 			led_classdev_unregister(&led[i].cdev);
179ed4a10b4SMichael Hennerich 			cancel_work_sync(&led[i].work);
180ed4a10b4SMichael Hennerich 		}
181ed4a10b4SMichael Hennerich 	}
182ed4a10b4SMichael Hennerich 
183ed4a10b4SMichael Hennerich 	return ret;
184ed4a10b4SMichael Hennerich }
185ed4a10b4SMichael Hennerich 
186ed4a10b4SMichael Hennerich static int __devexit adp5520_led_remove(struct platform_device *pdev)
187ed4a10b4SMichael Hennerich {
188ed4a10b4SMichael Hennerich 	struct adp5520_leds_platform_data *pdata = pdev->dev.platform_data;
189ed4a10b4SMichael Hennerich 	struct adp5520_led *led;
190ed4a10b4SMichael Hennerich 	int i;
191ed4a10b4SMichael Hennerich 
192ed4a10b4SMichael Hennerich 	led = platform_get_drvdata(pdev);
193ed4a10b4SMichael Hennerich 
194ed4a10b4SMichael Hennerich 	adp5520_clr_bits(led->master, ADP5520_LED_CONTROL,
195ed4a10b4SMichael Hennerich 		 ADP5520_LED1_EN | ADP5520_LED2_EN | ADP5520_LED3_EN);
196ed4a10b4SMichael Hennerich 
197ed4a10b4SMichael Hennerich 	for (i = 0; i < pdata->num_leds; i++) {
198ed4a10b4SMichael Hennerich 		led_classdev_unregister(&led[i].cdev);
199ed4a10b4SMichael Hennerich 		cancel_work_sync(&led[i].work);
200ed4a10b4SMichael Hennerich 	}
201ed4a10b4SMichael Hennerich 
202ed4a10b4SMichael Hennerich 	return 0;
203ed4a10b4SMichael Hennerich }
204ed4a10b4SMichael Hennerich 
205ed4a10b4SMichael Hennerich static struct platform_driver adp5520_led_driver = {
206ed4a10b4SMichael Hennerich 	.driver	= {
207ed4a10b4SMichael Hennerich 		.name	= "adp5520-led",
208ed4a10b4SMichael Hennerich 		.owner	= THIS_MODULE,
209ed4a10b4SMichael Hennerich 	},
210ed4a10b4SMichael Hennerich 	.probe		= adp5520_led_probe,
211df07cf81SBill Pemberton 	.remove		= adp5520_led_remove,
212ed4a10b4SMichael Hennerich };
213ed4a10b4SMichael Hennerich 
214892a8843SAxel Lin module_platform_driver(adp5520_led_driver);
215ed4a10b4SMichael Hennerich 
216ed4a10b4SMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
217ed4a10b4SMichael Hennerich MODULE_DESCRIPTION("LEDS ADP5520(01) Driver");
218ed4a10b4SMichael Hennerich MODULE_LICENSE("GPL");
219ed4a10b4SMichael Hennerich MODULE_ALIAS("platform:adp5520-led");
220