rmi_driver.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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/pm.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <uapi/linux/input.h>
  23. #include <linux/rmi.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. static 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. data->f01_container = NULL;
  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. }
  44. static int reset_one_function(struct rmi_function *fn)
  45. {
  46. struct rmi_function_handler *fh;
  47. int retval = 0;
  48. if (!fn || !fn->dev.driver)
  49. return 0;
  50. fh = to_rmi_function_handler(fn->dev.driver);
  51. if (fh->reset) {
  52. retval = fh->reset(fn);
  53. if (retval < 0)
  54. dev_err(&fn->dev, "Reset failed with code %d.\n",
  55. retval);
  56. }
  57. return retval;
  58. }
  59. static int configure_one_function(struct rmi_function *fn)
  60. {
  61. struct rmi_function_handler *fh;
  62. int retval = 0;
  63. if (!fn || !fn->dev.driver)
  64. return 0;
  65. fh = to_rmi_function_handler(fn->dev.driver);
  66. if (fh->config) {
  67. retval = fh->config(fn);
  68. if (retval < 0)
  69. dev_err(&fn->dev, "Config failed with code %d.\n",
  70. retval);
  71. }
  72. return retval;
  73. }
  74. static int rmi_driver_process_reset_requests(struct rmi_device *rmi_dev)
  75. {
  76. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  77. struct rmi_function *entry;
  78. int retval;
  79. list_for_each_entry(entry, &data->function_list, node) {
  80. retval = reset_one_function(entry);
  81. if (retval < 0)
  82. return retval;
  83. }
  84. return 0;
  85. }
  86. static int rmi_driver_process_config_requests(struct rmi_device *rmi_dev)
  87. {
  88. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  89. struct rmi_function *entry;
  90. int retval;
  91. list_for_each_entry(entry, &data->function_list, node) {
  92. retval = configure_one_function(entry);
  93. if (retval < 0)
  94. return retval;
  95. }
  96. return 0;
  97. }
  98. static void process_one_interrupt(struct rmi_driver_data *data,
  99. struct rmi_function *fn)
  100. {
  101. struct rmi_function_handler *fh;
  102. if (!fn || !fn->dev.driver)
  103. return;
  104. fh = to_rmi_function_handler(fn->dev.driver);
  105. if (fh->attention) {
  106. bitmap_and(data->fn_irq_bits, data->irq_status, fn->irq_mask,
  107. data->irq_count);
  108. if (!bitmap_empty(data->fn_irq_bits, data->irq_count))
  109. fh->attention(fn, data->fn_irq_bits);
  110. }
  111. }
  112. int rmi_process_interrupt_requests(struct rmi_device *rmi_dev)
  113. {
  114. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  115. struct device *dev = &rmi_dev->dev;
  116. struct rmi_function *entry;
  117. int error;
  118. if (!data)
  119. return 0;
  120. if (!rmi_dev->xport->attn_data) {
  121. error = rmi_read_block(rmi_dev,
  122. data->f01_container->fd.data_base_addr + 1,
  123. data->irq_status, data->num_of_irq_regs);
  124. if (error < 0) {
  125. dev_err(dev, "Failed to read irqs, code=%d\n", error);
  126. return error;
  127. }
  128. }
  129. mutex_lock(&data->irq_mutex);
  130. bitmap_and(data->irq_status, data->irq_status, data->current_irq_mask,
  131. data->irq_count);
  132. /*
  133. * At this point, irq_status has all bits that are set in the
  134. * interrupt status register and are enabled.
  135. */
  136. mutex_unlock(&data->irq_mutex);
  137. /*
  138. * It would be nice to be able to use irq_chip to handle these
  139. * nested IRQs. Unfortunately, most of the current customers for
  140. * this driver are using older kernels (3.0.x) that don't support
  141. * the features required for that. Once they've shifted to more
  142. * recent kernels (say, 3.3 and higher), this should be switched to
  143. * use irq_chip.
  144. */
  145. list_for_each_entry(entry, &data->function_list, node)
  146. process_one_interrupt(data, entry);
  147. if (data->input)
  148. input_sync(data->input);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(rmi_process_interrupt_requests);
  152. static int suspend_one_function(struct rmi_function *fn)
  153. {
  154. struct rmi_function_handler *fh;
  155. int retval = 0;
  156. if (!fn || !fn->dev.driver)
  157. return 0;
  158. fh = to_rmi_function_handler(fn->dev.driver);
  159. if (fh->suspend) {
  160. retval = fh->suspend(fn);
  161. if (retval < 0)
  162. dev_err(&fn->dev, "Suspend failed with code %d.\n",
  163. retval);
  164. }
  165. return retval;
  166. }
  167. static int rmi_suspend_functions(struct rmi_device *rmi_dev)
  168. {
  169. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  170. struct rmi_function *entry;
  171. int retval;
  172. list_for_each_entry(entry, &data->function_list, node) {
  173. retval = suspend_one_function(entry);
  174. if (retval < 0)
  175. return retval;
  176. }
  177. return 0;
  178. }
  179. static int resume_one_function(struct rmi_function *fn)
  180. {
  181. struct rmi_function_handler *fh;
  182. int retval = 0;
  183. if (!fn || !fn->dev.driver)
  184. return 0;
  185. fh = to_rmi_function_handler(fn->dev.driver);
  186. if (fh->resume) {
  187. retval = fh->resume(fn);
  188. if (retval < 0)
  189. dev_err(&fn->dev, "Resume failed with code %d.\n",
  190. retval);
  191. }
  192. return retval;
  193. }
  194. static int rmi_resume_functions(struct rmi_device *rmi_dev)
  195. {
  196. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  197. struct rmi_function *entry;
  198. int retval;
  199. list_for_each_entry(entry, &data->function_list, node) {
  200. retval = resume_one_function(entry);
  201. if (retval < 0)
  202. return retval;
  203. }
  204. return 0;
  205. }
  206. static int enable_sensor(struct rmi_device *rmi_dev)
  207. {
  208. int retval = 0;
  209. retval = rmi_driver_process_config_requests(rmi_dev);
  210. if (retval < 0)
  211. return retval;
  212. return rmi_process_interrupt_requests(rmi_dev);
  213. }
  214. /**
  215. * rmi_driver_set_input_params - set input device id and other data.
  216. *
  217. * @rmi_dev: Pointer to an RMI device
  218. * @input: Pointer to input device
  219. *
  220. */
  221. static int rmi_driver_set_input_params(struct rmi_device *rmi_dev,
  222. struct input_dev *input)
  223. {
  224. input->name = SYNAPTICS_INPUT_DEVICE_NAME;
  225. input->id.vendor = SYNAPTICS_VENDOR_ID;
  226. input->id.bustype = BUS_RMI;
  227. return 0;
  228. }
  229. static void rmi_driver_set_input_name(struct rmi_device *rmi_dev,
  230. struct input_dev *input)
  231. {
  232. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  233. char *device_name = rmi_f01_get_product_ID(data->f01_container);
  234. char *name;
  235. name = devm_kasprintf(&rmi_dev->dev, GFP_KERNEL,
  236. "Synaptics %s", device_name);
  237. if (!name)
  238. return;
  239. input->name = name;
  240. }
  241. static int rmi_driver_set_irq_bits(struct rmi_device *rmi_dev,
  242. unsigned long *mask)
  243. {
  244. int error = 0;
  245. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  246. struct device *dev = &rmi_dev->dev;
  247. mutex_lock(&data->irq_mutex);
  248. bitmap_or(data->new_irq_mask,
  249. data->current_irq_mask, mask, data->irq_count);
  250. error = rmi_write_block(rmi_dev,
  251. data->f01_container->fd.control_base_addr + 1,
  252. data->new_irq_mask, data->num_of_irq_regs);
  253. if (error < 0) {
  254. dev_err(dev, "%s: Failed to change enabled interrupts!",
  255. __func__);
  256. goto error_unlock;
  257. }
  258. bitmap_copy(data->current_irq_mask, data->new_irq_mask,
  259. data->num_of_irq_regs);
  260. error_unlock:
  261. mutex_unlock(&data->irq_mutex);
  262. return error;
  263. }
  264. static int rmi_driver_clear_irq_bits(struct rmi_device *rmi_dev,
  265. unsigned long *mask)
  266. {
  267. int error = 0;
  268. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  269. struct device *dev = &rmi_dev->dev;
  270. mutex_lock(&data->irq_mutex);
  271. bitmap_andnot(data->new_irq_mask,
  272. data->current_irq_mask, mask, data->irq_count);
  273. error = rmi_write_block(rmi_dev,
  274. data->f01_container->fd.control_base_addr + 1,
  275. data->new_irq_mask, data->num_of_irq_regs);
  276. if (error < 0) {
  277. dev_err(dev, "%s: Failed to change enabled interrupts!",
  278. __func__);
  279. goto error_unlock;
  280. }
  281. bitmap_copy(data->current_irq_mask, data->new_irq_mask,
  282. data->num_of_irq_regs);
  283. error_unlock:
  284. mutex_unlock(&data->irq_mutex);
  285. return error;
  286. }
  287. static int rmi_driver_reset_handler(struct rmi_device *rmi_dev)
  288. {
  289. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  290. int error;
  291. /*
  292. * Can get called before the driver is fully ready to deal with
  293. * this situation.
  294. */
  295. if (!data || !data->f01_container) {
  296. dev_warn(&rmi_dev->dev,
  297. "Not ready to handle reset yet!\n");
  298. return 0;
  299. }
  300. error = rmi_read_block(rmi_dev,
  301. data->f01_container->fd.control_base_addr + 1,
  302. data->current_irq_mask, data->num_of_irq_regs);
  303. if (error < 0) {
  304. dev_err(&rmi_dev->dev, "%s: Failed to read current IRQ mask.\n",
  305. __func__);
  306. return error;
  307. }
  308. error = rmi_driver_process_reset_requests(rmi_dev);
  309. if (error < 0)
  310. return error;
  311. error = rmi_driver_process_config_requests(rmi_dev);
  312. if (error < 0)
  313. return error;
  314. return 0;
  315. }
  316. int rmi_read_pdt_entry(struct rmi_device *rmi_dev, struct pdt_entry *entry,
  317. u16 pdt_address)
  318. {
  319. u8 buf[RMI_PDT_ENTRY_SIZE];
  320. int error;
  321. error = rmi_read_block(rmi_dev, pdt_address, buf, RMI_PDT_ENTRY_SIZE);
  322. if (error) {
  323. dev_err(&rmi_dev->dev, "Read PDT entry at %#06x failed, code: %d.\n",
  324. pdt_address, error);
  325. return error;
  326. }
  327. entry->page_start = pdt_address & RMI4_PAGE_MASK;
  328. entry->query_base_addr = buf[0];
  329. entry->command_base_addr = buf[1];
  330. entry->control_base_addr = buf[2];
  331. entry->data_base_addr = buf[3];
  332. entry->interrupt_source_count = buf[4] & RMI_PDT_INT_SOURCE_COUNT_MASK;
  333. entry->function_version = (buf[4] & RMI_PDT_FUNCTION_VERSION_MASK) >> 5;
  334. entry->function_number = buf[5];
  335. return 0;
  336. }
  337. EXPORT_SYMBOL_GPL(rmi_read_pdt_entry);
  338. static void rmi_driver_copy_pdt_to_fd(const struct pdt_entry *pdt,
  339. struct rmi_function_descriptor *fd)
  340. {
  341. fd->query_base_addr = pdt->query_base_addr + pdt->page_start;
  342. fd->command_base_addr = pdt->command_base_addr + pdt->page_start;
  343. fd->control_base_addr = pdt->control_base_addr + pdt->page_start;
  344. fd->data_base_addr = pdt->data_base_addr + pdt->page_start;
  345. fd->function_number = pdt->function_number;
  346. fd->interrupt_source_count = pdt->interrupt_source_count;
  347. fd->function_version = pdt->function_version;
  348. }
  349. #define RMI_SCAN_CONTINUE 0
  350. #define RMI_SCAN_DONE 1
  351. static int rmi_scan_pdt_page(struct rmi_device *rmi_dev,
  352. int page,
  353. void *ctx,
  354. int (*callback)(struct rmi_device *rmi_dev,
  355. void *ctx,
  356. const struct pdt_entry *entry))
  357. {
  358. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  359. struct pdt_entry pdt_entry;
  360. u16 page_start = RMI4_PAGE_SIZE * page;
  361. u16 pdt_start = page_start + PDT_START_SCAN_LOCATION;
  362. u16 pdt_end = page_start + PDT_END_SCAN_LOCATION;
  363. u16 addr;
  364. int error;
  365. int retval;
  366. for (addr = pdt_start; addr >= pdt_end; addr -= RMI_PDT_ENTRY_SIZE) {
  367. error = rmi_read_pdt_entry(rmi_dev, &pdt_entry, addr);
  368. if (error)
  369. return error;
  370. if (RMI4_END_OF_PDT(pdt_entry.function_number))
  371. break;
  372. retval = callback(rmi_dev, ctx, &pdt_entry);
  373. if (retval != RMI_SCAN_CONTINUE)
  374. return retval;
  375. }
  376. return (data->f01_bootloader_mode || addr == pdt_start) ?
  377. RMI_SCAN_DONE : RMI_SCAN_CONTINUE;
  378. }
  379. static int rmi_scan_pdt(struct rmi_device *rmi_dev, void *ctx,
  380. int (*callback)(struct rmi_device *rmi_dev,
  381. void *ctx,
  382. const struct pdt_entry *entry))
  383. {
  384. int page;
  385. int retval = RMI_SCAN_DONE;
  386. for (page = 0; page <= RMI4_MAX_PAGE; page++) {
  387. retval = rmi_scan_pdt_page(rmi_dev, page, ctx, callback);
  388. if (retval != RMI_SCAN_CONTINUE)
  389. break;
  390. }
  391. return retval < 0 ? retval : 0;
  392. }
  393. int rmi_read_register_desc(struct rmi_device *d, u16 addr,
  394. struct rmi_register_descriptor *rdesc)
  395. {
  396. int ret;
  397. u8 size_presence_reg;
  398. u8 buf[35];
  399. int presense_offset = 1;
  400. u8 *struct_buf;
  401. int reg;
  402. int offset = 0;
  403. int map_offset = 0;
  404. int i;
  405. int b;
  406. /*
  407. * The first register of the register descriptor is the size of
  408. * the register descriptor's presense register.
  409. */
  410. ret = rmi_read(d, addr, &size_presence_reg);
  411. if (ret)
  412. return ret;
  413. ++addr;
  414. if (size_presence_reg < 0 || size_presence_reg > 35)
  415. return -EIO;
  416. memset(buf, 0, sizeof(buf));
  417. /*
  418. * The presence register contains the size of the register structure
  419. * and a bitmap which identified which packet registers are present
  420. * for this particular register type (ie query, control, or data).
  421. */
  422. ret = rmi_read_block(d, addr, buf, size_presence_reg);
  423. if (ret)
  424. return ret;
  425. ++addr;
  426. if (buf[0] == 0) {
  427. presense_offset = 3;
  428. rdesc->struct_size = buf[1] | (buf[2] << 8);
  429. } else {
  430. rdesc->struct_size = buf[0];
  431. }
  432. for (i = presense_offset; i < size_presence_reg; i++) {
  433. for (b = 0; b < 8; b++) {
  434. if (buf[i] & (0x1 << b))
  435. bitmap_set(rdesc->presense_map, map_offset, 1);
  436. ++map_offset;
  437. }
  438. }
  439. rdesc->num_registers = bitmap_weight(rdesc->presense_map,
  440. RMI_REG_DESC_PRESENSE_BITS);
  441. rdesc->registers = devm_kzalloc(&d->dev, rdesc->num_registers *
  442. sizeof(struct rmi_register_desc_item),
  443. GFP_KERNEL);
  444. if (!rdesc->registers)
  445. return -ENOMEM;
  446. /*
  447. * Allocate a temporary buffer to hold the register structure.
  448. * I'm not using devm_kzalloc here since it will not be retained
  449. * after exiting this function
  450. */
  451. struct_buf = kzalloc(rdesc->struct_size, GFP_KERNEL);
  452. if (!struct_buf)
  453. return -ENOMEM;
  454. /*
  455. * The register structure contains information about every packet
  456. * register of this type. This includes the size of the packet
  457. * register and a bitmap of all subpackets contained in the packet
  458. * register.
  459. */
  460. ret = rmi_read_block(d, addr, struct_buf, rdesc->struct_size);
  461. if (ret)
  462. goto free_struct_buff;
  463. reg = find_first_bit(rdesc->presense_map, RMI_REG_DESC_PRESENSE_BITS);
  464. for (i = 0; i < rdesc->num_registers; i++) {
  465. struct rmi_register_desc_item *item = &rdesc->registers[i];
  466. int reg_size = struct_buf[offset];
  467. ++offset;
  468. if (reg_size == 0) {
  469. reg_size = struct_buf[offset] |
  470. (struct_buf[offset + 1] << 8);
  471. offset += 2;
  472. }
  473. if (reg_size == 0) {
  474. reg_size = struct_buf[offset] |
  475. (struct_buf[offset + 1] << 8) |
  476. (struct_buf[offset + 2] << 16) |
  477. (struct_buf[offset + 3] << 24);
  478. offset += 4;
  479. }
  480. item->reg = reg;
  481. item->reg_size = reg_size;
  482. map_offset = 0;
  483. do {
  484. for (b = 0; b < 7; b++) {
  485. if (struct_buf[offset] & (0x1 << b))
  486. bitmap_set(item->subpacket_map,
  487. map_offset, 1);
  488. ++map_offset;
  489. }
  490. } while (struct_buf[offset++] & 0x80);
  491. item->num_subpackets = bitmap_weight(item->subpacket_map,
  492. RMI_REG_DESC_SUBPACKET_BITS);
  493. rmi_dbg(RMI_DEBUG_CORE, &d->dev,
  494. "%s: reg: %d reg size: %ld subpackets: %d\n", __func__,
  495. item->reg, item->reg_size, item->num_subpackets);
  496. reg = find_next_bit(rdesc->presense_map,
  497. RMI_REG_DESC_PRESENSE_BITS, reg + 1);
  498. }
  499. free_struct_buff:
  500. kfree(struct_buf);
  501. return ret;
  502. }
  503. EXPORT_SYMBOL_GPL(rmi_read_register_desc);
  504. const struct rmi_register_desc_item *rmi_get_register_desc_item(
  505. struct rmi_register_descriptor *rdesc, u16 reg)
  506. {
  507. const struct rmi_register_desc_item *item;
  508. int i;
  509. for (i = 0; i < rdesc->num_registers; i++) {
  510. item = &rdesc->registers[i];
  511. if (item->reg == reg)
  512. return item;
  513. }
  514. return NULL;
  515. }
  516. EXPORT_SYMBOL_GPL(rmi_get_register_desc_item);
  517. size_t rmi_register_desc_calc_size(struct rmi_register_descriptor *rdesc)
  518. {
  519. const struct rmi_register_desc_item *item;
  520. int i;
  521. size_t size = 0;
  522. for (i = 0; i < rdesc->num_registers; i++) {
  523. item = &rdesc->registers[i];
  524. size += item->reg_size;
  525. }
  526. return size;
  527. }
  528. EXPORT_SYMBOL_GPL(rmi_register_desc_calc_size);
  529. /* Compute the register offset relative to the base address */
  530. int rmi_register_desc_calc_reg_offset(
  531. struct rmi_register_descriptor *rdesc, u16 reg)
  532. {
  533. const struct rmi_register_desc_item *item;
  534. int offset = 0;
  535. int i;
  536. for (i = 0; i < rdesc->num_registers; i++) {
  537. item = &rdesc->registers[i];
  538. if (item->reg == reg)
  539. return offset;
  540. ++offset;
  541. }
  542. return -1;
  543. }
  544. EXPORT_SYMBOL_GPL(rmi_register_desc_calc_reg_offset);
  545. bool rmi_register_desc_has_subpacket(const struct rmi_register_desc_item *item,
  546. u8 subpacket)
  547. {
  548. return find_next_bit(item->subpacket_map, RMI_REG_DESC_PRESENSE_BITS,
  549. subpacket) == subpacket;
  550. }
  551. /* Indicates that flash programming is enabled (bootloader mode). */
  552. #define RMI_F01_STATUS_BOOTLOADER(status) (!!((status) & 0x40))
  553. /*
  554. * Given the PDT entry for F01, read the device status register to determine
  555. * if we're stuck in bootloader mode or not.
  556. *
  557. */
  558. static int rmi_check_bootloader_mode(struct rmi_device *rmi_dev,
  559. const struct pdt_entry *pdt)
  560. {
  561. int error;
  562. u8 device_status;
  563. error = rmi_read(rmi_dev, pdt->data_base_addr + pdt->page_start,
  564. &device_status);
  565. if (error) {
  566. dev_err(&rmi_dev->dev,
  567. "Failed to read device status: %d.\n", error);
  568. return error;
  569. }
  570. return RMI_F01_STATUS_BOOTLOADER(device_status);
  571. }
  572. static int rmi_count_irqs(struct rmi_device *rmi_dev,
  573. void *ctx, const struct pdt_entry *pdt)
  574. {
  575. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  576. int *irq_count = ctx;
  577. *irq_count += pdt->interrupt_source_count;
  578. if (pdt->function_number == 0x01) {
  579. data->f01_bootloader_mode =
  580. rmi_check_bootloader_mode(rmi_dev, pdt);
  581. if (data->f01_bootloader_mode)
  582. dev_warn(&rmi_dev->dev,
  583. "WARNING: RMI4 device is in bootloader mode!\n");
  584. }
  585. return RMI_SCAN_CONTINUE;
  586. }
  587. static int rmi_initial_reset(struct rmi_device *rmi_dev,
  588. void *ctx, const struct pdt_entry *pdt)
  589. {
  590. int error;
  591. if (pdt->function_number == 0x01) {
  592. u16 cmd_addr = pdt->page_start + pdt->command_base_addr;
  593. u8 cmd_buf = RMI_DEVICE_RESET_CMD;
  594. const struct rmi_device_platform_data *pdata =
  595. rmi_get_platform_data(rmi_dev);
  596. if (rmi_dev->xport->ops->reset) {
  597. error = rmi_dev->xport->ops->reset(rmi_dev->xport,
  598. cmd_addr);
  599. if (error)
  600. return error;
  601. return RMI_SCAN_DONE;
  602. }
  603. error = rmi_write_block(rmi_dev, cmd_addr, &cmd_buf, 1);
  604. if (error) {
  605. dev_err(&rmi_dev->dev,
  606. "Initial reset failed. Code = %d.\n", error);
  607. return error;
  608. }
  609. mdelay(pdata->reset_delay_ms ?: DEFAULT_RESET_DELAY_MS);
  610. return RMI_SCAN_DONE;
  611. }
  612. /* F01 should always be on page 0. If we don't find it there, fail. */
  613. return pdt->page_start == 0 ? RMI_SCAN_CONTINUE : -ENODEV;
  614. }
  615. static int rmi_create_function(struct rmi_device *rmi_dev,
  616. void *ctx, const struct pdt_entry *pdt)
  617. {
  618. struct device *dev = &rmi_dev->dev;
  619. struct rmi_driver_data *data = dev_get_drvdata(&rmi_dev->dev);
  620. int *current_irq_count = ctx;
  621. struct rmi_function *fn;
  622. int i;
  623. int error;
  624. rmi_dbg(RMI_DEBUG_CORE, dev, "Initializing F%02X.\n",
  625. pdt->function_number);
  626. fn = kzalloc(sizeof(struct rmi_function) +
  627. BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long),
  628. GFP_KERNEL);
  629. if (!fn) {
  630. dev_err(dev, "Failed to allocate memory for F%02X\n",
  631. pdt->function_number);
  632. return -ENOMEM;
  633. }
  634. INIT_LIST_HEAD(&fn->node);
  635. rmi_driver_copy_pdt_to_fd(pdt, &fn->fd);
  636. fn->rmi_dev = rmi_dev;
  637. fn->num_of_irqs = pdt->interrupt_source_count;
  638. fn->irq_pos = *current_irq_count;
  639. *current_irq_count += fn->num_of_irqs;
  640. for (i = 0; i < fn->num_of_irqs; i++)
  641. set_bit(fn->irq_pos + i, fn->irq_mask);
  642. error = rmi_register_function(fn);
  643. if (error)
  644. goto err_put_fn;
  645. if (pdt->function_number == 0x01)
  646. data->f01_container = fn;
  647. list_add_tail(&fn->node, &data->function_list);
  648. return RMI_SCAN_CONTINUE;
  649. err_put_fn:
  650. put_device(&fn->dev);
  651. return error;
  652. }
  653. int rmi_driver_suspend(struct rmi_device *rmi_dev)
  654. {
  655. int retval = 0;
  656. retval = rmi_suspend_functions(rmi_dev);
  657. if (retval)
  658. dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
  659. retval);
  660. return retval;
  661. }
  662. EXPORT_SYMBOL_GPL(rmi_driver_suspend);
  663. int rmi_driver_resume(struct rmi_device *rmi_dev)
  664. {
  665. int retval;
  666. retval = rmi_resume_functions(rmi_dev);
  667. if (retval)
  668. dev_warn(&rmi_dev->dev, "Failed to suspend functions: %d\n",
  669. retval);
  670. return retval;
  671. }
  672. EXPORT_SYMBOL_GPL(rmi_driver_resume);
  673. static int rmi_driver_remove(struct device *dev)
  674. {
  675. struct rmi_device *rmi_dev = to_rmi_device(dev);
  676. rmi_free_function_list(rmi_dev);
  677. return 0;
  678. }
  679. #ifdef CONFIG_OF
  680. static int rmi_driver_of_probe(struct device *dev,
  681. struct rmi_device_platform_data *pdata)
  682. {
  683. int retval;
  684. retval = rmi_of_property_read_u32(dev, &pdata->reset_delay_ms,
  685. "syna,reset-delay-ms", 1);
  686. if (retval)
  687. return retval;
  688. return 0;
  689. }
  690. #else
  691. static inline int rmi_driver_of_probe(struct device *dev,
  692. struct rmi_device_platform_data *pdata)
  693. {
  694. return -ENODEV;
  695. }
  696. #endif
  697. static int rmi_driver_probe(struct device *dev)
  698. {
  699. struct rmi_driver *rmi_driver;
  700. struct rmi_driver_data *data;
  701. struct rmi_device_platform_data *pdata;
  702. struct rmi_device *rmi_dev;
  703. size_t size;
  704. void *irq_memory;
  705. int irq_count;
  706. int retval;
  707. rmi_dbg(RMI_DEBUG_CORE, dev, "%s: Starting probe.\n",
  708. __func__);
  709. if (!rmi_is_physical_device(dev)) {
  710. rmi_dbg(RMI_DEBUG_CORE, dev, "Not a physical device.\n");
  711. return -ENODEV;
  712. }
  713. rmi_dev = to_rmi_device(dev);
  714. rmi_driver = to_rmi_driver(dev->driver);
  715. rmi_dev->driver = rmi_driver;
  716. pdata = rmi_get_platform_data(rmi_dev);
  717. if (rmi_dev->xport->dev->of_node) {
  718. retval = rmi_driver_of_probe(rmi_dev->xport->dev, pdata);
  719. if (retval)
  720. return retval;
  721. }
  722. data = devm_kzalloc(dev, sizeof(struct rmi_driver_data), GFP_KERNEL);
  723. if (!data)
  724. return -ENOMEM;
  725. INIT_LIST_HEAD(&data->function_list);
  726. data->rmi_dev = rmi_dev;
  727. dev_set_drvdata(&rmi_dev->dev, data);
  728. /*
  729. * Right before a warm boot, the sensor might be in some unusual state,
  730. * such as F54 diagnostics, or F34 bootloader mode after a firmware
  731. * or configuration update. In order to clear the sensor to a known
  732. * state and/or apply any updates, we issue a initial reset to clear any
  733. * previous settings and force it into normal operation.
  734. *
  735. * We have to do this before actually building the PDT because
  736. * the reflash updates (if any) might cause various registers to move
  737. * around.
  738. *
  739. * For a number of reasons, this initial reset may fail to return
  740. * within the specified time, but we'll still be able to bring up the
  741. * driver normally after that failure. This occurs most commonly in
  742. * a cold boot situation (where then firmware takes longer to come up
  743. * than from a warm boot) and the reset_delay_ms in the platform data
  744. * has been set too short to accommodate that. Since the sensor will
  745. * eventually come up and be usable, we don't want to just fail here
  746. * and leave the customer's device unusable. So we warn them, and
  747. * continue processing.
  748. */
  749. retval = rmi_scan_pdt(rmi_dev, NULL, rmi_initial_reset);
  750. if (retval < 0)
  751. dev_warn(dev, "RMI initial reset failed! Continuing in spite of this.\n");
  752. retval = rmi_read(rmi_dev, PDT_PROPERTIES_LOCATION, &data->pdt_props);
  753. if (retval < 0) {
  754. /*
  755. * we'll print out a warning and continue since
  756. * failure to get the PDT properties is not a cause to fail
  757. */
  758. dev_warn(dev, "Could not read PDT properties from %#06x (code %d). Assuming 0x00.\n",
  759. PDT_PROPERTIES_LOCATION, retval);
  760. }
  761. /*
  762. * We need to count the IRQs and allocate their storage before scanning
  763. * the PDT and creating the function entries, because adding a new
  764. * function can trigger events that result in the IRQ related storage
  765. * being accessed.
  766. */
  767. rmi_dbg(RMI_DEBUG_CORE, dev, "Counting IRQs.\n");
  768. irq_count = 0;
  769. retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_count_irqs);
  770. if (retval < 0) {
  771. dev_err(dev, "IRQ counting failed with code %d.\n", retval);
  772. goto err;
  773. }
  774. data->irq_count = irq_count;
  775. data->num_of_irq_regs = (data->irq_count + 7) / 8;
  776. mutex_init(&data->irq_mutex);
  777. size = BITS_TO_LONGS(data->irq_count) * sizeof(unsigned long);
  778. irq_memory = devm_kzalloc(dev, size * 4, GFP_KERNEL);
  779. if (!irq_memory) {
  780. dev_err(dev, "Failed to allocate memory for irq masks.\n");
  781. goto err;
  782. }
  783. data->irq_status = irq_memory + size * 0;
  784. data->fn_irq_bits = irq_memory + size * 1;
  785. data->current_irq_mask = irq_memory + size * 2;
  786. data->new_irq_mask = irq_memory + size * 3;
  787. if (rmi_dev->xport->input) {
  788. /*
  789. * The transport driver already has an input device.
  790. * In some cases it is preferable to reuse the transport
  791. * devices input device instead of creating a new one here.
  792. * One example is some HID touchpads report "pass-through"
  793. * button events are not reported by rmi registers.
  794. */
  795. data->input = rmi_dev->xport->input;
  796. } else {
  797. data->input = devm_input_allocate_device(dev);
  798. if (!data->input) {
  799. dev_err(dev, "%s: Failed to allocate input device.\n",
  800. __func__);
  801. retval = -ENOMEM;
  802. goto err_destroy_functions;
  803. }
  804. rmi_driver_set_input_params(rmi_dev, data->input);
  805. data->input->phys = devm_kasprintf(dev, GFP_KERNEL,
  806. "%s/input0", dev_name(dev));
  807. }
  808. irq_count = 0;
  809. rmi_dbg(RMI_DEBUG_CORE, dev, "Creating functions.");
  810. retval = rmi_scan_pdt(rmi_dev, &irq_count, rmi_create_function);
  811. if (retval < 0) {
  812. dev_err(dev, "Function creation failed with code %d.\n",
  813. retval);
  814. goto err_destroy_functions;
  815. }
  816. if (!data->f01_container) {
  817. dev_err(dev, "Missing F01 container!\n");
  818. retval = -EINVAL;
  819. goto err_destroy_functions;
  820. }
  821. retval = rmi_read_block(rmi_dev,
  822. data->f01_container->fd.control_base_addr + 1,
  823. data->current_irq_mask, data->num_of_irq_regs);
  824. if (retval < 0) {
  825. dev_err(dev, "%s: Failed to read current IRQ mask.\n",
  826. __func__);
  827. goto err_destroy_functions;
  828. }
  829. if (data->input) {
  830. rmi_driver_set_input_name(rmi_dev, data->input);
  831. if (!rmi_dev->xport->input) {
  832. if (input_register_device(data->input)) {
  833. dev_err(dev, "%s: Failed to register input device.\n",
  834. __func__);
  835. goto err_destroy_functions;
  836. }
  837. }
  838. }
  839. if (data->f01_container->dev.driver)
  840. /* Driver already bound, so enable ATTN now. */
  841. return enable_sensor(rmi_dev);
  842. return 0;
  843. err_destroy_functions:
  844. rmi_free_function_list(rmi_dev);
  845. err:
  846. return retval < 0 ? retval : 0;
  847. }
  848. static struct rmi_driver rmi_physical_driver = {
  849. .driver = {
  850. .owner = THIS_MODULE,
  851. .name = "rmi4_physical",
  852. .bus = &rmi_bus_type,
  853. .probe = rmi_driver_probe,
  854. .remove = rmi_driver_remove,
  855. },
  856. .reset_handler = rmi_driver_reset_handler,
  857. .clear_irq_bits = rmi_driver_clear_irq_bits,
  858. .set_irq_bits = rmi_driver_set_irq_bits,
  859. .set_input_params = rmi_driver_set_input_params,
  860. };
  861. bool rmi_is_physical_driver(struct device_driver *drv)
  862. {
  863. return drv == &rmi_physical_driver.driver;
  864. }
  865. int __init rmi_register_physical_driver(void)
  866. {
  867. int error;
  868. error = driver_register(&rmi_physical_driver.driver);
  869. if (error) {
  870. pr_err("%s: driver register failed, code=%d.\n", __func__,
  871. error);
  872. return error;
  873. }
  874. return 0;
  875. }
  876. void __exit rmi_unregister_physical_driver(void)
  877. {
  878. driver_unregister(&rmi_physical_driver.driver);
  879. }