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