gpio-msm-smp2p.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /* drivers/gpio/gpio-msm-smp2p.c
  2. *
  3. * Copyright (c) 2013-2014, 2016 The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/bitmap.h>
  17. #include <linux/of.h>
  18. #include <linux/gpio.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/slab.h>
  23. #include <linux/list.h>
  24. #include <linux/ipc_logging.h>
  25. #include "../soc/qcom/smp2p_private_api.h"
  26. #include "../soc/qcom/smp2p_private.h"
  27. /* GPIO device - one per SMP2P entry. */
  28. struct smp2p_chip_dev {
  29. struct list_head entry_list;
  30. char name[SMP2P_MAX_ENTRY_NAME];
  31. int remote_pid;
  32. bool is_inbound;
  33. bool is_open;
  34. bool in_shadow;
  35. uint32_t shadow_value;
  36. struct work_struct shadow_work;
  37. spinlock_t shadow_lock;
  38. struct notifier_block out_notifier;
  39. struct notifier_block in_notifier;
  40. struct msm_smp2p_out *out_handle;
  41. struct gpio_chip gpio;
  42. struct irq_domain *irq_domain;
  43. int irq_base;
  44. spinlock_t irq_lock;
  45. DECLARE_BITMAP(irq_enabled, SMP2P_BITS_PER_ENTRY);
  46. DECLARE_BITMAP(irq_rising_edge, SMP2P_BITS_PER_ENTRY);
  47. DECLARE_BITMAP(irq_falling_edge, SMP2P_BITS_PER_ENTRY);
  48. };
  49. static struct platform_driver smp2p_gpio_driver;
  50. static struct lock_class_key smp2p_gpio_lock_class;
  51. static struct irq_chip smp2p_gpio_irq_chip;
  52. static DEFINE_SPINLOCK(smp2p_entry_lock_lha1);
  53. static LIST_HEAD(smp2p_entry_list);
  54. /* Used for mapping edge to name for logging. */
  55. static const char * const edge_names[] = {
  56. "-",
  57. "0->1",
  58. "1->0",
  59. "-",
  60. };
  61. /* Used for mapping edge to value for logging. */
  62. static const char * const edge_name_rising[] = {
  63. "-",
  64. "0->1",
  65. };
  66. /* Used for mapping edge to value for logging. */
  67. static const char * const edge_name_falling[] = {
  68. "-",
  69. "1->0",
  70. };
  71. static int smp2p_gpio_to_irq(struct gpio_chip *cp,
  72. unsigned int offset);
  73. /**
  74. * smp2p_get_value - Retrieves GPIO value.
  75. *
  76. * @cp: GPIO chip pointer
  77. * @offset: Pin offset
  78. * @returns: >=0: value of GPIO Pin; < 0 for error
  79. *
  80. * Error codes:
  81. * -ENODEV - chip/entry invalid
  82. * -ENETDOWN - valid entry, but entry not yet created
  83. */
  84. static int smp2p_get_value(struct gpio_chip *cp,
  85. unsigned int offset)
  86. {
  87. struct smp2p_chip_dev *chip;
  88. int ret = 0;
  89. uint32_t data;
  90. if (!cp)
  91. return -ENODEV;
  92. chip = container_of(cp, struct smp2p_chip_dev, gpio);
  93. if (!chip->is_open)
  94. return -ENETDOWN;
  95. if (chip->is_inbound)
  96. ret = msm_smp2p_in_read(chip->remote_pid, chip->name, &data);
  97. else
  98. ret = msm_smp2p_out_read(chip->out_handle, &data);
  99. if (!ret)
  100. ret = (data & (1 << offset)) ? 1 : 0;
  101. return ret;
  102. }
  103. /**
  104. * smp2p_set_value - Sets GPIO value.
  105. *
  106. * @cp: GPIO chip pointer
  107. * @offset: Pin offset
  108. * @value: New value
  109. */
  110. static void smp2p_set_value(struct gpio_chip *cp, unsigned int offset,
  111. int value)
  112. {
  113. struct smp2p_chip_dev *chip;
  114. uint32_t data_set;
  115. uint32_t data_clear;
  116. bool send_irq;
  117. int ret;
  118. unsigned long flags;
  119. if (!cp)
  120. return;
  121. chip = container_of(cp, struct smp2p_chip_dev, gpio);
  122. if (chip->is_inbound) {
  123. SMP2P_INFO("%s: '%s':%d virq %d invalid operation\n",
  124. __func__, chip->name, chip->remote_pid,
  125. chip->irq_base + offset);
  126. return;
  127. }
  128. if (value & SMP2P_GPIO_NO_INT) {
  129. value &= ~SMP2P_GPIO_NO_INT;
  130. send_irq = false;
  131. } else {
  132. send_irq = true;
  133. }
  134. if (value) {
  135. data_set = 1 << offset;
  136. data_clear = 0;
  137. } else {
  138. data_set = 0;
  139. data_clear = 1 << offset;
  140. }
  141. spin_lock_irqsave(&chip->shadow_lock, flags);
  142. if (!chip->is_open) {
  143. chip->in_shadow = true;
  144. chip->shadow_value &= ~data_clear;
  145. chip->shadow_value |= data_set;
  146. spin_unlock_irqrestore(&chip->shadow_lock, flags);
  147. return;
  148. }
  149. if (chip->in_shadow) {
  150. chip->in_shadow = false;
  151. chip->shadow_value &= ~data_clear;
  152. chip->shadow_value |= data_set;
  153. ret = msm_smp2p_out_modify(chip->out_handle,
  154. chip->shadow_value, 0x0, send_irq);
  155. chip->shadow_value = 0x0;
  156. } else {
  157. ret = msm_smp2p_out_modify(chip->out_handle,
  158. data_set, data_clear, send_irq);
  159. }
  160. spin_unlock_irqrestore(&chip->shadow_lock, flags);
  161. if (ret)
  162. SMP2P_GPIO("'%s':%d gpio %d set to %d failed (%d)\n",
  163. chip->name, chip->remote_pid,
  164. chip->gpio.base + offset, value, ret);
  165. else
  166. SMP2P_GPIO("'%s':%d gpio %d set to %d\n",
  167. chip->name, chip->remote_pid,
  168. chip->gpio.base + offset, value);
  169. }
  170. /**
  171. * smp2p_direction_input - Sets GPIO direction to input.
  172. *
  173. * @cp: GPIO chip pointer
  174. * @offset: Pin offset
  175. * @returns: 0 for success; < 0 for failure
  176. */
  177. static int smp2p_direction_input(struct gpio_chip *cp, unsigned int offset)
  178. {
  179. struct smp2p_chip_dev *chip;
  180. if (!cp)
  181. return -ENODEV;
  182. chip = container_of(cp, struct smp2p_chip_dev, gpio);
  183. if (!chip->is_inbound)
  184. return -EPERM;
  185. return 0;
  186. }
  187. /**
  188. * smp2p_direction_output - Sets GPIO direction to output.
  189. *
  190. * @cp: GPIO chip pointer
  191. * @offset: Pin offset
  192. * @value: Direction
  193. * @returns: 0 for success; < 0 for failure
  194. */
  195. static int smp2p_direction_output(struct gpio_chip *cp,
  196. unsigned int offset, int value)
  197. {
  198. struct smp2p_chip_dev *chip;
  199. if (!cp)
  200. return -ENODEV;
  201. chip = container_of(cp, struct smp2p_chip_dev, gpio);
  202. if (chip->is_inbound)
  203. return -EPERM;
  204. return 0;
  205. }
  206. /**
  207. * smp2p_gpio_to_irq - Convert GPIO pin to virtual IRQ pin.
  208. *
  209. * @cp: GPIO chip pointer
  210. * @offset: Pin offset
  211. * @returns: >0 for virtual irq value; < 0 for failure
  212. */
  213. static int smp2p_gpio_to_irq(struct gpio_chip *cp, unsigned int offset)
  214. {
  215. struct smp2p_chip_dev *chip;
  216. chip = container_of(cp, struct smp2p_chip_dev, gpio);
  217. if (!cp || chip->irq_base <= 0)
  218. return -ENODEV;
  219. return chip->irq_base + offset;
  220. }
  221. /**
  222. * smp2p_gpio_irq_mask_helper - Mask/Unmask interrupt.
  223. *
  224. * @d: IRQ data
  225. * @mask: true to mask (disable), false to unmask (enable)
  226. */
  227. static void smp2p_gpio_irq_mask_helper(struct irq_data *d, bool mask)
  228. {
  229. struct smp2p_chip_dev *chip;
  230. int offset;
  231. unsigned long flags;
  232. chip = (struct smp2p_chip_dev *)irq_get_chip_data(d->irq);
  233. if (!chip || chip->irq_base <= 0)
  234. return;
  235. offset = d->irq - chip->irq_base;
  236. spin_lock_irqsave(&chip->irq_lock, flags);
  237. if (mask)
  238. clear_bit(offset, chip->irq_enabled);
  239. else
  240. set_bit(offset, chip->irq_enabled);
  241. spin_unlock_irqrestore(&chip->irq_lock, flags);
  242. }
  243. /**
  244. * smp2p_gpio_irq_mask - Mask interrupt.
  245. *
  246. * @d: IRQ data
  247. */
  248. static void smp2p_gpio_irq_mask(struct irq_data *d)
  249. {
  250. smp2p_gpio_irq_mask_helper(d, true);
  251. }
  252. /**
  253. * smp2p_gpio_irq_unmask - Unmask interrupt.
  254. *
  255. * @d: IRQ data
  256. */
  257. static void smp2p_gpio_irq_unmask(struct irq_data *d)
  258. {
  259. smp2p_gpio_irq_mask_helper(d, false);
  260. }
  261. /**
  262. * smp2p_gpio_irq_set_type - Set interrupt edge type.
  263. *
  264. * @d: IRQ data
  265. * @type: Edge type for interrupt
  266. * @returns 0 for success; < 0 for failure
  267. */
  268. static int smp2p_gpio_irq_set_type(struct irq_data *d, unsigned int type)
  269. {
  270. struct smp2p_chip_dev *chip;
  271. int offset;
  272. unsigned long flags;
  273. int ret = 0;
  274. chip = (struct smp2p_chip_dev *)irq_get_chip_data(d->irq);
  275. if (!chip)
  276. return -ENODEV;
  277. if (chip->irq_base <= 0) {
  278. SMP2P_ERR("%s: '%s':%d virqbase %d invalid\n",
  279. __func__, chip->name, chip->remote_pid,
  280. chip->irq_base);
  281. return -ENODEV;
  282. }
  283. offset = d->irq - chip->irq_base;
  284. spin_lock_irqsave(&chip->irq_lock, flags);
  285. clear_bit(offset, chip->irq_rising_edge);
  286. clear_bit(offset, chip->irq_falling_edge);
  287. switch (type) {
  288. case IRQ_TYPE_EDGE_RISING:
  289. set_bit(offset, chip->irq_rising_edge);
  290. break;
  291. case IRQ_TYPE_EDGE_FALLING:
  292. set_bit(offset, chip->irq_falling_edge);
  293. break;
  294. case IRQ_TYPE_NONE:
  295. case IRQ_TYPE_DEFAULT:
  296. case IRQ_TYPE_EDGE_BOTH:
  297. set_bit(offset, chip->irq_rising_edge);
  298. set_bit(offset, chip->irq_falling_edge);
  299. break;
  300. default:
  301. SMP2P_ERR("%s: unsupported interrupt type 0x%x\n",
  302. __func__, type);
  303. ret = -EINVAL;
  304. break;
  305. }
  306. spin_unlock_irqrestore(&chip->irq_lock, flags);
  307. return ret;
  308. }
  309. /**
  310. * smp2p_irq_map - Creates or updates binding of virtual IRQ
  311. *
  312. * @domain_ptr: Interrupt domain pointer
  313. * @virq: Virtual IRQ
  314. * @hw: Hardware IRQ (same as virq for nomap)
  315. * @returns: 0 for success
  316. */
  317. static int smp2p_irq_map(struct irq_domain *domain_ptr, unsigned int virq,
  318. irq_hw_number_t hw)
  319. {
  320. struct smp2p_chip_dev *chip;
  321. chip = domain_ptr->host_data;
  322. if (!chip) {
  323. SMP2P_ERR("%s: invalid domain ptr\n", __func__);
  324. return -ENODEV;
  325. }
  326. /* map chip structures to device */
  327. irq_set_lockdep_class(virq, &smp2p_gpio_lock_class);
  328. irq_set_chip_and_handler(virq, &smp2p_gpio_irq_chip,
  329. handle_level_irq);
  330. irq_set_chip_data(virq, chip);
  331. return 0;
  332. }
  333. static struct irq_chip smp2p_gpio_irq_chip = {
  334. .name = "smp2p_gpio",
  335. .irq_mask = smp2p_gpio_irq_mask,
  336. .irq_unmask = smp2p_gpio_irq_unmask,
  337. .irq_set_type = smp2p_gpio_irq_set_type,
  338. };
  339. /* No-map interrupt Domain */
  340. static const struct irq_domain_ops smp2p_irq_domain_ops = {
  341. .map = smp2p_irq_map,
  342. };
  343. /**
  344. * msm_summary_irq_handler - Handles inbound entry change notification.
  345. *
  346. * @chip: GPIO chip pointer
  347. * @entry: Change notification data
  348. *
  349. * Whenever an entry changes, this callback is triggered to determine
  350. * which bits changed and if the corresponding interrupts need to be
  351. * triggered.
  352. */
  353. static void msm_summary_irq_handler(struct smp2p_chip_dev *chip,
  354. struct msm_smp2p_update_notif *entry)
  355. {
  356. int i;
  357. uint32_t cur_val;
  358. uint32_t prev_val;
  359. uint32_t edge;
  360. unsigned long flags;
  361. bool trigger_interrupt;
  362. bool irq_rising;
  363. bool irq_falling;
  364. cur_val = entry->current_value;
  365. prev_val = entry->previous_value;
  366. if (chip->irq_base <= 0)
  367. return;
  368. SMP2P_GPIO("'%s':%d GPIO Summary IRQ Change %08x->%08x\n",
  369. chip->name, chip->remote_pid, prev_val, cur_val);
  370. for (i = 0; i < SMP2P_BITS_PER_ENTRY; ++i) {
  371. spin_lock_irqsave(&chip->irq_lock, flags);
  372. trigger_interrupt = false;
  373. edge = (prev_val & 0x1) << 1 | (cur_val & 0x1);
  374. irq_rising = test_bit(i, chip->irq_rising_edge);
  375. irq_falling = test_bit(i, chip->irq_falling_edge);
  376. if (test_bit(i, chip->irq_enabled)) {
  377. if (edge == 0x1 && irq_rising)
  378. /* 0->1 transition */
  379. trigger_interrupt = true;
  380. else if (edge == 0x2 && irq_falling)
  381. /* 1->0 transition */
  382. trigger_interrupt = true;
  383. } else {
  384. SMP2P_GPIO(
  385. "'%s':%d GPIO bit %d virq %d (%s,%s) - edge %s disabled\n",
  386. chip->name, chip->remote_pid, i,
  387. chip->irq_base + i,
  388. edge_name_rising[irq_rising],
  389. edge_name_falling[irq_falling],
  390. edge_names[edge]);
  391. }
  392. spin_unlock_irqrestore(&chip->irq_lock, flags);
  393. if (trigger_interrupt) {
  394. SMP2P_INFO(
  395. "'%s':%d GPIO bit %d virq %d (%s,%s) - edge %s triggering\n",
  396. chip->name, chip->remote_pid, i,
  397. chip->irq_base + i,
  398. edge_name_rising[irq_rising],
  399. edge_name_falling[irq_falling],
  400. edge_names[edge]);
  401. (void)generic_handle_irq(chip->irq_base + i);
  402. }
  403. cur_val >>= 1;
  404. prev_val >>= 1;
  405. }
  406. }
  407. /**
  408. * Adds an interrupt domain based upon the DT node.
  409. *
  410. * @chip: pointer to GPIO chip
  411. * @node: pointer to Device Tree node
  412. */
  413. static void smp2p_add_irq_domain(struct smp2p_chip_dev *chip,
  414. struct device_node *node)
  415. {
  416. int irq_base;
  417. /* map GPIO pins to interrupts */
  418. chip->irq_domain = irq_domain_add_linear(node, SMP2P_BITS_PER_ENTRY,
  419. &smp2p_irq_domain_ops, chip);
  420. if (!chip->irq_domain) {
  421. SMP2P_ERR("%s: unable to create interrupt domain '%s':%d\n",
  422. __func__, chip->name, chip->remote_pid);
  423. goto domain_fail;
  424. }
  425. /* alloc a contiguous set of virt irqs from anywhere in the irq space */
  426. irq_base = irq_alloc_descs_from(0, SMP2P_BITS_PER_ENTRY, of_node_to_nid(
  427. irq_domain_get_of_node(chip->irq_domain)));
  428. if (irq_base < 0) {
  429. SMP2P_ERR("alloc virt irqs failed:%d name:%s pid%d\n", irq_base,
  430. chip->name, chip->remote_pid);
  431. goto irq_alloc_fail;
  432. }
  433. /* map the allocated irqs to gpios */
  434. irq_domain_associate_many(chip->irq_domain, irq_base, 0,
  435. SMP2P_BITS_PER_ENTRY);
  436. chip->irq_base = irq_base;
  437. SMP2P_DBG("create mapping:%d naem:%s pid:%d\n", chip->irq_base,
  438. chip->name, chip->remote_pid);
  439. return;
  440. irq_alloc_fail:
  441. irq_domain_remove(chip->irq_domain);
  442. domain_fail:
  443. return;
  444. }
  445. /**
  446. * Notifier function passed into smp2p API for out bound entries.
  447. *
  448. * @self: Pointer to calling notifier block
  449. * @event: Event
  450. * @data: Event-specific data
  451. * @returns: 0
  452. */
  453. static int smp2p_gpio_out_notify(struct notifier_block *self,
  454. unsigned long event, void *data)
  455. {
  456. struct smp2p_chip_dev *chip;
  457. chip = container_of(self, struct smp2p_chip_dev, out_notifier);
  458. switch (event) {
  459. case SMP2P_OPEN:
  460. chip->is_open = 1;
  461. SMP2P_GPIO("%s: Opened out '%s':%d in_shadow[%d]\n", __func__,
  462. chip->name, chip->remote_pid, chip->in_shadow);
  463. if (chip->in_shadow)
  464. schedule_work(&chip->shadow_work);
  465. break;
  466. case SMP2P_ENTRY_UPDATE:
  467. break;
  468. default:
  469. SMP2P_ERR("%s: Unknown event\n", __func__);
  470. break;
  471. }
  472. return 0;
  473. }
  474. /**
  475. * Notifier function passed into smp2p API for in bound entries.
  476. *
  477. * @self: Pointer to calling notifier block
  478. * @event: Event
  479. * @data: Event-specific data
  480. * @returns: 0
  481. */
  482. static int smp2p_gpio_in_notify(struct notifier_block *self,
  483. unsigned long event, void *data)
  484. {
  485. struct smp2p_chip_dev *chip;
  486. chip = container_of(self, struct smp2p_chip_dev, in_notifier);
  487. switch (event) {
  488. case SMP2P_OPEN:
  489. chip->is_open = 1;
  490. SMP2P_GPIO("%s: Opened in '%s':%d\n", __func__,
  491. chip->name, chip->remote_pid);
  492. break;
  493. case SMP2P_ENTRY_UPDATE:
  494. msm_summary_irq_handler(chip, data);
  495. break;
  496. default:
  497. SMP2P_ERR("%s: Unknown event\n", __func__);
  498. break;
  499. }
  500. return 0;
  501. }
  502. /**
  503. * smp2p_gpio_shadow_worker - Handles shadow updates of an entry.
  504. *
  505. * @work: Work Item scheduled to handle the shadow updates.
  506. */
  507. static void smp2p_gpio_shadow_worker(struct work_struct *work)
  508. {
  509. struct smp2p_chip_dev *chip;
  510. int ret;
  511. unsigned long flags;
  512. chip = container_of(work, struct smp2p_chip_dev, shadow_work);
  513. spin_lock_irqsave(&chip->shadow_lock, flags);
  514. if (chip->in_shadow) {
  515. ret = msm_smp2p_out_modify(chip->out_handle,
  516. chip->shadow_value, 0x0, true);
  517. if (ret)
  518. SMP2P_GPIO("'%s':%d shadow val[0x%x] failed(%d)\n",
  519. chip->name, chip->remote_pid,
  520. chip->shadow_value, ret);
  521. else
  522. SMP2P_GPIO("'%s':%d shadow val[0x%x]\n",
  523. chip->name, chip->remote_pid,
  524. chip->shadow_value);
  525. chip->shadow_value = 0;
  526. chip->in_shadow = false;
  527. }
  528. spin_unlock_irqrestore(&chip->shadow_lock, flags);
  529. }
  530. /**
  531. * Device tree probe function.
  532. *
  533. * @pdev: Pointer to device tree data.
  534. * @returns: 0 on success; -ENODEV otherwise
  535. *
  536. * Called for each smp2pgpio entry in the device tree.
  537. */
  538. static int smp2p_gpio_probe(struct platform_device *pdev)
  539. {
  540. struct device_node *node;
  541. char *key;
  542. struct smp2p_chip_dev *chip;
  543. const char *name_tmp;
  544. unsigned long flags;
  545. bool is_test_entry = false;
  546. int ret;
  547. chip = kzalloc(sizeof(struct smp2p_chip_dev), GFP_KERNEL);
  548. if (!chip) {
  549. SMP2P_ERR("%s: out of memory\n", __func__);
  550. ret = -ENOMEM;
  551. goto fail;
  552. }
  553. spin_lock_init(&chip->irq_lock);
  554. spin_lock_init(&chip->shadow_lock);
  555. INIT_WORK(&chip->shadow_work, smp2p_gpio_shadow_worker);
  556. /* parse device tree */
  557. node = pdev->dev.of_node;
  558. key = "qcom,entry-name";
  559. ret = of_property_read_string(node, key, &name_tmp);
  560. if (ret) {
  561. SMP2P_ERR("%s: missing DT key '%s'\n", __func__, key);
  562. goto fail;
  563. }
  564. strlcpy(chip->name, name_tmp, sizeof(chip->name));
  565. key = "qcom,remote-pid";
  566. ret = of_property_read_u32(node, key, &chip->remote_pid);
  567. if (ret) {
  568. SMP2P_ERR("%s: missing DT key '%s'\n", __func__, key);
  569. goto fail;
  570. }
  571. key = "qcom,is-inbound";
  572. chip->is_inbound = of_property_read_bool(node, key);
  573. /* create virtual GPIO controller */
  574. chip->gpio.label = chip->name;
  575. chip->gpio.parent = &pdev->dev;
  576. chip->gpio.owner = THIS_MODULE;
  577. chip->gpio.direction_input = smp2p_direction_input,
  578. chip->gpio.get = smp2p_get_value;
  579. chip->gpio.direction_output = smp2p_direction_output,
  580. chip->gpio.set = smp2p_set_value;
  581. chip->gpio.to_irq = smp2p_gpio_to_irq,
  582. chip->gpio.base = -1; /* use dynamic GPIO pin allocation */
  583. chip->gpio.ngpio = SMP2P_BITS_PER_ENTRY;
  584. ret = gpiochip_add(&chip->gpio);
  585. if (ret) {
  586. SMP2P_ERR("%s: unable to register GPIO '%s' ret %d\n",
  587. __func__, chip->name, ret);
  588. goto fail;
  589. }
  590. /*
  591. * Test entries opened by GPIO Test conflict with loopback
  592. * support, so the test entries must be explicitly opened
  593. * in the unit test framework.
  594. */
  595. if (strcmp("smp2p", chip->name) == 0)
  596. is_test_entry = true;
  597. if (!chip->is_inbound) {
  598. chip->out_notifier.notifier_call = smp2p_gpio_out_notify;
  599. if (!is_test_entry) {
  600. ret = msm_smp2p_out_open(chip->remote_pid, chip->name,
  601. &chip->out_notifier,
  602. &chip->out_handle);
  603. if (ret < 0)
  604. goto error;
  605. }
  606. } else {
  607. chip->in_notifier.notifier_call = smp2p_gpio_in_notify;
  608. if (!is_test_entry) {
  609. ret = msm_smp2p_in_register(chip->remote_pid,
  610. chip->name,
  611. &chip->in_notifier);
  612. if (ret < 0)
  613. goto error;
  614. }
  615. }
  616. spin_lock_irqsave(&smp2p_entry_lock_lha1, flags);
  617. list_add(&chip->entry_list, &smp2p_entry_list);
  618. spin_unlock_irqrestore(&smp2p_entry_lock_lha1, flags);
  619. /*
  620. * Create interrupt domain - note that chip can't be removed from the
  621. * interrupt domain, so chip cannot be deleted after this point.
  622. */
  623. if (chip->is_inbound)
  624. smp2p_add_irq_domain(chip, node);
  625. else
  626. chip->irq_base = -1;
  627. SMP2P_GPIO("%s: added %s%s entry '%s':%d gpio %d irq %d",
  628. __func__,
  629. is_test_entry ? "test " : "",
  630. chip->is_inbound ? "in" : "out",
  631. chip->name, chip->remote_pid,
  632. chip->gpio.base, chip->irq_base);
  633. return 0;
  634. error:
  635. gpiochip_remove(&chip->gpio);
  636. fail:
  637. kfree(chip);
  638. return ret;
  639. }
  640. /**
  641. * smp2p_gpio_open_close - Opens or closes entry.
  642. *
  643. * @entry: Entry to open or close
  644. * @do_open: true = open port; false = close
  645. */
  646. static void smp2p_gpio_open_close(struct smp2p_chip_dev *entry,
  647. bool do_open)
  648. {
  649. int ret;
  650. if (do_open) {
  651. /* open entry */
  652. if (entry->is_inbound)
  653. ret = msm_smp2p_in_register(entry->remote_pid,
  654. entry->name, &entry->in_notifier);
  655. else
  656. ret = msm_smp2p_out_open(entry->remote_pid,
  657. entry->name, &entry->out_notifier,
  658. &entry->out_handle);
  659. SMP2P_GPIO("%s: opened %s '%s':%d ret %d\n",
  660. __func__,
  661. entry->is_inbound ? "in" : "out",
  662. entry->name, entry->remote_pid,
  663. ret);
  664. } else {
  665. /* close entry */
  666. if (entry->is_inbound)
  667. ret = msm_smp2p_in_unregister(entry->remote_pid,
  668. entry->name, &entry->in_notifier);
  669. else
  670. ret = msm_smp2p_out_close(&entry->out_handle);
  671. entry->is_open = false;
  672. SMP2P_GPIO("%s: closed %s '%s':%d ret %d\n",
  673. __func__,
  674. entry->is_inbound ? "in" : "out",
  675. entry->name, entry->remote_pid, ret);
  676. }
  677. }
  678. /**
  679. * smp2p_gpio_open_test_entry - Opens or closes test entries for unit testing.
  680. *
  681. * @name: Name of the entry
  682. * @remote_pid: Remote processor ID
  683. * @do_open: true = open port; false = close
  684. */
  685. void smp2p_gpio_open_test_entry(const char *name, int remote_pid, bool do_open)
  686. {
  687. struct smp2p_chip_dev *entry;
  688. struct smp2p_chip_dev *start_entry;
  689. unsigned long flags;
  690. spin_lock_irqsave(&smp2p_entry_lock_lha1, flags);
  691. if (list_empty(&smp2p_entry_list)) {
  692. spin_unlock_irqrestore(&smp2p_entry_lock_lha1, flags);
  693. return;
  694. }
  695. start_entry = list_first_entry(&smp2p_entry_list,
  696. struct smp2p_chip_dev,
  697. entry_list);
  698. entry = start_entry;
  699. do {
  700. if (!strcmp(entry->name, name)
  701. && entry->remote_pid == remote_pid) {
  702. /* found entry to change */
  703. spin_unlock_irqrestore(&smp2p_entry_lock_lha1, flags);
  704. smp2p_gpio_open_close(entry, do_open);
  705. spin_lock_irqsave(&smp2p_entry_lock_lha1, flags);
  706. }
  707. list_rotate_left(&smp2p_entry_list);
  708. entry = list_first_entry(&smp2p_entry_list,
  709. struct smp2p_chip_dev,
  710. entry_list);
  711. } while (entry != start_entry);
  712. spin_unlock_irqrestore(&smp2p_entry_lock_lha1, flags);
  713. }
  714. static const struct of_device_id msm_smp2p_match_table[] = {
  715. {.compatible = "qcom,smp2pgpio", },
  716. {},
  717. };
  718. static struct platform_driver smp2p_gpio_driver = {
  719. .probe = smp2p_gpio_probe,
  720. .driver = {
  721. .name = "smp2pgpio",
  722. .owner = THIS_MODULE,
  723. .of_match_table = msm_smp2p_match_table,
  724. },
  725. };
  726. static int smp2p_init(void)
  727. {
  728. INIT_LIST_HEAD(&smp2p_entry_list);
  729. return platform_driver_register(&smp2p_gpio_driver);
  730. }
  731. module_init(smp2p_init);
  732. static void __exit smp2p_exit(void)
  733. {
  734. platform_driver_unregister(&smp2p_gpio_driver);
  735. }
  736. module_exit(smp2p_exit);
  737. MODULE_DESCRIPTION("SMP2P GPIO");
  738. MODULE_LICENSE("GPL v2");