gpio-ts4800.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * GPIO driver for the TS-4800 board
  3. *
  4. * Copyright (c) 2016 - Savoir-faire Linux
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/gpio/driver.h>
  11. #include <linux/module.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_device.h>
  14. #include <linux/platform_device.h>
  15. #define DEFAULT_PIN_NUMBER 16
  16. #define INPUT_REG_OFFSET 0x00
  17. #define OUTPUT_REG_OFFSET 0x02
  18. #define DIRECTION_REG_OFFSET 0x04
  19. static int ts4800_gpio_probe(struct platform_device *pdev)
  20. {
  21. struct device_node *node;
  22. struct gpio_chip *chip;
  23. struct resource *res;
  24. void __iomem *base_addr;
  25. int retval;
  26. u32 ngpios;
  27. chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL);
  28. if (!chip)
  29. return -ENOMEM;
  30. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  31. base_addr = devm_ioremap_resource(&pdev->dev, res);
  32. if (IS_ERR(base_addr))
  33. return PTR_ERR(base_addr);
  34. node = pdev->dev.of_node;
  35. if (!node)
  36. return -EINVAL;
  37. retval = of_property_read_u32(node, "ngpios", &ngpios);
  38. if (retval == -EINVAL)
  39. ngpios = DEFAULT_PIN_NUMBER;
  40. else if (retval)
  41. return retval;
  42. retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET,
  43. base_addr + OUTPUT_REG_OFFSET, NULL,
  44. base_addr + DIRECTION_REG_OFFSET, NULL, 0);
  45. if (retval) {
  46. dev_err(&pdev->dev, "bgpio_init failed\n");
  47. return retval;
  48. }
  49. chip->ngpio = ngpios;
  50. platform_set_drvdata(pdev, chip);
  51. return devm_gpiochip_add_data(&pdev->dev, chip, NULL);
  52. }
  53. static const struct of_device_id ts4800_gpio_of_match[] = {
  54. { .compatible = "technologic,ts4800-gpio", },
  55. {},
  56. };
  57. MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match);
  58. static struct platform_driver ts4800_gpio_driver = {
  59. .driver = {
  60. .name = "ts4800-gpio",
  61. .of_match_table = ts4800_gpio_of_match,
  62. },
  63. .probe = ts4800_gpio_probe,
  64. };
  65. module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
  66. MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>");
  67. MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
  68. MODULE_LICENSE("GPL v2");