2 * ROHM BU21023/24 Dual touch support resistive touch screen driver
3 * Copyright (C) 2012 ROHM CO.,LTD.
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.
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.
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>
23 #define BU21023_NAME "bu21023_ts"
24 #define BU21023_FIRMWARE_NAME "bu21023.bin"
26 #define MAX_CONTACTS 2
31 #define FIRMWARE_BLOCK_SIZE 32U
32 #define FIRMWARE_RETRY_MAX 4
34 #define SAMPLING_DELAY 12 /* msec */
36 #define CALIBRATION_RETRY_MAX 6
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 */
45 * BU21023GUL/BU21023MUV/BU21024FV-M registers map
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
65 #define MLT_PRM_MONI_X 0x10
66 #define MLT_PRM_MONI_Y 0x11
68 #define DEBUG_MONI_1 0x12
69 #define DEBUG_MONI_2 0x13
71 #define VADOUT_ZX_H 0x14
72 #define VADOUT_ZX_L 0x15
73 #define VADOUT_ZY_H 0x16
74 #define VADOUT_ZY_L 0x17
76 #define Z_PARAM_H 0x18
77 #define Z_PARAM_L 0x19
80 * Value for VADOUT_*_L
82 #define VADOUT_L_MASK 0x01
87 #define PRM_L_MASK 0x01
101 #define POS_L_MASK 0x01
104 #define TOUCH_DETECT 0x01
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
118 #define INT_STATUS 0x2a
119 #define INT_MASK 0x3d
120 #define INT_CLEAR 0x3e
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
133 #define ERR_STATUS 0x2b
134 #define ERR_MASK 0x3f
139 #define ADC_TIMEOUT 0x01
140 #define CPU_TIMEOUT 0x02
141 #define CALIBRATION_ERR 0x04
142 #define PROGRAM_LOAD_ERR 0x10
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
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
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
175 #define INTERVAL_TIME 0x33
176 #define INTERVAL_TIME_DEFAULT 0x10
179 #define STEP_X_DEFAULT 0x41
182 #define STEP_Y_DEFAULT 0x8d
184 #define OFFSET_X 0x38
185 #define OFFSET_X_DEFAULT 0x0c
187 #define OFFSET_Y 0x39
188 #define OFFSET_Y_DEFAULT 0x0c
190 #define THRESHOLD_TOUCH 0x3a
191 #define THRESHOLD_TOUCH_DEFAULT 0xa0
193 #define THRESHOLD_GESTURE 0x3b
194 #define THRESHOLD_GESTURE_DEFAULT 0x17
197 #define ANALOG_POWER_ON 0x01
198 #define ANALOG_POWER_OFF 0x00
199 #define CPU_POWER_ON 0x02
200 #define CPU_POWER_OFF 0x00
202 #define FORCE_CALIBRATION 0x42
203 #define FORCE_CALIBRATION_ON 0x01
204 #define FORCE_CALIBRATION_OFF 0x00
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
211 #define EEPROM_ADDR 0x51
213 #define CALIBRATION_ADJUST 0x52
214 #define CALIBRATION_ADJUST_DEFAULT 0x00
216 #define THRESHOLD_SLEEP_IN 0x53
219 #define EVR_XY_DEFAULT 0x10
221 #define PRM_SWOFF_TIME 0x57
222 #define PRM_SWOFF_TIME_DEFAULT 0x04
224 #define PROGRAM_VERSION 0x5f
226 #define ADC_CTRL 0x60
227 #define ADC_DIV_MASK 0x1f /* The minimum value is 4 */
228 #define ADC_DIV_DEFAULT 0x08
230 #define ADC_WAIT 0x61
231 #define ADC_WAIT_DEFAULT 0x0a
234 #define SWCONT_DEFAULT 0x0f
237 #define EVR_X_DEFAULT 0x86
240 #define EVR_Y_DEFAULT 0x64
243 #define DUALTOUCH_STABILIZE_ON 0x01
244 #define DUALTOUCH_STABILIZE_OFF 0x00
245 #define DUALTOUCH_REG_ON 0x20
246 #define DUALTOUCH_REG_OFF 0x00
248 #define CALIBRATION_REG1 0x68
249 #define CALIBRATION_REG1_DEFAULT 0xd9
251 #define CALIBRATION_REG2 0x69
252 #define CALIBRATION_REG2_DEFAULT 0x36
254 #define CALIBRATION_REG3 0x6a
255 #define CALIBRATION_REG3_DEFAULT 0x32
257 #define EX_ADDR_H 0x70
258 #define EX_ADDR_L 0x71
261 #define EX_CHK_SUM1 0x74
262 #define EX_CHK_SUM2 0x75
263 #define EX_CHK_SUM3 0x76
265 struct rohm_ts_data {
266 struct i2c_client *client;
267 struct input_dev *input;
271 unsigned int contact_count[MAX_CONTACTS + 1];
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
284 * Returns negative errno, else zero on success.
287 * In BU21023/24 burst read, stop condition is needed after "address write".
288 * Therefore, transmission is performed in 2 steps.
290 static int rohm_i2c_burst_read(struct i2c_client *client, u8 start, void *buf,
293 struct i2c_adapter *adap = client->adapter;
294 struct i2c_msg msg[2];
297 msg[0].addr = client->addr;
302 msg[1].addr = client->addr;
303 msg[1].flags = I2C_M_RD;
307 i2c_lock_adapter(adap);
309 for (i = 0; i < 2; i++) {
310 if (__i2c_transfer(adap, &msg[i], 1) < 0) {
316 i2c_unlock_adapter(adap);
321 static int rohm_ts_manual_calibration(struct rohm_ts_data *ts)
323 struct i2c_client *client = ts->client;
324 struct device *dev = &client->dev;
325 u8 buf[33]; /* for PRM1_X_H(0x08)-TOUCH(0x28) */
328 bool success = false;
329 bool first_time = true;
330 bool calibration_done;
333 s32 reg1_orig, reg2_orig, reg3_orig;
336 int calib_x = 0, calib_y = 0;
343 reg1_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG1);
347 reg2_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG2);
351 reg3_orig = i2c_smbus_read_byte_data(client, CALIBRATION_REG3);
355 error = i2c_smbus_write_byte_data(client, INT_MASK,
356 COORD_UPDATE | SLEEP_IN | SLEEP_OUT |
361 error = i2c_smbus_write_byte_data(client, TEST1,
362 DUALTOUCH_STABILIZE_ON);
366 for (retry = 0; retry < CALIBRATION_RETRY_MAX; retry++) {
367 /* wait 2 sampling for update */
368 mdelay(2 * SAMPLING_DELAY);
370 #define READ_CALIB_BUF(reg) buf[((reg) - PRM1_X_H)]
372 error = rohm_i2c_burst_read(client, PRM1_X_H, buf, sizeof(buf));
376 if (READ_CALIB_BUF(TOUCH) & TOUCH_DETECT)
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;
386 error = i2c_smbus_write_byte_data(client, TEST1,
387 DUALTOUCH_STABILIZE_ON | DUALTOUCH_REG_ON);
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);
401 calib_x -= AXIS_ADJUST;
402 else if (err_x >= 60)
403 calib_x += AXIS_ADJUST;
407 calib_y -= AXIS_ADJUST;
408 else if (err_y >= 60)
409 calib_y += AXIS_ADJUST;
412 /* generate calibration setting value */
413 reg_x = calib_x + ((calib_x & 0x200) << 1);
414 reg_y = calib_y + ((calib_y & 0x200) << 1);
416 /* convert for register format */
418 reg2 = (reg_y & 0x7) << 4 | (reg_x & 0x7);
421 error = i2c_smbus_write_byte_data(client,
422 CALIBRATION_REG1, reg1);
426 error = i2c_smbus_write_byte_data(client,
427 CALIBRATION_REG2, reg2);
431 error = i2c_smbus_write_byte_data(client,
432 CALIBRATION_REG3, reg3);
437 * force calibration sequcence
439 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
440 FORCE_CALIBRATION_OFF);
444 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
445 FORCE_CALIBRATION_ON);
449 /* clear all interrupts */
450 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
455 * Wait for the status change of calibration, max 10 sampling
457 calibration_done = false;
459 for (i = 0; i < 10; i++) {
460 mdelay(SAMPLING_DELAY);
462 val = i2c_smbus_read_byte_data(client, TOUCH_GESTURE);
463 if (!(val & CALIBRATION_MASK)) {
464 calibration_done = true;
466 } else if (val < 0) {
472 if (calibration_done) {
473 val = i2c_smbus_read_byte_data(client, INT_STATUS);
474 if (val == CALIBRATION_DONE) {
477 } else if (val < 0) {
482 dev_warn(dev, "calibration timeout\n");
487 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
492 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
497 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
502 /* calibration data enable */
503 error = i2c_smbus_write_byte_data(client, TEST1,
504 DUALTOUCH_STABILIZE_ON |
509 /* wait 10 sampling */
510 mdelay(10 * SAMPLING_DELAY);
516 error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
518 /* Clear all interrupts */
519 error2 = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
521 return error ? error : error2;
524 static const unsigned int untouch_threshold[3] = { 0, 1, 5 };
525 static const unsigned int single_touch_threshold[3] = { 0, 0, 4 };
526 static const unsigned int dual_touch_threshold[3] = { 10, 8, 0 };
528 static irqreturn_t rohm_ts_soft_irq(int irq, void *dev_id)
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;
535 u8 buf[10]; /* for POS_X1_H(0x20)-TOUCH_GESTURE(0x29) */
537 struct input_mt_pos pos[MAX_CONTACTS];
538 int slots[MAX_CONTACTS];
540 unsigned int threshold;
541 int finger_count = -1;
542 int prev_finger_count = ts->finger_count;
547 error = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
551 /* Clear all interrupts */
552 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
556 #define READ_POS_BUF(reg) buf[((reg) - POS_X1_H)]
558 error = rohm_i2c_burst_read(client, POS_X1_H, buf, sizeof(buf));
562 touch_flags = READ_POS_BUF(TOUCH_GESTURE) & TOUCH_MASK;
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);
575 switch (touch_flags) {
577 threshold = untouch_threshold[prev_finger_count];
578 if (++ts->contact_count[0] >= threshold)
583 threshold = single_touch_threshold[prev_finger_count];
584 if (++ts->contact_count[1] >= threshold)
587 if (finger_count == 1) {
588 if (pos[1].x != 0 && pos[1].y != 0) {
598 threshold = dual_touch_threshold[prev_finger_count];
599 if (++ts->contact_count[2] >= threshold)
605 "Three or more touches are not supported\n");
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;
616 input_mt_assign_slots(input_dev, slots, pos,
617 finger_count, ROHM_TS_DISPLACEMENT_MAX);
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);
629 input_mt_sync_frame(input_dev);
630 input_mt_report_pointer_emulation(input_dev, true);
631 input_sync(input_dev);
633 ts->finger_count = finger_count;
636 if (READ_POS_BUF(TOUCH_GESTURE) & CALIBRATION_REQUEST) {
637 error = rohm_ts_manual_calibration(ts);
639 dev_warn(dev, "manual calibration failed: %d\n",
643 i2c_smbus_write_byte_data(client, INT_MASK,
644 CALIBRATION_DONE | SLEEP_OUT | SLEEP_IN |
650 static int rohm_ts_load_firmware(struct i2c_client *client,
651 const char *firmware_name)
653 struct device *dev = &client->dev;
654 const struct firmware *fw;
656 unsigned int offset, len, xfer_len;
657 unsigned int retry = 0;
660 error = request_firmware(&fw, firmware_name, dev);
662 dev_err(dev, "unable to retrieve firmware %s: %d\n",
663 firmware_name, error);
667 error = i2c_smbus_write_byte_data(client, INT_MASK,
668 COORD_UPDATE | CALIBRATION_DONE |
669 SLEEP_IN | SLEEP_OUT);
675 dev_warn(dev, "retrying firmware load\n");
677 /* settings for retry */
678 error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
683 error = i2c_smbus_write_byte_data(client, EX_ADDR_H, 0);
687 error = i2c_smbus_write_byte_data(client, EX_ADDR_L, 0);
691 error = i2c_smbus_write_byte_data(client, COMMON_SETUP1,
692 COMMON_SETUP1_DEFAULT);
696 /* firmware load to the device */
701 xfer_len = min(FIRMWARE_BLOCK_SIZE, len);
703 error = i2c_smbus_write_i2c_block_data(client, EX_WDAT,
704 xfer_len, &fw->data[offset]);
712 /* check firmware load result */
713 status = i2c_smbus_read_byte_data(client, INT_STATUS);
719 /* clear all interrupts */
720 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
724 if (status == PROGRAM_LOAD_DONE)
728 } while (++retry <= FIRMWARE_RETRY_MAX);
731 error2 = i2c_smbus_write_byte_data(client, INT_MASK, INT_ALL);
733 release_firmware(fw);
735 return error ? error : error2;
738 static ssize_t swap_xy_show(struct device *dev, struct device_attribute *attr,
741 struct i2c_client *client = to_i2c_client(dev);
742 struct rohm_ts_data *ts = i2c_get_clientdata(client);
744 return sprintf(buf, "%d\n", !!(ts->setup2 & SWAP_XY));
747 static ssize_t swap_xy_store(struct device *dev, struct device_attribute *attr,
748 const char *buf, size_t count)
750 struct i2c_client *client = to_i2c_client(dev);
751 struct rohm_ts_data *ts = i2c_get_clientdata(client);
755 error = kstrtouint(buf, 0, &val);
759 error = mutex_lock_interruptible(&ts->input->mutex);
764 ts->setup2 |= SWAP_XY;
766 ts->setup2 &= ~SWAP_XY;
769 error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
772 mutex_unlock(&ts->input->mutex);
774 return error ? error : count;
777 static ssize_t inv_x_show(struct device *dev, struct device_attribute *attr,
780 struct i2c_client *client = to_i2c_client(dev);
781 struct rohm_ts_data *ts = i2c_get_clientdata(client);
783 return sprintf(buf, "%d\n", !!(ts->setup2 & INV_X));
786 static ssize_t inv_x_store(struct device *dev, struct device_attribute *attr,
787 const char *buf, size_t count)
789 struct i2c_client *client = to_i2c_client(dev);
790 struct rohm_ts_data *ts = i2c_get_clientdata(client);
794 error = kstrtouint(buf, 0, &val);
798 error = mutex_lock_interruptible(&ts->input->mutex);
805 ts->setup2 &= ~INV_X;
808 error = i2c_smbus_write_byte_data(ts->client, COMMON_SETUP2,
811 mutex_unlock(&ts->input->mutex);
813 return error ? error : count;
816 static ssize_t inv_y_show(struct device *dev, struct device_attribute *attr,
819 struct i2c_client *client = to_i2c_client(dev);
820 struct rohm_ts_data *ts = i2c_get_clientdata(client);
822 return sprintf(buf, "%d\n", !!(ts->setup2 & INV_Y));
825 static ssize_t inv_y_store(struct device *dev, struct device_attribute *attr,
826 const char *buf, size_t count)
828 struct i2c_client *client = to_i2c_client(dev);
829 struct rohm_ts_data *ts = i2c_get_clientdata(client);
833 error = kstrtouint(buf, 0, &val);
837 error = mutex_lock_interruptible(&ts->input->mutex);
844 ts->setup2 &= ~INV_Y;
847 error = i2c_smbus_write_byte_data(client, COMMON_SETUP2,
850 mutex_unlock(&ts->input->mutex);
852 return error ? error : count;
855 static DEVICE_ATTR_RW(swap_xy);
856 static DEVICE_ATTR_RW(inv_x);
857 static DEVICE_ATTR_RW(inv_y);
859 static struct attribute *rohm_ts_attrs[] = {
860 &dev_attr_swap_xy.attr,
861 &dev_attr_inv_x.attr,
862 &dev_attr_inv_y.attr,
866 static const struct attribute_group rohm_ts_attr_group = {
867 .attrs = rohm_ts_attrs,
870 static int rohm_ts_device_init(struct i2c_client *client, u8 setup2)
872 struct device *dev = &client->dev;
875 disable_irq(client->irq);
878 * Wait 200usec for reset
882 /* Release analog reset */
883 error = i2c_smbus_write_byte_data(client, SYSTEM,
884 ANALOG_POWER_ON | CPU_POWER_OFF);
888 /* Waiting for the analog warm-up, max. 200usec */
891 /* clear all interrupts */
892 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
896 error = i2c_smbus_write_byte_data(client, EX_WDAT, 0);
900 error = i2c_smbus_write_byte_data(client, COMMON_SETUP1, 0);
904 error = i2c_smbus_write_byte_data(client, COMMON_SETUP2, setup2);
908 error = i2c_smbus_write_byte_data(client, COMMON_SETUP3,
909 SEL_TBL_DEFAULT | EN_MULTI);
913 error = i2c_smbus_write_byte_data(client, THRESHOLD_GESTURE,
914 THRESHOLD_GESTURE_DEFAULT);
918 error = i2c_smbus_write_byte_data(client, INTERVAL_TIME,
919 INTERVAL_TIME_DEFAULT);
923 error = i2c_smbus_write_byte_data(client, CPU_FREQ, CPU_FREQ_10MHZ);
927 error = i2c_smbus_write_byte_data(client, PRM_SWOFF_TIME,
928 PRM_SWOFF_TIME_DEFAULT);
932 error = i2c_smbus_write_byte_data(client, ADC_CTRL, ADC_DIV_DEFAULT);
936 error = i2c_smbus_write_byte_data(client, ADC_WAIT, ADC_WAIT_DEFAULT);
941 * Panel setup, these values change with the panel.
943 error = i2c_smbus_write_byte_data(client, STEP_X, STEP_X_DEFAULT);
947 error = i2c_smbus_write_byte_data(client, STEP_Y, STEP_Y_DEFAULT);
951 error = i2c_smbus_write_byte_data(client, OFFSET_X, OFFSET_X_DEFAULT);
955 error = i2c_smbus_write_byte_data(client, OFFSET_Y, OFFSET_Y_DEFAULT);
959 error = i2c_smbus_write_byte_data(client, THRESHOLD_TOUCH,
960 THRESHOLD_TOUCH_DEFAULT);
964 error = i2c_smbus_write_byte_data(client, EVR_XY, EVR_XY_DEFAULT);
968 error = i2c_smbus_write_byte_data(client, EVR_X, EVR_X_DEFAULT);
972 error = i2c_smbus_write_byte_data(client, EVR_Y, EVR_Y_DEFAULT);
976 /* Fixed value settings */
977 error = i2c_smbus_write_byte_data(client, CALIBRATION_ADJUST,
978 CALIBRATION_ADJUST_DEFAULT);
982 error = i2c_smbus_write_byte_data(client, SWCONT, SWCONT_DEFAULT);
986 error = i2c_smbus_write_byte_data(client, TEST1,
987 DUALTOUCH_STABILIZE_ON |
992 error = rohm_ts_load_firmware(client, BU21023_FIRMWARE_NAME);
994 dev_err(dev, "failed to load firmware: %d\n", error);
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.
1004 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG1,
1005 CALIBRATION_REG1_DEFAULT);
1009 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG2,
1010 CALIBRATION_REG2_DEFAULT);
1014 error = i2c_smbus_write_byte_data(client, CALIBRATION_REG3,
1015 CALIBRATION_REG3_DEFAULT);
1019 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1020 FORCE_CALIBRATION_OFF);
1024 error = i2c_smbus_write_byte_data(client, FORCE_CALIBRATION,
1025 FORCE_CALIBRATION_ON);
1029 /* Clear all interrupts */
1030 error = i2c_smbus_write_byte_data(client, INT_CLEAR, 0xff);
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);
1041 error = i2c_smbus_write_byte_data(client, ERR_MASK,
1042 PROGRAM_LOAD_ERR | CPU_TIMEOUT |
1047 /* controller CPU power on */
1048 error = i2c_smbus_write_byte_data(client, SYSTEM,
1049 ANALOG_POWER_ON | CPU_POWER_ON);
1051 enable_irq(client->irq);
1056 static int rohm_ts_power_off(struct i2c_client *client)
1060 error = i2c_smbus_write_byte_data(client, SYSTEM,
1061 ANALOG_POWER_ON | CPU_POWER_OFF);
1063 dev_err(&client->dev,
1064 "failed to power off device CPU: %d\n", error);
1068 error = i2c_smbus_write_byte_data(client, SYSTEM,
1069 ANALOG_POWER_OFF | CPU_POWER_OFF);
1071 dev_err(&client->dev,
1072 "failed to power off the device: %d\n", error);
1077 static int rohm_ts_open(struct input_dev *input_dev)
1079 struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1080 struct i2c_client *client = ts->client;
1083 if (!ts->initialized) {
1084 error = rohm_ts_device_init(client, ts->setup2);
1086 dev_err(&client->dev,
1087 "device initialization failed: %d\n", error);
1091 ts->initialized = true;
1097 static void rohm_ts_close(struct input_dev *input_dev)
1099 struct rohm_ts_data *ts = input_get_drvdata(input_dev);
1101 rohm_ts_power_off(ts->client);
1103 ts->initialized = false;
1106 static void rohm_ts_remove_sysfs_group(void *_dev)
1108 struct device *dev = _dev;
1110 sysfs_remove_group(&dev->kobj, &rohm_ts_attr_group);
1113 static int rohm_bu21023_i2c_probe(struct i2c_client *client,
1114 const struct i2c_device_id *id)
1116 struct device *dev = &client->dev;
1117 struct rohm_ts_data *ts;
1118 struct input_dev *input;
1122 dev_err(dev, "IRQ is not assigned\n");
1126 if (!client->adapter->algo->master_xfer) {
1127 dev_err(dev, "I2C level transfers not supported\n");
1131 /* Turn off CPU just in case */
1132 error = rohm_ts_power_off(client);
1136 ts = devm_kzalloc(dev, sizeof(struct rohm_ts_data), GFP_KERNEL);
1140 ts->client = client;
1141 ts->setup2 = MAF_1SAMPLE;
1142 i2c_set_clientdata(client, ts);
1144 input = devm_input_allocate_device(dev);
1148 input->name = BU21023_NAME;
1149 input->id.bustype = BUS_I2C;
1150 input->open = rohm_ts_open;
1151 input->close = rohm_ts_close;
1154 input_set_drvdata(input, ts);
1156 input_set_abs_params(input, ABS_MT_POSITION_X,
1157 ROHM_TS_ABS_X_MIN, ROHM_TS_ABS_X_MAX, 0, 0);
1158 input_set_abs_params(input, ABS_MT_POSITION_Y,
1159 ROHM_TS_ABS_Y_MIN, ROHM_TS_ABS_Y_MAX, 0, 0);
1161 error = input_mt_init_slots(input, MAX_CONTACTS,
1162 INPUT_MT_DIRECT | INPUT_MT_TRACK |
1163 INPUT_MT_DROP_UNUSED);
1165 dev_err(dev, "failed to multi touch slots initialization\n");
1169 error = devm_request_threaded_irq(dev, client->irq,
1170 NULL, rohm_ts_soft_irq,
1171 IRQF_ONESHOT, client->name, ts);
1173 dev_err(dev, "failed to request IRQ: %d\n", error);
1177 error = input_register_device(input);
1179 dev_err(dev, "failed to register input device: %d\n", error);
1183 error = sysfs_create_group(&dev->kobj, &rohm_ts_attr_group);
1185 dev_err(dev, "failed to create sysfs group: %d\n", error);
1189 error = devm_add_action(dev, rohm_ts_remove_sysfs_group, dev);
1191 rohm_ts_remove_sysfs_group(dev);
1192 dev_err(&client->dev,
1193 "Failed to add sysfs cleanup action: %d\n",
1201 static const struct i2c_device_id rohm_bu21023_i2c_id[] = {
1202 { BU21023_NAME, 0 },
1205 MODULE_DEVICE_TABLE(i2c, rohm_bu21023_i2c_id);
1207 static struct i2c_driver rohm_bu21023_i2c_driver = {
1209 .name = BU21023_NAME,
1211 .probe = rohm_bu21023_i2c_probe,
1212 .id_table = rohm_bu21023_i2c_id,
1214 module_i2c_driver(rohm_bu21023_i2c_driver);
1216 MODULE_DESCRIPTION("ROHM BU21023/24 Touchscreen driver");
1217 MODULE_LICENSE("GPL v2");
1218 MODULE_AUTHOR("ROHM Co., Ltd.");