max9611.c 15 KB

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