pwc-rzv2m.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023 Renesas Electronics Corporation
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/gpio/driver.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/reboot.h>
  9. #define PWC_PWCRST 0x00
  10. #define PWC_PWCCKEN 0x04
  11. #define PWC_PWCCTL 0x50
  12. #define PWC_GPIO 0x80
  13. #define PWC_PWCRST_RSTSOFTAX 0x1
  14. #define PWC_PWCCKEN_ENGCKMAIN 0x1
  15. #define PWC_PWCCTL_PWOFF 0x1
  16. struct rzv2m_pwc_priv {
  17. void __iomem *base;
  18. struct device *dev;
  19. struct gpio_chip gp;
  20. DECLARE_BITMAP(ch_en_bits, 2);
  21. };
  22. static void rzv2m_pwc_gpio_set(struct gpio_chip *chip, unsigned int offset,
  23. int value)
  24. {
  25. struct rzv2m_pwc_priv *priv = gpiochip_get_data(chip);
  26. u32 reg;
  27. /* BIT 16 enables write to BIT 0, and BIT 17 enables write to BIT 1 */
  28. reg = BIT(offset + 16);
  29. if (value)
  30. reg |= BIT(offset);
  31. writel(reg, priv->base + PWC_GPIO);
  32. assign_bit(offset, priv->ch_en_bits, value);
  33. }
  34. static int rzv2m_pwc_gpio_get(struct gpio_chip *chip, unsigned int offset)
  35. {
  36. struct rzv2m_pwc_priv *priv = gpiochip_get_data(chip);
  37. return test_bit(offset, priv->ch_en_bits);
  38. }
  39. static int rzv2m_pwc_gpio_direction_output(struct gpio_chip *gc,
  40. unsigned int nr, int value)
  41. {
  42. if (nr > 1)
  43. return -EINVAL;
  44. rzv2m_pwc_gpio_set(gc, nr, value);
  45. return 0;
  46. }
  47. static const struct gpio_chip rzv2m_pwc_gc = {
  48. .label = "gpio_rzv2m_pwc",
  49. .owner = THIS_MODULE,
  50. .get = rzv2m_pwc_gpio_get,
  51. .set = rzv2m_pwc_gpio_set,
  52. .direction_output = rzv2m_pwc_gpio_direction_output,
  53. .can_sleep = false,
  54. .ngpio = 2,
  55. .base = -1,
  56. };
  57. static int rzv2m_pwc_poweroff(struct sys_off_data *data)
  58. {
  59. struct rzv2m_pwc_priv *priv = data->cb_data;
  60. writel(PWC_PWCRST_RSTSOFTAX, priv->base + PWC_PWCRST);
  61. writel(PWC_PWCCKEN_ENGCKMAIN, priv->base + PWC_PWCCKEN);
  62. writel(PWC_PWCCTL_PWOFF, priv->base + PWC_PWCCTL);
  63. mdelay(150);
  64. dev_err(priv->dev, "Failed to power off the system");
  65. return NOTIFY_DONE;
  66. }
  67. static int rzv2m_pwc_probe(struct platform_device *pdev)
  68. {
  69. struct rzv2m_pwc_priv *priv;
  70. int ret;
  71. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  72. if (!priv)
  73. return -ENOMEM;
  74. priv->base = devm_platform_ioremap_resource(pdev, 0);
  75. if (IS_ERR(priv->base))
  76. return PTR_ERR(priv->base);
  77. /*
  78. * The register used by this driver cannot be read, therefore set the
  79. * outputs to their default values and initialize priv->ch_en_bits
  80. * accordingly. BIT 16 enables write to BIT 0, BIT 17 enables write to
  81. * BIT 1, and the default value of both BIT 0 and BIT 1 is 0.
  82. */
  83. writel(BIT(17) | BIT(16), priv->base + PWC_GPIO);
  84. bitmap_zero(priv->ch_en_bits, 2);
  85. priv->gp = rzv2m_pwc_gc;
  86. priv->gp.parent = pdev->dev.parent;
  87. priv->gp.fwnode = dev_fwnode(&pdev->dev);
  88. ret = devm_gpiochip_add_data(&pdev->dev, &priv->gp, priv);
  89. if (ret)
  90. return ret;
  91. if (device_property_read_bool(&pdev->dev, "renesas,rzv2m-pwc-power"))
  92. ret = devm_register_power_off_handler(&pdev->dev,
  93. rzv2m_pwc_poweroff, priv);
  94. return ret;
  95. }
  96. static const struct of_device_id rzv2m_pwc_of_match[] = {
  97. { .compatible = "renesas,rzv2m-pwc" },
  98. { /* sentinel */ }
  99. };
  100. MODULE_DEVICE_TABLE(of, rzv2m_pwc_of_match);
  101. static struct platform_driver rzv2m_pwc_driver = {
  102. .probe = rzv2m_pwc_probe,
  103. .driver = {
  104. .name = "rzv2m_pwc",
  105. .of_match_table = rzv2m_pwc_of_match,
  106. },
  107. };
  108. module_platform_driver(rzv2m_pwc_driver);
  109. MODULE_LICENSE("GPL");
  110. MODULE_AUTHOR("Fabrizio Castro <castro.fabrizio.jz@renesas.com>");
  111. MODULE_DESCRIPTION("Renesas RZ/V2M PWC driver");