ltc4245.c 12 KB

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