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