Linux Audio

Check our new training course

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