xref: /linux/drivers/leds/leds-pca963x.c (revision 7db28abbea0f7dc1ec4fdfdc149db5fbd9e4c994)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2011 bct electronic GmbH
4  * Copyright 2013 Qtechnology/AS
5  *
6  * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
7  * Author: Ricardo Ribalda <ribalda@kernel.org>
8  *
9  * Based on leds-pca955x.c
10  *
11  * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
12  * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
13  *
14  * Note that hardware blinking violates the leds infrastructure driver
15  * interface since the hardware only supports blinking all LEDs with the
16  * same delay_on/delay_off rates.  That is, only the LEDs that are set to
17  * blink will actually blink but all LEDs that are set to blink will blink
18  * in identical fashion.  The delay_on/delay_off values of the last LED
19  * that is set to blink will be used for all of the blinking LEDs.
20  * Hardware blinking is disabled by default but can be enabled by setting
21  * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
22  * or by adding the 'nxp,hw-blink' property to the DTS.
23  */
24 
25 #include <linux/module.h>
26 #include <linux/delay.h>
27 #include <linux/string.h>
28 #include <linux/ctype.h>
29 #include <linux/leds.h>
30 #include <linux/led-class-multicolor.h>
31 #include <linux/err.h>
32 #include <linux/i2c.h>
33 #include <linux/property.h>
34 #include <linux/slab.h>
35 #include <linux/of.h>
36 
37 /* LED select registers determine the source that drives LED outputs */
38 #define PCA963X_LED_OFF		0x0	/* LED driver off */
39 #define PCA963X_LED_ON		0x1	/* LED driver on */
40 #define PCA963X_LED_PWM		0x2	/* Controlled through PWM */
41 #define PCA963X_LED_GRP_PWM	0x3	/* Controlled through PWM/GRPPWM */
42 
43 #define PCA963X_MODE1_SLEEP	0x04    /* Normal mode or Low Power mode, oscillator off */
44 #define PCA963X_MODE2_OUTDRV	0x04	/* Open-drain or totem pole */
45 #define PCA963X_MODE2_INVRT	0x10	/* Normal or inverted direction */
46 #define PCA963X_MODE2_DMBLNK	0x20	/* Enable blinking */
47 
48 #define PCA963X_MODE1		0x00
49 #define PCA963X_MODE2		0x01
50 #define PCA963X_PWM_BASE	0x02
51 
52 enum pca963x_type {
53 	pca9633,
54 	pca9634,
55 	pca9635,
56 };
57 
58 struct pca963x_chipdef {
59 	u8			grppwm;
60 	u8			grpfreq;
61 	u8			ledout_base;
62 	int			n_leds;
63 	unsigned int		scaling;
64 };
65 
66 static struct pca963x_chipdef pca963x_chipdefs[] = {
67 	[pca9633] = {
68 		.grppwm		= 0x6,
69 		.grpfreq	= 0x7,
70 		.ledout_base	= 0x8,
71 		.n_leds		= 4,
72 	},
73 	[pca9634] = {
74 		.grppwm		= 0xa,
75 		.grpfreq	= 0xb,
76 		.ledout_base	= 0xc,
77 		.n_leds		= 8,
78 	},
79 	[pca9635] = {
80 		.grppwm		= 0x12,
81 		.grpfreq	= 0x13,
82 		.ledout_base	= 0x14,
83 		.n_leds		= 16,
84 	},
85 };
86 
87 /* Total blink period in milliseconds */
88 #define PCA963X_BLINK_PERIOD_MIN	42
89 #define PCA963X_BLINK_PERIOD_MAX	10667
90 
91 static const struct i2c_device_id pca963x_id[] = {
92 	{ .name = "pca9632", .driver_data = pca9633 },
93 	{ .name = "pca9633", .driver_data = pca9633 },
94 	{ .name = "pca9634", .driver_data = pca9634 },
95 	{ .name = "pca9635", .driver_data = pca9635 },
96 	{ }
97 };
98 MODULE_DEVICE_TABLE(i2c, pca963x_id);
99 
100 struct pca963x;
101 
102 struct pca963x_led {
103 	struct pca963x *chip;
104 	struct led_classdev led_cdev;
105 	struct led_classdev_mc mc_cdev;
106 	struct mc_subled subleds[4];
107 	int led_num; /* 0 .. 15 potentially */
108 	bool blinking;
109 	bool is_mc;
110 	u8 gdc;
111 	u8 gfrq;
112 };
113 
114 struct pca963x {
115 	struct pca963x_chipdef *chipdef;
116 	struct mutex mutex;
117 	struct i2c_client *client;
118 	unsigned long leds_on;
119 	struct pca963x_led leds[];
120 };
121 
122 static int pca963x_brightness(struct pca963x_led *led, unsigned int led_num,
123 			      enum led_brightness brightness)
124 {
125 	struct i2c_client *client = led->chip->client;
126 	struct pca963x_chipdef *chipdef = led->chip->chipdef;
127 	u8 ledout_addr, ledout, mask, val;
128 	int shift;
129 	int ret;
130 
131 	ledout_addr = chipdef->ledout_base + (led_num / 4);
132 	shift = 2 * (led_num % 4);
133 	mask = 0x3 << shift;
134 	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
135 
136 	switch (brightness) {
137 	case LED_FULL:
138 		if (led->blinking) {
139 			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
140 			ret = i2c_smbus_write_byte_data(client,
141 						PCA963X_PWM_BASE +
142 						led_num,
143 						LED_FULL);
144 		} else {
145 			val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
146 		}
147 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
148 		break;
149 	case LED_OFF:
150 		val = ledout & ~mask;
151 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
152 		led->blinking = false;
153 		break;
154 	default:
155 		ret = i2c_smbus_write_byte_data(client,
156 						PCA963X_PWM_BASE +
157 						led_num,
158 						brightness);
159 		if (ret < 0)
160 			return ret;
161 
162 		if (led->blinking)
163 			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
164 		else
165 			val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
166 
167 		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
168 		break;
169 	}
170 
171 	return ret;
172 }
173 
174 static void pca963x_blink(struct pca963x_led *led)
175 {
176 	struct i2c_client *client = led->chip->client;
177 	struct pca963x_chipdef *chipdef = led->chip->chipdef;
178 	u8 ledout_addr, ledout, mask, val, mode2;
179 	int shift;
180 
181 	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
182 	shift = 2 * (led->led_num % 4);
183 	mask = 0x3 << shift;
184 	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
185 
186 	i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
187 
188 	i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
189 
190 	if (!(mode2 & PCA963X_MODE2_DMBLNK))
191 		i2c_smbus_write_byte_data(client, PCA963X_MODE2,
192 					  mode2 | PCA963X_MODE2_DMBLNK);
193 
194 	mutex_lock(&led->chip->mutex);
195 
196 	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
197 	if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
198 		val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
199 		i2c_smbus_write_byte_data(client, ledout_addr, val);
200 	}
201 
202 	mutex_unlock(&led->chip->mutex);
203 	led->blinking = true;
204 }
205 
206 static void pca963x_track_power_state(struct pca963x_led *led, unsigned int led_num,
207 				      enum led_brightness brightness)
208 {
209 	unsigned long *leds_on = &led->chip->leds_on;
210 
211 	if (brightness)
212 		set_bit(led_num, leds_on);
213 	else
214 		clear_bit(led_num, leds_on);
215 }
216 
217 static int pca963x_sync_power_state(struct pca963x_led *led, unsigned long cached_leds)
218 {
219 	struct i2c_client *client = led->chip->client;
220 
221 	if (!led->chip->leds_on != !cached_leds)
222 		return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
223 						 led->chip->leds_on ? 0 : BIT(4));
224 
225 	return 0;
226 }
227 
228 static int pca963x_led_set(struct led_classdev *led_cdev,
229 			   enum led_brightness value)
230 {
231 	struct pca963x_led *led;
232 	unsigned long cached_leds;
233 	int ret;
234 
235 	led = container_of(led_cdev, struct pca963x_led, led_cdev);
236 
237 	mutex_lock(&led->chip->mutex);
238 
239 	cached_leds = led->chip->leds_on;
240 	ret = pca963x_brightness(led, led->led_num, value);
241 	if (ret)
242 		goto unlock;
243 
244 	pca963x_track_power_state(led, led->led_num, value);
245 	ret = pca963x_sync_power_state(led, cached_leds);
246 
247 unlock:
248 	mutex_unlock(&led->chip->mutex);
249 	return ret;
250 }
251 
252 static int pca963x_led_mc_set(struct led_classdev *led_cdev,
253 			      enum led_brightness value)
254 {
255 	struct led_classdev_mc *mc_cdev = lcdev_to_mccdev(led_cdev);
256 	struct pca963x_led *led = container_of(mc_cdev, struct pca963x_led, mc_cdev);
257 	unsigned long cached_leds;
258 	int ret = 0, sync_ret;
259 
260 	led_mc_calc_color_components(mc_cdev, value);
261 
262 	guard(mutex)(&led->chip->mutex);
263 
264 	cached_leds = led->chip->leds_on;
265 	for (unsigned int i = 0; i < mc_cdev->num_colors; i++) {
266 		unsigned int channel = mc_cdev->subled_info[i].channel;
267 
268 		ret = pca963x_brightness(led, channel,
269 					 mc_cdev->subled_info[i].brightness);
270 		if (ret)
271 			break;
272 
273 		pca963x_track_power_state(led, channel,
274 					  mc_cdev->subled_info[i].brightness);
275 	}
276 
277 	/*
278 	 * Some channels may already have been updated before the error, so
279 	 * still sync the global on/off state to reflect what actually changed.
280 	 */
281 	sync_ret = pca963x_sync_power_state(led, cached_leds);
282 
283 	return ret ? : sync_ret;
284 }
285 
286 static unsigned int pca963x_period_scale(struct pca963x_led *led,
287 					 unsigned int val)
288 {
289 	unsigned int scaling = led->chip->chipdef->scaling;
290 
291 	return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
292 }
293 
294 static int pca963x_blink_set(struct led_classdev *led_cdev,
295 			     unsigned long *delay_on, unsigned long *delay_off)
296 {
297 	unsigned long time_on, time_off, period;
298 	struct pca963x_led *led;
299 	u8 gdc, gfrq;
300 
301 	led = container_of(led_cdev, struct pca963x_led, led_cdev);
302 
303 	time_on = *delay_on;
304 	time_off = *delay_off;
305 
306 	/* If both zero, pick reasonable defaults of 500ms each */
307 	if (!time_on && !time_off) {
308 		time_on = 500;
309 		time_off = 500;
310 	}
311 
312 	period = pca963x_period_scale(led, time_on + time_off);
313 
314 	/* If period not supported by hardware, default to someting sane. */
315 	if ((period < PCA963X_BLINK_PERIOD_MIN) ||
316 	    (period > PCA963X_BLINK_PERIOD_MAX)) {
317 		time_on = 500;
318 		time_off = 500;
319 		period = pca963x_period_scale(led, 1000);
320 	}
321 
322 	/*
323 	 * From manual: duty cycle = (GDC / 256) ->
324 	 *	(time_on / period) = (GDC / 256) ->
325 	 *		GDC = ((time_on * 256) / period)
326 	 */
327 	gdc = (pca963x_period_scale(led, time_on) * 256) / period;
328 
329 	/*
330 	 * From manual: period = ((GFRQ + 1) / 24) in seconds.
331 	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
332 	 *		GFRQ = ((period * 24 / 1000) - 1)
333 	 */
334 	gfrq = (period * 24 / 1000) - 1;
335 
336 	led->gdc = gdc;
337 	led->gfrq = gfrq;
338 
339 	pca963x_blink(led);
340 	led->led_cdev.brightness = LED_FULL;
341 	pca963x_led_set(led_cdev, LED_FULL);
342 
343 	*delay_on = time_on;
344 	*delay_off = time_off;
345 
346 	return 0;
347 }
348 
349 static int pca963x_parse_mc_subleds(struct device *dev, struct pca963x_led *led,
350 				    struct fwnode_handle *fwnode,
351 				    const struct pca963x_chipdef *chipdef)
352 {
353 	unsigned int num_colors = 0;
354 	int ret;
355 
356 	fwnode_for_each_child_node_scoped(fwnode, sub) {
357 		u32 color, subreg;
358 
359 		if (num_colors >= ARRAY_SIZE(led->subleds))
360 			return dev_err_probe(dev, -EINVAL, "Too many LEDs for node %pfw\n", fwnode);
361 
362 		ret = fwnode_property_read_u32(sub, "reg", &subreg);
363 		if (ret)
364 			return dev_err_probe(dev, ret, "Missing 'reg' for sub-LED %pfw\n", sub);
365 		if (subreg >= chipdef->n_leds)
366 			return dev_err_probe(dev, -EINVAL, "Invalid 'reg' for sub-LED %pfw\n", sub);
367 
368 		ret = fwnode_property_read_u32(sub, "color", &color);
369 		if (ret)
370 			return dev_err_probe(dev, ret, "Missing 'color' for sub-LED %pfw\n", sub);
371 
372 		led->subleds[num_colors].channel = subreg;
373 		led->subleds[num_colors].color_index = color;
374 		led->subleds[num_colors].intensity = LED_FULL;
375 		num_colors++;
376 	}
377 
378 	led->mc_cdev.subled_info = led->subleds;
379 	led->mc_cdev.num_colors = num_colors;
380 	led->mc_cdev.led_cdev.max_brightness = LED_FULL;
381 	led->mc_cdev.led_cdev.brightness_set_blocking = pca963x_led_mc_set;
382 
383 	return 0;
384 }
385 
386 static int pca963x_register_led(struct device *dev, struct pca963x_led *led,
387 				u32 reg, struct fwnode_handle *fwnode,
388 				const struct pca963x_chipdef *chipdef,
389 				bool hw_blink)
390 {
391 	struct i2c_client *client = led->chip->client;
392 	struct led_init_data init_data = {};
393 	char label[32];
394 	int ret;
395 
396 	led->led_num = reg;
397 
398 	/* A node with sub-children groups several channels into a multicolor LED. */
399 	led->is_mc = fwnode_get_child_node_count(fwnode) > 0;
400 
401 	if (led->is_mc) {
402 		ret = pca963x_parse_mc_subleds(dev, led, fwnode, chipdef);
403 		if (ret)
404 			return ret;
405 	} else {
406 		led->led_cdev.brightness_set_blocking = pca963x_led_set;
407 		if (hw_blink)
408 			led->led_cdev.blink_set = pca963x_blink_set;
409 	}
410 
411 	init_data.fwnode = fwnode;
412 	/* Keep the legacy device name to preserve existing sysfs LED names. */
413 	init_data.devicename = "pca963x";
414 	snprintf(label, sizeof(label), "%d:%.2x:%u", client->adapter->nr, client->addr, reg);
415 	init_data.default_label = label;
416 
417 	if (led->is_mc)
418 		return devm_led_classdev_multicolor_register_ext(dev, &led->mc_cdev,
419 								 &init_data);
420 
421 	return devm_led_classdev_register_ext(dev, &led->led_cdev, &init_data);
422 }
423 
424 static int pca963x_register_leds(struct i2c_client *client,
425 				 struct pca963x *chip)
426 {
427 	struct pca963x_chipdef *chipdef = chip->chipdef;
428 	struct pca963x_led *led = chip->leds;
429 	struct device *dev = &client->dev;
430 	bool hw_blink;
431 	s32 mode2;
432 	u32 reg;
433 	int ret;
434 
435 	if (device_property_read_u32(dev, "nxp,period-scale",
436 				     &chipdef->scaling))
437 		chipdef->scaling = 1000;
438 
439 	hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
440 
441 	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
442 	if (mode2 < 0)
443 		return mode2;
444 
445 	/* default to open-drain unless totem pole (push-pull) is specified */
446 	if (device_property_read_bool(dev, "nxp,totem-pole"))
447 		mode2 |= PCA963X_MODE2_OUTDRV;
448 	else
449 		mode2 &= ~PCA963X_MODE2_OUTDRV;
450 
451 	/* default to non-inverted output, unless inverted is specified */
452 	if (device_property_read_bool(dev, "nxp,inverted-out"))
453 		mode2 |= PCA963X_MODE2_INVRT;
454 	else
455 		mode2 &= ~PCA963X_MODE2_INVRT;
456 
457 	ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
458 	if (ret < 0)
459 		return ret;
460 
461 	device_for_each_child_node_scoped(dev, child) {
462 		ret = fwnode_property_read_u32(child, "reg", &reg);
463 		if (ret)
464 			return dev_err_probe(dev, ret,
465 					     "Missing 'reg' property for node %pfw\n", child);
466 		if (reg >= chipdef->n_leds)
467 			return dev_err_probe(dev, -EINVAL,
468 					     "Invalid 'reg' property for node %pfw\n", child);
469 
470 		led->chip = chip;
471 		led->blinking = false;
472 
473 		ret = pca963x_register_led(dev, led, reg, child, chipdef, hw_blink);
474 		if (ret)
475 			return dev_err_probe(dev, ret, "Failed to register LED for node %pfw\n",
476 					     child);
477 
478 		++led;
479 	}
480 
481 	return 0;
482 }
483 
484 static int pca963x_suspend(struct device *dev)
485 {
486 	struct pca963x *chip = dev_get_drvdata(dev);
487 	u8 reg;
488 
489 	reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
490 	reg = reg | BIT(PCA963X_MODE1_SLEEP);
491 	i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
492 
493 	return 0;
494 }
495 
496 static int pca963x_resume(struct device *dev)
497 {
498 	struct pca963x *chip = dev_get_drvdata(dev);
499 	u8 reg;
500 
501 	reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
502 	reg = reg & ~BIT(PCA963X_MODE1_SLEEP);
503 	i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
504 
505 	return 0;
506 }
507 
508 static DEFINE_SIMPLE_DEV_PM_OPS(pca963x_pm, pca963x_suspend, pca963x_resume);
509 
510 static const struct of_device_id of_pca963x_match[] = {
511 	{ .compatible = "nxp,pca9632", },
512 	{ .compatible = "nxp,pca9633", },
513 	{ .compatible = "nxp,pca9634", },
514 	{ .compatible = "nxp,pca9635", },
515 	{},
516 };
517 MODULE_DEVICE_TABLE(of, of_pca963x_match);
518 
519 static int pca963x_probe(struct i2c_client *client)
520 {
521 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
522 	struct device *dev = &client->dev;
523 	struct pca963x_chipdef *chipdef;
524 	struct pca963x *chip;
525 	int i, count;
526 
527 	chipdef = &pca963x_chipdefs[id->driver_data];
528 
529 	count = device_get_child_node_count(dev);
530 	if (!count || count > chipdef->n_leds) {
531 		dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
532 			dev_fwnode(dev), chipdef->n_leds);
533 		return -EINVAL;
534 	}
535 
536 	chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
537 	if (!chip)
538 		return -ENOMEM;
539 
540 	i2c_set_clientdata(client, chip);
541 
542 	mutex_init(&chip->mutex);
543 	chip->chipdef = chipdef;
544 	chip->client = client;
545 
546 	/* Turn off LEDs by default*/
547 	for (i = 0; i < chipdef->n_leds / 4; i++)
548 		i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
549 
550 	/* Disable LED all-call address, and power down initially */
551 	i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
552 
553 	return pca963x_register_leds(client, chip);
554 }
555 
556 static struct i2c_driver pca963x_driver = {
557 	.driver = {
558 		.name	= "leds-pca963x",
559 		.of_match_table = of_pca963x_match,
560 		.pm = pm_sleep_ptr(&pca963x_pm)
561 	},
562 	.probe = pca963x_probe,
563 	.id_table = pca963x_id,
564 };
565 
566 module_i2c_driver(pca963x_driver);
567 
568 MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
569 MODULE_DESCRIPTION("PCA963X LED driver");
570 MODULE_LICENSE("GPL v2");
571