gpio-xgene-sb.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * AppliedMicro X-Gene SoC GPIO-Standby Driver
  3. *
  4. * Copyright (c) 2014, Applied Micro Circuits Corporation
  5. * Author: Tin Huynh <tnhuynh@apm.com>.
  6. * Y Vo <yvo@apm.com>.
  7. * Quan Nguyen <qnguyen@apm.com>.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/io.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/gpio/driver.h>
  27. #include <linux/acpi.h>
  28. #include "gpiolib.h"
  29. /* Common property names */
  30. #define XGENE_NIRQ_PROPERTY "apm,nr-irqs"
  31. #define XGENE_NGPIO_PROPERTY "apm,nr-gpios"
  32. #define XGENE_IRQ_START_PROPERTY "apm,irq-start"
  33. #define XGENE_DFLT_MAX_NGPIO 22
  34. #define XGENE_DFLT_MAX_NIRQ 6
  35. #define XGENE_DFLT_IRQ_START_PIN 8
  36. #define GPIO_MASK(x) (1U << ((x) % 32))
  37. #define MPA_GPIO_INT_LVL 0x0290
  38. #define MPA_GPIO_OE_ADDR 0x029c
  39. #define MPA_GPIO_OUT_ADDR 0x02a0
  40. #define MPA_GPIO_IN_ADDR 0x02a4
  41. #define MPA_GPIO_SEL_LO 0x0294
  42. #define GPIO_INT_LEVEL_H 0x000001
  43. #define GPIO_INT_LEVEL_L 0x000000
  44. /**
  45. * struct xgene_gpio_sb - GPIO-Standby private data structure.
  46. * @gc: memory-mapped GPIO controllers.
  47. * @regs: GPIO register base offset
  48. * @irq_domain: GPIO interrupt domain
  49. * @irq_start: GPIO pin that start support interrupt
  50. * @nirq: Number of GPIO pins that supports interrupt
  51. * @parent_irq_base: Start parent HWIRQ
  52. */
  53. struct xgene_gpio_sb {
  54. struct gpio_chip gc;
  55. void __iomem *regs;
  56. struct irq_domain *irq_domain;
  57. u16 irq_start;
  58. u16 nirq;
  59. u16 parent_irq_base;
  60. };
  61. #define HWIRQ_TO_GPIO(priv, hwirq) ((hwirq) + (priv)->irq_start)
  62. #define GPIO_TO_HWIRQ(priv, gpio) ((gpio) - (priv)->irq_start)
  63. static void xgene_gpio_set_bit(struct gpio_chip *gc,
  64. void __iomem *reg, u32 gpio, int val)
  65. {
  66. u32 data;
  67. data = gc->read_reg(reg);
  68. if (val)
  69. data |= GPIO_MASK(gpio);
  70. else
  71. data &= ~GPIO_MASK(gpio);
  72. gc->write_reg(reg, data);
  73. }
  74. static int xgene_gpio_sb_irq_set_type(struct irq_data *d, unsigned int type)
  75. {
  76. struct xgene_gpio_sb *priv = irq_data_get_irq_chip_data(d);
  77. int gpio = HWIRQ_TO_GPIO(priv, d->hwirq);
  78. int lvl_type = GPIO_INT_LEVEL_H;
  79. switch (type & IRQ_TYPE_SENSE_MASK) {
  80. case IRQ_TYPE_EDGE_RISING:
  81. case IRQ_TYPE_LEVEL_HIGH:
  82. lvl_type = GPIO_INT_LEVEL_H;
  83. break;
  84. case IRQ_TYPE_EDGE_FALLING:
  85. case IRQ_TYPE_LEVEL_LOW:
  86. lvl_type = GPIO_INT_LEVEL_L;
  87. break;
  88. default:
  89. break;
  90. }
  91. xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
  92. gpio * 2, 1);
  93. xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_INT_LVL,
  94. d->hwirq, lvl_type);
  95. /* Propagate IRQ type setting to parent */
  96. if (type & IRQ_TYPE_EDGE_BOTH)
  97. return irq_chip_set_type_parent(d, IRQ_TYPE_EDGE_RISING);
  98. else
  99. return irq_chip_set_type_parent(d, IRQ_TYPE_LEVEL_HIGH);
  100. }
  101. static struct irq_chip xgene_gpio_sb_irq_chip = {
  102. .name = "sbgpio",
  103. .irq_eoi = irq_chip_eoi_parent,
  104. .irq_mask = irq_chip_mask_parent,
  105. .irq_unmask = irq_chip_unmask_parent,
  106. .irq_set_type = xgene_gpio_sb_irq_set_type,
  107. };
  108. static int xgene_gpio_sb_to_irq(struct gpio_chip *gc, u32 gpio)
  109. {
  110. struct xgene_gpio_sb *priv = gpiochip_get_data(gc);
  111. struct irq_fwspec fwspec;
  112. if ((gpio < priv->irq_start) ||
  113. (gpio > HWIRQ_TO_GPIO(priv, priv->nirq)))
  114. return -ENXIO;
  115. fwspec.fwnode = gc->parent->fwnode;
  116. fwspec.param_count = 2;
  117. fwspec.param[0] = GPIO_TO_HWIRQ(priv, gpio);
  118. fwspec.param[1] = IRQ_TYPE_NONE;
  119. return irq_create_fwspec_mapping(&fwspec);
  120. }
  121. static int xgene_gpio_sb_domain_activate(struct irq_domain *d,
  122. struct irq_data *irq_data,
  123. bool reserve)
  124. {
  125. struct xgene_gpio_sb *priv = d->host_data;
  126. u32 gpio = HWIRQ_TO_GPIO(priv, irq_data->hwirq);
  127. if (gpiochip_lock_as_irq(&priv->gc, gpio)) {
  128. dev_err(priv->gc.parent,
  129. "Unable to configure XGene GPIO standby pin %d as IRQ\n",
  130. gpio);
  131. return -ENOSPC;
  132. }
  133. xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
  134. gpio * 2, 1);
  135. return 0;
  136. }
  137. static void xgene_gpio_sb_domain_deactivate(struct irq_domain *d,
  138. struct irq_data *irq_data)
  139. {
  140. struct xgene_gpio_sb *priv = d->host_data;
  141. u32 gpio = HWIRQ_TO_GPIO(priv, irq_data->hwirq);
  142. gpiochip_unlock_as_irq(&priv->gc, gpio);
  143. xgene_gpio_set_bit(&priv->gc, priv->regs + MPA_GPIO_SEL_LO,
  144. gpio * 2, 0);
  145. }
  146. static int xgene_gpio_sb_domain_translate(struct irq_domain *d,
  147. struct irq_fwspec *fwspec,
  148. unsigned long *hwirq,
  149. unsigned int *type)
  150. {
  151. struct xgene_gpio_sb *priv = d->host_data;
  152. if ((fwspec->param_count != 2) ||
  153. (fwspec->param[0] >= priv->nirq))
  154. return -EINVAL;
  155. *hwirq = fwspec->param[0];
  156. *type = fwspec->param[1];
  157. return 0;
  158. }
  159. static int xgene_gpio_sb_domain_alloc(struct irq_domain *domain,
  160. unsigned int virq,
  161. unsigned int nr_irqs, void *data)
  162. {
  163. struct irq_fwspec *fwspec = data;
  164. struct irq_fwspec parent_fwspec;
  165. struct xgene_gpio_sb *priv = domain->host_data;
  166. irq_hw_number_t hwirq;
  167. unsigned int i;
  168. hwirq = fwspec->param[0];
  169. for (i = 0; i < nr_irqs; i++)
  170. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  171. &xgene_gpio_sb_irq_chip, priv);
  172. parent_fwspec.fwnode = domain->parent->fwnode;
  173. if (is_of_node(parent_fwspec.fwnode)) {
  174. parent_fwspec.param_count = 3;
  175. parent_fwspec.param[0] = 0;/* SPI */
  176. /* Skip SGIs and PPIs*/
  177. parent_fwspec.param[1] = hwirq + priv->parent_irq_base - 32;
  178. parent_fwspec.param[2] = fwspec->param[1];
  179. } else if (is_fwnode_irqchip(parent_fwspec.fwnode)) {
  180. parent_fwspec.param_count = 2;
  181. parent_fwspec.param[0] = hwirq + priv->parent_irq_base;
  182. parent_fwspec.param[1] = fwspec->param[1];
  183. } else
  184. return -EINVAL;
  185. return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
  186. &parent_fwspec);
  187. }
  188. static const struct irq_domain_ops xgene_gpio_sb_domain_ops = {
  189. .translate = xgene_gpio_sb_domain_translate,
  190. .alloc = xgene_gpio_sb_domain_alloc,
  191. .free = irq_domain_free_irqs_common,
  192. .activate = xgene_gpio_sb_domain_activate,
  193. .deactivate = xgene_gpio_sb_domain_deactivate,
  194. };
  195. static int xgene_gpio_sb_probe(struct platform_device *pdev)
  196. {
  197. struct xgene_gpio_sb *priv;
  198. int ret;
  199. struct resource *res;
  200. void __iomem *regs;
  201. struct irq_domain *parent_domain = NULL;
  202. u32 val32;
  203. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  204. if (!priv)
  205. return -ENOMEM;
  206. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  207. regs = devm_ioremap_resource(&pdev->dev, res);
  208. if (IS_ERR(regs))
  209. return PTR_ERR(regs);
  210. priv->regs = regs;
  211. ret = platform_get_irq(pdev, 0);
  212. if (ret > 0) {
  213. priv->parent_irq_base = irq_get_irq_data(ret)->hwirq;
  214. parent_domain = irq_get_irq_data(ret)->domain;
  215. }
  216. if (!parent_domain) {
  217. dev_err(&pdev->dev, "unable to obtain parent domain\n");
  218. return -ENODEV;
  219. }
  220. ret = bgpio_init(&priv->gc, &pdev->dev, 4,
  221. regs + MPA_GPIO_IN_ADDR,
  222. regs + MPA_GPIO_OUT_ADDR, NULL,
  223. regs + MPA_GPIO_OE_ADDR, NULL, 0);
  224. if (ret)
  225. return ret;
  226. priv->gc.to_irq = xgene_gpio_sb_to_irq;
  227. /* Retrieve start irq pin, use default if property not found */
  228. priv->irq_start = XGENE_DFLT_IRQ_START_PIN;
  229. if (!device_property_read_u32(&pdev->dev,
  230. XGENE_IRQ_START_PROPERTY, &val32))
  231. priv->irq_start = val32;
  232. /* Retrieve number irqs, use default if property not found */
  233. priv->nirq = XGENE_DFLT_MAX_NIRQ;
  234. if (!device_property_read_u32(&pdev->dev, XGENE_NIRQ_PROPERTY, &val32))
  235. priv->nirq = val32;
  236. /* Retrieve number gpio, use default if property not found */
  237. priv->gc.ngpio = XGENE_DFLT_MAX_NGPIO;
  238. if (!device_property_read_u32(&pdev->dev, XGENE_NGPIO_PROPERTY, &val32))
  239. priv->gc.ngpio = val32;
  240. dev_info(&pdev->dev, "Support %d gpios, %d irqs start from pin %d\n",
  241. priv->gc.ngpio, priv->nirq, priv->irq_start);
  242. platform_set_drvdata(pdev, priv);
  243. priv->irq_domain = irq_domain_create_hierarchy(parent_domain,
  244. 0, priv->nirq, pdev->dev.fwnode,
  245. &xgene_gpio_sb_domain_ops, priv);
  246. if (!priv->irq_domain)
  247. return -ENODEV;
  248. priv->gc.irq.domain = priv->irq_domain;
  249. ret = devm_gpiochip_add_data(&pdev->dev, &priv->gc, priv);
  250. if (ret) {
  251. dev_err(&pdev->dev,
  252. "failed to register X-Gene GPIO Standby driver\n");
  253. irq_domain_remove(priv->irq_domain);
  254. return ret;
  255. }
  256. dev_info(&pdev->dev, "X-Gene GPIO Standby driver registered\n");
  257. if (priv->nirq > 0) {
  258. /* Register interrupt handlers for gpio signaled acpi events */
  259. acpi_gpiochip_request_interrupts(&priv->gc);
  260. }
  261. return ret;
  262. }
  263. static int xgene_gpio_sb_remove(struct platform_device *pdev)
  264. {
  265. struct xgene_gpio_sb *priv = platform_get_drvdata(pdev);
  266. if (priv->nirq > 0) {
  267. acpi_gpiochip_free_interrupts(&priv->gc);
  268. }
  269. irq_domain_remove(priv->irq_domain);
  270. return 0;
  271. }
  272. static const struct of_device_id xgene_gpio_sb_of_match[] = {
  273. {.compatible = "apm,xgene-gpio-sb", },
  274. {},
  275. };
  276. MODULE_DEVICE_TABLE(of, xgene_gpio_sb_of_match);
  277. #ifdef CONFIG_ACPI
  278. static const struct acpi_device_id xgene_gpio_sb_acpi_match[] = {
  279. {"APMC0D15", 0},
  280. {},
  281. };
  282. MODULE_DEVICE_TABLE(acpi, xgene_gpio_sb_acpi_match);
  283. #endif
  284. static struct platform_driver xgene_gpio_sb_driver = {
  285. .driver = {
  286. .name = "xgene-gpio-sb",
  287. .of_match_table = xgene_gpio_sb_of_match,
  288. .acpi_match_table = ACPI_PTR(xgene_gpio_sb_acpi_match),
  289. },
  290. .probe = xgene_gpio_sb_probe,
  291. .remove = xgene_gpio_sb_remove,
  292. };
  293. module_platform_driver(xgene_gpio_sb_driver);
  294. MODULE_AUTHOR("AppliedMicro");
  295. MODULE_DESCRIPTION("APM X-Gene GPIO Standby driver");
  296. MODULE_LICENSE("GPL");