1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Chip driver for an unknown CPLD chip found on omap850 HTC devices like
5 * the HTC Wizard and HTC Herald.
6 * The cpld is located on the i2c bus and acts as an input/output GPIO
9 * Copyright (C) 2009 Cory Maccarrone <darkstar6262@gmail.com>
11 * Based on work done in the linwizard project
12 * Copyright (C) 2008-2009 Angelo Arrifano <miknix@gmail.com>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <linux/i2c.h>
20 #include <linux/irq.h>
21 #include <linux/spinlock.h>
22 #include <linux/htcpld.h>
23 #include <linux/gpio.h>
24 #include <linux/slab.h>
33 struct i2c_client
*client
;
37 struct gpio_chip chip_out
;
41 struct gpio_chip chip_in
;
47 unsigned int flow_type
;
49 * Work structure to allow for setting values outside of any
50 * possible interrupt context
52 struct work_struct set_val_work
;
61 unsigned int int_reset_gpio_hi
;
62 unsigned int int_reset_gpio_lo
;
65 struct htcpld_chip
*chip
;
69 /* There does not appear to be a way to proactively mask interrupts
70 * on the htcpld chip itself. So, we simply ignore interrupts that
72 static void htcpld_mask(struct irq_data
*data
)
74 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
75 chip
->irqs_enabled
&= ~(1 << (data
->irq
- chip
->irq_start
));
76 pr_debug("HTCPLD mask %d %04x\n", data
->irq
, chip
->irqs_enabled
);
78 static void htcpld_unmask(struct irq_data
*data
)
80 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
81 chip
->irqs_enabled
|= 1 << (data
->irq
- chip
->irq_start
);
82 pr_debug("HTCPLD unmask %d %04x\n", data
->irq
, chip
->irqs_enabled
);
85 static int htcpld_set_type(struct irq_data
*data
, unsigned int flags
)
87 struct htcpld_chip
*chip
= irq_data_get_irq_chip_data(data
);
89 if (flags
& ~IRQ_TYPE_SENSE_MASK
)
92 /* We only allow edge triggering */
93 if (flags
& (IRQ_TYPE_LEVEL_LOW
|IRQ_TYPE_LEVEL_HIGH
))
96 chip
->flow_type
= flags
;
100 static struct irq_chip htcpld_muxed_chip
= {
102 .irq_mask
= htcpld_mask
,
103 .irq_unmask
= htcpld_unmask
,
104 .irq_set_type
= htcpld_set_type
,
107 /* To properly dispatch IRQ events, we need to read from the
108 * chip. This is an I2C action that could possibly sleep
109 * (which is bad in interrupt context) -- so we use a threaded
110 * interrupt handler to get around that.
112 static irqreturn_t
htcpld_handler(int irq
, void *dev
)
114 struct htcpld_data
*htcpld
= dev
;
120 pr_debug("htcpld is null in ISR\n");
125 * For each chip, do a read of the chip and trigger any interrupts
126 * desired. The interrupts will be triggered from LSB to MSB (i.e.
127 * bit 0 first, then bit 1, etc.)
129 * For chips that have no interrupt range specified, just skip 'em.
131 for (i
= 0; i
< htcpld
->nchips
; i
++) {
132 struct htcpld_chip
*chip
= &htcpld
->chip
[i
];
133 struct i2c_client
*client
;
135 unsigned long uval
, old_val
;
138 pr_debug("chip %d is null in ISR\n", i
);
142 if (chip
->nirqs
== 0)
145 client
= chip
->client
;
147 pr_debug("client %d is null in ISR\n", i
);
152 val
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
154 /* Throw a warning and skip this chip */
155 dev_warn(chip
->dev
, "Unable to read from chip: %d\n",
160 uval
= (unsigned long)val
;
162 spin_lock_irqsave(&chip
->lock
, flags
);
164 /* Save away the old value so we can compare it */
165 old_val
= chip
->cache_in
;
167 /* Write the new value */
168 chip
->cache_in
= uval
;
170 spin_unlock_irqrestore(&chip
->lock
, flags
);
173 * For each bit in the data (starting at bit 0), trigger
174 * associated interrupts.
176 for (irqpin
= 0; irqpin
< chip
->nirqs
; irqpin
++) {
177 unsigned oldb
, newb
, type
= chip
->flow_type
;
179 irq
= chip
->irq_start
+ irqpin
;
181 /* Run the IRQ handler, but only if the bit value
182 * changed, and the proper flags are set */
183 oldb
= (old_val
>> irqpin
) & 1;
184 newb
= (uval
>> irqpin
) & 1;
186 if ((!oldb
&& newb
&& (type
& IRQ_TYPE_EDGE_RISING
)) ||
187 (oldb
&& !newb
&& (type
& IRQ_TYPE_EDGE_FALLING
))) {
188 pr_debug("fire IRQ %d\n", irqpin
);
189 generic_handle_irq(irq
);
195 * In order to continue receiving interrupts, the int_reset_gpio must
198 if (htcpld
->int_reset_gpio_hi
)
199 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
200 if (htcpld
->int_reset_gpio_lo
)
201 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
207 * The GPIO set routines can be called from interrupt context, especially if,
208 * for example they're attached to the led-gpio framework and a trigger is
209 * enabled. As such, we declared work above in the htcpld_chip structure,
210 * and that work is scheduled in the set routine. The kernel can then run
211 * the I2C functions, which will sleep, in process context.
213 static void htcpld_chip_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
215 struct i2c_client
*client
;
216 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
219 client
= chip_data
->client
;
223 spin_lock_irqsave(&chip_data
->lock
, flags
);
225 chip_data
->cache_out
|= (1 << offset
);
227 chip_data
->cache_out
&= ~(1 << offset
);
228 spin_unlock_irqrestore(&chip_data
->lock
, flags
);
230 schedule_work(&(chip_data
->set_val_work
));
233 static void htcpld_chip_set_ni(struct work_struct
*work
)
235 struct htcpld_chip
*chip_data
;
236 struct i2c_client
*client
;
238 chip_data
= container_of(work
, struct htcpld_chip
, set_val_work
);
239 client
= chip_data
->client
;
240 i2c_smbus_read_byte_data(client
, chip_data
->cache_out
);
243 static int htcpld_chip_get(struct gpio_chip
*chip
, unsigned offset
)
245 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
248 if (!strncmp(chip
->label
, "htcpld-out", 10)) {
249 cache
= chip_data
->cache_out
;
250 } else if (!strncmp(chip
->label
, "htcpld-in", 9)) {
251 cache
= chip_data
->cache_in
;
255 return (cache
>> offset
) & 1;
258 static int htcpld_direction_output(struct gpio_chip
*chip
,
259 unsigned offset
, int value
)
261 htcpld_chip_set(chip
, offset
, value
);
265 static int htcpld_direction_input(struct gpio_chip
*chip
,
269 * No-op: this function can only be called on the input chip.
270 * We do however make sure the offset is within range.
272 return (offset
< chip
->ngpio
) ? 0 : -EINVAL
;
275 static int htcpld_chip_to_irq(struct gpio_chip
*chip
, unsigned offset
)
277 struct htcpld_chip
*chip_data
= gpiochip_get_data(chip
);
279 if (offset
< chip_data
->nirqs
)
280 return chip_data
->irq_start
+ offset
;
285 static void htcpld_chip_reset(struct i2c_client
*client
)
287 struct htcpld_chip
*chip_data
= i2c_get_clientdata(client
);
291 i2c_smbus_read_byte_data(
292 client
, (chip_data
->cache_out
= chip_data
->reset
));
295 static int htcpld_setup_chip_irq(
296 struct platform_device
*pdev
,
299 struct htcpld_data
*htcpld
;
300 struct htcpld_chip
*chip
;
301 unsigned int irq
, irq_end
;
303 /* Get the platform and driver data */
304 htcpld
= platform_get_drvdata(pdev
);
305 chip
= &htcpld
->chip
[chip_index
];
307 /* Setup irq handlers */
308 irq_end
= chip
->irq_start
+ chip
->nirqs
;
309 for (irq
= chip
->irq_start
; irq
< irq_end
; irq
++) {
310 irq_set_chip_and_handler(irq
, &htcpld_muxed_chip
,
312 irq_set_chip_data(irq
, chip
);
313 irq_clear_status_flags(irq
, IRQ_NOREQUEST
| IRQ_NOPROBE
);
319 static int htcpld_register_chip_i2c(
320 struct platform_device
*pdev
,
323 struct htcpld_data
*htcpld
;
324 struct device
*dev
= &pdev
->dev
;
325 struct htcpld_core_platform_data
*pdata
;
326 struct htcpld_chip
*chip
;
327 struct htcpld_chip_platform_data
*plat_chip_data
;
328 struct i2c_adapter
*adapter
;
329 struct i2c_client
*client
;
330 struct i2c_board_info info
;
332 /* Get the platform and driver data */
333 pdata
= dev_get_platdata(dev
);
334 htcpld
= platform_get_drvdata(pdev
);
335 chip
= &htcpld
->chip
[chip_index
];
336 plat_chip_data
= &pdata
->chip
[chip_index
];
338 adapter
= i2c_get_adapter(pdata
->i2c_adapter_id
);
340 /* Eek, no such I2C adapter! Bail out. */
341 dev_warn(dev
, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
342 plat_chip_data
->addr
, pdata
->i2c_adapter_id
);
346 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
)) {
347 dev_warn(dev
, "i2c adapter %d non-functional\n",
348 pdata
->i2c_adapter_id
);
352 memset(&info
, 0, sizeof(struct i2c_board_info
));
353 info
.addr
= plat_chip_data
->addr
;
354 strlcpy(info
.type
, "htcpld-chip", I2C_NAME_SIZE
);
355 info
.platform_data
= chip
;
357 /* Add the I2C device. This calls the probe() function. */
358 client
= i2c_new_device(adapter
, &info
);
360 /* I2C device registration failed, contineu with the next */
361 dev_warn(dev
, "Unable to add I2C device for 0x%x\n",
362 plat_chip_data
->addr
);
366 i2c_set_clientdata(client
, chip
);
367 snprintf(client
->name
, I2C_NAME_SIZE
, "Chip_0x%x", client
->addr
);
368 chip
->client
= client
;
371 htcpld_chip_reset(client
);
372 chip
->cache_in
= i2c_smbus_read_byte_data(client
, chip
->cache_out
);
377 static void htcpld_unregister_chip_i2c(
378 struct platform_device
*pdev
,
381 struct htcpld_data
*htcpld
;
382 struct htcpld_chip
*chip
;
384 /* Get the platform and driver data */
385 htcpld
= platform_get_drvdata(pdev
);
386 chip
= &htcpld
->chip
[chip_index
];
388 i2c_unregister_device(chip
->client
);
391 static int htcpld_register_chip_gpio(
392 struct platform_device
*pdev
,
395 struct htcpld_data
*htcpld
;
396 struct device
*dev
= &pdev
->dev
;
397 struct htcpld_core_platform_data
*pdata
;
398 struct htcpld_chip
*chip
;
399 struct htcpld_chip_platform_data
*plat_chip_data
;
400 struct gpio_chip
*gpio_chip
;
403 /* Get the platform and driver data */
404 pdata
= dev_get_platdata(dev
);
405 htcpld
= platform_get_drvdata(pdev
);
406 chip
= &htcpld
->chip
[chip_index
];
407 plat_chip_data
= &pdata
->chip
[chip_index
];
409 /* Setup the GPIO chips */
410 gpio_chip
= &(chip
->chip_out
);
411 gpio_chip
->label
= "htcpld-out";
412 gpio_chip
->parent
= dev
;
413 gpio_chip
->owner
= THIS_MODULE
;
414 gpio_chip
->get
= htcpld_chip_get
;
415 gpio_chip
->set
= htcpld_chip_set
;
416 gpio_chip
->direction_input
= NULL
;
417 gpio_chip
->direction_output
= htcpld_direction_output
;
418 gpio_chip
->base
= plat_chip_data
->gpio_out_base
;
419 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
421 gpio_chip
= &(chip
->chip_in
);
422 gpio_chip
->label
= "htcpld-in";
423 gpio_chip
->parent
= dev
;
424 gpio_chip
->owner
= THIS_MODULE
;
425 gpio_chip
->get
= htcpld_chip_get
;
426 gpio_chip
->set
= NULL
;
427 gpio_chip
->direction_input
= htcpld_direction_input
;
428 gpio_chip
->direction_output
= NULL
;
429 gpio_chip
->to_irq
= htcpld_chip_to_irq
;
430 gpio_chip
->base
= plat_chip_data
->gpio_in_base
;
431 gpio_chip
->ngpio
= plat_chip_data
->num_gpios
;
433 /* Add the GPIO chips */
434 ret
= gpiochip_add_data(&(chip
->chip_out
), chip
);
436 dev_warn(dev
, "Unable to register output GPIOs for 0x%x: %d\n",
437 plat_chip_data
->addr
, ret
);
441 ret
= gpiochip_add_data(&(chip
->chip_in
), chip
);
443 dev_warn(dev
, "Unable to register input GPIOs for 0x%x: %d\n",
444 plat_chip_data
->addr
, ret
);
445 gpiochip_remove(&(chip
->chip_out
));
452 static int htcpld_setup_chips(struct platform_device
*pdev
)
454 struct htcpld_data
*htcpld
;
455 struct device
*dev
= &pdev
->dev
;
456 struct htcpld_core_platform_data
*pdata
;
459 /* Get the platform and driver data */
460 pdata
= dev_get_platdata(dev
);
461 htcpld
= platform_get_drvdata(pdev
);
463 /* Setup each chip's output GPIOs */
464 htcpld
->nchips
= pdata
->num_chip
;
465 htcpld
->chip
= devm_kcalloc(dev
,
467 sizeof(struct htcpld_chip
),
472 /* Add the chips as best we can */
473 for (i
= 0; i
< htcpld
->nchips
; i
++) {
476 /* Setup the HTCPLD chips */
477 htcpld
->chip
[i
].reset
= pdata
->chip
[i
].reset
;
478 htcpld
->chip
[i
].cache_out
= pdata
->chip
[i
].reset
;
479 htcpld
->chip
[i
].cache_in
= 0;
480 htcpld
->chip
[i
].dev
= dev
;
481 htcpld
->chip
[i
].irq_start
= pdata
->chip
[i
].irq_base
;
482 htcpld
->chip
[i
].nirqs
= pdata
->chip
[i
].num_irqs
;
484 INIT_WORK(&(htcpld
->chip
[i
].set_val_work
), &htcpld_chip_set_ni
);
485 spin_lock_init(&(htcpld
->chip
[i
].lock
));
487 /* Setup the interrupts for the chip */
488 if (htcpld
->chained_irq
) {
489 ret
= htcpld_setup_chip_irq(pdev
, i
);
494 /* Register the chip with I2C */
495 ret
= htcpld_register_chip_i2c(pdev
, i
);
500 /* Register the chips with the GPIO subsystem */
501 ret
= htcpld_register_chip_gpio(pdev
, i
);
503 /* Unregister the chip from i2c and continue */
504 htcpld_unregister_chip_i2c(pdev
, i
);
508 dev_info(dev
, "Registered chip at 0x%x\n", pdata
->chip
[i
].addr
);
514 static int htcpld_core_probe(struct platform_device
*pdev
)
516 struct htcpld_data
*htcpld
;
517 struct device
*dev
= &pdev
->dev
;
518 struct htcpld_core_platform_data
*pdata
;
519 struct resource
*res
;
525 pdata
= dev_get_platdata(dev
);
527 dev_warn(dev
, "Platform data not found for htcpld core!\n");
531 htcpld
= devm_kzalloc(dev
, sizeof(struct htcpld_data
), GFP_KERNEL
);
535 /* Find chained irq */
536 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
539 htcpld
->chained_irq
= res
->start
;
541 /* Setup the chained interrupt handler */
542 flags
= IRQF_TRIGGER_FALLING
| IRQF_TRIGGER_RISING
|
544 ret
= request_threaded_irq(htcpld
->chained_irq
,
545 NULL
, htcpld_handler
,
546 flags
, pdev
->name
, htcpld
);
548 dev_warn(dev
, "Unable to setup chained irq handler: %d\n", ret
);
551 device_init_wakeup(dev
, 0);
554 /* Set the driver data */
555 platform_set_drvdata(pdev
, htcpld
);
557 /* Setup the htcpld chips */
558 ret
= htcpld_setup_chips(pdev
);
562 /* Request the GPIO(s) for the int reset and set them up */
563 if (pdata
->int_reset_gpio_hi
) {
564 ret
= gpio_request(pdata
->int_reset_gpio_hi
, "htcpld-core");
567 * If it failed, that sucks, but we can probably
568 * continue on without it.
570 dev_warn(dev
, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
571 htcpld
->int_reset_gpio_hi
= 0;
573 htcpld
->int_reset_gpio_hi
= pdata
->int_reset_gpio_hi
;
574 gpio_set_value(htcpld
->int_reset_gpio_hi
, 1);
578 if (pdata
->int_reset_gpio_lo
) {
579 ret
= gpio_request(pdata
->int_reset_gpio_lo
, "htcpld-core");
582 * If it failed, that sucks, but we can probably
583 * continue on without it.
585 dev_warn(dev
, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
586 htcpld
->int_reset_gpio_lo
= 0;
588 htcpld
->int_reset_gpio_lo
= pdata
->int_reset_gpio_lo
;
589 gpio_set_value(htcpld
->int_reset_gpio_lo
, 0);
593 dev_info(dev
, "Initialized successfully\n");
597 /* The I2C Driver -- used internally */
598 static const struct i2c_device_id htcpld_chip_id
[] = {
599 { "htcpld-chip", 0 },
603 static struct i2c_driver htcpld_chip_driver
= {
605 .name
= "htcpld-chip",
607 .id_table
= htcpld_chip_id
,
610 /* The Core Driver */
611 static struct platform_driver htcpld_core_driver
= {
613 .name
= "i2c-htcpld",
617 static int __init
htcpld_core_init(void)
621 /* Register the I2C Chip driver */
622 ret
= i2c_add_driver(&htcpld_chip_driver
);
626 /* Probe for our chips */
627 return platform_driver_probe(&htcpld_core_driver
, htcpld_core_probe
);
629 device_initcall(htcpld_core_init
);