rmi_driver.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2011-2016 Synaptics Incorporated
  4. * Copyright (c) 2011 Unixphere
  5. *
  6. * This driver provides the core support for a single RMI4-based device.
  7. *
  8. * The RMI4 specification can be found here (URL split for line length):
  9. *
  10. * http://www.synaptics.com/sites/default/files/
  11. * 511-000136-01-Rev-E-RMI4-Interfacing-Guide.pdf
  12. */
  13. #include <linux/bitmap.h>
  14. #include <linux/delay.h>
  15. #include <linux/fs.h>
  16. #include <linux/irq.h>
  17. #include <linux/pm.h>
  18. #include <linux/slab.h>
  19. #include <linux/of.h>
  20. #include <linux/irqdomain.h>
  21. #include <uapi/linux/input.h>
  22. #include <linux/rmi.h>
  23. #include <linux/export.h>
  24. #include "rmi_bus.h"
  25. #include "rmi_driver.h"
  26. #define HAS_NONSTANDARD_PDT_MASK 0x40
  27. #define RMI4_MAX_PAGE 0xff
  28. #define RMI4_PAGE_SIZE 0x100
  29. #define RMI4_PAGE_MASK 0xFF00
  30. #define RMI_DEVICE_RESET_CMD 0x01
  31. #define DEFAULT_RESET_DELAY_MS 100
  32. void rmi_free_function_list(struct rmi_device *rmi_dev)
  33. {
  34. struct rmi_function *fn, *tmp;
  35. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  36. rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Freeing function list\n");
  37. /* Doing it in the reverse order so F01 will be removed last */
  38. list_for_each_entry_safe_reverse(fn, tmp,
  39. &data->function_list, node) {
  40. list_del(&fn->node);
  41. rmi_unregister_function(fn);
  42. }
  43. devm_kfree(&rmi_dev->dev, data->irq_memory);
  44. data->irq_memory = NULL;
  45. data->irq_status = NULL;
  46. data->fn_irq_bits = NULL;
  47. data->current_irq_mask = NULL;
  48. data->new_irq_mask = NULL;
  49. data->f01_container = NULL;
  50. data->f34_container = NULL;
  51. }
  52. static int reset_one_function(struct rmi_function *fn)
  53. {
  54. struct rmi_function_handler *fh;
  55. int retval = 0;
  56. if (!fn || !fn->dev.driver)
  57. return 0;
  58. fh = to_rmi_function_handler(fn->dev.driver);
  59. if (fh->reset) {
  60. retval = fh->reset(fn);
  61. if (retval < 0)
  62. dev_err(&fn->dev, "Reset failed with code %d.\n",
  63. retval);
  64. }
  65. return retval;
  66. }
  67. static int configure_one_function(struct rmi_function *fn)
  68. {
  69. struct rmi_function_handler *fh;
  70. int retval = 0;
  71. if (!fn || !fn->dev.driver)
  72. return 0;
  73. fh = to_rmi_function_handler(fn->dev.driver);
  74. if (fh->config) {
  75. retval = fh->config(fn);
  76. if (retval < 0)
  77. dev_err(&fn->dev, "Config failed with code %d.\n",
  78. retval);
  79. }
  80. return retval;
  81. }
  82. static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
  83. {
  84. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  85. struct rmi_function *entry;
  86. int retval;
  87. list_for_each_entry(entry, &data->function_list, node) {
  88. retval = reset_one_function(entry);
  89. if (retval < 0)
  90. return retval;
  91. }
  92. return 0;
  93. }
  94. static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
  95. {
  96. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  97. struct rmi_function *entry;
  98. int retval;
  99. list_for_each_entry(entry, &data->function_list, node) {
  100. retval = configure_one_function(entry);
  101. if (retval < 0)
  102. return retval;
  103. }
  104. return 0;
  105. }
  106. static int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
  107. {
  108. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  109. struct device *dev = &rmi_dev->dev;
  110. int i;
  111. int error;
  112. if (!data)
  113. return 0;
  114. if (!data->attn_data.data) {
  115. error = rmi_read_block(rmi_dev,
  116. data->f01_container->fd.data_base_addr + 1,
  117. data->irq_status, data->num_of_irq_regs);
  118. if (error < 0) {
  119. dev_err(dev, "Failed to read irqs, code=%d\n", error);
  120. return error;
  121. }
  122. }
  123. mutex_lock(&data->irq_mutex);
  124. bitmap_and(data->irq_status, data->irq_status, data->fn_irq_bits,
  125. data->irq_count);
  126. /*
  127. * At this point, irq_status has all bits that are set in the
  128. * interrupt status register and are enabled.
  129. */
  130. mutex_unlock(&data->irq_mutex);
  131. for_each_set_bit(i, data->irq_status, data->irq_count)
  132. handle_nested_irq(irq_find_mapping(data->irqdomain, i));
  133. if (data->input)
  134. input_sync(data->input);
  135. return 0;
  136. }
  137. void rmi_set_attn_data(struct rmi_device *rmi_dev, unsigned long irq_status,
  138. void *data, size_t size)
  139. {
  140. struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
  141. struct rmi4_attn_data attn_data;
  142. void *fifo_data;
  143. if (!drvdata->enabled)
  144. return;
  145. fifo_data = kmemdup(data, size, GFP_ATOMIC);
  146. if (!fifo_data)
  147. return;
  148. attn_data.irq_status = irq_status;
  149. attn_data.size = size;
  150. attn_data.data = fifo_data;
  151. kfifo_put(&drvdata->attn_fifo, attn_data);
  152. }
  153. EXPORT_SYMBOL_GPL(rmi_set_attn_data);
  154. static irqreturn_t rmi_irq_fn(int irq, void *dev_id)
  155. {
  156. struct rmi_device *rmi_dev = dev_id;
  157. struct rmi_driver_data *drvdata = dev_get_drvdata(&rmi_dev->dev);
  158. struct rmi4_attn_data attn_data = {0};
  159. int ret, count;
  160. count = kfifo_get(&drvdata->attn_fifo, &attn_data);
  161. if (count) {
  162. *(drvdata->irq_status) = attn_data.irq_status;
  163. drvdata->attn_data = attn_data;
  164. }
  165. ret = rmi_process_interrupt_requests(rmi_dev);
  166. if (ret)
  167. rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev,
  168. "Failed to process interrupt request: %d\n", ret);
  169. if (count) {
  170. kfree(attn_data.data);
  171. drvdata->attn_data.data = NULL;
  172. }
  173. if (!kfifo_is_empty(&drvdata->attn_fifo))
  174. return rmi_irq_fn(irq, dev_id);
  175. return IRQ_HANDLED;
  176. }
  177. static int rmi_irq_init(struct rmi_device *rmi_dev)
  178. {
  179. struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
  180. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  181. int irq_flags = irq_get_trigger_type(pdata->irq);
  182. int ret;
  183. if (!irq_flags)
  184. irq_flags = IRQF_TRIGGER_LOW;
  185. ret = devm_request_threaded_irq(&rmi_dev->dev, pdata->irq, NULL,
  186. rmi_irq_fn, irq_flags | IRQF_ONESHOT,
  187. dev_driver_string(rmi_dev->xport->dev),
  188. rmi_dev);
  189. if (ret < 0) {
  190. dev_err(&rmi_dev->dev, "Failed to register interrupt %d\n",
  191. pdata->irq);
  192. return ret;
  193. }
  194. data->enabled = true;
  195. return 0;
  196. }
  197. struct rmi_function *rmi_find_function(struct rmi_device *rmi_dev, u8 number)
  198. {
  199. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  200. struct rmi_function *entry;
  201. list_for_each_entry(entry, &data->function_list, node) {
  202. if (entry->fd.function_number == number)
  203. return entry;
  204. }
  205. return NULL;
  206. }
  207. static int suspend_one_function(struct rmi_function *fn)
  208. {
  209. struct rmi_function_handler *fh;
  210. int retval = 0;
  211. if (!fn || !fn->dev.driver)
  212. return 0;
  213. fh = to_rmi_function_handler(fn->dev.driver);
  214. if (fh->suspend) {
  215. retval = fh->suspend(fn);
  216. if (retval < 0)
  217. dev_err(&fn->dev, "Suspend failed with code %d.\n",
  218. retval);
  219. }
  220. return retval;
  221. }
  222. static int rmi_suspend_functions(struct rmi_device *rmi_dev)
  223. {
  224. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  225. struct rmi_function *entry;
  226. int retval;
  227. list_for_each_entry(entry, &data->function_list, node) {
  228. retval = suspend_one_function(entry);
  229. if (retval < 0)
  230. return retval;
  231. }
  232. return 0;
  233. }
  234. static int resume_one_function(struct rmi_function *fn)
  235. {
  236. struct rmi_function_handler *fh;
  237. int retval = 0;
  238. if (!fn || !fn->dev.driver)
  239. return 0;
  240. fh = to_rmi_function_handler(fn->dev.driver);
  241. if (fh->resume) {
  242. retval = fh->resume(fn);
  243. if (retval < 0)
  244. dev_err(&fn->dev, "Resume failed with code %d.\n",
  245. retval);
  246. }
  247. return retval;
  248. }
  249. static int rmi_resume_functions(struct rmi_device *rmi_dev)
  250. {
  251. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  252. struct rmi_function *entry;
  253. int retval;
  254. list_for_each_entry(entry, &data->function_list, node) {
  255. retval = resume_one_function(entry);
  256. if (retval < 0)
  257. return retval;
  258. }
  259. return 0;
  260. }
  261. int rmi_enable_sensor(struct rmi_device *rmi_dev)
  262. {
  263. int retval = 0;
  264. retval = rmi_driver_process_config_requests(rmi_dev);
  265. if (retval < 0)
  266. return retval;
  267. return rmi_process_interrupt_requests(rmi_dev);
  268. }
  269. /**
  270. * rmi_driver_set_input_params - set input device id and other data.
  271. *
  272. * @rmi_dev: Pointer to an RMI device
  273. * @input: Pointer to input device
  274. *
  275. */
  276. static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
  277. struct input_dev *input)
  278. {
  279. input->name = SYNAPTICS_INPUT_DEVICE_NAME;
  280. input->id.vendor = SYNAPTICS_VENDOR_ID;
  281. input->id.bustype = BUS_RMI;
  282. return 0;
  283. }
  284. static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
  285. struct input_dev *input)
  286. {
  287. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  288. const char *device_name = rmi_f01_get_product_ID(data->f01_container);
  289. char *name;
  290. name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
  291. "Synaptics %s", device_name);
  292. if (!name)
  293. return;
  294. input->name = name;
  295. }
  296. static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
  297. unsigned long *mask)
  298. {
  299. int error = 0;
  300. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  301. struct device *dev = &rmi_dev->dev;
  302. mutex_lock(&data->irq_mutex);
  303. bitmap_or(data->new_irq_mask,
  304. data->current_irq_mask, mask, data->irq_count);
  305. error = rmi_write_block(rmi_dev,
  306. data->f01_container->fd.control_base_addr + 1,
  307. data->new_irq_mask, data->num_of_irq_regs);
  308. if (error < 0) {
  309. dev_err(dev, "%s: Failed to change enabled interrupts!",
  310. __func__);
  311. goto error_unlock;
  312. }
  313. bitmap_copy(data->current_irq_mask, data->new_irq_mask,
  314. data->num_of_irq_regs);
  315. bitmap_or(data->fn_irq_bits, data->fn_irq_bits, mask, data->irq_count);
  316. error_unlock:
  317. mutex_unlock(&data->irq_mutex);
  318. return error;
  319. }
  320. static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
  321. unsigned long *mask)
  322. {
  323. int error = 0;
  324. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  325. struct device *dev = &rmi_dev->dev;
  326. mutex_lock(&data->irq_mutex);
  327. bitmap_andnot(data->fn_irq_bits,
  328. data->fn_irq_bits, mask, data->irq_count);
  329. bitmap_andnot(data->new_irq_mask,
  330. data->current_irq_mask, mask, data->irq_count);
  331. error = rmi_write_block(rmi_dev,
  332. data->f01_container->fd.control_base_addr + 1,
  333. data->new_irq_mask, data->num_of_irq_regs);
  334. if (error < 0) {
  335. dev_err(dev, "%s: Failed to change enabled interrupts!",
  336. __func__);
  337. goto error_unlock;
  338. }
  339. bitmap_copy(data->current_irq_mask, data->new_irq_mask,
  340. data->num_of_irq_regs);
  341. error_unlock:
  342. mutex_unlock(&data->irq_mutex);
  343. return error;
  344. }
  345. static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
  346. {
  347. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  348. int error;
  349. /*
  350. * Can get called before the driver is fully ready to deal with
  351. * this situation.
  352. */
  353. if (!data || !data->f01_container) {
  354. dev_warn(&rmi_dev->dev,
  355. "Not ready to handle reset yet!\n");
  356. return 0;
  357. }
  358. error = rmi_read_block(rmi_dev,
  359. data->f01_container->fd.control_base_addr + 1,
  360. data->current_irq_mask, data->num_of_irq_regs);
  361. if (error < 0) {
  362. dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
  363. __func__);
  364. return error;
  365. }
  366. error = rmi_driver_process_reset_requests(rmi_dev);
  367. if (error < 0)
  368. return error;
  369. error = rmi_driver_process_config_requests(rmi_dev);
  370. if (error < 0)
  371. return error;
  372. return 0;
  373. }
  374. static int rmi_read_pdt_entry(struct rmi_device *rmi_dev,
  375. struct pdt_entry *entry, u16 pdt_address)
  376. {
  377. u8 buf[RMI_PDT_ENTRY_SIZE];
  378. int error;
  379. error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
  380. if (error) {
  381. dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
  382. pdt_address, error);
  383. return error;
  384. }
  385. entry->page_start = pdt_address & RMI4_PAGE_MASK;
  386. entry->query_base_addr = buf[0];
  387. entry->command_base_addr = buf[1];
  388. entry->control_base_addr = buf[2];
  389. entry->data_base_addr = buf[3];
  390. entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
  391. entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
  392. entry->function_number = buf[5];
  393. return 0;
  394. }
  395. static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
  396. struct rmi_function_descriptor *fd)
  397. {
  398. fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
  399. fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
  400. fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
  401. fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
  402. fd->function_number = pdt->function_number;
  403. fd->interrupt_source_count = pdt->interrupt_source_count;
  404. fd->function_version = pdt->function_version;
  405. }
  406. #define RMI_SCAN_CONTINUE 0
  407. #define RMI_SCAN_DONE 1
  408. static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
  409. int page,
  410. int *empty_pages,
  411. void *ctx,
  412. int (*callback)(struct rmi_device *rmi_dev,
  413. void *ctx,
  414. const struct pdt_entry *entry))
  415. {
  416. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  417. struct pdt_entry pdt_entry;
  418. u16 page_start = RMI4_PAGE_SIZE * page;
  419. u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
  420. u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
  421. u16 addr;
  422. int error;
  423. int retval;
  424. for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
  425. error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
  426. if (error)
  427. return error;
  428. if (RMI4_END_OF_PDT(pdt_entry.function_number))
  429. break;
  430. retval = callback(rmi_dev, ctx, &pdt_entry);
  431. if (retval != RMI_SCAN_CONTINUE)
  432. return retval;
  433. }
  434. /*
  435. * Count number of empty PDT pages. If a gap of two pages
  436. * or more is found, stop scanning.
  437. */
  438. if (addr == pdt_start)
  439. ++*empty_pages;
  440. else
  441. *empty_pages = 0;
  442. return (data->bootloader_mode || *empty_pages >= 2) ?
  443. RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
  444. }
  445. int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
  446. int (*callback)(struct rmi_device *rmi_dev,
  447. void *ctx, const struct pdt_entry *entry))
  448. {
  449. int page;
  450. int empty_pages = 0;
  451. int retval = RMI_SCAN_DONE;
  452. for (page = 0; page <= RMI4_MAX_PAGE; page++) {
  453. retval = rmi_scan_pdt_page(rmi_dev, page, &empty_pages,
  454. ctx, callback);
  455. if (retval != RMI_SCAN_CONTINUE)
  456. break;
  457. }
  458. return retval < 0 ? retval : 0;
  459. }
  460. int rmi_read_register_desc(struct rmi_device *d, u16 addr,
  461. struct rmi_register_descriptor *rdesc)
  462. {
  463. int ret;
  464. u8 size_presence_reg;
  465. u8 buf[35];
  466. int presense_offset = 1;
  467. u8 *struct_buf;
  468. int reg;
  469. int offset = 0;
  470. int map_offset = 0;
  471. int i;
  472. int b;
  473. /*
  474. * The first register of the register descriptor is the size of
  475. * the register descriptor's presense register.
  476. */
  477. ret = rmi_read(d, addr, &size_presence_reg);
  478. if (ret)
  479. return ret;
  480. ++addr;
  481. if (size_presence_reg < 0 || size_presence_reg > 35)
  482. return -EIO;
  483. memset(buf, 0, sizeof(buf));
  484. /*
  485. * The presence register contains the size of the register structure
  486. * and a bitmap which identified which packet registers are present
  487. * for this particular register type (ie query, control, or data).
  488. */
  489. ret = rmi_read_block(d, addr, buf, size_presence_reg);
  490. if (ret)
  491. return ret;
  492. ++addr;
  493. if (buf[0] == 0) {
  494. presense_offset = 3;
  495. rdesc->struct_size = buf[1] | (buf[2] << 8);
  496. } else {
  497. rdesc->struct_size = buf[0];
  498. }
  499. for (i = presense_offset; i < size_presence_reg; i++) {
  500. for (b = 0; b < 8; b++) {
  501. if (buf[i] & (0x1 << b))
  502. bitmap_set(rdesc->presense_map, map_offset, 1);
  503. ++map_offset;
  504. }
  505. }
  506. rdesc->num_registers = bitmap_weight(rdesc->presense_map,
  507. RMI_REG_DESC_PRESENSE_BITS);
  508. rdesc->registers = devm_kcalloc(&d->dev,
  509. rdesc->num_registers,
  510. sizeof(struct rmi_register_desc_item),
  511. GFP_KERNEL);
  512. if (!rdesc->registers)
  513. return -ENOMEM;
  514. /*
  515. * Allocate a temporary buffer to hold the register structure.
  516. * I'm not using devm_kzalloc here since it will not be retained
  517. * after exiting this function
  518. */
  519. struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
  520. if (!struct_buf)
  521. return -ENOMEM;
  522. /*
  523. * The register structure contains information about every packet
  524. * register of this type. This includes the size of the packet
  525. * register and a bitmap of all subpackets contained in the packet
  526. * register.
  527. */
  528. ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
  529. if (ret)
  530. goto free_struct_buff;
  531. reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
  532. for (i = 0; i < rdesc->num_registers; i++) {
  533. struct rmi_register_desc_item *item = &rdesc->registers[i];
  534. int reg_size = struct_buf[offset];
  535. ++offset;
  536. if (reg_size == 0) {
  537. reg_size = struct_buf[offset] |
  538. (struct_buf[offset + 1] << 8);
  539. offset += 2;
  540. }
  541. if (reg_size == 0) {
  542. reg_size = struct_buf[offset] |
  543. (struct_buf[offset + 1] << 8) |
  544. (struct_buf[offset + 2] << 16) |
  545. (struct_buf[offset + 3] << 24);
  546. offset += 4;
  547. }
  548. item->reg = reg;
  549. item->reg_size = reg_size;
  550. map_offset = 0;
  551. do {
  552. for (b = 0; b < 7; b++) {
  553. if (struct_buf[offset] & (0x1 << b))
  554. bitmap_set(item->subpacket_map,
  555. map_offset, 1);
  556. ++map_offset;
  557. }
  558. } while (struct_buf[offset++] & 0x80);
  559. item->num_subpackets = bitmap_weight(item->subpacket_map,
  560. RMI_REG_DESC_SUBPACKET_BITS);
  561. rmi_dbg(RMI_DEBUG_CORE, &d->dev,
  562. "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
  563. item->reg, item->reg_size, item->num_subpackets);
  564. reg = find_next_bit(rdesc->presense_map,
  565. RMI_REG_DESC_PRESENSE_BITS, reg + 1);
  566. }
  567. free_struct_buff:
  568. kfree(struct_buf);
  569. return ret;
  570. }
  571. const struct rmi_register_desc_item *rmi_get_register_desc_item(
  572. struct rmi_register_descriptor *rdesc, u16 reg)
  573. {
  574. const struct rmi_register_desc_item *item;
  575. int i;
  576. for (i = 0; i < rdesc->num_registers; i++) {
  577. item = &rdesc->registers[i];
  578. if (item->reg == reg)
  579. return item;
  580. }
  581. return NULL;
  582. }
  583. size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
  584. {
  585. const struct rmi_register_desc_item *item;
  586. int i;
  587. size_t size = 0;
  588. for (i = 0; i < rdesc->num_registers; i++) {
  589. item = &rdesc->registers[i];
  590. size += item->reg_size;
  591. }
  592. return size;
  593. }
  594. /* Compute the register offset relative to the base address */
  595. int rmi_register_desc_calc_reg_offset(
  596. struct rmi_register_descriptor *rdesc, u16 reg)
  597. {
  598. const struct rmi_register_desc_item *item;
  599. int offset = 0;
  600. int i;
  601. for (i = 0; i < rdesc->num_registers; i++) {
  602. item = &rdesc->registers[i];
  603. if (item->reg == reg)
  604. return offset;
  605. ++offset;
  606. }
  607. return -1;
  608. }
  609. bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
  610. u8 subpacket)
  611. {
  612. return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
  613. subpacket) == subpacket;
  614. }
  615. static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
  616. const struct pdt_entry *pdt)
  617. {
  618. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  619. int ret;
  620. u8 status;
  621. if (pdt->function_number == 0x34 && pdt->function_version > 1) {
  622. ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
  623. if (ret) {
  624. dev_err(&rmi_dev->dev,
  625. "Failed to read F34 status: %d.\n", ret);
  626. return ret;
  627. }
  628. if (status & BIT(7))
  629. data->bootloader_mode = true;
  630. } else if (pdt->function_number == 0x01) {
  631. ret = rmi_read(rmi_dev, pdt->data_base_addr, &status);
  632. if (ret) {
  633. dev_err(&rmi_dev->dev,
  634. "Failed to read F01 status: %d.\n", ret);
  635. return ret;
  636. }
  637. if (status & BIT(6))
  638. data->bootloader_mode = true;
  639. }
  640. return 0;
  641. }
  642. static int rmi_count_irqs(struct rmi_device *rmi_dev,
  643. void *ctx, const struct pdt_entry *pdt)
  644. {
  645. int *irq_count = ctx;
  646. int ret;
  647. *irq_count += pdt->interrupt_source_count;
  648. ret = rmi_check_bootloader_mode(rmi_dev, pdt);
  649. if (ret < 0)
  650. return ret;
  651. return RMI_SCAN_CONTINUE;
  652. }
  653. int rmi_initial_reset(struct rmi_device *rmi_dev, void *ctx,
  654. const struct pdt_entry *pdt)
  655. {
  656. int error;
  657. if (pdt->function_number == 0x01) {
  658. u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
  659. u8 cmd_buf = RMI_DEVICE_RESET_CMD;
  660. const struct rmi_device_platform_data *pdata =
  661. rmi_get_platform_data(rmi_dev);
  662. if (rmi_dev->xport->ops->reset) {
  663. error = rmi_dev->xport->ops->reset(rmi_dev->xport,
  664. cmd_addr);
  665. if (error)
  666. return error;
  667. return RMI_SCAN_DONE;
  668. }
  669. rmi_dbg(RMI_DEBUG_CORE, &rmi_dev->dev, "Sending reset\n");
  670. error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
  671. if (error) {
  672. dev_err(&rmi_dev->dev,
  673. "Initial reset failed. Code = %d.\n", error);
  674. return error;
  675. }
  676. mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
  677. return RMI_SCAN_DONE;
  678. }
  679. /* F01 should always be on page 0. If we don't find it there, fail. */
  680. return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
  681. }
  682. static int rmi_create_function(struct rmi_device *rmi_dev,
  683. void *ctx, const struct pdt_entry *pdt)
  684. {
  685. struct device *dev = &rmi_dev->dev;
  686. struct rmi_driver_data *data = dev_get_drvdata(dev);
  687. int *current_irq_count = ctx;
  688. struct rmi_function *fn;
  689. int i;
  690. int error;
  691. rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
  692. pdt->function_number);
  693. fn = kzalloc(sizeof(struct rmi_function) +
  694. BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
  695. GFP_KERNEL);
  696. if (!fn) {
  697. dev_err(dev, "Failed to allocate memory for F%02X\n",
  698. pdt->function_number);
  699. return -ENOMEM;
  700. }
  701. INIT_LIST_HEAD(&fn->node);
  702. rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
  703. fn->rmi_dev = rmi_dev;
  704. fn->num_of_irqs = pdt->interrupt_source_count;
  705. fn->irq_pos = *current_irq_count;
  706. *current_irq_count += fn->num_of_irqs;
  707. for (i = 0; i < fn->num_of_irqs; i++)
  708. set_bit(fn->irq_pos + i, fn->irq_mask);
  709. error = rmi_register_function(fn);
  710. if (error)
  711. return error;
  712. if (pdt->function_number == 0x01)
  713. data->f01_container = fn;
  714. else if (pdt->function_number == 0x34)
  715. data->f34_container = fn;
  716. list_add_tail(&fn->node, &data->function_list);
  717. return RMI_SCAN_CONTINUE;
  718. }
  719. void rmi_enable_irq(struct rmi_device *rmi_dev, bool clear_wake)
  720. {
  721. struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
  722. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  723. int irq = pdata->irq;
  724. int irq_flags;
  725. int retval;
  726. mutex_lock(&data->enabled_mutex);
  727. if (data->enabled)
  728. goto out;
  729. enable_irq(irq);
  730. data->enabled = true;
  731. if (clear_wake && device_may_wakeup(rmi_dev->xport->dev)) {
  732. retval = disable_irq_wake(irq);
  733. if (retval)
  734. dev_warn(&rmi_dev->dev,
  735. "Failed to disable irq for wake: %d\n",
  736. retval);
  737. }
  738. /*
  739. * Call rmi_process_interrupt_requests() after enabling irq,
  740. * otherwise we may lose interrupt on edge-triggered systems.
  741. */
  742. irq_flags = irq_get_trigger_type(pdata->irq);
  743. if (irq_flags & IRQ_TYPE_EDGE_BOTH)
  744. rmi_process_interrupt_requests(rmi_dev);
  745. out:
  746. mutex_unlock(&data->enabled_mutex);
  747. }
  748. void rmi_disable_irq(struct rmi_device *rmi_dev, bool enable_wake)
  749. {
  750. struct rmi_device_platform_data *pdata = rmi_get_platform_data(rmi_dev);
  751. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  752. struct rmi4_attn_data attn_data = {0};
  753. int irq = pdata->irq;
  754. int retval, count;
  755. mutex_lock(&data->enabled_mutex);
  756. if (!data->enabled)
  757. goto out;
  758. data->enabled = false;
  759. disable_irq(irq);
  760. if (enable_wake && device_may_wakeup(rmi_dev->xport->dev)) {
  761. retval = enable_irq_wake(irq);
  762. if (retval)
  763. dev_warn(&rmi_dev->dev,
  764. "Failed to enable irq for wake: %d\n",
  765. retval);
  766. }
  767. /* make sure the fifo is clean */
  768. while (!kfifo_is_empty(&data->attn_fifo)) {
  769. count = kfifo_get(&data->attn_fifo, &attn_data);
  770. if (count)
  771. kfree(attn_data.data);
  772. }
  773. out:
  774. mutex_unlock(&data->enabled_mutex);
  775. }
  776. int rmi_driver_suspend(struct rmi_device *rmi_dev, bool enable_wake)
  777. {
  778. int retval;
  779. retval = rmi_suspend_functions(rmi_dev);
  780. if (retval)
  781. dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
  782. retval);
  783. rmi_disable_irq(rmi_dev, enable_wake);
  784. return retval;
  785. }
  786. EXPORT_SYMBOL_GPL(rmi_driver_suspend);
  787. int rmi_driver_resume(struct rmi_device *rmi_dev, bool clear_wake)
  788. {
  789. int retval;
  790. rmi_enable_irq(rmi_dev, clear_wake);
  791. retval = rmi_resume_functions(rmi_dev);
  792. if (retval)
  793. dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
  794. retval);
  795. return retval;
  796. }
  797. EXPORT_SYMBOL_GPL(rmi_driver_resume);
  798. static int rmi_driver_remove(struct device *dev)
  799. {
  800. struct rmi_device *rmi_dev = to_rmi_device(dev);
  801. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  802. rmi_disable_irq(rmi_dev, false);
  803. rmi_f34_remove_sysfs(rmi_dev);
  804. rmi_free_function_list(rmi_dev);
  805. irq_domain_remove(data->irqdomain);
  806. data->irqdomain = NULL;
  807. return 0;
  808. }
  809. #ifdef CONFIG_OF
  810. static int rmi_driver_of_probe(struct device *dev,
  811. struct rmi_device_platform_data *pdata)
  812. {
  813. int retval;
  814. retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
  815. "syna,reset-delay-ms", 1);
  816. if (retval)
  817. return retval;
  818. return 0;
  819. }
  820. #else
  821. static inline int rmi_driver_of_probe(struct device *dev,
  822. struct rmi_device_platform_data *pdata)
  823. {
  824. return -ENODEV;
  825. }
  826. #endif
  827. int rmi_probe_interrupts(struct rmi_driver_data *data)
  828. {
  829. struct rmi_device *rmi_dev = data->rmi_dev;
  830. struct device *dev = &rmi_dev->dev;
  831. struct fwnode_handle *fwnode = rmi_dev->xport->dev->fwnode;
  832. int irq_count = 0;
  833. size_t size;
  834. int retval;
  835. /*
  836. * We need to count the IRQs and allocate their storage before scanning
  837. * the PDT and creating the function entries, because adding a new
  838. * function can trigger events that result in the IRQ related storage
  839. * being accessed.
  840. */
  841. rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Counting IRQs.\n", __func__);
  842. data->bootloader_mode = false;
  843. retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
  844. if (retval < 0) {
  845. dev_err(dev, "IRQ counting failed with code %d.\n", retval);
  846. return retval;
  847. }
  848. if (data->bootloader_mode)
  849. dev_warn(dev, "Device in bootloader mode.\n");
  850. /* Allocate and register a linear revmap irq_domain */
  851. data->irqdomain = irq_domain_create_linear(fwnode, irq_count,
  852. &irq_domain_simple_ops,
  853. data);
  854. if (!data->irqdomain) {
  855. dev_err(&rmi_dev->dev, "Failed to create IRQ domain\n");
  856. return -ENOMEM;
  857. }
  858. data->irq_count = irq_count;
  859. data->num_of_irq_regs = (data->irq_count + 7) / 8;
  860. size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
  861. data->irq_memory = devm_kcalloc(dev, size, 4, GFP_KERNEL);
  862. if (!data->irq_memory) {
  863. dev_err(dev, "Failed to allocate memory for irq masks.\n");
  864. return -ENOMEM;
  865. }
  866. data->irq_status = data->irq_memory + size * 0;
  867. data->fn_irq_bits = data->irq_memory + size * 1;
  868. data->current_irq_mask = data->irq_memory + size * 2;
  869. data->new_irq_mask = data->irq_memory + size * 3;
  870. return retval;
  871. }
  872. int rmi_init_functions(struct rmi_driver_data *data)
  873. {
  874. struct rmi_device *rmi_dev = data->rmi_dev;
  875. struct device *dev = &rmi_dev->dev;
  876. int irq_count = 0;
  877. int retval;
  878. rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Creating functions.\n", __func__);
  879. retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
  880. if (retval < 0) {
  881. dev_err(dev, "Function creation failed with code %d.\n",
  882. retval);
  883. goto err_destroy_functions;
  884. }
  885. if (!data->f01_container) {
  886. dev_err(dev, "Missing F01 container!\n");
  887. retval = -EINVAL;
  888. goto err_destroy_functions;
  889. }
  890. retval = rmi_read_block(rmi_dev,
  891. data->f01_container->fd.control_base_addr + 1,
  892. data->current_irq_mask, data->num_of_irq_regs);
  893. if (retval < 0) {
  894. dev_err(dev, "%s: Failed to read current IRQ mask.\n",
  895. __func__);
  896. goto err_destroy_functions;
  897. }
  898. return 0;
  899. err_destroy_functions:
  900. rmi_free_function_list(rmi_dev);
  901. return retval;
  902. }
  903. static int rmi_driver_probe(struct device *dev)
  904. {
  905. struct rmi_driver *rmi_driver;
  906. struct rmi_driver_data *data;
  907. struct rmi_device_platform_data *pdata;
  908. struct rmi_device *rmi_dev;
  909. int retval;
  910. rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
  911. __func__);
  912. if (!rmi_is_physical_device(dev)) {
  913. rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
  914. return -ENODEV;
  915. }
  916. rmi_dev = to_rmi_device(dev);
  917. rmi_driver = to_rmi_driver(dev->driver);
  918. rmi_dev->driver = rmi_driver;
  919. pdata = rmi_get_platform_data(rmi_dev);
  920. if (rmi_dev->xport->dev->of_node) {
  921. retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
  922. if (retval)
  923. return retval;
  924. }
  925. data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
  926. if (!data)
  927. return -ENOMEM;
  928. INIT_LIST_HEAD(&data->function_list);
  929. data->rmi_dev = rmi_dev;
  930. dev_set_drvdata(&rmi_dev->dev, data);
  931. /*
  932. * Right before a warm boot, the sensor might be in some unusual state,
  933. * such as F54 diagnostics, or F34 bootloader mode after a firmware
  934. * or configuration update. In order to clear the sensor to a known
  935. * state and/or apply any updates, we issue a initial reset to clear any
  936. * previous settings and force it into normal operation.
  937. *
  938. * We have to do this before actually building the PDT because
  939. * the reflash updates (if any) might cause various registers to move
  940. * around.
  941. *
  942. * For a number of reasons, this initial reset may fail to return
  943. * within the specified time, but we'll still be able to bring up the
  944. * driver normally after that failure. This occurs most commonly in
  945. * a cold boot situation (where then firmware takes longer to come up
  946. * than from a warm boot) and the reset_delay_ms in the platform data
  947. * has been set too short to accommodate that. Since the sensor will
  948. * eventually come up and be usable, we don't want to just fail here
  949. * and leave the customer's device unusable. So we warn them, and
  950. * continue processing.
  951. */
  952. retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
  953. if (retval < 0)
  954. dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
  955. retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
  956. if (retval < 0) {
  957. /*
  958. * we'll print out a warning and continue since
  959. * failure to get the PDT properties is not a cause to fail
  960. */
  961. dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
  962. PDT_PROPERTIES_LOCATION, retval);
  963. }
  964. mutex_init(&data->irq_mutex);
  965. mutex_init(&data->enabled_mutex);
  966. retval = rmi_probe_interrupts(data);
  967. if (retval)
  968. goto err;
  969. if (rmi_dev->xport->input) {
  970. /*
  971. * The transport driver already has an input device.
  972. * In some cases it is preferable to reuse the transport
  973. * devices input device instead of creating a new one here.
  974. * One example is some HID touchpads report "pass-through"
  975. * button events are not reported by rmi registers.
  976. */
  977. data->input = rmi_dev->xport->input;
  978. } else {
  979. data->input = devm_input_allocate_device(dev);
  980. if (!data->input) {
  981. dev_err(dev, "%s: Failed to allocate input device.\n",
  982. __func__);
  983. retval = -ENOMEM;
  984. goto err;
  985. }
  986. rmi_driver_set_input_params(rmi_dev, data->input);
  987. data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
  988. "%s/input0", dev_name(dev));
  989. if (!data->input->phys) {
  990. retval = -ENOMEM;
  991. goto err;
  992. }
  993. }
  994. retval = rmi_init_functions(data);
  995. if (retval)
  996. goto err;
  997. retval = rmi_f34_create_sysfs(rmi_dev);
  998. if (retval)
  999. goto err;
  1000. if (data->input) {
  1001. rmi_driver_set_input_name(rmi_dev, data->input);
  1002. if (!rmi_dev->xport->input) {
  1003. retval = input_register_device(data->input);
  1004. if (retval) {
  1005. dev_err(dev, "%s: Failed to register input device.\n",
  1006. __func__);
  1007. goto err_destroy_functions;
  1008. }
  1009. }
  1010. }
  1011. retval = rmi_irq_init(rmi_dev);
  1012. if (retval < 0)
  1013. goto err_destroy_functions;
  1014. if (data->f01_container->dev.driver) {
  1015. /* Driver already bound, so enable ATTN now. */
  1016. retval = rmi_enable_sensor(rmi_dev);
  1017. if (retval)
  1018. goto err_disable_irq;
  1019. }
  1020. return 0;
  1021. err_disable_irq:
  1022. rmi_disable_irq(rmi_dev, false);
  1023. err_destroy_functions:
  1024. rmi_free_function_list(rmi_dev);
  1025. err:
  1026. return retval;
  1027. }
  1028. static struct rmi_driver rmi_physical_driver = {
  1029. .driver = {
  1030. .owner = THIS_MODULE,
  1031. .name = "rmi4_physical",
  1032. .bus = &rmi_bus_type,
  1033. .probe = rmi_driver_probe,
  1034. .remove = rmi_driver_remove,
  1035. },
  1036. .reset_handler = rmi_driver_reset_handler,
  1037. .clear_irq_bits = rmi_driver_clear_irq_bits,
  1038. .set_irq_bits = rmi_driver_set_irq_bits,
  1039. .set_input_params = rmi_driver_set_input_params,
  1040. };
  1041. bool rmi_is_physical_driver(const struct device_driver *drv)
  1042. {
  1043. return drv == &rmi_physical_driver.driver;
  1044. }
  1045. int __init rmi_register_physical_driver(void)
  1046. {
  1047. int error;
  1048. error = driver_register(&rmi_physical_driver.driver);
  1049. if (error) {
  1050. pr_err("%s: driver register failed, code=%d.\n", __func__,
  1051. error);
  1052. return error;
  1053. }
  1054. return 0;
  1055. }
  1056. void __exit rmi_unregister_physical_driver(void)
  1057. {
  1058. driver_unregister(&rmi_physical_driver.driver);
  1059. }