siox-bus-gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 = devm_siox_master_alloc(dev, sizeof(*ddata));
  74. if (!smaster)
  75. return dev_err_probe(dev, -ENOMEM,
  76. "failed to allocate siox master\n");
  77. platform_set_drvdata(pdev, smaster);
  78. ddata = siox_master_get_devdata(smaster);
  79. ddata->din = devm_gpiod_get(dev, "din", GPIOD_IN);
  80. if (IS_ERR(ddata->din))
  81. return dev_err_probe(dev, PTR_ERR(ddata->din),
  82. "Failed to get din GPIO\n");
  83. ddata->dout = devm_gpiod_get(dev, "dout", GPIOD_OUT_LOW);
  84. if (IS_ERR(ddata->dout))
  85. return dev_err_probe(dev, PTR_ERR(ddata->dout),
  86. "Failed to get dout GPIO\n");
  87. ddata->dclk = devm_gpiod_get(dev, "dclk", GPIOD_OUT_LOW);
  88. if (IS_ERR(ddata->dclk))
  89. return dev_err_probe(dev, PTR_ERR(ddata->dclk),
  90. "Failed to get dclk GPIO\n");
  91. ddata->dld = devm_gpiod_get(dev, "dld", GPIOD_OUT_LOW);
  92. if (IS_ERR(ddata->dld))
  93. return dev_err_probe(dev, PTR_ERR(ddata->dld),
  94. "Failed to get dld GPIO\n");
  95. smaster->pushpull = siox_gpio_pushpull;
  96. /* XXX: determine automatically like spi does */
  97. smaster->busno = 0;
  98. ret = devm_siox_master_register(dev, smaster);
  99. if (ret)
  100. return dev_err_probe(dev, ret,
  101. "Failed to register siox master\n");
  102. return 0;
  103. }
  104. static const struct of_device_id siox_gpio_dt_ids[] = {
  105. { .compatible = "eckelmann,siox-gpio", },
  106. { /* sentinel */ }
  107. };
  108. MODULE_DEVICE_TABLE(of, siox_gpio_dt_ids);
  109. static struct platform_driver siox_gpio_driver = {
  110. .probe = siox_gpio_probe,
  111. .driver = {
  112. .name = DRIVER_NAME,
  113. .of_match_table = siox_gpio_dt_ids,
  114. },
  115. };
  116. module_platform_driver(siox_gpio_driver);
  117. MODULE_AUTHOR("Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>");
  118. MODULE_DESCRIPTION("SIOX GPIO bus driver");
  119. MODULE_LICENSE("GPL v2");
  120. MODULE_ALIAS("platform:" DRIVER_NAME);