0001
0002
0003
0004
0005
0006 #include <linux/delay.h>
0007 #include <linux/firmware.h>
0008 #include <linux/i2c.h>
0009 #include <linux/input.h>
0010 #include <linux/input/mt.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014
0015 #define BU21023_NAME "bu21023_ts"
0016 #define BU21023_FIRMWARE_NAME "bu21023.bin"
0017
0018 #define MAX_CONTACTS 2
0019
0020 #define AXIS_ADJUST 4
0021 #define AXIS_OFFSET 8
0022
0023 #define FIRMWARE_BLOCK_SIZE 32U
0024 #define FIRMWARE_RETRY_MAX 4
0025
0026 #define SAMPLING_DELAY 12
0027
0028 #define CALIBRATION_RETRY_MAX 6
0029
0030 #define ROHM_TS_ABS_X_MIN 40
0031 #define ROHM_TS_ABS_X_MAX 990
0032 #define ROHM_TS_ABS_Y_MIN 160
0033 #define ROHM_TS_ABS_Y_MAX 920
0034 #define ROHM_TS_DISPLACEMENT_MAX 0
0035
0036
0037
0038
0039 #define VADOUT_YP_H 0x00
0040 #define VADOUT_YP_L 0x01
0041 #define VADOUT_XP_H 0x02
0042 #define VADOUT_XP_L 0x03
0043 #define VADOUT_YN_H 0x04
0044 #define VADOUT_YN_L 0x05
0045 #define VADOUT_XN_H 0x06
0046 #define VADOUT_XN_L 0x07
0047
0048 #define PRM1_X_H 0x08
0049 #define PRM1_X_L 0x09
0050 #define PRM1_Y_H 0x0a
0051 #define PRM1_Y_L 0x0b
0052 #define PRM2_X_H 0x0c
0053 #define PRM2_X_L 0x0d
0054 #define PRM2_Y_H 0x0e
0055 #define PRM2_Y_L 0x0f
0056
0057 #define MLT_PRM_MONI_X 0x10
0058 #define MLT_PRM_MONI_Y 0x11
0059
0060 #define DEBUG_MONI_1 0x12
0061 #define DEBUG_MONI_2 0x13
0062
0063 #define VADOUT_ZX_H 0x14
0064 #define VADOUT_ZX_L 0x15
0065 #define VADOUT_ZY_H 0x16
0066 #define VADOUT_ZY_L 0x17
0067
0068 #define Z_PARAM_H 0x18
0069 #define Z_PARAM_L 0x19
0070
0071
0072
0073
0074 #define VADOUT_L_MASK 0x01
0075
0076
0077
0078
0079 #define PRM_L_MASK 0x01
0080
0081 #define POS_X1_H 0x20
0082 #define POS_X1_L 0x21
0083 #define POS_Y1_H 0x22
0084 #define POS_Y1_L 0x23
0085 #define POS_X2_H 0x24
0086 #define POS_X2_L 0x25
0087 #define POS_Y2_H 0x26
0088 #define POS_Y2_L 0x27
0089
0090
0091
0092
0093 #define POS_L_MASK 0x01
0094
0095 #define TOUCH 0x28
0096 #define TOUCH_DETECT 0x01
0097
0098 #define TOUCH_GESTURE 0x29
0099 #define SINGLE_TOUCH 0x01
0100 #define DUAL_TOUCH 0x03
0101 #define TOUCH_MASK 0x03
0102 #define CALIBRATION_REQUEST 0x04
0103 #define CALIBRATION_STATUS 0x08
0104 #define CALIBRATION_MASK 0x0c
0105 #define GESTURE_SPREAD 0x10
0106 #define GESTURE_PINCH 0x20
0107 #define GESTURE_ROTATE_R 0x40
0108 #define GESTURE_ROTATE_L 0x80
0109
0110 #define INT_STATUS 0x2a
0111 #define INT_MASK 0x3d
0112 #define INT_CLEAR 0x3e
0113
0114
0115
0116
0117 #define COORD_UPDATE 0x01
0118 #define CALIBRATION_DONE 0x02
0119 #define SLEEP_IN 0x04
0120 #define SLEEP_OUT 0x08
0121 #define PROGRAM_LOAD_DONE 0x10
0122 #define ERROR 0x80
0123 #define INT_ALL 0x9f
0124
0125 #define ERR_STATUS 0x2b
0126 #define ERR_MASK 0x3f
0127
0128
0129
0130
0131 #define ADC_TIMEOUT 0x01
0132 #define CPU_TIMEOUT 0x02
0133 #define CALIBRATION_ERR 0x04
0134 #define PROGRAM_LOAD_ERR 0x10
0135
0136 #define COMMON_SETUP1 0x30
0137 #define PROGRAM_LOAD_HOST 0x02
0138 #define PROGRAM_LOAD_EEPROM 0x03
0139 #define CENSOR_4PORT 0x04
0140 #define CENSOR_8PORT 0x00
0141 #define CALIBRATION_TYPE_DEFAULT 0x08
0142 #define CALIBRATION_TYPE_SPECIAL 0x00
0143 #define INT_ACTIVE_HIGH 0x10
0144 #define INT_ACTIVE_LOW 0x00
0145 #define AUTO_CALIBRATION 0x40
0146 #define MANUAL_CALIBRATION 0x00
0147 #define COMMON_SETUP1_DEFAULT 0x4e
0148
0149 #define COMMON_SETUP2 0x31
0150 #define MAF_NONE 0x00
0151 #define MAF_1SAMPLE 0x01
0152 #define MAF_3SAMPLES 0x02
0153 #define MAF_5SAMPLES 0x03
0154 #define INV_Y 0x04
0155 #define INV_X 0x08
0156 #define SWAP_XY 0x10
0157
0158 #define COMMON_SETUP3 0x32
0159 #define EN_SLEEP 0x01
0160 #define EN_MULTI 0x02
0161 #define EN_GESTURE 0x04
0162 #define EN_INTVL 0x08
0163 #define SEL_STEP 0x10
0164 #define SEL_MULTI 0x20
0165 #define SEL_TBL_DEFAULT 0x40
0166
0167 #define INTERVAL_TIME 0x33
0168 #define INTERVAL_TIME_DEFAULT 0x10
0169
0170 #define STEP_X 0x34
0171 #define STEP_X_DEFAULT 0x41
0172
0173 #define STEP_Y 0x35
0174 #define STEP_Y_DEFAULT 0x8d
0175
0176 #define OFFSET_X 0x38
0177 #define OFFSET_X_DEFAULT 0x0c
0178
0179 #define OFFSET_Y 0x39
0180 #define OFFSET_Y_DEFAULT 0x0c
0181
0182 #define THRESHOLD_TOUCH 0x3a
0183 #define THRESHOLD_TOUCH_DEFAULT 0xa0
0184
0185 #define THRESHOLD_GESTURE 0x3b
0186 #define THRESHOLD_GESTURE_DEFAULT 0x17
0187
0188 #define SYSTEM 0x40
0189 #define ANALOG_POWER_ON 0x01
0190 #define ANALOG_POWER_OFF 0x00
0191 #define CPU_POWER_ON 0x02
0192 #define CPU_POWER_OFF 0x00
0193
0194 #define FORCE_CALIBRATION 0x42
0195 #define FORCE_CALIBRATION_ON 0x01
0196 #define FORCE_CALIBRATION_OFF 0x00
0197
0198 #define CPU_FREQ 0x50
0199 #define CPU_FREQ_10MHZ 0x00
0200 #define CPU_FREQ_5MHZ 0x01
0201 #define CPU_FREQ_1MHZ 0x09
0202
0203 #define EEPROM_ADDR 0x51
0204
0205 #define CALIBRATION_ADJUST 0x52
0206 #define CALIBRATION_ADJUST_DEFAULT 0x00
0207
0208 #define THRESHOLD_SLEEP_IN 0x53
0209
0210 #define EVR_XY 0x56
0211 #define EVR_XY_DEFAULT 0x10
0212
0213 #define PRM_SWOFF_TIME 0x57
0214 #define PRM_SWOFF_TIME_DEFAULT 0x04
0215
0216 #define PROGRAM_VERSION 0x5f
0217
0218 #define ADC_CTRL 0x60
0219 #define ADC_DIV_MASK 0x1f
0220 #define ADC_DIV_DEFAULT 0x08
0221
0222 #define ADC_WAIT 0x61
0223 #define ADC_WAIT_DEFAULT 0x0a
0224
0225 #define SWCONT 0x62
0226 #define SWCONT_DEFAULT 0x0f
0227
0228 #define EVR_X 0x63
0229 #define EVR_X_DEFAULT 0x86
0230
0231 #define EVR_Y 0x64
0232 #define EVR_Y_DEFAULT 0x64
0233
0234 #define TEST1 0x65
0235 #define DUALTOUCH_STABILIZE_ON 0x01
0236 #define DUALTOUCH_STABILIZE_OFF 0x00
0237 #define DUALTOUCH_REG_ON 0x20
0238 #define DUALTOUCH_REG_OFF 0x00
0239
0240 #define CALIBRATION_REG1 0x68
0241 #define CALIBRATION_REG1_DEFAULT 0xd9
0242
0243 #define CALIBRATION_REG2 0x69
0244 #define CALIBRATION_REG2_DEFAULT 0x36
0245
0246 #define CALIBRATION_REG3 0x6a
0247 #define CALIBRATION_REG3_DEFAULT 0x32
0248
0249 #define EX_ADDR_H 0x70
0250 #define EX_ADDR_L 0x71
0251 #define EX_WDAT 0x72
0252 #define EX_RDAT 0x73
0253 #define EX_CHK_SUM1 0x74
0254 #define EX_CHK_SUM2 0x75
0255 #define EX_CHK_SUM3 0x76
0256
0257 struct rohm_ts_data {
0258 struct i2c_client *client;
0259 struct input_dev *input;
0260
0261 bool initialized;
0262
0263 unsigned int contact_count[MAX_CONTACTS + 1];
0264 int finger_count;
0265
0266 u8 setup2;
0267 };
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282 static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
0283 size_t len)
0284 {
0285 struct i2c_adapter *adap = client->adapter;
0286 struct i2c_msg msg[2];
0287 int i, ret = 0;
0288
0289 msg[0].addr = client->addr;
0290 msg[0].flags = 0;
0291 msg[0].len = 1;
0292 msg[0].buf = &start;
0293
0294 msg[1].addr = client->addr;
0295 msg[1].flags = I2C_M_RD;
0296 msg[1].len = len;
0297 msg[1].buf = buf;
0298
0299 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
0300
0301 for (i = 0; i < 2; i++) {
0302 if (__i2c_transfer(adap, &msg[i], 1) < 0) {
0303 ret = -EIO;
0304 break;
0305 }
0306 }
0307
0308 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
0309
0310 return ret;
0311 }
0312
0313 static int rohm_ts_manual_calibration(struct rohm_ts_data *ts)
0314 {
0315 struct i2c_client *client = ts->client;
0316 struct device *dev = &client->dev;
0317 u8 buf[33];
0318
0319 int retry;
0320 bool success = false;
0321 bool first_time = true;
0322 bool calibration_done;
0323
0324 u8 reg1, reg2, reg3;
0325 s32 reg1_orig, reg2_orig, reg3_orig;
0326 s32 val;
0327
0328 int calib_x = 0, calib_y = 0;
0329 int reg_x, reg_y;
0330 int err_x, err_y;
0331
0332 int error, error2;
0333 int i;
0334
0335 reg1_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG1);
0336 if (reg1_orig < 0)
0337 return reg1_orig;
0338
0339 reg2_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG2);
0340 if (reg2_orig < 0)
0341 return reg2_orig;
0342
0343 reg3_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG3);
0344 if (reg3_orig < 0)
0345 return reg3_orig;
0346
0347 error = i2c_smbus_write_byte_data(client, INT_MASK,
0348 COORD_UPDATE | SLEEP_IN | SLEEP_OUT |
0349 PROGRAM_LOAD_DONE);
0350 if (error)
0351 goto out;
0352
0353 error = i2c_smbus_write_byte_data(client, TEST1,
0354 DUALTOUCH_STABILIZE_ON);
0355 if (error)
0356 goto out;
0357
0358 for (retry = 0; retry < CALIBRATION_RETRY_MAX; retry++) {
0359
0360 mdelay(2 * SAMPLING_DELAY);
0361
0362 #define READ_CALIB_BUF(reg) buf[((reg) - PRM1_X_H)]
0363
0364 error = rohm_i2c_burst_read(client, PRM1_X_H, buf, sizeof(buf));
0365 if (error)
0366 goto out;
0367
0368 if (READ_CALIB_BUF(TOUCH) & TOUCH_DETECT)
0369 continue;
0370
0371 if (first_time) {
0372
0373 calib_x = ((int)READ_CALIB_BUF(PRM1_X_H) << 2 |
0374 READ_CALIB_BUF(PRM1_X_L)) - AXIS_OFFSET;
0375 calib_y = ((int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
0376 READ_CALIB_BUF(PRM1_Y_L)) - AXIS_OFFSET;
0377
0378 error = i2c_smbus_write_byte_data(client, TEST1,
0379 DUALTOUCH_STABILIZE_ON | DUALTOUCH_REG_ON);
0380 if (error)
0381 goto out;
0382
0383 first_time = false;
0384 } else {
0385
0386 err_x = (int)READ_CALIB_BUF(PRM1_X_H) << 2 |
0387 READ_CALIB_BUF(PRM1_X_L);
0388 err_y = (int)READ_CALIB_BUF(PRM1_Y_H) << 2 |
0389 READ_CALIB_BUF(PRM1_Y_L);
0390
0391
0392 if (err_x <= 4)
0393 calib_x -= AXIS_ADJUST;
0394 else if (err_x >= 60)
0395 calib_x += AXIS_ADJUST;
0396
0397
0398 if (err_y <= 4)
0399 calib_y -= AXIS_ADJUST;
0400 else if (err_y >= 60)
0401 calib_y += AXIS_ADJUST;
0402 }
0403
0404
0405 reg_x = calib_x + ((calib_x & 0x200) << 1);
0406 reg_y = calib_y + ((calib_y & 0x200) << 1);
0407
0408
0409 reg1 = reg_x >> 3;
0410 reg2 = (reg_y & 0x7) << 4 | (reg_x & 0x7);
0411 reg3 = reg_y >> 3;
0412
0413 error = i2c_smbus_write_byte_data(client,
0414 CALIBRATION_REG1, reg1);
0415 if (error)
0416 goto out;
0417
0418 error = i2c_smbus_write_byte_data(client,
0419 CALIBRATION_REG2, reg2);
0420 if (error)
0421 goto out;
0422
0423 error = i2c_smbus_write_byte_data(client,
0424 CALIBRATION_REG3, reg3);
0425 if (error)
0426 goto out;
0427
0428
0429
0430
0431 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
0432 FORCE_CALIBRATION_OFF);
0433 if (error)
0434 goto out;
0435
0436 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
0437 FORCE_CALIBRATION_ON);
0438 if (error)
0439 goto out;
0440
0441
0442 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
0443 if (error)
0444 goto out;
0445
0446
0447
0448
0449 calibration_done = false;
0450
0451 for (i = 0; i < 10; i++) {
0452 mdelay(SAMPLING_DELAY);
0453
0454 val = i2c_smbus_read_byte_data(client, TOUCH_GESTURE);
0455 if (!(val & CALIBRATION_MASK)) {
0456 calibration_done = true;
0457 break;
0458 } else if (val < 0) {
0459 error = val;
0460 goto out;
0461 }
0462 }
0463
0464 if (calibration_done) {
0465 val = i2c_smbus_read_byte_data(client, INT_STATUS);
0466 if (val == CALIBRATION_DONE) {
0467 success = true;
0468 break;
0469 } else if (val < 0) {
0470 error = val;
0471 goto out;
0472 }
0473 } else {
0474 dev_warn(dev, "calibration timeout\n");
0475 }
0476 }
0477
0478 if (!success) {
0479 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
0480 reg1_orig);
0481 if (error)
0482 goto out;
0483
0484 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
0485 reg2_orig);
0486 if (error)
0487 goto out;
0488
0489 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
0490 reg3_orig);
0491 if (error)
0492 goto out;
0493
0494
0495 error = i2c_smbus_write_byte_data(client, TEST1,
0496 DUALTOUCH_STABILIZE_ON |
0497 DUALTOUCH_REG_ON);
0498 if (error)
0499 goto out;
0500
0501
0502 mdelay(10 * SAMPLING_DELAY);
0503
0504 error = -EBUSY;
0505 }
0506
0507 out:
0508 error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
0509 if (!error2)
0510
0511 error2 = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
0512
0513 return error ? error : error2;
0514 }
0515
0516 static const unsigned int untouch_threshold[3] = { 0, 1, 5 };
0517 static const unsigned int single_touch_threshold[3] = { 0, 0, 4 };
0518 static const unsigned int dual_touch_threshold[3] = { 10, 8, 0 };
0519
0520 static irqreturn_t rohm_ts_soft_irq(int irq, void *dev_id)
0521 {
0522 struct rohm_ts_data *ts = dev_id;
0523 struct i2c_client *client = ts->client;
0524 struct input_dev *input_dev = ts->input;
0525 struct device *dev = &client->dev;
0526
0527 u8 buf[10];
0528
0529 struct input_mt_pos pos[MAX_CONTACTS];
0530 int slots[MAX_CONTACTS];
0531 u8 touch_flags;
0532 unsigned int threshold;
0533 int finger_count = -1;
0534 int prev_finger_count = ts->finger_count;
0535 int count;
0536 int error;
0537 int i;
0538
0539 error = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
0540 if (error)
0541 return IRQ_HANDLED;
0542
0543
0544 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
0545 if (error)
0546 return IRQ_HANDLED;
0547
0548 #define READ_POS_BUF(reg) buf[((reg) - POS_X1_H)]
0549
0550 error = rohm_i2c_burst_read(client, POS_X1_H, buf, sizeof(buf));
0551 if (error)
0552 return IRQ_HANDLED;
0553
0554 touch_flags = READ_POS_BUF(TOUCH_GESTURE) & TOUCH_MASK;
0555 if (touch_flags) {
0556
0557 pos[0].x = ((s16)READ_POS_BUF(POS_X1_H) << 2) |
0558 READ_POS_BUF(POS_X1_L);
0559 pos[0].y = ((s16)READ_POS_BUF(POS_Y1_H) << 2) |
0560 READ_POS_BUF(POS_Y1_L);
0561 pos[1].x = ((s16)READ_POS_BUF(POS_X2_H) << 2) |
0562 READ_POS_BUF(POS_X2_L);
0563 pos[1].y = ((s16)READ_POS_BUF(POS_Y2_H) << 2) |
0564 READ_POS_BUF(POS_Y2_L);
0565 }
0566
0567 switch (touch_flags) {
0568 case 0:
0569 threshold = untouch_threshold[prev_finger_count];
0570 if (++ts->contact_count[0] >= threshold)
0571 finger_count = 0;
0572 break;
0573
0574 case SINGLE_TOUCH:
0575 threshold = single_touch_threshold[prev_finger_count];
0576 if (++ts->contact_count[1] >= threshold)
0577 finger_count = 1;
0578
0579 if (finger_count == 1) {
0580 if (pos[1].x != 0 && pos[1].y != 0) {
0581 pos[0].x = pos[1].x;
0582 pos[0].y = pos[1].y;
0583 pos[1].x = 0;
0584 pos[1].y = 0;
0585 }
0586 }
0587 break;
0588
0589 case DUAL_TOUCH:
0590 threshold = dual_touch_threshold[prev_finger_count];
0591 if (++ts->contact_count[2] >= threshold)
0592 finger_count = 2;
0593 break;
0594
0595 default:
0596 dev_dbg(dev,
0597 "Three or more touches are not supported\n");
0598 return IRQ_HANDLED;
0599 }
0600
0601 if (finger_count >= 0) {
0602 if (prev_finger_count != finger_count) {
0603 count = ts->contact_count[finger_count];
0604 memset(ts->contact_count, 0, sizeof(ts->contact_count));
0605 ts->contact_count[finger_count] = count;
0606 }
0607
0608 input_mt_assign_slots(input_dev, slots, pos,
0609 finger_count, ROHM_TS_DISPLACEMENT_MAX);
0610
0611 for (i = 0; i < finger_count; i++) {
0612 input_mt_slot(input_dev, slots[i]);
0613 input_mt_report_slot_state(input_dev,
0614 MT_TOOL_FINGER, true);
0615 input_report_abs(input_dev,
0616 ABS_MT_POSITION_X, pos[i].x);
0617 input_report_abs(input_dev,
0618 ABS_MT_POSITION_Y, pos[i].y);
0619 }
0620
0621 input_mt_sync_frame(input_dev);
0622 input_mt_report_pointer_emulation(input_dev, true);
0623 input_sync(input_dev);
0624
0625 ts->finger_count = finger_count;
0626 }
0627
0628 if (READ_POS_BUF(TOUCH_GESTURE) & CALIBRATION_REQUEST) {
0629 error = rohm_ts_manual_calibration(ts);
0630 if (error)
0631 dev_warn(dev, "manual calibration failed: %d\n",
0632 error);
0633 }
0634
0635 i2c_smbus_write_byte_data(client, INT_MASK,
0636 CALIBRATION_DONE | SLEEP_OUT | SLEEP_IN |
0637 PROGRAM_LOAD_DONE);
0638
0639 return IRQ_HANDLED;
0640 }
0641
0642 static int rohm_ts_load_firmware(struct i2c_client *client,
0643 const char *firmware_name)
0644 {
0645 struct device *dev = &client->dev;
0646 const struct firmware *fw;
0647 s32 status;
0648 unsigned int offset, len, xfer_len;
0649 unsigned int retry = 0;
0650 int error, error2;
0651
0652 error = request_firmware(&fw, firmware_name, dev);
0653 if (error) {
0654 dev_err(dev, "unable to retrieve firmware %s: %d\n",
0655 firmware_name, error);
0656 return error;
0657 }
0658
0659 error = i2c_smbus_write_byte_data(client, INT_MASK,
0660 COORD_UPDATE | CALIBRATION_DONE |
0661 SLEEP_IN | SLEEP_OUT);
0662 if (error)
0663 goto out;
0664
0665 do {
0666 if (retry) {
0667 dev_warn(dev, "retrying firmware load\n");
0668
0669
0670 error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
0671 if (error)
0672 goto out;
0673 }
0674
0675 error = i2c_smbus_write_byte_data(client, EX_ADDR_H, 0);
0676 if (error)
0677 goto out;
0678
0679 error = i2c_smbus_write_byte_data(client, EX_ADDR_L, 0);
0680 if (error)
0681 goto out;
0682
0683 error = i2c_smbus_write_byte_data(client, COMMON_SETUP1,
0684 COMMON_SETUP1_DEFAULT);
0685 if (error)
0686 goto out;
0687
0688
0689 offset = 0;
0690 len = fw->size;
0691
0692 while (len) {
0693 xfer_len = min(FIRMWARE_BLOCK_SIZE, len);
0694
0695 error = i2c_smbus_write_i2c_block_data(client, EX_WDAT,
0696 xfer_len, &fw->data[offset]);
0697 if (error)
0698 goto out;
0699
0700 len -= xfer_len;
0701 offset += xfer_len;
0702 }
0703
0704
0705 status = i2c_smbus_read_byte_data(client, INT_STATUS);
0706 if (status < 0) {
0707 error = status;
0708 goto out;
0709 }
0710
0711
0712 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
0713 if (error)
0714 goto out;
0715
0716 if (status == PROGRAM_LOAD_DONE)
0717 break;
0718
0719 error = -EIO;
0720 } while (++retry <= FIRMWARE_RETRY_MAX);
0721
0722 out:
0723 error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
0724
0725 release_firmware(fw);
0726
0727 return error ? error : error2;
0728 }
0729
0730 static ssize_t swap_xy_show(struct device *dev, struct device_attribute *attr,
0731 char *buf)
0732 {
0733 struct i2c_client *client = to_i2c_client(dev);
0734 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0735
0736 return sprintf(buf, "%d\n", !!(ts->setup2 & SWAP_XY));
0737 }
0738
0739 static ssize_t swap_xy_store(struct device *dev, struct device_attribute *attr,
0740 const char *buf, size_t count)
0741 {
0742 struct i2c_client *client = to_i2c_client(dev);
0743 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0744 unsigned int val;
0745 int error;
0746
0747 error = kstrtouint(buf, 0, &val);
0748 if (error)
0749 return error;
0750
0751 error = mutex_lock_interruptible(&ts->input->mutex);
0752 if (error)
0753 return error;
0754
0755 if (val)
0756 ts->setup2 |= SWAP_XY;
0757 else
0758 ts->setup2 &= ~SWAP_XY;
0759
0760 if (ts->initialized)
0761 error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
0762 ts->setup2);
0763
0764 mutex_unlock(&ts->input->mutex);
0765
0766 return error ? error : count;
0767 }
0768
0769 static ssize_t inv_x_show(struct device *dev, struct device_attribute *attr,
0770 char *buf)
0771 {
0772 struct i2c_client *client = to_i2c_client(dev);
0773 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0774
0775 return sprintf(buf, "%d\n", !!(ts->setup2 & INV_X));
0776 }
0777
0778 static ssize_t inv_x_store(struct device *dev, struct device_attribute *attr,
0779 const char *buf, size_t count)
0780 {
0781 struct i2c_client *client = to_i2c_client(dev);
0782 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0783 unsigned int val;
0784 int error;
0785
0786 error = kstrtouint(buf, 0, &val);
0787 if (error)
0788 return error;
0789
0790 error = mutex_lock_interruptible(&ts->input->mutex);
0791 if (error)
0792 return error;
0793
0794 if (val)
0795 ts->setup2 |= INV_X;
0796 else
0797 ts->setup2 &= ~INV_X;
0798
0799 if (ts->initialized)
0800 error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
0801 ts->setup2);
0802
0803 mutex_unlock(&ts->input->mutex);
0804
0805 return error ? error : count;
0806 }
0807
0808 static ssize_t inv_y_show(struct device *dev, struct device_attribute *attr,
0809 char *buf)
0810 {
0811 struct i2c_client *client = to_i2c_client(dev);
0812 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0813
0814 return sprintf(buf, "%d\n", !!(ts->setup2 & INV_Y));
0815 }
0816
0817 static ssize_t inv_y_store(struct device *dev, struct device_attribute *attr,
0818 const char *buf, size_t count)
0819 {
0820 struct i2c_client *client = to_i2c_client(dev);
0821 struct rohm_ts_data *ts = i2c_get_clientdata(client);
0822 unsigned int val;
0823 int error;
0824
0825 error = kstrtouint(buf, 0, &val);
0826 if (error)
0827 return error;
0828
0829 error = mutex_lock_interruptible(&ts->input->mutex);
0830 if (error)
0831 return error;
0832
0833 if (val)
0834 ts->setup2 |= INV_Y;
0835 else
0836 ts->setup2 &= ~INV_Y;
0837
0838 if (ts->initialized)
0839 error = i2c_smbus_write_byte_data(client, COMMON_SETUP2,
0840 ts->setup2);
0841
0842 mutex_unlock(&ts->input->mutex);
0843
0844 return error ? error : count;
0845 }
0846
0847 static DEVICE_ATTR_RW(swap_xy);
0848 static DEVICE_ATTR_RW(inv_x);
0849 static DEVICE_ATTR_RW(inv_y);
0850
0851 static struct attribute *rohm_ts_attrs[] = {
0852 &dev_attr_swap_xy.attr,
0853 &dev_attr_inv_x.attr,
0854 &dev_attr_inv_y.attr,
0855 NULL,
0856 };
0857
0858 static const struct attribute_group rohm_ts_attr_group = {
0859 .attrs = rohm_ts_attrs,
0860 };
0861
0862 static int rohm_ts_device_init(struct i2c_client *client, u8 setup2)
0863 {
0864 struct device *dev = &client->dev;
0865 int error;
0866
0867 disable_irq(client->irq);
0868
0869
0870
0871
0872 udelay(200);
0873
0874
0875 error = i2c_smbus_write_byte_data(client, SYSTEM,
0876 ANALOG_POWER_ON | CPU_POWER_OFF);
0877 if (error)
0878 return error;
0879
0880
0881 udelay(200);
0882
0883
0884 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
0885 if (error)
0886 return error;
0887
0888 error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
0889 if (error)
0890 return error;
0891
0892 error = i2c_smbus_write_byte_data(client, COMMON_SETUP1, 0);
0893 if (error)
0894 return error;
0895
0896 error = i2c_smbus_write_byte_data(client, COMMON_SETUP2, setup2);
0897 if (error)
0898 return error;
0899
0900 error = i2c_smbus_write_byte_data(client, COMMON_SETUP3,
0901 SEL_TBL_DEFAULT | EN_MULTI);
0902 if (error)
0903 return error;
0904
0905 error = i2c_smbus_write_byte_data(client, THRESHOLD_GESTURE,
0906 THRESHOLD_GESTURE_DEFAULT);
0907 if (error)
0908 return error;
0909
0910 error = i2c_smbus_write_byte_data(client, INTERVAL_TIME,
0911 INTERVAL_TIME_DEFAULT);
0912 if (error)
0913 return error;
0914
0915 error = i2c_smbus_write_byte_data(client, CPU_FREQ, CPU_FREQ_10MHZ);
0916 if (error)
0917 return error;
0918
0919 error = i2c_smbus_write_byte_data(client, PRM_SWOFF_TIME,
0920 PRM_SWOFF_TIME_DEFAULT);
0921 if (error)
0922 return error;
0923
0924 error = i2c_smbus_write_byte_data(client, ADC_CTRL, ADC_DIV_DEFAULT);
0925 if (error)
0926 return error;
0927
0928 error = i2c_smbus_write_byte_data(client, ADC_WAIT, ADC_WAIT_DEFAULT);
0929 if (error)
0930 return error;
0931
0932
0933
0934
0935 error = i2c_smbus_write_byte_data(client, STEP_X, STEP_X_DEFAULT);
0936 if (error)
0937 return error;
0938
0939 error = i2c_smbus_write_byte_data(client, STEP_Y, STEP_Y_DEFAULT);
0940 if (error)
0941 return error;
0942
0943 error = i2c_smbus_write_byte_data(client, OFFSET_X, OFFSET_X_DEFAULT);
0944 if (error)
0945 return error;
0946
0947 error = i2c_smbus_write_byte_data(client, OFFSET_Y, OFFSET_Y_DEFAULT);
0948 if (error)
0949 return error;
0950
0951 error = i2c_smbus_write_byte_data(client, THRESHOLD_TOUCH,
0952 THRESHOLD_TOUCH_DEFAULT);
0953 if (error)
0954 return error;
0955
0956 error = i2c_smbus_write_byte_data(client, EVR_XY, EVR_XY_DEFAULT);
0957 if (error)
0958 return error;
0959
0960 error = i2c_smbus_write_byte_data(client, EVR_X, EVR_X_DEFAULT);
0961 if (error)
0962 return error;
0963
0964 error = i2c_smbus_write_byte_data(client, EVR_Y, EVR_Y_DEFAULT);
0965 if (error)
0966 return error;
0967
0968
0969 error = i2c_smbus_write_byte_data(client, CALIBRATION_ADJUST,
0970 CALIBRATION_ADJUST_DEFAULT);
0971 if (error)
0972 return error;
0973
0974 error = i2c_smbus_write_byte_data(client, SWCONT, SWCONT_DEFAULT);
0975 if (error)
0976 return error;
0977
0978 error = i2c_smbus_write_byte_data(client, TEST1,
0979 DUALTOUCH_STABILIZE_ON |
0980 DUALTOUCH_REG_ON);
0981 if (error)
0982 return error;
0983
0984 error = rohm_ts_load_firmware(client, BU21023_FIRMWARE_NAME);
0985 if (error) {
0986 dev_err(dev, "failed to load firmware: %d\n", error);
0987 return error;
0988 }
0989
0990
0991
0992
0993
0994
0995
0996 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
0997 CALIBRATION_REG1_DEFAULT);
0998 if (error)
0999 return error;
1000
1001 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
1002 CALIBRATION_REG2_DEFAULT);
1003 if (error)
1004 return error;
1005
1006 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
1007 CALIBRATION_REG3_DEFAULT);
1008 if (error)
1009 return error;
1010
1011 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1012 FORCE_CALIBRATION_OFF);
1013 if (error)
1014 return error;
1015
1016 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1017 FORCE_CALIBRATION_ON);
1018 if (error)
1019 return error;
1020
1021
1022 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
1023 if (error)
1024 return error;
1025
1026
1027 error = i2c_smbus_write_byte_data(client, INT_MASK,
1028 CALIBRATION_DONE | SLEEP_OUT |
1029 SLEEP_IN | PROGRAM_LOAD_DONE);
1030 if (error)
1031 return error;
1032
1033 error = i2c_smbus_write_byte_data(client, ERR_MASK,
1034 PROGRAM_LOAD_ERR | CPU_TIMEOUT |
1035 ADC_TIMEOUT);
1036 if (error)
1037 return error;
1038
1039
1040 error = i2c_smbus_write_byte_data(client, SYSTEM,
1041 ANALOG_POWER_ON | CPU_POWER_ON);
1042
1043 enable_irq(client->irq);
1044
1045 return error;
1046 }
1047
1048 static int rohm_ts_power_off(struct i2c_client *client)
1049 {
1050 int error;
1051
1052 error = i2c_smbus_write_byte_data(client, SYSTEM,
1053 ANALOG_POWER_ON | CPU_POWER_OFF);
1054 if (error) {
1055 dev_err(&client->dev,
1056 "failed to power off device CPU: %d\n", error);
1057 return error;
1058 }
1059
1060 error = i2c_smbus_write_byte_data(client, SYSTEM,
1061 ANALOG_POWER_OFF | CPU_POWER_OFF);
1062 if (error)
1063 dev_err(&client->dev,
1064 "failed to power off the device: %d\n", error);
1065
1066 return error;
1067 }
1068
1069 static int rohm_ts_open(struct input_dev *input_dev)
1070 {
1071 struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1072 struct i2c_client *client = ts->client;
1073 int error;
1074
1075 if (!ts->initialized) {
1076 error = rohm_ts_device_init(client, ts->setup2);
1077 if (error) {
1078 dev_err(&client->dev,
1079 "device initialization failed: %d\n", error);
1080 return error;
1081 }
1082
1083 ts->initialized = true;
1084 }
1085
1086 return 0;
1087 }
1088
1089 static void rohm_ts_close(struct input_dev *input_dev)
1090 {
1091 struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1092
1093 rohm_ts_power_off(ts->client);
1094
1095 ts->initialized = false;
1096 }
1097
1098 static int rohm_bu21023_i2c_probe(struct i2c_client *client,
1099 const struct i2c_device_id *id)
1100 {
1101 struct device *dev = &client->dev;
1102 struct rohm_ts_data *ts;
1103 struct input_dev *input;
1104 int error;
1105
1106 if (!client->irq) {
1107 dev_err(dev, "IRQ is not assigned\n");
1108 return -EINVAL;
1109 }
1110
1111 if (!client->adapter->algo->master_xfer) {
1112 dev_err(dev, "I2C level transfers not supported\n");
1113 return -EOPNOTSUPP;
1114 }
1115
1116
1117 error = rohm_ts_power_off(client);
1118 if (error)
1119 return error;
1120
1121 ts = devm_kzalloc(dev, sizeof(struct rohm_ts_data), GFP_KERNEL);
1122 if (!ts)
1123 return -ENOMEM;
1124
1125 ts->client = client;
1126 ts->setup2 = MAF_1SAMPLE;
1127 i2c_set_clientdata(client, ts);
1128
1129 input = devm_input_allocate_device(dev);
1130 if (!input)
1131 return -ENOMEM;
1132
1133 input->name = BU21023_NAME;
1134 input->id.bustype = BUS_I2C;
1135 input->open = rohm_ts_open;
1136 input->close = rohm_ts_close;
1137
1138 ts->input = input;
1139 input_set_drvdata(input, ts);
1140
1141 input_set_abs_params(input, ABS_MT_POSITION_X,
1142 ROHM_TS_ABS_X_MIN, ROHM_TS_ABS_X_MAX, 0, 0);
1143 input_set_abs_params(input, ABS_MT_POSITION_Y,
1144 ROHM_TS_ABS_Y_MIN, ROHM_TS_ABS_Y_MAX, 0, 0);
1145
1146 error = input_mt_init_slots(input, MAX_CONTACTS,
1147 INPUT_MT_DIRECT | INPUT_MT_TRACK |
1148 INPUT_MT_DROP_UNUSED);
1149 if (error) {
1150 dev_err(dev, "failed to multi touch slots initialization\n");
1151 return error;
1152 }
1153
1154 error = devm_request_threaded_irq(dev, client->irq,
1155 NULL, rohm_ts_soft_irq,
1156 IRQF_ONESHOT, client->name, ts);
1157 if (error) {
1158 dev_err(dev, "failed to request IRQ: %d\n", error);
1159 return error;
1160 }
1161
1162 error = input_register_device(input);
1163 if (error) {
1164 dev_err(dev, "failed to register input device: %d\n", error);
1165 return error;
1166 }
1167
1168 error = devm_device_add_group(dev, &rohm_ts_attr_group);
1169 if (error) {
1170 dev_err(dev, "failed to create sysfs group: %d\n", error);
1171 return error;
1172 }
1173
1174 return error;
1175 }
1176
1177 static const struct i2c_device_id rohm_bu21023_i2c_id[] = {
1178 { BU21023_NAME, 0 },
1179 { }
1180 };
1181 MODULE_DEVICE_TABLE(i2c, rohm_bu21023_i2c_id);
1182
1183 static struct i2c_driver rohm_bu21023_i2c_driver = {
1184 .driver = {
1185 .name = BU21023_NAME,
1186 },
1187 .probe = rohm_bu21023_i2c_probe,
1188 .id_table = rohm_bu21023_i2c_id,
1189 };
1190 module_i2c_driver(rohm_bu21023_i2c_driver);
1191
1192 MODULE_DESCRIPTION("ROHM BU21023/24 Touchscreen driver");
1193 MODULE_LICENSE("GPL v2");
1194 MODULE_AUTHOR("ROHM Co., Ltd.");