xref: /openbmc/linux/drivers/leds/leds-88pm860x.c (revision ccb01374)
1 /*
2  * LED driver for Marvell 88PM860x
3  *
4  * Copyright (C) 2009 Marvell International Ltd.
5  *	Haojian Zhuang <haojian.zhuang@marvell.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/of.h>
15 #include <linux/platform_device.h>
16 #include <linux/i2c.h>
17 #include <linux/leds.h>
18 #include <linux/slab.h>
19 #include <linux/mfd/88pm860x.h>
20 #include <linux/module.h>
21 
22 #define LED_PWM_MASK		(0x1F)
23 #define LED_CURRENT_MASK	(0x07 << 5)
24 
25 #define LED_BLINK_MASK		(0x7F)
26 
27 #define LED_ON_CONTINUOUS	(0x0F << 3)
28 
29 #define LED1_BLINK_EN		(1 << 1)
30 #define LED2_BLINK_EN		(1 << 2)
31 
32 struct pm860x_led {
33 	struct led_classdev cdev;
34 	struct i2c_client *i2c;
35 	struct pm860x_chip *chip;
36 	struct mutex lock;
37 	char name[MFD_NAME_SIZE];
38 
39 	int port;
40 	int iset;
41 	unsigned char brightness;
42 	unsigned char current_brightness;
43 
44 	int reg_control;
45 	int reg_blink;
46 	int blink_mask;
47 };
48 
49 static int led_power_set(struct pm860x_chip *chip, int port, int on)
50 {
51 	int ret = -EINVAL;
52 
53 	switch (port) {
54 	case 0:
55 	case 1:
56 	case 2:
57 		ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
58 			pm8606_osc_disable(chip, RGB1_ENABLE);
59 		break;
60 	case 3:
61 	case 4:
62 	case 5:
63 		ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
64 			pm8606_osc_disable(chip, RGB2_ENABLE);
65 		break;
66 	}
67 	return ret;
68 }
69 
70 static int pm860x_led_set(struct led_classdev *cdev,
71 			   enum led_brightness value)
72 {
73 	struct pm860x_led *led = container_of(cdev, struct pm860x_led, cdev);
74 	struct pm860x_chip *chip;
75 	unsigned char buf[3];
76 	int ret;
77 
78 	chip = led->chip;
79 	mutex_lock(&led->lock);
80 	led->brightness = value >> 3;
81 
82 	if ((led->current_brightness == 0) && led->brightness) {
83 		led_power_set(chip, led->port, 1);
84 		if (led->iset) {
85 			pm860x_set_bits(led->i2c, led->reg_control,
86 					LED_CURRENT_MASK, led->iset);
87 		}
88 		pm860x_set_bits(led->i2c, led->reg_blink,
89 				LED_BLINK_MASK, LED_ON_CONTINUOUS);
90 		pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
91 				led->blink_mask);
92 	}
93 	pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
94 			led->brightness);
95 
96 	if (led->brightness == 0) {
97 		pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
98 		ret = buf[0] & LED_PWM_MASK;
99 		ret |= buf[1] & LED_PWM_MASK;
100 		ret |= buf[2] & LED_PWM_MASK;
101 		if (ret == 0) {
102 			/* unset current since no led is lighting */
103 			pm860x_set_bits(led->i2c, led->reg_control,
104 					LED_CURRENT_MASK, 0);
105 			pm860x_set_bits(led->i2c, PM8606_WLED3B,
106 					led->blink_mask, 0);
107 			led_power_set(chip, led->port, 0);
108 		}
109 	}
110 	led->current_brightness = led->brightness;
111 	dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
112 		led->reg_control, led->brightness);
113 	mutex_unlock(&led->lock);
114 
115 	return 0;
116 }
117 
118 #ifdef CONFIG_OF
119 static int pm860x_led_dt_init(struct platform_device *pdev,
120 			      struct pm860x_led *data)
121 {
122 	struct device_node *nproot, *np;
123 	int iset = 0;
124 
125 	if (!pdev->dev.parent->of_node)
126 		return -ENODEV;
127 	nproot = of_get_child_by_name(pdev->dev.parent->of_node, "leds");
128 	if (!nproot) {
129 		dev_err(&pdev->dev, "failed to find leds node\n");
130 		return -ENODEV;
131 	}
132 	for_each_child_of_node(nproot, np) {
133 		if (of_node_name_eq(np, data->name)) {
134 			of_property_read_u32(np, "marvell,88pm860x-iset",
135 					     &iset);
136 			data->iset = PM8606_LED_CURRENT(iset);
137 			of_node_put(np);
138 			break;
139 		}
140 	}
141 	of_node_put(nproot);
142 	return 0;
143 }
144 #else
145 #define pm860x_led_dt_init(x, y)	(-1)
146 #endif
147 
148 static int pm860x_led_probe(struct platform_device *pdev)
149 {
150 	struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
151 	struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev);
152 	struct pm860x_led *data;
153 	struct resource *res;
154 	int ret = 0;
155 
156 	data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
157 	if (data == NULL)
158 		return -ENOMEM;
159 	res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
160 	if (!res) {
161 		dev_err(&pdev->dev, "No REG resource for control\n");
162 		return -ENXIO;
163 	}
164 	data->reg_control = res->start;
165 	res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink");
166 	if (!res) {
167 		dev_err(&pdev->dev, "No REG resource for blink\n");
168 		return -ENXIO;
169 	}
170 	data->reg_blink = res->start;
171 	memset(data->name, 0, MFD_NAME_SIZE);
172 	switch (pdev->id) {
173 	case 0:
174 		data->blink_mask = LED1_BLINK_EN;
175 		sprintf(data->name, "led0-red");
176 		break;
177 	case 1:
178 		data->blink_mask = LED1_BLINK_EN;
179 		sprintf(data->name, "led0-green");
180 		break;
181 	case 2:
182 		data->blink_mask = LED1_BLINK_EN;
183 		sprintf(data->name, "led0-blue");
184 		break;
185 	case 3:
186 		data->blink_mask = LED2_BLINK_EN;
187 		sprintf(data->name, "led1-red");
188 		break;
189 	case 4:
190 		data->blink_mask = LED2_BLINK_EN;
191 		sprintf(data->name, "led1-green");
192 		break;
193 	case 5:
194 		data->blink_mask = LED2_BLINK_EN;
195 		sprintf(data->name, "led1-blue");
196 		break;
197 	}
198 	data->chip = chip;
199 	data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
200 	data->port = pdev->id;
201 	if (pm860x_led_dt_init(pdev, data))
202 		if (pdata)
203 			data->iset = pdata->iset;
204 
205 	data->current_brightness = 0;
206 	data->cdev.name = data->name;
207 	data->cdev.brightness_set_blocking = pm860x_led_set;
208 	mutex_init(&data->lock);
209 
210 	ret = devm_led_classdev_register(chip->dev, &data->cdev);
211 	if (ret < 0) {
212 		dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
213 		return ret;
214 	}
215 	pm860x_led_set(&data->cdev, 0);
216 	return 0;
217 }
218 
219 
220 static struct platform_driver pm860x_led_driver = {
221 	.driver	= {
222 		.name	= "88pm860x-led",
223 	},
224 	.probe	= pm860x_led_probe,
225 };
226 
227 module_platform_driver(pm860x_led_driver);
228 
229 MODULE_DESCRIPTION("LED driver for Marvell PM860x");
230 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
231 MODULE_LICENSE("GPL");
232 MODULE_ALIAS("platform:88pm860x-led");
233