A1-board-selector-CCD.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * board selector support for TCA9535 used in Bitmine's CoinCraft Desk
  3. *
  4. * Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <sys/ioctl.h>
  12. #include <errno.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <linux/i2c.h>
  18. #include <linux/i2c-dev.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. #include <fcntl.h>
  22. #include "miner.h"
  23. #include "A1-board-selector.h"
  24. #include "i2c-context.h"
  25. static struct board_selector ccd_selector;
  26. struct i2c_ctx *U1_tca9535;
  27. uint8_t chain_mask = 0xff;
  28. uint8_t active_chain = 255;
  29. pthread_mutex_t lock;
  30. #define UNUSED_BITS 0xe0
  31. static void ccd_unlock(void)
  32. {
  33. mutex_unlock(&lock);
  34. }
  35. static void ccd_exit(void)
  36. {
  37. if (U1_tca9535 != NULL)
  38. U1_tca9535->exit(U1_tca9535);
  39. }
  40. uint8_t retval = 0;
  41. extern struct board_selector *ccd_board_selector_init(void)
  42. {
  43. mutex_init(&lock);
  44. U1_tca9535 = i2c_slave_open(I2C_BUS, 0x27);
  45. if (U1_tca9535 == NULL)
  46. return NULL;
  47. bool retval = U1_tca9535->write(U1_tca9535, 0x06, 0xe0) &&
  48. U1_tca9535->write(U1_tca9535, 0x07, 0xe0) &&
  49. U1_tca9535->write(U1_tca9535, 0x02, 0x1f) &&
  50. U1_tca9535->write(U1_tca9535, 0x03, 0x00);
  51. if (retval)
  52. return &ccd_selector;
  53. ccd_exit();
  54. return NULL;
  55. }
  56. static bool ccd_select(uint8_t chain)
  57. {
  58. if (chain >= CCD_MAX_CHAINS)
  59. return false;
  60. mutex_lock(&lock);
  61. if (active_chain == chain)
  62. return true;
  63. active_chain = chain;
  64. chain_mask = 1 << active_chain;
  65. return U1_tca9535->write(U1_tca9535, 0x02, ~chain_mask);
  66. }
  67. static bool __ccd_board_selector_reset(uint8_t mask)
  68. {
  69. if (!U1_tca9535->write(U1_tca9535, 0x03, mask))
  70. return false;
  71. cgsleep_ms(RESET_LOW_TIME_MS);
  72. if (!U1_tca9535->write(U1_tca9535, 0x03, 0x00))
  73. return false;
  74. cgsleep_ms(RESET_HI_TIME_MS);
  75. return true;
  76. }
  77. // we assume we are already holding the mutex
  78. static bool ccd_reset(void)
  79. {
  80. return __ccd_board_selector_reset(chain_mask);
  81. }
  82. static bool ccd_reset_all(void)
  83. {
  84. mutex_lock(&lock);
  85. bool retval = __ccd_board_selector_reset(0xff & ~UNUSED_BITS);
  86. mutex_unlock(&lock);
  87. return retval;
  88. }
  89. static struct board_selector ccd_selector = {
  90. .select = ccd_select,
  91. .release = ccd_unlock,
  92. .exit = ccd_exit,
  93. .reset = ccd_reset,
  94. .reset_all = ccd_reset_all,
  95. /* don't have a temp sensor dedicated to chain */
  96. .get_temp = dummy_get_temp,
  97. };