leds-pca963x.c 13.2 KB
Newer Older
1 2
/*
 * Copyright 2011 bct electronic GmbH
3
 * Copyright 2013 Qtechnology/AS
4 5
 *
 * Author: Peter Meerwald <p.meerwald@bct-electronic.com>
6
 * Author: Ricardo Ribalda <ricardo.ribalda@gmail.com>
7 8 9 10 11 12 13 14
 *
 * Based on leds-pca955x.c
 *
 * This file is subject to the terms and conditions of version 2 of
 * the GNU General Public License.  See the file COPYING in the main
 * directory of this archive for more details.
 *
 * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62)
15
 * LED driver for the PCA9634/5 I2C LED driver (7-bit slave address set by hw.)
16
 *
17 18 19 20 21 22 23
 * Note that hardware blinking violates the leds infrastructure driver
 * interface since the hardware only supports blinking all LEDs with the
 * same delay_on/delay_off rates.  That is, only the LEDs that are set to
 * blink will actually blink but all LEDs that are set to blink will blink
 * in identical fashion.  The delay_on/delay_off values of the last LED
 * that is set to blink will be used for all of the blinking LEDs.
 * Hardware blinking is disabled by default but can be enabled by setting
24
 * the 'blink_type' member in the platform_data struct to 'PCA963X_HW_BLINK'
25
 * or by adding the 'nxp,hw-blink' property to the DTS.
26 27
 */

28
#include <linux/acpi.h>
29 30 31 32 33 34 35 36
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/slab.h>
37
#include <linux/of.h>
38
#include <linux/platform_data/leds-pca963x.h>
39 40

/* LED select registers determine the source that drives LED outputs */
41 42 43 44
#define PCA963X_LED_OFF		0x0	/* LED driver off */
#define PCA963X_LED_ON		0x1	/* LED driver on */
#define PCA963X_LED_PWM		0x2	/* Controlled through PWM */
#define PCA963X_LED_GRP_PWM	0x3	/* Controlled through PWM/GRPPWM */
45

46
#define PCA963X_MODE2_DMBLNK	0x20	/* Enable blinking */
47

48 49 50
#define PCA963X_MODE1		0x00
#define PCA963X_MODE2		0x01
#define PCA963X_PWM_BASE	0x02
51

52
enum pca963x_type {
53 54
	pca9633,
	pca9634,
55
	pca9635,
56 57
};

58
struct pca963x_chipdef {
59 60 61 62
	u8			grppwm;
	u8			grpfreq;
	u8			ledout_base;
	int			n_leds;
63
	unsigned int		scaling;
64 65
};

66
static struct pca963x_chipdef pca963x_chipdefs[] = {
67 68 69 70 71 72 73 74 75 76 77 78
	[pca9633] = {
		.grppwm		= 0x6,
		.grpfreq	= 0x7,
		.ledout_base	= 0x8,
		.n_leds		= 4,
	},
	[pca9634] = {
		.grppwm		= 0xa,
		.grpfreq	= 0xb,
		.ledout_base	= 0xc,
		.n_leds		= 8,
	},
79 80 81 82 83 84
	[pca9635] = {
		.grppwm		= 0x12,
		.grpfreq	= 0x13,
		.ledout_base	= 0x14,
		.n_leds		= 16,
	},
85
};
86

87
/* Total blink period in milliseconds */
88 89
#define PCA963X_BLINK_PERIOD_MIN	42
#define PCA963X_BLINK_PERIOD_MAX	10667
90

91
static const struct i2c_device_id pca963x_id[] = {
92 93 94
	{ "pca9632", pca9633 },
	{ "pca9633", pca9633 },
	{ "pca9634", pca9634 },
95
	{ "pca9635", pca9635 },
96 97
	{ }
};
98
MODULE_DEVICE_TABLE(i2c, pca963x_id);
99

100 101 102 103 104 105 106 107 108
static const struct acpi_device_id pca963x_acpi_ids[] = {
	{ "PCA9632", pca9633 },
	{ "PCA9633", pca9633 },
	{ "PCA9634", pca9634 },
	{ "PCA9635", pca9635 },
	{ }
};
MODULE_DEVICE_TABLE(acpi, pca963x_acpi_ids);

109
struct pca963x_led;
110

111 112
struct pca963x {
	struct pca963x_chipdef *chipdef;
113 114
	struct mutex mutex;
	struct i2c_client *client;
115
	struct pca963x_led *leds;
116
	unsigned long leds_on;
117 118
};

119 120
struct pca963x_led {
	struct pca963x *chip;
121
	struct led_classdev led_cdev;
122
	int led_num; /* 0 .. 15 potentially */
123
	char name[32];
124 125
	u8 gdc;
	u8 gfrq;
126 127
};

128 129
static int pca963x_brightness(struct pca963x_led *pca963x,
			       enum led_brightness brightness)
130
{
131 132
	u8 ledout_addr = pca963x->chip->chipdef->ledout_base
		+ (pca963x->led_num / 4);
133
	u8 ledout;
134
	int shift = 2 * (pca963x->led_num % 4);
135
	u8 mask = 0x3 << shift;
136
	int ret;
137

138
	ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
139
	switch (brightness) {
140
	case LED_FULL:
141 142
		ret = i2c_smbus_write_byte_data(pca963x->chip->client,
			ledout_addr,
143
			(ledout & ~mask) | (PCA963X_LED_ON << shift));
144 145
		break;
	case LED_OFF:
146 147
		ret = i2c_smbus_write_byte_data(pca963x->chip->client,
			ledout_addr, ledout & ~mask);
148 149
		break;
	default:
150
		ret = i2c_smbus_write_byte_data(pca963x->chip->client,
151
			PCA963X_PWM_BASE + pca963x->led_num,
152 153
			brightness);
		if (ret < 0)
154
			return ret;
155 156
		ret = i2c_smbus_write_byte_data(pca963x->chip->client,
			ledout_addr,
157
			(ledout & ~mask) | (PCA963X_LED_PWM << shift));
158 159
		break;
	}
160

161
	return ret;
162 163
}

164
static void pca963x_blink(struct pca963x_led *pca963x)
165
{
166 167
	u8 ledout_addr = pca963x->chip->chipdef->ledout_base +
		(pca963x->led_num / 4);
168
	u8 ledout;
169 170 171
	u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
							PCA963X_MODE2);
	int shift = 2 * (pca963x->led_num % 4);
172 173
	u8 mask = 0x3 << shift;

174 175
	i2c_smbus_write_byte_data(pca963x->chip->client,
			pca963x->chip->chipdef->grppwm,	pca963x->gdc);
176

177 178
	i2c_smbus_write_byte_data(pca963x->chip->client,
			pca963x->chip->chipdef->grpfreq, pca963x->gfrq);
179

180 181 182
	if (!(mode2 & PCA963X_MODE2_DMBLNK))
		i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
			mode2 | PCA963X_MODE2_DMBLNK);
183

184 185 186 187 188 189
	mutex_lock(&pca963x->chip->mutex);
	ledout = i2c_smbus_read_byte_data(pca963x->chip->client, ledout_addr);
	if ((ledout & mask) != (PCA963X_LED_GRP_PWM << shift))
		i2c_smbus_write_byte_data(pca963x->chip->client, ledout_addr,
			(ledout & ~mask) | (PCA963X_LED_GRP_PWM << shift));
	mutex_unlock(&pca963x->chip->mutex);
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
static int pca963x_power_state(struct pca963x_led *pca963x)
{
	unsigned long *leds_on = &pca963x->chip->leds_on;
	unsigned long cached_leds = pca963x->chip->leds_on;

	if (pca963x->led_cdev.brightness)
		set_bit(pca963x->led_num, leds_on);
	else
		clear_bit(pca963x->led_num, leds_on);

	if (!(*leds_on) != !cached_leds)
		return i2c_smbus_write_byte_data(pca963x->chip->client,
			PCA963X_MODE1, *leds_on ? 0 : BIT(4));

	return 0;
}

209
static int pca963x_led_set(struct led_classdev *led_cdev,
210 211
	enum led_brightness value)
{
212
	struct pca963x_led *pca963x;
213
	int ret;
214

215
	pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
216

217 218 219 220 221 222 223 224 225 226
	mutex_lock(&pca963x->chip->mutex);

	ret = pca963x_brightness(pca963x, value);
	if (ret < 0)
		goto unlock;
	ret = pca963x_power_state(pca963x);

unlock:
	mutex_unlock(&pca963x->chip->mutex);
	return ret;
227 228
}

229 230 231 232 233 234 235 236
static unsigned int pca963x_period_scale(struct pca963x_led *pca963x,
	unsigned int val)
{
	unsigned int scaling = pca963x->chip->chipdef->scaling;

	return scaling ? DIV_ROUND_CLOSEST(val * scaling, 1000) : val;
}

237
static int pca963x_blink_set(struct led_classdev *led_cdev,
238 239
		unsigned long *delay_on, unsigned long *delay_off)
{
240
	struct pca963x_led *pca963x;
241 242 243
	unsigned long time_on, time_off, period;
	u8 gdc, gfrq;

244
	pca963x = container_of(led_cdev, struct pca963x_led, led_cdev);
245 246 247 248 249 250 251 252 253 254

	time_on = *delay_on;
	time_off = *delay_off;

	/* If both zero, pick reasonable defaults of 500ms each */
	if (!time_on && !time_off) {
		time_on = 500;
		time_off = 500;
	}

255
	period = pca963x_period_scale(pca963x, time_on + time_off);
256 257

	/* If period not supported by hardware, default to someting sane. */
258 259
	if ((period < PCA963X_BLINK_PERIOD_MIN) ||
	    (period > PCA963X_BLINK_PERIOD_MAX)) {
260 261
		time_on = 500;
		time_off = 500;
262
		period = pca963x_period_scale(pca963x, 1000);
263 264 265 266 267 268 269
	}

	/*
	 * From manual: duty cycle = (GDC / 256) ->
	 *	(time_on / period) = (GDC / 256) ->
	 *		GDC = ((time_on * 256) / period)
	 */
270
	gdc = (pca963x_period_scale(pca963x, time_on) * 256) / period;
271 272 273 274 275 276 277 278

	/*
	 * From manual: period = ((GFRQ + 1) / 24) in seconds.
	 * So, period (in ms) = (((GFRQ + 1) / 24) * 1000) ->
	 *		GFRQ = ((period * 24 / 1000) - 1)
	 */
	gfrq = (period * 24 / 1000) - 1;

279 280
	pca963x->gdc = gdc;
	pca963x->gfrq = gfrq;
281

282
	pca963x_blink(pca963x);
283 284 285 286 287 288 289

	*delay_on = time_on;
	*delay_off = time_off;

	return 0;
}

290
#if IS_ENABLED(CONFIG_OF)
291 292
static struct pca963x_platform_data *
pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
293 294
{
	struct device_node *np = client->dev.of_node, *child;
295 296
	struct pca963x_platform_data *pdata;
	struct led_info *pca963x_leds;
297 298 299
	int count;

	count = of_get_child_count(np);
300
	if (!count || count > chip->n_leds)
301 302
		return ERR_PTR(-ENODEV);

303 304
	pca963x_leds = devm_kcalloc(&client->dev,
			chip->n_leds, sizeof(struct led_info), GFP_KERNEL);
305
	if (!pca963x_leds)
306 307 308
		return ERR_PTR(-ENOMEM);

	for_each_child_of_node(np, child) {
309
		struct led_info led = {};
310 311 312
		u32 reg;
		int res;

313 314 315
		res = of_property_read_u32(child, "reg", &reg);
		if ((res != 0) || (reg >= chip->n_leds))
			continue;
316 317 318 319
		led.name =
			of_get_property(child, "label", NULL) ? : child->name;
		led.default_trigger =
			of_get_property(child, "linux,default-trigger", NULL);
320
		pca963x_leds[reg] = led;
321 322
	}
	pdata = devm_kzalloc(&client->dev,
323
			     sizeof(struct pca963x_platform_data), GFP_KERNEL);
324 325 326
	if (!pdata)
		return ERR_PTR(-ENOMEM);

327
	pdata->leds.leds = pca963x_leds;
328
	pdata->leds.num_leds = chip->n_leds;
329 330 331

	/* default to open-drain unless totem pole (push-pull) is specified */
	if (of_property_read_bool(np, "nxp,totem-pole"))
332
		pdata->outdrv = PCA963X_TOTEM_POLE;
333
	else
334
		pdata->outdrv = PCA963X_OPEN_DRAIN;
335

336 337
	/* default to software blinking unless hardware blinking is specified */
	if (of_property_read_bool(np, "nxp,hw-blink"))
338
		pdata->blink_type = PCA963X_HW_BLINK;
339
	else
340
		pdata->blink_type = PCA963X_SW_BLINK;
341

342 343 344
	if (of_property_read_u32(np, "nxp,period-scale", &chip->scaling))
		chip->scaling = 1000;

345 346 347 348 349 350
	/* default to non-inverted output, unless inverted is specified */
	if (of_property_read_bool(np, "nxp,inverted-out"))
		pdata->dir = PCA963X_INVERTED;
	else
		pdata->dir = PCA963X_NORMAL;

351 352 353
	return pdata;
}

354
static const struct of_device_id of_pca963x_match[] = {
355 356 357
	{ .compatible = "nxp,pca9632", },
	{ .compatible = "nxp,pca9633", },
	{ .compatible = "nxp,pca9634", },
358
	{ .compatible = "nxp,pca9635", },
359 360
	{},
};
361
MODULE_DEVICE_TABLE(of, of_pca963x_match);
362
#else
363 364
static struct pca963x_platform_data *
pca963x_dt_init(struct i2c_client *client, struct pca963x_chipdef *chip)
365 366 367 368 369
{
	return ERR_PTR(-ENODEV);
}
#endif

370
static int pca963x_probe(struct i2c_client *client,
371 372
					const struct i2c_device_id *id)
{
373 374 375 376
	struct pca963x *pca963x_chip;
	struct pca963x_led *pca963x;
	struct pca963x_platform_data *pdata;
	struct pca963x_chipdef *chip;
377 378
	int i, err;

379 380 381 382 383 384 385 386 387 388
	if (id) {
		chip = &pca963x_chipdefs[id->driver_data];
	} else {
		const struct acpi_device_id *acpi_id;

		acpi_id = acpi_match_device(pca963x_acpi_ids, &client->dev);
		if (!acpi_id)
			return -ENODEV;
		chip = &pca963x_chipdefs[acpi_id->driver_data];
	}
389
	pdata = dev_get_platdata(&client->dev);
390

391
	if (!pdata) {
392
		pdata = pca963x_dt_init(client, chip);
393 394 395 396 397 398
		if (IS_ERR(pdata)) {
			dev_warn(&client->dev, "could not parse configuration\n");
			pdata = NULL;
		}
	}

399 400 401 402 403
	if (pdata && (pdata->leds.num_leds < 1 ||
				 pdata->leds.num_leds > chip->n_leds)) {
		dev_err(&client->dev, "board info must claim 1-%d LEDs",
								chip->n_leds);
		return -EINVAL;
404 405
	}

406
	pca963x_chip = devm_kzalloc(&client->dev, sizeof(*pca963x_chip),
407
								GFP_KERNEL);
408
	if (!pca963x_chip)
409
		return -ENOMEM;
410
	pca963x = devm_kcalloc(&client->dev, chip->n_leds, sizeof(*pca963x),
411
								GFP_KERNEL);
412
	if (!pca963x)
413 414
		return -ENOMEM;

415
	i2c_set_clientdata(client, pca963x_chip);
416

417 418 419 420
	mutex_init(&pca963x_chip->mutex);
	pca963x_chip->chipdef = chip;
	pca963x_chip->client = client;
	pca963x_chip->leds = pca963x;
421 422

	/* Turn off LEDs by default*/
423 424
	for (i = 0; i < chip->n_leds / 4; i++)
		i2c_smbus_write_byte_data(client, chip->ledout_base + i, 0x00);
425

426
	for (i = 0; i < chip->n_leds; i++) {
427 428
		pca963x[i].led_num = i;
		pca963x[i].chip = pca963x_chip;
429 430

		/* Platform data can specify LED names and default triggers */
431 432
		if (pdata && i < pdata->leds.num_leds) {
			if (pdata->leds.leds[i].name)
433 434
				snprintf(pca963x[i].name,
					 sizeof(pca963x[i].name), "pca963x:%s",
435 436
					 pdata->leds.leds[i].name);
			if (pdata->leds.leds[i].default_trigger)
437
				pca963x[i].led_cdev.default_trigger =
438
					pdata->leds.leds[i].default_trigger;
439
		}
440 441
		if (!pdata || i >= pdata->leds.num_leds ||
						!pdata->leds.leds[i].name)
442 443
			snprintf(pca963x[i].name, sizeof(pca963x[i].name),
				 "pca963x:%d:%.2x:%d", client->adapter->nr,
444
				 client->addr, i);
445

446
		pca963x[i].led_cdev.name = pca963x[i].name;
447
		pca963x[i].led_cdev.brightness_set_blocking = pca963x_led_set;
448

449 450
		if (pdata && pdata->blink_type == PCA963X_HW_BLINK)
			pca963x[i].led_cdev.blink_set = pca963x_blink_set;
451

452
		err = led_classdev_register(&client->dev, &pca963x[i].led_cdev);
453 454 455 456
		if (err < 0)
			goto exit;
	}

457 458
	/* Disable LED all-call address, and power down initially */
	i2c_smbus_write_byte_data(client, PCA963X_MODE1, BIT(4));
459

460
	if (pdata) {
461 462
		u8 mode2 = i2c_smbus_read_byte_data(pca963x->chip->client,
						    PCA963X_MODE2);
463 464
		/* Configure output: open-drain or totem pole (push-pull) */
		if (pdata->outdrv == PCA963X_OPEN_DRAIN)
465
			mode2 |= 0x01;
466
		else
467 468 469 470 471 472
			mode2 |= 0x05;
		/* Configure direction: normal or inverted */
		if (pdata->dir == PCA963X_INVERTED)
			mode2 |= 0x10;
		i2c_smbus_write_byte_data(pca963x->chip->client, PCA963X_MODE2,
					  mode2);
473
	}
474

475 476 477
	return 0;

exit:
478
	while (i--)
479
		led_classdev_unregister(&pca963x[i].led_cdev);
480 481 482 483

	return err;
}

484
static int pca963x_remove(struct i2c_client *client)
485
{
486
	struct pca963x *pca963x = i2c_get_clientdata(client);
487 488
	int i;

489
	for (i = 0; i < pca963x->chipdef->n_leds; i++)
490
		led_classdev_unregister(&pca963x->leds[i].led_cdev);
491 492 493 494

	return 0;
}

495
static struct i2c_driver pca963x_driver = {
496
	.driver = {
497 498
		.name	= "leds-pca963x",
		.of_match_table = of_match_ptr(of_pca963x_match),
499
		.acpi_match_table = ACPI_PTR(pca963x_acpi_ids),
500
	},
501 502 503
	.probe	= pca963x_probe,
	.remove	= pca963x_remove,
	.id_table = pca963x_id,
504 505
};

506
module_i2c_driver(pca963x_driver);
507 508

MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>");
509
MODULE_DESCRIPTION("PCA963X LED driver");
510
MODULE_LICENSE("GPL v2");