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/driver.h>
24 #include <linux/gpio/machine.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/slab.h>
35 struct i2c_client *client;
39 struct gpio_chip chip_out;
43 struct gpio_chip chip_in;
49 unsigned int flow_type;
51 * Work structure to allow for setting values outside of any
52 * possible interrupt context
54 struct work_struct set_val_work;
63 struct gpio_desc *int_reset_gpio_hi;
64 struct gpio_desc *int_reset_gpio_lo;
67 struct htcpld_chip *chip;
71 /* There does not appear to be a way to proactively mask interrupts
72 * on the htcpld chip itself. So, we simply ignore interrupts that
74 static void htcpld_mask(struct irq_data *data)
76 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
77 chip->irqs_enabled &= ~(1 << (data->irq - chip->irq_start));
78 pr_debug("HTCPLD mask %d %04x\n", data->irq, chip->irqs_enabled);
80 static void htcpld_unmask(struct irq_data *data)
82 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
83 chip->irqs_enabled |= 1 << (data->irq - chip->irq_start);
84 pr_debug("HTCPLD unmask %d %04x\n", data->irq, chip->irqs_enabled);
87 static int htcpld_set_type(struct irq_data *data, unsigned int flags)
89 struct htcpld_chip *chip = irq_data_get_irq_chip_data(data);
91 if (flags & ~IRQ_TYPE_SENSE_MASK)
94 /* We only allow edge triggering */
95 if (flags & (IRQ_TYPE_LEVEL_LOW|IRQ_TYPE_LEVEL_HIGH))
98 chip->flow_type = flags;
102 static struct irq_chip htcpld_muxed_chip = {
104 .irq_mask = htcpld_mask,
105 .irq_unmask = htcpld_unmask,
106 .irq_set_type = htcpld_set_type,
109 /* To properly dispatch IRQ events, we need to read from the
110 * chip. This is an I2C action that could possibly sleep
111 * (which is bad in interrupt context) -- so we use a threaded
112 * interrupt handler to get around that.
114 static irqreturn_t htcpld_handler(int irq, void *dev)
116 struct htcpld_data *htcpld = dev;
122 pr_debug("htcpld is null in ISR\n");
127 * For each chip, do a read of the chip and trigger any interrupts
128 * desired. The interrupts will be triggered from LSB to MSB (i.e.
129 * bit 0 first, then bit 1, etc.)
131 * For chips that have no interrupt range specified, just skip 'em.
133 for (i = 0; i < htcpld->nchips; i++) {
134 struct htcpld_chip *chip = &htcpld->chip[i];
135 struct i2c_client *client;
137 unsigned long uval, old_val;
140 pr_debug("chip %d is null in ISR\n", i);
144 if (chip->nirqs == 0)
147 client = chip->client;
149 pr_debug("client %d is null in ISR\n", i);
154 val = i2c_smbus_read_byte_data(client, chip->cache_out);
156 /* Throw a warning and skip this chip */
157 dev_warn(chip->dev, "Unable to read from chip: %d\n",
162 uval = (unsigned long)val;
164 spin_lock_irqsave(&chip->lock, flags);
166 /* Save away the old value so we can compare it */
167 old_val = chip->cache_in;
169 /* Write the new value */
170 chip->cache_in = uval;
172 spin_unlock_irqrestore(&chip->lock, flags);
175 * For each bit in the data (starting at bit 0), trigger
176 * associated interrupts.
178 for (irqpin = 0; irqpin < chip->nirqs; irqpin++) {
179 unsigned oldb, newb, type = chip->flow_type;
181 irq = chip->irq_start + irqpin;
183 /* Run the IRQ handler, but only if the bit value
184 * changed, and the proper flags are set */
185 oldb = (old_val >> irqpin) & 1;
186 newb = (uval >> irqpin) & 1;
188 if ((!oldb && newb && (type & IRQ_TYPE_EDGE_RISING)) ||
189 (oldb && !newb && (type & IRQ_TYPE_EDGE_FALLING))) {
190 pr_debug("fire IRQ %d\n", irqpin);
191 generic_handle_irq(irq);
197 * In order to continue receiving interrupts, the int_reset_gpio must
200 if (htcpld->int_reset_gpio_hi)
201 gpiod_set_value(htcpld->int_reset_gpio_hi, 1);
202 if (htcpld->int_reset_gpio_lo)
203 gpiod_set_value(htcpld->int_reset_gpio_lo, 0);
209 * The GPIO set routines can be called from interrupt context, especially if,
210 * for example they're attached to the led-gpio framework and a trigger is
211 * enabled. As such, we declared work above in the htcpld_chip structure,
212 * and that work is scheduled in the set routine. The kernel can then run
213 * the I2C functions, which will sleep, in process context.
215 static void htcpld_chip_set(struct gpio_chip *chip, unsigned offset, int val)
217 struct i2c_client *client;
218 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
221 client = chip_data->client;
225 spin_lock_irqsave(&chip_data->lock, flags);
227 chip_data->cache_out |= (1 << offset);
229 chip_data->cache_out &= ~(1 << offset);
230 spin_unlock_irqrestore(&chip_data->lock, flags);
232 schedule_work(&(chip_data->set_val_work));
235 static void htcpld_chip_set_ni(struct work_struct *work)
237 struct htcpld_chip *chip_data;
238 struct i2c_client *client;
240 chip_data = container_of(work, struct htcpld_chip, set_val_work);
241 client = chip_data->client;
242 i2c_smbus_read_byte_data(client, chip_data->cache_out);
245 static int htcpld_chip_get(struct gpio_chip *chip, unsigned offset)
247 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
250 if (!strncmp(chip->label, "htcpld-out", 10)) {
251 cache = chip_data->cache_out;
252 } else if (!strncmp(chip->label, "htcpld-in", 9)) {
253 cache = chip_data->cache_in;
257 return (cache >> offset) & 1;
260 static int htcpld_direction_output(struct gpio_chip *chip,
261 unsigned offset, int value)
263 htcpld_chip_set(chip, offset, value);
267 static int htcpld_direction_input(struct gpio_chip *chip,
271 * No-op: this function can only be called on the input chip.
272 * We do however make sure the offset is within range.
274 return (offset < chip->ngpio) ? 0 : -EINVAL;
277 static int htcpld_chip_to_irq(struct gpio_chip *chip, unsigned offset)
279 struct htcpld_chip *chip_data = gpiochip_get_data(chip);
281 if (offset < chip_data->nirqs)
282 return chip_data->irq_start + offset;
287 static void htcpld_chip_reset(struct i2c_client *client)
289 struct htcpld_chip *chip_data = i2c_get_clientdata(client);
293 i2c_smbus_read_byte_data(
294 client, (chip_data->cache_out = chip_data->reset));
297 static int htcpld_setup_chip_irq(
298 struct platform_device *pdev,
301 struct htcpld_data *htcpld;
302 struct htcpld_chip *chip;
303 unsigned int irq, irq_end;
305 /* Get the platform and driver data */
306 htcpld = platform_get_drvdata(pdev);
307 chip = &htcpld->chip[chip_index];
309 /* Setup irq handlers */
310 irq_end = chip->irq_start + chip->nirqs;
311 for (irq = chip->irq_start; irq < irq_end; irq++) {
312 irq_set_chip_and_handler(irq, &htcpld_muxed_chip,
314 irq_set_chip_data(irq, chip);
315 irq_clear_status_flags(irq, IRQ_NOREQUEST | IRQ_NOPROBE);
321 static int htcpld_register_chip_i2c(
322 struct platform_device *pdev,
325 struct htcpld_data *htcpld;
326 struct device *dev = &pdev->dev;
327 struct htcpld_core_platform_data *pdata;
328 struct htcpld_chip *chip;
329 struct htcpld_chip_platform_data *plat_chip_data;
330 struct i2c_adapter *adapter;
331 struct i2c_client *client;
332 struct i2c_board_info info;
334 /* Get the platform and driver data */
335 pdata = dev_get_platdata(dev);
336 htcpld = platform_get_drvdata(pdev);
337 chip = &htcpld->chip[chip_index];
338 plat_chip_data = &pdata->chip[chip_index];
340 adapter = i2c_get_adapter(pdata->i2c_adapter_id);
342 /* Eek, no such I2C adapter! Bail out. */
343 dev_warn(dev, "Chip at i2c address 0x%x: Invalid i2c adapter %d\n",
344 plat_chip_data->addr, pdata->i2c_adapter_id);
348 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
349 dev_warn(dev, "i2c adapter %d non-functional\n",
350 pdata->i2c_adapter_id);
351 i2c_put_adapter(adapter);
355 memset(&info, 0, sizeof(struct i2c_board_info));
356 info.addr = plat_chip_data->addr;
357 strscpy(info.type, "htcpld-chip", I2C_NAME_SIZE);
358 info.platform_data = chip;
360 /* Add the I2C device. This calls the probe() function. */
361 client = i2c_new_client_device(adapter, &info);
362 if (IS_ERR(client)) {
363 /* I2C device registration failed, contineu with the next */
364 dev_warn(dev, "Unable to add I2C device for 0x%x\n",
365 plat_chip_data->addr);
366 i2c_put_adapter(adapter);
367 return PTR_ERR(client);
370 i2c_set_clientdata(client, chip);
371 snprintf(client->name, I2C_NAME_SIZE, "Chip_0x%x", client->addr);
372 chip->client = client;
375 htcpld_chip_reset(client);
376 chip->cache_in = i2c_smbus_read_byte_data(client, chip->cache_out);
381 static void htcpld_unregister_chip_i2c(
382 struct platform_device *pdev,
385 struct htcpld_data *htcpld;
386 struct htcpld_chip *chip;
388 /* Get the platform and driver data */
389 htcpld = platform_get_drvdata(pdev);
390 chip = &htcpld->chip[chip_index];
392 i2c_unregister_device(chip->client);
395 static int htcpld_register_chip_gpio(
396 struct platform_device *pdev,
399 struct htcpld_data *htcpld;
400 struct device *dev = &pdev->dev;
401 struct htcpld_core_platform_data *pdata;
402 struct htcpld_chip *chip;
403 struct htcpld_chip_platform_data *plat_chip_data;
404 struct gpio_chip *gpio_chip;
407 /* Get the platform and driver data */
408 pdata = dev_get_platdata(dev);
409 htcpld = platform_get_drvdata(pdev);
410 chip = &htcpld->chip[chip_index];
411 plat_chip_data = &pdata->chip[chip_index];
413 /* Setup the GPIO chips */
414 gpio_chip = &(chip->chip_out);
415 gpio_chip->label = "htcpld-out";
416 gpio_chip->parent = dev;
417 gpio_chip->owner = THIS_MODULE;
418 gpio_chip->get = htcpld_chip_get;
419 gpio_chip->set = htcpld_chip_set;
420 gpio_chip->direction_input = NULL;
421 gpio_chip->direction_output = htcpld_direction_output;
422 gpio_chip->base = plat_chip_data->gpio_out_base;
423 gpio_chip->ngpio = plat_chip_data->num_gpios;
425 gpio_chip = &(chip->chip_in);
426 gpio_chip->label = "htcpld-in";
427 gpio_chip->parent = dev;
428 gpio_chip->owner = THIS_MODULE;
429 gpio_chip->get = htcpld_chip_get;
430 gpio_chip->set = NULL;
431 gpio_chip->direction_input = htcpld_direction_input;
432 gpio_chip->direction_output = NULL;
433 gpio_chip->to_irq = htcpld_chip_to_irq;
434 gpio_chip->base = plat_chip_data->gpio_in_base;
435 gpio_chip->ngpio = plat_chip_data->num_gpios;
437 /* Add the GPIO chips */
438 ret = gpiochip_add_data(&(chip->chip_out), chip);
440 dev_warn(dev, "Unable to register output GPIOs for 0x%x: %d\n",
441 plat_chip_data->addr, ret);
445 ret = gpiochip_add_data(&(chip->chip_in), chip);
447 dev_warn(dev, "Unable to register input GPIOs for 0x%x: %d\n",
448 plat_chip_data->addr, ret);
449 gpiochip_remove(&(chip->chip_out));
456 static int htcpld_setup_chips(struct platform_device *pdev)
458 struct htcpld_data *htcpld;
459 struct device *dev = &pdev->dev;
460 struct htcpld_core_platform_data *pdata;
463 /* Get the platform and driver data */
464 pdata = dev_get_platdata(dev);
465 htcpld = platform_get_drvdata(pdev);
467 /* Setup each chip's output GPIOs */
468 htcpld->nchips = pdata->num_chip;
469 htcpld->chip = devm_kcalloc(dev,
471 sizeof(struct htcpld_chip),
476 /* Add the chips as best we can */
477 for (i = 0; i < htcpld->nchips; i++) {
480 /* Setup the HTCPLD chips */
481 htcpld->chip[i].reset = pdata->chip[i].reset;
482 htcpld->chip[i].cache_out = pdata->chip[i].reset;
483 htcpld->chip[i].cache_in = 0;
484 htcpld->chip[i].dev = dev;
485 htcpld->chip[i].irq_start = pdata->chip[i].irq_base;
486 htcpld->chip[i].nirqs = pdata->chip[i].num_irqs;
488 INIT_WORK(&(htcpld->chip[i].set_val_work), &htcpld_chip_set_ni);
489 spin_lock_init(&(htcpld->chip[i].lock));
491 /* Setup the interrupts for the chip */
492 if (htcpld->chained_irq) {
493 ret = htcpld_setup_chip_irq(pdev, i);
498 /* Register the chip with I2C */
499 ret = htcpld_register_chip_i2c(pdev, i);
504 /* Register the chips with the GPIO subsystem */
505 ret = htcpld_register_chip_gpio(pdev, i);
507 /* Unregister the chip from i2c and continue */
508 htcpld_unregister_chip_i2c(pdev, i);
512 dev_info(dev, "Registered chip at 0x%x\n", pdata->chip[i].addr);
518 static int htcpld_core_probe(struct platform_device *pdev)
520 struct htcpld_data *htcpld;
521 struct device *dev = &pdev->dev;
522 struct htcpld_core_platform_data *pdata;
523 struct resource *res;
529 pdata = dev_get_platdata(dev);
531 dev_warn(dev, "Platform data not found for htcpld core!\n");
535 htcpld = devm_kzalloc(dev, sizeof(struct htcpld_data), GFP_KERNEL);
539 /* Find chained irq */
540 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
543 htcpld->chained_irq = res->start;
545 /* Setup the chained interrupt handler */
546 flags = IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING |
548 ret = request_threaded_irq(htcpld->chained_irq,
549 NULL, htcpld_handler,
550 flags, pdev->name, htcpld);
552 dev_warn(dev, "Unable to setup chained irq handler: %d\n", ret);
555 device_init_wakeup(dev, 0);
558 /* Set the driver data */
559 platform_set_drvdata(pdev, htcpld);
561 /* Setup the htcpld chips */
562 ret = htcpld_setup_chips(pdev);
566 /* Request the GPIO(s) for the int reset and set them up */
567 htcpld->int_reset_gpio_hi = gpiochip_request_own_desc(&htcpld->chip[2].chip_out,
568 7, "htcpld-core", GPIO_ACTIVE_HIGH,
570 if (IS_ERR(htcpld->int_reset_gpio_hi)) {
572 * If it failed, that sucks, but we can probably
573 * continue on without it.
575 htcpld->int_reset_gpio_hi = NULL;
576 dev_warn(dev, "Unable to request int_reset_gpio_hi -- interrupts may not work\n");
579 htcpld->int_reset_gpio_lo = gpiochip_request_own_desc(&htcpld->chip[2].chip_out,
580 0, "htcpld-core", GPIO_ACTIVE_HIGH,
582 if (IS_ERR(htcpld->int_reset_gpio_lo)) {
584 * If it failed, that sucks, but we can probably
585 * continue on without it.
587 htcpld->int_reset_gpio_lo = NULL;
588 dev_warn(dev, "Unable to request int_reset_gpio_lo -- interrupts may not work\n");
591 dev_info(dev, "Initialized successfully\n");
595 /* The I2C Driver -- used internally */
596 static const struct i2c_device_id htcpld_chip_id[] = {
597 { "htcpld-chip", 0 },
601 static struct i2c_driver htcpld_chip_driver = {
603 .name = "htcpld-chip",
605 .id_table = htcpld_chip_id,
608 /* The Core Driver */
609 static struct platform_driver htcpld_core_driver = {
611 .name = "i2c-htcpld",
615 static int __init htcpld_core_init(void)
619 /* Register the I2C Chip driver */
620 ret = i2c_add_driver(&htcpld_chip_driver);
624 /* Probe for our chips */
625 return platform_driver_probe(&htcpld_core_driver, htcpld_core_probe);
627 device_initcall(htcpld_core_init);