ltc4245.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. /* Valid addresses are 0x20 - 0x3f
  24. *
  25. * For now, we do not probe, since some of these addresses
  26. * are known to be unfriendly to probing */
  27. static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
  28. /* Insmod parameters */
  29. I2C_CLIENT_INSMOD_1(ltc4245);
  30. /* Here are names of the chip's registers (a.k.a. commands) */
  31. enum ltc4245_cmd {
  32. LTC4245_STATUS = 0x00, /* readonly */
  33. LTC4245_ALERT = 0x01,
  34. LTC4245_CONTROL = 0x02,
  35. LTC4245_ON = 0x03,
  36. LTC4245_FAULT1 = 0x04,
  37. LTC4245_FAULT2 = 0x05,
  38. LTC4245_GPIO = 0x06,
  39. LTC4245_ADCADR = 0x07,
  40. LTC4245_12VIN = 0x10,
  41. LTC4245_12VSENSE = 0x11,
  42. LTC4245_12VOUT = 0x12,
  43. LTC4245_5VIN = 0x13,
  44. LTC4245_5VSENSE = 0x14,
  45. LTC4245_5VOUT = 0x15,
  46. LTC4245_3VIN = 0x16,
  47. LTC4245_3VSENSE = 0x17,
  48. LTC4245_3VOUT = 0x18,
  49. LTC4245_VEEIN = 0x19,
  50. LTC4245_VEESENSE = 0x1a,
  51. LTC4245_VEEOUT = 0x1b,
  52. LTC4245_GPIOADC1 = 0x1c,
  53. LTC4245_GPIOADC2 = 0x1d,
  54. LTC4245_GPIOADC3 = 0x1e,
  55. };
  56. struct ltc4245_data {
  57. struct device *hwmon_dev;
  58. struct mutex update_lock;
  59. bool valid;
  60. unsigned long last_updated; /* in jiffies */
  61. /* Control registers */
  62. u8 cregs[0x08];
  63. /* Voltage registers */
  64. u8 vregs[0x0f];
  65. };
  66. static struct ltc4245_data *ltc4245_update_device(struct device *dev)
  67. {
  68. struct i2c_client *client = to_i2c_client(dev);
  69. struct ltc4245_data *data = i2c_get_clientdata(client);
  70. s32 val;
  71. int i;
  72. mutex_lock(&data->update_lock);
  73. if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
  74. dev_dbg(&client->dev, "Starting ltc4245 update\n");
  75. /* Read control registers -- 0x00 to 0x07 */
  76. for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
  77. val = i2c_smbus_read_byte_data(client, i);
  78. if (unlikely(val < 0))
  79. data->cregs[i] = 0;
  80. else
  81. data->cregs[i] = val;
  82. }
  83. /* Read voltage registers -- 0x10 to 0x1f */
  84. for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
  85. val = i2c_smbus_read_byte_data(client, i+0x10);
  86. if (unlikely(val < 0))
  87. data->vregs[i] = 0;
  88. else
  89. data->vregs[i] = val;
  90. }
  91. data->last_updated = jiffies;
  92. data->valid = 1;
  93. }
  94. mutex_unlock(&data->update_lock);
  95. return data;
  96. }
  97. /* Return the voltage from the given register in millivolts */
  98. static int ltc4245_get_voltage(struct device *dev, u8 reg)
  99. {
  100. struct ltc4245_data *data = ltc4245_update_device(dev);
  101. const u8 regval = data->vregs[reg - 0x10];
  102. u32 voltage = 0;
  103. switch (reg) {
  104. case LTC4245_12VIN:
  105. case LTC4245_12VOUT:
  106. voltage = regval * 55;
  107. break;
  108. case LTC4245_5VIN:
  109. case LTC4245_5VOUT:
  110. voltage = regval * 22;
  111. break;
  112. case LTC4245_3VIN:
  113. case LTC4245_3VOUT:
  114. voltage = regval * 15;
  115. break;
  116. case LTC4245_VEEIN:
  117. case LTC4245_VEEOUT:
  118. voltage = regval * -55;
  119. break;
  120. case LTC4245_GPIOADC1:
  121. case LTC4245_GPIOADC2:
  122. case LTC4245_GPIOADC3:
  123. voltage = regval * 10;
  124. break;
  125. default:
  126. /* If we get here, the developer messed up */
  127. WARN_ON_ONCE(1);
  128. break;
  129. }
  130. return voltage;
  131. }
  132. /* Return the current in the given sense register in milliAmperes */
  133. static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
  134. {
  135. struct ltc4245_data *data = ltc4245_update_device(dev);
  136. const u8 regval = data->vregs[reg - 0x10];
  137. unsigned int voltage;
  138. unsigned int curr;
  139. /* The strange looking conversions that follow are fixed-point
  140. * math, since we cannot do floating point in the kernel.
  141. *
  142. * Step 1: convert sense register to microVolts
  143. * Step 2: convert voltage to milliAmperes
  144. *
  145. * If you play around with the V=IR equation, you come up with
  146. * the following: X uV / Y mOhm == Z mA
  147. *
  148. * With the resistors that are fractions of a milliOhm, we multiply
  149. * the voltage and resistance by 10, to shift the decimal point.
  150. * Now we can use the normal division operator again.
  151. */
  152. switch (reg) {
  153. case LTC4245_12VSENSE:
  154. voltage = regval * 250; /* voltage in uV */
  155. curr = voltage / 50; /* sense resistor 50 mOhm */
  156. break;
  157. case LTC4245_5VSENSE:
  158. voltage = regval * 125; /* voltage in uV */
  159. curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
  160. break;
  161. case LTC4245_3VSENSE:
  162. voltage = regval * 125; /* voltage in uV */
  163. curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
  164. break;
  165. case LTC4245_VEESENSE:
  166. voltage = regval * 250; /* voltage in uV */
  167. curr = voltage / 100; /* sense resistor 100 mOhm */
  168. break;
  169. default:
  170. /* If we get here, the developer messed up */
  171. WARN_ON_ONCE(1);
  172. curr = 0;
  173. break;
  174. }
  175. return curr;
  176. }
  177. static ssize_t ltc4245_show_voltage(struct device *dev,
  178. struct device_attribute *da,
  179. char *buf)
  180. {
  181. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  182. const int voltage = ltc4245_get_voltage(dev, attr->index);
  183. return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
  184. }
  185. static ssize_t ltc4245_show_current(struct device *dev,
  186. struct device_attribute *da,
  187. char *buf)
  188. {
  189. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  190. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  191. return snprintf(buf, PAGE_SIZE, "%u\n", curr);
  192. }
  193. static ssize_t ltc4245_show_power(struct device *dev,
  194. struct device_attribute *da,
  195. char *buf)
  196. {
  197. struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
  198. const unsigned int curr = ltc4245_get_current(dev, attr->index);
  199. const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
  200. /* current in mA * voltage in mV == power in uW */
  201. const unsigned int power = abs(output_voltage * curr);
  202. return snprintf(buf, PAGE_SIZE, "%u\n", power);
  203. }
  204. static ssize_t ltc4245_show_alarm(struct device *dev,
  205. struct device_attribute *da,
  206. char *buf)
  207. {
  208. struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
  209. struct ltc4245_data *data = ltc4245_update_device(dev);
  210. const u8 reg = data->cregs[attr->index];
  211. const u32 mask = attr->nr;
  212. return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
  213. }
  214. /* These macros are used below in constructing device attribute objects
  215. * for use with sysfs_create_group() to make a sysfs device file
  216. * for each register.
  217. */
  218. #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
  219. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  220. ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
  221. #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
  222. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  223. ltc4245_show_current, NULL, ltc4245_cmd_idx)
  224. #define LTC4245_POWER(name, ltc4245_cmd_idx) \
  225. static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
  226. ltc4245_show_power, NULL, ltc4245_cmd_idx)
  227. #define LTC4245_ALARM(name, mask, reg) \
  228. static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
  229. ltc4245_show_alarm, NULL, (mask), reg)
  230. /* Construct a sensor_device_attribute structure for each register */
  231. /* Input voltages */
  232. LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
  233. LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
  234. LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
  235. LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
  236. /* Input undervoltage alarms */
  237. LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
  238. LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
  239. LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
  240. LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
  241. /* Currents (via sense resistor) */
  242. LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
  243. LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
  244. LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
  245. LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
  246. /* Overcurrent alarms */
  247. LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
  248. LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
  249. LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
  250. LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
  251. /* Output voltages */
  252. LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
  253. LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
  254. LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
  255. LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
  256. /* Power Bad alarms */
  257. LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
  258. LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
  259. LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
  260. LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
  261. /* GPIO voltages */
  262. LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC1);
  263. LTC4245_VOLTAGE(in10_input, LTC4245_GPIOADC2);
  264. LTC4245_VOLTAGE(in11_input, LTC4245_GPIOADC3);
  265. /* Power Consumption (virtual) */
  266. LTC4245_POWER(power1_input, LTC4245_12VSENSE);
  267. LTC4245_POWER(power2_input, LTC4245_5VSENSE);
  268. LTC4245_POWER(power3_input, LTC4245_3VSENSE);
  269. LTC4245_POWER(power4_input, LTC4245_VEESENSE);
  270. /* Finally, construct an array of pointers to members of the above objects,
  271. * as required for sysfs_create_group()
  272. */
  273. static struct attribute *ltc4245_attributes[] = {
  274. &sensor_dev_attr_in1_input.dev_attr.attr,
  275. &sensor_dev_attr_in2_input.dev_attr.attr,
  276. &sensor_dev_attr_in3_input.dev_attr.attr,
  277. &sensor_dev_attr_in4_input.dev_attr.attr,
  278. &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
  279. &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
  280. &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
  281. &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
  282. &sensor_dev_attr_curr1_input.dev_attr.attr,
  283. &sensor_dev_attr_curr2_input.dev_attr.attr,
  284. &sensor_dev_attr_curr3_input.dev_attr.attr,
  285. &sensor_dev_attr_curr4_input.dev_attr.attr,
  286. &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
  287. &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
  288. &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
  289. &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
  290. &sensor_dev_attr_in5_input.dev_attr.attr,
  291. &sensor_dev_attr_in6_input.dev_attr.attr,
  292. &sensor_dev_attr_in7_input.dev_attr.attr,
  293. &sensor_dev_attr_in8_input.dev_attr.attr,
  294. &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
  295. &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
  296. &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
  297. &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
  298. &sensor_dev_attr_in9_input.dev_attr.attr,
  299. &sensor_dev_attr_in10_input.dev_attr.attr,
  300. &sensor_dev_attr_in11_input.dev_attr.attr,
  301. &sensor_dev_attr_power1_input.dev_attr.attr,
  302. &sensor_dev_attr_power2_input.dev_attr.attr,
  303. &sensor_dev_attr_power3_input.dev_attr.attr,
  304. &sensor_dev_attr_power4_input.dev_attr.attr,
  305. NULL,
  306. };
  307. static const struct attribute_group ltc4245_group = {
  308. .attrs = ltc4245_attributes,
  309. };
  310. static int ltc4245_probe(struct i2c_client *client,
  311. const struct i2c_device_id *id)
  312. {
  313. struct ltc4245_data *data;
  314. int ret;
  315. data = kzalloc(sizeof(*data), GFP_KERNEL);
  316. if (!data) {
  317. ret = -ENOMEM;
  318. goto out_kzalloc;
  319. }
  320. i2c_set_clientdata(client, data);
  321. mutex_init(&data->update_lock);
  322. /* Initialize the LTC4245 chip */
  323. /* TODO */
  324. /* Register sysfs hooks */
  325. ret = sysfs_create_group(&client->dev.kobj, &ltc4245_group);
  326. if (ret)
  327. goto out_sysfs_create_group;
  328. data->hwmon_dev = hwmon_device_register(&client->dev);
  329. if (IS_ERR(data->hwmon_dev)) {
  330. ret = PTR_ERR(data->hwmon_dev);
  331. goto out_hwmon_device_register;
  332. }
  333. return 0;
  334. out_hwmon_device_register:
  335. sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
  336. out_sysfs_create_group:
  337. kfree(data);
  338. out_kzalloc:
  339. return ret;
  340. }
  341. static int ltc4245_remove(struct i2c_client *client)
  342. {
  343. struct ltc4245_data *data = i2c_get_clientdata(client);
  344. hwmon_device_unregister(data->hwmon_dev);
  345. sysfs_remove_group(&client->dev.kobj, &ltc4245_group);
  346. kfree(data);
  347. return 0;
  348. }
  349. /* Check that some bits in a control register appear at all possible
  350. * locations without changing value
  351. *
  352. * @client: the i2c client to use
  353. * @reg: the register to read
  354. * @bits: the bits to check (0xff checks all bits,
  355. * 0x03 checks only the last two bits)
  356. *
  357. * return -ERRNO if the register read failed
  358. * return -ENODEV if the register value doesn't stay constant at all
  359. * possible addresses
  360. *
  361. * return 0 for success
  362. */
  363. static int ltc4245_check_control_reg(struct i2c_client *client, u8 reg, u8 bits)
  364. {
  365. int i;
  366. s32 v, voff1, voff2;
  367. /* Read register and check for error */
  368. v = i2c_smbus_read_byte_data(client, reg);
  369. if (v < 0)
  370. return v;
  371. v &= bits;
  372. for (i = 0x00; i < 0xff; i += 0x20) {
  373. voff1 = i2c_smbus_read_byte_data(client, reg + i);
  374. if (voff1 < 0)
  375. return voff1;
  376. voff2 = i2c_smbus_read_byte_data(client, reg + i + 0x08);
  377. if (voff2 < 0)
  378. return voff2;
  379. voff1 &= bits;
  380. voff2 &= bits;
  381. if (v != voff1 || v != voff2)
  382. return -ENODEV;
  383. }
  384. return 0;
  385. }
  386. static int ltc4245_detect(struct i2c_client *client,
  387. int kind,
  388. struct i2c_board_info *info)
  389. {
  390. struct i2c_adapter *adapter = client->adapter;
  391. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  392. return -ENODEV;
  393. if (kind < 0) { /* probed detection - check the chip type */
  394. s32 v; /* 8 bits from the chip, or -ERRNO */
  395. /* Chip registers 0x00-0x07 are control registers
  396. * Chip registers 0x10-0x1f are data registers
  397. *
  398. * Address bits b7-b5 are ignored. This makes the chip "repeat"
  399. * in steps of 0x20. Any control registers should appear with
  400. * the same values across all duplicated addresses.
  401. *
  402. * Register 0x02 bit b2 is reserved, expect 0
  403. * Register 0x07 bits b7 to b4 are reserved, expect 0
  404. *
  405. * Registers 0x01, 0x02 are control registers and should not
  406. * change on their own.
  407. *
  408. * Register 0x06 bits b6 and b7 are control bits, and should
  409. * not change on their own.
  410. *
  411. * Register 0x07 bits b3 to b0 are control bits, and should
  412. * not change on their own.
  413. */
  414. /* read register 0x02 reserved bit, expect 0 */
  415. v = i2c_smbus_read_byte_data(client, LTC4245_CONTROL);
  416. if (v < 0 || (v & 0x04) != 0)
  417. return -ENODEV;
  418. /* read register 0x07 reserved bits, expect 0 */
  419. v = i2c_smbus_read_byte_data(client, LTC4245_ADCADR);
  420. if (v < 0 || (v & 0xf0) != 0)
  421. return -ENODEV;
  422. /* check that the alert register appears at all locations */
  423. if (ltc4245_check_control_reg(client, LTC4245_ALERT, 0xff))
  424. return -ENODEV;
  425. /* check that the control register appears at all locations */
  426. if (ltc4245_check_control_reg(client, LTC4245_CONTROL, 0xff))
  427. return -ENODEV;
  428. /* check that register 0x06 bits b6 and b7 stay constant */
  429. if (ltc4245_check_control_reg(client, LTC4245_GPIO, 0xc0))
  430. return -ENODEV;
  431. /* check that register 0x07 bits b3-b0 stay constant */
  432. if (ltc4245_check_control_reg(client, LTC4245_ADCADR, 0x0f))
  433. return -ENODEV;
  434. }
  435. strlcpy(info->type, "ltc4245", I2C_NAME_SIZE);
  436. dev_info(&adapter->dev, "ltc4245 %s at address 0x%02x\n",
  437. kind < 0 ? "probed" : "forced",
  438. client->addr);
  439. return 0;
  440. }
  441. static const struct i2c_device_id ltc4245_id[] = {
  442. { "ltc4245", ltc4245 },
  443. { }
  444. };
  445. MODULE_DEVICE_TABLE(i2c, ltc4245_id);
  446. /* This is the driver that will be inserted */
  447. static struct i2c_driver ltc4245_driver = {
  448. .class = I2C_CLASS_HWMON,
  449. .driver = {
  450. .name = "ltc4245",
  451. },
  452. .probe = ltc4245_probe,
  453. .remove = ltc4245_remove,
  454. .id_table = ltc4245_id,
  455. .detect = ltc4245_detect,
  456. .address_data = &addr_data,
  457. };
  458. static int __init ltc4245_init(void)
  459. {
  460. return i2c_add_driver(&ltc4245_driver);
  461. }
  462. static void __exit ltc4245_exit(void)
  463. {
  464. i2c_del_driver(&ltc4245_driver);
  465. }
  466. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  467. MODULE_DESCRIPTION("LTC4245 driver");
  468. MODULE_LICENSE("GPL");
  469. module_init(ltc4245_init);
  470. module_exit(ltc4245_exit);