0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitops.h>
0009 #include <linux/iio/events.h>
0010 #include <linux/iio/iio.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/module.h>
0013 #include <linux/regmap.h>
0014 #include <linux/regulator/consumer.h>
0015
0016 #include "ad7091r-base.h"
0017
0018 #define AD7091R_REG_RESULT 0
0019 #define AD7091R_REG_CHANNEL 1
0020 #define AD7091R_REG_CONF 2
0021 #define AD7091R_REG_ALERT 3
0022 #define AD7091R_REG_CH_LOW_LIMIT(ch) ((ch) * 3 + 4)
0023 #define AD7091R_REG_CH_HIGH_LIMIT(ch) ((ch) * 3 + 5)
0024 #define AD7091R_REG_CH_HYSTERESIS(ch) ((ch) * 3 + 6)
0025
0026
0027 #define AD7091R_REG_RESULT_CH_ID(x) (((x) >> 13) & 0x3)
0028 #define AD7091R_REG_RESULT_CONV_RESULT(x) ((x) & 0xfff)
0029
0030
0031 #define AD7091R_REG_CONF_AUTO BIT(8)
0032 #define AD7091R_REG_CONF_CMD BIT(10)
0033
0034 #define AD7091R_REG_CONF_MODE_MASK \
0035 (AD7091R_REG_CONF_AUTO | AD7091R_REG_CONF_CMD)
0036
0037 enum ad7091r_mode {
0038 AD7091R_MODE_SAMPLE,
0039 AD7091R_MODE_COMMAND,
0040 AD7091R_MODE_AUTOCYCLE,
0041 };
0042
0043 struct ad7091r_state {
0044 struct device *dev;
0045 struct regmap *map;
0046 struct regulator *vref;
0047 const struct ad7091r_chip_info *chip_info;
0048 enum ad7091r_mode mode;
0049 struct mutex lock;
0050 };
0051
0052 static int ad7091r_set_mode(struct ad7091r_state *st, enum ad7091r_mode mode)
0053 {
0054 int ret, conf;
0055
0056 switch (mode) {
0057 case AD7091R_MODE_SAMPLE:
0058 conf = 0;
0059 break;
0060 case AD7091R_MODE_COMMAND:
0061 conf = AD7091R_REG_CONF_CMD;
0062 break;
0063 case AD7091R_MODE_AUTOCYCLE:
0064 conf = AD7091R_REG_CONF_AUTO;
0065 break;
0066 default:
0067 return -EINVAL;
0068 }
0069
0070 ret = regmap_update_bits(st->map, AD7091R_REG_CONF,
0071 AD7091R_REG_CONF_MODE_MASK, conf);
0072 if (ret)
0073 return ret;
0074
0075 st->mode = mode;
0076
0077 return 0;
0078 }
0079
0080 static int ad7091r_set_channel(struct ad7091r_state *st, unsigned int channel)
0081 {
0082 unsigned int dummy;
0083 int ret;
0084
0085
0086 ret = regmap_write(st->map, AD7091R_REG_CHANNEL,
0087 BIT(channel) | (BIT(channel) << 8));
0088 if (ret)
0089 return ret;
0090
0091
0092
0093
0094
0095 return regmap_read(st->map, AD7091R_REG_RESULT, &dummy);
0096 }
0097
0098 static int ad7091r_read_one(struct iio_dev *iio_dev,
0099 unsigned int channel, unsigned int *read_val)
0100 {
0101 struct ad7091r_state *st = iio_priv(iio_dev);
0102 unsigned int val;
0103 int ret;
0104
0105 ret = ad7091r_set_channel(st, channel);
0106 if (ret)
0107 return ret;
0108
0109 ret = regmap_read(st->map, AD7091R_REG_RESULT, &val);
0110 if (ret)
0111 return ret;
0112
0113 if (AD7091R_REG_RESULT_CH_ID(val) != channel)
0114 return -EIO;
0115
0116 *read_val = AD7091R_REG_RESULT_CONV_RESULT(val);
0117
0118 return 0;
0119 }
0120
0121 static int ad7091r_read_raw(struct iio_dev *iio_dev,
0122 struct iio_chan_spec const *chan,
0123 int *val, int *val2, long m)
0124 {
0125 struct ad7091r_state *st = iio_priv(iio_dev);
0126 unsigned int read_val;
0127 int ret;
0128
0129 mutex_lock(&st->lock);
0130
0131 switch (m) {
0132 case IIO_CHAN_INFO_RAW:
0133 if (st->mode != AD7091R_MODE_COMMAND) {
0134 ret = -EBUSY;
0135 goto unlock;
0136 }
0137
0138 ret = ad7091r_read_one(iio_dev, chan->channel, &read_val);
0139 if (ret)
0140 goto unlock;
0141
0142 *val = read_val;
0143 ret = IIO_VAL_INT;
0144 break;
0145
0146 case IIO_CHAN_INFO_SCALE:
0147 if (st->vref) {
0148 ret = regulator_get_voltage(st->vref);
0149 if (ret < 0)
0150 goto unlock;
0151
0152 *val = ret / 1000;
0153 } else {
0154 *val = st->chip_info->vref_mV;
0155 }
0156
0157 *val2 = chan->scan_type.realbits;
0158 ret = IIO_VAL_FRACTIONAL_LOG2;
0159 break;
0160
0161 default:
0162 ret = -EINVAL;
0163 break;
0164 }
0165
0166 unlock:
0167 mutex_unlock(&st->lock);
0168 return ret;
0169 }
0170
0171 static const struct iio_info ad7091r_info = {
0172 .read_raw = ad7091r_read_raw,
0173 };
0174
0175 static irqreturn_t ad7091r_event_handler(int irq, void *private)
0176 {
0177 struct ad7091r_state *st = (struct ad7091r_state *) private;
0178 struct iio_dev *iio_dev = dev_get_drvdata(st->dev);
0179 unsigned int i, read_val;
0180 int ret;
0181 s64 timestamp = iio_get_time_ns(iio_dev);
0182
0183 ret = regmap_read(st->map, AD7091R_REG_ALERT, &read_val);
0184 if (ret)
0185 return IRQ_HANDLED;
0186
0187 for (i = 0; i < st->chip_info->num_channels; i++) {
0188 if (read_val & BIT(i * 2))
0189 iio_push_event(iio_dev,
0190 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
0191 IIO_EV_TYPE_THRESH,
0192 IIO_EV_DIR_RISING), timestamp);
0193 if (read_val & BIT(i * 2 + 1))
0194 iio_push_event(iio_dev,
0195 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
0196 IIO_EV_TYPE_THRESH,
0197 IIO_EV_DIR_FALLING), timestamp);
0198 }
0199
0200 return IRQ_HANDLED;
0201 }
0202
0203 static void ad7091r_remove(void *data)
0204 {
0205 struct ad7091r_state *st = data;
0206
0207 regulator_disable(st->vref);
0208 }
0209
0210 int ad7091r_probe(struct device *dev, const char *name,
0211 const struct ad7091r_chip_info *chip_info,
0212 struct regmap *map, int irq)
0213 {
0214 struct iio_dev *iio_dev;
0215 struct ad7091r_state *st;
0216 int ret;
0217
0218 iio_dev = devm_iio_device_alloc(dev, sizeof(*st));
0219 if (!iio_dev)
0220 return -ENOMEM;
0221
0222 st = iio_priv(iio_dev);
0223 st->dev = dev;
0224 st->chip_info = chip_info;
0225 st->map = map;
0226
0227 iio_dev->name = name;
0228 iio_dev->info = &ad7091r_info;
0229 iio_dev->modes = INDIO_DIRECT_MODE;
0230
0231 iio_dev->num_channels = chip_info->num_channels;
0232 iio_dev->channels = chip_info->channels;
0233
0234 if (irq) {
0235 ret = devm_request_threaded_irq(dev, irq, NULL,
0236 ad7091r_event_handler,
0237 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, name, st);
0238 if (ret)
0239 return ret;
0240 }
0241
0242 st->vref = devm_regulator_get_optional(dev, "vref");
0243 if (IS_ERR(st->vref)) {
0244 if (PTR_ERR(st->vref) == -EPROBE_DEFER)
0245 return -EPROBE_DEFER;
0246 st->vref = NULL;
0247 } else {
0248 ret = regulator_enable(st->vref);
0249 if (ret)
0250 return ret;
0251 ret = devm_add_action_or_reset(dev, ad7091r_remove, st);
0252 if (ret)
0253 return ret;
0254 }
0255
0256
0257 ret = ad7091r_set_mode(st, AD7091R_MODE_COMMAND);
0258 if (ret)
0259 return ret;
0260
0261 return devm_iio_device_register(dev, iio_dev);
0262 }
0263 EXPORT_SYMBOL_NS_GPL(ad7091r_probe, IIO_AD7091R);
0264
0265 static bool ad7091r_writeable_reg(struct device *dev, unsigned int reg)
0266 {
0267 switch (reg) {
0268 case AD7091R_REG_RESULT:
0269 case AD7091R_REG_ALERT:
0270 return false;
0271 default:
0272 return true;
0273 }
0274 }
0275
0276 static bool ad7091r_volatile_reg(struct device *dev, unsigned int reg)
0277 {
0278 switch (reg) {
0279 case AD7091R_REG_RESULT:
0280 case AD7091R_REG_ALERT:
0281 return true;
0282 default:
0283 return false;
0284 }
0285 }
0286
0287 const struct regmap_config ad7091r_regmap_config = {
0288 .reg_bits = 8,
0289 .val_bits = 16,
0290 .writeable_reg = ad7091r_writeable_reg,
0291 .volatile_reg = ad7091r_volatile_reg,
0292 };
0293 EXPORT_SYMBOL_NS_GPL(ad7091r_regmap_config, IIO_AD7091R);
0294
0295 MODULE_AUTHOR("Beniamin Bia <beniamin.bia@analog.com>");
0296 MODULE_DESCRIPTION("Analog Devices AD7091Rx multi-channel converters");
0297 MODULE_LICENSE("GPL v2");