ltc4215.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
  3. *
  4. * Copyright (C) 2009 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. * Datasheet:
  11. * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/init.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. static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
  22. /* Insmod parameters */
  23. I2C_CLIENT_INSMOD_1(ltc4215);
  24. /* Here are names of the chip's registers (a.k.a. commands) */
  25. enum ltc4215_cmd {
  26. LTC4215_CONTROL = 0x00, /* rw */
  27. LTC4215_ALERT = 0x01, /* rw */
  28. LTC4215_STATUS = 0x02, /* ro */
  29. LTC4215_FAULT = 0x03, /* rw */
  30. LTC4215_SENSE = 0x04, /* rw */
  31. LTC4215_SOURCE = 0x05, /* rw */
  32. LTC4215_ADIN = 0x06, /* rw */
  33. };
  34. struct ltc4215_data {
  35. struct device *hwmon_dev;
  36. struct mutex update_lock;
  37. bool valid;
  38. unsigned long last_updated; /* in jiffies */
  39. /* Registers */
  40. u8 regs[7];
  41. };
  42. static struct ltc4215_data *ltc4215_update_device(struct device *dev)
  43. {
  44. struct i2c_client *client = to_i2c_client(dev);
  45. struct ltc4215_data *data = i2c_get_clientdata(client);
  46. s32 val;
  47. int i;
  48. mutex_lock(&data->update_lock);
  49. /* The chip's A/D updates 10 times per second */
  50. if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
  51. dev_dbg(&client->dev, "Starting ltc4215 update\n");
  52. /* Read all registers */
  53. for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
  54. val = i2c_smbus_read_byte_data(client, i);
  55. if (unlikely(val < 0))
  56. data->regs[i] = 0;
  57. else
  58. data->regs[i] = val;
  59. }
  60. data->last_updated = jiffies;
  61. data->valid = 1;
  62. }
  63. mutex_unlock(&data->update_lock);
  64. return data;
  65. }
  66. /* Return the voltage from the given register in millivolts */
  67. static int ltc4215_get_voltage(struct device *dev, u8 reg)
  68. {
  69. struct ltc4215_data *data = ltc4215_update_device(dev);
  70. const u8 regval = data->regs[reg];
  71. u32 voltage = 0;
  72. switch (reg) {
  73. case LTC4215_SENSE:
  74. /* 151 uV per increment */
  75. voltage = regval * 151 / 1000;
  76. break;
  77. case LTC4215_SOURCE:
  78. /* 60.5 mV per increment */
  79. voltage = regval * 605 / 10;
  80. break;
  81. case LTC4215_ADIN:
  82. /* The ADIN input is divided by 12.5, and has 4.82 mV
  83. * per increment, so we have the additional multiply */
  84. voltage = regval * 482 * 125 / 1000;
  85. break;
  86. default:
  87. /* If we get here, the developer messed up */
  88. WARN_ON_ONCE(1);
  89. break;
  90. }
  91. return voltage;
  92. }
  93. /* Return the current from the sense resistor in mA */
  94. static unsigned int ltc4215_get_current(struct device *dev)
  95. {
  96. struct ltc4215_data *data = ltc4215_update_device(dev);
  97. /* The strange looking conversions that follow are fixed-point
  98. * math, since we cannot do floating point in the kernel.
  99. *
  100. * Step 1: convert sense register to microVolts
  101. * Step 2: convert voltage to milliAmperes
  102. *
  103. * If you play around with the V=IR equation, you come up with
  104. * the following: X uV / Y mOhm == Z mA
  105. *
  106. * With the resistors that are fractions of a milliOhm, we multiply
  107. * the voltage and resistance by 10, to shift the decimal point.
  108. * Now we can use the normal division operator again.
  109. */
  110. /* Calculate voltage in microVolts (151 uV per increment) */
  111. const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
  112. /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
  113. const unsigned int curr = voltage / 4;
  114. return curr;
  115. }
  116. static ssize_t ltc4215_show_voltage(struct device *dev,
  117. struct device_attribute *da,
  118. char *buf)
  119. {
  120. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  121. const int voltage = ltc4215_get_voltage(dev, attr->index);
  122. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  123. }
  124. static ssize_t ltc4215_show_current(struct device *dev,
  125. struct device_attribute *da,
  126. char *buf)
  127. {
  128. const unsigned int curr = ltc4215_get_current(dev);
  129. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  130. }
  131. static ssize_t ltc4215_show_power(struct device *dev,
  132. struct device_attribute *da,
  133. char *buf)
  134. {
  135. const unsigned int curr = ltc4215_get_current(dev);
  136. const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
  137. /* current in mA * voltage in mV == power in uW */
  138. const unsigned int power = abs(output_voltage * curr);
  139. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  140. }
  141. static ssize_t ltc4215_show_alarm(struct device *dev,
  142. struct device_attribute *da,
  143. char *buf)
  144. {
  145. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  146. struct ltc4215_data *data = ltc4215_update_device(dev);
  147. const u8 reg = data->regs[attr->index];
  148. const u32 mask = attr->nr;
  149. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  150. }
  151. /* These macros are used below in constructing device attribute objects
  152. * for use with sysfs_create_group() to make a sysfs device file
  153. * for each register.
  154. */
  155. #define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
  156. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  157. ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
  158. #define LTC4215_CURRENT(name) \
  159. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  160. ltc4215_show_current, NULL, 0);
  161. #define LTC4215_POWER(name) \
  162. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  163. ltc4215_show_power, NULL, 0);
  164. #define LTC4215_ALARM(name, mask, reg) \
  165. static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
  166. ltc4215_show_alarm, NULL, (mask), reg)
  167. /* Construct a sensor_device_attribute structure for each register */
  168. /* Current */
  169. LTC4215_CURRENT(curr1_input);
  170. LTC4215_ALARM(curr1_max_alarm, (1 << 2), LTC4215_STATUS);
  171. /* Power (virtual) */
  172. LTC4215_POWER(power1_input);
  173. LTC4215_ALARM(power1_alarm, (1 << 3), LTC4215_STATUS);
  174. /* Input Voltage */
  175. LTC4215_VOLTAGE(in1_input, LTC4215_ADIN);
  176. LTC4215_ALARM(in1_max_alarm, (1 << 0), LTC4215_STATUS);
  177. LTC4215_ALARM(in1_min_alarm, (1 << 1), LTC4215_STATUS);
  178. /* Output Voltage */
  179. LTC4215_VOLTAGE(in2_input, LTC4215_SOURCE);
  180. /* Finally, construct an array of pointers to members of the above objects,
  181. * as required for sysfs_create_group()
  182. */
  183. static struct attribute *ltc4215_attributes[] = {
  184. &sensor_dev_attr_curr1_input.dev_attr.attr,
  185. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  186. &sensor_dev_attr_power1_input.dev_attr.attr,
  187. &sensor_dev_attr_power1_alarm.dev_attr.attr,
  188. &sensor_dev_attr_in1_input.dev_attr.attr,
  189. &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
  190. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  191. &sensor_dev_attr_in2_input.dev_attr.attr,
  192. NULL,
  193. };
  194. static const struct attribute_group ltc4215_group = {
  195. .attrs = ltc4215_attributes,
  196. };
  197. static int ltc4215_probe(struct i2c_client *client,
  198. const struct i2c_device_id *id)
  199. {
  200. struct ltc4215_data *data;
  201. int ret;
  202. data = kzalloc(sizeof(*data), GFP_KERNEL);
  203. if (!data) {
  204. ret = -ENOMEM;
  205. goto out_kzalloc;
  206. }
  207. i2c_set_clientdata(client, data);
  208. mutex_init(&data->update_lock);
  209. /* Initialize the LTC4215 chip */
  210. /* TODO */
  211. /* Register sysfs hooks */
  212. ret = sysfs_create_group(&client->dev.kobj, &ltc4215_group);
  213. if (ret)
  214. goto out_sysfs_create_group;
  215. data->hwmon_dev = hwmon_device_register(&client->dev);
  216. if (IS_ERR(data->hwmon_dev)) {
  217. ret = PTR_ERR(data->hwmon_dev);
  218. goto out_hwmon_device_register;
  219. }
  220. return 0;
  221. out_hwmon_device_register:
  222. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  223. out_sysfs_create_group:
  224. kfree(data);
  225. out_kzalloc:
  226. return ret;
  227. }
  228. static int ltc4215_remove(struct i2c_client *client)
  229. {
  230. struct ltc4215_data *data = i2c_get_clientdata(client);
  231. hwmon_device_unregister(data->hwmon_dev);
  232. sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
  233. kfree(data);
  234. return 0;
  235. }
  236. static int ltc4215_detect(struct i2c_client *client,
  237. int kind,
  238. struct i2c_board_info *info)
  239. {
  240. struct i2c_adapter *adapter = client->adapter;
  241. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  242. return -ENODEV;
  243. if (kind < 0) { /* probed detection - check the chip type */
  244. s32 v; /* 8 bits from the chip, or -ERRNO */
  245. /*
  246. * Register 0x01 bit b7 is reserved, expect 0
  247. * Register 0x03 bit b6 and b7 are reserved, expect 0
  248. */
  249. v = i2c_smbus_read_byte_data(client, LTC4215_ALERT);
  250. if (v < 0 || (v & (1 << 7)) != 0)
  251. return -ENODEV;
  252. v = i2c_smbus_read_byte_data(client, LTC4215_FAULT);
  253. if (v < 0 || (v & ((1 << 6) | (1 << 7))) != 0)
  254. return -ENODEV;
  255. }
  256. strlcpy(info->type, "ltc4215", I2C_NAME_SIZE);
  257. dev_info(&adapter->dev, "ltc4215 %s at address 0x%02x\n",
  258. kind < 0 ? "probed" : "forced",
  259. client->addr);
  260. return 0;
  261. }
  262. static const struct i2c_device_id ltc4215_id[] = {
  263. { "ltc4215", ltc4215 },
  264. { }
  265. };
  266. MODULE_DEVICE_TABLE(i2c, ltc4215_id);
  267. /* This is the driver that will be inserted */
  268. static struct i2c_driver ltc4215_driver = {
  269. .class = I2C_CLASS_HWMON,
  270. .driver = {
  271. .name = "ltc4215",
  272. },
  273. .probe = ltc4215_probe,
  274. .remove = ltc4215_remove,
  275. .id_table = ltc4215_id,
  276. .detect = ltc4215_detect,
  277. .address_data = &addr_data,
  278. };
  279. static int __init ltc4215_init(void)
  280. {
  281. return i2c_add_driver(&ltc4215_driver);
  282. }
  283. static void __exit ltc4215_exit(void)
  284. {
  285. i2c_del_driver(&ltc4215_driver);
  286. }
  287. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  288. MODULE_DESCRIPTION("LTC4215 driver");
  289. MODULE_LICENSE("GPL");
  290. module_init(ltc4215_init);
  291. module_exit(ltc4215_exit);