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