gpio-zynqmp-modepin.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the ps-mode pin configuration.
  4. *
  5. * Copyright (c) 2021 Xilinx, Inc.
  6. */
  7. #include <linux/delay.h>
  8. #include <linux/err.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/firmware/xlnx-zynqmp.h>
  16. /* 4-bit boot mode pins */
  17. #define MODE_PINS 4
  18. /**
  19. * modepin_gpio_get_value - Get the state of the specified pin of GPIO device
  20. * @chip: gpio_chip instance to be worked on
  21. * @pin: gpio pin number within the device
  22. *
  23. * This function reads the state of the specified pin of the GPIO device.
  24. *
  25. * Return: 0 if the pin is low, 1 if pin is high, -EINVAL wrong pin configured
  26. * or error value.
  27. */
  28. static int modepin_gpio_get_value(struct gpio_chip *chip, unsigned int pin)
  29. {
  30. u32 regval = 0;
  31. int ret;
  32. ret = zynqmp_pm_bootmode_read(&regval);
  33. if (ret)
  34. return ret;
  35. /* When [0:3] corresponding bit is set, then read output bit [8:11],
  36. * if the bit is clear then read input bit [4:7] for status or value.
  37. */
  38. if (regval & BIT(pin))
  39. return !!(regval & BIT(pin + 8));
  40. else
  41. return !!(regval & BIT(pin + 4));
  42. }
  43. /**
  44. * modepin_gpio_set_value - Modify the state of the pin with specified value
  45. * @chip: gpio_chip instance to be worked on
  46. * @pin: gpio pin number within the device
  47. * @state: value used to modify the state of the specified pin
  48. *
  49. * This function reads the state of the specified pin of the GPIO device, mask
  50. * with the capture state of GPIO pin, and update pin of GPIO device.
  51. *
  52. * Return: None.
  53. */
  54. static void modepin_gpio_set_value(struct gpio_chip *chip, unsigned int pin,
  55. int state)
  56. {
  57. u32 bootpin_val = 0;
  58. int ret;
  59. zynqmp_pm_bootmode_read(&bootpin_val);
  60. /* Configure pin as an output by set bit [0:3] */
  61. bootpin_val |= BIT(pin);
  62. if (state)
  63. bootpin_val |= BIT(pin + 8);
  64. else
  65. bootpin_val &= ~BIT(pin + 8);
  66. /* Configure bootpin value */
  67. ret = zynqmp_pm_bootmode_write(bootpin_val);
  68. if (ret)
  69. pr_err("modepin: set value error %d for pin %d\n", ret, pin);
  70. }
  71. /**
  72. * modepin_gpio_dir_in - Set the direction of the specified GPIO pin as input
  73. * @chip: gpio_chip instance to be worked on
  74. * @pin: gpio pin number within the device
  75. *
  76. * Return: 0 always
  77. */
  78. static int modepin_gpio_dir_in(struct gpio_chip *chip, unsigned int pin)
  79. {
  80. return 0;
  81. }
  82. /**
  83. * modepin_gpio_dir_out - Set the direction of the specified GPIO pin as output
  84. * @chip: gpio_chip instance to be worked on
  85. * @pin: gpio pin number within the device
  86. * @state: value to be written to specified pin
  87. *
  88. * Return: 0 always
  89. */
  90. static int modepin_gpio_dir_out(struct gpio_chip *chip, unsigned int pin,
  91. int state)
  92. {
  93. return 0;
  94. }
  95. /**
  96. * modepin_gpio_probe - Initialization method for modepin_gpio
  97. * @pdev: platform device instance
  98. *
  99. * Return: 0 on success, negative error otherwise.
  100. */
  101. static int modepin_gpio_probe(struct platform_device *pdev)
  102. {
  103. struct gpio_chip *chip;
  104. int status;
  105. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  106. if (!chip)
  107. return -ENOMEM;
  108. platform_set_drvdata(pdev, chip);
  109. /* configure the gpio chip */
  110. chip->base = -1;
  111. chip->ngpio = MODE_PINS;
  112. chip->owner = THIS_MODULE;
  113. chip->parent = &pdev->dev;
  114. chip->get = modepin_gpio_get_value;
  115. chip->set = modepin_gpio_set_value;
  116. chip->direction_input = modepin_gpio_dir_in;
  117. chip->direction_output = modepin_gpio_dir_out;
  118. chip->label = dev_name(&pdev->dev);
  119. /* modepin gpio registration */
  120. status = devm_gpiochip_add_data(&pdev->dev, chip, chip);
  121. if (status)
  122. return dev_err_probe(&pdev->dev, status,
  123. "Failed to add GPIO chip\n");
  124. return status;
  125. }
  126. static const struct of_device_id modepin_platform_id[] = {
  127. { .compatible = "xlnx,zynqmp-gpio-modepin", },
  128. { }
  129. };
  130. MODULE_DEVICE_TABLE(of, modepin_platform_id);
  131. static struct platform_driver modepin_platform_driver = {
  132. .driver = {
  133. .name = "modepin-gpio",
  134. .of_match_table = modepin_platform_id,
  135. },
  136. .probe = modepin_gpio_probe,
  137. };
  138. module_platform_driver(modepin_platform_driver);
  139. MODULE_AUTHOR("Piyush Mehta <piyush.mehta@xilinx.com>");
  140. MODULE_DESCRIPTION("ZynqMP Boot PS_MODE Configuration");
  141. MODULE_LICENSE("GPL v2");