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