18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ROHM BU21023/24 Dual touch support resistive touch screen driver
48c2ecf20Sopenharmony_ci * Copyright (C) 2012 ROHM CO.,LTD.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci#include <linux/delay.h>
78c2ecf20Sopenharmony_ci#include <linux/firmware.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/input.h>
108c2ecf20Sopenharmony_ci#include <linux/input/mt.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define BU21023_NAME			"bu21023_ts"
168c2ecf20Sopenharmony_ci#define BU21023_FIRMWARE_NAME		"bu21023.bin"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define MAX_CONTACTS			2
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define AXIS_ADJUST			4
218c2ecf20Sopenharmony_ci#define AXIS_OFFSET			8
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define FIRMWARE_BLOCK_SIZE		32U
248c2ecf20Sopenharmony_ci#define FIRMWARE_RETRY_MAX		4
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define SAMPLING_DELAY			12	/* msec */
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define CALIBRATION_RETRY_MAX		6
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define ROHM_TS_ABS_X_MIN		40
318c2ecf20Sopenharmony_ci#define ROHM_TS_ABS_X_MAX		990
328c2ecf20Sopenharmony_ci#define ROHM_TS_ABS_Y_MIN		160
338c2ecf20Sopenharmony_ci#define ROHM_TS_ABS_Y_MAX		920
348c2ecf20Sopenharmony_ci#define ROHM_TS_DISPLACEMENT_MAX	0	/* zero for infinite */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * BU21023GUL/BU21023MUV/BU21024FV-M registers map
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci#define VADOUT_YP_H		0x00
408c2ecf20Sopenharmony_ci#define VADOUT_YP_L		0x01
418c2ecf20Sopenharmony_ci#define VADOUT_XP_H		0x02
428c2ecf20Sopenharmony_ci#define VADOUT_XP_L		0x03
438c2ecf20Sopenharmony_ci#define VADOUT_YN_H		0x04
448c2ecf20Sopenharmony_ci#define VADOUT_YN_L		0x05
458c2ecf20Sopenharmony_ci#define VADOUT_XN_H		0x06
468c2ecf20Sopenharmony_ci#define VADOUT_XN_L		0x07
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define PRM1_X_H		0x08
498c2ecf20Sopenharmony_ci#define PRM1_X_L		0x09
508c2ecf20Sopenharmony_ci#define PRM1_Y_H		0x0a
518c2ecf20Sopenharmony_ci#define PRM1_Y_L		0x0b
528c2ecf20Sopenharmony_ci#define PRM2_X_H		0x0c
538c2ecf20Sopenharmony_ci#define PRM2_X_L		0x0d
548c2ecf20Sopenharmony_ci#define PRM2_Y_H		0x0e
558c2ecf20Sopenharmony_ci#define PRM2_Y_L		0x0f
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define MLT_PRM_MONI_X		0x10
588c2ecf20Sopenharmony_ci#define MLT_PRM_MONI_Y		0x11
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define DEBUG_MONI_1		0x12
618c2ecf20Sopenharmony_ci#define DEBUG_MONI_2		0x13
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define VADOUT_ZX_H		0x14
648c2ecf20Sopenharmony_ci#define VADOUT_ZX_L		0x15
658c2ecf20Sopenharmony_ci#define VADOUT_ZY_H		0x16
668c2ecf20Sopenharmony_ci#define VADOUT_ZY_L		0x17
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define Z_PARAM_H		0x18
698c2ecf20Sopenharmony_ci#define Z_PARAM_L		0x19
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/*
728c2ecf20Sopenharmony_ci * Value for VADOUT_*_L
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci#define VADOUT_L_MASK		0x01
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Value for PRM*_*_L
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ci#define PRM_L_MASK		0x01
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define POS_X1_H		0x20
828c2ecf20Sopenharmony_ci#define POS_X1_L		0x21
838c2ecf20Sopenharmony_ci#define POS_Y1_H		0x22
848c2ecf20Sopenharmony_ci#define POS_Y1_L		0x23
858c2ecf20Sopenharmony_ci#define POS_X2_H		0x24
868c2ecf20Sopenharmony_ci#define POS_X2_L		0x25
878c2ecf20Sopenharmony_ci#define POS_Y2_H		0x26
888c2ecf20Sopenharmony_ci#define POS_Y2_L		0x27
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/*
918c2ecf20Sopenharmony_ci * Value for POS_*_L
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ci#define POS_L_MASK		0x01
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci#define TOUCH			0x28
968c2ecf20Sopenharmony_ci#define TOUCH_DETECT		0x01
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci#define TOUCH_GESTURE		0x29
998c2ecf20Sopenharmony_ci#define SINGLE_TOUCH		0x01
1008c2ecf20Sopenharmony_ci#define DUAL_TOUCH		0x03
1018c2ecf20Sopenharmony_ci#define TOUCH_MASK		0x03
1028c2ecf20Sopenharmony_ci#define CALIBRATION_REQUEST	0x04
1038c2ecf20Sopenharmony_ci#define CALIBRATION_STATUS	0x08
1048c2ecf20Sopenharmony_ci#define CALIBRATION_MASK	0x0c
1058c2ecf20Sopenharmony_ci#define GESTURE_SPREAD		0x10
1068c2ecf20Sopenharmony_ci#define GESTURE_PINCH		0x20
1078c2ecf20Sopenharmony_ci#define GESTURE_ROTATE_R	0x40
1088c2ecf20Sopenharmony_ci#define GESTURE_ROTATE_L	0x80
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define INT_STATUS		0x2a
1118c2ecf20Sopenharmony_ci#define INT_MASK		0x3d
1128c2ecf20Sopenharmony_ci#define INT_CLEAR		0x3e
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci/*
1158c2ecf20Sopenharmony_ci * Values for INT_*
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ci#define COORD_UPDATE		0x01
1188c2ecf20Sopenharmony_ci#define CALIBRATION_DONE	0x02
1198c2ecf20Sopenharmony_ci#define SLEEP_IN		0x04
1208c2ecf20Sopenharmony_ci#define SLEEP_OUT		0x08
1218c2ecf20Sopenharmony_ci#define PROGRAM_LOAD_DONE	0x10
1228c2ecf20Sopenharmony_ci#define ERROR			0x80
1238c2ecf20Sopenharmony_ci#define INT_ALL			0x9f
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define ERR_STATUS		0x2b
1268c2ecf20Sopenharmony_ci#define ERR_MASK		0x3f
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci * Values for ERR_*
1308c2ecf20Sopenharmony_ci */
1318c2ecf20Sopenharmony_ci#define ADC_TIMEOUT		0x01
1328c2ecf20Sopenharmony_ci#define CPU_TIMEOUT		0x02
1338c2ecf20Sopenharmony_ci#define CALIBRATION_ERR		0x04
1348c2ecf20Sopenharmony_ci#define PROGRAM_LOAD_ERR	0x10
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci#define COMMON_SETUP1			0x30
1378c2ecf20Sopenharmony_ci#define PROGRAM_LOAD_HOST		0x02
1388c2ecf20Sopenharmony_ci#define PROGRAM_LOAD_EEPROM		0x03
1398c2ecf20Sopenharmony_ci#define CENSOR_4PORT			0x04
1408c2ecf20Sopenharmony_ci#define CENSOR_8PORT			0x00	/* Not supported by BU21023 */
1418c2ecf20Sopenharmony_ci#define CALIBRATION_TYPE_DEFAULT	0x08
1428c2ecf20Sopenharmony_ci#define CALIBRATION_TYPE_SPECIAL	0x00
1438c2ecf20Sopenharmony_ci#define INT_ACTIVE_HIGH			0x10
1448c2ecf20Sopenharmony_ci#define INT_ACTIVE_LOW			0x00
1458c2ecf20Sopenharmony_ci#define AUTO_CALIBRATION		0x40
1468c2ecf20Sopenharmony_ci#define MANUAL_CALIBRATION		0x00
1478c2ecf20Sopenharmony_ci#define COMMON_SETUP1_DEFAULT		0x4e
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci#define COMMON_SETUP2		0x31
1508c2ecf20Sopenharmony_ci#define MAF_NONE		0x00
1518c2ecf20Sopenharmony_ci#define MAF_1SAMPLE		0x01
1528c2ecf20Sopenharmony_ci#define MAF_3SAMPLES		0x02
1538c2ecf20Sopenharmony_ci#define MAF_5SAMPLES		0x03
1548c2ecf20Sopenharmony_ci#define INV_Y			0x04
1558c2ecf20Sopenharmony_ci#define INV_X			0x08
1568c2ecf20Sopenharmony_ci#define SWAP_XY			0x10
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define COMMON_SETUP3		0x32
1598c2ecf20Sopenharmony_ci#define EN_SLEEP		0x01
1608c2ecf20Sopenharmony_ci#define EN_MULTI		0x02
1618c2ecf20Sopenharmony_ci#define EN_GESTURE		0x04
1628c2ecf20Sopenharmony_ci#define EN_INTVL		0x08
1638c2ecf20Sopenharmony_ci#define SEL_STEP		0x10
1648c2ecf20Sopenharmony_ci#define SEL_MULTI		0x20
1658c2ecf20Sopenharmony_ci#define SEL_TBL_DEFAULT		0x40
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#define INTERVAL_TIME		0x33
1688c2ecf20Sopenharmony_ci#define INTERVAL_TIME_DEFAULT	0x10
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define STEP_X			0x34
1718c2ecf20Sopenharmony_ci#define STEP_X_DEFAULT		0x41
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define STEP_Y			0x35
1748c2ecf20Sopenharmony_ci#define STEP_Y_DEFAULT		0x8d
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci#define OFFSET_X		0x38
1778c2ecf20Sopenharmony_ci#define OFFSET_X_DEFAULT	0x0c
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define OFFSET_Y		0x39
1808c2ecf20Sopenharmony_ci#define OFFSET_Y_DEFAULT	0x0c
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#define THRESHOLD_TOUCH		0x3a
1838c2ecf20Sopenharmony_ci#define THRESHOLD_TOUCH_DEFAULT	0xa0
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci#define THRESHOLD_GESTURE		0x3b
1868c2ecf20Sopenharmony_ci#define THRESHOLD_GESTURE_DEFAULT	0x17
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#define SYSTEM			0x40
1898c2ecf20Sopenharmony_ci#define ANALOG_POWER_ON		0x01
1908c2ecf20Sopenharmony_ci#define ANALOG_POWER_OFF	0x00
1918c2ecf20Sopenharmony_ci#define CPU_POWER_ON		0x02
1928c2ecf20Sopenharmony_ci#define CPU_POWER_OFF		0x00
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#define FORCE_CALIBRATION	0x42
1958c2ecf20Sopenharmony_ci#define FORCE_CALIBRATION_ON	0x01
1968c2ecf20Sopenharmony_ci#define FORCE_CALIBRATION_OFF	0x00
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci#define CPU_FREQ		0x50	/* 10 / (reg + 1) MHz */
1998c2ecf20Sopenharmony_ci#define CPU_FREQ_10MHZ		0x00
2008c2ecf20Sopenharmony_ci#define CPU_FREQ_5MHZ		0x01
2018c2ecf20Sopenharmony_ci#define CPU_FREQ_1MHZ		0x09
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define EEPROM_ADDR		0x51
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci#define CALIBRATION_ADJUST		0x52
2068c2ecf20Sopenharmony_ci#define CALIBRATION_ADJUST_DEFAULT	0x00
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci#define THRESHOLD_SLEEP_IN	0x53
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#define EVR_XY			0x56
2118c2ecf20Sopenharmony_ci#define EVR_XY_DEFAULT		0x10
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define PRM_SWOFF_TIME		0x57
2148c2ecf20Sopenharmony_ci#define PRM_SWOFF_TIME_DEFAULT	0x04
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#define PROGRAM_VERSION		0x5f
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#define ADC_CTRL		0x60
2198c2ecf20Sopenharmony_ci#define ADC_DIV_MASK		0x1f	/* The minimum value is 4 */
2208c2ecf20Sopenharmony_ci#define ADC_DIV_DEFAULT		0x08
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define ADC_WAIT		0x61
2238c2ecf20Sopenharmony_ci#define ADC_WAIT_DEFAULT	0x0a
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#define SWCONT			0x62
2268c2ecf20Sopenharmony_ci#define SWCONT_DEFAULT		0x0f
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci#define EVR_X			0x63
2298c2ecf20Sopenharmony_ci#define EVR_X_DEFAULT		0x86
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define EVR_Y			0x64
2328c2ecf20Sopenharmony_ci#define EVR_Y_DEFAULT		0x64
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci#define TEST1			0x65
2358c2ecf20Sopenharmony_ci#define DUALTOUCH_STABILIZE_ON	0x01
2368c2ecf20Sopenharmony_ci#define DUALTOUCH_STABILIZE_OFF	0x00
2378c2ecf20Sopenharmony_ci#define DUALTOUCH_REG_ON	0x20
2388c2ecf20Sopenharmony_ci#define DUALTOUCH_REG_OFF	0x00
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci#define CALIBRATION_REG1		0x68
2418c2ecf20Sopenharmony_ci#define CALIBRATION_REG1_DEFAULT	0xd9
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#define CALIBRATION_REG2		0x69
2448c2ecf20Sopenharmony_ci#define CALIBRATION_REG2_DEFAULT	0x36
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci#define CALIBRATION_REG3		0x6a
2478c2ecf20Sopenharmony_ci#define CALIBRATION_REG3_DEFAULT	0x32
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci#define EX_ADDR_H		0x70
2508c2ecf20Sopenharmony_ci#define EX_ADDR_L		0x71
2518c2ecf20Sopenharmony_ci#define EX_WDAT			0x72
2528c2ecf20Sopenharmony_ci#define EX_RDAT			0x73
2538c2ecf20Sopenharmony_ci#define EX_CHK_SUM1		0x74
2548c2ecf20Sopenharmony_ci#define EX_CHK_SUM2		0x75
2558c2ecf20Sopenharmony_ci#define EX_CHK_SUM3		0x76
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistruct rohm_ts_data {
2588c2ecf20Sopenharmony_ci	struct i2c_client *client;
2598c2ecf20Sopenharmony_ci	struct input_dev *input;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	bool initialized;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	unsigned int contact_count[MAX_CONTACTS + 1];
2648c2ecf20Sopenharmony_ci	int finger_count;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	u8 setup2;
2678c2ecf20Sopenharmony_ci};
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci/*
2708c2ecf20Sopenharmony_ci * rohm_i2c_burst_read - execute combined I2C message for ROHM BU21023/24
2718c2ecf20Sopenharmony_ci * @client: Handle to ROHM BU21023/24
2728c2ecf20Sopenharmony_ci * @start: Where to start read address from ROHM BU21023/24
2738c2ecf20Sopenharmony_ci * @buf: Where to store read data from ROHM BU21023/24
2748c2ecf20Sopenharmony_ci * @len: How many bytes to read
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * Returns negative errno, else zero on success.
2778c2ecf20Sopenharmony_ci *
2788c2ecf20Sopenharmony_ci * Note
2798c2ecf20Sopenharmony_ci * In BU21023/24 burst read, stop condition is needed after "address write".
2808c2ecf20Sopenharmony_ci * Therefore, transmission is performed in 2 steps.
2818c2ecf20Sopenharmony_ci */
2828c2ecf20Sopenharmony_cistatic int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
2838c2ecf20Sopenharmony_ci			       size_t len)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	struct i2c_adapter *adap = client->adapter;
2868c2ecf20Sopenharmony_ci	struct i2c_msg msg[2];
2878c2ecf20Sopenharmony_ci	int i, ret = 0;
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	msg[0].addr = client->addr;
2908c2ecf20Sopenharmony_ci	msg[0].flags = 0;
2918c2ecf20Sopenharmony_ci	msg[0].len = 1;
2928c2ecf20Sopenharmony_ci	msg[0].buf = &start;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	msg[1].addr = client->addr;
2958c2ecf20Sopenharmony_ci	msg[1].flags = I2C_M_RD;
2968c2ecf20Sopenharmony_ci	msg[1].len = len;
2978c2ecf20Sopenharmony_ci	msg[1].buf = buf;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
3028c2ecf20Sopenharmony_ci		if (__i2c_transfer(adap, &msg[i], 1) < 0) {
3038c2ecf20Sopenharmony_ci			ret = -EIO;
3048c2ecf20Sopenharmony_ci			break;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return ret;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int rohm_ts_manual_calibration(struct rohm_ts_data *ts)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	struct i2c_client *client = ts->client;
3168c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
3178c2ecf20Sopenharmony_ci	u8 buf[33];	/* for PRM1_X_H(0x08)-TOUCH(0x28) */
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	int retry;
3208c2ecf20Sopenharmony_ci	bool success = false;
3218c2ecf20Sopenharmony_ci	bool first_time = true;
3228c2ecf20Sopenharmony_ci	bool calibration_done;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	u8 reg1, reg2, reg3;
3258c2ecf20Sopenharmony_ci	s32 reg1_orig, reg2_orig, reg3_orig;
3268c2ecf20Sopenharmony_ci	s32 val;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	int calib_x = 0, calib_y = 0;
3298c2ecf20Sopenharmony_ci	int reg_x, reg_y;
3308c2ecf20Sopenharmony_ci	int err_x, err_y;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	int error, error2;
3338c2ecf20Sopenharmony_ci	int i;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	reg1_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG1);
3368c2ecf20Sopenharmony_ci	if (reg1_orig < 0)
3378c2ecf20Sopenharmony_ci		return reg1_orig;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	reg2_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG2);
3408c2ecf20Sopenharmony_ci	if (reg2_orig < 0)
3418c2ecf20Sopenharmony_ci		return reg2_orig;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	reg3_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG3);
3448c2ecf20Sopenharmony_ci	if (reg3_orig < 0)
3458c2ecf20Sopenharmony_ci		return reg3_orig;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_MASK,
3488c2ecf20Sopenharmony_ci					  COORD_UPDATE | SLEEP_IN | SLEEP_OUT |
3498c2ecf20Sopenharmony_ci					  PROGRAM_LOAD_DONE);
3508c2ecf20Sopenharmony_ci	if (error)
3518c2ecf20Sopenharmony_ci		goto out;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, TEST1,
3548c2ecf20Sopenharmony_ci					  DUALTOUCH_STABILIZE_ON);
3558c2ecf20Sopenharmony_ci	if (error)
3568c2ecf20Sopenharmony_ci		goto out;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	for (retry = 0; retry < CALIBRATION_RETRY_MAX; retry++) {
3598c2ecf20Sopenharmony_ci		/* wait 2 sampling for update */
3608c2ecf20Sopenharmony_ci		mdelay(2 * SAMPLING_DELAY);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci#define READ_CALIB_BUF(reg)	buf[((reg) - PRM1_X_H)]
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci		error = rohm_i2c_burst_read(client, PRM1_X_H, buf, sizeof(buf));
3658c2ecf20Sopenharmony_ci		if (error)
3668c2ecf20Sopenharmony_ci			goto out;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci		if (READ_CALIB_BUF(TOUCH) & TOUCH_DETECT)
3698c2ecf20Sopenharmony_ci			continue;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		if (first_time) {
3728c2ecf20Sopenharmony_ci			/* generate calibration parameter */
3738c2ecf20Sopenharmony_ci			calib_x = ((int)READ_CALIB_BUF(PRM1_X_H) << 2 |
3748c2ecf20Sopenharmony_ci				READ_CALIB_BUF(PRM1_X_L)) - AXIS_OFFSET;
3758c2ecf20Sopenharmony_ci			calib_y = ((int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
3768c2ecf20Sopenharmony_ci				READ_CALIB_BUF(PRM1_Y_L)) - AXIS_OFFSET;
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci			error = i2c_smbus_write_byte_data(client, TEST1,
3798c2ecf20Sopenharmony_ci				DUALTOUCH_STABILIZE_ON | DUALTOUCH_REG_ON);
3808c2ecf20Sopenharmony_ci			if (error)
3818c2ecf20Sopenharmony_ci				goto out;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci			first_time = false;
3848c2ecf20Sopenharmony_ci		} else {
3858c2ecf20Sopenharmony_ci			/* generate adjustment parameter */
3868c2ecf20Sopenharmony_ci			err_x = (int)READ_CALIB_BUF(PRM1_X_H) << 2 |
3878c2ecf20Sopenharmony_ci				READ_CALIB_BUF(PRM1_X_L);
3888c2ecf20Sopenharmony_ci			err_y = (int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
3898c2ecf20Sopenharmony_ci				READ_CALIB_BUF(PRM1_Y_L);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci			/* X axis ajust */
3928c2ecf20Sopenharmony_ci			if (err_x <= 4)
3938c2ecf20Sopenharmony_ci				calib_x -= AXIS_ADJUST;
3948c2ecf20Sopenharmony_ci			else if (err_x >= 60)
3958c2ecf20Sopenharmony_ci				calib_x += AXIS_ADJUST;
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci			/* Y axis ajust */
3988c2ecf20Sopenharmony_ci			if (err_y <= 4)
3998c2ecf20Sopenharmony_ci				calib_y -= AXIS_ADJUST;
4008c2ecf20Sopenharmony_ci			else if (err_y >= 60)
4018c2ecf20Sopenharmony_ci				calib_y += AXIS_ADJUST;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci		/* generate calibration setting value */
4058c2ecf20Sopenharmony_ci		reg_x = calib_x + ((calib_x & 0x200) << 1);
4068c2ecf20Sopenharmony_ci		reg_y = calib_y + ((calib_y & 0x200) << 1);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci		/* convert for register format */
4098c2ecf20Sopenharmony_ci		reg1 = reg_x >> 3;
4108c2ecf20Sopenharmony_ci		reg2 = (reg_y & 0x7) << 4 | (reg_x & 0x7);
4118c2ecf20Sopenharmony_ci		reg3 = reg_y >> 3;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client,
4148c2ecf20Sopenharmony_ci						  CALIBRATION_REG1, reg1);
4158c2ecf20Sopenharmony_ci		if (error)
4168c2ecf20Sopenharmony_ci			goto out;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client,
4198c2ecf20Sopenharmony_ci						  CALIBRATION_REG2, reg2);
4208c2ecf20Sopenharmony_ci		if (error)
4218c2ecf20Sopenharmony_ci			goto out;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client,
4248c2ecf20Sopenharmony_ci						  CALIBRATION_REG3, reg3);
4258c2ecf20Sopenharmony_ci		if (error)
4268c2ecf20Sopenharmony_ci			goto out;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		/*
4298c2ecf20Sopenharmony_ci		 * force calibration sequcence
4308c2ecf20Sopenharmony_ci		 */
4318c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
4328c2ecf20Sopenharmony_ci						  FORCE_CALIBRATION_OFF);
4338c2ecf20Sopenharmony_ci		if (error)
4348c2ecf20Sopenharmony_ci			goto out;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
4378c2ecf20Sopenharmony_ci						  FORCE_CALIBRATION_ON);
4388c2ecf20Sopenharmony_ci		if (error)
4398c2ecf20Sopenharmony_ci			goto out;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		/* clear all interrupts */
4428c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
4438c2ecf20Sopenharmony_ci		if (error)
4448c2ecf20Sopenharmony_ci			goto out;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		/*
4478c2ecf20Sopenharmony_ci		 * Wait for the status change of calibration, max 10 sampling
4488c2ecf20Sopenharmony_ci		 */
4498c2ecf20Sopenharmony_ci		calibration_done = false;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci		for (i = 0; i < 10; i++) {
4528c2ecf20Sopenharmony_ci			mdelay(SAMPLING_DELAY);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci			val = i2c_smbus_read_byte_data(client, TOUCH_GESTURE);
4558c2ecf20Sopenharmony_ci			if (!(val & CALIBRATION_MASK)) {
4568c2ecf20Sopenharmony_ci				calibration_done = true;
4578c2ecf20Sopenharmony_ci				break;
4588c2ecf20Sopenharmony_ci			} else if (val < 0) {
4598c2ecf20Sopenharmony_ci				error = val;
4608c2ecf20Sopenharmony_ci				goto out;
4618c2ecf20Sopenharmony_ci			}
4628c2ecf20Sopenharmony_ci		}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		if (calibration_done) {
4658c2ecf20Sopenharmony_ci			val = i2c_smbus_read_byte_data(client, INT_STATUS);
4668c2ecf20Sopenharmony_ci			if (val == CALIBRATION_DONE) {
4678c2ecf20Sopenharmony_ci				success = true;
4688c2ecf20Sopenharmony_ci				break;
4698c2ecf20Sopenharmony_ci			} else if (val < 0) {
4708c2ecf20Sopenharmony_ci				error = val;
4718c2ecf20Sopenharmony_ci				goto out;
4728c2ecf20Sopenharmony_ci			}
4738c2ecf20Sopenharmony_ci		} else {
4748c2ecf20Sopenharmony_ci			dev_warn(dev, "calibration timeout\n");
4758c2ecf20Sopenharmony_ci		}
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	if (!success) {
4798c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
4808c2ecf20Sopenharmony_ci						  reg1_orig);
4818c2ecf20Sopenharmony_ci		if (error)
4828c2ecf20Sopenharmony_ci			goto out;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
4858c2ecf20Sopenharmony_ci						  reg2_orig);
4868c2ecf20Sopenharmony_ci		if (error)
4878c2ecf20Sopenharmony_ci			goto out;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
4908c2ecf20Sopenharmony_ci						  reg3_orig);
4918c2ecf20Sopenharmony_ci		if (error)
4928c2ecf20Sopenharmony_ci			goto out;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		/* calibration data enable */
4958c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, TEST1,
4968c2ecf20Sopenharmony_ci						  DUALTOUCH_STABILIZE_ON |
4978c2ecf20Sopenharmony_ci						  DUALTOUCH_REG_ON);
4988c2ecf20Sopenharmony_ci		if (error)
4998c2ecf20Sopenharmony_ci			goto out;
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci		/* wait 10 sampling */
5028c2ecf20Sopenharmony_ci		mdelay(10 * SAMPLING_DELAY);
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci		error = -EBUSY;
5058c2ecf20Sopenharmony_ci	}
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ciout:
5088c2ecf20Sopenharmony_ci	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
5098c2ecf20Sopenharmony_ci	if (!error2)
5108c2ecf20Sopenharmony_ci		/* Clear all interrupts */
5118c2ecf20Sopenharmony_ci		error2 = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	return error ? error : error2;
5148c2ecf20Sopenharmony_ci}
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_cistatic const unsigned int untouch_threshold[3] = { 0, 1, 5 };
5178c2ecf20Sopenharmony_cistatic const unsigned int single_touch_threshold[3] = { 0, 0, 4 };
5188c2ecf20Sopenharmony_cistatic const unsigned int dual_touch_threshold[3] = { 10, 8, 0 };
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistatic irqreturn_t rohm_ts_soft_irq(int irq, void *dev_id)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = dev_id;
5238c2ecf20Sopenharmony_ci	struct i2c_client *client = ts->client;
5248c2ecf20Sopenharmony_ci	struct input_dev *input_dev = ts->input;
5258c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	u8 buf[10];	/* for POS_X1_H(0x20)-TOUCH_GESTURE(0x29) */
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	struct input_mt_pos pos[MAX_CONTACTS];
5308c2ecf20Sopenharmony_ci	int slots[MAX_CONTACTS];
5318c2ecf20Sopenharmony_ci	u8 touch_flags;
5328c2ecf20Sopenharmony_ci	unsigned int threshold;
5338c2ecf20Sopenharmony_ci	int finger_count = -1;
5348c2ecf20Sopenharmony_ci	int prev_finger_count = ts->finger_count;
5358c2ecf20Sopenharmony_ci	int count;
5368c2ecf20Sopenharmony_ci	int error;
5378c2ecf20Sopenharmony_ci	int i;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
5408c2ecf20Sopenharmony_ci	if (error)
5418c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	/* Clear all interrupts */
5448c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
5458c2ecf20Sopenharmony_ci	if (error)
5468c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci#define READ_POS_BUF(reg)	buf[((reg) - POS_X1_H)]
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	error = rohm_i2c_burst_read(client, POS_X1_H, buf, sizeof(buf));
5518c2ecf20Sopenharmony_ci	if (error)
5528c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	touch_flags = READ_POS_BUF(TOUCH_GESTURE) & TOUCH_MASK;
5558c2ecf20Sopenharmony_ci	if (touch_flags) {
5568c2ecf20Sopenharmony_ci		/* generate coordinates */
5578c2ecf20Sopenharmony_ci		pos[0].x = ((s16)READ_POS_BUF(POS_X1_H) << 2) |
5588c2ecf20Sopenharmony_ci			   READ_POS_BUF(POS_X1_L);
5598c2ecf20Sopenharmony_ci		pos[0].y = ((s16)READ_POS_BUF(POS_Y1_H) << 2) |
5608c2ecf20Sopenharmony_ci			   READ_POS_BUF(POS_Y1_L);
5618c2ecf20Sopenharmony_ci		pos[1].x = ((s16)READ_POS_BUF(POS_X2_H) << 2) |
5628c2ecf20Sopenharmony_ci			   READ_POS_BUF(POS_X2_L);
5638c2ecf20Sopenharmony_ci		pos[1].y = ((s16)READ_POS_BUF(POS_Y2_H) << 2) |
5648c2ecf20Sopenharmony_ci			   READ_POS_BUF(POS_Y2_L);
5658c2ecf20Sopenharmony_ci	}
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	switch (touch_flags) {
5688c2ecf20Sopenharmony_ci	case 0:
5698c2ecf20Sopenharmony_ci		threshold = untouch_threshold[prev_finger_count];
5708c2ecf20Sopenharmony_ci		if (++ts->contact_count[0] >= threshold)
5718c2ecf20Sopenharmony_ci			finger_count = 0;
5728c2ecf20Sopenharmony_ci		break;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	case SINGLE_TOUCH:
5758c2ecf20Sopenharmony_ci		threshold = single_touch_threshold[prev_finger_count];
5768c2ecf20Sopenharmony_ci		if (++ts->contact_count[1] >= threshold)
5778c2ecf20Sopenharmony_ci			finger_count = 1;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		if (finger_count == 1) {
5808c2ecf20Sopenharmony_ci			if (pos[1].x != 0 && pos[1].y != 0) {
5818c2ecf20Sopenharmony_ci				pos[0].x = pos[1].x;
5828c2ecf20Sopenharmony_ci				pos[0].y = pos[1].y;
5838c2ecf20Sopenharmony_ci				pos[1].x = 0;
5848c2ecf20Sopenharmony_ci				pos[1].y = 0;
5858c2ecf20Sopenharmony_ci			}
5868c2ecf20Sopenharmony_ci		}
5878c2ecf20Sopenharmony_ci		break;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	case DUAL_TOUCH:
5908c2ecf20Sopenharmony_ci		threshold = dual_touch_threshold[prev_finger_count];
5918c2ecf20Sopenharmony_ci		if (++ts->contact_count[2] >= threshold)
5928c2ecf20Sopenharmony_ci			finger_count = 2;
5938c2ecf20Sopenharmony_ci		break;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	default:
5968c2ecf20Sopenharmony_ci		dev_dbg(dev,
5978c2ecf20Sopenharmony_ci			"Three or more touches are not supported\n");
5988c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
5998c2ecf20Sopenharmony_ci	}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	if (finger_count >= 0) {
6028c2ecf20Sopenharmony_ci		if (prev_finger_count != finger_count) {
6038c2ecf20Sopenharmony_ci			count = ts->contact_count[finger_count];
6048c2ecf20Sopenharmony_ci			memset(ts->contact_count, 0, sizeof(ts->contact_count));
6058c2ecf20Sopenharmony_ci			ts->contact_count[finger_count] = count;
6068c2ecf20Sopenharmony_ci		}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci		input_mt_assign_slots(input_dev, slots, pos,
6098c2ecf20Sopenharmony_ci				      finger_count, ROHM_TS_DISPLACEMENT_MAX);
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ci		for (i = 0; i < finger_count; i++) {
6128c2ecf20Sopenharmony_ci			input_mt_slot(input_dev, slots[i]);
6138c2ecf20Sopenharmony_ci			input_mt_report_slot_state(input_dev,
6148c2ecf20Sopenharmony_ci						   MT_TOOL_FINGER, true);
6158c2ecf20Sopenharmony_ci			input_report_abs(input_dev,
6168c2ecf20Sopenharmony_ci					 ABS_MT_POSITION_X, pos[i].x);
6178c2ecf20Sopenharmony_ci			input_report_abs(input_dev,
6188c2ecf20Sopenharmony_ci					 ABS_MT_POSITION_Y, pos[i].y);
6198c2ecf20Sopenharmony_ci		}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci		input_mt_sync_frame(input_dev);
6228c2ecf20Sopenharmony_ci		input_mt_report_pointer_emulation(input_dev, true);
6238c2ecf20Sopenharmony_ci		input_sync(input_dev);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci		ts->finger_count = finger_count;
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (READ_POS_BUF(TOUCH_GESTURE) & CALIBRATION_REQUEST) {
6298c2ecf20Sopenharmony_ci		error = rohm_ts_manual_calibration(ts);
6308c2ecf20Sopenharmony_ci		if (error)
6318c2ecf20Sopenharmony_ci			dev_warn(dev, "manual calibration failed: %d\n",
6328c2ecf20Sopenharmony_ci				 error);
6338c2ecf20Sopenharmony_ci	}
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	i2c_smbus_write_byte_data(client, INT_MASK,
6368c2ecf20Sopenharmony_ci				  CALIBRATION_DONE | SLEEP_OUT | SLEEP_IN |
6378c2ecf20Sopenharmony_ci				  PROGRAM_LOAD_DONE);
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic int rohm_ts_load_firmware(struct i2c_client *client,
6438c2ecf20Sopenharmony_ci				 const char *firmware_name)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
6468c2ecf20Sopenharmony_ci	const struct firmware *fw;
6478c2ecf20Sopenharmony_ci	s32 status;
6488c2ecf20Sopenharmony_ci	unsigned int offset, len, xfer_len;
6498c2ecf20Sopenharmony_ci	unsigned int retry = 0;
6508c2ecf20Sopenharmony_ci	int error, error2;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	error = request_firmware(&fw, firmware_name, dev);
6538c2ecf20Sopenharmony_ci	if (error) {
6548c2ecf20Sopenharmony_ci		dev_err(dev, "unable to retrieve firmware %s: %d\n",
6558c2ecf20Sopenharmony_ci			firmware_name, error);
6568c2ecf20Sopenharmony_ci		return error;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_MASK,
6608c2ecf20Sopenharmony_ci					  COORD_UPDATE | CALIBRATION_DONE |
6618c2ecf20Sopenharmony_ci					  SLEEP_IN | SLEEP_OUT);
6628c2ecf20Sopenharmony_ci	if (error)
6638c2ecf20Sopenharmony_ci		goto out;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	do {
6668c2ecf20Sopenharmony_ci		if (retry) {
6678c2ecf20Sopenharmony_ci			dev_warn(dev, "retrying firmware load\n");
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci			/* settings for retry */
6708c2ecf20Sopenharmony_ci			error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
6718c2ecf20Sopenharmony_ci			if (error)
6728c2ecf20Sopenharmony_ci				goto out;
6738c2ecf20Sopenharmony_ci		}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, EX_ADDR_H, 0);
6768c2ecf20Sopenharmony_ci		if (error)
6778c2ecf20Sopenharmony_ci			goto out;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, EX_ADDR_L, 0);
6808c2ecf20Sopenharmony_ci		if (error)
6818c2ecf20Sopenharmony_ci			goto out;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, COMMON_SETUP1,
6848c2ecf20Sopenharmony_ci						  COMMON_SETUP1_DEFAULT);
6858c2ecf20Sopenharmony_ci		if (error)
6868c2ecf20Sopenharmony_ci			goto out;
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci		/* firmware load to the device */
6898c2ecf20Sopenharmony_ci		offset = 0;
6908c2ecf20Sopenharmony_ci		len = fw->size;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		while (len) {
6938c2ecf20Sopenharmony_ci			xfer_len = min(FIRMWARE_BLOCK_SIZE, len);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci			error = i2c_smbus_write_i2c_block_data(client, EX_WDAT,
6968c2ecf20Sopenharmony_ci						xfer_len, &fw->data[offset]);
6978c2ecf20Sopenharmony_ci			if (error)
6988c2ecf20Sopenharmony_ci				goto out;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci			len -= xfer_len;
7018c2ecf20Sopenharmony_ci			offset += xfer_len;
7028c2ecf20Sopenharmony_ci		}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci		/* check firmware load result */
7058c2ecf20Sopenharmony_ci		status = i2c_smbus_read_byte_data(client, INT_STATUS);
7068c2ecf20Sopenharmony_ci		if (status < 0) {
7078c2ecf20Sopenharmony_ci			error = status;
7088c2ecf20Sopenharmony_ci			goto out;
7098c2ecf20Sopenharmony_ci		}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci		/* clear all interrupts */
7128c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
7138c2ecf20Sopenharmony_ci		if (error)
7148c2ecf20Sopenharmony_ci			goto out;
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci		if (status == PROGRAM_LOAD_DONE)
7178c2ecf20Sopenharmony_ci			break;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		error = -EIO;
7208c2ecf20Sopenharmony_ci	} while (++retry <= FIRMWARE_RETRY_MAX);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ciout:
7238c2ecf20Sopenharmony_ci	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	release_firmware(fw);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	return error ? error : error2;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic ssize_t swap_xy_show(struct device *dev, struct device_attribute *attr,
7318c2ecf20Sopenharmony_ci			    char *buf)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
7348c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(ts->setup2 & SWAP_XY));
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_cistatic ssize_t swap_xy_store(struct device *dev, struct device_attribute *attr,
7408c2ecf20Sopenharmony_ci			     const char *buf, size_t count)
7418c2ecf20Sopenharmony_ci{
7428c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
7438c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
7448c2ecf20Sopenharmony_ci	unsigned int val;
7458c2ecf20Sopenharmony_ci	int error;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	error = kstrtouint(buf, 0, &val);
7488c2ecf20Sopenharmony_ci	if (error)
7498c2ecf20Sopenharmony_ci		return error;
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	error = mutex_lock_interruptible(&ts->input->mutex);
7528c2ecf20Sopenharmony_ci	if (error)
7538c2ecf20Sopenharmony_ci		return error;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	if (val)
7568c2ecf20Sopenharmony_ci		ts->setup2 |= SWAP_XY;
7578c2ecf20Sopenharmony_ci	else
7588c2ecf20Sopenharmony_ci		ts->setup2 &= ~SWAP_XY;
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_ci	if (ts->initialized)
7618c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
7628c2ecf20Sopenharmony_ci						  ts->setup2);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	mutex_unlock(&ts->input->mutex);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return error ? error : count;
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic ssize_t inv_x_show(struct device *dev, struct device_attribute *attr,
7708c2ecf20Sopenharmony_ci			  char *buf)
7718c2ecf20Sopenharmony_ci{
7728c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
7738c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_X));
7768c2ecf20Sopenharmony_ci}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_cistatic ssize_t inv_x_store(struct device *dev, struct device_attribute *attr,
7798c2ecf20Sopenharmony_ci			   const char *buf, size_t count)
7808c2ecf20Sopenharmony_ci{
7818c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
7828c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
7838c2ecf20Sopenharmony_ci	unsigned int val;
7848c2ecf20Sopenharmony_ci	int error;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	error = kstrtouint(buf, 0, &val);
7878c2ecf20Sopenharmony_ci	if (error)
7888c2ecf20Sopenharmony_ci		return error;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	error = mutex_lock_interruptible(&ts->input->mutex);
7918c2ecf20Sopenharmony_ci	if (error)
7928c2ecf20Sopenharmony_ci		return error;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	if (val)
7958c2ecf20Sopenharmony_ci		ts->setup2 |= INV_X;
7968c2ecf20Sopenharmony_ci	else
7978c2ecf20Sopenharmony_ci		ts->setup2 &= ~INV_X;
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ci	if (ts->initialized)
8008c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
8018c2ecf20Sopenharmony_ci						  ts->setup2);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	mutex_unlock(&ts->input->mutex);
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	return error ? error : count;
8068c2ecf20Sopenharmony_ci}
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic ssize_t inv_y_show(struct device *dev, struct device_attribute *attr,
8098c2ecf20Sopenharmony_ci			  char *buf)
8108c2ecf20Sopenharmony_ci{
8118c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
8128c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_Y));
8158c2ecf20Sopenharmony_ci}
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cistatic ssize_t inv_y_store(struct device *dev, struct device_attribute *attr,
8188c2ecf20Sopenharmony_ci			   const char *buf, size_t count)
8198c2ecf20Sopenharmony_ci{
8208c2ecf20Sopenharmony_ci	struct i2c_client *client = to_i2c_client(dev);
8218c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = i2c_get_clientdata(client);
8228c2ecf20Sopenharmony_ci	unsigned int val;
8238c2ecf20Sopenharmony_ci	int error;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	error = kstrtouint(buf, 0, &val);
8268c2ecf20Sopenharmony_ci	if (error)
8278c2ecf20Sopenharmony_ci		return error;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	error = mutex_lock_interruptible(&ts->input->mutex);
8308c2ecf20Sopenharmony_ci	if (error)
8318c2ecf20Sopenharmony_ci		return error;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (val)
8348c2ecf20Sopenharmony_ci		ts->setup2 |= INV_Y;
8358c2ecf20Sopenharmony_ci	else
8368c2ecf20Sopenharmony_ci		ts->setup2 &= ~INV_Y;
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	if (ts->initialized)
8398c2ecf20Sopenharmony_ci		error = i2c_smbus_write_byte_data(client, COMMON_SETUP2,
8408c2ecf20Sopenharmony_ci						  ts->setup2);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	mutex_unlock(&ts->input->mutex);
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	return error ? error : count;
8458c2ecf20Sopenharmony_ci}
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(swap_xy);
8488c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(inv_x);
8498c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RW(inv_y);
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_cistatic struct attribute *rohm_ts_attrs[] = {
8528c2ecf20Sopenharmony_ci	&dev_attr_swap_xy.attr,
8538c2ecf20Sopenharmony_ci	&dev_attr_inv_x.attr,
8548c2ecf20Sopenharmony_ci	&dev_attr_inv_y.attr,
8558c2ecf20Sopenharmony_ci	NULL,
8568c2ecf20Sopenharmony_ci};
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_cistatic const struct attribute_group rohm_ts_attr_group = {
8598c2ecf20Sopenharmony_ci	.attrs = rohm_ts_attrs,
8608c2ecf20Sopenharmony_ci};
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_cistatic int rohm_ts_device_init(struct i2c_client *client, u8 setup2)
8638c2ecf20Sopenharmony_ci{
8648c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
8658c2ecf20Sopenharmony_ci	int error;
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci	disable_irq(client->irq);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	/*
8708c2ecf20Sopenharmony_ci	 * Wait 200usec for reset
8718c2ecf20Sopenharmony_ci	 */
8728c2ecf20Sopenharmony_ci	udelay(200);
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	/* Release analog reset */
8758c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, SYSTEM,
8768c2ecf20Sopenharmony_ci					  ANALOG_POWER_ON | CPU_POWER_OFF);
8778c2ecf20Sopenharmony_ci	if (error)
8788c2ecf20Sopenharmony_ci		return error;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	/* Waiting for the analog warm-up, max. 200usec */
8818c2ecf20Sopenharmony_ci	udelay(200);
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	/* clear all interrupts */
8848c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
8858c2ecf20Sopenharmony_ci	if (error)
8868c2ecf20Sopenharmony_ci		return error;
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
8898c2ecf20Sopenharmony_ci	if (error)
8908c2ecf20Sopenharmony_ci		return error;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, COMMON_SETUP1, 0);
8938c2ecf20Sopenharmony_ci	if (error)
8948c2ecf20Sopenharmony_ci		return error;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, COMMON_SETUP2, setup2);
8978c2ecf20Sopenharmony_ci	if (error)
8988c2ecf20Sopenharmony_ci		return error;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, COMMON_SETUP3,
9018c2ecf20Sopenharmony_ci					  SEL_TBL_DEFAULT | EN_MULTI);
9028c2ecf20Sopenharmony_ci	if (error)
9038c2ecf20Sopenharmony_ci		return error;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, THRESHOLD_GESTURE,
9068c2ecf20Sopenharmony_ci					  THRESHOLD_GESTURE_DEFAULT);
9078c2ecf20Sopenharmony_ci	if (error)
9088c2ecf20Sopenharmony_ci		return error;
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INTERVAL_TIME,
9118c2ecf20Sopenharmony_ci					  INTERVAL_TIME_DEFAULT);
9128c2ecf20Sopenharmony_ci	if (error)
9138c2ecf20Sopenharmony_ci		return error;
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, CPU_FREQ, CPU_FREQ_10MHZ);
9168c2ecf20Sopenharmony_ci	if (error)
9178c2ecf20Sopenharmony_ci		return error;
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, PRM_SWOFF_TIME,
9208c2ecf20Sopenharmony_ci					  PRM_SWOFF_TIME_DEFAULT);
9218c2ecf20Sopenharmony_ci	if (error)
9228c2ecf20Sopenharmony_ci		return error;
9238c2ecf20Sopenharmony_ci
9248c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, ADC_CTRL, ADC_DIV_DEFAULT);
9258c2ecf20Sopenharmony_ci	if (error)
9268c2ecf20Sopenharmony_ci		return error;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, ADC_WAIT, ADC_WAIT_DEFAULT);
9298c2ecf20Sopenharmony_ci	if (error)
9308c2ecf20Sopenharmony_ci		return error;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	/*
9338c2ecf20Sopenharmony_ci	 * Panel setup, these values change with the panel.
9348c2ecf20Sopenharmony_ci	 */
9358c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, STEP_X, STEP_X_DEFAULT);
9368c2ecf20Sopenharmony_ci	if (error)
9378c2ecf20Sopenharmony_ci		return error;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, STEP_Y, STEP_Y_DEFAULT);
9408c2ecf20Sopenharmony_ci	if (error)
9418c2ecf20Sopenharmony_ci		return error;
9428c2ecf20Sopenharmony_ci
9438c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, OFFSET_X, OFFSET_X_DEFAULT);
9448c2ecf20Sopenharmony_ci	if (error)
9458c2ecf20Sopenharmony_ci		return error;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, OFFSET_Y, OFFSET_Y_DEFAULT);
9488c2ecf20Sopenharmony_ci	if (error)
9498c2ecf20Sopenharmony_ci		return error;
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, THRESHOLD_TOUCH,
9528c2ecf20Sopenharmony_ci					  THRESHOLD_TOUCH_DEFAULT);
9538c2ecf20Sopenharmony_ci	if (error)
9548c2ecf20Sopenharmony_ci		return error;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, EVR_XY, EVR_XY_DEFAULT);
9578c2ecf20Sopenharmony_ci	if (error)
9588c2ecf20Sopenharmony_ci		return error;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, EVR_X, EVR_X_DEFAULT);
9618c2ecf20Sopenharmony_ci	if (error)
9628c2ecf20Sopenharmony_ci		return error;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, EVR_Y, EVR_Y_DEFAULT);
9658c2ecf20Sopenharmony_ci	if (error)
9668c2ecf20Sopenharmony_ci		return error;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	/* Fixed value settings */
9698c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, CALIBRATION_ADJUST,
9708c2ecf20Sopenharmony_ci					  CALIBRATION_ADJUST_DEFAULT);
9718c2ecf20Sopenharmony_ci	if (error)
9728c2ecf20Sopenharmony_ci		return error;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, SWCONT, SWCONT_DEFAULT);
9758c2ecf20Sopenharmony_ci	if (error)
9768c2ecf20Sopenharmony_ci		return error;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, TEST1,
9798c2ecf20Sopenharmony_ci					  DUALTOUCH_STABILIZE_ON |
9808c2ecf20Sopenharmony_ci					  DUALTOUCH_REG_ON);
9818c2ecf20Sopenharmony_ci	if (error)
9828c2ecf20Sopenharmony_ci		return error;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	error = rohm_ts_load_firmware(client, BU21023_FIRMWARE_NAME);
9858c2ecf20Sopenharmony_ci	if (error) {
9868c2ecf20Sopenharmony_ci		dev_err(dev, "failed to load firmware: %d\n", error);
9878c2ecf20Sopenharmony_ci		return error;
9888c2ecf20Sopenharmony_ci	}
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	/*
9918c2ecf20Sopenharmony_ci	 * Manual calibration results are not changed in same environment.
9928c2ecf20Sopenharmony_ci	 * If the force calibration is performed,
9938c2ecf20Sopenharmony_ci	 * the controller will not require calibration request interrupt
9948c2ecf20Sopenharmony_ci	 * when the typical values are set to the calibration registers.
9958c2ecf20Sopenharmony_ci	 */
9968c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
9978c2ecf20Sopenharmony_ci					  CALIBRATION_REG1_DEFAULT);
9988c2ecf20Sopenharmony_ci	if (error)
9998c2ecf20Sopenharmony_ci		return error;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
10028c2ecf20Sopenharmony_ci					  CALIBRATION_REG2_DEFAULT);
10038c2ecf20Sopenharmony_ci	if (error)
10048c2ecf20Sopenharmony_ci		return error;
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
10078c2ecf20Sopenharmony_ci					  CALIBRATION_REG3_DEFAULT);
10088c2ecf20Sopenharmony_ci	if (error)
10098c2ecf20Sopenharmony_ci		return error;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
10128c2ecf20Sopenharmony_ci					  FORCE_CALIBRATION_OFF);
10138c2ecf20Sopenharmony_ci	if (error)
10148c2ecf20Sopenharmony_ci		return error;
10158c2ecf20Sopenharmony_ci
10168c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
10178c2ecf20Sopenharmony_ci					  FORCE_CALIBRATION_ON);
10188c2ecf20Sopenharmony_ci	if (error)
10198c2ecf20Sopenharmony_ci		return error;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	/* Clear all interrupts */
10228c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
10238c2ecf20Sopenharmony_ci	if (error)
10248c2ecf20Sopenharmony_ci		return error;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	/* Enable coordinates update interrupt */
10278c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, INT_MASK,
10288c2ecf20Sopenharmony_ci					  CALIBRATION_DONE | SLEEP_OUT |
10298c2ecf20Sopenharmony_ci					  SLEEP_IN | PROGRAM_LOAD_DONE);
10308c2ecf20Sopenharmony_ci	if (error)
10318c2ecf20Sopenharmony_ci		return error;
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, ERR_MASK,
10348c2ecf20Sopenharmony_ci					  PROGRAM_LOAD_ERR | CPU_TIMEOUT |
10358c2ecf20Sopenharmony_ci					  ADC_TIMEOUT);
10368c2ecf20Sopenharmony_ci	if (error)
10378c2ecf20Sopenharmony_ci		return error;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	/* controller CPU power on */
10408c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, SYSTEM,
10418c2ecf20Sopenharmony_ci					  ANALOG_POWER_ON | CPU_POWER_ON);
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	enable_irq(client->irq);
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci	return error;
10468c2ecf20Sopenharmony_ci}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_cistatic int rohm_ts_power_off(struct i2c_client *client)
10498c2ecf20Sopenharmony_ci{
10508c2ecf20Sopenharmony_ci	int error;
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, SYSTEM,
10538c2ecf20Sopenharmony_ci					  ANALOG_POWER_ON | CPU_POWER_OFF);
10548c2ecf20Sopenharmony_ci	if (error) {
10558c2ecf20Sopenharmony_ci		dev_err(&client->dev,
10568c2ecf20Sopenharmony_ci			"failed to power off device CPU: %d\n", error);
10578c2ecf20Sopenharmony_ci		return error;
10588c2ecf20Sopenharmony_ci	}
10598c2ecf20Sopenharmony_ci
10608c2ecf20Sopenharmony_ci	error = i2c_smbus_write_byte_data(client, SYSTEM,
10618c2ecf20Sopenharmony_ci					  ANALOG_POWER_OFF | CPU_POWER_OFF);
10628c2ecf20Sopenharmony_ci	if (error)
10638c2ecf20Sopenharmony_ci		dev_err(&client->dev,
10648c2ecf20Sopenharmony_ci			"failed to power off the device: %d\n", error);
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	return error;
10678c2ecf20Sopenharmony_ci}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_cistatic int rohm_ts_open(struct input_dev *input_dev)
10708c2ecf20Sopenharmony_ci{
10718c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
10728c2ecf20Sopenharmony_ci	struct i2c_client *client = ts->client;
10738c2ecf20Sopenharmony_ci	int error;
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	if (!ts->initialized) {
10768c2ecf20Sopenharmony_ci		error = rohm_ts_device_init(client, ts->setup2);
10778c2ecf20Sopenharmony_ci		if (error) {
10788c2ecf20Sopenharmony_ci			dev_err(&client->dev,
10798c2ecf20Sopenharmony_ci				"device initialization failed: %d\n", error);
10808c2ecf20Sopenharmony_ci			return error;
10818c2ecf20Sopenharmony_ci		}
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci		ts->initialized = true;
10848c2ecf20Sopenharmony_ci	}
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	return 0;
10878c2ecf20Sopenharmony_ci}
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_cistatic void rohm_ts_close(struct input_dev *input_dev)
10908c2ecf20Sopenharmony_ci{
10918c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	rohm_ts_power_off(ts->client);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	ts->initialized = false;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic int rohm_bu21023_i2c_probe(struct i2c_client *client,
10998c2ecf20Sopenharmony_ci				  const struct i2c_device_id *id)
11008c2ecf20Sopenharmony_ci{
11018c2ecf20Sopenharmony_ci	struct device *dev = &client->dev;
11028c2ecf20Sopenharmony_ci	struct rohm_ts_data *ts;
11038c2ecf20Sopenharmony_ci	struct input_dev *input;
11048c2ecf20Sopenharmony_ci	int error;
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci	if (!client->irq) {
11078c2ecf20Sopenharmony_ci		dev_err(dev, "IRQ is not assigned\n");
11088c2ecf20Sopenharmony_ci		return -EINVAL;
11098c2ecf20Sopenharmony_ci	}
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	if (!client->adapter->algo->master_xfer) {
11128c2ecf20Sopenharmony_ci		dev_err(dev, "I2C level transfers not supported\n");
11138c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
11148c2ecf20Sopenharmony_ci	}
11158c2ecf20Sopenharmony_ci
11168c2ecf20Sopenharmony_ci	/* Turn off CPU just in case */
11178c2ecf20Sopenharmony_ci	error = rohm_ts_power_off(client);
11188c2ecf20Sopenharmony_ci	if (error)
11198c2ecf20Sopenharmony_ci		return error;
11208c2ecf20Sopenharmony_ci
11218c2ecf20Sopenharmony_ci	ts = devm_kzalloc(dev, sizeof(struct rohm_ts_data), GFP_KERNEL);
11228c2ecf20Sopenharmony_ci	if (!ts)
11238c2ecf20Sopenharmony_ci		return -ENOMEM;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	ts->client = client;
11268c2ecf20Sopenharmony_ci	ts->setup2 = MAF_1SAMPLE;
11278c2ecf20Sopenharmony_ci	i2c_set_clientdata(client, ts);
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	input = devm_input_allocate_device(dev);
11308c2ecf20Sopenharmony_ci	if (!input)
11318c2ecf20Sopenharmony_ci		return -ENOMEM;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	input->name = BU21023_NAME;
11348c2ecf20Sopenharmony_ci	input->id.bustype = BUS_I2C;
11358c2ecf20Sopenharmony_ci	input->open = rohm_ts_open;
11368c2ecf20Sopenharmony_ci	input->close = rohm_ts_close;
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	ts->input = input;
11398c2ecf20Sopenharmony_ci	input_set_drvdata(input, ts);
11408c2ecf20Sopenharmony_ci
11418c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_MT_POSITION_X,
11428c2ecf20Sopenharmony_ci			     ROHM_TS_ABS_X_MIN, ROHM_TS_ABS_X_MAX, 0, 0);
11438c2ecf20Sopenharmony_ci	input_set_abs_params(input, ABS_MT_POSITION_Y,
11448c2ecf20Sopenharmony_ci			     ROHM_TS_ABS_Y_MIN, ROHM_TS_ABS_Y_MAX, 0, 0);
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	error = input_mt_init_slots(input, MAX_CONTACTS,
11478c2ecf20Sopenharmony_ci				    INPUT_MT_DIRECT | INPUT_MT_TRACK |
11488c2ecf20Sopenharmony_ci				    INPUT_MT_DROP_UNUSED);
11498c2ecf20Sopenharmony_ci	if (error) {
11508c2ecf20Sopenharmony_ci		dev_err(dev, "failed to multi touch slots initialization\n");
11518c2ecf20Sopenharmony_ci		return error;
11528c2ecf20Sopenharmony_ci	}
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci	error = devm_request_threaded_irq(dev, client->irq,
11558c2ecf20Sopenharmony_ci					  NULL, rohm_ts_soft_irq,
11568c2ecf20Sopenharmony_ci					  IRQF_ONESHOT, client->name, ts);
11578c2ecf20Sopenharmony_ci	if (error) {
11588c2ecf20Sopenharmony_ci		dev_err(dev, "failed to request IRQ: %d\n", error);
11598c2ecf20Sopenharmony_ci		return error;
11608c2ecf20Sopenharmony_ci	}
11618c2ecf20Sopenharmony_ci
11628c2ecf20Sopenharmony_ci	error = input_register_device(input);
11638c2ecf20Sopenharmony_ci	if (error) {
11648c2ecf20Sopenharmony_ci		dev_err(dev, "failed to register input device: %d\n", error);
11658c2ecf20Sopenharmony_ci		return error;
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_ci	error = devm_device_add_group(dev, &rohm_ts_attr_group);
11698c2ecf20Sopenharmony_ci	if (error) {
11708c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create sysfs group: %d\n", error);
11718c2ecf20Sopenharmony_ci		return error;
11728c2ecf20Sopenharmony_ci	}
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	return error;
11758c2ecf20Sopenharmony_ci}
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_cistatic const struct i2c_device_id rohm_bu21023_i2c_id[] = {
11788c2ecf20Sopenharmony_ci	{ BU21023_NAME, 0 },
11798c2ecf20Sopenharmony_ci	{ /* sentinel */ }
11808c2ecf20Sopenharmony_ci};
11818c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, rohm_bu21023_i2c_id);
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_cistatic struct i2c_driver rohm_bu21023_i2c_driver = {
11848c2ecf20Sopenharmony_ci	.driver = {
11858c2ecf20Sopenharmony_ci		.name = BU21023_NAME,
11868c2ecf20Sopenharmony_ci	},
11878c2ecf20Sopenharmony_ci	.probe = rohm_bu21023_i2c_probe,
11888c2ecf20Sopenharmony_ci	.id_table = rohm_bu21023_i2c_id,
11898c2ecf20Sopenharmony_ci};
11908c2ecf20Sopenharmony_cimodule_i2c_driver(rohm_bu21023_i2c_driver);
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ROHM BU21023/24 Touchscreen driver");
11938c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
11948c2ecf20Sopenharmony_ciMODULE_AUTHOR("ROHM Co., Ltd.");
1195