gpio-spear-spics.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPEAr platform SPI chipselect abstraction over gpiolib
  4. *
  5. * Copyright (C) 2012 ST Microelectronics
  6. * Shiraz Hashim <[email protected]>
  7. */
  8. #include <linux/err.h>
  9. #include <linux/gpio/driver.h>
  10. #include <linux/io.h>
  11. #include <linux/init.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/types.h>
  15. /* maximum chipselects */
  16. #define NUM_OF_GPIO 4
  17. /*
  18. * Provision is available on some SPEAr SoCs to control ARM PL022 spi cs
  19. * through system registers. This register lies outside spi (pl022)
  20. * address space into system registers.
  21. *
  22. * It provides control for spi chip select lines so that any chipselect
  23. * (out of 4 possible chipselects in pl022) can be made low to select
  24. * the particular slave.
  25. */
  26. /**
  27. * struct spear_spics - represents spi chip select control
  28. * @base: base address
  29. * @perip_cfg: configuration register
  30. * @sw_enable_bit: bit to enable s/w control over chipselects
  31. * @cs_value_bit: bit to program high or low chipselect
  32. * @cs_enable_mask: mask to select bits required to select chipselect
  33. * @cs_enable_shift: bit pos of cs_enable_mask
  34. * @use_count: use count of a spi controller cs lines
  35. * @last_off: stores last offset caller of set_value()
  36. * @chip: gpio_chip abstraction
  37. */
  38. struct spear_spics {
  39. void __iomem *base;
  40. u32 perip_cfg;
  41. u32 sw_enable_bit;
  42. u32 cs_value_bit;
  43. u32 cs_enable_mask;
  44. u32 cs_enable_shift;
  45. unsigned long use_count;
  46. int last_off;
  47. struct gpio_chip chip;
  48. };
  49. /* gpio framework specific routines */
  50. static int spics_get_value(struct gpio_chip *chip, unsigned offset)
  51. {
  52. return -ENXIO;
  53. }
  54. static void spics_set_value(struct gpio_chip *chip, unsigned offset, int value)
  55. {
  56. struct spear_spics *spics = gpiochip_get_data(chip);
  57. u32 tmp;
  58. /* select chip select from register */
  59. tmp = readl_relaxed(spics->base + spics->perip_cfg);
  60. if (spics->last_off != offset) {
  61. spics->last_off = offset;
  62. tmp &= ~(spics->cs_enable_mask << spics->cs_enable_shift);
  63. tmp |= offset << spics->cs_enable_shift;
  64. }
  65. /* toggle chip select line */
  66. tmp &= ~(0x1 << spics->cs_value_bit);
  67. tmp |= value << spics->cs_value_bit;
  68. writel_relaxed(tmp, spics->base + spics->perip_cfg);
  69. }
  70. static int spics_direction_input(struct gpio_chip *chip, unsigned offset)
  71. {
  72. return -ENXIO;
  73. }
  74. static int spics_direction_output(struct gpio_chip *chip, unsigned offset,
  75. int value)
  76. {
  77. spics_set_value(chip, offset, value);
  78. return 0;
  79. }
  80. static int spics_request(struct gpio_chip *chip, unsigned offset)
  81. {
  82. struct spear_spics *spics = gpiochip_get_data(chip);
  83. u32 tmp;
  84. if (!spics->use_count++) {
  85. tmp = readl_relaxed(spics->base + spics->perip_cfg);
  86. tmp |= 0x1 << spics->sw_enable_bit;
  87. tmp |= 0x1 << spics->cs_value_bit;
  88. writel_relaxed(tmp, spics->base + spics->perip_cfg);
  89. }
  90. return 0;
  91. }
  92. static void spics_free(struct gpio_chip *chip, unsigned offset)
  93. {
  94. struct spear_spics *spics = gpiochip_get_data(chip);
  95. u32 tmp;
  96. if (!--spics->use_count) {
  97. tmp = readl_relaxed(spics->base + spics->perip_cfg);
  98. tmp &= ~(0x1 << spics->sw_enable_bit);
  99. writel_relaxed(tmp, spics->base + spics->perip_cfg);
  100. }
  101. }
  102. static int spics_gpio_probe(struct platform_device *pdev)
  103. {
  104. struct device_node *np = pdev->dev.of_node;
  105. struct spear_spics *spics;
  106. spics = devm_kzalloc(&pdev->dev, sizeof(*spics), GFP_KERNEL);
  107. if (!spics)
  108. return -ENOMEM;
  109. spics->base = devm_platform_ioremap_resource(pdev, 0);
  110. if (IS_ERR(spics->base))
  111. return PTR_ERR(spics->base);
  112. if (of_property_read_u32(np, "st-spics,peripcfg-reg",
  113. &spics->perip_cfg))
  114. goto err_dt_data;
  115. if (of_property_read_u32(np, "st-spics,sw-enable-bit",
  116. &spics->sw_enable_bit))
  117. goto err_dt_data;
  118. if (of_property_read_u32(np, "st-spics,cs-value-bit",
  119. &spics->cs_value_bit))
  120. goto err_dt_data;
  121. if (of_property_read_u32(np, "st-spics,cs-enable-mask",
  122. &spics->cs_enable_mask))
  123. goto err_dt_data;
  124. if (of_property_read_u32(np, "st-spics,cs-enable-shift",
  125. &spics->cs_enable_shift))
  126. goto err_dt_data;
  127. spics->chip.ngpio = NUM_OF_GPIO;
  128. spics->chip.base = -1;
  129. spics->chip.request = spics_request;
  130. spics->chip.free = spics_free;
  131. spics->chip.direction_input = spics_direction_input;
  132. spics->chip.direction_output = spics_direction_output;
  133. spics->chip.get = spics_get_value;
  134. spics->chip.set = spics_set_value;
  135. spics->chip.label = dev_name(&pdev->dev);
  136. spics->chip.parent = &pdev->dev;
  137. spics->chip.owner = THIS_MODULE;
  138. spics->last_off = -1;
  139. return devm_gpiochip_add_data(&pdev->dev, &spics->chip, spics);
  140. err_dt_data:
  141. dev_err(&pdev->dev, "DT probe failed\n");
  142. return -EINVAL;
  143. }
  144. static const struct of_device_id spics_gpio_of_match[] = {
  145. { .compatible = "st,spear-spics-gpio" },
  146. {}
  147. };
  148. static struct platform_driver spics_gpio_driver = {
  149. .probe = spics_gpio_probe,
  150. .driver = {
  151. .name = "spear-spics-gpio",
  152. .of_match_table = spics_gpio_of_match,
  153. },
  154. };
  155. static int __init spics_gpio_init(void)
  156. {
  157. return platform_driver_register(&spics_gpio_driver);
  158. }
  159. subsys_initcall(spics_gpio_init);