max8688.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/i2c.h>
  25. #include "pmbus.h"
  26. #define MAX8688_MFR_VOUT_PEAK 0xd4
  27. #define MAX8688_MFR_IOUT_PEAK 0xd5
  28. #define MAX8688_MFR_TEMPERATURE_PEAK 0xd6
  29. #define MAX8688_MFG_STATUS 0xd8
  30. #define MAX8688_STATUS_OC_FAULT (1 << 4)
  31. #define MAX8688_STATUS_OV_FAULT (1 << 5)
  32. #define MAX8688_STATUS_OV_WARNING (1 << 8)
  33. #define MAX8688_STATUS_UV_FAULT (1 << 9)
  34. #define MAX8688_STATUS_UV_WARNING (1 << 10)
  35. #define MAX8688_STATUS_UC_FAULT (1 << 11)
  36. #define MAX8688_STATUS_OC_WARNING (1 << 12)
  37. #define MAX8688_STATUS_OT_FAULT (1 << 13)
  38. #define MAX8688_STATUS_OT_WARNING (1 << 14)
  39. static int max8688_read_word_data(struct i2c_client *client, int page, int reg)
  40. {
  41. int ret;
  42. if (page)
  43. return -EINVAL;
  44. switch (reg) {
  45. case PMBUS_VIRT_READ_VOUT_MAX:
  46. ret = pmbus_read_word_data(client, 0, MAX8688_MFR_VOUT_PEAK);
  47. break;
  48. case PMBUS_VIRT_READ_IOUT_MAX:
  49. ret = pmbus_read_word_data(client, 0, MAX8688_MFR_IOUT_PEAK);
  50. break;
  51. case PMBUS_VIRT_READ_TEMP_MAX:
  52. ret = pmbus_read_word_data(client, 0,
  53. MAX8688_MFR_TEMPERATURE_PEAK);
  54. break;
  55. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  56. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  57. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  58. ret = 0;
  59. break;
  60. default:
  61. ret = -ENODATA;
  62. break;
  63. }
  64. return ret;
  65. }
  66. static int max8688_write_word_data(struct i2c_client *client, int page, int reg,
  67. u16 word)
  68. {
  69. int ret;
  70. switch (reg) {
  71. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  72. ret = pmbus_write_word_data(client, 0, MAX8688_MFR_VOUT_PEAK,
  73. 0);
  74. break;
  75. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  76. ret = pmbus_write_word_data(client, 0, MAX8688_MFR_IOUT_PEAK,
  77. 0);
  78. break;
  79. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  80. ret = pmbus_write_word_data(client, 0,
  81. MAX8688_MFR_TEMPERATURE_PEAK,
  82. 0xffff);
  83. break;
  84. default:
  85. ret = -ENODATA;
  86. break;
  87. }
  88. return ret;
  89. }
  90. static int max8688_read_byte_data(struct i2c_client *client, int page, int reg)
  91. {
  92. int ret = 0;
  93. int mfg_status;
  94. if (page)
  95. return -EINVAL;
  96. switch (reg) {
  97. case PMBUS_STATUS_VOUT:
  98. mfg_status = pmbus_read_word_data(client, 0,
  99. MAX8688_MFG_STATUS);
  100. if (mfg_status < 0)
  101. return mfg_status;
  102. if (mfg_status & MAX8688_STATUS_UV_WARNING)
  103. ret |= PB_VOLTAGE_UV_WARNING;
  104. if (mfg_status & MAX8688_STATUS_UV_FAULT)
  105. ret |= PB_VOLTAGE_UV_FAULT;
  106. if (mfg_status & MAX8688_STATUS_OV_WARNING)
  107. ret |= PB_VOLTAGE_OV_WARNING;
  108. if (mfg_status & MAX8688_STATUS_OV_FAULT)
  109. ret |= PB_VOLTAGE_OV_FAULT;
  110. break;
  111. case PMBUS_STATUS_IOUT:
  112. mfg_status = pmbus_read_word_data(client, 0,
  113. MAX8688_MFG_STATUS);
  114. if (mfg_status < 0)
  115. return mfg_status;
  116. if (mfg_status & MAX8688_STATUS_UC_FAULT)
  117. ret |= PB_IOUT_UC_FAULT;
  118. if (mfg_status & MAX8688_STATUS_OC_WARNING)
  119. ret |= PB_IOUT_OC_WARNING;
  120. if (mfg_status & MAX8688_STATUS_OC_FAULT)
  121. ret |= PB_IOUT_OC_FAULT;
  122. break;
  123. case PMBUS_STATUS_TEMPERATURE:
  124. mfg_status = pmbus_read_word_data(client, 0,
  125. MAX8688_MFG_STATUS);
  126. if (mfg_status < 0)
  127. return mfg_status;
  128. if (mfg_status & MAX8688_STATUS_OT_WARNING)
  129. ret |= PB_TEMP_OT_WARNING;
  130. if (mfg_status & MAX8688_STATUS_OT_FAULT)
  131. ret |= PB_TEMP_OT_FAULT;
  132. break;
  133. default:
  134. ret = -ENODATA;
  135. break;
  136. }
  137. return ret;
  138. }
  139. static struct pmbus_driver_info max8688_info = {
  140. .pages = 1,
  141. .format[PSC_VOLTAGE_IN] = direct,
  142. .format[PSC_VOLTAGE_OUT] = direct,
  143. .format[PSC_TEMPERATURE] = direct,
  144. .format[PSC_CURRENT_OUT] = direct,
  145. .m[PSC_VOLTAGE_IN] = 19995,
  146. .b[PSC_VOLTAGE_IN] = 0,
  147. .R[PSC_VOLTAGE_IN] = -1,
  148. .m[PSC_VOLTAGE_OUT] = 19995,
  149. .b[PSC_VOLTAGE_OUT] = 0,
  150. .R[PSC_VOLTAGE_OUT] = -1,
  151. .m[PSC_CURRENT_OUT] = 23109,
  152. .b[PSC_CURRENT_OUT] = 0,
  153. .R[PSC_CURRENT_OUT] = -2,
  154. .m[PSC_TEMPERATURE] = -7612,
  155. .b[PSC_TEMPERATURE] = 335,
  156. .R[PSC_TEMPERATURE] = -3,
  157. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_IOUT | PMBUS_HAVE_TEMP
  158. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_STATUS_IOUT
  159. | PMBUS_HAVE_STATUS_TEMP,
  160. .read_byte_data = max8688_read_byte_data,
  161. .read_word_data = max8688_read_word_data,
  162. .write_word_data = max8688_write_word_data,
  163. };
  164. static int max8688_probe(struct i2c_client *client,
  165. const struct i2c_device_id *id)
  166. {
  167. return pmbus_do_probe(client, id, &max8688_info);
  168. }
  169. static int max8688_remove(struct i2c_client *client)
  170. {
  171. return pmbus_do_remove(client);
  172. }
  173. static const struct i2c_device_id max8688_id[] = {
  174. {"max8688", 0},
  175. { }
  176. };
  177. MODULE_DEVICE_TABLE(i2c, max8688_id);
  178. /* This is the driver that will be inserted */
  179. static struct i2c_driver max8688_driver = {
  180. .driver = {
  181. .name = "max8688",
  182. },
  183. .probe = max8688_probe,
  184. .remove = max8688_remove,
  185. .id_table = max8688_id,
  186. };
  187. static int __init max8688_init(void)
  188. {
  189. return i2c_add_driver(&max8688_driver);
  190. }
  191. static void __exit max8688_exit(void)
  192. {
  193. i2c_del_driver(&max8688_driver);
  194. }
  195. MODULE_AUTHOR("Guenter Roeck");
  196. MODULE_DESCRIPTION("PMBus driver for Maxim MAX8688");
  197. MODULE_LICENSE("GPL");
  198. module_init(max8688_init);
  199. module_exit(max8688_exit);