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>
46 struct i2c_client
*client
;
50 struct gpio_chip chip_out
;
54 struct gpio_chip chip_in
;
61 * Work structure to allow for setting values outside of any
62 * possible interrupt context
64 struct work_struct set_val_work
;
73 unsigned int int_reset_gpio_hi
;
74 unsigned int int_reset_gpio_lo
;
77 struct htcpld_chip
*chip
;
81 /* There does not appear to be a way to proactively mask interrupts
82 * on the htcpld chip itself. So, we simply ignore interrupts that
84 static void htcpld_mask(unsigned int irq
)
86 struct htcpld_chip
*chip
= get_irq_chip_data(irq
);
87 chip
->irqs_enabled
&= ~(1 << (irq
- chip
->irq_start
));
88 pr_debug("HTCPLD mask %d %04x\n", irq
, chip
->irqs_enabled
);
90 static void htcpld_unmask(unsigned int irq
)
92 struct htcpld_chip
*chip
= get_irq_chip_data(irq
);
93 chip
->irqs_enabled
|= 1 << (irq
- chip
->irq_start
);
94 pr_debug("HTCPLD unmask %d %04x\n", irq
, chip
->irqs_enabled
);
97 static int htcpld_set_type(unsigned int irq
, unsigned int flags
)
99 struct irq_desc
*d
= irq_to_desc(irq
);
102 pr_err("HTCPLD invalid IRQ: %d\n", irq
);
106 if (flags
& ~IRQ_TYPE_SENSE_MASK
)
109 /* We only allow edge triggering */
110 if (flags
& (IRQ_TYPE_LEVEL_LOW
|IRQ_TYPE_LEVEL_HIGH
))
113 d
->status
&= ~IRQ_TYPE_SENSE_MASK
;
119 static struct irq_chip htcpld_muxed_chip
= {
122 .unmask
= htcpld_unmask
,
123 .set_type
= htcpld_set_type
,
126 /* To properly dispatch IRQ events, we need to read from the
127 * chip. This is an I2C action that could possibly sleep
128 * (which is bad in interrupt context) -- so we use a threaded
129 * interrupt handler to get around that.
131 static irqreturn_t
htcpld_handler(int irq
, void *dev
)
133 struct htcpld_data
*htcpld
= dev
;
137 struct irq_desc
*desc
;
140 pr_debug("htcpld is null in ISR\n");
145 * For each chip, do a read of the chip and trigger any interrupts
146 * desired. The interrupts will be triggered from LSB to MSB (i.e.
147 * bit 0 first, then bit 1, etc.)
149 * For chips that have no interrupt range specified, just skip 'em.
151 for (i
= 0; i
< htcpld
->nchips
; i
++) {
152 struct htcpld_chip
*chip
= &htcpld
->chip
[i
];
153 struct i2c_client
*client
;
155 unsigned long uval
, old_val
;
158 pr_debug("chip %d is null in ISR\n", i
);
162 if (chip
->nirqs
== 0)
165 client
= chip
->client
;
167 pr_debug("client %d is null in ISR\n", i
);
172 val
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
174 /* Throw a warning and skip this chip */
175 dev_warn(chip
->dev
, "Unable to read from chip: %d\n",
180 uval
= (unsigned long)val
;
182 spin_lock_irqsave(&chip
->lock
, flags
);
184 /* Save away the old value so we can compare it */
185 old_val
= chip
->cache_in
;
187 /* Write the new value */
188 chip
->cache_in
= uval
;
190 spin_unlock_irqrestore(&chip
->lock
, flags
);
193 * For each bit in the data (starting at bit 0), trigger
194 * associated interrupts.
196 for (irqpin
= 0; irqpin
< chip
->nirqs
; irqpin
++) {
200 irq
= chip
->irq_start
+ irqpin
;
201 desc
= irq_to_desc(irq
);
202 flags
= desc
->status
;
204 /* Run the IRQ handler, but only if the bit value
205 * changed, and the proper flags are set */
206 oldb
= (old_val
>> irqpin
) & 1;
207 newb
= (uval
>> irqpin
) & 1;
209 if ((!oldb
&& newb
&& (flags
& IRQ_TYPE_EDGE_RISING
)) ||
211 (flags
& IRQ_TYPE_EDGE_FALLING
))) {
212 pr_debug("fire IRQ %d\n", irqpin
);
213 desc
->handle_irq(irq
, desc
);
219 * In order to continue receiving interrupts, the int_reset_gpio must
222 if (htcpld
->int_reset_gpio_hi
)
223 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
224 if (htcpld
->int_reset_gpio_lo
)
225 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
231 * The GPIO set routines can be called from interrupt context, especially if,
232 * for example they're attached to the led-gpio framework and a trigger is
233 * enabled. As such, we declared work above in the htcpld_chip structure,
234 * and that work is scheduled in the set routine. The kernel can then run
235 * the I2C functions, which will sleep, in process context.
237 void htcpld_chip_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
239 struct i2c_client
*client
;
240 struct htcpld_chip
*chip_data
;
243 chip_data
= container_of(chip
, struct htcpld_chip
, chip_out
);
247 client
= chip_data
->client
;
251 spin_lock_irqsave(&chip_data
->lock
, flags
);
253 chip_data
->cache_out
|= (1 << offset
);
255 chip_data
->cache_out
&= ~(1 << offset
);
256 spin_unlock_irqrestore(&chip_data
->lock
, flags
);
258 schedule_work(&(chip_data
->set_val_work
));
261 void htcpld_chip_set_ni(struct work_struct
*work
)
263 struct htcpld_chip
*chip_data
;
264 struct i2c_client
*client
;
266 chip_data
= container_of(work
, struct htcpld_chip
, set_val_work
);
267 client
= chip_data
->client
;
268 i2c_smbus_read_byte_data(client
, chip_data
->cache_out
);
271 int htcpld_chip_get(struct gpio_chip
*chip
, unsigned offset
)
273 struct htcpld_chip
*chip_data
;
278 chip_data
= container_of(chip
, struct htcpld_chip
, chip_out
);
282 chip_data
= container_of(chip
, struct htcpld_chip
, chip_in
);
287 /* Determine if this is an input or output GPIO */
289 /* Use the output cache */
290 val
= (chip_data
->cache_out
>> offset
) & 1;
292 /* Use the input cache */
293 val
= (chip_data
->cache_in
>> offset
) & 1;
301 static int htcpld_direction_output(struct gpio_chip
*chip
,
302 unsigned offset
, int value
)
304 htcpld_chip_set(chip
, offset
, value
);
308 static int htcpld_direction_input(struct gpio_chip
*chip
,
312 * No-op: this function can only be called on the input chip.
313 * We do however make sure the offset is within range.
315 return (offset
< chip
->ngpio
) ? 0 : -EINVAL
;
318 int htcpld_chip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
320 struct htcpld_chip
*chip_data
;
322 chip_data
= container_of(chip
, struct htcpld_chip
, chip_in
);
324 if (offset
< chip_data
->nirqs
)
325 return chip_data
->irq_start
+ offset
;
330 void htcpld_chip_reset(struct i2c_client
*client
)
332 struct htcpld_chip
*chip_data
= i2c_get_clientdata(client
);
336 i2c_smbus_read_byte_data(
337 client
, (chip_data
->cache_out
= chip_data
->reset
));
340 static int __devinit
htcpld_setup_chip_irq(
341 struct platform_device
*pdev
,
344 struct htcpld_data
*htcpld
;
345 struct device
*dev
= &pdev
->dev
;
346 struct htcpld_core_platform_data
*pdata
;
347 struct htcpld_chip
*chip
;
348 struct htcpld_chip_platform_data
*plat_chip_data
;
349 unsigned int irq
, irq_end
;
352 /* Get the platform and driver data */
353 pdata
= dev
->platform_data
;
354 htcpld
= platform_get_drvdata(pdev
);
355 chip
= &htcpld
->chip
[chip_index
];
356 plat_chip_data
= &pdata
->chip
[chip_index
];
358 /* Setup irq handlers */
359 irq_end
= chip
->irq_start
+ chip
->nirqs
;
360 for (irq
= chip
->irq_start
; irq
< irq_end
; irq
++) {
361 set_irq_chip(irq
, &htcpld_muxed_chip
);
362 set_irq_chip_data(irq
, chip
);
363 set_irq_handler(irq
, handle_simple_irq
);
365 set_irq_flags(irq
, IRQF_VALID
| IRQF_PROBE
);
374 static int __devinit
htcpld_register_chip_i2c(
375 struct platform_device
*pdev
,
378 struct htcpld_data
*htcpld
;
379 struct device
*dev
= &pdev
->dev
;
380 struct htcpld_core_platform_data
*pdata
;
381 struct htcpld_chip
*chip
;
382 struct htcpld_chip_platform_data
*plat_chip_data
;
383 struct i2c_adapter
*adapter
;
384 struct i2c_client
*client
;
385 struct i2c_board_info info
;
387 /* Get the platform and driver data */
388 pdata
= dev
->platform_data
;
389 htcpld
= platform_get_drvdata(pdev
);
390 chip
= &htcpld
->chip
[chip_index
];
391 plat_chip_data
= &pdata
->chip
[chip_index
];
393 adapter
= i2c_get_adapter(pdata
->i2c_adapter_id
);
394 if (adapter
== NULL
) {
395 /* Eek, no such I2C adapter! Bail out. */
396 dev_warn(dev
, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
397 plat_chip_data
->addr
, pdata
->i2c_adapter_id
);
401 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
)) {
402 dev_warn(dev
, "i2c adapter %d non-functional\n",
403 pdata
->i2c_adapter_id
);
407 memset(&info
, 0, sizeof(struct i2c_board_info
));
408 info
.addr
= plat_chip_data
->addr
;
409 strlcpy(info
.type
, "htcpld-chip", I2C_NAME_SIZE
);
410 info
.platform_data
= chip
;
412 /* Add the I2C device. This calls the probe() function. */
413 client
= i2c_new_device(adapter
, &info
);
415 /* I2C device registration failed, contineu with the next */
416 dev_warn(dev
, "Unable to add I2C device for 0x%x\n",
417 plat_chip_data
->addr
);
421 i2c_set_clientdata(client
, chip
);
422 snprintf(client
->name
, I2C_NAME_SIZE
, "Chip_0x%d", client
->addr
);
423 chip
->client
= client
;
426 htcpld_chip_reset(client
);
427 chip
->cache_in
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
432 static void __devinit
htcpld_unregister_chip_i2c(
433 struct platform_device
*pdev
,
436 struct htcpld_data
*htcpld
;
437 struct htcpld_chip
*chip
;
439 /* Get the platform and driver data */
440 htcpld
= platform_get_drvdata(pdev
);
441 chip
= &htcpld
->chip
[chip_index
];
444 i2c_unregister_device(chip
->client
);
447 static int __devinit
htcpld_register_chip_gpio(
448 struct platform_device
*pdev
,
451 struct htcpld_data
*htcpld
;
452 struct device
*dev
= &pdev
->dev
;
453 struct htcpld_core_platform_data
*pdata
;
454 struct htcpld_chip
*chip
;
455 struct htcpld_chip_platform_data
*plat_chip_data
;
456 struct gpio_chip
*gpio_chip
;
459 /* Get the platform and driver data */
460 pdata
= dev
->platform_data
;
461 htcpld
= platform_get_drvdata(pdev
);
462 chip
= &htcpld
->chip
[chip_index
];
463 plat_chip_data
= &pdata
->chip
[chip_index
];
465 /* Setup the GPIO chips */
466 gpio_chip
= &(chip
->chip_out
);
467 gpio_chip
->label
= "htcpld-out";
468 gpio_chip
->dev
= dev
;
469 gpio_chip
->owner
= THIS_MODULE
;
470 gpio_chip
->get
= htcpld_chip_get
;
471 gpio_chip
->set
= htcpld_chip_set
;
472 gpio_chip
->direction_input
= NULL
;
473 gpio_chip
->direction_output
= htcpld_direction_output
;
474 gpio_chip
->base
= plat_chip_data
->gpio_out_base
;
475 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
477 gpio_chip
= &(chip
->chip_in
);
478 gpio_chip
->label
= "htcpld-in";
479 gpio_chip
->dev
= dev
;
480 gpio_chip
->owner
= THIS_MODULE
;
481 gpio_chip
->get
= htcpld_chip_get
;
482 gpio_chip
->set
= NULL
;
483 gpio_chip
->direction_input
= htcpld_direction_input
;
484 gpio_chip
->direction_output
= NULL
;
485 gpio_chip
->to_irq
= htcpld_chip_to_irq
;
486 gpio_chip
->base
= plat_chip_data
->gpio_in_base
;
487 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
489 /* Add the GPIO chips */
490 ret
= gpiochip_add(&(chip
->chip_out
));
492 dev_warn(dev
, "Unable to register output GPIOs for 0x%x: %d\n",
493 plat_chip_data
->addr
, ret
);
497 ret
= gpiochip_add(&(chip
->chip_in
));
501 dev_warn(dev
, "Unable to register input GPIOs for 0x%x: %d\n",
502 plat_chip_data
->addr
, ret
);
504 error
= gpiochip_remove(&(chip
->chip_out
));
506 dev_warn(dev
, "Error while trying to unregister gpio chip: %d\n", error
);
514 static int __devinit
htcpld_setup_chips(struct platform_device
*pdev
)
516 struct htcpld_data
*htcpld
;
517 struct device
*dev
= &pdev
->dev
;
518 struct htcpld_core_platform_data
*pdata
;
521 /* Get the platform and driver data */
522 pdata
= dev
->platform_data
;
523 htcpld
= platform_get_drvdata(pdev
);
525 /* Setup each chip's output GPIOs */
526 htcpld
->nchips
= pdata
->num_chip
;
527 htcpld
->chip
= kzalloc(sizeof(struct htcpld_chip
) * htcpld
->nchips
,
530 dev_warn(dev
, "Unable to allocate memory for chips\n");
534 /* Add the chips as best we can */
535 for (i
= 0; i
< htcpld
->nchips
; i
++) {
538 /* Setup the HTCPLD chips */
539 htcpld
->chip
[i
].reset
= pdata
->chip
[i
].reset
;
540 htcpld
->chip
[i
].cache_out
= pdata
->chip
[i
].reset
;
541 htcpld
->chip
[i
].cache_in
= 0;
542 htcpld
->chip
[i
].dev
= dev
;
543 htcpld
->chip
[i
].irq_start
= pdata
->chip
[i
].irq_base
;
544 htcpld
->chip
[i
].nirqs
= pdata
->chip
[i
].num_irqs
;
546 INIT_WORK(&(htcpld
->chip
[i
].set_val_work
), &htcpld_chip_set_ni
);
547 spin_lock_init(&(htcpld
->chip
[i
].lock
));
549 /* Setup the interrupts for the chip */
550 if (htcpld
->chained_irq
) {
551 ret
= htcpld_setup_chip_irq(pdev
, i
);
556 /* Register the chip with I2C */
557 ret
= htcpld_register_chip_i2c(pdev
, i
);
562 /* Register the chips with the GPIO subsystem */
563 ret
= htcpld_register_chip_gpio(pdev
, i
);
565 /* Unregister the chip from i2c and continue */
566 htcpld_unregister_chip_i2c(pdev
, i
);
570 dev_info(dev
, "Registered chip at 0x%x\n", pdata
->chip
[i
].addr
);
576 static int __devinit
htcpld_core_probe(struct platform_device
*pdev
)
578 struct htcpld_data
*htcpld
;
579 struct device
*dev
= &pdev
->dev
;
580 struct htcpld_core_platform_data
*pdata
;
581 struct resource
*res
;
587 pdata
= dev
->platform_data
;
589 dev_warn(dev
, "Platform data not found for htcpld core!\n");
593 htcpld
= kzalloc(sizeof(struct htcpld_data
), GFP_KERNEL
);
597 /* Find chained irq */
599 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
602 htcpld
->chained_irq
= res
->start
;
604 /* Setup the chained interrupt handler */
605 flags
= IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
;
606 ret
= request_threaded_irq(htcpld
->chained_irq
,
607 NULL
, htcpld_handler
,
608 flags
, pdev
->name
, htcpld
);
610 dev_warn(dev
, "Unable to setup chained irq handler: %d\n", ret
);
613 device_init_wakeup(dev
, 0);
616 /* Set the driver data */
617 platform_set_drvdata(pdev
, htcpld
);
619 /* Setup the htcpld chips */
620 ret
= htcpld_setup_chips(pdev
);
624 /* Request the GPIO(s) for the int reset and set them up */
625 if (pdata
->int_reset_gpio_hi
) {
626 ret
= gpio_request(pdata
->int_reset_gpio_hi
, "htcpld-core");
629 * If it failed, that sucks, but we can probably
630 * continue on without it.
632 dev_warn(dev
, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
633 htcpld
->int_reset_gpio_hi
= 0;
635 htcpld
->int_reset_gpio_hi
= pdata
->int_reset_gpio_hi
;
636 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
640 if (pdata
->int_reset_gpio_lo
) {
641 ret
= gpio_request(pdata
->int_reset_gpio_lo
, "htcpld-core");
644 * If it failed, that sucks, but we can probably
645 * continue on without it.
647 dev_warn(dev
, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
648 htcpld
->int_reset_gpio_lo
= 0;
650 htcpld
->int_reset_gpio_lo
= pdata
->int_reset_gpio_lo
;
651 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
655 dev_info(dev
, "Initialized successfully\n");
663 /* The I2C Driver -- used internally */
664 static const struct i2c_device_id htcpld_chip_id
[] = {
665 { "htcpld-chip", 0 },
668 MODULE_DEVICE_TABLE(i2c
, htcpld_chip_id
);
671 static struct i2c_driver htcpld_chip_driver
= {
673 .name
= "htcpld-chip",
675 .id_table
= htcpld_chip_id
,
678 /* The Core Driver */
679 static struct platform_driver htcpld_core_driver
= {
681 .name
= "i2c-htcpld",
685 static int __init
htcpld_core_init(void)
689 /* Register the I2C Chip driver */
690 ret
= i2c_add_driver(&htcpld_chip_driver
);
694 /* Probe for our chips */
695 return platform_driver_probe(&htcpld_core_driver
, htcpld_core_probe
);
698 static void __exit
htcpld_core_exit(void)
700 i2c_del_driver(&htcpld_chip_driver
);
701 platform_driver_unregister(&htcpld_core_driver
);
704 module_init(htcpld_core_init
);
705 module_exit(htcpld_core_exit
);
707 MODULE_AUTHOR("Cory Maccarrone <darkstar6262@gmail.com>");
708 MODULE_DESCRIPTION("I2C HTC PLD Driver");
709 MODULE_LICENSE("GPL");