gpio-spear-spics.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * SPEAr platform SPI chipselect abstraction over gpiolib
  3. *
  4. * Copyright (C) 2012 ST Microelectronics
  5. * Shiraz Hashim <shiraz.linux.kernel@gmail.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. spics = devm_kzalloc(&pdev->dev, sizeof(*spics), GFP_KERNEL);
  115. if (!spics)
  116. return -ENOMEM;
  117. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  118. spics->base = devm_ioremap_resource(&pdev->dev, res);
  119. if (IS_ERR(spics->base))
  120. return PTR_ERR(spics->base);
  121. if (of_property_read_u32(np, "st-spics,peripcfg-reg",
  122. &spics->perip_cfg))
  123. goto err_dt_data;
  124. if (of_property_read_u32(np, "st-spics,sw-enable-bit",
  125. &spics->sw_enable_bit))
  126. goto err_dt_data;
  127. if (of_property_read_u32(np, "st-spics,cs-value-bit",
  128. &spics->cs_value_bit))
  129. goto err_dt_data;
  130. if (of_property_read_u32(np, "st-spics,cs-enable-mask",
  131. &spics->cs_enable_mask))
  132. goto err_dt_data;
  133. if (of_property_read_u32(np, "st-spics,cs-enable-shift",
  134. &spics->cs_enable_shift))
  135. goto err_dt_data;
  136. platform_set_drvdata(pdev, spics);
  137. spics->chip.ngpio = NUM_OF_GPIO;
  138. spics->chip.base = -1;
  139. spics->chip.request = spics_request;
  140. spics->chip.free = spics_free;
  141. spics->chip.direction_input = spics_direction_input;
  142. spics->chip.direction_output = spics_direction_output;
  143. spics->chip.get = spics_get_value;
  144. spics->chip.set = spics_set_value;
  145. spics->chip.label = dev_name(&pdev->dev);
  146. spics->chip.dev = &pdev->dev;
  147. spics->chip.owner = THIS_MODULE;
  148. spics->last_off = -1;
  149. ret = gpiochip_add(&spics->chip);
  150. if (ret) {
  151. dev_err(&pdev->dev, "unable to add gpio chip\n");
  152. return ret;
  153. }
  154. dev_info(&pdev->dev, "spear spics registered\n");
  155. return 0;
  156. err_dt_data:
  157. dev_err(&pdev->dev, "DT probe failed\n");
  158. return -EINVAL;
  159. }
  160. static const struct of_device_id spics_gpio_of_match[] = {
  161. { .compatible = "st,spear-spics-gpio" },
  162. {}
  163. };
  164. MODULE_DEVICE_TABLE(of, spics_gpio_of_match);
  165. static struct platform_driver spics_gpio_driver = {
  166. .probe = spics_gpio_probe,
  167. .driver = {
  168. .name = "spear-spics-gpio",
  169. .of_match_table = spics_gpio_of_match,
  170. },
  171. };
  172. static int __init spics_gpio_init(void)
  173. {
  174. return platform_driver_register(&spics_gpio_driver);
  175. }
  176. subsys_initcall(spics_gpio_init);
  177. MODULE_AUTHOR("Shiraz Hashim <shiraz.linux.kernel@gmail.com>");
  178. MODULE_DESCRIPTION("STMicroelectronics SPEAr SPI Chip Select Abstraction");
  179. MODULE_LICENSE("GPL");