| 1 | /***************************************************************************//** |
| 2 | * @file ad9250.c |
| 3 | * @brief Implementation of AD9250 Driver. |
| 4 | * @author DBogdan ([email protected]) |
| 5 | ******************************************************************************** |
| 6 | * Copyright 2014(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 <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include "platform_drivers.h" |
| 47 | #include "ad9250.h" |
| 48 | |
| 49 | /***************************************************************************//** |
| 50 | * @brief ad9250_spi_read |
| 51 | *******************************************************************************/ |
| 52 | int32_t ad9250_spi_read(ad9250_dev *dev, |
| 53 | uint16_t reg_addr, |
| 54 | uint8_t *reg_data) |
| 55 | { |
| 56 | uint8_t buf[4]; |
| 57 | int32_t ret; |
| 58 | |
| 59 | buf[0] = 0x80 | dev->id_no; |
| 60 | buf[1] = 0x80 | (reg_addr >> 8); |
| 61 | buf[2] = reg_addr & 0xFF; |
| 62 | buf[3] = 0x00; |
| 63 | |
| 64 | ret = spi_write_and_read(&dev->spi_dev, buf, 4); |
| 65 | *reg_data = buf[3]; |
| 66 | |
| 67 | return ret; |
| 68 | } |
| 69 | |
| 70 | /***************************************************************************//** |
| 71 | * @brief ad9250_spi_write |
| 72 | *******************************************************************************/ |
| 73 | int32_t ad9250_spi_write(ad9250_dev *dev, |
| 74 | uint16_t reg_addr, |
| 75 | uint8_t reg_data) |
| 76 | { |
| 77 | uint8_t buf[4]; |
| 78 | int32_t ret; |
| 79 | |
| 80 | buf[0] = 0x80 | dev->id_no; |
| 81 | buf[1] = reg_addr >> 8; |
| 82 | buf[2] = reg_addr & 0xFF; |
| 83 | buf[3] = reg_data; |
| 84 | |
| 85 | ret = spi_write_and_read(&dev->spi_dev, buf, 4); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /***************************************************************************//** |
| 91 | * @brief ad9250_setup |
| 92 | *******************************************************************************/ |
| 93 | int32_t ad9250_setup(ad9250_dev **device, |
| 94 | ad9250_init_param init_param) |
| 95 | { |
| 96 | ad9250_dev *dev; |
| 97 | uint8_t chip_id; |
| 98 | uint8_t stat; |
| 99 | int32_t ret; |
| 100 | |
| 101 | dev = (ad9250_dev *)malloc(sizeof(*dev)); |
| 102 | if (!dev) { |
| 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | dev->spi_dev.chip_select = init_param.spi_chip_select; |
| 107 | dev->spi_dev.mode = init_param.spi_mode; |
| 108 | dev->spi_dev.device_id = init_param.spi_device_id; |
| 109 | dev->spi_dev.type = init_param.spi_type; |
| 110 | ret = spi_init(&dev->spi_dev); |
| 111 | |
| 112 | dev->id_no = init_param.id_no; |
| 113 | |
| 114 | ad9250_spi_read(dev, 0x01, &chip_id); |
| 115 | printf("AD9250 CHIP ID %s (0x%x).\n", |
| 116 | (chip_id == 0xB9) ? "ok" : "errors", chip_id); |
| 117 | |
| 118 | ad9250_spi_write(dev, 0x5f, (0x16 | 0x1)); |
| 119 | ad9250_spi_write(dev, 0x5e, 0x22); |
| 120 | ad9250_spi_write(dev, 0x66, ((dev->id_no*2)+0)); |
| 121 | ad9250_spi_write(dev, 0x67, ((dev->id_no*2)+1)); |
| 122 | ad9250_spi_write(dev, 0x6e, 0x81); |
| 123 | ad9250_spi_write(dev, 0x70, 0x1f); |
| 124 | ad9250_spi_write(dev, 0x3a, 0x13); |
| 125 | ad9250_spi_write(dev, 0x5f, (0x16 | 0x0)); |
| 126 | ad9250_spi_write(dev, 0x14, 0x00); |
| 127 | ad9250_spi_write(dev, 0x0d, 0x00); |
| 128 | ad9250_spi_write(dev, 0xff, 0x01); |
| 129 | mdelay(10); |
| 130 | ad9250_spi_read(dev, 0x0a, &stat); |
| 131 | printf("AD9250 PLL/link %s (0x%x).\n", |
| 132 | (stat == 0x81) ? "ok" : "errors", stat); |
| 133 | |
| 134 | *device = dev; |
| 135 | |
| 136 | return ret; |
| 137 | } |