Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad9361/sw/platform_linux/platform.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Platform.c
3 * @brief Implementation of Platform Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
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 /************************ Variables Definitions *******************************/
60 /******************************************************************************/
61 int spidev_fd;
62 #ifdef FMCOMMS5
63 int spidev_b_fd;
64 #endif
65
66 /***************************************************************************//**
67 * @brief spi_init
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 // Unused variable - fix compiler warning
80 }
81 if (clk_pha) {
82 // Unused variable - fix compiler warning
83 }
84 if (clk_pol) {
85 // Unused variable - fix compiler warning
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 * @brief spi_write_then_read
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 // Unused variable - fix compiler warning
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 * @brief gpio_init
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 // Unused variable - fix compiler warning
203 }
204
205 close(fd);
206 }
207
208 /***************************************************************************//**
209 * @brief gpio_direction
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 // Unused variable - fix compiler warning
231 }
232
233 close(fd);
234 }
235
236 /***************************************************************************//**
237 * @brief gpio_is_valid
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 * @brief gpio_data
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 // Unused variable - fix compiler warning
270 }
271
272 close(fd);
273 }
274
275 /***************************************************************************//**
276 * @brief gpio_set_value
277 *******************************************************************************/
278 void gpio_set_value(unsigned gpio, int value)
279 {
280 gpio_data(gpio, value);
281 }
282
283 /***************************************************************************//**
284 * @brief udelay
285 *******************************************************************************/
286 void udelay(unsigned long usecs)
287 {
288 usleep(usecs);
289 }
290
291 /***************************************************************************//**
292 * @brief mdelay
293 *******************************************************************************/
294 void mdelay(unsigned long msecs)
295 {
296 usleep(msecs * 1000);
297 }
298
299 /***************************************************************************//**
300 * @brief msleep_interruptible
301 *******************************************************************************/
302 unsigned long msleep_interruptible(unsigned int msecs)
303 {
304 usleep(msecs * 1000);
305 return 0;
306 }
307
308 /***************************************************************************//**
309 * @brief axiadc_init
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 * @brief axiadc_post_setup
319 *******************************************************************************/
320 int axiadc_post_setup(struct ad9361_rf_phy *phy)
321 {
322 return ad9361_post_setup(phy);
323 }
324
325 /***************************************************************************//**
326 * @brief axiadc_read
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 * @brief axiadc_write
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 * @brief axiadc_set_pnsel
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 * @brief axiadc_idelay_set
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 }