Linux Audio

Check our new training course

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