| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#include <stdint.h> |
| 44 |
#include "../util.h" |
| 45 |
#include "parameters.h" |
| 46 |
#include "platform.h" |
| 47 |
#include "adc_core.h" |
| 48 |
#include "dac_core.h" |
| 49 |
|
| 50 |
#include <fcntl.h> |
| 51 |
#include <errno.h> |
| 52 |
#include <unistd.h> |
| 53 |
#include <sys/ioctl.h> |
| 54 |
#include <sys/mman.h> |
| 55 |
#include <linux/types.h> |
| 56 |
#include <linux/spi/spidev.h> |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
int spidev_fd; |
| 62 |
#ifdef FMCOMMS5 |
| 63 |
int spidev_b_fd; |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
int32_t spi_init(uint32_t device_id, |
| 70 |
uint8_t clk_pha, |
| 71 |
uint8_t clk_pol) |
| 72 |
{ |
| 73 |
uint8_t mode = SPI_CPHA; |
| 74 |
uint8_t bits = 8; |
| 75 |
uint32_t speed = 10000000; |
| 76 |
int ret; |
| 77 |
|
| 78 |
if (device_id) { |
| 79 |
|
| 80 |
} |
| 81 |
if (clk_pha) { |
| 82 |
|
| 83 |
} |
| 84 |
if (clk_pol) { |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
spidev_fd = open(SPIDEV_DEV, O_RDWR); |
| 89 |
if (spidev_fd < 0) { |
| 90 |
printf("%s: Can't open device\n\r", __func__); |
| 91 |
return -1; |
| 92 |
} |
| 93 |
|
| 94 |
ret = ioctl(spidev_fd, SPI_IOC_WR_MODE, &mode); |
| 95 |
if (ret == -1) { |
| 96 |
printf("%s: Can't set spi mode\n\r", __func__); |
| 97 |
return ret; |
| 98 |
} |
| 99 |
|
| 100 |
ret = ioctl(spidev_fd, SPI_IOC_WR_BITS_PER_WORD, &bits); |
| 101 |
if (ret == -1) { |
| 102 |
printf("%s: Can't set bits per word\n\r", __func__); |
| 103 |
return ret; |
| 104 |
} |
| 105 |
|
| 106 |
ret = ioctl(spidev_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); |
| 107 |
if (ret == -1) { |
| 108 |
printf("%s: Can't set max speed hz\n\r", __func__); |
| 109 |
return ret; |
| 110 |
} |
| 111 |
#ifdef FMCOMMS5 |
| 112 |
spidev_b_fd = open(SPIDEV_B_DEV, O_RDWR); |
| 113 |
if (spidev_b_fd < 0) { |
| 114 |
printf("%s: Can't open device\n\r", __func__); |
| 115 |
return -1; |
| 116 |
} |
| 117 |
|
| 118 |
ret = ioctl(spidev_b_fd, SPI_IOC_WR_MODE, &mode); |
| 119 |
if (ret == -1) { |
| 120 |
printf("%s: Can't set spi mode\n\r", __func__); |
| 121 |
return ret; |
| 122 |
} |
| 123 |
|
| 124 |
ret = ioctl(spidev_b_fd, SPI_IOC_WR_BITS_PER_WORD, &bits); |
| 125 |
if (ret == -1) { |
| 126 |
printf("%s: Can't set bits per word\n\r", __func__); |
| 127 |
return ret; |
| 128 |
} |
| 129 |
|
| 130 |
ret = ioctl(spidev_b_fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); |
| 131 |
if (ret == -1) { |
| 132 |
printf("%s: Can't set max speed hz\n\r", __func__); |
| 133 |
return ret; |
| 134 |
} |
| 135 |
#endif |
| 136 |
|
| 137 |
return 0; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
int spi_write_then_read(struct spi_device *spi, |
| 146 |
const unsigned char *txbuf, unsigned n_tx, |
| 147 |
unsigned char *rxbuf, unsigned n_rx) |
| 148 |
{ |
| 149 |
int ret = 0; |
| 150 |
|
| 151 |
if (spi) { |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
struct spi_ioc_transfer tr[2] = { |
| 156 |
{ |
| 157 |
.tx_buf = (unsigned long)txbuf, |
| 158 |
.len = n_tx, |
| 159 |
}, { |
| 160 |
.rx_buf = (unsigned long)rxbuf, |
| 161 |
.len = n_rx, |
| 162 |
}, |
| 163 |
}; |
| 164 |
|
| 165 |
if (spi->id_no == 0) { |
| 166 |
ret = ioctl(spidev_fd, SPI_IOC_MESSAGE(2), &tr); |
| 167 |
if (ret == 1) { |
| 168 |
printf("%s: Can't send spi message\n\r", __func__); |
| 169 |
return -EIO; |
| 170 |
} |
| 171 |
} else { |
| 172 |
#ifdef FMCOMMS5 |
| 173 |
ret = ioctl(spidev_b_fd, SPI_IOC_MESSAGE(2), &tr); |
| 174 |
if (ret == 1) { |
| 175 |
printf("%s: Can't send spi message\n\r", __func__); |
| 176 |
return -EIO; |
| 177 |
} |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
return ret; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
void gpio_init(uint32_t device_id) |
| 188 |
{ |
| 189 |
int fd, len; |
| 190 |
char buf[11]; |
| 191 |
int ret; |
| 192 |
|
| 193 |
fd = open("/sys/class/gpio/export", O_WRONLY); |
| 194 |
if (fd < 0) { |
| 195 |
printf("%s: Can't export GPIO\n\r", __func__); |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
len = snprintf(buf, sizeof(buf), "%d", device_id); |
| 200 |
ret = write(fd, buf, len); |
| 201 |
if (ret == -1) { |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
close(fd); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
void gpio_direction(uint16_t pin, uint8_t direction) |
| 212 |
{ |
| 213 |
int fd; |
| 214 |
char buf[60]; |
| 215 |
int ret; |
| 216 |
|
| 217 |
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", pin); |
| 218 |
|
| 219 |
fd = open(buf, O_WRONLY); |
| 220 |
if (fd < 0) { |
| 221 |
printf("%s: Can't set GPIO direction\n\r", __func__); |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
if (direction == 1) |
| 226 |
ret = write(fd, "out", 4); |
| 227 |
else |
| 228 |
ret = write(fd, "in", 3); |
| 229 |
if (ret == -1) { |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
close(fd); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
bool gpio_is_valid(int number) |
| 240 |
{ |
| 241 |
if(number >= 0) |
| 242 |
return 1; |
| 243 |
else |
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
void gpio_data(uint16_t pin, uint8_t data) |
| 251 |
{ |
| 252 |
int fd; |
| 253 |
char buf[60]; |
| 254 |
int ret; |
| 255 |
|
| 256 |
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", pin); |
| 257 |
|
| 258 |
fd = open(buf, O_WRONLY); |
| 259 |
if (fd < 0) { |
| 260 |
printf("%s: Can't set GPIO value\n\r", __func__); |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if (data) |
| 265 |
ret = write(fd, "1", 2); |
| 266 |
else |
| 267 |
ret = write(fd, "0", 2); |
| 268 |
if (ret == -1) { |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
close(fd); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
void gpio_set_value(unsigned gpio, int value) |
| 279 |
{ |
| 280 |
gpio_data(gpio, value); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
void udelay(unsigned long usecs) |
| 287 |
{ |
| 288 |
usleep(usecs); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
void mdelay(unsigned long msecs) |
| 295 |
{ |
| 296 |
usleep(msecs * 1000); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
unsigned long msleep_interruptible(unsigned int msecs) |
| 303 |
{ |
| 304 |
usleep(msecs * 1000); |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
void axiadc_init(struct ad9361_rf_phy *phy) |
| 312 |
{ |
| 313 |
adc_init(phy); |
| 314 |
dac_init(phy, DATA_SEL_DDS, 0); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
int axiadc_post_setup(struct ad9361_rf_phy *phy) |
| 321 |
{ |
| 322 |
return ad9361_post_setup(phy); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
unsigned int axiadc_read(struct axiadc_state *st, unsigned long reg) |
| 329 |
{ |
| 330 |
unsigned int val; |
| 331 |
|
| 332 |
adc_read(st->phy, reg, &val); |
| 333 |
|
| 334 |
return val; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
void axiadc_write(struct axiadc_state *st, unsigned reg, unsigned val) |
| 341 |
{ |
| 342 |
adc_write(st->phy, reg, val); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
int axiadc_set_pnsel(struct axiadc_state *st, int channel, enum adc_pn_sel sel) |
| 349 |
{ |
| 350 |
unsigned reg; |
| 351 |
|
| 352 |
uint32_t version = axiadc_read(st, 0x4000); |
| 353 |
|
| 354 |
if (PCORE_VERSION_MAJOR(version) > 7) { |
| 355 |
reg = axiadc_read(st, ADI_REG_CHAN_CNTRL_3(channel)); |
| 356 |
reg &= ~ADI_ADC_PN_SEL(~0); |
| 357 |
reg |= ADI_ADC_PN_SEL(sel); |
| 358 |
axiadc_write(st, ADI_REG_CHAN_CNTRL_3(channel), reg); |
| 359 |
} else { |
| 360 |
reg = axiadc_read(st, ADI_REG_CHAN_CNTRL(channel)); |
| 361 |
|
| 362 |
if (sel == ADC_PN_CUSTOM) { |
| 363 |
reg |= ADI_PN_SEL; |
| 364 |
} else if (sel == ADC_PN9) { |
| 365 |
reg &= ~ADI_PN23_TYPE; |
| 366 |
reg &= ~ADI_PN_SEL; |
| 367 |
} else { |
| 368 |
reg |= ADI_PN23_TYPE; |
| 369 |
reg &= ~ADI_PN_SEL; |
| 370 |
} |
| 371 |
|
| 372 |
axiadc_write(st, ADI_REG_CHAN_CNTRL(channel), reg); |
| 373 |
} |
| 374 |
|
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
void axiadc_idelay_set(struct axiadc_state *st, |
| 382 |
unsigned lane, unsigned val) |
| 383 |
{ |
| 384 |
if (PCORE_VERSION_MAJOR(st->pcore_version) > 8) { |
| 385 |
axiadc_write(st, ADI_REG_DELAY(lane), val); |
| 386 |
} else { |
| 387 |
axiadc_write(st, ADI_REG_DELAY_CNTRL, 0); |
| 388 |
axiadc_write(st, ADI_REG_DELAY_CNTRL, |
| 389 |
ADI_DELAY_ADDRESS(lane) |
| 390 |
| ADI_DELAY_WDATA(val) |
| 391 |
| ADI_DELAY_SEL); |
| 392 |
} |
| 393 |
} |