| 1 |
#include <stdint.h> |
| 2 |
|
| 3 |
#include "board/board.h" |
| 4 |
|
| 5 |
#include "platform.h" |
| 6 |
|
| 7 |
#include "adc_core.h" |
| 8 |
#include "dac_core.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
int spi_init(struct ad9361_rf_phy *phy, void *userdata) |
| 15 |
{ |
| 16 |
phy->spi->userdata = userdata; |
| 17 |
return 0; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
int spi_write(struct spi_device *spi, uint16_t cmd, const uint8_t *buf, |
| 25 |
unsigned int len) |
| 26 |
{ |
| 27 |
struct bladerf *dev = spi->userdata; |
| 28 |
int status; |
| 29 |
uint64_t data; |
| 30 |
unsigned int i; |
| 31 |
|
| 32 |
|
| 33 |
data = 0; |
| 34 |
for (i = 0; i < len; i++) { |
| 35 |
data |= (((uint64_t)buf[i]) << 8*(7-i)); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
status = dev->backend->ad9361_spi_write(dev, cmd, data); |
| 40 |
if (status < 0) { |
| 41 |
return -EIO; |
| 42 |
} |
| 43 |
|
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#include <inttypes.h> |
| 52 |
|
| 53 |
int spi_read(struct spi_device *spi, uint16_t cmd, uint8_t *buf, |
| 54 |
unsigned int len) |
| 55 |
{ |
| 56 |
struct bladerf *dev = spi->userdata; |
| 57 |
int status; |
| 58 |
uint64_t data = 0; |
| 59 |
unsigned int i; |
| 60 |
|
| 61 |
|
| 62 |
status = dev->backend->ad9361_spi_read(dev, cmd, &data); |
| 63 |
if (status < 0) { |
| 64 |
return -EIO; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
for (i = 0; i < len; i++) { |
| 69 |
buf[i] = (data >> 8*(7-i)) & 0xff; |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int gpio_init(struct ad9361_rf_phy *phy, void *userdata) |
| 80 |
{ |
| 81 |
phy->gpio->userdata = userdata; |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
bool gpio_is_valid(struct gpio_device *gpio, int32_t number) |
| 90 |
{ |
| 91 |
if (number == RFFE_CONTROL_RESET_N) { |
| 92 |
return true; |
| 93 |
} else if (number == RFFE_CONTROL_SYNC_IN) { |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
int gpio_set_value(struct gpio_device *gpio, int32_t number, bool value) |
| 105 |
{ |
| 106 |
struct bladerf *dev = gpio->userdata; |
| 107 |
int status; |
| 108 |
uint32_t reg; |
| 109 |
|
| 110 |
|
| 111 |
status = dev->backend->rffe_control_read(dev, ®); |
| 112 |
if (status < 0) { |
| 113 |
return -EIO; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if (value) { |
| 118 |
reg |= (1 << number); |
| 119 |
} else { |
| 120 |
reg &= ~(1 << number); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
status = dev->backend->rffe_control_write(dev, reg); |
| 125 |
if (status < 0) { |
| 126 |
return -EIO; |
| 127 |
} |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void udelay(unsigned long usecs) |
| 137 |
{ |
| 138 |
usleep(usecs); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
void mdelay(unsigned long msecs) |
| 146 |
{ |
| 147 |
usleep(msecs * 1000); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
unsigned long msleep_interruptible(unsigned int msecs) |
| 155 |
{ |
| 156 |
usleep(msecs * 1000); |
| 157 |
return 0; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
int axiadc_init(struct ad9361_rf_phy *phy, void *userdata) |
| 165 |
{ |
| 166 |
int status; |
| 167 |
|
| 168 |
phy->adc_state->userdata = userdata; |
| 169 |
|
| 170 |
status = adc_init(phy); |
| 171 |
if (status < 0) { |
| 172 |
return status; |
| 173 |
} |
| 174 |
|
| 175 |
status = dac_init(phy, DATA_SEL_DMA, 0); |
| 176 |
if (status < 0) { |
| 177 |
return status; |
| 178 |
} |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
int axiadc_post_setup(struct ad9361_rf_phy *phy) |
| 188 |
{ |
| 189 |
return ad9361_post_setup(phy); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
int axiadc_read(struct axiadc_state *st, uint32_t addr, uint32_t *data) |
| 197 |
{ |
| 198 |
struct bladerf *dev = st->userdata; |
| 199 |
int status; |
| 200 |
|
| 201 |
|
| 202 |
status = dev->backend->adi_axi_read(dev, addr, data); |
| 203 |
if (status < 0) { |
| 204 |
return -EIO; |
| 205 |
} |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
int axiadc_write(struct axiadc_state *st, uint32_t addr, uint32_t data) |
| 215 |
{ |
| 216 |
struct bladerf *dev = st->userdata; |
| 217 |
int status; |
| 218 |
|
| 219 |
|
| 220 |
status = dev->backend->adi_axi_write(dev, addr, data); |
| 221 |
if (status < 0) { |
| 222 |
return -EIO; |
| 223 |
} |
| 224 |
|
| 225 |
return 0; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
int axiadc_set_pnsel(struct axiadc_state *st, unsigned int channel, enum adc_pn_sel sel) |
| 233 |
{ |
| 234 |
int status; |
| 235 |
uint32_t reg; |
| 236 |
|
| 237 |
if (PCORE_VERSION_MAJOR(st->pcore_version) > 7) { |
| 238 |
status = axiadc_read(st, ADI_REG_CHAN_CNTRL_3(channel), ®); |
| 239 |
if (status != 0) |
| 240 |
return status; |
| 241 |
|
| 242 |
reg &= ~ADI_ADC_PN_SEL(~0); |
| 243 |
reg |= ADI_ADC_PN_SEL(sel); |
| 244 |
|
| 245 |
status = axiadc_write(st, ADI_REG_CHAN_CNTRL_3(channel), reg); |
| 246 |
if (status != 0) |
| 247 |
return status; |
| 248 |
} else { |
| 249 |
status = axiadc_read(st, ADI_REG_CHAN_CNTRL(channel), ®); |
| 250 |
if (status != 0) |
| 251 |
return status; |
| 252 |
|
| 253 |
if (sel == ADC_PN_CUSTOM) { |
| 254 |
reg |= ADI_PN_SEL; |
| 255 |
} else if (sel == ADC_PN9) { |
| 256 |
reg &= ~ADI_PN23_TYPE; |
| 257 |
reg &= ~ADI_PN_SEL; |
| 258 |
} else { |
| 259 |
reg |= ADI_PN23_TYPE; |
| 260 |
reg &= ~ADI_PN_SEL; |
| 261 |
} |
| 262 |
|
| 263 |
status = axiadc_write(st, ADI_REG_CHAN_CNTRL(channel), reg); |
| 264 |
if (status != 0) |
| 265 |
return status; |
| 266 |
} |
| 267 |
|
| 268 |
return 0; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
int axiadc_idelay_set(struct axiadc_state *st, unsigned int lane, unsigned int val) |
| 276 |
{ |
| 277 |
int status; |
| 278 |
|
| 279 |
if (PCORE_VERSION_MAJOR(st->pcore_version) > 8) { |
| 280 |
status = axiadc_write(st, ADI_REG_DELAY(lane), val); |
| 281 |
if (status != 0) |
| 282 |
return status; |
| 283 |
} else { |
| 284 |
status = axiadc_write(st, ADI_REG_DELAY_CNTRL, 0); |
| 285 |
if (status != 0) |
| 286 |
return status; |
| 287 |
|
| 288 |
status = axiadc_write(st, ADI_REG_DELAY_CNTRL, |
| 289 |
ADI_DELAY_ADDRESS(lane) | ADI_DELAY_WDATA(val) | ADI_DELAY_SEL); |
| 290 |
if (status != 0) |
| 291 |
return status; |
| 292 |
} |
| 293 |
|
| 294 |
return 0; |
| 295 |
} |