xref: /openbmc/linux/drivers/leds/leds-pca9532.c (revision 2208f39c)
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 int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
322 {
323 	int i = n_devs;
324 
325 	if (!data)
326 		return -EINVAL;
327 
328 	while (--i >= 0) {
329 		switch (data->leds[i].type) {
330 		case PCA9532_TYPE_NONE:
331 		case PCA9532_TYPE_GPIO:
332 			break;
333 		case PCA9532_TYPE_LED:
334 			led_classdev_unregister(&data->leds[i].ldev);
335 			break;
336 		case PCA9532_TYPE_N2100_BEEP:
337 			if (data->idev != NULL) {
338 				cancel_work_sync(&data->work);
339 				data->idev = NULL;
340 			}
341 			break;
342 		}
343 	}
344 
345 #ifdef CONFIG_LEDS_PCA9532_GPIO
346 	if (data->gpio.parent)
347 		gpiochip_remove(&data->gpio);
348 #endif
349 
350 	return 0;
351 }
352 
353 static int pca9532_configure(struct i2c_client *client,
354 	struct pca9532_data *data, struct pca9532_platform_data *pdata)
355 {
356 	int i, err = 0;
357 	int gpios = 0;
358 	u8 maxleds = data->chip_info->num_leds;
359 
360 	for (i = 0; i < 2; i++)	{
361 		data->pwm[i] = pdata->pwm[i];
362 		data->psc[i] = pdata->psc[i];
363 		i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
364 			data->pwm[i]);
365 		i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
366 			data->psc[i]);
367 	}
368 
369 	for (i = 0; i < data->chip_info->num_leds; i++) {
370 		struct pca9532_led *led = &data->leds[i];
371 		struct pca9532_led *pled = &pdata->leds[i];
372 		led->client = client;
373 		led->id = i;
374 		led->type = pled->type;
375 		switch (led->type) {
376 		case PCA9532_TYPE_NONE:
377 			break;
378 		case PCA9532_TYPE_GPIO:
379 			gpios++;
380 			break;
381 		case PCA9532_TYPE_LED:
382 			if (pled->state == PCA9532_KEEP)
383 				led->state = pca9532_getled(led);
384 			else
385 				led->state = pled->state;
386 			led->name = pled->name;
387 			led->ldev.name = led->name;
388 			led->ldev.default_trigger = pled->default_trigger;
389 			led->ldev.brightness = LED_OFF;
390 			led->ldev.brightness_set_blocking =
391 						pca9532_set_brightness;
392 			led->ldev.blink_set = pca9532_set_blink;
393 			err = led_classdev_register(&client->dev, &led->ldev);
394 			if (err < 0) {
395 				dev_err(&client->dev,
396 					"couldn't register LED %s\n",
397 					led->name);
398 				goto exit;
399 			}
400 			pca9532_setled(led);
401 			break;
402 		case PCA9532_TYPE_N2100_BEEP:
403 			BUG_ON(data->idev);
404 			led->state = PCA9532_PWM1;
405 			pca9532_setled(led);
406 			data->idev = devm_input_allocate_device(&client->dev);
407 			if (data->idev == NULL) {
408 				err = -ENOMEM;
409 				goto exit;
410 			}
411 			data->idev->name = pled->name;
412 			data->idev->phys = "i2c/pca9532";
413 			data->idev->id.bustype = BUS_HOST;
414 			data->idev->id.vendor = 0x001f;
415 			data->idev->id.product = 0x0001;
416 			data->idev->id.version = 0x0100;
417 			data->idev->evbit[0] = BIT_MASK(EV_SND);
418 			data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
419 						BIT_MASK(SND_TONE);
420 			data->idev->event = pca9532_event;
421 			input_set_drvdata(data->idev, data);
422 			INIT_WORK(&data->work, pca9532_input_work);
423 			err = input_register_device(data->idev);
424 			if (err) {
425 				cancel_work_sync(&data->work);
426 				data->idev = NULL;
427 				goto exit;
428 			}
429 			break;
430 		}
431 	}
432 
433 #ifdef CONFIG_LEDS_PCA9532_GPIO
434 	if (gpios) {
435 		data->gpio.label = "gpio-pca9532";
436 		data->gpio.direction_input = pca9532_gpio_direction_input;
437 		data->gpio.direction_output = pca9532_gpio_direction_output;
438 		data->gpio.set = pca9532_gpio_set_value;
439 		data->gpio.get = pca9532_gpio_get_value;
440 		data->gpio.request = pca9532_gpio_request_pin;
441 		data->gpio.can_sleep = 1;
442 		data->gpio.base = pdata->gpio_base;
443 		data->gpio.ngpio = data->chip_info->num_leds;
444 		data->gpio.parent = &client->dev;
445 		data->gpio.owner = THIS_MODULE;
446 
447 		err = gpiochip_add_data(&data->gpio, data);
448 		if (err) {
449 			/* Use data->gpio.dev as a flag for freeing gpiochip */
450 			data->gpio.parent = NULL;
451 			dev_warn(&client->dev, "could not add gpiochip\n");
452 		} else {
453 			dev_info(&client->dev, "gpios %i...%i\n",
454 				data->gpio.base, data->gpio.base +
455 				data->gpio.ngpio - 1);
456 		}
457 	}
458 #endif
459 
460 	return 0;
461 
462 exit:
463 	pca9532_destroy_devices(data, i);
464 	return err;
465 }
466 
467 static struct pca9532_platform_data *
468 pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
469 {
470 	struct pca9532_platform_data *pdata;
471 	struct device_node *child;
472 	int devid, maxleds;
473 	int i = 0;
474 	const char *state;
475 
476 	devid = (int)(uintptr_t)of_device_get_match_data(dev);
477 	maxleds = pca9532_chip_info_tbl[devid].num_leds;
478 
479 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
480 	if (!pdata)
481 		return ERR_PTR(-ENOMEM);
482 
483 	of_property_read_u8_array(np, "nxp,pwm", &pdata->pwm[0],
484 				  ARRAY_SIZE(pdata->pwm));
485 	of_property_read_u8_array(np, "nxp,psc", &pdata->psc[0],
486 				  ARRAY_SIZE(pdata->psc));
487 
488 	for_each_available_child_of_node(np, child) {
489 		if (of_property_read_string(child, "label",
490 					    &pdata->leds[i].name))
491 			pdata->leds[i].name = child->name;
492 		of_property_read_u32(child, "type", &pdata->leds[i].type);
493 		of_property_read_string(child, "linux,default-trigger",
494 					&pdata->leds[i].default_trigger);
495 		if (!of_property_read_string(child, "default-state", &state)) {
496 			if (!strcmp(state, "on"))
497 				pdata->leds[i].state = PCA9532_ON;
498 			else if (!strcmp(state, "keep"))
499 				pdata->leds[i].state = PCA9532_KEEP;
500 		}
501 		if (++i >= maxleds) {
502 			of_node_put(child);
503 			break;
504 		}
505 	}
506 
507 	return pdata;
508 }
509 
510 static int pca9532_probe(struct i2c_client *client,
511 	const struct i2c_device_id *id)
512 {
513 	int devid;
514 	struct pca9532_data *data = i2c_get_clientdata(client);
515 	struct pca9532_platform_data *pca9532_pdata =
516 			dev_get_platdata(&client->dev);
517 	struct device_node *np = dev_of_node(&client->dev);
518 
519 	if (!pca9532_pdata) {
520 		if (np) {
521 			pca9532_pdata =
522 				pca9532_of_populate_pdata(&client->dev, np);
523 			if (IS_ERR(pca9532_pdata))
524 				return PTR_ERR(pca9532_pdata);
525 		} else {
526 			dev_err(&client->dev, "no platform data\n");
527 			return -EINVAL;
528 		}
529 		devid = (int)(uintptr_t)of_device_get_match_data(&client->dev);
530 	} else {
531 		devid = id->driver_data;
532 	}
533 
534 	if (!i2c_check_functionality(client->adapter,
535 		I2C_FUNC_SMBUS_BYTE_DATA))
536 		return -EIO;
537 
538 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
539 	if (!data)
540 		return -ENOMEM;
541 
542 	data->chip_info = &pca9532_chip_info_tbl[devid];
543 
544 	dev_info(&client->dev, "setting platform data\n");
545 	i2c_set_clientdata(client, data);
546 	data->client = client;
547 	mutex_init(&data->update_lock);
548 
549 	return pca9532_configure(client, data, pca9532_pdata);
550 }
551 
552 static int pca9532_remove(struct i2c_client *client)
553 {
554 	struct pca9532_data *data = i2c_get_clientdata(client);
555 
556 	return pca9532_destroy_devices(data, data->chip_info->num_leds);
557 }
558 
559 module_i2c_driver(pca9532_driver);
560 
561 MODULE_AUTHOR("Riku Voipio");
562 MODULE_LICENSE("GPL");
563 MODULE_DESCRIPTION("PCA 9532 LED dimmer");
564