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