ltc4245.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
  3. *
  4. * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This driver is based on the ds1621 and ina209 drivers.
  11. *
  12. * Datasheet:
  13. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/i2c.h>
  21. #include <linux/hwmon.h>
  22. #include <linux/hwmon-sysfs.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_GPIOADC1 = 0x1c,
  46. LTC4245_GPIOADC2 = 0x1d,
  47. LTC4245_GPIOADC3 = 0x1e,
  48. };
  49. struct ltc4245_data {
  50. struct device *hwmon_dev;
  51. struct mutex update_lock;
  52. bool valid;
  53. unsigned long last_updated; /* in jiffies */
  54. /* Control registers */
  55. u8 cregs[0x08];
  56. /* Voltage registers */
  57. u8 vregs[0x0f];
  58. };
  59. static struct ltc4245_data *ltc4245_update_device(struct device *dev)
  60. {
  61. struct i2c_client *client = to_i2c_client(dev);
  62. struct ltc4245_data *data = i2c_get_clientdata(client);
  63. s32 val;
  64. int i;
  65. mutex_lock(&data->update_lock);
  66. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  67. dev_dbg(&client->dev, "Starting ltc4245 update\n");
  68. /* Read control registers -- 0x00 to 0x07 */
  69. for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
  70. val = i2c_smbus_read_byte_data(client, i);
  71. if (unlikely(val < 0))
  72. data->cregs[i] = 0;
  73. else
  74. data->cregs[i] = val;
  75. }
  76. /* Read voltage registers -- 0x10 to 0x1f */
  77. for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
  78. val = i2c_smbus_read_byte_data(client, i+0x10);
  79. if (unlikely(val < 0))
  80. data->vregs[i] = 0;
  81. else
  82. data->vregs[i] = val;
  83. }
  84. data->last_updated = jiffies;
  85. data->valid = 1;
  86. }
  87. mutex_unlock(&data->update_lock);
  88. return data;
  89. }
  90. /* Return the voltage from the given register in millivolts */
  91. static int ltc4245_get_voltage(struct device *dev, u8 reg)
  92. {
  93. struct ltc4245_data *data = ltc4245_update_device(dev);
  94. const u8 regval = data->vregs[reg - 0x10];
  95. u32 voltage = 0;
  96. switch (reg) {
  97. case LTC4245_12VIN:
  98. case LTC4245_12VOUT:
  99. voltage = regval * 55;
  100. break;
  101. case LTC4245_5VIN:
  102. case LTC4245_5VOUT:
  103. voltage = regval * 22;
  104. break;
  105. case LTC4245_3VIN:
  106. case LTC4245_3VOUT:
  107. voltage = regval * 15;
  108. break;
  109. case LTC4245_VEEIN:
  110. case LTC4245_VEEOUT:
  111. voltage = regval * -55;
  112. break;
  113. case LTC4245_GPIOADC1:
  114. case LTC4245_GPIOADC2:
  115. case LTC4245_GPIOADC3:
  116. voltage = regval * 10;
  117. break;
  118. default:
  119. /* If we get here, the developer messed up */
  120. WARN_ON_ONCE(1);
  121. break;
  122. }
  123. return voltage;
  124. }
  125. /* Return the current in the given sense register in milliAmperes */
  126. static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
  127. {
  128. struct ltc4245_data *data = ltc4245_update_device(dev);
  129. const u8 regval = data->vregs[reg - 0x10];
  130. unsigned int voltage;
  131. unsigned int curr;
  132. /* The strange looking conversions that follow are fixed-point
  133. * math, since we cannot do floating point in the kernel.
  134. *
  135. * Step 1: convert sense register to microVolts
  136. * Step 2: convert voltage to milliAmperes
  137. *
  138. * If you play around with the V=IR equation, you come up with
  139. * the following: X uV / Y mOhm == Z mA
  140. *
  141. * With the resistors that are fractions of a milliOhm, we multiply
  142. * the voltage and resistance by 10, to shift the decimal point.
  143. * Now we can use the normal division operator again.
  144. */
  145. switch (reg) {
  146. case LTC4245_12VSENSE:
  147. voltage = regval * 250; /* voltage in uV */
  148. curr = voltage / 50; /* sense resistor 50 mOhm */
  149. break;
  150. case LTC4245_5VSENSE:
  151. voltage = regval * 125; /* voltage in uV */
  152. curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
  153. break;
  154. case LTC4245_3VSENSE:
  155. voltage = regval * 125; /* voltage in uV */
  156. curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
  157. break;
  158. case LTC4245_VEESENSE:
  159. voltage = regval * 250; /* voltage in uV */
  160. curr = voltage / 100; /* sense resistor 100 mOhm */
  161. break;
  162. default:
  163. /* If we get here, the developer messed up */
  164. WARN_ON_ONCE(1);
  165. curr = 0;
  166. break;
  167. }
  168. return curr;
  169. }
  170. static ssize_t ltc4245_show_voltage(struct device *dev,
  171. struct device_attribute *da,
  172. char *buf)
  173. {
  174. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  175. const int voltage = ltc4245_get_voltage(dev, attr->index);
  176. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  177. }
  178. static ssize_t ltc4245_show_current(struct device *dev,
  179. struct device_attribute *da,
  180. char *buf)
  181. {
  182. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  183. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  184. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  185. }
  186. static ssize_t ltc4245_show_power(struct device *dev,
  187. struct device_attribute *da,
  188. char *buf)
  189. {
  190. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  191. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  192. const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
  193. /* current in mA * voltage in mV == power in uW */
  194. const unsigned int power = abs(output_voltage * curr);
  195. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  196. }
  197. static ssize_t ltc4245_show_alarm(struct device *dev,
  198. struct device_attribute *da,
  199. char *buf)
  200. {
  201. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  202. struct ltc4245_data *data = ltc4245_update_device(dev);
  203. const u8 reg = data->cregs[attr->index];
  204. const u32 mask = attr->nr;
  205. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  206. }
  207. /* These macros are used below in constructing device attribute objects
  208. * for use with sysfs_create_group() to make a sysfs device file
  209. * for each register.
  210. */
  211. #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
  212. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  213. ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
  214. #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
  215. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  216. ltc4245_show_current, NULL, ltc4245_cmd_idx)
  217. #define LTC4245_POWER(name, ltc4245_cmd_idx) \
  218. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  219. ltc4245_show_power, NULL, ltc4245_cmd_idx)
  220. #define LTC4245_ALARM(name, mask, reg) \
  221. static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
  222. ltc4245_show_alarm, NULL, (mask), reg)
  223. /* Construct a sensor_device_attribute structure for each register */
  224. /* Input voltages */
  225. LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
  226. LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
  227. LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
  228. LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
  229. /* Input undervoltage alarms */
  230. LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
  231. LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
  232. LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
  233. LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
  234. /* Currents (via sense resistor) */
  235. LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
  236. LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
  237. LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
  238. LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
  239. /* Overcurrent alarms */
  240. LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
  241. LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
  242. LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
  243. LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
  244. /* Output voltages */
  245. LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
  246. LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
  247. LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
  248. LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
  249. /* Power Bad alarms */
  250. LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
  251. LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
  252. LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
  253. LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
  254. /* GPIO voltages */
  255. LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC1);
  256. LTC4245_VOLTAGE(in10_input, LTC4245_GPIOADC2);
  257. LTC4245_VOLTAGE(in11_input, LTC4245_GPIOADC3);
  258. /* Power Consumption (virtual) */
  259. LTC4245_POWER(power1_input, LTC4245_12VSENSE);
  260. LTC4245_POWER(power2_input, LTC4245_5VSENSE);
  261. LTC4245_POWER(power3_input, LTC4245_3VSENSE);
  262. LTC4245_POWER(power4_input, LTC4245_VEESENSE);
  263. /* Finally, construct an array of pointers to members of the above objects,
  264. * as required for sysfs_create_group()
  265. */
  266. static struct attribute *ltc4245_attributes[] = {
  267. &sensor_dev_attr_in1_input.dev_attr.attr,
  268. &sensor_dev_attr_in2_input.dev_attr.attr,
  269. &sensor_dev_attr_in3_input.dev_attr.attr,
  270. &sensor_dev_attr_in4_input.dev_attr.attr,
  271. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  272. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  273. &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
  274. &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
  275. &sensor_dev_attr_curr1_input.dev_attr.attr,
  276. &sensor_dev_attr_curr2_input.dev_attr.attr,
  277. &sensor_dev_attr_curr3_input.dev_attr.attr,
  278. &sensor_dev_attr_curr4_input.dev_attr.attr,
  279. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  280. &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
  281. &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
  282. &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
  283. &sensor_dev_attr_in5_input.dev_attr.attr,
  284. &sensor_dev_attr_in6_input.dev_attr.attr,
  285. &sensor_dev_attr_in7_input.dev_attr.attr,
  286. &sensor_dev_attr_in8_input.dev_attr.attr,
  287. &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
  288. &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
  289. &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
  290. &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
  291. &sensor_dev_attr_in9_input.dev_attr.attr,
  292. &sensor_dev_attr_in10_input.dev_attr.attr,
  293. &sensor_dev_attr_in11_input.dev_attr.attr,
  294. &sensor_dev_attr_power1_input.dev_attr.attr,
  295. &sensor_dev_attr_power2_input.dev_attr.attr,
  296. &sensor_dev_attr_power3_input.dev_attr.attr,
  297. &sensor_dev_attr_power4_input.dev_attr.attr,
  298. NULL,
  299. };
  300. static const struct attribute_group ltc4245_group = {
  301. .attrs = ltc4245_attributes,
  302. };
  303. static int ltc4245_probe(struct i2c_client *client,
  304. const struct i2c_device_id *id)
  305. {
  306. struct i2c_adapter *adapter = client->adapter;
  307. struct ltc4245_data *data;
  308. int ret;
  309. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  310. return -ENODEV;
  311. data = kzalloc(sizeof(*data), GFP_KERNEL);
  312. if (!data) {
  313. ret = -ENOMEM;
  314. goto out_kzalloc;
  315. }
  316. i2c_set_clientdata(client, data);
  317. mutex_init(&data->update_lock);
  318. /* Initialize the LTC4245 chip */
  319. i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
  320. i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
  321. /* Register sysfs hooks */
  322. ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
  323. if (ret)
  324. goto out_sysfs_create_group;
  325. data->hwmon_dev = hwmon_device_register(&client->dev);
  326. if (IS_ERR(data->hwmon_dev)) {
  327. ret = PTR_ERR(data->hwmon_dev);
  328. goto out_hwmon_device_register;
  329. }
  330. return 0;
  331. out_hwmon_device_register:
  332. sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
  333. out_sysfs_create_group:
  334. kfree(data);
  335. out_kzalloc:
  336. return ret;
  337. }
  338. static int ltc4245_remove(struct i2c_client *client)
  339. {
  340. struct ltc4245_data *data = i2c_get_clientdata(client);
  341. hwmon_device_unregister(data->hwmon_dev);
  342. sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
  343. kfree(data);
  344. return 0;
  345. }
  346. static const struct i2c_device_id ltc4245_id[] = {
  347. { "ltc4245", 0 },
  348. { }
  349. };
  350. MODULE_DEVICE_TABLE(i2c, ltc4245_id);
  351. /* This is the driver that will be inserted */
  352. static struct i2c_driver ltc4245_driver = {
  353. .driver = {
  354. .name = "ltc4245",
  355. },
  356. .probe = ltc4245_probe,
  357. .remove = ltc4245_remove,
  358. .id_table = ltc4245_id,
  359. };
  360. static int __init ltc4245_init(void)
  361. {
  362. return i2c_add_driver(&ltc4245_driver);
  363. }
  364. static void __exit ltc4245_exit(void)
  365. {
  366. i2c_del_driver(&ltc4245_driver);
  367. }
  368. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  369. MODULE_DESCRIPTION("LTC4245 driver");
  370. MODULE_LICENSE("GPL");
  371. module_init(ltc4245_init);
  372. module_exit(ltc4245_exit);