gpio-spear-spics.c 5.7 KB

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