| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* AD9832 SPI DDS driver |
| 4 |
* |
| 5 |
* Copyright 2011 Analog Devices Inc. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <asm/div64.h> |
| 9 |
|
| 10 |
#include <linux/bitfield.h> |
| 11 |
#include <linux/bits.h> |
| 12 |
#include <linux/clk.h> |
| 13 |
#include <linux/device.h> |
| 14 |
#include <linux/err.h> |
| 15 |
#include <linux/kernel.h> |
| 16 |
#include <linux/module.h> |
| 17 |
#include <linux/regulator/consumer.h> |
| 18 |
#include <linux/slab.h> |
| 19 |
#include <linux/spi/spi.h> |
| 20 |
#include <linux/sysfs.h> |
| 21 |
#include <linux/unaligned.h> |
| 22 |
|
| 23 |
#include <linux/iio/iio.h> |
| 24 |
#include <linux/iio/sysfs.h> |
| 25 |
|
| 26 |
#include "dds.h" |
| 27 |
|
| 28 |
/* Registers */ |
| 29 |
#define AD9832_FREQ0LL 0x0 |
| 30 |
#define AD9832_FREQ0HL 0x1 |
| 31 |
#define AD9832_FREQ0LM 0x2 |
| 32 |
#define AD9832_FREQ0HM 0x3 |
| 33 |
#define AD9832_FREQ1LL 0x4 |
| 34 |
#define AD9832_FREQ1HL 0x5 |
| 35 |
#define AD9832_FREQ1LM 0x6 |
| 36 |
#define AD9832_FREQ1HM 0x7 |
| 37 |
#define AD9832_PHASE0L 0x8 |
| 38 |
#define AD9832_PHASE0H 0x9 |
| 39 |
#define AD9832_PHASE1L 0xA |
| 40 |
#define AD9832_PHASE1H 0xB |
| 41 |
#define AD9832_PHASE2L 0xC |
| 42 |
#define AD9832_PHASE2H 0xD |
| 43 |
#define AD9832_PHASE3L 0xE |
| 44 |
#define AD9832_PHASE3H 0xF |
| 45 |
#define AD9832_PHASE_SYM 0x10 |
| 46 |
#define AD9832_FREQ_SYM 0x11 |
| 47 |
#define AD9832_PINCTRL_EN 0x12 |
| 48 |
#define AD9832_OUTPUT_EN 0x13 |
| 49 |
|
| 50 |
/* Command Control Bits */ |
| 51 |
#define AD9832_CMD_PHA8BITSW 0x1 |
| 52 |
#define AD9832_CMD_PHA16BITSW 0x0 |
| 53 |
#define AD9832_CMD_FRE8BITSW 0x3 |
| 54 |
#define AD9832_CMD_FRE16BITSW 0x2 |
| 55 |
#define AD9832_CMD_FPSELECT 0x6 |
| 56 |
#define AD9832_CMD_SYNCSELSRC 0x8 |
| 57 |
#define AD9832_CMD_SLEEPRESCLR 0xC |
| 58 |
|
| 59 |
#define AD9832_FREQ BIT(11) |
| 60 |
#define AD9832_PHASE_MASK GENMASK(10, 9) |
| 61 |
#define AD9832_SYNC BIT(13) |
| 62 |
#define AD9832_SELSRC BIT(12) |
| 63 |
#define AD9832_SLEEP BIT(13) |
| 64 |
#define AD9832_RESET BIT(12) |
| 65 |
#define AD9832_CLR BIT(11) |
| 66 |
#define AD9832_FREQ_BITS 32 |
| 67 |
#define AD9832_PHASE_BITS 12 |
| 68 |
#define AD9832_CMD_MSK GENMASK(15, 12) |
| 69 |
#define AD9832_ADD_MSK GENMASK(11, 8) |
| 70 |
#define AD9832_DAT_MSK GENMASK(7, 0) |
| 71 |
|
| 72 |
/** |
| 73 |
* struct ad9832_state - driver instance specific data |
| 74 |
* @spi: spi_device |
| 75 |
* @mclk: external master clock |
| 76 |
* @ctrl_fp: cached frequency/phase control word |
| 77 |
* @ctrl_ss: cached sync/selsrc control word |
| 78 |
* @ctrl_src: cached sleep/reset/clr word |
| 79 |
* @xfer: default spi transfer |
| 80 |
* @msg: default spi message |
| 81 |
* @freq_xfer: tuning word spi transfer |
| 82 |
* @freq_msg: tuning word spi message |
| 83 |
* @phase_xfer: tuning word spi transfer |
| 84 |
* @phase_msg: tuning word spi message |
| 85 |
* @lock: protect sensor state |
| 86 |
* @data: spi transmit buffer |
| 87 |
* @phase_data: tuning word spi transmit buffer |
| 88 |
* @freq_data: tuning word spi transmit buffer |
| 89 |
*/ |
| 90 |
struct ad9832_state { |
| 91 |
struct spi_device *spi; |
| 92 |
struct clk *mclk; |
| 93 |
unsigned short ctrl_fp; |
| 94 |
unsigned short ctrl_ss; |
| 95 |
unsigned short ctrl_src; |
| 96 |
struct spi_transfer xfer; |
| 97 |
struct spi_message msg; |
| 98 |
struct spi_transfer freq_xfer[4]; |
| 99 |
struct spi_message freq_msg; |
| 100 |
struct spi_transfer phase_xfer[2]; |
| 101 |
struct spi_message phase_msg; |
| 102 |
struct mutex lock; /* protect sensor state */ |
| 103 |
/* |
| 104 |
* DMA (thus cache coherency maintenance) requires the |
| 105 |
* transfer buffers to live in their own cache lines. |
| 106 |
*/ |
| 107 |
union { |
| 108 |
__be16 freq_data[4]; |
| 109 |
__be16 phase_data[2]; |
| 110 |
__be16 data; |
| 111 |
} __aligned(IIO_DMA_MINALIGN); |
| 112 |
}; |
| 113 |
|
| 114 |
static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout) |
| 115 |
{ |
| 116 |
unsigned long long freqreg = (u64)fout * |
| 117 |
(u64)((u64)1L << AD9832_FREQ_BITS); |
| 118 |
do_div(freqreg, mclk); |
| 119 |
return freqreg; |
| 120 |
} |
| 121 |
|
| 122 |
static int ad9832_write_frequency(struct ad9832_state *st, |
| 123 |
unsigned int addr, unsigned long fout) |
| 124 |
{ |
| 125 |
unsigned long clk_freq; |
| 126 |
unsigned long regval; |
| 127 |
u8 regval_bytes[4]; |
| 128 |
u16 freq_cmd; |
| 129 |
|
| 130 |
clk_freq = clk_get_rate(st->mclk); |
| 131 |
|
| 132 |
if (!clk_freq || fout > (clk_freq / 2)) |
| 133 |
return -EINVAL; |
| 134 |
|
| 135 |
regval = ad9832_calc_freqreg(clk_freq, fout); |
| 136 |
put_unaligned_be32(regval, regval_bytes); |
| 137 |
|
| 138 |
for (int i = 0; i < ARRAY_SIZE(regval_bytes); i++) { |
| 139 |
freq_cmd = (i % 2 == 0) ? AD9832_CMD_FRE8BITSW : AD9832_CMD_FRE16BITSW; |
| 140 |
|
| 141 |
st->freq_data[i] = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, freq_cmd) | |
| 142 |
FIELD_PREP(AD9832_ADD_MSK, addr - i) | |
| 143 |
FIELD_PREP(AD9832_DAT_MSK, regval_bytes[i])); |
| 144 |
} |
| 145 |
|
| 146 |
return spi_sync(st->spi, &st->freq_msg); |
| 147 |
} |
| 148 |
|
| 149 |
static int ad9832_write_phase(struct ad9832_state *st, |
| 150 |
unsigned long addr, unsigned long phase) |
| 151 |
{ |
| 152 |
u8 phase_bytes[2]; |
| 153 |
u16 phase_cmd; |
| 154 |
|
| 155 |
if (phase >= BIT(AD9832_PHASE_BITS)) |
| 156 |
return -EINVAL; |
| 157 |
|
| 158 |
put_unaligned_be16(phase, phase_bytes); |
| 159 |
|
| 160 |
for (int i = 0; i < ARRAY_SIZE(phase_bytes); i++) { |
| 161 |
phase_cmd = (i % 2 == 0) ? AD9832_CMD_PHA8BITSW : AD9832_CMD_PHA16BITSW; |
| 162 |
|
| 163 |
st->phase_data[i] = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, phase_cmd) | |
| 164 |
FIELD_PREP(AD9832_ADD_MSK, addr - i) | |
| 165 |
FIELD_PREP(AD9832_DAT_MSK, phase_bytes[i])); |
| 166 |
} |
| 167 |
|
| 168 |
return spi_sync(st->spi, &st->phase_msg); |
| 169 |
} |
| 170 |
|
| 171 |
static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr, |
| 172 |
const char *buf, size_t len) |
| 173 |
{ |
| 174 |
struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 175 |
struct ad9832_state *st = iio_priv(indio_dev); |
| 176 |
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 177 |
int ret; |
| 178 |
unsigned long val; |
| 179 |
|
| 180 |
ret = kstrtoul(buf, 10, &val); |
| 181 |
if (ret) |
| 182 |
goto error_ret; |
| 183 |
|
| 184 |
mutex_lock(&st->lock); |
| 185 |
switch ((u32)this_attr->address) { |
| 186 |
case AD9832_FREQ0HM: |
| 187 |
case AD9832_FREQ1HM: |
| 188 |
ret = ad9832_write_frequency(st, this_attr->address, val); |
| 189 |
break; |
| 190 |
case AD9832_PHASE0H: |
| 191 |
case AD9832_PHASE1H: |
| 192 |
case AD9832_PHASE2H: |
| 193 |
case AD9832_PHASE3H: |
| 194 |
ret = ad9832_write_phase(st, this_attr->address, val); |
| 195 |
break; |
| 196 |
case AD9832_PINCTRL_EN: |
| 197 |
st->ctrl_ss &= ~AD9832_SELSRC; |
| 198 |
st->ctrl_ss |= FIELD_PREP(AD9832_SELSRC, val ? 0 : 1); |
| 199 |
|
| 200 |
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SYNCSELSRC) | |
| 201 |
st->ctrl_ss); |
| 202 |
ret = spi_sync(st->spi, &st->msg); |
| 203 |
break; |
| 204 |
case AD9832_FREQ_SYM: |
| 205 |
if (val == 1 || val == 0) { |
| 206 |
st->ctrl_fp &= ~AD9832_FREQ; |
| 207 |
st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, val ? 1 : 0); |
| 208 |
} else { |
| 209 |
ret = -EINVAL; |
| 210 |
break; |
| 211 |
} |
| 212 |
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) | |
| 213 |
st->ctrl_fp); |
| 214 |
ret = spi_sync(st->spi, &st->msg); |
| 215 |
break; |
| 216 |
case AD9832_PHASE_SYM: |
| 217 |
if (val > 3) { |
| 218 |
ret = -EINVAL; |
| 219 |
break; |
| 220 |
} |
| 221 |
|
| 222 |
st->ctrl_fp &= ~AD9832_PHASE_MASK; |
| 223 |
st->ctrl_fp |= FIELD_PREP(AD9832_PHASE_MASK, val); |
| 224 |
|
| 225 |
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) | |
| 226 |
st->ctrl_fp); |
| 227 |
ret = spi_sync(st->spi, &st->msg); |
| 228 |
break; |
| 229 |
case AD9832_OUTPUT_EN: |
| 230 |
if (val) |
| 231 |
st->ctrl_src &= ~(AD9832_RESET | AD9832_SLEEP | AD9832_CLR); |
| 232 |
else |
| 233 |
st->ctrl_src |= FIELD_PREP(AD9832_RESET, 1); |
| 234 |
|
| 235 |
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) | |
| 236 |
st->ctrl_src); |
| 237 |
ret = spi_sync(st->spi, &st->msg); |
| 238 |
break; |
| 239 |
default: |
| 240 |
ret = -ENODEV; |
| 241 |
} |
| 242 |
mutex_unlock(&st->lock); |
| 243 |
|
| 244 |
error_ret: |
| 245 |
return ret ? ret : len; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* see dds.h for further information |
| 250 |
*/ |
| 251 |
|
| 252 |
static IIO_DEV_ATTR_FREQ(0, 0, 0200, NULL, ad9832_write, AD9832_FREQ0HM); |
| 253 |
static IIO_DEV_ATTR_FREQ(0, 1, 0200, NULL, ad9832_write, AD9832_FREQ1HM); |
| 254 |
static IIO_DEV_ATTR_FREQSYMBOL(0, 0200, NULL, ad9832_write, AD9832_FREQ_SYM); |
| 255 |
static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */ |
| 256 |
|
| 257 |
static IIO_DEV_ATTR_PHASE(0, 0, 0200, NULL, ad9832_write, AD9832_PHASE0H); |
| 258 |
static IIO_DEV_ATTR_PHASE(0, 1, 0200, NULL, ad9832_write, AD9832_PHASE1H); |
| 259 |
static IIO_DEV_ATTR_PHASE(0, 2, 0200, NULL, ad9832_write, AD9832_PHASE2H); |
| 260 |
static IIO_DEV_ATTR_PHASE(0, 3, 0200, NULL, ad9832_write, AD9832_PHASE3H); |
| 261 |
static IIO_DEV_ATTR_PHASESYMBOL(0, 0200, NULL, |
| 262 |
ad9832_write, AD9832_PHASE_SYM); |
| 263 |
static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/ |
| 264 |
|
| 265 |
static IIO_DEV_ATTR_PINCONTROL_EN(0, 0200, NULL, |
| 266 |
ad9832_write, AD9832_PINCTRL_EN); |
| 267 |
static IIO_DEV_ATTR_OUT_ENABLE(0, 0200, NULL, |
| 268 |
ad9832_write, AD9832_OUTPUT_EN); |
| 269 |
|
| 270 |
static struct attribute *ad9832_attributes[] = { |
| 271 |
&iio_dev_attr_out_altvoltage0_frequency0.dev_attr.attr, |
| 272 |
&iio_dev_attr_out_altvoltage0_frequency1.dev_attr.attr, |
| 273 |
&iio_const_attr_out_altvoltage0_frequency_scale.dev_attr.attr, |
| 274 |
&iio_dev_attr_out_altvoltage0_phase0.dev_attr.attr, |
| 275 |
&iio_dev_attr_out_altvoltage0_phase1.dev_attr.attr, |
| 276 |
&iio_dev_attr_out_altvoltage0_phase2.dev_attr.attr, |
| 277 |
&iio_dev_attr_out_altvoltage0_phase3.dev_attr.attr, |
| 278 |
&iio_const_attr_out_altvoltage0_phase_scale.dev_attr.attr, |
| 279 |
&iio_dev_attr_out_altvoltage0_pincontrol_en.dev_attr.attr, |
| 280 |
&iio_dev_attr_out_altvoltage0_frequencysymbol.dev_attr.attr, |
| 281 |
&iio_dev_attr_out_altvoltage0_phasesymbol.dev_attr.attr, |
| 282 |
&iio_dev_attr_out_altvoltage0_out_enable.dev_attr.attr, |
| 283 |
NULL, |
| 284 |
}; |
| 285 |
|
| 286 |
static const struct attribute_group ad9832_attribute_group = { |
| 287 |
.attrs = ad9832_attributes, |
| 288 |
}; |
| 289 |
|
| 290 |
static const struct iio_info ad9832_info = { |
| 291 |
.attrs = &ad9832_attribute_group, |
| 292 |
}; |
| 293 |
|
| 294 |
static int ad9832_probe(struct spi_device *spi) |
| 295 |
{ |
| 296 |
struct iio_dev *indio_dev; |
| 297 |
struct ad9832_state *st; |
| 298 |
int ret; |
| 299 |
|
| 300 |
indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); |
| 301 |
if (!indio_dev) |
| 302 |
return -ENOMEM; |
| 303 |
|
| 304 |
st = iio_priv(indio_dev); |
| 305 |
|
| 306 |
ret = devm_regulator_get_enable(&spi->dev, "avdd"); |
| 307 |
if (ret) |
| 308 |
return dev_err_probe(&spi->dev, ret, "failed to enable specified AVDD voltage\n"); |
| 309 |
|
| 310 |
ret = devm_regulator_get_enable(&spi->dev, "dvdd"); |
| 311 |
if (ret) |
| 312 |
return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVDD supply\n"); |
| 313 |
|
| 314 |
st->mclk = devm_clk_get_enabled(&spi->dev, "mclk"); |
| 315 |
if (IS_ERR(st->mclk)) |
| 316 |
return PTR_ERR(st->mclk); |
| 317 |
|
| 318 |
st->spi = spi; |
| 319 |
mutex_init(&st->lock); |
| 320 |
|
| 321 |
indio_dev->name = spi_get_device_id(spi)->name; |
| 322 |
indio_dev->info = &ad9832_info; |
| 323 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 324 |
|
| 325 |
/* Setup default messages */ |
| 326 |
st->xfer.tx_buf = &st->data; |
| 327 |
st->xfer.len = 2; |
| 328 |
|
| 329 |
spi_message_init(&st->msg); |
| 330 |
spi_message_add_tail(&st->xfer, &st->msg); |
| 331 |
|
| 332 |
st->freq_xfer[0].tx_buf = &st->freq_data[0]; |
| 333 |
st->freq_xfer[0].len = 2; |
| 334 |
st->freq_xfer[0].cs_change = 1; |
| 335 |
st->freq_xfer[1].tx_buf = &st->freq_data[1]; |
| 336 |
st->freq_xfer[1].len = 2; |
| 337 |
st->freq_xfer[1].cs_change = 1; |
| 338 |
st->freq_xfer[2].tx_buf = &st->freq_data[2]; |
| 339 |
st->freq_xfer[2].len = 2; |
| 340 |
st->freq_xfer[2].cs_change = 1; |
| 341 |
st->freq_xfer[3].tx_buf = &st->freq_data[3]; |
| 342 |
st->freq_xfer[3].len = 2; |
| 343 |
|
| 344 |
spi_message_init(&st->freq_msg); |
| 345 |
spi_message_add_tail(&st->freq_xfer[0], &st->freq_msg); |
| 346 |
spi_message_add_tail(&st->freq_xfer[1], &st->freq_msg); |
| 347 |
spi_message_add_tail(&st->freq_xfer[2], &st->freq_msg); |
| 348 |
spi_message_add_tail(&st->freq_xfer[3], &st->freq_msg); |
| 349 |
|
| 350 |
st->phase_xfer[0].tx_buf = &st->phase_data[0]; |
| 351 |
st->phase_xfer[0].len = 2; |
| 352 |
st->phase_xfer[0].cs_change = 1; |
| 353 |
st->phase_xfer[1].tx_buf = &st->phase_data[1]; |
| 354 |
st->phase_xfer[1].len = 2; |
| 355 |
|
| 356 |
spi_message_init(&st->phase_msg); |
| 357 |
spi_message_add_tail(&st->phase_xfer[0], &st->phase_msg); |
| 358 |
spi_message_add_tail(&st->phase_xfer[1], &st->phase_msg); |
| 359 |
|
| 360 |
st->ctrl_src = AD9832_SLEEP | AD9832_RESET | AD9832_CLR; |
| 361 |
st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_SLEEPRESCLR) | |
| 362 |
st->ctrl_src); |
| 363 |
ret = spi_sync(st->spi, &st->msg); |
| 364 |
if (ret) { |
| 365 |
dev_err(&spi->dev, "device init failed\n"); |
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
return devm_iio_device_register(&spi->dev, indio_dev); |
| 370 |
} |
| 371 |
|
| 372 |
static const struct of_device_id ad9832_of_match[] = { |
| 373 |
{ .compatible = "adi,ad9832" }, |
| 374 |
{ .compatible = "adi,ad9835" }, |
| 375 |
{ } |
| 376 |
}; |
| 377 |
MODULE_DEVICE_TABLE(of, ad9832_of_match); |
| 378 |
|
| 379 |
static const struct spi_device_id ad9832_id[] = { |
| 380 |
{"ad9832", 0}, |
| 381 |
{"ad9835", 0}, |
| 382 |
{ } |
| 383 |
}; |
| 384 |
MODULE_DEVICE_TABLE(spi, ad9832_id); |
| 385 |
|
| 386 |
static struct spi_driver ad9832_driver = { |
| 387 |
.driver = { |
| 388 |
.name = "ad9832", |
| 389 |
.of_match_table = ad9832_of_match, |
| 390 |
}, |
| 391 |
.probe = ad9832_probe, |
| 392 |
.id_table = ad9832_id, |
| 393 |
}; |
| 394 |
module_spi_driver(ad9832_driver); |
| 395 |
|
| 396 |
MODULE_AUTHOR("Michael Hennerich <[email protected]>"); |
| 397 |
MODULE_DESCRIPTION("Analog Devices AD9832/AD9835 DDS"); |
| 398 |
MODULE_LICENSE("GPL v2"); |