gpio-octeon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2011, 2012 Cavium Inc.
  7. */
  8. #include <linux/platform_device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/gpio.h>
  12. #include <linux/io.h>
  13. #include <asm/octeon/octeon.h>
  14. #include <asm/octeon/cvmx-gpio-defs.h>
  15. #define RX_DAT 0x80
  16. #define TX_SET 0x88
  17. #define TX_CLEAR 0x90
  18. /*
  19. * The address offset of the GPIO configuration register for a given
  20. * line.
  21. */
  22. static unsigned int bit_cfg_reg(unsigned int offset)
  23. {
  24. /*
  25. * The register stride is 8, with a discontinuity after the
  26. * first 16.
  27. */
  28. if (offset < 16)
  29. return 8 * offset;
  30. else
  31. return 8 * (offset - 16) + 0x100;
  32. }
  33. struct octeon_gpio {
  34. struct gpio_chip chip;
  35. u64 register_base;
  36. };
  37. static int octeon_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  38. {
  39. struct octeon_gpio *gpio = container_of(chip, struct octeon_gpio, chip);
  40. cvmx_write_csr(gpio->register_base + bit_cfg_reg(offset), 0);
  41. return 0;
  42. }
  43. static void octeon_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  44. {
  45. struct octeon_gpio *gpio = container_of(chip, struct octeon_gpio, chip);
  46. u64 mask = 1ull << offset;
  47. u64 reg = gpio->register_base + (value ? TX_SET : TX_CLEAR);
  48. cvmx_write_csr(reg, mask);
  49. }
  50. static int octeon_gpio_dir_out(struct gpio_chip *chip, unsigned offset,
  51. int value)
  52. {
  53. struct octeon_gpio *gpio = container_of(chip, struct octeon_gpio, chip);
  54. union cvmx_gpio_bit_cfgx cfgx;
  55. octeon_gpio_set(chip, offset, value);
  56. cfgx.u64 = 0;
  57. cfgx.s.tx_oe = 1;
  58. cvmx_write_csr(gpio->register_base + bit_cfg_reg(offset), cfgx.u64);
  59. return 0;
  60. }
  61. static int octeon_gpio_get(struct gpio_chip *chip, unsigned offset)
  62. {
  63. struct octeon_gpio *gpio = container_of(chip, struct octeon_gpio, chip);
  64. u64 read_bits = cvmx_read_csr(gpio->register_base + RX_DAT);
  65. return ((1ull << offset) & read_bits) != 0;
  66. }
  67. static int octeon_gpio_probe(struct platform_device *pdev)
  68. {
  69. struct octeon_gpio *gpio;
  70. struct gpio_chip *chip;
  71. struct resource *res_mem;
  72. int err = 0;
  73. gpio = devm_kzalloc(&pdev->dev, sizeof(*gpio), GFP_KERNEL);
  74. if (!gpio)
  75. return -ENOMEM;
  76. chip = &gpio->chip;
  77. res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  78. if (res_mem == NULL) {
  79. dev_err(&pdev->dev, "found no memory resource\n");
  80. err = -ENXIO;
  81. goto out;
  82. }
  83. if (!devm_request_mem_region(&pdev->dev, res_mem->start,
  84. resource_size(res_mem),
  85. res_mem->name)) {
  86. dev_err(&pdev->dev, "request_mem_region failed\n");
  87. err = -ENXIO;
  88. goto out;
  89. }
  90. gpio->register_base = (u64)devm_ioremap(&pdev->dev, res_mem->start,
  91. resource_size(res_mem));
  92. pdev->dev.platform_data = chip;
  93. chip->label = "octeon-gpio";
  94. chip->dev = &pdev->dev;
  95. chip->owner = THIS_MODULE;
  96. chip->base = 0;
  97. chip->can_sleep = false;
  98. chip->ngpio = 20;
  99. chip->direction_input = octeon_gpio_dir_in;
  100. chip->get = octeon_gpio_get;
  101. chip->direction_output = octeon_gpio_dir_out;
  102. chip->set = octeon_gpio_set;
  103. err = gpiochip_add(chip);
  104. if (err)
  105. goto out;
  106. dev_info(&pdev->dev, "OCTEON GPIO driver probed.\n");
  107. out:
  108. return err;
  109. }
  110. static int octeon_gpio_remove(struct platform_device *pdev)
  111. {
  112. struct gpio_chip *chip = pdev->dev.platform_data;
  113. gpiochip_remove(chip);
  114. return 0;
  115. }
  116. static struct of_device_id octeon_gpio_match[] = {
  117. {
  118. .compatible = "cavium,octeon-3860-gpio",
  119. },
  120. {},
  121. };
  122. MODULE_DEVICE_TABLE(of, octeon_gpio_match);
  123. static struct platform_driver octeon_gpio_driver = {
  124. .driver = {
  125. .name = "octeon_gpio",
  126. .of_match_table = octeon_gpio_match,
  127. },
  128. .probe = octeon_gpio_probe,
  129. .remove = octeon_gpio_remove,
  130. };
  131. module_platform_driver(octeon_gpio_driver);
  132. MODULE_DESCRIPTION("Cavium Inc. OCTEON GPIO Driver");
  133. MODULE_AUTHOR("David Daney");
  134. MODULE_LICENSE("GPL");