xref: /openbmc/linux/drivers/leds/leds-adp5520.c (revision 0c9a03b6)
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/platform_device.h>
19ed4a10b4SMichael Hennerich #include <linux/leds.h>
20ed4a10b4SMichael Hennerich #include <linux/workqueue.h>
21ed4a10b4SMichael Hennerich #include <linux/mfd/adp5520.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
23ed4a10b4SMichael Hennerich 
24ed4a10b4SMichael Hennerich struct adp5520_led {
25ed4a10b4SMichael Hennerich 	struct led_classdev	cdev;
26ed4a10b4SMichael Hennerich 	struct work_struct	work;
27ed4a10b4SMichael Hennerich 	struct device		*master;
28ed4a10b4SMichael Hennerich 	enum led_brightness	new_brightness;
29ed4a10b4SMichael Hennerich 	int			id;
30ed4a10b4SMichael Hennerich 	int			flags;
31ed4a10b4SMichael Hennerich };
32ed4a10b4SMichael Hennerich 
33ed4a10b4SMichael Hennerich static void adp5520_led_work(struct work_struct *work)
34ed4a10b4SMichael Hennerich {
35ed4a10b4SMichael Hennerich 	struct adp5520_led *led = container_of(work, struct adp5520_led, work);
36ed4a10b4SMichael Hennerich 	adp5520_write(led->master, ADP5520_LED1_CURRENT + led->id - 1,
37ed4a10b4SMichael Hennerich 			 led->new_brightness >> 2);
38ed4a10b4SMichael Hennerich }
39ed4a10b4SMichael Hennerich 
40ed4a10b4SMichael Hennerich static void adp5520_led_set(struct led_classdev *led_cdev,
41ed4a10b4SMichael Hennerich 			   enum led_brightness value)
42ed4a10b4SMichael Hennerich {
43ed4a10b4SMichael Hennerich 	struct adp5520_led *led;
44ed4a10b4SMichael Hennerich 
45ed4a10b4SMichael Hennerich 	led = container_of(led_cdev, struct adp5520_led, cdev);
46ed4a10b4SMichael Hennerich 	led->new_brightness = value;
47ed4a10b4SMichael Hennerich 	schedule_work(&led->work);
48ed4a10b4SMichael Hennerich }
49ed4a10b4SMichael Hennerich 
50ed4a10b4SMichael Hennerich static int adp5520_led_setup(struct adp5520_led *led)
51ed4a10b4SMichael Hennerich {
52ed4a10b4SMichael Hennerich 	struct device *dev = led->master;
53ed4a10b4SMichael Hennerich 	int flags = led->flags;
54ed4a10b4SMichael Hennerich 	int ret = 0;
55ed4a10b4SMichael Hennerich 
56ed4a10b4SMichael Hennerich 	switch (led->id) {
57ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED1_ADP5501_LED0:
58ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_TIME,
59ed4a10b4SMichael Hennerich 					(flags >> ADP5520_FLAG_OFFT_SHIFT) &
60ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK);
61ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
62ed4a10b4SMichael Hennerich 					ADP5520_LED1_EN);
63ed4a10b4SMichael Hennerich 		break;
64ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED2_ADP5501_LED1:
65ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev,  ADP5520_LED_TIME,
66ed4a10b4SMichael Hennerich 					((flags >> ADP5520_FLAG_OFFT_SHIFT) &
67ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK) << 2);
68ed4a10b4SMichael Hennerich 		ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
69ed4a10b4SMichael Hennerich 					 ADP5520_R3_MODE);
70ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
71ed4a10b4SMichael Hennerich 					ADP5520_LED2_EN);
72ed4a10b4SMichael Hennerich 		break;
73ed4a10b4SMichael Hennerich 	case FLAG_ID_ADP5520_LED3_ADP5501_LED2:
74ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev,  ADP5520_LED_TIME,
75ed4a10b4SMichael Hennerich 					((flags >> ADP5520_FLAG_OFFT_SHIFT) &
76ed4a10b4SMichael Hennerich 					ADP5520_FLAG_OFFT_MASK) << 4);
77ed4a10b4SMichael Hennerich 		ret |= adp5520_clr_bits(dev, ADP5520_LED_CONTROL,
78ed4a10b4SMichael Hennerich 					ADP5520_C3_MODE);
79ed4a10b4SMichael Hennerich 		ret |= adp5520_set_bits(dev, ADP5520_LED_CONTROL,
80ed4a10b4SMichael Hennerich 					ADP5520_LED3_EN);
81ed4a10b4SMichael Hennerich 		break;
82ed4a10b4SMichael Hennerich 	}
83ed4a10b4SMichael Hennerich 
84ed4a10b4SMichael Hennerich 	return ret;
85ed4a10b4SMichael Hennerich }
86ed4a10b4SMichael Hennerich 
8798ea1ea2SBill Pemberton static int adp5520_led_prepare(struct platform_device *pdev)
88ed4a10b4SMichael Hennerich {
8987aae1eaSJingoo Han 	struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
90ed4a10b4SMichael Hennerich 	struct device *dev = pdev->dev.parent;
91ed4a10b4SMichael Hennerich 	int ret = 0;
92ed4a10b4SMichael Hennerich 
93ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED1_CURRENT, 0);
94ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED2_CURRENT, 0);
95ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED3_CURRENT, 0);
96ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED_TIME, pdata->led_on_time << 6);
97ed4a10b4SMichael Hennerich 	ret |= adp5520_write(dev, ADP5520_LED_FADE, FADE_VAL(pdata->fade_in,
98ed4a10b4SMichael Hennerich 		 pdata->fade_out));
99ed4a10b4SMichael Hennerich 
100ed4a10b4SMichael Hennerich 	return ret;
101ed4a10b4SMichael Hennerich }
102ed4a10b4SMichael Hennerich 
10398ea1ea2SBill Pemberton static int adp5520_led_probe(struct platform_device *pdev)
104ed4a10b4SMichael Hennerich {
10587aae1eaSJingoo Han 	struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
106ed4a10b4SMichael Hennerich 	struct adp5520_led *led, *led_dat;
107ed4a10b4SMichael Hennerich 	struct led_info *cur_led;
108ed4a10b4SMichael Hennerich 	int ret, i;
109ed4a10b4SMichael Hennerich 
110ed4a10b4SMichael Hennerich 	if (pdata == NULL) {
111ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "missing platform data\n");
112ed4a10b4SMichael Hennerich 		return -ENODEV;
113ed4a10b4SMichael Hennerich 	}
114ed4a10b4SMichael Hennerich 
115ed4a10b4SMichael Hennerich 	if (pdata->num_leds > ADP5520_01_MAXLEDS) {
116ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "can't handle more than %d LEDS\n",
117ed4a10b4SMichael Hennerich 				 ADP5520_01_MAXLEDS);
118ed4a10b4SMichael Hennerich 		return -EFAULT;
119ed4a10b4SMichael Hennerich 	}
120ed4a10b4SMichael Hennerich 
121bcbf0162SBryan Wu 	led = devm_kzalloc(&pdev->dev, sizeof(*led) * pdata->num_leds,
122bcbf0162SBryan Wu 				GFP_KERNEL);
1230c9a03b6SXiubo Li 	if (!led)
124ed4a10b4SMichael Hennerich 		return -ENOMEM;
125ed4a10b4SMichael Hennerich 
126ed4a10b4SMichael Hennerich 	ret = adp5520_led_prepare(pdev);
127ed4a10b4SMichael Hennerich 	if (ret) {
128ed4a10b4SMichael Hennerich 		dev_err(&pdev->dev, "failed to write\n");
129bcbf0162SBryan Wu 		return ret;
130ed4a10b4SMichael Hennerich 	}
131ed4a10b4SMichael Hennerich 
132ed4a10b4SMichael Hennerich 	for (i = 0; i < pdata->num_leds; ++i) {
133ed4a10b4SMichael Hennerich 		cur_led = &pdata->leds[i];
134ed4a10b4SMichael Hennerich 		led_dat = &led[i];
135ed4a10b4SMichael Hennerich 
136ed4a10b4SMichael Hennerich 		led_dat->cdev.name = cur_led->name;
137ed4a10b4SMichael Hennerich 		led_dat->cdev.default_trigger = cur_led->default_trigger;
138ed4a10b4SMichael Hennerich 		led_dat->cdev.brightness_set = adp5520_led_set;
139ed4a10b4SMichael Hennerich 		led_dat->cdev.brightness = LED_OFF;
140ed4a10b4SMichael Hennerich 
141ed4a10b4SMichael Hennerich 		if (cur_led->flags & ADP5520_FLAG_LED_MASK)
142ed4a10b4SMichael Hennerich 			led_dat->flags = cur_led->flags;
143ed4a10b4SMichael Hennerich 		else
144ed4a10b4SMichael Hennerich 			led_dat->flags = i + 1;
145ed4a10b4SMichael Hennerich 
146ed4a10b4SMichael Hennerich 		led_dat->id = led_dat->flags & ADP5520_FLAG_LED_MASK;
147ed4a10b4SMichael Hennerich 
148ed4a10b4SMichael Hennerich 		led_dat->master = pdev->dev.parent;
149ed4a10b4SMichael Hennerich 		led_dat->new_brightness = LED_OFF;
150ed4a10b4SMichael Hennerich 
151ed4a10b4SMichael Hennerich 		INIT_WORK(&led_dat->work, adp5520_led_work);
152ed4a10b4SMichael Hennerich 
153ed4a10b4SMichael Hennerich 		ret = led_classdev_register(led_dat->master, &led_dat->cdev);
154ed4a10b4SMichael Hennerich 		if (ret) {
155ed4a10b4SMichael Hennerich 			dev_err(&pdev->dev, "failed to register LED %d\n",
156ed4a10b4SMichael Hennerich 				led_dat->id);
157ed4a10b4SMichael Hennerich 			goto err;
158ed4a10b4SMichael Hennerich 		}
159ed4a10b4SMichael Hennerich 
160ed4a10b4SMichael Hennerich 		ret = adp5520_led_setup(led_dat);
161ed4a10b4SMichael Hennerich 		if (ret) {
162ed4a10b4SMichael Hennerich 			dev_err(&pdev->dev, "failed to write\n");
163ed4a10b4SMichael Hennerich 			i++;
164ed4a10b4SMichael Hennerich 			goto err;
165ed4a10b4SMichael Hennerich 		}
166ed4a10b4SMichael Hennerich 	}
167ed4a10b4SMichael Hennerich 
168ed4a10b4SMichael Hennerich 	platform_set_drvdata(pdev, led);
169ed4a10b4SMichael Hennerich 	return 0;
170ed4a10b4SMichael Hennerich 
171ed4a10b4SMichael Hennerich err:
172ed4a10b4SMichael Hennerich 	if (i > 0) {
173ed4a10b4SMichael Hennerich 		for (i = i - 1; i >= 0; i--) {
174ed4a10b4SMichael Hennerich 			led_classdev_unregister(&led[i].cdev);
175ed4a10b4SMichael Hennerich 			cancel_work_sync(&led[i].work);
176ed4a10b4SMichael Hennerich 		}
177ed4a10b4SMichael Hennerich 	}
178ed4a10b4SMichael Hennerich 
179ed4a10b4SMichael Hennerich 	return ret;
180ed4a10b4SMichael Hennerich }
181ed4a10b4SMichael Hennerich 
182678e8a6bSBill Pemberton static int adp5520_led_remove(struct platform_device *pdev)
183ed4a10b4SMichael Hennerich {
18487aae1eaSJingoo Han 	struct adp5520_leds_platform_data *pdata = dev_get_platdata(&pdev->dev);
185ed4a10b4SMichael Hennerich 	struct adp5520_led *led;
186ed4a10b4SMichael Hennerich 	int i;
187ed4a10b4SMichael Hennerich 
188ed4a10b4SMichael Hennerich 	led = platform_get_drvdata(pdev);
189ed4a10b4SMichael Hennerich 
190ed4a10b4SMichael Hennerich 	adp5520_clr_bits(led->master, ADP5520_LED_CONTROL,
191ed4a10b4SMichael Hennerich 		 ADP5520_LED1_EN | ADP5520_LED2_EN | ADP5520_LED3_EN);
192ed4a10b4SMichael Hennerich 
193ed4a10b4SMichael Hennerich 	for (i = 0; i < pdata->num_leds; i++) {
194ed4a10b4SMichael Hennerich 		led_classdev_unregister(&led[i].cdev);
195ed4a10b4SMichael Hennerich 		cancel_work_sync(&led[i].work);
196ed4a10b4SMichael Hennerich 	}
197ed4a10b4SMichael Hennerich 
198ed4a10b4SMichael Hennerich 	return 0;
199ed4a10b4SMichael Hennerich }
200ed4a10b4SMichael Hennerich 
201ed4a10b4SMichael Hennerich static struct platform_driver adp5520_led_driver = {
202ed4a10b4SMichael Hennerich 	.driver	= {
203ed4a10b4SMichael Hennerich 		.name	= "adp5520-led",
204ed4a10b4SMichael Hennerich 		.owner	= THIS_MODULE,
205ed4a10b4SMichael Hennerich 	},
206ed4a10b4SMichael Hennerich 	.probe		= adp5520_led_probe,
207df07cf81SBill Pemberton 	.remove		= adp5520_led_remove,
208ed4a10b4SMichael Hennerich };
209ed4a10b4SMichael Hennerich 
210892a8843SAxel Lin module_platform_driver(adp5520_led_driver);
211ed4a10b4SMichael Hennerich 
212ed4a10b4SMichael Hennerich MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
213ed4a10b4SMichael Hennerich MODULE_DESCRIPTION("LEDS ADP5520(01) Driver");
214ed4a10b4SMichael Hennerich MODULE_LICENSE("GPL");
215ed4a10b4SMichael Hennerich MODULE_ALIAS("platform:adp5520-led");
216