18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * AD9523 SPI Low Jitter Clock Generator 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2012 Analog Devices Inc. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/device.h> 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/slab.h> 118c2ecf20Sopenharmony_ci#include <linux/sysfs.h> 128c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 138c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 148c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 158c2ecf20Sopenharmony_ci#include <linux/err.h> 168c2ecf20Sopenharmony_ci#include <linux/module.h> 178c2ecf20Sopenharmony_ci#include <linux/delay.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 208c2ecf20Sopenharmony_ci#include <linux/iio/sysfs.h> 218c2ecf20Sopenharmony_ci#include <linux/iio/frequency/ad9523.h> 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define AD9523_READ (1 << 15) 248c2ecf20Sopenharmony_ci#define AD9523_WRITE (0 << 15) 258c2ecf20Sopenharmony_ci#define AD9523_CNT(x) (((x) - 1) << 13) 268c2ecf20Sopenharmony_ci#define AD9523_ADDR(x) ((x) & 0xFFF) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define AD9523_R1B (1 << 16) 298c2ecf20Sopenharmony_ci#define AD9523_R2B (2 << 16) 308c2ecf20Sopenharmony_ci#define AD9523_R3B (3 << 16) 318c2ecf20Sopenharmony_ci#define AD9523_TRANSF_LEN(x) ((x) >> 16) 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define AD9523_SERIAL_PORT_CONFIG (AD9523_R1B | 0x0) 348c2ecf20Sopenharmony_ci#define AD9523_VERSION_REGISTER (AD9523_R1B | 0x2) 358c2ecf20Sopenharmony_ci#define AD9523_PART_REGISTER (AD9523_R1B | 0x3) 368c2ecf20Sopenharmony_ci#define AD9523_READBACK_CTRL (AD9523_R1B | 0x4) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CUSTOMER_VERSION_ID (AD9523_R2B | 0x6) 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_A_DIVIDER (AD9523_R2B | 0x11) 418c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_B_DIVIDER (AD9523_R2B | 0x13) 428c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_TEST_DIVIDER (AD9523_R1B | 0x14) 438c2ecf20Sopenharmony_ci#define AD9523_PLL1_FEEDBACK_DIVIDER (AD9523_R2B | 0x17) 448c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_CTRL (AD9523_R2B | 0x19) 458c2ecf20Sopenharmony_ci#define AD9523_PLL1_INPUT_RECEIVERS_CTRL (AD9523_R1B | 0x1A) 468c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_CTRL (AD9523_R1B | 0x1B) 478c2ecf20Sopenharmony_ci#define AD9523_PLL1_MISC_CTRL (AD9523_R1B | 0x1C) 488c2ecf20Sopenharmony_ci#define AD9523_PLL1_LOOP_FILTER_CTRL (AD9523_R1B | 0x1D) 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP (AD9523_R1B | 0xF0) 518c2ecf20Sopenharmony_ci#define AD9523_PLL2_FEEDBACK_DIVIDER_AB (AD9523_R1B | 0xF1) 528c2ecf20Sopenharmony_ci#define AD9523_PLL2_CTRL (AD9523_R1B | 0xF2) 538c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_CTRL (AD9523_R1B | 0xF3) 548c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_DIVIDER (AD9523_R1B | 0xF4) 558c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOOP_FILTER_CTRL (AD9523_R2B | 0xF6) 568c2ecf20Sopenharmony_ci#define AD9523_PLL2_R2_DIVIDER (AD9523_R1B | 0xF7) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#define AD9523_CHANNEL_CLOCK_DIST(ch) (AD9523_R3B | (0x192 + 3 * ch)) 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTPUT_CTRL (AD9523_R1B | 0x1BA) 618c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTPUT_CHANNEL_CTRL (AD9523_R1B | 0x1BB) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define AD9523_READBACK_0 (AD9523_R1B | 0x22C) 648c2ecf20Sopenharmony_ci#define AD9523_READBACK_1 (AD9523_R1B | 0x22D) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define AD9523_STATUS_SIGNALS (AD9523_R3B | 0x232) 678c2ecf20Sopenharmony_ci#define AD9523_POWER_DOWN_CTRL (AD9523_R1B | 0x233) 688c2ecf20Sopenharmony_ci#define AD9523_IO_UPDATE (AD9523_R1B | 0x234) 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci#define AD9523_EEPROM_DATA_XFER_STATUS (AD9523_R1B | 0xB00) 718c2ecf20Sopenharmony_ci#define AD9523_EEPROM_ERROR_READBACK (AD9523_R1B | 0xB01) 728c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CTRL1 (AD9523_R1B | 0xB02) 738c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CTRL2 (AD9523_R1B | 0xB03) 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* AD9523_SERIAL_PORT_CONFIG */ 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#define AD9523_SER_CONF_SDO_ACTIVE (1 << 7) 788c2ecf20Sopenharmony_ci#define AD9523_SER_CONF_SOFT_RESET (1 << 5) 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci/* AD9523_READBACK_CTRL */ 818c2ecf20Sopenharmony_ci#define AD9523_READBACK_CTRL_READ_BUFFERED (1 << 0) 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci/* AD9523_PLL1_CHARGE_PUMP_CTRL */ 848c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_CURRENT_nA(x) (((x) / 500) & 0x7F) 858c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_TRISTATE (1 << 7) 868c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_MODE_NORMAL (3 << 8) 878c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_MODE_PUMP_DOWN (2 << 8) 888c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_MODE_PUMP_UP (1 << 8) 898c2ecf20Sopenharmony_ci#define AD9523_PLL1_CHARGE_PUMP_MODE_TRISTATE (0 << 8) 908c2ecf20Sopenharmony_ci#define AD9523_PLL1_BACKLASH_PW_MIN (0 << 10) 918c2ecf20Sopenharmony_ci#define AD9523_PLL1_BACKLASH_PW_LOW (1 << 10) 928c2ecf20Sopenharmony_ci#define AD9523_PLL1_BACKLASH_PW_HIGH (2 << 10) 938c2ecf20Sopenharmony_ci#define AD9523_PLL1_BACKLASH_PW_MAX (3 << 10) 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci/* AD9523_PLL1_INPUT_RECEIVERS_CTRL */ 968c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_TEST_RCV_EN (1 << 7) 978c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFB_DIFF_RCV_EN (1 << 6) 988c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFA_DIFF_RCV_EN (1 << 5) 998c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFB_RCV_EN (1 << 4) 1008c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFA_RCV_EN (1 << 3) 1018c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFA_REFB_PWR_CTRL_EN (1 << 2) 1028c2ecf20Sopenharmony_ci#define AD9523_PLL1_OSC_IN_CMOS_NEG_INP_EN (1 << 1) 1038c2ecf20Sopenharmony_ci#define AD9523_PLL1_OSC_IN_DIFF_EN (1 << 0) 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/* AD9523_PLL1_REF_CTRL */ 1068c2ecf20Sopenharmony_ci#define AD9523_PLL1_BYPASS_REF_TEST_DIV_EN (1 << 7) 1078c2ecf20Sopenharmony_ci#define AD9523_PLL1_BYPASS_FEEDBACK_DIV_EN (1 << 6) 1088c2ecf20Sopenharmony_ci#define AD9523_PLL1_ZERO_DELAY_MODE_INT (1 << 5) 1098c2ecf20Sopenharmony_ci#define AD9523_PLL1_ZERO_DELAY_MODE_EXT (0 << 5) 1108c2ecf20Sopenharmony_ci#define AD9523_PLL1_OSC_IN_PLL_FEEDBACK_EN (1 << 4) 1118c2ecf20Sopenharmony_ci#define AD9523_PLL1_ZD_IN_CMOS_NEG_INP_EN (1 << 3) 1128c2ecf20Sopenharmony_ci#define AD9523_PLL1_ZD_IN_DIFF_EN (1 << 2) 1138c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFB_CMOS_NEG_INP_EN (1 << 1) 1148c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFA_CMOS_NEG_INP_EN (1 << 0) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci/* AD9523_PLL1_MISC_CTRL */ 1178c2ecf20Sopenharmony_ci#define AD9523_PLL1_REFB_INDEP_DIV_CTRL_EN (1 << 7) 1188c2ecf20Sopenharmony_ci#define AD9523_PLL1_OSC_CTRL_FAIL_VCC_BY2_EN (1 << 6) 1198c2ecf20Sopenharmony_ci#define AD9523_PLL1_REF_MODE(x) ((x) << 2) 1208c2ecf20Sopenharmony_ci#define AD9523_PLL1_BYPASS_REFB_DIV (1 << 1) 1218c2ecf20Sopenharmony_ci#define AD9523_PLL1_BYPASS_REFA_DIV (1 << 0) 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci/* AD9523_PLL1_LOOP_FILTER_CTRL */ 1248c2ecf20Sopenharmony_ci#define AD9523_PLL1_LOOP_FILTER_RZERO(x) ((x) & 0xF) 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci/* AD9523_PLL2_CHARGE_PUMP */ 1278c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP_CURRENT_nA(x) ((x) / 3500) 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci/* AD9523_PLL2_FEEDBACK_DIVIDER_AB */ 1308c2ecf20Sopenharmony_ci#define AD9523_PLL2_FB_NDIV_A_CNT(x) (((x) & 0x3) << 6) 1318c2ecf20Sopenharmony_ci#define AD9523_PLL2_FB_NDIV_B_CNT(x) (((x) & 0x3F) << 0) 1328c2ecf20Sopenharmony_ci#define AD9523_PLL2_FB_NDIV(a, b) (4 * (b) + (a)) 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci/* AD9523_PLL2_CTRL */ 1358c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP_MODE_NORMAL (3 << 0) 1368c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP_MODE_PUMP_DOWN (2 << 0) 1378c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP_MODE_PUMP_UP (1 << 0) 1388c2ecf20Sopenharmony_ci#define AD9523_PLL2_CHARGE_PUMP_MODE_TRISTATE (0 << 0) 1398c2ecf20Sopenharmony_ci#define AD9523_PLL2_BACKLASH_PW_MIN (0 << 2) 1408c2ecf20Sopenharmony_ci#define AD9523_PLL2_BACKLASH_PW_LOW (1 << 2) 1418c2ecf20Sopenharmony_ci#define AD9523_PLL2_BACKLASH_PW_HIGH (2 << 2) 1428c2ecf20Sopenharmony_ci#define AD9523_PLL2_BACKLASH_PW_MAX (3 << 1) 1438c2ecf20Sopenharmony_ci#define AD9523_PLL2_BACKLASH_CTRL_EN (1 << 4) 1448c2ecf20Sopenharmony_ci#define AD9523_PLL2_FREQ_DOUBLER_EN (1 << 5) 1458c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOCK_DETECT_PWR_DOWN_EN (1 << 7) 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci/* AD9523_PLL2_VCO_CTRL */ 1488c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_CALIBRATE (1 << 1) 1498c2ecf20Sopenharmony_ci#define AD9523_PLL2_FORCE_VCO_MIDSCALE (1 << 2) 1508c2ecf20Sopenharmony_ci#define AD9523_PLL2_FORCE_REFERENCE_VALID (1 << 3) 1518c2ecf20Sopenharmony_ci#define AD9523_PLL2_FORCE_RELEASE_SYNC (1 << 4) 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/* AD9523_PLL2_VCO_DIVIDER */ 1548c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_DIV_M1(x) ((((x) - 3) & 0x3) << 0) 1558c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_DIV_M2(x) ((((x) - 3) & 0x3) << 4) 1568c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_DIV_M1_PWR_DOWN_EN (1 << 2) 1578c2ecf20Sopenharmony_ci#define AD9523_PLL2_VCO_DIV_M2_PWR_DOWN_EN (1 << 6) 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci/* AD9523_PLL2_LOOP_FILTER_CTRL */ 1608c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOOP_FILTER_CPOLE1(x) (((x) & 0x7) << 0) 1618c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOOP_FILTER_RZERO(x) (((x) & 0x7) << 3) 1628c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOOP_FILTER_RPOLE2(x) (((x) & 0x7) << 6) 1638c2ecf20Sopenharmony_ci#define AD9523_PLL2_LOOP_FILTER_RZERO_BYPASS_EN (1 << 8) 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* AD9523_PLL2_R2_DIVIDER */ 1668c2ecf20Sopenharmony_ci#define AD9523_PLL2_R2_DIVIDER_VAL(x) (((x) & 0x1F) << 0) 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/* AD9523_CHANNEL_CLOCK_DIST */ 1698c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_DIV_PHASE(x) (((x) & 0x3F) << 18) 1708c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_DIV_PHASE_REV(x) ((ret >> 18) & 0x3F) 1718c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_DIV(x) ((((x) - 1) & 0x3FF) << 8) 1728c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_DIV_REV(x) (((ret >> 8) & 0x3FF) + 1) 1738c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_INV_DIV_OUTPUT_EN (1 << 7) 1748c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_IGNORE_SYNC_EN (1 << 6) 1758c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_PWR_DOWN_EN (1 << 5) 1768c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_LOW_PWR_MODE_EN (1 << 4) 1778c2ecf20Sopenharmony_ci#define AD9523_CLK_DIST_DRIVER_MODE(x) (((x) & 0xF) << 0) 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci/* AD9523_PLL1_OUTPUT_CTRL */ 1808c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH6_M2 (1 << 7) 1818c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH5_M2 (1 << 6) 1828c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH4_M2 (1 << 5) 1838c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_CMOS_DRV_WEAK (1 << 4) 1848c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_1 (0 << 0) 1858c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_2 (1 << 0) 1868c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_4 (2 << 0) 1878c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_8 (4 << 0) 1888c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CTRL_OUTPUT_DIV_16 (8 << 0) 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* AD9523_PLL1_OUTPUT_CHANNEL_CTRL */ 1918c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_OUTPUT_PWR_DOWN_EN (1 << 7) 1928c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH9_M2 (1 << 6) 1938c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH8_M2 (1 << 5) 1948c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH7_M2 (1 << 4) 1958c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH3 (1 << 3) 1968c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH2 (1 << 2) 1978c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH1 (1 << 1) 1988c2ecf20Sopenharmony_ci#define AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH0 (1 << 0) 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci/* AD9523_READBACK_0 */ 2018c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_PLL2_REF_CLK (1 << 7) 2028c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_PLL2_FB_CLK (1 << 6) 2038c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_VCXO (1 << 5) 2048c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_REF_TEST (1 << 4) 2058c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_REFB (1 << 3) 2068c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_REFA (1 << 2) 2078c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_PLL2_LD (1 << 1) 2088c2ecf20Sopenharmony_ci#define AD9523_READBACK_0_STAT_PLL1_LD (1 << 0) 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci/* AD9523_READBACK_1 */ 2118c2ecf20Sopenharmony_ci#define AD9523_READBACK_1_HOLDOVER_ACTIVE (1 << 3) 2128c2ecf20Sopenharmony_ci#define AD9523_READBACK_1_AUTOMODE_SEL_REFB (1 << 2) 2138c2ecf20Sopenharmony_ci#define AD9523_READBACK_1_VCO_CALIB_IN_PROGRESS (1 << 0) 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci/* AD9523_STATUS_SIGNALS */ 2168c2ecf20Sopenharmony_ci#define AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL (1 << 16) 2178c2ecf20Sopenharmony_ci#define AD9523_STATUS_MONITOR_01_PLL12_LOCKED (0x302) 2188c2ecf20Sopenharmony_ci/* AD9523_POWER_DOWN_CTRL */ 2198c2ecf20Sopenharmony_ci#define AD9523_POWER_DOWN_CTRL_PLL1_PWR_DOWN (1 << 2) 2208c2ecf20Sopenharmony_ci#define AD9523_POWER_DOWN_CTRL_PLL2_PWR_DOWN (1 << 1) 2218c2ecf20Sopenharmony_ci#define AD9523_POWER_DOWN_CTRL_DIST_PWR_DOWN (1 << 0) 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci/* AD9523_IO_UPDATE */ 2248c2ecf20Sopenharmony_ci#define AD9523_IO_UPDATE_EN (1 << 0) 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci/* AD9523_EEPROM_DATA_XFER_STATUS */ 2278c2ecf20Sopenharmony_ci#define AD9523_EEPROM_DATA_XFER_IN_PROGRESS (1 << 0) 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci/* AD9523_EEPROM_ERROR_READBACK */ 2308c2ecf20Sopenharmony_ci#define AD9523_EEPROM_ERROR_READBACK_FAIL (1 << 0) 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci/* AD9523_EEPROM_CTRL1 */ 2338c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CTRL1_SOFT_EEPROM (1 << 1) 2348c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CTRL1_EEPROM_WRITE_PROT_DIS (1 << 0) 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci/* AD9523_EEPROM_CTRL2 */ 2378c2ecf20Sopenharmony_ci#define AD9523_EEPROM_CTRL2_REG2EEPROM (1 << 0) 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci#define AD9523_NUM_CHAN 14 2408c2ecf20Sopenharmony_ci#define AD9523_NUM_CHAN_ALT_CLK_SRC 10 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci/* Helpers to avoid excess line breaks */ 2438c2ecf20Sopenharmony_ci#define AD_IFE(_pde, _a, _b) ((pdata->_pde) ? _a : _b) 2448c2ecf20Sopenharmony_ci#define AD_IF(_pde, _a) AD_IFE(_pde, _a, 0) 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cienum { 2478c2ecf20Sopenharmony_ci AD9523_STAT_PLL1_LD, 2488c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_LD, 2498c2ecf20Sopenharmony_ci AD9523_STAT_REFA, 2508c2ecf20Sopenharmony_ci AD9523_STAT_REFB, 2518c2ecf20Sopenharmony_ci AD9523_STAT_REF_TEST, 2528c2ecf20Sopenharmony_ci AD9523_STAT_VCXO, 2538c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_FB_CLK, 2548c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_REF_CLK, 2558c2ecf20Sopenharmony_ci AD9523_SYNC, 2568c2ecf20Sopenharmony_ci AD9523_EEPROM, 2578c2ecf20Sopenharmony_ci}; 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_cienum { 2608c2ecf20Sopenharmony_ci AD9523_VCO1, 2618c2ecf20Sopenharmony_ci AD9523_VCO2, 2628c2ecf20Sopenharmony_ci AD9523_VCXO, 2638c2ecf20Sopenharmony_ci AD9523_NUM_CLK_SRC, 2648c2ecf20Sopenharmony_ci}; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistruct ad9523_state { 2678c2ecf20Sopenharmony_ci struct spi_device *spi; 2688c2ecf20Sopenharmony_ci struct regulator *reg; 2698c2ecf20Sopenharmony_ci struct ad9523_platform_data *pdata; 2708c2ecf20Sopenharmony_ci struct iio_chan_spec ad9523_channels[AD9523_NUM_CHAN]; 2718c2ecf20Sopenharmony_ci struct gpio_desc *pwrdown_gpio; 2728c2ecf20Sopenharmony_ci struct gpio_desc *reset_gpio; 2738c2ecf20Sopenharmony_ci struct gpio_desc *sync_gpio; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci unsigned long vcxo_freq; 2768c2ecf20Sopenharmony_ci unsigned long vco_freq; 2778c2ecf20Sopenharmony_ci unsigned long vco_out_freq[AD9523_NUM_CLK_SRC]; 2788c2ecf20Sopenharmony_ci unsigned char vco_out_map[AD9523_NUM_CHAN_ALT_CLK_SRC]; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci /* 2818c2ecf20Sopenharmony_ci * Lock for accessing device registers. Some operations require 2828c2ecf20Sopenharmony_ci * multiple consecutive R/W operations, during which the device 2838c2ecf20Sopenharmony_ci * shouldn't be interrupted. The buffers are also shared across 2848c2ecf20Sopenharmony_ci * all operations so need to be protected on stand alone reads and 2858c2ecf20Sopenharmony_ci * writes. 2868c2ecf20Sopenharmony_ci */ 2878c2ecf20Sopenharmony_ci struct mutex lock; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci /* 2908c2ecf20Sopenharmony_ci * DMA (thus cache coherency maintenance) requires the 2918c2ecf20Sopenharmony_ci * transfer buffers to live in their own cache lines. 2928c2ecf20Sopenharmony_ci */ 2938c2ecf20Sopenharmony_ci union { 2948c2ecf20Sopenharmony_ci __be32 d32; 2958c2ecf20Sopenharmony_ci u8 d8[4]; 2968c2ecf20Sopenharmony_ci } data[2] ____cacheline_aligned; 2978c2ecf20Sopenharmony_ci}; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic int ad9523_read(struct iio_dev *indio_dev, unsigned int addr) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 3028c2ecf20Sopenharmony_ci int ret; 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci /* We encode the register size 1..3 bytes into the register address. 3058c2ecf20Sopenharmony_ci * On transfer we get the size from the register datum, and make sure 3068c2ecf20Sopenharmony_ci * the result is properly aligned. 3078c2ecf20Sopenharmony_ci */ 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci struct spi_transfer t[] = { 3108c2ecf20Sopenharmony_ci { 3118c2ecf20Sopenharmony_ci .tx_buf = &st->data[0].d8[2], 3128c2ecf20Sopenharmony_ci .len = 2, 3138c2ecf20Sopenharmony_ci }, { 3148c2ecf20Sopenharmony_ci .rx_buf = &st->data[1].d8[4 - AD9523_TRANSF_LEN(addr)], 3158c2ecf20Sopenharmony_ci .len = AD9523_TRANSF_LEN(addr), 3168c2ecf20Sopenharmony_ci }, 3178c2ecf20Sopenharmony_ci }; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci st->data[0].d32 = cpu_to_be32(AD9523_READ | 3208c2ecf20Sopenharmony_ci AD9523_CNT(AD9523_TRANSF_LEN(addr)) | 3218c2ecf20Sopenharmony_ci AD9523_ADDR(addr)); 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t)); 3248c2ecf20Sopenharmony_ci if (ret < 0) 3258c2ecf20Sopenharmony_ci dev_err(&indio_dev->dev, "read failed (%d)", ret); 3268c2ecf20Sopenharmony_ci else 3278c2ecf20Sopenharmony_ci ret = be32_to_cpu(st->data[1].d32) & (0xFFFFFF >> 3288c2ecf20Sopenharmony_ci (8 * (3 - AD9523_TRANSF_LEN(addr)))); 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return ret; 3318c2ecf20Sopenharmony_ci}; 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic int ad9523_write(struct iio_dev *indio_dev, 3348c2ecf20Sopenharmony_ci unsigned int addr, unsigned int val) 3358c2ecf20Sopenharmony_ci{ 3368c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 3378c2ecf20Sopenharmony_ci int ret; 3388c2ecf20Sopenharmony_ci struct spi_transfer t[] = { 3398c2ecf20Sopenharmony_ci { 3408c2ecf20Sopenharmony_ci .tx_buf = &st->data[0].d8[2], 3418c2ecf20Sopenharmony_ci .len = 2, 3428c2ecf20Sopenharmony_ci }, { 3438c2ecf20Sopenharmony_ci .tx_buf = &st->data[1].d8[4 - AD9523_TRANSF_LEN(addr)], 3448c2ecf20Sopenharmony_ci .len = AD9523_TRANSF_LEN(addr), 3458c2ecf20Sopenharmony_ci }, 3468c2ecf20Sopenharmony_ci }; 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci st->data[0].d32 = cpu_to_be32(AD9523_WRITE | 3498c2ecf20Sopenharmony_ci AD9523_CNT(AD9523_TRANSF_LEN(addr)) | 3508c2ecf20Sopenharmony_ci AD9523_ADDR(addr)); 3518c2ecf20Sopenharmony_ci st->data[1].d32 = cpu_to_be32(val); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci ret = spi_sync_transfer(st->spi, t, ARRAY_SIZE(t)); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci if (ret < 0) 3568c2ecf20Sopenharmony_ci dev_err(&indio_dev->dev, "write failed (%d)", ret); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci return ret; 3598c2ecf20Sopenharmony_ci} 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_cistatic int ad9523_io_update(struct iio_dev *indio_dev) 3628c2ecf20Sopenharmony_ci{ 3638c2ecf20Sopenharmony_ci return ad9523_write(indio_dev, AD9523_IO_UPDATE, AD9523_IO_UPDATE_EN); 3648c2ecf20Sopenharmony_ci} 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_cistatic int ad9523_vco_out_map(struct iio_dev *indio_dev, 3678c2ecf20Sopenharmony_ci unsigned int ch, unsigned int out) 3688c2ecf20Sopenharmony_ci{ 3698c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 3708c2ecf20Sopenharmony_ci int ret; 3718c2ecf20Sopenharmony_ci unsigned int mask; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci switch (ch) { 3748c2ecf20Sopenharmony_ci case 0 ... 3: 3758c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CHANNEL_CTRL); 3768c2ecf20Sopenharmony_ci if (ret < 0) 3778c2ecf20Sopenharmony_ci break; 3788c2ecf20Sopenharmony_ci mask = AD9523_PLL1_OUTP_CH_CTRL_VCXO_SRC_SEL_CH0 << ch; 3798c2ecf20Sopenharmony_ci if (out) { 3808c2ecf20Sopenharmony_ci ret |= mask; 3818c2ecf20Sopenharmony_ci out = 2; 3828c2ecf20Sopenharmony_ci } else { 3838c2ecf20Sopenharmony_ci ret &= ~mask; 3848c2ecf20Sopenharmony_ci } 3858c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, 3868c2ecf20Sopenharmony_ci AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret); 3878c2ecf20Sopenharmony_ci break; 3888c2ecf20Sopenharmony_ci case 4 ... 6: 3898c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CTRL); 3908c2ecf20Sopenharmony_ci if (ret < 0) 3918c2ecf20Sopenharmony_ci break; 3928c2ecf20Sopenharmony_ci mask = AD9523_PLL1_OUTP_CTRL_VCO_DIV_SEL_CH4_M2 << (ch - 4); 3938c2ecf20Sopenharmony_ci if (out) 3948c2ecf20Sopenharmony_ci ret |= mask; 3958c2ecf20Sopenharmony_ci else 3968c2ecf20Sopenharmony_ci ret &= ~mask; 3978c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_OUTPUT_CTRL, ret); 3988c2ecf20Sopenharmony_ci break; 3998c2ecf20Sopenharmony_ci case 7 ... 9: 4008c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_PLL1_OUTPUT_CHANNEL_CTRL); 4018c2ecf20Sopenharmony_ci if (ret < 0) 4028c2ecf20Sopenharmony_ci break; 4038c2ecf20Sopenharmony_ci mask = AD9523_PLL1_OUTP_CH_CTRL_VCO_DIV_SEL_CH7_M2 << (ch - 7); 4048c2ecf20Sopenharmony_ci if (out) 4058c2ecf20Sopenharmony_ci ret |= mask; 4068c2ecf20Sopenharmony_ci else 4078c2ecf20Sopenharmony_ci ret &= ~mask; 4088c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, 4098c2ecf20Sopenharmony_ci AD9523_PLL1_OUTPUT_CHANNEL_CTRL, ret); 4108c2ecf20Sopenharmony_ci break; 4118c2ecf20Sopenharmony_ci default: 4128c2ecf20Sopenharmony_ci return 0; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci st->vco_out_map[ch] = out; 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci return ret; 4188c2ecf20Sopenharmony_ci} 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_cistatic int ad9523_set_clock_provider(struct iio_dev *indio_dev, 4218c2ecf20Sopenharmony_ci unsigned int ch, unsigned long freq) 4228c2ecf20Sopenharmony_ci{ 4238c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 4248c2ecf20Sopenharmony_ci long tmp1, tmp2; 4258c2ecf20Sopenharmony_ci bool use_alt_clk_src; 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci switch (ch) { 4288c2ecf20Sopenharmony_ci case 0 ... 3: 4298c2ecf20Sopenharmony_ci use_alt_clk_src = (freq == st->vco_out_freq[AD9523_VCXO]); 4308c2ecf20Sopenharmony_ci break; 4318c2ecf20Sopenharmony_ci case 4 ... 9: 4328c2ecf20Sopenharmony_ci tmp1 = st->vco_out_freq[AD9523_VCO1] / freq; 4338c2ecf20Sopenharmony_ci tmp2 = st->vco_out_freq[AD9523_VCO2] / freq; 4348c2ecf20Sopenharmony_ci tmp1 *= freq; 4358c2ecf20Sopenharmony_ci tmp2 *= freq; 4368c2ecf20Sopenharmony_ci use_alt_clk_src = (abs(tmp1 - freq) > abs(tmp2 - freq)); 4378c2ecf20Sopenharmony_ci break; 4388c2ecf20Sopenharmony_ci default: 4398c2ecf20Sopenharmony_ci /* Ch 10..14: No action required, return success */ 4408c2ecf20Sopenharmony_ci return 0; 4418c2ecf20Sopenharmony_ci } 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ci return ad9523_vco_out_map(indio_dev, ch, use_alt_clk_src); 4448c2ecf20Sopenharmony_ci} 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_cistatic int ad9523_store_eeprom(struct iio_dev *indio_dev) 4478c2ecf20Sopenharmony_ci{ 4488c2ecf20Sopenharmony_ci int ret, tmp; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL1, 4518c2ecf20Sopenharmony_ci AD9523_EEPROM_CTRL1_EEPROM_WRITE_PROT_DIS); 4528c2ecf20Sopenharmony_ci if (ret < 0) 4538c2ecf20Sopenharmony_ci return ret; 4548c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL2, 4558c2ecf20Sopenharmony_ci AD9523_EEPROM_CTRL2_REG2EEPROM); 4568c2ecf20Sopenharmony_ci if (ret < 0) 4578c2ecf20Sopenharmony_ci return ret; 4588c2ecf20Sopenharmony_ci 4598c2ecf20Sopenharmony_ci tmp = 4; 4608c2ecf20Sopenharmony_ci do { 4618c2ecf20Sopenharmony_ci msleep(20); 4628c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, 4638c2ecf20Sopenharmony_ci AD9523_EEPROM_DATA_XFER_STATUS); 4648c2ecf20Sopenharmony_ci if (ret < 0) 4658c2ecf20Sopenharmony_ci return ret; 4668c2ecf20Sopenharmony_ci } while ((ret & AD9523_EEPROM_DATA_XFER_IN_PROGRESS) && tmp--); 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_EEPROM_CTRL1, 0); 4698c2ecf20Sopenharmony_ci if (ret < 0) 4708c2ecf20Sopenharmony_ci return ret; 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_EEPROM_ERROR_READBACK); 4738c2ecf20Sopenharmony_ci if (ret < 0) 4748c2ecf20Sopenharmony_ci return ret; 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci if (ret & AD9523_EEPROM_ERROR_READBACK_FAIL) { 4778c2ecf20Sopenharmony_ci dev_err(&indio_dev->dev, "Verify EEPROM failed"); 4788c2ecf20Sopenharmony_ci ret = -EIO; 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return ret; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic int ad9523_sync(struct iio_dev *indio_dev) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci int ret, tmp; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_STATUS_SIGNALS); 4898c2ecf20Sopenharmony_ci if (ret < 0) 4908c2ecf20Sopenharmony_ci return ret; 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ci tmp = ret; 4938c2ecf20Sopenharmony_ci tmp |= AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS, tmp); 4968c2ecf20Sopenharmony_ci if (ret < 0) 4978c2ecf20Sopenharmony_ci return ret; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci ad9523_io_update(indio_dev); 5008c2ecf20Sopenharmony_ci tmp &= ~AD9523_STATUS_SIGNALS_SYNC_MAN_CTRL; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS, tmp); 5038c2ecf20Sopenharmony_ci if (ret < 0) 5048c2ecf20Sopenharmony_ci return ret; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci return ad9523_io_update(indio_dev); 5078c2ecf20Sopenharmony_ci} 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_cistatic ssize_t ad9523_store(struct device *dev, 5108c2ecf20Sopenharmony_ci struct device_attribute *attr, 5118c2ecf20Sopenharmony_ci const char *buf, size_t len) 5128c2ecf20Sopenharmony_ci{ 5138c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 5148c2ecf20Sopenharmony_ci struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5158c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 5168c2ecf20Sopenharmony_ci bool state; 5178c2ecf20Sopenharmony_ci int ret; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci ret = strtobool(buf, &state); 5208c2ecf20Sopenharmony_ci if (ret < 0) 5218c2ecf20Sopenharmony_ci return ret; 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci if (!state) 5248c2ecf20Sopenharmony_ci return len; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 5278c2ecf20Sopenharmony_ci switch ((u32)this_attr->address) { 5288c2ecf20Sopenharmony_ci case AD9523_SYNC: 5298c2ecf20Sopenharmony_ci ret = ad9523_sync(indio_dev); 5308c2ecf20Sopenharmony_ci break; 5318c2ecf20Sopenharmony_ci case AD9523_EEPROM: 5328c2ecf20Sopenharmony_ci ret = ad9523_store_eeprom(indio_dev); 5338c2ecf20Sopenharmony_ci break; 5348c2ecf20Sopenharmony_ci default: 5358c2ecf20Sopenharmony_ci ret = -ENODEV; 5368c2ecf20Sopenharmony_ci } 5378c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci return ret ? ret : len; 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_cistatic ssize_t ad9523_show(struct device *dev, 5438c2ecf20Sopenharmony_ci struct device_attribute *attr, 5448c2ecf20Sopenharmony_ci char *buf) 5458c2ecf20Sopenharmony_ci{ 5468c2ecf20Sopenharmony_ci struct iio_dev *indio_dev = dev_to_iio_dev(dev); 5478c2ecf20Sopenharmony_ci struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); 5488c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 5498c2ecf20Sopenharmony_ci int ret; 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 5528c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_READBACK_0); 5538c2ecf20Sopenharmony_ci if (ret >= 0) { 5548c2ecf20Sopenharmony_ci ret = sprintf(buf, "%d\n", !!(ret & (1 << 5558c2ecf20Sopenharmony_ci (u32)this_attr->address))); 5568c2ecf20Sopenharmony_ci } 5578c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci return ret; 5608c2ecf20Sopenharmony_ci} 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll1_locked, S_IRUGO, 5638c2ecf20Sopenharmony_ci ad9523_show, 5648c2ecf20Sopenharmony_ci NULL, 5658c2ecf20Sopenharmony_ci AD9523_STAT_PLL1_LD); 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll2_locked, S_IRUGO, 5688c2ecf20Sopenharmony_ci ad9523_show, 5698c2ecf20Sopenharmony_ci NULL, 5708c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_LD); 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll1_reference_clk_a_present, S_IRUGO, 5738c2ecf20Sopenharmony_ci ad9523_show, 5748c2ecf20Sopenharmony_ci NULL, 5758c2ecf20Sopenharmony_ci AD9523_STAT_REFA); 5768c2ecf20Sopenharmony_ci 5778c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll1_reference_clk_b_present, S_IRUGO, 5788c2ecf20Sopenharmony_ci ad9523_show, 5798c2ecf20Sopenharmony_ci NULL, 5808c2ecf20Sopenharmony_ci AD9523_STAT_REFB); 5818c2ecf20Sopenharmony_ci 5828c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll1_reference_clk_test_present, S_IRUGO, 5838c2ecf20Sopenharmony_ci ad9523_show, 5848c2ecf20Sopenharmony_ci NULL, 5858c2ecf20Sopenharmony_ci AD9523_STAT_REF_TEST); 5868c2ecf20Sopenharmony_ci 5878c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(vcxo_clk_present, S_IRUGO, 5888c2ecf20Sopenharmony_ci ad9523_show, 5898c2ecf20Sopenharmony_ci NULL, 5908c2ecf20Sopenharmony_ci AD9523_STAT_VCXO); 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll2_feedback_clk_present, S_IRUGO, 5938c2ecf20Sopenharmony_ci ad9523_show, 5948c2ecf20Sopenharmony_ci NULL, 5958c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_FB_CLK); 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(pll2_reference_clk_present, S_IRUGO, 5988c2ecf20Sopenharmony_ci ad9523_show, 5998c2ecf20Sopenharmony_ci NULL, 6008c2ecf20Sopenharmony_ci AD9523_STAT_PLL2_REF_CLK); 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(sync_dividers, S_IWUSR, 6038c2ecf20Sopenharmony_ci NULL, 6048c2ecf20Sopenharmony_ci ad9523_store, 6058c2ecf20Sopenharmony_ci AD9523_SYNC); 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_cistatic IIO_DEVICE_ATTR(store_eeprom, S_IWUSR, 6088c2ecf20Sopenharmony_ci NULL, 6098c2ecf20Sopenharmony_ci ad9523_store, 6108c2ecf20Sopenharmony_ci AD9523_EEPROM); 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_cistatic struct attribute *ad9523_attributes[] = { 6138c2ecf20Sopenharmony_ci &iio_dev_attr_sync_dividers.dev_attr.attr, 6148c2ecf20Sopenharmony_ci &iio_dev_attr_store_eeprom.dev_attr.attr, 6158c2ecf20Sopenharmony_ci &iio_dev_attr_pll2_feedback_clk_present.dev_attr.attr, 6168c2ecf20Sopenharmony_ci &iio_dev_attr_pll2_reference_clk_present.dev_attr.attr, 6178c2ecf20Sopenharmony_ci &iio_dev_attr_pll1_reference_clk_a_present.dev_attr.attr, 6188c2ecf20Sopenharmony_ci &iio_dev_attr_pll1_reference_clk_b_present.dev_attr.attr, 6198c2ecf20Sopenharmony_ci &iio_dev_attr_pll1_reference_clk_test_present.dev_attr.attr, 6208c2ecf20Sopenharmony_ci &iio_dev_attr_vcxo_clk_present.dev_attr.attr, 6218c2ecf20Sopenharmony_ci &iio_dev_attr_pll1_locked.dev_attr.attr, 6228c2ecf20Sopenharmony_ci &iio_dev_attr_pll2_locked.dev_attr.attr, 6238c2ecf20Sopenharmony_ci NULL, 6248c2ecf20Sopenharmony_ci}; 6258c2ecf20Sopenharmony_ci 6268c2ecf20Sopenharmony_cistatic const struct attribute_group ad9523_attribute_group = { 6278c2ecf20Sopenharmony_ci .attrs = ad9523_attributes, 6288c2ecf20Sopenharmony_ci}; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_cistatic int ad9523_read_raw(struct iio_dev *indio_dev, 6318c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 6328c2ecf20Sopenharmony_ci int *val, 6338c2ecf20Sopenharmony_ci int *val2, 6348c2ecf20Sopenharmony_ci long m) 6358c2ecf20Sopenharmony_ci{ 6368c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 6378c2ecf20Sopenharmony_ci unsigned int code; 6388c2ecf20Sopenharmony_ci int ret; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 6418c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel)); 6428c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci if (ret < 0) 6458c2ecf20Sopenharmony_ci return ret; 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci switch (m) { 6488c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 6498c2ecf20Sopenharmony_ci *val = !(ret & AD9523_CLK_DIST_PWR_DOWN_EN); 6508c2ecf20Sopenharmony_ci return IIO_VAL_INT; 6518c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_FREQUENCY: 6528c2ecf20Sopenharmony_ci *val = st->vco_out_freq[st->vco_out_map[chan->channel]] / 6538c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DIV_REV(ret); 6548c2ecf20Sopenharmony_ci return IIO_VAL_INT; 6558c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PHASE: 6568c2ecf20Sopenharmony_ci code = (AD9523_CLK_DIST_DIV_PHASE_REV(ret) * 3141592) / 6578c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DIV_REV(ret); 6588c2ecf20Sopenharmony_ci *val = code / 1000000; 6598c2ecf20Sopenharmony_ci *val2 = code % 1000000; 6608c2ecf20Sopenharmony_ci return IIO_VAL_INT_PLUS_MICRO; 6618c2ecf20Sopenharmony_ci default: 6628c2ecf20Sopenharmony_ci return -EINVAL; 6638c2ecf20Sopenharmony_ci } 6648c2ecf20Sopenharmony_ci}; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_cistatic int ad9523_write_raw(struct iio_dev *indio_dev, 6678c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 6688c2ecf20Sopenharmony_ci int val, 6698c2ecf20Sopenharmony_ci int val2, 6708c2ecf20Sopenharmony_ci long mask) 6718c2ecf20Sopenharmony_ci{ 6728c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 6738c2ecf20Sopenharmony_ci unsigned int reg; 6748c2ecf20Sopenharmony_ci int ret, tmp, code; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 6778c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel)); 6788c2ecf20Sopenharmony_ci if (ret < 0) 6798c2ecf20Sopenharmony_ci goto out; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci reg = ret; 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci switch (mask) { 6848c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 6858c2ecf20Sopenharmony_ci if (val) 6868c2ecf20Sopenharmony_ci reg &= ~AD9523_CLK_DIST_PWR_DOWN_EN; 6878c2ecf20Sopenharmony_ci else 6888c2ecf20Sopenharmony_ci reg |= AD9523_CLK_DIST_PWR_DOWN_EN; 6898c2ecf20Sopenharmony_ci break; 6908c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_FREQUENCY: 6918c2ecf20Sopenharmony_ci if (val <= 0) { 6928c2ecf20Sopenharmony_ci ret = -EINVAL; 6938c2ecf20Sopenharmony_ci goto out; 6948c2ecf20Sopenharmony_ci } 6958c2ecf20Sopenharmony_ci ret = ad9523_set_clock_provider(indio_dev, chan->channel, val); 6968c2ecf20Sopenharmony_ci if (ret < 0) 6978c2ecf20Sopenharmony_ci goto out; 6988c2ecf20Sopenharmony_ci tmp = st->vco_out_freq[st->vco_out_map[chan->channel]] / val; 6998c2ecf20Sopenharmony_ci tmp = clamp(tmp, 1, 1024); 7008c2ecf20Sopenharmony_ci reg &= ~(0x3FF << 8); 7018c2ecf20Sopenharmony_ci reg |= AD9523_CLK_DIST_DIV(tmp); 7028c2ecf20Sopenharmony_ci break; 7038c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_PHASE: 7048c2ecf20Sopenharmony_ci code = val * 1000000 + val2 % 1000000; 7058c2ecf20Sopenharmony_ci tmp = (code * AD9523_CLK_DIST_DIV_REV(ret)) / 3141592; 7068c2ecf20Sopenharmony_ci tmp = clamp(tmp, 0, 63); 7078c2ecf20Sopenharmony_ci reg &= ~AD9523_CLK_DIST_DIV_PHASE(~0); 7088c2ecf20Sopenharmony_ci reg |= AD9523_CLK_DIST_DIV_PHASE(tmp); 7098c2ecf20Sopenharmony_ci break; 7108c2ecf20Sopenharmony_ci default: 7118c2ecf20Sopenharmony_ci ret = -EINVAL; 7128c2ecf20Sopenharmony_ci goto out; 7138c2ecf20Sopenharmony_ci } 7148c2ecf20Sopenharmony_ci 7158c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_CHANNEL_CLOCK_DIST(chan->channel), 7168c2ecf20Sopenharmony_ci reg); 7178c2ecf20Sopenharmony_ci if (ret < 0) 7188c2ecf20Sopenharmony_ci goto out; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci ad9523_io_update(indio_dev); 7218c2ecf20Sopenharmony_ciout: 7228c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 7238c2ecf20Sopenharmony_ci return ret; 7248c2ecf20Sopenharmony_ci} 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_cistatic int ad9523_reg_access(struct iio_dev *indio_dev, 7278c2ecf20Sopenharmony_ci unsigned int reg, unsigned int writeval, 7288c2ecf20Sopenharmony_ci unsigned int *readval) 7298c2ecf20Sopenharmony_ci{ 7308c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 7318c2ecf20Sopenharmony_ci int ret; 7328c2ecf20Sopenharmony_ci 7338c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 7348c2ecf20Sopenharmony_ci if (readval == NULL) { 7358c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, reg | AD9523_R1B, writeval); 7368c2ecf20Sopenharmony_ci ad9523_io_update(indio_dev); 7378c2ecf20Sopenharmony_ci } else { 7388c2ecf20Sopenharmony_ci ret = ad9523_read(indio_dev, reg | AD9523_R1B); 7398c2ecf20Sopenharmony_ci if (ret < 0) 7408c2ecf20Sopenharmony_ci goto out_unlock; 7418c2ecf20Sopenharmony_ci *readval = ret; 7428c2ecf20Sopenharmony_ci ret = 0; 7438c2ecf20Sopenharmony_ci } 7448c2ecf20Sopenharmony_ci 7458c2ecf20Sopenharmony_ciout_unlock: 7468c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci return ret; 7498c2ecf20Sopenharmony_ci} 7508c2ecf20Sopenharmony_ci 7518c2ecf20Sopenharmony_cistatic const struct iio_info ad9523_info = { 7528c2ecf20Sopenharmony_ci .read_raw = &ad9523_read_raw, 7538c2ecf20Sopenharmony_ci .write_raw = &ad9523_write_raw, 7548c2ecf20Sopenharmony_ci .debugfs_reg_access = &ad9523_reg_access, 7558c2ecf20Sopenharmony_ci .attrs = &ad9523_attribute_group, 7568c2ecf20Sopenharmony_ci}; 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_cistatic int ad9523_setup(struct iio_dev *indio_dev) 7598c2ecf20Sopenharmony_ci{ 7608c2ecf20Sopenharmony_ci struct ad9523_state *st = iio_priv(indio_dev); 7618c2ecf20Sopenharmony_ci struct ad9523_platform_data *pdata = st->pdata; 7628c2ecf20Sopenharmony_ci struct ad9523_channel_spec *chan; 7638c2ecf20Sopenharmony_ci unsigned long active_mask = 0; 7648c2ecf20Sopenharmony_ci int ret, i; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_SERIAL_PORT_CONFIG, 7678c2ecf20Sopenharmony_ci AD9523_SER_CONF_SOFT_RESET | 7688c2ecf20Sopenharmony_ci (st->spi->mode & SPI_3WIRE ? 0 : 7698c2ecf20Sopenharmony_ci AD9523_SER_CONF_SDO_ACTIVE)); 7708c2ecf20Sopenharmony_ci if (ret < 0) 7718c2ecf20Sopenharmony_ci return ret; 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_READBACK_CTRL, 7748c2ecf20Sopenharmony_ci AD9523_READBACK_CTRL_READ_BUFFERED); 7758c2ecf20Sopenharmony_ci if (ret < 0) 7768c2ecf20Sopenharmony_ci return ret; 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci ret = ad9523_io_update(indio_dev); 7798c2ecf20Sopenharmony_ci if (ret < 0) 7808c2ecf20Sopenharmony_ci return ret; 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci /* 7838c2ecf20Sopenharmony_ci * PLL1 Setup 7848c2ecf20Sopenharmony_ci */ 7858c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_REF_A_DIVIDER, 7868c2ecf20Sopenharmony_ci pdata->refa_r_div); 7878c2ecf20Sopenharmony_ci if (ret < 0) 7888c2ecf20Sopenharmony_ci return ret; 7898c2ecf20Sopenharmony_ci 7908c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_REF_B_DIVIDER, 7918c2ecf20Sopenharmony_ci pdata->refb_r_div); 7928c2ecf20Sopenharmony_ci if (ret < 0) 7938c2ecf20Sopenharmony_ci return ret; 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_FEEDBACK_DIVIDER, 7968c2ecf20Sopenharmony_ci pdata->pll1_feedback_div); 7978c2ecf20Sopenharmony_ci if (ret < 0) 7988c2ecf20Sopenharmony_ci return ret; 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_CHARGE_PUMP_CTRL, 8018c2ecf20Sopenharmony_ci AD9523_PLL1_CHARGE_PUMP_CURRENT_nA(pdata-> 8028c2ecf20Sopenharmony_ci pll1_charge_pump_current_nA) | 8038c2ecf20Sopenharmony_ci AD9523_PLL1_CHARGE_PUMP_MODE_NORMAL | 8048c2ecf20Sopenharmony_ci AD9523_PLL1_BACKLASH_PW_MIN); 8058c2ecf20Sopenharmony_ci if (ret < 0) 8068c2ecf20Sopenharmony_ci return ret; 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_INPUT_RECEIVERS_CTRL, 8098c2ecf20Sopenharmony_ci AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_RCV_EN) | 8108c2ecf20Sopenharmony_ci AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_RCV_EN) | 8118c2ecf20Sopenharmony_ci AD_IF(osc_in_diff_en, AD9523_PLL1_OSC_IN_DIFF_EN) | 8128c2ecf20Sopenharmony_ci AD_IF(osc_in_cmos_neg_inp_en, 8138c2ecf20Sopenharmony_ci AD9523_PLL1_OSC_IN_CMOS_NEG_INP_EN) | 8148c2ecf20Sopenharmony_ci AD_IF(refa_diff_rcv_en, AD9523_PLL1_REFA_DIFF_RCV_EN) | 8158c2ecf20Sopenharmony_ci AD_IF(refb_diff_rcv_en, AD9523_PLL1_REFB_DIFF_RCV_EN)); 8168c2ecf20Sopenharmony_ci if (ret < 0) 8178c2ecf20Sopenharmony_ci return ret; 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_REF_CTRL, 8208c2ecf20Sopenharmony_ci AD_IF(zd_in_diff_en, AD9523_PLL1_ZD_IN_DIFF_EN) | 8218c2ecf20Sopenharmony_ci AD_IF(zd_in_cmos_neg_inp_en, 8228c2ecf20Sopenharmony_ci AD9523_PLL1_ZD_IN_CMOS_NEG_INP_EN) | 8238c2ecf20Sopenharmony_ci AD_IF(zero_delay_mode_internal_en, 8248c2ecf20Sopenharmony_ci AD9523_PLL1_ZERO_DELAY_MODE_INT) | 8258c2ecf20Sopenharmony_ci AD_IF(osc_in_feedback_en, AD9523_PLL1_OSC_IN_PLL_FEEDBACK_EN) | 8268c2ecf20Sopenharmony_ci AD_IF(refa_cmos_neg_inp_en, AD9523_PLL1_REFA_CMOS_NEG_INP_EN) | 8278c2ecf20Sopenharmony_ci AD_IF(refb_cmos_neg_inp_en, AD9523_PLL1_REFB_CMOS_NEG_INP_EN)); 8288c2ecf20Sopenharmony_ci if (ret < 0) 8298c2ecf20Sopenharmony_ci return ret; 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_MISC_CTRL, 8328c2ecf20Sopenharmony_ci AD9523_PLL1_REFB_INDEP_DIV_CTRL_EN | 8338c2ecf20Sopenharmony_ci AD9523_PLL1_REF_MODE(pdata->ref_mode)); 8348c2ecf20Sopenharmony_ci if (ret < 0) 8358c2ecf20Sopenharmony_ci return ret; 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL1_LOOP_FILTER_CTRL, 8388c2ecf20Sopenharmony_ci AD9523_PLL1_LOOP_FILTER_RZERO(pdata->pll1_loop_filter_rzero)); 8398c2ecf20Sopenharmony_ci if (ret < 0) 8408c2ecf20Sopenharmony_ci return ret; 8418c2ecf20Sopenharmony_ci /* 8428c2ecf20Sopenharmony_ci * PLL2 Setup 8438c2ecf20Sopenharmony_ci */ 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_CHARGE_PUMP, 8468c2ecf20Sopenharmony_ci AD9523_PLL2_CHARGE_PUMP_CURRENT_nA(pdata-> 8478c2ecf20Sopenharmony_ci pll2_charge_pump_current_nA)); 8488c2ecf20Sopenharmony_ci if (ret < 0) 8498c2ecf20Sopenharmony_ci return ret; 8508c2ecf20Sopenharmony_ci 8518c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_FEEDBACK_DIVIDER_AB, 8528c2ecf20Sopenharmony_ci AD9523_PLL2_FB_NDIV_A_CNT(pdata->pll2_ndiv_a_cnt) | 8538c2ecf20Sopenharmony_ci AD9523_PLL2_FB_NDIV_B_CNT(pdata->pll2_ndiv_b_cnt)); 8548c2ecf20Sopenharmony_ci if (ret < 0) 8558c2ecf20Sopenharmony_ci return ret; 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_CTRL, 8588c2ecf20Sopenharmony_ci AD9523_PLL2_CHARGE_PUMP_MODE_NORMAL | 8598c2ecf20Sopenharmony_ci AD9523_PLL2_BACKLASH_CTRL_EN | 8608c2ecf20Sopenharmony_ci AD_IF(pll2_freq_doubler_en, AD9523_PLL2_FREQ_DOUBLER_EN)); 8618c2ecf20Sopenharmony_ci if (ret < 0) 8628c2ecf20Sopenharmony_ci return ret; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci st->vco_freq = div_u64((unsigned long long)pdata->vcxo_freq * 8658c2ecf20Sopenharmony_ci (pdata->pll2_freq_doubler_en ? 2 : 1) * 8668c2ecf20Sopenharmony_ci AD9523_PLL2_FB_NDIV(pdata->pll2_ndiv_a_cnt, 8678c2ecf20Sopenharmony_ci pdata->pll2_ndiv_b_cnt), 8688c2ecf20Sopenharmony_ci pdata->pll2_r2_div); 8698c2ecf20Sopenharmony_ci 8708c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_VCO_CTRL, 8718c2ecf20Sopenharmony_ci AD9523_PLL2_VCO_CALIBRATE); 8728c2ecf20Sopenharmony_ci if (ret < 0) 8738c2ecf20Sopenharmony_ci return ret; 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_VCO_DIVIDER, 8768c2ecf20Sopenharmony_ci AD9523_PLL2_VCO_DIV_M1(pdata->pll2_vco_div_m1) | 8778c2ecf20Sopenharmony_ci AD9523_PLL2_VCO_DIV_M2(pdata->pll2_vco_div_m2) | 8788c2ecf20Sopenharmony_ci AD_IFE(pll2_vco_div_m1, 0, 8798c2ecf20Sopenharmony_ci AD9523_PLL2_VCO_DIV_M1_PWR_DOWN_EN) | 8808c2ecf20Sopenharmony_ci AD_IFE(pll2_vco_div_m2, 0, 8818c2ecf20Sopenharmony_ci AD9523_PLL2_VCO_DIV_M2_PWR_DOWN_EN)); 8828c2ecf20Sopenharmony_ci if (ret < 0) 8838c2ecf20Sopenharmony_ci return ret; 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci if (pdata->pll2_vco_div_m1) 8868c2ecf20Sopenharmony_ci st->vco_out_freq[AD9523_VCO1] = 8878c2ecf20Sopenharmony_ci st->vco_freq / pdata->pll2_vco_div_m1; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci if (pdata->pll2_vco_div_m2) 8908c2ecf20Sopenharmony_ci st->vco_out_freq[AD9523_VCO2] = 8918c2ecf20Sopenharmony_ci st->vco_freq / pdata->pll2_vco_div_m2; 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci st->vco_out_freq[AD9523_VCXO] = pdata->vcxo_freq; 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_R2_DIVIDER, 8968c2ecf20Sopenharmony_ci AD9523_PLL2_R2_DIVIDER_VAL(pdata->pll2_r2_div)); 8978c2ecf20Sopenharmony_ci if (ret < 0) 8988c2ecf20Sopenharmony_ci return ret; 8998c2ecf20Sopenharmony_ci 9008c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_PLL2_LOOP_FILTER_CTRL, 9018c2ecf20Sopenharmony_ci AD9523_PLL2_LOOP_FILTER_CPOLE1(pdata->cpole1) | 9028c2ecf20Sopenharmony_ci AD9523_PLL2_LOOP_FILTER_RZERO(pdata->rzero) | 9038c2ecf20Sopenharmony_ci AD9523_PLL2_LOOP_FILTER_RPOLE2(pdata->rpole2) | 9048c2ecf20Sopenharmony_ci AD_IF(rzero_bypass_en, 9058c2ecf20Sopenharmony_ci AD9523_PLL2_LOOP_FILTER_RZERO_BYPASS_EN)); 9068c2ecf20Sopenharmony_ci if (ret < 0) 9078c2ecf20Sopenharmony_ci return ret; 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci for (i = 0; i < pdata->num_channels; i++) { 9108c2ecf20Sopenharmony_ci chan = &pdata->channels[i]; 9118c2ecf20Sopenharmony_ci if (chan->channel_num < AD9523_NUM_CHAN) { 9128c2ecf20Sopenharmony_ci __set_bit(chan->channel_num, &active_mask); 9138c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, 9148c2ecf20Sopenharmony_ci AD9523_CHANNEL_CLOCK_DIST(chan->channel_num), 9158c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DRIVER_MODE(chan->driver_mode) | 9168c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DIV(chan->channel_divider) | 9178c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DIV_PHASE(chan->divider_phase) | 9188c2ecf20Sopenharmony_ci (chan->sync_ignore_en ? 9198c2ecf20Sopenharmony_ci AD9523_CLK_DIST_IGNORE_SYNC_EN : 0) | 9208c2ecf20Sopenharmony_ci (chan->divider_output_invert_en ? 9218c2ecf20Sopenharmony_ci AD9523_CLK_DIST_INV_DIV_OUTPUT_EN : 0) | 9228c2ecf20Sopenharmony_ci (chan->low_power_mode_en ? 9238c2ecf20Sopenharmony_ci AD9523_CLK_DIST_LOW_PWR_MODE_EN : 0) | 9248c2ecf20Sopenharmony_ci (chan->output_dis ? 9258c2ecf20Sopenharmony_ci AD9523_CLK_DIST_PWR_DOWN_EN : 0)); 9268c2ecf20Sopenharmony_ci if (ret < 0) 9278c2ecf20Sopenharmony_ci return ret; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci ret = ad9523_vco_out_map(indio_dev, chan->channel_num, 9308c2ecf20Sopenharmony_ci chan->use_alt_clock_src); 9318c2ecf20Sopenharmony_ci if (ret < 0) 9328c2ecf20Sopenharmony_ci return ret; 9338c2ecf20Sopenharmony_ci 9348c2ecf20Sopenharmony_ci st->ad9523_channels[i].type = IIO_ALTVOLTAGE; 9358c2ecf20Sopenharmony_ci st->ad9523_channels[i].output = 1; 9368c2ecf20Sopenharmony_ci st->ad9523_channels[i].indexed = 1; 9378c2ecf20Sopenharmony_ci st->ad9523_channels[i].channel = chan->channel_num; 9388c2ecf20Sopenharmony_ci st->ad9523_channels[i].extend_name = 9398c2ecf20Sopenharmony_ci chan->extended_name; 9408c2ecf20Sopenharmony_ci st->ad9523_channels[i].info_mask_separate = 9418c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_RAW) | 9428c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_PHASE) | 9438c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_FREQUENCY); 9448c2ecf20Sopenharmony_ci } 9458c2ecf20Sopenharmony_ci } 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_ci for_each_clear_bit(i, &active_mask, AD9523_NUM_CHAN) { 9488c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, 9498c2ecf20Sopenharmony_ci AD9523_CHANNEL_CLOCK_DIST(i), 9508c2ecf20Sopenharmony_ci AD9523_CLK_DIST_DRIVER_MODE(TRISTATE) | 9518c2ecf20Sopenharmony_ci AD9523_CLK_DIST_PWR_DOWN_EN); 9528c2ecf20Sopenharmony_ci if (ret < 0) 9538c2ecf20Sopenharmony_ci return ret; 9548c2ecf20Sopenharmony_ci } 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_POWER_DOWN_CTRL, 0); 9578c2ecf20Sopenharmony_ci if (ret < 0) 9588c2ecf20Sopenharmony_ci return ret; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci ret = ad9523_write(indio_dev, AD9523_STATUS_SIGNALS, 9618c2ecf20Sopenharmony_ci AD9523_STATUS_MONITOR_01_PLL12_LOCKED); 9628c2ecf20Sopenharmony_ci if (ret < 0) 9638c2ecf20Sopenharmony_ci return ret; 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci ret = ad9523_io_update(indio_dev); 9668c2ecf20Sopenharmony_ci if (ret < 0) 9678c2ecf20Sopenharmony_ci return ret; 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci return 0; 9708c2ecf20Sopenharmony_ci} 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_cistatic void ad9523_reg_disable(void *data) 9738c2ecf20Sopenharmony_ci{ 9748c2ecf20Sopenharmony_ci struct regulator *reg = data; 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci regulator_disable(reg); 9778c2ecf20Sopenharmony_ci} 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_cistatic int ad9523_probe(struct spi_device *spi) 9808c2ecf20Sopenharmony_ci{ 9818c2ecf20Sopenharmony_ci struct ad9523_platform_data *pdata = spi->dev.platform_data; 9828c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 9838c2ecf20Sopenharmony_ci struct ad9523_state *st; 9848c2ecf20Sopenharmony_ci int ret; 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci if (!pdata) { 9878c2ecf20Sopenharmony_ci dev_err(&spi->dev, "no platform data?\n"); 9888c2ecf20Sopenharmony_ci return -EINVAL; 9898c2ecf20Sopenharmony_ci } 9908c2ecf20Sopenharmony_ci 9918c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 9928c2ecf20Sopenharmony_ci if (indio_dev == NULL) 9938c2ecf20Sopenharmony_ci return -ENOMEM; 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_ci st = iio_priv(indio_dev); 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ci mutex_init(&st->lock); 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci st->reg = devm_regulator_get(&spi->dev, "vcc"); 10008c2ecf20Sopenharmony_ci if (!IS_ERR(st->reg)) { 10018c2ecf20Sopenharmony_ci ret = regulator_enable(st->reg); 10028c2ecf20Sopenharmony_ci if (ret) 10038c2ecf20Sopenharmony_ci return ret; 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci ret = devm_add_action_or_reset(&spi->dev, ad9523_reg_disable, 10068c2ecf20Sopenharmony_ci st->reg); 10078c2ecf20Sopenharmony_ci if (ret) 10088c2ecf20Sopenharmony_ci return ret; 10098c2ecf20Sopenharmony_ci } 10108c2ecf20Sopenharmony_ci 10118c2ecf20Sopenharmony_ci st->pwrdown_gpio = devm_gpiod_get_optional(&spi->dev, "powerdown", 10128c2ecf20Sopenharmony_ci GPIOD_OUT_HIGH); 10138c2ecf20Sopenharmony_ci if (IS_ERR(st->pwrdown_gpio)) 10148c2ecf20Sopenharmony_ci return PTR_ERR(st->pwrdown_gpio); 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_ci st->reset_gpio = devm_gpiod_get_optional(&spi->dev, "reset", 10178c2ecf20Sopenharmony_ci GPIOD_OUT_LOW); 10188c2ecf20Sopenharmony_ci if (IS_ERR(st->reset_gpio)) 10198c2ecf20Sopenharmony_ci return PTR_ERR(st->reset_gpio); 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci if (st->reset_gpio) { 10228c2ecf20Sopenharmony_ci udelay(1); 10238c2ecf20Sopenharmony_ci gpiod_direction_output(st->reset_gpio, 1); 10248c2ecf20Sopenharmony_ci } 10258c2ecf20Sopenharmony_ci 10268c2ecf20Sopenharmony_ci st->sync_gpio = devm_gpiod_get_optional(&spi->dev, "sync", 10278c2ecf20Sopenharmony_ci GPIOD_OUT_HIGH); 10288c2ecf20Sopenharmony_ci if (IS_ERR(st->sync_gpio)) 10298c2ecf20Sopenharmony_ci return PTR_ERR(st->sync_gpio); 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_ci spi_set_drvdata(spi, indio_dev); 10328c2ecf20Sopenharmony_ci st->spi = spi; 10338c2ecf20Sopenharmony_ci st->pdata = pdata; 10348c2ecf20Sopenharmony_ci 10358c2ecf20Sopenharmony_ci indio_dev->name = (pdata->name[0] != 0) ? pdata->name : 10368c2ecf20Sopenharmony_ci spi_get_device_id(spi)->name; 10378c2ecf20Sopenharmony_ci indio_dev->info = &ad9523_info; 10388c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 10398c2ecf20Sopenharmony_ci indio_dev->channels = st->ad9523_channels; 10408c2ecf20Sopenharmony_ci indio_dev->num_channels = pdata->num_channels; 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci ret = ad9523_setup(indio_dev); 10438c2ecf20Sopenharmony_ci if (ret < 0) 10448c2ecf20Sopenharmony_ci return ret; 10458c2ecf20Sopenharmony_ci 10468c2ecf20Sopenharmony_ci return devm_iio_device_register(&spi->dev, indio_dev); 10478c2ecf20Sopenharmony_ci} 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_cistatic const struct spi_device_id ad9523_id[] = { 10508c2ecf20Sopenharmony_ci {"ad9523-1", 9523}, 10518c2ecf20Sopenharmony_ci {} 10528c2ecf20Sopenharmony_ci}; 10538c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, ad9523_id); 10548c2ecf20Sopenharmony_ci 10558c2ecf20Sopenharmony_cistatic struct spi_driver ad9523_driver = { 10568c2ecf20Sopenharmony_ci .driver = { 10578c2ecf20Sopenharmony_ci .name = "ad9523", 10588c2ecf20Sopenharmony_ci }, 10598c2ecf20Sopenharmony_ci .probe = ad9523_probe, 10608c2ecf20Sopenharmony_ci .id_table = ad9523_id, 10618c2ecf20Sopenharmony_ci}; 10628c2ecf20Sopenharmony_cimodule_spi_driver(ad9523_driver); 10638c2ecf20Sopenharmony_ci 10648c2ecf20Sopenharmony_ciMODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 10658c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Analog Devices AD9523 CLOCKDIST/PLL"); 10668c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1067