PageRenderTime 37ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/input/touchscreen/max11801_ts.c

http://github.com/mirrors/linux
C | 242 lines | 161 code | 37 blank | 44 comment | 15 complexity | 8c18d34026a62836bbb6df3f706576e6 MD5 | raw file
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Driver for MAXI MAX11801 - A Resistive touch screen controller with
  4 * i2c interface
  5 *
  6 * Copyright (C) 2011 Freescale Semiconductor, Inc.
  7 * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
  8 *
  9 * Based on mcs5000_ts.c
 10 */
 11
 12/*
 13 * This driver aims to support the series of MAXI touch chips max11801
 14 * through max11803. The main difference between these 4 chips can be
 15 * found in the table below:
 16 * -----------------------------------------------------
 17 * | CHIP     |  AUTO MODE SUPPORT(FIFO) | INTERFACE    |
 18 * |----------------------------------------------------|
 19 * | max11800 |  YES                     |   SPI        |
 20 * | max11801 |  YES                     |   I2C        |
 21 * | max11802 |  NO                      |   SPI        |
 22 * | max11803 |  NO                      |   I2C        |
 23 * ------------------------------------------------------
 24 *
 25 * Currently, this driver only supports max11801.
 26 *
 27 * Data Sheet:
 28 * http://www.maxim-ic.com/datasheet/index.mvp/id/5943
 29 */
 30
 31#include <linux/module.h>
 32#include <linux/i2c.h>
 33#include <linux/interrupt.h>
 34#include <linux/input.h>
 35#include <linux/slab.h>
 36#include <linux/bitops.h>
 37
 38/* Register Address define */
 39#define GENERNAL_STATUS_REG		0x00
 40#define GENERNAL_CONF_REG		0x01
 41#define MESURE_RES_CONF_REG		0x02
 42#define MESURE_AVER_CONF_REG		0x03
 43#define ADC_SAMPLE_TIME_CONF_REG	0x04
 44#define PANEL_SETUPTIME_CONF_REG	0x05
 45#define DELAY_CONVERSION_CONF_REG	0x06
 46#define TOUCH_DETECT_PULLUP_CONF_REG	0x07
 47#define AUTO_MODE_TIME_CONF_REG		0x08 /* only for max11800/max11801 */
 48#define APERTURE_CONF_REG		0x09 /* only for max11800/max11801 */
 49#define AUX_MESURE_CONF_REG		0x0a
 50#define OP_MODE_CONF_REG		0x0b
 51
 52/* FIFO is found only in max11800 and max11801 */
 53#define FIFO_RD_CMD			(0x50 << 1)
 54#define MAX11801_FIFO_INT		(1 << 2)
 55#define MAX11801_FIFO_OVERFLOW		(1 << 3)
 56
 57#define XY_BUFSIZE			4
 58#define XY_BUF_OFFSET			4
 59
 60#define MAX11801_MAX_X			0xfff
 61#define MAX11801_MAX_Y			0xfff
 62
 63#define MEASURE_TAG_OFFSET		2
 64#define MEASURE_TAG_MASK		(3 << MEASURE_TAG_OFFSET)
 65#define EVENT_TAG_OFFSET		0
 66#define EVENT_TAG_MASK			(3 << EVENT_TAG_OFFSET)
 67#define MEASURE_X_TAG			(0 << MEASURE_TAG_OFFSET)
 68#define MEASURE_Y_TAG			(1 << MEASURE_TAG_OFFSET)
 69
 70/* These are the state of touch event state machine */
 71enum {
 72	EVENT_INIT,
 73	EVENT_MIDDLE,
 74	EVENT_RELEASE,
 75	EVENT_FIFO_END
 76};
 77
 78struct max11801_data {
 79	struct i2c_client		*client;
 80	struct input_dev		*input_dev;
 81};
 82
 83static u8 read_register(struct i2c_client *client, int addr)
 84{
 85	/* XXX: The chip ignores LSB of register address */
 86	return i2c_smbus_read_byte_data(client, addr << 1);
 87}
 88
 89static int max11801_write_reg(struct i2c_client *client, int addr, int data)
 90{
 91	/* XXX: The chip ignores LSB of register address */
 92	return i2c_smbus_write_byte_data(client, addr << 1, data);
 93}
 94
 95static irqreturn_t max11801_ts_interrupt(int irq, void *dev_id)
 96{
 97	struct max11801_data *data = dev_id;
 98	struct i2c_client *client = data->client;
 99	int status, i, ret;
100	u8 buf[XY_BUFSIZE];
101	int x = -1;
102	int y = -1;
103
104	status = read_register(data->client, GENERNAL_STATUS_REG);
105
106	if (status & (MAX11801_FIFO_INT | MAX11801_FIFO_OVERFLOW)) {
107		status = read_register(data->client, GENERNAL_STATUS_REG);
108
109		ret = i2c_smbus_read_i2c_block_data(client, FIFO_RD_CMD,
110						    XY_BUFSIZE, buf);
111
112		/*
113		 * We should get 4 bytes buffer that contains X,Y
114		 * and event tag
115		 */
116		if (ret < XY_BUFSIZE)
117			goto out;
118
119		for (i = 0; i < XY_BUFSIZE; i += XY_BUFSIZE / 2) {
120			if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_X_TAG)
121				x = (buf[i] << XY_BUF_OFFSET) +
122				    (buf[i + 1] >> XY_BUF_OFFSET);
123			else if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_Y_TAG)
124				y = (buf[i] << XY_BUF_OFFSET) +
125				    (buf[i + 1] >> XY_BUF_OFFSET);
126		}
127
128		if ((buf[1] & EVENT_TAG_MASK) != (buf[3] & EVENT_TAG_MASK))
129			goto out;
130
131		switch (buf[1] & EVENT_TAG_MASK) {
132		case EVENT_INIT:
133			/* fall through */
134		case EVENT_MIDDLE:
135			input_report_abs(data->input_dev, ABS_X, x);
136			input_report_abs(data->input_dev, ABS_Y, y);
137			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 1);
138			input_sync(data->input_dev);
139			break;
140
141		case EVENT_RELEASE:
142			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 0);
143			input_sync(data->input_dev);
144			break;
145
146		case EVENT_FIFO_END:
147			break;
148		}
149	}
150out:
151	return IRQ_HANDLED;
152}
153
154static void max11801_ts_phy_init(struct max11801_data *data)
155{
156	struct i2c_client *client = data->client;
157
158	/* Average X,Y, take 16 samples, average eight media sample */
159	max11801_write_reg(client, MESURE_AVER_CONF_REG, 0xff);
160	/* X,Y panel setup time set to 20us */
161	max11801_write_reg(client, PANEL_SETUPTIME_CONF_REG, 0x11);
162	/* Rough pullup time (2uS), Fine pullup time (10us)  */
163	max11801_write_reg(client, TOUCH_DETECT_PULLUP_CONF_REG, 0x10);
164	/* Auto mode init period = 5ms , scan period = 5ms*/
165	max11801_write_reg(client, AUTO_MODE_TIME_CONF_REG, 0xaa);
166	/* Aperture X,Y set to +- 4LSB */
167	max11801_write_reg(client, APERTURE_CONF_REG, 0x33);
168	/* Enable Power, enable Automode, enable Aperture, enable Average X,Y */
169	max11801_write_reg(client, OP_MODE_CONF_REG, 0x36);
170}
171
172static int max11801_ts_probe(struct i2c_client *client,
173				       const struct i2c_device_id *id)
174{
175	struct max11801_data *data;
176	struct input_dev *input_dev;
177	int error;
178
179	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
180	input_dev = devm_input_allocate_device(&client->dev);
181	if (!data || !input_dev) {
182		dev_err(&client->dev, "Failed to allocate memory\n");
183		return -ENOMEM;
184	}
185
186	data->client = client;
187	data->input_dev = input_dev;
188
189	input_dev->name = "max11801_ts";
190	input_dev->id.bustype = BUS_I2C;
191	input_dev->dev.parent = &client->dev;
192
193	__set_bit(EV_ABS, input_dev->evbit);
194	__set_bit(EV_KEY, input_dev->evbit);
195	__set_bit(BTN_TOUCH, input_dev->keybit);
196	input_set_abs_params(input_dev, ABS_X, 0, MAX11801_MAX_X, 0, 0);
197	input_set_abs_params(input_dev, ABS_Y, 0, MAX11801_MAX_Y, 0, 0);
198
199	max11801_ts_phy_init(data);
200
201	error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
202					  max11801_ts_interrupt,
203					  IRQF_TRIGGER_LOW | IRQF_ONESHOT,
204					  "max11801_ts", data);
205	if (error) {
206		dev_err(&client->dev, "Failed to register interrupt\n");
207		return error;
208	}
209
210	error = input_register_device(data->input_dev);
211	if (error)
212		return error;
213
214	return 0;
215}
216
217static const struct i2c_device_id max11801_ts_id[] = {
218	{"max11801", 0},
219	{ }
220};
221MODULE_DEVICE_TABLE(i2c, max11801_ts_id);
222
223static const struct of_device_id max11801_ts_dt_ids[] = {
224	{ .compatible = "maxim,max11801" },
225	{ /* sentinel */ }
226};
227MODULE_DEVICE_TABLE(of, max11801_ts_dt_ids);
228
229static struct i2c_driver max11801_ts_driver = {
230	.driver = {
231		.name	= "max11801_ts",
232		.of_match_table = max11801_ts_dt_ids,
233	},
234	.id_table	= max11801_ts_id,
235	.probe		= max11801_ts_probe,
236};
237
238module_i2c_driver(max11801_ts_driver);
239
240MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
241MODULE_DESCRIPTION("Touchscreen driver for MAXI MAX11801 controller");
242MODULE_LICENSE("GPL");