xref: /linux/drivers/leds/leds-st1202.c (revision 49bda4826843be0ef97a162009a29ea3a63f3935)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * LED driver for STMicroelectronics LED1202 chip
4  *
5  * Copyright (C) 2024 Remote-Tech Ltd. UK
6  */
7 
8 #include <linux/cleanup.h>
9 #include <linux/ctype.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/i2c.h>
13 #include <linux/leds.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/string.h>
17 
18 #define ST1202_CHAN_DISABLE_ALL            0x00
19 #define ST1202_CHAN_ENABLE_HIGH            0x03
20 #define ST1202_CHAN_ENABLE_LOW             0x02
21 #define ST1202_CONFIG_REG                  0x04
22 /* PATS: Pattern sequence feature enable */
23 #define ST1202_CONFIG_REG_PATS             BIT(7)
24 /* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
25 #define ST1202_CONFIG_REG_PATSR            BIT(6)
26 #define ST1202_CONFIG_REG_SHFT             BIT(3)
27 #define ST1202_DEV_ENABLE                  0x01
28 #define ST1202_DEV_ENABLE_ON               BIT(0)
29 #define ST1202_DEV_ENABLE_RESET            BIT(7)
30 #define ST1202_DEVICE_ID                   0x00
31 #define ST1202_ILED_REG0                   0x09
32 #define ST1202_MAX_LEDS                    12
33 #define ST1202_MAX_PATTERNS                8
34 #define ST1202_MILLIS_PATTERN_DUR_MAX      (ST1202_MILLIS_PATTERN_DUR_MIN * U8_MAX)
35 #define ST1202_MILLIS_PATTERN_DUR_MIN      22
36 #define ST1202_PATTERN_DUR                 0x16
37 #define ST1202_PATTERN_PWM                 0x1E
38 #define ST1202_PATTERN_PWM_FULL            0x0FFF
39 #define ST1202_PATTERN_REP                 0x15
40 
41 struct st1202_led {
42 	struct fwnode_handle *fwnode;
43 	struct led_classdev led_cdev;
44 	struct st1202_chip *chip;
45 	bool is_active;
46 	int led_num;
47 };
48 
49 struct st1202_chip {
50 	struct i2c_client *client;
51 	struct mutex lock;
52 	struct st1202_led leds[ST1202_MAX_LEDS];
53 };
54 
cdev_to_st1202_led(struct led_classdev * cdev)55 static struct st1202_led *cdev_to_st1202_led(struct led_classdev *cdev)
56 {
57 	return container_of(cdev, struct st1202_led, led_cdev);
58 }
59 
st1202_read_reg(struct st1202_chip * chip,int reg,uint8_t * val)60 static int st1202_read_reg(struct st1202_chip *chip, int reg, uint8_t *val)
61 {
62 	struct device *dev = &chip->client->dev;
63 	int ret;
64 
65 	ret = i2c_smbus_read_byte_data(chip->client, reg);
66 	if (ret < 0) {
67 		dev_err(dev, "Failed to read register [0x%x]: %d\n", reg, ret);
68 		return ret;
69 	}
70 
71 	*val = (uint8_t)ret;
72 	return 0;
73 }
74 
st1202_write_reg(struct st1202_chip * chip,int reg,uint8_t val)75 static int st1202_write_reg(struct st1202_chip *chip, int reg, uint8_t val)
76 {
77 	struct device *dev = &chip->client->dev;
78 	int ret;
79 
80 	ret = i2c_smbus_write_byte_data(chip->client, reg, val);
81 	if (ret != 0)
82 		dev_err(dev, "Failed to write %d to register [0x%x]: %d\n", val, reg, ret);
83 
84 	return ret;
85 }
86 
st1202_prescalar_to_miliseconds(unsigned int value)87 static uint8_t st1202_prescalar_to_miliseconds(unsigned int value)
88 {
89 	return value / ST1202_MILLIS_PATTERN_DUR_MIN;
90 }
91 
st1202_pwm_pattern_write(struct st1202_chip * chip,int led_num,int pattern,unsigned int value)92 static int st1202_pwm_pattern_write(struct st1202_chip *chip, int led_num,
93 				int pattern, unsigned int value)
94 {
95 	u8 value_l, value_h;
96 	int ret;
97 
98 	value_l = (u8)value;
99 	value_h = (u8)(value >> 8);
100 
101 	/*
102 	 * Datasheet: Register address low = 1Eh + 2*(xh) + 18h*(yh),
103 	 * where x is the channel number (led number) in hexadecimal (x = 00h .. 0Bh)
104 	 * and y is the pattern number in hexadecimal (y = 00h .. 07h)
105 	 */
106 	ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + (led_num * 2) + 0x18 * pattern),
107 				value_l);
108 	if (ret != 0)
109 		return ret;
110 
111 	/*
112 	 * Datasheet: Register address high = 1Eh + 01h + 2(xh) +18h*(yh),
113 	 * where x is the channel number in hexadecimal (x = 00h .. 0Bh)
114 	 * and y is the pattern number in hexadecimal (y = 00h .. 07h)
115 	 */
116 	ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + 0x1 + (led_num * 2) + 0x18 * pattern),
117 				value_h);
118 	if (ret != 0)
119 		return ret;
120 
121 	return 0;
122 }
123 
st1202_duration_pattern_write(struct st1202_chip * chip,int pattern,unsigned int value)124 static int st1202_duration_pattern_write(struct st1202_chip *chip, int pattern,
125 					unsigned int value)
126 {
127 	return st1202_write_reg(chip, (ST1202_PATTERN_DUR + pattern),
128 				st1202_prescalar_to_miliseconds(value));
129 }
130 
__st1202_channel_set(struct st1202_chip * chip,int led_num,bool active)131 static int __st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
132 {
133 	u8 chan_low, chan_high;
134 	int ret;
135 
136 	if (led_num <= 7) {
137 		ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
138 		if (ret < 0)
139 			return ret;
140 
141 		chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
142 
143 		ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
144 		if (ret < 0)
145 			return ret;
146 
147 	} else {
148 		ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
149 		if (ret < 0)
150 			return ret;
151 
152 		chan_high = active ? chan_high | (BIT(led_num) >> 8) :
153 					chan_high & ~(BIT(led_num) >> 8);
154 
155 		ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
156 		if (ret < 0)
157 			return ret;
158 	}
159 
160 	return 0;
161 }
162 
st1202_channel_set(struct st1202_chip * chip,int led_num,bool active)163 static int st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
164 {
165 	guard(mutex)(&chip->lock);
166 
167 	return __st1202_channel_set(chip, led_num, active);
168 }
169 
st1202_brightness_set(struct led_classdev * led_cdev,enum led_brightness value)170 static void st1202_brightness_set(struct led_classdev *led_cdev,
171 				enum led_brightness value)
172 {
173 	struct st1202_led *led = cdev_to_st1202_led(led_cdev);
174 	struct st1202_chip *chip = led->chip;
175 
176 	guard(mutex)(&chip->lock);
177 
178 	for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++)
179 		st1202_pwm_pattern_write(chip, led->led_num, patt, ST1202_PATTERN_PWM_FULL);
180 	st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, value);
181 	__st1202_channel_set(chip, led->led_num, !!value);
182 }
183 
st1202_brightness_get(struct led_classdev * led_cdev)184 static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
185 {
186 	struct st1202_led *led = cdev_to_st1202_led(led_cdev);
187 	struct st1202_chip *chip = led->chip;
188 	u8 value = 0;
189 
190 	guard(mutex)(&chip->lock);
191 
192 	st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
193 
194 	return value;
195 }
196 
st1202_led_set(struct led_classdev * ldev,enum led_brightness value)197 static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
198 {
199 	struct st1202_led *led = cdev_to_st1202_led(ldev);
200 
201 	return st1202_channel_set(led->chip, led->led_num, !!value);
202 }
203 
st1202_led_pattern_clear(struct led_classdev * ldev)204 static int st1202_led_pattern_clear(struct led_classdev *ldev)
205 {
206 	struct st1202_led *led = cdev_to_st1202_led(ldev);
207 	struct st1202_chip *chip = led->chip;
208 	int ret;
209 
210 	guard(mutex)(&chip->lock);
211 
212 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
213 	if (ret != 0)
214 		return ret;
215 
216 	for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++) {
217 		ret = st1202_pwm_pattern_write(chip, led->led_num, patt, ST1202_PATTERN_PWM_FULL);
218 		if (ret != 0)
219 			return ret;
220 
221 		ret = st1202_write_reg(chip, ST1202_PATTERN_DUR + patt, 0);
222 		if (ret != 0)
223 			return ret;
224 	}
225 
226 	return 0;
227 }
228 
st1202_led_pattern_set(struct led_classdev * ldev,struct led_pattern * pattern,u32 len,int repeat)229 static int st1202_led_pattern_set(struct led_classdev *ldev,
230 				struct led_pattern *pattern,
231 				u32 len, int repeat)
232 {
233 	struct st1202_led *led = cdev_to_st1202_led(ldev);
234 	struct st1202_chip *chip = led->chip;
235 	int ret;
236 
237 	if (len > ST1202_MAX_PATTERNS)
238 		return -EINVAL;
239 
240 	for (int patt = 0; patt < len; patt++) {
241 		if (pattern[patt].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
242 				pattern[patt].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
243 			return -EINVAL;
244 	}
245 
246 	guard(mutex)(&chip->lock);
247 
248 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
249 	if (ret != 0)
250 		return ret;
251 
252 	for (int patt = 0; patt < len; patt++) {
253 		ret = st1202_pwm_pattern_write(chip, led->led_num, patt, pattern[patt].brightness);
254 		if (ret != 0)
255 			return ret;
256 
257 		ret = st1202_duration_pattern_write(chip, patt, pattern[patt].delta_t);
258 		if (ret != 0)
259 			return ret;
260 	}
261 
262 	ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
263 	if (ret != 0)
264 		return ret;
265 
266 	ret = __st1202_channel_set(chip, led->led_num, true);
267 	if (ret != 0)
268 		return ret;
269 
270 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, (ST1202_CONFIG_REG_PATSR |
271 							ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_SHFT));
272 	if (ret != 0)
273 		return ret;
274 
275 	return 0;
276 }
277 
st1202_dt_init(struct st1202_chip * chip)278 static int st1202_dt_init(struct st1202_chip *chip)
279 {
280 	struct device *dev = &chip->client->dev;
281 	struct st1202_led *led;
282 	int err;
283 	u32 reg;
284 
285 	for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
286 		err = of_property_read_u32(child, "reg", &reg);
287 		if (err)
288 			return dev_err_probe(dev, err, "Invalid register\n");
289 
290 		if (reg >= ST1202_MAX_LEDS)
291 			return dev_err_probe(dev, -EINVAL,
292 					"LED reg %u out of range [0, %d]\n",
293 					reg, ST1202_MAX_LEDS - 1);
294 
295 		led = &chip->leds[reg];
296 		led->is_active = true;
297 		led->fwnode = of_fwnode_handle(child);
298 
299 		led->led_cdev.max_brightness = U8_MAX;
300 		led->led_cdev.brightness_set_blocking = st1202_led_set;
301 		led->led_cdev.pattern_set = st1202_led_pattern_set;
302 		led->led_cdev.pattern_clear = st1202_led_pattern_clear;
303 		led->led_cdev.default_trigger = "pattern";
304 		led->led_cdev.brightness_set = st1202_brightness_set;
305 		led->led_cdev.brightness_get = st1202_brightness_get;
306 	}
307 
308 	return 0;
309 }
310 
st1202_setup(struct st1202_chip * chip)311 static int st1202_setup(struct st1202_chip *chip)
312 {
313 	int ret;
314 
315 	guard(mutex)(&chip->lock);
316 
317 	/*
318 	 * Once the supply voltage is applied, the LED1202 executes some internal checks.
319 	 * Afterwards, it stops the oscillator and puts the internal LDO in quiescent mode.
320 	 * To start the device, EN bit must be set inside the “Device Enable” register at
321 	 * address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
322 	 * from the internal non-volatile memory and performs an auto-calibration procedure
323 	 * in order to increase the output current precision.
324 	 * Such initialization lasts about 6.5 ms.
325 	 */
326 
327 	/* Reset the chip during setup */
328 	ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
329 	if (ret < 0)
330 		return ret;
331 
332 	/* Enable phase-shift delay feature */
333 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
334 	if (ret < 0)
335 		return ret;
336 
337 	/* Enable the device */
338 	ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
339 	if (ret < 0)
340 		return ret;
341 
342 	/* Duration of initialization */
343 	usleep_range(6500, 10000);
344 
345 	/* Deactivate all LEDS (channels) and activate only the ones found in Device Tree */
346 	ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
347 	if (ret < 0)
348 		return ret;
349 
350 	ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
351 	if (ret < 0)
352 		return ret;
353 
354 	return 0;
355 }
356 
st1202_probe(struct i2c_client * client)357 static int st1202_probe(struct i2c_client *client)
358 {
359 	struct st1202_chip *chip;
360 	struct st1202_led *led;
361 	int ret;
362 
363 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
364 		return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
365 
366 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
367 	if (!chip)
368 		return -ENOMEM;
369 
370 	ret = devm_mutex_init(&client->dev, &chip->lock);
371 	if (ret < 0)
372 		return ret;
373 	chip->client = client;
374 
375 	ret = st1202_setup(chip);
376 	if (ret < 0)
377 		return ret;
378 
379 	ret = st1202_dt_init(chip);
380 	if (ret < 0)
381 		return ret;
382 
383 	for (int i = 0; i < ST1202_MAX_LEDS; i++) {
384 		struct led_init_data init_data = {};
385 		led = &chip->leds[i];
386 		led->chip = chip;
387 		led->led_num = i;
388 
389 		if (!led->is_active)
390 			continue;
391 
392 		ret = st1202_channel_set(led->chip, led->led_num, true);
393 		if (ret < 0)
394 			return dev_err_probe(&client->dev, ret,
395 					"Failed to activate LED channel\n");
396 
397 		ret = st1202_led_pattern_clear(&led->led_cdev);
398 		if (ret < 0)
399 			return dev_err_probe(&client->dev, ret,
400 					"Failed to clear LED pattern\n");
401 
402 		init_data.fwnode = led->fwnode;
403 		init_data.devicename = "st1202";
404 		init_data.default_label = ":";
405 
406 		ret = devm_led_classdev_register_ext(&client->dev, &led->led_cdev, &init_data);
407 		if (ret < 0)
408 			return dev_err_probe(&client->dev, ret,
409 					"Failed to register LED class device\n");
410 	}
411 
412 	return 0;
413 }
414 
415 static const struct i2c_device_id st1202_id[] = {
416 	{ .name = "st1202-i2c" },
417 	{ /* sentinel */ }
418 };
419 MODULE_DEVICE_TABLE(i2c, st1202_id);
420 
421 static const struct of_device_id st1202_dt_ids[] = {
422 	{ .compatible = "st,led1202" },
423 	{ /* sentinel */ }
424 };
425 MODULE_DEVICE_TABLE(of, st1202_dt_ids);
426 
427 static struct i2c_driver st1202_driver = {
428 	.driver = {
429 		.name = "leds-st1202",
430 		.of_match_table = of_match_ptr(st1202_dt_ids),
431 	},
432 	.probe = st1202_probe,
433 	.id_table = st1202_id,
434 };
435 module_i2c_driver(st1202_driver);
436 
437 MODULE_AUTHOR("Remote Tech LTD");
438 MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
439 MODULE_LICENSE("GPL");
440