gpio-em.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. spinlock_t sense_lock;
  37. struct platform_device *pdev;
  38. struct gpio_chip gpio_chip;
  39. struct irq_chip irq_chip;
  40. struct irq_domain *irq_domain;
  41. };
  42. #define GIO_E1 0x00
  43. #define GIO_E0 0x04
  44. #define GIO_EM 0x04
  45. #define GIO_OL 0x08
  46. #define GIO_OH 0x0c
  47. #define GIO_I 0x10
  48. #define GIO_IIA 0x14
  49. #define GIO_IEN 0x18
  50. #define GIO_IDS 0x1c
  51. #define GIO_IIM 0x1c
  52. #define GIO_RAW 0x20
  53. #define GIO_MST 0x24
  54. #define GIO_IIR 0x28
  55. #define GIO_IDT0 0x40
  56. #define GIO_IDT1 0x44
  57. #define GIO_IDT2 0x48
  58. #define GIO_IDT3 0x4c
  59. #define GIO_RAWBL 0x50
  60. #define GIO_RAWBH 0x54
  61. #define GIO_IRBL 0x58
  62. #define GIO_IRBH 0x5c
  63. #define GIO_IDT(n) (GIO_IDT0 + ((n) * 4))
  64. static inline unsigned long em_gio_read(struct em_gio_priv *p, int offs)
  65. {
  66. if (offs < GIO_IDT0)
  67. return ioread32(p->base0 + offs);
  68. else
  69. return ioread32(p->base1 + (offs - GIO_IDT0));
  70. }
  71. static inline void em_gio_write(struct em_gio_priv *p, int offs,
  72. unsigned long value)
  73. {
  74. if (offs < GIO_IDT0)
  75. iowrite32(value, p->base0 + offs);
  76. else
  77. iowrite32(value, p->base1 + (offs - GIO_IDT0));
  78. }
  79. static void em_gio_irq_disable(struct irq_data *d)
  80. {
  81. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  82. em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
  83. }
  84. static void em_gio_irq_enable(struct irq_data *d)
  85. {
  86. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  87. em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
  88. }
  89. #define GIO_ASYNC(x) (x + 8)
  90. static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
  91. [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
  92. [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
  93. [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
  94. [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
  95. [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
  96. };
  97. static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
  98. {
  99. unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
  100. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  101. unsigned int reg, offset, shift;
  102. unsigned long flags;
  103. unsigned long tmp;
  104. if (!value)
  105. return -EINVAL;
  106. offset = irqd_to_hwirq(d);
  107. pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
  108. /* 8 x 4 bit fields in 4 IDT registers */
  109. reg = GIO_IDT(offset >> 3);
  110. shift = (offset & 0x07) << 4;
  111. spin_lock_irqsave(&p->sense_lock, flags);
  112. /* disable the interrupt in IIA */
  113. tmp = em_gio_read(p, GIO_IIA);
  114. tmp &= ~BIT(offset);
  115. em_gio_write(p, GIO_IIA, tmp);
  116. /* change the sense setting in IDT */
  117. tmp = em_gio_read(p, reg);
  118. tmp &= ~(0xf << shift);
  119. tmp |= value << shift;
  120. em_gio_write(p, reg, tmp);
  121. /* clear pending interrupts */
  122. em_gio_write(p, GIO_IIR, BIT(offset));
  123. /* enable the interrupt in IIA */
  124. tmp = em_gio_read(p, GIO_IIA);
  125. tmp |= BIT(offset);
  126. em_gio_write(p, GIO_IIA, tmp);
  127. spin_unlock_irqrestore(&p->sense_lock, flags);
  128. return 0;
  129. }
  130. static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
  131. {
  132. struct em_gio_priv *p = dev_id;
  133. unsigned long pending;
  134. unsigned int offset, irqs_handled = 0;
  135. while ((pending = em_gio_read(p, GIO_MST))) {
  136. offset = __ffs(pending);
  137. em_gio_write(p, GIO_IIR, BIT(offset));
  138. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  139. irqs_handled++;
  140. }
  141. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  142. }
  143. static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
  144. {
  145. return container_of(chip, struct em_gio_priv, gpio_chip);
  146. }
  147. static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
  148. {
  149. em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
  150. return 0;
  151. }
  152. static int em_gio_get(struct gpio_chip *chip, unsigned offset)
  153. {
  154. return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
  155. }
  156. static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
  157. unsigned shift, int value)
  158. {
  159. /* upper 16 bits contains mask and lower 16 actual value */
  160. em_gio_write(gpio_to_priv(chip), reg,
  161. (1 << (shift + 16)) | (value << shift));
  162. }
  163. static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
  164. {
  165. /* output is split into two registers */
  166. if (offset < 16)
  167. __em_gio_set(chip, GIO_OL, offset, value);
  168. else
  169. __em_gio_set(chip, GIO_OH, offset - 16, value);
  170. }
  171. static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
  172. int value)
  173. {
  174. /* write GPIO value to output before selecting output mode of pin */
  175. em_gio_set(chip, offset, value);
  176. em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
  177. return 0;
  178. }
  179. static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
  180. {
  181. return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
  182. }
  183. static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int virq,
  184. irq_hw_number_t hw)
  185. {
  186. struct em_gio_priv *p = h->host_data;
  187. pr_debug("gio: map hw irq = %d, virq = %d\n", (int)hw, virq);
  188. irq_set_chip_data(virq, h->host_data);
  189. irq_set_chip_and_handler(virq, &p->irq_chip, handle_level_irq);
  190. set_irq_flags(virq, IRQF_VALID); /* kill me now */
  191. return 0;
  192. }
  193. static struct irq_domain_ops em_gio_irq_domain_ops = {
  194. .map = em_gio_irq_domain_map,
  195. .xlate = irq_domain_xlate_twocell,
  196. };
  197. static int em_gio_probe(struct platform_device *pdev)
  198. {
  199. struct gpio_em_config pdata_dt;
  200. struct gpio_em_config *pdata = pdev->dev.platform_data;
  201. struct em_gio_priv *p;
  202. struct resource *io[2], *irq[2];
  203. struct gpio_chip *gpio_chip;
  204. struct irq_chip *irq_chip;
  205. const char *name = dev_name(&pdev->dev);
  206. int ret;
  207. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  208. if (!p) {
  209. dev_err(&pdev->dev, "failed to allocate driver data\n");
  210. ret = -ENOMEM;
  211. goto err0;
  212. }
  213. p->pdev = pdev;
  214. platform_set_drvdata(pdev, p);
  215. spin_lock_init(&p->sense_lock);
  216. io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  217. io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  218. irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  219. irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  220. if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
  221. dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
  222. ret = -EINVAL;
  223. goto err0;
  224. }
  225. p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
  226. resource_size(io[0]));
  227. if (!p->base0) {
  228. dev_err(&pdev->dev, "failed to remap low I/O memory\n");
  229. ret = -ENXIO;
  230. goto err0;
  231. }
  232. p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
  233. resource_size(io[1]));
  234. if (!p->base1) {
  235. dev_err(&pdev->dev, "failed to remap high I/O memory\n");
  236. ret = -ENXIO;
  237. goto err0;
  238. }
  239. if (!pdata) {
  240. memset(&pdata_dt, 0, sizeof(pdata_dt));
  241. pdata = &pdata_dt;
  242. if (of_property_read_u32(pdev->dev.of_node, "ngpios",
  243. &pdata->number_of_pins)) {
  244. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  245. ret = -EINVAL;
  246. goto err0;
  247. }
  248. ret = of_alias_get_id(pdev->dev.of_node, "gpio");
  249. if (ret < 0) {
  250. dev_err(&pdev->dev, "Couldn't get OF id\n");
  251. goto err0;
  252. }
  253. pdata->gpio_base = ret * 32; /* 32 GPIOs per instance */
  254. }
  255. gpio_chip = &p->gpio_chip;
  256. gpio_chip->direction_input = em_gio_direction_input;
  257. gpio_chip->get = em_gio_get;
  258. gpio_chip->direction_output = em_gio_direction_output;
  259. gpio_chip->set = em_gio_set;
  260. gpio_chip->to_irq = em_gio_to_irq;
  261. gpio_chip->label = name;
  262. gpio_chip->owner = THIS_MODULE;
  263. gpio_chip->base = pdata->gpio_base;
  264. gpio_chip->ngpio = pdata->number_of_pins;
  265. irq_chip = &p->irq_chip;
  266. irq_chip->name = name;
  267. irq_chip->irq_mask = em_gio_irq_disable;
  268. irq_chip->irq_unmask = em_gio_irq_enable;
  269. irq_chip->irq_enable = em_gio_irq_enable;
  270. irq_chip->irq_disable = em_gio_irq_disable;
  271. irq_chip->irq_set_type = em_gio_irq_set_type;
  272. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE;
  273. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  274. pdata->number_of_pins,
  275. pdata->irq_base,
  276. &em_gio_irq_domain_ops, p);
  277. if (!p->irq_domain) {
  278. ret = -ENXIO;
  279. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  280. goto err0;
  281. }
  282. if (devm_request_irq(&pdev->dev, irq[0]->start,
  283. em_gio_irq_handler, 0, name, p)) {
  284. dev_err(&pdev->dev, "failed to request low IRQ\n");
  285. ret = -ENOENT;
  286. goto err1;
  287. }
  288. if (devm_request_irq(&pdev->dev, irq[1]->start,
  289. em_gio_irq_handler, 0, name, p)) {
  290. dev_err(&pdev->dev, "failed to request high IRQ\n");
  291. ret = -ENOENT;
  292. goto err1;
  293. }
  294. ret = gpiochip_add(gpio_chip);
  295. if (ret) {
  296. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  297. goto err1;
  298. }
  299. return 0;
  300. err1:
  301. irq_domain_remove(p->irq_domain);
  302. err0:
  303. return ret;
  304. }
  305. static int em_gio_remove(struct platform_device *pdev)
  306. {
  307. struct em_gio_priv *p = platform_get_drvdata(pdev);
  308. int ret;
  309. ret = gpiochip_remove(&p->gpio_chip);
  310. if (ret)
  311. return ret;
  312. irq_domain_remove(p->irq_domain);
  313. return 0;
  314. }
  315. static const struct of_device_id em_gio_dt_ids[] = {
  316. { .compatible = "renesas,em-gio", },
  317. {},
  318. };
  319. MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
  320. static struct platform_driver em_gio_device_driver = {
  321. .probe = em_gio_probe,
  322. .remove = em_gio_remove,
  323. .driver = {
  324. .name = "em_gio",
  325. .of_match_table = em_gio_dt_ids,
  326. .owner = THIS_MODULE,
  327. }
  328. };
  329. static int __init em_gio_init(void)
  330. {
  331. return platform_driver_register(&em_gio_device_driver);
  332. }
  333. postcore_initcall(em_gio_init);
  334. static void __exit em_gio_exit(void)
  335. {
  336. platform_driver_unregister(&em_gio_device_driver);
  337. }
  338. module_exit(em_gio_exit);
  339. MODULE_AUTHOR("Magnus Damm");
  340. MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
  341. MODULE_LICENSE("GPL v2");