mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 10:40:15 +02:00 
			
		
		
		
	gpio: pcf857x: enable gpio_to_irq() support
pcf857x chip has some pins, but interrupt pin is only 1 pin. And gpiolib requests 1 interrupt per 1 gpio pin. Thus, this patch added demuxed irq to pcf857x driver, and enabled gpio_to_irq(). Signed-off-by: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
This commit is contained in:
		
							parent
							
								
									7cb6580c0b
								
							
						
					
					
						commit
						6e20a0a429
					
				
					 2 changed files with 125 additions and 0 deletions
				
			
		| 
						 | 
					@ -23,7 +23,12 @@
 | 
				
			||||||
#include <linux/gpio.h>
 | 
					#include <linux/gpio.h>
 | 
				
			||||||
#include <linux/i2c.h>
 | 
					#include <linux/i2c.h>
 | 
				
			||||||
#include <linux/i2c/pcf857x.h>
 | 
					#include <linux/i2c/pcf857x.h>
 | 
				
			||||||
 | 
					#include <linux/interrupt.h>
 | 
				
			||||||
 | 
					#include <linux/irq.h>
 | 
				
			||||||
 | 
					#include <linux/irqdomain.h>
 | 
				
			||||||
#include <linux/module.h>
 | 
					#include <linux/module.h>
 | 
				
			||||||
 | 
					#include <linux/spinlock.h>
 | 
				
			||||||
 | 
					#include <linux/workqueue.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const struct i2c_device_id pcf857x_id[] = {
 | 
					static const struct i2c_device_id pcf857x_id[] = {
 | 
				
			||||||
| 
						 | 
					@ -60,7 +65,12 @@ struct pcf857x {
 | 
				
			||||||
	struct gpio_chip	chip;
 | 
						struct gpio_chip	chip;
 | 
				
			||||||
	struct i2c_client	*client;
 | 
						struct i2c_client	*client;
 | 
				
			||||||
	struct mutex		lock;		/* protect 'out' */
 | 
						struct mutex		lock;		/* protect 'out' */
 | 
				
			||||||
 | 
						struct work_struct	work;		/* irq demux work */
 | 
				
			||||||
 | 
						struct irq_domain	*irq_domain;	/* for irq demux  */
 | 
				
			||||||
 | 
						spinlock_t		slock;		/* protect irq demux */
 | 
				
			||||||
	unsigned		out;		/* software latch */
 | 
						unsigned		out;		/* software latch */
 | 
				
			||||||
 | 
						unsigned		status;		/* current status */
 | 
				
			||||||
 | 
						int			irq;		/* real irq number */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	int (*write)(struct i2c_client *client, unsigned data);
 | 
						int (*write)(struct i2c_client *client, unsigned data);
 | 
				
			||||||
	int (*read)(struct i2c_client *client);
 | 
						int (*read)(struct i2c_client *client);
 | 
				
			||||||
| 
						 | 
					@ -150,6 +160,100 @@ static void pcf857x_set(struct gpio_chip *chip, unsigned offset, int value)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*-------------------------------------------------------------------------*/
 | 
					/*-------------------------------------------------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int pcf857x_to_irq(struct gpio_chip *chip, unsigned offset)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct pcf857x *gpio = container_of(chip, struct pcf857x, chip);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return irq_create_mapping(gpio->irq_domain, offset);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void pcf857x_irq_demux_work(struct work_struct *work)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct pcf857x *gpio = container_of(work,
 | 
				
			||||||
 | 
										       struct pcf857x,
 | 
				
			||||||
 | 
										       work);
 | 
				
			||||||
 | 
						unsigned long change, i, status, flags;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						status = gpio->read(gpio->client);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						spin_lock_irqsave(&gpio->slock, flags);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						change = gpio->status ^ status;
 | 
				
			||||||
 | 
						for_each_set_bit(i, &change, gpio->chip.ngpio)
 | 
				
			||||||
 | 
							generic_handle_irq(irq_find_mapping(gpio->irq_domain, i));
 | 
				
			||||||
 | 
						gpio->status = status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						spin_unlock_irqrestore(&gpio->slock, flags);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static irqreturn_t pcf857x_irq_demux(int irq, void *data)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						struct pcf857x	*gpio = data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/*
 | 
				
			||||||
 | 
						 * pcf857x can't read/write data here,
 | 
				
			||||||
 | 
						 * since i2c data access might go to sleep.
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						schedule_work(&gpio->work);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return IRQ_HANDLED;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int pcf857x_irq_domain_map(struct irq_domain *domain, unsigned int virq,
 | 
				
			||||||
 | 
									 irq_hw_number_t hw)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						irq_set_chip_and_handler(virq,
 | 
				
			||||||
 | 
									 &dummy_irq_chip,
 | 
				
			||||||
 | 
									 handle_level_irq);
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static struct irq_domain_ops pcf857x_irq_domain_ops = {
 | 
				
			||||||
 | 
						.map	= pcf857x_irq_domain_map,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void pcf857x_irq_domain_cleanup(struct pcf857x *gpio)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (gpio->irq_domain)
 | 
				
			||||||
 | 
							irq_domain_remove(gpio->irq_domain);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (gpio->irq)
 | 
				
			||||||
 | 
							free_irq(gpio->irq, gpio);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int pcf857x_irq_domain_init(struct pcf857x *gpio,
 | 
				
			||||||
 | 
									   struct pcf857x_platform_data *pdata,
 | 
				
			||||||
 | 
									   struct device *dev)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						int status;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						gpio->irq_domain = irq_domain_add_linear(dev->of_node,
 | 
				
			||||||
 | 
											 gpio->chip.ngpio,
 | 
				
			||||||
 | 
											 &pcf857x_irq_domain_ops,
 | 
				
			||||||
 | 
											 NULL);
 | 
				
			||||||
 | 
						if (!gpio->irq_domain)
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* enable real irq */
 | 
				
			||||||
 | 
						status = request_irq(pdata->irq, pcf857x_irq_demux, 0,
 | 
				
			||||||
 | 
								     dev_name(dev), gpio);
 | 
				
			||||||
 | 
						if (status)
 | 
				
			||||||
 | 
							goto fail;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* enable gpio_to_irq() */
 | 
				
			||||||
 | 
						INIT_WORK(&gpio->work, pcf857x_irq_demux_work);
 | 
				
			||||||
 | 
						gpio->chip.to_irq	= pcf857x_to_irq;
 | 
				
			||||||
 | 
						gpio->irq		= pdata->irq;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fail:
 | 
				
			||||||
 | 
						pcf857x_irq_domain_cleanup(gpio);
 | 
				
			||||||
 | 
						return -EINVAL;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*-------------------------------------------------------------------------*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int pcf857x_probe(struct i2c_client *client,
 | 
					static int pcf857x_probe(struct i2c_client *client,
 | 
				
			||||||
			 const struct i2c_device_id *id)
 | 
								 const struct i2c_device_id *id)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -168,6 +272,7 @@ static int pcf857x_probe(struct i2c_client *client,
 | 
				
			||||||
		return -ENOMEM;
 | 
							return -ENOMEM;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	mutex_init(&gpio->lock);
 | 
						mutex_init(&gpio->lock);
 | 
				
			||||||
 | 
						spin_lock_init(&gpio->slock);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	gpio->chip.base			= pdata ? pdata->gpio_base : -1;
 | 
						gpio->chip.base			= pdata ? pdata->gpio_base : -1;
 | 
				
			||||||
	gpio->chip.can_sleep		= 1;
 | 
						gpio->chip.can_sleep		= 1;
 | 
				
			||||||
| 
						 | 
					@ -179,6 +284,15 @@ static int pcf857x_probe(struct i2c_client *client,
 | 
				
			||||||
	gpio->chip.direction_output	= pcf857x_output;
 | 
						gpio->chip.direction_output	= pcf857x_output;
 | 
				
			||||||
	gpio->chip.ngpio		= id->driver_data;
 | 
						gpio->chip.ngpio		= id->driver_data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/* enable gpio_to_irq() if platform has settings */
 | 
				
			||||||
 | 
						if (pdata->irq) {
 | 
				
			||||||
 | 
							status = pcf857x_irq_domain_init(gpio, pdata, &client->dev);
 | 
				
			||||||
 | 
							if (status < 0) {
 | 
				
			||||||
 | 
								dev_err(&client->dev, "irq_domain init failed\n");
 | 
				
			||||||
 | 
								goto fail;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/* NOTE:  the OnSemi jlc1562b is also largely compatible with
 | 
						/* NOTE:  the OnSemi jlc1562b is also largely compatible with
 | 
				
			||||||
	 * these parts, notably for output.  It has a low-resolution
 | 
						 * these parts, notably for output.  It has a low-resolution
 | 
				
			||||||
	 * DAC instead of pin change IRQs; and its inputs can be the
 | 
						 * DAC instead of pin change IRQs; and its inputs can be the
 | 
				
			||||||
| 
						 | 
					@ -248,6 +362,7 @@ static int pcf857x_probe(struct i2c_client *client,
 | 
				
			||||||
	 * all-ones reset state.  Otherwise it flags pins to be driven low.
 | 
						 * all-ones reset state.  Otherwise it flags pins to be driven low.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	gpio->out = pdata ? ~pdata->n_latch : ~0;
 | 
						gpio->out = pdata ? ~pdata->n_latch : ~0;
 | 
				
			||||||
 | 
						gpio->status = gpio->out;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	status = gpiochip_add(&gpio->chip);
 | 
						status = gpiochip_add(&gpio->chip);
 | 
				
			||||||
	if (status < 0)
 | 
						if (status < 0)
 | 
				
			||||||
| 
						 | 
					@ -278,6 +393,10 @@ static int pcf857x_probe(struct i2c_client *client,
 | 
				
			||||||
fail:
 | 
					fail:
 | 
				
			||||||
	dev_dbg(&client->dev, "probe error %d for '%s'\n",
 | 
						dev_dbg(&client->dev, "probe error %d for '%s'\n",
 | 
				
			||||||
			status, client->name);
 | 
								status, client->name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (pdata->irq)
 | 
				
			||||||
 | 
							pcf857x_irq_domain_cleanup(gpio);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	kfree(gpio);
 | 
						kfree(gpio);
 | 
				
			||||||
	return status;
 | 
						return status;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -299,6 +418,9 @@ static int pcf857x_remove(struct i2c_client *client)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (pdata->irq)
 | 
				
			||||||
 | 
							pcf857x_irq_domain_cleanup(gpio);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	status = gpiochip_remove(&gpio->chip);
 | 
						status = gpiochip_remove(&gpio->chip);
 | 
				
			||||||
	if (status == 0)
 | 
						if (status == 0)
 | 
				
			||||||
		kfree(gpio);
 | 
							kfree(gpio);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -10,6 +10,7 @@
 | 
				
			||||||
 * @setup: optional callback issued once the GPIOs are valid
 | 
					 * @setup: optional callback issued once the GPIOs are valid
 | 
				
			||||||
 * @teardown: optional callback issued before the GPIOs are invalidated
 | 
					 * @teardown: optional callback issued before the GPIOs are invalidated
 | 
				
			||||||
 * @context: optional parameter passed to setup() and teardown()
 | 
					 * @context: optional parameter passed to setup() and teardown()
 | 
				
			||||||
 | 
					 * @irq: optional interrupt number
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * In addition to the I2C_BOARD_INFO() state appropriate to each chip,
 | 
					 * In addition to the I2C_BOARD_INFO() state appropriate to each chip,
 | 
				
			||||||
 * the i2c_board_info used with the pcf875x driver must provide its
 | 
					 * the i2c_board_info used with the pcf875x driver must provide its
 | 
				
			||||||
| 
						 | 
					@ -39,6 +40,8 @@ struct pcf857x_platform_data {
 | 
				
			||||||
					int gpio, unsigned ngpio,
 | 
										int gpio, unsigned ngpio,
 | 
				
			||||||
					void *context);
 | 
										void *context);
 | 
				
			||||||
	void		*context;
 | 
						void		*context;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						int		irq;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* __LINUX_PCF857X_H */
 | 
					#endif /* __LINUX_PCF857X_H */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue