gpio-ts4800.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * GPIO driver for the TS-4800 board
  4. *
  5. * Copyright (c) 2016 - Savoir-faire Linux
  6. */
  7. #include <linux/gpio/driver.h>
  8. #include <linux/module.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #define DEFAULT_PIN_NUMBER 16
  13. #define INPUT_REG_OFFSET 0x00
  14. #define OUTPUT_REG_OFFSET 0x02
  15. #define DIRECTION_REG_OFFSET 0x04
  16. static int ts4800_gpio_probe(struct platform_device *pdev)
  17. {
  18. struct device_node *node;
  19. struct gpio_chip *chip;
  20. void __iomem *base_addr;
  21. int retval;
  22. u32 ngpios;
  23. chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL);
  24. if (!chip)
  25. return -ENOMEM;
  26. base_addr = devm_platform_ioremap_resource(pdev, 0);
  27. if (IS_ERR(base_addr))
  28. return PTR_ERR(base_addr);
  29. node = pdev->dev.of_node;
  30. if (!node)
  31. return -EINVAL;
  32. retval = of_property_read_u32(node, "ngpios", &ngpios);
  33. if (retval == -EINVAL)
  34. ngpios = DEFAULT_PIN_NUMBER;
  35. else if (retval)
  36. return retval;
  37. retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET,
  38. base_addr + OUTPUT_REG_OFFSET, NULL,
  39. base_addr + DIRECTION_REG_OFFSET, NULL, 0);
  40. if (retval) {
  41. dev_err(&pdev->dev, "bgpio_init failed\n");
  42. return retval;
  43. }
  44. chip->ngpio = ngpios;
  45. platform_set_drvdata(pdev, chip);
  46. return devm_gpiochip_add_data(&pdev->dev, chip, NULL);
  47. }
  48. static const struct of_device_id ts4800_gpio_of_match[] = {
  49. { .compatible = "technologic,ts4800-gpio", },
  50. {},
  51. };
  52. MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match);
  53. static struct platform_driver ts4800_gpio_driver = {
  54. .driver = {
  55. .name = "ts4800-gpio",
  56. .of_match_table = ts4800_gpio_of_match,
  57. },
  58. .probe = ts4800_gpio_probe,
  59. };
  60. module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
  61. MODULE_AUTHOR("Julien Grossholtz <[email protected]>");
  62. MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
  63. MODULE_LICENSE("GPL v2");