rmi_f01.c 19 KB

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