gpio-ts4800.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. void __iomem *base_addr;
  24. int retval;
  25. u32 ngpios;
  26. chip = devm_kzalloc(&pdev->dev, sizeof(struct gpio_chip), GFP_KERNEL);
  27. if (!chip)
  28. return -ENOMEM;
  29. base_addr = devm_platform_ioremap_resource(pdev, 0);
  30. if (IS_ERR(base_addr))
  31. return PTR_ERR(base_addr);
  32. node = pdev->dev.of_node;
  33. if (!node)
  34. return -EINVAL;
  35. retval = of_property_read_u32(node, "ngpios", &ngpios);
  36. if (retval == -EINVAL)
  37. ngpios = DEFAULT_PIN_NUMBER;
  38. else if (retval)
  39. return retval;
  40. retval = bgpio_init(chip, &pdev->dev, 2, base_addr + INPUT_REG_OFFSET,
  41. base_addr + OUTPUT_REG_OFFSET, NULL,
  42. base_addr + DIRECTION_REG_OFFSET, NULL, 0);
  43. if (retval) {
  44. dev_err(&pdev->dev, "bgpio_init failed\n");
  45. return retval;
  46. }
  47. chip->ngpio = ngpios;
  48. platform_set_drvdata(pdev, chip);
  49. return devm_gpiochip_add_data(&pdev->dev, chip, NULL);
  50. }
  51. static const struct of_device_id ts4800_gpio_of_match[] = {
  52. { .compatible = "technologic,ts4800-gpio", },
  53. {},
  54. };
  55. MODULE_DEVICE_TABLE(of, ts4800_gpio_of_match);
  56. static struct platform_driver ts4800_gpio_driver = {
  57. .driver = {
  58. .name = "ts4800-gpio",
  59. .of_match_table = ts4800_gpio_of_match,
  60. },
  61. .probe = ts4800_gpio_probe,
  62. };
  63. module_platform_driver_probe(ts4800_gpio_driver, ts4800_gpio_probe);
  64. MODULE_AUTHOR("Julien Grossholtz <julien.grossholtz@savoirfairelinux.com>");
  65. MODULE_DESCRIPTION("TS4800 FPGA GPIO driver");
  66. MODULE_LICENSE("GPL v2");