qca_7k.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. *
  3. * Copyright (c) 2011, 2012, Qualcomm Atheros Communications Inc.
  4. * Copyright (c) 2014, I2SE GmbH
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software
  7. * for any purpose with or without fee is hereby granted, provided
  8. * that the above copyright notice and this permission notice appear
  9. * in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  12. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  13. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  14. * THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  15. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  16. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  17. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. /* This module implements the Qualcomm Atheros SPI protocol for
  22. * kernel-based SPI device.
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/version.h>
  29. #include "qca_7k.h"
  30. void
  31. qcaspi_spi_error(struct qcaspi *qca)
  32. {
  33. if (qca->sync != QCASPI_SYNC_READY)
  34. return;
  35. netdev_err(qca->net_dev, "spi error\n");
  36. qca->sync = QCASPI_SYNC_UNKNOWN;
  37. qca->stats.spi_err++;
  38. }
  39. int
  40. qcaspi_read_register(struct qcaspi *qca, u16 reg, u16 *result)
  41. {
  42. __be16 rx_data;
  43. __be16 tx_data;
  44. struct spi_transfer *transfer;
  45. struct spi_message *msg;
  46. int ret;
  47. tx_data = cpu_to_be16(QCA7K_SPI_READ | QCA7K_SPI_INTERNAL | reg);
  48. if (qca->legacy_mode) {
  49. msg = &qca->spi_msg1;
  50. transfer = &qca->spi_xfer1;
  51. transfer->tx_buf = &tx_data;
  52. transfer->rx_buf = NULL;
  53. transfer->len = QCASPI_CMD_LEN;
  54. spi_sync(qca->spi_dev, msg);
  55. } else {
  56. msg = &qca->spi_msg2;
  57. transfer = &qca->spi_xfer2[0];
  58. transfer->tx_buf = &tx_data;
  59. transfer->rx_buf = NULL;
  60. transfer->len = QCASPI_CMD_LEN;
  61. transfer = &qca->spi_xfer2[1];
  62. }
  63. transfer->tx_buf = NULL;
  64. transfer->rx_buf = &rx_data;
  65. transfer->len = QCASPI_CMD_LEN;
  66. ret = spi_sync(qca->spi_dev, msg);
  67. if (!ret)
  68. ret = msg->status;
  69. if (ret)
  70. qcaspi_spi_error(qca);
  71. else
  72. *result = be16_to_cpu(rx_data);
  73. return ret;
  74. }
  75. int
  76. qcaspi_write_register(struct qcaspi *qca, u16 reg, u16 value)
  77. {
  78. __be16 tx_data[2];
  79. struct spi_transfer *transfer;
  80. struct spi_message *msg;
  81. int ret;
  82. tx_data[0] = cpu_to_be16(QCA7K_SPI_WRITE | QCA7K_SPI_INTERNAL | reg);
  83. tx_data[1] = cpu_to_be16(value);
  84. if (qca->legacy_mode) {
  85. msg = &qca->spi_msg1;
  86. transfer = &qca->spi_xfer1;
  87. transfer->tx_buf = &tx_data[0];
  88. transfer->rx_buf = NULL;
  89. transfer->len = QCASPI_CMD_LEN;
  90. spi_sync(qca->spi_dev, msg);
  91. } else {
  92. msg = &qca->spi_msg2;
  93. transfer = &qca->spi_xfer2[0];
  94. transfer->tx_buf = &tx_data[0];
  95. transfer->rx_buf = NULL;
  96. transfer->len = QCASPI_CMD_LEN;
  97. transfer = &qca->spi_xfer2[1];
  98. }
  99. transfer->tx_buf = &tx_data[1];
  100. transfer->rx_buf = NULL;
  101. transfer->len = QCASPI_CMD_LEN;
  102. ret = spi_sync(qca->spi_dev, msg);
  103. if (!ret)
  104. ret = msg->status;
  105. if (ret)
  106. qcaspi_spi_error(qca);
  107. return ret;
  108. }
  109. int
  110. qcaspi_tx_cmd(struct qcaspi *qca, u16 cmd)
  111. {
  112. __be16 tx_data;
  113. struct spi_message *msg = &qca->spi_msg1;
  114. struct spi_transfer *transfer = &qca->spi_xfer1;
  115. int ret;
  116. tx_data = cpu_to_be16(cmd);
  117. transfer->len = sizeof(tx_data);
  118. transfer->tx_buf = &tx_data;
  119. transfer->rx_buf = NULL;
  120. ret = spi_sync(qca->spi_dev, msg);
  121. if (!ret)
  122. ret = msg->status;
  123. if (ret)
  124. qcaspi_spi_error(qca);
  125. return ret;
  126. }