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