| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <linux/device.h> |
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/slab.h> |
| 12 |
#include <linux/sysfs.h> |
| 13 |
#include <linux/spi/spi.h> |
| 14 |
#include <linux/regulator/consumer.h> |
| 15 |
#include <linux/err.h> |
| 16 |
#include <linux/module.h> |
| 17 |
#include <linux/gcd.h> |
| 18 |
#include <linux/gpio.h> |
| 19 |
#include <asm/div64.h> |
| 20 |
|
| 21 |
#include "../iio.h" |
| 22 |
#include "../sysfs.h" |
| 23 |
|
| 24 |
#include "adf4350.h" |
| 25 |
|
| 26 |
enum { |
| 27 |
ADF4350_SET_FREQ, |
| 28 |
ADF4350_SET_FREQ_REFIN, |
| 29 |
ADF4350_SET_FREQ_RESOLUTION, |
| 30 |
ADF4350_PLL_LOCKED, |
| 31 |
ADF4350_PLL_PWRDOWN, |
| 32 |
}; |
| 33 |
|
| 34 |
struct adf4350_state { |
| 35 |
struct spi_device *spi; |
| 36 |
struct regulator *reg; |
| 37 |
struct adf4350_platform_data *pdata; |
| 38 |
unsigned long clkin; |
| 39 |
unsigned long chspc; |
| 40 |
unsigned long fpfd; |
| 41 |
unsigned long min_out_freq; |
| 42 |
unsigned r0_fract; |
| 43 |
unsigned r0_int; |
| 44 |
unsigned r1_mod; |
| 45 |
unsigned r4_rf_div_sel; |
| 46 |
unsigned long regs[6]; |
| 47 |
unsigned long regs_hw[6]; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
__be32 val ____cacheline_aligned; |
| 54 |
}; |
| 55 |
|
| 56 |
static struct adf4350_platform_data default_pdata = { |
| 57 |
.clkin = 122880000, |
| 58 |
.channel_spacing = 10000, |
| 59 |
.r2_user_settings = ADF4350_REG2_PD_POLARITY_POS, |
| 60 |
ADF4350_REG2_CHARGE_PUMP_CURR_uA(2500), |
| 61 |
.r3_user_settings = ADF4350_REG3_12BIT_CLKDIV_MODE(0), |
| 62 |
.r4_user_settings = ADF4350_REG4_OUTPUT_PWR(0) | |
| 63 |
ADF4350_REG4_MUTE_TILL_LOCK_EN, |
| 64 |
.gpio_lock_detect = -1, |
| 65 |
}; |
| 66 |
|
| 67 |
static int adf4350_sync_config(struct adf4350_state *st) |
| 68 |
{ |
| 69 |
int ret, i, doublebuf = 0; |
| 70 |
|
| 71 |
for (i = ADF4350_REG5; i >= ADF4350_REG0; i--) { |
| 72 |
if ((st->regs_hw[i] != st->regs[i]) || |
| 73 |
((i == ADF4350_REG0) && doublebuf)) { |
| 74 |
|
| 75 |
switch (i) { |
| 76 |
case ADF4350_REG1: |
| 77 |
case ADF4350_REG4: |
| 78 |
doublebuf = 1; |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
st->val = cpu_to_be32(st->regs[i] | i); |
| 83 |
ret = spi_write(st->spi, &st->val, 4); |
| 84 |
if (ret < 0) |
| 85 |
return ret; |
| 86 |
st->regs_hw[i] = st->regs[i]; |
| 87 |
dev_dbg(&st->spi->dev, "[%d] 0x%X\n", |
| 88 |
i, (u32)st->regs[i] | i); |
| 89 |
} |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
static int adf4350_reg_access(struct iio_dev *indio_dev, |
| 95 |
unsigned reg, unsigned writeval, |
| 96 |
unsigned *readval) |
| 97 |
{ |
| 98 |
struct adf4350_state *st = iio_priv(indio_dev); |
| 99 |
int ret; |
| 100 |
|
| 101 |
if (reg > ADF4350_REG5) |
| 102 |
return -EINVAL; |
| 103 |
|
| 104 |
mutex_lock(&indio_dev->mlock); |
| 105 |
if (readval == NULL) { |
| 106 |
st->regs[reg] = writeval & ~(BIT(0) | BIT(1) | BIT(2)); |
| 107 |
ret = adf4350_sync_config(st); |
| 108 |
} else { |
| 109 |
*readval = st->regs_hw[reg]; |
| 110 |
ret = 0; |
| 111 |
} |
| 112 |
mutex_unlock(&indio_dev->mlock); |
| 113 |
|
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
int adf4350_set_freq(struct adf4350_state *st, unsigned long long freq) |
| 118 |
{ |
| 119 |
struct adf4350_platform_data *pdata = st->pdata; |
| 120 |
u64 tmp; |
| 121 |
u32 div_gcd, prescaler; |
| 122 |
u16 mdiv, r_cnt = 0; |
| 123 |
u8 band_sel_div; |
| 124 |
|
| 125 |
if (freq > ADF4350_MAX_OUT_FREQ || freq < st->min_out_freq) |
| 126 |
return -EINVAL; |
| 127 |
|
| 128 |
if (freq > ADF4350_MAX_FREQ_45_PRESC) { |
| 129 |
prescaler = ADF4350_REG1_PRESCALER; |
| 130 |
mdiv = 75; |
| 131 |
} else { |
| 132 |
prescaler = 0; |
| 133 |
mdiv = 23; |
| 134 |
} |
| 135 |
|
| 136 |
st->r4_rf_div_sel = 0; |
| 137 |
|
| 138 |
while (freq < ADF4350_MIN_VCO_FREQ) { |
| 139 |
freq <<= 1; |
| 140 |
st->r4_rf_div_sel++; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
if (pdata->ref_div_factor) |
| 148 |
r_cnt = pdata->ref_div_factor - 1; |
| 149 |
|
| 150 |
do { |
| 151 |
r_cnt = adf4350_tune_r_cnt(st, r_cnt); |
| 152 |
|
| 153 |
st->r1_mod = st->fpfd / st->chspc; |
| 154 |
while (st->r1_mod > ADF4350_MAX_MODULUS) { |
| 155 |
r_cnt = adf4350_tune_r_cnt(st, r_cnt); |
| 156 |
st->r1_mod = st->fpfd / st->chspc; |
| 157 |
} |
| 158 |
|
| 159 |
tmp = freq * (u64)st->r1_mod + (st->fpfd > 1); |
| 160 |
do_div(tmp, st->fpfd); |
| 161 |
st->r0_fract = do_div(tmp, st->r1_mod); |
| 162 |
st->r0_int = tmp; |
| 163 |
} while (mdiv > st->r0_int); |
| 164 |
|
| 165 |
band_sel_div = DIV_ROUND_UP(st->fpfd, ADF4350_MAX_BANDSEL_CLK); |
| 166 |
|
| 167 |
if (st->r0_fract && st->r1_mod) { |
| 168 |
div_gcd = gcd(st->r1_mod, st->r0_fract); |
| 169 |
st->r1_mod /= div_gcd; |
| 170 |
st->r0_fract /= div_gcd; |
| 171 |
} else { |
| 172 |
st->r0_fract = 0; |
| 173 |
st->r1_mod = 1; |
| 174 |
} |
| 175 |
|
| 176 |
dev_dbg(&st->spi->dev, "VCO: %llu Hz, PFD %lu Hz\n" |
| 177 |
"REF_DIV %d, R0_INT %d, R0_FRACT %d\n" |
| 178 |
"R1_MOD %d, RF_DIV %d\nPRESCALER %s, BAND_SEL_DIV %d\n", |
| 179 |
freq, st->fpfd, r_cnt, st->r0_int, st->r0_fract, st->r1_mod, |
| 180 |
1 << st->r4_rf_div_sel, prescaler ? "8/9" : "4/5", |
| 181 |
band_sel_div); |
| 182 |
|
| 183 |
st->regs[ADF4350_REG0] = ADF4350_REG0_INT(st->r0_int) | |
| 184 |
ADF4350_REG0_FRACT(st->r0_fract); |
| 185 |
|
| 186 |
st->regs[ADF4350_REG1] = ADF4350_REG1_PHASE(0) | |
| 187 |
ADF4350_REG1_MOD(st->r1_mod) | |
| 188 |
prescaler; |
| 189 |
|
| 190 |
st->regs[ADF4350_REG2] = |
| 191 |
ADF4350_REG2_10BIT_R_CNT(r_cnt) | |
| 192 |
ADF4350_REG2_DOUBLE_BUFF_EN | |
| 193 |
(pdata->ref_doubler_en ? ADF4350_REG2_RMULT2_EN : 0) | |
| 194 |
(pdata->ref_div2_en ? ADF4350_REG2_RDIV2_EN : 0) | |
| 195 |
(pdata->r2_user_settings & (ADF4350_REG2_PD_POLARITY_POS | |
| 196 |
ADF4350_REG2_LDP_6ns | ADF4350_REG2_LDF_INT_N | |
| 197 |
ADF4350_REG2_CHARGE_PUMP_CURR_uA(5000) | |
| 198 |
ADF4350_REG2_MUXOUT(0x7) | ADF4350_REG2_NOISE_MODE(0x9))); |
| 199 |
|
| 200 |
st->regs[ADF4350_REG3] = pdata->r3_user_settings & |
| 201 |
(ADF4350_REG3_12BIT_CLKDIV(0xFFF) | |
| 202 |
ADF4350_REG3_12BIT_CLKDIV_MODE(0x3) | |
| 203 |
ADF4350_REG3_12BIT_CSR_EN | |
| 204 |
ADF4351_REG3_CHARGE_CANCELLATION_EN | |
| 205 |
ADF4351_REG3_ANTI_BACKLASH_3ns_EN | |
| 206 |
ADF4351_REG3_BAND_SEL_CLOCK_MODE_HIGH); |
| 207 |
|
| 208 |
st->regs[ADF4350_REG4] = |
| 209 |
ADF4350_REG4_FEEDBACK_FUND | |
| 210 |
ADF4350_REG4_RF_DIV_SEL(st->r4_rf_div_sel) | |
| 211 |
ADF4350_REG4_8BIT_BAND_SEL_CLKDIV(band_sel_div) | |
| 212 |
ADF4350_REG4_RF_OUT_EN | |
| 213 |
(pdata->r4_user_settings & |
| 214 |
(ADF4350_REG4_OUTPUT_PWR(0x3) | |
| 215 |
ADF4350_REG4_AUX_OUTPUT_PWR(0x3) | |
| 216 |
ADF4350_REG4_AUX_OUTPUT_EN | |
| 217 |
ADF4350_REG4_AUX_OUTPUT_FUND | |
| 218 |
ADF4350_REG4_MUTE_TILL_LOCK_EN)); |
| 219 |
|
| 220 |
st->regs[ADF4350_REG5] = ADF4350_REG5_LD_PIN_MODE_DIGITAL; |
| 221 |
|
| 222 |
return adf4350_sync_config(st); |
| 223 |
} |
| 224 |
|
| 225 |
static ssize_t adf4350_store(struct device *dev, |
| 226 |
struct device_attribute *attr, |
| 227 |
const char *buf, size_t len) |
| 228 |
{ |
| 229 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 230 |
struct adf4350_state *st = iio_priv(indio_dev); |
| 231 |
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 232 |
unsigned long long readin; |
| 233 |
int ret; |
| 234 |
|
| 235 |
ret = kstrtoull(buf, 10, &readin); |
| 236 |
if (ret) |
| 237 |
return ret; |
| 238 |
|
| 239 |
mutex_lock(&indio_dev->mlock); |
| 240 |
switch ((u32)this_attr->address) { |
| 241 |
case ADF4350_SET_FREQ: |
| 242 |
ret = adf4350_set_freq(st, readin); |
| 243 |
break; |
| 244 |
case ADF4350_SET_FREQ_REFIN: |
| 245 |
if (readin > ADF4350_MAX_FREQ_REFIN) |
| 246 |
ret = -EINVAL; |
| 247 |
else |
| 248 |
st->clkin = readin; |
| 249 |
break; |
| 250 |
case ADF4350_SET_FREQ_RESOLUTION: |
| 251 |
if (readin == 0) |
| 252 |
ret = -EINVAL; |
| 253 |
else |
| 254 |
st->chspc = readin; |
| 255 |
break; |
| 256 |
case ADF4350_PLL_PWRDOWN: |
| 257 |
if (readin) |
| 258 |
st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; |
| 259 |
else |
| 260 |
st->regs[ADF4350_REG2] &= ~ADF4350_REG2_POWER_DOWN_EN; |
| 261 |
|
| 262 |
adf4350_sync_config(st); |
| 263 |
break; |
| 264 |
default: |
| 265 |
ret = -ENODEV; |
| 266 |
} |
| 267 |
mutex_unlock(&indio_dev->mlock); |
| 268 |
|
| 269 |
return ret ? ret : len; |
| 270 |
} |
| 271 |
|
| 272 |
static ssize_t adf4350_show(struct device *dev, |
| 273 |
struct device_attribute *attr, |
| 274 |
char *buf) |
| 275 |
{ |
| 276 |
struct iio_dev *indio_dev = dev_get_drvdata(dev); |
| 277 |
struct adf4350_state *st = iio_priv(indio_dev); |
| 278 |
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 279 |
unsigned long long val; |
| 280 |
int ret = 0; |
| 281 |
|
| 282 |
mutex_lock(&indio_dev->mlock); |
| 283 |
switch ((u32)this_attr->address) { |
| 284 |
case ADF4350_SET_FREQ: |
| 285 |
val = (u64)((st->r0_int * st->r1_mod) + st->r0_fract) * |
| 286 |
(u64)st->fpfd; |
| 287 |
do_div(val, st->r1_mod * (1 << st->r4_rf_div_sel)); |
| 288 |
break; |
| 289 |
case ADF4350_SET_FREQ_REFIN: |
| 290 |
val = st->clkin; |
| 291 |
break; |
| 292 |
case ADF4350_SET_FREQ_RESOLUTION: |
| 293 |
val = st->chspc; |
| 294 |
break; |
| 295 |
case ADF4350_PLL_LOCKED: |
| 296 |
if (gpio_is_valid(st->pdata->gpio_lock_detect)) |
| 297 |
val = gpio_get_value(st->pdata->gpio_lock_detect); |
| 298 |
else |
| 299 |
ret = -ENODEV; |
| 300 |
break; |
| 301 |
case ADF4350_PLL_PWRDOWN: |
| 302 |
val = !!(st->regs[ADF4350_REG2] & ADF4350_REG2_POWER_DOWN_EN); |
| 303 |
break; |
| 304 |
} |
| 305 |
mutex_unlock(&indio_dev->mlock); |
| 306 |
|
| 307 |
return ret < 0 ? ret : sprintf(buf, "%llu\n", val); |
| 308 |
} |
| 309 |
|
| 310 |
static IIO_DEVICE_ATTR(out_pll0_freq, S_IRUGO | S_IWUSR, |
| 311 |
adf4350_show, |
| 312 |
adf4350_store, |
| 313 |
ADF4350_SET_FREQ); |
| 314 |
|
| 315 |
static IIO_DEVICE_ATTR(out_pll0_refin_freq, S_IRUGO | S_IWUSR, |
| 316 |
adf4350_show, |
| 317 |
adf4350_store, |
| 318 |
ADF4350_SET_FREQ_REFIN); |
| 319 |
|
| 320 |
static IIO_DEVICE_ATTR(out_pll0_freq_resolution, S_IRUGO | S_IWUSR, |
| 321 |
adf4350_show, |
| 322 |
adf4350_store, |
| 323 |
ADF4350_SET_FREQ_RESOLUTION); |
| 324 |
|
| 325 |
static IIO_DEVICE_ATTR(out_pll0_locked, S_IRUGO, |
| 326 |
adf4350_show, |
| 327 |
NULL, |
| 328 |
ADF4350_PLL_LOCKED); |
| 329 |
|
| 330 |
static IIO_DEVICE_ATTR(out_pll0_powerdown, S_IRUGO | S_IWUSR, |
| 331 |
adf4350_show, |
| 332 |
adf4350_store, |
| 333 |
ADF4350_PLL_PWRDOWN); |
| 334 |
|
| 335 |
static struct attribute *adf4350_attributes[] = { |
| 336 |
&iio_dev_attr_out_pll0_freq.dev_attr.attr, |
| 337 |
&iio_dev_attr_out_pll0_refin_freq.dev_attr.attr, |
| 338 |
&iio_dev_attr_out_pll0_freq_resolution.dev_attr.attr, |
| 339 |
&iio_dev_attr_out_pll0_locked.dev_attr.attr, |
| 340 |
&iio_dev_attr_out_pll0_powerdown.dev_attr.attr, |
| 341 |
NULL, |
| 342 |
}; |
| 343 |
|
| 344 |
static const struct attribute_group adf4350_attribute_group = { |
| 345 |
.attrs = adf4350_attributes, |
| 346 |
}; |
| 347 |
|
| 348 |
static const struct iio_info adf4350_info = { |
| 349 |
.attrs = &adf4350_attribute_group, |
| 350 |
.debugfs_reg_access = &adf4350_reg_access, |
| 351 |
.driver_module = THIS_MODULE, |
| 352 |
}; |
| 353 |
|
| 354 |
static int __devinit adf4350_probe(struct spi_device *spi) |
| 355 |
{ |
| 356 |
struct adf4350_platform_data *pdata = spi->dev.platform_data; |
| 357 |
struct iio_dev *indio_dev; |
| 358 |
struct adf4350_state *st; |
| 359 |
int ret; |
| 360 |
|
| 361 |
if (!pdata) { |
| 362 |
dev_warn(&spi->dev, "no platform data? using default\n"); |
| 363 |
pdata = &default_pdata; |
| 364 |
} |
| 365 |
|
| 366 |
indio_dev = iio_allocate_device(sizeof(*st)); |
| 367 |
if (indio_dev == NULL) |
| 368 |
return -ENOMEM; |
| 369 |
|
| 370 |
st = iio_priv(indio_dev); |
| 371 |
|
| 372 |
st->reg = regulator_get(&spi->dev, "vcc"); |
| 373 |
if (!IS_ERR(st->reg)) { |
| 374 |
ret = regulator_enable(st->reg); |
| 375 |
if (ret) |
| 376 |
goto error_put_reg; |
| 377 |
} |
| 378 |
|
| 379 |
spi_set_drvdata(spi, indio_dev); |
| 380 |
st->spi = spi; |
| 381 |
st->pdata = pdata; |
| 382 |
|
| 383 |
indio_dev->dev.parent = &spi->dev; |
| 384 |
indio_dev->name = (pdata->name[0] != 0) ? pdata->name : spi_get_device_id(spi)->name; |
| 385 |
|
| 386 |
printk("%s: %d::: %s %s\n",__func__,__LINE__, pdata->name, spi_get_device_id(spi)->name); |
| 387 |
indio_dev->info = &adf4350_info; |
| 388 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 389 |
|
| 390 |
st->chspc = pdata->channel_spacing; |
| 391 |
st->clkin = pdata->clkin; |
| 392 |
|
| 393 |
st->min_out_freq = spi_get_device_id(spi)->driver_data == 4351 ? |
| 394 |
ADF4351_MIN_OUT_FREQ : ADF4350_MIN_OUT_FREQ; |
| 395 |
|
| 396 |
memset(st->regs_hw, 0xFF, sizeof(st->regs_hw)); |
| 397 |
|
| 398 |
if (gpio_is_valid(pdata->gpio_lock_detect)) { |
| 399 |
ret = gpio_request(pdata->gpio_lock_detect, indio_dev->name); |
| 400 |
if (ret) { |
| 401 |
dev_err(&spi->dev, "fail to request lock detect GPIO-%d", |
| 402 |
pdata->gpio_lock_detect); |
| 403 |
goto error_disable_reg; |
| 404 |
} |
| 405 |
gpio_direction_input(pdata->gpio_lock_detect); |
| 406 |
} |
| 407 |
|
| 408 |
if (pdata->power_up_frequency) { |
| 409 |
ret = adf4350_set_freq(st, pdata->power_up_frequency); |
| 410 |
if (ret) |
| 411 |
goto error_free_gpio; |
| 412 |
} |
| 413 |
|
| 414 |
ret = iio_device_register(indio_dev); |
| 415 |
if (ret) |
| 416 |
goto error_free_gpio; |
| 417 |
|
| 418 |
return 0; |
| 419 |
|
| 420 |
error_free_gpio: |
| 421 |
if (gpio_is_valid(pdata->gpio_lock_detect)) |
| 422 |
gpio_free(pdata->gpio_lock_detect); |
| 423 |
|
| 424 |
error_disable_reg: |
| 425 |
if (!IS_ERR(st->reg)) |
| 426 |
regulator_disable(st->reg); |
| 427 |
error_put_reg: |
| 428 |
if (!IS_ERR(st->reg)) |
| 429 |
regulator_put(st->reg); |
| 430 |
|
| 431 |
iio_free_device(indio_dev); |
| 432 |
|
| 433 |
return ret; |
| 434 |
} |
| 435 |
|
| 436 |
static int __devexit adf4350_remove(struct spi_device *spi) |
| 437 |
{ |
| 438 |
struct iio_dev *indio_dev = spi_get_drvdata(spi); |
| 439 |
struct adf4350_state *st = iio_priv(indio_dev); |
| 440 |
struct regulator *reg = st->reg; |
| 441 |
|
| 442 |
st->regs[ADF4350_REG2] |= ADF4350_REG2_POWER_DOWN_EN; |
| 443 |
adf4350_sync_config(st); |
| 444 |
|
| 445 |
if (gpio_is_valid(st->pdata->gpio_lock_detect)) |
| 446 |
gpio_free(st->pdata->gpio_lock_detect); |
| 447 |
|
| 448 |
iio_device_unregister(indio_dev); |
| 449 |
|
| 450 |
if (!IS_ERR(reg)) { |
| 451 |
regulator_disable(reg); |
| 452 |
regulator_put(reg); |
| 453 |
} |
| 454 |
|
| 455 |
iio_free_device(indio_dev); |
| 456 |
|
| 457 |
return 0; |
| 458 |
} |
| 459 |
|
| 460 |
static const struct spi_device_id adf4350_id[] = { |
| 461 |
{"adf4350", 4350}, |
| 462 |
{"adf4351", 4351}, |
| 463 |
{} |
| 464 |
}; |
| 465 |
|
| 466 |
static struct spi_driver adf4350_driver = { |
| 467 |
.driver = { |
| 468 |
.name = "adf4350", |
| 469 |
.owner = THIS_MODULE, |
| 470 |
}, |
| 471 |
.probe = adf4350_probe, |
| 472 |
.remove = __devexit_p(adf4350_remove), |
| 473 |
.id_table = adf4350_id, |
| 474 |
}; |
| 475 |
|
| 476 |
static int __init adf4350_init(void) |
| 477 |
{ |
| 478 |
return spi_register_driver(&adf4350_driver); |
| 479 |
} |
| 480 |
module_init(adf4350_init); |
| 481 |
|
| 482 |
static void __exit adf4350_exit(void) |
| 483 |
{ |
| 484 |
spi_unregister_driver(&adf4350_driver); |
| 485 |
} |
| 486 |
module_exit(adf4350_exit); |
| 487 |
|
| 488 |
MODULE_AUTHOR("Michael Hennerich <[email protected]>"); |
| 489 |
MODULE_DESCRIPTION("Analog Devices ADF4350/ADF4351 PLL"); |
| 490 |
MODULE_LICENSE("GPL v2"); |