ltc4245.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
  4. *
  5. * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
  6. *
  7. * This driver is based on the ds1621 and ina209 drivers.
  8. *
  9. * Datasheet:
  10. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/bitops.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/i2c.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/platform_data/ltc4245.h>
  22. /* Here are names of the chip's registers (a.k.a. commands) */
  23. enum ltc4245_cmd {
  24. LTC4245_STATUS = 0x00, /* readonly */
  25. LTC4245_ALERT = 0x01,
  26. LTC4245_CONTROL = 0x02,
  27. LTC4245_ON = 0x03,
  28. LTC4245_FAULT1 = 0x04,
  29. LTC4245_FAULT2 = 0x05,
  30. LTC4245_GPIO = 0x06,
  31. LTC4245_ADCADR = 0x07,
  32. LTC4245_12VIN = 0x10,
  33. LTC4245_12VSENSE = 0x11,
  34. LTC4245_12VOUT = 0x12,
  35. LTC4245_5VIN = 0x13,
  36. LTC4245_5VSENSE = 0x14,
  37. LTC4245_5VOUT = 0x15,
  38. LTC4245_3VIN = 0x16,
  39. LTC4245_3VSENSE = 0x17,
  40. LTC4245_3VOUT = 0x18,
  41. LTC4245_VEEIN = 0x19,
  42. LTC4245_VEESENSE = 0x1a,
  43. LTC4245_VEEOUT = 0x1b,
  44. LTC4245_GPIOADC = 0x1c,
  45. };
  46. struct ltc4245_data {
  47. struct i2c_client *client;
  48. bool valid;
  49. unsigned long last_updated; /* in jiffies */
  50. /* Control registers */
  51. u8 cregs[0x08];
  52. /* Voltage registers */
  53. u8 vregs[0x0d];
  54. /* GPIO ADC registers */
  55. bool use_extra_gpios;
  56. int gpios[3];
  57. };
  58. /*
  59. * Update the readings from the GPIO pins. If the driver has been configured to
  60. * sample all GPIO's as analog voltages, a round-robin sampling method is used.
  61. * Otherwise, only the configured GPIO pin is sampled.
  62. *
  63. * LOCKING: must hold data->update_lock
  64. */
  65. static void ltc4245_update_gpios(struct device *dev)
  66. {
  67. struct ltc4245_data *data = dev_get_drvdata(dev);
  68. struct i2c_client *client = data->client;
  69. u8 gpio_curr, gpio_next, gpio_reg;
  70. int i;
  71. /* no extra gpio support, we're basically done */
  72. if (!data->use_extra_gpios) {
  73. data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
  74. return;
  75. }
  76. /*
  77. * If the last reading was too long ago, then we mark all old GPIO
  78. * readings as stale by setting them to -EAGAIN
  79. */
  80. if (time_after(jiffies, data->last_updated + 5 * HZ)) {
  81. for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
  82. data->gpios[i] = -EAGAIN;
  83. }
  84. /*
  85. * Get the current GPIO pin
  86. *
  87. * The datasheet calls these GPIO[1-3], but we'll calculate the zero
  88. * based array index instead, and call them GPIO[0-2]. This is much
  89. * easier to think about.
  90. */
  91. gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
  92. if (gpio_curr > 0)
  93. gpio_curr -= 1;
  94. /* Read the GPIO voltage from the GPIOADC register */
  95. data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
  96. /* Find the next GPIO pin to read */
  97. gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
  98. /*
  99. * Calculate the correct setting for the GPIO register so it will
  100. * sample the next GPIO pin
  101. */
  102. gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
  103. /* Update the GPIO register */
  104. i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
  105. /* Update saved data */
  106. data->cregs[LTC4245_GPIO] = gpio_reg;
  107. }
  108. static struct ltc4245_data *ltc4245_update_device(struct device *dev)
  109. {
  110. struct ltc4245_data *data = dev_get_drvdata(dev);
  111. struct i2c_client *client = data->client;
  112. s32 val;
  113. int i;
  114. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  115. /* Read control registers -- 0x00 to 0x07 */
  116. for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
  117. val = i2c_smbus_read_byte_data(client, i);
  118. if (unlikely(val < 0))
  119. data->cregs[i] = 0;
  120. else
  121. data->cregs[i] = val;
  122. }
  123. /* Read voltage registers -- 0x10 to 0x1c */
  124. for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
  125. val = i2c_smbus_read_byte_data(client, i+0x10);
  126. if (unlikely(val < 0))
  127. data->vregs[i] = 0;
  128. else
  129. data->vregs[i] = val;
  130. }
  131. /* Update GPIO readings */
  132. ltc4245_update_gpios(dev);
  133. data->last_updated = jiffies;
  134. data->valid = true;
  135. }
  136. return data;
  137. }
  138. /* Return the voltage from the given register in millivolts */
  139. static int ltc4245_get_voltage(struct device *dev, u8 reg)
  140. {
  141. struct ltc4245_data *data = ltc4245_update_device(dev);
  142. const u8 regval = data->vregs[reg - 0x10];
  143. u32 voltage = 0;
  144. switch (reg) {
  145. case LTC4245_12VIN:
  146. case LTC4245_12VOUT:
  147. voltage = regval * 55;
  148. break;
  149. case LTC4245_5VIN:
  150. case LTC4245_5VOUT:
  151. voltage = regval * 22;
  152. break;
  153. case LTC4245_3VIN:
  154. case LTC4245_3VOUT:
  155. voltage = regval * 15;
  156. break;
  157. case LTC4245_VEEIN:
  158. case LTC4245_VEEOUT:
  159. voltage = regval * -55;
  160. break;
  161. case LTC4245_GPIOADC:
  162. voltage = regval * 10;
  163. break;
  164. default:
  165. /* If we get here, the developer messed up */
  166. WARN_ON_ONCE(1);
  167. break;
  168. }
  169. return voltage;
  170. }
  171. /* Return the current in the given sense register in milliAmperes */
  172. static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
  173. {
  174. struct ltc4245_data *data = ltc4245_update_device(dev);
  175. const u8 regval = data->vregs[reg - 0x10];
  176. unsigned int voltage;
  177. unsigned int curr;
  178. /*
  179. * The strange looking conversions that follow are fixed-point
  180. * math, since we cannot do floating point in the kernel.
  181. *
  182. * Step 1: convert sense register to microVolts
  183. * Step 2: convert voltage to milliAmperes
  184. *
  185. * If you play around with the V=IR equation, you come up with
  186. * the following: X uV / Y mOhm == Z mA
  187. *
  188. * With the resistors that are fractions of a milliOhm, we multiply
  189. * the voltage and resistance by 10, to shift the decimal point.
  190. * Now we can use the normal division operator again.
  191. */
  192. switch (reg) {
  193. case LTC4245_12VSENSE:
  194. voltage = regval * 250; /* voltage in uV */
  195. curr = voltage / 50; /* sense resistor 50 mOhm */
  196. break;
  197. case LTC4245_5VSENSE:
  198. voltage = regval * 125; /* voltage in uV */
  199. curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
  200. break;
  201. case LTC4245_3VSENSE:
  202. voltage = regval * 125; /* voltage in uV */
  203. curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
  204. break;
  205. case LTC4245_VEESENSE:
  206. voltage = regval * 250; /* voltage in uV */
  207. curr = voltage / 100; /* sense resistor 100 mOhm */
  208. break;
  209. default:
  210. /* If we get here, the developer messed up */
  211. WARN_ON_ONCE(1);
  212. curr = 0;
  213. break;
  214. }
  215. return curr;
  216. }
  217. /* Map from voltage channel index to voltage register */
  218. static const s8 ltc4245_in_regs[] = {
  219. LTC4245_12VIN, LTC4245_5VIN, LTC4245_3VIN, LTC4245_VEEIN,
  220. LTC4245_12VOUT, LTC4245_5VOUT, LTC4245_3VOUT, LTC4245_VEEOUT,
  221. };
  222. /* Map from current channel index to current register */
  223. static const s8 ltc4245_curr_regs[] = {
  224. LTC4245_12VSENSE, LTC4245_5VSENSE, LTC4245_3VSENSE, LTC4245_VEESENSE,
  225. };
  226. static int ltc4245_read_curr(struct device *dev, u32 attr, int channel,
  227. long *val)
  228. {
  229. struct ltc4245_data *data = ltc4245_update_device(dev);
  230. switch (attr) {
  231. case hwmon_curr_input:
  232. *val = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
  233. return 0;
  234. case hwmon_curr_max_alarm:
  235. *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel + 4));
  236. return 0;
  237. default:
  238. return -EOPNOTSUPP;
  239. }
  240. }
  241. static int ltc4245_read_in(struct device *dev, u32 attr, int channel, long *val)
  242. {
  243. struct ltc4245_data *data = ltc4245_update_device(dev);
  244. switch (attr) {
  245. case hwmon_in_input:
  246. if (channel < 8) {
  247. *val = ltc4245_get_voltage(dev,
  248. ltc4245_in_regs[channel]);
  249. } else {
  250. int regval = data->gpios[channel - 8];
  251. if (regval < 0)
  252. return regval;
  253. *val = regval * 10;
  254. }
  255. return 0;
  256. case hwmon_in_min_alarm:
  257. if (channel < 4)
  258. *val = !!(data->cregs[LTC4245_FAULT1] & BIT(channel));
  259. else
  260. *val = !!(data->cregs[LTC4245_FAULT2] &
  261. BIT(channel - 4));
  262. return 0;
  263. default:
  264. return -EOPNOTSUPP;
  265. }
  266. }
  267. static int ltc4245_read_power(struct device *dev, u32 attr, int channel,
  268. long *val)
  269. {
  270. unsigned long curr;
  271. long voltage;
  272. switch (attr) {
  273. case hwmon_power_input:
  274. (void)ltc4245_update_device(dev);
  275. curr = ltc4245_get_current(dev, ltc4245_curr_regs[channel]);
  276. voltage = ltc4245_get_voltage(dev, ltc4245_in_regs[channel]);
  277. *val = abs(curr * voltage);
  278. return 0;
  279. default:
  280. return -EOPNOTSUPP;
  281. }
  282. }
  283. static int ltc4245_read(struct device *dev, enum hwmon_sensor_types type,
  284. u32 attr, int channel, long *val)
  285. {
  286. switch (type) {
  287. case hwmon_curr:
  288. return ltc4245_read_curr(dev, attr, channel, val);
  289. case hwmon_power:
  290. return ltc4245_read_power(dev, attr, channel, val);
  291. case hwmon_in:
  292. return ltc4245_read_in(dev, attr, channel - 1, val);
  293. default:
  294. return -EOPNOTSUPP;
  295. }
  296. }
  297. static umode_t ltc4245_is_visible(const void *_data,
  298. enum hwmon_sensor_types type,
  299. u32 attr, int channel)
  300. {
  301. const struct ltc4245_data *data = _data;
  302. switch (type) {
  303. case hwmon_in:
  304. if (channel == 0)
  305. return 0;
  306. switch (attr) {
  307. case hwmon_in_input:
  308. if (channel > 9 && !data->use_extra_gpios)
  309. return 0;
  310. return 0444;
  311. case hwmon_in_min_alarm:
  312. if (channel > 8)
  313. return 0;
  314. return 0444;
  315. default:
  316. return 0;
  317. }
  318. case hwmon_curr:
  319. switch (attr) {
  320. case hwmon_curr_input:
  321. case hwmon_curr_max_alarm:
  322. return 0444;
  323. default:
  324. return 0;
  325. }
  326. case hwmon_power:
  327. switch (attr) {
  328. case hwmon_power_input:
  329. return 0444;
  330. default:
  331. return 0;
  332. }
  333. default:
  334. return 0;
  335. }
  336. }
  337. static const struct hwmon_channel_info * const ltc4245_info[] = {
  338. HWMON_CHANNEL_INFO(in,
  339. HWMON_I_INPUT,
  340. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  341. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  342. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  343. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  344. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  345. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  346. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  347. HWMON_I_INPUT | HWMON_I_MIN_ALARM,
  348. HWMON_I_INPUT,
  349. HWMON_I_INPUT,
  350. HWMON_I_INPUT),
  351. HWMON_CHANNEL_INFO(curr,
  352. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  353. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  354. HWMON_C_INPUT | HWMON_C_MAX_ALARM,
  355. HWMON_C_INPUT | HWMON_C_MAX_ALARM),
  356. HWMON_CHANNEL_INFO(power,
  357. HWMON_P_INPUT,
  358. HWMON_P_INPUT,
  359. HWMON_P_INPUT,
  360. HWMON_P_INPUT),
  361. NULL
  362. };
  363. static const struct hwmon_ops ltc4245_hwmon_ops = {
  364. .is_visible = ltc4245_is_visible,
  365. .read = ltc4245_read,
  366. };
  367. static const struct hwmon_chip_info ltc4245_chip_info = {
  368. .ops = &ltc4245_hwmon_ops,
  369. .info = ltc4245_info,
  370. };
  371. static bool ltc4245_use_extra_gpios(struct i2c_client *client)
  372. {
  373. struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
  374. struct device_node *np = client->dev.of_node;
  375. /* prefer platform data */
  376. if (pdata)
  377. return pdata->use_extra_gpios;
  378. /* fallback on OF */
  379. if (of_property_read_bool(np, "ltc4245,use-extra-gpios"))
  380. return true;
  381. return false;
  382. }
  383. static int ltc4245_probe(struct i2c_client *client)
  384. {
  385. struct i2c_adapter *adapter = client->adapter;
  386. struct ltc4245_data *data;
  387. struct device *hwmon_dev;
  388. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  389. return -ENODEV;
  390. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  391. if (!data)
  392. return -ENOMEM;
  393. data->client = client;
  394. data->use_extra_gpios = ltc4245_use_extra_gpios(client);
  395. /* Initialize the LTC4245 chip */
  396. i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
  397. i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
  398. hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
  399. client->name, data,
  400. &ltc4245_chip_info,
  401. NULL);
  402. return PTR_ERR_OR_ZERO(hwmon_dev);
  403. }
  404. static const struct i2c_device_id ltc4245_id[] = {
  405. { "ltc4245" },
  406. { }
  407. };
  408. MODULE_DEVICE_TABLE(i2c, ltc4245_id);
  409. /* This is the driver that will be inserted */
  410. static struct i2c_driver ltc4245_driver = {
  411. .driver = {
  412. .name = "ltc4245",
  413. },
  414. .probe = ltc4245_probe,
  415. .id_table = ltc4245_id,
  416. };
  417. module_i2c_driver(ltc4245_driver);
  418. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  419. MODULE_DESCRIPTION("LTC4245 driver");
  420. MODULE_LICENSE("GPL");