xref: /linux/drivers/leds/leds-pca9532.c (revision fa8a4d3659d0c1ad73d5f59b2e0a6d408de5b317)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * pca9532.c - 16-bit Led dimmer
4  *
5  * Copyright (C) 2011 Jan Weitzel
6  * Copyright (C) 2008 Riku Voipio
7  *
8  * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
9  */
10 
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/leds.h>
15 #include <linux/input.h>
16 #include <linux/mutex.h>
17 #include <linux/workqueue.h>
18 #include <linux/leds-pca9532.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/of.h>
21 
22 /* m =  num_leds*/
23 #define PCA9532_REG_INPUT(i)	((i) >> 3)
24 #define PCA9532_REG_OFFSET(m)	((m) >> 4)
25 #define PCA9532_REG_PSC(m, i)	(PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
26 #define PCA9532_REG_PWM(m, i)	(PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
27 #define LED_REG(m, led)		(PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
28 #define LED_NUM(led)		(led & 0x3)
29 #define LED_SHIFT(led)		(LED_NUM(led) * 2)
30 #define LED_MASK(led)		(0x3 << LED_SHIFT(led))
31 
32 #define PCA9532_PWM_PERIOD_DIV	152
33 #define PCA9532_PWM_DUTY_DIV	256
34 
35 #define ldev_to_led(c)       container_of(c, struct pca9532_led, ldev)
36 
37 struct pca9532_chip_info {
38 	u8	num_leds;
39 };
40 
41 struct pca9532_data {
42 	struct i2c_client *client;
43 	struct pca9532_led leds[16];
44 	struct mutex update_lock;
45 	struct input_dev *idev;
46 	struct work_struct work;
47 #ifdef CONFIG_LEDS_PCA9532_GPIO
48 	struct gpio_chip gpio;
49 #endif
50 	const struct pca9532_chip_info *chip_info;
51 
52 #define PCA9532_PWM_ID_0	0
53 #define PCA9532_PWM_ID_1	1
54 	u8 pwm[2];
55 	u8 psc[2];
56 	bool hw_blink;
57 };
58 
59 static int pca9532_probe(struct i2c_client *client);
60 static void pca9532_remove(struct i2c_client *client);
61 
62 enum {
63 	pca9530,
64 	pca9531,
65 	pca9532,
66 	pca9533,
67 };
68 
69 static const struct i2c_device_id pca9532_id[] = {
70 	{ "pca9530", pca9530 },
71 	{ "pca9531", pca9531 },
72 	{ "pca9532", pca9532 },
73 	{ "pca9533", pca9533 },
74 	{ }
75 };
76 
77 MODULE_DEVICE_TABLE(i2c, pca9532_id);
78 
79 static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
80 	[pca9530] = {
81 		.num_leds = 2,
82 	},
83 	[pca9531] = {
84 		.num_leds = 8,
85 	},
86 	[pca9532] = {
87 		.num_leds = 16,
88 	},
89 	[pca9533] = {
90 		.num_leds = 4,
91 	},
92 };
93 
94 #ifdef CONFIG_OF
95 static const struct of_device_id of_pca9532_leds_match[] = {
96 	{ .compatible = "nxp,pca9530", .data = (void *)pca9530 },
97 	{ .compatible = "nxp,pca9531", .data = (void *)pca9531 },
98 	{ .compatible = "nxp,pca9532", .data = (void *)pca9532 },
99 	{ .compatible = "nxp,pca9533", .data = (void *)pca9533 },
100 	{},
101 };
102 
103 MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
104 #endif
105 
106 static struct i2c_driver pca9532_driver = {
107 	.driver = {
108 		.name = "leds-pca953x",
109 		.of_match_table = of_match_ptr(of_pca9532_leds_match),
110 	},
111 	.probe = pca9532_probe,
112 	.remove = pca9532_remove,
113 	.id_table = pca9532_id,
114 };
115 
116 /* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
117  * the clever Thecus people are using one pwm to drive the beeper. So,
118  * as a compromise we average one pwm to the values requested by all
119  * leds that are not ON/OFF.
120  * */
121 static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
122 	enum led_brightness value)
123 {
124 	int a = 0, b = 0, i = 0;
125 	struct pca9532_data *data = i2c_get_clientdata(client);
126 	for (i = 0; i < data->chip_info->num_leds; i++) {
127 		if (data->leds[i].type == PCA9532_TYPE_LED &&
128 			data->leds[i].state == PCA9532_PWM0+pwm) {
129 				a++;
130 				b += data->leds[i].ldev.brightness;
131 		}
132 	}
133 	if (a == 0) {
134 		dev_err(&client->dev,
135 		"fear of division by zero %d/%d, wanted %d\n",
136 			b, a, value);
137 		return -EINVAL;
138 	}
139 	b = b/a;
140 	if (b > 0xFF)
141 		return -EINVAL;
142 	data->pwm[pwm] = b;
143 	data->psc[pwm] = blink;
144 	return 0;
145 }
146 
147 static int pca9532_setpwm(struct i2c_client *client, int pwm)
148 {
149 	struct pca9532_data *data = i2c_get_clientdata(client);
150 	u8 maxleds = data->chip_info->num_leds;
151 
152 	mutex_lock(&data->update_lock);
153 	i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
154 		data->pwm[pwm]);
155 	i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
156 		data->psc[pwm]);
157 	mutex_unlock(&data->update_lock);
158 	return 0;
159 }
160 
161 /* Set LED routing */
162 static void pca9532_setled(struct pca9532_led *led)
163 {
164 	struct i2c_client *client = led->client;
165 	struct pca9532_data *data = i2c_get_clientdata(client);
166 	u8 maxleds = data->chip_info->num_leds;
167 	char reg;
168 
169 	mutex_lock(&data->update_lock);
170 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
171 	/* zero led bits */
172 	reg = reg & ~LED_MASK(led->id);
173 	/* set the new value */
174 	reg = reg | (led->state << LED_SHIFT(led->id));
175 	i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
176 	mutex_unlock(&data->update_lock);
177 }
178 
179 static int pca9532_set_brightness(struct led_classdev *led_cdev,
180 	enum led_brightness value)
181 {
182 	int err = 0;
183 	struct pca9532_led *led = ldev_to_led(led_cdev);
184 
185 	if (value == LED_OFF)
186 		led->state = PCA9532_OFF;
187 	else if (value == LED_FULL)
188 		led->state = PCA9532_ON;
189 	else {
190 		led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
191 		err = pca9532_calcpwm(led->client, PCA9532_PWM_ID_0, 0, value);
192 		if (err)
193 			return err;
194 	}
195 	if (led->state == PCA9532_PWM0)
196 		pca9532_setpwm(led->client, PCA9532_PWM_ID_0);
197 	pca9532_setled(led);
198 	return err;
199 }
200 
201 static int pca9532_update_hw_blink(struct pca9532_led *led,
202 				   unsigned long delay_on, unsigned long delay_off)
203 {
204 	struct pca9532_data *data = i2c_get_clientdata(led->client);
205 	unsigned int psc;
206 	int i;
207 
208 	/* Look for others LEDs that already use PWM1 */
209 	for (i = 0; i < data->chip_info->num_leds; i++) {
210 		struct pca9532_led *other = &data->leds[i];
211 
212 		if (other == led)
213 			continue;
214 
215 		if (other->state == PCA9532_PWM1) {
216 			if (other->ldev.blink_delay_on != delay_on ||
217 			    other->ldev.blink_delay_off != delay_off) {
218 				dev_err(&led->client->dev,
219 					"HW can handle only one blink configuration at a time\n");
220 				return -EINVAL;
221 			}
222 		}
223 	}
224 
225 	psc = ((delay_on + delay_off) * PCA9532_PWM_PERIOD_DIV - 1) / 1000;
226 	if (psc > U8_MAX) {
227 		dev_err(&led->client->dev, "Blink period too long to be handled by hardware\n");
228 		return -EINVAL;
229 	}
230 
231 	led->state = PCA9532_PWM1;
232 	data->psc[PCA9532_PWM_ID_1] = psc;
233 	data->pwm[PCA9532_PWM_ID_1] = (delay_on * PCA9532_PWM_DUTY_DIV) / (delay_on + delay_off);
234 
235 	return pca9532_setpwm(data->client, PCA9532_PWM_ID_1);
236 }
237 
238 static int pca9532_set_blink(struct led_classdev *led_cdev,
239 	unsigned long *delay_on, unsigned long *delay_off)
240 {
241 	struct pca9532_led *led = ldev_to_led(led_cdev);
242 	struct i2c_client *client = led->client;
243 	struct pca9532_data *data = i2c_get_clientdata(client);
244 	int err;
245 
246 	if (!data->hw_blink)
247 		return -EINVAL;
248 
249 	if (*delay_on == 0 && *delay_off == 0) {
250 		/* led subsystem ask us for a blink rate */
251 		*delay_on = 500;
252 		*delay_off = 500;
253 	}
254 
255 	err = pca9532_update_hw_blink(led, *delay_on, *delay_off);
256 	if (err)
257 		return err;
258 
259 	pca9532_setled(led);
260 
261 	return 0;
262 }
263 
264 static int pca9532_event(struct input_dev *dev, unsigned int type,
265 	unsigned int code, int value)
266 {
267 	struct pca9532_data *data = input_get_drvdata(dev);
268 
269 	if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
270 		return -1;
271 
272 	/* XXX: allow different kind of beeps with psc/pwm modifications */
273 	if (value > 1 && value < 32767)
274 		data->pwm[PCA9532_PWM_ID_1] = 127;
275 	else
276 		data->pwm[PCA9532_PWM_ID_1] = 0;
277 
278 	schedule_work(&data->work);
279 
280 	return 0;
281 }
282 
283 static void pca9532_input_work(struct work_struct *work)
284 {
285 	struct pca9532_data *data =
286 		container_of(work, struct pca9532_data, work);
287 	u8 maxleds = data->chip_info->num_leds;
288 
289 	mutex_lock(&data->update_lock);
290 	i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
291 		data->pwm[PCA9532_PWM_ID_1]);
292 	mutex_unlock(&data->update_lock);
293 }
294 
295 static enum pca9532_state pca9532_getled(struct pca9532_led *led)
296 {
297 	struct i2c_client *client = led->client;
298 	struct pca9532_data *data = i2c_get_clientdata(client);
299 	u8 maxleds = data->chip_info->num_leds;
300 	char reg;
301 	enum pca9532_state ret;
302 
303 	mutex_lock(&data->update_lock);
304 	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
305 	ret = (reg & LED_MASK(led->id)) >> LED_SHIFT(led->id);
306 	mutex_unlock(&data->update_lock);
307 	return ret;
308 }
309 
310 #ifdef CONFIG_LEDS_PCA9532_GPIO
311 static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
312 {
313 	struct pca9532_data *data = gpiochip_get_data(gc);
314 	struct pca9532_led *led = &data->leds[offset];
315 
316 	if (led->type == PCA9532_TYPE_GPIO)
317 		return 0;
318 
319 	return -EBUSY;
320 }
321 
322 static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
323 {
324 	struct pca9532_data *data = gpiochip_get_data(gc);
325 	struct pca9532_led *led = &data->leds[offset];
326 
327 	if (val)
328 		led->state = PCA9532_ON;
329 	else
330 		led->state = PCA9532_OFF;
331 
332 	pca9532_setled(led);
333 }
334 
335 static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
336 {
337 	struct pca9532_data *data = gpiochip_get_data(gc);
338 	unsigned char reg;
339 
340 	reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
341 
342 	return !!(reg & (1 << (offset % 8)));
343 }
344 
345 static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
346 {
347 	/* To use as input ensure pin is not driven */
348 	pca9532_gpio_set_value(gc, offset, 0);
349 
350 	return 0;
351 }
352 
353 static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
354 {
355 	pca9532_gpio_set_value(gc, offset, val);
356 
357 	return 0;
358 }
359 #endif /* CONFIG_LEDS_PCA9532_GPIO */
360 
361 static void pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
362 {
363 	int i = n_devs;
364 
365 	while (--i >= 0) {
366 		switch (data->leds[i].type) {
367 		case PCA9532_TYPE_NONE:
368 		case PCA9532_TYPE_GPIO:
369 			break;
370 		case PCA9532_TYPE_LED:
371 			led_classdev_unregister(&data->leds[i].ldev);
372 			break;
373 		case PCA9532_TYPE_N2100_BEEP:
374 			if (data->idev != NULL) {
375 				cancel_work_sync(&data->work);
376 				data->idev = NULL;
377 			}
378 			break;
379 		}
380 	}
381 
382 #ifdef CONFIG_LEDS_PCA9532_GPIO
383 	if (data->gpio.parent)
384 		gpiochip_remove(&data->gpio);
385 #endif
386 }
387 
388 static int pca9532_configure(struct i2c_client *client,
389 	struct pca9532_data *data, struct pca9532_platform_data *pdata)
390 {
391 	int i, err = 0;
392 	int gpios = 0;
393 	u8 maxleds = data->chip_info->num_leds;
394 
395 	for (i = 0; i < 2; i++)	{
396 		data->pwm[i] = pdata->pwm[i];
397 		data->psc[i] = pdata->psc[i];
398 		i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
399 			data->pwm[i]);
400 		i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
401 			data->psc[i]);
402 	}
403 
404 	data->hw_blink = true;
405 	for (i = 0; i < data->chip_info->num_leds; i++) {
406 		struct pca9532_led *led = &data->leds[i];
407 		struct pca9532_led *pled = &pdata->leds[i];
408 		led->client = client;
409 		led->id = i;
410 		led->type = pled->type;
411 		switch (led->type) {
412 		case PCA9532_TYPE_NONE:
413 			break;
414 		case PCA9532_TYPE_GPIO:
415 			gpios++;
416 			break;
417 		case PCA9532_TYPE_LED:
418 			if (pled->state == PCA9532_KEEP)
419 				led->state = pca9532_getled(led);
420 			else
421 				led->state = pled->state;
422 			led->name = pled->name;
423 			led->ldev.name = led->name;
424 			led->ldev.default_trigger = pled->default_trigger;
425 			led->ldev.brightness = LED_OFF;
426 			led->ldev.brightness_set_blocking =
427 						pca9532_set_brightness;
428 			led->ldev.blink_set = pca9532_set_blink;
429 			err = led_classdev_register(&client->dev, &led->ldev);
430 			if (err < 0) {
431 				dev_err(&client->dev,
432 					"couldn't register LED %s\n",
433 					led->name);
434 				goto exit;
435 			}
436 			pca9532_setled(led);
437 			break;
438 		case PCA9532_TYPE_N2100_BEEP:
439 			/* PWM1 is reserved for beeper so blink will not use hardware */
440 			data->hw_blink = false;
441 			BUG_ON(data->idev);
442 			led->state = PCA9532_PWM1;
443 			pca9532_setled(led);
444 			data->idev = devm_input_allocate_device(&client->dev);
445 			if (data->idev == NULL) {
446 				err = -ENOMEM;
447 				goto exit;
448 			}
449 			data->idev->name = pled->name;
450 			data->idev->phys = "i2c/pca9532";
451 			data->idev->id.bustype = BUS_HOST;
452 			data->idev->id.vendor = 0x001f;
453 			data->idev->id.product = 0x0001;
454 			data->idev->id.version = 0x0100;
455 			data->idev->evbit[0] = BIT_MASK(EV_SND);
456 			data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
457 						BIT_MASK(SND_TONE);
458 			data->idev->event = pca9532_event;
459 			input_set_drvdata(data->idev, data);
460 			INIT_WORK(&data->work, pca9532_input_work);
461 			err = input_register_device(data->idev);
462 			if (err) {
463 				cancel_work_sync(&data->work);
464 				data->idev = NULL;
465 				goto exit;
466 			}
467 			break;
468 		}
469 	}
470 
471 #ifdef CONFIG_LEDS_PCA9532_GPIO
472 	if (gpios) {
473 		data->gpio.label = "gpio-pca9532";
474 		data->gpio.direction_input = pca9532_gpio_direction_input;
475 		data->gpio.direction_output = pca9532_gpio_direction_output;
476 		data->gpio.set = pca9532_gpio_set_value;
477 		data->gpio.get = pca9532_gpio_get_value;
478 		data->gpio.request = pca9532_gpio_request_pin;
479 		data->gpio.can_sleep = 1;
480 		data->gpio.base = pdata->gpio_base;
481 		data->gpio.ngpio = data->chip_info->num_leds;
482 		data->gpio.parent = &client->dev;
483 		data->gpio.owner = THIS_MODULE;
484 
485 		err = gpiochip_add_data(&data->gpio, data);
486 		if (err) {
487 			/* Use data->gpio.dev as a flag for freeing gpiochip */
488 			data->gpio.parent = NULL;
489 			dev_warn(&client->dev, "could not add gpiochip\n");
490 		} else {
491 			dev_info(&client->dev, "gpios %i...%i\n",
492 				data->gpio.base, data->gpio.base +
493 				data->gpio.ngpio - 1);
494 		}
495 	}
496 #endif
497 
498 	return 0;
499 
500 exit:
501 	pca9532_destroy_devices(data, i);
502 	return err;
503 }
504 
505 static struct pca9532_platform_data *
506 pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
507 {
508 	struct pca9532_platform_data *pdata;
509 	struct device_node *child;
510 	int devid, maxleds;
511 	int i = 0;
512 	const char *state;
513 
514 	devid = (int)(uintptr_t)of_device_get_match_data(dev);
515 	maxleds = pca9532_chip_info_tbl[devid].num_leds;
516 
517 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
518 	if (!pdata)
519 		return ERR_PTR(-ENOMEM);
520 
521 	pdata->gpio_base = -1;
522 
523 	of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[PCA9532_PWM_ID_0],
524 				  ARRAY_SIZE(pdata->pwm));
525 	of_property_read_u8_array(np, "nxp,psc", &pdata->psc[PCA9532_PWM_ID_0],
526 				  ARRAY_SIZE(pdata->psc));
527 
528 	for_each_available_child_of_node(np, child) {
529 		if (of_property_read_string(child, "label",
530 					    &pdata->leds[i].name))
531 			pdata->leds[i].name = child->name;
532 		of_property_read_u32(child, "type", &pdata->leds[i].type);
533 		of_property_read_string(child, "linux,default-trigger",
534 					&pdata->leds[i].default_trigger);
535 		if (!of_property_read_string(child, "default-state", &state)) {
536 			if (!strcmp(state, "on"))
537 				pdata->leds[i].state = PCA9532_ON;
538 			else if (!strcmp(state, "keep"))
539 				pdata->leds[i].state = PCA9532_KEEP;
540 		}
541 		if (++i >= maxleds) {
542 			of_node_put(child);
543 			break;
544 		}
545 	}
546 
547 	return pdata;
548 }
549 
550 static int pca9532_probe(struct i2c_client *client)
551 {
552 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
553 	int devid;
554 	struct pca9532_data *data = i2c_get_clientdata(client);
555 	struct pca9532_platform_data *pca9532_pdata =
556 			dev_get_platdata(&client->dev);
557 	struct device_node *np = dev_of_node(&client->dev);
558 
559 	if (!pca9532_pdata) {
560 		if (np) {
561 			pca9532_pdata =
562 				pca9532_of_populate_pdata(&client->dev, np);
563 			if (IS_ERR(pca9532_pdata))
564 				return PTR_ERR(pca9532_pdata);
565 		} else {
566 			dev_err(&client->dev, "no platform data\n");
567 			return -EINVAL;
568 		}
569 		devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
570 	} else {
571 		devid = id->driver_data;
572 	}
573 
574 	if (!i2c_check_functionality(client->adapter,
575 		I2C_FUNC_SMBUS_BYTE_DATA))
576 		return -EIO;
577 
578 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
579 	if (!data)
580 		return -ENOMEM;
581 
582 	data->chip_info = &pca9532_chip_info_tbl[devid];
583 
584 	dev_info(&client->dev, "setting platform data\n");
585 	i2c_set_clientdata(client, data);
586 	data->client = client;
587 	mutex_init(&data->update_lock);
588 
589 	return pca9532_configure(client, data, pca9532_pdata);
590 }
591 
592 static void pca9532_remove(struct i2c_client *client)
593 {
594 	struct pca9532_data *data = i2c_get_clientdata(client);
595 
596 	pca9532_destroy_devices(data, data->chip_info->num_leds);
597 }
598 
599 module_i2c_driver(pca9532_driver);
600 
601 MODULE_AUTHOR("Riku Voipio");
602 MODULE_LICENSE("GPL");
603 MODULE_DESCRIPTION("PCA 9532 LED dimmer");
604