gpio_ebu.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License version 2 as published
  4. * by the Free Software Foundation.
  5. *
  6. * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/export.h>
  10. #include <linux/types.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/mutex.h>
  13. #include <linux/gpio.h>
  14. #include <linux/io.h>
  15. #include <lantiq_soc.h>
  16. /*
  17. * By attaching hardware latches to the EBU it is possible to create output
  18. * only gpios. This driver configures a special memory address, which when
  19. * written to outputs 16 bit to the latches.
  20. */
  21. #define LTQ_EBU_BUSCON 0x1e7ff /* 16 bit access, slowest timing */
  22. #define LTQ_EBU_WP 0x80000000 /* write protect bit */
  23. /* we keep a shadow value of the last value written to the ebu */
  24. static int ltq_ebu_gpio_shadow = 0x0;
  25. static void __iomem *ltq_ebu_gpio_membase;
  26. static void ltq_ebu_apply(void)
  27. {
  28. unsigned long flags;
  29. spin_lock_irqsave(&ebu_lock, flags);
  30. ltq_ebu_w32(LTQ_EBU_BUSCON, LTQ_EBU_BUSCON1);
  31. *((__u16 *)ltq_ebu_gpio_membase) = ltq_ebu_gpio_shadow;
  32. ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
  33. spin_unlock_irqrestore(&ebu_lock, flags);
  34. }
  35. static void ltq_ebu_set(struct gpio_chip *chip, unsigned offset, int value)
  36. {
  37. if (value)
  38. ltq_ebu_gpio_shadow |= (1 << offset);
  39. else
  40. ltq_ebu_gpio_shadow &= ~(1 << offset);
  41. ltq_ebu_apply();
  42. }
  43. static int ltq_ebu_direction_output(struct gpio_chip *chip, unsigned offset,
  44. int value)
  45. {
  46. ltq_ebu_set(chip, offset, value);
  47. return 0;
  48. }
  49. static struct gpio_chip ltq_ebu_chip = {
  50. .label = "ltq_ebu",
  51. .direction_output = ltq_ebu_direction_output,
  52. .set = ltq_ebu_set,
  53. .base = 72,
  54. .ngpio = 16,
  55. .can_sleep = 1,
  56. .owner = THIS_MODULE,
  57. };
  58. static int ltq_ebu_probe(struct platform_device *pdev)
  59. {
  60. int ret = 0;
  61. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  62. if (!res) {
  63. dev_err(&pdev->dev, "failed to get memory resource\n");
  64. return -ENOENT;
  65. }
  66. res = devm_request_mem_region(&pdev->dev, res->start,
  67. resource_size(res), dev_name(&pdev->dev));
  68. if (!res) {
  69. dev_err(&pdev->dev, "failed to request memory resource\n");
  70. return -EBUSY;
  71. }
  72. ltq_ebu_gpio_membase = devm_ioremap_nocache(&pdev->dev, res->start,
  73. resource_size(res));
  74. if (!ltq_ebu_gpio_membase) {
  75. dev_err(&pdev->dev, "Failed to ioremap mem region\n");
  76. return -ENOMEM;
  77. }
  78. /* grab the default shadow value passed form the platform code */
  79. ltq_ebu_gpio_shadow = (unsigned int) pdev->dev.platform_data;
  80. /* tell the ebu controller which memory address we will be using */
  81. ltq_ebu_w32(pdev->resource->start | 0x1, LTQ_EBU_ADDRSEL1);
  82. /* write protect the region */
  83. ltq_ebu_w32(LTQ_EBU_BUSCON | LTQ_EBU_WP, LTQ_EBU_BUSCON1);
  84. ret = gpiochip_add(&ltq_ebu_chip);
  85. if (!ret)
  86. ltq_ebu_apply();
  87. return ret;
  88. }
  89. static struct platform_driver ltq_ebu_driver = {
  90. .probe = ltq_ebu_probe,
  91. .driver = {
  92. .name = "ltq_ebu",
  93. .owner = THIS_MODULE,
  94. },
  95. };
  96. static int __init ltq_ebu_init(void)
  97. {
  98. int ret = platform_driver_register(&ltq_ebu_driver);
  99. if (ret)
  100. pr_info("ltq_ebu : Error registering platfom driver!");
  101. return ret;
  102. }
  103. postcore_initcall(ltq_ebu_init);