xref: /openbmc/linux/drivers/input/touchscreen/max11801_ts.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
242033065SZhang Jiejing /*
342033065SZhang Jiejing  * Driver for MAXI MAX11801 - A Resistive touch screen controller with
442033065SZhang Jiejing  * i2c interface
542033065SZhang Jiejing  *
642033065SZhang Jiejing  * Copyright (C) 2011 Freescale Semiconductor, Inc.
742033065SZhang Jiejing  * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
842033065SZhang Jiejing  *
942033065SZhang Jiejing  * Based on mcs5000_ts.c
1042033065SZhang Jiejing  */
1142033065SZhang Jiejing 
1242033065SZhang Jiejing /*
1342033065SZhang Jiejing  * This driver aims to support the series of MAXI touch chips max11801
1442033065SZhang Jiejing  * through max11803. The main difference between these 4 chips can be
1542033065SZhang Jiejing  * found in the table below:
1642033065SZhang Jiejing  * -----------------------------------------------------
1742033065SZhang Jiejing  * | CHIP     |  AUTO MODE SUPPORT(FIFO) | INTERFACE    |
1842033065SZhang Jiejing  * |----------------------------------------------------|
1942033065SZhang Jiejing  * | max11800 |  YES                     |   SPI        |
2042033065SZhang Jiejing  * | max11801 |  YES                     |   I2C        |
2142033065SZhang Jiejing  * | max11802 |  NO                      |   SPI        |
2242033065SZhang Jiejing  * | max11803 |  NO                      |   I2C        |
2342033065SZhang Jiejing  * ------------------------------------------------------
2442033065SZhang Jiejing  *
2542033065SZhang Jiejing  * Currently, this driver only supports max11801.
2642033065SZhang Jiejing  *
2742033065SZhang Jiejing  * Data Sheet:
2842033065SZhang Jiejing  * http://www.maxim-ic.com/datasheet/index.mvp/id/5943
2942033065SZhang Jiejing  */
3042033065SZhang Jiejing 
3142033065SZhang Jiejing #include <linux/module.h>
3242033065SZhang Jiejing #include <linux/i2c.h>
3342033065SZhang Jiejing #include <linux/interrupt.h>
3442033065SZhang Jiejing #include <linux/input.h>
3542033065SZhang Jiejing #include <linux/slab.h>
3642033065SZhang Jiejing #include <linux/bitops.h>
3742033065SZhang Jiejing 
3842033065SZhang Jiejing /* Register Address define */
3942033065SZhang Jiejing #define GENERNAL_STATUS_REG		0x00
4042033065SZhang Jiejing #define GENERNAL_CONF_REG		0x01
4142033065SZhang Jiejing #define MESURE_RES_CONF_REG		0x02
4242033065SZhang Jiejing #define MESURE_AVER_CONF_REG		0x03
4342033065SZhang Jiejing #define ADC_SAMPLE_TIME_CONF_REG	0x04
4442033065SZhang Jiejing #define PANEL_SETUPTIME_CONF_REG	0x05
4542033065SZhang Jiejing #define DELAY_CONVERSION_CONF_REG	0x06
4642033065SZhang Jiejing #define TOUCH_DETECT_PULLUP_CONF_REG	0x07
4742033065SZhang Jiejing #define AUTO_MODE_TIME_CONF_REG		0x08 /* only for max11800/max11801 */
4842033065SZhang Jiejing #define APERTURE_CONF_REG		0x09 /* only for max11800/max11801 */
4942033065SZhang Jiejing #define AUX_MESURE_CONF_REG		0x0a
5042033065SZhang Jiejing #define OP_MODE_CONF_REG		0x0b
5142033065SZhang Jiejing 
5242033065SZhang Jiejing /* FIFO is found only in max11800 and max11801 */
5342033065SZhang Jiejing #define FIFO_RD_CMD			(0x50 << 1)
5442033065SZhang Jiejing #define MAX11801_FIFO_INT		(1 << 2)
5542033065SZhang Jiejing #define MAX11801_FIFO_OVERFLOW		(1 << 3)
5642033065SZhang Jiejing 
5742033065SZhang Jiejing #define XY_BUFSIZE			4
5842033065SZhang Jiejing #define XY_BUF_OFFSET			4
5942033065SZhang Jiejing 
6042033065SZhang Jiejing #define MAX11801_MAX_X			0xfff
6142033065SZhang Jiejing #define MAX11801_MAX_Y			0xfff
6242033065SZhang Jiejing 
6342033065SZhang Jiejing #define MEASURE_TAG_OFFSET		2
6442033065SZhang Jiejing #define MEASURE_TAG_MASK		(3 << MEASURE_TAG_OFFSET)
6542033065SZhang Jiejing #define EVENT_TAG_OFFSET		0
6642033065SZhang Jiejing #define EVENT_TAG_MASK			(3 << EVENT_TAG_OFFSET)
6742033065SZhang Jiejing #define MEASURE_X_TAG			(0 << MEASURE_TAG_OFFSET)
6842033065SZhang Jiejing #define MEASURE_Y_TAG			(1 << MEASURE_TAG_OFFSET)
6942033065SZhang Jiejing 
7042033065SZhang Jiejing /* These are the state of touch event state machine */
7142033065SZhang Jiejing enum {
7242033065SZhang Jiejing 	EVENT_INIT,
7342033065SZhang Jiejing 	EVENT_MIDDLE,
7442033065SZhang Jiejing 	EVENT_RELEASE,
7542033065SZhang Jiejing 	EVENT_FIFO_END
7642033065SZhang Jiejing };
7742033065SZhang Jiejing 
7842033065SZhang Jiejing struct max11801_data {
7942033065SZhang Jiejing 	struct i2c_client		*client;
8042033065SZhang Jiejing 	struct input_dev		*input_dev;
8142033065SZhang Jiejing };
8242033065SZhang Jiejing 
read_register(struct i2c_client * client,int addr)8342033065SZhang Jiejing static u8 read_register(struct i2c_client *client, int addr)
8442033065SZhang Jiejing {
8542033065SZhang Jiejing 	/* XXX: The chip ignores LSB of register address */
8642033065SZhang Jiejing 	return i2c_smbus_read_byte_data(client, addr << 1);
8742033065SZhang Jiejing }
8842033065SZhang Jiejing 
max11801_write_reg(struct i2c_client * client,int addr,int data)8942033065SZhang Jiejing static int max11801_write_reg(struct i2c_client *client, int addr, int data)
9042033065SZhang Jiejing {
9142033065SZhang Jiejing 	/* XXX: The chip ignores LSB of register address */
9242033065SZhang Jiejing 	return i2c_smbus_write_byte_data(client, addr << 1, data);
9342033065SZhang Jiejing }
9442033065SZhang Jiejing 
max11801_ts_interrupt(int irq,void * dev_id)9542033065SZhang Jiejing static irqreturn_t max11801_ts_interrupt(int irq, void *dev_id)
9642033065SZhang Jiejing {
9742033065SZhang Jiejing 	struct max11801_data *data = dev_id;
9842033065SZhang Jiejing 	struct i2c_client *client = data->client;
9942033065SZhang Jiejing 	int status, i, ret;
10042033065SZhang Jiejing 	u8 buf[XY_BUFSIZE];
10142033065SZhang Jiejing 	int x = -1;
10242033065SZhang Jiejing 	int y = -1;
10342033065SZhang Jiejing 
10442033065SZhang Jiejing 	status = read_register(data->client, GENERNAL_STATUS_REG);
10542033065SZhang Jiejing 
10642033065SZhang Jiejing 	if (status & (MAX11801_FIFO_INT | MAX11801_FIFO_OVERFLOW)) {
10742033065SZhang Jiejing 		status = read_register(data->client, GENERNAL_STATUS_REG);
10842033065SZhang Jiejing 
10942033065SZhang Jiejing 		ret = i2c_smbus_read_i2c_block_data(client, FIFO_RD_CMD,
11042033065SZhang Jiejing 						    XY_BUFSIZE, buf);
11142033065SZhang Jiejing 
11242033065SZhang Jiejing 		/*
11342033065SZhang Jiejing 		 * We should get 4 bytes buffer that contains X,Y
11442033065SZhang Jiejing 		 * and event tag
11542033065SZhang Jiejing 		 */
11642033065SZhang Jiejing 		if (ret < XY_BUFSIZE)
11742033065SZhang Jiejing 			goto out;
11842033065SZhang Jiejing 
11942033065SZhang Jiejing 		for (i = 0; i < XY_BUFSIZE; i += XY_BUFSIZE / 2) {
12042033065SZhang Jiejing 			if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_X_TAG)
12142033065SZhang Jiejing 				x = (buf[i] << XY_BUF_OFFSET) +
12242033065SZhang Jiejing 				    (buf[i + 1] >> XY_BUF_OFFSET);
12342033065SZhang Jiejing 			else if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_Y_TAG)
12442033065SZhang Jiejing 				y = (buf[i] << XY_BUF_OFFSET) +
12542033065SZhang Jiejing 				    (buf[i + 1] >> XY_BUF_OFFSET);
12642033065SZhang Jiejing 		}
12742033065SZhang Jiejing 
12842033065SZhang Jiejing 		if ((buf[1] & EVENT_TAG_MASK) != (buf[3] & EVENT_TAG_MASK))
12942033065SZhang Jiejing 			goto out;
13042033065SZhang Jiejing 
13142033065SZhang Jiejing 		switch (buf[1] & EVENT_TAG_MASK) {
13242033065SZhang Jiejing 		case EVENT_INIT:
13342033065SZhang Jiejing 		case EVENT_MIDDLE:
13442033065SZhang Jiejing 			input_report_abs(data->input_dev, ABS_X, x);
13542033065SZhang Jiejing 			input_report_abs(data->input_dev, ABS_Y, y);
13642033065SZhang Jiejing 			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 1);
13742033065SZhang Jiejing 			input_sync(data->input_dev);
13842033065SZhang Jiejing 			break;
13942033065SZhang Jiejing 
14042033065SZhang Jiejing 		case EVENT_RELEASE:
14142033065SZhang Jiejing 			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 0);
14242033065SZhang Jiejing 			input_sync(data->input_dev);
14342033065SZhang Jiejing 			break;
14442033065SZhang Jiejing 
14542033065SZhang Jiejing 		case EVENT_FIFO_END:
14642033065SZhang Jiejing 			break;
14742033065SZhang Jiejing 		}
14842033065SZhang Jiejing 	}
14942033065SZhang Jiejing out:
15042033065SZhang Jiejing 	return IRQ_HANDLED;
15142033065SZhang Jiejing }
15242033065SZhang Jiejing 
max11801_ts_phy_init(struct max11801_data * data)1535298cc4cSBill Pemberton static void max11801_ts_phy_init(struct max11801_data *data)
15442033065SZhang Jiejing {
15542033065SZhang Jiejing 	struct i2c_client *client = data->client;
15642033065SZhang Jiejing 
15742033065SZhang Jiejing 	/* Average X,Y, take 16 samples, average eight media sample */
15842033065SZhang Jiejing 	max11801_write_reg(client, MESURE_AVER_CONF_REG, 0xff);
15942033065SZhang Jiejing 	/* X,Y panel setup time set to 20us */
16042033065SZhang Jiejing 	max11801_write_reg(client, PANEL_SETUPTIME_CONF_REG, 0x11);
16142033065SZhang Jiejing 	/* Rough pullup time (2uS), Fine pullup time (10us)  */
16242033065SZhang Jiejing 	max11801_write_reg(client, TOUCH_DETECT_PULLUP_CONF_REG, 0x10);
16342033065SZhang Jiejing 	/* Auto mode init period = 5ms , scan period = 5ms*/
16442033065SZhang Jiejing 	max11801_write_reg(client, AUTO_MODE_TIME_CONF_REG, 0xaa);
16542033065SZhang Jiejing 	/* Aperture X,Y set to +- 4LSB */
16642033065SZhang Jiejing 	max11801_write_reg(client, APERTURE_CONF_REG, 0x33);
16742033065SZhang Jiejing 	/* Enable Power, enable Automode, enable Aperture, enable Average X,Y */
16842033065SZhang Jiejing 	max11801_write_reg(client, OP_MODE_CONF_REG, 0x36);
16942033065SZhang Jiejing }
17042033065SZhang Jiejing 
max11801_ts_probe(struct i2c_client * client)171edaa7aa9SUwe Kleine-König static int max11801_ts_probe(struct i2c_client *client)
17242033065SZhang Jiejing {
17342033065SZhang Jiejing 	struct max11801_data *data;
17442033065SZhang Jiejing 	struct input_dev *input_dev;
17542033065SZhang Jiejing 	int error;
17642033065SZhang Jiejing 
177d33c4aa3SFabio Estevam 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
178d33c4aa3SFabio Estevam 	input_dev = devm_input_allocate_device(&client->dev);
17942033065SZhang Jiejing 	if (!data || !input_dev) {
18042033065SZhang Jiejing 		dev_err(&client->dev, "Failed to allocate memory\n");
181d33c4aa3SFabio Estevam 		return -ENOMEM;
18242033065SZhang Jiejing 	}
18342033065SZhang Jiejing 
18442033065SZhang Jiejing 	data->client = client;
18542033065SZhang Jiejing 	data->input_dev = input_dev;
18642033065SZhang Jiejing 
18742033065SZhang Jiejing 	input_dev->name = "max11801_ts";
18842033065SZhang Jiejing 	input_dev->id.bustype = BUS_I2C;
18942033065SZhang Jiejing 	input_dev->dev.parent = &client->dev;
19042033065SZhang Jiejing 
19142033065SZhang Jiejing 	__set_bit(EV_ABS, input_dev->evbit);
19242033065SZhang Jiejing 	__set_bit(EV_KEY, input_dev->evbit);
19342033065SZhang Jiejing 	__set_bit(BTN_TOUCH, input_dev->keybit);
19442033065SZhang Jiejing 	input_set_abs_params(input_dev, ABS_X, 0, MAX11801_MAX_X, 0, 0);
19542033065SZhang Jiejing 	input_set_abs_params(input_dev, ABS_Y, 0, MAX11801_MAX_Y, 0, 0);
19642033065SZhang Jiejing 
19742033065SZhang Jiejing 	max11801_ts_phy_init(data);
19842033065SZhang Jiejing 
199d33c4aa3SFabio Estevam 	error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
200d33c4aa3SFabio Estevam 					  max11801_ts_interrupt,
20142033065SZhang Jiejing 					  IRQF_TRIGGER_LOW | IRQF_ONESHOT,
20242033065SZhang Jiejing 					  "max11801_ts", data);
20342033065SZhang Jiejing 	if (error) {
20442033065SZhang Jiejing 		dev_err(&client->dev, "Failed to register interrupt\n");
205d33c4aa3SFabio Estevam 		return error;
20642033065SZhang Jiejing 	}
20742033065SZhang Jiejing 
20842033065SZhang Jiejing 	error = input_register_device(data->input_dev);
20942033065SZhang Jiejing 	if (error)
210d33c4aa3SFabio Estevam 		return error;
21142033065SZhang Jiejing 
21242033065SZhang Jiejing 	return 0;
21342033065SZhang Jiejing }
21442033065SZhang Jiejing 
21542033065SZhang Jiejing static const struct i2c_device_id max11801_ts_id[] = {
21642033065SZhang Jiejing 	{"max11801", 0},
21742033065SZhang Jiejing 	{ }
21842033065SZhang Jiejing };
21942033065SZhang Jiejing MODULE_DEVICE_TABLE(i2c, max11801_ts_id);
22042033065SZhang Jiejing 
22100480324SJagan Teki static const struct of_device_id max11801_ts_dt_ids[] = {
22200480324SJagan Teki 	{ .compatible = "maxim,max11801" },
22300480324SJagan Teki 	{ /* sentinel */ }
22400480324SJagan Teki };
22500480324SJagan Teki MODULE_DEVICE_TABLE(of, max11801_ts_dt_ids);
22600480324SJagan Teki 
22742033065SZhang Jiejing static struct i2c_driver max11801_ts_driver = {
22842033065SZhang Jiejing 	.driver = {
22942033065SZhang Jiejing 		.name	= "max11801_ts",
23000480324SJagan Teki 		.of_match_table = max11801_ts_dt_ids,
23142033065SZhang Jiejing 	},
23242033065SZhang Jiejing 	.id_table	= max11801_ts_id,
233*d8bde56dSUwe Kleine-König 	.probe		= max11801_ts_probe,
23442033065SZhang Jiejing };
23542033065SZhang Jiejing 
2361b92c1cfSAxel Lin module_i2c_driver(max11801_ts_driver);
23742033065SZhang Jiejing 
23842033065SZhang Jiejing MODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
23942033065SZhang Jiejing MODULE_DESCRIPTION("Touchscreen driver for MAXI MAX11801 controller");
24042033065SZhang Jiejing MODULE_LICENSE("GPL");
241