max9611.c 15 KB

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