18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for MAXI MAX11801 - A Resistive touch screen controller with
48c2ecf20Sopenharmony_ci * i2c interface
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Freescale Semiconductor, Inc.
78c2ecf20Sopenharmony_ci * Author: Zhang Jiejing <jiejing.zhang@freescale.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Based on mcs5000_ts.c
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * This driver aims to support the series of MAXI touch chips max11801
148c2ecf20Sopenharmony_ci * through max11803. The main difference between these 4 chips can be
158c2ecf20Sopenharmony_ci * found in the table below:
168c2ecf20Sopenharmony_ci * -----------------------------------------------------
178c2ecf20Sopenharmony_ci * | CHIP     |  AUTO MODE SUPPORT(FIFO) | INTERFACE    |
188c2ecf20Sopenharmony_ci * |----------------------------------------------------|
198c2ecf20Sopenharmony_ci * | max11800 |  YES                     |   SPI        |
208c2ecf20Sopenharmony_ci * | max11801 |  YES                     |   I2C        |
218c2ecf20Sopenharmony_ci * | max11802 |  NO                      |   SPI        |
228c2ecf20Sopenharmony_ci * | max11803 |  NO                      |   I2C        |
238c2ecf20Sopenharmony_ci * ------------------------------------------------------
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Currently, this driver only supports max11801.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Data Sheet:
288c2ecf20Sopenharmony_ci * http://www.maxim-ic.com/datasheet/index.mvp/id/5943
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/module.h>
328c2ecf20Sopenharmony_ci#include <linux/i2c.h>
338c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
348c2ecf20Sopenharmony_ci#include <linux/input.h>
358c2ecf20Sopenharmony_ci#include <linux/slab.h>
368c2ecf20Sopenharmony_ci#include <linux/bitops.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/* Register Address define */
398c2ecf20Sopenharmony_ci#define GENERNAL_STATUS_REG		0x00
408c2ecf20Sopenharmony_ci#define GENERNAL_CONF_REG		0x01
418c2ecf20Sopenharmony_ci#define MESURE_RES_CONF_REG		0x02
428c2ecf20Sopenharmony_ci#define MESURE_AVER_CONF_REG		0x03
438c2ecf20Sopenharmony_ci#define ADC_SAMPLE_TIME_CONF_REG	0x04
448c2ecf20Sopenharmony_ci#define PANEL_SETUPTIME_CONF_REG	0x05
458c2ecf20Sopenharmony_ci#define DELAY_CONVERSION_CONF_REG	0x06
468c2ecf20Sopenharmony_ci#define TOUCH_DETECT_PULLUP_CONF_REG	0x07
478c2ecf20Sopenharmony_ci#define AUTO_MODE_TIME_CONF_REG		0x08 /* only for max11800/max11801 */
488c2ecf20Sopenharmony_ci#define APERTURE_CONF_REG		0x09 /* only for max11800/max11801 */
498c2ecf20Sopenharmony_ci#define AUX_MESURE_CONF_REG		0x0a
508c2ecf20Sopenharmony_ci#define OP_MODE_CONF_REG		0x0b
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/* FIFO is found only in max11800 and max11801 */
538c2ecf20Sopenharmony_ci#define FIFO_RD_CMD			(0x50 << 1)
548c2ecf20Sopenharmony_ci#define MAX11801_FIFO_INT		(1 << 2)
558c2ecf20Sopenharmony_ci#define MAX11801_FIFO_OVERFLOW		(1 << 3)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define XY_BUFSIZE			4
588c2ecf20Sopenharmony_ci#define XY_BUF_OFFSET			4
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define MAX11801_MAX_X			0xfff
618c2ecf20Sopenharmony_ci#define MAX11801_MAX_Y			0xfff
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define MEASURE_TAG_OFFSET		2
648c2ecf20Sopenharmony_ci#define MEASURE_TAG_MASK		(3 << MEASURE_TAG_OFFSET)
658c2ecf20Sopenharmony_ci#define EVENT_TAG_OFFSET		0
668c2ecf20Sopenharmony_ci#define EVENT_TAG_MASK			(3 << EVENT_TAG_OFFSET)
678c2ecf20Sopenharmony_ci#define MEASURE_X_TAG			(0 << MEASURE_TAG_OFFSET)
688c2ecf20Sopenharmony_ci#define MEASURE_Y_TAG			(1 << MEASURE_TAG_OFFSET)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* These are the state of touch event state machine */
718c2ecf20Sopenharmony_cienum {
728c2ecf20Sopenharmony_ci	EVENT_INIT,
738c2ecf20Sopenharmony_ci	EVENT_MIDDLE,
748c2ecf20Sopenharmony_ci	EVENT_RELEASE,
758c2ecf20Sopenharmony_ci	EVENT_FIFO_END
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistruct max11801_data {
798c2ecf20Sopenharmony_ci	struct i2c_client		*client;
808c2ecf20Sopenharmony_ci	struct input_dev		*input_dev;
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic u8 read_register(struct i2c_client *client, int addr)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	/* XXX: The chip ignores LSB of register address */
868c2ecf20Sopenharmony_ci	return i2c_smbus_read_byte_data(client, addr << 1);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int max11801_write_reg(struct i2c_client *client, int addr, int data)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	/* XXX: The chip ignores LSB of register address */
928c2ecf20Sopenharmony_ci	return i2c_smbus_write_byte_data(client, addr << 1, data);
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic irqreturn_t max11801_ts_interrupt(int irq, void *dev_id)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	struct max11801_data *data = dev_id;
988c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
998c2ecf20Sopenharmony_ci	int status, i, ret;
1008c2ecf20Sopenharmony_ci	u8 buf[XY_BUFSIZE];
1018c2ecf20Sopenharmony_ci	int x = -1;
1028c2ecf20Sopenharmony_ci	int y = -1;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	status = read_register(data->client, GENERNAL_STATUS_REG);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (status & (MAX11801_FIFO_INT | MAX11801_FIFO_OVERFLOW)) {
1078c2ecf20Sopenharmony_ci		status = read_register(data->client, GENERNAL_STATUS_REG);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		ret = i2c_smbus_read_i2c_block_data(client, FIFO_RD_CMD,
1108c2ecf20Sopenharmony_ci						    XY_BUFSIZE, buf);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci		/*
1138c2ecf20Sopenharmony_ci		 * We should get 4 bytes buffer that contains X,Y
1148c2ecf20Sopenharmony_ci		 * and event tag
1158c2ecf20Sopenharmony_ci		 */
1168c2ecf20Sopenharmony_ci		if (ret < XY_BUFSIZE)
1178c2ecf20Sopenharmony_ci			goto out;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		for (i = 0; i < XY_BUFSIZE; i += XY_BUFSIZE / 2) {
1208c2ecf20Sopenharmony_ci			if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_X_TAG)
1218c2ecf20Sopenharmony_ci				x = (buf[i] << XY_BUF_OFFSET) +
1228c2ecf20Sopenharmony_ci				    (buf[i + 1] >> XY_BUF_OFFSET);
1238c2ecf20Sopenharmony_ci			else if ((buf[i + 1] & MEASURE_TAG_MASK) == MEASURE_Y_TAG)
1248c2ecf20Sopenharmony_ci				y = (buf[i] << XY_BUF_OFFSET) +
1258c2ecf20Sopenharmony_ci				    (buf[i + 1] >> XY_BUF_OFFSET);
1268c2ecf20Sopenharmony_ci		}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		if ((buf[1] & EVENT_TAG_MASK) != (buf[3] & EVENT_TAG_MASK))
1298c2ecf20Sopenharmony_ci			goto out;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		switch (buf[1] & EVENT_TAG_MASK) {
1328c2ecf20Sopenharmony_ci		case EVENT_INIT:
1338c2ecf20Sopenharmony_ci		case EVENT_MIDDLE:
1348c2ecf20Sopenharmony_ci			input_report_abs(data->input_dev, ABS_X, x);
1358c2ecf20Sopenharmony_ci			input_report_abs(data->input_dev, ABS_Y, y);
1368c2ecf20Sopenharmony_ci			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 1);
1378c2ecf20Sopenharmony_ci			input_sync(data->input_dev);
1388c2ecf20Sopenharmony_ci			break;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		case EVENT_RELEASE:
1418c2ecf20Sopenharmony_ci			input_event(data->input_dev, EV_KEY, BTN_TOUCH, 0);
1428c2ecf20Sopenharmony_ci			input_sync(data->input_dev);
1438c2ecf20Sopenharmony_ci			break;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		case EVENT_FIFO_END:
1468c2ecf20Sopenharmony_ci			break;
1478c2ecf20Sopenharmony_ci		}
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ciout:
1508c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic void max11801_ts_phy_init(struct max11801_data *data)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct i2c_client *client = data->client;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Average X,Y, take 16 samples, average eight media sample */
1588c2ecf20Sopenharmony_ci	max11801_write_reg(client, MESURE_AVER_CONF_REG, 0xff);
1598c2ecf20Sopenharmony_ci	/* X,Y panel setup time set to 20us */
1608c2ecf20Sopenharmony_ci	max11801_write_reg(client, PANEL_SETUPTIME_CONF_REG, 0x11);
1618c2ecf20Sopenharmony_ci	/* Rough pullup time (2uS), Fine pullup time (10us)  */
1628c2ecf20Sopenharmony_ci	max11801_write_reg(client, TOUCH_DETECT_PULLUP_CONF_REG, 0x10);
1638c2ecf20Sopenharmony_ci	/* Auto mode init period = 5ms , scan period = 5ms*/
1648c2ecf20Sopenharmony_ci	max11801_write_reg(client, AUTO_MODE_TIME_CONF_REG, 0xaa);
1658c2ecf20Sopenharmony_ci	/* Aperture X,Y set to +- 4LSB */
1668c2ecf20Sopenharmony_ci	max11801_write_reg(client, APERTURE_CONF_REG, 0x33);
1678c2ecf20Sopenharmony_ci	/* Enable Power, enable Automode, enable Aperture, enable Average X,Y */
1688c2ecf20Sopenharmony_ci	max11801_write_reg(client, OP_MODE_CONF_REG, 0x36);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic int max11801_ts_probe(struct i2c_client *client,
1728c2ecf20Sopenharmony_ci				       const struct i2c_device_id *id)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	struct max11801_data *data;
1758c2ecf20Sopenharmony_ci	struct input_dev *input_dev;
1768c2ecf20Sopenharmony_ci	int error;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1798c2ecf20Sopenharmony_ci	input_dev = devm_input_allocate_device(&client->dev);
1808c2ecf20Sopenharmony_ci	if (!data || !input_dev) {
1818c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to allocate memory\n");
1828c2ecf20Sopenharmony_ci		return -ENOMEM;
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	data->client = client;
1868c2ecf20Sopenharmony_ci	data->input_dev = input_dev;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	input_dev->name = "max11801_ts";
1898c2ecf20Sopenharmony_ci	input_dev->id.bustype = BUS_I2C;
1908c2ecf20Sopenharmony_ci	input_dev->dev.parent = &client->dev;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	__set_bit(EV_ABS, input_dev->evbit);
1938c2ecf20Sopenharmony_ci	__set_bit(EV_KEY, input_dev->evbit);
1948c2ecf20Sopenharmony_ci	__set_bit(BTN_TOUCH, input_dev->keybit);
1958c2ecf20Sopenharmony_ci	input_set_abs_params(input_dev, ABS_X, 0, MAX11801_MAX_X, 0, 0);
1968c2ecf20Sopenharmony_ci	input_set_abs_params(input_dev, ABS_Y, 0, MAX11801_MAX_Y, 0, 0);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	max11801_ts_phy_init(data);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
2018c2ecf20Sopenharmony_ci					  max11801_ts_interrupt,
2028c2ecf20Sopenharmony_ci					  IRQF_TRIGGER_LOW | IRQF_ONESHOT,
2038c2ecf20Sopenharmony_ci					  "max11801_ts", data);
2048c2ecf20Sopenharmony_ci	if (error) {
2058c2ecf20Sopenharmony_ci		dev_err(&client->dev, "Failed to register interrupt\n");
2068c2ecf20Sopenharmony_ci		return error;
2078c2ecf20Sopenharmony_ci	}
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	error = input_register_device(data->input_dev);
2108c2ecf20Sopenharmony_ci	if (error)
2118c2ecf20Sopenharmony_ci		return error;
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return 0;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic const struct i2c_device_id max11801_ts_id[] = {
2178c2ecf20Sopenharmony_ci	{"max11801", 0},
2188c2ecf20Sopenharmony_ci	{ }
2198c2ecf20Sopenharmony_ci};
2208c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max11801_ts_id);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic const struct of_device_id max11801_ts_dt_ids[] = {
2238c2ecf20Sopenharmony_ci	{ .compatible = "maxim,max11801" },
2248c2ecf20Sopenharmony_ci	{ /* sentinel */ }
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, max11801_ts_dt_ids);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_cistatic struct i2c_driver max11801_ts_driver = {
2298c2ecf20Sopenharmony_ci	.driver = {
2308c2ecf20Sopenharmony_ci		.name	= "max11801_ts",
2318c2ecf20Sopenharmony_ci		.of_match_table = max11801_ts_dt_ids,
2328c2ecf20Sopenharmony_ci	},
2338c2ecf20Sopenharmony_ci	.id_table	= max11801_ts_id,
2348c2ecf20Sopenharmony_ci	.probe		= max11801_ts_probe,
2358c2ecf20Sopenharmony_ci};
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cimodule_i2c_driver(max11801_ts_driver);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ciMODULE_AUTHOR("Zhang Jiejing <jiejing.zhang@freescale.com>");
2408c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Touchscreen driver for MAXI MAX11801 controller");
2418c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
242