xref: /openbmc/linux/drivers/input/touchscreen/migor_ts.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1dda2af74SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+
2885c316dSMagnus Damm /*
3885c316dSMagnus Damm  * Touch Screen driver for Renesas MIGO-R Platform
4885c316dSMagnus Damm  *
5885c316dSMagnus Damm  * Copyright (c) 2008 Magnus Damm
6885c316dSMagnus Damm  * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
7885c316dSMagnus Damm  *  Kenati Technologies Pvt Ltd.
8885c316dSMagnus Damm  */
9885c316dSMagnus Damm #include <linux/module.h>
10885c316dSMagnus Damm #include <linux/kernel.h>
11885c316dSMagnus Damm #include <linux/input.h>
12885c316dSMagnus Damm #include <linux/interrupt.h>
13d72e64ecSMark Brown #include <linux/pm.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15885c316dSMagnus Damm #include <asm/io.h>
16885c316dSMagnus Damm #include <linux/i2c.h>
17885c316dSMagnus Damm #include <linux/timer.h>
18885c316dSMagnus Damm 
19885c316dSMagnus Damm #define EVENT_PENDOWN 1
20885c316dSMagnus Damm #define EVENT_REPEAT  2
21885c316dSMagnus Damm #define EVENT_PENUP   3
22885c316dSMagnus Damm 
23885c316dSMagnus Damm struct migor_ts_priv {
24885c316dSMagnus Damm 	struct i2c_client *client;
25885c316dSMagnus Damm 	struct input_dev *input;
26885c316dSMagnus Damm 	int irq;
27885c316dSMagnus Damm };
28885c316dSMagnus Damm 
29885c316dSMagnus Damm static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
30885c316dSMagnus Damm 					       0x01, 0x06, 0x07, };
31885c316dSMagnus Damm static const u_int8_t migor_ts_dis_seq[17] = { };
32885c316dSMagnus Damm 
migor_ts_isr(int irq,void * dev_id)33468792ebSDmitry Torokhov static irqreturn_t migor_ts_isr(int irq, void *dev_id)
34885c316dSMagnus Damm {
35468792ebSDmitry Torokhov 	struct migor_ts_priv *priv = dev_id;
36885c316dSMagnus Damm 	unsigned short xpos, ypos;
37885c316dSMagnus Damm 	unsigned char event;
38885c316dSMagnus Damm 	u_int8_t buf[16];
39885c316dSMagnus Damm 
40468792ebSDmitry Torokhov 	/*
41468792ebSDmitry Torokhov 	 * The touch screen controller chip is hooked up to the CPU
42468792ebSDmitry Torokhov 	 * using I2C and a single interrupt line. The interrupt line
43468792ebSDmitry Torokhov 	 * is pulled low whenever someone taps the screen. To deassert
44468792ebSDmitry Torokhov 	 * the interrupt line we need to acknowledge the interrupt by
45468792ebSDmitry Torokhov 	 * communicating with the controller over the slow i2c bus.
46468792ebSDmitry Torokhov 	 *
47468792ebSDmitry Torokhov 	 * Since I2C bus controller may sleep we are using threaded
48468792ebSDmitry Torokhov 	 * IRQ here.
49468792ebSDmitry Torokhov 	 */
50468792ebSDmitry Torokhov 
51885c316dSMagnus Damm 	memset(buf, 0, sizeof(buf));
52885c316dSMagnus Damm 
53885c316dSMagnus Damm 	/* Set Index 0 */
54885c316dSMagnus Damm 	buf[0] = 0;
55885c316dSMagnus Damm 	if (i2c_master_send(priv->client, buf, 1) != 1) {
56885c316dSMagnus Damm 		dev_err(&priv->client->dev, "Unable to write i2c index\n");
57885c316dSMagnus Damm 		goto out;
58885c316dSMagnus Damm 	}
59885c316dSMagnus Damm 
60885c316dSMagnus Damm 	/* Now do Page Read */
61885c316dSMagnus Damm 	if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
62885c316dSMagnus Damm 		dev_err(&priv->client->dev, "Unable to read i2c page\n");
63885c316dSMagnus Damm 		goto out;
64885c316dSMagnus Damm 	}
65885c316dSMagnus Damm 
66885c316dSMagnus Damm 	ypos = ((buf[9] & 0x03) << 8 | buf[8]);
67885c316dSMagnus Damm 	xpos = ((buf[11] & 0x03) << 8 | buf[10]);
68885c316dSMagnus Damm 	event = buf[12];
69885c316dSMagnus Damm 
70468792ebSDmitry Torokhov 	switch (event) {
71468792ebSDmitry Torokhov 	case EVENT_PENDOWN:
72468792ebSDmitry Torokhov 	case EVENT_REPEAT:
73885c316dSMagnus Damm 		input_report_key(priv->input, BTN_TOUCH, 1);
74885c316dSMagnus Damm 		input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
75885c316dSMagnus Damm 		input_report_abs(priv->input, ABS_Y, xpos);
76885c316dSMagnus Damm 		input_sync(priv->input);
77468792ebSDmitry Torokhov 		break;
78468792ebSDmitry Torokhov 
79468792ebSDmitry Torokhov 	case EVENT_PENUP:
80885c316dSMagnus Damm 		input_report_key(priv->input, BTN_TOUCH, 0);
81885c316dSMagnus Damm 		input_sync(priv->input);
82468792ebSDmitry Torokhov 		break;
83885c316dSMagnus Damm 	}
84468792ebSDmitry Torokhov 
85885c316dSMagnus Damm  out:
86885c316dSMagnus Damm 	return IRQ_HANDLED;
87885c316dSMagnus Damm }
88885c316dSMagnus Damm 
migor_ts_open(struct input_dev * dev)89885c316dSMagnus Damm static int migor_ts_open(struct input_dev *dev)
90885c316dSMagnus Damm {
91885c316dSMagnus Damm 	struct migor_ts_priv *priv = input_get_drvdata(dev);
92885c316dSMagnus Damm 	struct i2c_client *client = priv->client;
93885c316dSMagnus Damm 	int count;
94885c316dSMagnus Damm 
95885c316dSMagnus Damm 	/* enable controller */
96885c316dSMagnus Damm 	count = i2c_master_send(client, migor_ts_ena_seq,
97885c316dSMagnus Damm 				sizeof(migor_ts_ena_seq));
98885c316dSMagnus Damm 	if (count != sizeof(migor_ts_ena_seq)) {
99885c316dSMagnus Damm 		dev_err(&client->dev, "Unable to enable touchscreen.\n");
100885c316dSMagnus Damm 		return -ENXIO;
101885c316dSMagnus Damm 	}
102885c316dSMagnus Damm 
103885c316dSMagnus Damm 	return 0;
104885c316dSMagnus Damm }
105885c316dSMagnus Damm 
migor_ts_close(struct input_dev * dev)106885c316dSMagnus Damm static void migor_ts_close(struct input_dev *dev)
107885c316dSMagnus Damm {
108885c316dSMagnus Damm 	struct migor_ts_priv *priv = input_get_drvdata(dev);
109885c316dSMagnus Damm 	struct i2c_client *client = priv->client;
110885c316dSMagnus Damm 
111885c316dSMagnus Damm 	disable_irq(priv->irq);
112885c316dSMagnus Damm 
113885c316dSMagnus Damm 	/* disable controller */
114885c316dSMagnus Damm 	i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
115885c316dSMagnus Damm 
116885c316dSMagnus Damm 	enable_irq(priv->irq);
117885c316dSMagnus Damm }
118885c316dSMagnus Damm 
migor_ts_probe(struct i2c_client * client)119ff3de863SUwe Kleine-König static int migor_ts_probe(struct i2c_client *client)
120885c316dSMagnus Damm {
121885c316dSMagnus Damm 	struct migor_ts_priv *priv;
122885c316dSMagnus Damm 	struct input_dev *input;
123885c316dSMagnus Damm 	int error;
124885c316dSMagnus Damm 
125885c316dSMagnus Damm 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
126ff4d0492SDmitry Torokhov 	input = input_allocate_device();
127ff4d0492SDmitry Torokhov 	if (!priv || !input) {
128ff4d0492SDmitry Torokhov 		dev_err(&client->dev, "failed to allocate memory\n");
129885c316dSMagnus Damm 		error = -ENOMEM;
130ff4d0492SDmitry Torokhov 		goto err_free_mem;
131885c316dSMagnus Damm 	}
132885c316dSMagnus Damm 
133ff4d0492SDmitry Torokhov 	priv->client = client;
134ff4d0492SDmitry Torokhov 	priv->input = input;
135ff4d0492SDmitry Torokhov 	priv->irq = client->irq;
136885c316dSMagnus Damm 
137885c316dSMagnus Damm 	input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
138ff4d0492SDmitry Torokhov 
139ff4d0492SDmitry Torokhov 	__set_bit(BTN_TOUCH, input->keybit);
140885c316dSMagnus Damm 
141885c316dSMagnus Damm 	input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
142885c316dSMagnus Damm 	input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
143885c316dSMagnus Damm 
144b6ce9ad0SMagnus Damm 	input->name = client->name;
145885c316dSMagnus Damm 	input->id.bustype = BUS_I2C;
146885c316dSMagnus Damm 	input->dev.parent = &client->dev;
147885c316dSMagnus Damm 
148885c316dSMagnus Damm 	input->open = migor_ts_open;
149885c316dSMagnus Damm 	input->close = migor_ts_close;
150885c316dSMagnus Damm 
151885c316dSMagnus Damm 	input_set_drvdata(input, priv);
152885c316dSMagnus Damm 
153468792ebSDmitry Torokhov 	error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
154468792ebSDmitry Torokhov                                      IRQF_TRIGGER_LOW | IRQF_ONESHOT,
155b6ce9ad0SMagnus Damm                                      client->name, priv);
156885c316dSMagnus Damm 	if (error) {
157885c316dSMagnus Damm 		dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
158ff4d0492SDmitry Torokhov 		goto err_free_mem;
159885c316dSMagnus Damm 	}
160885c316dSMagnus Damm 
161ff4d0492SDmitry Torokhov 	error = input_register_device(input);
162ff4d0492SDmitry Torokhov 	if (error)
163ff4d0492SDmitry Torokhov 		goto err_free_irq;
164ff4d0492SDmitry Torokhov 
165ec208612SDmitry Torokhov 	i2c_set_clientdata(client, priv);
1660935ade0SMagnus Damm 	device_init_wakeup(&client->dev, 1);
167ff4d0492SDmitry Torokhov 
168885c316dSMagnus Damm 	return 0;
169885c316dSMagnus Damm 
170ff4d0492SDmitry Torokhov  err_free_irq:
171ff4d0492SDmitry Torokhov 	free_irq(priv->irq, priv);
172ff4d0492SDmitry Torokhov  err_free_mem:
173885c316dSMagnus Damm 	input_free_device(input);
174885c316dSMagnus Damm 	kfree(priv);
175885c316dSMagnus Damm 	return error;
176885c316dSMagnus Damm }
177885c316dSMagnus Damm 
migor_ts_remove(struct i2c_client * client)178ed5c2f5fSUwe Kleine-König static void migor_ts_remove(struct i2c_client *client)
179885c316dSMagnus Damm {
180ec208612SDmitry Torokhov 	struct migor_ts_priv *priv = i2c_get_clientdata(client);
181885c316dSMagnus Damm 
182885c316dSMagnus Damm 	free_irq(priv->irq, priv);
183885c316dSMagnus Damm 	input_unregister_device(priv->input);
184885c316dSMagnus Damm 	kfree(priv);
185885c316dSMagnus Damm 
186885c316dSMagnus Damm 	dev_set_drvdata(&client->dev, NULL);
187885c316dSMagnus Damm }
188885c316dSMagnus Damm 
migor_ts_suspend(struct device * dev)1893a841b3eSJonathan Cameron static int migor_ts_suspend(struct device *dev)
1900935ade0SMagnus Damm {
191d72e64ecSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
192ec208612SDmitry Torokhov 	struct migor_ts_priv *priv = i2c_get_clientdata(client);
1930935ade0SMagnus Damm 
1940935ade0SMagnus Damm 	if (device_may_wakeup(&client->dev))
1950935ade0SMagnus Damm 		enable_irq_wake(priv->irq);
1960935ade0SMagnus Damm 
1970935ade0SMagnus Damm 	return 0;
1980935ade0SMagnus Damm }
1990935ade0SMagnus Damm 
migor_ts_resume(struct device * dev)2003a841b3eSJonathan Cameron static int migor_ts_resume(struct device *dev)
2010935ade0SMagnus Damm {
202d72e64ecSMark Brown 	struct i2c_client *client = to_i2c_client(dev);
203ec208612SDmitry Torokhov 	struct migor_ts_priv *priv = i2c_get_clientdata(client);
2040935ade0SMagnus Damm 
2050935ade0SMagnus Damm 	if (device_may_wakeup(&client->dev))
2060935ade0SMagnus Damm 		disable_irq_wake(priv->irq);
2070935ade0SMagnus Damm 
2080935ade0SMagnus Damm 	return 0;
2090935ade0SMagnus Damm }
2100935ade0SMagnus Damm 
2113a841b3eSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
212d72e64ecSMark Brown 
213b6ce9ad0SMagnus Damm static const struct i2c_device_id migor_ts_id[] = {
214b6ce9ad0SMagnus Damm 	{ "migor_ts", 0 },
215b6ce9ad0SMagnus Damm 	{ }
216b6ce9ad0SMagnus Damm };
2178f4decc3SJavier Martinez Canillas MODULE_DEVICE_TABLE(i2c, migor_ts_id);
218b6ce9ad0SMagnus Damm 
219885c316dSMagnus Damm static struct i2c_driver migor_ts_driver = {
220885c316dSMagnus Damm 	.driver = {
221885c316dSMagnus Damm 		.name = "migor_ts",
2223a841b3eSJonathan Cameron 		.pm = pm_sleep_ptr(&migor_ts_pm),
223885c316dSMagnus Damm 	},
224*d8bde56dSUwe Kleine-König 	.probe = migor_ts_probe,
225885c316dSMagnus Damm 	.remove = migor_ts_remove,
226b6ce9ad0SMagnus Damm 	.id_table = migor_ts_id,
227885c316dSMagnus Damm };
228885c316dSMagnus Damm 
2291b92c1cfSAxel Lin module_i2c_driver(migor_ts_driver);
230885c316dSMagnus Damm 
231885c316dSMagnus Damm MODULE_DESCRIPTION("MigoR Touchscreen driver");
232885c316dSMagnus Damm MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
233885c316dSMagnus Damm MODULE_LICENSE("GPL");
234