[ar71xx] preliminary support for the Ubiquiti RouterStation Pro
[openwrt.git] / target / linux / s3c24xx / patches-2.6.24 / 1291-gta03-pca9632.patch.patch
1 From f6b1f49300a24c329f5d1049031f711c6268be95 Mon Sep 17 00:00:00 2001
2 From: Matt Hsu <matt_hsu@openmoko.org>
3 Date: Thu, 18 Sep 2008 11:14:50 +0100
4 Subject: [PATCH] gta03-pca9632.patch
5
6 - pca9632 is a LED driver which will be adopted in gta03.
7
8 Signed-off-by: Matt Hsu <matt_hsu@openmoko.org>
9 ---
10 drivers/i2c/chips/Kconfig | 9 +
11 drivers/i2c/chips/Makefile | 1 +
12 drivers/i2c/chips/pca9632.c | 551 +++++++++++++++++++++++++++++++++++++++++++
13 drivers/i2c/chips/pca9632.h | 24 ++
14 include/linux/i2c-id.h | 1 +
15 5 files changed, 586 insertions(+), 0 deletions(-)
16 create mode 100644 drivers/i2c/chips/pca9632.c
17 create mode 100644 drivers/i2c/chips/pca9632.h
18
19 diff --git a/drivers/i2c/chips/Kconfig b/drivers/i2c/chips/Kconfig
20 index 1d6d36f..9af3c62 100644
21 --- a/drivers/i2c/chips/Kconfig
22 +++ b/drivers/i2c/chips/Kconfig
23 @@ -193,4 +193,13 @@ config SENSORS_TSL256X
24 This driver can also be built as a module. If so, the module
25 will be called tsl256x.
26
27 +config PCA9632
28 + tristate "Philips/NXP PCA9632 low power LED driver"
29 + depends on I2C
30 + help
31 + If you say yes here you get support for the Philips/NXP PCA9632
32 + LED driver.
33 +
34 + This driver can also be built as a module. If so, the module
35 + will be called pca9632.
36 endmenu
37 diff --git a/drivers/i2c/chips/Makefile b/drivers/i2c/chips/Makefile
38 index 4b6ba04..67cd1e5 100644
39 --- a/drivers/i2c/chips/Makefile
40 +++ b/drivers/i2c/chips/Makefile
41 @@ -18,6 +18,7 @@ obj-$(CONFIG_TPS65010) += tps65010.o
42 obj-$(CONFIG_MENELAUS) += menelaus.o
43 obj-$(CONFIG_SENSORS_TSL2550) += tsl2550.o
44 obj-$(CONFIG_SENSORS_TSL256X) += tsl256x.o
45 +obj-$(CONFIG_PCA9632) += pca9632.o
46
47 ifeq ($(CONFIG_I2C_DEBUG_CHIP),y)
48 EXTRA_CFLAGS += -DDEBUG
49 diff --git a/drivers/i2c/chips/pca9632.c b/drivers/i2c/chips/pca9632.c
50 new file mode 100644
51 index 0000000..deda6a1
52 --- /dev/null
53 +++ b/drivers/i2c/chips/pca9632.c
54 @@ -0,0 +1,551 @@
55 +/*
56 + * Philips/NXP PCA9632 low power LED driver.
57 + * Copyright (C) 2008 Matt Hsu <matt_hsu@openmoko.org>
58 + *
59 + * low_level implementation are based on pcf50606 driver
60 + *
61 + * This program is free software; you can redistribute it and/or modify
62 + * it under the terms of the GNU General Public License as published by
63 + * the Free Software Foundation; version 2 of the License.
64 + *
65 + * TODO:
66 + * - attach ledclass??
67 + * - add platform data
68 + *
69 + */
70 +
71 +#include <linux/module.h>
72 +#include <linux/init.h>
73 +#include <linux/i2c.h>
74 +#include <linux/platform_device.h>
75 +
76 +#include "pca9632.h"
77 +
78 +/* Addresses to scan */
79 +static unsigned short normal_i2c[] = { 0x62, I2C_CLIENT_END };
80 +
81 +/* Insmod parameters */
82 +I2C_CLIENT_INSMOD_1(pca9632);
83 +
84 +enum pca9632_pwr_state {
85 + PCA9632_NORMAL,
86 + PCA9632_SLEEP,
87 +};
88 +
89 +enum pca9632_led_output {
90 + PCA9632_OFF,
91 + PCA9632_ON,
92 + PCA9632_CTRL_BY_PWM,
93 + PCA9632_CTRL_BY_PWM_GRPPWM,
94 +};
95 +
96 +static const char *led_output_name[] = {
97 + [PCA9632_OFF] = "off",
98 + [PCA9632_ON] = "fully-on",
99 + [PCA9632_CTRL_BY_PWM] = "ctrl-by-pwm",
100 + [PCA9632_CTRL_BY_PWM_GRPPWM] = "ctrl-by-pwm-grppwm",
101 +};
102 +
103 +struct pca9632_data {
104 + struct i2c_client client;
105 + struct mutex lock;
106 +};
107 +
108 +static struct i2c_driver pca9632_driver;
109 +static struct platform_device *pca9632_pdev;
110 +
111 +static int pca9632_attach_adapter(struct i2c_adapter *adapter);
112 +static int pca9632_detach_client(struct i2c_client *client);
113 +
114 +static int __reg_write(struct pca9632_data *pca, u_int8_t reg, u_int8_t val)
115 +{
116 + return i2c_smbus_write_byte_data(&pca->client, reg, val);
117 +}
118 +
119 +static int reg_write(struct pca9632_data *pca, u_int8_t reg, u_int8_t val)
120 +{
121 + int ret;
122 +
123 + mutex_lock(&pca->lock);
124 + ret = __reg_write(pca, reg, val);
125 + mutex_unlock(&pca->lock);
126 +
127 + return ret;
128 +}
129 +
130 +static int32_t __reg_read(struct pca9632_data *pca, u_int8_t reg)
131 +{
132 + int32_t ret;
133 +
134 + ret = i2c_smbus_read_byte_data(&pca->client, reg);
135 +
136 + return ret;
137 +}
138 +
139 +static u_int8_t reg_read(struct pca9632_data *pca, u_int8_t reg)
140 +{
141 + int32_t ret;
142 +
143 + mutex_lock(&pca->lock);
144 + ret = __reg_read(pca, reg);
145 + mutex_unlock(&pca->lock);
146 +
147 + return ret & 0xff;
148 +}
149 +
150 +static int reg_set_bit_mask(struct pca9632_data *pca,
151 + u_int8_t reg, u_int8_t mask, u_int8_t val)
152 +{
153 + int ret;
154 + u_int8_t tmp;
155 +
156 + val &= mask;
157 +
158 + mutex_lock(&pca->lock);
159 +
160 + tmp = __reg_read(pca, reg);
161 + tmp &= ~mask;
162 + tmp |= val;
163 + ret = __reg_write(pca, reg, tmp);
164 +
165 + mutex_unlock(&pca->lock);
166 +
167 + return ret;
168 +}
169 +
170 +static inline int calc_dc(uint8_t idc)
171 +{
172 + return (idc * 100) / 256;
173 +}
174 +
175 +/*
176 + * Software reset
177 + */
178 +static int software_rst(struct i2c_adapter *adapter)
179 +{
180 + u8 buf[] = { 0xa5, 0x5a };
181 +
182 + struct i2c_msg msg[] = {
183 + {
184 + .addr = 0x3,
185 + .flags = 0,
186 + .buf = &buf,
187 + .len = sizeof(buf)
188 + }
189 + };
190 +
191 + return i2c_transfer(adapter, msg, 1);
192 +}
193 +
194 +/*
195 + * Group dmblnk control
196 + */
197 +static void config_group_dmblnk(struct pca9632_data *pca, int group_dmblnk_mode)
198 +{
199 + reg_set_bit_mask(pca, PCA9632_REG_MODE2, 0x20,
200 + group_dmblnk_mode << PCA9632_DMBLNK_SHIFT);
201 +}
202 +
203 +static int get_group_dmblnk(struct pca9632_data *pca)
204 +{
205 + return reg_read(pca, PCA9632_REG_MODE2) >> PCA9632_DMBLNK_SHIFT;
206 +}
207 +
208 +static ssize_t show_group_dmblnk(struct device *dev, struct device_attribute
209 + *attr, char *buf)
210 +{
211 + struct i2c_client *client = to_i2c_client(dev);
212 + struct pca9632_data *pca = i2c_get_clientdata(client);
213 +
214 + if (get_group_dmblnk(pca))
215 + return sprintf(buf, "blinking\n");
216 + else
217 + return sprintf(buf, "dimming\n");
218 +}
219 +
220 +static ssize_t set_group_dmblnk(struct device *dev, struct device_attribute
221 + *attr, const char *buf, size_t count)
222 +{
223 + struct i2c_client *client = to_i2c_client(dev);
224 + struct pca9632_data *pca = i2c_get_clientdata(client);
225 + unsigned int mode = simple_strtoul(buf, NULL, 10);
226 +
227 + if (mode)
228 + dev_info(&pca->client.dev, "blinking\n");
229 + else
230 + dev_info(&pca->client.dev, "dimming\n");
231 +
232 + config_group_dmblnk(pca, mode);
233 +
234 + return count;
235 +}
236 +
237 +static DEVICE_ATTR(group_dmblnk, S_IRUGO | S_IWUSR, show_group_dmblnk,
238 + set_group_dmblnk);
239 +
240 +static int reg_id_by_name(const char *name)
241 +{
242 + int reg_id = -1;
243 +
244 + if (!strncmp(name, "led0", 4))
245 + reg_id = PCA9632_REG_PWM0;
246 + else if (!strncmp(name, "led1", 4))
247 + reg_id = PCA9632_REG_PWM1;
248 + else if (!strncmp(name, "led2", 4))
249 + reg_id = PCA9632_REG_PWM2;
250 + else if (!strncmp(name, "led3", 4))
251 + reg_id = PCA9632_REG_PWM3;
252 +
253 + return reg_id;
254 +}
255 +
256 +static int get_led_output(struct pca9632_data *pca, int ldrx)
257 +{
258 + u_int8_t led_state;
259 +
260 + ldrx = ldrx - 2;
261 + led_state = reg_read(pca, PCA9632_REG_LEDOUT);
262 + led_state = (led_state >> (2 * ldrx)) & 0x03;
263 +
264 + return led_state;
265 +}
266 +
267 +static void config_led_output(struct pca9632_data *pca, int ldrx,
268 + enum pca9632_led_output led_output)
269 +{
270 + u_int8_t mask;
271 + int tmp;
272 +
273 + ldrx = ldrx - 2;
274 + mask = 0x03 << (2 * ldrx);
275 + tmp = reg_set_bit_mask(pca, PCA9632_REG_LEDOUT,
276 + mask, led_output << (2 * ldrx));
277 +}
278 +
279 +/*
280 + * Individual brightness control
281 + */
282 +static ssize_t show_brightness(struct device *dev, struct device_attribute
283 + *attr, char *buf)
284 +{
285 + struct i2c_client *client = to_i2c_client(dev);
286 + struct pca9632_data *pca = i2c_get_clientdata(client);
287 + int ldrx;
288 +
289 + ldrx = reg_id_by_name(attr->attr.name);
290 +
291 + switch (get_led_output(pca, ldrx)) {
292 +
293 + case PCA9632_OFF:
294 + case PCA9632_ON:
295 + return sprintf(buf, "%s",
296 + led_output_name[get_led_output(pca, ldrx)]);
297 +
298 + case PCA9632_CTRL_BY_PWM:
299 + return sprintf(buf, "%d%% \n", calc_dc(reg_read(pca, ldrx)));
300 +
301 + case PCA9632_CTRL_BY_PWM_GRPPWM:
302 + /* check group dmblnk */
303 + if (get_group_dmblnk(pca))
304 + return sprintf(buf, "%d%% \n",
305 + calc_dc(reg_read(pca, ldrx)));
306 + return sprintf(buf, "%d%% \n",
307 + calc_dc((reg_read(pca, ldrx) & 0xfc)));
308 + default:
309 + break;
310 + }
311 +
312 + return sprintf(buf, "invalid argument\n");
313 +}
314 +
315 +static ssize_t set_brightness(struct device *dev, struct device_attribute *attr,
316 + const char *buf, size_t count)
317 +{
318 + struct i2c_client *client = to_i2c_client(dev);
319 + struct pca9632_data *pca = i2c_get_clientdata(client);
320 + unsigned int pwm = simple_strtoul(buf, NULL, 10);
321 + int ldrx;
322 +
323 + ldrx = reg_id_by_name(attr->attr.name);
324 + reg_set_bit_mask(pca, ldrx, 0xff, pwm);
325 +
326 + return count;
327 +}
328 +
329 +static
330 +DEVICE_ATTR(led0_pwm, S_IRUGO | S_IWUSR, show_brightness, set_brightness);
331 +static
332 +DEVICE_ATTR(led1_pwm, S_IRUGO | S_IWUSR, show_brightness, set_brightness);
333 +static
334 +DEVICE_ATTR(led2_pwm, S_IRUGO | S_IWUSR, show_brightness, set_brightness);
335 +static
336 +DEVICE_ATTR(led3_pwm, S_IRUGO | S_IWUSR, show_brightness, set_brightness);
337 +
338 +/*
339 + * Group frequency control
340 + */
341 +static ssize_t show_group_freq(struct device *dev, struct device_attribute
342 + *attr, char *buf)
343 +{
344 + uint32_t period;
345 + struct i2c_client *client = to_i2c_client(dev);
346 + struct pca9632_data *pca = i2c_get_clientdata(client);
347 +
348 + period = ((reg_read(pca, PCA9632_REG_GRPFREQ) + 1) * 1000) / 24;
349 +
350 + return sprintf(buf, "%d ms\n", period);
351 +}
352 +
353 +static ssize_t set_group_freq(struct device *dev, struct device_attribute *attr,
354 + const char *buf, size_t count)
355 +{
356 + struct i2c_client *client = to_i2c_client(dev);
357 + struct pca9632_data *pca = i2c_get_clientdata(client);
358 +
359 + unsigned int freq = simple_strtoul(buf, NULL, 10);
360 + reg_write(pca, PCA9632_REG_GRPFREQ, freq);
361 + return count;
362 +}
363 +
364 +static
365 +DEVICE_ATTR(group_freq, S_IRUGO | S_IWUSR, show_group_freq, set_group_freq);
366 +
367 +/*
368 + * Group duty cycle tonrol*
369 + */
370 +static ssize_t show_group_dc(struct device *dev, struct device_attribute *attr,
371 + char *buf)
372 +{
373 + struct i2c_client *client = to_i2c_client(dev);
374 + struct pca9632_data *pca = i2c_get_clientdata(client);
375 +
376 + if (get_group_dmblnk(pca)) {
377 +
378 + if (reg_read(pca, PCA9632_REG_GRPFREQ) <= 0x03)
379 + return sprintf(buf, "%d%% \n",
380 + calc_dc(reg_read(pca, PCA9632_REG_GRPPWM) & 0xfc));
381 +
382 + return sprintf(buf, "%d%% \n", calc_dc(reg_read(pca,
383 + PCA9632_REG_GRPPWM)));
384 + }
385 +
386 + return sprintf(buf, "%d%% \n", calc_dc(reg_read(pca,
387 + PCA9632_REG_GRPPWM) & 0xf0));
388 +}
389 +
390 +static ssize_t set_group_dc(struct device *dev, struct device_attribute *attr,
391 + const char *buf, size_t count)
392 +{
393 + struct i2c_client *client = to_i2c_client(dev);
394 + struct pca9632_data *pca = i2c_get_clientdata(client);
395 +
396 + unsigned int dc = simple_strtoul(buf, NULL, 10);
397 +
398 + reg_set_bit_mask(pca, PCA9632_REG_GRPPWM, 0xff, dc);
399 +
400 + return count;
401 +}
402 +
403 +static DEVICE_ATTR(group_dc, S_IRUGO | S_IWUSR, show_group_dc, set_group_dc);
404 +
405 +/*
406 + * LED driver output
407 + */
408 +static ssize_t show_led_output(struct device *dev, struct device_attribute
409 + *attr, char *buf)
410 +{
411 + struct i2c_client *client = to_i2c_client(dev);
412 + struct pca9632_data *pca = i2c_get_clientdata(client);
413 + int ldrx;
414 +
415 + ldrx = reg_id_by_name(attr->attr.name);
416 +
417 + return sprintf(buf, "%s \n",
418 + led_output_name[get_led_output(pca, ldrx)]);
419 +
420 +}
421 +static ssize_t set_led_output(struct device *dev, struct device_attribute *attr,
422 + const char *buf, size_t count)
423 +{
424 + struct i2c_client *client = to_i2c_client(dev);
425 + struct pca9632_data *pca = i2c_get_clientdata(client);
426 + enum pca9632_led_output led_output;
427 + int ldrx;
428 +
429 + led_output = simple_strtoul(buf, NULL, 10);
430 + ldrx = reg_id_by_name(attr->attr.name);
431 + config_led_output(pca, ldrx, led_output);
432 +
433 + return count;
434 +}
435 +
436 +static
437 +DEVICE_ATTR(led0_output, S_IRUGO | S_IWUSR, show_led_output, set_led_output);
438 +static
439 +DEVICE_ATTR(led1_output, S_IRUGO | S_IWUSR, show_led_output, set_led_output);
440 +static
441 +DEVICE_ATTR(led2_output, S_IRUGO | S_IWUSR, show_led_output, set_led_output);
442 +static
443 +DEVICE_ATTR(led3_output, S_IRUGO | S_IWUSR, show_led_output, set_led_output);
444 +
445 +static struct attribute *pca_sysfs_entries[] = {
446 + &dev_attr_group_dmblnk.attr,
447 + &dev_attr_led0_pwm.attr,
448 + &dev_attr_led1_pwm.attr,
449 + &dev_attr_led2_pwm.attr,
450 + &dev_attr_led3_pwm.attr,
451 + &dev_attr_group_dc.attr,
452 + &dev_attr_group_freq.attr,
453 + &dev_attr_led0_output.attr,
454 + &dev_attr_led1_output.attr,
455 + &dev_attr_led2_output.attr,
456 + &dev_attr_led3_output.attr,
457 + NULL
458 +};
459 +
460 +static struct attribute_group pca_attr_group = {
461 + .name = NULL, /* put in device directory */
462 + .attrs = pca_sysfs_entries,
463 +};
464 +
465 +#ifdef CONFIG_PM
466 +static int pca9632_suspend(struct device *dev, pm_message_t state)
467 +{
468 + /* FIXME: Not implemented */
469 + return 0;
470 +}
471 +
472 +static int pca9632_resume(struct device *dev)
473 +{
474 + /* FIXME: Not implemented */
475 + return 0;
476 +}
477 +#else
478 +#define pca9632_suspend NULL
479 +#define pca9632_resume NULL
480 +#endif
481 +
482 +static struct i2c_driver pca9632_driver = {
483 + .driver = {
484 + .name = "pca9632",
485 + .suspend = pca9632_suspend,
486 + .resume = pca9632_resume,
487 + },
488 + .id = I2C_DRIVERID_PCA9632,
489 + .attach_adapter = pca9632_attach_adapter,
490 + .detach_client = pca9632_detach_client,
491 +};
492 +
493 +static int pca9632_detect(struct i2c_adapter *adapter, int address, int kind)
494 +{
495 + struct i2c_client *new_client;
496 + struct pca9632_data *pca;
497 + int err;
498 +
499 + pca = kzalloc(sizeof(struct pca9632_data), GFP_KERNEL);
500 + if (!pca)
501 + return -ENOMEM;
502 +
503 + mutex_init(&pca->lock);
504 +
505 + new_client = &pca->client;
506 + i2c_set_clientdata(new_client, pca);
507 + new_client->addr = address;
508 + new_client->adapter = adapter;
509 + new_client->driver = &pca9632_driver;
510 + new_client->flags = 0;
511 +
512 + strlcpy(new_client->name, "pca9632", I2C_NAME_SIZE);
513 +
514 + /* register with i2c core */
515 + err = i2c_attach_client(new_client);
516 + if (err)
517 + goto exit_kfree;
518 +
519 + err = sysfs_create_group(&new_client->dev.kobj, &pca_attr_group);
520 + if (err)
521 + goto exit_detach;
522 +
523 + /* software reset */
524 + if (!software_rst(adapter))
525 + dev_info(&pca->client.dev, "pca9632 sw-rst done\n");
526 +
527 + /* enter normal mode */
528 + reg_set_bit_mask(pca, PCA9632_REG_MODE1, 0x10, PCA9632_NORMAL);
529 +
530 + return 0;
531 +
532 +exit_detach:
533 + i2c_detach_client(new_client);
534 +exit_kfree:
535 + kfree(pca);
536 +
537 + return err;
538 +}
539 +
540 +static int pca9632_attach_adapter(struct i2c_adapter *adapter)
541 +{
542 + return i2c_probe(adapter, &addr_data, pca9632_detect);
543 +}
544 +
545 +static int pca9632_detach_client(struct i2c_client *client)
546 +{
547 + int err;
548 +
549 + sysfs_remove_group(&client->dev.kobj, &pca_attr_group);
550 + err = i2c_detach_client(client);
551 +
552 + if (err)
553 + return err;
554 +
555 + kfree(i2c_get_clientdata(client));
556 +
557 + return 0;
558 +}
559 +
560 +static int __init pca9632_plat_probe(struct platform_device *pdev)
561 +{
562 + /* FIXME: platform data should be attached here */
563 + pca9632_pdev = pdev;
564 +
565 + return 0;
566 +}
567 +
568 +static int pca9632_plat_remove(struct platform_device *pdev)
569 +{
570 + return 0;
571 +}
572 +
573 +static struct platform_driver pca9632_plat_driver = {
574 + .probe = pca9632_plat_probe,
575 + .remove = pca9632_plat_remove,
576 + .driver = {
577 + .owner = THIS_MODULE,
578 + .name = "pca9632",
579 + },
580 +};
581 +
582 +static int __init pca9632_init(void)
583 +{
584 + int rc;
585 +
586 + rc = platform_driver_register(&pca9632_plat_driver);
587 + if (!rc)
588 + i2c_add_driver(&pca9632_driver);
589 +
590 + return rc;
591 +}
592 +
593 +static void __exit pca9632_exit(void)
594 +{
595 + i2c_del_driver(&pca9632_driver);
596 +
597 + platform_driver_unregister(&pca9632_plat_driver);
598 +}
599 +
600 +MODULE_AUTHOR("Matt Hsu <matt_hsu@openmoko.org>");
601 +MODULE_DESCRIPTION("NXP PCA9632 driver");
602 +MODULE_LICENSE("GPL");
603 +
604 +module_init(pca9632_init);
605 +module_exit(pca9632_exit);
606 diff --git a/drivers/i2c/chips/pca9632.h b/drivers/i2c/chips/pca9632.h
607 new file mode 100644
608 index 0000000..be2892e
609 --- /dev/null
610 +++ b/drivers/i2c/chips/pca9632.h
611 @@ -0,0 +1,24 @@
612 +#ifndef _PCA9632_H
613 +#define _PCA9632_H
614 +
615 +
616 +enum pca9632_regs{
617 +
618 + PCA9632_REG_MODE1 = 0x00,
619 + PCA9632_REG_MODE2 = 0x01,
620 + PCA9632_REG_PWM0 = 0x02,
621 + PCA9632_REG_PWM1 = 0x03,
622 + PCA9632_REG_PWM2 = 0x04,
623 + PCA9632_REG_PWM3 = 0x05,
624 + PCA9632_REG_GRPPWM = 0x06,
625 + PCA9632_REG_GRPFREQ = 0x07,
626 + PCA9632_REG_LEDOUT = 0x08,
627 + PCA9632_REG_SUBADDR1 = 0x09,
628 + PCA9632_REG_SUBADDR2 = 0x0a,
629 + PCA9632_REG_SUBADDR3 = 0x0b,
630 + PCA9632_REG_ALLCALLADR1 = 0x0c,
631 +};
632 +
633 +#define PCA9632_DMBLNK_SHIFT 5
634 +
635 +#endif /* _PCA9632_H */
636 diff --git a/include/linux/i2c-id.h b/include/linux/i2c-id.h
637 index ee60156..d5a63fe 100644
638 --- a/include/linux/i2c-id.h
639 +++ b/include/linux/i2c-id.h
640 @@ -170,6 +170,7 @@
641 #define I2C_DRIVERID_PCF50606 1049
642 #define I2C_DRIVERID_TSL256X 1050
643 #define I2C_DRIVERID_PCF50633 1051
644 +#define I2C_DRIVERID_PCA9632 1052
645
646 /*
647 * ---- Adapter types ----------------------------------------------------
648 --
649 1.5.6.5
650
This page took 0.061315 seconds and 5 git commands to generate.