xref: /linux/drivers/leds/leds-st1202.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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/gpio.h>
13 #include <linux/i2c.h>
14 #include <linux/leds.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 
19 #define ST1202_CHAN_DISABLE_ALL            0x00
20 #define ST1202_CHAN_ENABLE_HIGH            0x03
21 #define ST1202_CHAN_ENABLE_LOW             0x02
22 #define ST1202_CONFIG_REG                  0x04
23 /* PATS: Pattern sequence feature enable */
24 #define ST1202_CONFIG_REG_PATS             BIT(7)
25 /* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
26 #define ST1202_CONFIG_REG_PATSR            BIT(6)
27 #define ST1202_CONFIG_REG_SHFT             BIT(3)
28 #define ST1202_DEV_ENABLE                  0x01
29 #define ST1202_DEV_ENABLE_ON               BIT(0)
30 #define ST1202_DEV_ENABLE_RESET            BIT(7)
31 #define ST1202_DEVICE_ID                   0x00
32 #define ST1202_ILED_REG0                   0x09
33 #define ST1202_MAX_LEDS                    12
34 #define ST1202_MAX_PATTERNS                8
35 #define ST1202_MILLIS_PATTERN_DUR_MAX      5660
36 #define ST1202_MILLIS_PATTERN_DUR_MIN      22
37 #define ST1202_PATTERN_DUR                 0x16
38 #define ST1202_PATTERN_PWM                 0x1E
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 
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 
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 
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 
87 static uint8_t st1202_prescalar_to_miliseconds(unsigned int value)
88 {
89 	return value / ST1202_MILLIS_PATTERN_DUR_MIN - 1;
90 }
91 
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 
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 
131 static void st1202_brightness_set(struct led_classdev *led_cdev,
132 				enum led_brightness value)
133 {
134 	struct st1202_led *led = cdev_to_st1202_led(led_cdev);
135 	struct st1202_chip *chip = led->chip;
136 
137 	guard(mutex)(&chip->lock);
138 
139 	st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, value);
140 }
141 
142 static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
143 {
144 	struct st1202_led *led = cdev_to_st1202_led(led_cdev);
145 	struct st1202_chip *chip = led->chip;
146 	u8 value = 0;
147 
148 	guard(mutex)(&chip->lock);
149 
150 	st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
151 
152 	return value;
153 }
154 
155 static int st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
156 {
157 	u8 chan_low, chan_high;
158 	int ret;
159 
160 	guard(mutex)(&chip->lock);
161 
162 	if (led_num <= 7) {
163 		ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
164 		if (ret < 0)
165 			return ret;
166 
167 		chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
168 
169 		ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
170 		if (ret < 0)
171 			return ret;
172 
173 	} else {
174 		ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
175 		if (ret < 0)
176 			return ret;
177 
178 		chan_high = active ? chan_high | (BIT(led_num) >> 8) :
179 					chan_high & ~(BIT(led_num) >> 8);
180 
181 		ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
182 		if (ret < 0)
183 			return ret;
184 	}
185 
186 	return 0;
187 }
188 
189 static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
190 {
191 	struct st1202_led *led = cdev_to_st1202_led(ldev);
192 	struct st1202_chip *chip = led->chip;
193 
194 	return st1202_channel_set(chip, led->led_num, value == LED_OFF ? false : true);
195 }
196 
197 static int st1202_led_pattern_clear(struct led_classdev *ldev)
198 {
199 	struct st1202_led *led = cdev_to_st1202_led(ldev);
200 	struct st1202_chip *chip = led->chip;
201 	int ret;
202 
203 	guard(mutex)(&chip->lock);
204 
205 	for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++) {
206 		ret = st1202_pwm_pattern_write(chip, led->led_num, patt, LED_OFF);
207 		if (ret != 0)
208 			return ret;
209 
210 		ret = st1202_duration_pattern_write(chip, patt, ST1202_MILLIS_PATTERN_DUR_MIN);
211 		if (ret != 0)
212 			return ret;
213 	}
214 
215 	return 0;
216 }
217 
218 static int st1202_led_pattern_set(struct led_classdev *ldev,
219 				struct led_pattern *pattern,
220 				u32 len, int repeat)
221 {
222 	struct st1202_led *led = cdev_to_st1202_led(ldev);
223 	struct st1202_chip *chip = led->chip;
224 	int ret;
225 
226 	if (len > ST1202_MAX_PATTERNS)
227 		return -EINVAL;
228 
229 	guard(mutex)(&chip->lock);
230 
231 	for (int patt = 0; patt < len; patt++) {
232 		if (pattern[patt].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
233 				pattern[patt].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
234 			return -EINVAL;
235 
236 		ret = st1202_pwm_pattern_write(chip, led->led_num, patt, pattern[patt].brightness);
237 		if (ret != 0)
238 			return ret;
239 
240 		ret = st1202_duration_pattern_write(chip, patt, pattern[patt].delta_t);
241 		if (ret != 0)
242 			return ret;
243 	}
244 
245 	ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
246 	if (ret != 0)
247 		return ret;
248 
249 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, (ST1202_CONFIG_REG_PATSR |
250 							ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_SHFT));
251 	if (ret != 0)
252 		return ret;
253 
254 	return 0;
255 }
256 
257 static int st1202_dt_init(struct st1202_chip *chip)
258 {
259 	struct device *dev = &chip->client->dev;
260 	struct st1202_led *led;
261 	int err, reg;
262 
263 	for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
264 		struct led_init_data init_data = {};
265 
266 		err = of_property_read_u32(child, "reg", &reg);
267 		if (err)
268 			return dev_err_probe(dev, err, "Invalid register\n");
269 
270 		led = &chip->leds[reg];
271 		led->is_active = true;
272 		led->fwnode = of_fwnode_handle(child);
273 
274 		led->led_cdev.max_brightness = U8_MAX;
275 		led->led_cdev.brightness_set_blocking = st1202_led_set;
276 		led->led_cdev.pattern_set = st1202_led_pattern_set;
277 		led->led_cdev.pattern_clear = st1202_led_pattern_clear;
278 		led->led_cdev.default_trigger = "pattern";
279 
280 		init_data.fwnode = led->fwnode;
281 		init_data.devicename = "st1202";
282 		init_data.default_label = ":";
283 
284 		err = devm_led_classdev_register_ext(dev, &led->led_cdev, &init_data);
285 		if (err < 0)
286 			return dev_err_probe(dev, err, "Failed to register LED class device\n");
287 
288 		led->led_cdev.brightness_set = st1202_brightness_set;
289 		led->led_cdev.brightness_get = st1202_brightness_get;
290 	}
291 
292 	return 0;
293 }
294 
295 static int st1202_setup(struct st1202_chip *chip)
296 {
297 	int ret;
298 
299 	guard(mutex)(&chip->lock);
300 
301 	/*
302 	 * Once the supply voltage is applied, the LED1202 executes some internal checks,
303 	 * afterwords it stops the oscillator and puts the internal LDO in quiescent mode.
304 	 * To start the device, EN bit must be set inside the “Device Enable” register at
305 	 * address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
306 	 * from the internal non-volatile memory and performs an auto-calibration procedure
307 	 * in order to increase the output current precision.
308 	 * Such initialization lasts about 6.5 ms.
309 	 */
310 
311 	/* Reset the chip during setup */
312 	ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
313 	if (ret < 0)
314 		return ret;
315 
316 	/* Enable phase-shift delay feature */
317 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
318 	if (ret < 0)
319 		return ret;
320 
321 	/* Enable the device */
322 	ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
323 	if (ret < 0)
324 		return ret;
325 
326 	/* Duration of initialization */
327 	usleep_range(6500, 10000);
328 
329 	/* Deactivate all LEDS (channels) and activate only the ones found in Device Tree */
330 	ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
331 	if (ret < 0)
332 		return ret;
333 
334 	ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
335 	if (ret < 0)
336 		return ret;
337 
338 	ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
339 				ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_PATSR);
340 	if (ret < 0)
341 		return ret;
342 
343 	return 0;
344 }
345 
346 static int st1202_probe(struct i2c_client *client)
347 {
348 	struct st1202_chip *chip;
349 	struct st1202_led *led;
350 	int ret;
351 
352 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
353 		return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
354 
355 	chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
356 	if (!chip)
357 		return -ENOMEM;
358 
359 	devm_mutex_init(&client->dev, &chip->lock);
360 	chip->client = client;
361 
362 	ret = st1202_dt_init(chip);
363 	if (ret < 0)
364 		return ret;
365 
366 	ret = st1202_setup(chip);
367 	if (ret < 0)
368 		return ret;
369 
370 	for (int i = 0; i < ST1202_MAX_LEDS; i++) {
371 		led = &chip->leds[i];
372 		led->chip = chip;
373 		led->led_num = i;
374 
375 		if (!led->is_active)
376 			continue;
377 
378 		ret = st1202_channel_set(led->chip, led->led_num, true);
379 		if (ret < 0)
380 			return dev_err_probe(&client->dev, ret,
381 					"Failed to activate LED channel\n");
382 
383 		ret = st1202_led_pattern_clear(&led->led_cdev);
384 		if (ret < 0)
385 			return dev_err_probe(&client->dev, ret,
386 					"Failed to clear LED pattern\n");
387 	}
388 
389 	return 0;
390 }
391 
392 static const struct i2c_device_id st1202_id[] = {
393 	{ "st1202-i2c" },
394 	{ /* sentinel */ }
395 };
396 MODULE_DEVICE_TABLE(i2c, st1202_id);
397 
398 static const struct of_device_id st1202_dt_ids[] = {
399 	{ .compatible = "st,led1202" },
400 	{ /* sentinel */ }
401 };
402 MODULE_DEVICE_TABLE(of, st1202_dt_ids);
403 
404 static struct i2c_driver st1202_driver = {
405 	.driver = {
406 		.name = "leds-st1202",
407 		.of_match_table = of_match_ptr(st1202_dt_ids),
408 	},
409 	.probe = st1202_probe,
410 	.id_table = st1202_id,
411 };
412 module_i2c_driver(st1202_driver);
413 
414 MODULE_AUTHOR("Remote Tech LTD");
415 MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
416 MODULE_LICENSE("GPL");
417