1 /* 2 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC 3 * 4 * Copyright (C) 2017 Analog Devices Inc. 5 * 6 * Licensed under the GPL-2. 7 * 8 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/i2c.h> 13 #include <linux/iio/iio.h> 14 #include <linux/iio/sysfs.h> 15 #include <linux/module.h> 16 #include <linux/of.h> 17 #include <linux/regulator/consumer.h> 18 19 #define LTC2497_ENABLE 0xA0 20 #define LTC2497_SGL BIT(4) 21 #define LTC2497_DIFF 0 22 #define LTC2497_SIGN BIT(3) 23 #define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE 24 #define LTC2497_CONVERSION_TIME_MS 150ULL 25 26 struct ltc2497_st { 27 struct i2c_client *client; 28 struct regulator *ref; 29 ktime_t time_prev; 30 u8 addr_prev; 31 /* 32 * DMA (thus cache coherency maintenance) requires the 33 * transfer buffers to live in their own cache lines. 34 */ 35 __be32 buf ____cacheline_aligned; 36 }; 37 38 static int ltc2497_wait_conv(struct ltc2497_st *st) 39 { 40 s64 time_elapsed; 41 42 time_elapsed = ktime_ms_delta(ktime_get(), st->time_prev); 43 44 if (time_elapsed < LTC2497_CONVERSION_TIME_MS) { 45 /* delay if conversion time not passed 46 * since last read or write 47 */ 48 if (msleep_interruptible( 49 LTC2497_CONVERSION_TIME_MS - time_elapsed)) 50 return -ERESTARTSYS; 51 52 return 0; 53 } 54 55 if (time_elapsed - LTC2497_CONVERSION_TIME_MS <= 0) { 56 /* We're in automatic mode - 57 * so the last reading is stil not outdated 58 */ 59 return 0; 60 } 61 62 return 1; 63 } 64 65 static int ltc2497_read(struct ltc2497_st *st, u8 address, int *val) 66 { 67 struct i2c_client *client = st->client; 68 int ret; 69 70 ret = ltc2497_wait_conv(st); 71 if (ret < 0) 72 return ret; 73 74 if (ret || st->addr_prev != address) { 75 ret = i2c_smbus_write_byte(st->client, 76 LTC2497_ENABLE | address); 77 if (ret < 0) 78 return ret; 79 st->addr_prev = address; 80 if (msleep_interruptible(LTC2497_CONVERSION_TIME_MS)) 81 return -ERESTARTSYS; 82 } 83 ret = i2c_master_recv(client, (char *)&st->buf, 3); 84 if (ret < 0) { 85 dev_err(&client->dev, "i2c_master_recv failed\n"); 86 return ret; 87 } 88 st->time_prev = ktime_get(); 89 90 /* convert and shift the result, 91 * and finally convert from offset binary to signed integer 92 */ 93 *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17); 94 95 return ret; 96 } 97 98 static int ltc2497_read_raw(struct iio_dev *indio_dev, 99 struct iio_chan_spec const *chan, 100 int *val, int *val2, long mask) 101 { 102 struct ltc2497_st *st = iio_priv(indio_dev); 103 int ret; 104 105 switch (mask) { 106 case IIO_CHAN_INFO_RAW: 107 mutex_lock(&indio_dev->mlock); 108 ret = ltc2497_read(st, chan->address, val); 109 mutex_unlock(&indio_dev->mlock); 110 if (ret < 0) 111 return ret; 112 113 return IIO_VAL_INT; 114 115 case IIO_CHAN_INFO_SCALE: 116 ret = regulator_get_voltage(st->ref); 117 if (ret < 0) 118 return ret; 119 120 *val = ret / 1000; 121 *val2 = 17; 122 123 return IIO_VAL_FRACTIONAL_LOG2; 124 125 default: 126 return -EINVAL; 127 } 128 } 129 130 #define LTC2497_CHAN(_chan, _addr) { \ 131 .type = IIO_VOLTAGE, \ 132 .indexed = 1, \ 133 .channel = (_chan), \ 134 .address = (_addr | (_chan / 2) | ((_chan & 1) ? LTC2497_SIGN : 0)), \ 135 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 136 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 137 } 138 139 #define LTC2497_CHAN_DIFF(_chan, _addr) { \ 140 .type = IIO_VOLTAGE, \ 141 .indexed = 1, \ 142 .channel = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 1 : 0), \ 143 .channel2 = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 0 : 1),\ 144 .address = (_addr | _chan), \ 145 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 146 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 147 .differential = 1, \ 148 } 149 150 static const struct iio_chan_spec ltc2497_channel[] = { 151 LTC2497_CHAN(0, LTC2497_SGL), 152 LTC2497_CHAN(1, LTC2497_SGL), 153 LTC2497_CHAN(2, LTC2497_SGL), 154 LTC2497_CHAN(3, LTC2497_SGL), 155 LTC2497_CHAN(4, LTC2497_SGL), 156 LTC2497_CHAN(5, LTC2497_SGL), 157 LTC2497_CHAN(6, LTC2497_SGL), 158 LTC2497_CHAN(7, LTC2497_SGL), 159 LTC2497_CHAN(8, LTC2497_SGL), 160 LTC2497_CHAN(9, LTC2497_SGL), 161 LTC2497_CHAN(10, LTC2497_SGL), 162 LTC2497_CHAN(11, LTC2497_SGL), 163 LTC2497_CHAN(12, LTC2497_SGL), 164 LTC2497_CHAN(13, LTC2497_SGL), 165 LTC2497_CHAN(14, LTC2497_SGL), 166 LTC2497_CHAN(15, LTC2497_SGL), 167 LTC2497_CHAN_DIFF(0, LTC2497_DIFF), 168 LTC2497_CHAN_DIFF(1, LTC2497_DIFF), 169 LTC2497_CHAN_DIFF(2, LTC2497_DIFF), 170 LTC2497_CHAN_DIFF(3, LTC2497_DIFF), 171 LTC2497_CHAN_DIFF(4, LTC2497_DIFF), 172 LTC2497_CHAN_DIFF(5, LTC2497_DIFF), 173 LTC2497_CHAN_DIFF(6, LTC2497_DIFF), 174 LTC2497_CHAN_DIFF(7, LTC2497_DIFF), 175 LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN), 176 LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN), 177 LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN), 178 LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN), 179 LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN), 180 LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN), 181 LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN), 182 LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN), 183 }; 184 185 static const struct iio_info ltc2497_info = { 186 .read_raw = ltc2497_read_raw, 187 .driver_module = THIS_MODULE, 188 }; 189 190 static int ltc2497_probe(struct i2c_client *client, 191 const struct i2c_device_id *id) 192 { 193 struct iio_dev *indio_dev; 194 struct ltc2497_st *st; 195 int ret; 196 197 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 198 I2C_FUNC_SMBUS_WRITE_BYTE)) 199 return -EOPNOTSUPP; 200 201 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); 202 if (!indio_dev) 203 return -ENOMEM; 204 205 st = iio_priv(indio_dev); 206 i2c_set_clientdata(client, indio_dev); 207 st->client = client; 208 209 indio_dev->dev.parent = &client->dev; 210 indio_dev->name = id->name; 211 indio_dev->info = <c2497_info; 212 indio_dev->modes = INDIO_DIRECT_MODE; 213 indio_dev->channels = ltc2497_channel; 214 indio_dev->num_channels = ARRAY_SIZE(ltc2497_channel); 215 216 st->ref = devm_regulator_get(&client->dev, "vref"); 217 if (IS_ERR(st->ref)) 218 return PTR_ERR(st->ref); 219 220 ret = regulator_enable(st->ref); 221 if (ret < 0) 222 return ret; 223 224 ret = i2c_smbus_write_byte(st->client, LTC2497_CONFIG_DEFAULT); 225 if (ret < 0) 226 goto err_regulator_disable; 227 228 st->addr_prev = LTC2497_CONFIG_DEFAULT; 229 st->time_prev = ktime_get(); 230 231 ret = iio_device_register(indio_dev); 232 if (ret < 0) 233 goto err_regulator_disable; 234 235 return 0; 236 237 err_regulator_disable: 238 regulator_disable(st->ref); 239 240 return ret; 241 } 242 243 static int ltc2497_remove(struct i2c_client *client) 244 { 245 struct iio_dev *indio_dev = i2c_get_clientdata(client); 246 struct ltc2497_st *st = iio_priv(indio_dev); 247 248 iio_device_unregister(indio_dev); 249 regulator_disable(st->ref); 250 251 return 0; 252 } 253 254 static const struct i2c_device_id ltc2497_id[] = { 255 { "ltc2497", 0 }, 256 { } 257 }; 258 MODULE_DEVICE_TABLE(i2c, ltc2497_id); 259 260 static const struct of_device_id ltc2497_of_match[] = { 261 { .compatible = "lltc,ltc2497", }, 262 {}, 263 }; 264 MODULE_DEVICE_TABLE(of, ltc2497_of_match); 265 266 static struct i2c_driver ltc2497_driver = { 267 .driver = { 268 .name = "ltc2497", 269 .of_match_table = of_match_ptr(ltc2497_of_match), 270 }, 271 .probe = ltc2497_probe, 272 .remove = ltc2497_remove, 273 .id_table = ltc2497_id, 274 }; 275 module_i2c_driver(ltc2497_driver); 276 277 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 278 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver"); 279 MODULE_LICENSE("GPL v2"); 280