spi-slave-time.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SPI slave handler reporting uptime at reception of previous SPI message
  3. *
  4. * This SPI slave handler sends the time of reception of the last SPI message
  5. * as two 32-bit unsigned integers in binary format and in network byte order,
  6. * representing the number of seconds and fractional seconds (in microseconds)
  7. * since boot up.
  8. *
  9. * Copyright (C) 2016-2017 Glider bvba
  10. *
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file "COPYING" in the main directory of this archive
  13. * for more details.
  14. *
  15. * Usage (assuming /dev/spidev2.0 corresponds to the SPI master on the remote
  16. * system):
  17. *
  18. * # spidev_test -D /dev/spidev2.0 -p dummy-8B
  19. * spi mode: 0x0
  20. * bits per word: 8
  21. * max speed: 500000 Hz (500 KHz)
  22. * RX | 00 00 04 6D 00 09 5B BB ...
  23. * ^^^^^ ^^^^^^^^
  24. * seconds microseconds
  25. */
  26. #include <linux/completion.h>
  27. #include <linux/module.h>
  28. #include <linux/sched/clock.h>
  29. #include <linux/spi/spi.h>
  30. struct spi_slave_time_priv {
  31. struct spi_device *spi;
  32. struct completion finished;
  33. struct spi_transfer xfer;
  34. struct spi_message msg;
  35. __be32 buf[2];
  36. };
  37. static int spi_slave_time_submit(struct spi_slave_time_priv *priv);
  38. static void spi_slave_time_complete(void *arg)
  39. {
  40. struct spi_slave_time_priv *priv = arg;
  41. int ret;
  42. ret = priv->msg.status;
  43. if (ret)
  44. goto terminate;
  45. ret = spi_slave_time_submit(priv);
  46. if (ret)
  47. goto terminate;
  48. return;
  49. terminate:
  50. dev_info(&priv->spi->dev, "Terminating\n");
  51. complete(&priv->finished);
  52. }
  53. static int spi_slave_time_submit(struct spi_slave_time_priv *priv)
  54. {
  55. u32 rem_us;
  56. int ret;
  57. u64 ts;
  58. ts = local_clock();
  59. rem_us = do_div(ts, 1000000000) / 1000;
  60. priv->buf[0] = cpu_to_be32(ts);
  61. priv->buf[1] = cpu_to_be32(rem_us);
  62. spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
  63. priv->msg.complete = spi_slave_time_complete;
  64. priv->msg.context = priv;
  65. ret = spi_async(priv->spi, &priv->msg);
  66. if (ret)
  67. dev_err(&priv->spi->dev, "spi_async() failed %d\n", ret);
  68. return ret;
  69. }
  70. static int spi_slave_time_probe(struct spi_device *spi)
  71. {
  72. struct spi_slave_time_priv *priv;
  73. int ret;
  74. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  75. if (!priv)
  76. return -ENOMEM;
  77. priv->spi = spi;
  78. init_completion(&priv->finished);
  79. priv->xfer.tx_buf = priv->buf;
  80. priv->xfer.len = sizeof(priv->buf);
  81. ret = spi_slave_time_submit(priv);
  82. if (ret)
  83. return ret;
  84. spi_set_drvdata(spi, priv);
  85. return 0;
  86. }
  87. static int spi_slave_time_remove(struct spi_device *spi)
  88. {
  89. struct spi_slave_time_priv *priv = spi_get_drvdata(spi);
  90. spi_slave_abort(spi);
  91. wait_for_completion(&priv->finished);
  92. return 0;
  93. }
  94. static struct spi_driver spi_slave_time_driver = {
  95. .driver = {
  96. .name = "spi-slave-time",
  97. },
  98. .probe = spi_slave_time_probe,
  99. .remove = spi_slave_time_remove,
  100. };
  101. module_spi_driver(spi_slave_time_driver);
  102. MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
  103. MODULE_DESCRIPTION("SPI slave reporting uptime at previous SPI message");
  104. MODULE_LICENSE("GPL v2");