Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.9.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * ROHM BU21023/24 Dual touch support resistive touch screen driver
   4 * Copyright (C) 2012 ROHM CO.,LTD.
 
 
 
 
 
 
 
 
 
   5 */
   6#include <linux/delay.h>
   7#include <linux/firmware.h>
   8#include <linux/i2c.h>
   9#include <linux/input.h>
  10#include <linux/input/mt.h>
  11#include <linux/interrupt.h>
  12#include <linux/module.h>
  13#include <linux/slab.h>
  14
  15#define BU21023_NAME			"bu21023_ts"
  16#define BU21023_FIRMWARE_NAME		"bu21023.bin"
  17
  18#define MAX_CONTACTS			2
  19
  20#define AXIS_ADJUST			4
  21#define AXIS_OFFSET			8
  22
  23#define FIRMWARE_BLOCK_SIZE		32U
  24#define FIRMWARE_RETRY_MAX		4
  25
  26#define SAMPLING_DELAY			12	/* msec */
  27
  28#define CALIBRATION_RETRY_MAX		6
  29
  30#define ROHM_TS_ABS_X_MIN		40
  31#define ROHM_TS_ABS_X_MAX		990
  32#define ROHM_TS_ABS_Y_MIN		160
  33#define ROHM_TS_ABS_Y_MAX		920
  34#define ROHM_TS_DISPLACEMENT_MAX	0	/* zero for infinite */
  35
  36/*
  37 * BU21023GUL/BU21023MUV/BU21024FV-M registers map
  38 */
  39#define VADOUT_YP_H		0x00
  40#define VADOUT_YP_L		0x01
  41#define VADOUT_XP_H		0x02
  42#define VADOUT_XP_L		0x03
  43#define VADOUT_YN_H		0x04
  44#define VADOUT_YN_L		0x05
  45#define VADOUT_XN_H		0x06
  46#define VADOUT_XN_L		0x07
  47
  48#define PRM1_X_H		0x08
  49#define PRM1_X_L		0x09
  50#define PRM1_Y_H		0x0a
  51#define PRM1_Y_L		0x0b
  52#define PRM2_X_H		0x0c
  53#define PRM2_X_L		0x0d
  54#define PRM2_Y_H		0x0e
  55#define PRM2_Y_L		0x0f
  56
  57#define MLT_PRM_MONI_X		0x10
  58#define MLT_PRM_MONI_Y		0x11
  59
  60#define DEBUG_MONI_1		0x12
  61#define DEBUG_MONI_2		0x13
  62
  63#define VADOUT_ZX_H		0x14
  64#define VADOUT_ZX_L		0x15
  65#define VADOUT_ZY_H		0x16
  66#define VADOUT_ZY_L		0x17
  67
  68#define Z_PARAM_H		0x18
  69#define Z_PARAM_L		0x19
  70
  71/*
  72 * Value for VADOUT_*_L
  73 */
  74#define VADOUT_L_MASK		0x01
  75
  76/*
  77 * Value for PRM*_*_L
  78 */
  79#define PRM_L_MASK		0x01
  80
  81#define POS_X1_H		0x20
  82#define POS_X1_L		0x21
  83#define POS_Y1_H		0x22
  84#define POS_Y1_L		0x23
  85#define POS_X2_H		0x24
  86#define POS_X2_L		0x25
  87#define POS_Y2_H		0x26
  88#define POS_Y2_L		0x27
  89
  90/*
  91 * Value for POS_*_L
  92 */
  93#define POS_L_MASK		0x01
  94
  95#define TOUCH			0x28
  96#define TOUCH_DETECT		0x01
  97
  98#define TOUCH_GESTURE		0x29
  99#define SINGLE_TOUCH		0x01
 100#define DUAL_TOUCH		0x03
 101#define TOUCH_MASK		0x03
 102#define CALIBRATION_REQUEST	0x04
 103#define CALIBRATION_STATUS	0x08
 104#define CALIBRATION_MASK	0x0c
 105#define GESTURE_SPREAD		0x10
 106#define GESTURE_PINCH		0x20
 107#define GESTURE_ROTATE_R	0x40
 108#define GESTURE_ROTATE_L	0x80
 109
 110#define INT_STATUS		0x2a
 111#define INT_MASK		0x3d
 112#define INT_CLEAR		0x3e
 113
 114/*
 115 * Values for INT_*
 116 */
 117#define COORD_UPDATE		0x01
 118#define CALIBRATION_DONE	0x02
 119#define SLEEP_IN		0x04
 120#define SLEEP_OUT		0x08
 121#define PROGRAM_LOAD_DONE	0x10
 122#define ERROR			0x80
 123#define INT_ALL			0x9f
 124
 125#define ERR_STATUS		0x2b
 126#define ERR_MASK		0x3f
 127
 128/*
 129 * Values for ERR_*
 130 */
 131#define ADC_TIMEOUT		0x01
 132#define CPU_TIMEOUT		0x02
 133#define CALIBRATION_ERR		0x04
 134#define PROGRAM_LOAD_ERR	0x10
 135
 136#define COMMON_SETUP1			0x30
 137#define PROGRAM_LOAD_HOST		0x02
 138#define PROGRAM_LOAD_EEPROM		0x03
 139#define CENSOR_4PORT			0x04
 140#define CENSOR_8PORT			0x00	/* Not supported by BU21023 */
 141#define CALIBRATION_TYPE_DEFAULT	0x08
 142#define CALIBRATION_TYPE_SPECIAL	0x00
 143#define INT_ACTIVE_HIGH			0x10
 144#define INT_ACTIVE_LOW			0x00
 145#define AUTO_CALIBRATION		0x40
 146#define MANUAL_CALIBRATION		0x00
 147#define COMMON_SETUP1_DEFAULT		0x4e
 148
 149#define COMMON_SETUP2		0x31
 150#define MAF_NONE		0x00
 151#define MAF_1SAMPLE		0x01
 152#define MAF_3SAMPLES		0x02
 153#define MAF_5SAMPLES		0x03
 154#define INV_Y			0x04
 155#define INV_X			0x08
 156#define SWAP_XY			0x10
 157
 158#define COMMON_SETUP3		0x32
 159#define EN_SLEEP		0x01
 160#define EN_MULTI		0x02
 161#define EN_GESTURE		0x04
 162#define EN_INTVL		0x08
 163#define SEL_STEP		0x10
 164#define SEL_MULTI		0x20
 165#define SEL_TBL_DEFAULT		0x40
 166
 167#define INTERVAL_TIME		0x33
 168#define INTERVAL_TIME_DEFAULT	0x10
 169
 170#define STEP_X			0x34
 171#define STEP_X_DEFAULT		0x41
 172
 173#define STEP_Y			0x35
 174#define STEP_Y_DEFAULT		0x8d
 175
 176#define OFFSET_X		0x38
 177#define OFFSET_X_DEFAULT	0x0c
 178
 179#define OFFSET_Y		0x39
 180#define OFFSET_Y_DEFAULT	0x0c
 181
 182#define THRESHOLD_TOUCH		0x3a
 183#define THRESHOLD_TOUCH_DEFAULT	0xa0
 184
 185#define THRESHOLD_GESTURE		0x3b
 186#define THRESHOLD_GESTURE_DEFAULT	0x17
 187
 188#define SYSTEM			0x40
 189#define ANALOG_POWER_ON		0x01
 190#define ANALOG_POWER_OFF	0x00
 191#define CPU_POWER_ON		0x02
 192#define CPU_POWER_OFF		0x00
 193
 194#define FORCE_CALIBRATION	0x42
 195#define FORCE_CALIBRATION_ON	0x01
 196#define FORCE_CALIBRATION_OFF	0x00
 197
 198#define CPU_FREQ		0x50	/* 10 / (reg + 1) MHz */
 199#define CPU_FREQ_10MHZ		0x00
 200#define CPU_FREQ_5MHZ		0x01
 201#define CPU_FREQ_1MHZ		0x09
 202
 203#define EEPROM_ADDR		0x51
 204
 205#define CALIBRATION_ADJUST		0x52
 206#define CALIBRATION_ADJUST_DEFAULT	0x00
 207
 208#define THRESHOLD_SLEEP_IN	0x53
 209
 210#define EVR_XY			0x56
 211#define EVR_XY_DEFAULT		0x10
 212
 213#define PRM_SWOFF_TIME		0x57
 214#define PRM_SWOFF_TIME_DEFAULT	0x04
 215
 216#define PROGRAM_VERSION		0x5f
 217
 218#define ADC_CTRL		0x60
 219#define ADC_DIV_MASK		0x1f	/* The minimum value is 4 */
 220#define ADC_DIV_DEFAULT		0x08
 221
 222#define ADC_WAIT		0x61
 223#define ADC_WAIT_DEFAULT	0x0a
 224
 225#define SWCONT			0x62
 226#define SWCONT_DEFAULT		0x0f
 227
 228#define EVR_X			0x63
 229#define EVR_X_DEFAULT		0x86
 230
 231#define EVR_Y			0x64
 232#define EVR_Y_DEFAULT		0x64
 233
 234#define TEST1			0x65
 235#define DUALTOUCH_STABILIZE_ON	0x01
 236#define DUALTOUCH_STABILIZE_OFF	0x00
 237#define DUALTOUCH_REG_ON	0x20
 238#define DUALTOUCH_REG_OFF	0x00
 239
 240#define CALIBRATION_REG1		0x68
 241#define CALIBRATION_REG1_DEFAULT	0xd9
 242
 243#define CALIBRATION_REG2		0x69
 244#define CALIBRATION_REG2_DEFAULT	0x36
 245
 246#define CALIBRATION_REG3		0x6a
 247#define CALIBRATION_REG3_DEFAULT	0x32
 248
 249#define EX_ADDR_H		0x70
 250#define EX_ADDR_L		0x71
 251#define EX_WDAT			0x72
 252#define EX_RDAT			0x73
 253#define EX_CHK_SUM1		0x74
 254#define EX_CHK_SUM2		0x75
 255#define EX_CHK_SUM3		0x76
 256
 257struct rohm_ts_data {
 258	struct i2c_client *client;
 259	struct input_dev *input;
 260
 261	bool initialized;
 262
 263	unsigned int contact_count[MAX_CONTACTS + 1];
 264	int finger_count;
 265
 266	u8 setup2;
 267};
 268
 269/*
 270 * rohm_i2c_burst_read - execute combined I2C message for ROHM BU21023/24
 271 * @client: Handle to ROHM BU21023/24
 272 * @start: Where to start read address from ROHM BU21023/24
 273 * @buf: Where to store read data from ROHM BU21023/24
 274 * @len: How many bytes to read
 275 *
 276 * Returns negative errno, else zero on success.
 277 *
 278 * Note
 279 * In BU21023/24 burst read, stop condition is needed after "address write".
 280 * Therefore, transmission is performed in 2 steps.
 281 */
 282static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
 283			       size_t len)
 284{
 285	struct i2c_adapter *adap = client->adapter;
 286	struct i2c_msg msg[2];
 287	int i, ret = 0;
 288
 289	msg[0].addr = client->addr;
 290	msg[0].flags = 0;
 291	msg[0].len = 1;
 292	msg[0].buf = &start;
 293
 294	msg[1].addr = client->addr;
 295	msg[1].flags = I2C_M_RD;
 296	msg[1].len = len;
 297	msg[1].buf = buf;
 298
 299	i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
 300
 301	for (i = 0; i < 2; i++) {
 302		if (__i2c_transfer(adap, &msg[i], 1) < 0) {
 303			ret = -EIO;
 304			break;
 305		}
 306	}
 307
 308	i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
 309
 310	return ret;
 311}
 312
 313static int rohm_ts_manual_calibration(struct rohm_ts_data *ts)
 314{
 315	struct i2c_client *client = ts->client;
 316	struct device *dev = &client->dev;
 317	u8 buf[33];	/* for PRM1_X_H(0x08)-TOUCH(0x28) */
 318
 319	int retry;
 320	bool success = false;
 321	bool first_time = true;
 322	bool calibration_done;
 323
 324	u8 reg1, reg2, reg3;
 325	s32 reg1_orig, reg2_orig, reg3_orig;
 326	s32 val;
 327
 328	int calib_x = 0, calib_y = 0;
 329	int reg_x, reg_y;
 330	int err_x, err_y;
 331
 332	int error, error2;
 333	int i;
 334
 335	reg1_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG1);
 336	if (reg1_orig < 0)
 337		return reg1_orig;
 338
 339	reg2_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG2);
 340	if (reg2_orig < 0)
 341		return reg2_orig;
 342
 343	reg3_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG3);
 344	if (reg3_orig < 0)
 345		return reg3_orig;
 346
 347	error = i2c_smbus_write_byte_data(client, INT_MASK,
 348					  COORD_UPDATE | SLEEP_IN | SLEEP_OUT |
 349					  PROGRAM_LOAD_DONE);
 350	if (error)
 351		goto out;
 352
 353	error = i2c_smbus_write_byte_data(client, TEST1,
 354					  DUALTOUCH_STABILIZE_ON);
 355	if (error)
 356		goto out;
 357
 358	for (retry = 0; retry < CALIBRATION_RETRY_MAX; retry++) {
 359		/* wait 2 sampling for update */
 360		mdelay(2 * SAMPLING_DELAY);
 361
 362#define READ_CALIB_BUF(reg)	buf[((reg) - PRM1_X_H)]
 363
 364		error = rohm_i2c_burst_read(client, PRM1_X_H, buf, sizeof(buf));
 365		if (error)
 366			goto out;
 367
 368		if (READ_CALIB_BUF(TOUCH) & TOUCH_DETECT)
 369			continue;
 370
 371		if (first_time) {
 372			/* generate calibration parameter */
 373			calib_x = ((int)READ_CALIB_BUF(PRM1_X_H) << 2 |
 374				READ_CALIB_BUF(PRM1_X_L)) - AXIS_OFFSET;
 375			calib_y = ((int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
 376				READ_CALIB_BUF(PRM1_Y_L)) - AXIS_OFFSET;
 377
 378			error = i2c_smbus_write_byte_data(client, TEST1,
 379				DUALTOUCH_STABILIZE_ON | DUALTOUCH_REG_ON);
 380			if (error)
 381				goto out;
 382
 383			first_time = false;
 384		} else {
 385			/* generate adjustment parameter */
 386			err_x = (int)READ_CALIB_BUF(PRM1_X_H) << 2 |
 387				READ_CALIB_BUF(PRM1_X_L);
 388			err_y = (int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
 389				READ_CALIB_BUF(PRM1_Y_L);
 390
 391			/* X axis ajust */
 392			if (err_x <= 4)
 393				calib_x -= AXIS_ADJUST;
 394			else if (err_x >= 60)
 395				calib_x += AXIS_ADJUST;
 396
 397			/* Y axis ajust */
 398			if (err_y <= 4)
 399				calib_y -= AXIS_ADJUST;
 400			else if (err_y >= 60)
 401				calib_y += AXIS_ADJUST;
 402		}
 403
 404		/* generate calibration setting value */
 405		reg_x = calib_x + ((calib_x & 0x200) << 1);
 406		reg_y = calib_y + ((calib_y & 0x200) << 1);
 407
 408		/* convert for register format */
 409		reg1 = reg_x >> 3;
 410		reg2 = (reg_y & 0x7) << 4 | (reg_x & 0x7);
 411		reg3 = reg_y >> 3;
 412
 413		error = i2c_smbus_write_byte_data(client,
 414						  CALIBRATION_REG1, reg1);
 415		if (error)
 416			goto out;
 417
 418		error = i2c_smbus_write_byte_data(client,
 419						  CALIBRATION_REG2, reg2);
 420		if (error)
 421			goto out;
 422
 423		error = i2c_smbus_write_byte_data(client,
 424						  CALIBRATION_REG3, reg3);
 425		if (error)
 426			goto out;
 427
 428		/*
 429		 * force calibration sequcence
 430		 */
 431		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
 432						  FORCE_CALIBRATION_OFF);
 433		if (error)
 434			goto out;
 435
 436		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
 437						  FORCE_CALIBRATION_ON);
 438		if (error)
 439			goto out;
 440
 441		/* clear all interrupts */
 442		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 443		if (error)
 444			goto out;
 445
 446		/*
 447		 * Wait for the status change of calibration, max 10 sampling
 448		 */
 449		calibration_done = false;
 450
 451		for (i = 0; i < 10; i++) {
 452			mdelay(SAMPLING_DELAY);
 453
 454			val = i2c_smbus_read_byte_data(client, TOUCH_GESTURE);
 455			if (!(val & CALIBRATION_MASK)) {
 456				calibration_done = true;
 457				break;
 458			} else if (val < 0) {
 459				error = val;
 460				goto out;
 461			}
 462		}
 463
 464		if (calibration_done) {
 465			val = i2c_smbus_read_byte_data(client, INT_STATUS);
 466			if (val == CALIBRATION_DONE) {
 467				success = true;
 468				break;
 469			} else if (val < 0) {
 470				error = val;
 471				goto out;
 472			}
 473		} else {
 474			dev_warn(dev, "calibration timeout\n");
 475		}
 476	}
 477
 478	if (!success) {
 479		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
 480						  reg1_orig);
 481		if (error)
 482			goto out;
 483
 484		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
 485						  reg2_orig);
 486		if (error)
 487			goto out;
 488
 489		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
 490						  reg3_orig);
 491		if (error)
 492			goto out;
 493
 494		/* calibration data enable */
 495		error = i2c_smbus_write_byte_data(client, TEST1,
 496						  DUALTOUCH_STABILIZE_ON |
 497						  DUALTOUCH_REG_ON);
 498		if (error)
 499			goto out;
 500
 501		/* wait 10 sampling */
 502		mdelay(10 * SAMPLING_DELAY);
 503
 504		error = -EBUSY;
 505	}
 506
 507out:
 508	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 509	if (!error2)
 510		/* Clear all interrupts */
 511		error2 = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 512
 513	return error ? error : error2;
 514}
 515
 516static const unsigned int untouch_threshold[3] = { 0, 1, 5 };
 517static const unsigned int single_touch_threshold[3] = { 0, 0, 4 };
 518static const unsigned int dual_touch_threshold[3] = { 10, 8, 0 };
 519
 520static irqreturn_t rohm_ts_soft_irq(int irq, void *dev_id)
 521{
 522	struct rohm_ts_data *ts = dev_id;
 523	struct i2c_client *client = ts->client;
 524	struct input_dev *input_dev = ts->input;
 525	struct device *dev = &client->dev;
 526
 527	u8 buf[10];	/* for POS_X1_H(0x20)-TOUCH_GESTURE(0x29) */
 528
 529	struct input_mt_pos pos[MAX_CONTACTS];
 530	int slots[MAX_CONTACTS];
 531	u8 touch_flags;
 532	unsigned int threshold;
 533	int finger_count = -1;
 534	int prev_finger_count = ts->finger_count;
 535	int count;
 536	int error;
 537	int i;
 538
 539	error = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 540	if (error)
 541		return IRQ_HANDLED;
 542
 543	/* Clear all interrupts */
 544	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 545	if (error)
 546		return IRQ_HANDLED;
 547
 548#define READ_POS_BUF(reg)	buf[((reg) - POS_X1_H)]
 549
 550	error = rohm_i2c_burst_read(client, POS_X1_H, buf, sizeof(buf));
 551	if (error)
 552		return IRQ_HANDLED;
 553
 554	touch_flags = READ_POS_BUF(TOUCH_GESTURE) & TOUCH_MASK;
 555	if (touch_flags) {
 556		/* generate coordinates */
 557		pos[0].x = ((s16)READ_POS_BUF(POS_X1_H) << 2) |
 558			   READ_POS_BUF(POS_X1_L);
 559		pos[0].y = ((s16)READ_POS_BUF(POS_Y1_H) << 2) |
 560			   READ_POS_BUF(POS_Y1_L);
 561		pos[1].x = ((s16)READ_POS_BUF(POS_X2_H) << 2) |
 562			   READ_POS_BUF(POS_X2_L);
 563		pos[1].y = ((s16)READ_POS_BUF(POS_Y2_H) << 2) |
 564			   READ_POS_BUF(POS_Y2_L);
 565	}
 566
 567	switch (touch_flags) {
 568	case 0:
 569		threshold = untouch_threshold[prev_finger_count];
 570		if (++ts->contact_count[0] >= threshold)
 571			finger_count = 0;
 572		break;
 573
 574	case SINGLE_TOUCH:
 575		threshold = single_touch_threshold[prev_finger_count];
 576		if (++ts->contact_count[1] >= threshold)
 577			finger_count = 1;
 578
 579		if (finger_count == 1) {
 580			if (pos[1].x != 0 && pos[1].y != 0) {
 581				pos[0].x = pos[1].x;
 582				pos[0].y = pos[1].y;
 583				pos[1].x = 0;
 584				pos[1].y = 0;
 585			}
 586		}
 587		break;
 588
 589	case DUAL_TOUCH:
 590		threshold = dual_touch_threshold[prev_finger_count];
 591		if (++ts->contact_count[2] >= threshold)
 592			finger_count = 2;
 593		break;
 594
 595	default:
 596		dev_dbg(dev,
 597			"Three or more touches are not supported\n");
 598		return IRQ_HANDLED;
 599	}
 600
 601	if (finger_count >= 0) {
 602		if (prev_finger_count != finger_count) {
 603			count = ts->contact_count[finger_count];
 604			memset(ts->contact_count, 0, sizeof(ts->contact_count));
 605			ts->contact_count[finger_count] = count;
 606		}
 607
 608		input_mt_assign_slots(input_dev, slots, pos,
 609				      finger_count, ROHM_TS_DISPLACEMENT_MAX);
 610
 611		for (i = 0; i < finger_count; i++) {
 612			input_mt_slot(input_dev, slots[i]);
 613			input_mt_report_slot_state(input_dev,
 614						   MT_TOOL_FINGER, true);
 615			input_report_abs(input_dev,
 616					 ABS_MT_POSITION_X, pos[i].x);
 617			input_report_abs(input_dev,
 618					 ABS_MT_POSITION_Y, pos[i].y);
 619		}
 620
 621		input_mt_sync_frame(input_dev);
 622		input_mt_report_pointer_emulation(input_dev, true);
 623		input_sync(input_dev);
 624
 625		ts->finger_count = finger_count;
 626	}
 627
 628	if (READ_POS_BUF(TOUCH_GESTURE) & CALIBRATION_REQUEST) {
 629		error = rohm_ts_manual_calibration(ts);
 630		if (error)
 631			dev_warn(dev, "manual calibration failed: %d\n",
 632				 error);
 633	}
 634
 635	i2c_smbus_write_byte_data(client, INT_MASK,
 636				  CALIBRATION_DONE | SLEEP_OUT | SLEEP_IN |
 637				  PROGRAM_LOAD_DONE);
 638
 639	return IRQ_HANDLED;
 640}
 641
 642static int rohm_ts_load_firmware(struct i2c_client *client,
 643				 const char *firmware_name)
 644{
 645	struct device *dev = &client->dev;
 646	const struct firmware *fw;
 647	s32 status;
 648	unsigned int offset, len, xfer_len;
 649	unsigned int retry = 0;
 650	int error, error2;
 651
 652	error = request_firmware(&fw, firmware_name, dev);
 653	if (error) {
 654		dev_err(dev, "unable to retrieve firmware %s: %d\n",
 655			firmware_name, error);
 656		return error;
 657	}
 658
 659	error = i2c_smbus_write_byte_data(client, INT_MASK,
 660					  COORD_UPDATE | CALIBRATION_DONE |
 661					  SLEEP_IN | SLEEP_OUT);
 662	if (error)
 663		goto out;
 664
 665	do {
 666		if (retry) {
 667			dev_warn(dev, "retrying firmware load\n");
 668
 669			/* settings for retry */
 670			error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
 671			if (error)
 672				goto out;
 673		}
 674
 675		error = i2c_smbus_write_byte_data(client, EX_ADDR_H, 0);
 676		if (error)
 677			goto out;
 678
 679		error = i2c_smbus_write_byte_data(client, EX_ADDR_L, 0);
 680		if (error)
 681			goto out;
 682
 683		error = i2c_smbus_write_byte_data(client, COMMON_SETUP1,
 684						  COMMON_SETUP1_DEFAULT);
 685		if (error)
 686			goto out;
 687
 688		/* firmware load to the device */
 689		offset = 0;
 690		len = fw->size;
 691
 692		while (len) {
 693			xfer_len = min(FIRMWARE_BLOCK_SIZE, len);
 694
 695			error = i2c_smbus_write_i2c_block_data(client, EX_WDAT,
 696						xfer_len, &fw->data[offset]);
 697			if (error)
 698				goto out;
 699
 700			len -= xfer_len;
 701			offset += xfer_len;
 702		}
 703
 704		/* check firmware load result */
 705		status = i2c_smbus_read_byte_data(client, INT_STATUS);
 706		if (status < 0) {
 707			error = status;
 708			goto out;
 709		}
 710
 711		/* clear all interrupts */
 712		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 713		if (error)
 714			goto out;
 715
 716		if (status == PROGRAM_LOAD_DONE)
 717			break;
 718
 719		error = -EIO;
 720	} while (++retry <= FIRMWARE_RETRY_MAX);
 721
 722out:
 723	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 724
 725	release_firmware(fw);
 726
 727	return error ? error : error2;
 728}
 729
 730static ssize_t swap_xy_show(struct device *dev, struct device_attribute *attr,
 731			    char *buf)
 732{
 733	struct i2c_client *client = to_i2c_client(dev);
 734	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 735
 736	return sprintf(buf, "%d\n", !!(ts->setup2 & SWAP_XY));
 737}
 738
 739static ssize_t swap_xy_store(struct device *dev, struct device_attribute *attr,
 740			     const char *buf, size_t count)
 741{
 742	struct i2c_client *client = to_i2c_client(dev);
 743	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 744	unsigned int val;
 745	int error;
 746
 747	error = kstrtouint(buf, 0, &val);
 748	if (error)
 749		return error;
 750
 751	error = mutex_lock_interruptible(&ts->input->mutex);
 752	if (error)
 753		return error;
 754
 755	if (val)
 756		ts->setup2 |= SWAP_XY;
 757	else
 758		ts->setup2 &= ~SWAP_XY;
 759
 760	if (ts->initialized)
 761		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
 762						  ts->setup2);
 763
 764	mutex_unlock(&ts->input->mutex);
 765
 766	return error ? error : count;
 767}
 768
 769static ssize_t inv_x_show(struct device *dev, struct device_attribute *attr,
 770			  char *buf)
 771{
 772	struct i2c_client *client = to_i2c_client(dev);
 773	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 774
 775	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_X));
 776}
 777
 778static ssize_t inv_x_store(struct device *dev, struct device_attribute *attr,
 779			   const char *buf, size_t count)
 780{
 781	struct i2c_client *client = to_i2c_client(dev);
 782	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 783	unsigned int val;
 784	int error;
 785
 786	error = kstrtouint(buf, 0, &val);
 787	if (error)
 788		return error;
 789
 790	error = mutex_lock_interruptible(&ts->input->mutex);
 791	if (error)
 792		return error;
 793
 794	if (val)
 795		ts->setup2 |= INV_X;
 796	else
 797		ts->setup2 &= ~INV_X;
 798
 799	if (ts->initialized)
 800		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
 801						  ts->setup2);
 802
 803	mutex_unlock(&ts->input->mutex);
 804
 805	return error ? error : count;
 806}
 807
 808static ssize_t inv_y_show(struct device *dev, struct device_attribute *attr,
 809			  char *buf)
 810{
 811	struct i2c_client *client = to_i2c_client(dev);
 812	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 813
 814	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_Y));
 815}
 816
 817static ssize_t inv_y_store(struct device *dev, struct device_attribute *attr,
 818			   const char *buf, size_t count)
 819{
 820	struct i2c_client *client = to_i2c_client(dev);
 821	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 822	unsigned int val;
 823	int error;
 824
 825	error = kstrtouint(buf, 0, &val);
 826	if (error)
 827		return error;
 828
 829	error = mutex_lock_interruptible(&ts->input->mutex);
 830	if (error)
 831		return error;
 832
 833	if (val)
 834		ts->setup2 |= INV_Y;
 835	else
 836		ts->setup2 &= ~INV_Y;
 837
 838	if (ts->initialized)
 839		error = i2c_smbus_write_byte_data(client, COMMON_SETUP2,
 840						  ts->setup2);
 841
 842	mutex_unlock(&ts->input->mutex);
 843
 844	return error ? error : count;
 845}
 846
 847static DEVICE_ATTR_RW(swap_xy);
 848static DEVICE_ATTR_RW(inv_x);
 849static DEVICE_ATTR_RW(inv_y);
 850
 851static struct attribute *rohm_ts_attrs[] = {
 852	&dev_attr_swap_xy.attr,
 853	&dev_attr_inv_x.attr,
 854	&dev_attr_inv_y.attr,
 855	NULL,
 856};
 857ATTRIBUTE_GROUPS(rohm_ts);
 
 
 
 858
 859static int rohm_ts_device_init(struct i2c_client *client, u8 setup2)
 860{
 861	struct device *dev = &client->dev;
 862	int error;
 863
 864	disable_irq(client->irq);
 865
 866	/*
 867	 * Wait 200usec for reset
 868	 */
 869	udelay(200);
 870
 871	/* Release analog reset */
 872	error = i2c_smbus_write_byte_data(client, SYSTEM,
 873					  ANALOG_POWER_ON | CPU_POWER_OFF);
 874	if (error)
 875		return error;
 876
 877	/* Waiting for the analog warm-up, max. 200usec */
 878	udelay(200);
 879
 880	/* clear all interrupts */
 881	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 882	if (error)
 883		return error;
 884
 885	error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
 886	if (error)
 887		return error;
 888
 889	error = i2c_smbus_write_byte_data(client, COMMON_SETUP1, 0);
 890	if (error)
 891		return error;
 892
 893	error = i2c_smbus_write_byte_data(client, COMMON_SETUP2, setup2);
 894	if (error)
 895		return error;
 896
 897	error = i2c_smbus_write_byte_data(client, COMMON_SETUP3,
 898					  SEL_TBL_DEFAULT | EN_MULTI);
 899	if (error)
 900		return error;
 901
 902	error = i2c_smbus_write_byte_data(client, THRESHOLD_GESTURE,
 903					  THRESHOLD_GESTURE_DEFAULT);
 904	if (error)
 905		return error;
 906
 907	error = i2c_smbus_write_byte_data(client, INTERVAL_TIME,
 908					  INTERVAL_TIME_DEFAULT);
 909	if (error)
 910		return error;
 911
 912	error = i2c_smbus_write_byte_data(client, CPU_FREQ, CPU_FREQ_10MHZ);
 913	if (error)
 914		return error;
 915
 916	error = i2c_smbus_write_byte_data(client, PRM_SWOFF_TIME,
 917					  PRM_SWOFF_TIME_DEFAULT);
 918	if (error)
 919		return error;
 920
 921	error = i2c_smbus_write_byte_data(client, ADC_CTRL, ADC_DIV_DEFAULT);
 922	if (error)
 923		return error;
 924
 925	error = i2c_smbus_write_byte_data(client, ADC_WAIT, ADC_WAIT_DEFAULT);
 926	if (error)
 927		return error;
 928
 929	/*
 930	 * Panel setup, these values change with the panel.
 931	 */
 932	error = i2c_smbus_write_byte_data(client, STEP_X, STEP_X_DEFAULT);
 933	if (error)
 934		return error;
 935
 936	error = i2c_smbus_write_byte_data(client, STEP_Y, STEP_Y_DEFAULT);
 937	if (error)
 938		return error;
 939
 940	error = i2c_smbus_write_byte_data(client, OFFSET_X, OFFSET_X_DEFAULT);
 941	if (error)
 942		return error;
 943
 944	error = i2c_smbus_write_byte_data(client, OFFSET_Y, OFFSET_Y_DEFAULT);
 945	if (error)
 946		return error;
 947
 948	error = i2c_smbus_write_byte_data(client, THRESHOLD_TOUCH,
 949					  THRESHOLD_TOUCH_DEFAULT);
 950	if (error)
 951		return error;
 952
 953	error = i2c_smbus_write_byte_data(client, EVR_XY, EVR_XY_DEFAULT);
 954	if (error)
 955		return error;
 956
 957	error = i2c_smbus_write_byte_data(client, EVR_X, EVR_X_DEFAULT);
 958	if (error)
 959		return error;
 960
 961	error = i2c_smbus_write_byte_data(client, EVR_Y, EVR_Y_DEFAULT);
 962	if (error)
 963		return error;
 964
 965	/* Fixed value settings */
 966	error = i2c_smbus_write_byte_data(client, CALIBRATION_ADJUST,
 967					  CALIBRATION_ADJUST_DEFAULT);
 968	if (error)
 969		return error;
 970
 971	error = i2c_smbus_write_byte_data(client, SWCONT, SWCONT_DEFAULT);
 972	if (error)
 973		return error;
 974
 975	error = i2c_smbus_write_byte_data(client, TEST1,
 976					  DUALTOUCH_STABILIZE_ON |
 977					  DUALTOUCH_REG_ON);
 978	if (error)
 979		return error;
 980
 981	error = rohm_ts_load_firmware(client, BU21023_FIRMWARE_NAME);
 982	if (error) {
 983		dev_err(dev, "failed to load firmware: %d\n", error);
 984		return error;
 985	}
 986
 987	/*
 988	 * Manual calibration results are not changed in same environment.
 989	 * If the force calibration is performed,
 990	 * the controller will not require calibration request interrupt
 991	 * when the typical values are set to the calibration registers.
 992	 */
 993	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
 994					  CALIBRATION_REG1_DEFAULT);
 995	if (error)
 996		return error;
 997
 998	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
 999					  CALIBRATION_REG2_DEFAULT);
1000	if (error)
1001		return error;
1002
1003	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
1004					  CALIBRATION_REG3_DEFAULT);
1005	if (error)
1006		return error;
1007
1008	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1009					  FORCE_CALIBRATION_OFF);
1010	if (error)
1011		return error;
1012
1013	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1014					  FORCE_CALIBRATION_ON);
1015	if (error)
1016		return error;
1017
1018	/* Clear all interrupts */
1019	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
1020	if (error)
1021		return error;
1022
1023	/* Enable coordinates update interrupt */
1024	error = i2c_smbus_write_byte_data(client, INT_MASK,
1025					  CALIBRATION_DONE | SLEEP_OUT |
1026					  SLEEP_IN | PROGRAM_LOAD_DONE);
1027	if (error)
1028		return error;
1029
1030	error = i2c_smbus_write_byte_data(client, ERR_MASK,
1031					  PROGRAM_LOAD_ERR | CPU_TIMEOUT |
1032					  ADC_TIMEOUT);
1033	if (error)
1034		return error;
1035
1036	/* controller CPU power on */
1037	error = i2c_smbus_write_byte_data(client, SYSTEM,
1038					  ANALOG_POWER_ON | CPU_POWER_ON);
1039
1040	enable_irq(client->irq);
1041
1042	return error;
1043}
1044
1045static int rohm_ts_power_off(struct i2c_client *client)
1046{
1047	int error;
1048
1049	error = i2c_smbus_write_byte_data(client, SYSTEM,
1050					  ANALOG_POWER_ON | CPU_POWER_OFF);
1051	if (error) {
1052		dev_err(&client->dev,
1053			"failed to power off device CPU: %d\n", error);
1054		return error;
1055	}
1056
1057	error = i2c_smbus_write_byte_data(client, SYSTEM,
1058					  ANALOG_POWER_OFF | CPU_POWER_OFF);
1059	if (error)
1060		dev_err(&client->dev,
1061			"failed to power off the device: %d\n", error);
1062
1063	return error;
1064}
1065
1066static int rohm_ts_open(struct input_dev *input_dev)
1067{
1068	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1069	struct i2c_client *client = ts->client;
1070	int error;
1071
1072	if (!ts->initialized) {
1073		error = rohm_ts_device_init(client, ts->setup2);
1074		if (error) {
1075			dev_err(&client->dev,
1076				"device initialization failed: %d\n", error);
1077			return error;
1078		}
1079
1080		ts->initialized = true;
1081	}
1082
1083	return 0;
1084}
1085
1086static void rohm_ts_close(struct input_dev *input_dev)
1087{
1088	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1089
1090	rohm_ts_power_off(ts->client);
1091
1092	ts->initialized = false;
1093}
1094
1095static int rohm_bu21023_i2c_probe(struct i2c_client *client)
 
1096{
1097	struct device *dev = &client->dev;
1098	struct rohm_ts_data *ts;
1099	struct input_dev *input;
1100	int error;
1101
1102	if (!client->irq) {
1103		dev_err(dev, "IRQ is not assigned\n");
1104		return -EINVAL;
1105	}
1106
1107	if (!client->adapter->algo->master_xfer) {
1108		dev_err(dev, "I2C level transfers not supported\n");
1109		return -EOPNOTSUPP;
1110	}
1111
1112	/* Turn off CPU just in case */
1113	error = rohm_ts_power_off(client);
1114	if (error)
1115		return error;
1116
1117	ts = devm_kzalloc(dev, sizeof(struct rohm_ts_data), GFP_KERNEL);
1118	if (!ts)
1119		return -ENOMEM;
1120
1121	ts->client = client;
1122	ts->setup2 = MAF_1SAMPLE;
1123	i2c_set_clientdata(client, ts);
1124
1125	input = devm_input_allocate_device(dev);
1126	if (!input)
1127		return -ENOMEM;
1128
1129	input->name = BU21023_NAME;
1130	input->id.bustype = BUS_I2C;
1131	input->open = rohm_ts_open;
1132	input->close = rohm_ts_close;
1133
1134	ts->input = input;
1135	input_set_drvdata(input, ts);
1136
1137	input_set_abs_params(input, ABS_MT_POSITION_X,
1138			     ROHM_TS_ABS_X_MIN, ROHM_TS_ABS_X_MAX, 0, 0);
1139	input_set_abs_params(input, ABS_MT_POSITION_Y,
1140			     ROHM_TS_ABS_Y_MIN, ROHM_TS_ABS_Y_MAX, 0, 0);
1141
1142	error = input_mt_init_slots(input, MAX_CONTACTS,
1143				    INPUT_MT_DIRECT | INPUT_MT_TRACK |
1144				    INPUT_MT_DROP_UNUSED);
1145	if (error) {
1146		dev_err(dev, "failed to multi touch slots initialization\n");
1147		return error;
1148	}
1149
1150	error = devm_request_threaded_irq(dev, client->irq,
1151					  NULL, rohm_ts_soft_irq,
1152					  IRQF_ONESHOT, client->name, ts);
1153	if (error) {
1154		dev_err(dev, "failed to request IRQ: %d\n", error);
1155		return error;
1156	}
1157
1158	error = input_register_device(input);
1159	if (error) {
1160		dev_err(dev, "failed to register input device: %d\n", error);
1161		return error;
1162	}
1163
 
 
 
 
 
 
1164	return error;
1165}
1166
1167static const struct i2c_device_id rohm_bu21023_i2c_id[] = {
1168	{ BU21023_NAME, 0 },
1169	{ /* sentinel */ }
1170};
1171MODULE_DEVICE_TABLE(i2c, rohm_bu21023_i2c_id);
1172
1173static struct i2c_driver rohm_bu21023_i2c_driver = {
1174	.driver = {
1175		.name = BU21023_NAME,
1176		.dev_groups = rohm_ts_groups,
1177	},
1178	.probe = rohm_bu21023_i2c_probe,
1179	.id_table = rohm_bu21023_i2c_id,
1180};
1181module_i2c_driver(rohm_bu21023_i2c_driver);
1182
1183MODULE_DESCRIPTION("ROHM BU21023/24 Touchscreen driver");
1184MODULE_LICENSE("GPL v2");
1185MODULE_AUTHOR("ROHM Co., Ltd.");
v4.17
 
   1/*
   2 * ROHM BU21023/24 Dual touch support resistive touch screen driver
   3 * Copyright (C) 2012 ROHM CO.,LTD.
   4 *
   5 * This software is licensed under the terms of the GNU General Public
   6 * License version 2, as published by the Free Software Foundation, and
   7 * may be copied, distributed, and modified under those terms.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 */
  14#include <linux/delay.h>
  15#include <linux/firmware.h>
  16#include <linux/i2c.h>
  17#include <linux/input.h>
  18#include <linux/input/mt.h>
  19#include <linux/interrupt.h>
  20#include <linux/module.h>
  21#include <linux/slab.h>
  22
  23#define BU21023_NAME			"bu21023_ts"
  24#define BU21023_FIRMWARE_NAME		"bu21023.bin"
  25
  26#define MAX_CONTACTS			2
  27
  28#define AXIS_ADJUST			4
  29#define AXIS_OFFSET			8
  30
  31#define FIRMWARE_BLOCK_SIZE		32U
  32#define FIRMWARE_RETRY_MAX		4
  33
  34#define SAMPLING_DELAY			12	/* msec */
  35
  36#define CALIBRATION_RETRY_MAX		6
  37
  38#define ROHM_TS_ABS_X_MIN		40
  39#define ROHM_TS_ABS_X_MAX		990
  40#define ROHM_TS_ABS_Y_MIN		160
  41#define ROHM_TS_ABS_Y_MAX		920
  42#define ROHM_TS_DISPLACEMENT_MAX	0	/* zero for infinite */
  43
  44/*
  45 * BU21023GUL/BU21023MUV/BU21024FV-M registers map
  46 */
  47#define VADOUT_YP_H		0x00
  48#define VADOUT_YP_L		0x01
  49#define VADOUT_XP_H		0x02
  50#define VADOUT_XP_L		0x03
  51#define VADOUT_YN_H		0x04
  52#define VADOUT_YN_L		0x05
  53#define VADOUT_XN_H		0x06
  54#define VADOUT_XN_L		0x07
  55
  56#define PRM1_X_H		0x08
  57#define PRM1_X_L		0x09
  58#define PRM1_Y_H		0x0a
  59#define PRM1_Y_L		0x0b
  60#define PRM2_X_H		0x0c
  61#define PRM2_X_L		0x0d
  62#define PRM2_Y_H		0x0e
  63#define PRM2_Y_L		0x0f
  64
  65#define MLT_PRM_MONI_X		0x10
  66#define MLT_PRM_MONI_Y		0x11
  67
  68#define DEBUG_MONI_1		0x12
  69#define DEBUG_MONI_2		0x13
  70
  71#define VADOUT_ZX_H		0x14
  72#define VADOUT_ZX_L		0x15
  73#define VADOUT_ZY_H		0x16
  74#define VADOUT_ZY_L		0x17
  75
  76#define Z_PARAM_H		0x18
  77#define Z_PARAM_L		0x19
  78
  79/*
  80 * Value for VADOUT_*_L
  81 */
  82#define VADOUT_L_MASK		0x01
  83
  84/*
  85 * Value for PRM*_*_L
  86 */
  87#define PRM_L_MASK		0x01
  88
  89#define POS_X1_H		0x20
  90#define POS_X1_L		0x21
  91#define POS_Y1_H		0x22
  92#define POS_Y1_L		0x23
  93#define POS_X2_H		0x24
  94#define POS_X2_L		0x25
  95#define POS_Y2_H		0x26
  96#define POS_Y2_L		0x27
  97
  98/*
  99 * Value for POS_*_L
 100 */
 101#define POS_L_MASK		0x01
 102
 103#define TOUCH			0x28
 104#define TOUCH_DETECT		0x01
 105
 106#define TOUCH_GESTURE		0x29
 107#define SINGLE_TOUCH		0x01
 108#define DUAL_TOUCH		0x03
 109#define TOUCH_MASK		0x03
 110#define CALIBRATION_REQUEST	0x04
 111#define CALIBRATION_STATUS	0x08
 112#define CALIBRATION_MASK	0x0c
 113#define GESTURE_SPREAD		0x10
 114#define GESTURE_PINCH		0x20
 115#define GESTURE_ROTATE_R	0x40
 116#define GESTURE_ROTATE_L	0x80
 117
 118#define INT_STATUS		0x2a
 119#define INT_MASK		0x3d
 120#define INT_CLEAR		0x3e
 121
 122/*
 123 * Values for INT_*
 124 */
 125#define COORD_UPDATE		0x01
 126#define CALIBRATION_DONE	0x02
 127#define SLEEP_IN		0x04
 128#define SLEEP_OUT		0x08
 129#define PROGRAM_LOAD_DONE	0x10
 130#define ERROR			0x80
 131#define INT_ALL			0x9f
 132
 133#define ERR_STATUS		0x2b
 134#define ERR_MASK		0x3f
 135
 136/*
 137 * Values for ERR_*
 138 */
 139#define ADC_TIMEOUT		0x01
 140#define CPU_TIMEOUT		0x02
 141#define CALIBRATION_ERR		0x04
 142#define PROGRAM_LOAD_ERR	0x10
 143
 144#define COMMON_SETUP1			0x30
 145#define PROGRAM_LOAD_HOST		0x02
 146#define PROGRAM_LOAD_EEPROM		0x03
 147#define CENSOR_4PORT			0x04
 148#define CENSOR_8PORT			0x00	/* Not supported by BU21023 */
 149#define CALIBRATION_TYPE_DEFAULT	0x08
 150#define CALIBRATION_TYPE_SPECIAL	0x00
 151#define INT_ACTIVE_HIGH			0x10
 152#define INT_ACTIVE_LOW			0x00
 153#define AUTO_CALIBRATION		0x40
 154#define MANUAL_CALIBRATION		0x00
 155#define COMMON_SETUP1_DEFAULT		0x4e
 156
 157#define COMMON_SETUP2		0x31
 158#define MAF_NONE		0x00
 159#define MAF_1SAMPLE		0x01
 160#define MAF_3SAMPLES		0x02
 161#define MAF_5SAMPLES		0x03
 162#define INV_Y			0x04
 163#define INV_X			0x08
 164#define SWAP_XY			0x10
 165
 166#define COMMON_SETUP3		0x32
 167#define EN_SLEEP		0x01
 168#define EN_MULTI		0x02
 169#define EN_GESTURE		0x04
 170#define EN_INTVL		0x08
 171#define SEL_STEP		0x10
 172#define SEL_MULTI		0x20
 173#define SEL_TBL_DEFAULT		0x40
 174
 175#define INTERVAL_TIME		0x33
 176#define INTERVAL_TIME_DEFAULT	0x10
 177
 178#define STEP_X			0x34
 179#define STEP_X_DEFAULT		0x41
 180
 181#define STEP_Y			0x35
 182#define STEP_Y_DEFAULT		0x8d
 183
 184#define OFFSET_X		0x38
 185#define OFFSET_X_DEFAULT	0x0c
 186
 187#define OFFSET_Y		0x39
 188#define OFFSET_Y_DEFAULT	0x0c
 189
 190#define THRESHOLD_TOUCH		0x3a
 191#define THRESHOLD_TOUCH_DEFAULT	0xa0
 192
 193#define THRESHOLD_GESTURE		0x3b
 194#define THRESHOLD_GESTURE_DEFAULT	0x17
 195
 196#define SYSTEM			0x40
 197#define ANALOG_POWER_ON		0x01
 198#define ANALOG_POWER_OFF	0x00
 199#define CPU_POWER_ON		0x02
 200#define CPU_POWER_OFF		0x00
 201
 202#define FORCE_CALIBRATION	0x42
 203#define FORCE_CALIBRATION_ON	0x01
 204#define FORCE_CALIBRATION_OFF	0x00
 205
 206#define CPU_FREQ		0x50	/* 10 / (reg + 1) MHz */
 207#define CPU_FREQ_10MHZ		0x00
 208#define CPU_FREQ_5MHZ		0x01
 209#define CPU_FREQ_1MHZ		0x09
 210
 211#define EEPROM_ADDR		0x51
 212
 213#define CALIBRATION_ADJUST		0x52
 214#define CALIBRATION_ADJUST_DEFAULT	0x00
 215
 216#define THRESHOLD_SLEEP_IN	0x53
 217
 218#define EVR_XY			0x56
 219#define EVR_XY_DEFAULT		0x10
 220
 221#define PRM_SWOFF_TIME		0x57
 222#define PRM_SWOFF_TIME_DEFAULT	0x04
 223
 224#define PROGRAM_VERSION		0x5f
 225
 226#define ADC_CTRL		0x60
 227#define ADC_DIV_MASK		0x1f	/* The minimum value is 4 */
 228#define ADC_DIV_DEFAULT		0x08
 229
 230#define ADC_WAIT		0x61
 231#define ADC_WAIT_DEFAULT	0x0a
 232
 233#define SWCONT			0x62
 234#define SWCONT_DEFAULT		0x0f
 235
 236#define EVR_X			0x63
 237#define EVR_X_DEFAULT		0x86
 238
 239#define EVR_Y			0x64
 240#define EVR_Y_DEFAULT		0x64
 241
 242#define TEST1			0x65
 243#define DUALTOUCH_STABILIZE_ON	0x01
 244#define DUALTOUCH_STABILIZE_OFF	0x00
 245#define DUALTOUCH_REG_ON	0x20
 246#define DUALTOUCH_REG_OFF	0x00
 247
 248#define CALIBRATION_REG1		0x68
 249#define CALIBRATION_REG1_DEFAULT	0xd9
 250
 251#define CALIBRATION_REG2		0x69
 252#define CALIBRATION_REG2_DEFAULT	0x36
 253
 254#define CALIBRATION_REG3		0x6a
 255#define CALIBRATION_REG3_DEFAULT	0x32
 256
 257#define EX_ADDR_H		0x70
 258#define EX_ADDR_L		0x71
 259#define EX_WDAT			0x72
 260#define EX_RDAT			0x73
 261#define EX_CHK_SUM1		0x74
 262#define EX_CHK_SUM2		0x75
 263#define EX_CHK_SUM3		0x76
 264
 265struct rohm_ts_data {
 266	struct i2c_client *client;
 267	struct input_dev *input;
 268
 269	bool initialized;
 270
 271	unsigned int contact_count[MAX_CONTACTS + 1];
 272	int finger_count;
 273
 274	u8 setup2;
 275};
 276
 277/*
 278 * rohm_i2c_burst_read - execute combined I2C message for ROHM BU21023/24
 279 * @client: Handle to ROHM BU21023/24
 280 * @start: Where to start read address from ROHM BU21023/24
 281 * @buf: Where to store read data from ROHM BU21023/24
 282 * @len: How many bytes to read
 283 *
 284 * Returns negative errno, else zero on success.
 285 *
 286 * Note
 287 * In BU21023/24 burst read, stop condition is needed after "address write".
 288 * Therefore, transmission is performed in 2 steps.
 289 */
 290static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
 291			       size_t len)
 292{
 293	struct i2c_adapter *adap = client->adapter;
 294	struct i2c_msg msg[2];
 295	int i, ret = 0;
 296
 297	msg[0].addr = client->addr;
 298	msg[0].flags = 0;
 299	msg[0].len = 1;
 300	msg[0].buf = &start;
 301
 302	msg[1].addr = client->addr;
 303	msg[1].flags = I2C_M_RD;
 304	msg[1].len = len;
 305	msg[1].buf = buf;
 306
 307	i2c_lock_adapter(adap);
 308
 309	for (i = 0; i < 2; i++) {
 310		if (__i2c_transfer(adap, &msg[i], 1) < 0) {
 311			ret = -EIO;
 312			break;
 313		}
 314	}
 315
 316	i2c_unlock_adapter(adap);
 317
 318	return ret;
 319}
 320
 321static int rohm_ts_manual_calibration(struct rohm_ts_data *ts)
 322{
 323	struct i2c_client *client = ts->client;
 324	struct device *dev = &client->dev;
 325	u8 buf[33];	/* for PRM1_X_H(0x08)-TOUCH(0x28) */
 326
 327	int retry;
 328	bool success = false;
 329	bool first_time = true;
 330	bool calibration_done;
 331
 332	u8 reg1, reg2, reg3;
 333	s32 reg1_orig, reg2_orig, reg3_orig;
 334	s32 val;
 335
 336	int calib_x = 0, calib_y = 0;
 337	int reg_x, reg_y;
 338	int err_x, err_y;
 339
 340	int error, error2;
 341	int i;
 342
 343	reg1_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG1);
 344	if (reg1_orig < 0)
 345		return reg1_orig;
 346
 347	reg2_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG2);
 348	if (reg2_orig < 0)
 349		return reg2_orig;
 350
 351	reg3_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG3);
 352	if (reg3_orig < 0)
 353		return reg3_orig;
 354
 355	error = i2c_smbus_write_byte_data(client, INT_MASK,
 356					  COORD_UPDATE | SLEEP_IN | SLEEP_OUT |
 357					  PROGRAM_LOAD_DONE);
 358	if (error)
 359		goto out;
 360
 361	error = i2c_smbus_write_byte_data(client, TEST1,
 362					  DUALTOUCH_STABILIZE_ON);
 363	if (error)
 364		goto out;
 365
 366	for (retry = 0; retry < CALIBRATION_RETRY_MAX; retry++) {
 367		/* wait 2 sampling for update */
 368		mdelay(2 * SAMPLING_DELAY);
 369
 370#define READ_CALIB_BUF(reg)	buf[((reg) - PRM1_X_H)]
 371
 372		error = rohm_i2c_burst_read(client, PRM1_X_H, buf, sizeof(buf));
 373		if (error)
 374			goto out;
 375
 376		if (READ_CALIB_BUF(TOUCH) & TOUCH_DETECT)
 377			continue;
 378
 379		if (first_time) {
 380			/* generate calibration parameter */
 381			calib_x = ((int)READ_CALIB_BUF(PRM1_X_H) << 2 |
 382				READ_CALIB_BUF(PRM1_X_L)) - AXIS_OFFSET;
 383			calib_y = ((int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
 384				READ_CALIB_BUF(PRM1_Y_L)) - AXIS_OFFSET;
 385
 386			error = i2c_smbus_write_byte_data(client, TEST1,
 387				DUALTOUCH_STABILIZE_ON | DUALTOUCH_REG_ON);
 388			if (error)
 389				goto out;
 390
 391			first_time = false;
 392		} else {
 393			/* generate adjustment parameter */
 394			err_x = (int)READ_CALIB_BUF(PRM1_X_H) << 2 |
 395				READ_CALIB_BUF(PRM1_X_L);
 396			err_y = (int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
 397				READ_CALIB_BUF(PRM1_Y_L);
 398
 399			/* X axis ajust */
 400			if (err_x <= 4)
 401				calib_x -= AXIS_ADJUST;
 402			else if (err_x >= 60)
 403				calib_x += AXIS_ADJUST;
 404
 405			/* Y axis ajust */
 406			if (err_y <= 4)
 407				calib_y -= AXIS_ADJUST;
 408			else if (err_y >= 60)
 409				calib_y += AXIS_ADJUST;
 410		}
 411
 412		/* generate calibration setting value */
 413		reg_x = calib_x + ((calib_x & 0x200) << 1);
 414		reg_y = calib_y + ((calib_y & 0x200) << 1);
 415
 416		/* convert for register format */
 417		reg1 = reg_x >> 3;
 418		reg2 = (reg_y & 0x7) << 4 | (reg_x & 0x7);
 419		reg3 = reg_y >> 3;
 420
 421		error = i2c_smbus_write_byte_data(client,
 422						  CALIBRATION_REG1, reg1);
 423		if (error)
 424			goto out;
 425
 426		error = i2c_smbus_write_byte_data(client,
 427						  CALIBRATION_REG2, reg2);
 428		if (error)
 429			goto out;
 430
 431		error = i2c_smbus_write_byte_data(client,
 432						  CALIBRATION_REG3, reg3);
 433		if (error)
 434			goto out;
 435
 436		/*
 437		 * force calibration sequcence
 438		 */
 439		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
 440						  FORCE_CALIBRATION_OFF);
 441		if (error)
 442			goto out;
 443
 444		error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
 445						  FORCE_CALIBRATION_ON);
 446		if (error)
 447			goto out;
 448
 449		/* clear all interrupts */
 450		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 451		if (error)
 452			goto out;
 453
 454		/*
 455		 * Wait for the status change of calibration, max 10 sampling
 456		 */
 457		calibration_done = false;
 458
 459		for (i = 0; i < 10; i++) {
 460			mdelay(SAMPLING_DELAY);
 461
 462			val = i2c_smbus_read_byte_data(client, TOUCH_GESTURE);
 463			if (!(val & CALIBRATION_MASK)) {
 464				calibration_done = true;
 465				break;
 466			} else if (val < 0) {
 467				error = val;
 468				goto out;
 469			}
 470		}
 471
 472		if (calibration_done) {
 473			val = i2c_smbus_read_byte_data(client, INT_STATUS);
 474			if (val == CALIBRATION_DONE) {
 475				success = true;
 476				break;
 477			} else if (val < 0) {
 478				error = val;
 479				goto out;
 480			}
 481		} else {
 482			dev_warn(dev, "calibration timeout\n");
 483		}
 484	}
 485
 486	if (!success) {
 487		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
 488						  reg1_orig);
 489		if (error)
 490			goto out;
 491
 492		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
 493						  reg2_orig);
 494		if (error)
 495			goto out;
 496
 497		error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
 498						  reg3_orig);
 499		if (error)
 500			goto out;
 501
 502		/* calibration data enable */
 503		error = i2c_smbus_write_byte_data(client, TEST1,
 504						  DUALTOUCH_STABILIZE_ON |
 505						  DUALTOUCH_REG_ON);
 506		if (error)
 507			goto out;
 508
 509		/* wait 10 sampling */
 510		mdelay(10 * SAMPLING_DELAY);
 511
 512		error = -EBUSY;
 513	}
 514
 515out:
 516	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 517	if (!error2)
 518		/* Clear all interrupts */
 519		error2 = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 520
 521	return error ? error : error2;
 522}
 523
 524static const unsigned int untouch_threshold[3] = { 0, 1, 5 };
 525static const unsigned int single_touch_threshold[3] = { 0, 0, 4 };
 526static const unsigned int dual_touch_threshold[3] = { 10, 8, 0 };
 527
 528static irqreturn_t rohm_ts_soft_irq(int irq, void *dev_id)
 529{
 530	struct rohm_ts_data *ts = dev_id;
 531	struct i2c_client *client = ts->client;
 532	struct input_dev *input_dev = ts->input;
 533	struct device *dev = &client->dev;
 534
 535	u8 buf[10];	/* for POS_X1_H(0x20)-TOUCH_GESTURE(0x29) */
 536
 537	struct input_mt_pos pos[MAX_CONTACTS];
 538	int slots[MAX_CONTACTS];
 539	u8 touch_flags;
 540	unsigned int threshold;
 541	int finger_count = -1;
 542	int prev_finger_count = ts->finger_count;
 543	int count;
 544	int error;
 545	int i;
 546
 547	error = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 548	if (error)
 549		return IRQ_HANDLED;
 550
 551	/* Clear all interrupts */
 552	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 553	if (error)
 554		return IRQ_HANDLED;
 555
 556#define READ_POS_BUF(reg)	buf[((reg) - POS_X1_H)]
 557
 558	error = rohm_i2c_burst_read(client, POS_X1_H, buf, sizeof(buf));
 559	if (error)
 560		return IRQ_HANDLED;
 561
 562	touch_flags = READ_POS_BUF(TOUCH_GESTURE) & TOUCH_MASK;
 563	if (touch_flags) {
 564		/* generate coordinates */
 565		pos[0].x = ((s16)READ_POS_BUF(POS_X1_H) << 2) |
 566			   READ_POS_BUF(POS_X1_L);
 567		pos[0].y = ((s16)READ_POS_BUF(POS_Y1_H) << 2) |
 568			   READ_POS_BUF(POS_Y1_L);
 569		pos[1].x = ((s16)READ_POS_BUF(POS_X2_H) << 2) |
 570			   READ_POS_BUF(POS_X2_L);
 571		pos[1].y = ((s16)READ_POS_BUF(POS_Y2_H) << 2) |
 572			   READ_POS_BUF(POS_Y2_L);
 573	}
 574
 575	switch (touch_flags) {
 576	case 0:
 577		threshold = untouch_threshold[prev_finger_count];
 578		if (++ts->contact_count[0] >= threshold)
 579			finger_count = 0;
 580		break;
 581
 582	case SINGLE_TOUCH:
 583		threshold = single_touch_threshold[prev_finger_count];
 584		if (++ts->contact_count[1] >= threshold)
 585			finger_count = 1;
 586
 587		if (finger_count == 1) {
 588			if (pos[1].x != 0 && pos[1].y != 0) {
 589				pos[0].x = pos[1].x;
 590				pos[0].y = pos[1].y;
 591				pos[1].x = 0;
 592				pos[1].y = 0;
 593			}
 594		}
 595		break;
 596
 597	case DUAL_TOUCH:
 598		threshold = dual_touch_threshold[prev_finger_count];
 599		if (++ts->contact_count[2] >= threshold)
 600			finger_count = 2;
 601		break;
 602
 603	default:
 604		dev_dbg(dev,
 605			"Three or more touches are not supported\n");
 606		return IRQ_HANDLED;
 607	}
 608
 609	if (finger_count >= 0) {
 610		if (prev_finger_count != finger_count) {
 611			count = ts->contact_count[finger_count];
 612			memset(ts->contact_count, 0, sizeof(ts->contact_count));
 613			ts->contact_count[finger_count] = count;
 614		}
 615
 616		input_mt_assign_slots(input_dev, slots, pos,
 617				      finger_count, ROHM_TS_DISPLACEMENT_MAX);
 618
 619		for (i = 0; i < finger_count; i++) {
 620			input_mt_slot(input_dev, slots[i]);
 621			input_mt_report_slot_state(input_dev,
 622						   MT_TOOL_FINGER, true);
 623			input_report_abs(input_dev,
 624					 ABS_MT_POSITION_X, pos[i].x);
 625			input_report_abs(input_dev,
 626					 ABS_MT_POSITION_Y, pos[i].y);
 627		}
 628
 629		input_mt_sync_frame(input_dev);
 630		input_mt_report_pointer_emulation(input_dev, true);
 631		input_sync(input_dev);
 632
 633		ts->finger_count = finger_count;
 634	}
 635
 636	if (READ_POS_BUF(TOUCH_GESTURE) & CALIBRATION_REQUEST) {
 637		error = rohm_ts_manual_calibration(ts);
 638		if (error)
 639			dev_warn(dev, "manual calibration failed: %d\n",
 640				 error);
 641	}
 642
 643	i2c_smbus_write_byte_data(client, INT_MASK,
 644				  CALIBRATION_DONE | SLEEP_OUT | SLEEP_IN |
 645				  PROGRAM_LOAD_DONE);
 646
 647	return IRQ_HANDLED;
 648}
 649
 650static int rohm_ts_load_firmware(struct i2c_client *client,
 651				 const char *firmware_name)
 652{
 653	struct device *dev = &client->dev;
 654	const struct firmware *fw;
 655	s32 status;
 656	unsigned int offset, len, xfer_len;
 657	unsigned int retry = 0;
 658	int error, error2;
 659
 660	error = request_firmware(&fw, firmware_name, dev);
 661	if (error) {
 662		dev_err(dev, "unable to retrieve firmware %s: %d\n",
 663			firmware_name, error);
 664		return error;
 665	}
 666
 667	error = i2c_smbus_write_byte_data(client, INT_MASK,
 668					  COORD_UPDATE | CALIBRATION_DONE |
 669					  SLEEP_IN | SLEEP_OUT);
 670	if (error)
 671		goto out;
 672
 673	do {
 674		if (retry) {
 675			dev_warn(dev, "retrying firmware load\n");
 676
 677			/* settings for retry */
 678			error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
 679			if (error)
 680				goto out;
 681		}
 682
 683		error = i2c_smbus_write_byte_data(client, EX_ADDR_H, 0);
 684		if (error)
 685			goto out;
 686
 687		error = i2c_smbus_write_byte_data(client, EX_ADDR_L, 0);
 688		if (error)
 689			goto out;
 690
 691		error = i2c_smbus_write_byte_data(client, COMMON_SETUP1,
 692						  COMMON_SETUP1_DEFAULT);
 693		if (error)
 694			goto out;
 695
 696		/* firmware load to the device */
 697		offset = 0;
 698		len = fw->size;
 699
 700		while (len) {
 701			xfer_len = min(FIRMWARE_BLOCK_SIZE, len);
 702
 703			error = i2c_smbus_write_i2c_block_data(client, EX_WDAT,
 704						xfer_len, &fw->data[offset]);
 705			if (error)
 706				goto out;
 707
 708			len -= xfer_len;
 709			offset += xfer_len;
 710		}
 711
 712		/* check firmware load result */
 713		status = i2c_smbus_read_byte_data(client, INT_STATUS);
 714		if (status < 0) {
 715			error = status;
 716			goto out;
 717		}
 718
 719		/* clear all interrupts */
 720		error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 721		if (error)
 722			goto out;
 723
 724		if (status == PROGRAM_LOAD_DONE)
 725			break;
 726
 727		error = -EIO;
 728	} while (++retry <= FIRMWARE_RETRY_MAX);
 729
 730out:
 731	error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
 732
 733	release_firmware(fw);
 734
 735	return error ? error : error2;
 736}
 737
 738static ssize_t swap_xy_show(struct device *dev, struct device_attribute *attr,
 739			    char *buf)
 740{
 741	struct i2c_client *client = to_i2c_client(dev);
 742	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 743
 744	return sprintf(buf, "%d\n", !!(ts->setup2 & SWAP_XY));
 745}
 746
 747static ssize_t swap_xy_store(struct device *dev, struct device_attribute *attr,
 748			     const char *buf, size_t count)
 749{
 750	struct i2c_client *client = to_i2c_client(dev);
 751	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 752	unsigned int val;
 753	int error;
 754
 755	error = kstrtouint(buf, 0, &val);
 756	if (error)
 757		return error;
 758
 759	error = mutex_lock_interruptible(&ts->input->mutex);
 760	if (error)
 761		return error;
 762
 763	if (val)
 764		ts->setup2 |= SWAP_XY;
 765	else
 766		ts->setup2 &= ~SWAP_XY;
 767
 768	if (ts->initialized)
 769		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
 770						  ts->setup2);
 771
 772	mutex_unlock(&ts->input->mutex);
 773
 774	return error ? error : count;
 775}
 776
 777static ssize_t inv_x_show(struct device *dev, struct device_attribute *attr,
 778			  char *buf)
 779{
 780	struct i2c_client *client = to_i2c_client(dev);
 781	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 782
 783	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_X));
 784}
 785
 786static ssize_t inv_x_store(struct device *dev, struct device_attribute *attr,
 787			   const char *buf, size_t count)
 788{
 789	struct i2c_client *client = to_i2c_client(dev);
 790	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 791	unsigned int val;
 792	int error;
 793
 794	error = kstrtouint(buf, 0, &val);
 795	if (error)
 796		return error;
 797
 798	error = mutex_lock_interruptible(&ts->input->mutex);
 799	if (error)
 800		return error;
 801
 802	if (val)
 803		ts->setup2 |= INV_X;
 804	else
 805		ts->setup2 &= ~INV_X;
 806
 807	if (ts->initialized)
 808		error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
 809						  ts->setup2);
 810
 811	mutex_unlock(&ts->input->mutex);
 812
 813	return error ? error : count;
 814}
 815
 816static ssize_t inv_y_show(struct device *dev, struct device_attribute *attr,
 817			  char *buf)
 818{
 819	struct i2c_client *client = to_i2c_client(dev);
 820	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 821
 822	return sprintf(buf, "%d\n", !!(ts->setup2 & INV_Y));
 823}
 824
 825static ssize_t inv_y_store(struct device *dev, struct device_attribute *attr,
 826			   const char *buf, size_t count)
 827{
 828	struct i2c_client *client = to_i2c_client(dev);
 829	struct rohm_ts_data *ts = i2c_get_clientdata(client);
 830	unsigned int val;
 831	int error;
 832
 833	error = kstrtouint(buf, 0, &val);
 834	if (error)
 835		return error;
 836
 837	error = mutex_lock_interruptible(&ts->input->mutex);
 838	if (error)
 839		return error;
 840
 841	if (val)
 842		ts->setup2 |= INV_Y;
 843	else
 844		ts->setup2 &= ~INV_Y;
 845
 846	if (ts->initialized)
 847		error = i2c_smbus_write_byte_data(client, COMMON_SETUP2,
 848						  ts->setup2);
 849
 850	mutex_unlock(&ts->input->mutex);
 851
 852	return error ? error : count;
 853}
 854
 855static DEVICE_ATTR_RW(swap_xy);
 856static DEVICE_ATTR_RW(inv_x);
 857static DEVICE_ATTR_RW(inv_y);
 858
 859static struct attribute *rohm_ts_attrs[] = {
 860	&dev_attr_swap_xy.attr,
 861	&dev_attr_inv_x.attr,
 862	&dev_attr_inv_y.attr,
 863	NULL,
 864};
 865
 866static const struct attribute_group rohm_ts_attr_group = {
 867	.attrs = rohm_ts_attrs,
 868};
 869
 870static int rohm_ts_device_init(struct i2c_client *client, u8 setup2)
 871{
 872	struct device *dev = &client->dev;
 873	int error;
 874
 875	disable_irq(client->irq);
 876
 877	/*
 878	 * Wait 200usec for reset
 879	 */
 880	udelay(200);
 881
 882	/* Release analog reset */
 883	error = i2c_smbus_write_byte_data(client, SYSTEM,
 884					  ANALOG_POWER_ON | CPU_POWER_OFF);
 885	if (error)
 886		return error;
 887
 888	/* Waiting for the analog warm-up, max. 200usec */
 889	udelay(200);
 890
 891	/* clear all interrupts */
 892	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
 893	if (error)
 894		return error;
 895
 896	error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
 897	if (error)
 898		return error;
 899
 900	error = i2c_smbus_write_byte_data(client, COMMON_SETUP1, 0);
 901	if (error)
 902		return error;
 903
 904	error = i2c_smbus_write_byte_data(client, COMMON_SETUP2, setup2);
 905	if (error)
 906		return error;
 907
 908	error = i2c_smbus_write_byte_data(client, COMMON_SETUP3,
 909					  SEL_TBL_DEFAULT | EN_MULTI);
 910	if (error)
 911		return error;
 912
 913	error = i2c_smbus_write_byte_data(client, THRESHOLD_GESTURE,
 914					  THRESHOLD_GESTURE_DEFAULT);
 915	if (error)
 916		return error;
 917
 918	error = i2c_smbus_write_byte_data(client, INTERVAL_TIME,
 919					  INTERVAL_TIME_DEFAULT);
 920	if (error)
 921		return error;
 922
 923	error = i2c_smbus_write_byte_data(client, CPU_FREQ, CPU_FREQ_10MHZ);
 924	if (error)
 925		return error;
 926
 927	error = i2c_smbus_write_byte_data(client, PRM_SWOFF_TIME,
 928					  PRM_SWOFF_TIME_DEFAULT);
 929	if (error)
 930		return error;
 931
 932	error = i2c_smbus_write_byte_data(client, ADC_CTRL, ADC_DIV_DEFAULT);
 933	if (error)
 934		return error;
 935
 936	error = i2c_smbus_write_byte_data(client, ADC_WAIT, ADC_WAIT_DEFAULT);
 937	if (error)
 938		return error;
 939
 940	/*
 941	 * Panel setup, these values change with the panel.
 942	 */
 943	error = i2c_smbus_write_byte_data(client, STEP_X, STEP_X_DEFAULT);
 944	if (error)
 945		return error;
 946
 947	error = i2c_smbus_write_byte_data(client, STEP_Y, STEP_Y_DEFAULT);
 948	if (error)
 949		return error;
 950
 951	error = i2c_smbus_write_byte_data(client, OFFSET_X, OFFSET_X_DEFAULT);
 952	if (error)
 953		return error;
 954
 955	error = i2c_smbus_write_byte_data(client, OFFSET_Y, OFFSET_Y_DEFAULT);
 956	if (error)
 957		return error;
 958
 959	error = i2c_smbus_write_byte_data(client, THRESHOLD_TOUCH,
 960					  THRESHOLD_TOUCH_DEFAULT);
 961	if (error)
 962		return error;
 963
 964	error = i2c_smbus_write_byte_data(client, EVR_XY, EVR_XY_DEFAULT);
 965	if (error)
 966		return error;
 967
 968	error = i2c_smbus_write_byte_data(client, EVR_X, EVR_X_DEFAULT);
 969	if (error)
 970		return error;
 971
 972	error = i2c_smbus_write_byte_data(client, EVR_Y, EVR_Y_DEFAULT);
 973	if (error)
 974		return error;
 975
 976	/* Fixed value settings */
 977	error = i2c_smbus_write_byte_data(client, CALIBRATION_ADJUST,
 978					  CALIBRATION_ADJUST_DEFAULT);
 979	if (error)
 980		return error;
 981
 982	error = i2c_smbus_write_byte_data(client, SWCONT, SWCONT_DEFAULT);
 983	if (error)
 984		return error;
 985
 986	error = i2c_smbus_write_byte_data(client, TEST1,
 987					  DUALTOUCH_STABILIZE_ON |
 988					  DUALTOUCH_REG_ON);
 989	if (error)
 990		return error;
 991
 992	error = rohm_ts_load_firmware(client, BU21023_FIRMWARE_NAME);
 993	if (error) {
 994		dev_err(dev, "failed to load firmware: %d\n", error);
 995		return error;
 996	}
 997
 998	/*
 999	 * Manual calibration results are not changed in same environment.
1000	 * If the force calibration is performed,
1001	 * the controller will not require calibration request interrupt
1002	 * when the typical values are set to the calibration registers.
1003	 */
1004	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
1005					  CALIBRATION_REG1_DEFAULT);
1006	if (error)
1007		return error;
1008
1009	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
1010					  CALIBRATION_REG2_DEFAULT);
1011	if (error)
1012		return error;
1013
1014	error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
1015					  CALIBRATION_REG3_DEFAULT);
1016	if (error)
1017		return error;
1018
1019	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1020					  FORCE_CALIBRATION_OFF);
1021	if (error)
1022		return error;
1023
1024	error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1025					  FORCE_CALIBRATION_ON);
1026	if (error)
1027		return error;
1028
1029	/* Clear all interrupts */
1030	error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
1031	if (error)
1032		return error;
1033
1034	/* Enable coordinates update interrupt */
1035	error = i2c_smbus_write_byte_data(client, INT_MASK,
1036					  CALIBRATION_DONE | SLEEP_OUT |
1037					  SLEEP_IN | PROGRAM_LOAD_DONE);
1038	if (error)
1039		return error;
1040
1041	error = i2c_smbus_write_byte_data(client, ERR_MASK,
1042					  PROGRAM_LOAD_ERR | CPU_TIMEOUT |
1043					  ADC_TIMEOUT);
1044	if (error)
1045		return error;
1046
1047	/* controller CPU power on */
1048	error = i2c_smbus_write_byte_data(client, SYSTEM,
1049					  ANALOG_POWER_ON | CPU_POWER_ON);
1050
1051	enable_irq(client->irq);
1052
1053	return error;
1054}
1055
1056static int rohm_ts_power_off(struct i2c_client *client)
1057{
1058	int error;
1059
1060	error = i2c_smbus_write_byte_data(client, SYSTEM,
1061					  ANALOG_POWER_ON | CPU_POWER_OFF);
1062	if (error) {
1063		dev_err(&client->dev,
1064			"failed to power off device CPU: %d\n", error);
1065		return error;
1066	}
1067
1068	error = i2c_smbus_write_byte_data(client, SYSTEM,
1069					  ANALOG_POWER_OFF | CPU_POWER_OFF);
1070	if (error)
1071		dev_err(&client->dev,
1072			"failed to power off the device: %d\n", error);
1073
1074	return error;
1075}
1076
1077static int rohm_ts_open(struct input_dev *input_dev)
1078{
1079	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1080	struct i2c_client *client = ts->client;
1081	int error;
1082
1083	if (!ts->initialized) {
1084		error = rohm_ts_device_init(client, ts->setup2);
1085		if (error) {
1086			dev_err(&client->dev,
1087				"device initialization failed: %d\n", error);
1088			return error;
1089		}
1090
1091		ts->initialized = true;
1092	}
1093
1094	return 0;
1095}
1096
1097static void rohm_ts_close(struct input_dev *input_dev)
1098{
1099	struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1100
1101	rohm_ts_power_off(ts->client);
1102
1103	ts->initialized = false;
1104}
1105
1106static int rohm_bu21023_i2c_probe(struct i2c_client *client,
1107				  const struct i2c_device_id *id)
1108{
1109	struct device *dev = &client->dev;
1110	struct rohm_ts_data *ts;
1111	struct input_dev *input;
1112	int error;
1113
1114	if (!client->irq) {
1115		dev_err(dev, "IRQ is not assigned\n");
1116		return -EINVAL;
1117	}
1118
1119	if (!client->adapter->algo->master_xfer) {
1120		dev_err(dev, "I2C level transfers not supported\n");
1121		return -EOPNOTSUPP;
1122	}
1123
1124	/* Turn off CPU just in case */
1125	error = rohm_ts_power_off(client);
1126	if (error)
1127		return error;
1128
1129	ts = devm_kzalloc(dev, sizeof(struct rohm_ts_data), GFP_KERNEL);
1130	if (!ts)
1131		return -ENOMEM;
1132
1133	ts->client = client;
1134	ts->setup2 = MAF_1SAMPLE;
1135	i2c_set_clientdata(client, ts);
1136
1137	input = devm_input_allocate_device(dev);
1138	if (!input)
1139		return -ENOMEM;
1140
1141	input->name = BU21023_NAME;
1142	input->id.bustype = BUS_I2C;
1143	input->open = rohm_ts_open;
1144	input->close = rohm_ts_close;
1145
1146	ts->input = input;
1147	input_set_drvdata(input, ts);
1148
1149	input_set_abs_params(input, ABS_MT_POSITION_X,
1150			     ROHM_TS_ABS_X_MIN, ROHM_TS_ABS_X_MAX, 0, 0);
1151	input_set_abs_params(input, ABS_MT_POSITION_Y,
1152			     ROHM_TS_ABS_Y_MIN, ROHM_TS_ABS_Y_MAX, 0, 0);
1153
1154	error = input_mt_init_slots(input, MAX_CONTACTS,
1155				    INPUT_MT_DIRECT | INPUT_MT_TRACK |
1156				    INPUT_MT_DROP_UNUSED);
1157	if (error) {
1158		dev_err(dev, "failed to multi touch slots initialization\n");
1159		return error;
1160	}
1161
1162	error = devm_request_threaded_irq(dev, client->irq,
1163					  NULL, rohm_ts_soft_irq,
1164					  IRQF_ONESHOT, client->name, ts);
1165	if (error) {
1166		dev_err(dev, "failed to request IRQ: %d\n", error);
1167		return error;
1168	}
1169
1170	error = input_register_device(input);
1171	if (error) {
1172		dev_err(dev, "failed to register input device: %d\n", error);
1173		return error;
1174	}
1175
1176	error = devm_device_add_group(dev, &rohm_ts_attr_group);
1177	if (error) {
1178		dev_err(dev, "failed to create sysfs group: %d\n", error);
1179		return error;
1180	}
1181
1182	return error;
1183}
1184
1185static const struct i2c_device_id rohm_bu21023_i2c_id[] = {
1186	{ BU21023_NAME, 0 },
1187	{ /* sentinel */ }
1188};
1189MODULE_DEVICE_TABLE(i2c, rohm_bu21023_i2c_id);
1190
1191static struct i2c_driver rohm_bu21023_i2c_driver = {
1192	.driver = {
1193		.name = BU21023_NAME,
 
1194	},
1195	.probe = rohm_bu21023_i2c_probe,
1196	.id_table = rohm_bu21023_i2c_id,
1197};
1198module_i2c_driver(rohm_bu21023_i2c_driver);
1199
1200MODULE_DESCRIPTION("ROHM BU21023/24 Touchscreen driver");
1201MODULE_LICENSE("GPL v2");
1202MODULE_AUTHOR("ROHM Co., Ltd.");