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
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>
47 struct i2c_client *client;
51 struct gpio_chip chip_out;
55 struct gpio_chip chip_in;
62 * Work structure to allow for setting values outside of any
63 * possible interrupt context
65 struct work_struct set_val_work;
74 unsigned int int_reset_gpio_hi;
75 unsigned int int_reset_gpio_lo;
78 struct htcpld_chip *chip;
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
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);
103 pr_err("HTCPLD invalid IRQ: %d\n", irq);
107 if (flags & ~IRQ_TYPE_SENSE_MASK)
110 /* We only allow edge triggering */
111 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
114 d->status &= ~IRQ_TYPE_SENSE_MASK;
120 static struct irq_chip htcpld_muxed_chip = {
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;
138 struct irq_desc *desc;
141 pr_debug("htcpld is null in ISR\n");
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;
156 unsigned long uval, old_val;
159 pr_debug("chip %d is null in ISR\n", i);
163 if (chip->nirqs == 0)
166 client = chip->client;
168 pr_debug("client %d is null in ISR\n", i);
173 val = i2c_smbus_read_byte_data(client, chip->cache_out);
175 /* Throw a warning and skip this chip */
176 dev_warn(chip->dev, "Unable to read from chip: %d\n",
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++) {
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)) ||
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
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);
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;
244 chip_data = container_of(chip, struct htcpld_chip, chip_out);
248 client = chip_data->client;
252 spin_lock_irqsave(&chip_data->lock, flags);
254 chip_data->cache_out |= (1 << offset);
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;
279 chip_data = container_of(chip, struct htcpld_chip, chip_out);
283 chip_data = container_of(chip, struct htcpld_chip, chip_in);
288 /* Determine if this is an input or output GPIO */
290 /* Use the output cache */
291 val = (chip_data->cache_out >> offset) & 1;
293 /* Use the input cache */
294 val = (chip_data->cache_in >> offset) & 1;
302 static int htcpld_direction_output(struct gpio_chip *chip,
303 unsigned offset, int value)
305 htcpld_chip_set(chip, offset, value);
309 static int htcpld_direction_input(struct gpio_chip *chip,
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;
331 void htcpld_chip_reset(struct i2c_client *client)
333 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
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,
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;
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);
366 set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
375 static int __devinit htcpld_register_chip_i2c(
376 struct platform_device *pdev,
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);
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);
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);
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);
422 i2c_set_clientdata(client, chip);
423 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%d", client->addr);
424 chip->client = client;
427 htcpld_chip_reset(client);
428 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
433 static void __devinit htcpld_unregister_chip_i2c(
434 struct platform_device *pdev,
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];
445 i2c_unregister_device(chip->client);
448 static int __devinit htcpld_register_chip_gpio(
449 struct platform_device *pdev,
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;
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));
493 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
494 plat_chip_data->addr, ret);
498 ret = gpiochip_add(&(chip->chip_in));
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));
507 dev_warn(dev, "Error while trying to unregister gpio chip: %d\n", error);
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;
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,
531 dev_warn(dev, "Unable to allocate memory for chips\n");
535 /* Add the chips as best we can */
536 for (i = 0; i < htcpld->nchips; i++) {
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);
557 /* Register the chip with I2C */
558 ret = htcpld_register_chip_i2c(pdev, i);
563 /* Register the chips with the GPIO subsystem */
564 ret = htcpld_register_chip_gpio(pdev, i);
566 /* Unregister the chip from i2c and continue */
567 htcpld_unregister_chip_i2c(pdev, i);
571 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
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;
588 pdata = dev->platform_data;
590 dev_warn(dev, "Platform data not found for htcpld core!\n");
594 htcpld = kzalloc(sizeof(struct htcpld_data), GFP_KERNEL);
598 /* Find chained irq */
600 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
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);
611 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
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);
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");
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;
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");
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;
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");
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 = {
674 .name = "htcpld-chip",
676 .id_table = htcpld_chip_id,
679 /* The Core Driver */
680 static struct platform_driver htcpld_core_driver = {
682 .name = "i2c-htcpld",
686 static int __init htcpld_core_init(void)
690 /* Register the I2C Chip driver */
691 ret = i2c_add_driver(&htcpld_chip_driver);
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");