1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * mcs5000_ts.c - Touchscreen driver for MELFAS MCS-5000 controller
4  *
5  * Copyright (C) 2009 Samsung Electronics Co.Ltd
6  * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7  *
8  * Based on wm97xx-core.c
9  */
10 
11 #include <linux/module.h>
12 #include <linux/i2c.h>
13 #include <linux/interrupt.h>
14 #include <linux/input.h>
15 #include <linux/irq.h>
16 #include <linux/platform_data/mcs.h>
17 #include <linux/slab.h>
18 
19 /* Registers */
20 #define MCS5000_TS_STATUS		0x00
21 #define STATUS_OFFSET			0
22 #define STATUS_NO			(0 << STATUS_OFFSET)
23 #define STATUS_INIT			(1 << STATUS_OFFSET)
24 #define STATUS_SENSING			(2 << STATUS_OFFSET)
25 #define STATUS_COORD			(3 << STATUS_OFFSET)
26 #define STATUS_GESTURE			(4 << STATUS_OFFSET)
27 #define ERROR_OFFSET			4
28 #define ERROR_NO			(0 << ERROR_OFFSET)
29 #define ERROR_POWER_ON_RESET		(1 << ERROR_OFFSET)
30 #define ERROR_INT_RESET			(2 << ERROR_OFFSET)
31 #define ERROR_EXT_RESET			(3 << ERROR_OFFSET)
32 #define ERROR_INVALID_REG_ADDRESS	(8 << ERROR_OFFSET)
33 #define ERROR_INVALID_REG_VALUE		(9 << ERROR_OFFSET)
34 
35 #define MCS5000_TS_OP_MODE		0x01
36 #define RESET_OFFSET			0
37 #define RESET_NO			(0 << RESET_OFFSET)
38 #define RESET_EXT_SOFT			(1 << RESET_OFFSET)
39 #define OP_MODE_OFFSET			1
40 #define OP_MODE_SLEEP			(0 << OP_MODE_OFFSET)
41 #define OP_MODE_ACTIVE			(1 << OP_MODE_OFFSET)
42 #define GESTURE_OFFSET			4
43 #define GESTURE_DISABLE			(0 << GESTURE_OFFSET)
44 #define GESTURE_ENABLE			(1 << GESTURE_OFFSET)
45 #define PROXIMITY_OFFSET		5
46 #define PROXIMITY_DISABLE		(0 << PROXIMITY_OFFSET)
47 #define PROXIMITY_ENABLE		(1 << PROXIMITY_OFFSET)
48 #define SCAN_MODE_OFFSET		6
49 #define SCAN_MODE_INTERRUPT		(0 << SCAN_MODE_OFFSET)
50 #define SCAN_MODE_POLLING		(1 << SCAN_MODE_OFFSET)
51 #define REPORT_RATE_OFFSET		7
52 #define REPORT_RATE_40			(0 << REPORT_RATE_OFFSET)
53 #define REPORT_RATE_80			(1 << REPORT_RATE_OFFSET)
54 
55 #define MCS5000_TS_SENS_CTL		0x02
56 #define MCS5000_TS_FILTER_CTL		0x03
57 #define PRI_FILTER_OFFSET		0
58 #define SEC_FILTER_OFFSET		4
59 
60 #define MCS5000_TS_X_SIZE_UPPER		0x08
61 #define MCS5000_TS_X_SIZE_LOWER		0x09
62 #define MCS5000_TS_Y_SIZE_UPPER		0x0A
63 #define MCS5000_TS_Y_SIZE_LOWER		0x0B
64 
65 #define MCS5000_TS_INPUT_INFO		0x10
66 #define INPUT_TYPE_OFFSET		0
67 #define INPUT_TYPE_NONTOUCH		(0 << INPUT_TYPE_OFFSET)
68 #define INPUT_TYPE_SINGLE		(1 << INPUT_TYPE_OFFSET)
69 #define INPUT_TYPE_DUAL			(2 << INPUT_TYPE_OFFSET)
70 #define INPUT_TYPE_PALM			(3 << INPUT_TYPE_OFFSET)
71 #define INPUT_TYPE_PROXIMITY		(7 << INPUT_TYPE_OFFSET)
72 #define GESTURE_CODE_OFFSET		3
73 #define GESTURE_CODE_NO			(0 << GESTURE_CODE_OFFSET)
74 
75 #define MCS5000_TS_X_POS_UPPER		0x11
76 #define MCS5000_TS_X_POS_LOWER		0x12
77 #define MCS5000_TS_Y_POS_UPPER		0x13
78 #define MCS5000_TS_Y_POS_LOWER		0x14
79 #define MCS5000_TS_Z_POS		0x15
80 #define MCS5000_TS_WIDTH		0x16
81 #define MCS5000_TS_GESTURE_VAL		0x17
82 #define MCS5000_TS_MODULE_REV		0x20
83 #define MCS5000_TS_FIRMWARE_VER		0x21
84 
85 /* Touchscreen absolute values */
86 #define MCS5000_MAX_XC			0x3ff
87 #define MCS5000_MAX_YC			0x3ff
88 
89 enum mcs5000_ts_read_offset {
90 	READ_INPUT_INFO,
91 	READ_X_POS_UPPER,
92 	READ_X_POS_LOWER,
93 	READ_Y_POS_UPPER,
94 	READ_Y_POS_LOWER,
95 	READ_BLOCK_SIZE,
96 };
97 
98 /* Each client has this additional data */
99 struct mcs5000_ts_data {
100 	struct i2c_client *client;
101 	struct input_dev *input_dev;
102 	const struct mcs_platform_data *platform_data;
103 };
104 
105 static irqreturn_t mcs5000_ts_interrupt(int irq, void *dev_id)
106 {
107 	struct mcs5000_ts_data *data = dev_id;
108 	struct i2c_client *client = data->client;
109 	u8 buffer[READ_BLOCK_SIZE];
110 	int err;
111 	int x;
112 	int y;
113 
114 	err = i2c_smbus_read_i2c_block_data(client, MCS5000_TS_INPUT_INFO,
115 			READ_BLOCK_SIZE, buffer);
116 	if (err < 0) {
117 		dev_err(&client->dev, "%s, err[%d]\n", __func__, err);
118 		goto out;
119 	}
120 
121 	switch (buffer[READ_INPUT_INFO]) {
122 	case INPUT_TYPE_NONTOUCH:
123 		input_report_key(data->input_dev, BTN_TOUCH, 0);
124 		input_sync(data->input_dev);
125 		break;
126 
127 	case INPUT_TYPE_SINGLE:
128 		x = (buffer[READ_X_POS_UPPER] << 8) | buffer[READ_X_POS_LOWER];
129 		y = (buffer[READ_Y_POS_UPPER] << 8) | buffer[READ_Y_POS_LOWER];
130 
131 		input_report_key(data->input_dev, BTN_TOUCH, 1);
132 		input_report_abs(data->input_dev, ABS_X, x);
133 		input_report_abs(data->input_dev, ABS_Y, y);
134 		input_sync(data->input_dev);
135 		break;
136 
137 	case INPUT_TYPE_DUAL:
138 		/* TODO */
139 		break;
140 
141 	case INPUT_TYPE_PALM:
142 		/* TODO */
143 		break;
144 
145 	case INPUT_TYPE_PROXIMITY:
146 		/* TODO */
147 		break;
148 
149 	default:
150 		dev_err(&client->dev, "Unknown ts input type %d\n",
151 				buffer[READ_INPUT_INFO]);
152 		break;
153 	}
154 
155  out:
156 	return IRQ_HANDLED;
157 }
158 
159 static void mcs5000_ts_phys_init(struct mcs5000_ts_data *data,
160 				 const struct mcs_platform_data *platform_data)
161 {
162 	struct i2c_client *client = data->client;
163 
164 	/* Touch reset & sleep mode */
165 	i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE,
166 			RESET_EXT_SOFT | OP_MODE_SLEEP);
167 
168 	/* Touch size */
169 	i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_UPPER,
170 			platform_data->x_size >> 8);
171 	i2c_smbus_write_byte_data(client, MCS5000_TS_X_SIZE_LOWER,
172 			platform_data->x_size & 0xff);
173 	i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_UPPER,
174 			platform_data->y_size >> 8);
175 	i2c_smbus_write_byte_data(client, MCS5000_TS_Y_SIZE_LOWER,
176 			platform_data->y_size & 0xff);
177 
178 	/* Touch active mode & 80 report rate */
179 	i2c_smbus_write_byte_data(data->client, MCS5000_TS_OP_MODE,
180 			OP_MODE_ACTIVE | REPORT_RATE_80);
181 }
182 
183 static int mcs5000_ts_probe(struct i2c_client *client)
184 {
185 	const struct mcs_platform_data *pdata;
186 	struct mcs5000_ts_data *data;
187 	struct input_dev *input_dev;
188 	int error;
189 
190 	pdata = dev_get_platdata(&client->dev);
191 	if (!pdata)
192 		return -EINVAL;
193 
194 	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
195 	if (!data) {
196 		dev_err(&client->dev, "Failed to allocate memory\n");
197 		return -ENOMEM;
198 	}
199 
200 	data->client = client;
201 
202 	input_dev = devm_input_allocate_device(&client->dev);
203 	if (!input_dev) {
204 		dev_err(&client->dev, "Failed to allocate input device\n");
205 		return -ENOMEM;
206 	}
207 
208 	input_dev->name = "MELFAS MCS-5000 Touchscreen";
209 	input_dev->id.bustype = BUS_I2C;
210 	input_dev->dev.parent = &client->dev;
211 
212 	__set_bit(EV_ABS, input_dev->evbit);
213 	__set_bit(EV_KEY, input_dev->evbit);
214 	__set_bit(BTN_TOUCH, input_dev->keybit);
215 	input_set_abs_params(input_dev, ABS_X, 0, MCS5000_MAX_XC, 0, 0);
216 	input_set_abs_params(input_dev, ABS_Y, 0, MCS5000_MAX_YC, 0, 0);
217 
218 	data->input_dev = input_dev;
219 
220 	if (pdata->cfg_pin)
221 		pdata->cfg_pin();
222 
223 	error = devm_request_threaded_irq(&client->dev, client->irq,
224 					  NULL, mcs5000_ts_interrupt,
225 					  IRQF_TRIGGER_LOW | IRQF_ONESHOT,
226 					  "mcs5000_ts", data);
227 	if (error) {
228 		dev_err(&client->dev, "Failed to register interrupt\n");
229 		return error;
230 	}
231 
232 	error = input_register_device(data->input_dev);
233 	if (error) {
234 		dev_err(&client->dev, "Failed to register input device\n");
235 		return error;
236 	}
237 
238 	mcs5000_ts_phys_init(data, pdata);
239 	i2c_set_clientdata(client, data);
240 
241 	return 0;
242 }
243 
244 static int __maybe_unused mcs5000_ts_suspend(struct device *dev)
245 {
246 	struct i2c_client *client = to_i2c_client(dev);
247 
248 	/* Touch sleep mode */
249 	i2c_smbus_write_byte_data(client, MCS5000_TS_OP_MODE, OP_MODE_SLEEP);
250 
251 	return 0;
252 }
253 
254 static int __maybe_unused mcs5000_ts_resume(struct device *dev)
255 {
256 	struct i2c_client *client = to_i2c_client(dev);
257 	struct mcs5000_ts_data *data = i2c_get_clientdata(client);
258 	const struct mcs_platform_data *pdata = dev_get_platdata(dev);
259 
260 	mcs5000_ts_phys_init(data, pdata);
261 
262 	return 0;
263 }
264 
265 static SIMPLE_DEV_PM_OPS(mcs5000_ts_pm, mcs5000_ts_suspend, mcs5000_ts_resume);
266 
267 static const struct i2c_device_id mcs5000_ts_id[] = {
268 	{ "mcs5000_ts", 0 },
269 	{ }
270 };
271 MODULE_DEVICE_TABLE(i2c, mcs5000_ts_id);
272 
273 static struct i2c_driver mcs5000_ts_driver = {
274 	.probe_new	= mcs5000_ts_probe,
275 	.driver = {
276 		.name = "mcs5000_ts",
277 		.pm   = &mcs5000_ts_pm,
278 	},
279 	.id_table	= mcs5000_ts_id,
280 };
281 
282 module_i2c_driver(mcs5000_ts_driver);
283 
284 /* Module information */
285 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
286 MODULE_DESCRIPTION("Touchscreen driver for MELFAS MCS-5000 controller");
287 MODULE_LICENSE("GPL");
288