Lines Matching +full:wdt +full:- +full:gpio

1 // SPDX-License-Identifier: GPL-2.0-or-later
10 * for GPIO or can be used to raise interrupts. The timer function can
14 * This driver supports the GPIO and IRQ controller functions of the GPT
17 * The timer gpt0 can be used as watchdog (wdt). If the wdt mode is used,
19 * -EBUSY). Thus, the safety wdt function always has precedence over the gpt
21 * this means that gpt0 is locked in wdt mode until the next reboot - this
24 * To use the GPIO function, the following two properties must be added
27 * gpio-controller;
28 * #gpio-cells = < 2 >;
29 * This driver will register the GPIO pin if it finds the gpio-controller
34 * interrupt-controller;
35 * #interrupt-cells = < 1 >;
41 * The GPIO and the IRQ controller functions can be used at the same time,
43 * use it as a GPIO output will not work.
45 * When using the GPIO line as an output, it can either be driven as normal
51 #include <linux/gpio/driver.h>
77 * struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver
81 * @gc: gpio_chip instance structure; used when GPIO is enabled
84 * if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates
85 * if the timer is actively used as wdt which blocks gpt functions
133 /* ---------------------------------------------------------------------
142 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_irq_unmask()
143 setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN); in mpc52xx_gpt_irq_unmask()
144 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_irq_unmask()
152 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_irq_mask()
153 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN); in mpc52xx_gpt_irq_mask()
154 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_irq_mask()
161 out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK); in mpc52xx_gpt_irq_ack()
170 dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, d->irq, flow_type); in mpc52xx_gpt_irq_set_type()
172 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_irq_set_type()
173 reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK; in mpc52xx_gpt_irq_set_type()
178 out_be32(&gpt->regs->mode, reg); in mpc52xx_gpt_irq_set_type()
179 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_irq_set_type()
197 status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK; in mpc52xx_gpt_irq_cascade()
199 generic_handle_domain_irq(gpt->irqhost, 0); in mpc52xx_gpt_irq_cascade()
205 struct mpc52xx_gpt_priv *gpt = h->host_data; in mpc52xx_gpt_irq_map()
207 dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq); in mpc52xx_gpt_irq_map()
219 struct mpc52xx_gpt_priv *gpt = h->host_data; in mpc52xx_gpt_irq_xlate()
221 dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]); in mpc52xx_gpt_irq_xlate()
224 dev_err(gpt->dev, "bad irq specifier in %pOF\n", ct); in mpc52xx_gpt_irq_xlate()
225 return -EINVAL; in mpc52xx_gpt_irq_xlate()
250 gpt->irqhost = irq_domain_add_linear(node, 1, &mpc52xx_gpt_irq_ops, gpt); in mpc52xx_gpt_irq_setup()
251 if (!gpt->irqhost) { in mpc52xx_gpt_irq_setup()
252 dev_err(gpt->dev, "irq_domain_add_linear() failed\n"); in mpc52xx_gpt_irq_setup()
260 * Capture mode. If the mode is non-zero, then the pin could be in mpc52xx_gpt_irq_setup()
262 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_irq_setup()
263 mode = in_be32(&gpt->regs->mode); in mpc52xx_gpt_irq_setup()
265 out_be32(&gpt->regs->mode, mode | MPC52xx_GPT_MODE_MS_IC); in mpc52xx_gpt_irq_setup()
266 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_irq_setup()
268 dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq); in mpc52xx_gpt_irq_setup()
272 /* ---------------------------------------------------------------------
276 static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio) in mpc52xx_gpt_gpio_get() argument
280 return (in_be32(&gpt->regs->status) >> 8) & 1; in mpc52xx_gpt_gpio_get()
284 mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v) in mpc52xx_gpt_gpio_set() argument
290 dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v); in mpc52xx_gpt_gpio_set()
293 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_gpio_set()
294 clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r); in mpc52xx_gpt_gpio_set()
295 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_gpio_set()
298 static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio) in mpc52xx_gpt_gpio_dir_in() argument
303 dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio); in mpc52xx_gpt_gpio_dir_in()
305 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_gpio_dir_in()
306 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK); in mpc52xx_gpt_gpio_dir_in()
307 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_gpio_dir_in()
313 mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val) in mpc52xx_gpt_gpio_dir_out() argument
315 mpc52xx_gpt_gpio_set(gc, gpio, val); in mpc52xx_gpt_gpio_dir_out()
323 /* Only setup GPIO if the device claims the GPT is a GPIO controller */ in mpc52xx_gpt_gpio_setup()
324 if (!device_property_present(gpt->dev, "gpio-controller")) in mpc52xx_gpt_gpio_setup()
327 gpt->gc.label = kasprintf(GFP_KERNEL, "%pfw", dev_fwnode(gpt->dev)); in mpc52xx_gpt_gpio_setup()
328 if (!gpt->gc.label) { in mpc52xx_gpt_gpio_setup()
329 dev_err(gpt->dev, "out of memory\n"); in mpc52xx_gpt_gpio_setup()
333 gpt->gc.ngpio = 1; in mpc52xx_gpt_gpio_setup()
334 gpt->gc.direction_input = mpc52xx_gpt_gpio_dir_in; in mpc52xx_gpt_gpio_setup()
335 gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out; in mpc52xx_gpt_gpio_setup()
336 gpt->gc.get = mpc52xx_gpt_gpio_get; in mpc52xx_gpt_gpio_setup()
337 gpt->gc.set = mpc52xx_gpt_gpio_set; in mpc52xx_gpt_gpio_setup()
338 gpt->gc.base = -1; in mpc52xx_gpt_gpio_setup()
339 gpt->gc.parent = gpt->dev; in mpc52xx_gpt_gpio_setup()
341 /* Setup external pin in GPIO mode */ in mpc52xx_gpt_gpio_setup()
342 clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK, in mpc52xx_gpt_gpio_setup()
345 rc = gpiochip_add_data(&gpt->gc, gpt); in mpc52xx_gpt_gpio_setup()
347 dev_err(gpt->dev, "gpiochip_add_data() failed; rc=%i\n", rc); in mpc52xx_gpt_gpio_setup()
349 dev_dbg(gpt->dev, "%s() complete.\n", __func__); in mpc52xx_gpt_gpio_setup()
360 * mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number
372 if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) { in mpc52xx_gpt_from_irq()
403 clocks = period * (u64)gpt->ipb_freq; in mpc52xx_gpt_do_start()
408 return -EINVAL; in mpc52xx_gpt_do_start()
427 return -EINVAL; in mpc52xx_gpt_do_start()
430 /* Set and enable the timer, reject an attempt to use a wdt as gpt */ in mpc52xx_gpt_do_start()
431 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_do_start()
433 gpt->wdt_mode |= MPC52xx_GPT_IS_WDT; in mpc52xx_gpt_do_start()
434 else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) { in mpc52xx_gpt_do_start()
435 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_do_start()
436 return -EBUSY; in mpc52xx_gpt_do_start()
438 out_be32(&gpt->regs->count, prescale << 16 | clocks); in mpc52xx_gpt_do_start()
439 clrsetbits_be32(&gpt->regs->mode, clear, set); in mpc52xx_gpt_do_start()
440 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_do_start()
446 * mpc52xx_gpt_start_timer - Set and enable the GPT timer
461 * mpc52xx_gpt_stop_timer - Stop a gpt
464 * Returns an error if attempting to stop a wdt
471 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_stop_timer()
472 if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) { in mpc52xx_gpt_stop_timer()
473 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_stop_timer()
474 return -EBUSY; in mpc52xx_gpt_stop_timer()
477 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE); in mpc52xx_gpt_stop_timer()
478 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_stop_timer()
484 * mpc52xx_gpt_timer_period - Read the timer period
495 raw_spin_lock_irqsave(&gpt->lock, flags); in mpc52xx_gpt_timer_period()
496 period = in_be32(&gpt->regs->count); in mpc52xx_gpt_timer_period()
497 raw_spin_unlock_irqrestore(&gpt->lock, flags); in mpc52xx_gpt_timer_period()
504 do_div(period, gpt->ipb_freq); in mpc52xx_gpt_timer_period()
519 /* wdt-capable gpt */
522 /* low-level wdt functions */
527 raw_spin_lock_irqsave(&gpt_wdt->lock, flags); in mpc52xx_gpt_wdt_ping()
528 out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING); in mpc52xx_gpt_wdt_ping()
529 raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags); in mpc52xx_gpt_wdt_ping()
532 /* wdt misc device api */
536 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data; in mpc52xx_wdt_write()
549 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data; in mpc52xx_wdt_ioctl()
560 ret = -EFAULT; in mpc52xx_wdt_ioctl()
586 * - timeout requested is 1 second; in mpc52xx_wdt_ioctl()
587 * - real timeout @33MHz is 999997090ns in mpc52xx_wdt_ioctl()
588 * - the int divide by 10^9 will return 0. in mpc52xx_wdt_ioctl()
598 ret = -ENOTTY; in mpc52xx_wdt_ioctl()
609 return -ENODEV; in mpc52xx_wdt_open()
613 return -EBUSY; in mpc52xx_wdt_open()
623 file->private_data = mpc52xx_gpt_wdt; in mpc52xx_wdt_open()
629 /* note: releasing the wdt in NOWAYOUT-mode does not stop it */ in mpc52xx_wdt_release()
631 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data; in mpc52xx_wdt_release()
634 raw_spin_lock_irqsave(&gpt_wdt->lock, flags); in mpc52xx_wdt_release()
635 clrbits32(&gpt_wdt->regs->mode, in mpc52xx_wdt_release()
637 gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT; in mpc52xx_wdt_release()
638 raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags); in mpc52xx_wdt_release()
678 /* remember the gpt for the wdt operation */ in mpc52xx_gpt_wdt_setup()
681 /* configure the wdt if the device tree contained a timeout */ in mpc52xx_gpt_wdt_setup()
687 dev_warn(gpt->dev, "starting as wdt failed\n"); in mpc52xx_gpt_wdt_setup()
689 dev_info(gpt->dev, "watchdog set to %us timeout\n", *period); in mpc52xx_gpt_wdt_setup()
708 /* ---------------------------------------------------------------------
715 gpt = devm_kzalloc(&ofdev->dev, sizeof *gpt, GFP_KERNEL); in mpc52xx_gpt_probe()
717 return -ENOMEM; in mpc52xx_gpt_probe()
719 raw_spin_lock_init(&gpt->lock); in mpc52xx_gpt_probe()
720 gpt->dev = &ofdev->dev; in mpc52xx_gpt_probe()
721 gpt->ipb_freq = mpc5xxx_get_bus_frequency(&ofdev->dev); in mpc52xx_gpt_probe()
722 gpt->regs = of_iomap(ofdev->dev.of_node, 0); in mpc52xx_gpt_probe()
723 if (!gpt->regs) in mpc52xx_gpt_probe()
724 return -ENOMEM; in mpc52xx_gpt_probe()
726 dev_set_drvdata(&ofdev->dev, gpt); in mpc52xx_gpt_probe()
729 mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node); in mpc52xx_gpt_probe()
732 list_add(&gpt->list, &mpc52xx_gpt_list); in mpc52xx_gpt_probe()
736 if (of_property_read_bool(ofdev->dev.of_node, "fsl,has-wdt") || in mpc52xx_gpt_probe()
737 of_property_read_bool(ofdev->dev.of_node, "has-wdt")) { in mpc52xx_gpt_probe()
740 gpt->wdt_mode = MPC52xx_GPT_CAN_WDT; in mpc52xx_gpt_probe()
741 on_boot_wdt = of_get_property(ofdev->dev.of_node, in mpc52xx_gpt_probe()
742 "fsl,wdt-on-boot", NULL); in mpc52xx_gpt_probe()
744 dev_info(gpt->dev, "used as watchdog\n"); in mpc52xx_gpt_probe()
745 gpt->wdt_mode |= MPC52xx_GPT_IS_WDT; in mpc52xx_gpt_probe()
747 dev_info(gpt->dev, "can function as watchdog\n"); in mpc52xx_gpt_probe()
755 { .compatible = "fsl,mpc5200-gpt", },
758 { .compatible = "fsl,mpc5200-gpt-gpio", },
759 { .compatible = "mpc5200-gpt", },
765 .name = "mpc52xx-gpt",