1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright 2007-2008 Extreme Engineering Solutions, Inc.
4 *
5 * Author: Nate Case <ncase@xes-inc.com>
6 *
7 * LED driver for various PCA955x I2C LED drivers
8 *
9 * Supported devices:
10 *
11 *	Device		Description		7-bit slave address
12 *	------		-----------		-------------------
13 *	PCA9550		2-bit driver		0x60 .. 0x61
14 *	PCA9551		8-bit driver		0x60 .. 0x67
15 *	PCA9552		16-bit driver		0x60 .. 0x67
16 *	PCA9553/01	4-bit driver		0x62
17 *	PCA9553/02	4-bit driver		0x63
18 *
19 * Philips PCA955x LED driver chips follow a register map as shown below:
20 *
21 *	Control Register		Description
22 *	----------------		-----------
23 *	0x0				Input register 0
24 *					..
25 *	NUM_INPUT_REGS - 1		Last Input register X
26 *
27 *	NUM_INPUT_REGS			Frequency prescaler 0
28 *	NUM_INPUT_REGS + 1		PWM register 0
29 *	NUM_INPUT_REGS + 2		Frequency prescaler 1
30 *	NUM_INPUT_REGS + 3		PWM register 1
31 *
32 *	NUM_INPUT_REGS + 4		LED selector 0
33 *	NUM_INPUT_REGS + 4
34 *	    + NUM_LED_REGS - 1		Last LED selector
35 *
36 *  where NUM_INPUT_REGS and NUM_LED_REGS vary depending on how many
37 *  bits the chip supports.
38 */
39
40#include <linux/bitops.h>
41#include <linux/ctype.h>
42#include <linux/delay.h>
43#include <linux/err.h>
44#include <linux/gpio/driver.h>
45#include <linux/i2c.h>
46#include <linux/leds.h>
47#include <linux/module.h>
48#include <linux/of.h>
49#include <linux/property.h>
50#include <linux/slab.h>
51#include <linux/string.h>
52
53#include <dt-bindings/leds/leds-pca955x.h>
54
55/* LED select registers determine the source that drives LED outputs */
56#define PCA955X_LS_LED_ON	0x0	/* Output LOW */
57#define PCA955X_LS_LED_OFF	0x1	/* Output HI-Z */
58#define PCA955X_LS_BLINK0	0x2	/* Blink at PWM0 rate */
59#define PCA955X_LS_BLINK1	0x3	/* Blink at PWM1 rate */
60
61#define PCA955X_GPIO_INPUT	LED_OFF
62#define PCA955X_GPIO_HIGH	LED_OFF
63#define PCA955X_GPIO_LOW	LED_FULL
64
65enum pca955x_type {
66	pca9550,
67	pca9551,
68	pca9552,
69	ibm_pca9552,
70	pca9553,
71};
72
73struct pca955x_chipdef {
74	int			bits;
75	u8			slv_addr;	/* 7-bit slave address mask */
76	int			slv_addr_shift;	/* Number of bits to ignore */
77};
78
79static struct pca955x_chipdef pca955x_chipdefs[] = {
80	[pca9550] = {
81		.bits		= 2,
82		.slv_addr	= /* 110000x */ 0x60,
83		.slv_addr_shift	= 1,
84	},
85	[pca9551] = {
86		.bits		= 8,
87		.slv_addr	= /* 1100xxx */ 0x60,
88		.slv_addr_shift	= 3,
89	},
90	[pca9552] = {
91		.bits		= 16,
92		.slv_addr	= /* 1100xxx */ 0x60,
93		.slv_addr_shift	= 3,
94	},
95	[ibm_pca9552] = {
96		.bits		= 16,
97		.slv_addr	= /* 0110xxx */ 0x30,
98		.slv_addr_shift	= 3,
99	},
100	[pca9553] = {
101		.bits		= 4,
102		.slv_addr	= /* 110001x */ 0x62,
103		.slv_addr_shift	= 1,
104	},
105};
106
107static const struct i2c_device_id pca955x_id[] = {
108	{ "pca9550", pca9550 },
109	{ "pca9551", pca9551 },
110	{ "pca9552", pca9552 },
111	{ "ibm-pca9552", ibm_pca9552 },
112	{ "pca9553", pca9553 },
113	{ }
114};
115MODULE_DEVICE_TABLE(i2c, pca955x_id);
116
117struct pca955x {
118	struct mutex lock;
119	struct pca955x_led *leds;
120	struct pca955x_chipdef	*chipdef;
121	struct i2c_client	*client;
122	unsigned long active_pins;
123#ifdef CONFIG_LEDS_PCA955X_GPIO
124	struct gpio_chip gpio;
125#endif
126};
127
128struct pca955x_led {
129	struct pca955x	*pca955x;
130	struct led_classdev	led_cdev;
131	int			led_num;	/* 0 .. 15 potentially */
132	u32			type;
133	enum led_default_state	default_state;
134	struct fwnode_handle	*fwnode;
135};
136
137struct pca955x_platform_data {
138	struct pca955x_led	*leds;
139	int			num_leds;
140};
141
142/* 8 bits per input register */
143static inline int pca95xx_num_input_regs(int bits)
144{
145	return (bits + 7) / 8;
146}
147
148/*
149 * Return an LED selector register value based on an existing one, with
150 * the appropriate 2-bit state value set for the given LED number (0-3).
151 */
152static inline u8 pca955x_ledsel(u8 oldval, int led_num, int state)
153{
154	return (oldval & (~(0x3 << (led_num << 1)))) |
155		((state & 0x3) << (led_num << 1));
156}
157
158/*
159 * Write to frequency prescaler register, used to program the
160 * period of the PWM output.  period = (PSCx + 1) / 38
161 */
162static int pca955x_write_psc(struct i2c_client *client, int n, u8 val)
163{
164	struct pca955x *pca955x = i2c_get_clientdata(client);
165	u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + (2 * n);
166	int ret;
167
168	ret = i2c_smbus_write_byte_data(client, cmd, val);
169	if (ret < 0)
170		dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
171			__func__, n, val, ret);
172	return ret;
173}
174
175/*
176 * Write to PWM register, which determines the duty cycle of the
177 * output.  LED is OFF when the count is less than the value of this
178 * register, and ON when it is greater.  If PWMx == 0, LED is always OFF.
179 *
180 * Duty cycle is (256 - PWMx) / 256
181 */
182static int pca955x_write_pwm(struct i2c_client *client, int n, u8 val)
183{
184	struct pca955x *pca955x = i2c_get_clientdata(client);
185	u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
186	int ret;
187
188	ret = i2c_smbus_write_byte_data(client, cmd, val);
189	if (ret < 0)
190		dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
191			__func__, n, val, ret);
192	return ret;
193}
194
195/*
196 * Write to LED selector register, which determines the source that
197 * drives the LED output.
198 */
199static int pca955x_write_ls(struct i2c_client *client, int n, u8 val)
200{
201	struct pca955x *pca955x = i2c_get_clientdata(client);
202	u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
203	int ret;
204
205	ret = i2c_smbus_write_byte_data(client, cmd, val);
206	if (ret < 0)
207		dev_err(&client->dev, "%s: reg 0x%x, val 0x%x, err %d\n",
208			__func__, n, val, ret);
209	return ret;
210}
211
212/*
213 * Read the LED selector register, which determines the source that
214 * drives the LED output.
215 */
216static int pca955x_read_ls(struct i2c_client *client, int n, u8 *val)
217{
218	struct pca955x *pca955x = i2c_get_clientdata(client);
219	u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 4 + n;
220	int ret;
221
222	ret = i2c_smbus_read_byte_data(client, cmd);
223	if (ret < 0) {
224		dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
225			__func__, n, ret);
226		return ret;
227	}
228	*val = (u8)ret;
229	return 0;
230}
231
232static int pca955x_read_pwm(struct i2c_client *client, int n, u8 *val)
233{
234	struct pca955x *pca955x = i2c_get_clientdata(client);
235	u8 cmd = pca95xx_num_input_regs(pca955x->chipdef->bits) + 1 + (2 * n);
236	int ret;
237
238	ret = i2c_smbus_read_byte_data(client, cmd);
239	if (ret < 0) {
240		dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
241			__func__, n, ret);
242		return ret;
243	}
244	*val = (u8)ret;
245	return 0;
246}
247
248static enum led_brightness pca955x_led_get(struct led_classdev *led_cdev)
249{
250	struct pca955x_led *pca955x_led = container_of(led_cdev,
251						       struct pca955x_led,
252						       led_cdev);
253	struct pca955x *pca955x = pca955x_led->pca955x;
254	u8 ls, pwm;
255	int ret;
256
257	ret = pca955x_read_ls(pca955x->client, pca955x_led->led_num / 4, &ls);
258	if (ret)
259		return ret;
260
261	ls = (ls >> ((pca955x_led->led_num % 4) << 1)) & 0x3;
262	switch (ls) {
263	case PCA955X_LS_LED_ON:
264		ret = LED_FULL;
265		break;
266	case PCA955X_LS_LED_OFF:
267		ret = LED_OFF;
268		break;
269	case PCA955X_LS_BLINK0:
270		ret = LED_HALF;
271		break;
272	case PCA955X_LS_BLINK1:
273		ret = pca955x_read_pwm(pca955x->client, 1, &pwm);
274		if (ret)
275			return ret;
276		ret = 255 - pwm;
277		break;
278	}
279
280	return ret;
281}
282
283static int pca955x_led_set(struct led_classdev *led_cdev,
284			    enum led_brightness value)
285{
286	struct pca955x_led *pca955x_led;
287	struct pca955x *pca955x;
288	u8 ls;
289	int chip_ls;	/* which LSx to use (0-3 potentially) */
290	int ls_led;	/* which set of bits within LSx to use (0-3) */
291	int ret;
292
293	pca955x_led = container_of(led_cdev, struct pca955x_led, led_cdev);
294	pca955x = pca955x_led->pca955x;
295
296	chip_ls = pca955x_led->led_num / 4;
297	ls_led = pca955x_led->led_num % 4;
298
299	mutex_lock(&pca955x->lock);
300
301	ret = pca955x_read_ls(pca955x->client, chip_ls, &ls);
302	if (ret)
303		goto out;
304
305	switch (value) {
306	case LED_FULL:
307		ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_ON);
308		break;
309	case LED_OFF:
310		ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_LED_OFF);
311		break;
312	case LED_HALF:
313		ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK0);
314		break;
315	default:
316		/*
317		 * Use PWM1 for all other values.  This has the unwanted
318		 * side effect of making all LEDs on the chip share the
319		 * same brightness level if set to a value other than
320		 * OFF, HALF, or FULL.  But, this is probably better than
321		 * just turning off for all other values.
322		 */
323		ret = pca955x_write_pwm(pca955x->client, 1, 255 - value);
324		if (ret)
325			goto out;
326		ls = pca955x_ledsel(ls, ls_led, PCA955X_LS_BLINK1);
327		break;
328	}
329
330	ret = pca955x_write_ls(pca955x->client, chip_ls, ls);
331
332out:
333	mutex_unlock(&pca955x->lock);
334
335	return ret;
336}
337
338#ifdef CONFIG_LEDS_PCA955X_GPIO
339/*
340 * Read the INPUT register, which contains the state of LEDs.
341 */
342static int pca955x_read_input(struct i2c_client *client, int n, u8 *val)
343{
344	int ret = i2c_smbus_read_byte_data(client, n);
345
346	if (ret < 0) {
347		dev_err(&client->dev, "%s: reg 0x%x, err %d\n",
348			__func__, n, ret);
349		return ret;
350	}
351	*val = (u8)ret;
352	return 0;
353
354}
355
356static int pca955x_gpio_request_pin(struct gpio_chip *gc, unsigned int offset)
357{
358	struct pca955x *pca955x = gpiochip_get_data(gc);
359
360	return test_and_set_bit(offset, &pca955x->active_pins) ? -EBUSY : 0;
361}
362
363static void pca955x_gpio_free_pin(struct gpio_chip *gc, unsigned int offset)
364{
365	struct pca955x *pca955x = gpiochip_get_data(gc);
366
367	clear_bit(offset, &pca955x->active_pins);
368}
369
370static int pca955x_set_value(struct gpio_chip *gc, unsigned int offset,
371			     int val)
372{
373	struct pca955x *pca955x = gpiochip_get_data(gc);
374	struct pca955x_led *led = &pca955x->leds[offset];
375
376	if (val)
377		return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_HIGH);
378
379	return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_LOW);
380}
381
382static void pca955x_gpio_set_value(struct gpio_chip *gc, unsigned int offset,
383				   int val)
384{
385	pca955x_set_value(gc, offset, val);
386}
387
388static int pca955x_gpio_get_value(struct gpio_chip *gc, unsigned int offset)
389{
390	struct pca955x *pca955x = gpiochip_get_data(gc);
391	struct pca955x_led *led = &pca955x->leds[offset];
392	u8 reg = 0;
393
394	/* There is nothing we can do about errors */
395	pca955x_read_input(pca955x->client, led->led_num / 8, &reg);
396
397	return !!(reg & (1 << (led->led_num % 8)));
398}
399
400static int pca955x_gpio_direction_input(struct gpio_chip *gc,
401					unsigned int offset)
402{
403	struct pca955x *pca955x = gpiochip_get_data(gc);
404	struct pca955x_led *led = &pca955x->leds[offset];
405
406	/* To use as input ensure pin is not driven. */
407	return pca955x_led_set(&led->led_cdev, PCA955X_GPIO_INPUT);
408}
409
410static int pca955x_gpio_direction_output(struct gpio_chip *gc,
411					 unsigned int offset, int val)
412{
413	return pca955x_set_value(gc, offset, val);
414}
415#endif /* CONFIG_LEDS_PCA955X_GPIO */
416
417static struct pca955x_platform_data *
418pca955x_get_pdata(struct i2c_client *client, struct pca955x_chipdef *chip)
419{
420	struct pca955x_platform_data *pdata;
421	struct pca955x_led *led;
422	struct fwnode_handle *child;
423	int count;
424
425	count = device_get_child_node_count(&client->dev);
426	if (count > chip->bits)
427		return ERR_PTR(-ENODEV);
428
429	pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
430	if (!pdata)
431		return ERR_PTR(-ENOMEM);
432
433	pdata->leds = devm_kcalloc(&client->dev,
434				   chip->bits, sizeof(struct pca955x_led),
435				   GFP_KERNEL);
436	if (!pdata->leds)
437		return ERR_PTR(-ENOMEM);
438
439	device_for_each_child_node(&client->dev, child) {
440		u32 reg;
441		int res;
442
443		res = fwnode_property_read_u32(child, "reg", &reg);
444		if ((res != 0) || (reg >= chip->bits))
445			continue;
446
447		led = &pdata->leds[reg];
448		led->type = PCA955X_TYPE_LED;
449		led->fwnode = child;
450		led->default_state = led_init_default_state_get(child);
451
452		fwnode_property_read_u32(child, "type", &led->type);
453	}
454
455	pdata->num_leds = chip->bits;
456
457	return pdata;
458}
459
460static const struct of_device_id of_pca955x_match[] = {
461	{ .compatible = "nxp,pca9550", .data = (void *)pca9550 },
462	{ .compatible = "nxp,pca9551", .data = (void *)pca9551 },
463	{ .compatible = "nxp,pca9552", .data = (void *)pca9552 },
464	{ .compatible = "ibm,pca9552", .data = (void *)ibm_pca9552 },
465	{ .compatible = "nxp,pca9553", .data = (void *)pca9553 },
466	{},
467};
468MODULE_DEVICE_TABLE(of, of_pca955x_match);
469
470static int pca955x_probe(struct i2c_client *client)
471{
472	struct pca955x *pca955x;
473	struct pca955x_led *pca955x_led;
474	struct pca955x_chipdef *chip;
475	struct led_classdev *led;
476	struct led_init_data init_data;
477	struct i2c_adapter *adapter;
478	int i, err;
479	struct pca955x_platform_data *pdata;
480	bool set_default_label = false;
481	bool keep_pwm = false;
482	char default_label[8];
483	enum pca955x_type chip_type;
484	const void *md = device_get_match_data(&client->dev);
485
486	if (md) {
487		chip_type = (enum pca955x_type)md;
488	} else {
489		const struct i2c_device_id *id = i2c_match_id(pca955x_id,
490							      client);
491
492		if (id) {
493			chip_type = (enum pca955x_type)id->driver_data;
494		} else {
495			dev_err(&client->dev, "unknown chip\n");
496			return -ENODEV;
497		}
498	}
499
500	chip = &pca955x_chipdefs[chip_type];
501	adapter = client->adapter;
502	pdata = dev_get_platdata(&client->dev);
503	if (!pdata) {
504		pdata =	pca955x_get_pdata(client, chip);
505		if (IS_ERR(pdata))
506			return PTR_ERR(pdata);
507	}
508
509	/* Make sure the slave address / chip type combo given is possible */
510	if ((client->addr & ~((1 << chip->slv_addr_shift) - 1)) !=
511	    chip->slv_addr) {
512		dev_err(&client->dev, "invalid slave address %02x\n",
513			client->addr);
514		return -ENODEV;
515	}
516
517	dev_info(&client->dev, "leds-pca955x: Using %s %d-bit LED driver at "
518		 "slave address 0x%02x\n", client->name, chip->bits,
519		 client->addr);
520
521	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
522		return -EIO;
523
524	if (pdata->num_leds != chip->bits) {
525		dev_err(&client->dev,
526			"board info claims %d LEDs on a %d-bit chip\n",
527			pdata->num_leds, chip->bits);
528		return -ENODEV;
529	}
530
531	pca955x = devm_kzalloc(&client->dev, sizeof(*pca955x), GFP_KERNEL);
532	if (!pca955x)
533		return -ENOMEM;
534
535	pca955x->leds = devm_kcalloc(&client->dev, chip->bits,
536				     sizeof(*pca955x_led), GFP_KERNEL);
537	if (!pca955x->leds)
538		return -ENOMEM;
539
540	i2c_set_clientdata(client, pca955x);
541
542	mutex_init(&pca955x->lock);
543	pca955x->client = client;
544	pca955x->chipdef = chip;
545
546	init_data.devname_mandatory = false;
547	init_data.devicename = "pca955x";
548
549	for (i = 0; i < chip->bits; i++) {
550		pca955x_led = &pca955x->leds[i];
551		pca955x_led->led_num = i;
552		pca955x_led->pca955x = pca955x;
553		pca955x_led->type = pdata->leds[i].type;
554
555		switch (pca955x_led->type) {
556		case PCA955X_TYPE_NONE:
557		case PCA955X_TYPE_GPIO:
558			break;
559		case PCA955X_TYPE_LED:
560			led = &pca955x_led->led_cdev;
561			led->brightness_set_blocking = pca955x_led_set;
562			led->brightness_get = pca955x_led_get;
563
564			if (pdata->leds[i].default_state == LEDS_DEFSTATE_OFF) {
565				err = pca955x_led_set(led, LED_OFF);
566				if (err)
567					return err;
568			} else if (pdata->leds[i].default_state == LEDS_DEFSTATE_ON) {
569				err = pca955x_led_set(led, LED_FULL);
570				if (err)
571					return err;
572			}
573
574			init_data.fwnode = pdata->leds[i].fwnode;
575
576			if (is_of_node(init_data.fwnode)) {
577				if (to_of_node(init_data.fwnode)->name[0] ==
578				    '\0')
579					set_default_label = true;
580				else
581					set_default_label = false;
582			} else {
583				set_default_label = true;
584			}
585
586			if (set_default_label) {
587				snprintf(default_label, sizeof(default_label),
588					 "%d", i);
589				init_data.default_label = default_label;
590			} else {
591				init_data.default_label = NULL;
592			}
593
594			err = devm_led_classdev_register_ext(&client->dev, led,
595							     &init_data);
596			if (err)
597				return err;
598
599			set_bit(i, &pca955x->active_pins);
600
601			/*
602			 * For default-state == "keep", let the core update the
603			 * brightness from the hardware, then check the
604			 * brightness to see if it's using PWM1. If so, PWM1
605			 * should not be written below.
606			 */
607			if (pdata->leds[i].default_state == LEDS_DEFSTATE_KEEP) {
608				if (led->brightness != LED_FULL &&
609				    led->brightness != LED_OFF &&
610				    led->brightness != LED_HALF)
611					keep_pwm = true;
612			}
613		}
614	}
615
616	/* PWM0 is used for half brightness or 50% duty cycle */
617	err = pca955x_write_pwm(client, 0, 255 - LED_HALF);
618	if (err)
619		return err;
620
621	if (!keep_pwm) {
622		/* PWM1 is used for variable brightness, default to OFF */
623		err = pca955x_write_pwm(client, 1, 0);
624		if (err)
625			return err;
626	}
627
628	/* Set to fast frequency so we do not see flashing */
629	err = pca955x_write_psc(client, 0, 0);
630	if (err)
631		return err;
632	err = pca955x_write_psc(client, 1, 0);
633	if (err)
634		return err;
635
636#ifdef CONFIG_LEDS_PCA955X_GPIO
637	pca955x->gpio.label = "gpio-pca955x";
638	pca955x->gpio.direction_input = pca955x_gpio_direction_input;
639	pca955x->gpio.direction_output = pca955x_gpio_direction_output;
640	pca955x->gpio.set = pca955x_gpio_set_value;
641	pca955x->gpio.get = pca955x_gpio_get_value;
642	pca955x->gpio.request = pca955x_gpio_request_pin;
643	pca955x->gpio.free = pca955x_gpio_free_pin;
644	pca955x->gpio.can_sleep = 1;
645	pca955x->gpio.base = -1;
646	pca955x->gpio.ngpio = chip->bits;
647	pca955x->gpio.parent = &client->dev;
648	pca955x->gpio.owner = THIS_MODULE;
649
650	err = devm_gpiochip_add_data(&client->dev, &pca955x->gpio,
651				     pca955x);
652	if (err) {
653		/* Use data->gpio.dev as a flag for freeing gpiochip */
654		pca955x->gpio.parent = NULL;
655		dev_warn(&client->dev, "could not add gpiochip\n");
656		return err;
657	}
658	dev_info(&client->dev, "gpios %i...%i\n",
659		 pca955x->gpio.base, pca955x->gpio.base +
660		 pca955x->gpio.ngpio - 1);
661#endif
662
663	return 0;
664}
665
666static struct i2c_driver pca955x_driver = {
667	.driver = {
668		.name	= "leds-pca955x",
669		.of_match_table = of_pca955x_match,
670	},
671	.probe = pca955x_probe,
672	.id_table = pca955x_id,
673};
674
675module_i2c_driver(pca955x_driver);
676
677MODULE_AUTHOR("Nate Case <ncase@xes-inc.com>");
678MODULE_DESCRIPTION("PCA955x LED driver");
679MODULE_LICENSE("GPL v2");
680