gpio-em.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Emma Mobile GPIO Support - GIO
  3. *
  4. * Copyright (C) 2012 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/irqdomain.h>
  27. #include <linux/bitops.h>
  28. #include <linux/err.h>
  29. #include <linux/gpio.h>
  30. #include <linux/slab.h>
  31. #include <linux/module.h>
  32. #include <linux/platform_data/gpio-em.h>
  33. struct em_gio_priv {
  34. void __iomem *base0;
  35. void __iomem *base1;
  36. unsigned int irq_base;
  37. spinlock_t sense_lock;
  38. struct platform_device *pdev;
  39. struct gpio_chip gpio_chip;
  40. struct irq_chip irq_chip;
  41. struct irq_domain *irq_domain;
  42. };
  43. #define GIO_E1 0x00
  44. #define GIO_E0 0x04
  45. #define GIO_EM 0x04
  46. #define GIO_OL 0x08
  47. #define GIO_OH 0x0c
  48. #define GIO_I 0x10
  49. #define GIO_IIA 0x14
  50. #define GIO_IEN 0x18
  51. #define GIO_IDS 0x1c
  52. #define GIO_IIM 0x1c
  53. #define GIO_RAW 0x20
  54. #define GIO_MST 0x24
  55. #define GIO_IIR 0x28
  56. #define GIO_IDT0 0x40
  57. #define GIO_IDT1 0x44
  58. #define GIO_IDT2 0x48
  59. #define GIO_IDT3 0x4c
  60. #define GIO_RAWBL 0x50
  61. #define GIO_RAWBH 0x54
  62. #define GIO_IRBL 0x58
  63. #define GIO_IRBH 0x5c
  64. #define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
  65. static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
  66. {
  67. if (offs < GIO_IDT0)
  68. return ioread32(p->base0 + offs);
  69. else
  70. return ioread32(p->base1 + (offs - GIO_IDT0));
  71. }
  72. static inline void em_gio_write(struct em_gio_priv *p, int offs,
  73. unsigned long value)
  74. {
  75. if (offs < GIO_IDT0)
  76. iowrite32(value, p->base0 + offs);
  77. else
  78. iowrite32(value, p->base1 + (offs - GIO_IDT0));
  79. }
  80. static inline struct em_gio_priv *irq_to_priv(struct irq_data *d)
  81. {
  82. struct irq_chip *chip = irq_data_get_irq_chip(d);
  83. return container_of(chip, struct em_gio_priv, irq_chip);
  84. }
  85. static void em_gio_irq_disable(struct irq_data *d)
  86. {
  87. struct em_gio_priv *p = irq_to_priv(d);
  88. em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
  89. }
  90. static void em_gio_irq_enable(struct irq_data *d)
  91. {
  92. struct em_gio_priv *p = irq_to_priv(d);
  93. em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
  94. }
  95. #define GIO_ASYNC(x) (x + 8)
  96. static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
  97. [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
  98. [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
  99. [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
  100. [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
  101. [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
  102. };
  103. static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
  104. {
  105. unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
  106. struct em_gio_priv *p = irq_to_priv(d);
  107. unsigned int reg, offset, shift;
  108. unsigned long flags;
  109. unsigned long tmp;
  110. if (!value)
  111. return -EINVAL;
  112. offset = irqd_to_hwirq(d);
  113. pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
  114. /* 8 x 4 bit fields in 4 IDT registers */
  115. reg = GIO_IDT(offset >> 3);
  116. shift = (offset & 0x07) << 4;
  117. spin_lock_irqsave(&p->sense_lock, flags);
  118. /* disable the interrupt in IIA */
  119. tmp = em_gio_read(p, GIO_IIA);
  120. tmp &= ~BIT(offset);
  121. em_gio_write(p, GIO_IIA, tmp);
  122. /* change the sense setting in IDT */
  123. tmp = em_gio_read(p, reg);
  124. tmp &= ~(0xf << shift);
  125. tmp |= value << shift;
  126. em_gio_write(p, reg, tmp);
  127. /* clear pending interrupts */
  128. em_gio_write(p, GIO_IIR, BIT(offset));
  129. /* enable the interrupt in IIA */
  130. tmp = em_gio_read(p, GIO_IIA);
  131. tmp |= BIT(offset);
  132. em_gio_write(p, GIO_IIA, tmp);
  133. spin_unlock_irqrestore(&p->sense_lock, flags);
  134. return 0;
  135. }
  136. static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
  137. {
  138. struct em_gio_priv *p = dev_id;
  139. unsigned long pending;
  140. unsigned int offset, irqs_handled = 0;
  141. while ((pending = em_gio_read(p, GIO_MST))) {
  142. offset = __ffs(pending);
  143. em_gio_write(p, GIO_IIR, BIT(offset));
  144. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  145. irqs_handled++;
  146. }
  147. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  148. }
  149. static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
  150. {
  151. return container_of(chip, struct em_gio_priv, gpio_chip);
  152. }
  153. static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
  154. {
  155. em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
  156. return 0;
  157. }
  158. static int em_gio_get(struct gpio_chip *chip, unsigned offset)
  159. {
  160. return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
  161. }
  162. static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
  163. unsigned shift, int value)
  164. {
  165. /* upper 16 bits contains mask and lower 16 actual value */
  166. em_gio_write(gpio_to_priv(chip), reg,
  167. (1 << (shift + 16)) | (value << shift));
  168. }
  169. static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
  170. {
  171. /* output is split into two registers */
  172. if (offset < 16)
  173. __em_gio_set(chip, GIO_OL, offset, value);
  174. else
  175. __em_gio_set(chip, GIO_OH, offset - 16, value);
  176. }
  177. static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
  178. int value)
  179. {
  180. /* write GPIO value to output before selecting output mode of pin */
  181. em_gio_set(chip, offset, value);
  182. em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
  183. return 0;
  184. }
  185. static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
  186. {
  187. return irq_find_mapping(gpio_to_priv(chip)->irq_domain, offset);
  188. }
  189. static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq,
  190. irq_hw_number_t hw)
  191. {
  192. struct em_gio_priv *p = h->host_data;
  193. pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq);
  194. irq_set_chip_data(virq, h->host_data);
  195. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  196. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  197. return 0;
  198. }
  199. static struct irq_domain_ops em_gio_irq_domain_ops = {
  200. .map = em_gio_irq_domain_map,
  201. };
  202. static int __devinit em_gio_irq_domain_init(struct em_gio_priv *p)
  203. {
  204. struct platform_device *pdev = p->pdev;
  205. struct gpio_em_config *pdata = pdev->dev.platform_data;
  206. p->irq_base = irq_alloc_descs(pdata->irq_base, 0,
  207. pdata->number_of_pins, numa_node_id());
  208. if (p->irq_base < 0) {
  209. dev_err(&pdev->dev, "cannot get irq_desc\n");
  210. return p->irq_base;
  211. }
  212. pr_debug("gio: hw base = %d, nr = %d, sw base = %d\n",
  213. pdata->gpio_base, pdata->number_of_pins, p->irq_base);
  214. p->irq_domain = irq_domain_add_legacy(pdev->dev.of_node,
  215. pdata->number_of_pins,
  216. p->irq_base, 0,
  217. &em_gio_irq_domain_ops, p);
  218. if (!p->irq_domain) {
  219. irq_free_descs(p->irq_base, pdata->number_of_pins);
  220. return -ENXIO;
  221. }
  222. return 0;
  223. }
  224. static void em_gio_irq_domain_cleanup(struct em_gio_priv *p)
  225. {
  226. struct gpio_em_config *pdata = p->pdev->dev.platform_data;
  227. irq_free_descs(p->irq_base, pdata->number_of_pins);
  228. /* FIXME: irq domain wants to be freed! */
  229. }
  230. static int __devinit em_gio_probe(struct platform_device *pdev)
  231. {
  232. struct gpio_em_config *pdata = pdev->dev.platform_data;
  233. struct em_gio_priv *p;
  234. struct resource *io[2], *irq[2];
  235. struct gpio_chip *gpio_chip;
  236. struct irq_chip *irq_chip;
  237. const char *name = dev_name(&pdev->dev);
  238. int ret;
  239. p = kzalloc(sizeof(*p), GFP_KERNEL);
  240. if (!p) {
  241. dev_err(&pdev->dev, "failed to allocate driver data\n");
  242. ret = -ENOMEM;
  243. goto err0;
  244. }
  245. p->pdev = pdev;
  246. platform_set_drvdata(pdev, p);
  247. spin_lock_init(&p->sense_lock);
  248. io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  250. irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  251. irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  252. if (!io[0] || !io[1] || !irq[0] || !irq[1] || !pdata) {
  253. dev_err(&pdev->dev, "missing IRQ, IOMEM or configuration\n");
  254. ret = -EINVAL;
  255. goto err1;
  256. }
  257. p->base0 = ioremap_nocache(io[0]->start, resource_size(io[0]));
  258. if (!p->base0) {
  259. dev_err(&pdev->dev, "failed to remap low I/O memory\n");
  260. ret = -ENXIO;
  261. goto err1;
  262. }
  263. p->base1 = ioremap_nocache(io[1]->start, resource_size(io[1]));
  264. if (!p->base1) {
  265. dev_err(&pdev->dev, "failed to remap high I/O memory\n");
  266. ret = -ENXIO;
  267. goto err2;
  268. }
  269. gpio_chip = &p->gpio_chip;
  270. gpio_chip->direction_input = em_gio_direction_input;
  271. gpio_chip->get = em_gio_get;
  272. gpio_chip->direction_output = em_gio_direction_output;
  273. gpio_chip->set = em_gio_set;
  274. gpio_chip->to_irq = em_gio_to_irq;
  275. gpio_chip->label = name;
  276. gpio_chip->owner = THIS_MODULE;
  277. gpio_chip->base = pdata->gpio_base;
  278. gpio_chip->ngpio = pdata->number_of_pins;
  279. irq_chip = &p->irq_chip;
  280. irq_chip->name = name;
  281. irq_chip->irq_mask = em_gio_irq_disable;
  282. irq_chip->irq_unmask = em_gio_irq_enable;
  283. irq_chip->irq_enable = em_gio_irq_enable;
  284. irq_chip->irq_disable = em_gio_irq_disable;
  285. irq_chip->irq_set_type = em_gio_irq_set_type;
  286. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
  287. ret = em_gio_irq_domain_init(p);
  288. if (ret) {
  289. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  290. goto err3;
  291. }
  292. if (request_irq(irq[0]->start, em_gio_irq_handler, 0, name, p)) {
  293. dev_err(&pdev->dev, "failed to request low IRQ\n");
  294. ret = -ENOENT;
  295. goto err4;
  296. }
  297. if (request_irq(irq[1]->start, em_gio_irq_handler, 0, name, p)) {
  298. dev_err(&pdev->dev, "failed to request high IRQ\n");
  299. ret = -ENOENT;
  300. goto err5;
  301. }
  302. ret = gpiochip_add(gpio_chip);
  303. if (ret) {
  304. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  305. goto err6;
  306. }
  307. return 0;
  308. err6:
  309. free_irq(irq[1]->start, pdev);
  310. err5:
  311. free_irq(irq[0]->start, pdev);
  312. err4:
  313. em_gio_irq_domain_cleanup(p);
  314. err3:
  315. iounmap(p->base1);
  316. err2:
  317. iounmap(p->base0);
  318. err1:
  319. kfree(p);
  320. err0:
  321. return ret;
  322. }
  323. static int __devexit em_gio_remove(struct platform_device *pdev)
  324. {
  325. struct em_gio_priv *p = platform_get_drvdata(pdev);
  326. struct resource *irq[2];
  327. int ret;
  328. ret = gpiochip_remove(&p->gpio_chip);
  329. if (ret)
  330. return ret;
  331. irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  332. irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  333. free_irq(irq[1]->start, pdev);
  334. free_irq(irq[0]->start, pdev);
  335. em_gio_irq_domain_cleanup(p);
  336. iounmap(p->base1);
  337. iounmap(p->base0);
  338. kfree(p);
  339. return 0;
  340. }
  341. static struct platform_driver em_gio_device_driver = {
  342. .probe = em_gio_probe,
  343. .remove = __devexit_p(em_gio_remove),
  344. .driver = {
  345. .name = "em_gio",
  346. }
  347. };
  348. module_platform_driver(em_gio_device_driver);
  349. MODULE_AUTHOR("Magnus Damm");
  350. MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
  351. MODULE_LICENSE("GPL v2");