ina2xx.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Texas Instruments INA219, INA226 power monitor chips
  4. *
  5. * INA219:
  6. * Zero Drift Bi-Directional Current/Power Monitor with I2C Interface
  7. * Datasheet: https://www.ti.com/product/ina219
  8. *
  9. * INA220:
  10. * Bi-Directional Current/Power Monitor with I2C Interface
  11. * Datasheet: https://www.ti.com/product/ina220
  12. *
  13. * INA226:
  14. * Bi-Directional Current/Power Monitor with I2C Interface
  15. * Datasheet: https://www.ti.com/product/ina226
  16. *
  17. * INA230:
  18. * Bi-directional Current/Power Monitor with I2C Interface
  19. * Datasheet: https://www.ti.com/product/ina230
  20. *
  21. * Copyright (C) 2012 Lothar Felten <lothar.felten@gmail.com>
  22. * Thanks to Jan Volkering
  23. */
  24. #include <linux/bitfield.h>
  25. #include <linux/bits.h>
  26. #include <linux/delay.h>
  27. #include <linux/device.h>
  28. #include <linux/err.h>
  29. #include <linux/hwmon.h>
  30. #include <linux/i2c.h>
  31. #include <linux/init.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/property.h>
  35. #include <linux/regmap.h>
  36. #include <linux/slab.h>
  37. #include <linux/sysfs.h>
  38. #include <linux/util_macros.h>
  39. /* common register definitions */
  40. #define INA2XX_CONFIG 0x00
  41. #define INA2XX_SHUNT_VOLTAGE 0x01 /* readonly */
  42. #define INA2XX_BUS_VOLTAGE 0x02 /* readonly */
  43. #define INA2XX_POWER 0x03 /* readonly */
  44. #define INA2XX_CURRENT 0x04 /* readonly */
  45. #define INA2XX_CALIBRATION 0x05
  46. /* INA226 register definitions */
  47. #define INA226_MASK_ENABLE 0x06
  48. #define INA226_ALERT_LIMIT 0x07
  49. #define INA226_DIE_ID 0xFF
  50. /* SY24655 register definitions */
  51. #define SY24655_EIN 0x0A
  52. #define SY24655_ACCUM_CONFIG 0x0D
  53. #define INA2XX_MAX_REGISTERS 0x0D
  54. /* settings - depend on use case */
  55. #define INA219_CONFIG_DEFAULT 0x399F /* PGA=8 */
  56. #define INA226_CONFIG_DEFAULT 0x4527 /* averages=16 */
  57. #define INA260_CONFIG_DEFAULT 0x6527 /* averages=16 */
  58. #define SY24655_CONFIG_DEFAULT 0x4527 /* averages=16 */
  59. /* (only for sy24655) */
  60. #define SY24655_ACCUM_CONFIG_DEFAULT 0x044C /* continuous mode, clear after read*/
  61. /* worst case is 68.10 ms (~14.6Hz, ina219) */
  62. #define INA2XX_CONVERSION_RATE 15
  63. #define INA2XX_MAX_DELAY 69 /* worst case delay in ms */
  64. #define INA2XX_RSHUNT_DEFAULT 10000
  65. #define INA260_RSHUNT 2000
  66. /* bit mask for reading the averaging setting in the configuration register */
  67. #define INA226_AVG_RD_MASK GENMASK(11, 9)
  68. #define INA226_READ_AVG(reg) FIELD_GET(INA226_AVG_RD_MASK, reg)
  69. #define INA226_ALERT_LATCH_ENABLE BIT(0)
  70. #define INA226_ALERT_POLARITY BIT(1)
  71. /* bit number of alert functions in Mask/Enable Register */
  72. #define INA226_SHUNT_OVER_VOLTAGE_MASK BIT(15)
  73. #define INA226_SHUNT_UNDER_VOLTAGE_MASK BIT(14)
  74. #define INA226_BUS_OVER_VOLTAGE_MASK BIT(13)
  75. #define INA226_BUS_UNDER_VOLTAGE_MASK BIT(12)
  76. #define INA226_POWER_OVER_LIMIT_MASK BIT(11)
  77. /* bit mask for alert config bits of Mask/Enable Register */
  78. #define INA226_ALERT_CONFIG_MASK GENMASK(15, 10)
  79. #define INA226_ALERT_FUNCTION_FLAG BIT(4)
  80. /*
  81. * Both bus voltage and shunt voltage conversion times for ina226 are set
  82. * to 0b0100 on POR, which translates to 2200 microseconds in total.
  83. */
  84. #define INA226_TOTAL_CONV_TIME_DEFAULT 2200
  85. static bool ina2xx_writeable_reg(struct device *dev, unsigned int reg)
  86. {
  87. switch (reg) {
  88. case INA2XX_CONFIG:
  89. case INA2XX_CALIBRATION:
  90. case INA226_MASK_ENABLE:
  91. case INA226_ALERT_LIMIT:
  92. case SY24655_ACCUM_CONFIG:
  93. return true;
  94. default:
  95. return false;
  96. }
  97. }
  98. static bool ina2xx_volatile_reg(struct device *dev, unsigned int reg)
  99. {
  100. switch (reg) {
  101. case INA2XX_SHUNT_VOLTAGE:
  102. case INA2XX_BUS_VOLTAGE:
  103. case INA2XX_POWER:
  104. case INA2XX_CURRENT:
  105. return true;
  106. default:
  107. return false;
  108. }
  109. }
  110. static const struct regmap_config ina2xx_regmap_config = {
  111. .reg_bits = 8,
  112. .val_bits = 16,
  113. .use_single_write = true,
  114. .use_single_read = true,
  115. .max_register = INA2XX_MAX_REGISTERS,
  116. .cache_type = REGCACHE_MAPLE,
  117. .volatile_reg = ina2xx_volatile_reg,
  118. .writeable_reg = ina2xx_writeable_reg,
  119. };
  120. enum ina2xx_ids { ina219, ina226, ina260, sy24655 };
  121. struct ina2xx_config {
  122. u16 config_default;
  123. bool has_alerts; /* chip supports alerts and limits */
  124. bool has_ishunt; /* chip has internal shunt resistor */
  125. bool has_power_average; /* chip has internal shunt resistor */
  126. int calibration_value;
  127. int shunt_div;
  128. int bus_voltage_shift;
  129. int bus_voltage_lsb; /* uV */
  130. int power_lsb_factor;
  131. };
  132. struct ina2xx_data {
  133. const struct ina2xx_config *config;
  134. enum ina2xx_ids chip;
  135. long rshunt;
  136. long current_lsb_uA;
  137. long power_lsb_uW;
  138. struct regmap *regmap;
  139. struct i2c_client *client;
  140. };
  141. static const struct ina2xx_config ina2xx_config[] = {
  142. [ina219] = {
  143. .config_default = INA219_CONFIG_DEFAULT,
  144. .calibration_value = 4096,
  145. .shunt_div = 100,
  146. .bus_voltage_shift = 3,
  147. .bus_voltage_lsb = 4000,
  148. .power_lsb_factor = 20,
  149. .has_alerts = false,
  150. .has_ishunt = false,
  151. .has_power_average = false,
  152. },
  153. [ina226] = {
  154. .config_default = INA226_CONFIG_DEFAULT,
  155. .calibration_value = 2048,
  156. .shunt_div = 400,
  157. .bus_voltage_shift = 0,
  158. .bus_voltage_lsb = 1250,
  159. .power_lsb_factor = 25,
  160. .has_alerts = true,
  161. .has_ishunt = false,
  162. .has_power_average = false,
  163. },
  164. [ina260] = {
  165. .config_default = INA260_CONFIG_DEFAULT,
  166. .shunt_div = 400,
  167. .bus_voltage_shift = 0,
  168. .bus_voltage_lsb = 1250,
  169. .power_lsb_factor = 8,
  170. .has_alerts = true,
  171. .has_ishunt = true,
  172. .has_power_average = false,
  173. },
  174. [sy24655] = {
  175. .config_default = SY24655_CONFIG_DEFAULT,
  176. .calibration_value = 4096,
  177. .shunt_div = 400,
  178. .bus_voltage_shift = 0,
  179. .bus_voltage_lsb = 1250,
  180. .power_lsb_factor = 25,
  181. .has_alerts = true,
  182. .has_ishunt = false,
  183. .has_power_average = true,
  184. },
  185. };
  186. /*
  187. * Available averaging rates for ina226. The indices correspond with
  188. * the bit values expected by the chip (according to the ina226 datasheet,
  189. * table 3 AVG bit settings, found at
  190. * https://www.ti.com/lit/ds/symlink/ina226.pdf.
  191. */
  192. static const int ina226_avg_tab[] = { 1, 4, 16, 64, 128, 256, 512, 1024 };
  193. static int ina226_reg_to_interval(u16 config)
  194. {
  195. int avg = ina226_avg_tab[INA226_READ_AVG(config)];
  196. /*
  197. * Multiply the total conversion time by the number of averages.
  198. * Return the result in milliseconds.
  199. */
  200. return DIV_ROUND_CLOSEST(avg * INA226_TOTAL_CONV_TIME_DEFAULT, 1000);
  201. }
  202. /*
  203. * Return the new, shifted AVG field value of CONFIG register,
  204. * to use with regmap_update_bits
  205. */
  206. static u16 ina226_interval_to_reg(long interval)
  207. {
  208. int avg, avg_bits;
  209. /*
  210. * The maximum supported interval is 1,024 * (2 * 8.244ms) ~= 16.8s.
  211. * Clamp to 32 seconds before calculations to avoid overflows.
  212. */
  213. interval = clamp_val(interval, 0, 32000);
  214. avg = DIV_ROUND_CLOSEST(interval * 1000,
  215. INA226_TOTAL_CONV_TIME_DEFAULT);
  216. avg_bits = find_closest(avg, ina226_avg_tab,
  217. ARRAY_SIZE(ina226_avg_tab));
  218. return FIELD_PREP(INA226_AVG_RD_MASK, avg_bits);
  219. }
  220. static int ina2xx_get_value(struct ina2xx_data *data, u8 reg,
  221. unsigned int regval)
  222. {
  223. int val;
  224. switch (reg) {
  225. case INA2XX_SHUNT_VOLTAGE:
  226. /* signed register */
  227. val = DIV_ROUND_CLOSEST((s16)regval, data->config->shunt_div);
  228. break;
  229. case INA2XX_BUS_VOLTAGE:
  230. val = (regval >> data->config->bus_voltage_shift) *
  231. data->config->bus_voltage_lsb;
  232. val = DIV_ROUND_CLOSEST(val, 1000);
  233. break;
  234. case INA2XX_POWER:
  235. val = regval * data->power_lsb_uW;
  236. break;
  237. case INA2XX_CURRENT:
  238. /* signed register, result in mA */
  239. val = (s16)regval * data->current_lsb_uA;
  240. val = DIV_ROUND_CLOSEST(val, 1000);
  241. break;
  242. case INA2XX_CALIBRATION:
  243. val = regval;
  244. break;
  245. default:
  246. /* programmer goofed */
  247. WARN_ON_ONCE(1);
  248. val = 0;
  249. break;
  250. }
  251. return val;
  252. }
  253. /*
  254. * Read and convert register value from chip. If the register value is 0,
  255. * check if the chip has been power cycled or reset. If so, re-initialize it.
  256. */
  257. static int ina2xx_read_init(struct device *dev, int reg, long *val)
  258. {
  259. struct ina2xx_data *data = dev_get_drvdata(dev);
  260. struct regmap *regmap = data->regmap;
  261. unsigned int regval;
  262. int ret, retry;
  263. if (data->config->has_ishunt) {
  264. /* No calibration needed */
  265. ret = regmap_read(regmap, reg, &regval);
  266. if (ret < 0)
  267. return ret;
  268. *val = ina2xx_get_value(data, reg, regval);
  269. return 0;
  270. }
  271. for (retry = 5; retry; retry--) {
  272. ret = regmap_read(regmap, reg, &regval);
  273. if (ret < 0)
  274. return ret;
  275. /*
  276. * If the current value in the calibration register is 0, the
  277. * power and current registers will also remain at 0. In case
  278. * the chip has been reset let's check the calibration
  279. * register and reinitialize if needed.
  280. * We do that extra read of the calibration register if there
  281. * is some hint of a chip reset.
  282. */
  283. if (regval == 0) {
  284. unsigned int cal;
  285. ret = regmap_read_bypassed(regmap, INA2XX_CALIBRATION, &cal);
  286. if (ret < 0)
  287. return ret;
  288. if (cal == 0) {
  289. dev_warn(dev, "chip not calibrated, reinitializing\n");
  290. regcache_mark_dirty(regmap);
  291. regcache_sync(regmap);
  292. /*
  293. * Let's make sure the power and current
  294. * registers have been updated before trying
  295. * again.
  296. */
  297. msleep(INA2XX_MAX_DELAY);
  298. continue;
  299. }
  300. }
  301. *val = ina2xx_get_value(data, reg, regval);
  302. return 0;
  303. }
  304. /*
  305. * If we're here then although all write operations succeeded, the
  306. * chip still returns 0 in the calibration register. Nothing more we
  307. * can do here.
  308. */
  309. dev_err(dev, "unable to reinitialize the chip\n");
  310. return -ENODEV;
  311. }
  312. /*
  313. * Turns alert limit values into register values.
  314. * Opposite of the formula in ina2xx_get_value().
  315. */
  316. static u16 ina226_alert_to_reg(struct ina2xx_data *data, int reg, long val)
  317. {
  318. switch (reg) {
  319. case INA2XX_SHUNT_VOLTAGE:
  320. val = clamp_val(val, 0, SHRT_MAX * data->config->shunt_div);
  321. val *= data->config->shunt_div;
  322. return clamp_val(val, 0, SHRT_MAX);
  323. case INA2XX_BUS_VOLTAGE:
  324. val = clamp_val(val, 0, 200000);
  325. val = (val * 1000) << data->config->bus_voltage_shift;
  326. val = DIV_ROUND_CLOSEST(val, data->config->bus_voltage_lsb);
  327. return clamp_val(val, 0, USHRT_MAX);
  328. case INA2XX_POWER:
  329. val = clamp_val(val, 0, UINT_MAX - data->power_lsb_uW);
  330. val = DIV_ROUND_CLOSEST(val, data->power_lsb_uW);
  331. return clamp_val(val, 0, USHRT_MAX);
  332. case INA2XX_CURRENT:
  333. val = clamp_val(val, INT_MIN / 1000, INT_MAX / 1000);
  334. /* signed register, result in mA */
  335. val = DIV_ROUND_CLOSEST(val * 1000, data->current_lsb_uA);
  336. return clamp_val(val, SHRT_MIN, SHRT_MAX);
  337. default:
  338. /* programmer goofed */
  339. WARN_ON_ONCE(1);
  340. return 0;
  341. }
  342. }
  343. static int ina226_alert_limit_read(struct ina2xx_data *data, u32 mask, int reg, long *val)
  344. {
  345. struct regmap *regmap = data->regmap;
  346. int regval;
  347. int ret;
  348. ret = regmap_read(regmap, INA226_MASK_ENABLE, &regval);
  349. if (ret)
  350. return ret;
  351. if (regval & mask) {
  352. ret = regmap_read(regmap, INA226_ALERT_LIMIT, &regval);
  353. if (ret)
  354. return ret;
  355. *val = ina2xx_get_value(data, reg, regval);
  356. } else {
  357. *val = 0;
  358. }
  359. return 0;
  360. }
  361. static int ina226_alert_limit_write(struct ina2xx_data *data, u32 mask, int reg, long val)
  362. {
  363. struct regmap *regmap = data->regmap;
  364. int ret;
  365. if (val < 0)
  366. return -EINVAL;
  367. /*
  368. * Clear all alerts first to avoid accidentally triggering ALERT pin
  369. * due to register write sequence. Then, only enable the alert
  370. * if the value is non-zero.
  371. */
  372. ret = regmap_update_bits(regmap, INA226_MASK_ENABLE,
  373. INA226_ALERT_CONFIG_MASK, 0);
  374. if (ret < 0)
  375. return ret;
  376. ret = regmap_write(regmap, INA226_ALERT_LIMIT,
  377. ina226_alert_to_reg(data, reg, val));
  378. if (ret < 0)
  379. return ret;
  380. if (val)
  381. return regmap_update_bits(regmap, INA226_MASK_ENABLE,
  382. INA226_ALERT_CONFIG_MASK, mask);
  383. return 0;
  384. }
  385. static int ina2xx_chip_read(struct device *dev, u32 attr, long *val)
  386. {
  387. struct ina2xx_data *data = dev_get_drvdata(dev);
  388. u32 regval;
  389. int ret;
  390. switch (attr) {
  391. case hwmon_chip_update_interval:
  392. ret = regmap_read(data->regmap, INA2XX_CONFIG, &regval);
  393. if (ret)
  394. return ret;
  395. *val = ina226_reg_to_interval(regval);
  396. break;
  397. default:
  398. return -EOPNOTSUPP;
  399. }
  400. return 0;
  401. }
  402. static int ina226_alert_read(struct regmap *regmap, u32 mask, long *val)
  403. {
  404. unsigned int regval;
  405. int ret;
  406. ret = regmap_read_bypassed(regmap, INA226_MASK_ENABLE, &regval);
  407. if (ret)
  408. return ret;
  409. *val = (regval & mask) && (regval & INA226_ALERT_FUNCTION_FLAG);
  410. return 0;
  411. }
  412. static int ina2xx_in_read(struct device *dev, u32 attr, int channel, long *val)
  413. {
  414. int voltage_reg = channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE;
  415. u32 under_voltage_mask = channel ? INA226_BUS_UNDER_VOLTAGE_MASK
  416. : INA226_SHUNT_UNDER_VOLTAGE_MASK;
  417. u32 over_voltage_mask = channel ? INA226_BUS_OVER_VOLTAGE_MASK
  418. : INA226_SHUNT_OVER_VOLTAGE_MASK;
  419. struct ina2xx_data *data = dev_get_drvdata(dev);
  420. struct regmap *regmap = data->regmap;
  421. unsigned int regval;
  422. int ret;
  423. switch (attr) {
  424. case hwmon_in_input:
  425. ret = regmap_read(regmap, voltage_reg, &regval);
  426. if (ret)
  427. return ret;
  428. *val = ina2xx_get_value(data, voltage_reg, regval);
  429. break;
  430. case hwmon_in_lcrit:
  431. return ina226_alert_limit_read(data, under_voltage_mask,
  432. voltage_reg, val);
  433. case hwmon_in_crit:
  434. return ina226_alert_limit_read(data, over_voltage_mask,
  435. voltage_reg, val);
  436. case hwmon_in_lcrit_alarm:
  437. return ina226_alert_read(regmap, under_voltage_mask, val);
  438. case hwmon_in_crit_alarm:
  439. return ina226_alert_read(regmap, over_voltage_mask, val);
  440. default:
  441. return -EOPNOTSUPP;
  442. }
  443. return 0;
  444. }
  445. /*
  446. * Configuring the READ_EIN (bit 10) of the ACCUM_CONFIG register to 1
  447. * can clear accumulator and sample_count after reading the EIN register.
  448. * This way, the average power between the last read and the current
  449. * read can be obtained. By combining with accurate time data from
  450. * outside, the energy consumption during that period can be calculated.
  451. */
  452. static int sy24655_average_power_read(struct ina2xx_data *data, u8 reg, long *val)
  453. {
  454. u8 template[6];
  455. int ret;
  456. long accumulator_24, sample_count;
  457. /* 48-bit register read */
  458. ret = i2c_smbus_read_i2c_block_data(data->client, reg, 6, template);
  459. if (ret < 0)
  460. return ret;
  461. if (ret != 6)
  462. return -EIO;
  463. accumulator_24 = ((template[3] << 16) |
  464. (template[4] << 8) |
  465. template[5]);
  466. sample_count = ((template[0] << 16) |
  467. (template[1] << 8) |
  468. template[2]);
  469. if (sample_count <= 0) {
  470. *val = 0;
  471. return 0;
  472. }
  473. *val = DIV_ROUND_CLOSEST(accumulator_24, sample_count) * data->power_lsb_uW;
  474. return 0;
  475. }
  476. static int ina2xx_power_read(struct device *dev, u32 attr, long *val)
  477. {
  478. struct ina2xx_data *data = dev_get_drvdata(dev);
  479. switch (attr) {
  480. case hwmon_power_input:
  481. return ina2xx_read_init(dev, INA2XX_POWER, val);
  482. case hwmon_power_average:
  483. return sy24655_average_power_read(data, SY24655_EIN, val);
  484. case hwmon_power_crit:
  485. return ina226_alert_limit_read(data, INA226_POWER_OVER_LIMIT_MASK,
  486. INA2XX_POWER, val);
  487. case hwmon_power_crit_alarm:
  488. return ina226_alert_read(data->regmap, INA226_POWER_OVER_LIMIT_MASK, val);
  489. default:
  490. return -EOPNOTSUPP;
  491. }
  492. }
  493. static int ina2xx_curr_read(struct device *dev, u32 attr, long *val)
  494. {
  495. struct ina2xx_data *data = dev_get_drvdata(dev);
  496. struct regmap *regmap = data->regmap;
  497. unsigned int regval;
  498. int ret;
  499. /*
  500. * While the chips supported by this driver do not directly support
  501. * current limits, they do support setting shunt voltage limits.
  502. * The shunt voltage divided by the shunt resistor value is the current.
  503. * On top of that, calibration values are set such that in the shunt
  504. * voltage register and the current register report the same values.
  505. * That means we can report and configure current limits based on shunt
  506. * voltage limits.
  507. */
  508. switch (attr) {
  509. case hwmon_curr_input:
  510. /*
  511. * Since the shunt voltage and the current register report the
  512. * same values when the chip is calibrated, we can calculate
  513. * the current directly from the shunt voltage without relying
  514. * on chip calibration.
  515. */
  516. ret = regmap_read(regmap, INA2XX_SHUNT_VOLTAGE, &regval);
  517. if (ret)
  518. return ret;
  519. *val = ina2xx_get_value(data, INA2XX_CURRENT, regval);
  520. return 0;
  521. case hwmon_curr_lcrit:
  522. return ina226_alert_limit_read(data, INA226_SHUNT_UNDER_VOLTAGE_MASK,
  523. INA2XX_CURRENT, val);
  524. case hwmon_curr_crit:
  525. return ina226_alert_limit_read(data, INA226_SHUNT_OVER_VOLTAGE_MASK,
  526. INA2XX_CURRENT, val);
  527. case hwmon_curr_lcrit_alarm:
  528. return ina226_alert_read(regmap, INA226_SHUNT_UNDER_VOLTAGE_MASK, val);
  529. case hwmon_curr_crit_alarm:
  530. return ina226_alert_read(regmap, INA226_SHUNT_OVER_VOLTAGE_MASK, val);
  531. default:
  532. return -EOPNOTSUPP;
  533. }
  534. }
  535. static int ina2xx_read(struct device *dev, enum hwmon_sensor_types type,
  536. u32 attr, int channel, long *val)
  537. {
  538. switch (type) {
  539. case hwmon_chip:
  540. return ina2xx_chip_read(dev, attr, val);
  541. case hwmon_in:
  542. return ina2xx_in_read(dev, attr, channel, val);
  543. case hwmon_power:
  544. return ina2xx_power_read(dev, attr, val);
  545. case hwmon_curr:
  546. return ina2xx_curr_read(dev, attr, val);
  547. default:
  548. return -EOPNOTSUPP;
  549. }
  550. }
  551. static int ina2xx_chip_write(struct device *dev, u32 attr, long val)
  552. {
  553. struct ina2xx_data *data = dev_get_drvdata(dev);
  554. switch (attr) {
  555. case hwmon_chip_update_interval:
  556. return regmap_update_bits(data->regmap, INA2XX_CONFIG,
  557. INA226_AVG_RD_MASK,
  558. ina226_interval_to_reg(val));
  559. default:
  560. return -EOPNOTSUPP;
  561. }
  562. }
  563. static int ina2xx_in_write(struct device *dev, u32 attr, int channel, long val)
  564. {
  565. struct ina2xx_data *data = dev_get_drvdata(dev);
  566. switch (attr) {
  567. case hwmon_in_lcrit:
  568. return ina226_alert_limit_write(data,
  569. channel ? INA226_BUS_UNDER_VOLTAGE_MASK : INA226_SHUNT_UNDER_VOLTAGE_MASK,
  570. channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE,
  571. val);
  572. case hwmon_in_crit:
  573. return ina226_alert_limit_write(data,
  574. channel ? INA226_BUS_OVER_VOLTAGE_MASK : INA226_SHUNT_OVER_VOLTAGE_MASK,
  575. channel ? INA2XX_BUS_VOLTAGE : INA2XX_SHUNT_VOLTAGE,
  576. val);
  577. default:
  578. return -EOPNOTSUPP;
  579. }
  580. return 0;
  581. }
  582. static int ina2xx_power_write(struct device *dev, u32 attr, long val)
  583. {
  584. struct ina2xx_data *data = dev_get_drvdata(dev);
  585. switch (attr) {
  586. case hwmon_power_crit:
  587. return ina226_alert_limit_write(data, INA226_POWER_OVER_LIMIT_MASK,
  588. INA2XX_POWER, val);
  589. default:
  590. return -EOPNOTSUPP;
  591. }
  592. return 0;
  593. }
  594. static int ina2xx_curr_write(struct device *dev, u32 attr, long val)
  595. {
  596. struct ina2xx_data *data = dev_get_drvdata(dev);
  597. switch (attr) {
  598. case hwmon_curr_lcrit:
  599. return ina226_alert_limit_write(data, INA226_SHUNT_UNDER_VOLTAGE_MASK,
  600. INA2XX_CURRENT, val);
  601. case hwmon_curr_crit:
  602. return ina226_alert_limit_write(data, INA226_SHUNT_OVER_VOLTAGE_MASK,
  603. INA2XX_CURRENT, val);
  604. default:
  605. return -EOPNOTSUPP;
  606. }
  607. return 0;
  608. }
  609. static int ina2xx_write(struct device *dev, enum hwmon_sensor_types type,
  610. u32 attr, int channel, long val)
  611. {
  612. switch (type) {
  613. case hwmon_chip:
  614. return ina2xx_chip_write(dev, attr, val);
  615. case hwmon_in:
  616. return ina2xx_in_write(dev, attr, channel, val);
  617. case hwmon_power:
  618. return ina2xx_power_write(dev, attr, val);
  619. case hwmon_curr:
  620. return ina2xx_curr_write(dev, attr, val);
  621. default:
  622. return -EOPNOTSUPP;
  623. }
  624. }
  625. static umode_t ina2xx_is_visible(const void *_data, enum hwmon_sensor_types type,
  626. u32 attr, int channel)
  627. {
  628. const struct ina2xx_data *data = _data;
  629. bool has_alerts = data->config->has_alerts;
  630. bool has_power_average = data->config->has_power_average;
  631. enum ina2xx_ids chip = data->chip;
  632. switch (type) {
  633. case hwmon_in:
  634. switch (attr) {
  635. case hwmon_in_input:
  636. return 0444;
  637. case hwmon_in_lcrit:
  638. case hwmon_in_crit:
  639. if (has_alerts)
  640. return 0644;
  641. break;
  642. case hwmon_in_lcrit_alarm:
  643. case hwmon_in_crit_alarm:
  644. if (has_alerts)
  645. return 0444;
  646. break;
  647. default:
  648. break;
  649. }
  650. break;
  651. case hwmon_curr:
  652. switch (attr) {
  653. case hwmon_curr_input:
  654. return 0444;
  655. case hwmon_curr_lcrit:
  656. case hwmon_curr_crit:
  657. if (has_alerts)
  658. return 0644;
  659. break;
  660. case hwmon_curr_lcrit_alarm:
  661. case hwmon_curr_crit_alarm:
  662. if (has_alerts)
  663. return 0444;
  664. break;
  665. default:
  666. break;
  667. }
  668. break;
  669. case hwmon_power:
  670. switch (attr) {
  671. case hwmon_power_input:
  672. return 0444;
  673. case hwmon_power_crit:
  674. if (has_alerts)
  675. return 0644;
  676. break;
  677. case hwmon_power_crit_alarm:
  678. if (has_alerts)
  679. return 0444;
  680. break;
  681. case hwmon_power_average:
  682. if (has_power_average)
  683. return 0444;
  684. break;
  685. default:
  686. break;
  687. }
  688. break;
  689. case hwmon_chip:
  690. switch (attr) {
  691. case hwmon_chip_update_interval:
  692. if (chip == ina226 || chip == ina260)
  693. return 0644;
  694. break;
  695. default:
  696. break;
  697. }
  698. break;
  699. default:
  700. break;
  701. }
  702. return 0;
  703. }
  704. static const struct hwmon_channel_info * const ina2xx_info[] = {
  705. HWMON_CHANNEL_INFO(chip,
  706. HWMON_C_UPDATE_INTERVAL),
  707. HWMON_CHANNEL_INFO(in,
  708. HWMON_I_INPUT | HWMON_I_CRIT | HWMON_I_CRIT_ALARM |
  709. HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM,
  710. HWMON_I_INPUT | HWMON_I_CRIT | HWMON_I_CRIT_ALARM |
  711. HWMON_I_LCRIT | HWMON_I_LCRIT_ALARM
  712. ),
  713. HWMON_CHANNEL_INFO(curr, HWMON_C_INPUT | HWMON_C_CRIT | HWMON_C_CRIT_ALARM |
  714. HWMON_C_LCRIT | HWMON_C_LCRIT_ALARM),
  715. HWMON_CHANNEL_INFO(power,
  716. HWMON_P_INPUT | HWMON_P_CRIT | HWMON_P_CRIT_ALARM |
  717. HWMON_P_AVERAGE),
  718. NULL
  719. };
  720. static const struct hwmon_ops ina2xx_hwmon_ops = {
  721. .is_visible = ina2xx_is_visible,
  722. .read = ina2xx_read,
  723. .write = ina2xx_write,
  724. };
  725. static const struct hwmon_chip_info ina2xx_chip_info = {
  726. .ops = &ina2xx_hwmon_ops,
  727. .info = ina2xx_info,
  728. };
  729. /* shunt resistance */
  730. /*
  731. * In order to keep calibration register value fixed, the product
  732. * of current_lsb and shunt_resistor should also be fixed and equal
  733. * to shunt_voltage_lsb = 1 / shunt_div multiplied by 10^9 in order
  734. * to keep the scale.
  735. */
  736. static int ina2xx_set_shunt(struct ina2xx_data *data, unsigned long val)
  737. {
  738. unsigned int dividend = DIV_ROUND_CLOSEST(1000000000,
  739. data->config->shunt_div);
  740. if (!val || val > dividend)
  741. return -EINVAL;
  742. data->rshunt = val;
  743. data->current_lsb_uA = DIV_ROUND_CLOSEST(dividend, val);
  744. data->power_lsb_uW = data->config->power_lsb_factor *
  745. data->current_lsb_uA;
  746. return 0;
  747. }
  748. static ssize_t shunt_resistor_show(struct device *dev,
  749. struct device_attribute *da, char *buf)
  750. {
  751. struct ina2xx_data *data = dev_get_drvdata(dev);
  752. return sysfs_emit(buf, "%li\n", data->rshunt);
  753. }
  754. static ssize_t shunt_resistor_store(struct device *dev,
  755. struct device_attribute *da,
  756. const char *buf, size_t count)
  757. {
  758. struct ina2xx_data *data = dev_get_drvdata(dev);
  759. unsigned long val;
  760. int status;
  761. status = kstrtoul(buf, 10, &val);
  762. if (status < 0)
  763. return status;
  764. hwmon_lock(dev);
  765. status = ina2xx_set_shunt(data, val);
  766. hwmon_unlock(dev);
  767. if (status < 0)
  768. return status;
  769. return count;
  770. }
  771. static DEVICE_ATTR_RW(shunt_resistor);
  772. /* pointers to created device attributes */
  773. static struct attribute *ina2xx_attrs[] = {
  774. &dev_attr_shunt_resistor.attr,
  775. NULL,
  776. };
  777. ATTRIBUTE_GROUPS(ina2xx);
  778. /*
  779. * Initialize chip
  780. */
  781. static int ina2xx_init(struct device *dev, struct ina2xx_data *data)
  782. {
  783. struct regmap *regmap = data->regmap;
  784. u32 shunt;
  785. int ret;
  786. if (data->config->has_ishunt)
  787. shunt = INA260_RSHUNT;
  788. else if (device_property_read_u32(dev, "shunt-resistor", &shunt) < 0)
  789. shunt = INA2XX_RSHUNT_DEFAULT;
  790. ret = ina2xx_set_shunt(data, shunt);
  791. if (ret < 0)
  792. return ret;
  793. ret = regmap_write(regmap, INA2XX_CONFIG, data->config->config_default);
  794. if (ret < 0)
  795. return ret;
  796. if (data->config->has_alerts) {
  797. bool active_high = device_property_read_bool(dev, "ti,alert-polarity-active-high");
  798. regmap_update_bits(regmap, INA226_MASK_ENABLE,
  799. INA226_ALERT_LATCH_ENABLE | INA226_ALERT_POLARITY,
  800. INA226_ALERT_LATCH_ENABLE |
  801. FIELD_PREP(INA226_ALERT_POLARITY, active_high));
  802. }
  803. if (data->config->has_power_average) {
  804. if (data->chip == sy24655) {
  805. /*
  806. * Initialize the power accumulation method to continuous
  807. * mode and clear the EIN register after each read of the
  808. * EIN register
  809. */
  810. ret = regmap_write(regmap, SY24655_ACCUM_CONFIG,
  811. SY24655_ACCUM_CONFIG_DEFAULT);
  812. if (ret < 0)
  813. return ret;
  814. }
  815. }
  816. if (data->config->has_ishunt)
  817. return 0;
  818. /*
  819. * Calibration register is set to the best value, which eliminates
  820. * truncation errors on calculating current register in hardware.
  821. * According to datasheet (eq. 3) the best values are 2048 for
  822. * ina226 and 4096 for ina219. They are hardcoded as calibration_value.
  823. */
  824. return regmap_write(regmap, INA2XX_CALIBRATION,
  825. data->config->calibration_value);
  826. }
  827. static int ina2xx_probe(struct i2c_client *client)
  828. {
  829. struct device *dev = &client->dev;
  830. struct ina2xx_data *data;
  831. struct device *hwmon_dev;
  832. enum ina2xx_ids chip;
  833. int ret;
  834. chip = (uintptr_t)i2c_get_match_data(client);
  835. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  836. if (!data)
  837. return -ENOMEM;
  838. /* set the device type */
  839. data->client = client;
  840. data->config = &ina2xx_config[chip];
  841. data->chip = chip;
  842. data->regmap = devm_regmap_init_i2c(client, &ina2xx_regmap_config);
  843. if (IS_ERR(data->regmap)) {
  844. dev_err(dev, "failed to allocate register map\n");
  845. return PTR_ERR(data->regmap);
  846. }
  847. /*
  848. * Regulator core returns -ENODEV if the 'vs' is not available.
  849. * Hence the check for -ENODEV return code is necessary.
  850. */
  851. ret = devm_regulator_get_enable_optional(dev, "vs");
  852. if (ret < 0 && ret != -ENODEV)
  853. return dev_err_probe(dev, ret, "failed to enable vs regulator\n");
  854. ret = ina2xx_init(dev, data);
  855. if (ret < 0)
  856. return dev_err_probe(dev, ret, "failed to configure device\n");
  857. hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
  858. data, &ina2xx_chip_info,
  859. data->config->has_ishunt ?
  860. NULL : ina2xx_groups);
  861. if (IS_ERR(hwmon_dev))
  862. return PTR_ERR(hwmon_dev);
  863. dev_info(dev, "power monitor %s (Rshunt = %li uOhm)\n",
  864. client->name, data->rshunt);
  865. return 0;
  866. }
  867. static const struct i2c_device_id ina2xx_id[] = {
  868. { "ina219", ina219 },
  869. { "ina220", ina219 },
  870. { "ina226", ina226 },
  871. { "ina230", ina226 },
  872. { "ina231", ina226 },
  873. { "ina260", ina260 },
  874. { "sy24655", sy24655 },
  875. { }
  876. };
  877. MODULE_DEVICE_TABLE(i2c, ina2xx_id);
  878. static const struct of_device_id __maybe_unused ina2xx_of_match[] = {
  879. {
  880. .compatible = "silergy,sy24655",
  881. .data = (void *)sy24655
  882. },
  883. {
  884. .compatible = "ti,ina219",
  885. .data = (void *)ina219
  886. },
  887. {
  888. .compatible = "ti,ina220",
  889. .data = (void *)ina219
  890. },
  891. {
  892. .compatible = "ti,ina226",
  893. .data = (void *)ina226
  894. },
  895. {
  896. .compatible = "ti,ina230",
  897. .data = (void *)ina226
  898. },
  899. {
  900. .compatible = "ti,ina231",
  901. .data = (void *)ina226
  902. },
  903. {
  904. .compatible = "ti,ina260",
  905. .data = (void *)ina260
  906. },
  907. { }
  908. };
  909. MODULE_DEVICE_TABLE(of, ina2xx_of_match);
  910. static struct i2c_driver ina2xx_driver = {
  911. .driver = {
  912. .name = "ina2xx",
  913. .of_match_table = of_match_ptr(ina2xx_of_match),
  914. },
  915. .probe = ina2xx_probe,
  916. .id_table = ina2xx_id,
  917. };
  918. module_i2c_driver(ina2xx_driver);
  919. MODULE_AUTHOR("Lothar Felten <l-felten@ti.com>");
  920. MODULE_DESCRIPTION("ina2xx driver");
  921. MODULE_LICENSE("GPL");