28 #include <linux/kernel.h>
30 #include <linux/module.h>
33 #include <linux/i2c.h>
38 #include <linux/slab.h>
114 static struct irq_chip htcpld_muxed_chip = {
116 .irq_mask = htcpld_mask,
117 .irq_unmask = htcpld_unmask,
118 .irq_set_type = htcpld_set_type,
134 pr_debug(
"htcpld is null in ISR\n");
145 for (i = 0; i < htcpld->
nchips; i++) {
149 unsigned long uval, old_val;
152 pr_debug(
"chip %d is null in ISR\n", i);
156 if (chip->
nirqs == 0)
161 pr_debug(
"client %d is null in ISR\n", i);
169 dev_warn(chip->
dev,
"Unable to read from chip: %d\n",
174 uval = (
unsigned long)val;
184 spin_unlock_irqrestore(&chip->
lock, flags);
190 for (irqpin = 0; irqpin < chip->
nirqs; irqpin++) {
197 oldb = (old_val >> irqpin) & 1;
198 newb = (uval >> irqpin) & 1;
227 static void htcpld_chip_set(
struct gpio_chip *chip,
unsigned offset,
int val)
237 client = chip_data->
client;
246 spin_unlock_irqrestore(&chip_data->
lock, flags);
257 client = chip_data->
client;
261 static int htcpld_chip_get(
struct gpio_chip *chip,
unsigned offset)
291 static int htcpld_direction_output(
struct gpio_chip *chip,
292 unsigned offset,
int value)
294 htcpld_chip_set(chip, offset, value);
298 static int htcpld_direction_input(
struct gpio_chip *chip,
308 static int htcpld_chip_to_irq(
struct gpio_chip *chip,
unsigned offset)
320 static void htcpld_chip_reset(
struct i2c_client *client)
322 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
330 static int __devinit htcpld_setup_chip_irq(
339 unsigned int irq, irq_end;
344 htcpld = platform_get_drvdata(pdev);
345 chip = &htcpld->
chip[chip_index];
346 plat_chip_data = &pdata->
chip[chip_index];
350 for (irq = chip->
irq_start; irq < irq_end; irq++) {
351 irq_set_chip_and_handler(irq, &htcpld_muxed_chip,
364 static int __devinit htcpld_register_chip_i2c(
379 htcpld = platform_get_drvdata(pdev);
380 chip = &htcpld->
chip[chip_index];
381 plat_chip_data = &pdata->chip[chip_index];
384 if (adapter ==
NULL) {
386 dev_warn(dev,
"Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
387 plat_chip_data->
addr, pdata->i2c_adapter_id);
392 dev_warn(dev,
"i2c adapter %d non-functional\n",
393 pdata->i2c_adapter_id);
406 dev_warn(dev,
"Unable to add I2C device for 0x%x\n",
407 plat_chip_data->
addr);
411 i2c_set_clientdata(client, chip);
416 htcpld_chip_reset(client);
422 static void __devinit htcpld_unregister_chip_i2c(
430 htcpld = platform_get_drvdata(pdev);
431 chip = &htcpld->
chip[chip_index];
437 static int __devinit htcpld_register_chip_gpio(
446 struct gpio_chip *gpio_chip;
451 htcpld = platform_get_drvdata(pdev);
452 chip = &htcpld->
chip[chip_index];
453 plat_chip_data = &pdata->
chip[chip_index];
457 gpio_chip->label =
"htcpld-out";
458 gpio_chip->dev =
dev;
460 gpio_chip->get = htcpld_chip_get;
461 gpio_chip->set = htcpld_chip_set;
462 gpio_chip->direction_input =
NULL;
463 gpio_chip->direction_output = htcpld_direction_output;
465 gpio_chip->ngpio = plat_chip_data->
num_gpios;
468 gpio_chip->label =
"htcpld-in";
469 gpio_chip->dev =
dev;
471 gpio_chip->get = htcpld_chip_get;
472 gpio_chip->set =
NULL;
473 gpio_chip->direction_input = htcpld_direction_input;
474 gpio_chip->direction_output =
NULL;
475 gpio_chip->to_irq = htcpld_chip_to_irq;
477 gpio_chip->ngpio = plat_chip_data->
num_gpios;
482 dev_warn(dev,
"Unable to register output GPIOs for 0x%x: %d\n",
483 plat_chip_data->
addr, ret);
491 dev_warn(dev,
"Unable to register input GPIOs for 0x%x: %d\n",
492 plat_chip_data->
addr, ret);
496 dev_warn(dev,
"Error while trying to unregister gpio chip: %d\n", error);
513 htcpld = platform_get_drvdata(pdev);
520 dev_warn(dev,
"Unable to allocate memory for chips\n");
525 for (i = 0; i < htcpld->
nchips; i++) {
530 htcpld->
chip[
i].cache_out = pdata->
chip[
i].reset;
531 htcpld->
chip[
i].cache_in = 0;
533 htcpld->
chip[
i].irq_start = pdata->
chip[
i].irq_base;
534 htcpld->
chip[
i].nirqs = pdata->
chip[
i].num_irqs;
536 INIT_WORK(&(htcpld->
chip[i].set_val_work), &htcpld_chip_set_ni);
541 ret = htcpld_setup_chip_irq(pdev, i);
547 ret = htcpld_register_chip_i2c(pdev, i);
553 ret = htcpld_register_chip_gpio(pdev, i);
556 htcpld_unregister_chip_i2c(pdev, i);
560 dev_info(dev,
"Registered chip at 0x%x\n", pdata->
chip[i].addr);
579 dev_warn(dev,
"Platform data not found for htcpld core!\n");
597 NULL, htcpld_handler,
598 flags, pdev->
name, htcpld);
600 dev_warn(dev,
"Unable to setup chained irq handler: %d\n", ret);
607 platform_set_drvdata(pdev, htcpld);
610 ret = htcpld_setup_chips(pdev);
622 dev_warn(dev,
"Unable to request int_reset_gpio_hi -- interrupts may not work\n");
637 dev_warn(dev,
"Unable to request int_reset_gpio_lo -- interrupts may not work\n");
645 dev_info(dev,
"Initialized successfully\n");
655 {
"htcpld-chip", 0 },
661 static struct i2c_driver htcpld_chip_driver = {
663 .name =
"htcpld-chip",
665 .id_table = htcpld_chip_id,
671 .name =
"i2c-htcpld",
675 static int __init htcpld_core_init(
void)
680 ret = i2c_add_driver(&htcpld_chip_driver);
688 static void __exit htcpld_core_exit(
void)