0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/kernel.h>
0009 #include <linux/device.h>
0010 #include <linux/errno.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/init.h>
0013 #include <linux/module.h>
0014 #include <linux/mfd/rn5t618.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/completion.h>
0017 #include <linux/regmap.h>
0018 #include <linux/iio/iio.h>
0019 #include <linux/iio/driver.h>
0020 #include <linux/iio/machine.h>
0021 #include <linux/slab.h>
0022
0023 #define RN5T618_ADC_CONVERSION_TIMEOUT (msecs_to_jiffies(500))
0024 #define RN5T618_REFERENCE_VOLT 2500
0025
0026
0027 #define RN5T618_ADCCNT3_CHANNEL_MASK 0x7
0028
0029 #define RN5T618_ADCCNT3_AVG BIT(3)
0030
0031 #define RN5T618_ADCCNT3_GODONE BIT(4)
0032
0033
0034
0035 #define RN5T618_ADCCNT3_AUTO BIT(5)
0036 #define RN5T618_ADCEND_IRQ BIT(0)
0037
0038 struct rn5t618_adc_data {
0039 struct device *dev;
0040 struct rn5t618 *rn5t618;
0041 struct completion conv_completion;
0042 int irq;
0043 };
0044
0045 enum rn5t618_channels {
0046 LIMMON = 0,
0047 VBAT,
0048 VADP,
0049 VUSB,
0050 VSYS,
0051 VTHM,
0052 AIN1,
0053 AIN0
0054 };
0055
0056 static const struct u16_fract rn5t618_ratios[8] = {
0057 [LIMMON] = {50, 32},
0058 [VBAT] = {2, 1},
0059 [VADP] = {3, 1},
0060 [VUSB] = {3, 1},
0061 [VSYS] = {3, 1},
0062 [VTHM] = {1, 1},
0063 [AIN1] = {1, 1},
0064 [AIN0] = {1, 1},
0065 };
0066
0067 static int rn5t618_read_adc_reg(struct rn5t618 *rn5t618, int reg, u16 *val)
0068 {
0069 u8 data[2];
0070 int ret;
0071
0072 ret = regmap_bulk_read(rn5t618->regmap, reg, data, sizeof(data));
0073 if (ret < 0)
0074 return ret;
0075
0076 *val = (data[0] << 4) | (data[1] & 0xF);
0077
0078 return 0;
0079 }
0080
0081 static irqreturn_t rn5t618_adc_irq(int irq, void *data)
0082 {
0083 struct rn5t618_adc_data *adc = data;
0084 unsigned int r = 0;
0085 int ret;
0086
0087
0088 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC1, 0);
0089 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC2, 0);
0090
0091 ret = regmap_read(adc->rn5t618->regmap, RN5T618_IR_ADC3, &r);
0092 if (ret < 0)
0093 dev_err(adc->dev, "failed to read IRQ status: %d\n", ret);
0094
0095 regmap_write(adc->rn5t618->regmap, RN5T618_IR_ADC3, 0);
0096
0097 if (r & RN5T618_ADCEND_IRQ)
0098 complete(&adc->conv_completion);
0099
0100 return IRQ_HANDLED;
0101 }
0102
0103 static int rn5t618_adc_read(struct iio_dev *iio_dev,
0104 const struct iio_chan_spec *chan,
0105 int *val, int *val2, long mask)
0106 {
0107 struct rn5t618_adc_data *adc = iio_priv(iio_dev);
0108 u16 raw;
0109 int ret;
0110
0111 if (mask == IIO_CHAN_INFO_SCALE) {
0112 *val = RN5T618_REFERENCE_VOLT *
0113 rn5t618_ratios[chan->channel].numerator;
0114 *val2 = rn5t618_ratios[chan->channel].denominator * 4095;
0115
0116 return IIO_VAL_FRACTIONAL;
0117 }
0118
0119
0120 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
0121 RN5T618_ADCCNT3_CHANNEL_MASK,
0122 chan->channel);
0123 if (ret < 0)
0124 return ret;
0125
0126 ret = regmap_write(adc->rn5t618->regmap, RN5T618_EN_ADCIR3,
0127 RN5T618_ADCEND_IRQ);
0128 if (ret < 0)
0129 return ret;
0130
0131 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
0132 RN5T618_ADCCNT3_AVG,
0133 mask == IIO_CHAN_INFO_AVERAGE_RAW ?
0134 RN5T618_ADCCNT3_AVG : 0);
0135 if (ret < 0)
0136 return ret;
0137
0138 init_completion(&adc->conv_completion);
0139
0140 ret = regmap_update_bits(adc->rn5t618->regmap, RN5T618_ADCCNT3,
0141 RN5T618_ADCCNT3_GODONE,
0142 RN5T618_ADCCNT3_GODONE);
0143 if (ret < 0)
0144 return ret;
0145
0146 ret = wait_for_completion_timeout(&adc->conv_completion,
0147 RN5T618_ADC_CONVERSION_TIMEOUT);
0148 if (ret == 0) {
0149 dev_warn(adc->dev, "timeout waiting for adc result\n");
0150 return -ETIMEDOUT;
0151 }
0152
0153 ret = rn5t618_read_adc_reg(adc->rn5t618,
0154 RN5T618_ILIMDATAH + 2 * chan->channel,
0155 &raw);
0156 if (ret < 0)
0157 return ret;
0158
0159 *val = raw;
0160
0161 return IIO_VAL_INT;
0162 }
0163
0164 static const struct iio_info rn5t618_adc_iio_info = {
0165 .read_raw = &rn5t618_adc_read,
0166 };
0167
0168 #define RN5T618_ADC_CHANNEL(_channel, _type, _name) { \
0169 .type = _type, \
0170 .channel = _channel, \
0171 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
0172 BIT(IIO_CHAN_INFO_AVERAGE_RAW) | \
0173 BIT(IIO_CHAN_INFO_SCALE), \
0174 .datasheet_name = _name, \
0175 .indexed = 1. \
0176 }
0177
0178 static const struct iio_chan_spec rn5t618_adc_iio_channels[] = {
0179 RN5T618_ADC_CHANNEL(LIMMON, IIO_CURRENT, "LIMMON"),
0180 RN5T618_ADC_CHANNEL(VBAT, IIO_VOLTAGE, "VBAT"),
0181 RN5T618_ADC_CHANNEL(VADP, IIO_VOLTAGE, "VADP"),
0182 RN5T618_ADC_CHANNEL(VUSB, IIO_VOLTAGE, "VUSB"),
0183 RN5T618_ADC_CHANNEL(VSYS, IIO_VOLTAGE, "VSYS"),
0184 RN5T618_ADC_CHANNEL(VTHM, IIO_VOLTAGE, "VTHM"),
0185 RN5T618_ADC_CHANNEL(AIN1, IIO_VOLTAGE, "AIN1"),
0186 RN5T618_ADC_CHANNEL(AIN0, IIO_VOLTAGE, "AIN0")
0187 };
0188
0189 static struct iio_map rn5t618_maps[] = {
0190 IIO_MAP("VADP", "rn5t618-power", "vadp"),
0191 IIO_MAP("VUSB", "rn5t618-power", "vusb"),
0192 { }
0193 };
0194
0195 static int rn5t618_adc_probe(struct platform_device *pdev)
0196 {
0197 int ret;
0198 struct iio_dev *iio_dev;
0199 struct rn5t618_adc_data *adc;
0200 struct rn5t618 *rn5t618 = dev_get_drvdata(pdev->dev.parent);
0201
0202 iio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc));
0203 if (!iio_dev) {
0204 dev_err(&pdev->dev, "failed allocating iio device\n");
0205 return -ENOMEM;
0206 }
0207
0208 adc = iio_priv(iio_dev);
0209 adc->dev = &pdev->dev;
0210 adc->rn5t618 = rn5t618;
0211
0212 if (rn5t618->irq_data)
0213 adc->irq = regmap_irq_get_virq(rn5t618->irq_data,
0214 RN5T618_IRQ_ADC);
0215
0216 if (adc->irq <= 0) {
0217 dev_err(&pdev->dev, "get virq failed\n");
0218 return -EINVAL;
0219 }
0220
0221 init_completion(&adc->conv_completion);
0222
0223 iio_dev->name = dev_name(&pdev->dev);
0224 iio_dev->info = &rn5t618_adc_iio_info;
0225 iio_dev->modes = INDIO_DIRECT_MODE;
0226 iio_dev->channels = rn5t618_adc_iio_channels;
0227 iio_dev->num_channels = ARRAY_SIZE(rn5t618_adc_iio_channels);
0228
0229
0230 ret = regmap_write(rn5t618->regmap, RN5T618_ADCCNT3, 0);
0231 if (ret < 0)
0232 return ret;
0233
0234 platform_set_drvdata(pdev, iio_dev);
0235
0236 ret = devm_request_threaded_irq(adc->dev, adc->irq, NULL,
0237 rn5t618_adc_irq,
0238 IRQF_ONESHOT, dev_name(adc->dev),
0239 adc);
0240 if (ret < 0) {
0241 dev_err(adc->dev, "request irq %d failed: %d\n", adc->irq, ret);
0242 return ret;
0243 }
0244
0245 ret = devm_iio_map_array_register(adc->dev, iio_dev, rn5t618_maps);
0246 if (ret < 0)
0247 return ret;
0248
0249 return devm_iio_device_register(adc->dev, iio_dev);
0250 }
0251
0252 static struct platform_driver rn5t618_adc_driver = {
0253 .driver = {
0254 .name = "rn5t618-adc",
0255 },
0256 .probe = rn5t618_adc_probe,
0257 };
0258
0259 module_platform_driver(rn5t618_adc_driver);
0260 MODULE_ALIAS("platform:rn5t618-adc");
0261 MODULE_DESCRIPTION("RICOH RN5T618 ADC driver");
0262 MODULE_LICENSE("GPL");