rohm_bu21023.c 27 KB

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