Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ads7828.c - driver for TI ADS7828 8-channel A/D converter and compatibles
0004  * (C) 2007 EADS Astrium
0005  *
0006  * This driver is based on the lm75 and other lm_sensors/hwmon drivers
0007  *
0008  * Written by Steve Hardy <shardy@redhat.com>
0009  *
0010  * ADS7830 support, by Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
0011  *
0012  * For further information, see the Documentation/hwmon/ads7828.rst file.
0013  */
0014 
0015 #include <linux/err.h>
0016 #include <linux/hwmon.h>
0017 #include <linux/hwmon-sysfs.h>
0018 #include <linux/i2c.h>
0019 #include <linux/init.h>
0020 #include <linux/module.h>
0021 #include <linux/of_device.h>
0022 #include <linux/platform_data/ads7828.h>
0023 #include <linux/regmap.h>
0024 #include <linux/slab.h>
0025 #include <linux/regulator/consumer.h>
0026 
0027 /* The ADS7828 registers */
0028 #define ADS7828_CMD_SD_SE   0x80    /* Single ended inputs */
0029 #define ADS7828_CMD_PD1     0x04    /* Internal vref OFF && A/D ON */
0030 #define ADS7828_CMD_PD3     0x0C    /* Internal vref ON && A/D ON */
0031 #define ADS7828_INT_VREF_MV 2500    /* Internal vref is 2.5V, 2500mV */
0032 #define ADS7828_EXT_VREF_MV_MIN 50  /* External vref min value 0.05V */
0033 #define ADS7828_EXT_VREF_MV_MAX 5250    /* External vref max value 5.25V */
0034 
0035 /* List of supported devices */
0036 enum ads7828_chips { ads7828, ads7830 };
0037 
0038 /* Client specific data */
0039 struct ads7828_data {
0040     struct regmap *regmap;
0041     u8 cmd_byte;            /* Command byte without channel bits */
0042     unsigned int lsb_resol;     /* Resolution of the ADC sample LSB */
0043 };
0044 
0045 /* Command byte C2,C1,C0 - see datasheet */
0046 static inline u8 ads7828_cmd_byte(u8 cmd, int ch)
0047 {
0048     return cmd | (((ch >> 1) | (ch & 0x01) << 2) << 4);
0049 }
0050 
0051 /* sysfs callback function */
0052 static ssize_t ads7828_in_show(struct device *dev,
0053                    struct device_attribute *da, char *buf)
0054 {
0055     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0056     struct ads7828_data *data = dev_get_drvdata(dev);
0057     u8 cmd = ads7828_cmd_byte(data->cmd_byte, attr->index);
0058     unsigned int regval;
0059     int err;
0060 
0061     err = regmap_read(data->regmap, cmd, &regval);
0062     if (err < 0)
0063         return err;
0064 
0065     return sprintf(buf, "%d\n",
0066                DIV_ROUND_CLOSEST(regval * data->lsb_resol, 1000));
0067 }
0068 
0069 static SENSOR_DEVICE_ATTR_RO(in0_input, ads7828_in, 0);
0070 static SENSOR_DEVICE_ATTR_RO(in1_input, ads7828_in, 1);
0071 static SENSOR_DEVICE_ATTR_RO(in2_input, ads7828_in, 2);
0072 static SENSOR_DEVICE_ATTR_RO(in3_input, ads7828_in, 3);
0073 static SENSOR_DEVICE_ATTR_RO(in4_input, ads7828_in, 4);
0074 static SENSOR_DEVICE_ATTR_RO(in5_input, ads7828_in, 5);
0075 static SENSOR_DEVICE_ATTR_RO(in6_input, ads7828_in, 6);
0076 static SENSOR_DEVICE_ATTR_RO(in7_input, ads7828_in, 7);
0077 
0078 static struct attribute *ads7828_attrs[] = {
0079     &sensor_dev_attr_in0_input.dev_attr.attr,
0080     &sensor_dev_attr_in1_input.dev_attr.attr,
0081     &sensor_dev_attr_in2_input.dev_attr.attr,
0082     &sensor_dev_attr_in3_input.dev_attr.attr,
0083     &sensor_dev_attr_in4_input.dev_attr.attr,
0084     &sensor_dev_attr_in5_input.dev_attr.attr,
0085     &sensor_dev_attr_in6_input.dev_attr.attr,
0086     &sensor_dev_attr_in7_input.dev_attr.attr,
0087     NULL
0088 };
0089 
0090 ATTRIBUTE_GROUPS(ads7828);
0091 
0092 static const struct regmap_config ads2828_regmap_config = {
0093     .reg_bits = 8,
0094     .val_bits = 16,
0095 };
0096 
0097 static const struct regmap_config ads2830_regmap_config = {
0098     .reg_bits = 8,
0099     .val_bits = 8,
0100 };
0101 
0102 static const struct i2c_device_id ads7828_device_ids[];
0103 
0104 static int ads7828_probe(struct i2c_client *client)
0105 {
0106     struct device *dev = &client->dev;
0107     struct ads7828_platform_data *pdata = dev_get_platdata(dev);
0108     struct ads7828_data *data;
0109     struct device *hwmon_dev;
0110     unsigned int vref_mv = ADS7828_INT_VREF_MV;
0111     unsigned int vref_uv;
0112     bool diff_input = false;
0113     bool ext_vref = false;
0114     unsigned int regval;
0115     enum ads7828_chips chip;
0116     struct regulator *reg;
0117 
0118     data = devm_kzalloc(dev, sizeof(struct ads7828_data), GFP_KERNEL);
0119     if (!data)
0120         return -ENOMEM;
0121 
0122     if (pdata) {
0123         diff_input = pdata->diff_input;
0124         ext_vref = pdata->ext_vref;
0125         if (ext_vref && pdata->vref_mv)
0126             vref_mv = pdata->vref_mv;
0127     } else if (dev->of_node) {
0128         diff_input = of_property_read_bool(dev->of_node,
0129                            "ti,differential-input");
0130         reg = devm_regulator_get_optional(dev, "vref");
0131         if (!IS_ERR(reg)) {
0132             vref_uv = regulator_get_voltage(reg);
0133             vref_mv = DIV_ROUND_CLOSEST(vref_uv, 1000);
0134             if (vref_mv < ADS7828_EXT_VREF_MV_MIN ||
0135                 vref_mv > ADS7828_EXT_VREF_MV_MAX)
0136                 return -EINVAL;
0137             ext_vref = true;
0138         }
0139     }
0140 
0141     if (client->dev.of_node)
0142         chip = (enum ads7828_chips)
0143             of_device_get_match_data(&client->dev);
0144     else
0145         chip = i2c_match_id(ads7828_device_ids, client)->driver_data;
0146 
0147     /* Bound Vref with min/max values */
0148     vref_mv = clamp_val(vref_mv, ADS7828_EXT_VREF_MV_MIN,
0149                 ADS7828_EXT_VREF_MV_MAX);
0150 
0151     /* ADS7828 uses 12-bit samples, while ADS7830 is 8-bit */
0152     if (chip == ads7828) {
0153         data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 4096);
0154         data->regmap = devm_regmap_init_i2c(client,
0155                             &ads2828_regmap_config);
0156     } else {
0157         data->lsb_resol = DIV_ROUND_CLOSEST(vref_mv * 1000, 256);
0158         data->regmap = devm_regmap_init_i2c(client,
0159                             &ads2830_regmap_config);
0160     }
0161 
0162     if (IS_ERR(data->regmap))
0163         return PTR_ERR(data->regmap);
0164 
0165     data->cmd_byte = ext_vref ? ADS7828_CMD_PD1 : ADS7828_CMD_PD3;
0166     if (!diff_input)
0167         data->cmd_byte |= ADS7828_CMD_SD_SE;
0168 
0169     /*
0170      * Datasheet specifies internal reference voltage is disabled by
0171      * default. The internal reference voltage needs to be enabled and
0172      * voltage needs to settle before getting valid ADC data. So perform a
0173      * dummy read to enable the internal reference voltage.
0174      */
0175     if (!ext_vref)
0176         regmap_read(data->regmap, data->cmd_byte, &regval);
0177 
0178     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0179                                data,
0180                                ads7828_groups);
0181     return PTR_ERR_OR_ZERO(hwmon_dev);
0182 }
0183 
0184 static const struct i2c_device_id ads7828_device_ids[] = {
0185     { "ads7828", ads7828 },
0186     { "ads7830", ads7830 },
0187     { }
0188 };
0189 MODULE_DEVICE_TABLE(i2c, ads7828_device_ids);
0190 
0191 static const struct of_device_id __maybe_unused ads7828_of_match[] = {
0192     {
0193         .compatible = "ti,ads7828",
0194         .data = (void *)ads7828
0195     },
0196     {
0197         .compatible = "ti,ads7830",
0198         .data = (void *)ads7830
0199     },
0200     { },
0201 };
0202 MODULE_DEVICE_TABLE(of, ads7828_of_match);
0203 
0204 static struct i2c_driver ads7828_driver = {
0205     .driver = {
0206         .name = "ads7828",
0207         .of_match_table = of_match_ptr(ads7828_of_match),
0208     },
0209 
0210     .id_table = ads7828_device_ids,
0211     .probe_new = ads7828_probe,
0212 };
0213 
0214 module_i2c_driver(ads7828_driver);
0215 
0216 MODULE_LICENSE("GPL");
0217 MODULE_AUTHOR("Steve Hardy <shardy@redhat.com>");
0218 MODULE_DESCRIPTION("Driver for TI ADS7828 A/D converter and compatibles");