1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rohm BU21029 touchscreen controller driver
4  *
5  * Copyright (C) 2015-2018 Bosch Sicherheitssysteme GmbH
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/input.h>
16 #include <linux/input/touchscreen.h>
17 #include <linux/interrupt.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/timer.h>
22 
23 /*
24  * HW_ID1 Register (PAGE=0, ADDR=0x0E, Reset value=0x02, Read only)
25  * +--------+--------+--------+--------+--------+--------+--------+--------+
26  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
27  * +--------+--------+--------+--------+--------+--------+--------+--------+
28  * |                                 HW_IDH                                |
29  * +--------+--------+--------+--------+--------+--------+--------+--------+
30  * HW_ID2 Register (PAGE=0, ADDR=0x0F, Reset value=0x29, Read only)
31  * +--------+--------+--------+--------+--------+--------+--------+--------+
32  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
33  * +--------+--------+--------+--------+--------+--------+--------+--------+
34  * |                                 HW_IDL                                |
35  * +--------+--------+--------+--------+--------+--------+--------+--------+
36  * HW_IDH: high 8bits of IC's ID
37  * HW_IDL: low  8bits of IC's ID
38  */
39 #define BU21029_HWID_REG	(0x0E << 3)
40 #define SUPPORTED_HWID		0x0229
41 
42 /*
43  * CFR0 Register (PAGE=0, ADDR=0x00, Reset value=0x20)
44  * +--------+--------+--------+--------+--------+--------+--------+--------+
45  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
46  * +--------+--------+--------+--------+--------+--------+--------+--------+
47  * |   0    |   0    |  CALIB |  INTRM |   0    |   0    |   0    |   0    |
48  * +--------+--------+--------+--------+--------+--------+--------+--------+
49  * CALIB: 0 = not to use calibration result (*)
50  *        1 = use calibration result
51  * INTRM: 0 = INT output depend on "pen down" (*)
52  *        1 = INT output always "0"
53  */
54 #define BU21029_CFR0_REG	(0x00 << 3)
55 #define CFR0_VALUE		0x00
56 
57 /*
58  * CFR1 Register (PAGE=0, ADDR=0x01, Reset value=0xA6)
59  * +--------+--------+--------+--------+--------+--------+--------+--------+
60  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
61  * +--------+--------+--------+--------+--------+--------+--------+--------+
62  * |  MAV   |         AVE[2:0]         |   0    |         SMPL[2:0]        |
63  * +--------+--------+--------+--------+--------+--------+--------+--------+
64  * MAV:  0 = median average filter off
65  *       1 = median average filter on (*)
66  * AVE:  AVE+1 = number of average samples for MAV,
67  *               if AVE>SMPL, then AVE=SMPL (=3)
68  * SMPL: SMPL+1 = number of conversion samples for MAV (=7)
69  */
70 #define BU21029_CFR1_REG	(0x01 << 3)
71 #define CFR1_VALUE		0xA6
72 
73 /*
74  * CFR2 Register (PAGE=0, ADDR=0x02, Reset value=0x04)
75  * +--------+--------+--------+--------+--------+--------+--------+--------+
76  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
77  * +--------+--------+--------+--------+--------+--------+--------+--------+
78  * |          INTVL_TIME[3:0]          |          TIME_ST_ADC[3:0]         |
79  * +--------+--------+--------+--------+--------+--------+--------+--------+
80  * INTVL_TIME: waiting time between completion of conversion
81  *             and start of next conversion, only usable in
82  *             autoscan mode (=20.480ms)
83  * TIME_ST_ADC: waiting time between application of voltage
84  *              to panel and start of A/D conversion (=100us)
85  */
86 #define BU21029_CFR2_REG	(0x02 << 3)
87 #define CFR2_VALUE		0xC9
88 
89 /*
90  * CFR3 Register (PAGE=0, ADDR=0x0B, Reset value=0x72)
91  * +--------+--------+--------+--------+--------+--------+--------+--------+
92  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
93  * +--------+--------+--------+--------+--------+--------+--------+--------+
94  * |  RM8   | STRETCH|  PU90K |  DUAL  |           PIDAC_OFS[3:0]          |
95  * +--------+--------+--------+--------+--------+--------+--------+--------+
96  * RM8: 0 = coordinate resolution is 12bit (*)
97  *      1 = coordinate resolution is 8bit
98  * STRETCH: 0 = SCL_STRETCH function off
99  *          1 = SCL_STRETCH function on (*)
100  * PU90K: 0 = internal pull-up resistance for touch detection is ~50kohms (*)
101  *        1 = internal pull-up resistance for touch detection is ~90kohms
102  * DUAL: 0 = dual touch detection off (*)
103  *       1 = dual touch detection on
104  * PIDAC_OFS: dual touch detection circuit adjustment, it is not necessary
105  *            to change this from initial value
106  */
107 #define BU21029_CFR3_REG	(0x0B << 3)
108 #define CFR3_VALUE		0x42
109 
110 /*
111  * LDO Register (PAGE=0, ADDR=0x0C, Reset value=0x00)
112  * +--------+--------+--------+--------+--------+--------+--------+--------+
113  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
114  * +--------+--------+--------+--------+--------+--------+--------+--------+
115  * |   0    |         PVDD[2:0]        |   0    |         AVDD[2:0]        |
116  * +--------+--------+--------+--------+--------+--------+--------+--------+
117  * PVDD: output voltage of panel output regulator (=2.000V)
118  * AVDD: output voltage of analog circuit regulator (=2.000V)
119  */
120 #define BU21029_LDO_REG		(0x0C << 3)
121 #define LDO_VALUE		0x77
122 
123 /*
124  * Serial Interface Command Byte 1 (CID=1)
125  * +--------+--------+--------+--------+--------+--------+--------+--------+
126  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
127  * +--------+--------+--------+--------+--------+--------+--------+--------+
128  * |   1    |                 CF                |  CMSK  |  PDM   |  STP   |
129  * +--------+--------+--------+--------+--------+--------+--------+--------+
130  * CF: conversion function, see table 3 in datasheet p6 (=0000, automatic scan)
131  * CMSK: 0 = executes convert function (*)
132  *       1 = reads the convert result
133  * PDM: 0 = power down after convert function stops (*)
134  *      1 = keep power on after convert function stops
135  * STP: 1 = abort current conversion and power down, set to "0" automatically
136  */
137 #define BU21029_AUTOSCAN	0x80
138 
139 /*
140  * The timeout value needs to be larger than INTVL_TIME + tConv4 (sample and
141  * conversion time), where tConv4 is calculated by formula:
142  * tPON + tDLY1 + (tTIME_ST_ADC + (tADC * tSMPL) * 2 + tDLY2) * 3
143  * see figure 8 in datasheet p15 for details of each field.
144  */
145 #define PEN_UP_TIMEOUT_MS	50
146 
147 #define STOP_DELAY_MIN_US	50
148 #define STOP_DELAY_MAX_US	1000
149 #define START_DELAY_MS		2
150 #define BUF_LEN			8
151 #define SCALE_12BIT		(1 << 12)
152 #define MAX_12BIT		((1 << 12) - 1)
153 #define DRIVER_NAME		"bu21029"
154 
155 struct bu21029_ts_data {
156 	struct i2c_client		*client;
157 	struct input_dev		*in_dev;
158 	struct timer_list		timer;
159 	struct regulator		*vdd;
160 	struct gpio_desc		*reset_gpios;
161 	u32				x_plate_ohms;
162 	struct touchscreen_properties	prop;
163 };
164 
bu21029_touch_report(struct bu21029_ts_data * bu21029,const u8 * buf)165 static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
166 {
167 	u16 x, y, z1, z2;
168 	u32 rz;
169 	s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
170 
171 	/*
172 	 * compose upper 8 and lower 4 bits into a 12bit value:
173 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
174 	 * |            ByteH              |            ByteL              |
175 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
176 	 * |b07|b06|b05|b04|b03|b02|b01|b00|b07|b06|b05|b04|b03|b02|b01|b00|
177 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
178 	 * |v11|v10|v09|v08|v07|v06|v05|v04|v03|v02|v01|v00| 0 | 0 | 0 | 0 |
179 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
180 	 */
181 	x  = (buf[0] << 4) | (buf[1] >> 4);
182 	y  = (buf[2] << 4) | (buf[3] >> 4);
183 	z1 = (buf[4] << 4) | (buf[5] >> 4);
184 	z2 = (buf[6] << 4) | (buf[7] >> 4);
185 
186 	if (z1 && z2) {
187 		/*
188 		 * calculate Rz (pressure resistance value) by equation:
189 		 * Rz = Rx * (x/Q) * ((z2/z1) - 1), where
190 		 * Rx is x-plate resistance,
191 		 * Q  is the touch screen resolution (8bit = 256, 12bit = 4096)
192 		 * x, z1, z2 are the measured positions.
193 		 */
194 		rz  = z2 - z1;
195 		rz *= x;
196 		rz *= bu21029->x_plate_ohms;
197 		rz /= z1;
198 		rz  = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
199 		if (rz <= max_pressure) {
200 			touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
201 					       x, y, false);
202 			input_report_abs(bu21029->in_dev, ABS_PRESSURE,
203 					 max_pressure - rz);
204 			input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
205 			input_sync(bu21029->in_dev);
206 		}
207 	}
208 }
209 
bu21029_touch_release(struct timer_list * t)210 static void bu21029_touch_release(struct timer_list *t)
211 {
212 	struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
213 
214 	input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
215 	input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
216 	input_sync(bu21029->in_dev);
217 }
218 
bu21029_touch_soft_irq(int irq,void * data)219 static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
220 {
221 	struct bu21029_ts_data *bu21029 = data;
222 	u8 buf[BUF_LEN];
223 	int error;
224 
225 	/*
226 	 * Read touch data and deassert interrupt (will assert again after
227 	 * INTVL_TIME + tConv4 for continuous touch)
228 	 */
229 	error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
230 					      sizeof(buf), buf);
231 	if (error < 0)
232 		goto out;
233 
234 	bu21029_touch_report(bu21029, buf);
235 
236 	/* reset timer for pen up detection */
237 	mod_timer(&bu21029->timer,
238 		  jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
239 
240 out:
241 	return IRQ_HANDLED;
242 }
243 
bu21029_put_chip_in_reset(struct bu21029_ts_data * bu21029)244 static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
245 {
246 	if (bu21029->reset_gpios) {
247 		gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
248 		usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
249 	}
250 }
251 
bu21029_start_chip(struct input_dev * dev)252 static int bu21029_start_chip(struct input_dev *dev)
253 {
254 	struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
255 	struct i2c_client *i2c = bu21029->client;
256 	struct {
257 		u8 reg;
258 		u8 value;
259 	} init_table[] = {
260 		{BU21029_CFR0_REG, CFR0_VALUE},
261 		{BU21029_CFR1_REG, CFR1_VALUE},
262 		{BU21029_CFR2_REG, CFR2_VALUE},
263 		{BU21029_CFR3_REG, CFR3_VALUE},
264 		{BU21029_LDO_REG,  LDO_VALUE}
265 	};
266 	int error, i;
267 	__be16 hwid;
268 
269 	error = regulator_enable(bu21029->vdd);
270 	if (error) {
271 		dev_err(&i2c->dev, "failed to power up chip: %d", error);
272 		return error;
273 	}
274 
275 	/* take chip out of reset */
276 	if (bu21029->reset_gpios) {
277 		gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
278 		msleep(START_DELAY_MS);
279 	}
280 
281 	error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
282 					      sizeof(hwid), (u8 *)&hwid);
283 	if (error < 0) {
284 		dev_err(&i2c->dev, "failed to read HW ID\n");
285 		goto err_out;
286 	}
287 
288 	if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
289 		dev_err(&i2c->dev,
290 			"unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
291 		error = -ENODEV;
292 		goto err_out;
293 	}
294 
295 	for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
296 		error = i2c_smbus_write_byte_data(i2c,
297 						  init_table[i].reg,
298 						  init_table[i].value);
299 		if (error < 0) {
300 			dev_err(&i2c->dev,
301 				"failed to write %#02x to register %#02x: %d\n",
302 				init_table[i].value, init_table[i].reg,
303 				error);
304 			goto err_out;
305 		}
306 	}
307 
308 	error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
309 	if (error < 0) {
310 		dev_err(&i2c->dev, "failed to start autoscan\n");
311 		goto err_out;
312 	}
313 
314 	enable_irq(bu21029->client->irq);
315 	return 0;
316 
317 err_out:
318 	bu21029_put_chip_in_reset(bu21029);
319 	regulator_disable(bu21029->vdd);
320 	return error;
321 }
322 
bu21029_stop_chip(struct input_dev * dev)323 static void bu21029_stop_chip(struct input_dev *dev)
324 {
325 	struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
326 
327 	disable_irq(bu21029->client->irq);
328 	del_timer_sync(&bu21029->timer);
329 
330 	bu21029_put_chip_in_reset(bu21029);
331 	regulator_disable(bu21029->vdd);
332 }
333 
bu21029_probe(struct i2c_client * client)334 static int bu21029_probe(struct i2c_client *client)
335 {
336 	struct bu21029_ts_data *bu21029;
337 	struct input_dev *in_dev;
338 	int error;
339 
340 	if (!i2c_check_functionality(client->adapter,
341 				     I2C_FUNC_SMBUS_WRITE_BYTE |
342 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
343 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
344 		dev_err(&client->dev,
345 			"i2c functionality support is not sufficient\n");
346 		return -EIO;
347 	}
348 
349 	bu21029 = devm_kzalloc(&client->dev, sizeof(*bu21029), GFP_KERNEL);
350 	if (!bu21029)
351 		return -ENOMEM;
352 
353 	error = device_property_read_u32(&client->dev, "rohm,x-plate-ohms",
354 					 &bu21029->x_plate_ohms);
355 	if (error) {
356 		dev_err(&client->dev,
357 			"invalid 'x-plate-ohms' supplied: %d\n", error);
358 		return error;
359 	}
360 
361 	bu21029->vdd = devm_regulator_get(&client->dev, "vdd");
362 	if (IS_ERR(bu21029->vdd)) {
363 		error = PTR_ERR(bu21029->vdd);
364 		if (error != -EPROBE_DEFER)
365 			dev_err(&client->dev,
366 				"failed to acquire 'vdd' supply: %d\n", error);
367 		return error;
368 	}
369 
370 	bu21029->reset_gpios = devm_gpiod_get_optional(&client->dev,
371 						       "reset", GPIOD_OUT_HIGH);
372 	if (IS_ERR(bu21029->reset_gpios)) {
373 		error = PTR_ERR(bu21029->reset_gpios);
374 		if (error != -EPROBE_DEFER)
375 			dev_err(&client->dev,
376 				"failed to acquire 'reset' gpio: %d\n", error);
377 		return error;
378 	}
379 
380 	in_dev = devm_input_allocate_device(&client->dev);
381 	if (!in_dev) {
382 		dev_err(&client->dev, "unable to allocate input device\n");
383 		return -ENOMEM;
384 	}
385 
386 	bu21029->client = client;
387 	bu21029->in_dev = in_dev;
388 	timer_setup(&bu21029->timer, bu21029_touch_release, 0);
389 
390 	in_dev->name		= DRIVER_NAME;
391 	in_dev->id.bustype	= BUS_I2C;
392 	in_dev->open		= bu21029_start_chip;
393 	in_dev->close		= bu21029_stop_chip;
394 
395 	input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
396 	input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
397 	input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
398 	input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
399 	touchscreen_parse_properties(in_dev, false, &bu21029->prop);
400 
401 	input_set_drvdata(in_dev, bu21029);
402 
403 	error = devm_request_threaded_irq(&client->dev, client->irq,
404 					  NULL, bu21029_touch_soft_irq,
405 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
406 					  DRIVER_NAME, bu21029);
407 	if (error) {
408 		dev_err(&client->dev,
409 			"unable to request touch irq: %d\n", error);
410 		return error;
411 	}
412 
413 	error = input_register_device(in_dev);
414 	if (error) {
415 		dev_err(&client->dev,
416 			"unable to register input device: %d\n", error);
417 		return error;
418 	}
419 
420 	i2c_set_clientdata(client, bu21029);
421 
422 	return 0;
423 }
424 
bu21029_suspend(struct device * dev)425 static int bu21029_suspend(struct device *dev)
426 {
427 	struct i2c_client *i2c = to_i2c_client(dev);
428 	struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
429 
430 	if (!device_may_wakeup(dev)) {
431 		mutex_lock(&bu21029->in_dev->mutex);
432 		if (input_device_enabled(bu21029->in_dev))
433 			bu21029_stop_chip(bu21029->in_dev);
434 		mutex_unlock(&bu21029->in_dev->mutex);
435 	}
436 
437 	return 0;
438 }
439 
bu21029_resume(struct device * dev)440 static int bu21029_resume(struct device *dev)
441 {
442 	struct i2c_client *i2c = to_i2c_client(dev);
443 	struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
444 
445 	if (!device_may_wakeup(dev)) {
446 		mutex_lock(&bu21029->in_dev->mutex);
447 		if (input_device_enabled(bu21029->in_dev))
448 			bu21029_start_chip(bu21029->in_dev);
449 		mutex_unlock(&bu21029->in_dev->mutex);
450 	}
451 
452 	return 0;
453 }
454 static DEFINE_SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
455 
456 static const struct i2c_device_id bu21029_ids[] = {
457 	{ DRIVER_NAME, 0 },
458 	{ /* sentinel */ }
459 };
460 MODULE_DEVICE_TABLE(i2c, bu21029_ids);
461 
462 #ifdef CONFIG_OF
463 static const struct of_device_id bu21029_of_ids[] = {
464 	{ .compatible = "rohm,bu21029" },
465 	{ /* sentinel */ }
466 };
467 MODULE_DEVICE_TABLE(of, bu21029_of_ids);
468 #endif
469 
470 static struct i2c_driver bu21029_driver = {
471 	.driver	= {
472 		.name		= DRIVER_NAME,
473 		.of_match_table	= of_match_ptr(bu21029_of_ids),
474 		.pm		= pm_sleep_ptr(&bu21029_pm_ops),
475 	},
476 	.id_table	= bu21029_ids,
477 	.probe_new	= bu21029_probe,
478 };
479 module_i2c_driver(bu21029_driver);
480 
481 MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
482 MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
483 MODULE_LICENSE("GPL v2");
484