siox-bus-gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015-2017 Pengutronix, Uwe Kleine-König <kernel@pengutronix.de>
  4. */
  5. #include <linux/gpio/consumer.h>
  6. #include <linux/module.h>
  7. #include <linux/mod_devicetable.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/delay.h>
  10. #include "siox.h"
  11. #define DRIVER_NAME "siox-gpio"
  12. struct siox_gpio_ddata {
  13. struct gpio_desc *din;
  14. struct gpio_desc *dout;
  15. struct gpio_desc *dclk;
  16. struct gpio_desc *dld;
  17. };
  18. static unsigned int siox_clkhigh_ns = 1000;
  19. static unsigned int siox_loadhigh_ns;
  20. static unsigned int siox_bytegap_ns;
  21. static int siox_gpio_pushpull(struct siox_master *smaster,
  22. size_t setbuf_len, const u8 setbuf[],
  23. size_t getbuf_len, u8 getbuf[])
  24. {
  25. struct siox_gpio_ddata *ddata = siox_master_get_devdata(smaster);
  26. size_t i;
  27. size_t cycles = max(setbuf_len, getbuf_len);
  28. /* reset data and clock */
  29. gpiod_set_value_cansleep(ddata->dout, 0);
  30. gpiod_set_value_cansleep(ddata->dclk, 0);
  31. gpiod_set_value_cansleep(ddata->dld, 1);
  32. ndelay(siox_loadhigh_ns);
  33. gpiod_set_value_cansleep(ddata->dld, 0);
  34. for (i = 0; i < cycles; ++i) {
  35. u8 set = 0, get = 0;
  36. size_t j;
  37. if (i >= cycles - setbuf_len)
  38. set = setbuf[i - (cycles - setbuf_len)];
  39. for (j = 0; j < 8; ++j) {
  40. get <<= 1;
  41. if (gpiod_get_value_cansleep(ddata->din))
  42. get |= 1;
  43. /* DOUT is logically inverted */
  44. gpiod_set_value_cansleep(ddata->dout, !(set & 0x80));
  45. set <<= 1;
  46. gpiod_set_value_cansleep(ddata->dclk, 1);
  47. ndelay(siox_clkhigh_ns);
  48. gpiod_set_value_cansleep(ddata->dclk, 0);
  49. }
  50. if (i < getbuf_len)
  51. getbuf[i] = get;
  52. ndelay(siox_bytegap_ns);
  53. }
  54. gpiod_set_value_cansleep(ddata->dld, 1);
  55. ndelay(siox_loadhigh_ns);
  56. gpiod_set_value_cansleep(ddata->dld, 0);
  57. /*
  58. * Resetting dout isn't necessary protocol wise, but it makes the
  59. * signals more pretty because the dout level is deterministic between
  60. * cycles. Note that this only affects dout between the master and the
  61. * first siox device. dout for the later devices depend on the output of
  62. * the previous siox device.
  63. */
  64. gpiod_set_value_cansleep(ddata->dout, 0);
  65. return 0;
  66. }
  67. static int siox_gpio_probe(struct platform_device *pdev)
  68. {
  69. struct device *dev = &pdev->dev;
  70. struct siox_gpio_ddata *ddata;
  71. int ret;
  72. struct siox_master *smaster;
  73. smaster = siox_master_alloc(&pdev->dev, sizeof(*ddata));
  74. if (!smaster) {
  75. dev_err(dev, "failed to allocate siox master\n");
  76. return -ENOMEM;
  77. }
  78. platform_set_drvdata(pdev, smaster);
  79. ddata = siox_master_get_devdata(smaster);
  80. ddata->din = devm_gpiod_get(dev, "din", GPIOD_IN);
  81. if (IS_ERR(ddata->din)) {
  82. ret = PTR_ERR(ddata->din);
  83. dev_err(dev, "Failed to get %s GPIO: %d\n", "din", ret);
  84. goto err;
  85. }
  86. ddata->dout = devm_gpiod_get(dev, "dout", GPIOD_OUT_LOW);
  87. if (IS_ERR(ddata->dout)) {
  88. ret = PTR_ERR(ddata->dout);
  89. dev_err(dev, "Failed to get %s GPIO: %d\n", "dout", ret);
  90. goto err;
  91. }
  92. ddata->dclk = devm_gpiod_get(dev, "dclk", GPIOD_OUT_LOW);
  93. if (IS_ERR(ddata->dclk)) {
  94. ret = PTR_ERR(ddata->dclk);
  95. dev_err(dev, "Failed to get %s GPIO: %d\n", "dclk", ret);
  96. goto err;
  97. }
  98. ddata->dld = devm_gpiod_get(dev, "dld", GPIOD_OUT_LOW);
  99. if (IS_ERR(ddata->dld)) {
  100. ret = PTR_ERR(ddata->dld);
  101. dev_err(dev, "Failed to get %s GPIO: %d\n", "dld", ret);
  102. goto err;
  103. }
  104. smaster->pushpull = siox_gpio_pushpull;
  105. /* XXX: determine automatically like spi does */
  106. smaster->busno = 0;
  107. ret = siox_master_register(smaster);
  108. if (ret) {
  109. dev_err(dev, "Failed to register siox master: %d\n", ret);
  110. err:
  111. siox_master_put(smaster);
  112. }
  113. return ret;
  114. }
  115. static int siox_gpio_remove(struct platform_device *pdev)
  116. {
  117. struct siox_master *master = platform_get_drvdata(pdev);
  118. siox_master_unregister(master);
  119. return 0;
  120. }
  121. static const struct of_device_id siox_gpio_dt_ids[] = {
  122. { .compatible = "eckelmann,siox-gpio", },
  123. { /* sentinel */ }
  124. };
  125. MODULE_DEVICE_TABLE(of, siox_gpio_dt_ids);
  126. static struct platform_driver siox_gpio_driver = {
  127. .probe = siox_gpio_probe,
  128. .remove = siox_gpio_remove,
  129. .driver = {
  130. .name = DRIVER_NAME,
  131. .of_match_table = siox_gpio_dt_ids,
  132. },
  133. };
  134. module_platform_driver(siox_gpio_driver);
  135. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  136. MODULE_LICENSE("GPL v2");
  137. MODULE_ALIAS("platform:" DRIVER_NAME);