Linux Audio

Check our new training course

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