xref: /linux/drivers/leds/leds-tlc591xx.c (revision 5b4b723c483f5027da77409f34b8b4602ee1e557)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e370d010SAndrew Lunn /*
3e370d010SAndrew Lunn  * Copyright 2014 Belkin Inc.
4e370d010SAndrew Lunn  * Copyright 2015 Andrew Lunn <andrew@lunn.ch>
5e370d010SAndrew Lunn  */
6e370d010SAndrew Lunn 
7e370d010SAndrew Lunn #include <linux/i2c.h>
8e370d010SAndrew Lunn #include <linux/leds.h>
9e370d010SAndrew Lunn #include <linux/module.h>
10e370d010SAndrew Lunn #include <linux/of.h>
11e370d010SAndrew Lunn #include <linux/of_device.h>
12e370d010SAndrew Lunn #include <linux/regmap.h>
13e370d010SAndrew Lunn #include <linux/slab.h>
14e370d010SAndrew Lunn 
15e370d010SAndrew Lunn #define TLC591XX_MAX_LEDS	16
16e370d010SAndrew Lunn 
17e370d010SAndrew Lunn #define TLC591XX_REG_MODE1	0x00
18e370d010SAndrew Lunn #define MODE1_RESPON_ADDR_MASK	0xF0
19e370d010SAndrew Lunn #define MODE1_NORMAL_MODE	(0 << 4)
20e370d010SAndrew Lunn #define MODE1_SPEED_MODE	(1 << 4)
21e370d010SAndrew Lunn 
22e370d010SAndrew Lunn #define TLC591XX_REG_MODE2	0x01
23e370d010SAndrew Lunn #define MODE2_DIM		(0 << 5)
24e370d010SAndrew Lunn #define MODE2_BLINK		(1 << 5)
25e370d010SAndrew Lunn #define MODE2_OCH_STOP		(0 << 3)
26e370d010SAndrew Lunn #define MODE2_OCH_ACK		(1 << 3)
27e370d010SAndrew Lunn 
28e370d010SAndrew Lunn #define TLC591XX_REG_PWM(x)	(0x02 + (x))
29e370d010SAndrew Lunn 
30e370d010SAndrew Lunn #define TLC591XX_REG_GRPPWM	0x12
31e370d010SAndrew Lunn #define TLC591XX_REG_GRPFREQ	0x13
32e370d010SAndrew Lunn 
33e370d010SAndrew Lunn /* LED Driver Output State, determine the source that drives LED outputs */
34e370d010SAndrew Lunn #define LEDOUT_OFF		0x0	/* Output LOW */
35e370d010SAndrew Lunn #define LEDOUT_ON		0x1	/* Output HI-Z */
36e370d010SAndrew Lunn #define LEDOUT_DIM		0x2	/* Dimming */
37e370d010SAndrew Lunn #define LEDOUT_BLINK		0x3	/* Blinking */
38e370d010SAndrew Lunn #define LEDOUT_MASK		0x3
39e370d010SAndrew Lunn 
40e370d010SAndrew Lunn #define ldev_to_led(c)		container_of(c, struct tlc591xx_led, ldev)
41e370d010SAndrew Lunn 
42e370d010SAndrew Lunn struct tlc591xx_led {
43e370d010SAndrew Lunn 	bool active;
44e370d010SAndrew Lunn 	unsigned int led_no;
45e370d010SAndrew Lunn 	struct led_classdev ldev;
46e370d010SAndrew Lunn 	struct tlc591xx_priv *priv;
47e370d010SAndrew Lunn };
48e370d010SAndrew Lunn 
49e370d010SAndrew Lunn struct tlc591xx_priv {
50e370d010SAndrew Lunn 	struct tlc591xx_led leds[TLC591XX_MAX_LEDS];
51e370d010SAndrew Lunn 	struct regmap *regmap;
52e370d010SAndrew Lunn 	unsigned int reg_ledout_offset;
53e370d010SAndrew Lunn };
54e370d010SAndrew Lunn 
55e370d010SAndrew Lunn struct tlc591xx {
56e370d010SAndrew Lunn 	unsigned int max_leds;
57e370d010SAndrew Lunn 	unsigned int reg_ledout_offset;
58e370d010SAndrew Lunn };
59e370d010SAndrew Lunn 
60e370d010SAndrew Lunn static const struct tlc591xx tlc59116 = {
61e370d010SAndrew Lunn 	.max_leds = 16,
62e370d010SAndrew Lunn 	.reg_ledout_offset = 0x14,
63e370d010SAndrew Lunn };
64e370d010SAndrew Lunn 
65e370d010SAndrew Lunn static const struct tlc591xx tlc59108 = {
66e370d010SAndrew Lunn 	.max_leds = 8,
67e370d010SAndrew Lunn 	.reg_ledout_offset = 0x0c,
68e370d010SAndrew Lunn };
69e370d010SAndrew Lunn 
70e370d010SAndrew Lunn static int
71e370d010SAndrew Lunn tlc591xx_set_mode(struct regmap *regmap, u8 mode)
72e370d010SAndrew Lunn {
73e370d010SAndrew Lunn 	int err;
74e370d010SAndrew Lunn 	u8 val;
75e370d010SAndrew Lunn 
76e370d010SAndrew Lunn 	err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE);
77e370d010SAndrew Lunn 	if (err)
78e370d010SAndrew Lunn 		return err;
79e370d010SAndrew Lunn 
80e370d010SAndrew Lunn 	val = MODE2_OCH_STOP | mode;
81e370d010SAndrew Lunn 
82e370d010SAndrew Lunn 	return regmap_write(regmap, TLC591XX_REG_MODE2, val);
83e370d010SAndrew Lunn }
84e370d010SAndrew Lunn 
85e370d010SAndrew Lunn static int
86e370d010SAndrew Lunn tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
87e370d010SAndrew Lunn 		    u8 val)
88e370d010SAndrew Lunn {
89e370d010SAndrew Lunn 	unsigned int i = (led->led_no % 4) * 2;
90e370d010SAndrew Lunn 	unsigned int mask = LEDOUT_MASK << i;
91e370d010SAndrew Lunn 	unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2);
92e370d010SAndrew Lunn 
93e370d010SAndrew Lunn 	val = val << i;
94e370d010SAndrew Lunn 
95e370d010SAndrew Lunn 	return regmap_update_bits(priv->regmap, addr, mask, val);
96e370d010SAndrew Lunn }
97e370d010SAndrew Lunn 
98e370d010SAndrew Lunn static int
99e370d010SAndrew Lunn tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
100e370d010SAndrew Lunn 		 u8 brightness)
101e370d010SAndrew Lunn {
102e370d010SAndrew Lunn 	u8 pwm = TLC591XX_REG_PWM(led->led_no);
103e370d010SAndrew Lunn 
104e370d010SAndrew Lunn 	return regmap_write(priv->regmap, pwm, brightness);
105e370d010SAndrew Lunn }
106e370d010SAndrew Lunn 
107d890389fSAndrew Lunn static int
108d890389fSAndrew Lunn tlc591xx_brightness_set(struct led_classdev *led_cdev,
109d890389fSAndrew Lunn 			enum led_brightness brightness)
110e370d010SAndrew Lunn {
111d890389fSAndrew Lunn 	struct tlc591xx_led *led = ldev_to_led(led_cdev);
112e370d010SAndrew Lunn 	struct tlc591xx_priv *priv = led->priv;
113e370d010SAndrew Lunn 	int err;
114e370d010SAndrew Lunn 
115e370d010SAndrew Lunn 	switch (brightness) {
116e370d010SAndrew Lunn 	case 0:
117e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF);
118e370d010SAndrew Lunn 		break;
119e370d010SAndrew Lunn 	case LED_FULL:
120e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_ON);
121e370d010SAndrew Lunn 		break;
122e370d010SAndrew Lunn 	default:
123e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM);
124e370d010SAndrew Lunn 		if (!err)
125e370d010SAndrew Lunn 			err = tlc591xx_set_pwm(priv, led, brightness);
126e370d010SAndrew Lunn 	}
127e370d010SAndrew Lunn 
128d890389fSAndrew Lunn 	return err;
129e370d010SAndrew Lunn }
130e370d010SAndrew Lunn 
131e370d010SAndrew Lunn static const struct regmap_config tlc591xx_regmap = {
132e370d010SAndrew Lunn 	.reg_bits = 8,
133e370d010SAndrew Lunn 	.val_bits = 8,
134e370d010SAndrew Lunn 	.max_register = 0x1e,
135e370d010SAndrew Lunn };
136e370d010SAndrew Lunn 
137e370d010SAndrew Lunn static const struct of_device_id of_tlc591xx_leds_match[] = {
138e370d010SAndrew Lunn 	{ .compatible = "ti,tlc59116",
139e370d010SAndrew Lunn 	  .data = &tlc59116 },
140e370d010SAndrew Lunn 	{ .compatible = "ti,tlc59108",
141e370d010SAndrew Lunn 	  .data = &tlc59108 },
142e370d010SAndrew Lunn 	{},
143e370d010SAndrew Lunn };
144e370d010SAndrew Lunn MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match);
145e370d010SAndrew Lunn 
146e370d010SAndrew Lunn static int
147e370d010SAndrew Lunn tlc591xx_probe(struct i2c_client *client,
148e370d010SAndrew Lunn 	       const struct i2c_device_id *id)
149e370d010SAndrew Lunn {
150e370d010SAndrew Lunn 	struct device_node *np = client->dev.of_node, *child;
151e370d010SAndrew Lunn 	struct device *dev = &client->dev;
152e370d010SAndrew Lunn 	const struct of_device_id *match;
153e370d010SAndrew Lunn 	const struct tlc591xx *tlc591xx;
154e370d010SAndrew Lunn 	struct tlc591xx_priv *priv;
155e370d010SAndrew Lunn 	int err, count, reg;
156e370d010SAndrew Lunn 
157e370d010SAndrew Lunn 	match = of_match_device(of_tlc591xx_leds_match, dev);
158e370d010SAndrew Lunn 	if (!match)
159e370d010SAndrew Lunn 		return -ENODEV;
160e370d010SAndrew Lunn 
161e370d010SAndrew Lunn 	tlc591xx = match->data;
162e370d010SAndrew Lunn 	if (!np)
163e370d010SAndrew Lunn 		return -ENODEV;
164e370d010SAndrew Lunn 
165e370d010SAndrew Lunn 	count = of_get_child_count(np);
166e370d010SAndrew Lunn 	if (!count || count > tlc591xx->max_leds)
167e370d010SAndrew Lunn 		return -EINVAL;
168e370d010SAndrew Lunn 
169e370d010SAndrew Lunn 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
170e370d010SAndrew Lunn 	if (!priv)
171e370d010SAndrew Lunn 		return -ENOMEM;
172e370d010SAndrew Lunn 
173e370d010SAndrew Lunn 	priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap);
174e370d010SAndrew Lunn 	if (IS_ERR(priv->regmap)) {
175e370d010SAndrew Lunn 		err = PTR_ERR(priv->regmap);
176e370d010SAndrew Lunn 		dev_err(dev, "Failed to allocate register map: %d\n", err);
177e370d010SAndrew Lunn 		return err;
178e370d010SAndrew Lunn 	}
179e370d010SAndrew Lunn 	priv->reg_ledout_offset = tlc591xx->reg_ledout_offset;
180e370d010SAndrew Lunn 
181e370d010SAndrew Lunn 	i2c_set_clientdata(client, priv);
182e370d010SAndrew Lunn 
1831ab4531aSJean-Jacques Hiblot 	err = tlc591xx_set_mode(priv->regmap, MODE2_DIM);
1841ab4531aSJean-Jacques Hiblot 	if (err < 0)
1851ab4531aSJean-Jacques Hiblot 		return err;
1861ab4531aSJean-Jacques Hiblot 
187e370d010SAndrew Lunn 	for_each_child_of_node(np, child) {
1881ab4531aSJean-Jacques Hiblot 		struct tlc591xx_led *led;
189*5b4b723cSJean-Jacques Hiblot 		struct led_init_data init_data = {};
190*5b4b723cSJean-Jacques Hiblot 
191*5b4b723cSJean-Jacques Hiblot 		init_data.fwnode = of_fwnode_handle(child);
1921ab4531aSJean-Jacques Hiblot 
193e370d010SAndrew Lunn 		err = of_property_read_u32(child, "reg", &reg);
194c6872911SJulia Lawall 		if (err) {
195c6872911SJulia Lawall 			of_node_put(child);
196e370d010SAndrew Lunn 			return err;
197c6872911SJulia Lawall 		}
1981055790bSJulia Lawall 		if (reg < 0 || reg >= tlc591xx->max_leds ||
199c6872911SJulia Lawall 		    priv->leds[reg].active) {
200c6872911SJulia Lawall 			of_node_put(child);
201e370d010SAndrew Lunn 			return -EINVAL;
202c6872911SJulia Lawall 		}
2031ab4531aSJean-Jacques Hiblot 		led = &priv->leds[reg];
2041ab4531aSJean-Jacques Hiblot 
2051ab4531aSJean-Jacques Hiblot 		led->active = true;
2061ab4531aSJean-Jacques Hiblot 		led->ldev.default_trigger =
207e370d010SAndrew Lunn 			of_get_property(child, "linux,default-trigger", NULL);
2081ab4531aSJean-Jacques Hiblot 
2091ab4531aSJean-Jacques Hiblot 		led->priv = priv;
2101ab4531aSJean-Jacques Hiblot 		led->led_no = reg;
2111ab4531aSJean-Jacques Hiblot 		led->ldev.brightness_set_blocking = tlc591xx_brightness_set;
2121ab4531aSJean-Jacques Hiblot 		led->ldev.max_brightness = LED_FULL;
213*5b4b723cSJean-Jacques Hiblot 		err = devm_led_classdev_register_ext(dev, &led->ldev,
214*5b4b723cSJean-Jacques Hiblot 						     &init_data);
2151ab4531aSJean-Jacques Hiblot 		if (err < 0) {
2161ab4531aSJean-Jacques Hiblot 			dev_err(dev, "couldn't register LED %s\n",
2171ab4531aSJean-Jacques Hiblot 				led->ldev.name);
2181ab4531aSJean-Jacques Hiblot 			return err;
219e370d010SAndrew Lunn 		}
220e370d010SAndrew Lunn 	}
221e370d010SAndrew Lunn 	return 0;
222e370d010SAndrew Lunn }
223e370d010SAndrew Lunn 
224e370d010SAndrew Lunn static const struct i2c_device_id tlc591xx_id[] = {
225e370d010SAndrew Lunn 	{ "tlc59116" },
226e370d010SAndrew Lunn 	{ "tlc59108" },
227e370d010SAndrew Lunn 	{},
228e370d010SAndrew Lunn };
229e370d010SAndrew Lunn MODULE_DEVICE_TABLE(i2c, tlc591xx_id);
230e370d010SAndrew Lunn 
231e370d010SAndrew Lunn static struct i2c_driver tlc591xx_driver = {
232e370d010SAndrew Lunn 	.driver = {
233e370d010SAndrew Lunn 		.name = "tlc591xx",
234e370d010SAndrew Lunn 		.of_match_table = of_match_ptr(of_tlc591xx_leds_match),
235e370d010SAndrew Lunn 	},
236e370d010SAndrew Lunn 	.probe = tlc591xx_probe,
237e370d010SAndrew Lunn 	.id_table = tlc591xx_id,
238e370d010SAndrew Lunn };
239e370d010SAndrew Lunn 
240e370d010SAndrew Lunn module_i2c_driver(tlc591xx_driver);
241e370d010SAndrew Lunn 
242e370d010SAndrew Lunn MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
243e370d010SAndrew Lunn MODULE_LICENSE("GPL");
244e370d010SAndrew Lunn MODULE_DESCRIPTION("TLC591XX LED driver");
245