xref: /openbmc/linux/drivers/input/touchscreen/bu21029_ts.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
13a658f23SZhu Yi // SPDX-License-Identifier: GPL-2.0
23a658f23SZhu Yi /*
33a658f23SZhu Yi  * Rohm BU21029 touchscreen controller driver
43a658f23SZhu Yi  *
53a658f23SZhu Yi  * Copyright (C) 2015-2018 Bosch Sicherheitssysteme GmbH
63a658f23SZhu Yi  *
73a658f23SZhu Yi  * This program is free software; you can redistribute it and/or modify
83a658f23SZhu Yi  * it under the terms of the GNU General Public License version 2 as
93a658f23SZhu Yi  * published by the Free Software Foundation.
103a658f23SZhu Yi  */
113a658f23SZhu Yi 
123a658f23SZhu Yi #include <linux/delay.h>
133a658f23SZhu Yi #include <linux/gpio/consumer.h>
143a658f23SZhu Yi #include <linux/i2c.h>
153a658f23SZhu Yi #include <linux/input.h>
163a658f23SZhu Yi #include <linux/input/touchscreen.h>
173a658f23SZhu Yi #include <linux/interrupt.h>
183a658f23SZhu Yi #include <linux/irq.h>
193a658f23SZhu Yi #include <linux/module.h>
203a658f23SZhu Yi #include <linux/regulator/consumer.h>
213a658f23SZhu Yi #include <linux/timer.h>
223a658f23SZhu Yi 
233a658f23SZhu Yi /*
243a658f23SZhu Yi  * HW_ID1 Register (PAGE=0, ADDR=0x0E, Reset value=0x02, Read only)
253a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
263a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
273a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
283a658f23SZhu Yi  * |                                 HW_IDH                                |
293a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
303a658f23SZhu Yi  * HW_ID2 Register (PAGE=0, ADDR=0x0F, Reset value=0x29, Read only)
313a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
323a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
333a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
343a658f23SZhu Yi  * |                                 HW_IDL                                |
353a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
363a658f23SZhu Yi  * HW_IDH: high 8bits of IC's ID
373a658f23SZhu Yi  * HW_IDL: low  8bits of IC's ID
383a658f23SZhu Yi  */
393a658f23SZhu Yi #define BU21029_HWID_REG	(0x0E << 3)
403a658f23SZhu Yi #define SUPPORTED_HWID		0x0229
413a658f23SZhu Yi 
423a658f23SZhu Yi /*
433a658f23SZhu Yi  * CFR0 Register (PAGE=0, ADDR=0x00, Reset value=0x20)
443a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
453a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
463a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
473a658f23SZhu Yi  * |   0    |   0    |  CALIB |  INTRM |   0    |   0    |   0    |   0    |
483a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
493a658f23SZhu Yi  * CALIB: 0 = not to use calibration result (*)
503a658f23SZhu Yi  *        1 = use calibration result
513a658f23SZhu Yi  * INTRM: 0 = INT output depend on "pen down" (*)
523a658f23SZhu Yi  *        1 = INT output always "0"
533a658f23SZhu Yi  */
543a658f23SZhu Yi #define BU21029_CFR0_REG	(0x00 << 3)
553a658f23SZhu Yi #define CFR0_VALUE		0x00
563a658f23SZhu Yi 
573a658f23SZhu Yi /*
583a658f23SZhu Yi  * CFR1 Register (PAGE=0, ADDR=0x01, Reset value=0xA6)
593a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
603a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
613a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
623a658f23SZhu Yi  * |  MAV   |         AVE[2:0]         |   0    |         SMPL[2:0]        |
633a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
643a658f23SZhu Yi  * MAV:  0 = median average filter off
653a658f23SZhu Yi  *       1 = median average filter on (*)
663a658f23SZhu Yi  * AVE:  AVE+1 = number of average samples for MAV,
673a658f23SZhu Yi  *               if AVE>SMPL, then AVE=SMPL (=3)
683a658f23SZhu Yi  * SMPL: SMPL+1 = number of conversion samples for MAV (=7)
693a658f23SZhu Yi  */
703a658f23SZhu Yi #define BU21029_CFR1_REG	(0x01 << 3)
713a658f23SZhu Yi #define CFR1_VALUE		0xA6
723a658f23SZhu Yi 
733a658f23SZhu Yi /*
743a658f23SZhu Yi  * CFR2 Register (PAGE=0, ADDR=0x02, Reset value=0x04)
753a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
763a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
773a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
783a658f23SZhu Yi  * |          INTVL_TIME[3:0]          |          TIME_ST_ADC[3:0]         |
793a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
803a658f23SZhu Yi  * INTVL_TIME: waiting time between completion of conversion
813a658f23SZhu Yi  *             and start of next conversion, only usable in
823a658f23SZhu Yi  *             autoscan mode (=20.480ms)
833a658f23SZhu Yi  * TIME_ST_ADC: waiting time between application of voltage
843a658f23SZhu Yi  *              to panel and start of A/D conversion (=100us)
853a658f23SZhu Yi  */
863a658f23SZhu Yi #define BU21029_CFR2_REG	(0x02 << 3)
873a658f23SZhu Yi #define CFR2_VALUE		0xC9
883a658f23SZhu Yi 
893a658f23SZhu Yi /*
903a658f23SZhu Yi  * CFR3 Register (PAGE=0, ADDR=0x0B, Reset value=0x72)
913a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
923a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
933a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
943a658f23SZhu Yi  * |  RM8   | STRETCH|  PU90K |  DUAL  |           PIDAC_OFS[3:0]          |
953a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
963a658f23SZhu Yi  * RM8: 0 = coordinate resolution is 12bit (*)
973a658f23SZhu Yi  *      1 = coordinate resolution is 8bit
983a658f23SZhu Yi  * STRETCH: 0 = SCL_STRETCH function off
993a658f23SZhu Yi  *          1 = SCL_STRETCH function on (*)
1003a658f23SZhu Yi  * PU90K: 0 = internal pull-up resistance for touch detection is ~50kohms (*)
1013a658f23SZhu Yi  *        1 = internal pull-up resistance for touch detection is ~90kohms
1023a658f23SZhu Yi  * DUAL: 0 = dual touch detection off (*)
1033a658f23SZhu Yi  *       1 = dual touch detection on
1043a658f23SZhu Yi  * PIDAC_OFS: dual touch detection circuit adjustment, it is not necessary
1053a658f23SZhu Yi  *            to change this from initial value
1063a658f23SZhu Yi  */
1073a658f23SZhu Yi #define BU21029_CFR3_REG	(0x0B << 3)
1083a658f23SZhu Yi #define CFR3_VALUE		0x42
1093a658f23SZhu Yi 
1103a658f23SZhu Yi /*
1113a658f23SZhu Yi  * LDO Register (PAGE=0, ADDR=0x0C, Reset value=0x00)
1123a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1133a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
1143a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1153a658f23SZhu Yi  * |   0    |         PVDD[2:0]        |   0    |         AVDD[2:0]        |
1163a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1173a658f23SZhu Yi  * PVDD: output voltage of panel output regulator (=2.000V)
1183a658f23SZhu Yi  * AVDD: output voltage of analog circuit regulator (=2.000V)
1193a658f23SZhu Yi  */
1203a658f23SZhu Yi #define BU21029_LDO_REG		(0x0C << 3)
1213a658f23SZhu Yi #define LDO_VALUE		0x77
1223a658f23SZhu Yi 
1233a658f23SZhu Yi /*
1243a658f23SZhu Yi  * Serial Interface Command Byte 1 (CID=1)
1253a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1263a658f23SZhu Yi  * |   D7   |   D6   |   D5   |   D4   |   D3   |   D2   |   D1   |   D0   |
1273a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1283a658f23SZhu Yi  * |   1    |                 CF                |  CMSK  |  PDM   |  STP   |
1293a658f23SZhu Yi  * +--------+--------+--------+--------+--------+--------+--------+--------+
1303a658f23SZhu Yi  * CF: conversion function, see table 3 in datasheet p6 (=0000, automatic scan)
1313a658f23SZhu Yi  * CMSK: 0 = executes convert function (*)
1323a658f23SZhu Yi  *       1 = reads the convert result
1333a658f23SZhu Yi  * PDM: 0 = power down after convert function stops (*)
1343a658f23SZhu Yi  *      1 = keep power on after convert function stops
1353a658f23SZhu Yi  * STP: 1 = abort current conversion and power down, set to "0" automatically
1363a658f23SZhu Yi  */
1373a658f23SZhu Yi #define BU21029_AUTOSCAN	0x80
1383a658f23SZhu Yi 
1393a658f23SZhu Yi /*
1403a658f23SZhu Yi  * The timeout value needs to be larger than INTVL_TIME + tConv4 (sample and
1413a658f23SZhu Yi  * conversion time), where tConv4 is calculated by formula:
1423a658f23SZhu Yi  * tPON + tDLY1 + (tTIME_ST_ADC + (tADC * tSMPL) * 2 + tDLY2) * 3
1433a658f23SZhu Yi  * see figure 8 in datasheet p15 for details of each field.
1443a658f23SZhu Yi  */
1453a658f23SZhu Yi #define PEN_UP_TIMEOUT_MS	50
1463a658f23SZhu Yi 
1473a658f23SZhu Yi #define STOP_DELAY_MIN_US	50
1483a658f23SZhu Yi #define STOP_DELAY_MAX_US	1000
1493a658f23SZhu Yi #define START_DELAY_MS		2
1503a658f23SZhu Yi #define BUF_LEN			8
1513a658f23SZhu Yi #define SCALE_12BIT		(1 << 12)
1523a658f23SZhu Yi #define MAX_12BIT		((1 << 12) - 1)
1533a658f23SZhu Yi #define DRIVER_NAME		"bu21029"
1543a658f23SZhu Yi 
1553a658f23SZhu Yi struct bu21029_ts_data {
1563a658f23SZhu Yi 	struct i2c_client		*client;
1573a658f23SZhu Yi 	struct input_dev		*in_dev;
1583a658f23SZhu Yi 	struct timer_list		timer;
1593a658f23SZhu Yi 	struct regulator		*vdd;
1603a658f23SZhu Yi 	struct gpio_desc		*reset_gpios;
1613a658f23SZhu Yi 	u32				x_plate_ohms;
1623a658f23SZhu Yi 	struct touchscreen_properties	prop;
1633a658f23SZhu Yi };
1643a658f23SZhu Yi 
bu21029_touch_report(struct bu21029_ts_data * bu21029,const u8 * buf)1653a658f23SZhu Yi static void bu21029_touch_report(struct bu21029_ts_data *bu21029, const u8 *buf)
1663a658f23SZhu Yi {
1673a658f23SZhu Yi 	u16 x, y, z1, z2;
1683a658f23SZhu Yi 	u32 rz;
1693a658f23SZhu Yi 	s32 max_pressure = input_abs_get_max(bu21029->in_dev, ABS_PRESSURE);
1703a658f23SZhu Yi 
1713a658f23SZhu Yi 	/*
1723a658f23SZhu Yi 	 * compose upper 8 and lower 4 bits into a 12bit value:
1733a658f23SZhu Yi 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1743a658f23SZhu Yi 	 * |            ByteH              |            ByteL              |
1753a658f23SZhu Yi 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1763a658f23SZhu Yi 	 * |b07|b06|b05|b04|b03|b02|b01|b00|b07|b06|b05|b04|b03|b02|b01|b00|
1773a658f23SZhu Yi 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1783a658f23SZhu Yi 	 * |v11|v10|v09|v08|v07|v06|v05|v04|v03|v02|v01|v00| 0 | 0 | 0 | 0 |
1793a658f23SZhu Yi 	 * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1803a658f23SZhu Yi 	 */
1813a658f23SZhu Yi 	x  = (buf[0] << 4) | (buf[1] >> 4);
1823a658f23SZhu Yi 	y  = (buf[2] << 4) | (buf[3] >> 4);
1833a658f23SZhu Yi 	z1 = (buf[4] << 4) | (buf[5] >> 4);
1843a658f23SZhu Yi 	z2 = (buf[6] << 4) | (buf[7] >> 4);
1853a658f23SZhu Yi 
1863a658f23SZhu Yi 	if (z1 && z2) {
1873a658f23SZhu Yi 		/*
1883a658f23SZhu Yi 		 * calculate Rz (pressure resistance value) by equation:
1893a658f23SZhu Yi 		 * Rz = Rx * (x/Q) * ((z2/z1) - 1), where
1903a658f23SZhu Yi 		 * Rx is x-plate resistance,
1913a658f23SZhu Yi 		 * Q  is the touch screen resolution (8bit = 256, 12bit = 4096)
1923a658f23SZhu Yi 		 * x, z1, z2 are the measured positions.
1933a658f23SZhu Yi 		 */
1943a658f23SZhu Yi 		rz  = z2 - z1;
1953a658f23SZhu Yi 		rz *= x;
1963a658f23SZhu Yi 		rz *= bu21029->x_plate_ohms;
1973a658f23SZhu Yi 		rz /= z1;
1983a658f23SZhu Yi 		rz  = DIV_ROUND_CLOSEST(rz, SCALE_12BIT);
1993a658f23SZhu Yi 		if (rz <= max_pressure) {
2003a658f23SZhu Yi 			touchscreen_report_pos(bu21029->in_dev, &bu21029->prop,
2013a658f23SZhu Yi 					       x, y, false);
2023a658f23SZhu Yi 			input_report_abs(bu21029->in_dev, ABS_PRESSURE,
2033a658f23SZhu Yi 					 max_pressure - rz);
2043a658f23SZhu Yi 			input_report_key(bu21029->in_dev, BTN_TOUCH, 1);
2053a658f23SZhu Yi 			input_sync(bu21029->in_dev);
2063a658f23SZhu Yi 		}
2073a658f23SZhu Yi 	}
2083a658f23SZhu Yi }
2093a658f23SZhu Yi 
bu21029_touch_release(struct timer_list * t)2103a658f23SZhu Yi static void bu21029_touch_release(struct timer_list *t)
2113a658f23SZhu Yi {
2123a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = from_timer(bu21029, t, timer);
2133a658f23SZhu Yi 
2143a658f23SZhu Yi 	input_report_abs(bu21029->in_dev, ABS_PRESSURE, 0);
2153a658f23SZhu Yi 	input_report_key(bu21029->in_dev, BTN_TOUCH, 0);
2163a658f23SZhu Yi 	input_sync(bu21029->in_dev);
2173a658f23SZhu Yi }
2183a658f23SZhu Yi 
bu21029_touch_soft_irq(int irq,void * data)2193a658f23SZhu Yi static irqreturn_t bu21029_touch_soft_irq(int irq, void *data)
2203a658f23SZhu Yi {
2213a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = data;
2223a658f23SZhu Yi 	u8 buf[BUF_LEN];
2233a658f23SZhu Yi 	int error;
2243a658f23SZhu Yi 
2253a658f23SZhu Yi 	/*
2263a658f23SZhu Yi 	 * Read touch data and deassert interrupt (will assert again after
2273a658f23SZhu Yi 	 * INTVL_TIME + tConv4 for continuous touch)
2283a658f23SZhu Yi 	 */
2293a658f23SZhu Yi 	error = i2c_smbus_read_i2c_block_data(bu21029->client, BU21029_AUTOSCAN,
2303a658f23SZhu Yi 					      sizeof(buf), buf);
2313a658f23SZhu Yi 	if (error < 0)
2323a658f23SZhu Yi 		goto out;
2333a658f23SZhu Yi 
2343a658f23SZhu Yi 	bu21029_touch_report(bu21029, buf);
2353a658f23SZhu Yi 
2363a658f23SZhu Yi 	/* reset timer for pen up detection */
2373a658f23SZhu Yi 	mod_timer(&bu21029->timer,
2383a658f23SZhu Yi 		  jiffies + msecs_to_jiffies(PEN_UP_TIMEOUT_MS));
2393a658f23SZhu Yi 
2403a658f23SZhu Yi out:
2413a658f23SZhu Yi 	return IRQ_HANDLED;
2423a658f23SZhu Yi }
2433a658f23SZhu Yi 
bu21029_put_chip_in_reset(struct bu21029_ts_data * bu21029)2443a658f23SZhu Yi static void bu21029_put_chip_in_reset(struct bu21029_ts_data *bu21029)
2453a658f23SZhu Yi {
2463a658f23SZhu Yi 	if (bu21029->reset_gpios) {
2473a658f23SZhu Yi 		gpiod_set_value_cansleep(bu21029->reset_gpios, 1);
2483a658f23SZhu Yi 		usleep_range(STOP_DELAY_MIN_US, STOP_DELAY_MAX_US);
2493a658f23SZhu Yi 	}
2503a658f23SZhu Yi }
2513a658f23SZhu Yi 
bu21029_start_chip(struct input_dev * dev)2523a658f23SZhu Yi static int bu21029_start_chip(struct input_dev *dev)
2533a658f23SZhu Yi {
2543a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
2553a658f23SZhu Yi 	struct i2c_client *i2c = bu21029->client;
2563a658f23SZhu Yi 	struct {
2573a658f23SZhu Yi 		u8 reg;
2583a658f23SZhu Yi 		u8 value;
2593a658f23SZhu Yi 	} init_table[] = {
2603a658f23SZhu Yi 		{BU21029_CFR0_REG, CFR0_VALUE},
2613a658f23SZhu Yi 		{BU21029_CFR1_REG, CFR1_VALUE},
2623a658f23SZhu Yi 		{BU21029_CFR2_REG, CFR2_VALUE},
2633a658f23SZhu Yi 		{BU21029_CFR3_REG, CFR3_VALUE},
2643a658f23SZhu Yi 		{BU21029_LDO_REG,  LDO_VALUE}
2653a658f23SZhu Yi 	};
2663a658f23SZhu Yi 	int error, i;
2673a658f23SZhu Yi 	__be16 hwid;
2683a658f23SZhu Yi 
2693a658f23SZhu Yi 	error = regulator_enable(bu21029->vdd);
2703a658f23SZhu Yi 	if (error) {
2713a658f23SZhu Yi 		dev_err(&i2c->dev, "failed to power up chip: %d", error);
2723a658f23SZhu Yi 		return error;
2733a658f23SZhu Yi 	}
2743a658f23SZhu Yi 
2753a658f23SZhu Yi 	/* take chip out of reset */
2763a658f23SZhu Yi 	if (bu21029->reset_gpios) {
2773a658f23SZhu Yi 		gpiod_set_value_cansleep(bu21029->reset_gpios, 0);
2783a658f23SZhu Yi 		msleep(START_DELAY_MS);
2793a658f23SZhu Yi 	}
2803a658f23SZhu Yi 
2813a658f23SZhu Yi 	error = i2c_smbus_read_i2c_block_data(i2c, BU21029_HWID_REG,
2823a658f23SZhu Yi 					      sizeof(hwid), (u8 *)&hwid);
2833a658f23SZhu Yi 	if (error < 0) {
2843a658f23SZhu Yi 		dev_err(&i2c->dev, "failed to read HW ID\n");
2853a658f23SZhu Yi 		goto err_out;
2863a658f23SZhu Yi 	}
2873a658f23SZhu Yi 
2883a658f23SZhu Yi 	if (be16_to_cpu(hwid) != SUPPORTED_HWID) {
2893a658f23SZhu Yi 		dev_err(&i2c->dev,
2903a658f23SZhu Yi 			"unsupported HW ID 0x%x\n", be16_to_cpu(hwid));
2913a658f23SZhu Yi 		error = -ENODEV;
2923a658f23SZhu Yi 		goto err_out;
2933a658f23SZhu Yi 	}
2943a658f23SZhu Yi 
2953a658f23SZhu Yi 	for (i = 0; i < ARRAY_SIZE(init_table); ++i) {
2963a658f23SZhu Yi 		error = i2c_smbus_write_byte_data(i2c,
2973a658f23SZhu Yi 						  init_table[i].reg,
2983a658f23SZhu Yi 						  init_table[i].value);
2993a658f23SZhu Yi 		if (error < 0) {
3003a658f23SZhu Yi 			dev_err(&i2c->dev,
3013a658f23SZhu Yi 				"failed to write %#02x to register %#02x: %d\n",
3023a658f23SZhu Yi 				init_table[i].value, init_table[i].reg,
3033a658f23SZhu Yi 				error);
3043a658f23SZhu Yi 			goto err_out;
3053a658f23SZhu Yi 		}
3063a658f23SZhu Yi 	}
3073a658f23SZhu Yi 
3083a658f23SZhu Yi 	error = i2c_smbus_write_byte(i2c, BU21029_AUTOSCAN);
3093a658f23SZhu Yi 	if (error < 0) {
3103a658f23SZhu Yi 		dev_err(&i2c->dev, "failed to start autoscan\n");
3113a658f23SZhu Yi 		goto err_out;
3123a658f23SZhu Yi 	}
3133a658f23SZhu Yi 
3143a658f23SZhu Yi 	enable_irq(bu21029->client->irq);
3153a658f23SZhu Yi 	return 0;
3163a658f23SZhu Yi 
3173a658f23SZhu Yi err_out:
3183a658f23SZhu Yi 	bu21029_put_chip_in_reset(bu21029);
3193a658f23SZhu Yi 	regulator_disable(bu21029->vdd);
3203a658f23SZhu Yi 	return error;
3213a658f23SZhu Yi }
3223a658f23SZhu Yi 
bu21029_stop_chip(struct input_dev * dev)3233a658f23SZhu Yi static void bu21029_stop_chip(struct input_dev *dev)
3243a658f23SZhu Yi {
3253a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = input_get_drvdata(dev);
3263a658f23SZhu Yi 
3273a658f23SZhu Yi 	disable_irq(bu21029->client->irq);
3283a658f23SZhu Yi 	del_timer_sync(&bu21029->timer);
3293a658f23SZhu Yi 
3303a658f23SZhu Yi 	bu21029_put_chip_in_reset(bu21029);
3313a658f23SZhu Yi 	regulator_disable(bu21029->vdd);
3323a658f23SZhu Yi }
3333a658f23SZhu Yi 
bu21029_probe(struct i2c_client * client)334dbb690c1SUwe Kleine-König static int bu21029_probe(struct i2c_client *client)
3353a658f23SZhu Yi {
336*59a51ba6SKrzysztof Kozlowski 	struct device *dev = &client->dev;
3373a658f23SZhu Yi 	struct bu21029_ts_data *bu21029;
3383a658f23SZhu Yi 	struct input_dev *in_dev;
3393a658f23SZhu Yi 	int error;
3403a658f23SZhu Yi 
3413a658f23SZhu Yi 	if (!i2c_check_functionality(client->adapter,
3423a658f23SZhu Yi 				     I2C_FUNC_SMBUS_WRITE_BYTE |
3433a658f23SZhu Yi 				     I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
3443a658f23SZhu Yi 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
345*59a51ba6SKrzysztof Kozlowski 		dev_err(dev, "i2c functionality support is not sufficient\n");
3463a658f23SZhu Yi 		return -EIO;
3473a658f23SZhu Yi 	}
3483a658f23SZhu Yi 
349*59a51ba6SKrzysztof Kozlowski 	bu21029 = devm_kzalloc(dev, sizeof(*bu21029), GFP_KERNEL);
3503a658f23SZhu Yi 	if (!bu21029)
3513a658f23SZhu Yi 		return -ENOMEM;
3523a658f23SZhu Yi 
353*59a51ba6SKrzysztof Kozlowski 	error = device_property_read_u32(dev, "rohm,x-plate-ohms", &bu21029->x_plate_ohms);
3543a658f23SZhu Yi 	if (error) {
355*59a51ba6SKrzysztof Kozlowski 		dev_err(dev, "invalid 'x-plate-ohms' supplied: %d\n", error);
3563a658f23SZhu Yi 		return error;
3573a658f23SZhu Yi 	}
3583a658f23SZhu Yi 
359*59a51ba6SKrzysztof Kozlowski 	bu21029->vdd = devm_regulator_get(dev, "vdd");
36064eb0f74SKrzysztof Kozlowski 	if (IS_ERR(bu21029->vdd))
361*59a51ba6SKrzysztof Kozlowski 		return dev_err_probe(dev, PTR_ERR(bu21029->vdd),
36264eb0f74SKrzysztof Kozlowski 				     "failed to acquire 'vdd' supply\n");
3633a658f23SZhu Yi 
364*59a51ba6SKrzysztof Kozlowski 	bu21029->reset_gpios = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
36564eb0f74SKrzysztof Kozlowski 	if (IS_ERR(bu21029->reset_gpios))
366*59a51ba6SKrzysztof Kozlowski 		return dev_err_probe(dev, PTR_ERR(bu21029->reset_gpios),
36764eb0f74SKrzysztof Kozlowski 				     "failed to acquire 'reset' gpio\n");
3683a658f23SZhu Yi 
369*59a51ba6SKrzysztof Kozlowski 	in_dev = devm_input_allocate_device(dev);
3703a658f23SZhu Yi 	if (!in_dev) {
371*59a51ba6SKrzysztof Kozlowski 		dev_err(dev, "unable to allocate input device\n");
3723a658f23SZhu Yi 		return -ENOMEM;
3733a658f23SZhu Yi 	}
3743a658f23SZhu Yi 
3753a658f23SZhu Yi 	bu21029->client = client;
3763a658f23SZhu Yi 	bu21029->in_dev = in_dev;
3773a658f23SZhu Yi 	timer_setup(&bu21029->timer, bu21029_touch_release, 0);
3783a658f23SZhu Yi 
3793a658f23SZhu Yi 	in_dev->name		= DRIVER_NAME;
3803a658f23SZhu Yi 	in_dev->id.bustype	= BUS_I2C;
3813a658f23SZhu Yi 	in_dev->open		= bu21029_start_chip;
3823a658f23SZhu Yi 	in_dev->close		= bu21029_stop_chip;
3833a658f23SZhu Yi 
3843a658f23SZhu Yi 	input_set_capability(in_dev, EV_KEY, BTN_TOUCH);
3853a658f23SZhu Yi 	input_set_abs_params(in_dev, ABS_X, 0, MAX_12BIT, 0, 0);
3863a658f23SZhu Yi 	input_set_abs_params(in_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
3873a658f23SZhu Yi 	input_set_abs_params(in_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
3883a658f23SZhu Yi 	touchscreen_parse_properties(in_dev, false, &bu21029->prop);
3893a658f23SZhu Yi 
3903a658f23SZhu Yi 	input_set_drvdata(in_dev, bu21029);
3913a658f23SZhu Yi 
392*59a51ba6SKrzysztof Kozlowski 	error = devm_request_threaded_irq(dev, client->irq, NULL,
393*59a51ba6SKrzysztof Kozlowski 					  bu21029_touch_soft_irq,
394bcd9730aSBarry Song 					  IRQF_ONESHOT | IRQF_NO_AUTOEN,
395bcd9730aSBarry Song 					  DRIVER_NAME, bu21029);
3963a658f23SZhu Yi 	if (error) {
397*59a51ba6SKrzysztof Kozlowski 		dev_err(dev, "unable to request touch irq: %d\n", error);
3983a658f23SZhu Yi 		return error;
3993a658f23SZhu Yi 	}
4003a658f23SZhu Yi 
4013a658f23SZhu Yi 	error = input_register_device(in_dev);
4023a658f23SZhu Yi 	if (error) {
403*59a51ba6SKrzysztof Kozlowski 		dev_err(dev, "unable to register input device: %d\n", error);
4043a658f23SZhu Yi 		return error;
4053a658f23SZhu Yi 	}
4063a658f23SZhu Yi 
4073a658f23SZhu Yi 	i2c_set_clientdata(client, bu21029);
4083a658f23SZhu Yi 
4093a658f23SZhu Yi 	return 0;
4103a658f23SZhu Yi }
4113a658f23SZhu Yi 
bu21029_suspend(struct device * dev)412a259afe9SJonathan Cameron static int bu21029_suspend(struct device *dev)
4133a658f23SZhu Yi {
4143a658f23SZhu Yi 	struct i2c_client *i2c = to_i2c_client(dev);
4153a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
4163a658f23SZhu Yi 
4173a658f23SZhu Yi 	if (!device_may_wakeup(dev)) {
4183a658f23SZhu Yi 		mutex_lock(&bu21029->in_dev->mutex);
419d69f0a43SAndrzej Pietrasiewicz 		if (input_device_enabled(bu21029->in_dev))
4203a658f23SZhu Yi 			bu21029_stop_chip(bu21029->in_dev);
4213a658f23SZhu Yi 		mutex_unlock(&bu21029->in_dev->mutex);
4223a658f23SZhu Yi 	}
4233a658f23SZhu Yi 
4243a658f23SZhu Yi 	return 0;
4253a658f23SZhu Yi }
4263a658f23SZhu Yi 
bu21029_resume(struct device * dev)427a259afe9SJonathan Cameron static int bu21029_resume(struct device *dev)
4283a658f23SZhu Yi {
4293a658f23SZhu Yi 	struct i2c_client *i2c = to_i2c_client(dev);
4303a658f23SZhu Yi 	struct bu21029_ts_data *bu21029 = i2c_get_clientdata(i2c);
4313a658f23SZhu Yi 
4323a658f23SZhu Yi 	if (!device_may_wakeup(dev)) {
4333a658f23SZhu Yi 		mutex_lock(&bu21029->in_dev->mutex);
434d69f0a43SAndrzej Pietrasiewicz 		if (input_device_enabled(bu21029->in_dev))
4353a658f23SZhu Yi 			bu21029_start_chip(bu21029->in_dev);
4363a658f23SZhu Yi 		mutex_unlock(&bu21029->in_dev->mutex);
4373a658f23SZhu Yi 	}
4383a658f23SZhu Yi 
4393a658f23SZhu Yi 	return 0;
4403a658f23SZhu Yi }
441a259afe9SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(bu21029_pm_ops, bu21029_suspend, bu21029_resume);
4423a658f23SZhu Yi 
4433a658f23SZhu Yi static const struct i2c_device_id bu21029_ids[] = {
4443a658f23SZhu Yi 	{ DRIVER_NAME, 0 },
4453a658f23SZhu Yi 	{ /* sentinel */ }
4463a658f23SZhu Yi };
4473a658f23SZhu Yi MODULE_DEVICE_TABLE(i2c, bu21029_ids);
4483a658f23SZhu Yi 
4493a658f23SZhu Yi #ifdef CONFIG_OF
4503a658f23SZhu Yi static const struct of_device_id bu21029_of_ids[] = {
4513a658f23SZhu Yi 	{ .compatible = "rohm,bu21029" },
4523a658f23SZhu Yi 	{ /* sentinel */ }
4533a658f23SZhu Yi };
4543a658f23SZhu Yi MODULE_DEVICE_TABLE(of, bu21029_of_ids);
4553a658f23SZhu Yi #endif
4563a658f23SZhu Yi 
4573a658f23SZhu Yi static struct i2c_driver bu21029_driver = {
4583a658f23SZhu Yi 	.driver	= {
4593a658f23SZhu Yi 		.name		= DRIVER_NAME,
4603a658f23SZhu Yi 		.of_match_table	= of_match_ptr(bu21029_of_ids),
461a259afe9SJonathan Cameron 		.pm		= pm_sleep_ptr(&bu21029_pm_ops),
4623a658f23SZhu Yi 	},
4633a658f23SZhu Yi 	.id_table	= bu21029_ids,
464d8bde56dSUwe Kleine-König 	.probe		= bu21029_probe,
4653a658f23SZhu Yi };
4663a658f23SZhu Yi module_i2c_driver(bu21029_driver);
4673a658f23SZhu Yi 
4683a658f23SZhu Yi MODULE_AUTHOR("Zhu Yi <yi.zhu5@cn.bosch.com>");
4693a658f23SZhu Yi MODULE_DESCRIPTION("Rohm BU21029 touchscreen controller driver");
4703a658f23SZhu Yi MODULE_LICENSE("GPL v2");
471