forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-04 1543e317f1da31b75942316931e8f491a8920811
kernel/drivers/iio/adc/ti-ads1015.c
....@@ -1,11 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * ADS1015 - Texas Instruments Analog-to-Digital Converter
34 *
45 * Copyright (c) 2016, Intel Corporation.
5
- *
6
- * This file is subject to the terms and conditions of version 2 of
7
- * the GNU General Public License. See the file COPYING in the main
8
- * directory of this archive for more details.
96 *
107 * IIO driver for ADS1015 ADC 7-bit I2C slave address:
118 * * 0x48 - ADDR connected to Ground
....@@ -15,16 +12,14 @@
1512 */
1613
1714 #include <linux/module.h>
18
-#include <linux/of_device.h>
1915 #include <linux/init.h>
2016 #include <linux/irq.h>
2117 #include <linux/i2c.h>
18
+#include <linux/property.h>
2219 #include <linux/regmap.h>
2320 #include <linux/pm_runtime.h>
2421 #include <linux/mutex.h>
2522 #include <linux/delay.h>
26
-
27
-#include <linux/platform_data/ads1015.h>
2823
2924 #include <linux/iio/iio.h>
3025 #include <linux/iio/types.h>
....@@ -35,6 +30,8 @@
3530 #include <linux/iio/trigger_consumer.h>
3631
3732 #define ADS1015_DRV_NAME "ads1015"
33
+
34
+#define ADS1015_CHANNELS 8
3835
3936 #define ADS1015_CONV_REG 0x00
4037 #define ADS1015_CFG_REG 0x01
....@@ -80,6 +77,7 @@
8077 #define ADS1015_DEFAULT_CHAN 0
8178
8279 enum chip_ids {
80
+ ADSXXXX = 0,
8381 ADS1015,
8482 ADS1115,
8583 };
....@@ -221,6 +219,12 @@
221219 .num_event_specs = ARRAY_SIZE(ads1015_events), \
222220 .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
223221 }
222
+
223
+struct ads1015_channel_data {
224
+ bool enabled;
225
+ unsigned int pga;
226
+ unsigned int data_rate;
227
+};
224228
225229 struct ads1015_thresh_data {
226230 unsigned int comp_queue;
....@@ -798,8 +802,6 @@
798802
799803 static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
800804 .preenable = ads1015_buffer_preenable,
801
- .postenable = iio_triggered_buffer_postenable,
802
- .predisable = iio_triggered_buffer_predisable,
803805 .postdisable = ads1015_buffer_postdisable,
804806 .validate_scan_mask = &iio_validate_scan_mask_onehot,
805807 };
....@@ -854,65 +856,58 @@
854856 .attrs = &ads1115_attribute_group,
855857 };
856858
857
-#ifdef CONFIG_OF
858
-static int ads1015_get_channels_config_of(struct i2c_client *client)
859
+static int ads1015_client_get_channels_config(struct i2c_client *client)
859860 {
860861 struct iio_dev *indio_dev = i2c_get_clientdata(client);
861862 struct ads1015_data *data = iio_priv(indio_dev);
862
- struct device_node *node;
863
+ struct device *dev = &client->dev;
864
+ struct fwnode_handle *node;
865
+ int i = -1;
863866
864
- if (!client->dev.of_node ||
865
- !of_get_next_child(client->dev.of_node, NULL))
866
- return -EINVAL;
867
-
868
- for_each_child_of_node(client->dev.of_node, node) {
867
+ device_for_each_child_node(dev, node) {
869868 u32 pval;
870869 unsigned int channel;
871870 unsigned int pga = ADS1015_DEFAULT_PGA;
872871 unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
873872
874
- if (of_property_read_u32(node, "reg", &pval)) {
875
- dev_err(&client->dev, "invalid reg on %pOF\n",
876
- node);
873
+ if (fwnode_property_read_u32(node, "reg", &pval)) {
874
+ dev_err(dev, "invalid reg on %pfw\n", node);
877875 continue;
878876 }
879877
880878 channel = pval;
881879 if (channel >= ADS1015_CHANNELS) {
882
- dev_err(&client->dev,
883
- "invalid channel index %d on %pOF\n",
880
+ dev_err(dev, "invalid channel index %d on %pfw\n",
884881 channel, node);
885882 continue;
886883 }
887884
888
- if (!of_property_read_u32(node, "ti,gain", &pval)) {
885
+ if (!fwnode_property_read_u32(node, "ti,gain", &pval)) {
889886 pga = pval;
890887 if (pga > 6) {
891
- dev_err(&client->dev, "invalid gain on %pOF\n",
892
- node);
893
- of_node_put(node);
888
+ dev_err(dev, "invalid gain on %pfw\n", node);
889
+ fwnode_handle_put(node);
894890 return -EINVAL;
895891 }
896892 }
897893
898
- if (!of_property_read_u32(node, "ti,datarate", &pval)) {
894
+ if (!fwnode_property_read_u32(node, "ti,datarate", &pval)) {
899895 data_rate = pval;
900896 if (data_rate > 7) {
901
- dev_err(&client->dev,
902
- "invalid data_rate on %pOF\n",
903
- node);
904
- of_node_put(node);
897
+ dev_err(dev, "invalid data_rate on %pfw\n", node);
898
+ fwnode_handle_put(node);
905899 return -EINVAL;
906900 }
907901 }
908902
909903 data->channel_data[channel].pga = pga;
910904 data->channel_data[channel].data_rate = data_rate;
905
+
906
+ i++;
911907 }
912908
913
- return 0;
909
+ return i < 0 ? -EINVAL : 0;
914910 }
915
-#endif
916911
917912 static void ads1015_get_channels_config(struct i2c_client *client)
918913 {
....@@ -920,19 +915,10 @@
920915
921916 struct iio_dev *indio_dev = i2c_get_clientdata(client);
922917 struct ads1015_data *data = iio_priv(indio_dev);
923
- struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
924918
925
- /* prefer platform data */
926
- if (pdata) {
927
- memcpy(data->channel_data, pdata->channel_data,
928
- sizeof(data->channel_data));
919
+ if (!ads1015_client_get_channels_config(client))
929920 return;
930
- }
931921
932
-#ifdef CONFIG_OF
933
- if (!ads1015_get_channels_config_of(client))
934
- return;
935
-#endif
936922 /* fallback on default configuration */
937923 for (k = 0; k < ADS1015_CHANNELS; ++k) {
938924 data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
....@@ -965,14 +951,11 @@
965951
966952 mutex_init(&data->lock);
967953
968
- indio_dev->dev.parent = &client->dev;
969
- indio_dev->dev.of_node = client->dev.of_node;
970954 indio_dev->name = ADS1015_DRV_NAME;
971955 indio_dev->modes = INDIO_DIRECT_MODE;
972956
973
- if (client->dev.of_node)
974
- chip = (enum chip_ids)of_device_get_match_data(&client->dev);
975
- else
957
+ chip = (enum chip_ids)device_get_match_data(&client->dev);
958
+ if (chip == ADSXXXX)
976959 chip = id->driver_data;
977960 switch (chip) {
978961 case ADS1015:
....@@ -987,6 +970,9 @@
987970 indio_dev->info = &ads1115_info;
988971 data->data_rate = (unsigned int *) &ads1115_data_rate;
989972 break;
973
+ default:
974
+ dev_err(&client->dev, "Unknown chip %d\n", chip);
975
+ return -EINVAL;
990976 }
991977
992978 data->event_channel = ADS1015_CHANNELS;