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 
30 static const struct i2c_device_id tca6416_id[] = {
31 	{ "tca6416-keys", 16, },
32 	{ "tca6408-keys", 8, },
33 	{ }
34 };
35 MODULE_DEVICE_TABLE(i2c, tca6416_id);
36 
37 struct tca6416_drv_data {
38 	struct input_dev *input;
39 	struct tca6416_button data[0];
40 };
41 
42 struct 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 
57 static 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 
74 static 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 
91 static 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  */
126 static 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 
135 static 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 
144 static 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 
159 static 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 
169 static int 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 
200 static int 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 = dev_get_platdata(&client->dev);
217 	if (!pdata) {
218 		dev_dbg(&client->dev, "no platform data\n");
219 		return -EINVAL;
220 	}
221 
222 	chip = kzalloc(struct_size(chip, buttons, pdata->nbuttons), GFP_KERNEL);
223 	input = input_allocate_device();
224 	if (!chip || !input) {
225 		error = -ENOMEM;
226 		goto fail1;
227 	}
228 
229 	chip->client = client;
230 	chip->input = input;
231 	chip->io_size = id->driver_data;
232 	chip->pinmask = pdata->pinmask;
233 	chip->use_polling = pdata->use_polling;
234 
235 	INIT_DELAYED_WORK(&chip->dwork, tca6416_keys_work_func);
236 
237 	input->phys = "tca6416-keys/input0";
238 	input->name = client->name;
239 	input->dev.parent = &client->dev;
240 
241 	input->open = tca6416_keys_open;
242 	input->close = tca6416_keys_close;
243 
244 	input->id.bustype = BUS_HOST;
245 	input->id.vendor = 0x0001;
246 	input->id.product = 0x0001;
247 	input->id.version = 0x0100;
248 
249 	/* Enable auto repeat feature of Linux input subsystem */
250 	if (pdata->rep)
251 		__set_bit(EV_REP, input->evbit);
252 
253 	for (i = 0; i < pdata->nbuttons; i++) {
254 		unsigned int type;
255 
256 		chip->buttons[i] = pdata->buttons[i];
257 		type = (pdata->buttons[i].type) ?: EV_KEY;
258 		input_set_capability(input, type, pdata->buttons[i].code);
259 	}
260 
261 	input_set_drvdata(input, chip);
262 
263 	/*
264 	 * Initialize cached registers from their original values.
265 	 * we can't share this chip with another i2c master.
266 	 */
267 	error = tca6416_setup_registers(chip);
268 	if (error)
269 		goto fail1;
270 
271 	if (!chip->use_polling) {
272 		if (pdata->irq_is_gpio)
273 			chip->irqnum = gpio_to_irq(client->irq);
274 		else
275 			chip->irqnum = client->irq;
276 
277 		error = request_threaded_irq(chip->irqnum, NULL,
278 					     tca6416_keys_isr,
279 					     IRQF_TRIGGER_FALLING |
280 						IRQF_ONESHOT,
281 					     "tca6416-keypad", chip);
282 		if (error) {
283 			dev_dbg(&client->dev,
284 				"Unable to claim irq %d; error %d\n",
285 				chip->irqnum, error);
286 			goto fail1;
287 		}
288 		disable_irq(chip->irqnum);
289 	}
290 
291 	error = input_register_device(input);
292 	if (error) {
293 		dev_dbg(&client->dev,
294 			"Unable to register input device, error: %d\n", error);
295 		goto fail2;
296 	}
297 
298 	i2c_set_clientdata(client, chip);
299 	device_init_wakeup(&client->dev, 1);
300 
301 	return 0;
302 
303 fail2:
304 	if (!chip->use_polling) {
305 		free_irq(chip->irqnum, chip);
306 		enable_irq(chip->irqnum);
307 	}
308 fail1:
309 	input_free_device(input);
310 	kfree(chip);
311 	return error;
312 }
313 
314 static int tca6416_keypad_remove(struct i2c_client *client)
315 {
316 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
317 
318 	if (!chip->use_polling) {
319 		free_irq(chip->irqnum, chip);
320 		enable_irq(chip->irqnum);
321 	}
322 
323 	input_unregister_device(chip->input);
324 	kfree(chip);
325 
326 	return 0;
327 }
328 
329 #ifdef CONFIG_PM_SLEEP
330 static int tca6416_keypad_suspend(struct device *dev)
331 {
332 	struct i2c_client *client = to_i2c_client(dev);
333 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
334 
335 	if (device_may_wakeup(dev))
336 		enable_irq_wake(chip->irqnum);
337 
338 	return 0;
339 }
340 
341 static int tca6416_keypad_resume(struct device *dev)
342 {
343 	struct i2c_client *client = to_i2c_client(dev);
344 	struct tca6416_keypad_chip *chip = i2c_get_clientdata(client);
345 
346 	if (device_may_wakeup(dev))
347 		disable_irq_wake(chip->irqnum);
348 
349 	return 0;
350 }
351 #endif
352 
353 static SIMPLE_DEV_PM_OPS(tca6416_keypad_dev_pm_ops,
354 			 tca6416_keypad_suspend, tca6416_keypad_resume);
355 
356 static struct i2c_driver tca6416_keypad_driver = {
357 	.driver = {
358 		.name	= "tca6416-keypad",
359 		.pm	= &tca6416_keypad_dev_pm_ops,
360 	},
361 	.probe		= tca6416_keypad_probe,
362 	.remove		= tca6416_keypad_remove,
363 	.id_table	= tca6416_id,
364 };
365 
366 static int __init tca6416_keypad_init(void)
367 {
368 	return i2c_add_driver(&tca6416_keypad_driver);
369 }
370 
371 subsys_initcall(tca6416_keypad_init);
372 
373 static void __exit tca6416_keypad_exit(void)
374 {
375 	i2c_del_driver(&tca6416_keypad_driver);
376 }
377 module_exit(tca6416_keypad_exit);
378 
379 MODULE_AUTHOR("Sriramakrishnan <srk@ti.com>");
380 MODULE_DESCRIPTION("Keypad driver over tca6146 IO expander");
381 MODULE_LICENSE("GPL");
382