Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 * Touch Screen driver for Renesas MIGO-R Platform
  4 *
  5 * Copyright (c) 2008 Magnus Damm
  6 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
  7 *  Kenati Technologies Pvt Ltd.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/input.h>
 12#include <linux/interrupt.h>
 13#include <linux/pm.h>
 14#include <linux/slab.h>
 15#include <asm/io.h>
 16#include <linux/i2c.h>
 17#include <linux/timer.h>
 18
 19#define EVENT_PENDOWN 1
 20#define EVENT_REPEAT  2
 21#define EVENT_PENUP   3
 22
 23struct migor_ts_priv {
 24	struct i2c_client *client;
 25	struct input_dev *input;
 
 26	int irq;
 27};
 28
 29static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
 30					       0x01, 0x06, 0x07, };
 31static const u_int8_t migor_ts_dis_seq[17] = { };
 32
 33static irqreturn_t migor_ts_isr(int irq, void *dev_id)
 34{
 35	struct migor_ts_priv *priv = dev_id;
 
 
 36	unsigned short xpos, ypos;
 37	unsigned char event;
 38	u_int8_t buf[16];
 39
 40	/*
 41	 * The touch screen controller chip is hooked up to the CPU
 42	 * using I2C and a single interrupt line. The interrupt line
 43	 * is pulled low whenever someone taps the screen. To deassert
 44	 * the interrupt line we need to acknowledge the interrupt by
 45	 * communicating with the controller over the slow i2c bus.
 46	 *
 47	 * Since I2C bus controller may sleep we are using threaded
 48	 * IRQ here.
 49	 */
 50
 51	memset(buf, 0, sizeof(buf));
 52
 53	/* Set Index 0 */
 54	buf[0] = 0;
 55	if (i2c_master_send(priv->client, buf, 1) != 1) {
 56		dev_err(&priv->client->dev, "Unable to write i2c index\n");
 57		goto out;
 58	}
 59
 60	/* Now do Page Read */
 61	if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
 62		dev_err(&priv->client->dev, "Unable to read i2c page\n");
 63		goto out;
 64	}
 65
 66	ypos = ((buf[9] & 0x03) << 8 | buf[8]);
 67	xpos = ((buf[11] & 0x03) << 8 | buf[10]);
 68	event = buf[12];
 69
 70	switch (event) {
 71	case EVENT_PENDOWN:
 72	case EVENT_REPEAT:
 73		input_report_key(priv->input, BTN_TOUCH, 1);
 74		input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
 75		input_report_abs(priv->input, ABS_Y, xpos);
 76		input_sync(priv->input);
 77		break;
 78
 79	case EVENT_PENUP:
 80		input_report_key(priv->input, BTN_TOUCH, 0);
 81		input_sync(priv->input);
 82		break;
 83	}
 84
 85 out:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 86	return IRQ_HANDLED;
 87}
 88
 
 89static int migor_ts_open(struct input_dev *dev)
 90{
 91	struct migor_ts_priv *priv = input_get_drvdata(dev);
 92	struct i2c_client *client = priv->client;
 93	int count;
 94
 95	/* enable controller */
 96	count = i2c_master_send(client, migor_ts_ena_seq,
 97				sizeof(migor_ts_ena_seq));
 98	if (count != sizeof(migor_ts_ena_seq)) {
 99		dev_err(&client->dev, "Unable to enable touchscreen.\n");
100		return -ENXIO;
101	}
102
103	return 0;
104}
105
106static void migor_ts_close(struct input_dev *dev)
107{
108	struct migor_ts_priv *priv = input_get_drvdata(dev);
109	struct i2c_client *client = priv->client;
110
111	disable_irq(priv->irq);
112
 
 
 
 
 
 
 
 
 
113	/* disable controller */
114	i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
115
116	enable_irq(priv->irq);
117}
118
119static int migor_ts_probe(struct i2c_client *client)
 
120{
121	struct migor_ts_priv *priv;
122	struct input_dev *input;
123	int error;
124
125	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
126	input = input_allocate_device();
127	if (!priv || !input) {
128		dev_err(&client->dev, "failed to allocate memory\n");
129		error = -ENOMEM;
130		goto err_free_mem;
131	}
132
133	priv->client = client;
134	priv->input = input;
135	priv->irq = client->irq;
136
137	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 
 
 
 
 
138
139	__set_bit(BTN_TOUCH, input->keybit);
 
140
141	input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
142	input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
143
144	input->name = client->name;
145	input->id.bustype = BUS_I2C;
146	input->dev.parent = &client->dev;
147
148	input->open = migor_ts_open;
149	input->close = migor_ts_close;
150
151	input_set_drvdata(input, priv);
152
153	error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
154                                     IRQF_TRIGGER_LOW | IRQF_ONESHOT,
155                                     client->name, priv);
156	if (error) {
157		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
158		goto err_free_mem;
159	}
160
161	error = input_register_device(input);
162	if (error)
163		goto err_free_irq;
164
165	i2c_set_clientdata(client, priv);
166	device_init_wakeup(&client->dev, 1);
 
 
 
 
167
 
168	return 0;
169
170 err_free_irq:
171	free_irq(priv->irq, priv);
172 err_free_mem:
 
173	input_free_device(input);
174	kfree(priv);
 
 
175	return error;
176}
177
178static void migor_ts_remove(struct i2c_client *client)
179{
180	struct migor_ts_priv *priv = i2c_get_clientdata(client);
181
182	free_irq(priv->irq, priv);
183	input_unregister_device(priv->input);
184	kfree(priv);
185
186	dev_set_drvdata(&client->dev, NULL);
 
 
187}
188
189static int __maybe_unused migor_ts_suspend(struct device *dev)
190{
191	struct i2c_client *client = to_i2c_client(dev);
192	struct migor_ts_priv *priv = i2c_get_clientdata(client);
193
194	if (device_may_wakeup(&client->dev))
195		enable_irq_wake(priv->irq);
196
197	return 0;
198}
199
200static int __maybe_unused migor_ts_resume(struct device *dev)
201{
202	struct i2c_client *client = to_i2c_client(dev);
203	struct migor_ts_priv *priv = i2c_get_clientdata(client);
204
205	if (device_may_wakeup(&client->dev))
206		disable_irq_wake(priv->irq);
207
208	return 0;
209}
210
211static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
212
213static const struct i2c_device_id migor_ts_id[] = {
214	{ "migor_ts", 0 },
215	{ }
216};
217MODULE_DEVICE_TABLE(i2c, migor_ts_id);
218
219static struct i2c_driver migor_ts_driver = {
220	.driver = {
221		.name = "migor_ts",
222		.pm = &migor_ts_pm,
223	},
224	.probe_new = migor_ts_probe,
225	.remove = migor_ts_remove,
226	.id_table = migor_ts_id,
227};
228
229module_i2c_driver(migor_ts_driver);
 
 
 
 
 
 
 
 
230
231MODULE_DESCRIPTION("MigoR Touchscreen driver");
232MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
233MODULE_LICENSE("GPL");
v3.1
 
  1/*
  2 * Touch Screen driver for Renesas MIGO-R Platform
  3 *
  4 * Copyright (c) 2008 Magnus Damm
  5 * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
  6 *  Kenati Technologies Pvt Ltd.
  7 *
  8 * This file is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU  General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2 of the License, or (at your option) any later version.
 12 *
 13 * This file is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 *  General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 21 */
 22#include <linux/module.h>
 23#include <linux/kernel.h>
 24#include <linux/input.h>
 25#include <linux/interrupt.h>
 26#include <linux/pm.h>
 27#include <linux/slab.h>
 28#include <asm/io.h>
 29#include <linux/i2c.h>
 30#include <linux/timer.h>
 31
 32#define EVENT_PENDOWN 1
 33#define EVENT_REPEAT  2
 34#define EVENT_PENUP   3
 35
 36struct migor_ts_priv {
 37	struct i2c_client *client;
 38	struct input_dev *input;
 39	struct delayed_work work;
 40	int irq;
 41};
 42
 43static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
 44					       0x01, 0x06, 0x07, };
 45static const u_int8_t migor_ts_dis_seq[17] = { };
 46
 47static void migor_ts_poscheck(struct work_struct *work)
 48{
 49	struct migor_ts_priv *priv = container_of(work,
 50						  struct migor_ts_priv,
 51						  work.work);
 52	unsigned short xpos, ypos;
 53	unsigned char event;
 54	u_int8_t buf[16];
 55
 
 
 
 
 
 
 
 
 
 
 
 56	memset(buf, 0, sizeof(buf));
 57
 58	/* Set Index 0 */
 59	buf[0] = 0;
 60	if (i2c_master_send(priv->client, buf, 1) != 1) {
 61		dev_err(&priv->client->dev, "Unable to write i2c index\n");
 62		goto out;
 63	}
 64
 65	/* Now do Page Read */
 66	if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
 67		dev_err(&priv->client->dev, "Unable to read i2c page\n");
 68		goto out;
 69	}
 70
 71	ypos = ((buf[9] & 0x03) << 8 | buf[8]);
 72	xpos = ((buf[11] & 0x03) << 8 | buf[10]);
 73	event = buf[12];
 74
 75	if (event == EVENT_PENDOWN || event == EVENT_REPEAT) {
 
 
 76		input_report_key(priv->input, BTN_TOUCH, 1);
 77		input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
 78		input_report_abs(priv->input, ABS_Y, xpos);
 79		input_sync(priv->input);
 80	} else if (event == EVENT_PENUP) {
 
 
 81		input_report_key(priv->input, BTN_TOUCH, 0);
 82		input_sync(priv->input);
 
 83	}
 
 84 out:
 85	enable_irq(priv->irq);
 86}
 87
 88static irqreturn_t migor_ts_isr(int irq, void *dev_id)
 89{
 90	struct migor_ts_priv *priv = dev_id;
 91
 92	/* the touch screen controller chip is hooked up to the cpu
 93	 * using i2c and a single interrupt line. the interrupt line
 94	 * is pulled low whenever someone taps the screen. to deassert
 95	 * the interrupt line we need to acknowledge the interrupt by
 96	 * communicating with the controller over the slow i2c bus.
 97	 *
 98	 * we can't acknowledge from interrupt context since the i2c
 99	 * bus controller may sleep, so we just disable the interrupt
100	 * here and handle the acknowledge using delayed work.
101	 */
102
103	disable_irq_nosync(irq);
104	schedule_delayed_work(&priv->work, HZ / 20);
105
106	return IRQ_HANDLED;
107}
108
109
110static int migor_ts_open(struct input_dev *dev)
111{
112	struct migor_ts_priv *priv = input_get_drvdata(dev);
113	struct i2c_client *client = priv->client;
114	int count;
115
116	/* enable controller */
117	count = i2c_master_send(client, migor_ts_ena_seq,
118				sizeof(migor_ts_ena_seq));
119	if (count != sizeof(migor_ts_ena_seq)) {
120		dev_err(&client->dev, "Unable to enable touchscreen.\n");
121		return -ENXIO;
122	}
123
124	return 0;
125}
126
127static void migor_ts_close(struct input_dev *dev)
128{
129	struct migor_ts_priv *priv = input_get_drvdata(dev);
130	struct i2c_client *client = priv->client;
131
132	disable_irq(priv->irq);
133
134	/* cancel pending work and wait for migor_ts_poscheck() to finish */
135	if (cancel_delayed_work_sync(&priv->work)) {
136		/*
137		 * if migor_ts_poscheck was canceled we need to enable IRQ
138		 * here to balance disable done in migor_ts_isr.
139		 */
140		enable_irq(priv->irq);
141	}
142
143	/* disable controller */
144	i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
145
146	enable_irq(priv->irq);
147}
148
149static int migor_ts_probe(struct i2c_client *client,
150			  const struct i2c_device_id *idp)
151{
152	struct migor_ts_priv *priv;
153	struct input_dev *input;
154	int error;
155
156	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
157	if (!priv) {
158		dev_err(&client->dev, "failed to allocate driver data\n");
 
159		error = -ENOMEM;
160		goto err0;
161	}
162
163	dev_set_drvdata(&client->dev, priv);
 
 
164
165	input = input_allocate_device();
166	if (!input) {
167		dev_err(&client->dev, "Failed to allocate input device.\n");
168		error = -ENOMEM;
169		goto err1;
170	}
171
172	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
173	input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
174
175	input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
176	input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
177
178	input->name = client->name;
179	input->id.bustype = BUS_I2C;
180	input->dev.parent = &client->dev;
181
182	input->open = migor_ts_open;
183	input->close = migor_ts_close;
184
185	input_set_drvdata(input, priv);
186
187	priv->client = client;
188	priv->input = input;
189	INIT_DELAYED_WORK(&priv->work, migor_ts_poscheck);
190	priv->irq = client->irq;
 
 
 
191
192	error = input_register_device(input);
193	if (error)
194		goto err1;
195
196	error = request_irq(priv->irq, migor_ts_isr, IRQF_TRIGGER_LOW,
197			    client->name, priv);
198	if (error) {
199		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
200		goto err2;
201	}
202
203	device_init_wakeup(&client->dev, 1);
204	return 0;
205
206 err2:
207	input_unregister_device(input);
208	input = NULL; /* so we dont try to free it below */
209 err1:
210	input_free_device(input);
211	kfree(priv);
212 err0:
213	dev_set_drvdata(&client->dev, NULL);
214	return error;
215}
216
217static int migor_ts_remove(struct i2c_client *client)
218{
219	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
220
221	free_irq(priv->irq, priv);
222	input_unregister_device(priv->input);
223	kfree(priv);
224
225	dev_set_drvdata(&client->dev, NULL);
226
227	return 0;
228}
229
230static int migor_ts_suspend(struct device *dev)
231{
232	struct i2c_client *client = to_i2c_client(dev);
233	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
234
235	if (device_may_wakeup(&client->dev))
236		enable_irq_wake(priv->irq);
237
238	return 0;
239}
240
241static int migor_ts_resume(struct device *dev)
242{
243	struct i2c_client *client = to_i2c_client(dev);
244	struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
245
246	if (device_may_wakeup(&client->dev))
247		disable_irq_wake(priv->irq);
248
249	return 0;
250}
251
252static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
253
254static const struct i2c_device_id migor_ts_id[] = {
255	{ "migor_ts", 0 },
256	{ }
257};
258MODULE_DEVICE_TABLE(i2c, migor_ts);
259
260static struct i2c_driver migor_ts_driver = {
261	.driver = {
262		.name = "migor_ts",
263		.pm = &migor_ts_pm,
264	},
265	.probe = migor_ts_probe,
266	.remove = migor_ts_remove,
267	.id_table = migor_ts_id,
268};
269
270static int __init migor_ts_init(void)
271{
272	return i2c_add_driver(&migor_ts_driver);
273}
274
275static void __exit migor_ts_exit(void)
276{
277	i2c_del_driver(&migor_ts_driver);
278}
279
280MODULE_DESCRIPTION("MigoR Touchscreen driver");
281MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
282MODULE_LICENSE("GPL");
283
284module_init(migor_ts_init);
285module_exit(migor_ts_exit);