| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * ADS1015 - Texas Instruments Analog-to-Digital Converter |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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. |
|---|
| 9 | 6 | * |
|---|
| 10 | 7 | * IIO driver for ADS1015 ADC 7-bit I2C slave address: |
|---|
| 11 | 8 | * * 0x48 - ADDR connected to Ground |
|---|
| .. | .. |
|---|
| 15 | 12 | */ |
|---|
| 16 | 13 | |
|---|
| 17 | 14 | #include <linux/module.h> |
|---|
| 18 | | -#include <linux/of_device.h> |
|---|
| 19 | 15 | #include <linux/init.h> |
|---|
| 20 | 16 | #include <linux/irq.h> |
|---|
| 21 | 17 | #include <linux/i2c.h> |
|---|
| 18 | +#include <linux/property.h> |
|---|
| 22 | 19 | #include <linux/regmap.h> |
|---|
| 23 | 20 | #include <linux/pm_runtime.h> |
|---|
| 24 | 21 | #include <linux/mutex.h> |
|---|
| 25 | 22 | #include <linux/delay.h> |
|---|
| 26 | | - |
|---|
| 27 | | -#include <linux/platform_data/ads1015.h> |
|---|
| 28 | 23 | |
|---|
| 29 | 24 | #include <linux/iio/iio.h> |
|---|
| 30 | 25 | #include <linux/iio/types.h> |
|---|
| .. | .. |
|---|
| 35 | 30 | #include <linux/iio/trigger_consumer.h> |
|---|
| 36 | 31 | |
|---|
| 37 | 32 | #define ADS1015_DRV_NAME "ads1015" |
|---|
| 33 | + |
|---|
| 34 | +#define ADS1015_CHANNELS 8 |
|---|
| 38 | 35 | |
|---|
| 39 | 36 | #define ADS1015_CONV_REG 0x00 |
|---|
| 40 | 37 | #define ADS1015_CFG_REG 0x01 |
|---|
| .. | .. |
|---|
| 80 | 77 | #define ADS1015_DEFAULT_CHAN 0 |
|---|
| 81 | 78 | |
|---|
| 82 | 79 | enum chip_ids { |
|---|
| 80 | + ADSXXXX = 0, |
|---|
| 83 | 81 | ADS1015, |
|---|
| 84 | 82 | ADS1115, |
|---|
| 85 | 83 | }; |
|---|
| .. | .. |
|---|
| 221 | 219 | .num_event_specs = ARRAY_SIZE(ads1015_events), \ |
|---|
| 222 | 220 | .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \ |
|---|
| 223 | 221 | } |
|---|
| 222 | + |
|---|
| 223 | +struct ads1015_channel_data { |
|---|
| 224 | + bool enabled; |
|---|
| 225 | + unsigned int pga; |
|---|
| 226 | + unsigned int data_rate; |
|---|
| 227 | +}; |
|---|
| 224 | 228 | |
|---|
| 225 | 229 | struct ads1015_thresh_data { |
|---|
| 226 | 230 | unsigned int comp_queue; |
|---|
| .. | .. |
|---|
| 798 | 802 | |
|---|
| 799 | 803 | static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = { |
|---|
| 800 | 804 | .preenable = ads1015_buffer_preenable, |
|---|
| 801 | | - .postenable = iio_triggered_buffer_postenable, |
|---|
| 802 | | - .predisable = iio_triggered_buffer_predisable, |
|---|
| 803 | 805 | .postdisable = ads1015_buffer_postdisable, |
|---|
| 804 | 806 | .validate_scan_mask = &iio_validate_scan_mask_onehot, |
|---|
| 805 | 807 | }; |
|---|
| .. | .. |
|---|
| 854 | 856 | .attrs = &ads1115_attribute_group, |
|---|
| 855 | 857 | }; |
|---|
| 856 | 858 | |
|---|
| 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) |
|---|
| 859 | 860 | { |
|---|
| 860 | 861 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
|---|
| 861 | 862 | 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; |
|---|
| 863 | 866 | |
|---|
| 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) { |
|---|
| 869 | 868 | u32 pval; |
|---|
| 870 | 869 | unsigned int channel; |
|---|
| 871 | 870 | unsigned int pga = ADS1015_DEFAULT_PGA; |
|---|
| 872 | 871 | unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE; |
|---|
| 873 | 872 | |
|---|
| 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); |
|---|
| 877 | 875 | continue; |
|---|
| 878 | 876 | } |
|---|
| 879 | 877 | |
|---|
| 880 | 878 | channel = pval; |
|---|
| 881 | 879 | 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", |
|---|
| 884 | 881 | channel, node); |
|---|
| 885 | 882 | continue; |
|---|
| 886 | 883 | } |
|---|
| 887 | 884 | |
|---|
| 888 | | - if (!of_property_read_u32(node, "ti,gain", &pval)) { |
|---|
| 885 | + if (!fwnode_property_read_u32(node, "ti,gain", &pval)) { |
|---|
| 889 | 886 | pga = pval; |
|---|
| 890 | 887 | 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); |
|---|
| 894 | 890 | return -EINVAL; |
|---|
| 895 | 891 | } |
|---|
| 896 | 892 | } |
|---|
| 897 | 893 | |
|---|
| 898 | | - if (!of_property_read_u32(node, "ti,datarate", &pval)) { |
|---|
| 894 | + if (!fwnode_property_read_u32(node, "ti,datarate", &pval)) { |
|---|
| 899 | 895 | data_rate = pval; |
|---|
| 900 | 896 | 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); |
|---|
| 905 | 899 | return -EINVAL; |
|---|
| 906 | 900 | } |
|---|
| 907 | 901 | } |
|---|
| 908 | 902 | |
|---|
| 909 | 903 | data->channel_data[channel].pga = pga; |
|---|
| 910 | 904 | data->channel_data[channel].data_rate = data_rate; |
|---|
| 905 | + |
|---|
| 906 | + i++; |
|---|
| 911 | 907 | } |
|---|
| 912 | 908 | |
|---|
| 913 | | - return 0; |
|---|
| 909 | + return i < 0 ? -EINVAL : 0; |
|---|
| 914 | 910 | } |
|---|
| 915 | | -#endif |
|---|
| 916 | 911 | |
|---|
| 917 | 912 | static void ads1015_get_channels_config(struct i2c_client *client) |
|---|
| 918 | 913 | { |
|---|
| .. | .. |
|---|
| 920 | 915 | |
|---|
| 921 | 916 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
|---|
| 922 | 917 | struct ads1015_data *data = iio_priv(indio_dev); |
|---|
| 923 | | - struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev); |
|---|
| 924 | 918 | |
|---|
| 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)) |
|---|
| 929 | 920 | return; |
|---|
| 930 | | - } |
|---|
| 931 | 921 | |
|---|
| 932 | | -#ifdef CONFIG_OF |
|---|
| 933 | | - if (!ads1015_get_channels_config_of(client)) |
|---|
| 934 | | - return; |
|---|
| 935 | | -#endif |
|---|
| 936 | 922 | /* fallback on default configuration */ |
|---|
| 937 | 923 | for (k = 0; k < ADS1015_CHANNELS; ++k) { |
|---|
| 938 | 924 | data->channel_data[k].pga = ADS1015_DEFAULT_PGA; |
|---|
| .. | .. |
|---|
| 965 | 951 | |
|---|
| 966 | 952 | mutex_init(&data->lock); |
|---|
| 967 | 953 | |
|---|
| 968 | | - indio_dev->dev.parent = &client->dev; |
|---|
| 969 | | - indio_dev->dev.of_node = client->dev.of_node; |
|---|
| 970 | 954 | indio_dev->name = ADS1015_DRV_NAME; |
|---|
| 971 | 955 | indio_dev->modes = INDIO_DIRECT_MODE; |
|---|
| 972 | 956 | |
|---|
| 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) |
|---|
| 976 | 959 | chip = id->driver_data; |
|---|
| 977 | 960 | switch (chip) { |
|---|
| 978 | 961 | case ADS1015: |
|---|
| .. | .. |
|---|
| 987 | 970 | indio_dev->info = &ads1115_info; |
|---|
| 988 | 971 | data->data_rate = (unsigned int *) &ads1115_data_rate; |
|---|
| 989 | 972 | break; |
|---|
| 973 | + default: |
|---|
| 974 | + dev_err(&client->dev, "Unknown chip %d\n", chip); |
|---|
| 975 | + return -EINVAL; |
|---|
| 990 | 976 | } |
|---|
| 991 | 977 | |
|---|
| 992 | 978 | data->event_channel = ADS1015_CHANNELS; |
|---|