rmi_driver.c 31 KB

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