Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for cypress touch screen controller
  4 *
  5 * Copyright (c) 2009 Aava Mobile
  6 *
  7 * Some cleanups by Alan Cox <alan@linux.intel.com>
  8 */
  9
 10#include <linux/i2c.h>
 11#include <linux/input.h>
 12#include <linux/interrupt.h>
 13#include <linux/gpio/consumer.h>
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 
 
 16#include <linux/slab.h>
 17#include <asm/byteorder.h>
 
 
 
 
 18
 19#define CY8CTMG110_DRIVER_NAME      "cy8ctmg110"
 20
 21/* Touch coordinates */
 22#define CY8CTMG110_X_MIN		0
 23#define CY8CTMG110_Y_MIN		0
 24#define CY8CTMG110_X_MAX		759
 25#define CY8CTMG110_Y_MAX		465
 26
 27
 28/* cy8ctmg110 register definitions */
 29#define CY8CTMG110_TOUCH_WAKEUP_TIME	0
 30#define CY8CTMG110_TOUCH_SLEEP_TIME	2
 31#define CY8CTMG110_TOUCH_X1		3
 32#define CY8CTMG110_TOUCH_Y1		5
 33#define CY8CTMG110_TOUCH_X2		7
 34#define CY8CTMG110_TOUCH_Y2		9
 35#define CY8CTMG110_FINGERS		11
 36#define CY8CTMG110_GESTURE		12
 37#define CY8CTMG110_REG_MAX		13
 38
 39
 40/*
 41 * The touch driver structure.
 42 */
 43struct cy8ctmg110 {
 44	struct input_dev *input;
 45	char phys[32];
 46	struct i2c_client *client;
 47	struct gpio_desc *reset_gpio;
 
 48};
 49
 50/*
 51 * cy8ctmg110_power is the routine that is called when touch hardware
 52 * is being powered off or on. When powering on this routine de-asserts
 53 * the RESET line, when powering off reset line is asserted.
 54 */
 55static void cy8ctmg110_power(struct cy8ctmg110 *ts, bool poweron)
 56{
 57	if (ts->reset_gpio)
 58		gpiod_set_value_cansleep(ts->reset_gpio, !poweron);
 59}
 60
 61static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
 62		unsigned char len, unsigned char *value)
 63{
 64	struct i2c_client *client = tsc->client;
 65	int ret;
 66	unsigned char i2c_data[6];
 67
 68	BUG_ON(len > 5);
 69
 70	i2c_data[0] = reg;
 71	memcpy(i2c_data + 1, value, len);
 72
 73	ret = i2c_master_send(client, i2c_data, len + 1);
 74	if (ret != len + 1) {
 75		dev_err(&client->dev, "i2c write data cmd failed\n");
 76		return ret < 0 ? ret : -EIO;
 77	}
 78
 79	return 0;
 80}
 81
 82static int cy8ctmg110_read_regs(struct cy8ctmg110 *tsc,
 83		unsigned char *data, unsigned char len, unsigned char cmd)
 84{
 85	struct i2c_client *client = tsc->client;
 86	int ret;
 87	struct i2c_msg msg[2] = {
 88		/* first write slave position to i2c devices */
 89		{
 90			.addr = client->addr,
 91			.len = 1,
 92			.buf = &cmd
 93		},
 94		/* Second read data from position */
 95		{
 96			.addr = client->addr,
 97			.flags = I2C_M_RD,
 98			.len = len,
 99			.buf = data
100		}
101	};
102
103	ret = i2c_transfer(client->adapter, msg, 2);
104	if (ret < 0)
105		return ret;
106
107	return 0;
108}
109
110static int cy8ctmg110_touch_pos(struct cy8ctmg110 *tsc)
111{
112	struct input_dev *input = tsc->input;
113	unsigned char reg_p[CY8CTMG110_REG_MAX];
 
114
115	memset(reg_p, 0, CY8CTMG110_REG_MAX);
116
117	/* Reading coordinates */
118	if (cy8ctmg110_read_regs(tsc, reg_p, 9, CY8CTMG110_TOUCH_X1) != 0)
119		return -EIO;
120
 
 
 
121	/* Number of touch */
122	if (reg_p[8] == 0) {
123		input_report_key(input, BTN_TOUCH, 0);
124	} else  {
125		input_report_key(input, BTN_TOUCH, 1);
126		input_report_abs(input, ABS_X,
127				 be16_to_cpup((__be16 *)(reg_p + 0)));
128		input_report_abs(input, ABS_Y,
129				 be16_to_cpup((__be16 *)(reg_p + 2)));
130	}
131
132	input_sync(input);
133
134	return 0;
135}
136
137static int cy8ctmg110_set_sleepmode(struct cy8ctmg110 *ts, bool sleep)
138{
139	unsigned char reg_p[3];
140
141	if (sleep) {
142		reg_p[0] = 0x00;
143		reg_p[1] = 0xff;
144		reg_p[2] = 5;
145	} else {
146		reg_p[0] = 0x10;
147		reg_p[1] = 0xff;
148		reg_p[2] = 0;
149	}
150
151	return cy8ctmg110_write_regs(ts, CY8CTMG110_TOUCH_WAKEUP_TIME, 3, reg_p);
152}
153
154static irqreturn_t cy8ctmg110_irq_thread(int irq, void *dev_id)
155{
156	struct cy8ctmg110 *tsc = dev_id;
157
158	cy8ctmg110_touch_pos(tsc);
159
160	return IRQ_HANDLED;
161}
162
163static void cy8ctmg110_shut_off(void *_ts)
164{
165	struct cy8ctmg110 *ts = _ts;
166
167	cy8ctmg110_set_sleepmode(ts, true);
168	cy8ctmg110_power(ts, false);
169}
170
171static int cy8ctmg110_probe(struct i2c_client *client)
172{
 
173	struct cy8ctmg110 *ts;
174	struct input_dev *input_dev;
175	int err;
176
 
 
 
 
 
 
177	if (!i2c_check_functionality(client->adapter,
178					I2C_FUNC_SMBUS_READ_WORD_DATA))
179		return -EIO;
180
181	ts = devm_kzalloc(&client->dev, sizeof(*ts), GFP_KERNEL);
182	if (!ts)
183		return -ENOMEM;
184
185	input_dev = devm_input_allocate_device(&client->dev);
186	if (!input_dev)
187		return -ENOMEM;
188
189	ts->client = client;
190	ts->input = input_dev;
 
 
191
192	snprintf(ts->phys, sizeof(ts->phys),
193		 "%s/input0", dev_name(&client->dev));
194
195	input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
196	input_dev->phys = ts->phys;
197	input_dev->id.bustype = BUS_I2C;
 
 
 
 
198
199	input_set_capability(input_dev, EV_KEY, BTN_TOUCH);
200	input_set_abs_params(input_dev, ABS_X,
201			CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
202	input_set_abs_params(input_dev, ABS_Y,
203			CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
204
205	/* Request and assert reset line */
206	ts->reset_gpio = devm_gpiod_get_optional(&client->dev, NULL,
207						 GPIOD_OUT_HIGH);
208	if (IS_ERR(ts->reset_gpio)) {
209		err = PTR_ERR(ts->reset_gpio);
210		dev_err(&client->dev,
211			"Unable to request reset GPIO: %d\n", err);
212		return err;
213	}
214
215	cy8ctmg110_power(ts, true);
216	cy8ctmg110_set_sleepmode(ts, false);
217
218	err = devm_add_action_or_reset(&client->dev, cy8ctmg110_shut_off, ts);
219	if (err)
220		return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
222	err = devm_request_threaded_irq(&client->dev, client->irq,
223					NULL, cy8ctmg110_irq_thread,
224					IRQF_ONESHOT, "touch_reset_key", ts);
225	if (err) {
226		dev_err(&client->dev,
227			"irq %d busy? error %d\n", client->irq, err);
228		return err;
229	}
230
231	err = input_register_device(input_dev);
232	if (err)
233		return err;
234
235	i2c_set_clientdata(client, ts);
236
237	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238}
239
240static int __maybe_unused cy8ctmg110_suspend(struct device *dev)
241{
242	struct i2c_client *client = to_i2c_client(dev);
243	struct cy8ctmg110 *ts = i2c_get_clientdata(client);
244
245	if (!device_may_wakeup(&client->dev)) {
 
 
246		cy8ctmg110_set_sleepmode(ts, true);
247		cy8ctmg110_power(ts, false);
248	}
249
250	return 0;
251}
252
253static int __maybe_unused cy8ctmg110_resume(struct device *dev)
254{
255	struct i2c_client *client = to_i2c_client(dev);
256	struct cy8ctmg110 *ts = i2c_get_clientdata(client);
257
258	if (!device_may_wakeup(&client->dev)) {
 
 
259		cy8ctmg110_power(ts, true);
260		cy8ctmg110_set_sleepmode(ts, false);
261	}
262
263	return 0;
264}
265
266static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268static const struct i2c_device_id cy8ctmg110_idtable[] = {
269	{ CY8CTMG110_DRIVER_NAME, 1 },
270	{ }
271};
272
273MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
274
275static struct i2c_driver cy8ctmg110_driver = {
276	.driver		= {
277		.name	= CY8CTMG110_DRIVER_NAME,
278		.pm	= &cy8ctmg110_pm,
279	},
280	.id_table	= cy8ctmg110_idtable,
281	.probe_new	= cy8ctmg110_probe,
 
282};
283
284module_i2c_driver(cy8ctmg110_driver);
285
286MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
287MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
288MODULE_LICENSE("GPL v2");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Driver for cypress touch screen controller
  4 *
  5 * Copyright (c) 2009 Aava Mobile
  6 *
  7 * Some cleanups by Alan Cox <alan@linux.intel.com>
  8 */
  9
 
 
 
 
 
 10#include <linux/module.h>
 11#include <linux/kernel.h>
 12#include <linux/input.h>
 13#include <linux/slab.h>
 14#include <linux/interrupt.h>
 15#include <linux/io.h>
 16#include <linux/i2c.h>
 17#include <linux/gpio.h>
 18#include <linux/input/cy8ctmg110_pdata.h>
 19
 20#define CY8CTMG110_DRIVER_NAME      "cy8ctmg110"
 21
 22/* Touch coordinates */
 23#define CY8CTMG110_X_MIN		0
 24#define CY8CTMG110_Y_MIN		0
 25#define CY8CTMG110_X_MAX		759
 26#define CY8CTMG110_Y_MAX		465
 27
 28
 29/* cy8ctmg110 register definitions */
 30#define CY8CTMG110_TOUCH_WAKEUP_TIME	0
 31#define CY8CTMG110_TOUCH_SLEEP_TIME	2
 32#define CY8CTMG110_TOUCH_X1		3
 33#define CY8CTMG110_TOUCH_Y1		5
 34#define CY8CTMG110_TOUCH_X2		7
 35#define CY8CTMG110_TOUCH_Y2		9
 36#define CY8CTMG110_FINGERS		11
 37#define CY8CTMG110_GESTURE		12
 38#define CY8CTMG110_REG_MAX		13
 39
 40
 41/*
 42 * The touch driver structure.
 43 */
 44struct cy8ctmg110 {
 45	struct input_dev *input;
 46	char phys[32];
 47	struct i2c_client *client;
 48	int reset_pin;
 49	int irq_pin;
 50};
 51
 52/*
 53 * cy8ctmg110_power is the routine that is called when touch hardware
 54 * will powered off or on.
 
 55 */
 56static void cy8ctmg110_power(struct cy8ctmg110 *ts, bool poweron)
 57{
 58	if (ts->reset_pin)
 59		gpio_direction_output(ts->reset_pin, 1 - poweron);
 60}
 61
 62static int cy8ctmg110_write_regs(struct cy8ctmg110 *tsc, unsigned char reg,
 63		unsigned char len, unsigned char *value)
 64{
 65	struct i2c_client *client = tsc->client;
 66	int ret;
 67	unsigned char i2c_data[6];
 68
 69	BUG_ON(len > 5);
 70
 71	i2c_data[0] = reg;
 72	memcpy(i2c_data + 1, value, len);
 73
 74	ret = i2c_master_send(client, i2c_data, len + 1);
 75	if (ret != len + 1) {
 76		dev_err(&client->dev, "i2c write data cmd failed\n");
 77		return ret < 0 ? ret : -EIO;
 78	}
 79
 80	return 0;
 81}
 82
 83static int cy8ctmg110_read_regs(struct cy8ctmg110 *tsc,
 84		unsigned char *data, unsigned char len, unsigned char cmd)
 85{
 86	struct i2c_client *client = tsc->client;
 87	int ret;
 88	struct i2c_msg msg[2] = {
 89		/* first write slave position to i2c devices */
 90		{
 91			.addr = client->addr,
 92			.len = 1,
 93			.buf = &cmd
 94		},
 95		/* Second read data from position */
 96		{
 97			.addr = client->addr,
 98			.flags = I2C_M_RD,
 99			.len = len,
100			.buf = data
101		}
102	};
103
104	ret = i2c_transfer(client->adapter, msg, 2);
105	if (ret < 0)
106		return ret;
107
108	return 0;
109}
110
111static int cy8ctmg110_touch_pos(struct cy8ctmg110 *tsc)
112{
113	struct input_dev *input = tsc->input;
114	unsigned char reg_p[CY8CTMG110_REG_MAX];
115	int x, y;
116
117	memset(reg_p, 0, CY8CTMG110_REG_MAX);
118
119	/* Reading coordinates */
120	if (cy8ctmg110_read_regs(tsc, reg_p, 9, CY8CTMG110_TOUCH_X1) != 0)
121		return -EIO;
122
123	y = reg_p[2] << 8 | reg_p[3];
124	x = reg_p[0] << 8 | reg_p[1];
125
126	/* Number of touch */
127	if (reg_p[8] == 0) {
128		input_report_key(input, BTN_TOUCH, 0);
129	} else  {
130		input_report_key(input, BTN_TOUCH, 1);
131		input_report_abs(input, ABS_X, x);
132		input_report_abs(input, ABS_Y, y);
 
 
133	}
134
135	input_sync(input);
136
137	return 0;
138}
139
140static int cy8ctmg110_set_sleepmode(struct cy8ctmg110 *ts, bool sleep)
141{
142	unsigned char reg_p[3];
143
144	if (sleep) {
145		reg_p[0] = 0x00;
146		reg_p[1] = 0xff;
147		reg_p[2] = 5;
148	} else {
149		reg_p[0] = 0x10;
150		reg_p[1] = 0xff;
151		reg_p[2] = 0;
152	}
153
154	return cy8ctmg110_write_regs(ts, CY8CTMG110_TOUCH_WAKEUP_TIME, 3, reg_p);
155}
156
157static irqreturn_t cy8ctmg110_irq_thread(int irq, void *dev_id)
158{
159	struct cy8ctmg110 *tsc = dev_id;
160
161	cy8ctmg110_touch_pos(tsc);
162
163	return IRQ_HANDLED;
164}
165
166static int cy8ctmg110_probe(struct i2c_client *client,
167					const struct i2c_device_id *id)
 
 
 
 
 
 
 
168{
169	const struct cy8ctmg110_pdata *pdata = dev_get_platdata(&client->dev);
170	struct cy8ctmg110 *ts;
171	struct input_dev *input_dev;
172	int err;
173
174	/* No pdata no way forward */
175	if (pdata == NULL) {
176		dev_err(&client->dev, "no pdata\n");
177		return -ENODEV;
178	}
179
180	if (!i2c_check_functionality(client->adapter,
181					I2C_FUNC_SMBUS_READ_WORD_DATA))
182		return -EIO;
183
184	ts = kzalloc(sizeof(struct cy8ctmg110), GFP_KERNEL);
185	input_dev = input_allocate_device();
186	if (!ts || !input_dev) {
187		err = -ENOMEM;
188		goto err_free_mem;
189	}
 
190
191	ts->client = client;
192	ts->input = input_dev;
193	ts->reset_pin = pdata->reset_pin;
194	ts->irq_pin = pdata->irq_pin;
195
196	snprintf(ts->phys, sizeof(ts->phys),
197		 "%s/input0", dev_name(&client->dev));
198
199	input_dev->name = CY8CTMG110_DRIVER_NAME " Touchscreen";
200	input_dev->phys = ts->phys;
201	input_dev->id.bustype = BUS_I2C;
202	input_dev->dev.parent = &client->dev;
203
204	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
205	input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
206
 
207	input_set_abs_params(input_dev, ABS_X,
208			CY8CTMG110_X_MIN, CY8CTMG110_X_MAX, 4, 0);
209	input_set_abs_params(input_dev, ABS_Y,
210			CY8CTMG110_Y_MIN, CY8CTMG110_Y_MAX, 4, 0);
211
212	if (ts->reset_pin) {
213		err = gpio_request(ts->reset_pin, NULL);
214		if (err) {
215			dev_err(&client->dev,
216				"Unable to request GPIO pin %d.\n",
217				ts->reset_pin);
218			goto err_free_mem;
219		}
220	}
221
222	cy8ctmg110_power(ts, true);
223	cy8ctmg110_set_sleepmode(ts, false);
224
225	err = gpio_request(ts->irq_pin, "touch_irq_key");
226	if (err < 0) {
227		dev_err(&client->dev,
228			"Failed to request GPIO %d, error %d\n",
229			ts->irq_pin, err);
230		goto err_shutoff_device;
231	}
232
233	err = gpio_direction_input(ts->irq_pin);
234	if (err < 0) {
235		dev_err(&client->dev,
236			"Failed to configure input direction for GPIO %d, error %d\n",
237			ts->irq_pin, err);
238		goto err_free_irq_gpio;
239	}
240
241	client->irq = gpio_to_irq(ts->irq_pin);
242	if (client->irq < 0) {
243		err = client->irq;
244		dev_err(&client->dev,
245			"Unable to get irq number for GPIO %d, error %d\n",
246			ts->irq_pin, err);
247		goto err_free_irq_gpio;
248	}
249
250	err = request_threaded_irq(client->irq, NULL, cy8ctmg110_irq_thread,
251				   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
252				   "touch_reset_key", ts);
253	if (err < 0) {
254		dev_err(&client->dev,
255			"irq %d busy? error %d\n", client->irq, err);
256		goto err_free_irq_gpio;
257	}
258
259	err = input_register_device(input_dev);
260	if (err)
261		goto err_free_irq;
262
263	i2c_set_clientdata(client, ts);
264	device_init_wakeup(&client->dev, 1);
265	return 0;
266
267err_free_irq:
268	free_irq(client->irq, ts);
269err_free_irq_gpio:
270	gpio_free(ts->irq_pin);
271err_shutoff_device:
272	cy8ctmg110_set_sleepmode(ts, true);
273	cy8ctmg110_power(ts, false);
274	if (ts->reset_pin)
275		gpio_free(ts->reset_pin);
276err_free_mem:
277	input_free_device(input_dev);
278	kfree(ts);
279	return err;
280}
281
282static int __maybe_unused cy8ctmg110_suspend(struct device *dev)
283{
284	struct i2c_client *client = to_i2c_client(dev);
285	struct cy8ctmg110 *ts = i2c_get_clientdata(client);
286
287	if (device_may_wakeup(&client->dev))
288		enable_irq_wake(client->irq);
289	else {
290		cy8ctmg110_set_sleepmode(ts, true);
291		cy8ctmg110_power(ts, false);
292	}
 
293	return 0;
294}
295
296static int __maybe_unused cy8ctmg110_resume(struct device *dev)
297{
298	struct i2c_client *client = to_i2c_client(dev);
299	struct cy8ctmg110 *ts = i2c_get_clientdata(client);
300
301	if (device_may_wakeup(&client->dev))
302		disable_irq_wake(client->irq);
303	else {
304		cy8ctmg110_power(ts, true);
305		cy8ctmg110_set_sleepmode(ts, false);
306	}
 
307	return 0;
308}
309
310static SIMPLE_DEV_PM_OPS(cy8ctmg110_pm, cy8ctmg110_suspend, cy8ctmg110_resume);
311
312static int cy8ctmg110_remove(struct i2c_client *client)
313{
314	struct cy8ctmg110 *ts = i2c_get_clientdata(client);
315
316	cy8ctmg110_set_sleepmode(ts, true);
317	cy8ctmg110_power(ts, false);
318
319	free_irq(client->irq, ts);
320	input_unregister_device(ts->input);
321	gpio_free(ts->irq_pin);
322	if (ts->reset_pin)
323		gpio_free(ts->reset_pin);
324	kfree(ts);
325
326	return 0;
327}
328
329static const struct i2c_device_id cy8ctmg110_idtable[] = {
330	{ CY8CTMG110_DRIVER_NAME, 1 },
331	{ }
332};
333
334MODULE_DEVICE_TABLE(i2c, cy8ctmg110_idtable);
335
336static struct i2c_driver cy8ctmg110_driver = {
337	.driver		= {
338		.name	= CY8CTMG110_DRIVER_NAME,
339		.pm	= &cy8ctmg110_pm,
340	},
341	.id_table	= cy8ctmg110_idtable,
342	.probe		= cy8ctmg110_probe,
343	.remove		= cy8ctmg110_remove,
344};
345
346module_i2c_driver(cy8ctmg110_driver);
347
348MODULE_AUTHOR("Samuli Konttila <samuli.konttila@aavamobile.com>");
349MODULE_DESCRIPTION("cy8ctmg110 TouchScreen Driver");
350MODULE_LICENSE("GPL v2");