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