18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Allwinner sunxi resistive touchscreen controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013 - 2014 Hans de Goede <hdegoede@redhat.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * The hwmon parts are based on work by Corentin LABBE which is:
88c2ecf20Sopenharmony_ci * Copyright (C) 2013 Corentin LABBE <clabbe.montjoie@gmail.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci * The sun4i-ts controller is capable of detecting a second touch, but when a
138c2ecf20Sopenharmony_ci * second touch is present then the accuracy becomes so bad the reported touch
148c2ecf20Sopenharmony_ci * location is not useable.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * The original android driver contains some complicated heuristics using the
178c2ecf20Sopenharmony_ci * aprox. distance between the 2 touches to see if the user is making a pinch
188c2ecf20Sopenharmony_ci * open / close movement, and then reports emulated multi-touch events around
198c2ecf20Sopenharmony_ci * the last touch coordinate (as the dual-touch coordinates are worthless).
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * These kinds of heuristics are just asking for trouble (and don't belong
228c2ecf20Sopenharmony_ci * in the kernel). So this driver offers straight forward, reliable single
238c2ecf20Sopenharmony_ci * touch functionality only.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * s.a. A20 User Manual "1.15 TP" (Documentation/arm/sunxi.rst)
268c2ecf20Sopenharmony_ci * (looks like the description in the A20 User Manual v1.3 is better
278c2ecf20Sopenharmony_ci * than the one in the A10 User Manual v.1.5)
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include <linux/err.h>
318c2ecf20Sopenharmony_ci#include <linux/hwmon.h>
328c2ecf20Sopenharmony_ci#include <linux/thermal.h>
338c2ecf20Sopenharmony_ci#include <linux/init.h>
348c2ecf20Sopenharmony_ci#include <linux/input.h>
358c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
368c2ecf20Sopenharmony_ci#include <linux/io.h>
378c2ecf20Sopenharmony_ci#include <linux/module.h>
388c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
398c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
408c2ecf20Sopenharmony_ci#include <linux/slab.h>
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define TP_CTRL0		0x00
438c2ecf20Sopenharmony_ci#define TP_CTRL1		0x04
448c2ecf20Sopenharmony_ci#define TP_CTRL2		0x08
458c2ecf20Sopenharmony_ci#define TP_CTRL3		0x0c
468c2ecf20Sopenharmony_ci#define TP_INT_FIFOC		0x10
478c2ecf20Sopenharmony_ci#define TP_INT_FIFOS		0x14
488c2ecf20Sopenharmony_ci#define TP_TPR			0x18
498c2ecf20Sopenharmony_ci#define TP_CDAT			0x1c
508c2ecf20Sopenharmony_ci#define TEMP_DATA		0x20
518c2ecf20Sopenharmony_ci#define TP_DATA			0x24
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* TP_CTRL0 bits */
548c2ecf20Sopenharmony_ci#define ADC_FIRST_DLY(x)	((x) << 24) /* 8 bits */
558c2ecf20Sopenharmony_ci#define ADC_FIRST_DLY_MODE(x)	((x) << 23)
568c2ecf20Sopenharmony_ci#define ADC_CLK_SEL(x)		((x) << 22)
578c2ecf20Sopenharmony_ci#define ADC_CLK_DIV(x)		((x) << 20) /* 3 bits */
588c2ecf20Sopenharmony_ci#define FS_DIV(x)		((x) << 16) /* 4 bits */
598c2ecf20Sopenharmony_ci#define T_ACQ(x)		((x) << 0) /* 16 bits */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* TP_CTRL1 bits */
628c2ecf20Sopenharmony_ci#define STYLUS_UP_DEBOUN(x)	((x) << 12) /* 8 bits */
638c2ecf20Sopenharmony_ci#define STYLUS_UP_DEBOUN_EN(x)	((x) << 9)
648c2ecf20Sopenharmony_ci#define TOUCH_PAN_CALI_EN(x)	((x) << 6)
658c2ecf20Sopenharmony_ci#define TP_DUAL_EN(x)		((x) << 5)
668c2ecf20Sopenharmony_ci#define TP_MODE_EN(x)		((x) << 4)
678c2ecf20Sopenharmony_ci#define TP_ADC_SELECT(x)	((x) << 3)
688c2ecf20Sopenharmony_ci#define ADC_CHAN_SELECT(x)	((x) << 0)  /* 3 bits */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* on sun6i, bits 3~6 are left shifted by 1 to 4~7 */
718c2ecf20Sopenharmony_ci#define SUN6I_TP_MODE_EN(x)	((x) << 5)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/* TP_CTRL2 bits */
748c2ecf20Sopenharmony_ci#define TP_SENSITIVE_ADJUST(x)	((x) << 28) /* 4 bits */
758c2ecf20Sopenharmony_ci#define TP_MODE_SELECT(x)	((x) << 26) /* 2 bits */
768c2ecf20Sopenharmony_ci#define PRE_MEA_EN(x)		((x) << 24)
778c2ecf20Sopenharmony_ci#define PRE_MEA_THRE_CNT(x)	((x) << 0) /* 24 bits */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* TP_CTRL3 bits */
808c2ecf20Sopenharmony_ci#define FILTER_EN(x)		((x) << 2)
818c2ecf20Sopenharmony_ci#define FILTER_TYPE(x)		((x) << 0)  /* 2 bits */
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* TP_INT_FIFOC irq and fifo mask / control bits */
848c2ecf20Sopenharmony_ci#define TEMP_IRQ_EN(x)		((x) << 18)
858c2ecf20Sopenharmony_ci#define OVERRUN_IRQ_EN(x)	((x) << 17)
868c2ecf20Sopenharmony_ci#define DATA_IRQ_EN(x)		((x) << 16)
878c2ecf20Sopenharmony_ci#define TP_DATA_XY_CHANGE(x)	((x) << 13)
888c2ecf20Sopenharmony_ci#define FIFO_TRIG(x)		((x) << 8)  /* 5 bits */
898c2ecf20Sopenharmony_ci#define DATA_DRQ_EN(x)		((x) << 7)
908c2ecf20Sopenharmony_ci#define FIFO_FLUSH(x)		((x) << 4)
918c2ecf20Sopenharmony_ci#define TP_UP_IRQ_EN(x)		((x) << 1)
928c2ecf20Sopenharmony_ci#define TP_DOWN_IRQ_EN(x)	((x) << 0)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* TP_INT_FIFOS irq and fifo status bits */
958c2ecf20Sopenharmony_ci#define TEMP_DATA_PENDING	BIT(18)
968c2ecf20Sopenharmony_ci#define FIFO_OVERRUN_PENDING	BIT(17)
978c2ecf20Sopenharmony_ci#define FIFO_DATA_PENDING	BIT(16)
988c2ecf20Sopenharmony_ci#define TP_IDLE_FLG		BIT(2)
998c2ecf20Sopenharmony_ci#define TP_UP_PENDING		BIT(1)
1008c2ecf20Sopenharmony_ci#define TP_DOWN_PENDING		BIT(0)
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* TP_TPR bits */
1038c2ecf20Sopenharmony_ci#define TEMP_ENABLE(x)		((x) << 16)
1048c2ecf20Sopenharmony_ci#define TEMP_PERIOD(x)		((x) << 0)  /* t = x * 256 * 16 / clkin */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistruct sun4i_ts_data {
1078c2ecf20Sopenharmony_ci	struct device *dev;
1088c2ecf20Sopenharmony_ci	struct input_dev *input;
1098c2ecf20Sopenharmony_ci	void __iomem *base;
1108c2ecf20Sopenharmony_ci	unsigned int irq;
1118c2ecf20Sopenharmony_ci	bool ignore_fifo_data;
1128c2ecf20Sopenharmony_ci	int temp_data;
1138c2ecf20Sopenharmony_ci	int temp_offset;
1148c2ecf20Sopenharmony_ci	int temp_step;
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void sun4i_ts_irq_handle_input(struct sun4i_ts_data *ts, u32 reg_val)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	u32 x, y;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	if (reg_val & FIFO_DATA_PENDING) {
1228c2ecf20Sopenharmony_ci		x = readl(ts->base + TP_DATA);
1238c2ecf20Sopenharmony_ci		y = readl(ts->base + TP_DATA);
1248c2ecf20Sopenharmony_ci		/* The 1st location reported after an up event is unreliable */
1258c2ecf20Sopenharmony_ci		if (!ts->ignore_fifo_data) {
1268c2ecf20Sopenharmony_ci			input_report_abs(ts->input, ABS_X, x);
1278c2ecf20Sopenharmony_ci			input_report_abs(ts->input, ABS_Y, y);
1288c2ecf20Sopenharmony_ci			/*
1298c2ecf20Sopenharmony_ci			 * The hardware has a separate down status bit, but
1308c2ecf20Sopenharmony_ci			 * that gets set before we get the first location,
1318c2ecf20Sopenharmony_ci			 * resulting in reporting a click on the old location.
1328c2ecf20Sopenharmony_ci			 */
1338c2ecf20Sopenharmony_ci			input_report_key(ts->input, BTN_TOUCH, 1);
1348c2ecf20Sopenharmony_ci			input_sync(ts->input);
1358c2ecf20Sopenharmony_ci		} else {
1368c2ecf20Sopenharmony_ci			ts->ignore_fifo_data = false;
1378c2ecf20Sopenharmony_ci		}
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	if (reg_val & TP_UP_PENDING) {
1418c2ecf20Sopenharmony_ci		ts->ignore_fifo_data = true;
1428c2ecf20Sopenharmony_ci		input_report_key(ts->input, BTN_TOUCH, 0);
1438c2ecf20Sopenharmony_ci		input_sync(ts->input);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic irqreturn_t sun4i_ts_irq(int irq, void *dev_id)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts = dev_id;
1508c2ecf20Sopenharmony_ci	u32 reg_val;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	reg_val  = readl(ts->base + TP_INT_FIFOS);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (reg_val & TEMP_DATA_PENDING)
1558c2ecf20Sopenharmony_ci		ts->temp_data = readl(ts->base + TEMP_DATA);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (ts->input)
1588c2ecf20Sopenharmony_ci		sun4i_ts_irq_handle_input(ts, reg_val);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	writel(reg_val, ts->base + TP_INT_FIFOS);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int sun4i_ts_open(struct input_dev *dev)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts = input_get_drvdata(dev);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* Flush, set trig level to 1, enable temp, data and up irqs */
1708c2ecf20Sopenharmony_ci	writel(TEMP_IRQ_EN(1) | DATA_IRQ_EN(1) | FIFO_TRIG(1) | FIFO_FLUSH(1) |
1718c2ecf20Sopenharmony_ci		TP_UP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return 0;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic void sun4i_ts_close(struct input_dev *dev)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts = input_get_drvdata(dev);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	/* Deactivate all input IRQs */
1818c2ecf20Sopenharmony_ci	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic int sun4i_get_temp(const struct sun4i_ts_data *ts, int *temp)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	/* No temp_data until the first irq */
1878c2ecf20Sopenharmony_ci	if (ts->temp_data == -1)
1888c2ecf20Sopenharmony_ci		return -EAGAIN;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	*temp = ts->temp_data * ts->temp_step - ts->temp_offset;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	return 0;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic int sun4i_get_tz_temp(void *data, int *temp)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	return sun4i_get_temp(data, temp);
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic const struct thermal_zone_of_device_ops sun4i_ts_tz_ops = {
2018c2ecf20Sopenharmony_ci	.get_temp = sun4i_get_tz_temp,
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
2058c2ecf20Sopenharmony_ci			 char *buf)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts = dev_get_drvdata(dev);
2088c2ecf20Sopenharmony_ci	int temp;
2098c2ecf20Sopenharmony_ci	int error;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	error = sun4i_get_temp(ts, &temp);
2128c2ecf20Sopenharmony_ci	if (error)
2138c2ecf20Sopenharmony_ci		return error;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", temp);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic ssize_t show_temp_label(struct device *dev,
2198c2ecf20Sopenharmony_ci			      struct device_attribute *devattr, char *buf)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	return sprintf(buf, "SoC temperature\n");
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
2258c2ecf20Sopenharmony_cistatic DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic struct attribute *sun4i_ts_attrs[] = {
2288c2ecf20Sopenharmony_ci	&dev_attr_temp1_input.attr,
2298c2ecf20Sopenharmony_ci	&dev_attr_temp1_label.attr,
2308c2ecf20Sopenharmony_ci	NULL
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(sun4i_ts);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int sun4i_ts_probe(struct platform_device *pdev)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts;
2378c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2388c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
2398c2ecf20Sopenharmony_ci	struct device *hwmon;
2408c2ecf20Sopenharmony_ci	struct thermal_zone_device *thermal;
2418c2ecf20Sopenharmony_ci	int error;
2428c2ecf20Sopenharmony_ci	u32 reg;
2438c2ecf20Sopenharmony_ci	bool ts_attached;
2448c2ecf20Sopenharmony_ci	u32 tp_sensitive_adjust = 15;
2458c2ecf20Sopenharmony_ci	u32 filter_type = 1;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	ts = devm_kzalloc(dev, sizeof(struct sun4i_ts_data), GFP_KERNEL);
2488c2ecf20Sopenharmony_ci	if (!ts)
2498c2ecf20Sopenharmony_ci		return -ENOMEM;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	ts->dev = dev;
2528c2ecf20Sopenharmony_ci	ts->ignore_fifo_data = true;
2538c2ecf20Sopenharmony_ci	ts->temp_data = -1;
2548c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts")) {
2558c2ecf20Sopenharmony_ci		/* Allwinner SDK has temperature (C) = (value / 6) - 271 */
2568c2ecf20Sopenharmony_ci		ts->temp_offset = 271000;
2578c2ecf20Sopenharmony_ci		ts->temp_step = 167;
2588c2ecf20Sopenharmony_ci	} else if (of_device_is_compatible(np, "allwinner,sun4i-a10-ts")) {
2598c2ecf20Sopenharmony_ci		/*
2608c2ecf20Sopenharmony_ci		 * The A10 temperature sensor has quite a wide spread, these
2618c2ecf20Sopenharmony_ci		 * parameters are based on the averaging of the calibration
2628c2ecf20Sopenharmony_ci		 * results of 4 completely different boards, with a spread of
2638c2ecf20Sopenharmony_ci		 * temp_step from 0.096 - 0.170 and temp_offset from 176 - 331.
2648c2ecf20Sopenharmony_ci		 */
2658c2ecf20Sopenharmony_ci		ts->temp_offset = 257000;
2668c2ecf20Sopenharmony_ci		ts->temp_step = 133;
2678c2ecf20Sopenharmony_ci	} else {
2688c2ecf20Sopenharmony_ci		/*
2698c2ecf20Sopenharmony_ci		 * The user manuals do not contain the formula for calculating
2708c2ecf20Sopenharmony_ci		 * the temperature. The formula used here is from the AXP209,
2718c2ecf20Sopenharmony_ci		 * which is designed by X-Powers, an affiliate of Allwinner:
2728c2ecf20Sopenharmony_ci		 *
2738c2ecf20Sopenharmony_ci		 *     temperature (C) = (value * 0.1) - 144.7
2748c2ecf20Sopenharmony_ci		 *
2758c2ecf20Sopenharmony_ci		 * Allwinner does not have any documentation whatsoever for
2768c2ecf20Sopenharmony_ci		 * this hardware. Moreover, it is claimed that the sensor
2778c2ecf20Sopenharmony_ci		 * is inaccurate and cannot work properly.
2788c2ecf20Sopenharmony_ci		 */
2798c2ecf20Sopenharmony_ci		ts->temp_offset = 144700;
2808c2ecf20Sopenharmony_ci		ts->temp_step = 100;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	ts_attached = of_property_read_bool(np, "allwinner,ts-attached");
2848c2ecf20Sopenharmony_ci	if (ts_attached) {
2858c2ecf20Sopenharmony_ci		ts->input = devm_input_allocate_device(dev);
2868c2ecf20Sopenharmony_ci		if (!ts->input)
2878c2ecf20Sopenharmony_ci			return -ENOMEM;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci		ts->input->name = pdev->name;
2908c2ecf20Sopenharmony_ci		ts->input->phys = "sun4i_ts/input0";
2918c2ecf20Sopenharmony_ci		ts->input->open = sun4i_ts_open;
2928c2ecf20Sopenharmony_ci		ts->input->close = sun4i_ts_close;
2938c2ecf20Sopenharmony_ci		ts->input->id.bustype = BUS_HOST;
2948c2ecf20Sopenharmony_ci		ts->input->id.vendor = 0x0001;
2958c2ecf20Sopenharmony_ci		ts->input->id.product = 0x0001;
2968c2ecf20Sopenharmony_ci		ts->input->id.version = 0x0100;
2978c2ecf20Sopenharmony_ci		ts->input->evbit[0] =  BIT(EV_SYN) | BIT(EV_KEY) | BIT(EV_ABS);
2988c2ecf20Sopenharmony_ci		__set_bit(BTN_TOUCH, ts->input->keybit);
2998c2ecf20Sopenharmony_ci		input_set_abs_params(ts->input, ABS_X, 0, 4095, 0, 0);
3008c2ecf20Sopenharmony_ci		input_set_abs_params(ts->input, ABS_Y, 0, 4095, 0, 0);
3018c2ecf20Sopenharmony_ci		input_set_drvdata(ts->input, ts);
3028c2ecf20Sopenharmony_ci	}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	ts->base = devm_platform_ioremap_resource(pdev, 0);
3058c2ecf20Sopenharmony_ci	if (IS_ERR(ts->base))
3068c2ecf20Sopenharmony_ci		return PTR_ERR(ts->base);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	ts->irq = platform_get_irq(pdev, 0);
3098c2ecf20Sopenharmony_ci	error = devm_request_irq(dev, ts->irq, sun4i_ts_irq, 0, "sun4i-ts", ts);
3108c2ecf20Sopenharmony_ci	if (error)
3118c2ecf20Sopenharmony_ci		return error;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	/*
3148c2ecf20Sopenharmony_ci	 * Select HOSC clk, clkin = clk / 6, adc samplefreq = clkin / 8192,
3158c2ecf20Sopenharmony_ci	 * t_acq = clkin / (16 * 64)
3168c2ecf20Sopenharmony_ci	 */
3178c2ecf20Sopenharmony_ci	writel(ADC_CLK_SEL(0) | ADC_CLK_DIV(2) | FS_DIV(7) | T_ACQ(63),
3188c2ecf20Sopenharmony_ci	       ts->base + TP_CTRL0);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/*
3218c2ecf20Sopenharmony_ci	 * tp_sensitive_adjust is an optional property
3228c2ecf20Sopenharmony_ci	 * tp_mode = 0 : only x and y coordinates, as we don't use dual touch
3238c2ecf20Sopenharmony_ci	 */
3248c2ecf20Sopenharmony_ci	of_property_read_u32(np, "allwinner,tp-sensitive-adjust",
3258c2ecf20Sopenharmony_ci			     &tp_sensitive_adjust);
3268c2ecf20Sopenharmony_ci	writel(TP_SENSITIVE_ADJUST(tp_sensitive_adjust) | TP_MODE_SELECT(0),
3278c2ecf20Sopenharmony_ci	       ts->base + TP_CTRL2);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/*
3308c2ecf20Sopenharmony_ci	 * Enable median and averaging filter, optional property for
3318c2ecf20Sopenharmony_ci	 * filter type.
3328c2ecf20Sopenharmony_ci	 */
3338c2ecf20Sopenharmony_ci	of_property_read_u32(np, "allwinner,filter-type", &filter_type);
3348c2ecf20Sopenharmony_ci	writel(FILTER_EN(1) | FILTER_TYPE(filter_type), ts->base + TP_CTRL3);
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* Enable temperature measurement, period 1953 (2 seconds) */
3378c2ecf20Sopenharmony_ci	writel(TEMP_ENABLE(1) | TEMP_PERIOD(1953), ts->base + TP_TPR);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/*
3408c2ecf20Sopenharmony_ci	 * Set stylus up debounce to aprox 10 ms, enable debounce, and
3418c2ecf20Sopenharmony_ci	 * finally enable tp mode.
3428c2ecf20Sopenharmony_ci	 */
3438c2ecf20Sopenharmony_ci	reg = STYLUS_UP_DEBOUN(5) | STYLUS_UP_DEBOUN_EN(1);
3448c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun6i-a31-ts"))
3458c2ecf20Sopenharmony_ci		reg |= SUN6I_TP_MODE_EN(1);
3468c2ecf20Sopenharmony_ci	else
3478c2ecf20Sopenharmony_ci		reg |= TP_MODE_EN(1);
3488c2ecf20Sopenharmony_ci	writel(reg, ts->base + TP_CTRL1);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	/*
3518c2ecf20Sopenharmony_ci	 * The thermal core does not register hwmon devices for DT-based
3528c2ecf20Sopenharmony_ci	 * thermal zone sensors, such as this one.
3538c2ecf20Sopenharmony_ci	 */
3548c2ecf20Sopenharmony_ci	hwmon = devm_hwmon_device_register_with_groups(ts->dev, "sun4i_ts",
3558c2ecf20Sopenharmony_ci						       ts, sun4i_ts_groups);
3568c2ecf20Sopenharmony_ci	if (IS_ERR(hwmon))
3578c2ecf20Sopenharmony_ci		return PTR_ERR(hwmon);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	thermal = devm_thermal_zone_of_sensor_register(ts->dev, 0, ts,
3608c2ecf20Sopenharmony_ci						       &sun4i_ts_tz_ops);
3618c2ecf20Sopenharmony_ci	if (IS_ERR(thermal))
3628c2ecf20Sopenharmony_ci		return PTR_ERR(thermal);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	writel(TEMP_IRQ_EN(1), ts->base + TP_INT_FIFOC);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	if (ts_attached) {
3678c2ecf20Sopenharmony_ci		error = input_register_device(ts->input);
3688c2ecf20Sopenharmony_ci		if (error) {
3698c2ecf20Sopenharmony_ci			writel(0, ts->base + TP_INT_FIFOC);
3708c2ecf20Sopenharmony_ci			return error;
3718c2ecf20Sopenharmony_ci		}
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, ts);
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic int sun4i_ts_remove(struct platform_device *pdev)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct sun4i_ts_data *ts = platform_get_drvdata(pdev);
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* Explicit unregister to avoid open/close changing the imask later */
3838c2ecf20Sopenharmony_ci	if (ts->input)
3848c2ecf20Sopenharmony_ci		input_unregister_device(ts->input);
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	/* Deactivate all IRQs */
3878c2ecf20Sopenharmony_ci	writel(0, ts->base + TP_INT_FIFOC);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic const struct of_device_id sun4i_ts_of_match[] = {
3938c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun4i-a10-ts", },
3948c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun5i-a13-ts", },
3958c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun6i-a31-ts", },
3968c2ecf20Sopenharmony_ci	{ /* sentinel */ }
3978c2ecf20Sopenharmony_ci};
3988c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sun4i_ts_of_match);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic struct platform_driver sun4i_ts_driver = {
4018c2ecf20Sopenharmony_ci	.driver = {
4028c2ecf20Sopenharmony_ci		.name	= "sun4i-ts",
4038c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(sun4i_ts_of_match),
4048c2ecf20Sopenharmony_ci	},
4058c2ecf20Sopenharmony_ci	.probe	= sun4i_ts_probe,
4068c2ecf20Sopenharmony_ci	.remove	= sun4i_ts_remove,
4078c2ecf20Sopenharmony_ci};
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cimodule_platform_driver(sun4i_ts_driver);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner sun4i resistive touchscreen controller driver");
4128c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
4138c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
414