driver-jingtian.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <sys/stat.h>
  16. #include <fcntl.h>
  17. #include <unistd.h>
  18. #include "deviceapi.h"
  19. #include "driver-aan.h"
  20. #include "logging.h"
  21. #include "lowl-spi.h"
  22. #include "util.h"
  23. static const int jingtian_cs_gpio[] = {14, 15, 18};
  24. static const int jingtian_max_cs = 1 << (sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio));
  25. static const uint8_t jingtian_pre_header[] = {0xb5, 0xb5};
  26. BFG_REGISTER_DRIVER(jingtian_drv)
  27. static
  28. bool jingtian_spi_txrx(struct spi_port * const port)
  29. {
  30. if (*port->chipselect_current != port->chipselect)
  31. {
  32. unsigned cs_set_low = 0, cs_set_high = 0, cur_cs_bit;
  33. bool bit_desired;
  34. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  35. {
  36. cur_cs_bit = (1 << i);
  37. bit_desired = (port->chipselect & cur_cs_bit);
  38. if (bit_desired == (bool)(*port->chipselect_current & cur_cs_bit))
  39. // No change needed
  40. continue;
  41. if (bit_desired)
  42. cs_set_high |= (1 << jingtian_cs_gpio[i]);
  43. else
  44. cs_set_low |= (1 << jingtian_cs_gpio[i]);
  45. }
  46. if (cs_set_low)
  47. bfg_gpio_set_low(cs_set_low);
  48. if (cs_set_high)
  49. bfg_gpio_set_high(cs_set_high);
  50. *port->chipselect_current = port->chipselect;
  51. }
  52. return linux_spi_txrx(port);
  53. }
  54. static
  55. void jingtian_precmd(struct spi_port * const spi)
  56. {
  57. spi_emit_buf(spi, jingtian_pre_header, sizeof(jingtian_pre_header));
  58. }
  59. static
  60. struct aan_hooks jingtian_hooks = {
  61. .precmd = jingtian_precmd,
  62. };
  63. static
  64. void jingtian_common_init(void)
  65. {
  66. RUNONCE();
  67. spi_init();
  68. for (int i = 0; i < sizeof(jingtian_cs_gpio) / sizeof(*jingtian_cs_gpio); ++i)
  69. bfg_gpio_setpin_output(jingtian_cs_gpio[i]);
  70. }
  71. static
  72. bool jingtian_detect_one(const char * const devpath)
  73. {
  74. int found = 0, chips;
  75. jingtian_common_init();
  76. const int fd = open(devpath, O_RDWR);
  77. if (fd < 0)
  78. applogr(false, LOG_DEBUG, "%s: Failed to open %s", jingtian_drv.dname, devpath);
  79. struct cgpu_info *cgpu, *prev_cgpu = NULL;
  80. struct spi_port * const spi = calloc(sizeof(*spi), 1), *spicopy;
  81. spi->txrx = jingtian_spi_txrx;
  82. spi->userp = &jingtian_hooks;
  83. spi->fd = fd;
  84. spi->speed = 4000000;
  85. spi->delay = 0;
  86. spi->mode = 1;
  87. spi->bits = 8;
  88. spi->chipselect_current = malloc(sizeof(*spi->chipselect_current));
  89. *spi->chipselect_current = -1;
  90. for (int i = 0; i < jingtian_max_cs; ++i)
  91. {
  92. spi->chipselect = i;
  93. chips = aan_detect_spi(spi);
  94. applog(LOG_DEBUG, "%s: %d chips found on %s CS %d",
  95. jingtian_drv.dname, chips, devpath, i);
  96. if (chips <= 0)
  97. continue;
  98. spicopy = malloc(sizeof(*spicopy));
  99. memcpy(spicopy, spi, sizeof(*spicopy));
  100. cgpu = malloc(sizeof(*cgpu));
  101. *cgpu = (struct cgpu_info){
  102. .drv = &jingtian_drv,
  103. .procs = chips,
  104. .device_data = spicopy,
  105. };
  106. add_cgpu_slave(cgpu, prev_cgpu);
  107. prev_cgpu = cgpu;
  108. found += chips;
  109. }
  110. close(fd);
  111. if (!found)
  112. free(spi->chipselect_current);
  113. free(spi);
  114. return found;
  115. }
  116. static
  117. int jingtian_detect_auto(void)
  118. {
  119. return jingtian_detect_one("/dev/spidev0.0") ? 1 : 0;
  120. }
  121. static
  122. void jingtian_detect(void)
  123. {
  124. generic_detect(&jingtian_drv, jingtian_detect_one, jingtian_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
  125. }
  126. struct device_drv jingtian_drv = {
  127. .dname = "jingtian",
  128. .name = "JTN",
  129. .drv_detect = jingtian_detect,
  130. };