ltc4245.c 12 KB

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