xref: /openbmc/linux/drivers/leds/leds-tlc591xx.c (revision ca55b2fe)
1 /*
2  * Copyright 2014 Belkin Inc.
3  * Copyright 2015 Andrew Lunn <andrew@lunn.ch>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/leds.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_device.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
17 #include <linux/workqueue.h>
18 
19 #define TLC591XX_MAX_LEDS	16
20 
21 #define TLC591XX_REG_MODE1	0x00
22 #define MODE1_RESPON_ADDR_MASK	0xF0
23 #define MODE1_NORMAL_MODE	(0 << 4)
24 #define MODE1_SPEED_MODE	(1 << 4)
25 
26 #define TLC591XX_REG_MODE2	0x01
27 #define MODE2_DIM		(0 << 5)
28 #define MODE2_BLINK		(1 << 5)
29 #define MODE2_OCH_STOP		(0 << 3)
30 #define MODE2_OCH_ACK		(1 << 3)
31 
32 #define TLC591XX_REG_PWM(x)	(0x02 + (x))
33 
34 #define TLC591XX_REG_GRPPWM	0x12
35 #define TLC591XX_REG_GRPFREQ	0x13
36 
37 /* LED Driver Output State, determine the source that drives LED outputs */
38 #define LEDOUT_OFF		0x0	/* Output LOW */
39 #define LEDOUT_ON		0x1	/* Output HI-Z */
40 #define LEDOUT_DIM		0x2	/* Dimming */
41 #define LEDOUT_BLINK		0x3	/* Blinking */
42 #define LEDOUT_MASK		0x3
43 
44 #define ldev_to_led(c)		container_of(c, struct tlc591xx_led, ldev)
45 #define work_to_led(work)	container_of(work, struct tlc591xx_led, work)
46 
47 struct tlc591xx_led {
48 	bool active;
49 	unsigned int led_no;
50 	struct led_classdev ldev;
51 	struct work_struct work;
52 	struct tlc591xx_priv *priv;
53 };
54 
55 struct tlc591xx_priv {
56 	struct tlc591xx_led leds[TLC591XX_MAX_LEDS];
57 	struct regmap *regmap;
58 	unsigned int reg_ledout_offset;
59 };
60 
61 struct tlc591xx {
62 	unsigned int max_leds;
63 	unsigned int reg_ledout_offset;
64 };
65 
66 static const struct tlc591xx tlc59116 = {
67 	.max_leds = 16,
68 	.reg_ledout_offset = 0x14,
69 };
70 
71 static const struct tlc591xx tlc59108 = {
72 	.max_leds = 8,
73 	.reg_ledout_offset = 0x0c,
74 };
75 
76 static int
77 tlc591xx_set_mode(struct regmap *regmap, u8 mode)
78 {
79 	int err;
80 	u8 val;
81 
82 	err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE);
83 	if (err)
84 		return err;
85 
86 	val = MODE2_OCH_STOP | mode;
87 
88 	return regmap_write(regmap, TLC591XX_REG_MODE2, val);
89 }
90 
91 static int
92 tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
93 		    u8 val)
94 {
95 	unsigned int i = (led->led_no % 4) * 2;
96 	unsigned int mask = LEDOUT_MASK << i;
97 	unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2);
98 
99 	val = val << i;
100 
101 	return regmap_update_bits(priv->regmap, addr, mask, val);
102 }
103 
104 static int
105 tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
106 		 u8 brightness)
107 {
108 	u8 pwm = TLC591XX_REG_PWM(led->led_no);
109 
110 	return regmap_write(priv->regmap, pwm, brightness);
111 }
112 
113 static void
114 tlc591xx_led_work(struct work_struct *work)
115 {
116 	struct tlc591xx_led *led = work_to_led(work);
117 	struct tlc591xx_priv *priv = led->priv;
118 	enum led_brightness brightness = led->ldev.brightness;
119 	int err;
120 
121 	switch (brightness) {
122 	case 0:
123 		err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF);
124 		break;
125 	case LED_FULL:
126 		err = tlc591xx_set_ledout(priv, led, LEDOUT_ON);
127 		break;
128 	default:
129 		err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM);
130 		if (!err)
131 			err = tlc591xx_set_pwm(priv, led, brightness);
132 	}
133 
134 	if (err)
135 		dev_err(led->ldev.dev, "Failed setting brightness\n");
136 }
137 
138 static void
139 tlc591xx_brightness_set(struct led_classdev *led_cdev,
140 			enum led_brightness brightness)
141 {
142 	struct tlc591xx_led *led = ldev_to_led(led_cdev);
143 
144 	led->ldev.brightness = brightness;
145 	schedule_work(&led->work);
146 }
147 
148 static void
149 tlc591xx_destroy_devices(struct tlc591xx_priv *priv, unsigned int j)
150 {
151 	int i = j;
152 
153 	while (--i >= 0) {
154 		if (priv->leds[i].active) {
155 			led_classdev_unregister(&priv->leds[i].ldev);
156 			cancel_work_sync(&priv->leds[i].work);
157 		}
158 	}
159 }
160 
161 static int
162 tlc591xx_configure(struct device *dev,
163 		   struct tlc591xx_priv *priv,
164 		   const struct tlc591xx *tlc591xx)
165 {
166 	unsigned int i;
167 	int err = 0;
168 
169 	tlc591xx_set_mode(priv->regmap, MODE2_DIM);
170 	for (i = 0; i < TLC591XX_MAX_LEDS; i++) {
171 		struct tlc591xx_led *led = &priv->leds[i];
172 
173 		if (!led->active)
174 			continue;
175 
176 		led->priv = priv;
177 		led->led_no = i;
178 		led->ldev.brightness_set = tlc591xx_brightness_set;
179 		led->ldev.max_brightness = LED_FULL;
180 		INIT_WORK(&led->work, tlc591xx_led_work);
181 		err = led_classdev_register(dev, &led->ldev);
182 		if (err < 0) {
183 			dev_err(dev, "couldn't register LED %s\n",
184 				led->ldev.name);
185 			goto exit;
186 		}
187 	}
188 
189 	return 0;
190 
191 exit:
192 	tlc591xx_destroy_devices(priv, i);
193 	return err;
194 }
195 
196 static const struct regmap_config tlc591xx_regmap = {
197 	.reg_bits = 8,
198 	.val_bits = 8,
199 	.max_register = 0x1e,
200 };
201 
202 static const struct of_device_id of_tlc591xx_leds_match[] = {
203 	{ .compatible = "ti,tlc59116",
204 	  .data = &tlc59116 },
205 	{ .compatible = "ti,tlc59108",
206 	  .data = &tlc59108 },
207 	{},
208 };
209 MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match);
210 
211 static int
212 tlc591xx_probe(struct i2c_client *client,
213 	       const struct i2c_device_id *id)
214 {
215 	struct device_node *np = client->dev.of_node, *child;
216 	struct device *dev = &client->dev;
217 	const struct of_device_id *match;
218 	const struct tlc591xx *tlc591xx;
219 	struct tlc591xx_priv *priv;
220 	int err, count, reg;
221 
222 	match = of_match_device(of_tlc591xx_leds_match, dev);
223 	if (!match)
224 		return -ENODEV;
225 
226 	tlc591xx = match->data;
227 	if (!np)
228 		return -ENODEV;
229 
230 	count = of_get_child_count(np);
231 	if (!count || count > tlc591xx->max_leds)
232 		return -EINVAL;
233 
234 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
235 	if (!priv)
236 		return -ENOMEM;
237 
238 	priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap);
239 	if (IS_ERR(priv->regmap)) {
240 		err = PTR_ERR(priv->regmap);
241 		dev_err(dev, "Failed to allocate register map: %d\n", err);
242 		return err;
243 	}
244 	priv->reg_ledout_offset = tlc591xx->reg_ledout_offset;
245 
246 	i2c_set_clientdata(client, priv);
247 
248 	for_each_child_of_node(np, child) {
249 		err = of_property_read_u32(child, "reg", &reg);
250 		if (err)
251 			return err;
252 		if (reg < 0 || reg >= tlc591xx->max_leds)
253 			return -EINVAL;
254 		if (priv->leds[reg].active)
255 			return -EINVAL;
256 		priv->leds[reg].active = true;
257 		priv->leds[reg].ldev.name =
258 			of_get_property(child, "label", NULL) ? : child->name;
259 		priv->leds[reg].ldev.default_trigger =
260 			of_get_property(child, "linux,default-trigger", NULL);
261 	}
262 	return tlc591xx_configure(dev, priv, tlc591xx);
263 }
264 
265 static int
266 tlc591xx_remove(struct i2c_client *client)
267 {
268 	struct tlc591xx_priv *priv = i2c_get_clientdata(client);
269 
270 	tlc591xx_destroy_devices(priv, TLC591XX_MAX_LEDS);
271 
272 	return 0;
273 }
274 
275 static const struct i2c_device_id tlc591xx_id[] = {
276 	{ "tlc59116" },
277 	{ "tlc59108" },
278 	{},
279 };
280 MODULE_DEVICE_TABLE(i2c, tlc591xx_id);
281 
282 static struct i2c_driver tlc591xx_driver = {
283 	.driver = {
284 		.name = "tlc591xx",
285 		.of_match_table = of_match_ptr(of_tlc591xx_leds_match),
286 	},
287 	.probe = tlc591xx_probe,
288 	.remove = tlc591xx_remove,
289 	.id_table = tlc591xx_id,
290 };
291 
292 module_i2c_driver(tlc591xx_driver);
293 
294 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
295 MODULE_LICENSE("GPL");
296 MODULE_DESCRIPTION("TLC591XX LED driver");
297