.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
---|
1 | 2 | /* |
---|
2 | 3 | * AD9832 SPI DDS driver |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright 2011 Analog Devices Inc. |
---|
5 | | - * |
---|
6 | | - * Licensed under the GPL-2. |
---|
7 | 6 | */ |
---|
8 | 7 | |
---|
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 | 8 | #include <asm/div64.h> |
---|
| 9 | + |
---|
| 10 | +#include <linux/clk.h> |
---|
| 11 | +#include <linux/device.h> |
---|
| 12 | +#include <linux/err.h> |
---|
| 13 | +#include <linux/kernel.h> |
---|
| 14 | +#include <linux/module.h> |
---|
| 15 | +#include <linux/regulator/consumer.h> |
---|
| 16 | +#include <linux/slab.h> |
---|
| 17 | +#include <linux/spi/spi.h> |
---|
| 18 | +#include <linux/sysfs.h> |
---|
18 | 19 | |
---|
19 | 20 | #include <linux/iio/iio.h> |
---|
20 | 21 | #include <linux/iio/sysfs.h> |
---|
21 | | -#include "dds.h" |
---|
22 | 22 | |
---|
23 | 23 | #include "ad9832.h" |
---|
| 24 | + |
---|
| 25 | +#include "dds.h" |
---|
24 | 26 | |
---|
25 | 27 | /* Registers */ |
---|
26 | 28 | |
---|
.. | .. |
---|
94 | 96 | struct spi_device *spi; |
---|
95 | 97 | struct regulator *avdd; |
---|
96 | 98 | struct regulator *dvdd; |
---|
97 | | - unsigned long mclk; |
---|
| 99 | + struct clk *mclk; |
---|
98 | 100 | unsigned short ctrl_fp; |
---|
99 | 101 | unsigned short ctrl_ss; |
---|
100 | 102 | unsigned short ctrl_src; |
---|
.. | .. |
---|
129 | 131 | { |
---|
130 | 132 | unsigned long regval; |
---|
131 | 133 | |
---|
132 | | - if (fout > (st->mclk / 2)) |
---|
| 134 | + if (fout > (clk_get_rate(st->mclk) / 2)) |
---|
133 | 135 | return -EINVAL; |
---|
134 | 136 | |
---|
135 | | - regval = ad9832_calc_freqreg(st->mclk, fout); |
---|
| 137 | + regval = ad9832_calc_freqreg(clk_get_rate(st->mclk), fout); |
---|
136 | 138 | |
---|
137 | 139 | st->freq_data[0] = cpu_to_be16((AD9832_CMD_FRE8BITSW << CMD_SHIFT) | |
---|
138 | 140 | (addr << ADD_SHIFT) | |
---|
.. | .. |
---|
333 | 335 | goto error_disable_avdd; |
---|
334 | 336 | } |
---|
335 | 337 | |
---|
336 | | - st->mclk = pdata->mclk; |
---|
| 338 | + st->mclk = devm_clk_get(&spi->dev, "mclk"); |
---|
| 339 | + if (IS_ERR(st->mclk)) { |
---|
| 340 | + ret = PTR_ERR(st->mclk); |
---|
| 341 | + goto error_disable_dvdd; |
---|
| 342 | + } |
---|
| 343 | + |
---|
| 344 | + ret = clk_prepare_enable(st->mclk); |
---|
| 345 | + if (ret < 0) |
---|
| 346 | + goto error_disable_dvdd; |
---|
| 347 | + |
---|
337 | 348 | st->spi = spi; |
---|
338 | 349 | mutex_init(&st->lock); |
---|
339 | 350 | |
---|
340 | | - indio_dev->dev.parent = &spi->dev; |
---|
341 | 351 | indio_dev->name = spi_get_device_id(spi)->name; |
---|
342 | 352 | indio_dev->info = &ad9832_info; |
---|
343 | 353 | indio_dev->modes = INDIO_DIRECT_MODE; |
---|
.. | .. |
---|
384 | 394 | ret = spi_sync(st->spi, &st->msg); |
---|
385 | 395 | if (ret) { |
---|
386 | 396 | dev_err(&spi->dev, "device init failed\n"); |
---|
387 | | - goto error_disable_dvdd; |
---|
| 397 | + goto error_unprepare_mclk; |
---|
388 | 398 | } |
---|
389 | 399 | |
---|
390 | 400 | ret = ad9832_write_frequency(st, AD9832_FREQ0HM, pdata->freq0); |
---|
391 | 401 | if (ret) |
---|
392 | | - goto error_disable_dvdd; |
---|
| 402 | + goto error_unprepare_mclk; |
---|
393 | 403 | |
---|
394 | 404 | ret = ad9832_write_frequency(st, AD9832_FREQ1HM, pdata->freq1); |
---|
395 | 405 | if (ret) |
---|
396 | | - goto error_disable_dvdd; |
---|
| 406 | + goto error_unprepare_mclk; |
---|
397 | 407 | |
---|
398 | 408 | ret = ad9832_write_phase(st, AD9832_PHASE0H, pdata->phase0); |
---|
399 | 409 | if (ret) |
---|
400 | | - goto error_disable_dvdd; |
---|
| 410 | + goto error_unprepare_mclk; |
---|
401 | 411 | |
---|
402 | 412 | ret = ad9832_write_phase(st, AD9832_PHASE1H, pdata->phase1); |
---|
403 | 413 | if (ret) |
---|
404 | | - goto error_disable_dvdd; |
---|
| 414 | + goto error_unprepare_mclk; |
---|
405 | 415 | |
---|
406 | 416 | ret = ad9832_write_phase(st, AD9832_PHASE2H, pdata->phase2); |
---|
407 | 417 | if (ret) |
---|
408 | | - goto error_disable_dvdd; |
---|
| 418 | + goto error_unprepare_mclk; |
---|
409 | 419 | |
---|
410 | 420 | ret = ad9832_write_phase(st, AD9832_PHASE3H, pdata->phase3); |
---|
411 | 421 | if (ret) |
---|
412 | | - goto error_disable_dvdd; |
---|
| 422 | + goto error_unprepare_mclk; |
---|
413 | 423 | |
---|
414 | 424 | ret = iio_device_register(indio_dev); |
---|
415 | 425 | if (ret) |
---|
416 | | - goto error_disable_dvdd; |
---|
| 426 | + goto error_unprepare_mclk; |
---|
417 | 427 | |
---|
418 | 428 | return 0; |
---|
419 | 429 | |
---|
| 430 | +error_unprepare_mclk: |
---|
| 431 | + clk_disable_unprepare(st->mclk); |
---|
420 | 432 | error_disable_dvdd: |
---|
421 | 433 | regulator_disable(st->dvdd); |
---|
422 | 434 | error_disable_avdd: |
---|
.. | .. |
---|
431 | 443 | struct ad9832_state *st = iio_priv(indio_dev); |
---|
432 | 444 | |
---|
433 | 445 | iio_device_unregister(indio_dev); |
---|
| 446 | + clk_disable_unprepare(st->mclk); |
---|
434 | 447 | regulator_disable(st->dvdd); |
---|
435 | 448 | regulator_disable(st->avdd); |
---|
436 | 449 | |
---|
.. | .. |
---|
454 | 467 | }; |
---|
455 | 468 | module_spi_driver(ad9832_driver); |
---|
456 | 469 | |
---|
457 | | -MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); |
---|
| 470 | +MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); |
---|
458 | 471 | MODULE_DESCRIPTION("Analog Devices AD9832/AD9835 DDS"); |
---|
459 | 472 | MODULE_LICENSE("GPL v2"); |
---|