Linux Audio

Check our new training course

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