1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/slab.h>
18#include <linux/leds.h>
19#include <linux/input.h>
20#include <linux/mutex.h>
21#include <linux/workqueue.h>
22#include <linux/leds-pca9532.h>
23#include <linux/gpio.h>
24
25
26#define PCA9532_REG_INPUT(i) ((i) >> 3)
27#define PCA9532_REG_OFFSET(m) ((m) >> 4)
28#define PCA9532_REG_PSC(m, i) (PCA9532_REG_OFFSET(m) + 0x1 + (i) * 2)
29#define PCA9532_REG_PWM(m, i) (PCA9532_REG_OFFSET(m) + 0x2 + (i) * 2)
30#define LED_REG(m, led) (PCA9532_REG_OFFSET(m) + 0x5 + (led >> 2))
31#define LED_NUM(led) (led & 0x3)
32
33#define ldev_to_led(c) container_of(c, struct pca9532_led, ldev)
34
35struct pca9532_chip_info {
36 u8 num_leds;
37};
38
39struct 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
53static int pca9532_probe(struct i2c_client *client,
54 const struct i2c_device_id *id);
55static int pca9532_remove(struct i2c_client *client);
56
57enum {
58 pca9530,
59 pca9531,
60 pca9532,
61 pca9533,
62};
63
64static const struct i2c_device_id pca9532_id[] = {
65 { "pca9530", pca9530 },
66 { "pca9531", pca9531 },
67 { "pca9532", pca9532 },
68 { "pca9533", pca9533 },
69 { }
70};
71
72MODULE_DEVICE_TABLE(i2c, pca9532_id);
73
74static const struct pca9532_chip_info pca9532_chip_info_tbl[] = {
75 [pca9530] = {
76 .num_leds = 2,
77 },
78 [pca9531] = {
79 .num_leds = 8,
80 },
81 [pca9532] = {
82 .num_leds = 16,
83 },
84 [pca9533] = {
85 .num_leds = 4,
86 },
87};
88
89static struct i2c_driver pca9532_driver = {
90 .driver = {
91 .name = "leds-pca953x",
92 },
93 .probe = pca9532_probe,
94 .remove = pca9532_remove,
95 .id_table = pca9532_id,
96};
97
98
99
100
101
102
103static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink,
104 enum led_brightness value)
105{
106 int a = 0, b = 0, i = 0;
107 struct pca9532_data *data = i2c_get_clientdata(client);
108 for (i = 0; i < data->chip_info->num_leds; i++) {
109 if (data->leds[i].type == PCA9532_TYPE_LED &&
110 data->leds[i].state == PCA9532_PWM0+pwm) {
111 a++;
112 b += data->leds[i].ldev.brightness;
113 }
114 }
115 if (a == 0) {
116 dev_err(&client->dev,
117 "fear of division by zero %d/%d, wanted %d\n",
118 b, a, value);
119 return -EINVAL;
120 }
121 b = b/a;
122 if (b > 0xFF)
123 return -EINVAL;
124 data->pwm[pwm] = b;
125 data->psc[pwm] = blink;
126 return 0;
127}
128
129static int pca9532_setpwm(struct i2c_client *client, int pwm)
130{
131 struct pca9532_data *data = i2c_get_clientdata(client);
132 u8 maxleds = data->chip_info->num_leds;
133
134 mutex_lock(&data->update_lock);
135 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, pwm),
136 data->pwm[pwm]);
137 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, pwm),
138 data->psc[pwm]);
139 mutex_unlock(&data->update_lock);
140 return 0;
141}
142
143
144static void pca9532_setled(struct pca9532_led *led)
145{
146 struct i2c_client *client = led->client;
147 struct pca9532_data *data = i2c_get_clientdata(client);
148 u8 maxleds = data->chip_info->num_leds;
149 char reg;
150
151 mutex_lock(&data->update_lock);
152 reg = i2c_smbus_read_byte_data(client, LED_REG(maxleds, led->id));
153
154 reg = reg & ~(0x3<<LED_NUM(led->id)*2);
155
156 reg = reg | (led->state << LED_NUM(led->id)*2);
157 i2c_smbus_write_byte_data(client, LED_REG(maxleds, led->id), reg);
158 mutex_unlock(&data->update_lock);
159}
160
161static void pca9532_set_brightness(struct led_classdev *led_cdev,
162 enum led_brightness value)
163{
164 int err = 0;
165 struct pca9532_led *led = ldev_to_led(led_cdev);
166
167 if (value == LED_OFF)
168 led->state = PCA9532_OFF;
169 else if (value == LED_FULL)
170 led->state = PCA9532_ON;
171 else {
172 led->state = PCA9532_PWM0;
173 err = pca9532_calcpwm(led->client, 0, 0, value);
174 if (err)
175 return;
176 }
177 schedule_work(&led->work);
178}
179
180static int pca9532_set_blink(struct led_classdev *led_cdev,
181 unsigned long *delay_on, unsigned long *delay_off)
182{
183 struct pca9532_led *led = ldev_to_led(led_cdev);
184 struct i2c_client *client = led->client;
185 int psc;
186 int err = 0;
187
188 if (*delay_on == 0 && *delay_off == 0) {
189
190 *delay_on = 1000;
191 *delay_off = 1000;
192 }
193 if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6)
194 return -EINVAL;
195
196
197 psc = (*delay_on * 152-1)/1000;
198 err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness);
199 if (err)
200 return err;
201 schedule_work(&led->work);
202 return 0;
203}
204
205static int pca9532_event(struct input_dev *dev, unsigned int type,
206 unsigned int code, int value)
207{
208 struct pca9532_data *data = input_get_drvdata(dev);
209
210 if (!(type == EV_SND && (code == SND_BELL || code == SND_TONE)))
211 return -1;
212
213
214 if (value > 1 && value < 32767)
215 data->pwm[1] = 127;
216 else
217 data->pwm[1] = 0;
218
219 schedule_work(&data->work);
220
221 return 0;
222}
223
224static void pca9532_input_work(struct work_struct *work)
225{
226 struct pca9532_data *data =
227 container_of(work, struct pca9532_data, work);
228 u8 maxleds = data->chip_info->num_leds;
229
230 mutex_lock(&data->update_lock);
231 i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(maxleds, 1),
232 data->pwm[1]);
233 mutex_unlock(&data->update_lock);
234}
235
236static void pca9532_led_work(struct work_struct *work)
237{
238 struct pca9532_led *led;
239 led = container_of(work, struct pca9532_led, work);
240 if (led->state == PCA9532_PWM0)
241 pca9532_setpwm(led->client, 0);
242 pca9532_setled(led);
243}
244
245#ifdef CONFIG_LEDS_PCA9532_GPIO
246static int pca9532_gpio_request_pin(struct gpio_chip *gc, unsigned offset)
247{
248 struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
249 struct pca9532_led *led = &data->leds[offset];
250
251 if (led->type == PCA9532_TYPE_GPIO)
252 return 0;
253
254 return -EBUSY;
255}
256
257static void pca9532_gpio_set_value(struct gpio_chip *gc, unsigned offset, int val)
258{
259 struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
260 struct pca9532_led *led = &data->leds[offset];
261
262 if (val)
263 led->state = PCA9532_ON;
264 else
265 led->state = PCA9532_OFF;
266
267 pca9532_setled(led);
268}
269
270static int pca9532_gpio_get_value(struct gpio_chip *gc, unsigned offset)
271{
272 struct pca9532_data *data = container_of(gc, struct pca9532_data, gpio);
273 unsigned char reg;
274
275 reg = i2c_smbus_read_byte_data(data->client, PCA9532_REG_INPUT(offset));
276
277 return !!(reg & (1 << (offset % 8)));
278}
279
280static int pca9532_gpio_direction_input(struct gpio_chip *gc, unsigned offset)
281{
282
283 pca9532_gpio_set_value(gc, offset, 0);
284
285 return 0;
286}
287
288static int pca9532_gpio_direction_output(struct gpio_chip *gc, unsigned offset, int val)
289{
290 pca9532_gpio_set_value(gc, offset, val);
291
292 return 0;
293}
294#endif
295
296static int pca9532_destroy_devices(struct pca9532_data *data, int n_devs)
297{
298 int i = n_devs;
299
300 if (!data)
301 return -EINVAL;
302
303 while (--i >= 0) {
304 switch (data->leds[i].type) {
305 case PCA9532_TYPE_NONE:
306 case PCA9532_TYPE_GPIO:
307 break;
308 case PCA9532_TYPE_LED:
309 led_classdev_unregister(&data->leds[i].ldev);
310 cancel_work_sync(&data->leds[i].work);
311 break;
312 case PCA9532_TYPE_N2100_BEEP:
313 if (data->idev != NULL) {
314 input_unregister_device(data->idev);
315 cancel_work_sync(&data->work);
316 data->idev = NULL;
317 }
318 break;
319 }
320 }
321
322#ifdef CONFIG_LEDS_PCA9532_GPIO
323 if (data->gpio.dev) {
324 int err = gpiochip_remove(&data->gpio);
325 if (err) {
326 dev_err(&data->client->dev, "%s failed, %d\n",
327 "gpiochip_remove()", err);
328 return err;
329 }
330 }
331#endif
332
333 return 0;
334}
335
336static int pca9532_configure(struct i2c_client *client,
337 struct pca9532_data *data, struct pca9532_platform_data *pdata)
338{
339 int i, err = 0;
340 int gpios = 0;
341 u8 maxleds = data->chip_info->num_leds;
342
343 for (i = 0; i < 2; i++) {
344 data->pwm[i] = pdata->pwm[i];
345 data->psc[i] = pdata->psc[i];
346 i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(maxleds, i),
347 data->pwm[i]);
348 i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(maxleds, i),
349 data->psc[i]);
350 }
351
352 for (i = 0; i < data->chip_info->num_leds; i++) {
353 struct pca9532_led *led = &data->leds[i];
354 struct pca9532_led *pled = &pdata->leds[i];
355 led->client = client;
356 led->id = i;
357 led->type = pled->type;
358 switch (led->type) {
359 case PCA9532_TYPE_NONE:
360 break;
361 case PCA9532_TYPE_GPIO:
362 gpios++;
363 break;
364 case PCA9532_TYPE_LED:
365 led->state = pled->state;
366 led->name = pled->name;
367 led->ldev.name = led->name;
368 led->ldev.brightness = LED_OFF;
369 led->ldev.brightness_set = pca9532_set_brightness;
370 led->ldev.blink_set = pca9532_set_blink;
371 INIT_WORK(&led->work, pca9532_led_work);
372 err = led_classdev_register(&client->dev, &led->ldev);
373 if (err < 0) {
374 dev_err(&client->dev,
375 "couldn't register LED %s\n",
376 led->name);
377 goto exit;
378 }
379 pca9532_setled(led);
380 break;
381 case PCA9532_TYPE_N2100_BEEP:
382 BUG_ON(data->idev);
383 led->state = PCA9532_PWM1;
384 pca9532_setled(led);
385 data->idev = input_allocate_device();
386 if (data->idev == NULL) {
387 err = -ENOMEM;
388 goto exit;
389 }
390 data->idev->name = pled->name;
391 data->idev->phys = "i2c/pca9532";
392 data->idev->id.bustype = BUS_HOST;
393 data->idev->id.vendor = 0x001f;
394 data->idev->id.product = 0x0001;
395 data->idev->id.version = 0x0100;
396 data->idev->evbit[0] = BIT_MASK(EV_SND);
397 data->idev->sndbit[0] = BIT_MASK(SND_BELL) |
398 BIT_MASK(SND_TONE);
399 data->idev->event = pca9532_event;
400 input_set_drvdata(data->idev, data);
401 INIT_WORK(&data->work, pca9532_input_work);
402 err = input_register_device(data->idev);
403 if (err) {
404 input_free_device(data->idev);
405 cancel_work_sync(&data->work);
406 data->idev = NULL;
407 goto exit;
408 }
409 break;
410 }
411 }
412
413#ifdef CONFIG_LEDS_PCA9532_GPIO
414 if (gpios) {
415 data->gpio.label = "gpio-pca9532";
416 data->gpio.direction_input = pca9532_gpio_direction_input;
417 data->gpio.direction_output = pca9532_gpio_direction_output;
418 data->gpio.set = pca9532_gpio_set_value;
419 data->gpio.get = pca9532_gpio_get_value;
420 data->gpio.request = pca9532_gpio_request_pin;
421 data->gpio.can_sleep = 1;
422 data->gpio.base = pdata->gpio_base;
423 data->gpio.ngpio = data->chip_info->num_leds;
424 data->gpio.dev = &client->dev;
425 data->gpio.owner = THIS_MODULE;
426
427 err = gpiochip_add(&data->gpio);
428 if (err) {
429
430 data->gpio.dev = NULL;
431 dev_warn(&client->dev, "could not add gpiochip\n");
432 } else {
433 dev_info(&client->dev, "gpios %i...%i\n",
434 data->gpio.base, data->gpio.base +
435 data->gpio.ngpio - 1);
436 }
437 }
438#endif
439
440 return 0;
441
442exit:
443 pca9532_destroy_devices(data, i);
444 return err;
445}
446
447static int pca9532_probe(struct i2c_client *client,
448 const struct i2c_device_id *id)
449{
450 struct pca9532_data *data = i2c_get_clientdata(client);
451 struct pca9532_platform_data *pca9532_pdata = client->dev.platform_data;
452 int err;
453
454 if (!pca9532_pdata)
455 return -EIO;
456
457 if (!i2c_check_functionality(client->adapter,
458 I2C_FUNC_SMBUS_BYTE_DATA))
459 return -EIO;
460
461 data = kzalloc(sizeof(*data), GFP_KERNEL);
462 if (!data)
463 return -ENOMEM;
464
465 data->chip_info = &pca9532_chip_info_tbl[id->driver_data];
466
467 dev_info(&client->dev, "setting platform data\n");
468 i2c_set_clientdata(client, data);
469 data->client = client;
470 mutex_init(&data->update_lock);
471
472 err = pca9532_configure(client, data, pca9532_pdata);
473 if (err)
474 kfree(data);
475
476 return err;
477}
478
479static int pca9532_remove(struct i2c_client *client)
480{
481 struct pca9532_data *data = i2c_get_clientdata(client);
482 int err;
483
484 err = pca9532_destroy_devices(data, data->chip_info->num_leds);
485 if (err)
486 return err;
487
488 kfree(data);
489 return 0;
490}
491
492static int __init pca9532_init(void)
493{
494 return i2c_add_driver(&pca9532_driver);
495}
496
497static void __exit pca9532_exit(void)
498{
499 i2c_del_driver(&pca9532_driver);
500}
501
502MODULE_AUTHOR("Riku Voipio");
503MODULE_LICENSE("GPL");
504MODULE_DESCRIPTION("PCA 9532 LED dimmer");
505
506module_init(pca9532_init);
507module_exit(pca9532_exit);
508
509