rmi_f01.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /*
  2. * Copyright (c) 2011-2016 Synaptics Incorporated
  3. * Copyright (c) 2011 Unixphere
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/rmi.h>
  11. #include <linux/slab.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/of.h>
  14. #include "rmi_driver.h"
  15. #define RMI_PRODUCT_ID_LENGTH 10
  16. #define RMI_PRODUCT_INFO_LENGTH 2
  17. #define RMI_DATE_CODE_LENGTH 3
  18. #define PRODUCT_ID_OFFSET 0x10
  19. #define PRODUCT_INFO_OFFSET 0x1E
  20. /* Force a firmware reset of the sensor */
  21. #define RMI_F01_CMD_DEVICE_RESET 1
  22. /* Various F01_RMI_QueryX bits */
  23. #define RMI_F01_QRY1_CUSTOM_MAP BIT(0)
  24. #define RMI_F01_QRY1_NON_COMPLIANT BIT(1)
  25. #define RMI_F01_QRY1_HAS_LTS BIT(2)
  26. #define RMI_F01_QRY1_HAS_SENSOR_ID BIT(3)
  27. #define RMI_F01_QRY1_HAS_CHARGER_INP BIT(4)
  28. #define RMI_F01_QRY1_HAS_ADJ_DOZE BIT(5)
  29. #define RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF BIT(6)
  30. #define RMI_F01_QRY1_HAS_QUERY42 BIT(7)
  31. #define RMI_F01_QRY5_YEAR_MASK 0x1f
  32. #define RMI_F01_QRY6_MONTH_MASK 0x0f
  33. #define RMI_F01_QRY7_DAY_MASK 0x1f
  34. #define RMI_F01_QRY2_PRODINFO_MASK 0x7f
  35. #define RMI_F01_BASIC_QUERY_LEN 21 /* From Query 00 through 20 */
  36. struct f01_basic_properties {
  37. u8 manufacturer_id;
  38. bool has_lts;
  39. bool has_adjustable_doze;
  40. bool has_adjustable_doze_holdoff;
  41. char dom[11]; /* YYYY/MM/DD + '\0' */
  42. u8 product_id[RMI_PRODUCT_ID_LENGTH + 1];
  43. u16 productinfo;
  44. u32 firmware_id;
  45. };
  46. /* F01 device status bits */
  47. /* Most recent device status event */
  48. #define RMI_F01_STATUS_CODE(status) ((status) & 0x0f)
  49. /* The device has lost its configuration for some reason. */
  50. #define RMI_F01_STATUS_UNCONFIGURED(status) (!!((status) & 0x80))
  51. /* Control register bits */
  52. /*
  53. * Sleep mode controls power management on the device and affects all
  54. * functions of the device.
  55. */
  56. #define RMI_F01_CTRL0_SLEEP_MODE_MASK 0x03
  57. #define RMI_SLEEP_MODE_NORMAL 0x00
  58. #define RMI_SLEEP_MODE_SENSOR_SLEEP 0x01
  59. #define RMI_SLEEP_MODE_RESERVED0 0x02
  60. #define RMI_SLEEP_MODE_RESERVED1 0x03
  61. /*
  62. * This bit disables whatever sleep mode may be selected by the sleep_mode
  63. * field and forces the device to run at full power without sleeping.
  64. */
  65. #define RMI_F01_CTRL0_NOSLEEP_BIT BIT(2)
  66. /*
  67. * When this bit is set, the touch controller employs a noise-filtering
  68. * algorithm designed for use with a connected battery charger.
  69. */
  70. #define RMI_F01_CTRL0_CHARGER_BIT BIT(5)
  71. /*
  72. * Sets the report rate for the device. The effect of this setting is
  73. * highly product dependent. Check the spec sheet for your particular
  74. * touch sensor.
  75. */
  76. #define RMI_F01_CTRL0_REPORTRATE_BIT BIT(6)
  77. /*
  78. * Written by the host as an indicator that the device has been
  79. * successfully configured.
  80. */
  81. #define RMI_F01_CTRL0_CONFIGURED_BIT BIT(7)
  82. /**
  83. * @ctrl0 - see the bit definitions above.
  84. * @doze_interval - controls the interval between checks for finger presence
  85. * when the touch sensor is in doze mode, in units of 10ms.
  86. * @wakeup_threshold - controls the capacitance threshold at which the touch
  87. * sensor will decide to wake up from that low power state.
  88. * @doze_holdoff - controls how long the touch sensor waits after the last
  89. * finger lifts before entering the doze state, in units of 100ms.
  90. */
  91. struct f01_device_control {
  92. u8 ctrl0;
  93. u8 doze_interval;
  94. u8 wakeup_threshold;
  95. u8 doze_holdoff;
  96. };
  97. struct f01_data {
  98. struct f01_basic_properties properties;
  99. struct f01_device_control device_control;
  100. u16 doze_interval_addr;
  101. u16 wakeup_threshold_addr;
  102. u16 doze_holdoff_addr;
  103. bool suspended;
  104. bool old_nosleep;
  105. unsigned int num_of_irq_regs;
  106. };
  107. static int rmi_f01_read_properties(struct rmi_device *rmi_dev,
  108. u16 query_base_addr,
  109. struct f01_basic_properties *props)
  110. {
  111. u8 queries[RMI_F01_BASIC_QUERY_LEN];
  112. int ret;
  113. int query_offset = query_base_addr;
  114. bool has_ds4_queries = false;
  115. bool has_query42 = false;
  116. bool has_sensor_id = false;
  117. bool has_package_id_query = false;
  118. bool has_build_id_query = false;
  119. u16 prod_info_addr;
  120. u8 ds4_query_len;
  121. ret = rmi_read_block(rmi_dev, query_offset,
  122. queries, RMI_F01_BASIC_QUERY_LEN);
  123. if (ret) {
  124. dev_err(&rmi_dev->dev,
  125. "Failed to read device query registers: %d\n", ret);
  126. return ret;
  127. }
  128. prod_info_addr = query_offset + 17;
  129. query_offset += RMI_F01_BASIC_QUERY_LEN;
  130. /* Now parse what we got */
  131. props->manufacturer_id = queries[0];
  132. props->has_lts = queries[1] & RMI_F01_QRY1_HAS_LTS;
  133. props->has_adjustable_doze =
  134. queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE;
  135. props->has_adjustable_doze_holdoff =
  136. queries[1] & RMI_F01_QRY1_HAS_ADJ_DOZE_HOFF;
  137. has_query42 = queries[1] & RMI_F01_QRY1_HAS_QUERY42;
  138. has_sensor_id = queries[1] & RMI_F01_QRY1_HAS_SENSOR_ID;
  139. snprintf(props->dom, sizeof(props->dom), "20%02d/%02d/%02d",
  140. queries[5] & RMI_F01_QRY5_YEAR_MASK,
  141. queries[6] & RMI_F01_QRY6_MONTH_MASK,
  142. queries[7] & RMI_F01_QRY7_DAY_MASK);
  143. memcpy(props->product_id, &queries[11],
  144. RMI_PRODUCT_ID_LENGTH);
  145. props->product_id[RMI_PRODUCT_ID_LENGTH] = '\0';
  146. props->productinfo =
  147. ((queries[2] & RMI_F01_QRY2_PRODINFO_MASK) << 7) |
  148. (queries[3] & RMI_F01_QRY2_PRODINFO_MASK);
  149. if (has_sensor_id)
  150. query_offset++;
  151. if (has_query42) {
  152. ret = rmi_read(rmi_dev, query_offset, queries);
  153. if (ret) {
  154. dev_err(&rmi_dev->dev,
  155. "Failed to read query 42 register: %d\n", ret);
  156. return ret;
  157. }
  158. has_ds4_queries = !!(queries[0] & BIT(0));
  159. query_offset++;
  160. }
  161. if (has_ds4_queries) {
  162. ret = rmi_read(rmi_dev, query_offset, &ds4_query_len);
  163. if (ret) {
  164. dev_err(&rmi_dev->dev,
  165. "Failed to read DS4 queries length: %d\n", ret);
  166. return ret;
  167. }
  168. query_offset++;
  169. if (ds4_query_len > 0) {
  170. ret = rmi_read(rmi_dev, query_offset, queries);
  171. if (ret) {
  172. dev_err(&rmi_dev->dev,
  173. "Failed to read DS4 queries: %d\n",
  174. ret);
  175. return ret;
  176. }
  177. has_package_id_query = !!(queries[0] & BIT(0));
  178. has_build_id_query = !!(queries[0] & BIT(1));
  179. }
  180. if (has_package_id_query)
  181. prod_info_addr++;
  182. if (has_build_id_query) {
  183. ret = rmi_read_block(rmi_dev, prod_info_addr, queries,
  184. 3);
  185. if (ret) {
  186. dev_err(&rmi_dev->dev,
  187. "Failed to read product info: %d\n",
  188. ret);
  189. return ret;
  190. }
  191. props->firmware_id = queries[1] << 8 | queries[0];
  192. props->firmware_id += queries[2] * 65536;
  193. }
  194. }
  195. return 0;
  196. }
  197. char *rmi_f01_get_product_ID(struct rmi_function *fn)
  198. {
  199. struct f01_data *f01 = dev_get_drvdata(&fn->dev);
  200. return f01->properties.product_id;
  201. }
  202. #ifdef CONFIG_OF
  203. static int rmi_f01_of_probe(struct device *dev,
  204. struct rmi_device_platform_data *pdata)
  205. {
  206. int retval;
  207. u32 val;
  208. retval = rmi_of_property_read_u32(dev,
  209. (u32 *)&pdata->power_management.nosleep,
  210. "syna,nosleep-mode", 1);
  211. if (retval)
  212. return retval;
  213. retval = rmi_of_property_read_u32(dev, &val,
  214. "syna,wakeup-threshold", 1);
  215. if (retval)
  216. return retval;
  217. pdata->power_management.wakeup_threshold = val;
  218. retval = rmi_of_property_read_u32(dev, &val,
  219. "syna,doze-holdoff-ms", 1);
  220. if (retval)
  221. return retval;
  222. pdata->power_management.doze_holdoff = val * 100;
  223. retval = rmi_of_property_read_u32(dev, &val,
  224. "syna,doze-interval-ms", 1);
  225. if (retval)
  226. return retval;
  227. pdata->power_management.doze_interval = val / 10;
  228. return 0;
  229. }
  230. #else
  231. static inline int rmi_f01_of_probe(struct device *dev,
  232. struct rmi_device_platform_data *pdata)
  233. {
  234. return -ENODEV;
  235. }
  236. #endif
  237. static int rmi_f01_probe(struct rmi_function *fn)
  238. {
  239. struct rmi_device *rmi_dev = fn->rmi_dev;
  240. struct rmi_driver_data *driver_data = dev_get_drvdata(&rmi_dev->dev);
  241. struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
  242. struct f01_data *f01;
  243. int error;
  244. u16 ctrl_base_addr = fn->fd.control_base_addr;
  245. u8 device_status;
  246. u8 temp;
  247. if (fn->dev.of_node) {
  248. error = rmi_f01_of_probe(&fn->dev, pdata);
  249. if (error)
  250. return error;
  251. }
  252. f01 = devm_kzalloc(&fn->dev, sizeof(struct f01_data), GFP_KERNEL);
  253. if (!f01)
  254. return -ENOMEM;
  255. f01->num_of_irq_regs = driver_data->num_of_irq_regs;
  256. /*
  257. * Set the configured bit and (optionally) other important stuff
  258. * in the device control register.
  259. */
  260. error = rmi_read(rmi_dev, fn->fd.control_base_addr,
  261. &f01->device_control.ctrl0);
  262. if (error) {
  263. dev_err(&fn->dev, "Failed to read F01 control: %d\n", error);
  264. return error;
  265. }
  266. switch (pdata->power_management.nosleep) {
  267. case RMI_F01_NOSLEEP_DEFAULT:
  268. break;
  269. case RMI_F01_NOSLEEP_OFF:
  270. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
  271. break;
  272. case RMI_F01_NOSLEEP_ON:
  273. f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
  274. break;
  275. }
  276. /*
  277. * Sleep mode might be set as a hangover from a system crash or
  278. * reboot without power cycle. If so, clear it so the sensor
  279. * is certain to function.
  280. */
  281. if ((f01->device_control.ctrl0 & RMI_F01_CTRL0_SLEEP_MODE_MASK) !=
  282. RMI_SLEEP_MODE_NORMAL) {
  283. dev_warn(&fn->dev,
  284. "WARNING: Non-zero sleep mode found. Clearing...\n");
  285. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
  286. }
  287. f01->device_control.ctrl0 |= RMI_F01_CTRL0_CONFIGURED_BIT;
  288. error = rmi_write(rmi_dev, fn->fd.control_base_addr,
  289. f01->device_control.ctrl0);
  290. if (error) {
  291. dev_err(&fn->dev, "Failed to write F01 control: %d\n", error);
  292. return error;
  293. }
  294. /* Dummy read in order to clear irqs */
  295. error = rmi_read(rmi_dev, fn->fd.data_base_addr + 1, &temp);
  296. if (error < 0) {
  297. dev_err(&fn->dev, "Failed to read Interrupt Status.\n");
  298. return error;
  299. }
  300. error = rmi_f01_read_properties(rmi_dev, fn->fd.query_base_addr,
  301. &f01->properties);
  302. if (error < 0) {
  303. dev_err(&fn->dev, "Failed to read F01 properties.\n");
  304. return error;
  305. }
  306. dev_info(&fn->dev, "found RMI device, manufacturer: %s, product: %s, fw id: %d\n",
  307. f01->properties.manufacturer_id == 1 ? "Synaptics" : "unknown",
  308. f01->properties.product_id, f01->properties.firmware_id);
  309. /* Advance to interrupt control registers, then skip over them. */
  310. ctrl_base_addr++;
  311. ctrl_base_addr += f01->num_of_irq_regs;
  312. /* read control register */
  313. if (f01->properties.has_adjustable_doze) {
  314. f01->doze_interval_addr = ctrl_base_addr;
  315. ctrl_base_addr++;
  316. if (pdata->power_management.doze_interval) {
  317. f01->device_control.doze_interval =
  318. pdata->power_management.doze_interval;
  319. error = rmi_write(rmi_dev, f01->doze_interval_addr,
  320. f01->device_control.doze_interval);
  321. if (error) {
  322. dev_err(&fn->dev,
  323. "Failed to configure F01 doze interval register: %d\n",
  324. error);
  325. return error;
  326. }
  327. } else {
  328. error = rmi_read(rmi_dev, f01->doze_interval_addr,
  329. &f01->device_control.doze_interval);
  330. if (error) {
  331. dev_err(&fn->dev,
  332. "Failed to read F01 doze interval register: %d\n",
  333. error);
  334. return error;
  335. }
  336. }
  337. f01->wakeup_threshold_addr = ctrl_base_addr;
  338. ctrl_base_addr++;
  339. if (pdata->power_management.wakeup_threshold) {
  340. f01->device_control.wakeup_threshold =
  341. pdata->power_management.wakeup_threshold;
  342. error = rmi_write(rmi_dev, f01->wakeup_threshold_addr,
  343. f01->device_control.wakeup_threshold);
  344. if (error) {
  345. dev_err(&fn->dev,
  346. "Failed to configure F01 wakeup threshold register: %d\n",
  347. error);
  348. return error;
  349. }
  350. } else {
  351. error = rmi_read(rmi_dev, f01->wakeup_threshold_addr,
  352. &f01->device_control.wakeup_threshold);
  353. if (error < 0) {
  354. dev_err(&fn->dev,
  355. "Failed to read F01 wakeup threshold register: %d\n",
  356. error);
  357. return error;
  358. }
  359. }
  360. }
  361. if (f01->properties.has_lts)
  362. ctrl_base_addr++;
  363. if (f01->properties.has_adjustable_doze_holdoff) {
  364. f01->doze_holdoff_addr = ctrl_base_addr;
  365. ctrl_base_addr++;
  366. if (pdata->power_management.doze_holdoff) {
  367. f01->device_control.doze_holdoff =
  368. pdata->power_management.doze_holdoff;
  369. error = rmi_write(rmi_dev, f01->doze_holdoff_addr,
  370. f01->device_control.doze_holdoff);
  371. if (error) {
  372. dev_err(&fn->dev,
  373. "Failed to configure F01 doze holdoff register: %d\n",
  374. error);
  375. return error;
  376. }
  377. } else {
  378. error = rmi_read(rmi_dev, f01->doze_holdoff_addr,
  379. &f01->device_control.doze_holdoff);
  380. if (error) {
  381. dev_err(&fn->dev,
  382. "Failed to read F01 doze holdoff register: %d\n",
  383. error);
  384. return error;
  385. }
  386. }
  387. }
  388. error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
  389. if (error < 0) {
  390. dev_err(&fn->dev,
  391. "Failed to read device status: %d\n", error);
  392. return error;
  393. }
  394. if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
  395. dev_err(&fn->dev,
  396. "Device was reset during configuration process, status: %#02x!\n",
  397. RMI_F01_STATUS_CODE(device_status));
  398. return -EINVAL;
  399. }
  400. dev_set_drvdata(&fn->dev, f01);
  401. return 0;
  402. }
  403. static int rmi_f01_config(struct rmi_function *fn)
  404. {
  405. struct f01_data *f01 = dev_get_drvdata(&fn->dev);
  406. int error;
  407. error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
  408. f01->device_control.ctrl0);
  409. if (error) {
  410. dev_err(&fn->dev,
  411. "Failed to write device_control register: %d\n", error);
  412. return error;
  413. }
  414. if (f01->properties.has_adjustable_doze) {
  415. error = rmi_write(fn->rmi_dev, f01->doze_interval_addr,
  416. f01->device_control.doze_interval);
  417. if (error) {
  418. dev_err(&fn->dev,
  419. "Failed to write doze interval: %d\n", error);
  420. return error;
  421. }
  422. error = rmi_write_block(fn->rmi_dev,
  423. f01->wakeup_threshold_addr,
  424. &f01->device_control.wakeup_threshold,
  425. sizeof(u8));
  426. if (error) {
  427. dev_err(&fn->dev,
  428. "Failed to write wakeup threshold: %d\n",
  429. error);
  430. return error;
  431. }
  432. }
  433. if (f01->properties.has_adjustable_doze_holdoff) {
  434. error = rmi_write(fn->rmi_dev, f01->doze_holdoff_addr,
  435. f01->device_control.doze_holdoff);
  436. if (error) {
  437. dev_err(&fn->dev,
  438. "Failed to write doze holdoff: %d\n", error);
  439. return error;
  440. }
  441. }
  442. return 0;
  443. }
  444. static int rmi_f01_suspend(struct rmi_function *fn)
  445. {
  446. struct f01_data *f01 = dev_get_drvdata(&fn->dev);
  447. int error;
  448. f01->old_nosleep =
  449. f01->device_control.ctrl0 & RMI_F01_CTRL0_NOSLEEP_BIT;
  450. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_NOSLEEP_BIT;
  451. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
  452. if (device_may_wakeup(fn->rmi_dev->xport->dev))
  453. f01->device_control.ctrl0 |= RMI_SLEEP_MODE_RESERVED1;
  454. else
  455. f01->device_control.ctrl0 |= RMI_SLEEP_MODE_SENSOR_SLEEP;
  456. error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
  457. f01->device_control.ctrl0);
  458. if (error) {
  459. dev_err(&fn->dev, "Failed to write sleep mode: %d.\n", error);
  460. if (f01->old_nosleep)
  461. f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
  462. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
  463. f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
  464. return error;
  465. }
  466. return 0;
  467. }
  468. static int rmi_f01_resume(struct rmi_function *fn)
  469. {
  470. struct f01_data *f01 = dev_get_drvdata(&fn->dev);
  471. int error;
  472. if (f01->old_nosleep)
  473. f01->device_control.ctrl0 |= RMI_F01_CTRL0_NOSLEEP_BIT;
  474. f01->device_control.ctrl0 &= ~RMI_F01_CTRL0_SLEEP_MODE_MASK;
  475. f01->device_control.ctrl0 |= RMI_SLEEP_MODE_NORMAL;
  476. error = rmi_write(fn->rmi_dev, fn->fd.control_base_addr,
  477. f01->device_control.ctrl0);
  478. if (error) {
  479. dev_err(&fn->dev,
  480. "Failed to restore normal operation: %d.\n", error);
  481. return error;
  482. }
  483. return 0;
  484. }
  485. static int rmi_f01_attention(struct rmi_function *fn,
  486. unsigned long *irq_bits)
  487. {
  488. struct rmi_device *rmi_dev = fn->rmi_dev;
  489. int error;
  490. u8 device_status;
  491. error = rmi_read(rmi_dev, fn->fd.data_base_addr, &device_status);
  492. if (error) {
  493. dev_err(&fn->dev,
  494. "Failed to read device status: %d.\n", error);
  495. return error;
  496. }
  497. if (RMI_F01_STATUS_UNCONFIGURED(device_status)) {
  498. dev_warn(&fn->dev, "Device reset detected.\n");
  499. error = rmi_dev->driver->reset_handler(rmi_dev);
  500. if (error) {
  501. dev_err(&fn->dev, "Device reset failed: %d\n", error);
  502. return error;
  503. }
  504. }
  505. return 0;
  506. }
  507. struct rmi_function_handler rmi_f01_handler = {
  508. .driver = {
  509. .name = "rmi4_f01",
  510. /*
  511. * Do not allow user unbinding F01 as it is critical
  512. * function.
  513. */
  514. .suppress_bind_attrs = true,
  515. },
  516. .func = 0x01,
  517. .probe = rmi_f01_probe,
  518. .config = rmi_f01_config,
  519. .attention = rmi_f01_attention,
  520. .suspend = rmi_f01_suspend,
  521. .resume = rmi_f01_resume,
  522. };