Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Driver for keys on TCA6416 I2C IO expander
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Author : Sriramakrishnan.A.G. <srk@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/tca6416_keypad.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define TCA6416_INPUT          0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define TCA6416_OUTPUT         1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define TCA6416_INVERT         2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define TCA6416_DIRECTION      3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static const struct i2c_device_id tca6416_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	{ "tca6416-keys", 16, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	{ "tca6408-keys", 8, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) MODULE_DEVICE_TABLE(i2c, tca6416_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) struct tca6416_drv_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	struct tca6416_button data[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct tca6416_keypad_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	uint16_t reg_output;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	uint16_t reg_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	uint16_t reg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct i2c_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	struct delayed_work dwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	int io_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	int irqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u16 pinmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	bool use_polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	struct tca6416_button buttons[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int tca6416_write_reg(struct tca6416_keypad_chip *chip, int reg, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	error = chip->io_size > 8 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		i2c_smbus_write_word_data(chip->client, reg << 1, val) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		i2c_smbus_write_byte_data(chip->client, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		dev_err(&chip->client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 			"%s failed, reg: %d, val: %d, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			__func__, reg, val, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static int tca6416_read_reg(struct tca6416_keypad_chip *chip, int reg, u16 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	retval = chip->io_size > 8 ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 i2c_smbus_read_word_data(chip->client, reg << 1) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		 i2c_smbus_read_byte_data(chip->client, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (retval < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		dev_err(&chip->client->dev, "%s failed, reg: %d, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			__func__, reg, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	*val = (u16)retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static void tca6416_keys_scan(struct tca6416_keypad_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	struct input_dev *input = chip->input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	u16 reg_val, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	int error, i, pin_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	error = tca6416_read_reg(chip, TCA6416_INPUT, &reg_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	reg_val &= chip->pinmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/* Figure out which lines have changed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	val = reg_val ^ chip->reg_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	chip->reg_input = reg_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	for (i = 0, pin_index = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (val & (1 << i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			struct tca6416_button *button = &chip->buttons[pin_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 			unsigned int type = button->type ?: EV_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			int state = ((reg_val & (1 << i)) ? 1 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 						^ button->active_low;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 			input_event(input, type, button->code, !!state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			input_sync(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		if (chip->pinmask & (1 << i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 			pin_index++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * This is threaded IRQ handler and this can (and will) sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static irqreturn_t tca6416_keys_isr(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	struct tca6416_keypad_chip *chip = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	tca6416_keys_scan(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void tca6416_keys_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct tca6416_keypad_chip *chip =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		container_of(work, struct tca6416_keypad_chip, dwork.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	tca6416_keys_scan(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int tca6416_keys_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	/* Get initial device state in case it has switches */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	tca6416_keys_scan(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (chip->use_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		schedule_delayed_work(&chip->dwork, msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		enable_irq(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static void tca6416_keys_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	struct tca6416_keypad_chip *chip = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	if (chip->use_polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		cancel_delayed_work_sync(&chip->dwork);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		disable_irq(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static int tca6416_setup_registers(struct tca6416_keypad_chip *chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	error = tca6416_read_reg(chip, TCA6416_OUTPUT, &chip->reg_output);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* ensure that keypad pins are set to input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	error = tca6416_write_reg(chip, TCA6416_DIRECTION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 				  chip->reg_direction | chip->pinmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	error = tca6416_read_reg(chip, TCA6416_DIRECTION, &chip->reg_direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	error = tca6416_read_reg(chip, TCA6416_INPUT, &chip->reg_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	chip->reg_input &= chip->pinmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int tca6416_keypad_probe(struct i2c_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 				   const struct i2c_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	struct tca6416_keys_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	struct tca6416_keypad_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	struct input_dev *input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/* Check functionality */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		dev_err(&client->dev, "%s adapter not supported\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			dev_driver_string(&client->adapter->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		dev_dbg(&client->dev, "no platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	chip = kzalloc(struct_size(chip, buttons, pdata->nbuttons), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	input = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (!chip || !input) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	chip->client = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	chip->input = input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	chip->io_size = id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	chip->pinmask = pdata->pinmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	chip->use_polling = pdata->use_polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	input->phys = "tca6416-keys/input0";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	input->name = client->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	input->dev.parent = &client->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	input->open = tca6416_keys_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	input->close = tca6416_keys_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	input->id.bustype = BUS_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	input->id.vendor = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	input->id.product = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	input->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* Enable auto repeat feature of Linux input subsystem */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (pdata->rep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		__set_bit(EV_REP, input->evbit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	for (i = 0; i < pdata->nbuttons; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		chip->buttons[i] = pdata->buttons[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		type = (pdata->buttons[i].type) ?: EV_KEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		input_set_capability(input, type, pdata->buttons[i].code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	input_set_drvdata(input, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * Initialize cached registers from their original values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 * we can't share this chip with another i2c master.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	error = tca6416_setup_registers(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (!chip->use_polling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (pdata->irq_is_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			chip->irqnum = gpio_to_irq(client->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			chip->irqnum = client->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		error = request_threaded_irq(chip->irqnum, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 					     tca6416_keys_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 					     IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 						IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 					     "tca6416-keypad", chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			dev_dbg(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				"Unable to claim irq %d; error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 				chip->irqnum, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		disable_irq(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	error = input_register_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		dev_dbg(&client->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			"Unable to register input device, error: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	i2c_set_clientdata(client, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	device_init_wakeup(&client->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) fail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (!chip->use_polling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		free_irq(chip->irqnum, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		enable_irq(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	input_free_device(input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int tca6416_keypad_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (!chip->use_polling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		free_irq(chip->irqnum, chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		enable_irq(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	input_unregister_device(chip->input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	kfree(chip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int tca6416_keypad_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		enable_irq_wake(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int tca6416_keypad_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (device_may_wakeup(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		disable_irq_wake(chip->irqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			 tca6416_keypad_suspend, tca6416_keypad_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static struct i2c_driver tca6416_keypad_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		.name	= "tca6416-keypad",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		.pm	= &tca6416_keypad_dev_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.probe		= tca6416_keypad_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	.remove		= tca6416_keypad_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.id_table	= tca6416_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int __init tca6416_keypad_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	return i2c_add_driver(&tca6416_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) subsys_initcall(tca6416_keypad_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static void __exit tca6416_keypad_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	i2c_del_driver(&tca6416_keypad_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) module_exit(tca6416_keypad_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) MODULE_DESCRIPTION("Keypad driver over tca6416 IO expander");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) MODULE_LICENSE("GPL");