forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/staging/iio/impedance-analyzer/ad5933.c
....@@ -1,26 +1,26 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * AD5933 AD5934 Impedance Converter, Network Analyzer
34 *
45 * Copyright 2011 Analog Devices Inc.
5
- *
6
- * Licensed under the GPL-2.
76 */
87
9
-#include <linux/interrupt.h>
10
-#include <linux/device.h>
11
-#include <linux/kernel.h>
12
-#include <linux/sysfs.h>
13
-#include <linux/i2c.h>
14
-#include <linux/regulator/consumer.h>
15
-#include <linux/types.h>
16
-#include <linux/err.h>
8
+#include <linux/clk.h>
179 #include <linux/delay.h>
10
+#include <linux/device.h>
11
+#include <linux/err.h>
12
+#include <linux/i2c.h>
13
+#include <linux/interrupt.h>
14
+#include <linux/kernel.h>
1815 #include <linux/module.h>
16
+#include <linux/regulator/consumer.h>
17
+#include <linux/sysfs.h>
18
+#include <linux/types.h>
1919
20
-#include <linux/iio/iio.h>
21
-#include <linux/iio/sysfs.h>
2220 #include <linux/iio/buffer.h>
21
+#include <linux/iio/iio.h>
2322 #include <linux/iio/kfifo_buf.h>
23
+#include <linux/iio/sysfs.h>
2424
2525 /* AD5933/AD5934 Registers */
2626 #define AD5933_REG_CONTROL_HB 0x80 /* R/W, 1 byte */
....@@ -82,21 +82,10 @@
8282 #define AD5933_POLL_TIME_ms 10
8383 #define AD5933_INIT_EXCITATION_TIME_ms 100
8484
85
-/**
86
- * struct ad5933_platform_data - platform specific data
87
- * @ext_clk_Hz: the external clock frequency in Hz, if not set
88
- * the driver uses the internal clock (16.776 MHz)
89
- * @vref_mv: the external reference voltage in millivolt
90
- */
91
-
92
-struct ad5933_platform_data {
93
- unsigned long ext_clk_Hz;
94
- unsigned short vref_mv;
95
-};
96
-
9785 struct ad5933_state {
9886 struct i2c_client *client;
9987 struct regulator *reg;
88
+ struct clk *mclk;
10089 struct delayed_work work;
10190 struct mutex lock; /* Protect sensor state */
10291 unsigned long mclk_hz;
....@@ -110,10 +99,6 @@
11099 unsigned int freq_inc;
111100 unsigned int state;
112101 unsigned int poll_time_jiffies;
113
-};
114
-
115
-static struct ad5933_platform_data ad5933_default_pdata = {
116
- .vref_mv = 3300,
117102 };
118103
119104 #define AD5933_CHANNEL(_type, _extend_name, _info_mask_separate, _address, \
....@@ -210,7 +195,7 @@
210195 u8 d8[4];
211196 } dat;
212197
213
- freqreg = (u64) freq * (u64) (1 << 27);
198
+ freqreg = (u64)freq * (u64)(1 << 27);
214199 do_div(freqreg, st->mclk_hz / 4);
215200
216201 switch (reg) {
....@@ -267,7 +252,6 @@
267252
268253 for (i = 0; i < 4; i++)
269254 st->range_avail[i] = normalized_3v3[i] * st->vref_mv / 3300;
270
-
271255 }
272256
273257 /*
....@@ -299,7 +283,7 @@
299283 freqreg = be32_to_cpu(dat.d32) & 0xFFFFFF;
300284
301285 freqreg = (u64)freqreg * (u64)(st->mclk_hz / 4);
302
- do_div(freqreg, 1 << 27);
286
+ do_div(freqreg, BIT(27));
303287
304288 return sprintf(buf, "%d\n", (int)freqreg);
305289 }
....@@ -331,12 +315,12 @@
331315 return ret ? ret : len;
332316 }
333317
334
-static IIO_DEVICE_ATTR(out_voltage0_freq_start, 0644,
318
+static IIO_DEVICE_ATTR(out_altvoltage0_frequency_start, 0644,
335319 ad5933_show_frequency,
336320 ad5933_store_frequency,
337321 AD5933_REG_FREQ_START);
338322
339
-static IIO_DEVICE_ATTR(out_voltage0_freq_increment, 0644,
323
+static IIO_DEVICE_ATTR(out_altvoltage0_frequency_increment, 0644,
340324 ad5933_show_frequency,
341325 ad5933_store_frequency,
342326 AD5933_REG_FREQ_INC);
....@@ -435,7 +419,7 @@
435419 if (val > 1022)
436420 val = (val >> 2) | (3 << 9);
437421 else if (val > 511)
438
- val = (val >> 1) | (1 << 9);
422
+ val = (val >> 1) | BIT(9);
439423
440424 dat = cpu_to_be16(val);
441425 ret = ad5933_i2c_write(st->client,
....@@ -459,12 +443,12 @@
459443 return ret ? ret : len;
460444 }
461445
462
-static IIO_DEVICE_ATTR(out_voltage0_scale, 0644,
446
+static IIO_DEVICE_ATTR(out_altvoltage0_raw, 0644,
463447 ad5933_show,
464448 ad5933_store,
465449 AD5933_OUT_RANGE);
466450
467
-static IIO_DEVICE_ATTR(out_voltage0_scale_available, 0444,
451
+static IIO_DEVICE_ATTR(out_altvoltage0_scale_available, 0444,
468452 ad5933_show,
469453 NULL,
470454 AD5933_OUT_RANGE_AVAIL);
....@@ -479,28 +463,29 @@
479463 NULL,
480464 AD5933_IN_PGA_GAIN_AVAIL);
481465
482
-static IIO_DEVICE_ATTR(out_voltage0_freq_points, 0644,
466
+static IIO_DEVICE_ATTR(out_altvoltage0_frequency_points, 0644,
483467 ad5933_show,
484468 ad5933_store,
485469 AD5933_FREQ_POINTS);
486470
487
-static IIO_DEVICE_ATTR(out_voltage0_settling_cycles, 0644,
471
+static IIO_DEVICE_ATTR(out_altvoltage0_settling_cycles, 0644,
488472 ad5933_show,
489473 ad5933_store,
490474 AD5933_OUT_SETTLING_CYCLES);
491475
492
-/* note:
476
+/*
477
+ * note:
493478 * ideally we would handle the scale attributes via the iio_info
494479 * (read|write)_raw methods, however this part is a untypical since we
495480 * don't create dedicated sysfs channel attributes for out0 and in0.
496481 */
497482 static struct attribute *ad5933_attributes[] = {
498
- &iio_dev_attr_out_voltage0_scale.dev_attr.attr,
499
- &iio_dev_attr_out_voltage0_scale_available.dev_attr.attr,
500
- &iio_dev_attr_out_voltage0_freq_start.dev_attr.attr,
501
- &iio_dev_attr_out_voltage0_freq_increment.dev_attr.attr,
502
- &iio_dev_attr_out_voltage0_freq_points.dev_attr.attr,
503
- &iio_dev_attr_out_voltage0_settling_cycles.dev_attr.attr,
483
+ &iio_dev_attr_out_altvoltage0_raw.dev_attr.attr,
484
+ &iio_dev_attr_out_altvoltage0_scale_available.dev_attr.attr,
485
+ &iio_dev_attr_out_altvoltage0_frequency_start.dev_attr.attr,
486
+ &iio_dev_attr_out_altvoltage0_frequency_increment.dev_attr.attr,
487
+ &iio_dev_attr_out_altvoltage0_frequency_points.dev_attr.attr,
488
+ &iio_dev_attr_out_altvoltage0_settling_cycles.dev_attr.attr,
504489 &iio_dev_attr_in_voltage0_scale.dev_attr.attr,
505490 &iio_dev_attr_in_voltage0_scale_available.dev_attr.attr,
506491 NULL
....@@ -587,7 +572,8 @@
587572 {
588573 struct ad5933_state *st = iio_priv(indio_dev);
589574
590
- /* AD5933_CTRL_INIT_START_FREQ:
575
+ /*
576
+ * AD5933_CTRL_INIT_START_FREQ:
591577 * High Q complex circuits require a long time to reach steady state.
592578 * To facilitate the measurement of such impedances, this mode allows
593579 * the user full control of the settling time requirement before
....@@ -616,11 +602,12 @@
616602 .postdisable = ad5933_ring_postdisable,
617603 };
618604
619
-static int ad5933_register_ring_funcs_and_init(struct iio_dev *indio_dev)
605
+static int ad5933_register_ring_funcs_and_init(struct device *dev,
606
+ struct iio_dev *indio_dev)
620607 {
621608 struct iio_buffer *buffer;
622609
623
- buffer = iio_kfifo_allocate();
610
+ buffer = devm_iio_kfifo_allocate(dev);
624611 if (!buffer)
625612 return -ENOMEM;
626613
....@@ -678,7 +665,8 @@
678665 }
679666
680667 if (status & AD5933_STAT_SWEEP_DONE) {
681
- /* last sample received - power down do
668
+ /*
669
+ * last sample received - power down do
682670 * nothing until the ring enable is toggled
683671 */
684672 ad5933_cmd(st, AD5933_CTRL_POWER_DOWN);
....@@ -689,13 +677,27 @@
689677 }
690678 }
691679
680
+static void ad5933_reg_disable(void *data)
681
+{
682
+ struct ad5933_state *st = data;
683
+
684
+ regulator_disable(st->reg);
685
+}
686
+
687
+static void ad5933_clk_disable(void *data)
688
+{
689
+ struct ad5933_state *st = data;
690
+
691
+ clk_disable_unprepare(st->mclk);
692
+}
693
+
692694 static int ad5933_probe(struct i2c_client *client,
693695 const struct i2c_device_id *id)
694696 {
695
- int ret, voltage_uv = 0;
696
- struct ad5933_platform_data *pdata = dev_get_platdata(&client->dev);
697
+ int ret;
697698 struct ad5933_state *st;
698699 struct iio_dev *indio_dev;
700
+ unsigned long ext_clk_hz = 0;
699701
700702 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st));
701703 if (!indio_dev)
....@@ -707,9 +709,6 @@
707709
708710 mutex_init(&st->lock);
709711
710
- if (!pdata)
711
- pdata = &ad5933_default_pdata;
712
-
713712 st->reg = devm_regulator_get(&client->dev, "vdd");
714713 if (IS_ERR(st->reg))
715714 return PTR_ERR(st->reg);
....@@ -719,15 +718,37 @@
719718 dev_err(&client->dev, "Failed to enable specified VDD supply\n");
720719 return ret;
721720 }
722
- voltage_uv = regulator_get_voltage(st->reg);
723721
724
- if (voltage_uv)
725
- st->vref_mv = voltage_uv / 1000;
726
- else
727
- st->vref_mv = pdata->vref_mv;
722
+ ret = devm_add_action_or_reset(&client->dev, ad5933_reg_disable, st);
723
+ if (ret)
724
+ return ret;
728725
729
- if (pdata->ext_clk_Hz) {
730
- st->mclk_hz = pdata->ext_clk_Hz;
726
+ ret = regulator_get_voltage(st->reg);
727
+ if (ret < 0)
728
+ return ret;
729
+
730
+ st->vref_mv = ret / 1000;
731
+
732
+ st->mclk = devm_clk_get(&client->dev, "mclk");
733
+ if (IS_ERR(st->mclk) && PTR_ERR(st->mclk) != -ENOENT)
734
+ return PTR_ERR(st->mclk);
735
+
736
+ if (!IS_ERR(st->mclk)) {
737
+ ret = clk_prepare_enable(st->mclk);
738
+ if (ret < 0)
739
+ return ret;
740
+
741
+ ret = devm_add_action_or_reset(&client->dev,
742
+ ad5933_clk_disable,
743
+ st);
744
+ if (ret)
745
+ return ret;
746
+
747
+ ext_clk_hz = clk_get_rate(st->mclk);
748
+ }
749
+
750
+ if (ext_clk_hz) {
751
+ st->mclk_hz = ext_clk_hz;
731752 st->ctrl_lb = AD5933_CTRL_EXT_SYSCLK;
732753 } else {
733754 st->mclk_hz = AD5933_INT_OSC_FREQ_Hz;
....@@ -738,45 +759,21 @@
738759 INIT_DELAYED_WORK(&st->work, ad5933_work);
739760 st->poll_time_jiffies = msecs_to_jiffies(AD5933_POLL_TIME_ms);
740761
741
- indio_dev->dev.parent = &client->dev;
742762 indio_dev->info = &ad5933_info;
743763 indio_dev->name = id->name;
744764 indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
745765 indio_dev->channels = ad5933_channels;
746766 indio_dev->num_channels = ARRAY_SIZE(ad5933_channels);
747767
748
- ret = ad5933_register_ring_funcs_and_init(indio_dev);
768
+ ret = ad5933_register_ring_funcs_and_init(&client->dev, indio_dev);
749769 if (ret)
750
- goto error_disable_reg;
770
+ return ret;
751771
752772 ret = ad5933_setup(st);
753773 if (ret)
754
- goto error_unreg_ring;
774
+ return ret;
755775
756
- ret = iio_device_register(indio_dev);
757
- if (ret)
758
- goto error_unreg_ring;
759
-
760
- return 0;
761
-
762
-error_unreg_ring:
763
- iio_kfifo_free(indio_dev->buffer);
764
-error_disable_reg:
765
- regulator_disable(st->reg);
766
-
767
- return ret;
768
-}
769
-
770
-static int ad5933_remove(struct i2c_client *client)
771
-{
772
- struct iio_dev *indio_dev = i2c_get_clientdata(client);
773
- struct ad5933_state *st = iio_priv(indio_dev);
774
-
775
- iio_device_unregister(indio_dev);
776
- iio_kfifo_free(indio_dev->buffer);
777
- regulator_disable(st->reg);
778
-
779
- return 0;
776
+ return devm_iio_device_register(&client->dev, indio_dev);
780777 }
781778
782779 static const struct i2c_device_id ad5933_id[] = {
....@@ -787,16 +784,24 @@
787784
788785 MODULE_DEVICE_TABLE(i2c, ad5933_id);
789786
787
+static const struct of_device_id ad5933_of_match[] = {
788
+ { .compatible = "adi,ad5933" },
789
+ { .compatible = "adi,ad5934" },
790
+ { },
791
+};
792
+
793
+MODULE_DEVICE_TABLE(of, ad5933_of_match);
794
+
790795 static struct i2c_driver ad5933_driver = {
791796 .driver = {
792797 .name = "ad5933",
798
+ .of_match_table = ad5933_of_match,
793799 },
794800 .probe = ad5933_probe,
795
- .remove = ad5933_remove,
796801 .id_table = ad5933_id,
797802 };
798803 module_i2c_driver(ad5933_driver);
799804
800
-MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
805
+MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
801806 MODULE_DESCRIPTION("Analog Devices AD5933 Impedance Conv. Network Analyzer");
802807 MODULE_LICENSE("GPL v2");