ina209.c 17 KB

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