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