xref: /linux/drivers/leds/leds-tlc591xx.c (revision 99a013c840a05083fd82d220685af7579238bfa8)
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
16a2cafdfdSJean-Jacques Hiblot #define TLC591XX_MAX_BRIGHTNESS	256
17e370d010SAndrew Lunn 
18e370d010SAndrew Lunn #define TLC591XX_REG_MODE1	0x00
19e370d010SAndrew Lunn #define MODE1_RESPON_ADDR_MASK	0xF0
20e370d010SAndrew Lunn #define MODE1_NORMAL_MODE	(0 << 4)
21e370d010SAndrew Lunn #define MODE1_SPEED_MODE	(1 << 4)
22e370d010SAndrew Lunn 
23e370d010SAndrew Lunn #define TLC591XX_REG_MODE2	0x01
24e370d010SAndrew Lunn #define MODE2_DIM		(0 << 5)
25e370d010SAndrew Lunn #define MODE2_BLINK		(1 << 5)
26e370d010SAndrew Lunn #define MODE2_OCH_STOP		(0 << 3)
27e370d010SAndrew Lunn #define MODE2_OCH_ACK		(1 << 3)
28e370d010SAndrew Lunn 
29e370d010SAndrew Lunn #define TLC591XX_REG_PWM(x)	(0x02 + (x))
30e370d010SAndrew Lunn 
31e370d010SAndrew Lunn #define TLC591XX_REG_GRPPWM	0x12
32e370d010SAndrew Lunn #define TLC591XX_REG_GRPFREQ	0x13
33e370d010SAndrew Lunn 
34e370d010SAndrew Lunn /* LED Driver Output State, determine the source that drives LED outputs */
35e370d010SAndrew Lunn #define LEDOUT_OFF		0x0	/* Output LOW */
36e370d010SAndrew Lunn #define LEDOUT_ON		0x1	/* Output HI-Z */
37e370d010SAndrew Lunn #define LEDOUT_DIM		0x2	/* Dimming */
38e370d010SAndrew Lunn #define LEDOUT_BLINK		0x3	/* Blinking */
39e370d010SAndrew Lunn #define LEDOUT_MASK		0x3
40e370d010SAndrew Lunn 
41e370d010SAndrew Lunn #define ldev_to_led(c)		container_of(c, struct tlc591xx_led, ldev)
42e370d010SAndrew Lunn 
43e370d010SAndrew Lunn struct tlc591xx_led {
44e370d010SAndrew Lunn 	bool active;
45e370d010SAndrew Lunn 	unsigned int led_no;
46e370d010SAndrew Lunn 	struct led_classdev ldev;
47e370d010SAndrew Lunn 	struct tlc591xx_priv *priv;
48e370d010SAndrew Lunn };
49e370d010SAndrew Lunn 
50e370d010SAndrew Lunn struct tlc591xx_priv {
51e370d010SAndrew Lunn 	struct tlc591xx_led leds[TLC591XX_MAX_LEDS];
52e370d010SAndrew Lunn 	struct regmap *regmap;
53e370d010SAndrew Lunn 	unsigned int reg_ledout_offset;
54e370d010SAndrew Lunn };
55e370d010SAndrew Lunn 
56e370d010SAndrew Lunn struct tlc591xx {
57e370d010SAndrew Lunn 	unsigned int max_leds;
58e370d010SAndrew Lunn 	unsigned int reg_ledout_offset;
59e370d010SAndrew Lunn };
60e370d010SAndrew Lunn 
61e370d010SAndrew Lunn static const struct tlc591xx tlc59116 = {
62e370d010SAndrew Lunn 	.max_leds = 16,
63e370d010SAndrew Lunn 	.reg_ledout_offset = 0x14,
64e370d010SAndrew Lunn };
65e370d010SAndrew Lunn 
66e370d010SAndrew Lunn static const struct tlc591xx tlc59108 = {
67e370d010SAndrew Lunn 	.max_leds = 8,
68e370d010SAndrew Lunn 	.reg_ledout_offset = 0x0c,
69e370d010SAndrew Lunn };
70e370d010SAndrew Lunn 
71e370d010SAndrew Lunn static int
72e370d010SAndrew Lunn tlc591xx_set_mode(struct regmap *regmap, u8 mode)
73e370d010SAndrew Lunn {
74e370d010SAndrew Lunn 	int err;
75e370d010SAndrew Lunn 	u8 val;
76e370d010SAndrew Lunn 
77e370d010SAndrew Lunn 	err = regmap_write(regmap, TLC591XX_REG_MODE1, MODE1_NORMAL_MODE);
78e370d010SAndrew Lunn 	if (err)
79e370d010SAndrew Lunn 		return err;
80e370d010SAndrew Lunn 
81e370d010SAndrew Lunn 	val = MODE2_OCH_STOP | mode;
82e370d010SAndrew Lunn 
83e370d010SAndrew Lunn 	return regmap_write(regmap, TLC591XX_REG_MODE2, val);
84e370d010SAndrew Lunn }
85e370d010SAndrew Lunn 
86e370d010SAndrew Lunn static int
87e370d010SAndrew Lunn tlc591xx_set_ledout(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
88e370d010SAndrew Lunn 		    u8 val)
89e370d010SAndrew Lunn {
90e370d010SAndrew Lunn 	unsigned int i = (led->led_no % 4) * 2;
91e370d010SAndrew Lunn 	unsigned int mask = LEDOUT_MASK << i;
92e370d010SAndrew Lunn 	unsigned int addr = priv->reg_ledout_offset + (led->led_no >> 2);
93e370d010SAndrew Lunn 
94e370d010SAndrew Lunn 	val = val << i;
95e370d010SAndrew Lunn 
96e370d010SAndrew Lunn 	return regmap_update_bits(priv->regmap, addr, mask, val);
97e370d010SAndrew Lunn }
98e370d010SAndrew Lunn 
99e370d010SAndrew Lunn static int
100e370d010SAndrew Lunn tlc591xx_set_pwm(struct tlc591xx_priv *priv, struct tlc591xx_led *led,
101e370d010SAndrew Lunn 		 u8 brightness)
102e370d010SAndrew Lunn {
103e370d010SAndrew Lunn 	u8 pwm = TLC591XX_REG_PWM(led->led_no);
104e370d010SAndrew Lunn 
105e370d010SAndrew Lunn 	return regmap_write(priv->regmap, pwm, brightness);
106e370d010SAndrew Lunn }
107e370d010SAndrew Lunn 
108d890389fSAndrew Lunn static int
109d890389fSAndrew Lunn tlc591xx_brightness_set(struct led_classdev *led_cdev,
110d890389fSAndrew Lunn 			enum led_brightness brightness)
111e370d010SAndrew Lunn {
112d890389fSAndrew Lunn 	struct tlc591xx_led *led = ldev_to_led(led_cdev);
113e370d010SAndrew Lunn 	struct tlc591xx_priv *priv = led->priv;
114e370d010SAndrew Lunn 	int err;
115e370d010SAndrew Lunn 
116a2cafdfdSJean-Jacques Hiblot 	switch ((int)brightness) {
117e370d010SAndrew Lunn 	case 0:
118e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_OFF);
119e370d010SAndrew Lunn 		break;
120a2cafdfdSJean-Jacques Hiblot 	case TLC591XX_MAX_BRIGHTNESS:
121e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_ON);
122e370d010SAndrew Lunn 		break;
123e370d010SAndrew Lunn 	default:
124e370d010SAndrew Lunn 		err = tlc591xx_set_ledout(priv, led, LEDOUT_DIM);
125e370d010SAndrew Lunn 		if (!err)
126e370d010SAndrew Lunn 			err = tlc591xx_set_pwm(priv, led, brightness);
127e370d010SAndrew Lunn 	}
128e370d010SAndrew Lunn 
129d890389fSAndrew Lunn 	return err;
130e370d010SAndrew Lunn }
131e370d010SAndrew Lunn 
132e370d010SAndrew Lunn static const struct regmap_config tlc591xx_regmap = {
133e370d010SAndrew Lunn 	.reg_bits = 8,
134e370d010SAndrew Lunn 	.val_bits = 8,
135e370d010SAndrew Lunn 	.max_register = 0x1e,
136e370d010SAndrew Lunn };
137e370d010SAndrew Lunn 
138e370d010SAndrew Lunn static const struct of_device_id of_tlc591xx_leds_match[] = {
139e370d010SAndrew Lunn 	{ .compatible = "ti,tlc59116",
140e370d010SAndrew Lunn 	  .data = &tlc59116 },
141e370d010SAndrew Lunn 	{ .compatible = "ti,tlc59108",
142e370d010SAndrew Lunn 	  .data = &tlc59108 },
143e370d010SAndrew Lunn 	{},
144e370d010SAndrew Lunn };
145e370d010SAndrew Lunn MODULE_DEVICE_TABLE(of, of_tlc591xx_leds_match);
146e370d010SAndrew Lunn 
147e370d010SAndrew Lunn static int
148e370d010SAndrew Lunn tlc591xx_probe(struct i2c_client *client,
149e370d010SAndrew Lunn 	       const struct i2c_device_id *id)
150e370d010SAndrew Lunn {
1518853c95eSMarek Behún 	struct device_node *np = dev_of_node(&client->dev), *child;
152e370d010SAndrew Lunn 	struct device *dev = &client->dev;
153e370d010SAndrew Lunn 	const struct tlc591xx *tlc591xx;
154e370d010SAndrew Lunn 	struct tlc591xx_priv *priv;
155e370d010SAndrew Lunn 	int err, count, reg;
156e370d010SAndrew Lunn 
1572779f472SMarek Behún 	tlc591xx = device_get_match_data(dev);
158e370d010SAndrew Lunn 	if (!np)
159e370d010SAndrew Lunn 		return -ENODEV;
160e370d010SAndrew Lunn 
161*99a013c8SMarek Behún 	count = of_get_available_child_count(np);
162e370d010SAndrew Lunn 	if (!count || count > tlc591xx->max_leds)
163e370d010SAndrew Lunn 		return -EINVAL;
164e370d010SAndrew Lunn 
165e370d010SAndrew Lunn 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
166e370d010SAndrew Lunn 	if (!priv)
167e370d010SAndrew Lunn 		return -ENOMEM;
168e370d010SAndrew Lunn 
169e370d010SAndrew Lunn 	priv->regmap = devm_regmap_init_i2c(client, &tlc591xx_regmap);
170e370d010SAndrew Lunn 	if (IS_ERR(priv->regmap)) {
171e370d010SAndrew Lunn 		err = PTR_ERR(priv->regmap);
172e370d010SAndrew Lunn 		dev_err(dev, "Failed to allocate register map: %d\n", err);
173e370d010SAndrew Lunn 		return err;
174e370d010SAndrew Lunn 	}
175e370d010SAndrew Lunn 	priv->reg_ledout_offset = tlc591xx->reg_ledout_offset;
176e370d010SAndrew Lunn 
177e370d010SAndrew Lunn 	i2c_set_clientdata(client, priv);
178e370d010SAndrew Lunn 
1791ab4531aSJean-Jacques Hiblot 	err = tlc591xx_set_mode(priv->regmap, MODE2_DIM);
1801ab4531aSJean-Jacques Hiblot 	if (err < 0)
1811ab4531aSJean-Jacques Hiblot 		return err;
1821ab4531aSJean-Jacques Hiblot 
183*99a013c8SMarek Behún 	for_each_available_child_of_node(np, child) {
1841ab4531aSJean-Jacques Hiblot 		struct tlc591xx_led *led;
1855b4b723cSJean-Jacques Hiblot 		struct led_init_data init_data = {};
1865b4b723cSJean-Jacques Hiblot 
1875b4b723cSJean-Jacques Hiblot 		init_data.fwnode = of_fwnode_handle(child);
1881ab4531aSJean-Jacques Hiblot 
189e370d010SAndrew Lunn 		err = of_property_read_u32(child, "reg", &reg);
190c6872911SJulia Lawall 		if (err) {
191c6872911SJulia Lawall 			of_node_put(child);
192e370d010SAndrew Lunn 			return err;
193c6872911SJulia Lawall 		}
1941055790bSJulia Lawall 		if (reg < 0 || reg >= tlc591xx->max_leds ||
195c6872911SJulia Lawall 		    priv->leds[reg].active) {
196c6872911SJulia Lawall 			of_node_put(child);
197e370d010SAndrew Lunn 			return -EINVAL;
198c6872911SJulia Lawall 		}
1991ab4531aSJean-Jacques Hiblot 		led = &priv->leds[reg];
2001ab4531aSJean-Jacques Hiblot 
2011ab4531aSJean-Jacques Hiblot 		led->active = true;
2021ab4531aSJean-Jacques Hiblot 		led->ldev.default_trigger =
203e370d010SAndrew Lunn 			of_get_property(child, "linux,default-trigger", NULL);
2041ab4531aSJean-Jacques Hiblot 
2051ab4531aSJean-Jacques Hiblot 		led->priv = priv;
2061ab4531aSJean-Jacques Hiblot 		led->led_no = reg;
2071ab4531aSJean-Jacques Hiblot 		led->ldev.brightness_set_blocking = tlc591xx_brightness_set;
208a2cafdfdSJean-Jacques Hiblot 		led->ldev.max_brightness = TLC591XX_MAX_BRIGHTNESS;
2095b4b723cSJean-Jacques Hiblot 		err = devm_led_classdev_register_ext(dev, &led->ldev,
2105b4b723cSJean-Jacques Hiblot 						     &init_data);
21103eb2ca4SKrzysztof Kozlowski 		if (err < 0)
21203eb2ca4SKrzysztof Kozlowski 			return dev_err_probe(dev, err,
21303eb2ca4SKrzysztof Kozlowski 					     "couldn't register LED %s\n",
2141ab4531aSJean-Jacques Hiblot 					     led->ldev.name);
215e370d010SAndrew Lunn 	}
216e370d010SAndrew Lunn 	return 0;
217e370d010SAndrew Lunn }
218e370d010SAndrew Lunn 
219e370d010SAndrew Lunn static const struct i2c_device_id tlc591xx_id[] = {
220e370d010SAndrew Lunn 	{ "tlc59116" },
221e370d010SAndrew Lunn 	{ "tlc59108" },
222e370d010SAndrew Lunn 	{},
223e370d010SAndrew Lunn };
224e370d010SAndrew Lunn MODULE_DEVICE_TABLE(i2c, tlc591xx_id);
225e370d010SAndrew Lunn 
226e370d010SAndrew Lunn static struct i2c_driver tlc591xx_driver = {
227e370d010SAndrew Lunn 	.driver = {
228e370d010SAndrew Lunn 		.name = "tlc591xx",
229e370d010SAndrew Lunn 		.of_match_table = of_match_ptr(of_tlc591xx_leds_match),
230e370d010SAndrew Lunn 	},
231e370d010SAndrew Lunn 	.probe = tlc591xx_probe,
232e370d010SAndrew Lunn 	.id_table = tlc591xx_id,
233e370d010SAndrew Lunn };
234e370d010SAndrew Lunn 
235e370d010SAndrew Lunn module_i2c_driver(tlc591xx_driver);
236e370d010SAndrew Lunn 
237e370d010SAndrew Lunn MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
238e370d010SAndrew Lunn MODULE_LICENSE("GPL");
239e370d010SAndrew Lunn MODULE_DESCRIPTION("TLC591XX LED driver");
240