max34440.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Hardware monitoring driver for Maxim MAX34440/MAX34441
  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. enum chips { max34440, max34441 };
  27. #define MAX34440_MFR_VOUT_PEAK 0xd4
  28. #define MAX34440_MFR_IOUT_PEAK 0xd5
  29. #define MAX34440_MFR_TEMPERATURE_PEAK 0xd6
  30. #define MAX34440_STATUS_OC_WARN (1 << 0)
  31. #define MAX34440_STATUS_OC_FAULT (1 << 1)
  32. #define MAX34440_STATUS_OT_FAULT (1 << 5)
  33. #define MAX34440_STATUS_OT_WARN (1 << 6)
  34. static int max34440_read_word_data(struct i2c_client *client, int page, int reg)
  35. {
  36. int ret;
  37. switch (reg) {
  38. case PMBUS_VIRT_READ_VOUT_MAX:
  39. ret = pmbus_read_word_data(client, page,
  40. MAX34440_MFR_VOUT_PEAK);
  41. break;
  42. case PMBUS_VIRT_READ_IOUT_MAX:
  43. ret = pmbus_read_word_data(client, page,
  44. MAX34440_MFR_IOUT_PEAK);
  45. break;
  46. case PMBUS_VIRT_READ_TEMP_MAX:
  47. ret = pmbus_read_word_data(client, page,
  48. MAX34440_MFR_TEMPERATURE_PEAK);
  49. break;
  50. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  51. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  52. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  53. ret = 0;
  54. break;
  55. default:
  56. ret = -ENODATA;
  57. break;
  58. }
  59. return ret;
  60. }
  61. static int max34440_write_word_data(struct i2c_client *client, int page,
  62. int reg, u16 word)
  63. {
  64. int ret;
  65. switch (reg) {
  66. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  67. ret = pmbus_write_word_data(client, page,
  68. MAX34440_MFR_VOUT_PEAK, 0);
  69. break;
  70. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  71. ret = pmbus_write_word_data(client, page,
  72. MAX34440_MFR_IOUT_PEAK, 0);
  73. break;
  74. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  75. ret = pmbus_write_word_data(client, page,
  76. MAX34440_MFR_TEMPERATURE_PEAK,
  77. 0xffff);
  78. break;
  79. default:
  80. ret = -ENODATA;
  81. break;
  82. }
  83. return ret;
  84. }
  85. static int max34440_read_byte_data(struct i2c_client *client, int page, int reg)
  86. {
  87. int ret;
  88. int mfg_status;
  89. ret = pmbus_set_page(client, page);
  90. if (ret < 0)
  91. return ret;
  92. switch (reg) {
  93. case PMBUS_STATUS_IOUT:
  94. mfg_status = pmbus_read_word_data(client, 0,
  95. PMBUS_STATUS_MFR_SPECIFIC);
  96. if (mfg_status < 0)
  97. return mfg_status;
  98. if (mfg_status & MAX34440_STATUS_OC_WARN)
  99. ret |= PB_IOUT_OC_WARNING;
  100. if (mfg_status & MAX34440_STATUS_OC_FAULT)
  101. ret |= PB_IOUT_OC_FAULT;
  102. break;
  103. case PMBUS_STATUS_TEMPERATURE:
  104. mfg_status = pmbus_read_word_data(client, 0,
  105. PMBUS_STATUS_MFR_SPECIFIC);
  106. if (mfg_status < 0)
  107. return mfg_status;
  108. if (mfg_status & MAX34440_STATUS_OT_WARN)
  109. ret |= PB_TEMP_OT_WARNING;
  110. if (mfg_status & MAX34440_STATUS_OT_FAULT)
  111. ret |= PB_TEMP_OT_FAULT;
  112. break;
  113. default:
  114. ret = -ENODATA;
  115. break;
  116. }
  117. return ret;
  118. }
  119. static struct pmbus_driver_info max34440_info[] = {
  120. [max34440] = {
  121. .pages = 14,
  122. .format[PSC_VOLTAGE_IN] = direct,
  123. .format[PSC_VOLTAGE_OUT] = direct,
  124. .format[PSC_TEMPERATURE] = direct,
  125. .format[PSC_CURRENT_OUT] = direct,
  126. .m[PSC_VOLTAGE_IN] = 1,
  127. .b[PSC_VOLTAGE_IN] = 0,
  128. .R[PSC_VOLTAGE_IN] = 3, /* R = 0 in datasheet reflects mV */
  129. .m[PSC_VOLTAGE_OUT] = 1,
  130. .b[PSC_VOLTAGE_OUT] = 0,
  131. .R[PSC_VOLTAGE_OUT] = 3, /* R = 0 in datasheet reflects mV */
  132. .m[PSC_CURRENT_OUT] = 1,
  133. .b[PSC_CURRENT_OUT] = 0,
  134. .R[PSC_CURRENT_OUT] = 3, /* R = 0 in datasheet reflects mA */
  135. .m[PSC_TEMPERATURE] = 1,
  136. .b[PSC_TEMPERATURE] = 0,
  137. .R[PSC_TEMPERATURE] = 2,
  138. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  139. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  140. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  141. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  142. .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  143. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  144. .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  145. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  146. .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  147. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  148. .func[5] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  149. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  150. .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  151. .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  152. .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  153. .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  154. .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  155. .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  156. .func[12] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  157. .func[13] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  158. .read_byte_data = max34440_read_byte_data,
  159. .read_word_data = max34440_read_word_data,
  160. .write_word_data = max34440_write_word_data,
  161. },
  162. [max34441] = {
  163. .pages = 12,
  164. .format[PSC_VOLTAGE_IN] = direct,
  165. .format[PSC_VOLTAGE_OUT] = direct,
  166. .format[PSC_TEMPERATURE] = direct,
  167. .format[PSC_CURRENT_OUT] = direct,
  168. .format[PSC_FAN] = direct,
  169. .m[PSC_VOLTAGE_IN] = 1,
  170. .b[PSC_VOLTAGE_IN] = 0,
  171. .R[PSC_VOLTAGE_IN] = 3,
  172. .m[PSC_VOLTAGE_OUT] = 1,
  173. .b[PSC_VOLTAGE_OUT] = 0,
  174. .R[PSC_VOLTAGE_OUT] = 3,
  175. .m[PSC_CURRENT_OUT] = 1,
  176. .b[PSC_CURRENT_OUT] = 0,
  177. .R[PSC_CURRENT_OUT] = 3,
  178. .m[PSC_TEMPERATURE] = 1,
  179. .b[PSC_TEMPERATURE] = 0,
  180. .R[PSC_TEMPERATURE] = 2,
  181. .m[PSC_FAN] = 1,
  182. .b[PSC_FAN] = 0,
  183. .R[PSC_FAN] = 0,
  184. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  185. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  186. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  187. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  188. .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  189. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  190. .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  191. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  192. .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  193. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  194. .func[5] = PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12,
  195. .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  196. .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  197. .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  198. .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  199. .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  200. .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  201. .read_byte_data = max34440_read_byte_data,
  202. .read_word_data = max34440_read_word_data,
  203. .write_word_data = max34440_write_word_data,
  204. },
  205. };
  206. static int max34440_probe(struct i2c_client *client,
  207. const struct i2c_device_id *id)
  208. {
  209. return pmbus_do_probe(client, id, &max34440_info[id->driver_data]);
  210. }
  211. static int max34440_remove(struct i2c_client *client)
  212. {
  213. return pmbus_do_remove(client);
  214. }
  215. static const struct i2c_device_id max34440_id[] = {
  216. {"max34440", max34440},
  217. {"max34441", max34441},
  218. {}
  219. };
  220. MODULE_DEVICE_TABLE(i2c, max34440_id);
  221. /* This is the driver that will be inserted */
  222. static struct i2c_driver max34440_driver = {
  223. .driver = {
  224. .name = "max34440",
  225. },
  226. .probe = max34440_probe,
  227. .remove = max34440_remove,
  228. .id_table = max34440_id,
  229. };
  230. static int __init max34440_init(void)
  231. {
  232. return i2c_add_driver(&max34440_driver);
  233. }
  234. static void __exit max34440_exit(void)
  235. {
  236. i2c_del_driver(&max34440_driver);
  237. }
  238. MODULE_AUTHOR("Guenter Roeck");
  239. MODULE_DESCRIPTION("PMBus driver for Maxim MAX34440/MAX34441");
  240. MODULE_LICENSE("GPL");
  241. module_init(max34440_init);
  242. module_exit(max34440_exit);