GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mfd / htc-i2cpld.c
blob3cf0191ef5557a288341bdd99bd314ace228b89a
1 /*
2 * htc-i2cpld.c
3 * Chip driver for an unknown CPLD chip found on omap850 HTC devices like
4 * the HTC Wizard and HTC Herald.
5 * The cpld is located on the i2c bus and acts as an input/output GPIO
6 * extender.
8 * Copyright (C) 2009 Cory Maccarrone <darkstar6262@gmail.com>
10 * Based on work done in the linwizard project
11 * Copyright (C) 2008-2009 Angelo Arrifano <miknix@gmail.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/interrupt.h>
32 #include <linux/platform_device.h>
33 #include <linux/i2c.h>
34 #include <linux/irq.h>
35 #include <linux/spinlock.h>
36 #include <linux/htcpld.h>
37 #include <linux/gpio.h>
38 #include <linux/slab.h>
40 struct htcpld_chip {
41 spinlock_t lock;
43 /* chip info */
44 u8 reset;
45 u8 addr;
46 struct device *dev;
47 struct i2c_client *client;
49 /* Output details */
50 u8 cache_out;
51 struct gpio_chip chip_out;
53 /* Input details */
54 u8 cache_in;
55 struct gpio_chip chip_in;
57 u16 irqs_enabled;
58 uint irq_start;
59 int nirqs;
62 * Work structure to allow for setting values outside of any
63 * possible interrupt context
65 struct work_struct set_val_work;
68 struct htcpld_data {
69 /* irq info */
70 u16 irqs_enabled;
71 uint irq_start;
72 int nirqs;
73 uint chained_irq;
74 unsigned int int_reset_gpio_hi;
75 unsigned int int_reset_gpio_lo;
77 /* htcpld info */
78 struct htcpld_chip *chip;
79 unsigned int nchips;
82 /* There does not appear to be a way to proactively mask interrupts
83 * on the htcpld chip itself. So, we simply ignore interrupts that
84 * aren't desired. */
85 static void htcpld_mask(unsigned int irq)
87 struct htcpld_chip *chip = get_irq_chip_data(irq);
88 chip->irqs_enabled &= ~(1 << (irq - chip->irq_start));
89 pr_debug("HTCPLD mask %d %04x\n", irq, chip->irqs_enabled);
91 static void htcpld_unmask(unsigned int irq)
93 struct htcpld_chip *chip = get_irq_chip_data(irq);
94 chip->irqs_enabled |= 1 << (irq - chip->irq_start);
95 pr_debug("HTCPLD unmask %d %04x\n", irq, chip->irqs_enabled);
98 static int htcpld_set_type(unsigned int irq, unsigned int flags)
100 struct irq_desc *d = irq_to_desc(irq);
102 if (!d) {
103 pr_err("HTCPLD invalid IRQ: %d\n", irq);
104 return -EINVAL;
107 if (flags & ~IRQ_TYPE_SENSE_MASK)
108 return -EINVAL;
110 /* We only allow edge triggering */
111 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
112 return -EINVAL;
114 d->status &= ~IRQ_TYPE_SENSE_MASK;
115 d->status |= flags;
117 return 0;
120 static struct irq_chip htcpld_muxed_chip = {
121 .name = "htcpld",
122 .mask = htcpld_mask,
123 .unmask = htcpld_unmask,
124 .set_type = htcpld_set_type,
127 /* To properly dispatch IRQ events, we need to read from the
128 * chip. This is an I2C action that could possibly sleep
129 * (which is bad in interrupt context) -- so we use a threaded
130 * interrupt handler to get around that.
132 static irqreturn_t htcpld_handler(int irq, void *dev)
134 struct htcpld_data *htcpld = dev;
135 unsigned int i;
136 unsigned long flags;
137 int irqpin;
138 struct irq_desc *desc;
140 if (!htcpld) {
141 pr_debug("htcpld is null in ISR\n");
142 return IRQ_HANDLED;
146 * For each chip, do a read of the chip and trigger any interrupts
147 * desired. The interrupts will be triggered from LSB to MSB (i.e.
148 * bit 0 first, then bit 1, etc.)
150 * For chips that have no interrupt range specified, just skip 'em.
152 for (i = 0; i < htcpld->nchips; i++) {
153 struct htcpld_chip *chip = &htcpld->chip[i];
154 struct i2c_client *client;
155 int val;
156 unsigned long uval, old_val;
158 if (!chip) {
159 pr_debug("chip %d is null in ISR\n", i);
160 continue;
163 if (chip->nirqs == 0)
164 continue;
166 client = chip->client;
167 if (!client) {
168 pr_debug("client %d is null in ISR\n", i);
169 continue;
172 /* Scan the chip */
173 val = i2c_smbus_read_byte_data(client, chip->cache_out);
174 if (val < 0) {
175 /* Throw a warning and skip this chip */
176 dev_warn(chip->dev, "Unable to read from chip: %d\n",
177 val);
178 continue;
181 uval = (unsigned long)val;
183 spin_lock_irqsave(&chip->lock, flags);
185 /* Save away the old value so we can compare it */
186 old_val = chip->cache_in;
188 /* Write the new value */
189 chip->cache_in = uval;
191 spin_unlock_irqrestore(&chip->lock, flags);
194 * For each bit in the data (starting at bit 0), trigger
195 * associated interrupts.
197 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) {
198 unsigned oldb, newb;
199 int flags;
201 irq = chip->irq_start + irqpin;
202 desc = irq_to_desc(irq);
203 flags = desc->status;
205 /* Run the IRQ handler, but only if the bit value
206 * changed, and the proper flags are set */
207 oldb = (old_val >> irqpin) & 1;
208 newb = (uval >> irqpin) & 1;
210 if ((!oldb && newb && (flags & IRQ_TYPE_EDGE_RISING)) ||
211 (oldb && !newb &&
212 (flags & IRQ_TYPE_EDGE_FALLING))) {
213 pr_debug("fire IRQ %d\n", irqpin);
214 desc->handle_irq(irq, desc);
220 * In order to continue receiving interrupts, the int_reset_gpio must
221 * be asserted.
223 if (htcpld->int_reset_gpio_hi)
224 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
225 if (htcpld->int_reset_gpio_lo)
226 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
228 return IRQ_HANDLED;
232 * The GPIO set routines can be called from interrupt context, especially if,
233 * for example they're attached to the led-gpio framework and a trigger is
234 * enabled. As such, we declared work above in the htcpld_chip structure,
235 * and that work is scheduled in the set routine. The kernel can then run
236 * the I2C functions, which will sleep, in process context.
238 void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
240 struct i2c_client *client;
241 struct htcpld_chip *chip_data;
242 unsigned long flags;
244 chip_data = container_of(chip, struct htcpld_chip, chip_out);
245 if (!chip_data)
246 return;
248 client = chip_data->client;
249 if (client == NULL)
250 return;
252 spin_lock_irqsave(&chip_data->lock, flags);
253 if (val)
254 chip_data->cache_out |= (1 << offset);
255 else
256 chip_data->cache_out &= ~(1 << offset);
257 spin_unlock_irqrestore(&chip_data->lock, flags);
259 schedule_work(&(chip_data->set_val_work));
262 void htcpld_chip_set_ni(struct work_struct *work)
264 struct htcpld_chip *chip_data;
265 struct i2c_client *client;
267 chip_data = container_of(work, struct htcpld_chip, set_val_work);
268 client = chip_data->client;
269 i2c_smbus_read_byte_data(client, chip_data->cache_out);
272 int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
274 struct htcpld_chip *chip_data;
275 int val = 0;
276 int is_input = 0;
278 /* Try out first */
279 chip_data = container_of(chip, struct htcpld_chip, chip_out);
280 if (!chip_data) {
281 /* Try in */
282 is_input = 1;
283 chip_data = container_of(chip, struct htcpld_chip, chip_in);
284 if (!chip_data)
285 return -EINVAL;
288 /* Determine if this is an input or output GPIO */
289 if (!is_input)
290 /* Use the output cache */
291 val = (chip_data->cache_out >> offset) & 1;
292 else
293 /* Use the input cache */
294 val = (chip_data->cache_in >> offset) & 1;
296 if (val)
297 return 1;
298 else
299 return 0;
302 static int htcpld_direction_output(struct gpio_chip *chip,
303 unsigned offset, int value)
305 htcpld_chip_set(chip, offset, value);
306 return 0;
309 static int htcpld_direction_input(struct gpio_chip *chip,
310 unsigned offset)
313 * No-op: this function can only be called on the input chip.
314 * We do however make sure the offset is within range.
316 return (offset < chip->ngpio) ? 0 : -EINVAL;
319 int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
321 struct htcpld_chip *chip_data;
323 chip_data = container_of(chip, struct htcpld_chip, chip_in);
325 if (offset < chip_data->nirqs)
326 return chip_data->irq_start + offset;
327 else
328 return -EINVAL;
331 void htcpld_chip_reset(struct i2c_client *client)
333 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
334 if (!chip_data)
335 return;
337 i2c_smbus_read_byte_data(
338 client, (chip_data->cache_out = chip_data->reset));
341 static int __devinit htcpld_setup_chip_irq(
342 struct platform_device *pdev,
343 int chip_index)
345 struct htcpld_data *htcpld;
346 struct device *dev = &pdev->dev;
347 struct htcpld_core_platform_data *pdata;
348 struct htcpld_chip *chip;
349 struct htcpld_chip_platform_data *plat_chip_data;
350 unsigned int irq, irq_end;
351 int ret = 0;
353 /* Get the platform and driver data */
354 pdata = dev->platform_data;
355 htcpld = platform_get_drvdata(pdev);
356 chip = &htcpld->chip[chip_index];
357 plat_chip_data = &pdata->chip[chip_index];
359 /* Setup irq handlers */
360 irq_end = chip->irq_start + chip->nirqs;
361 for (irq = chip->irq_start; irq < irq_end; irq++) {
362 set_irq_chip(irq, &htcpld_muxed_chip);
363 set_irq_chip_data(irq, chip);
364 set_irq_handler(irq, handle_simple_irq);
365 #ifdef CONFIG_ARM
366 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
367 #else
368 set_irq_probe(irq);
369 #endif
372 return ret;
375 static int __devinit htcpld_register_chip_i2c(
376 struct platform_device *pdev,
377 int chip_index)
379 struct htcpld_data *htcpld;
380 struct device *dev = &pdev->dev;
381 struct htcpld_core_platform_data *pdata;
382 struct htcpld_chip *chip;
383 struct htcpld_chip_platform_data *plat_chip_data;
384 struct i2c_adapter *adapter;
385 struct i2c_client *client;
386 struct i2c_board_info info;
388 /* Get the platform and driver data */
389 pdata = dev->platform_data;
390 htcpld = platform_get_drvdata(pdev);
391 chip = &htcpld->chip[chip_index];
392 plat_chip_data = &pdata->chip[chip_index];
394 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
395 if (adapter == NULL) {
396 /* Eek, no such I2C adapter! Bail out. */
397 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
398 plat_chip_data->addr, pdata->i2c_adapter_id);
399 return -ENODEV;
402 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
403 dev_warn(dev, "i2c adapter %d non-functional\n",
404 pdata->i2c_adapter_id);
405 return -EINVAL;
408 memset(&info, 0, sizeof(struct i2c_board_info));
409 info.addr = plat_chip_data->addr;
410 strlcpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
411 info.platform_data = chip;
413 /* Add the I2C device. This calls the probe() function. */
414 client = i2c_new_device(adapter, &info);
415 if (!client) {
416 /* I2C device registration failed, contineu with the next */
417 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
418 plat_chip_data->addr);
419 return -ENODEV;
422 i2c_set_clientdata(client, chip);
423 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%d", client->addr);
424 chip->client = client;
426 /* Reset the chip */
427 htcpld_chip_reset(client);
428 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
430 return 0;
433 static void __devinit htcpld_unregister_chip_i2c(
434 struct platform_device *pdev,
435 int chip_index)
437 struct htcpld_data *htcpld;
438 struct htcpld_chip *chip;
440 /* Get the platform and driver data */
441 htcpld = platform_get_drvdata(pdev);
442 chip = &htcpld->chip[chip_index];
444 if (chip->client)
445 i2c_unregister_device(chip->client);
448 static int __devinit htcpld_register_chip_gpio(
449 struct platform_device *pdev,
450 int chip_index)
452 struct htcpld_data *htcpld;
453 struct device *dev = &pdev->dev;
454 struct htcpld_core_platform_data *pdata;
455 struct htcpld_chip *chip;
456 struct htcpld_chip_platform_data *plat_chip_data;
457 struct gpio_chip *gpio_chip;
458 int ret = 0;
460 /* Get the platform and driver data */
461 pdata = dev->platform_data;
462 htcpld = platform_get_drvdata(pdev);
463 chip = &htcpld->chip[chip_index];
464 plat_chip_data = &pdata->chip[chip_index];
466 /* Setup the GPIO chips */
467 gpio_chip = &(chip->chip_out);
468 gpio_chip->label = "htcpld-out";
469 gpio_chip->dev = dev;
470 gpio_chip->owner = THIS_MODULE;
471 gpio_chip->get = htcpld_chip_get;
472 gpio_chip->set = htcpld_chip_set;
473 gpio_chip->direction_input = NULL;
474 gpio_chip->direction_output = htcpld_direction_output;
475 gpio_chip->base = plat_chip_data->gpio_out_base;
476 gpio_chip->ngpio = plat_chip_data->num_gpios;
478 gpio_chip = &(chip->chip_in);
479 gpio_chip->label = "htcpld-in";
480 gpio_chip->dev = dev;
481 gpio_chip->owner = THIS_MODULE;
482 gpio_chip->get = htcpld_chip_get;
483 gpio_chip->set = NULL;
484 gpio_chip->direction_input = htcpld_direction_input;
485 gpio_chip->direction_output = NULL;
486 gpio_chip->to_irq = htcpld_chip_to_irq;
487 gpio_chip->base = plat_chip_data->gpio_in_base;
488 gpio_chip->ngpio = plat_chip_data->num_gpios;
490 /* Add the GPIO chips */
491 ret = gpiochip_add(&(chip->chip_out));
492 if (ret) {
493 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
494 plat_chip_data->addr, ret);
495 return ret;
498 ret = gpiochip_add(&(chip->chip_in));
499 if (ret) {
500 int error;
502 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
503 plat_chip_data->addr, ret);
505 error = gpiochip_remove(&(chip->chip_out));
506 if (error)
507 dev_warn(dev, "Error while trying to unregister gpio chip: %d\n", error);
509 return ret;
512 return 0;
515 static int __devinit htcpld_setup_chips(struct platform_device *pdev)
517 struct htcpld_data *htcpld;
518 struct device *dev = &pdev->dev;
519 struct htcpld_core_platform_data *pdata;
520 int i;
522 /* Get the platform and driver data */
523 pdata = dev->platform_data;
524 htcpld = platform_get_drvdata(pdev);
526 /* Setup each chip's output GPIOs */
527 htcpld->nchips = pdata->num_chip;
528 htcpld->chip = kzalloc(sizeof(struct htcpld_chip) * htcpld->nchips,
529 GFP_KERNEL);
530 if (!htcpld->chip) {
531 dev_warn(dev, "Unable to allocate memory for chips\n");
532 return -ENOMEM;
535 /* Add the chips as best we can */
536 for (i = 0; i < htcpld->nchips; i++) {
537 int ret;
539 /* Setup the HTCPLD chips */
540 htcpld->chip[i].reset = pdata->chip[i].reset;
541 htcpld->chip[i].cache_out = pdata->chip[i].reset;
542 htcpld->chip[i].cache_in = 0;
543 htcpld->chip[i].dev = dev;
544 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
545 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
547 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
548 spin_lock_init(&(htcpld->chip[i].lock));
550 /* Setup the interrupts for the chip */
551 if (htcpld->chained_irq) {
552 ret = htcpld_setup_chip_irq(pdev, i);
553 if (ret)
554 continue;
557 /* Register the chip with I2C */
558 ret = htcpld_register_chip_i2c(pdev, i);
559 if (ret)
560 continue;
563 /* Register the chips with the GPIO subsystem */
564 ret = htcpld_register_chip_gpio(pdev, i);
565 if (ret) {
566 /* Unregister the chip from i2c and continue */
567 htcpld_unregister_chip_i2c(pdev, i);
568 continue;
571 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
574 return 0;
577 static int __devinit htcpld_core_probe(struct platform_device *pdev)
579 struct htcpld_data *htcpld;
580 struct device *dev = &pdev->dev;
581 struct htcpld_core_platform_data *pdata;
582 struct resource *res;
583 int ret = 0;
585 if (!dev)
586 return -ENODEV;
588 pdata = dev->platform_data;
589 if (!pdata) {
590 dev_warn(dev, "Platform data not found for htcpld core!\n");
591 return -ENXIO;
594 htcpld = kzalloc(sizeof(struct htcpld_data), GFP_KERNEL);
595 if (!htcpld)
596 return -ENOMEM;
598 /* Find chained irq */
599 ret = -EINVAL;
600 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
601 if (res) {
602 int flags;
603 htcpld->chained_irq = res->start;
605 /* Setup the chained interrupt handler */
606 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING;
607 ret = request_threaded_irq(htcpld->chained_irq,
608 NULL, htcpld_handler,
609 flags, pdev->name, htcpld);
610 if (ret) {
611 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
612 goto fail;
613 } else
614 device_init_wakeup(dev, 0);
617 /* Set the driver data */
618 platform_set_drvdata(pdev, htcpld);
620 /* Setup the htcpld chips */
621 ret = htcpld_setup_chips(pdev);
622 if (ret)
623 goto fail;
625 /* Request the GPIO(s) for the int reset and set them up */
626 if (pdata->int_reset_gpio_hi) {
627 ret = gpio_request(pdata->int_reset_gpio_hi, "htcpld-core");
628 if (ret) {
630 * If it failed, that sucks, but we can probably
631 * continue on without it.
633 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
634 htcpld->int_reset_gpio_hi = 0;
635 } else {
636 htcpld->int_reset_gpio_hi = pdata->int_reset_gpio_hi;
637 gpio_set_value(htcpld->int_reset_gpio_hi, 1);
641 if (pdata->int_reset_gpio_lo) {
642 ret = gpio_request(pdata->int_reset_gpio_lo, "htcpld-core");
643 if (ret) {
645 * If it failed, that sucks, but we can probably
646 * continue on without it.
648 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
649 htcpld->int_reset_gpio_lo = 0;
650 } else {
651 htcpld->int_reset_gpio_lo = pdata->int_reset_gpio_lo;
652 gpio_set_value(htcpld->int_reset_gpio_lo, 0);
656 dev_info(dev, "Initialized successfully\n");
657 return 0;
659 fail:
660 kfree(htcpld);
661 return ret;
664 /* The I2C Driver -- used internally */
665 static const struct i2c_device_id htcpld_chip_id[] = {
666 { "htcpld-chip", 0 },
669 MODULE_DEVICE_TABLE(i2c, htcpld_chip_id);
672 static struct i2c_driver htcpld_chip_driver = {
673 .driver = {
674 .name = "htcpld-chip",
676 .id_table = htcpld_chip_id,
679 /* The Core Driver */
680 static struct platform_driver htcpld_core_driver = {
681 .driver = {
682 .name = "i2c-htcpld",
686 static int __init htcpld_core_init(void)
688 int ret;
690 /* Register the I2C Chip driver */
691 ret = i2c_add_driver(&htcpld_chip_driver);
692 if (ret)
693 return ret;
695 /* Probe for our chips */
696 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
699 static void __exit htcpld_core_exit(void)
701 i2c_del_driver(&htcpld_chip_driver);
702 platform_driver_unregister(&htcpld_core_driver);
705 module_init(htcpld_core_init);
706 module_exit(htcpld_core_exit);
708 MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>");
709 MODULE_DESCRIPTION("I2C HTC PLD Driver");
710 MODULE_LICENSE("GPL");