leds-pca9532.c 13.4 KB
Newer Older
R
Riku Voipio 已提交
1 2 3
/*
 * pca9532.c - 16-bit Led dimmer
 *
4
 * Copyright (C) 2011 Jan Weitzel
R
Riku Voipio 已提交
5
 * Copyright (C) 2008 Riku Voipio
R
Riku Voipio 已提交
6 7 8 9 10
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
11
 * Datasheet: http://www.nxp.com/documents/data_sheet/PCA9532.pdf
R
Riku Voipio 已提交
12 13 14 15 16
 *
 */

#include <linux/module.h>
#include <linux/i2c.h>
17
#include <linux/slab.h>
R
Riku Voipio 已提交
18 19 20
#include <linux/leds.h>
#include <linux/input.h>
#include <linux/mutex.h>
21
#include <linux/workqueue.h>
R
Riku Voipio 已提交
22
#include <linux/leds-pca9532.h>
23
#include <linux/gpio.h>
24 25
#include <linux/of.h>
#include <linux/of_device.h>
R
Riku Voipio 已提交
26

27 28 29 30 31 32 33
/* m =  num_leds*/
#define PCA9532_REG_INPUT(i)	((i) >> 3)
#define PCA9532_REG_OFFSET(m)	((m) >> 4)
#define PCA9532_REG_PSC(m, i)	(PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
#define PCA9532_REG_PWM(m, i)	(PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
#define LED_REG(m, led)		(PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
#define LED_NUM(led)		(led & 0x3)
R
Riku Voipio 已提交
34 35 36

#define ldev_to_led(c)       container_of(c, struct pca9532_led, ldev)

37 38 39 40
struct pca9532_chip_info {
	u8	num_leds;
};

R
Riku Voipio 已提交
41 42 43 44
struct pca9532_data {
	struct i2c_client *client;
	struct pca9532_led leds[16];
	struct mutex update_lock;
45
	struct input_dev *idev;
46
	struct work_struct work;
47 48 49
#ifdef CONFIG_LEDS_PCA9532_GPIO
	struct gpio_chip gpio;
#endif
50
	const struct pca9532_chip_info *chip_info;
R
Riku Voipio 已提交
51 52 53 54 55 56 57 58
	u8 pwm[2];
	u8 psc[2];
};

static int pca9532_probe(struct i2c_client *client,
	const struct i2c_device_id *id);
static int pca9532_remove(struct i2c_client *client);

59 60 61 62 63 64 65
enum {
	pca9530,
	pca9531,
	pca9532,
	pca9533,
};

R
Riku Voipio 已提交
66
static const struct i2c_device_id pca9532_id[] = {
67 68 69 70
	{ "pca9530", pca9530 },
	{ "pca9531", pca9531 },
	{ "pca9532", pca9532 },
	{ "pca9533", pca9533 },
R
Riku Voipio 已提交
71 72 73 74 75
	{ }
};

MODULE_DEVICE_TABLE(i2c, pca9532_id);

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
	[pca9530] = {
		.num_leds = 2,
	},
	[pca9531] = {
		.num_leds = 8,
	},
	[pca9532] = {
		.num_leds = 16,
	},
	[pca9533] = {
		.num_leds = 4,
	},
};

91 92 93 94 95 96 97 98 99 100 101 102
#ifdef CONFIG_OF
static const struct of_device_id of_pca9532_leds_match[] = {
	{ .compatible = "nxp,pca9530", .data = (void *)pca9530 },
	{ .compatible = "nxp,pca9531", .data = (void *)pca9531 },
	{ .compatible = "nxp,pca9532", .data = (void *)pca9532 },
	{ .compatible = "nxp,pca9533", .data = (void *)pca9533 },
	{},
};

MODULE_DEVICE_TABLE(of, of_pca9532_leds_match);
#endif

R
Riku Voipio 已提交
103 104
static struct i2c_driver pca9532_driver = {
	.driver = {
105
		.name = "leds-pca953x",
106
		.of_match_table = of_match_ptr(of_pca9532_leds_match),
R
Riku Voipio 已提交
107
	},
108
	.probe = pca9532_probe,
R
Riku Voipio 已提交
109 110 111 112
	.remove = pca9532_remove,
	.id_table = pca9532_id,
};

L
Lucas De Marchi 已提交
113
/* We have two pwm/blinkers, but 16 possible leds to drive. Additionally,
R
Riku Voipio 已提交
114 115 116 117
 * the clever Thecus people are using one pwm to drive the beeper. So,
 * as a compromise we average one pwm to the values requested by all
 * leds that are not ON/OFF.
 * */
118
static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
R
Riku Voipio 已提交
119 120 121 122
	enum led_brightness value)
{
	int a = 0, b = 0, i = 0;
	struct pca9532_data *data = i2c_get_clientdata(client);
123
	for (i = 0; i < data->chip_info->num_leds; i++) {
R
Riku Voipio 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		if (data->leds[i].type == PCA9532_TYPE_LED &&
			data->leds[i].state == PCA9532_PWM0+pwm) {
				a++;
				b += data->leds[i].ldev.brightness;
		}
	}
	if (a == 0) {
		dev_err(&client->dev,
		"fear of division by zero %d/%d, wanted %d\n",
			b, a, value);
		return -EINVAL;
	}
	b = b/a;
	if (b > 0xFF)
		return -EINVAL;
	data->pwm[pwm] = b;
140 141
	data->psc[pwm] = blink;
	return 0;
142 143 144 145
}

static int pca9532_setpwm(struct i2c_client *client, int pwm)
{
146
	struct pca9532_data *data = i2c_get_clientdata(client);
147 148
	u8 maxleds = data->chip_info->num_leds;

149
	mutex_lock(&data->update_lock);
150
	i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
R
Riku Voipio 已提交
151
		data->pwm[pwm]);
152
	i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
R
Riku Voipio 已提交
153 154 155 156 157 158 159 160 161 162
		data->psc[pwm]);
	mutex_unlock(&data->update_lock);
	return 0;
}

/* Set LED routing */
static void pca9532_setled(struct pca9532_led *led)
{
	struct i2c_client *client = led->client;
	struct pca9532_data *data = i2c_get_clientdata(client);
163
	u8 maxleds = data->chip_info->num_leds;
R
Riku Voipio 已提交
164 165 166
	char reg;

	mutex_lock(&data->update_lock);
167
	reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
R
Riku Voipio 已提交
168 169 170 171
	/* zero led bits */
	reg = reg & ~(0x3<<LED_NUM(led->id)*2);
	/* set the new value */
	reg = reg | (led->state << LED_NUM(led->id)*2);
172
	i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
R
Riku Voipio 已提交
173 174 175
	mutex_unlock(&data->update_lock);
}

176
static int pca9532_set_brightness(struct led_classdev *led_cdev,
R
Riku Voipio 已提交
177 178 179 180 181 182 183 184 185 186 187
	enum led_brightness value)
{
	int err = 0;
	struct pca9532_led *led = ldev_to_led(led_cdev);

	if (value == LED_OFF)
		led->state = PCA9532_OFF;
	else if (value == LED_FULL)
		led->state = PCA9532_ON;
	else {
		led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */
188
		err = pca9532_calcpwm(led->client, 0, 0, value);
R
Riku Voipio 已提交
189
		if (err)
190
			return err;
R
Riku Voipio 已提交
191
	}
192 193 194 195
	if (led->state == PCA9532_PWM0)
		pca9532_setpwm(led->client, 0);
	pca9532_setled(led);
	return err;
R
Riku Voipio 已提交
196 197 198 199 200 201 202 203
}

static int pca9532_set_blink(struct led_classdev *led_cdev,
	unsigned long *delay_on, unsigned long *delay_off)
{
	struct pca9532_led *led = ldev_to_led(led_cdev);
	struct i2c_client *client = led->client;
	int psc;
204
	int err = 0;
R
Riku Voipio 已提交
205 206

	if (*delay_on == 0 && *delay_off == 0) {
207
		/* led subsystem ask us for a blink rate */
208
		*delay_on = 1000;
R
Riku Voipio 已提交
209 210 211 212 213 214 215
		*delay_off = 1000;
	}
	if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
		return -EINVAL;

	/* Thecus specific: only use PSC/PWM 0 */
	psc = (*delay_on * 152-1)/1000;
216 217 218
	err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
	if (err)
		return err;
219 220 221 222
	if (led->state == PCA9532_PWM0)
		pca9532_setpwm(led->client, 0);
	pca9532_setled(led);

223
	return 0;
R
Riku Voipio 已提交
224 225
}

226 227
static int pca9532_event(struct input_dev *dev, unsigned int type,
	unsigned int code, int value)
R
Riku Voipio 已提交
228 229 230
{
	struct pca9532_data *data = input_get_drvdata(dev);

231
	if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
R
Riku Voipio 已提交
232 233 234 235 236 237 238 239
		return -1;

	/* XXX: allow different kind of beeps with psc/pwm modifications */
	if (value > 1 && value < 32767)
		data->pwm[1] = 127;
	else
		data->pwm[1] = 0;

240
	schedule_work(&data->work);
241

242
	return 0;
243 244 245 246
}

static void pca9532_input_work(struct work_struct *work)
{
247 248 249 250
	struct pca9532_data *data =
		container_of(work, struct pca9532_data, work);
	u8 maxleds = data->chip_info->num_leds;

R
Riku Voipio 已提交
251
	mutex_lock(&data->update_lock);
252
	i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
R
Riku Voipio 已提交
253 254
		data->pwm[1]);
	mutex_unlock(&data->update_lock);
255
}
R
Riku Voipio 已提交
256

257 258 259
#ifdef CONFIG_LEDS_PCA9532_GPIO
static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
{
260
	struct pca9532_data *data = gpiochip_get_data(gc);
261 262 263 264 265 266 267 268 269 270
	struct pca9532_led *led = &data->leds[offset];

	if (led->type == PCA9532_TYPE_GPIO)
		return 0;

	return -EBUSY;
}

static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
{
271
	struct pca9532_data *data = gpiochip_get_data(gc);
272 273 274 275 276 277 278 279 280 281 282 283
	struct pca9532_led *led = &data->leds[offset];

	if (val)
		led->state = PCA9532_ON;
	else
		led->state = PCA9532_OFF;

	pca9532_setled(led);
}

static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
{
284
	struct pca9532_data *data = gpiochip_get_data(gc);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	unsigned char reg;

	reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));

	return !!(reg & (1 << (offset % 8)));
}

static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
{
	/* To use as input ensure pin is not driven */
	pca9532_gpio_set_value(gc, offset, 0);

	return 0;
}

static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
{
	pca9532_gpio_set_value(gc, offset, val);

	return 0;
}
#endif /* CONFIG_LEDS_PCA9532_GPIO */

static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
A
Axel Lin 已提交
309 310 311 312
{
	int i = n_devs;

	if (!data)
313
		return -EINVAL;
A
Axel Lin 已提交
314 315 316 317

	while (--i >= 0) {
		switch (data->leds[i].type) {
		case PCA9532_TYPE_NONE:
318
		case PCA9532_TYPE_GPIO:
A
Axel Lin 已提交
319 320 321 322 323 324 325 326 327 328 329 330
			break;
		case PCA9532_TYPE_LED:
			led_classdev_unregister(&data->leds[i].ldev);
			break;
		case PCA9532_TYPE_N2100_BEEP:
			if (data->idev != NULL) {
				cancel_work_sync(&data->work);
				data->idev = NULL;
			}
			break;
		}
	}
331 332

#ifdef CONFIG_LEDS_PCA9532_GPIO
333
	if (data->gpio.parent)
334
		gpiochip_remove(&data->gpio);
335 336 337
#endif

	return 0;
A
Axel Lin 已提交
338 339
}

R
Riku Voipio 已提交
340 341 342 343
static int pca9532_configure(struct i2c_client *client,
	struct pca9532_data *data, struct pca9532_platform_data *pdata)
{
	int i, err = 0;
344
	int gpios = 0;
345
	u8 maxleds = data->chip_info->num_leds;
R
Riku Voipio 已提交
346 347 348 349

	for (i = 0; i < 2; i++)	{
		data->pwm[i] = pdata->pwm[i];
		data->psc[i] = pdata->psc[i];
350
		i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
R
Riku Voipio 已提交
351
			data->pwm[i]);
352
		i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
R
Riku Voipio 已提交
353 354 355
			data->psc[i]);
	}

356
	for (i = 0; i < data->chip_info->num_leds; i++) {
R
Riku Voipio 已提交
357 358 359 360 361 362 363 364
		struct pca9532_led *led = &data->leds[i];
		struct pca9532_led *pled = &pdata->leds[i];
		led->client = client;
		led->id = i;
		led->type = pled->type;
		switch (led->type) {
		case PCA9532_TYPE_NONE:
			break;
365 366 367
		case PCA9532_TYPE_GPIO:
			gpios++;
			break;
R
Riku Voipio 已提交
368 369
		case PCA9532_TYPE_LED:
			led->state = pled->state;
370
			led->name = pled->name;
R
Riku Voipio 已提交
371
			led->ldev.name = led->name;
372
			led->ldev.default_trigger = pled->default_trigger;
R
Riku Voipio 已提交
373
			led->ldev.brightness = LED_OFF;
374 375
			led->ldev.brightness_set_blocking =
						pca9532_set_brightness;
R
Riku Voipio 已提交
376
			led->ldev.blink_set = pca9532_set_blink;
377 378
			err = led_classdev_register(&client->dev, &led->ldev);
			if (err < 0) {
R
Riku Voipio 已提交
379 380 381 382 383 384 385 386 387 388 389
				dev_err(&client->dev,
					"couldn't register LED %s\n",
					led->name);
				goto exit;
			}
			pca9532_setled(led);
			break;
		case PCA9532_TYPE_N2100_BEEP:
			BUG_ON(data->idev);
			led->state = PCA9532_PWM1;
			pca9532_setled(led);
390
			data->idev = devm_input_allocate_device(&client->dev);
R
Riku Voipio 已提交
391 392 393 394 395 396 397
			if (data->idev == NULL) {
				err = -ENOMEM;
				goto exit;
			}
			data->idev->name = pled->name;
			data->idev->phys = "i2c/pca9532";
			data->idev->id.bustype = BUS_HOST;
398
			data->idev->id.vendor = 0x001f;
R
Riku Voipio 已提交
399 400 401 402 403 404 405
			data->idev->id.product = 0x0001;
			data->idev->id.version = 0x0100;
			data->idev->evbit[0] = BIT_MASK(EV_SND);
			data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
						BIT_MASK(SND_TONE);
			data->idev->event = pca9532_event;
			input_set_drvdata(data->idev, data);
406
			INIT_WORK(&data->work, pca9532_input_work);
R
Riku Voipio 已提交
407 408
			err = input_register_device(data->idev);
			if (err) {
409
				cancel_work_sync(&data->work);
R
Riku Voipio 已提交
410 411 412 413 414 415
				data->idev = NULL;
				goto exit;
			}
			break;
		}
	}
416 417 418 419 420 421 422 423 424 425 426

#ifdef CONFIG_LEDS_PCA9532_GPIO
	if (gpios) {
		data->gpio.label = "gpio-pca9532";
		data->gpio.direction_input = pca9532_gpio_direction_input;
		data->gpio.direction_output = pca9532_gpio_direction_output;
		data->gpio.set = pca9532_gpio_set_value;
		data->gpio.get = pca9532_gpio_get_value;
		data->gpio.request = pca9532_gpio_request_pin;
		data->gpio.can_sleep = 1;
		data->gpio.base = pdata->gpio_base;
427
		data->gpio.ngpio = data->chip_info->num_leds;
428
		data->gpio.parent = &client->dev;
429 430
		data->gpio.owner = THIS_MODULE;

431
		err = gpiochip_add_data(&data->gpio, data);
432 433
		if (err) {
			/* Use data->gpio.dev as a flag for freeing gpiochip */
434
			data->gpio.parent = NULL;
435 436 437 438 439 440 441 442 443
			dev_warn(&client->dev, "could not add gpiochip\n");
		} else {
			dev_info(&client->dev, "gpios %i...%i\n",
				data->gpio.base, data->gpio.base +
				data->gpio.ngpio - 1);
		}
	}
#endif

R
Riku Voipio 已提交
444 445 446
	return 0;

exit:
A
Axel Lin 已提交
447
	pca9532_destroy_devices(data, i);
R
Riku Voipio 已提交
448 449 450
	return err;
}

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
static struct pca9532_platform_data *
pca9532_of_populate_pdata(struct device *dev, struct device_node *np)
{
	struct pca9532_platform_data *pdata;
	struct device_node *child;
	const struct of_device_id *match;
	int devid, maxleds;
	int i = 0;

	match = of_match_device(of_pca9532_leds_match, dev);
	if (!match)
		return ERR_PTR(-ENODEV);

	devid = (int)(uintptr_t)match->data;
	maxleds = pca9532_chip_info_tbl[devid].num_leds;

	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return ERR_PTR(-ENOMEM);

	for_each_child_of_node(np, child) {
		if (of_property_read_string(child, "label",
					    &pdata->leds[i].name))
			pdata->leds[i].name = child->name;
		of_property_read_u32(child, "type", &pdata->leds[i].type);
		of_property_read_string(child, "linux,default-trigger",
					&pdata->leds[i].default_trigger);
		if (++i >= maxleds) {
			of_node_put(child);
			break;
		}
	}

	return pdata;
}

R
Riku Voipio 已提交
487 488 489
static int pca9532_probe(struct i2c_client *client,
	const struct i2c_device_id *id)
{
490
	int devid;
R
Riku Voipio 已提交
491
	struct pca9532_data *data = i2c_get_clientdata(client);
J
Jingoo Han 已提交
492 493
	struct pca9532_platform_data *pca9532_pdata =
			dev_get_platdata(&client->dev);
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	struct device_node *np = client->dev.of_node;

	if (!pca9532_pdata) {
		if (np) {
			pca9532_pdata =
				pca9532_of_populate_pdata(&client->dev, np);
			if (IS_ERR(pca9532_pdata))
				return PTR_ERR(pca9532_pdata);
		} else {
			dev_err(&client->dev, "no platform data\n");
			return -EINVAL;
		}
		devid = (int)(uintptr_t)of_match_device(
			of_pca9532_leds_match, &client->dev)->data;
	} else {
		devid = id->driver_data;
	}
R
Riku Voipio 已提交
511 512 513 514 515

	if (!i2c_check_functionality(client->adapter,
		I2C_FUNC_SMBUS_BYTE_DATA))
		return -EIO;

516
	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
R
Riku Voipio 已提交
517 518 519
	if (!data)
		return -ENOMEM;

520
	data->chip_info = &pca9532_chip_info_tbl[devid];
521

R
Riku Voipio 已提交
522 523 524 525 526
	dev_info(&client->dev, "setting platform data\n");
	i2c_set_clientdata(client, data);
	data->client = client;
	mutex_init(&data->update_lock);

527
	return pca9532_configure(client, data, pca9532_pdata);
R
Riku Voipio 已提交
528 529 530 531 532
}

static int pca9532_remove(struct i2c_client *client)
{
	struct pca9532_data *data = i2c_get_clientdata(client);
533 534
	int err;

535
	err = pca9532_destroy_devices(data, data->chip_info->num_leds);
536 537 538
	if (err)
		return err;

R
Riku Voipio 已提交
539 540 541
	return 0;
}

542
module_i2c_driver(pca9532_driver);
R
Riku Voipio 已提交
543

R
Riku Voipio 已提交
544
MODULE_AUTHOR("Riku Voipio");
R
Riku Voipio 已提交
545 546
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("PCA 9532 LED dimmer");