max8688.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Hardware monitoring driver for Maxim MAX8688
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  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; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/bitops.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include <linux/i2c.h>
  26. #include "pmbus.h"
  27. #define MAX8688_MFR_VOUT_PEAK 0xd4
  28. #define MAX8688_MFR_IOUT_PEAK 0xd5
  29. #define MAX8688_MFR_TEMPERATURE_PEAK 0xd6
  30. #define MAX8688_MFG_STATUS 0xd8
  31. #define MAX8688_STATUS_OC_FAULT BIT(4)
  32. #define MAX8688_STATUS_OV_FAULT BIT(5)
  33. #define MAX8688_STATUS_OV_WARNING BIT(8)
  34. #define MAX8688_STATUS_UV_FAULT BIT(9)
  35. #define MAX8688_STATUS_UV_WARNING BIT(10)
  36. #define MAX8688_STATUS_UC_FAULT BIT(11)
  37. #define MAX8688_STATUS_OC_WARNING BIT(12)
  38. #define MAX8688_STATUS_OT_FAULT BIT(13)
  39. #define MAX8688_STATUS_OT_WARNING BIT(14)
  40. static int max8688_read_word_data(struct i2c_client *client, int page, int reg)
  41. {
  42. int ret;
  43. if (page > 0)
  44. return -ENXIO;
  45. switch (reg) {
  46. case PMBUS_VIRT_READ_VOUT_MAX:
  47. ret = pmbus_read_word_data(client, 0, MAX8688_MFR_VOUT_PEAK);
  48. break;
  49. case PMBUS_VIRT_READ_IOUT_MAX:
  50. ret = pmbus_read_word_data(client, 0, MAX8688_MFR_IOUT_PEAK);
  51. break;
  52. case PMBUS_VIRT_READ_TEMP_MAX:
  53. ret = pmbus_read_word_data(client, 0,
  54. MAX8688_MFR_TEMPERATURE_PEAK);
  55. break;
  56. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  57. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  58. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  59. ret = 0;
  60. break;
  61. default:
  62. ret = -ENODATA;
  63. break;
  64. }
  65. return ret;
  66. }
  67. static int max8688_write_word_data(struct i2c_client *client, int page, int reg,
  68. u16 word)
  69. {
  70. int ret;
  71. switch (reg) {
  72. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  73. ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK,
  74. 0);
  75. break;
  76. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  77. ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK,
  78. 0);
  79. break;
  80. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  81. ret = pmbus_write_word_data(client, 0,
  82. MAX8688_MFR_TEMPERATURE_PEAK,
  83. 0xffff);
  84. break;
  85. default:
  86. ret = -ENODATA;
  87. break;
  88. }
  89. return ret;
  90. }
  91. static int max8688_read_byte_data(struct i2c_client *client, int page, int reg)
  92. {
  93. int ret = 0;
  94. int mfg_status;
  95. if (page > 0)
  96. return -ENXIO;
  97. switch (reg) {
  98. case PMBUS_STATUS_VOUT:
  99. mfg_status = pmbus_read_word_data(client, 0,
  100. MAX8688_MFG_STATUS);
  101. if (mfg_status < 0)
  102. return mfg_status;
  103. if (mfg_status & MAX8688_STATUS_UV_WARNING)
  104. ret |= PB_VOLTAGE_UV_WARNING;
  105. if (mfg_status & MAX8688_STATUS_UV_FAULT)
  106. ret |= PB_VOLTAGE_UV_FAULT;
  107. if (mfg_status & MAX8688_STATUS_OV_WARNING)
  108. ret |= PB_VOLTAGE_OV_WARNING;
  109. if (mfg_status & MAX8688_STATUS_OV_FAULT)
  110. ret |= PB_VOLTAGE_OV_FAULT;
  111. break;
  112. case PMBUS_STATUS_IOUT:
  113. mfg_status = pmbus_read_word_data(client, 0,
  114. MAX8688_MFG_STATUS);
  115. if (mfg_status < 0)
  116. return mfg_status;
  117. if (mfg_status & MAX8688_STATUS_UC_FAULT)
  118. ret |= PB_IOUT_UC_FAULT;
  119. if (mfg_status & MAX8688_STATUS_OC_WARNING)
  120. ret |= PB_IOUT_OC_WARNING;
  121. if (mfg_status & MAX8688_STATUS_OC_FAULT)
  122. ret |= PB_IOUT_OC_FAULT;
  123. break;
  124. case PMBUS_STATUS_TEMPERATURE:
  125. mfg_status = pmbus_read_word_data(client, 0,
  126. MAX8688_MFG_STATUS);
  127. if (mfg_status < 0)
  128. return mfg_status;
  129. if (mfg_status & MAX8688_STATUS_OT_WARNING)
  130. ret |= PB_TEMP_OT_WARNING;
  131. if (mfg_status & MAX8688_STATUS_OT_FAULT)
  132. ret |= PB_TEMP_OT_FAULT;
  133. break;
  134. default:
  135. ret = -ENODATA;
  136. break;
  137. }
  138. return ret;
  139. }
  140. static struct pmbus_driver_info max8688_info = {
  141. .pages = 1,
  142. .format[PSC_VOLTAGE_IN] = direct,
  143. .format[PSC_VOLTAGE_OUT] = direct,
  144. .format[PSC_TEMPERATURE] = direct,
  145. .format[PSC_CURRENT_OUT] = direct,
  146. .m[PSC_VOLTAGE_IN] = 19995,
  147. .b[PSC_VOLTAGE_IN] = 0,
  148. .R[PSC_VOLTAGE_IN] = -1,
  149. .m[PSC_VOLTAGE_OUT] = 19995,
  150. .b[PSC_VOLTAGE_OUT] = 0,
  151. .R[PSC_VOLTAGE_OUT] = -1,
  152. .m[PSC_CURRENT_OUT] = 23109,
  153. .b[PSC_CURRENT_OUT] = 0,
  154. .R[PSC_CURRENT_OUT] = -2,
  155. .m[PSC_TEMPERATURE] = -7612,
  156. .b[PSC_TEMPERATURE] = 335,
  157. .R[PSC_TEMPERATURE] = -3,
  158. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
  159. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
  160. | PMBUS_HAVE_STATUS_TEMP,
  161. .read_byte_data = max8688_read_byte_data,
  162. .read_word_data = max8688_read_word_data,
  163. .write_word_data = max8688_write_word_data,
  164. };
  165. static int max8688_probe(struct i2c_client *client,
  166. const struct i2c_device_id *id)
  167. {
  168. return pmbus_do_probe(client, id, &max8688_info);
  169. }
  170. static const struct i2c_device_id max8688_id[] = {
  171. {"max8688", 0},
  172. { }
  173. };
  174. MODULE_DEVICE_TABLE(i2c, max8688_id);
  175. /* This is the driver that will be inserted */
  176. static struct i2c_driver max8688_driver = {
  177. .driver = {
  178. .name = "max8688",
  179. },
  180. .probe = max8688_probe,
  181. .remove = pmbus_do_remove,
  182. .id_table = max8688_id,
  183. };
  184. module_i2c_driver(max8688_driver);
  185. MODULE_AUTHOR("Guenter Roeck");
  186. MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
  187. MODULE_LICENSE("GPL");