wcxb_spi.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * wcxb SPI library
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. */
  9. /*
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2 as published by the
  18. * Free Software Foundation. See the LICENSE file included with
  19. * this program for more details.
  20. */
  21. #ifndef __WCXB_SPI_H
  22. #define __WCXB_SPI_H
  23. #include <linux/spi/spi.h>
  24. #include <stdbool.h>
  25. struct wcxb_spi_transfer {
  26. const void *tx_buf;
  27. void *rx_buf;
  28. u32 len:16;
  29. u16 delay_usecs;
  30. struct list_head node;
  31. };
  32. struct wcxb_spi_message {
  33. struct list_head transfers;
  34. struct list_head node;
  35. struct wcxb_spi_device *spi;
  36. void (*complete)(void *arg);
  37. void *arg;
  38. int status;
  39. };
  40. struct wcxb_spi_master;
  41. struct wcxb_spi_device {
  42. struct wcxb_spi_master *master;
  43. u16 chip_select;
  44. };
  45. extern struct wcxb_spi_master *wcxb_spi_master_create(struct device *parent,
  46. void __iomem *base, bool auto_cs);
  47. extern void wcxb_spi_master_destroy(struct wcxb_spi_master *master);
  48. extern int wcxb_spi_sync(struct wcxb_spi_device *spi,
  49. struct wcxb_spi_message *message);
  50. extern int wcxb_spi_async(struct wcxb_spi_device *spi,
  51. struct wcxb_spi_message *message);
  52. extern void wcxb_spi_handle_interrupt(struct wcxb_spi_master *master);
  53. static inline struct wcxb_spi_device *
  54. wcxb_spi_device_create(struct wcxb_spi_master *master, u16 chip_select)
  55. {
  56. struct wcxb_spi_device *spi = kzalloc(sizeof(*spi), GFP_KERNEL);
  57. if (!spi)
  58. return NULL;
  59. spi->master = master;
  60. spi->chip_select = chip_select;
  61. return spi;
  62. }
  63. static inline void wcxb_spi_device_destroy(struct wcxb_spi_device *spi)
  64. {
  65. kfree(spi);
  66. }
  67. static inline void wcxb_spi_message_init(struct wcxb_spi_message *m)
  68. {
  69. memset(m, 0, sizeof(*m));
  70. INIT_LIST_HEAD(&m->transfers);
  71. }
  72. static inline void wcxb_spi_message_add_tail(struct wcxb_spi_transfer *t,
  73. struct wcxb_spi_message *m)
  74. {
  75. list_add_tail(&t->node, &m->transfers);
  76. }
  77. static inline int
  78. wcxb_spi_write(struct wcxb_spi_device *spi, const void *buffer, size_t len)
  79. {
  80. struct wcxb_spi_transfer t = {
  81. .tx_buf = buffer,
  82. .len = len,
  83. };
  84. struct wcxb_spi_message m;
  85. wcxb_spi_message_init(&m);
  86. wcxb_spi_message_add_tail(&t, &m);
  87. return wcxb_spi_sync(spi, &m);
  88. }
  89. static inline int
  90. wcxb_spi_read(struct wcxb_spi_device *spi, void *buffer, size_t len)
  91. {
  92. struct wcxb_spi_transfer t = {
  93. .rx_buf = buffer,
  94. .len = len,
  95. };
  96. struct wcxb_spi_message m;
  97. wcxb_spi_message_init(&m);
  98. wcxb_spi_message_add_tail(&t, &m);
  99. return wcxb_spi_sync(spi, &m);
  100. }
  101. #endif