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