rmi_f01.c 19 KB

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