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/interrupt.h>
31 #include <linux/platform_device.h>
32 #include <linux/i2c.h>
33 #include <linux/irq.h>
34 #include <linux/spinlock.h>
35 #include <linux/htcpld.h>
36 #include <linux/gpio.h>
37 #include <linux/slab.h>
46 struct i2c_client
*client
;
50 struct gpio_chip chip_out
;
54 struct gpio_chip chip_in
;
60 unsigned int flow_type
;
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(struct irq_data
*data
)
87 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
88 chip
->irqs_enabled
&= ~(1 << (data
->irq
- chip
->irq_start
));
89 pr_debug("HTCPLD mask %d %04x\n", data
->irq
, chip
->irqs_enabled
);
91 static void htcpld_unmask(struct irq_data
*data
)
93 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
94 chip
->irqs_enabled
|= 1 << (data
->irq
- chip
->irq_start
);
95 pr_debug("HTCPLD unmask %d %04x\n", data
->irq
, chip
->irqs_enabled
);
98 static int htcpld_set_type(struct irq_data
*data
, unsigned int flags
)
100 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
102 if (flags
& ~IRQ_TYPE_SENSE_MASK
)
105 /* We only allow edge triggering */
106 if (flags
& (IRQ_TYPE_LEVEL_LOW
|IRQ_TYPE_LEVEL_HIGH
))
109 chip
->flow_type
= flags
;
113 static struct irq_chip htcpld_muxed_chip
= {
115 .irq_mask
= htcpld_mask
,
116 .irq_unmask
= htcpld_unmask
,
117 .irq_set_type
= htcpld_set_type
,
120 /* To properly dispatch IRQ events, we need to read from the
121 * chip. This is an I2C action that could possibly sleep
122 * (which is bad in interrupt context) -- so we use a threaded
123 * interrupt handler to get around that.
125 static irqreturn_t
htcpld_handler(int irq
, void *dev
)
127 struct htcpld_data
*htcpld
= dev
;
133 pr_debug("htcpld is null in ISR\n");
138 * For each chip, do a read of the chip and trigger any interrupts
139 * desired. The interrupts will be triggered from LSB to MSB (i.e.
140 * bit 0 first, then bit 1, etc.)
142 * For chips that have no interrupt range specified, just skip 'em.
144 for (i
= 0; i
< htcpld
->nchips
; i
++) {
145 struct htcpld_chip
*chip
= &htcpld
->chip
[i
];
146 struct i2c_client
*client
;
148 unsigned long uval
, old_val
;
151 pr_debug("chip %d is null in ISR\n", i
);
155 if (chip
->nirqs
== 0)
158 client
= chip
->client
;
160 pr_debug("client %d is null in ISR\n", i
);
165 val
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
167 /* Throw a warning and skip this chip */
168 dev_warn(chip
->dev
, "Unable to read from chip: %d\n",
173 uval
= (unsigned long)val
;
175 spin_lock_irqsave(&chip
->lock
, flags
);
177 /* Save away the old value so we can compare it */
178 old_val
= chip
->cache_in
;
180 /* Write the new value */
181 chip
->cache_in
= uval
;
183 spin_unlock_irqrestore(&chip
->lock
, flags
);
186 * For each bit in the data (starting at bit 0), trigger
187 * associated interrupts.
189 for (irqpin
= 0; irqpin
< chip
->nirqs
; irqpin
++) {
190 unsigned oldb
, newb
, type
= chip
->flow_type
;
192 irq
= chip
->irq_start
+ irqpin
;
194 /* Run the IRQ handler, but only if the bit value
195 * changed, and the proper flags are set */
196 oldb
= (old_val
>> irqpin
) & 1;
197 newb
= (uval
>> irqpin
) & 1;
199 if ((!oldb
&& newb
&& (type
& IRQ_TYPE_EDGE_RISING
)) ||
200 (oldb
&& !newb
&& (type
& IRQ_TYPE_EDGE_FALLING
))) {
201 pr_debug("fire IRQ %d\n", irqpin
);
202 generic_handle_irq(irq
);
208 * In order to continue receiving interrupts, the int_reset_gpio must
211 if (htcpld
->int_reset_gpio_hi
)
212 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
213 if (htcpld
->int_reset_gpio_lo
)
214 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
220 * The GPIO set routines can be called from interrupt context, especially if,
221 * for example they're attached to the led-gpio framework and a trigger is
222 * enabled. As such, we declared work above in the htcpld_chip structure,
223 * and that work is scheduled in the set routine. The kernel can then run
224 * the I2C functions, which will sleep, in process context.
226 static void htcpld_chip_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
228 struct i2c_client
*client
;
229 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
232 client
= chip_data
->client
;
236 spin_lock_irqsave(&chip_data
->lock
, flags
);
238 chip_data
->cache_out
|= (1 << offset
);
240 chip_data
->cache_out
&= ~(1 << offset
);
241 spin_unlock_irqrestore(&chip_data
->lock
, flags
);
243 schedule_work(&(chip_data
->set_val_work
));
246 static void htcpld_chip_set_ni(struct work_struct
*work
)
248 struct htcpld_chip
*chip_data
;
249 struct i2c_client
*client
;
251 chip_data
= container_of(work
, struct htcpld_chip
, set_val_work
);
252 client
= chip_data
->client
;
253 i2c_smbus_read_byte_data(client
, chip_data
->cache_out
);
256 static int htcpld_chip_get(struct gpio_chip
*chip
, unsigned offset
)
258 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
261 if (!strncmp(chip
->label
, "htcpld-out", 10)) {
262 cache
= chip_data
->cache_out
;
263 } else if (!strncmp(chip
->label
, "htcpld-in", 9)) {
264 cache
= chip_data
->cache_in
;
268 return (cache
>> offset
) & 1;
271 static int htcpld_direction_output(struct gpio_chip
*chip
,
272 unsigned offset
, int value
)
274 htcpld_chip_set(chip
, offset
, value
);
278 static int htcpld_direction_input(struct gpio_chip
*chip
,
282 * No-op: this function can only be called on the input chip.
283 * We do however make sure the offset is within range.
285 return (offset
< chip
->ngpio
) ? 0 : -EINVAL
;
288 static int htcpld_chip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
290 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
292 if (offset
< chip_data
->nirqs
)
293 return chip_data
->irq_start
+ offset
;
298 static void htcpld_chip_reset(struct i2c_client
*client
)
300 struct htcpld_chip
*chip_data
= i2c_get_clientdata(client
);
304 i2c_smbus_read_byte_data(
305 client
, (chip_data
->cache_out
= chip_data
->reset
));
308 static int htcpld_setup_chip_irq(
309 struct platform_device
*pdev
,
312 struct htcpld_data
*htcpld
;
313 struct htcpld_chip
*chip
;
314 unsigned int irq
, irq_end
;
316 /* Get the platform and driver data */
317 htcpld
= platform_get_drvdata(pdev
);
318 chip
= &htcpld
->chip
[chip_index
];
320 /* Setup irq handlers */
321 irq_end
= chip
->irq_start
+ chip
->nirqs
;
322 for (irq
= chip
->irq_start
; irq
< irq_end
; irq
++) {
323 irq_set_chip_and_handler(irq
, &htcpld_muxed_chip
,
325 irq_set_chip_data(irq
, chip
);
326 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
332 static int htcpld_register_chip_i2c(
333 struct platform_device
*pdev
,
336 struct htcpld_data
*htcpld
;
337 struct device
*dev
= &pdev
->dev
;
338 struct htcpld_core_platform_data
*pdata
;
339 struct htcpld_chip
*chip
;
340 struct htcpld_chip_platform_data
*plat_chip_data
;
341 struct i2c_adapter
*adapter
;
342 struct i2c_client
*client
;
343 struct i2c_board_info info
;
345 /* Get the platform and driver data */
346 pdata
= dev_get_platdata(dev
);
347 htcpld
= platform_get_drvdata(pdev
);
348 chip
= &htcpld
->chip
[chip_index
];
349 plat_chip_data
= &pdata
->chip
[chip_index
];
351 adapter
= i2c_get_adapter(pdata
->i2c_adapter_id
);
353 /* Eek, no such I2C adapter! Bail out. */
354 dev_warn(dev
, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
355 plat_chip_data
->addr
, pdata
->i2c_adapter_id
);
359 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
)) {
360 dev_warn(dev
, "i2c adapter %d non-functional\n",
361 pdata
->i2c_adapter_id
);
365 memset(&info
, 0, sizeof(struct i2c_board_info
));
366 info
.addr
= plat_chip_data
->addr
;
367 strlcpy(info
.type
, "htcpld-chip", I2C_NAME_SIZE
);
368 info
.platform_data
= chip
;
370 /* Add the I2C device. This calls the probe() function. */
371 client
= i2c_new_device(adapter
, &info
);
373 /* I2C device registration failed, contineu with the next */
374 dev_warn(dev
, "Unable to add I2C device for 0x%x\n",
375 plat_chip_data
->addr
);
379 i2c_set_clientdata(client
, chip
);
380 snprintf(client
->name
, I2C_NAME_SIZE
, "Chip_0x%x", client
->addr
);
381 chip
->client
= client
;
384 htcpld_chip_reset(client
);
385 chip
->cache_in
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
390 static void htcpld_unregister_chip_i2c(
391 struct platform_device
*pdev
,
394 struct htcpld_data
*htcpld
;
395 struct htcpld_chip
*chip
;
397 /* Get the platform and driver data */
398 htcpld
= platform_get_drvdata(pdev
);
399 chip
= &htcpld
->chip
[chip_index
];
402 i2c_unregister_device(chip
->client
);
405 static int htcpld_register_chip_gpio(
406 struct platform_device
*pdev
,
409 struct htcpld_data
*htcpld
;
410 struct device
*dev
= &pdev
->dev
;
411 struct htcpld_core_platform_data
*pdata
;
412 struct htcpld_chip
*chip
;
413 struct htcpld_chip_platform_data
*plat_chip_data
;
414 struct gpio_chip
*gpio_chip
;
417 /* Get the platform and driver data */
418 pdata
= dev_get_platdata(dev
);
419 htcpld
= platform_get_drvdata(pdev
);
420 chip
= &htcpld
->chip
[chip_index
];
421 plat_chip_data
= &pdata
->chip
[chip_index
];
423 /* Setup the GPIO chips */
424 gpio_chip
= &(chip
->chip_out
);
425 gpio_chip
->label
= "htcpld-out";
426 gpio_chip
->parent
= dev
;
427 gpio_chip
->owner
= THIS_MODULE
;
428 gpio_chip
->get
= htcpld_chip_get
;
429 gpio_chip
->set
= htcpld_chip_set
;
430 gpio_chip
->direction_input
= NULL
;
431 gpio_chip
->direction_output
= htcpld_direction_output
;
432 gpio_chip
->base
= plat_chip_data
->gpio_out_base
;
433 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
435 gpio_chip
= &(chip
->chip_in
);
436 gpio_chip
->label
= "htcpld-in";
437 gpio_chip
->parent
= dev
;
438 gpio_chip
->owner
= THIS_MODULE
;
439 gpio_chip
->get
= htcpld_chip_get
;
440 gpio_chip
->set
= NULL
;
441 gpio_chip
->direction_input
= htcpld_direction_input
;
442 gpio_chip
->direction_output
= NULL
;
443 gpio_chip
->to_irq
= htcpld_chip_to_irq
;
444 gpio_chip
->base
= plat_chip_data
->gpio_in_base
;
445 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
447 /* Add the GPIO chips */
448 ret
= gpiochip_add_data(&(chip
->chip_out
), chip
);
450 dev_warn(dev
, "Unable to register output GPIOs for 0x%x: %d\n",
451 plat_chip_data
->addr
, ret
);
455 ret
= gpiochip_add_data(&(chip
->chip_in
), chip
);
457 dev_warn(dev
, "Unable to register input GPIOs for 0x%x: %d\n",
458 plat_chip_data
->addr
, ret
);
459 gpiochip_remove(&(chip
->chip_out
));
466 static int htcpld_setup_chips(struct platform_device
*pdev
)
468 struct htcpld_data
*htcpld
;
469 struct device
*dev
= &pdev
->dev
;
470 struct htcpld_core_platform_data
*pdata
;
473 /* Get the platform and driver data */
474 pdata
= dev_get_platdata(dev
);
475 htcpld
= platform_get_drvdata(pdev
);
477 /* Setup each chip's output GPIOs */
478 htcpld
->nchips
= pdata
->num_chip
;
479 htcpld
->chip
= devm_kcalloc(dev
,
481 sizeof(struct htcpld_chip
),
486 /* Add the chips as best we can */
487 for (i
= 0; i
< htcpld
->nchips
; i
++) {
490 /* Setup the HTCPLD chips */
491 htcpld
->chip
[i
].reset
= pdata
->chip
[i
].reset
;
492 htcpld
->chip
[i
].cache_out
= pdata
->chip
[i
].reset
;
493 htcpld
->chip
[i
].cache_in
= 0;
494 htcpld
->chip
[i
].dev
= dev
;
495 htcpld
->chip
[i
].irq_start
= pdata
->chip
[i
].irq_base
;
496 htcpld
->chip
[i
].nirqs
= pdata
->chip
[i
].num_irqs
;
498 INIT_WORK(&(htcpld
->chip
[i
].set_val_work
), &htcpld_chip_set_ni
);
499 spin_lock_init(&(htcpld
->chip
[i
].lock
));
501 /* Setup the interrupts for the chip */
502 if (htcpld
->chained_irq
) {
503 ret
= htcpld_setup_chip_irq(pdev
, i
);
508 /* Register the chip with I2C */
509 ret
= htcpld_register_chip_i2c(pdev
, i
);
514 /* Register the chips with the GPIO subsystem */
515 ret
= htcpld_register_chip_gpio(pdev
, i
);
517 /* Unregister the chip from i2c and continue */
518 htcpld_unregister_chip_i2c(pdev
, i
);
522 dev_info(dev
, "Registered chip at 0x%x\n", pdata
->chip
[i
].addr
);
528 static int htcpld_core_probe(struct platform_device
*pdev
)
530 struct htcpld_data
*htcpld
;
531 struct device
*dev
= &pdev
->dev
;
532 struct htcpld_core_platform_data
*pdata
;
533 struct resource
*res
;
539 pdata
= dev_get_platdata(dev
);
541 dev_warn(dev
, "Platform data not found for htcpld core!\n");
545 htcpld
= devm_kzalloc(dev
, sizeof(struct htcpld_data
), GFP_KERNEL
);
549 /* Find chained irq */
550 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
553 htcpld
->chained_irq
= res
->start
;
555 /* Setup the chained interrupt handler */
556 flags
= IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
|
558 ret
= request_threaded_irq(htcpld
->chained_irq
,
559 NULL
, htcpld_handler
,
560 flags
, pdev
->name
, htcpld
);
562 dev_warn(dev
, "Unable to setup chained irq handler: %d\n", ret
);
565 device_init_wakeup(dev
, 0);
568 /* Set the driver data */
569 platform_set_drvdata(pdev
, htcpld
);
571 /* Setup the htcpld chips */
572 ret
= htcpld_setup_chips(pdev
);
576 /* Request the GPIO(s) for the int reset and set them up */
577 if (pdata
->int_reset_gpio_hi
) {
578 ret
= gpio_request(pdata
->int_reset_gpio_hi
, "htcpld-core");
581 * If it failed, that sucks, but we can probably
582 * continue on without it.
584 dev_warn(dev
, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
585 htcpld
->int_reset_gpio_hi
= 0;
587 htcpld
->int_reset_gpio_hi
= pdata
->int_reset_gpio_hi
;
588 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
592 if (pdata
->int_reset_gpio_lo
) {
593 ret
= gpio_request(pdata
->int_reset_gpio_lo
, "htcpld-core");
596 * If it failed, that sucks, but we can probably
597 * continue on without it.
599 dev_warn(dev
, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
600 htcpld
->int_reset_gpio_lo
= 0;
602 htcpld
->int_reset_gpio_lo
= pdata
->int_reset_gpio_lo
;
603 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
607 dev_info(dev
, "Initialized successfully\n");
611 /* The I2C Driver -- used internally */
612 static const struct i2c_device_id htcpld_chip_id
[] = {
613 { "htcpld-chip", 0 },
617 static struct i2c_driver htcpld_chip_driver
= {
619 .name
= "htcpld-chip",
621 .id_table
= htcpld_chip_id
,
624 /* The Core Driver */
625 static struct platform_driver htcpld_core_driver
= {
627 .name
= "i2c-htcpld",
631 static int __init
htcpld_core_init(void)
635 /* Register the I2C Chip driver */
636 ret
= i2c_add_driver(&htcpld_chip_driver
);
640 /* Probe for our chips */
641 return platform_driver_probe(&htcpld_core_driver
, htcpld_core_probe
);
643 device_initcall(htcpld_core_init
);