synaptics_i2c_rmi4.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /**
  2. *
  3. * Synaptics Register Mapped Interface (RMI4) I2C Physical Layer Driver.
  4. * Copyright (c) 2007-2010, Synaptics Incorporated
  5. *
  6. * Author: Js HA <js.ha@stericsson.com> for ST-Ericsson
  7. * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
  8. * Copyright 2010 (c) ST-Ericsson AB
  9. */
  10. /*
  11. * This file is licensed under the GPL2 license.
  12. *
  13. *#############################################################################
  14. * GPL
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License version 2 as published
  18. * by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful, but
  21. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  22. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  23. * for more details.
  24. *
  25. *#############################################################################
  26. */
  27. #include <linux/input.h>
  28. #include <linux/slab.h>
  29. #include <linux/i2c.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/module.h>
  33. #include <linux/input/mt.h>
  34. #include "synaptics_i2c_rmi4.h"
  35. /* TODO: for multiple device support will need a per-device mutex */
  36. #define DRIVER_NAME "synaptics_rmi4_i2c"
  37. #define MAX_ERROR_REPORT 6
  38. #define MAX_TOUCH_MAJOR 15
  39. #define MAX_RETRY_COUNT 5
  40. #define STD_QUERY_LEN 21
  41. #define PAGE_LEN 2
  42. #define DATA_BUF_LEN 32
  43. #define BUF_LEN 37
  44. #define QUERY_LEN 9
  45. #define DATA_LEN 12
  46. #define HAS_TAP 0x01
  47. #define HAS_PALMDETECT 0x01
  48. #define HAS_ROTATE 0x02
  49. #define HAS_TAPANDHOLD 0x02
  50. #define HAS_DOUBLETAP 0x04
  51. #define HAS_EARLYTAP 0x08
  52. #define HAS_RELEASE 0x08
  53. #define HAS_FLICK 0x10
  54. #define HAS_PRESS 0x20
  55. #define HAS_PINCH 0x40
  56. #define MASK_16BIT 0xFFFF
  57. #define MASK_8BIT 0xFF
  58. #define MASK_7BIT 0x7F
  59. #define MASK_5BIT 0x1F
  60. #define MASK_4BIT 0x0F
  61. #define MASK_3BIT 0x07
  62. #define MASK_2BIT 0x03
  63. #define TOUCHPAD_CTRL_INTR 0x8
  64. #define PDT_START_SCAN_LOCATION (0x00E9)
  65. #define PDT_END_SCAN_LOCATION (0x000A)
  66. #define PDT_ENTRY_SIZE (0x0006)
  67. #define SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM (0x11)
  68. #define SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM (0x01)
  69. /**
  70. * struct synaptics_rmi4_fn_desc - contains the function descriptor information
  71. * @query_base_addr: base address for query
  72. * @cmd_base_addr: base address for command
  73. * @ctrl_base_addr: base address for control
  74. * @data_base_addr: base address for data
  75. * @intr_src_count: count for the interrupt source
  76. * @fn_number: function number
  77. *
  78. * This structure is used to gives the function descriptor information
  79. * of the particular functionality.
  80. */
  81. struct synaptics_rmi4_fn_desc {
  82. unsigned char query_base_addr;
  83. unsigned char cmd_base_addr;
  84. unsigned char ctrl_base_addr;
  85. unsigned char data_base_addr;
  86. unsigned char intr_src_count;
  87. unsigned char fn_number;
  88. };
  89. /**
  90. * struct synaptics_rmi4_fn - contains the function information
  91. * @fn_number: function number
  92. * @num_of_data_sources: number of data sources
  93. * @num_of_data_points: number of fingers touched
  94. * @size_of_data_register_block: data register block size
  95. * @index_to_intr_reg: index for interrupt register
  96. * @intr_mask: interrupt mask value
  97. * @fn_desc: variable for function descriptor structure
  98. * @link: linked list for function descriptors
  99. *
  100. * This structure gives information about the number of data sources and
  101. * the number of data registers associated with the function.
  102. */
  103. struct synaptics_rmi4_fn {
  104. unsigned char fn_number;
  105. unsigned char num_of_data_sources;
  106. unsigned char num_of_data_points;
  107. unsigned char size_of_data_register_block;
  108. unsigned char index_to_intr_reg;
  109. unsigned char intr_mask;
  110. struct synaptics_rmi4_fn_desc fn_desc;
  111. struct list_head link;
  112. };
  113. /**
  114. * struct synaptics_rmi4_device_info - contains the rmi4 device information
  115. * @version_major: protocol major version number
  116. * @version_minor: protocol minor version number
  117. * @manufacturer_id: manufacturer identification byte
  118. * @product_props: product properties information
  119. * @product_info: product info array
  120. * @date_code: device manufacture date
  121. * @tester_id: tester id array
  122. * @serial_number: serial number for that device
  123. * @product_id_string: product id for the device
  124. * @support_fn_list: linked list for device information
  125. *
  126. * This structure gives information about the number of data sources and
  127. * the number of data registers associated with the function.
  128. */
  129. struct synaptics_rmi4_device_info {
  130. unsigned int version_major;
  131. unsigned int version_minor;
  132. unsigned char manufacturer_id;
  133. unsigned char product_props;
  134. unsigned char product_info[2];
  135. unsigned char date_code[3];
  136. unsigned short tester_id;
  137. unsigned short serial_number;
  138. unsigned char product_id_string[11];
  139. struct list_head support_fn_list;
  140. };
  141. /**
  142. * struct synaptics_rmi4_data - contains the rmi4 device data
  143. * @rmi4_mod_info: structure variable for rmi4 device info
  144. * @input_dev: pointer for input device
  145. * @i2c_client: pointer for i2c client
  146. * @board: constant pointer for touch platform data
  147. * @fn_list_mutex: mutex for function list
  148. * @rmi4_page_mutex: mutex for rmi4 page
  149. * @current_page: variable for integer
  150. * @number_of_interrupt_register: interrupt registers count
  151. * @fn01_ctrl_base_addr: control base address for fn01
  152. * @fn01_query_base_addr: query base address for fn01
  153. * @fn01_data_base_addr: data base address for fn01
  154. * @sensor_max_x: sensor maximum x value
  155. * @sensor_max_y: sensor maximum y value
  156. * @regulator: pointer to the regulator structure
  157. * @wait: wait queue structure variable
  158. * @touch_stopped: flag to stop the thread function
  159. * @fingers_supported: maximum supported fingers
  160. *
  161. * This structure gives the device data information.
  162. */
  163. struct synaptics_rmi4_data {
  164. struct synaptics_rmi4_device_info rmi4_mod_info;
  165. struct input_dev *input_dev;
  166. struct i2c_client *i2c_client;
  167. const struct synaptics_rmi4_platform_data *board;
  168. struct mutex fn_list_mutex;
  169. struct mutex rmi4_page_mutex;
  170. int current_page;
  171. unsigned int number_of_interrupt_register;
  172. unsigned short fn01_ctrl_base_addr;
  173. unsigned short fn01_query_base_addr;
  174. unsigned short fn01_data_base_addr;
  175. int sensor_max_x;
  176. int sensor_max_y;
  177. struct regulator *regulator;
  178. wait_queue_head_t wait;
  179. bool touch_stopped;
  180. unsigned char fingers_supported;
  181. };
  182. /**
  183. * synaptics_rmi4_set_page() - sets the page
  184. * @pdata: pointer to synaptics_rmi4_data structure
  185. * @address: set the address of the page
  186. *
  187. * This function is used to set the page and returns integer.
  188. */
  189. static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *pdata,
  190. unsigned int address)
  191. {
  192. unsigned char txbuf[PAGE_LEN];
  193. int retval;
  194. unsigned int page;
  195. struct i2c_client *i2c = pdata->i2c_client;
  196. page = ((address >> 8) & MASK_8BIT);
  197. if (page != pdata->current_page) {
  198. txbuf[0] = MASK_8BIT;
  199. txbuf[1] = page;
  200. retval = i2c_master_send(i2c, txbuf, PAGE_LEN);
  201. if (retval != PAGE_LEN)
  202. dev_err(&i2c->dev, "failed:%d\n", retval);
  203. else
  204. pdata->current_page = page;
  205. } else
  206. retval = PAGE_LEN;
  207. return retval;
  208. }
  209. /**
  210. * synaptics_rmi4_i2c_block_read() - read the block of data
  211. * @pdata: pointer to synaptics_rmi4_data structure
  212. * @address: read the block of data from this offset
  213. * @valp: pointer to a buffer containing the data to be read
  214. * @size: number of bytes to read
  215. *
  216. * This function is to read the block of data and returns integer.
  217. */
  218. static int synaptics_rmi4_i2c_block_read(struct synaptics_rmi4_data *pdata,
  219. unsigned short address,
  220. unsigned char *valp, int size)
  221. {
  222. int retval = 0;
  223. int retry_count = 0;
  224. int index;
  225. struct i2c_client *i2c = pdata->i2c_client;
  226. mutex_lock(&(pdata->rmi4_page_mutex));
  227. retval = synaptics_rmi4_set_page(pdata, address);
  228. if (retval != PAGE_LEN)
  229. goto exit;
  230. index = address & MASK_8BIT;
  231. retry:
  232. retval = i2c_smbus_read_i2c_block_data(i2c, index, size, valp);
  233. if (retval != size) {
  234. if (++retry_count == MAX_RETRY_COUNT)
  235. dev_err(&i2c->dev,
  236. "%s:address 0x%04x size %d failed:%d\n",
  237. __func__, address, size, retval);
  238. else {
  239. synaptics_rmi4_set_page(pdata, address);
  240. goto retry;
  241. }
  242. }
  243. exit:
  244. mutex_unlock(&(pdata->rmi4_page_mutex));
  245. return retval;
  246. }
  247. /**
  248. * synaptics_rmi4_i2c_byte_write() - write the single byte data
  249. * @pdata: pointer to synaptics_rmi4_data structure
  250. * @address: write the block of data from this offset
  251. * @data: data to be write
  252. *
  253. * This function is to write the single byte data and returns integer.
  254. */
  255. static int synaptics_rmi4_i2c_byte_write(struct synaptics_rmi4_data *pdata,
  256. unsigned short address,
  257. unsigned char data)
  258. {
  259. unsigned char txbuf[2];
  260. int retval = 0;
  261. struct i2c_client *i2c = pdata->i2c_client;
  262. /* Can't have anyone else changing the page behind our backs */
  263. mutex_lock(&(pdata->rmi4_page_mutex));
  264. retval = synaptics_rmi4_set_page(pdata, address);
  265. if (retval != PAGE_LEN)
  266. goto exit;
  267. txbuf[0] = address & MASK_8BIT;
  268. txbuf[1] = data;
  269. retval = i2c_master_send(pdata->i2c_client, txbuf, 2);
  270. /* Add in retry on writes only in certain error return values */
  271. if (retval != 2) {
  272. dev_err(&i2c->dev, "failed:%d\n", retval);
  273. retval = -EIO;
  274. } else
  275. retval = 1;
  276. exit:
  277. mutex_unlock(&(pdata->rmi4_page_mutex));
  278. return retval;
  279. }
  280. /**
  281. * synpatics_rmi4_touchpad_report() - reports for the rmi4 touchpad device
  282. * @pdata: pointer to synaptics_rmi4_data structure
  283. * @rfi: pointer to synaptics_rmi4_fn structure
  284. *
  285. * This function calls to reports for the rmi4 touchpad device
  286. */
  287. static int synpatics_rmi4_touchpad_report(struct synaptics_rmi4_data *pdata,
  288. struct synaptics_rmi4_fn *rfi)
  289. {
  290. /* number of touch points - fingers down in this case */
  291. int touch_count = 0;
  292. int finger;
  293. int finger_registers;
  294. int reg;
  295. int finger_shift;
  296. int finger_status;
  297. int retval;
  298. int x, y;
  299. int wx, wy;
  300. unsigned short data_base_addr;
  301. unsigned short data_offset;
  302. unsigned char data_reg_blk_size;
  303. unsigned char values[2];
  304. unsigned char data[DATA_LEN];
  305. unsigned char fingers_supported = pdata->fingers_supported;
  306. struct i2c_client *client = pdata->i2c_client;
  307. struct input_dev *input_dev = pdata->input_dev;
  308. /* get 2D sensor finger data */
  309. /*
  310. * First get the finger status field - the size of the finger status
  311. * field is determined by the number of finger supporte - 2 bits per
  312. * finger, so the number of registers to read is:
  313. * registerCount = ceil(numberOfFingers/4).
  314. * Read the required number of registers and check each 2 bit field to
  315. * determine if a finger is down:
  316. * 00 = finger not present,
  317. * 01 = finger present and data accurate,
  318. * 10 = finger present but data may not be accurate,
  319. * 11 = reserved for product use.
  320. */
  321. finger_registers = (fingers_supported + 3)/4;
  322. data_base_addr = rfi->fn_desc.data_base_addr;
  323. retval = synaptics_rmi4_i2c_block_read(pdata, data_base_addr, values,
  324. finger_registers);
  325. if (retval != finger_registers) {
  326. dev_err(&client->dev, "%s:read status registers failed\n",
  327. __func__);
  328. return 0;
  329. }
  330. /*
  331. * For each finger present, read the proper number of registers
  332. * to get absolute data.
  333. */
  334. data_reg_blk_size = rfi->size_of_data_register_block;
  335. for (finger = 0; finger < fingers_supported; finger++) {
  336. /* determine which data byte the finger status is in */
  337. reg = finger/4;
  338. /* bit shift to get finger's status */
  339. finger_shift = (finger % 4) * 2;
  340. finger_status = (values[reg] >> finger_shift) & 3;
  341. /*
  342. * if finger status indicates a finger is present then
  343. * read the finger data and report it
  344. */
  345. input_mt_slot(input_dev, finger);
  346. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER,
  347. finger_status != 0);
  348. if (finger_status) {
  349. /* Read the finger data */
  350. data_offset = data_base_addr +
  351. ((finger * data_reg_blk_size) +
  352. finger_registers);
  353. retval = synaptics_rmi4_i2c_block_read(pdata,
  354. data_offset, data,
  355. data_reg_blk_size);
  356. if (retval != data_reg_blk_size) {
  357. dev_err(&client->dev, "%s:read data failed\n",
  358. __func__);
  359. return 0;
  360. }
  361. x = (data[0] << 4) | (data[2] & MASK_4BIT);
  362. y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
  363. wy = (data[3] >> 4) & MASK_4BIT;
  364. wx = (data[3] & MASK_4BIT);
  365. if (pdata->board->x_flip)
  366. x = pdata->sensor_max_x - x;
  367. if (pdata->board->y_flip)
  368. y = pdata->sensor_max_y - y;
  369. input_report_abs(input_dev, ABS_MT_TOUCH_MAJOR,
  370. max(wx, wy));
  371. input_report_abs(input_dev, ABS_MT_POSITION_X, x);
  372. input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
  373. /* number of active touch points */
  374. touch_count++;
  375. }
  376. }
  377. /* sync after groups of events */
  378. input_mt_sync_frame(input_dev);
  379. input_sync(input_dev);
  380. /* return the number of touch points */
  381. return touch_count;
  382. }
  383. /**
  384. * synaptics_rmi4_report_device() - reports the rmi4 device
  385. * @pdata: pointer to synaptics_rmi4_data structure
  386. * @rfi: pointer to synaptics_rmi4_fn
  387. *
  388. * This function is used to call the report function of the rmi4 device.
  389. */
  390. static int synaptics_rmi4_report_device(struct synaptics_rmi4_data *pdata,
  391. struct synaptics_rmi4_fn *rfi)
  392. {
  393. int touch = 0;
  394. struct i2c_client *client = pdata->i2c_client;
  395. static int num_error_reports;
  396. if (rfi->fn_number != SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
  397. num_error_reports++;
  398. if (num_error_reports < MAX_ERROR_REPORT)
  399. dev_err(&client->dev, "%s:report not supported\n",
  400. __func__);
  401. } else
  402. touch = synpatics_rmi4_touchpad_report(pdata, rfi);
  403. return touch;
  404. }
  405. /**
  406. * synaptics_rmi4_sensor_report() - reports to input subsystem
  407. * @pdata: pointer to synaptics_rmi4_data structure
  408. *
  409. * This function is used to reads in all data sources and reports
  410. * them to the input subsystem.
  411. */
  412. static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *pdata)
  413. {
  414. unsigned char intr_status[4];
  415. /* number of touch points - fingers or buttons */
  416. int touch = 0;
  417. unsigned int retval;
  418. struct synaptics_rmi4_fn *rfi;
  419. struct synaptics_rmi4_device_info *rmi;
  420. struct i2c_client *client = pdata->i2c_client;
  421. /*
  422. * Get the interrupt status from the function $01
  423. * control register+1 to find which source(s) were interrupting
  424. * so we can read the data from the source(s) (2D sensor, buttons..)
  425. */
  426. retval = synaptics_rmi4_i2c_block_read(pdata,
  427. pdata->fn01_data_base_addr + 1,
  428. intr_status,
  429. pdata->number_of_interrupt_register);
  430. if (retval != pdata->number_of_interrupt_register) {
  431. dev_err(&client->dev,
  432. "could not read interrupt status registers\n");
  433. return 0;
  434. }
  435. /*
  436. * check each function that has data sources and if the interrupt for
  437. * that triggered then call that RMI4 functions report() function to
  438. * gather data and report it to the input subsystem
  439. */
  440. rmi = &(pdata->rmi4_mod_info);
  441. list_for_each_entry(rfi, &rmi->support_fn_list, link) {
  442. if (rfi->num_of_data_sources) {
  443. if (intr_status[rfi->index_to_intr_reg] &
  444. rfi->intr_mask)
  445. touch = synaptics_rmi4_report_device(pdata,
  446. rfi);
  447. }
  448. }
  449. /* return the number of touch points */
  450. return touch;
  451. }
  452. /**
  453. * synaptics_rmi4_irq() - thread function for rmi4 attention line
  454. * @irq: irq value
  455. * @data: void pointer
  456. *
  457. * This function is interrupt thread function. It just notifies the
  458. * application layer that attention is required.
  459. */
  460. static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
  461. {
  462. struct synaptics_rmi4_data *pdata = data;
  463. int touch_count;
  464. do {
  465. touch_count = synaptics_rmi4_sensor_report(pdata);
  466. if (touch_count)
  467. wait_event_timeout(pdata->wait, pdata->touch_stopped,
  468. msecs_to_jiffies(1));
  469. else
  470. break;
  471. } while (!pdata->touch_stopped);
  472. return IRQ_HANDLED;
  473. }
  474. /**
  475. * synpatics_rmi4_touchpad_detect() - detects the rmi4 touchpad device
  476. * @pdata: pointer to synaptics_rmi4_data structure
  477. * @rfi: pointer to synaptics_rmi4_fn structure
  478. * @fd: pointer to synaptics_rmi4_fn_desc structure
  479. * @interruptcount: count the number of interrupts
  480. *
  481. * This function calls to detects the rmi4 touchpad device
  482. */
  483. static int synpatics_rmi4_touchpad_detect(struct synaptics_rmi4_data *pdata,
  484. struct synaptics_rmi4_fn *rfi,
  485. struct synaptics_rmi4_fn_desc *fd,
  486. unsigned int interruptcount)
  487. {
  488. unsigned char queries[QUERY_LEN];
  489. unsigned short intr_offset;
  490. unsigned char abs_data_size;
  491. unsigned char abs_data_blk_size;
  492. unsigned char egr_0, egr_1;
  493. unsigned int all_data_blk_size;
  494. int has_pinch, has_flick, has_tap;
  495. int has_tapandhold, has_doubletap;
  496. int has_earlytap, has_press;
  497. int has_palmdetect, has_rotate;
  498. int has_rel;
  499. int i;
  500. int retval;
  501. struct i2c_client *client = pdata->i2c_client;
  502. rfi->fn_desc.query_base_addr = fd->query_base_addr;
  503. rfi->fn_desc.data_base_addr = fd->data_base_addr;
  504. rfi->fn_desc.intr_src_count = fd->intr_src_count;
  505. rfi->fn_desc.fn_number = fd->fn_number;
  506. rfi->fn_number = fd->fn_number;
  507. rfi->num_of_data_sources = fd->intr_src_count;
  508. rfi->fn_desc.ctrl_base_addr = fd->ctrl_base_addr;
  509. rfi->fn_desc.cmd_base_addr = fd->cmd_base_addr;
  510. /*
  511. * need to get number of fingers supported, data size, etc.
  512. * to be used when getting data since the number of registers to
  513. * read depends on the number of fingers supported and data size.
  514. */
  515. retval = synaptics_rmi4_i2c_block_read(pdata, fd->query_base_addr,
  516. queries,
  517. sizeof(queries));
  518. if (retval != sizeof(queries)) {
  519. dev_err(&client->dev, "%s:read function query registers\n",
  520. __func__);
  521. return retval;
  522. }
  523. /*
  524. * 2D data sources have only 3 bits for the number of fingers
  525. * supported - so the encoding is a bit weird.
  526. */
  527. if ((queries[1] & MASK_3BIT) <= 4)
  528. /* add 1 since zero based */
  529. rfi->num_of_data_points = (queries[1] & MASK_3BIT) + 1;
  530. else {
  531. /*
  532. * a value of 5 is up to 10 fingers - 6 and 7 are reserved
  533. * (shouldn't get these i int retval;n a normal 2D source).
  534. */
  535. if ((queries[1] & MASK_3BIT) == 5)
  536. rfi->num_of_data_points = 10;
  537. }
  538. pdata->fingers_supported = rfi->num_of_data_points;
  539. /* Need to get interrupt info for handling interrupts */
  540. rfi->index_to_intr_reg = (interruptcount + 7)/8;
  541. if (rfi->index_to_intr_reg != 0)
  542. rfi->index_to_intr_reg -= 1;
  543. /*
  544. * loop through interrupts for each source in fn $11
  545. * and or in a bit to the interrupt mask for each.
  546. */
  547. intr_offset = interruptcount % 8;
  548. rfi->intr_mask = 0;
  549. for (i = intr_offset;
  550. i < ((fd->intr_src_count & MASK_3BIT) + intr_offset); i++)
  551. rfi->intr_mask |= 1 << i;
  552. /* Size of just the absolute data for one finger */
  553. abs_data_size = queries[5] & MASK_2BIT;
  554. /* One each for X and Y, one for LSB for X & Y, one for W, one for Z */
  555. abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
  556. rfi->size_of_data_register_block = abs_data_blk_size;
  557. /*
  558. * need to determine the size of data to read - this depends on
  559. * conditions such as whether Relative data is reported and if Gesture
  560. * data is reported.
  561. */
  562. egr_0 = queries[7];
  563. egr_1 = queries[8];
  564. /*
  565. * Get info about what EGR data is supported, whether it has
  566. * Relative data supported, etc.
  567. */
  568. has_pinch = egr_0 & HAS_PINCH;
  569. has_flick = egr_0 & HAS_FLICK;
  570. has_tap = egr_0 & HAS_TAP;
  571. has_earlytap = egr_0 & HAS_EARLYTAP;
  572. has_press = egr_0 & HAS_PRESS;
  573. has_rotate = egr_1 & HAS_ROTATE;
  574. has_rel = queries[1] & HAS_RELEASE;
  575. has_tapandhold = egr_0 & HAS_TAPANDHOLD;
  576. has_doubletap = egr_0 & HAS_DOUBLETAP;
  577. has_palmdetect = egr_1 & HAS_PALMDETECT;
  578. /*
  579. * Size of all data including finger status, absolute data for each
  580. * finger, relative data and EGR data
  581. */
  582. all_data_blk_size =
  583. /* finger status, four fingers per register */
  584. ((rfi->num_of_data_points + 3) / 4) +
  585. /* absolute data, per finger times number of fingers */
  586. (abs_data_blk_size * rfi->num_of_data_points) +
  587. /*
  588. * two relative registers (if relative is being reported)
  589. */
  590. 2 * has_rel +
  591. /*
  592. * F11_2D_data8 is only present if the egr_0
  593. * register is non-zero.
  594. */
  595. !!(egr_0) +
  596. /*
  597. * F11_2D_data9 is only present if either egr_0 or
  598. * egr_1 registers are non-zero.
  599. */
  600. (egr_0 || egr_1) +
  601. /*
  602. * F11_2D_data10 is only present if EGR_PINCH or EGR_FLICK of
  603. * egr_0 reports as 1.
  604. */
  605. !!(has_pinch | has_flick) +
  606. /*
  607. * F11_2D_data11 and F11_2D_data12 are only present if
  608. * EGR_FLICK of egr_0 reports as 1.
  609. */
  610. 2 * !!(has_flick);
  611. return retval;
  612. }
  613. /**
  614. * synaptics_rmi4_touchpad_config() - configures the rmi4 touchpad device
  615. * @pdata: pointer to synaptics_rmi4_data structure
  616. * @rfi: pointer to synaptics_rmi4_fn structure
  617. *
  618. * This function calls to configures the rmi4 touchpad device
  619. */
  620. static int synaptics_rmi4_touchpad_config(struct synaptics_rmi4_data *pdata,
  621. struct synaptics_rmi4_fn *rfi)
  622. {
  623. /*
  624. * For the data source - print info and do any
  625. * source specific configuration.
  626. */
  627. unsigned char data[BUF_LEN];
  628. int retval = 0;
  629. struct i2c_client *client = pdata->i2c_client;
  630. /* Get and print some info about the data source... */
  631. /* To Query 2D devices we need to read from the address obtained
  632. * from the function descriptor stored in the RMI function info.
  633. */
  634. retval = synaptics_rmi4_i2c_block_read(pdata,
  635. rfi->fn_desc.query_base_addr,
  636. data, QUERY_LEN);
  637. if (retval != QUERY_LEN)
  638. dev_err(&client->dev, "%s:read query registers failed\n",
  639. __func__);
  640. else {
  641. retval = synaptics_rmi4_i2c_block_read(pdata,
  642. rfi->fn_desc.ctrl_base_addr,
  643. data, DATA_BUF_LEN);
  644. if (retval != DATA_BUF_LEN) {
  645. dev_err(&client->dev,
  646. "%s:read control registers failed\n",
  647. __func__);
  648. return retval;
  649. }
  650. /* Store these for use later*/
  651. pdata->sensor_max_x = ((data[6] & MASK_8BIT) << 0) |
  652. ((data[7] & MASK_4BIT) << 8);
  653. pdata->sensor_max_y = ((data[8] & MASK_5BIT) << 0) |
  654. ((data[9] & MASK_4BIT) << 8);
  655. }
  656. return retval;
  657. }
  658. /**
  659. * synaptics_rmi4_i2c_query_device() - query the rmi4 device
  660. * @pdata: pointer to synaptics_rmi4_data structure
  661. *
  662. * This function is used to query the rmi4 device.
  663. */
  664. static int synaptics_rmi4_i2c_query_device(struct synaptics_rmi4_data *pdata)
  665. {
  666. int i;
  667. int retval;
  668. unsigned char std_queries[STD_QUERY_LEN];
  669. unsigned char intr_count = 0;
  670. int data_sources = 0;
  671. unsigned int ctrl_offset;
  672. struct synaptics_rmi4_fn *rfi;
  673. struct synaptics_rmi4_fn_desc rmi_fd;
  674. struct synaptics_rmi4_device_info *rmi;
  675. struct i2c_client *client = pdata->i2c_client;
  676. /*
  677. * init the physical drivers RMI module
  678. * info list of functions
  679. */
  680. INIT_LIST_HEAD(&pdata->rmi4_mod_info.support_fn_list);
  681. /*
  682. * Read the Page Descriptor Table to determine what functions
  683. * are present
  684. */
  685. for (i = PDT_START_SCAN_LOCATION; i > PDT_END_SCAN_LOCATION;
  686. i -= PDT_ENTRY_SIZE) {
  687. retval = synaptics_rmi4_i2c_block_read(pdata, i,
  688. (unsigned char *)&rmi_fd,
  689. sizeof(rmi_fd));
  690. if (retval != sizeof(rmi_fd)) {
  691. /* failed to read next PDT entry */
  692. dev_err(&client->dev, "%s: read error\n", __func__);
  693. return -EIO;
  694. }
  695. rfi = NULL;
  696. if (rmi_fd.fn_number) {
  697. switch (rmi_fd.fn_number & MASK_8BIT) {
  698. case SYNAPTICS_RMI4_DEVICE_CONTROL_FUNC_NUM:
  699. pdata->fn01_query_base_addr =
  700. rmi_fd.query_base_addr;
  701. pdata->fn01_ctrl_base_addr =
  702. rmi_fd.ctrl_base_addr;
  703. pdata->fn01_data_base_addr =
  704. rmi_fd.data_base_addr;
  705. break;
  706. case SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM:
  707. if (rmi_fd.intr_src_count) {
  708. rfi = kmalloc(sizeof(*rfi),
  709. GFP_KERNEL);
  710. if (!rfi)
  711. return -ENOMEM;
  712. retval = synpatics_rmi4_touchpad_detect
  713. (pdata, rfi,
  714. &rmi_fd,
  715. intr_count);
  716. if (retval < 0) {
  717. kfree(rfi);
  718. return retval;
  719. }
  720. }
  721. break;
  722. }
  723. /* interrupt count for next iteration */
  724. intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
  725. /*
  726. * We only want to add functions to the list
  727. * that have data associated with them.
  728. */
  729. if (rfi && rmi_fd.intr_src_count) {
  730. /* link this function info to the RMI module */
  731. mutex_lock(&(pdata->fn_list_mutex));
  732. list_add_tail(&rfi->link,
  733. &pdata->rmi4_mod_info.support_fn_list);
  734. mutex_unlock(&(pdata->fn_list_mutex));
  735. }
  736. } else {
  737. /*
  738. * A zero in the function number
  739. * signals the end of the PDT
  740. */
  741. dev_dbg(&client->dev,
  742. "%s:end of PDT\n", __func__);
  743. break;
  744. }
  745. }
  746. /*
  747. * calculate the interrupt register count - used in the
  748. * ISR to read the correct number of interrupt registers
  749. */
  750. pdata->number_of_interrupt_register = (intr_count + 7) / 8;
  751. /*
  752. * Function $01 will be used to query the product properties,
  753. * and product ID so we had to read the PDT above first to get
  754. * the Fn $01 query address and prior to filling in the product
  755. * info. NOTE: Even an unflashed device will still have FN $01.
  756. */
  757. /* Load up the standard queries and get the RMI4 module info */
  758. retval = synaptics_rmi4_i2c_block_read(pdata,
  759. pdata->fn01_query_base_addr,
  760. std_queries,
  761. sizeof(std_queries));
  762. if (retval != sizeof(std_queries)) {
  763. dev_err(&client->dev, "%s:Failed reading queries\n",
  764. __func__);
  765. return -EIO;
  766. }
  767. /* Currently supported RMI version is 4.0 */
  768. pdata->rmi4_mod_info.version_major = 4;
  769. pdata->rmi4_mod_info.version_minor = 0;
  770. /*
  771. * get manufacturer id, product_props, product info,
  772. * date code, tester id, serial num and product id (name)
  773. */
  774. pdata->rmi4_mod_info.manufacturer_id = std_queries[0];
  775. pdata->rmi4_mod_info.product_props = std_queries[1];
  776. pdata->rmi4_mod_info.product_info[0] = std_queries[2];
  777. pdata->rmi4_mod_info.product_info[1] = std_queries[3];
  778. /* year - 2001-2032 */
  779. pdata->rmi4_mod_info.date_code[0] = std_queries[4] & MASK_5BIT;
  780. /* month - 1-12 */
  781. pdata->rmi4_mod_info.date_code[1] = std_queries[5] & MASK_4BIT;
  782. /* day - 1-31 */
  783. pdata->rmi4_mod_info.date_code[2] = std_queries[6] & MASK_5BIT;
  784. pdata->rmi4_mod_info.tester_id = ((std_queries[7] & MASK_7BIT) << 8) |
  785. (std_queries[8] & MASK_7BIT);
  786. pdata->rmi4_mod_info.serial_number =
  787. ((std_queries[9] & MASK_7BIT) << 8) |
  788. (std_queries[10] & MASK_7BIT);
  789. memcpy(pdata->rmi4_mod_info.product_id_string, &std_queries[11], 10);
  790. /* Check if this is a Synaptics device - report if not. */
  791. if (pdata->rmi4_mod_info.manufacturer_id != 1)
  792. dev_err(&client->dev, "non-Synaptics mfg id:%d\n",
  793. pdata->rmi4_mod_info.manufacturer_id);
  794. list_for_each_entry(rfi, &pdata->rmi4_mod_info.support_fn_list, link)
  795. data_sources += rfi->num_of_data_sources;
  796. if (data_sources) {
  797. rmi = &(pdata->rmi4_mod_info);
  798. list_for_each_entry(rfi, &rmi->support_fn_list, link) {
  799. if (rfi->num_of_data_sources) {
  800. if (rfi->fn_number ==
  801. SYNAPTICS_RMI4_TOUCHPAD_FUNC_NUM) {
  802. retval = synaptics_rmi4_touchpad_config
  803. (pdata, rfi);
  804. if (retval < 0)
  805. return retval;
  806. } else
  807. dev_err(&client->dev,
  808. "%s:fn_number not supported\n",
  809. __func__);
  810. /*
  811. * Turn on interrupts for this
  812. * function's data sources.
  813. */
  814. ctrl_offset = pdata->fn01_ctrl_base_addr + 1 +
  815. rfi->index_to_intr_reg;
  816. retval = synaptics_rmi4_i2c_byte_write(pdata,
  817. ctrl_offset,
  818. rfi->intr_mask);
  819. if (retval < 0)
  820. return retval;
  821. }
  822. }
  823. }
  824. return 0;
  825. }
  826. /*
  827. * Descriptor structure.
  828. * Describes the number of i2c devices on the bus that speak RMI.
  829. */
  830. static struct synaptics_rmi4_platform_data synaptics_rmi4_platformdata = {
  831. .irq_type = (IRQF_TRIGGER_FALLING | IRQF_SHARED),
  832. .x_flip = false,
  833. .y_flip = true,
  834. };
  835. /**
  836. * synaptics_rmi4_probe() - Initialze the i2c-client touchscreen driver
  837. * @i2c: i2c client structure pointer
  838. * @id:i2c device id pointer
  839. *
  840. * This function will allocate and initialize the instance
  841. * data and request the irq and set the instance data as the clients
  842. * platform data then register the physical driver which will do a scan of
  843. * the rmi4 Physical Device Table and enumerate any rmi4 functions that
  844. * have data sources associated with them.
  845. */
  846. static int synaptics_rmi4_probe
  847. (struct i2c_client *client, const struct i2c_device_id *dev_id)
  848. {
  849. int retval;
  850. unsigned char intr_status[4];
  851. struct synaptics_rmi4_data *rmi4_data;
  852. const struct synaptics_rmi4_platform_data *platformdata =
  853. client->dev.platform_data;
  854. if (!i2c_check_functionality(client->adapter,
  855. I2C_FUNC_SMBUS_BYTE_DATA)) {
  856. dev_err(&client->dev, "i2c smbus byte data not supported\n");
  857. return -EIO;
  858. }
  859. if (!platformdata)
  860. platformdata = &synaptics_rmi4_platformdata;
  861. /* Allocate and initialize the instance data for this client */
  862. rmi4_data = kcalloc(2, sizeof(struct synaptics_rmi4_data),
  863. GFP_KERNEL);
  864. if (!rmi4_data)
  865. return -ENOMEM;
  866. rmi4_data->input_dev = input_allocate_device();
  867. if (rmi4_data->input_dev == NULL) {
  868. retval = -ENOMEM;
  869. goto err_input;
  870. }
  871. rmi4_data->regulator = regulator_get(&client->dev, "vdd");
  872. if (IS_ERR(rmi4_data->regulator)) {
  873. dev_err(&client->dev, "%s:get regulator failed\n",
  874. __func__);
  875. retval = PTR_ERR(rmi4_data->regulator);
  876. goto err_get_regulator;
  877. }
  878. retval = regulator_enable(rmi4_data->regulator);
  879. if (retval < 0) {
  880. dev_err(&client->dev, "%s:regulator enable failed\n",
  881. __func__);
  882. goto err_regulator_enable;
  883. }
  884. init_waitqueue_head(&rmi4_data->wait);
  885. /*
  886. * Copy i2c_client pointer into RTID's i2c_client pointer for
  887. * later use in rmi4_read, rmi4_write, etc.
  888. */
  889. rmi4_data->i2c_client = client;
  890. /* So we set the page correctly the first time */
  891. rmi4_data->current_page = MASK_16BIT;
  892. rmi4_data->board = platformdata;
  893. rmi4_data->touch_stopped = false;
  894. /* init the mutexes for maintain the lists */
  895. mutex_init(&(rmi4_data->fn_list_mutex));
  896. mutex_init(&(rmi4_data->rmi4_page_mutex));
  897. /*
  898. * Register physical driver - this will call the detect function that
  899. * will then scan the device and determine the supported
  900. * rmi4 functions.
  901. */
  902. retval = synaptics_rmi4_i2c_query_device(rmi4_data);
  903. if (retval) {
  904. dev_err(&client->dev, "%s: rmi4 query device failed\n",
  905. __func__);
  906. goto err_query_dev;
  907. }
  908. /* Store the instance data in the i2c_client */
  909. i2c_set_clientdata(client, rmi4_data);
  910. /*initialize the input device parameters */
  911. rmi4_data->input_dev->name = DRIVER_NAME;
  912. rmi4_data->input_dev->phys = "Synaptics_Clearpad";
  913. rmi4_data->input_dev->id.bustype = BUS_I2C;
  914. rmi4_data->input_dev->dev.parent = &client->dev;
  915. input_set_drvdata(rmi4_data->input_dev, rmi4_data);
  916. /* Initialize the function handlers for rmi4 */
  917. set_bit(EV_SYN, rmi4_data->input_dev->evbit);
  918. set_bit(EV_KEY, rmi4_data->input_dev->evbit);
  919. set_bit(EV_ABS, rmi4_data->input_dev->evbit);
  920. input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_X, 0,
  921. rmi4_data->sensor_max_x, 0, 0);
  922. input_set_abs_params(rmi4_data->input_dev, ABS_MT_POSITION_Y, 0,
  923. rmi4_data->sensor_max_y, 0, 0);
  924. input_set_abs_params(rmi4_data->input_dev, ABS_MT_TOUCH_MAJOR, 0,
  925. MAX_TOUCH_MAJOR, 0, 0);
  926. input_mt_init_slots(rmi4_data->input_dev,
  927. rmi4_data->fingers_supported, 0);
  928. /* Clear interrupts */
  929. synaptics_rmi4_i2c_block_read(rmi4_data,
  930. rmi4_data->fn01_data_base_addr + 1, intr_status,
  931. rmi4_data->number_of_interrupt_register);
  932. retval = request_threaded_irq(client->irq, NULL,
  933. synaptics_rmi4_irq,
  934. platformdata->irq_type,
  935. DRIVER_NAME, rmi4_data);
  936. if (retval) {
  937. dev_err(&client->dev, "Unable to get attn irq %d\n",
  938. client->irq);
  939. goto err_query_dev;
  940. }
  941. retval = input_register_device(rmi4_data->input_dev);
  942. if (retval) {
  943. dev_err(&client->dev, "%s:input register failed\n", __func__);
  944. goto err_free_irq;
  945. }
  946. return retval;
  947. err_free_irq:
  948. free_irq(client->irq, rmi4_data);
  949. err_query_dev:
  950. regulator_disable(rmi4_data->regulator);
  951. err_regulator_enable:
  952. regulator_put(rmi4_data->regulator);
  953. err_get_regulator:
  954. input_free_device(rmi4_data->input_dev);
  955. rmi4_data->input_dev = NULL;
  956. err_input:
  957. kfree(rmi4_data);
  958. return retval;
  959. }
  960. /**
  961. * synaptics_rmi4_remove() - Removes the i2c-client touchscreen driver
  962. * @client: i2c client structure pointer
  963. *
  964. * This function uses to remove the i2c-client
  965. * touchscreen driver and returns integer.
  966. */
  967. static int synaptics_rmi4_remove(struct i2c_client *client)
  968. {
  969. struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
  970. rmi4_data->touch_stopped = true;
  971. wake_up(&rmi4_data->wait);
  972. free_irq(client->irq, rmi4_data);
  973. input_unregister_device(rmi4_data->input_dev);
  974. regulator_disable(rmi4_data->regulator);
  975. regulator_put(rmi4_data->regulator);
  976. kfree(rmi4_data);
  977. return 0;
  978. }
  979. /**
  980. * synaptics_rmi4_suspend() - suspend the touch screen controller
  981. * @dev: pointer to device structure
  982. *
  983. * This function is used to suspend the
  984. * touch panel controller and returns integer
  985. */
  986. static int __maybe_unused synaptics_rmi4_suspend(struct device *dev)
  987. {
  988. /* Touch sleep mode */
  989. int retval;
  990. unsigned char intr_status;
  991. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  992. rmi4_data->touch_stopped = true;
  993. disable_irq(rmi4_data->i2c_client->irq);
  994. retval = synaptics_rmi4_i2c_block_read(rmi4_data,
  995. rmi4_data->fn01_data_base_addr + 1,
  996. &intr_status,
  997. rmi4_data->number_of_interrupt_register);
  998. if (retval < 0)
  999. return retval;
  1000. retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
  1001. rmi4_data->fn01_ctrl_base_addr + 1,
  1002. (intr_status & ~TOUCHPAD_CTRL_INTR));
  1003. if (retval < 0)
  1004. return retval;
  1005. regulator_disable(rmi4_data->regulator);
  1006. return 0;
  1007. }
  1008. /**
  1009. * synaptics_rmi4_resume() - resume the touch screen controller
  1010. * @dev: pointer to device structure
  1011. *
  1012. * This function is used to resume the touch panel
  1013. * controller and returns integer.
  1014. */
  1015. static int __maybe_unused synaptics_rmi4_resume(struct device *dev)
  1016. {
  1017. int retval;
  1018. unsigned char intr_status;
  1019. struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
  1020. retval = regulator_enable(rmi4_data->regulator);
  1021. if (retval) {
  1022. dev_err(dev, "Regulator enable failed (%d)\n", retval);
  1023. return retval;
  1024. }
  1025. enable_irq(rmi4_data->i2c_client->irq);
  1026. rmi4_data->touch_stopped = false;
  1027. retval = synaptics_rmi4_i2c_block_read(rmi4_data,
  1028. rmi4_data->fn01_data_base_addr + 1,
  1029. &intr_status,
  1030. rmi4_data->number_of_interrupt_register);
  1031. if (retval < 0)
  1032. return retval;
  1033. retval = synaptics_rmi4_i2c_byte_write(rmi4_data,
  1034. rmi4_data->fn01_ctrl_base_addr + 1,
  1035. (intr_status | TOUCHPAD_CTRL_INTR));
  1036. if (retval < 0)
  1037. return retval;
  1038. return 0;
  1039. }
  1040. static SIMPLE_DEV_PM_OPS(synaptics_rmi4_dev_pm_ops, synaptics_rmi4_suspend,
  1041. synaptics_rmi4_resume);
  1042. static const struct i2c_device_id synaptics_rmi4_id_table[] = {
  1043. { DRIVER_NAME, 0 },
  1044. { },
  1045. };
  1046. MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
  1047. static struct i2c_driver synaptics_rmi4_driver = {
  1048. .driver = {
  1049. .name = DRIVER_NAME,
  1050. .pm = &synaptics_rmi4_dev_pm_ops,
  1051. },
  1052. .probe = synaptics_rmi4_probe,
  1053. .remove = synaptics_rmi4_remove,
  1054. .id_table = synaptics_rmi4_id_table,
  1055. };
  1056. module_i2c_driver(synaptics_rmi4_driver);
  1057. MODULE_LICENSE("GPL v2");
  1058. MODULE_AUTHOR("naveen.gaddipati@stericsson.com, js.ha@stericsson.com");
  1059. MODULE_DESCRIPTION("synaptics rmi4 i2c touch Driver");