ina209.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * Driver for the Texas Instruments / Burr Brown INA209
  3. * Bidirectional Current/Power Monitor
  4. *
  5. * Copyright (C) 2012 Guenter Roeck <linux@roeck-us.net>
  6. *
  7. * Derived from Ira W. Snyder's original driver submission
  8. * Copyright (C) 2008 Paul Hays <Paul.Hays@cattail.ca>
  9. * Copyright (C) 2008-2009 Ira W. Snyder <iws@ovro.caltech.edu>
  10. *
  11. * Aligned with ina2xx driver
  12. * Copyright (C) 2012 Lothar Felten <l-felten@ti.com>
  13. * Thanks to Jan Volkering
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; version 2 of the License.
  18. *
  19. * Datasheet:
  20. * http://www.ti.com/lit/gpn/ina209
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <linux/bug.h>
  28. #include <linux/i2c.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/hwmon-sysfs.h>
  31. #include <linux/platform_data/ina2xx.h>
  32. /* register definitions */
  33. #define INA209_CONFIGURATION 0x00
  34. #define INA209_STATUS 0x01
  35. #define INA209_STATUS_MASK 0x02
  36. #define INA209_SHUNT_VOLTAGE 0x03
  37. #define INA209_BUS_VOLTAGE 0x04
  38. #define INA209_POWER 0x05
  39. #define INA209_CURRENT 0x06
  40. #define INA209_SHUNT_VOLTAGE_POS_PEAK 0x07
  41. #define INA209_SHUNT_VOLTAGE_NEG_PEAK 0x08
  42. #define INA209_BUS_VOLTAGE_MAX_PEAK 0x09
  43. #define INA209_BUS_VOLTAGE_MIN_PEAK 0x0a
  44. #define INA209_POWER_PEAK 0x0b
  45. #define INA209_SHUNT_VOLTAGE_POS_WARN 0x0c
  46. #define INA209_SHUNT_VOLTAGE_NEG_WARN 0x0d
  47. #define INA209_POWER_WARN 0x0e
  48. #define INA209_BUS_VOLTAGE_OVER_WARN 0x0f
  49. #define INA209_BUS_VOLTAGE_UNDER_WARN 0x10
  50. #define INA209_POWER_OVER_LIMIT 0x11
  51. #define INA209_BUS_VOLTAGE_OVER_LIMIT 0x12
  52. #define INA209_BUS_VOLTAGE_UNDER_LIMIT 0x13
  53. #define INA209_CRITICAL_DAC_POS 0x14
  54. #define INA209_CRITICAL_DAC_NEG 0x15
  55. #define INA209_CALIBRATION 0x16
  56. #define INA209_REGISTERS 0x17
  57. #define INA209_CONFIG_DEFAULT 0x3c47 /* PGA=8, full range */
  58. #define INA209_SHUNT_DEFAULT 10000 /* uOhm */
  59. struct ina209_data {
  60. struct i2c_client *client;
  61. struct mutex update_lock;
  62. bool valid;
  63. unsigned long last_updated; /* in jiffies */
  64. u16 regs[INA209_REGISTERS]; /* All chip registers */
  65. u16 config_orig; /* Original configuration */
  66. u16 calibration_orig; /* Original calibration */
  67. u16 update_interval;
  68. };
  69. static struct ina209_data *ina209_update_device(struct device *dev)
  70. {
  71. struct ina209_data *data = dev_get_drvdata(dev);
  72. struct i2c_client *client = data->client;
  73. struct ina209_data *ret = data;
  74. s32 val;
  75. int i;
  76. mutex_lock(&data->update_lock);
  77. if (!data->valid ||
  78. time_after(jiffies, data->last_updated + data->update_interval)) {
  79. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  80. val = i2c_smbus_read_word_swapped(client, i);
  81. if (val < 0) {
  82. ret = ERR_PTR(val);
  83. goto abort;
  84. }
  85. data->regs[i] = val;
  86. }
  87. data->last_updated = jiffies;
  88. data->valid = true;
  89. }
  90. abort:
  91. mutex_unlock(&data->update_lock);
  92. return ret;
  93. }
  94. /*
  95. * Read a value from a device register and convert it to the
  96. * appropriate sysfs units
  97. */
  98. static long ina209_from_reg(const u8 reg, const u16 val)
  99. {
  100. switch (reg) {
  101. case INA209_SHUNT_VOLTAGE:
  102. case INA209_SHUNT_VOLTAGE_POS_PEAK:
  103. case INA209_SHUNT_VOLTAGE_NEG_PEAK:
  104. case INA209_SHUNT_VOLTAGE_POS_WARN:
  105. case INA209_SHUNT_VOLTAGE_NEG_WARN:
  106. /* LSB=10 uV. Convert to mV. */
  107. return DIV_ROUND_CLOSEST(val, 100);
  108. case INA209_BUS_VOLTAGE:
  109. case INA209_BUS_VOLTAGE_MAX_PEAK:
  110. case INA209_BUS_VOLTAGE_MIN_PEAK:
  111. case INA209_BUS_VOLTAGE_OVER_WARN:
  112. case INA209_BUS_VOLTAGE_UNDER_WARN:
  113. case INA209_BUS_VOLTAGE_OVER_LIMIT:
  114. case INA209_BUS_VOLTAGE_UNDER_LIMIT:
  115. /* LSB=4 mV, last 3 bits unused */
  116. return (val >> 3) * 4;
  117. case INA209_CRITICAL_DAC_POS:
  118. /* LSB=1 mV, in the upper 8 bits */
  119. return val >> 8;
  120. case INA209_CRITICAL_DAC_NEG:
  121. /* LSB=1 mV, in the upper 8 bits */
  122. return -1 * (val >> 8);
  123. case INA209_POWER:
  124. case INA209_POWER_PEAK:
  125. case INA209_POWER_WARN:
  126. case INA209_POWER_OVER_LIMIT:
  127. /* LSB=20 mW. Convert to uW */
  128. return val * 20 * 1000L;
  129. case INA209_CURRENT:
  130. /* LSB=1 mA (selected). Is in mA */
  131. return val;
  132. }
  133. /* programmer goofed */
  134. WARN_ON_ONCE(1);
  135. return 0;
  136. }
  137. /*
  138. * Take a value and convert it to register format, clamping the value
  139. * to the appropriate range.
  140. */
  141. static int ina209_to_reg(u8 reg, u16 old, long val)
  142. {
  143. switch (reg) {
  144. case INA209_SHUNT_VOLTAGE_POS_WARN:
  145. case INA209_SHUNT_VOLTAGE_NEG_WARN:
  146. /* Limit to +- 320 mV, 10 uV LSB */
  147. return clamp_val(val, -320, 320) * 100;
  148. case INA209_BUS_VOLTAGE_OVER_WARN:
  149. case INA209_BUS_VOLTAGE_UNDER_WARN:
  150. case INA209_BUS_VOLTAGE_OVER_LIMIT:
  151. case INA209_BUS_VOLTAGE_UNDER_LIMIT:
  152. /*
  153. * Limit to 0-32000 mV, 4 mV LSB
  154. *
  155. * The last three bits aren't part of the value, but we'll
  156. * preserve them in their original state.
  157. */
  158. return (DIV_ROUND_CLOSEST(clamp_val(val, 0, 32000), 4) << 3)
  159. | (old & 0x7);
  160. case INA209_CRITICAL_DAC_NEG:
  161. /*
  162. * Limit to -255-0 mV, 1 mV LSB
  163. * Convert the value to a positive value for the register
  164. *
  165. * The value lives in the top 8 bits only, be careful
  166. * and keep original value of other bits.
  167. */
  168. return (clamp_val(-val, 0, 255) << 8) | (old & 0xff);
  169. case INA209_CRITICAL_DAC_POS:
  170. /*
  171. * Limit to 0-255 mV, 1 mV LSB
  172. *
  173. * The value lives in the top 8 bits only, be careful
  174. * and keep original value of other bits.
  175. */
  176. return (clamp_val(val, 0, 255) << 8) | (old & 0xff);
  177. case INA209_POWER_WARN:
  178. case INA209_POWER_OVER_LIMIT:
  179. /* 20 mW LSB */
  180. return DIV_ROUND_CLOSEST(val, 20 * 1000);
  181. }
  182. /* Other registers are read-only, return access error */
  183. return -EACCES;
  184. }
  185. static int ina209_interval_from_reg(u16 reg)
  186. {
  187. return 68 >> (15 - ((reg >> 3) & 0x0f));
  188. }
  189. static u16 ina209_reg_from_interval(u16 config, long interval)
  190. {
  191. int i, adc;
  192. if (interval <= 0) {
  193. adc = 8;
  194. } else {
  195. adc = 15;
  196. for (i = 34 + 34 / 2; i; i >>= 1) {
  197. if (i < interval)
  198. break;
  199. adc--;
  200. }
  201. }
  202. return (config & 0xf807) | (adc << 3) | (adc << 7);
  203. }
  204. static ssize_t ina209_set_interval(struct device *dev,
  205. struct device_attribute *da,
  206. const char *buf, size_t count)
  207. {
  208. struct ina209_data *data = ina209_update_device(dev);
  209. long val;
  210. u16 regval;
  211. int ret;
  212. if (IS_ERR(data))
  213. return PTR_ERR(data);
  214. ret = kstrtol(buf, 10, &val);
  215. if (ret < 0)
  216. return ret;
  217. mutex_lock(&data->update_lock);
  218. regval = ina209_reg_from_interval(data->regs[INA209_CONFIGURATION],
  219. val);
  220. i2c_smbus_write_word_swapped(data->client, INA209_CONFIGURATION,
  221. regval);
  222. data->regs[INA209_CONFIGURATION] = regval;
  223. data->update_interval = ina209_interval_from_reg(regval);
  224. mutex_unlock(&data->update_lock);
  225. return count;
  226. }
  227. static ssize_t ina209_show_interval(struct device *dev,
  228. struct device_attribute *da, char *buf)
  229. {
  230. struct ina209_data *data = dev_get_drvdata(dev);
  231. return snprintf(buf, PAGE_SIZE, "%d\n", data->update_interval);
  232. }
  233. /*
  234. * History is reset by writing 1 into bit 0 of the respective peak register.
  235. * Since more than one peak register may be affected by the scope of a
  236. * reset_history attribute write, use a bit mask in attr->index to identify
  237. * which registers are affected.
  238. */
  239. static u16 ina209_reset_history_regs[] = {
  240. INA209_SHUNT_VOLTAGE_POS_PEAK,
  241. INA209_SHUNT_VOLTAGE_NEG_PEAK,
  242. INA209_BUS_VOLTAGE_MAX_PEAK,
  243. INA209_BUS_VOLTAGE_MIN_PEAK,
  244. INA209_POWER_PEAK
  245. };
  246. static ssize_t ina209_reset_history(struct device *dev,
  247. struct device_attribute *da,
  248. const char *buf,
  249. size_t count)
  250. {
  251. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  252. struct ina209_data *data = dev_get_drvdata(dev);
  253. struct i2c_client *client = data->client;
  254. u32 mask = attr->index;
  255. long val;
  256. int i, ret;
  257. ret = kstrtol(buf, 10, &val);
  258. if (ret < 0)
  259. return ret;
  260. mutex_lock(&data->update_lock);
  261. for (i = 0; i < ARRAY_SIZE(ina209_reset_history_regs); i++) {
  262. if (mask & (1 << i))
  263. i2c_smbus_write_word_swapped(client,
  264. ina209_reset_history_regs[i], 1);
  265. }
  266. data->valid = false;
  267. mutex_unlock(&data->update_lock);
  268. return count;
  269. }
  270. static ssize_t ina209_set_value(struct device *dev,
  271. struct device_attribute *da,
  272. const char *buf,
  273. size_t count)
  274. {
  275. struct ina209_data *data = ina209_update_device(dev);
  276. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  277. int reg = attr->index;
  278. long val;
  279. int ret;
  280. if (IS_ERR(data))
  281. return PTR_ERR(data);
  282. ret = kstrtol(buf, 10, &val);
  283. if (ret < 0)
  284. return ret;
  285. mutex_lock(&data->update_lock);
  286. ret = ina209_to_reg(reg, data->regs[reg], val);
  287. if (ret < 0) {
  288. count = ret;
  289. goto abort;
  290. }
  291. i2c_smbus_write_word_swapped(data->client, reg, ret);
  292. data->regs[reg] = ret;
  293. abort:
  294. mutex_unlock(&data->update_lock);
  295. return count;
  296. }
  297. static ssize_t ina209_show_value(struct device *dev,
  298. struct device_attribute *da,
  299. char *buf)
  300. {
  301. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  302. struct ina209_data *data = ina209_update_device(dev);
  303. long val;
  304. if (IS_ERR(data))
  305. return PTR_ERR(data);
  306. val = ina209_from_reg(attr->index, data->regs[attr->index]);
  307. return snprintf(buf, PAGE_SIZE, "%ld\n", val);
  308. }
  309. static ssize_t ina209_show_alarm(struct device *dev,
  310. struct device_attribute *da,
  311. char *buf)
  312. {
  313. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  314. struct ina209_data *data = ina209_update_device(dev);
  315. const unsigned int mask = attr->index;
  316. u16 status;
  317. if (IS_ERR(data))
  318. return PTR_ERR(data);
  319. status = data->regs[INA209_STATUS];
  320. /*
  321. * All alarms are in the INA209_STATUS register. To avoid a long
  322. * switch statement, the mask is passed in attr->index
  323. */
  324. return snprintf(buf, PAGE_SIZE, "%u\n", !!(status & mask));
  325. }
  326. /* Shunt voltage, history, limits, alarms */
  327. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, ina209_show_value, NULL,
  328. INA209_SHUNT_VOLTAGE);
  329. static SENSOR_DEVICE_ATTR(in0_input_highest, S_IRUGO, ina209_show_value, NULL,
  330. INA209_SHUNT_VOLTAGE_POS_PEAK);
  331. static SENSOR_DEVICE_ATTR(in0_input_lowest, S_IRUGO, ina209_show_value, NULL,
  332. INA209_SHUNT_VOLTAGE_NEG_PEAK);
  333. static SENSOR_DEVICE_ATTR(in0_reset_history, S_IWUSR, NULL,
  334. ina209_reset_history, (1 << 0) | (1 << 1));
  335. static SENSOR_DEVICE_ATTR(in0_max, S_IRUGO | S_IWUSR, ina209_show_value,
  336. ina209_set_value, INA209_SHUNT_VOLTAGE_POS_WARN);
  337. static SENSOR_DEVICE_ATTR(in0_min, S_IRUGO | S_IWUSR, ina209_show_value,
  338. ina209_set_value, INA209_SHUNT_VOLTAGE_NEG_WARN);
  339. static SENSOR_DEVICE_ATTR(in0_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
  340. ina209_set_value, INA209_CRITICAL_DAC_POS);
  341. static SENSOR_DEVICE_ATTR(in0_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
  342. ina209_set_value, INA209_CRITICAL_DAC_NEG);
  343. static SENSOR_DEVICE_ATTR(in0_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
  344. 1 << 11);
  345. static SENSOR_DEVICE_ATTR(in0_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
  346. 1 << 12);
  347. static SENSOR_DEVICE_ATTR(in0_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
  348. 1 << 6);
  349. static SENSOR_DEVICE_ATTR(in0_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
  350. 1 << 7);
  351. /* Bus voltage, history, limits, alarms */
  352. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ina209_show_value, NULL,
  353. INA209_BUS_VOLTAGE);
  354. static SENSOR_DEVICE_ATTR(in1_input_highest, S_IRUGO, ina209_show_value, NULL,
  355. INA209_BUS_VOLTAGE_MAX_PEAK);
  356. static SENSOR_DEVICE_ATTR(in1_input_lowest, S_IRUGO, ina209_show_value, NULL,
  357. INA209_BUS_VOLTAGE_MIN_PEAK);
  358. static SENSOR_DEVICE_ATTR(in1_reset_history, S_IWUSR, NULL,
  359. ina209_reset_history, (1 << 2) | (1 << 3));
  360. static SENSOR_DEVICE_ATTR(in1_max, S_IRUGO | S_IWUSR, ina209_show_value,
  361. ina209_set_value, INA209_BUS_VOLTAGE_OVER_WARN);
  362. static SENSOR_DEVICE_ATTR(in1_min, S_IRUGO | S_IWUSR, ina209_show_value,
  363. ina209_set_value, INA209_BUS_VOLTAGE_UNDER_WARN);
  364. static SENSOR_DEVICE_ATTR(in1_crit_max, S_IRUGO | S_IWUSR, ina209_show_value,
  365. ina209_set_value, INA209_BUS_VOLTAGE_OVER_LIMIT);
  366. static SENSOR_DEVICE_ATTR(in1_crit_min, S_IRUGO | S_IWUSR, ina209_show_value,
  367. ina209_set_value, INA209_BUS_VOLTAGE_UNDER_LIMIT);
  368. static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
  369. 1 << 14);
  370. static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
  371. 1 << 15);
  372. static SENSOR_DEVICE_ATTR(in1_crit_min_alarm, S_IRUGO, ina209_show_alarm, NULL,
  373. 1 << 9);
  374. static SENSOR_DEVICE_ATTR(in1_crit_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
  375. 1 << 10);
  376. /* Power */
  377. static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ina209_show_value, NULL,
  378. INA209_POWER);
  379. static SENSOR_DEVICE_ATTR(power1_input_highest, S_IRUGO, ina209_show_value,
  380. NULL, INA209_POWER_PEAK);
  381. static SENSOR_DEVICE_ATTR(power1_reset_history, S_IWUSR, NULL,
  382. ina209_reset_history, 1 << 4);
  383. static SENSOR_DEVICE_ATTR(power1_max, S_IRUGO | S_IWUSR, ina209_show_value,
  384. ina209_set_value, INA209_POWER_WARN);
  385. static SENSOR_DEVICE_ATTR(power1_crit, S_IRUGO | S_IWUSR, ina209_show_value,
  386. ina209_set_value, INA209_POWER_OVER_LIMIT);
  387. static SENSOR_DEVICE_ATTR(power1_max_alarm, S_IRUGO, ina209_show_alarm, NULL,
  388. 1 << 13);
  389. static SENSOR_DEVICE_ATTR(power1_crit_alarm, S_IRUGO, ina209_show_alarm, NULL,
  390. 1 << 8);
  391. /* Current */
  392. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ina209_show_value, NULL,
  393. INA209_CURRENT);
  394. static SENSOR_DEVICE_ATTR(update_interval, S_IRUGO | S_IWUSR,
  395. ina209_show_interval, ina209_set_interval, 0);
  396. /*
  397. * Finally, construct an array of pointers to members of the above objects,
  398. * as required for sysfs_create_group()
  399. */
  400. static struct attribute *ina209_attrs[] = {
  401. &sensor_dev_attr_in0_input.dev_attr.attr,
  402. &sensor_dev_attr_in0_input_highest.dev_attr.attr,
  403. &sensor_dev_attr_in0_input_lowest.dev_attr.attr,
  404. &sensor_dev_attr_in0_reset_history.dev_attr.attr,
  405. &sensor_dev_attr_in0_max.dev_attr.attr,
  406. &sensor_dev_attr_in0_min.dev_attr.attr,
  407. &sensor_dev_attr_in0_crit_max.dev_attr.attr,
  408. &sensor_dev_attr_in0_crit_min.dev_attr.attr,
  409. &sensor_dev_attr_in0_max_alarm.dev_attr.attr,
  410. &sensor_dev_attr_in0_min_alarm.dev_attr.attr,
  411. &sensor_dev_attr_in0_crit_max_alarm.dev_attr.attr,
  412. &sensor_dev_attr_in0_crit_min_alarm.dev_attr.attr,
  413. &sensor_dev_attr_in1_input.dev_attr.attr,
  414. &sensor_dev_attr_in1_input_highest.dev_attr.attr,
  415. &sensor_dev_attr_in1_input_lowest.dev_attr.attr,
  416. &sensor_dev_attr_in1_reset_history.dev_attr.attr,
  417. &sensor_dev_attr_in1_max.dev_attr.attr,
  418. &sensor_dev_attr_in1_min.dev_attr.attr,
  419. &sensor_dev_attr_in1_crit_max.dev_attr.attr,
  420. &sensor_dev_attr_in1_crit_min.dev_attr.attr,
  421. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  422. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  423. &sensor_dev_attr_in1_crit_max_alarm.dev_attr.attr,
  424. &sensor_dev_attr_in1_crit_min_alarm.dev_attr.attr,
  425. &sensor_dev_attr_power1_input.dev_attr.attr,
  426. &sensor_dev_attr_power1_input_highest.dev_attr.attr,
  427. &sensor_dev_attr_power1_reset_history.dev_attr.attr,
  428. &sensor_dev_attr_power1_max.dev_attr.attr,
  429. &sensor_dev_attr_power1_crit.dev_attr.attr,
  430. &sensor_dev_attr_power1_max_alarm.dev_attr.attr,
  431. &sensor_dev_attr_power1_crit_alarm.dev_attr.attr,
  432. &sensor_dev_attr_curr1_input.dev_attr.attr,
  433. &sensor_dev_attr_update_interval.dev_attr.attr,
  434. NULL,
  435. };
  436. ATTRIBUTE_GROUPS(ina209);
  437. static void ina209_restore_conf(struct i2c_client *client,
  438. struct ina209_data *data)
  439. {
  440. /* Restore initial configuration */
  441. i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
  442. data->config_orig);
  443. i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
  444. data->calibration_orig);
  445. }
  446. static int ina209_init_client(struct i2c_client *client,
  447. struct ina209_data *data)
  448. {
  449. struct ina2xx_platform_data *pdata = dev_get_platdata(&client->dev);
  450. u32 shunt;
  451. int reg;
  452. reg = i2c_smbus_read_word_swapped(client, INA209_CALIBRATION);
  453. if (reg < 0)
  454. return reg;
  455. data->calibration_orig = reg;
  456. reg = i2c_smbus_read_word_swapped(client, INA209_CONFIGURATION);
  457. if (reg < 0)
  458. return reg;
  459. data->config_orig = reg;
  460. if (pdata) {
  461. if (pdata->shunt_uohms <= 0)
  462. return -EINVAL;
  463. shunt = pdata->shunt_uohms;
  464. } else if (!of_property_read_u32(client->dev.of_node, "shunt-resistor",
  465. &shunt)) {
  466. if (shunt == 0)
  467. return -EINVAL;
  468. } else {
  469. shunt = data->calibration_orig ?
  470. 40960000 / data->calibration_orig : INA209_SHUNT_DEFAULT;
  471. }
  472. i2c_smbus_write_word_swapped(client, INA209_CONFIGURATION,
  473. INA209_CONFIG_DEFAULT);
  474. data->update_interval = ina209_interval_from_reg(INA209_CONFIG_DEFAULT);
  475. /*
  476. * Calibrate current LSB to 1mA. Shunt is in uOhms.
  477. * See equation 13 in datasheet.
  478. */
  479. i2c_smbus_write_word_swapped(client, INA209_CALIBRATION,
  480. clamp_val(40960000 / shunt, 1, 65535));
  481. /* Clear status register */
  482. i2c_smbus_read_word_swapped(client, INA209_STATUS);
  483. return 0;
  484. }
  485. static int ina209_probe(struct i2c_client *client,
  486. const struct i2c_device_id *id)
  487. {
  488. struct i2c_adapter *adapter = client->adapter;
  489. struct ina209_data *data;
  490. struct device *hwmon_dev;
  491. int ret;
  492. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA))
  493. return -ENODEV;
  494. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  495. if (!data)
  496. return -ENOMEM;
  497. i2c_set_clientdata(client, data);
  498. data->client = client;
  499. mutex_init(&data->update_lock);
  500. ret = ina209_init_client(client, data);
  501. if (ret)
  502. return ret;
  503. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  504. client->name,
  505. data, ina209_groups);
  506. if (IS_ERR(hwmon_dev)) {
  507. ret = PTR_ERR(hwmon_dev);
  508. goto out_restore_conf;
  509. }
  510. return 0;
  511. out_restore_conf:
  512. ina209_restore_conf(client, data);
  513. return ret;
  514. }
  515. static int ina209_remove(struct i2c_client *client)
  516. {
  517. struct ina209_data *data = i2c_get_clientdata(client);
  518. ina209_restore_conf(client, data);
  519. return 0;
  520. }
  521. static const struct i2c_device_id ina209_id[] = {
  522. { "ina209", 0 },
  523. { }
  524. };
  525. MODULE_DEVICE_TABLE(i2c, ina209_id);
  526. /* This is the driver that will be inserted */
  527. static struct i2c_driver ina209_driver = {
  528. .class = I2C_CLASS_HWMON,
  529. .driver = {
  530. .name = "ina209",
  531. },
  532. .probe = ina209_probe,
  533. .remove = ina209_remove,
  534. .id_table = ina209_id,
  535. };
  536. module_i2c_driver(ina209_driver);
  537. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>, Paul Hays <Paul.Hays@cattail.ca>, Guenter Roeck <linux@roeck-us.net>");
  538. MODULE_DESCRIPTION("INA209 driver");
  539. MODULE_LICENSE("GPL");