gpio-em.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/pinctrl/consumer.h>
  33. #include <linux/platform_data/gpio-em.h>
  34. struct em_gio_priv {
  35. void __iomem *base0;
  36. void __iomem *base1;
  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 void em_gio_irq_disable(struct irq_data *d)
  81. {
  82. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  83. em_gio_write(p, GIO_IDS, BIT(irqd_to_hwirq(d)));
  84. }
  85. static void em_gio_irq_enable(struct irq_data *d)
  86. {
  87. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  88. em_gio_write(p, GIO_IEN, BIT(irqd_to_hwirq(d)));
  89. }
  90. static int em_gio_irq_reqres(struct irq_data *d)
  91. {
  92. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  93. if (gpiochip_lock_as_irq(&p->gpio_chip, irqd_to_hwirq(d))) {
  94. dev_err(p->gpio_chip.dev,
  95. "unable to lock HW IRQ %lu for IRQ\n",
  96. irqd_to_hwirq(d));
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. static void em_gio_irq_relres(struct irq_data *d)
  102. {
  103. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  104. gpiochip_unlock_as_irq(&p->gpio_chip, irqd_to_hwirq(d));
  105. }
  106. #define GIO_ASYNC(x) (x + 8)
  107. static unsigned char em_gio_sense_table[IRQ_TYPE_SENSE_MASK + 1] = {
  108. [IRQ_TYPE_EDGE_RISING] = GIO_ASYNC(0x00),
  109. [IRQ_TYPE_EDGE_FALLING] = GIO_ASYNC(0x01),
  110. [IRQ_TYPE_LEVEL_HIGH] = GIO_ASYNC(0x02),
  111. [IRQ_TYPE_LEVEL_LOW] = GIO_ASYNC(0x03),
  112. [IRQ_TYPE_EDGE_BOTH] = GIO_ASYNC(0x04),
  113. };
  114. static int em_gio_irq_set_type(struct irq_data *d, unsigned int type)
  115. {
  116. unsigned char value = em_gio_sense_table[type & IRQ_TYPE_SENSE_MASK];
  117. struct em_gio_priv *p = irq_data_get_irq_chip_data(d);
  118. unsigned int reg, offset, shift;
  119. unsigned long flags;
  120. unsigned long tmp;
  121. if (!value)
  122. return -EINVAL;
  123. offset = irqd_to_hwirq(d);
  124. pr_debug("gio: sense irq = %d, mode = %d\n", offset, value);
  125. /* 8 x 4 bit fields in 4 IDT registers */
  126. reg = GIO_IDT(offset >> 3);
  127. shift = (offset & 0x07) << 4;
  128. spin_lock_irqsave(&p->sense_lock, flags);
  129. /* disable 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. /* change the sense setting in IDT */
  134. tmp = em_gio_read(p, reg);
  135. tmp &= ~(0xf << shift);
  136. tmp |= value << shift;
  137. em_gio_write(p, reg, tmp);
  138. /* clear pending interrupts */
  139. em_gio_write(p, GIO_IIR, BIT(offset));
  140. /* enable the interrupt in IIA */
  141. tmp = em_gio_read(p, GIO_IIA);
  142. tmp |= BIT(offset);
  143. em_gio_write(p, GIO_IIA, tmp);
  144. spin_unlock_irqrestore(&p->sense_lock, flags);
  145. return 0;
  146. }
  147. static irqreturn_t em_gio_irq_handler(int irq, void *dev_id)
  148. {
  149. struct em_gio_priv *p = dev_id;
  150. unsigned long pending;
  151. unsigned int offset, irqs_handled = 0;
  152. while ((pending = em_gio_read(p, GIO_MST))) {
  153. offset = __ffs(pending);
  154. em_gio_write(p, GIO_IIR, BIT(offset));
  155. generic_handle_irq(irq_find_mapping(p->irq_domain, offset));
  156. irqs_handled++;
  157. }
  158. return irqs_handled ? IRQ_HANDLED : IRQ_NONE;
  159. }
  160. static inline struct em_gio_priv *gpio_to_priv(struct gpio_chip *chip)
  161. {
  162. return container_of(chip, struct em_gio_priv, gpio_chip);
  163. }
  164. static int em_gio_direction_input(struct gpio_chip *chip, unsigned offset)
  165. {
  166. em_gio_write(gpio_to_priv(chip), GIO_E0, BIT(offset));
  167. return 0;
  168. }
  169. static int em_gio_get(struct gpio_chip *chip, unsigned offset)
  170. {
  171. return (int)(em_gio_read(gpio_to_priv(chip), GIO_I) & BIT(offset));
  172. }
  173. static void __em_gio_set(struct gpio_chip *chip, unsigned int reg,
  174. unsigned shift, int value)
  175. {
  176. /* upper 16 bits contains mask and lower 16 actual value */
  177. em_gio_write(gpio_to_priv(chip), reg,
  178. (BIT(shift + 16)) | (value << shift));
  179. }
  180. static void em_gio_set(struct gpio_chip *chip, unsigned offset, int value)
  181. {
  182. /* output is split into two registers */
  183. if (offset < 16)
  184. __em_gio_set(chip, GIO_OL, offset, value);
  185. else
  186. __em_gio_set(chip, GIO_OH, offset - 16, value);
  187. }
  188. static int em_gio_direction_output(struct gpio_chip *chip, unsigned offset,
  189. int value)
  190. {
  191. /* write GPIO value to output before selecting output mode of pin */
  192. em_gio_set(chip, offset, value);
  193. em_gio_write(gpio_to_priv(chip), GIO_E1, BIT(offset));
  194. return 0;
  195. }
  196. static int em_gio_to_irq(struct gpio_chip *chip, unsigned offset)
  197. {
  198. return irq_create_mapping(gpio_to_priv(chip)->irq_domain, offset);
  199. }
  200. static int em_gio_request(struct gpio_chip *chip, unsigned offset)
  201. {
  202. return pinctrl_request_gpio(chip->base + offset);
  203. }
  204. static void em_gio_free(struct gpio_chip *chip, unsigned offset)
  205. {
  206. pinctrl_free_gpio(chip->base + offset);
  207. /* Set the GPIO as an input to ensure that the next GPIO request won't
  208. * drive the GPIO pin as an output.
  209. */
  210. em_gio_direction_input(chip, offset);
  211. }
  212. static int em_gio_irq_domain_map(struct irq_domain *h, unsigned int irq,
  213. irq_hw_number_t hwirq)
  214. {
  215. struct em_gio_priv *p = h->host_data;
  216. pr_debug("gio: map hw irq = %d, irq = %d\n", (int)hwirq, irq);
  217. irq_set_chip_data(irq, h->host_data);
  218. irq_set_chip_and_handler(irq, &p->irq_chip, handle_level_irq);
  219. set_irq_flags(irq, IRQF_VALID); /* kill me now */
  220. return 0;
  221. }
  222. static const struct irq_domain_ops em_gio_irq_domain_ops = {
  223. .map = em_gio_irq_domain_map,
  224. .xlate = irq_domain_xlate_twocell,
  225. };
  226. static int em_gio_probe(struct platform_device *pdev)
  227. {
  228. struct gpio_em_config pdata_dt;
  229. struct gpio_em_config *pdata = dev_get_platdata(&pdev->dev);
  230. struct em_gio_priv *p;
  231. struct resource *io[2], *irq[2];
  232. struct gpio_chip *gpio_chip;
  233. struct irq_chip *irq_chip;
  234. const char *name = dev_name(&pdev->dev);
  235. int ret;
  236. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  237. if (!p) {
  238. ret = -ENOMEM;
  239. goto err0;
  240. }
  241. p->pdev = pdev;
  242. platform_set_drvdata(pdev, p);
  243. spin_lock_init(&p->sense_lock);
  244. io[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  245. io[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  246. irq[0] = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  247. irq[1] = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  248. if (!io[0] || !io[1] || !irq[0] || !irq[1]) {
  249. dev_err(&pdev->dev, "missing IRQ or IOMEM\n");
  250. ret = -EINVAL;
  251. goto err0;
  252. }
  253. p->base0 = devm_ioremap_nocache(&pdev->dev, io[0]->start,
  254. resource_size(io[0]));
  255. if (!p->base0) {
  256. dev_err(&pdev->dev, "failed to remap low I/O memory\n");
  257. ret = -ENXIO;
  258. goto err0;
  259. }
  260. p->base1 = devm_ioremap_nocache(&pdev->dev, io[1]->start,
  261. resource_size(io[1]));
  262. if (!p->base1) {
  263. dev_err(&pdev->dev, "failed to remap high I/O memory\n");
  264. ret = -ENXIO;
  265. goto err0;
  266. }
  267. if (!pdata) {
  268. memset(&pdata_dt, 0, sizeof(pdata_dt));
  269. pdata = &pdata_dt;
  270. if (of_property_read_u32(pdev->dev.of_node, "ngpios",
  271. &pdata->number_of_pins)) {
  272. dev_err(&pdev->dev, "Missing ngpios OF property\n");
  273. ret = -EINVAL;
  274. goto err0;
  275. }
  276. pdata->gpio_base = -1;
  277. }
  278. gpio_chip = &p->gpio_chip;
  279. gpio_chip->of_node = pdev->dev.of_node;
  280. gpio_chip->direction_input = em_gio_direction_input;
  281. gpio_chip->get = em_gio_get;
  282. gpio_chip->direction_output = em_gio_direction_output;
  283. gpio_chip->set = em_gio_set;
  284. gpio_chip->to_irq = em_gio_to_irq;
  285. gpio_chip->request = em_gio_request;
  286. gpio_chip->free = em_gio_free;
  287. gpio_chip->label = name;
  288. gpio_chip->dev = &pdev->dev;
  289. gpio_chip->owner = THIS_MODULE;
  290. gpio_chip->base = pdata->gpio_base;
  291. gpio_chip->ngpio = pdata->number_of_pins;
  292. irq_chip = &p->irq_chip;
  293. irq_chip->name = name;
  294. irq_chip->irq_mask = em_gio_irq_disable;
  295. irq_chip->irq_unmask = em_gio_irq_enable;
  296. irq_chip->irq_set_type = em_gio_irq_set_type;
  297. irq_chip->irq_request_resources = em_gio_irq_reqres;
  298. irq_chip->irq_release_resources = em_gio_irq_relres;
  299. irq_chip->flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_MASK_ON_SUSPEND;
  300. p->irq_domain = irq_domain_add_simple(pdev->dev.of_node,
  301. pdata->number_of_pins,
  302. pdata->irq_base,
  303. &em_gio_irq_domain_ops, p);
  304. if (!p->irq_domain) {
  305. ret = -ENXIO;
  306. dev_err(&pdev->dev, "cannot initialize irq domain\n");
  307. goto err0;
  308. }
  309. if (devm_request_irq(&pdev->dev, irq[0]->start,
  310. em_gio_irq_handler, 0, name, p)) {
  311. dev_err(&pdev->dev, "failed to request low IRQ\n");
  312. ret = -ENOENT;
  313. goto err1;
  314. }
  315. if (devm_request_irq(&pdev->dev, irq[1]->start,
  316. em_gio_irq_handler, 0, name, p)) {
  317. dev_err(&pdev->dev, "failed to request high IRQ\n");
  318. ret = -ENOENT;
  319. goto err1;
  320. }
  321. ret = gpiochip_add(gpio_chip);
  322. if (ret) {
  323. dev_err(&pdev->dev, "failed to add GPIO controller\n");
  324. goto err1;
  325. }
  326. if (pdata->pctl_name) {
  327. ret = gpiochip_add_pin_range(gpio_chip, pdata->pctl_name, 0,
  328. gpio_chip->base, gpio_chip->ngpio);
  329. if (ret < 0)
  330. dev_warn(&pdev->dev, "failed to add pin range\n");
  331. }
  332. return 0;
  333. err1:
  334. irq_domain_remove(p->irq_domain);
  335. err0:
  336. return ret;
  337. }
  338. static int em_gio_remove(struct platform_device *pdev)
  339. {
  340. struct em_gio_priv *p = platform_get_drvdata(pdev);
  341. gpiochip_remove(&p->gpio_chip);
  342. irq_domain_remove(p->irq_domain);
  343. return 0;
  344. }
  345. static const struct of_device_id em_gio_dt_ids[] = {
  346. { .compatible = "renesas,em-gio", },
  347. {},
  348. };
  349. MODULE_DEVICE_TABLE(of, em_gio_dt_ids);
  350. static struct platform_driver em_gio_device_driver = {
  351. .probe = em_gio_probe,
  352. .remove = em_gio_remove,
  353. .driver = {
  354. .name = "em_gio",
  355. .of_match_table = em_gio_dt_ids,
  356. }
  357. };
  358. static int __init em_gio_init(void)
  359. {
  360. return platform_driver_register(&em_gio_device_driver);
  361. }
  362. postcore_initcall(em_gio_init);
  363. static void __exit em_gio_exit(void)
  364. {
  365. platform_driver_unregister(&em_gio_device_driver);
  366. }
  367. module_exit(em_gio_exit);
  368. MODULE_AUTHOR("Magnus Damm");
  369. MODULE_DESCRIPTION("Renesas Emma Mobile GIO Driver");
  370. MODULE_LICENSE("GPL v2");