ina3221.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * INA3221 Triple Current/Voltage Monitor
  3. *
  4. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  5. * Andrew F. Davis <afd@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. */
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/i2c.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/regmap.h>
  22. #define INA3221_DRIVER_NAME "ina3221"
  23. #define INA3221_CONFIG 0x00
  24. #define INA3221_SHUNT1 0x01
  25. #define INA3221_BUS1 0x02
  26. #define INA3221_SHUNT2 0x03
  27. #define INA3221_BUS2 0x04
  28. #define INA3221_SHUNT3 0x05
  29. #define INA3221_BUS3 0x06
  30. #define INA3221_CRIT1 0x07
  31. #define INA3221_WARN1 0x08
  32. #define INA3221_CRIT2 0x09
  33. #define INA3221_WARN2 0x0a
  34. #define INA3221_CRIT3 0x0b
  35. #define INA3221_WARN3 0x0c
  36. #define INA3221_MASK_ENABLE 0x0f
  37. #define INA3221_CONFIG_MODE_SHUNT BIT(1)
  38. #define INA3221_CONFIG_MODE_BUS BIT(2)
  39. #define INA3221_CONFIG_MODE_CONTINUOUS BIT(3)
  40. #define INA3221_RSHUNT_DEFAULT 10000
  41. enum ina3221_fields {
  42. /* Configuration */
  43. F_RST,
  44. /* Alert Flags */
  45. F_WF3, F_WF2, F_WF1,
  46. F_CF3, F_CF2, F_CF1,
  47. /* sentinel */
  48. F_MAX_FIELDS
  49. };
  50. static const struct reg_field ina3221_reg_fields[] = {
  51. [F_RST] = REG_FIELD(INA3221_CONFIG, 15, 15),
  52. [F_WF3] = REG_FIELD(INA3221_MASK_ENABLE, 3, 3),
  53. [F_WF2] = REG_FIELD(INA3221_MASK_ENABLE, 4, 4),
  54. [F_WF1] = REG_FIELD(INA3221_MASK_ENABLE, 5, 5),
  55. [F_CF3] = REG_FIELD(INA3221_MASK_ENABLE, 7, 7),
  56. [F_CF2] = REG_FIELD(INA3221_MASK_ENABLE, 8, 8),
  57. [F_CF1] = REG_FIELD(INA3221_MASK_ENABLE, 9, 9),
  58. };
  59. enum ina3221_channels {
  60. INA3221_CHANNEL1,
  61. INA3221_CHANNEL2,
  62. INA3221_CHANNEL3,
  63. INA3221_NUM_CHANNELS
  64. };
  65. static const unsigned int register_channel[] = {
  66. [INA3221_SHUNT1] = INA3221_CHANNEL1,
  67. [INA3221_SHUNT2] = INA3221_CHANNEL2,
  68. [INA3221_SHUNT3] = INA3221_CHANNEL3,
  69. [INA3221_CRIT1] = INA3221_CHANNEL1,
  70. [INA3221_CRIT2] = INA3221_CHANNEL2,
  71. [INA3221_CRIT3] = INA3221_CHANNEL3,
  72. [INA3221_WARN1] = INA3221_CHANNEL1,
  73. [INA3221_WARN2] = INA3221_CHANNEL2,
  74. [INA3221_WARN3] = INA3221_CHANNEL3,
  75. };
  76. /**
  77. * struct ina3221_data - device specific information
  78. * @regmap: Register map of the device
  79. * @fields: Register fields of the device
  80. * @shunt_resistors: Array of resistor values per channel
  81. */
  82. struct ina3221_data {
  83. struct regmap *regmap;
  84. struct regmap_field *fields[F_MAX_FIELDS];
  85. int shunt_resistors[INA3221_NUM_CHANNELS];
  86. };
  87. static int ina3221_read_value(struct ina3221_data *ina, unsigned int reg,
  88. int *val)
  89. {
  90. unsigned int regval;
  91. int ret;
  92. ret = regmap_read(ina->regmap, reg, &regval);
  93. if (ret)
  94. return ret;
  95. *val = sign_extend32(regval >> 3, 12);
  96. return 0;
  97. }
  98. static ssize_t ina3221_show_bus_voltage(struct device *dev,
  99. struct device_attribute *attr,
  100. char *buf)
  101. {
  102. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  103. struct ina3221_data *ina = dev_get_drvdata(dev);
  104. unsigned int reg = sd_attr->index;
  105. int val, voltage_mv, ret;
  106. ret = ina3221_read_value(ina, reg, &val);
  107. if (ret)
  108. return ret;
  109. voltage_mv = val * 8;
  110. return snprintf(buf, PAGE_SIZE, "%d\n", voltage_mv);
  111. }
  112. static ssize_t ina3221_show_shunt_voltage(struct device *dev,
  113. struct device_attribute *attr,
  114. char *buf)
  115. {
  116. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  117. struct ina3221_data *ina = dev_get_drvdata(dev);
  118. unsigned int reg = sd_attr->index;
  119. int val, voltage_uv, ret;
  120. ret = ina3221_read_value(ina, reg, &val);
  121. if (ret)
  122. return ret;
  123. voltage_uv = val * 40;
  124. return snprintf(buf, PAGE_SIZE, "%d\n", voltage_uv);
  125. }
  126. static ssize_t ina3221_show_current(struct device *dev,
  127. struct device_attribute *attr, char *buf)
  128. {
  129. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  130. struct ina3221_data *ina = dev_get_drvdata(dev);
  131. unsigned int reg = sd_attr->index;
  132. unsigned int channel = register_channel[reg];
  133. int resistance_uo = ina->shunt_resistors[channel];
  134. int val, current_ma, voltage_nv, ret;
  135. ret = ina3221_read_value(ina, reg, &val);
  136. if (ret)
  137. return ret;
  138. voltage_nv = val * 40000;
  139. current_ma = DIV_ROUND_CLOSEST(voltage_nv, resistance_uo);
  140. return snprintf(buf, PAGE_SIZE, "%d\n", current_ma);
  141. }
  142. static ssize_t ina3221_set_current(struct device *dev,
  143. struct device_attribute *attr,
  144. const char *buf, size_t count)
  145. {
  146. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  147. struct ina3221_data *ina = dev_get_drvdata(dev);
  148. unsigned int reg = sd_attr->index;
  149. unsigned int channel = register_channel[reg];
  150. int resistance_uo = ina->shunt_resistors[channel];
  151. int val, current_ma, voltage_uv, ret;
  152. ret = kstrtoint(buf, 0, &current_ma);
  153. if (ret)
  154. return ret;
  155. /* clamp current */
  156. current_ma = clamp_val(current_ma,
  157. INT_MIN / resistance_uo,
  158. INT_MAX / resistance_uo);
  159. voltage_uv = DIV_ROUND_CLOSEST(current_ma * resistance_uo, 1000);
  160. /* clamp voltage */
  161. voltage_uv = clamp_val(voltage_uv, -163800, 163800);
  162. /* 1 / 40uV(scale) << 3(register shift) = 5 */
  163. val = DIV_ROUND_CLOSEST(voltage_uv, 5) & 0xfff8;
  164. ret = regmap_write(ina->regmap, reg, val);
  165. if (ret)
  166. return ret;
  167. return count;
  168. }
  169. static ssize_t ina3221_show_shunt(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  173. struct ina3221_data *ina = dev_get_drvdata(dev);
  174. unsigned int channel = sd_attr->index;
  175. unsigned int resistance_uo;
  176. resistance_uo = ina->shunt_resistors[channel];
  177. return snprintf(buf, PAGE_SIZE, "%d\n", resistance_uo);
  178. }
  179. static ssize_t ina3221_set_shunt(struct device *dev,
  180. struct device_attribute *attr,
  181. const char *buf, size_t count)
  182. {
  183. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  184. struct ina3221_data *ina = dev_get_drvdata(dev);
  185. unsigned int channel = sd_attr->index;
  186. int val;
  187. int ret;
  188. ret = kstrtoint(buf, 0, &val);
  189. if (ret)
  190. return ret;
  191. val = clamp_val(val, 1, INT_MAX);
  192. ina->shunt_resistors[channel] = val;
  193. return count;
  194. }
  195. static ssize_t ina3221_show_alert(struct device *dev,
  196. struct device_attribute *attr, char *buf)
  197. {
  198. struct sensor_device_attribute *sd_attr = to_sensor_dev_attr(attr);
  199. struct ina3221_data *ina = dev_get_drvdata(dev);
  200. unsigned int field = sd_attr->index;
  201. unsigned int regval;
  202. int ret;
  203. ret = regmap_field_read(ina->fields[field], &regval);
  204. if (ret)
  205. return ret;
  206. return snprintf(buf, PAGE_SIZE, "%d\n", regval);
  207. }
  208. /* bus voltage */
  209. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO,
  210. ina3221_show_bus_voltage, NULL, INA3221_BUS1);
  211. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO,
  212. ina3221_show_bus_voltage, NULL, INA3221_BUS2);
  213. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO,
  214. ina3221_show_bus_voltage, NULL, INA3221_BUS3);
  215. /* calculated current */
  216. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO,
  217. ina3221_show_current, NULL, INA3221_SHUNT1);
  218. static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO,
  219. ina3221_show_current, NULL, INA3221_SHUNT2);
  220. static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO,
  221. ina3221_show_current, NULL, INA3221_SHUNT3);
  222. /* shunt resistance */
  223. static SENSOR_DEVICE_ATTR(shunt1_resistor, S_IRUGO | S_IWUSR,
  224. ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL1);
  225. static SENSOR_DEVICE_ATTR(shunt2_resistor, S_IRUGO | S_IWUSR,
  226. ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL2);
  227. static SENSOR_DEVICE_ATTR(shunt3_resistor, S_IRUGO | S_IWUSR,
  228. ina3221_show_shunt, ina3221_set_shunt, INA3221_CHANNEL3);
  229. /* critical current */
  230. static SENSOR_DEVICE_ATTR(curr1_crit, S_IRUGO | S_IWUSR,
  231. ina3221_show_current, ina3221_set_current, INA3221_CRIT1);
  232. static SENSOR_DEVICE_ATTR(curr2_crit, S_IRUGO | S_IWUSR,
  233. ina3221_show_current, ina3221_set_current, INA3221_CRIT2);
  234. static SENSOR_DEVICE_ATTR(curr3_crit, S_IRUGO | S_IWUSR,
  235. ina3221_show_current, ina3221_set_current, INA3221_CRIT3);
  236. /* critical current alert */
  237. static SENSOR_DEVICE_ATTR(curr1_crit_alarm, S_IRUGO,
  238. ina3221_show_alert, NULL, F_CF1);
  239. static SENSOR_DEVICE_ATTR(curr2_crit_alarm, S_IRUGO,
  240. ina3221_show_alert, NULL, F_CF2);
  241. static SENSOR_DEVICE_ATTR(curr3_crit_alarm, S_IRUGO,
  242. ina3221_show_alert, NULL, F_CF3);
  243. /* warning current */
  244. static SENSOR_DEVICE_ATTR(curr1_max, S_IRUGO | S_IWUSR,
  245. ina3221_show_current, ina3221_set_current, INA3221_WARN1);
  246. static SENSOR_DEVICE_ATTR(curr2_max, S_IRUGO | S_IWUSR,
  247. ina3221_show_current, ina3221_set_current, INA3221_WARN2);
  248. static SENSOR_DEVICE_ATTR(curr3_max, S_IRUGO | S_IWUSR,
  249. ina3221_show_current, ina3221_set_current, INA3221_WARN3);
  250. /* warning current alert */
  251. static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO,
  252. ina3221_show_alert, NULL, F_WF1);
  253. static SENSOR_DEVICE_ATTR(curr2_max_alarm, S_IRUGO,
  254. ina3221_show_alert, NULL, F_WF2);
  255. static SENSOR_DEVICE_ATTR(curr3_max_alarm, S_IRUGO,
  256. ina3221_show_alert, NULL, F_WF3);
  257. /* shunt voltage */
  258. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO,
  259. ina3221_show_shunt_voltage, NULL, INA3221_SHUNT1);
  260. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO,
  261. ina3221_show_shunt_voltage, NULL, INA3221_SHUNT2);
  262. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO,
  263. ina3221_show_shunt_voltage, NULL, INA3221_SHUNT3);
  264. static struct attribute *ina3221_attrs[] = {
  265. /* channel 1 */
  266. &sensor_dev_attr_in1_input.dev_attr.attr,
  267. &sensor_dev_attr_curr1_input.dev_attr.attr,
  268. &sensor_dev_attr_shunt1_resistor.dev_attr.attr,
  269. &sensor_dev_attr_curr1_crit.dev_attr.attr,
  270. &sensor_dev_attr_curr1_crit_alarm.dev_attr.attr,
  271. &sensor_dev_attr_curr1_max.dev_attr.attr,
  272. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  273. &sensor_dev_attr_in4_input.dev_attr.attr,
  274. /* channel 2 */
  275. &sensor_dev_attr_in2_input.dev_attr.attr,
  276. &sensor_dev_attr_curr2_input.dev_attr.attr,
  277. &sensor_dev_attr_shunt2_resistor.dev_attr.attr,
  278. &sensor_dev_attr_curr2_crit.dev_attr.attr,
  279. &sensor_dev_attr_curr2_crit_alarm.dev_attr.attr,
  280. &sensor_dev_attr_curr2_max.dev_attr.attr,
  281. &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
  282. &sensor_dev_attr_in5_input.dev_attr.attr,
  283. /* channel 3 */
  284. &sensor_dev_attr_in3_input.dev_attr.attr,
  285. &sensor_dev_attr_curr3_input.dev_attr.attr,
  286. &sensor_dev_attr_shunt3_resistor.dev_attr.attr,
  287. &sensor_dev_attr_curr3_crit.dev_attr.attr,
  288. &sensor_dev_attr_curr3_crit_alarm.dev_attr.attr,
  289. &sensor_dev_attr_curr3_max.dev_attr.attr,
  290. &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
  291. &sensor_dev_attr_in6_input.dev_attr.attr,
  292. NULL,
  293. };
  294. ATTRIBUTE_GROUPS(ina3221);
  295. static const struct regmap_range ina3221_yes_ranges[] = {
  296. regmap_reg_range(INA3221_SHUNT1, INA3221_BUS3),
  297. regmap_reg_range(INA3221_MASK_ENABLE, INA3221_MASK_ENABLE),
  298. };
  299. static const struct regmap_access_table ina3221_volatile_table = {
  300. .yes_ranges = ina3221_yes_ranges,
  301. .n_yes_ranges = ARRAY_SIZE(ina3221_yes_ranges),
  302. };
  303. static const struct regmap_config ina3221_regmap_config = {
  304. .reg_bits = 8,
  305. .val_bits = 16,
  306. .cache_type = REGCACHE_RBTREE,
  307. .volatile_table = &ina3221_volatile_table,
  308. };
  309. static int ina3221_probe(struct i2c_client *client,
  310. const struct i2c_device_id *id)
  311. {
  312. struct device *dev = &client->dev;
  313. struct ina3221_data *ina;
  314. struct device *hwmon_dev;
  315. int i, ret;
  316. ina = devm_kzalloc(dev, sizeof(*ina), GFP_KERNEL);
  317. if (!ina)
  318. return -ENOMEM;
  319. ina->regmap = devm_regmap_init_i2c(client, &ina3221_regmap_config);
  320. if (IS_ERR(ina->regmap)) {
  321. dev_err(dev, "Unable to allocate register map\n");
  322. return PTR_ERR(ina->regmap);
  323. }
  324. for (i = 0; i < F_MAX_FIELDS; i++) {
  325. ina->fields[i] = devm_regmap_field_alloc(dev,
  326. ina->regmap,
  327. ina3221_reg_fields[i]);
  328. if (IS_ERR(ina->fields[i])) {
  329. dev_err(dev, "Unable to allocate regmap fields\n");
  330. return PTR_ERR(ina->fields[i]);
  331. }
  332. }
  333. for (i = 0; i < INA3221_NUM_CHANNELS; i++)
  334. ina->shunt_resistors[i] = INA3221_RSHUNT_DEFAULT;
  335. ret = regmap_field_write(ina->fields[F_RST], true);
  336. if (ret) {
  337. dev_err(dev, "Unable to reset device\n");
  338. return ret;
  339. }
  340. hwmon_dev = devm_hwmon_device_register_with_groups(dev,
  341. client->name,
  342. ina, ina3221_groups);
  343. if (IS_ERR(hwmon_dev)) {
  344. dev_err(dev, "Unable to register hwmon device\n");
  345. return PTR_ERR(hwmon_dev);
  346. }
  347. return 0;
  348. }
  349. static const struct of_device_id ina3221_of_match_table[] = {
  350. { .compatible = "ti,ina3221", },
  351. { /* sentinel */ }
  352. };
  353. MODULE_DEVICE_TABLE(of, ina3221_of_match_table);
  354. static const struct i2c_device_id ina3221_ids[] = {
  355. { "ina3221", 0 },
  356. { /* sentinel */ }
  357. };
  358. MODULE_DEVICE_TABLE(i2c, ina3221_ids);
  359. static struct i2c_driver ina3221_i2c_driver = {
  360. .probe = ina3221_probe,
  361. .driver = {
  362. .name = INA3221_DRIVER_NAME,
  363. .of_match_table = ina3221_of_match_table,
  364. },
  365. .id_table = ina3221_ids,
  366. };
  367. module_i2c_driver(ina3221_i2c_driver);
  368. MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
  369. MODULE_DESCRIPTION("Texas Instruments INA3221 HWMon Driver");
  370. MODULE_LICENSE("GPL v2");