Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright 2011 bct electronic GmbH
  4 * Copyright 2013 Qtechnology/AS
  5 *
  6 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  7 * Author: Ricardo Ribalda <ribalda@kernel.org>
  8 *
  9 * Based on leds-pca955x.c
 10 *
 11 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
 12 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
 13 *
 14 * Note that hardware blinking violates the leds infrastructure driver
 15 * interface since the hardware only supports blinking all LEDs with the
 16 * same delay_on/delay_off rates.  That is, only the LEDs that are set to
 17 * blink will actually blink but all LEDs that are set to blink will blink
 18 * in identical fashion.  The delay_on/delay_off values of the last LED
 19 * that is set to blink will be used for all of the blinking LEDs.
 20 * Hardware blinking is disabled by default but can be enabled by setting
 21 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
 22 * or by adding the 'nxp,hw-blink' property to the DTS.
 23 */
 24
 25#include <linux/module.h>
 26#include <linux/delay.h>
 27#include <linux/string.h>
 28#include <linux/ctype.h>
 29#include <linux/leds.h>
 30#include <linux/err.h>
 31#include <linux/i2c.h>
 32#include <linux/property.h>
 33#include <linux/slab.h>
 34#include <linux/of.h>
 35
 36/* LED select registers determine the source that drives LED outputs */
 37#define PCA963X_LED_OFF		0x0	/* LED driver off */
 38#define PCA963X_LED_ON		0x1	/* LED driver on */
 39#define PCA963X_LED_PWM		0x2	/* Controlled through PWM */
 40#define PCA963X_LED_GRP_PWM	0x3	/* Controlled through PWM/GRPPWM */
 41
 42#define PCA963X_MODE1_SLEEP	0x04    /* Normal mode or Low Power mode, oscillator off */
 43#define PCA963X_MODE2_OUTDRV	0x04	/* Open-drain or totem pole */
 44#define PCA963X_MODE2_INVRT	0x10	/* Normal or inverted direction */
 45#define PCA963X_MODE2_DMBLNK	0x20	/* Enable blinking */
 46
 47#define PCA963X_MODE1		0x00
 48#define PCA963X_MODE2		0x01
 49#define PCA963X_PWM_BASE	0x02
 50
 51enum pca963x_type {
 52	pca9633,
 53	pca9634,
 54	pca9635,
 55};
 56
 57struct pca963x_chipdef {
 58	u8			grppwm;
 59	u8			grpfreq;
 60	u8			ledout_base;
 61	int			n_leds;
 62	unsigned int		scaling;
 63};
 64
 65static struct pca963x_chipdef pca963x_chipdefs[] = {
 66	[pca9633] = {
 67		.grppwm		= 0x6,
 68		.grpfreq	= 0x7,
 69		.ledout_base	= 0x8,
 70		.n_leds		= 4,
 71	},
 72	[pca9634] = {
 73		.grppwm		= 0xa,
 74		.grpfreq	= 0xb,
 75		.ledout_base	= 0xc,
 76		.n_leds		= 8,
 77	},
 78	[pca9635] = {
 79		.grppwm		= 0x12,
 80		.grpfreq	= 0x13,
 81		.ledout_base	= 0x14,
 82		.n_leds		= 16,
 83	},
 84};
 85
 86/* Total blink period in milliseconds */
 87#define PCA963X_BLINK_PERIOD_MIN	42
 88#define PCA963X_BLINK_PERIOD_MAX	10667
 89
 90static const struct i2c_device_id pca963x_id[] = {
 91	{ "pca9632", pca9633 },
 92	{ "pca9633", pca9633 },
 93	{ "pca9634", pca9634 },
 94	{ "pca9635", pca9635 },
 95	{ }
 96};
 97MODULE_DEVICE_TABLE(i2c, pca963x_id);
 98
 99struct pca963x;
100
101struct pca963x_led {
102	struct pca963x *chip;
103	struct led_classdev led_cdev;
104	int led_num; /* 0 .. 15 potentially */
105	bool blinking;
106	u8 gdc;
107	u8 gfrq;
108};
109
110struct pca963x {
111	struct pca963x_chipdef *chipdef;
112	struct mutex mutex;
113	struct i2c_client *client;
114	unsigned long leds_on;
115	struct pca963x_led leds[];
116};
117
118static int pca963x_brightness(struct pca963x_led *led,
119			      enum led_brightness brightness)
120{
121	struct i2c_client *client = led->chip->client;
122	struct pca963x_chipdef *chipdef = led->chip->chipdef;
123	u8 ledout_addr, ledout, mask, val;
124	int shift;
125	int ret;
126
127	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
128	shift = 2 * (led->led_num % 4);
129	mask = 0x3 << shift;
130	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
131
132	switch (brightness) {
133	case LED_FULL:
134		if (led->blinking) {
135			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
136			ret = i2c_smbus_write_byte_data(client,
137						PCA963X_PWM_BASE +
138						led->led_num,
139						LED_FULL);
140		} else {
141			val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
142		}
143		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
144		break;
145	case LED_OFF:
146		val = ledout & ~mask;
147		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
148		led->blinking = false;
149		break;
150	default:
151		ret = i2c_smbus_write_byte_data(client,
152						PCA963X_PWM_BASE +
153						led->led_num,
154						brightness);
155		if (ret < 0)
156			return ret;
157
158		if (led->blinking)
159			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
160		else
161			val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
162
163		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
164		break;
165	}
166
167	return ret;
168}
169
170static void pca963x_blink(struct pca963x_led *led)
171{
172	struct i2c_client *client = led->chip->client;
173	struct pca963x_chipdef *chipdef = led->chip->chipdef;
174	u8 ledout_addr, ledout, mask, val, mode2;
175	int shift;
176
177	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
178	shift = 2 * (led->led_num % 4);
179	mask = 0x3 << shift;
180	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
181
182	i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
183
184	i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
185
186	if (!(mode2 & PCA963X_MODE2_DMBLNK))
187		i2c_smbus_write_byte_data(client, PCA963X_MODE2,
188					  mode2 | PCA963X_MODE2_DMBLNK);
189
190	mutex_lock(&led->chip->mutex);
191
192	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
193	if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
194		val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
195		i2c_smbus_write_byte_data(client, ledout_addr, val);
196	}
197
198	mutex_unlock(&led->chip->mutex);
199	led->blinking = true;
200}
201
202static int pca963x_power_state(struct pca963x_led *led)
203{
204	struct i2c_client *client = led->chip->client;
205	unsigned long *leds_on = &led->chip->leds_on;
206	unsigned long cached_leds = *leds_on;
207
208	if (led->led_cdev.brightness)
209		set_bit(led->led_num, leds_on);
210	else
211		clear_bit(led->led_num, leds_on);
212
213	if (!(*leds_on) != !cached_leds)
214		return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
215						 *leds_on ? 0 : BIT(4));
216
217	return 0;
218}
219
220static int pca963x_led_set(struct led_classdev *led_cdev,
221			   enum led_brightness value)
222{
223	struct pca963x_led *led;
224	int ret;
225
226	led = container_of(led_cdev, struct pca963x_led, led_cdev);
227
228	mutex_lock(&led->chip->mutex);
229
230	ret = pca963x_brightness(led, value);
231	if (ret < 0)
232		goto unlock;
233	ret = pca963x_power_state(led);
234
235unlock:
236	mutex_unlock(&led->chip->mutex);
237	return ret;
238}
239
240static unsigned int pca963x_period_scale(struct pca963x_led *led,
241					 unsigned int val)
242{
243	unsigned int scaling = led->chip->chipdef->scaling;
244
245	return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
246}
247
248static int pca963x_blink_set(struct led_classdev *led_cdev,
249			     unsigned long *delay_on, unsigned long *delay_off)
250{
251	unsigned long time_on, time_off, period;
252	struct pca963x_led *led;
253	u8 gdc, gfrq;
254
255	led = container_of(led_cdev, struct pca963x_led, led_cdev);
256
257	time_on = *delay_on;
258	time_off = *delay_off;
259
260	/* If both zero, pick reasonable defaults of 500ms each */
261	if (!time_on && !time_off) {
262		time_on = 500;
263		time_off = 500;
264	}
265
266	period = pca963x_period_scale(led, time_on + time_off);
267
268	/* If period not supported by hardware, default to someting sane. */
269	if ((period < PCA963X_BLINK_PERIOD_MIN) ||
270	    (period > PCA963X_BLINK_PERIOD_MAX)) {
271		time_on = 500;
272		time_off = 500;
273		period = pca963x_period_scale(led, 1000);
274	}
275
276	/*
277	 * From manual: duty cycle = (GDC / 256) ->
278	 *	(time_on / period) = (GDC / 256) ->
279	 *		GDC = ((time_on * 256) / period)
280	 */
281	gdc = (pca963x_period_scale(led, time_on) * 256) / period;
282
283	/*
284	 * From manual: period = ((GFRQ + 1) / 24) in seconds.
285	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
286	 *		GFRQ = ((period * 24 / 1000) - 1)
287	 */
288	gfrq = (period * 24 / 1000) - 1;
289
290	led->gdc = gdc;
291	led->gfrq = gfrq;
292
293	pca963x_blink(led);
294	led->led_cdev.brightness = LED_FULL;
295	pca963x_led_set(led_cdev, LED_FULL);
296
297	*delay_on = time_on;
298	*delay_off = time_off;
299
300	return 0;
301}
302
303static int pca963x_register_leds(struct i2c_client *client,
304				 struct pca963x *chip)
305{
306	struct pca963x_chipdef *chipdef = chip->chipdef;
307	struct pca963x_led *led = chip->leds;
308	struct device *dev = &client->dev;
 
309	bool hw_blink;
310	s32 mode2;
311	u32 reg;
312	int ret;
313
314	if (device_property_read_u32(dev, "nxp,period-scale",
315				     &chipdef->scaling))
316		chipdef->scaling = 1000;
317
318	hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
319
320	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
321	if (mode2 < 0)
322		return mode2;
323
324	/* default to open-drain unless totem pole (push-pull) is specified */
325	if (device_property_read_bool(dev, "nxp,totem-pole"))
326		mode2 |= PCA963X_MODE2_OUTDRV;
327	else
328		mode2 &= ~PCA963X_MODE2_OUTDRV;
329
330	/* default to non-inverted output, unless inverted is specified */
331	if (device_property_read_bool(dev, "nxp,inverted-out"))
332		mode2 |= PCA963X_MODE2_INVRT;
333	else
334		mode2 &= ~PCA963X_MODE2_INVRT;
335
336	ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
337	if (ret < 0)
338		return ret;
339
340	device_for_each_child_node_scoped(dev, child) {
341		struct led_init_data init_data = {};
342		char default_label[32];
343
344		ret = fwnode_property_read_u32(child, "reg", &reg);
345		if (ret || reg >= chipdef->n_leds) {
346			dev_err(dev, "Invalid 'reg' property for node %pfw\n",
347				child);
348			return -EINVAL;
 
349		}
350
351		led->led_num = reg;
352		led->chip = chip;
353		led->led_cdev.brightness_set_blocking = pca963x_led_set;
354		if (hw_blink)
355			led->led_cdev.blink_set = pca963x_blink_set;
356		led->blinking = false;
357
358		init_data.fwnode = child;
359		/* for backwards compatibility */
360		init_data.devicename = "pca963x";
361		snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
362			 client->adapter->nr, client->addr, reg);
363		init_data.default_label = default_label;
364
365		ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
366						     &init_data);
367		if (ret) {
368			dev_err(dev, "Failed to register LED for node %pfw\n",
369				child);
370			return ret;
371		}
372
373		++led;
374	}
375
376	return 0;
 
 
 
377}
378
379static int pca963x_suspend(struct device *dev)
380{
381	struct pca963x *chip = dev_get_drvdata(dev);
382	u8 reg;
383
384	reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
385	reg = reg | BIT(PCA963X_MODE1_SLEEP);
386	i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
387
388	return 0;
389}
390
391static int pca963x_resume(struct device *dev)
392{
393	struct pca963x *chip = dev_get_drvdata(dev);
394	u8 reg;
395
396	reg = i2c_smbus_read_byte_data(chip->client, PCA963X_MODE1);
397	reg = reg & ~BIT(PCA963X_MODE1_SLEEP);
398	i2c_smbus_write_byte_data(chip->client, PCA963X_MODE1, reg);
399
400	return 0;
401}
402
403static DEFINE_SIMPLE_DEV_PM_OPS(pca963x_pm, pca963x_suspend, pca963x_resume);
404
405static const struct of_device_id of_pca963x_match[] = {
406	{ .compatible = "nxp,pca9632", },
407	{ .compatible = "nxp,pca9633", },
408	{ .compatible = "nxp,pca9634", },
409	{ .compatible = "nxp,pca9635", },
410	{},
411};
412MODULE_DEVICE_TABLE(of, of_pca963x_match);
413
414static int pca963x_probe(struct i2c_client *client)
 
415{
416	const struct i2c_device_id *id = i2c_client_get_device_id(client);
417	struct device *dev = &client->dev;
418	struct pca963x_chipdef *chipdef;
419	struct pca963x *chip;
420	int i, count;
421
422	chipdef = &pca963x_chipdefs[id->driver_data];
423
424	count = device_get_child_node_count(dev);
425	if (!count || count > chipdef->n_leds) {
426		dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
427			dev_fwnode(dev), chipdef->n_leds);
428		return -EINVAL;
429	}
430
431	chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
432	if (!chip)
433		return -ENOMEM;
434
435	i2c_set_clientdata(client, chip);
436
437	mutex_init(&chip->mutex);
438	chip->chipdef = chipdef;
439	chip->client = client;
440
441	/* Turn off LEDs by default*/
442	for (i = 0; i < chipdef->n_leds / 4; i++)
443		i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
444
445	/* Disable LED all-call address, and power down initially */
446	i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
447
448	return pca963x_register_leds(client, chip);
449}
450
451static struct i2c_driver pca963x_driver = {
452	.driver = {
453		.name	= "leds-pca963x",
454		.of_match_table = of_pca963x_match,
455		.pm = pm_sleep_ptr(&pca963x_pm)
456	},
457	.probe = pca963x_probe,
458	.id_table = pca963x_id,
459};
460
461module_i2c_driver(pca963x_driver);
462
463MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
464MODULE_DESCRIPTION("PCA963X LED driver");
465MODULE_LICENSE("GPL v2");
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright 2011 bct electronic GmbH
  4 * Copyright 2013 Qtechnology/AS
  5 *
  6 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
  7 * Author: Ricardo Ribalda <ribalda@kernel.org>
  8 *
  9 * Based on leds-pca955x.c
 10 *
 11 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
 12 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
 13 *
 14 * Note that hardware blinking violates the leds infrastructure driver
 15 * interface since the hardware only supports blinking all LEDs with the
 16 * same delay_on/delay_off rates.  That is, only the LEDs that are set to
 17 * blink will actually blink but all LEDs that are set to blink will blink
 18 * in identical fashion.  The delay_on/delay_off values of the last LED
 19 * that is set to blink will be used for all of the blinking LEDs.
 20 * Hardware blinking is disabled by default but can be enabled by setting
 21 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
 22 * or by adding the 'nxp,hw-blink' property to the DTS.
 23 */
 24
 25#include <linux/module.h>
 26#include <linux/delay.h>
 27#include <linux/string.h>
 28#include <linux/ctype.h>
 29#include <linux/leds.h>
 30#include <linux/err.h>
 31#include <linux/i2c.h>
 32#include <linux/property.h>
 33#include <linux/slab.h>
 34#include <linux/of.h>
 35
 36/* LED select registers determine the source that drives LED outputs */
 37#define PCA963X_LED_OFF		0x0	/* LED driver off */
 38#define PCA963X_LED_ON		0x1	/* LED driver on */
 39#define PCA963X_LED_PWM		0x2	/* Controlled through PWM */
 40#define PCA963X_LED_GRP_PWM	0x3	/* Controlled through PWM/GRPPWM */
 41
 
 42#define PCA963X_MODE2_OUTDRV	0x04	/* Open-drain or totem pole */
 43#define PCA963X_MODE2_INVRT	0x10	/* Normal or inverted direction */
 44#define PCA963X_MODE2_DMBLNK	0x20	/* Enable blinking */
 45
 46#define PCA963X_MODE1		0x00
 47#define PCA963X_MODE2		0x01
 48#define PCA963X_PWM_BASE	0x02
 49
 50enum pca963x_type {
 51	pca9633,
 52	pca9634,
 53	pca9635,
 54};
 55
 56struct pca963x_chipdef {
 57	u8			grppwm;
 58	u8			grpfreq;
 59	u8			ledout_base;
 60	int			n_leds;
 61	unsigned int		scaling;
 62};
 63
 64static struct pca963x_chipdef pca963x_chipdefs[] = {
 65	[pca9633] = {
 66		.grppwm		= 0x6,
 67		.grpfreq	= 0x7,
 68		.ledout_base	= 0x8,
 69		.n_leds		= 4,
 70	},
 71	[pca9634] = {
 72		.grppwm		= 0xa,
 73		.grpfreq	= 0xb,
 74		.ledout_base	= 0xc,
 75		.n_leds		= 8,
 76	},
 77	[pca9635] = {
 78		.grppwm		= 0x12,
 79		.grpfreq	= 0x13,
 80		.ledout_base	= 0x14,
 81		.n_leds		= 16,
 82	},
 83};
 84
 85/* Total blink period in milliseconds */
 86#define PCA963X_BLINK_PERIOD_MIN	42
 87#define PCA963X_BLINK_PERIOD_MAX	10667
 88
 89static const struct i2c_device_id pca963x_id[] = {
 90	{ "pca9632", pca9633 },
 91	{ "pca9633", pca9633 },
 92	{ "pca9634", pca9634 },
 93	{ "pca9635", pca9635 },
 94	{ }
 95};
 96MODULE_DEVICE_TABLE(i2c, pca963x_id);
 97
 98struct pca963x;
 99
100struct pca963x_led {
101	struct pca963x *chip;
102	struct led_classdev led_cdev;
103	int led_num; /* 0 .. 15 potentially */
104	bool blinking;
105	u8 gdc;
106	u8 gfrq;
107};
108
109struct pca963x {
110	struct pca963x_chipdef *chipdef;
111	struct mutex mutex;
112	struct i2c_client *client;
113	unsigned long leds_on;
114	struct pca963x_led leds[];
115};
116
117static int pca963x_brightness(struct pca963x_led *led,
118			      enum led_brightness brightness)
119{
120	struct i2c_client *client = led->chip->client;
121	struct pca963x_chipdef *chipdef = led->chip->chipdef;
122	u8 ledout_addr, ledout, mask, val;
123	int shift;
124	int ret;
125
126	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
127	shift = 2 * (led->led_num % 4);
128	mask = 0x3 << shift;
129	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
130
131	switch (brightness) {
132	case LED_FULL:
133		if (led->blinking) {
134			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
135			ret = i2c_smbus_write_byte_data(client,
136						PCA963X_PWM_BASE +
137						led->led_num,
138						LED_FULL);
139		} else {
140			val = (ledout & ~mask) | (PCA963X_LED_ON << shift);
141		}
142		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
143		break;
144	case LED_OFF:
145		val = ledout & ~mask;
146		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
147		led->blinking = false;
148		break;
149	default:
150		ret = i2c_smbus_write_byte_data(client,
151						PCA963X_PWM_BASE +
152						led->led_num,
153						brightness);
154		if (ret < 0)
155			return ret;
156
157		if (led->blinking)
158			val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
159		else
160			val = (ledout & ~mask) | (PCA963X_LED_PWM << shift);
161
162		ret = i2c_smbus_write_byte_data(client, ledout_addr, val);
163		break;
164	}
165
166	return ret;
167}
168
169static void pca963x_blink(struct pca963x_led *led)
170{
171	struct i2c_client *client = led->chip->client;
172	struct pca963x_chipdef *chipdef = led->chip->chipdef;
173	u8 ledout_addr, ledout, mask, val, mode2;
174	int shift;
175
176	ledout_addr = chipdef->ledout_base + (led->led_num / 4);
177	shift = 2 * (led->led_num % 4);
178	mask = 0x3 << shift;
179	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
180
181	i2c_smbus_write_byte_data(client, chipdef->grppwm, led->gdc);
182
183	i2c_smbus_write_byte_data(client, chipdef->grpfreq, led->gfrq);
184
185	if (!(mode2 & PCA963X_MODE2_DMBLNK))
186		i2c_smbus_write_byte_data(client, PCA963X_MODE2,
187					  mode2 | PCA963X_MODE2_DMBLNK);
188
189	mutex_lock(&led->chip->mutex);
190
191	ledout = i2c_smbus_read_byte_data(client, ledout_addr);
192	if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift)) {
193		val = (ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift);
194		i2c_smbus_write_byte_data(client, ledout_addr, val);
195	}
196
197	mutex_unlock(&led->chip->mutex);
198	led->blinking = true;
199}
200
201static int pca963x_power_state(struct pca963x_led *led)
202{
203	struct i2c_client *client = led->chip->client;
204	unsigned long *leds_on = &led->chip->leds_on;
205	unsigned long cached_leds = *leds_on;
206
207	if (led->led_cdev.brightness)
208		set_bit(led->led_num, leds_on);
209	else
210		clear_bit(led->led_num, leds_on);
211
212	if (!(*leds_on) != !cached_leds)
213		return i2c_smbus_write_byte_data(client, PCA963X_MODE1,
214						 *leds_on ? 0 : BIT(4));
215
216	return 0;
217}
218
219static int pca963x_led_set(struct led_classdev *led_cdev,
220			   enum led_brightness value)
221{
222	struct pca963x_led *led;
223	int ret;
224
225	led = container_of(led_cdev, struct pca963x_led, led_cdev);
226
227	mutex_lock(&led->chip->mutex);
228
229	ret = pca963x_brightness(led, value);
230	if (ret < 0)
231		goto unlock;
232	ret = pca963x_power_state(led);
233
234unlock:
235	mutex_unlock(&led->chip->mutex);
236	return ret;
237}
238
239static unsigned int pca963x_period_scale(struct pca963x_led *led,
240					 unsigned int val)
241{
242	unsigned int scaling = led->chip->chipdef->scaling;
243
244	return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
245}
246
247static int pca963x_blink_set(struct led_classdev *led_cdev,
248			     unsigned long *delay_on, unsigned long *delay_off)
249{
250	unsigned long time_on, time_off, period;
251	struct pca963x_led *led;
252	u8 gdc, gfrq;
253
254	led = container_of(led_cdev, struct pca963x_led, led_cdev);
255
256	time_on = *delay_on;
257	time_off = *delay_off;
258
259	/* If both zero, pick reasonable defaults of 500ms each */
260	if (!time_on && !time_off) {
261		time_on = 500;
262		time_off = 500;
263	}
264
265	period = pca963x_period_scale(led, time_on + time_off);
266
267	/* If period not supported by hardware, default to someting sane. */
268	if ((period < PCA963X_BLINK_PERIOD_MIN) ||
269	    (period > PCA963X_BLINK_PERIOD_MAX)) {
270		time_on = 500;
271		time_off = 500;
272		period = pca963x_period_scale(led, 1000);
273	}
274
275	/*
276	 * From manual: duty cycle = (GDC / 256) ->
277	 *	(time_on / period) = (GDC / 256) ->
278	 *		GDC = ((time_on * 256) / period)
279	 */
280	gdc = (pca963x_period_scale(led, time_on) * 256) / period;
281
282	/*
283	 * From manual: period = ((GFRQ + 1) / 24) in seconds.
284	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
285	 *		GFRQ = ((period * 24 / 1000) - 1)
286	 */
287	gfrq = (period * 24 / 1000) - 1;
288
289	led->gdc = gdc;
290	led->gfrq = gfrq;
291
292	pca963x_blink(led);
293	led->led_cdev.brightness = LED_FULL;
294	pca963x_led_set(led_cdev, LED_FULL);
295
296	*delay_on = time_on;
297	*delay_off = time_off;
298
299	return 0;
300}
301
302static int pca963x_register_leds(struct i2c_client *client,
303				 struct pca963x *chip)
304{
305	struct pca963x_chipdef *chipdef = chip->chipdef;
306	struct pca963x_led *led = chip->leds;
307	struct device *dev = &client->dev;
308	struct fwnode_handle *child;
309	bool hw_blink;
310	s32 mode2;
311	u32 reg;
312	int ret;
313
314	if (device_property_read_u32(dev, "nxp,period-scale",
315				     &chipdef->scaling))
316		chipdef->scaling = 1000;
317
318	hw_blink = device_property_read_bool(dev, "nxp,hw-blink");
319
320	mode2 = i2c_smbus_read_byte_data(client, PCA963X_MODE2);
321	if (mode2 < 0)
322		return mode2;
323
324	/* default to open-drain unless totem pole (push-pull) is specified */
325	if (device_property_read_bool(dev, "nxp,totem-pole"))
326		mode2 |= PCA963X_MODE2_OUTDRV;
327	else
328		mode2 &= ~PCA963X_MODE2_OUTDRV;
329
330	/* default to non-inverted output, unless inverted is specified */
331	if (device_property_read_bool(dev, "nxp,inverted-out"))
332		mode2 |= PCA963X_MODE2_INVRT;
333	else
334		mode2 &= ~PCA963X_MODE2_INVRT;
335
336	ret = i2c_smbus_write_byte_data(client, PCA963X_MODE2, mode2);
337	if (ret < 0)
338		return ret;
339
340	device_for_each_child_node(dev, child) {
341		struct led_init_data init_data = {};
342		char default_label[32];
343
344		ret = fwnode_property_read_u32(child, "reg", &reg);
345		if (ret || reg >= chipdef->n_leds) {
346			dev_err(dev, "Invalid 'reg' property for node %pfw\n",
347				child);
348			ret = -EINVAL;
349			goto err;
350		}
351
352		led->led_num = reg;
353		led->chip = chip;
354		led->led_cdev.brightness_set_blocking = pca963x_led_set;
355		if (hw_blink)
356			led->led_cdev.blink_set = pca963x_blink_set;
357		led->blinking = false;
358
359		init_data.fwnode = child;
360		/* for backwards compatibility */
361		init_data.devicename = "pca963x";
362		snprintf(default_label, sizeof(default_label), "%d:%.2x:%u",
363			 client->adapter->nr, client->addr, reg);
364		init_data.default_label = default_label;
365
366		ret = devm_led_classdev_register_ext(dev, &led->led_cdev,
367						     &init_data);
368		if (ret) {
369			dev_err(dev, "Failed to register LED for node %pfw\n",
370				child);
371			goto err;
372		}
373
374		++led;
375	}
376
377	return 0;
378err:
379	fwnode_handle_put(child);
380	return ret;
381}
382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383static const struct of_device_id of_pca963x_match[] = {
384	{ .compatible = "nxp,pca9632", },
385	{ .compatible = "nxp,pca9633", },
386	{ .compatible = "nxp,pca9634", },
387	{ .compatible = "nxp,pca9635", },
388	{},
389};
390MODULE_DEVICE_TABLE(of, of_pca963x_match);
391
392static int pca963x_probe(struct i2c_client *client,
393			 const struct i2c_device_id *id)
394{
 
395	struct device *dev = &client->dev;
396	struct pca963x_chipdef *chipdef;
397	struct pca963x *chip;
398	int i, count;
399
400	chipdef = &pca963x_chipdefs[id->driver_data];
401
402	count = device_get_child_node_count(dev);
403	if (!count || count > chipdef->n_leds) {
404		dev_err(dev, "Node %pfw must define between 1 and %d LEDs\n",
405			dev_fwnode(dev), chipdef->n_leds);
406		return -EINVAL;
407	}
408
409	chip = devm_kzalloc(dev, struct_size(chip, leds, count), GFP_KERNEL);
410	if (!chip)
411		return -ENOMEM;
412
413	i2c_set_clientdata(client, chip);
414
415	mutex_init(&chip->mutex);
416	chip->chipdef = chipdef;
417	chip->client = client;
418
419	/* Turn off LEDs by default*/
420	for (i = 0; i < chipdef->n_leds / 4; i++)
421		i2c_smbus_write_byte_data(client, chipdef->ledout_base + i, 0x00);
422
423	/* Disable LED all-call address, and power down initially */
424	i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
425
426	return pca963x_register_leds(client, chip);
427}
428
429static struct i2c_driver pca963x_driver = {
430	.driver = {
431		.name	= "leds-pca963x",
432		.of_match_table = of_pca963x_match,
 
433	},
434	.probe	= pca963x_probe,
435	.id_table = pca963x_id,
436};
437
438module_i2c_driver(pca963x_driver);
439
440MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
441MODULE_DESCRIPTION("PCA963X LED driver");
442MODULE_LICENSE("GPL v2");