| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* iio/adc/max9611.c |
| 4 |
* |
| 5 |
* Maxim max9611/max9612 high side current sense amplifier with |
| 6 |
* 12-bit ADC interface. |
| 7 |
* |
| 8 |
* Copyright (C) 2017 Jacopo Mondi |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
* This driver supports input common-mode voltage, current-sense |
| 13 |
* amplifier with programmable gains and die temperature reading from |
| 14 |
* Maxim max9611/max9612. |
| 15 |
* |
| 16 |
* Op-amp, analog comparator, and watchdog functionalities are not |
| 17 |
* supported by this driver. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <linux/delay.h> |
| 21 |
#include <linux/i2c.h> |
| 22 |
#include <linux/iio/iio.h> |
| 23 |
#include <linux/iio/sysfs.h> |
| 24 |
#include <linux/module.h> |
| 25 |
#include <linux/mod_devicetable.h> |
| 26 |
#include <linux/property.h> |
| 27 |
|
| 28 |
/* max9611 register addresses */ |
| 29 |
#define MAX9611_REG_CSA_DATA 0x00 |
| 30 |
#define MAX9611_REG_RS_DATA 0x02 |
| 31 |
#define MAX9611_REG_TEMP_DATA 0x08 |
| 32 |
#define MAX9611_REG_CTRL1 0x0a |
| 33 |
#define MAX9611_REG_CTRL2 0x0b |
| 34 |
|
| 35 |
/* max9611 REG1 mux configuration options */ |
| 36 |
#define MAX9611_MUX_MASK GENMASK(3, 0) |
| 37 |
#define MAX9611_MUX_SENSE_1x 0x00 |
| 38 |
#define MAX9611_MUX_SENSE_4x 0x01 |
| 39 |
#define MAX9611_MUX_SENSE_8x 0x02 |
| 40 |
#define MAX9611_INPUT_VOLT 0x03 |
| 41 |
#define MAX9611_MUX_TEMP 0x06 |
| 42 |
|
| 43 |
/* max9611 voltage (both csa and input) helper macros */ |
| 44 |
#define MAX9611_VOLTAGE_SHIFT 0x04 |
| 45 |
#define MAX9611_VOLTAGE_RAW(_r) ((_r) >> MAX9611_VOLTAGE_SHIFT) |
| 46 |
|
| 47 |
/* |
| 48 |
* max9611 current sense amplifier voltage output: |
| 49 |
* LSB and offset values depends on selected gain (1x, 4x, 8x) |
| 50 |
* |
| 51 |
* GAIN LSB (nV) OFFSET (LSB steps) |
| 52 |
* 1x 107500 1 |
| 53 |
* 4x 26880 1 |
| 54 |
* 8x 13440 3 |
| 55 |
* |
| 56 |
* The complete formula to calculate current sense voltage is: |
| 57 |
* (((adc_read >> 4) - offset) / ((1 / LSB) * 10^-3) |
| 58 |
*/ |
| 59 |
#define MAX9611_CSA_1X_LSB_nV 107500 |
| 60 |
#define MAX9611_CSA_4X_LSB_nV 26880 |
| 61 |
#define MAX9611_CSA_8X_LSB_nV 13440 |
| 62 |
|
| 63 |
#define MAX9611_CSA_1X_OFFS_RAW 1 |
| 64 |
#define MAX9611_CSA_4X_OFFS_RAW 1 |
| 65 |
#define MAX9611_CSA_8X_OFFS_RAW 3 |
| 66 |
|
| 67 |
/* |
| 68 |
* max9611 common input mode (CIM): LSB is 14mV, with 14mV offset at 25 C |
| 69 |
* |
| 70 |
* The complete formula to calculate input common voltage is: |
| 71 |
* (((adc_read >> 4) * 1000) - offset) / (1 / 14 * 1000) |
| 72 |
*/ |
| 73 |
#define MAX9611_CIM_LSB_mV 14 |
| 74 |
#define MAX9611_CIM_OFFSET_RAW 1 |
| 75 |
|
| 76 |
/* |
| 77 |
* max9611 temperature reading: LSB is 480 milli degrees Celsius |
| 78 |
* |
| 79 |
* The complete formula to calculate temperature is: |
| 80 |
* ((adc_read >> 7) * 1000) / (1 / 480 * 1000) |
| 81 |
*/ |
| 82 |
#define MAX9611_TEMP_MAX_POS 0x7f80 |
| 83 |
#define MAX9611_TEMP_MAX_NEG 0xff80 |
| 84 |
#define MAX9611_TEMP_MIN_NEG 0xd980 |
| 85 |
#define MAX9611_TEMP_MASK GENMASK(15, 7) |
| 86 |
#define MAX9611_TEMP_SHIFT 0x07 |
| 87 |
#define MAX9611_TEMP_RAW(_r) ((_r) >> MAX9611_TEMP_SHIFT) |
| 88 |
#define MAX9611_TEMP_SCALE_NUM 1000000 |
| 89 |
#define MAX9611_TEMP_SCALE_DIV 2083 |
| 90 |
|
| 91 |
/* |
| 92 |
* Conversion time is 2 ms (typically) at Ta=25 degreeC |
| 93 |
* No maximum value is known, so play it safe. |
| 94 |
*/ |
| 95 |
#define MAX9611_CONV_TIME_US_RANGE 3000, 3300 |
| 96 |
|
| 97 |
struct max9611_dev { |
| 98 |
struct device *dev; |
| 99 |
struct i2c_client *i2c_client; |
| 100 |
struct mutex lock; |
| 101 |
unsigned int shunt_resistor_uohm; |
| 102 |
}; |
| 103 |
|
| 104 |
enum max9611_conf_ids { |
| 105 |
CONF_SENSE_1x, |
| 106 |
CONF_SENSE_4x, |
| 107 |
CONF_SENSE_8x, |
| 108 |
CONF_IN_VOLT, |
| 109 |
CONF_TEMP, |
| 110 |
}; |
| 111 |
|
| 112 |
/* |
| 113 |
* max9611_mux_conf - associate ADC mux configuration with register address |
| 114 |
* where data shall be read from |
| 115 |
*/ |
| 116 |
static const unsigned int max9611_mux_conf[][2] = { |
| 117 |
[CONF_SENSE_1x] = { MAX9611_MUX_SENSE_1x, MAX9611_REG_CSA_DATA }, |
| 118 |
[CONF_SENSE_4x] = { MAX9611_MUX_SENSE_4x, MAX9611_REG_CSA_DATA }, |
| 119 |
[CONF_SENSE_8x] = { MAX9611_MUX_SENSE_8x, MAX9611_REG_CSA_DATA }, |
| 120 |
[CONF_IN_VOLT] = { MAX9611_INPUT_VOLT, MAX9611_REG_RS_DATA }, |
| 121 |
[CONF_TEMP] = { MAX9611_MUX_TEMP, MAX9611_REG_TEMP_DATA }, |
| 122 |
}; |
| 123 |
|
| 124 |
enum max9611_csa_gain { |
| 125 |
CSA_GAIN_1x = CONF_SENSE_1x, |
| 126 |
CSA_GAIN_4x = CONF_SENSE_4x, |
| 127 |
CSA_GAIN_8x = CONF_SENSE_8x, |
| 128 |
}; |
| 129 |
|
| 130 |
enum max9611_csa_gain_params { |
| 131 |
CSA_GAIN_LSB_nV, |
| 132 |
CSA_GAIN_OFFS_RAW, |
| 133 |
}; |
| 134 |
|
| 135 |
/* |
| 136 |
* max9611_csa_gain_conf - associate gain multiplier with LSB and |
| 137 |
* offset values. |
| 138 |
* |
| 139 |
* Group together parameters associated with configurable gain |
| 140 |
* on current sense amplifier path to ADC interface. |
| 141 |
* Current sense read routine adjusts gain until it gets a meaningful |
| 142 |
* value; use this structure to retrieve the correct LSB and offset values. |
| 143 |
*/ |
| 144 |
static const unsigned int max9611_gain_conf[][2] = { |
| 145 |
[CSA_GAIN_1x] = { MAX9611_CSA_1X_LSB_nV, MAX9611_CSA_1X_OFFS_RAW, }, |
| 146 |
[CSA_GAIN_4x] = { MAX9611_CSA_4X_LSB_nV, MAX9611_CSA_4X_OFFS_RAW, }, |
| 147 |
[CSA_GAIN_8x] = { MAX9611_CSA_8X_LSB_nV, MAX9611_CSA_8X_OFFS_RAW, }, |
| 148 |
}; |
| 149 |
|
| 150 |
enum max9611_chan_addrs { |
| 151 |
MAX9611_CHAN_VOLTAGE_INPUT, |
| 152 |
MAX9611_CHAN_VOLTAGE_SENSE, |
| 153 |
MAX9611_CHAN_TEMPERATURE, |
| 154 |
MAX9611_CHAN_CURRENT_LOAD, |
| 155 |
MAX9611_CHAN_POWER_LOAD, |
| 156 |
}; |
| 157 |
|
| 158 |
static const struct iio_chan_spec max9611_channels[] = { |
| 159 |
{ |
| 160 |
.type = IIO_TEMP, |
| 161 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 162 |
BIT(IIO_CHAN_INFO_SCALE), |
| 163 |
.address = MAX9611_CHAN_TEMPERATURE, |
| 164 |
}, |
| 165 |
{ |
| 166 |
.type = IIO_VOLTAGE, |
| 167 |
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 168 |
.address = MAX9611_CHAN_VOLTAGE_SENSE, |
| 169 |
.indexed = 1, |
| 170 |
.channel = 0, |
| 171 |
}, |
| 172 |
{ |
| 173 |
.type = IIO_VOLTAGE, |
| 174 |
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 175 |
BIT(IIO_CHAN_INFO_SCALE) | |
| 176 |
BIT(IIO_CHAN_INFO_OFFSET), |
| 177 |
.address = MAX9611_CHAN_VOLTAGE_INPUT, |
| 178 |
.indexed = 1, |
| 179 |
.channel = 1, |
| 180 |
}, |
| 181 |
{ |
| 182 |
.type = IIO_CURRENT, |
| 183 |
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 184 |
.address = MAX9611_CHAN_CURRENT_LOAD, |
| 185 |
}, |
| 186 |
{ |
| 187 |
.type = IIO_POWER, |
| 188 |
.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), |
| 189 |
.address = MAX9611_CHAN_POWER_LOAD |
| 190 |
}, |
| 191 |
}; |
| 192 |
|
| 193 |
/** |
| 194 |
* max9611_read_single() - read a single value from ADC interface |
| 195 |
* |
| 196 |
* Data registers are 16 bit long, spread between two 8 bit registers |
| 197 |
* with consecutive addresses. |
| 198 |
* Configure ADC mux first, then read register at address "reg_addr". |
| 199 |
* The smbus_read_word routine asks for 16 bits and the ADC is kind enough |
| 200 |
* to return values from "reg_addr" and "reg_addr + 1" consecutively. |
| 201 |
* Data are transmitted with big-endian ordering: MSB arrives first. |
| 202 |
* |
| 203 |
* @max9611: max9611 device |
| 204 |
* @selector: index for mux and register configuration |
| 205 |
* @raw_val: the value returned from ADC |
| 206 |
*/ |
| 207 |
static int max9611_read_single(struct max9611_dev *max9611, |
| 208 |
enum max9611_conf_ids selector, |
| 209 |
u16 *raw_val) |
| 210 |
{ |
| 211 |
int ret; |
| 212 |
|
| 213 |
u8 mux_conf = max9611_mux_conf[selector][0] & MAX9611_MUX_MASK; |
| 214 |
u8 reg_addr = max9611_mux_conf[selector][1]; |
| 215 |
|
| 216 |
/* |
| 217 |
* Keep mutex lock held during read-write to avoid mux register |
| 218 |
* (CTRL1) re-configuration. |
| 219 |
*/ |
| 220 |
mutex_lock(&max9611->lock); |
| 221 |
ret = i2c_smbus_write_byte_data(max9611->i2c_client, |
| 222 |
MAX9611_REG_CTRL1, mux_conf); |
| 223 |
if (ret) { |
| 224 |
dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n", |
| 225 |
MAX9611_REG_CTRL1, mux_conf); |
| 226 |
mutex_unlock(&max9611->lock); |
| 227 |
return ret; |
| 228 |
} |
| 229 |
|
| 230 |
/* need a delay here to make register configuration stabilize. */ |
| 231 |
|
| 232 |
usleep_range(MAX9611_CONV_TIME_US_RANGE); |
| 233 |
|
| 234 |
ret = i2c_smbus_read_word_swapped(max9611->i2c_client, reg_addr); |
| 235 |
if (ret < 0) { |
| 236 |
dev_err(max9611->dev, "i2c read word from 0x%2x failed\n", |
| 237 |
reg_addr); |
| 238 |
mutex_unlock(&max9611->lock); |
| 239 |
return ret; |
| 240 |
} |
| 241 |
|
| 242 |
*raw_val = ret; |
| 243 |
mutex_unlock(&max9611->lock); |
| 244 |
|
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* max9611_read_csa_voltage() - read current sense amplifier output voltage |
| 250 |
* |
| 251 |
* Current sense amplifier output voltage is read through a configurable |
| 252 |
* 1x, 4x or 8x gain. |
| 253 |
* Start with plain 1x gain, and adjust gain control properly until a |
| 254 |
* meaningful value is read from ADC output. |
| 255 |
* |
| 256 |
* @max9611: max9611 device |
| 257 |
* @adc_raw: raw value read from ADC output |
| 258 |
* @csa_gain: gain configuration option selector |
| 259 |
*/ |
| 260 |
static int max9611_read_csa_voltage(struct max9611_dev *max9611, |
| 261 |
u16 *adc_raw, |
| 262 |
enum max9611_csa_gain *csa_gain) |
| 263 |
{ |
| 264 |
enum max9611_conf_ids gain_selectors[] = { |
| 265 |
CONF_SENSE_1x, |
| 266 |
CONF_SENSE_4x, |
| 267 |
CONF_SENSE_8x |
| 268 |
}; |
| 269 |
unsigned int i; |
| 270 |
int ret; |
| 271 |
|
| 272 |
for (i = 0; i < ARRAY_SIZE(gain_selectors); ++i) { |
| 273 |
ret = max9611_read_single(max9611, gain_selectors[i], adc_raw); |
| 274 |
if (ret) |
| 275 |
return ret; |
| 276 |
|
| 277 |
if (*adc_raw > 0) { |
| 278 |
*csa_gain = (enum max9611_csa_gain)gain_selectors[i]; |
| 279 |
return 0; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return -EIO; |
| 284 |
} |
| 285 |
|
| 286 |
static int max9611_read_raw(struct iio_dev *indio_dev, |
| 287 |
struct iio_chan_spec const *chan, |
| 288 |
int *val, int *val2, long mask) |
| 289 |
{ |
| 290 |
struct max9611_dev *dev = iio_priv(indio_dev); |
| 291 |
enum max9611_csa_gain gain_selector; |
| 292 |
const unsigned int *csa_gain; |
| 293 |
u16 adc_data; |
| 294 |
int ret; |
| 295 |
|
| 296 |
switch (mask) { |
| 297 |
case IIO_CHAN_INFO_RAW: |
| 298 |
|
| 299 |
switch (chan->address) { |
| 300 |
case MAX9611_CHAN_TEMPERATURE: |
| 301 |
ret = max9611_read_single(dev, CONF_TEMP, |
| 302 |
&adc_data); |
| 303 |
if (ret) |
| 304 |
return -EINVAL; |
| 305 |
|
| 306 |
*val = MAX9611_TEMP_RAW(adc_data); |
| 307 |
return IIO_VAL_INT; |
| 308 |
|
| 309 |
case MAX9611_CHAN_VOLTAGE_INPUT: |
| 310 |
ret = max9611_read_single(dev, CONF_IN_VOLT, |
| 311 |
&adc_data); |
| 312 |
if (ret) |
| 313 |
return -EINVAL; |
| 314 |
|
| 315 |
*val = MAX9611_VOLTAGE_RAW(adc_data); |
| 316 |
return IIO_VAL_INT; |
| 317 |
} |
| 318 |
|
| 319 |
break; |
| 320 |
|
| 321 |
case IIO_CHAN_INFO_OFFSET: |
| 322 |
/* MAX9611_CHAN_VOLTAGE_INPUT */ |
| 323 |
*val = MAX9611_CIM_OFFSET_RAW; |
| 324 |
|
| 325 |
return IIO_VAL_INT; |
| 326 |
|
| 327 |
case IIO_CHAN_INFO_SCALE: |
| 328 |
|
| 329 |
switch (chan->address) { |
| 330 |
case MAX9611_CHAN_TEMPERATURE: |
| 331 |
*val = MAX9611_TEMP_SCALE_NUM; |
| 332 |
*val2 = MAX9611_TEMP_SCALE_DIV; |
| 333 |
|
| 334 |
return IIO_VAL_FRACTIONAL; |
| 335 |
|
| 336 |
case MAX9611_CHAN_VOLTAGE_INPUT: |
| 337 |
*val = MAX9611_CIM_LSB_mV; |
| 338 |
|
| 339 |
return IIO_VAL_INT; |
| 340 |
} |
| 341 |
|
| 342 |
break; |
| 343 |
|
| 344 |
case IIO_CHAN_INFO_PROCESSED: |
| 345 |
|
| 346 |
switch (chan->address) { |
| 347 |
case MAX9611_CHAN_VOLTAGE_SENSE: |
| 348 |
/* |
| 349 |
* processed (mV): (raw - offset) * LSB (nV) / 10^6 |
| 350 |
* |
| 351 |
* Even if max9611 can output raw csa voltage readings, |
| 352 |
* use a produced value as scale depends on gain. |
| 353 |
*/ |
| 354 |
ret = max9611_read_csa_voltage(dev, &adc_data, |
| 355 |
&gain_selector); |
| 356 |
if (ret) |
| 357 |
return -EINVAL; |
| 358 |
|
| 359 |
csa_gain = max9611_gain_conf[gain_selector]; |
| 360 |
|
| 361 |
adc_data -= csa_gain[CSA_GAIN_OFFS_RAW]; |
| 362 |
*val = MAX9611_VOLTAGE_RAW(adc_data) * |
| 363 |
csa_gain[CSA_GAIN_LSB_nV]; |
| 364 |
*val2 = 1000000; |
| 365 |
|
| 366 |
return IIO_VAL_FRACTIONAL; |
| 367 |
|
| 368 |
case MAX9611_CHAN_CURRENT_LOAD: |
| 369 |
/* processed (mA): Vcsa (nV) / Rshunt (uOhm) */ |
| 370 |
ret = max9611_read_csa_voltage(dev, &adc_data, |
| 371 |
&gain_selector); |
| 372 |
if (ret) |
| 373 |
return -EINVAL; |
| 374 |
|
| 375 |
csa_gain = max9611_gain_conf[gain_selector]; |
| 376 |
|
| 377 |
adc_data -= csa_gain[CSA_GAIN_OFFS_RAW]; |
| 378 |
*val = MAX9611_VOLTAGE_RAW(adc_data) * |
| 379 |
csa_gain[CSA_GAIN_LSB_nV]; |
| 380 |
*val2 = dev->shunt_resistor_uohm; |
| 381 |
|
| 382 |
return IIO_VAL_FRACTIONAL; |
| 383 |
|
| 384 |
case MAX9611_CHAN_POWER_LOAD: |
| 385 |
/* |
| 386 |
* processed (mW): Vin (mV) * Vcsa (uV) / |
| 387 |
* Rshunt (uOhm) |
| 388 |
*/ |
| 389 |
ret = max9611_read_single(dev, CONF_IN_VOLT, |
| 390 |
&adc_data); |
| 391 |
if (ret) |
| 392 |
return -EINVAL; |
| 393 |
|
| 394 |
adc_data -= MAX9611_CIM_OFFSET_RAW; |
| 395 |
*val = MAX9611_VOLTAGE_RAW(adc_data) * |
| 396 |
MAX9611_CIM_LSB_mV; |
| 397 |
|
| 398 |
ret = max9611_read_csa_voltage(dev, &adc_data, |
| 399 |
&gain_selector); |
| 400 |
if (ret) |
| 401 |
return -EINVAL; |
| 402 |
|
| 403 |
csa_gain = max9611_gain_conf[gain_selector]; |
| 404 |
|
| 405 |
/* divide by 10^3 here to avoid 32bit overflow */ |
| 406 |
adc_data -= csa_gain[CSA_GAIN_OFFS_RAW]; |
| 407 |
*val *= MAX9611_VOLTAGE_RAW(adc_data) * |
| 408 |
csa_gain[CSA_GAIN_LSB_nV] / 1000; |
| 409 |
*val2 = dev->shunt_resistor_uohm; |
| 410 |
|
| 411 |
return IIO_VAL_FRACTIONAL; |
| 412 |
} |
| 413 |
|
| 414 |
break; |
| 415 |
} |
| 416 |
|
| 417 |
return -EINVAL; |
| 418 |
} |
| 419 |
|
| 420 |
static ssize_t max9611_shunt_resistor_show(struct device *dev, |
| 421 |
struct device_attribute *attr, |
| 422 |
char *buf) |
| 423 |
{ |
| 424 |
struct max9611_dev *max9611 = iio_priv(dev_to_iio_dev(dev)); |
| 425 |
unsigned int i, r; |
| 426 |
|
| 427 |
i = max9611->shunt_resistor_uohm / 1000000; |
| 428 |
r = max9611->shunt_resistor_uohm % 1000000; |
| 429 |
|
| 430 |
return sysfs_emit(buf, "%u.%06u\n", i, r); |
| 431 |
} |
| 432 |
|
| 433 |
static IIO_DEVICE_ATTR(in_power_shunt_resistor, 0444, |
| 434 |
max9611_shunt_resistor_show, NULL, 0); |
| 435 |
static IIO_DEVICE_ATTR(in_current_shunt_resistor, 0444, |
| 436 |
max9611_shunt_resistor_show, NULL, 0); |
| 437 |
|
| 438 |
static struct attribute *max9611_attributes[] = { |
| 439 |
&iio_dev_attr_in_power_shunt_resistor.dev_attr.attr, |
| 440 |
&iio_dev_attr_in_current_shunt_resistor.dev_attr.attr, |
| 441 |
NULL, |
| 442 |
}; |
| 443 |
|
| 444 |
static const struct attribute_group max9611_attribute_group = { |
| 445 |
.attrs = max9611_attributes, |
| 446 |
}; |
| 447 |
|
| 448 |
static const struct iio_info indio_info = { |
| 449 |
.read_raw = max9611_read_raw, |
| 450 |
.attrs = &max9611_attribute_group, |
| 451 |
}; |
| 452 |
|
| 453 |
static int max9611_init(struct max9611_dev *max9611) |
| 454 |
{ |
| 455 |
struct i2c_client *client = max9611->i2c_client; |
| 456 |
u16 regval; |
| 457 |
int ret; |
| 458 |
|
| 459 |
if (!i2c_check_functionality(client->adapter, |
| 460 |
I2C_FUNC_SMBUS_WRITE_BYTE | |
| 461 |
I2C_FUNC_SMBUS_READ_WORD_DATA)) { |
| 462 |
dev_err(max9611->dev, |
| 463 |
"I2c adapter does not support smbus write_byte or read_word functionalities: aborting probe.\n"); |
| 464 |
return -EINVAL; |
| 465 |
} |
| 466 |
|
| 467 |
/* Make sure die temperature is in range to test communications. */ |
| 468 |
ret = max9611_read_single(max9611, CONF_TEMP, ®val); |
| 469 |
if (ret) |
| 470 |
return ret; |
| 471 |
|
| 472 |
regval &= MAX9611_TEMP_MASK; |
| 473 |
|
| 474 |
if ((regval > MAX9611_TEMP_MAX_POS && |
| 475 |
regval < MAX9611_TEMP_MIN_NEG) || |
| 476 |
regval > MAX9611_TEMP_MAX_NEG) { |
| 477 |
dev_err(max9611->dev, |
| 478 |
"Invalid value received from ADC 0x%4x: aborting\n", |
| 479 |
regval); |
| 480 |
return -EIO; |
| 481 |
} |
| 482 |
|
| 483 |
/* Mux shall be zeroed back before applying other configurations */ |
| 484 |
ret = i2c_smbus_write_byte_data(max9611->i2c_client, |
| 485 |
MAX9611_REG_CTRL1, 0); |
| 486 |
if (ret) { |
| 487 |
dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n", |
| 488 |
MAX9611_REG_CTRL1, 0); |
| 489 |
return ret; |
| 490 |
} |
| 491 |
|
| 492 |
ret = i2c_smbus_write_byte_data(max9611->i2c_client, |
| 493 |
MAX9611_REG_CTRL2, 0); |
| 494 |
if (ret) { |
| 495 |
dev_err(max9611->dev, "i2c write byte failed: 0x%2x - 0x%2x\n", |
| 496 |
MAX9611_REG_CTRL2, 0); |
| 497 |
return ret; |
| 498 |
} |
| 499 |
usleep_range(MAX9611_CONV_TIME_US_RANGE); |
| 500 |
|
| 501 |
return 0; |
| 502 |
} |
| 503 |
|
| 504 |
static const struct of_device_id max9611_of_table[] = { |
| 505 |
{ .compatible = "maxim,max9611", .data = "max9611" }, |
| 506 |
{ .compatible = "maxim,max9612", .data = "max9612" }, |
| 507 |
{ } |
| 508 |
}; |
| 509 |
|
| 510 |
MODULE_DEVICE_TABLE(of, max9611_of_table); |
| 511 |
static int max9611_probe(struct i2c_client *client) |
| 512 |
{ |
| 513 |
const char * const shunt_res_prop = "shunt-resistor-micro-ohms"; |
| 514 |
struct max9611_dev *max9611; |
| 515 |
struct iio_dev *indio_dev; |
| 516 |
struct device *dev = &client->dev; |
| 517 |
unsigned int of_shunt; |
| 518 |
int ret; |
| 519 |
|
| 520 |
indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*max9611)); |
| 521 |
if (!indio_dev) |
| 522 |
return -ENOMEM; |
| 523 |
|
| 524 |
i2c_set_clientdata(client, indio_dev); |
| 525 |
|
| 526 |
max9611 = iio_priv(indio_dev); |
| 527 |
max9611->dev = dev; |
| 528 |
max9611->i2c_client = client; |
| 529 |
mutex_init(&max9611->lock); |
| 530 |
|
| 531 |
ret = device_property_read_u32(dev, shunt_res_prop, &of_shunt); |
| 532 |
if (ret) { |
| 533 |
dev_err(dev, "Missing %s property for %pfw node\n", |
| 534 |
shunt_res_prop, dev_fwnode(dev)); |
| 535 |
return ret; |
| 536 |
} |
| 537 |
max9611->shunt_resistor_uohm = of_shunt; |
| 538 |
|
| 539 |
ret = max9611_init(max9611); |
| 540 |
if (ret) |
| 541 |
return ret; |
| 542 |
|
| 543 |
indio_dev->name = device_get_match_data(dev); |
| 544 |
indio_dev->modes = INDIO_DIRECT_MODE; |
| 545 |
indio_dev->info = &indio_info; |
| 546 |
indio_dev->channels = max9611_channels; |
| 547 |
indio_dev->num_channels = ARRAY_SIZE(max9611_channels); |
| 548 |
|
| 549 |
return devm_iio_device_register(dev, indio_dev); |
| 550 |
} |
| 551 |
|
| 552 |
static struct i2c_driver max9611_driver = { |
| 553 |
.driver = { |
| 554 |
.name = "max9611", |
| 555 |
.of_match_table = max9611_of_table, |
| 556 |
}, |
| 557 |
.probe = max9611_probe, |
| 558 |
}; |
| 559 |
module_i2c_driver(max9611_driver); |
| 560 |
|
| 561 |
MODULE_AUTHOR("Jacopo Mondi <[email protected]>"); |
| 562 |
MODULE_DESCRIPTION("Maxim max9611/12 current sense amplifier with 12bit ADC"); |
| 563 |
MODULE_LICENSE("GPL v2"); |