Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * AD7291 8-Channel, I2C, 12-Bit SAR ADC with Temperature Sensor
0004  *
0005  * Copyright 2010-2011 Analog Devices Inc.
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/err.h>
0010 #include <linux/i2c.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/mutex.h>
0015 #include <linux/regulator/consumer.h>
0016 #include <linux/slab.h>
0017 #include <linux/sysfs.h>
0018 
0019 #include <linux/iio/iio.h>
0020 #include <linux/iio/sysfs.h>
0021 #include <linux/iio/events.h>
0022 
0023 /*
0024  * Simplified handling
0025  *
0026  * If no events enabled - single polled channel read
0027  * If event enabled direct reads disable unless channel
0028  * is in the read mask.
0029  *
0030  * The noise-delayed bit as per datasheet suggestion is always enabled.
0031  */
0032 
0033 /*
0034  * AD7291 registers definition
0035  */
0036 #define AD7291_COMMAND          0x00
0037 #define AD7291_VOLTAGE          0x01
0038 #define AD7291_T_SENSE          0x02
0039 #define AD7291_T_AVERAGE        0x03
0040 #define AD7291_DATA_HIGH(x)     ((x) * 3 + 0x4)
0041 #define AD7291_DATA_LOW(x)      ((x) * 3 + 0x5)
0042 #define AD7291_HYST(x)          ((x) * 3 + 0x6)
0043 #define AD7291_VOLTAGE_ALERT_STATUS 0x1F
0044 #define AD7291_T_ALERT_STATUS       0x20
0045 
0046 #define AD7291_BITS         12
0047 #define AD7291_VOLTAGE_LIMIT_COUNT  8
0048 
0049 
0050 /*
0051  * AD7291 command
0052  */
0053 #define AD7291_AUTOCYCLE        BIT(0)
0054 #define AD7291_RESET            BIT(1)
0055 #define AD7291_ALERT_CLEAR      BIT(2)
0056 #define AD7291_ALERT_POLARITY       BIT(3)
0057 #define AD7291_EXT_REF          BIT(4)
0058 #define AD7291_NOISE_DELAY      BIT(5)
0059 #define AD7291_T_SENSE_MASK     BIT(7)
0060 #define AD7291_VOLTAGE_MASK     GENMASK(15, 8)
0061 #define AD7291_VOLTAGE_OFFSET       8
0062 
0063 /*
0064  * AD7291 value masks
0065  */
0066 #define AD7291_VALUE_MASK       GENMASK(11, 0)
0067 
0068 /*
0069  * AD7291 alert register bits
0070  */
0071 #define AD7291_T_LOW            BIT(0)
0072 #define AD7291_T_HIGH           BIT(1)
0073 #define AD7291_T_AVG_LOW        BIT(2)
0074 #define AD7291_T_AVG_HIGH       BIT(3)
0075 #define AD7291_V_LOW(x)         BIT((x) * 2)
0076 #define AD7291_V_HIGH(x)        BIT((x) * 2 + 1)
0077 
0078 
0079 struct ad7291_chip_info {
0080     struct i2c_client   *client;
0081     struct regulator    *reg;
0082     u16         command;
0083     u16         c_mask; /* Active voltage channels for events */
0084     struct mutex        state_lock;
0085 };
0086 
0087 static int ad7291_i2c_read(struct ad7291_chip_info *chip, u8 reg, u16 *data)
0088 {
0089     struct i2c_client *client = chip->client;
0090     int ret = 0;
0091 
0092     ret = i2c_smbus_read_word_swapped(client, reg);
0093     if (ret < 0) {
0094         dev_err(&client->dev, "I2C read error\n");
0095         return ret;
0096     }
0097 
0098     *data = ret;
0099 
0100     return 0;
0101 }
0102 
0103 static int ad7291_i2c_write(struct ad7291_chip_info *chip, u8 reg, u16 data)
0104 {
0105     return i2c_smbus_write_word_swapped(chip->client, reg, data);
0106 }
0107 
0108 static irqreturn_t ad7291_event_handler(int irq, void *private)
0109 {
0110     struct iio_dev *indio_dev = private;
0111     struct ad7291_chip_info *chip = iio_priv(private);
0112     u16 t_status, v_status;
0113     u16 command;
0114     int i;
0115     s64 timestamp = iio_get_time_ns(indio_dev);
0116 
0117     if (ad7291_i2c_read(chip, AD7291_T_ALERT_STATUS, &t_status))
0118         return IRQ_HANDLED;
0119 
0120     if (ad7291_i2c_read(chip, AD7291_VOLTAGE_ALERT_STATUS, &v_status))
0121         return IRQ_HANDLED;
0122 
0123     if (!(t_status || v_status))
0124         return IRQ_HANDLED;
0125 
0126     command = chip->command | AD7291_ALERT_CLEAR;
0127     ad7291_i2c_write(chip, AD7291_COMMAND, command);
0128 
0129     command = chip->command & ~AD7291_ALERT_CLEAR;
0130     ad7291_i2c_write(chip, AD7291_COMMAND, command);
0131 
0132     /* For now treat t_sense and t_sense_average the same */
0133     if ((t_status & AD7291_T_LOW) || (t_status & AD7291_T_AVG_LOW))
0134         iio_push_event(indio_dev,
0135                    IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0136                             0,
0137                             IIO_EV_TYPE_THRESH,
0138                             IIO_EV_DIR_FALLING),
0139                    timestamp);
0140     if ((t_status & AD7291_T_HIGH) || (t_status & AD7291_T_AVG_HIGH))
0141         iio_push_event(indio_dev,
0142                    IIO_UNMOD_EVENT_CODE(IIO_TEMP,
0143                             0,
0144                             IIO_EV_TYPE_THRESH,
0145                             IIO_EV_DIR_RISING),
0146                    timestamp);
0147 
0148     for (i = 0; i < AD7291_VOLTAGE_LIMIT_COUNT; i++) {
0149         if (v_status & AD7291_V_LOW(i))
0150             iio_push_event(indio_dev,
0151                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
0152                                 i,
0153                                 IIO_EV_TYPE_THRESH,
0154                                 IIO_EV_DIR_FALLING),
0155                        timestamp);
0156         if (v_status & AD7291_V_HIGH(i))
0157             iio_push_event(indio_dev,
0158                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
0159                                 i,
0160                                 IIO_EV_TYPE_THRESH,
0161                                 IIO_EV_DIR_RISING),
0162                        timestamp);
0163     }
0164 
0165     return IRQ_HANDLED;
0166 }
0167 
0168 static unsigned int ad7291_threshold_reg(const struct iio_chan_spec *chan,
0169                      enum iio_event_direction dir,
0170                      enum iio_event_info info)
0171 {
0172     unsigned int offset;
0173 
0174     switch (chan->type) {
0175     case IIO_VOLTAGE:
0176         offset = chan->channel;
0177         break;
0178     case IIO_TEMP:
0179         offset = AD7291_VOLTAGE_OFFSET;
0180         break;
0181     default:
0182         return 0;
0183     }
0184 
0185     switch (info) {
0186     case IIO_EV_INFO_VALUE:
0187         if (dir == IIO_EV_DIR_FALLING)
0188             return AD7291_DATA_HIGH(offset);
0189         else
0190             return AD7291_DATA_LOW(offset);
0191     case IIO_EV_INFO_HYSTERESIS:
0192         return AD7291_HYST(offset);
0193     default:
0194         break;
0195     }
0196     return 0;
0197 }
0198 
0199 static int ad7291_read_event_value(struct iio_dev *indio_dev,
0200                    const struct iio_chan_spec *chan,
0201                    enum iio_event_type type,
0202                    enum iio_event_direction dir,
0203                    enum iio_event_info info,
0204                    int *val, int *val2)
0205 {
0206     struct ad7291_chip_info *chip = iio_priv(indio_dev);
0207     int ret;
0208     u16 uval;
0209 
0210     ret = ad7291_i2c_read(chip, ad7291_threshold_reg(chan, dir, info),
0211                   &uval);
0212     if (ret < 0)
0213         return ret;
0214 
0215     if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE)
0216         *val = uval & AD7291_VALUE_MASK;
0217 
0218     else
0219         *val = sign_extend32(uval, 11);
0220 
0221     return IIO_VAL_INT;
0222 }
0223 
0224 static int ad7291_write_event_value(struct iio_dev *indio_dev,
0225                     const struct iio_chan_spec *chan,
0226                     enum iio_event_type type,
0227                     enum iio_event_direction dir,
0228                     enum iio_event_info info,
0229                     int val, int val2)
0230 {
0231     struct ad7291_chip_info *chip = iio_priv(indio_dev);
0232 
0233     if (info == IIO_EV_INFO_HYSTERESIS || chan->type == IIO_VOLTAGE) {
0234         if (val > AD7291_VALUE_MASK || val < 0)
0235             return -EINVAL;
0236     } else {
0237         if (val > 2047 || val < -2048)
0238             return -EINVAL;
0239     }
0240 
0241     return ad7291_i2c_write(chip, ad7291_threshold_reg(chan, dir, info),
0242                 val);
0243 }
0244 
0245 static int ad7291_read_event_config(struct iio_dev *indio_dev,
0246                     const struct iio_chan_spec *chan,
0247                     enum iio_event_type type,
0248                     enum iio_event_direction dir)
0249 {
0250     struct ad7291_chip_info *chip = iio_priv(indio_dev);
0251     /*
0252      * To be enabled the channel must simply be on. If any are enabled
0253      * we are in continuous sampling mode
0254      */
0255 
0256     switch (chan->type) {
0257     case IIO_VOLTAGE:
0258         return !!(chip->c_mask & BIT(15 - chan->channel));
0259     case IIO_TEMP:
0260         /* always on */
0261         return 1;
0262     default:
0263         return -EINVAL;
0264     }
0265 
0266 }
0267 
0268 static int ad7291_write_event_config(struct iio_dev *indio_dev,
0269                      const struct iio_chan_spec *chan,
0270                      enum iio_event_type type,
0271                      enum iio_event_direction dir,
0272                      int state)
0273 {
0274     int ret = 0;
0275     struct ad7291_chip_info *chip = iio_priv(indio_dev);
0276     unsigned int mask;
0277     u16 regval;
0278 
0279     mutex_lock(&chip->state_lock);
0280     regval = chip->command;
0281     /*
0282      * To be enabled the channel must simply be on. If any are enabled
0283      * use continuous sampling mode.
0284      * Possible to disable temp as well but that makes single read tricky.
0285      */
0286 
0287     mask = BIT(15 - chan->channel);
0288 
0289     switch (chan->type) {
0290     case IIO_VOLTAGE:
0291         if ((!state) && (chip->c_mask & mask))
0292             chip->c_mask &= ~mask;
0293         else if (state && (!(chip->c_mask & mask)))
0294             chip->c_mask |= mask;
0295         else
0296             break;
0297 
0298         regval &= ~AD7291_AUTOCYCLE;
0299         regval |= chip->c_mask;
0300         if (chip->c_mask) /* Enable autocycle? */
0301             regval |= AD7291_AUTOCYCLE;
0302 
0303         ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
0304         if (ret < 0)
0305             goto error_ret;
0306 
0307         chip->command = regval;
0308         break;
0309     default:
0310         ret = -EINVAL;
0311     }
0312 
0313 error_ret:
0314     mutex_unlock(&chip->state_lock);
0315     return ret;
0316 }
0317 
0318 static int ad7291_read_raw(struct iio_dev *indio_dev,
0319                struct iio_chan_spec const *chan,
0320                int *val,
0321                int *val2,
0322                long mask)
0323 {
0324     int ret;
0325     struct ad7291_chip_info *chip = iio_priv(indio_dev);
0326     u16 regval;
0327 
0328     switch (mask) {
0329     case IIO_CHAN_INFO_RAW:
0330         switch (chan->type) {
0331         case IIO_VOLTAGE:
0332             mutex_lock(&chip->state_lock);
0333             /* If in autocycle mode drop through */
0334             if (chip->command & AD7291_AUTOCYCLE) {
0335                 mutex_unlock(&chip->state_lock);
0336                 return -EBUSY;
0337             }
0338             /* Enable this channel alone */
0339             regval = chip->command & (~AD7291_VOLTAGE_MASK);
0340             regval |= BIT(15 - chan->channel);
0341             ret = ad7291_i2c_write(chip, AD7291_COMMAND, regval);
0342             if (ret < 0) {
0343                 mutex_unlock(&chip->state_lock);
0344                 return ret;
0345             }
0346             /* Read voltage */
0347             ret = i2c_smbus_read_word_swapped(chip->client,
0348                               AD7291_VOLTAGE);
0349             if (ret < 0) {
0350                 mutex_unlock(&chip->state_lock);
0351                 return ret;
0352             }
0353             *val = ret & AD7291_VALUE_MASK;
0354             mutex_unlock(&chip->state_lock);
0355             return IIO_VAL_INT;
0356         case IIO_TEMP:
0357             /* Assumes tsense bit of command register always set */
0358             ret = i2c_smbus_read_word_swapped(chip->client,
0359                               AD7291_T_SENSE);
0360             if (ret < 0)
0361                 return ret;
0362             *val = sign_extend32(ret, 11);
0363             return IIO_VAL_INT;
0364         default:
0365             return -EINVAL;
0366         }
0367     case IIO_CHAN_INFO_AVERAGE_RAW:
0368         ret = i2c_smbus_read_word_swapped(chip->client,
0369                           AD7291_T_AVERAGE);
0370             if (ret < 0)
0371                 return ret;
0372             *val = sign_extend32(ret, 11);
0373             return IIO_VAL_INT;
0374     case IIO_CHAN_INFO_SCALE:
0375         switch (chan->type) {
0376         case IIO_VOLTAGE:
0377             if (chip->reg) {
0378                 int vref;
0379 
0380                 vref = regulator_get_voltage(chip->reg);
0381                 if (vref < 0)
0382                     return vref;
0383                 *val = vref / 1000;
0384             } else {
0385                 *val = 2500;
0386             }
0387             *val2 = AD7291_BITS;
0388             return IIO_VAL_FRACTIONAL_LOG2;
0389         case IIO_TEMP:
0390             /*
0391              * One LSB of the ADC corresponds to 0.25 deg C.
0392              * The temperature reading is in 12-bit twos
0393              * complement format
0394              */
0395             *val = 250;
0396             return IIO_VAL_INT;
0397         default:
0398             return -EINVAL;
0399         }
0400     default:
0401         return -EINVAL;
0402     }
0403 }
0404 
0405 static const struct iio_event_spec ad7291_events[] = {
0406     {
0407         .type = IIO_EV_TYPE_THRESH,
0408         .dir = IIO_EV_DIR_RISING,
0409         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0410             BIT(IIO_EV_INFO_ENABLE),
0411     }, {
0412         .type = IIO_EV_TYPE_THRESH,
0413         .dir = IIO_EV_DIR_FALLING,
0414         .mask_separate = BIT(IIO_EV_INFO_VALUE) |
0415             BIT(IIO_EV_INFO_ENABLE),
0416     }, {
0417         .type = IIO_EV_TYPE_THRESH,
0418         .dir = IIO_EV_DIR_EITHER,
0419         .mask_separate = BIT(IIO_EV_INFO_HYSTERESIS),
0420     },
0421 };
0422 
0423 #define AD7291_VOLTAGE_CHAN(_chan)                  \
0424 {                                   \
0425     .type = IIO_VOLTAGE,                        \
0426     .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),           \
0427     .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),       \
0428     .indexed = 1,                           \
0429     .channel = _chan,                       \
0430     .event_spec = ad7291_events,                    \
0431     .num_event_specs = ARRAY_SIZE(ad7291_events),           \
0432 }
0433 
0434 static const struct iio_chan_spec ad7291_channels[] = {
0435     AD7291_VOLTAGE_CHAN(0),
0436     AD7291_VOLTAGE_CHAN(1),
0437     AD7291_VOLTAGE_CHAN(2),
0438     AD7291_VOLTAGE_CHAN(3),
0439     AD7291_VOLTAGE_CHAN(4),
0440     AD7291_VOLTAGE_CHAN(5),
0441     AD7291_VOLTAGE_CHAN(6),
0442     AD7291_VOLTAGE_CHAN(7),
0443     {
0444         .type = IIO_TEMP,
0445         .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
0446                 BIT(IIO_CHAN_INFO_AVERAGE_RAW) |
0447                 BIT(IIO_CHAN_INFO_SCALE),
0448         .indexed = 1,
0449         .channel = 0,
0450         .event_spec = ad7291_events,
0451         .num_event_specs = ARRAY_SIZE(ad7291_events),
0452     }
0453 };
0454 
0455 static const struct iio_info ad7291_info = {
0456     .read_raw = &ad7291_read_raw,
0457     .read_event_config = &ad7291_read_event_config,
0458     .write_event_config = &ad7291_write_event_config,
0459     .read_event_value = &ad7291_read_event_value,
0460     .write_event_value = &ad7291_write_event_value,
0461 };
0462 
0463 static void ad7291_reg_disable(void *reg)
0464 {
0465     regulator_disable(reg);
0466 }
0467 
0468 static int ad7291_probe(struct i2c_client *client,
0469             const struct i2c_device_id *id)
0470 {
0471     struct ad7291_chip_info *chip;
0472     struct iio_dev *indio_dev;
0473     int ret;
0474 
0475     indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
0476     if (!indio_dev)
0477         return -ENOMEM;
0478     chip = iio_priv(indio_dev);
0479 
0480     mutex_init(&chip->state_lock);
0481 
0482     chip->client = client;
0483 
0484     chip->command = AD7291_NOISE_DELAY |
0485             AD7291_T_SENSE_MASK | /* Tsense always enabled */
0486             AD7291_ALERT_POLARITY; /* set irq polarity low level */
0487 
0488     chip->reg = devm_regulator_get_optional(&client->dev, "vref");
0489     if (IS_ERR(chip->reg)) {
0490         if (PTR_ERR(chip->reg) != -ENODEV)
0491             return PTR_ERR(chip->reg);
0492 
0493         chip->reg = NULL;
0494     }
0495 
0496     if (chip->reg) {
0497         ret = regulator_enable(chip->reg);
0498         if (ret)
0499             return ret;
0500 
0501         ret = devm_add_action_or_reset(&client->dev, ad7291_reg_disable,
0502                            chip->reg);
0503         if (ret)
0504             return ret;
0505 
0506         chip->command |= AD7291_EXT_REF;
0507     }
0508 
0509     indio_dev->name = id->name;
0510     indio_dev->channels = ad7291_channels;
0511     indio_dev->num_channels = ARRAY_SIZE(ad7291_channels);
0512 
0513     indio_dev->info = &ad7291_info;
0514     indio_dev->modes = INDIO_DIRECT_MODE;
0515 
0516     ret = ad7291_i2c_write(chip, AD7291_COMMAND, AD7291_RESET);
0517     if (ret)
0518         return -EIO;
0519 
0520     ret = ad7291_i2c_write(chip, AD7291_COMMAND, chip->command);
0521     if (ret)
0522         return -EIO;
0523 
0524     if (client->irq > 0) {
0525         ret = devm_request_threaded_irq(&client->dev, client->irq,
0526                         NULL,
0527                         &ad7291_event_handler,
0528                         IRQF_TRIGGER_LOW | IRQF_ONESHOT,
0529                         id->name,
0530                         indio_dev);
0531         if (ret)
0532             return ret;
0533     }
0534 
0535     return devm_iio_device_register(&client->dev, indio_dev);
0536 }
0537 
0538 static const struct i2c_device_id ad7291_id[] = {
0539     { "ad7291", 0 },
0540     {}
0541 };
0542 
0543 MODULE_DEVICE_TABLE(i2c, ad7291_id);
0544 
0545 static const struct of_device_id ad7291_of_match[] = {
0546     { .compatible = "adi,ad7291" },
0547     {}
0548 };
0549 MODULE_DEVICE_TABLE(of, ad7291_of_match);
0550 
0551 static struct i2c_driver ad7291_driver = {
0552     .driver = {
0553         .name = KBUILD_MODNAME,
0554         .of_match_table = ad7291_of_match,
0555     },
0556     .probe = ad7291_probe,
0557     .id_table = ad7291_id,
0558 };
0559 module_i2c_driver(ad7291_driver);
0560 
0561 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
0562 MODULE_DESCRIPTION("Analog Devices AD7291 ADC driver");
0563 MODULE_LICENSE("GPL v2");