Angelo Compagnucci | 07914c8 | 2013-09-02 15:00:00 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * mcp3422.c - driver for the Microchip mcp3422/3/4 chip family |
| 3 | * |
| 4 | * Copyright (C) 2013, Angelo Compagnucci |
| 5 | * Author: Angelo Compagnucci <angelo.compagnucci@gmail.com> |
| 6 | * |
| 7 | * Datasheet: http://ww1.microchip.com/downloads/en/devicedoc/22088b.pdf |
| 8 | * |
| 9 | * This driver exports the value of analog input voltage to sysfs, the |
| 10 | * voltage unit is nV. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/i2c.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/sysfs.h> |
| 23 | |
| 24 | #include <linux/iio/iio.h> |
| 25 | #include <linux/iio/sysfs.h> |
| 26 | |
| 27 | /* Masks */ |
| 28 | #define MCP3422_CHANNEL_MASK 0x60 |
| 29 | #define MCP3422_PGA_MASK 0x03 |
| 30 | #define MCP3422_SRATE_MASK 0x0C |
| 31 | #define MCP3422_SRATE_240 0x0 |
| 32 | #define MCP3422_SRATE_60 0x1 |
| 33 | #define MCP3422_SRATE_15 0x2 |
| 34 | #define MCP3422_SRATE_3 0x3 |
| 35 | #define MCP3422_PGA_1 0 |
| 36 | #define MCP3422_PGA_2 1 |
| 37 | #define MCP3422_PGA_4 2 |
| 38 | #define MCP3422_PGA_8 3 |
| 39 | #define MCP3422_CONT_SAMPLING 0x10 |
| 40 | |
| 41 | #define MCP3422_CHANNEL(config) (((config) & MCP3422_CHANNEL_MASK) >> 5) |
| 42 | #define MCP3422_PGA(config) ((config) & MCP3422_PGA_MASK) |
| 43 | #define MCP3422_SAMPLE_RATE(config) (((config) & MCP3422_SRATE_MASK) >> 2) |
| 44 | |
| 45 | #define MCP3422_CHANNEL_VALUE(value) (((value) << 5) & MCP3422_CHANNEL_MASK) |
| 46 | #define MCP3422_PGA_VALUE(value) ((value) & MCP3422_PGA_MASK) |
| 47 | #define MCP3422_SAMPLE_RATE_VALUE(value) ((value << 2) & MCP3422_SRATE_MASK) |
| 48 | |
| 49 | #define MCP3422_CHAN(_index) \ |
| 50 | { \ |
| 51 | .type = IIO_VOLTAGE, \ |
| 52 | .indexed = 1, \ |
| 53 | .channel = _index, \ |
| 54 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) \ |
| 55 | | BIT(IIO_CHAN_INFO_SCALE), \ |
| 56 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ |
| 57 | } |
| 58 | |
| 59 | /* LSB is in nV to eliminate floating point */ |
| 60 | static const u32 rates_to_lsb[] = {1000000, 250000, 62500, 15625}; |
| 61 | |
| 62 | /* |
| 63 | * scales calculated as: |
| 64 | * rates_to_lsb[sample_rate] / (1 << pga); |
| 65 | * pga is 1 for 0, 2 |
| 66 | */ |
| 67 | |
| 68 | static const int mcp3422_scales[4][4] = { |
| 69 | { 1000000, 250000, 62500, 15625 }, |
| 70 | { 500000 , 125000, 31250, 7812 }, |
| 71 | { 250000 , 62500 , 15625, 3906 }, |
| 72 | { 125000 , 31250 , 7812 , 1953 } }; |
| 73 | |
| 74 | /* Constant msleep times for data acquisitions */ |
| 75 | static const int mcp3422_read_times[4] = { |
| 76 | [MCP3422_SRATE_240] = 1000 / 240, |
| 77 | [MCP3422_SRATE_60] = 1000 / 60, |
| 78 | [MCP3422_SRATE_15] = 1000 / 15, |
| 79 | [MCP3422_SRATE_3] = 1000 / 3 }; |
| 80 | |
| 81 | /* sample rates to integer conversion table */ |
| 82 | static const int mcp3422_sample_rates[4] = { |
| 83 | [MCP3422_SRATE_240] = 240, |
| 84 | [MCP3422_SRATE_60] = 60, |
| 85 | [MCP3422_SRATE_15] = 15, |
| 86 | [MCP3422_SRATE_3] = 3 }; |
| 87 | |
| 88 | /* sample rates to sign extension table */ |
| 89 | static const int mcp3422_sign_extend[4] = { |
| 90 | [MCP3422_SRATE_240] = 12, |
| 91 | [MCP3422_SRATE_60] = 14, |
| 92 | [MCP3422_SRATE_15] = 16, |
| 93 | [MCP3422_SRATE_3] = 18 }; |
| 94 | |
| 95 | /* Client data (each client gets its own) */ |
| 96 | struct mcp3422 { |
| 97 | struct i2c_client *i2c; |
| 98 | u8 config; |
| 99 | u8 pga[4]; |
| 100 | struct mutex lock; |
| 101 | }; |
| 102 | |
| 103 | static int mcp3422_update_config(struct mcp3422 *adc, u8 newconfig) |
| 104 | { |
| 105 | int ret; |
| 106 | |
| 107 | mutex_lock(&adc->lock); |
| 108 | |
| 109 | ret = i2c_master_send(adc->i2c, &newconfig, 1); |
| 110 | if (ret > 0) { |
| 111 | adc->config = newconfig; |
| 112 | ret = 0; |
| 113 | } |
| 114 | |
| 115 | mutex_unlock(&adc->lock); |
| 116 | |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | static int mcp3422_read(struct mcp3422 *adc, int *value, u8 *config) |
| 121 | { |
| 122 | int ret = 0; |
| 123 | u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); |
| 124 | u8 buf[4] = {0, 0, 0, 0}; |
| 125 | u32 temp; |
| 126 | |
| 127 | if (sample_rate == MCP3422_SRATE_3) { |
| 128 | ret = i2c_master_recv(adc->i2c, buf, 4); |
| 129 | temp = buf[0] << 16 | buf[1] << 8 | buf[2]; |
| 130 | *config = buf[3]; |
| 131 | } else { |
| 132 | ret = i2c_master_recv(adc->i2c, buf, 3); |
| 133 | temp = buf[0] << 8 | buf[1]; |
| 134 | *config = buf[2]; |
| 135 | } |
| 136 | |
| 137 | *value = sign_extend32(temp, mcp3422_sign_extend[sample_rate]); |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | static int mcp3422_read_channel(struct mcp3422 *adc, |
| 143 | struct iio_chan_spec const *channel, int *value) |
| 144 | { |
| 145 | int ret; |
| 146 | u8 config; |
| 147 | u8 req_channel = channel->channel; |
| 148 | |
| 149 | if (req_channel != MCP3422_CHANNEL(adc->config)) { |
| 150 | config = adc->config; |
| 151 | config &= ~MCP3422_CHANNEL_MASK; |
| 152 | config |= MCP3422_CHANNEL_VALUE(req_channel); |
| 153 | config &= ~MCP3422_PGA_MASK; |
| 154 | config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); |
| 155 | ret = mcp3422_update_config(adc, config); |
| 156 | if (ret < 0) |
| 157 | return ret; |
| 158 | msleep(mcp3422_read_times[MCP3422_SAMPLE_RATE(adc->config)]); |
| 159 | } |
| 160 | |
| 161 | return mcp3422_read(adc, value, &config); |
| 162 | } |
| 163 | |
| 164 | static int mcp3422_read_raw(struct iio_dev *iio, |
| 165 | struct iio_chan_spec const *channel, int *val1, |
| 166 | int *val2, long mask) |
| 167 | { |
| 168 | struct mcp3422 *adc = iio_priv(iio); |
| 169 | int err; |
| 170 | |
| 171 | u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); |
| 172 | u8 pga = MCP3422_PGA(adc->config); |
| 173 | |
| 174 | switch (mask) { |
| 175 | case IIO_CHAN_INFO_RAW: |
| 176 | err = mcp3422_read_channel(adc, channel, val1); |
| 177 | if (err < 0) |
| 178 | return -EINVAL; |
| 179 | return IIO_VAL_INT; |
| 180 | |
| 181 | case IIO_CHAN_INFO_SCALE: |
| 182 | |
| 183 | *val1 = 0; |
| 184 | *val2 = mcp3422_scales[sample_rate][pga]; |
| 185 | return IIO_VAL_INT_PLUS_NANO; |
| 186 | |
| 187 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 188 | *val1 = mcp3422_sample_rates[MCP3422_SAMPLE_RATE(adc->config)]; |
| 189 | return IIO_VAL_INT; |
| 190 | |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | |
| 198 | static int mcp3422_write_raw(struct iio_dev *iio, |
| 199 | struct iio_chan_spec const *channel, int val1, |
| 200 | int val2, long mask) |
| 201 | { |
| 202 | struct mcp3422 *adc = iio_priv(iio); |
| 203 | u8 temp; |
| 204 | u8 config = adc->config; |
| 205 | u8 req_channel = channel->channel; |
| 206 | u8 sample_rate = MCP3422_SAMPLE_RATE(config); |
| 207 | u8 i; |
| 208 | |
| 209 | switch (mask) { |
| 210 | case IIO_CHAN_INFO_SCALE: |
| 211 | if (val1 != 0) |
| 212 | return -EINVAL; |
| 213 | |
| 214 | for (i = 0; i < ARRAY_SIZE(mcp3422_scales[0]); i++) { |
| 215 | if (val2 == mcp3422_scales[sample_rate][i]) { |
| 216 | adc->pga[req_channel] = i; |
| 217 | |
| 218 | config &= ~MCP3422_CHANNEL_MASK; |
| 219 | config |= MCP3422_CHANNEL_VALUE(req_channel); |
| 220 | config &= ~MCP3422_PGA_MASK; |
| 221 | config |= MCP3422_PGA_VALUE(adc->pga[req_channel]); |
| 222 | |
| 223 | return mcp3422_update_config(adc, config); |
| 224 | } |
| 225 | } |
| 226 | return -EINVAL; |
| 227 | |
| 228 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 229 | switch (val1) { |
| 230 | case 240: |
| 231 | temp = MCP3422_SRATE_240; |
| 232 | break; |
| 233 | case 60: |
| 234 | temp = MCP3422_SRATE_60; |
| 235 | break; |
| 236 | case 15: |
| 237 | temp = MCP3422_SRATE_15; |
| 238 | break; |
| 239 | case 3: |
| 240 | temp = MCP3422_SRATE_3; |
| 241 | break; |
| 242 | default: |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | |
| 246 | config &= ~MCP3422_CHANNEL_MASK; |
| 247 | config |= MCP3422_CHANNEL_VALUE(req_channel); |
| 248 | config &= ~MCP3422_SRATE_MASK; |
| 249 | config |= MCP3422_SAMPLE_RATE_VALUE(temp); |
| 250 | |
| 251 | return mcp3422_update_config(adc, config); |
| 252 | |
| 253 | default: |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | |
| 260 | static int mcp3422_write_raw_get_fmt(struct iio_dev *indio_dev, |
| 261 | struct iio_chan_spec const *chan, long mask) |
| 262 | { |
| 263 | switch (mask) { |
| 264 | case IIO_CHAN_INFO_SCALE: |
| 265 | return IIO_VAL_INT_PLUS_NANO; |
| 266 | case IIO_CHAN_INFO_SAMP_FREQ: |
| 267 | return IIO_VAL_INT_PLUS_MICRO; |
| 268 | default: |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | static ssize_t mcp3422_show_scales(struct device *dev, |
| 274 | struct device_attribute *attr, char *buf) |
| 275 | { |
| 276 | struct mcp3422 *adc = iio_priv(dev_to_iio_dev(dev)); |
| 277 | u8 sample_rate = MCP3422_SAMPLE_RATE(adc->config); |
| 278 | |
| 279 | return sprintf(buf, "0.%09u 0.%09u 0.%09u 0.%09u\n", |
| 280 | mcp3422_scales[sample_rate][0], |
| 281 | mcp3422_scales[sample_rate][1], |
| 282 | mcp3422_scales[sample_rate][2], |
| 283 | mcp3422_scales[sample_rate][3]); |
| 284 | } |
| 285 | |
| 286 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("240 60 15 3"); |
| 287 | static IIO_DEVICE_ATTR(in_voltage_scale_available, S_IRUGO, |
| 288 | mcp3422_show_scales, NULL, 0); |
| 289 | |
| 290 | static struct attribute *mcp3422_attributes[] = { |
| 291 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
| 292 | &iio_dev_attr_in_voltage_scale_available.dev_attr.attr, |
| 293 | NULL, |
| 294 | }; |
| 295 | |
| 296 | static const struct attribute_group mcp3422_attribute_group = { |
| 297 | .attrs = mcp3422_attributes, |
| 298 | }; |
| 299 | |
| 300 | static const struct iio_chan_spec mcp3422_channels[] = { |
| 301 | MCP3422_CHAN(0), |
| 302 | MCP3422_CHAN(1), |
| 303 | }; |
| 304 | |
| 305 | static const struct iio_chan_spec mcp3424_channels[] = { |
| 306 | MCP3422_CHAN(0), |
| 307 | MCP3422_CHAN(1), |
| 308 | MCP3422_CHAN(2), |
| 309 | MCP3422_CHAN(3), |
| 310 | }; |
| 311 | |
| 312 | static const struct iio_info mcp3422_info = { |
| 313 | .read_raw = mcp3422_read_raw, |
| 314 | .write_raw = mcp3422_write_raw, |
| 315 | .write_raw_get_fmt = mcp3422_write_raw_get_fmt, |
| 316 | .attrs = &mcp3422_attribute_group, |
| 317 | .driver_module = THIS_MODULE, |
| 318 | }; |
| 319 | |
| 320 | static int mcp3422_probe(struct i2c_client *client, |
| 321 | const struct i2c_device_id *id) |
| 322 | { |
| 323 | struct iio_dev *indio_dev; |
| 324 | struct mcp3422 *adc; |
| 325 | int err; |
| 326 | u8 config; |
| 327 | |
| 328 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adc)); |
| 332 | if (!indio_dev) |
| 333 | return -ENOMEM; |
| 334 | |
| 335 | adc = iio_priv(indio_dev); |
| 336 | adc->i2c = client; |
| 337 | |
| 338 | mutex_init(&adc->lock); |
| 339 | |
| 340 | indio_dev->dev.parent = &client->dev; |
| 341 | indio_dev->name = dev_name(&client->dev); |
| 342 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 343 | indio_dev->info = &mcp3422_info; |
| 344 | |
| 345 | switch ((unsigned int)(id->driver_data)) { |
| 346 | case 2: |
| 347 | case 3: |
| 348 | indio_dev->channels = mcp3422_channels; |
| 349 | indio_dev->num_channels = ARRAY_SIZE(mcp3422_channels); |
| 350 | break; |
| 351 | case 4: |
| 352 | indio_dev->channels = mcp3424_channels; |
| 353 | indio_dev->num_channels = ARRAY_SIZE(mcp3424_channels); |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | /* meaningful default configuration */ |
| 358 | config = (MCP3422_CONT_SAMPLING |
| 359 | | MCP3422_CHANNEL_VALUE(1) |
| 360 | | MCP3422_PGA_VALUE(MCP3422_PGA_1) |
| 361 | | MCP3422_SAMPLE_RATE_VALUE(MCP3422_SRATE_240)); |
| 362 | mcp3422_update_config(adc, config); |
| 363 | |
| 364 | err = iio_device_register(indio_dev); |
| 365 | if (err < 0) |
| 366 | return err; |
| 367 | |
| 368 | i2c_set_clientdata(client, indio_dev); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | static int mcp3422_remove(struct i2c_client *client) |
| 374 | { |
| 375 | iio_device_unregister(i2c_get_clientdata(client)); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static const struct i2c_device_id mcp3422_id[] = { |
| 380 | { "mcp3422", 2 }, |
| 381 | { "mcp3423", 3 }, |
| 382 | { "mcp3424", 4 }, |
| 383 | { } |
| 384 | }; |
| 385 | MODULE_DEVICE_TABLE(i2c, mcp3422_id); |
| 386 | |
| 387 | #ifdef CONFIG_OF |
| 388 | static const struct of_device_id mcp3422_of_match[] = { |
| 389 | { .compatible = "mcp3422" }, |
| 390 | { } |
| 391 | }; |
| 392 | MODULE_DEVICE_TABLE(of, mcp3422_of_match); |
| 393 | #endif |
| 394 | |
| 395 | static struct i2c_driver mcp3422_driver = { |
| 396 | .driver = { |
| 397 | .name = "mcp3422", |
| 398 | .owner = THIS_MODULE, |
| 399 | .of_match_table = of_match_ptr(mcp3422_of_match), |
| 400 | }, |
| 401 | .probe = mcp3422_probe, |
| 402 | .remove = mcp3422_remove, |
| 403 | .id_table = mcp3422_id, |
| 404 | }; |
| 405 | module_i2c_driver(mcp3422_driver); |
| 406 | |
| 407 | MODULE_AUTHOR("Angelo Compagnucci <angelo.compagnucci@gmail.com>"); |
| 408 | MODULE_DESCRIPTION("Microchip mcp3422/3/4 driver"); |
| 409 | MODULE_LICENSE("GPL v2"); |