mpc52xx_gpt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * MPC5200 General Purpose Timer device driver
  3. *
  4. * Copyright (c) 2009 Secret Lab Technologies Ltd.
  5. * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * This file is a driver for the the General Purpose Timer (gpt) devices
  13. * found on the MPC5200 SoC. Each timer has an IO pin which can be used
  14. * for GPIO or can be used to raise interrupts. The timer function can
  15. * be used independently from the IO pin, or it can be used to control
  16. * output signals or measure input signals.
  17. *
  18. * This driver supports the GPIO and IRQ controller functions of the GPT
  19. * device. Timer functions are not yet supported, nor is the watchdog
  20. * timer.
  21. *
  22. * To use the GPIO function, the following two properties must be added
  23. * to the device tree node for the gpt device (typically in the .dts file
  24. * for the board):
  25. * gpio-controller;
  26. * #gpio-cells = < 2 >;
  27. * This driver will register the GPIO pin if it finds the gpio-controller
  28. * property in the device tree.
  29. *
  30. * To use the IRQ controller function, the following two properties must
  31. * be added to the device tree node for the gpt device:
  32. * interrupt-controller;
  33. * #interrupt-cells = < 1 >;
  34. * The IRQ controller binding only uses one cell to specify the interrupt,
  35. * and the IRQ flags are encoded in the cell. A cell is not used to encode
  36. * the IRQ number because the GPT only has a single IRQ source. For flags,
  37. * a value of '1' means rising edge sensitive and '2' means falling edge.
  38. *
  39. * The GPIO and the IRQ controller functions can be used at the same time,
  40. * but in this use case the IO line will only work as an input. Trying to
  41. * use it as a GPIO output will not work.
  42. *
  43. * When using the GPIO line as an output, it can either be driven as normal
  44. * IO, or it can be an Open Collector (OC) output. At the moment it is the
  45. * responsibility of either the bootloader or the platform setup code to set
  46. * the output mode. This driver does not change the output mode setting.
  47. */
  48. #include <linux/irq.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/io.h>
  51. #include <linux/of.h>
  52. #include <linux/of_platform.h>
  53. #include <linux/of_gpio.h>
  54. #include <linux/kernel.h>
  55. #include <asm/mpc52xx.h>
  56. MODULE_DESCRIPTION("Freescale MPC52xx gpt driver");
  57. MODULE_AUTHOR("Sascha Hauer, Grant Likely");
  58. MODULE_LICENSE("GPL");
  59. /**
  60. * struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver
  61. * @dev: pointer to device structure
  62. * @regs: virtual address of GPT registers
  63. * @lock: spinlock to coordinate between different functions.
  64. * @of_gc: of_gpio_chip instance structure; used when GPIO is enabled
  65. * @irqhost: Pointer to irq_host instance; used when IRQ mode is supported
  66. */
  67. struct mpc52xx_gpt_priv {
  68. struct device *dev;
  69. struct mpc52xx_gpt __iomem *regs;
  70. spinlock_t lock;
  71. struct irq_host *irqhost;
  72. #if defined(CONFIG_GPIOLIB)
  73. struct of_gpio_chip of_gc;
  74. #endif
  75. };
  76. #define MPC52xx_GPT_MODE_MS_MASK (0x07)
  77. #define MPC52xx_GPT_MODE_MS_IC (0x01)
  78. #define MPC52xx_GPT_MODE_MS_OC (0x02)
  79. #define MPC52xx_GPT_MODE_MS_PWM (0x03)
  80. #define MPC52xx_GPT_MODE_MS_GPIO (0x04)
  81. #define MPC52xx_GPT_MODE_GPIO_MASK (0x30)
  82. #define MPC52xx_GPT_MODE_GPIO_OUT_LOW (0x20)
  83. #define MPC52xx_GPT_MODE_GPIO_OUT_HIGH (0x30)
  84. #define MPC52xx_GPT_MODE_IRQ_EN (0x0100)
  85. #define MPC52xx_GPT_MODE_ICT_MASK (0x030000)
  86. #define MPC52xx_GPT_MODE_ICT_RISING (0x010000)
  87. #define MPC52xx_GPT_MODE_ICT_FALLING (0x020000)
  88. #define MPC52xx_GPT_MODE_ICT_TOGGLE (0x030000)
  89. #define MPC52xx_GPT_STATUS_IRQMASK (0x000f)
  90. /* ---------------------------------------------------------------------
  91. * Cascaded interrupt controller hooks
  92. */
  93. static void mpc52xx_gpt_irq_unmask(unsigned int virq)
  94. {
  95. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  96. unsigned long flags;
  97. spin_lock_irqsave(&gpt->lock, flags);
  98. setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
  99. spin_unlock_irqrestore(&gpt->lock, flags);
  100. }
  101. static void mpc52xx_gpt_irq_mask(unsigned int virq)
  102. {
  103. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  104. unsigned long flags;
  105. spin_lock_irqsave(&gpt->lock, flags);
  106. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
  107. spin_unlock_irqrestore(&gpt->lock, flags);
  108. }
  109. static void mpc52xx_gpt_irq_ack(unsigned int virq)
  110. {
  111. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  112. out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);
  113. }
  114. static int mpc52xx_gpt_irq_set_type(unsigned int virq, unsigned int flow_type)
  115. {
  116. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  117. unsigned long flags;
  118. u32 reg;
  119. dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, virq, flow_type);
  120. spin_lock_irqsave(&gpt->lock, flags);
  121. reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;
  122. if (flow_type & IRQF_TRIGGER_RISING)
  123. reg |= MPC52xx_GPT_MODE_ICT_RISING;
  124. if (flow_type & IRQF_TRIGGER_FALLING)
  125. reg |= MPC52xx_GPT_MODE_ICT_FALLING;
  126. out_be32(&gpt->regs->mode, reg);
  127. spin_unlock_irqrestore(&gpt->lock, flags);
  128. return 0;
  129. }
  130. static struct irq_chip mpc52xx_gpt_irq_chip = {
  131. .typename = "MPC52xx GPT",
  132. .unmask = mpc52xx_gpt_irq_unmask,
  133. .mask = mpc52xx_gpt_irq_mask,
  134. .ack = mpc52xx_gpt_irq_ack,
  135. .set_type = mpc52xx_gpt_irq_set_type,
  136. };
  137. void mpc52xx_gpt_irq_cascade(unsigned int virq, struct irq_desc *desc)
  138. {
  139. struct mpc52xx_gpt_priv *gpt = get_irq_data(virq);
  140. int sub_virq;
  141. u32 status;
  142. status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;
  143. if (status) {
  144. sub_virq = irq_linear_revmap(gpt->irqhost, 0);
  145. generic_handle_irq(sub_virq);
  146. }
  147. }
  148. static int mpc52xx_gpt_irq_map(struct irq_host *h, unsigned int virq,
  149. irq_hw_number_t hw)
  150. {
  151. struct mpc52xx_gpt_priv *gpt = h->host_data;
  152. dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);
  153. set_irq_chip_data(virq, gpt);
  154. set_irq_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);
  155. return 0;
  156. }
  157. static int mpc52xx_gpt_irq_xlate(struct irq_host *h, struct device_node *ct,
  158. u32 *intspec, unsigned int intsize,
  159. irq_hw_number_t *out_hwirq,
  160. unsigned int *out_flags)
  161. {
  162. struct mpc52xx_gpt_priv *gpt = h->host_data;
  163. dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);
  164. if ((intsize < 1) || (intspec[0] < 1) || (intspec[0] > 3)) {
  165. dev_err(gpt->dev, "bad irq specifier in %s\n", ct->full_name);
  166. return -EINVAL;
  167. }
  168. *out_hwirq = 0; /* The GPT only has 1 IRQ line */
  169. *out_flags = intspec[0];
  170. return 0;
  171. }
  172. static struct irq_host_ops mpc52xx_gpt_irq_ops = {
  173. .map = mpc52xx_gpt_irq_map,
  174. .xlate = mpc52xx_gpt_irq_xlate,
  175. };
  176. static void
  177. mpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
  178. {
  179. int cascade_virq;
  180. unsigned long flags;
  181. /* Only setup cascaded IRQ if device tree claims the GPT is
  182. * an interrupt controller */
  183. if (!of_find_property(node, "interrupt-controller", NULL))
  184. return;
  185. cascade_virq = irq_of_parse_and_map(node, 0);
  186. gpt->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR, 1,
  187. &mpc52xx_gpt_irq_ops, -1);
  188. if (!gpt->irqhost) {
  189. dev_err(gpt->dev, "irq_alloc_host() failed\n");
  190. return;
  191. }
  192. gpt->irqhost->host_data = gpt;
  193. set_irq_data(cascade_virq, gpt);
  194. set_irq_chained_handler(cascade_virq, mpc52xx_gpt_irq_cascade);
  195. /* Set to Input Capture mode */
  196. spin_lock_irqsave(&gpt->lock, flags);
  197. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
  198. MPC52xx_GPT_MODE_MS_IC);
  199. spin_unlock_irqrestore(&gpt->lock, flags);
  200. dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq);
  201. }
  202. /* ---------------------------------------------------------------------
  203. * GPIOLIB hooks
  204. */
  205. #if defined(CONFIG_GPIOLIB)
  206. static inline struct mpc52xx_gpt_priv *gc_to_mpc52xx_gpt(struct gpio_chip *gc)
  207. {
  208. return container_of(to_of_gpio_chip(gc), struct mpc52xx_gpt_priv,of_gc);
  209. }
  210. static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  211. {
  212. struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
  213. return (in_be32(&gpt->regs->status) >> 8) & 1;
  214. }
  215. static void
  216. mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)
  217. {
  218. struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
  219. unsigned long flags;
  220. u32 r;
  221. dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);
  222. r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;
  223. spin_lock_irqsave(&gpt->lock, flags);
  224. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);
  225. spin_unlock_irqrestore(&gpt->lock, flags);
  226. }
  227. static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  228. {
  229. struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
  230. unsigned long flags;
  231. dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);
  232. spin_lock_irqsave(&gpt->lock, flags);
  233. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);
  234. spin_unlock_irqrestore(&gpt->lock, flags);
  235. return 0;
  236. }
  237. static int
  238. mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  239. {
  240. mpc52xx_gpt_gpio_set(gc, gpio, val);
  241. return 0;
  242. }
  243. static void
  244. mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
  245. {
  246. int rc;
  247. /* Only setup GPIO if the device tree claims the GPT is
  248. * a GPIO controller */
  249. if (!of_find_property(node, "gpio-controller", NULL))
  250. return;
  251. gpt->of_gc.gc.label = kstrdup(node->full_name, GFP_KERNEL);
  252. if (!gpt->of_gc.gc.label) {
  253. dev_err(gpt->dev, "out of memory\n");
  254. return;
  255. }
  256. gpt->of_gc.gpio_cells = 2;
  257. gpt->of_gc.gc.ngpio = 1;
  258. gpt->of_gc.gc.direction_input = mpc52xx_gpt_gpio_dir_in;
  259. gpt->of_gc.gc.direction_output = mpc52xx_gpt_gpio_dir_out;
  260. gpt->of_gc.gc.get = mpc52xx_gpt_gpio_get;
  261. gpt->of_gc.gc.set = mpc52xx_gpt_gpio_set;
  262. gpt->of_gc.gc.base = -1;
  263. gpt->of_gc.xlate = of_gpio_simple_xlate;
  264. node->data = &gpt->of_gc;
  265. of_node_get(node);
  266. /* Setup external pin in GPIO mode */
  267. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
  268. MPC52xx_GPT_MODE_MS_GPIO);
  269. rc = gpiochip_add(&gpt->of_gc.gc);
  270. if (rc)
  271. dev_err(gpt->dev, "gpiochip_add() failed; rc=%i\n", rc);
  272. dev_dbg(gpt->dev, "%s() complete.\n", __func__);
  273. }
  274. #else /* defined(CONFIG_GPIOLIB) */
  275. static void
  276. mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }
  277. #endif /* defined(CONFIG_GPIOLIB) */
  278. /* ---------------------------------------------------------------------
  279. * of_platform bus binding code
  280. */
  281. static int __devinit mpc52xx_gpt_probe(struct of_device *ofdev,
  282. const struct of_device_id *match)
  283. {
  284. struct mpc52xx_gpt_priv *gpt;
  285. gpt = kzalloc(sizeof *gpt, GFP_KERNEL);
  286. if (!gpt)
  287. return -ENOMEM;
  288. spin_lock_init(&gpt->lock);
  289. gpt->dev = &ofdev->dev;
  290. gpt->regs = of_iomap(ofdev->node, 0);
  291. if (!gpt->regs) {
  292. kfree(gpt);
  293. return -ENOMEM;
  294. }
  295. dev_set_drvdata(&ofdev->dev, gpt);
  296. mpc52xx_gpt_gpio_setup(gpt, ofdev->node);
  297. mpc52xx_gpt_irq_setup(gpt, ofdev->node);
  298. return 0;
  299. }
  300. static int mpc52xx_gpt_remove(struct of_device *ofdev)
  301. {
  302. return -EBUSY;
  303. }
  304. static const struct of_device_id mpc52xx_gpt_match[] = {
  305. { .compatible = "fsl,mpc5200-gpt", },
  306. /* Depreciated compatible values; don't use for new dts files */
  307. { .compatible = "fsl,mpc5200-gpt-gpio", },
  308. { .compatible = "mpc5200-gpt", },
  309. {}
  310. };
  311. static struct of_platform_driver mpc52xx_gpt_driver = {
  312. .name = "mpc52xx-gpt",
  313. .match_table = mpc52xx_gpt_match,
  314. .probe = mpc52xx_gpt_probe,
  315. .remove = mpc52xx_gpt_remove,
  316. };
  317. static int __init mpc52xx_gpt_init(void)
  318. {
  319. if (of_register_platform_driver(&mpc52xx_gpt_driver))
  320. pr_err("error registering MPC52xx GPT driver\n");
  321. return 0;
  322. }
  323. /* Make sure GPIOs and IRQs get set up before anyone tries to use them */
  324. subsys_initcall(mpc52xx_gpt_init);