ltc3815.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Hardware monitoring driver for LTC3815
  3. *
  4. * Copyright (c) 2015 Linear Technology
  5. * Copyright (c) 2015 Guenter Roeck
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/err.h>
  18. #include <linux/i2c.h>
  19. #include <linux/init.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "pmbus.h"
  24. #define LTC3815_MFR_IOUT_PEAK 0xd7
  25. #define LTC3815_MFR_VOUT_PEAK 0xdd
  26. #define LTC3815_MFR_VIN_PEAK 0xde
  27. #define LTC3815_MFR_TEMP_PEAK 0xdf
  28. #define LTC3815_MFR_IIN_PEAK 0xe1
  29. #define LTC3815_MFR_SPECIAL_ID 0xe7
  30. #define LTC3815_ID 0x8000
  31. #define LTC3815_ID_MASK 0xff00
  32. static int ltc3815_read_byte_data(struct i2c_client *client, int page, int reg)
  33. {
  34. int ret;
  35. switch (reg) {
  36. case PMBUS_VOUT_MODE:
  37. /*
  38. * The chip returns 0x3e, suggesting VID mode with manufacturer
  39. * specific VID codes. Since the output voltage is reported
  40. * with a LSB of 0.5mV, override and report direct mode with
  41. * appropriate coefficients.
  42. */
  43. ret = 0x40;
  44. break;
  45. default:
  46. ret = -ENODATA;
  47. break;
  48. }
  49. return ret;
  50. }
  51. static int ltc3815_write_byte(struct i2c_client *client, int page, u8 reg)
  52. {
  53. int ret;
  54. switch (reg) {
  55. case PMBUS_CLEAR_FAULTS:
  56. /*
  57. * LTC3815 does not support the CLEAR_FAULTS command.
  58. * Emulate it by clearing the status register.
  59. */
  60. ret = pmbus_read_word_data(client, 0, PMBUS_STATUS_WORD);
  61. if (ret > 0) {
  62. pmbus_write_word_data(client, 0, PMBUS_STATUS_WORD,
  63. ret);
  64. ret = 0;
  65. }
  66. break;
  67. default:
  68. ret = -ENODATA;
  69. break;
  70. }
  71. return ret;
  72. }
  73. static int ltc3815_read_word_data(struct i2c_client *client, int page, int reg)
  74. {
  75. int ret;
  76. switch (reg) {
  77. case PMBUS_VIRT_READ_VIN_MAX:
  78. ret = pmbus_read_word_data(client, page, LTC3815_MFR_VIN_PEAK);
  79. break;
  80. case PMBUS_VIRT_READ_VOUT_MAX:
  81. ret = pmbus_read_word_data(client, page, LTC3815_MFR_VOUT_PEAK);
  82. break;
  83. case PMBUS_VIRT_READ_TEMP_MAX:
  84. ret = pmbus_read_word_data(client, page, LTC3815_MFR_TEMP_PEAK);
  85. break;
  86. case PMBUS_VIRT_READ_IOUT_MAX:
  87. ret = pmbus_read_word_data(client, page, LTC3815_MFR_IOUT_PEAK);
  88. break;
  89. case PMBUS_VIRT_READ_IIN_MAX:
  90. ret = pmbus_read_word_data(client, page, LTC3815_MFR_IIN_PEAK);
  91. break;
  92. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  93. case PMBUS_VIRT_RESET_VIN_HISTORY:
  94. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  95. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  96. case PMBUS_VIRT_RESET_IIN_HISTORY:
  97. ret = 0;
  98. break;
  99. default:
  100. ret = -ENODATA;
  101. break;
  102. }
  103. return ret;
  104. }
  105. static int ltc3815_write_word_data(struct i2c_client *client, int page,
  106. int reg, u16 word)
  107. {
  108. int ret;
  109. switch (reg) {
  110. case PMBUS_VIRT_RESET_IIN_HISTORY:
  111. ret = pmbus_write_word_data(client, page,
  112. LTC3815_MFR_IIN_PEAK, 0);
  113. break;
  114. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  115. ret = pmbus_write_word_data(client, page,
  116. LTC3815_MFR_IOUT_PEAK, 0);
  117. break;
  118. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  119. ret = pmbus_write_word_data(client, page,
  120. LTC3815_MFR_VOUT_PEAK, 0);
  121. break;
  122. case PMBUS_VIRT_RESET_VIN_HISTORY:
  123. ret = pmbus_write_word_data(client, page,
  124. LTC3815_MFR_VIN_PEAK, 0);
  125. break;
  126. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  127. ret = pmbus_write_word_data(client, page,
  128. LTC3815_MFR_TEMP_PEAK, 0);
  129. break;
  130. default:
  131. ret = -ENODATA;
  132. break;
  133. }
  134. return ret;
  135. }
  136. static const struct i2c_device_id ltc3815_id[] = {
  137. {"ltc3815", 0},
  138. { }
  139. };
  140. MODULE_DEVICE_TABLE(i2c, ltc3815_id);
  141. static struct pmbus_driver_info ltc3815_info = {
  142. .pages = 1,
  143. .format[PSC_VOLTAGE_IN] = direct,
  144. .format[PSC_VOLTAGE_OUT] = direct,
  145. .format[PSC_CURRENT_IN] = direct,
  146. .format[PSC_CURRENT_OUT] = direct,
  147. .format[PSC_TEMPERATURE] = direct,
  148. .m[PSC_VOLTAGE_IN] = 250,
  149. .b[PSC_VOLTAGE_IN] = 0,
  150. .R[PSC_VOLTAGE_IN] = 0,
  151. .m[PSC_VOLTAGE_OUT] = 2,
  152. .b[PSC_VOLTAGE_OUT] = 0,
  153. .R[PSC_VOLTAGE_OUT] = 3,
  154. .m[PSC_CURRENT_IN] = 1,
  155. .b[PSC_CURRENT_IN] = 0,
  156. .R[PSC_CURRENT_IN] = 2,
  157. .m[PSC_CURRENT_OUT] = 1,
  158. .b[PSC_CURRENT_OUT] = 0,
  159. .R[PSC_CURRENT_OUT] = 2,
  160. .m[PSC_TEMPERATURE] = 1,
  161. .b[PSC_TEMPERATURE] = 0,
  162. .R[PSC_TEMPERATURE] = 0,
  163. .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_VOUT |
  164. PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP,
  165. .read_byte_data = ltc3815_read_byte_data,
  166. .read_word_data = ltc3815_read_word_data,
  167. .write_byte = ltc3815_write_byte,
  168. .write_word_data = ltc3815_write_word_data,
  169. };
  170. static int ltc3815_probe(struct i2c_client *client,
  171. const struct i2c_device_id *id)
  172. {
  173. int chip_id;
  174. if (!i2c_check_functionality(client->adapter,
  175. I2C_FUNC_SMBUS_READ_WORD_DATA))
  176. return -ENODEV;
  177. chip_id = i2c_smbus_read_word_data(client, LTC3815_MFR_SPECIAL_ID);
  178. if (chip_id < 0)
  179. return chip_id;
  180. if ((chip_id & LTC3815_ID_MASK) != LTC3815_ID)
  181. return -ENODEV;
  182. return pmbus_do_probe(client, id, &ltc3815_info);
  183. }
  184. static struct i2c_driver ltc3815_driver = {
  185. .driver = {
  186. .name = "ltc3815",
  187. },
  188. .probe = ltc3815_probe,
  189. .remove = pmbus_do_remove,
  190. .id_table = ltc3815_id,
  191. };
  192. module_i2c_driver(ltc3815_driver);
  193. MODULE_AUTHOR("Guenter Roeck");
  194. MODULE_DESCRIPTION("PMBus driver for LTC3815");
  195. MODULE_LICENSE("GPL");