rohm_bu21023.c 27 KB

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