max34440.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 = 0;
  88. int mfg_status;
  89. if (page >= 0) {
  90. ret = pmbus_set_page(client, page);
  91. if (ret < 0)
  92. return ret;
  93. }
  94. switch (reg) {
  95. case PMBUS_STATUS_IOUT:
  96. mfg_status = pmbus_read_word_data(client, 0,
  97. PMBUS_STATUS_MFR_SPECIFIC);
  98. if (mfg_status < 0)
  99. return mfg_status;
  100. if (mfg_status & MAX34440_STATUS_OC_WARN)
  101. ret |= PB_IOUT_OC_WARNING;
  102. if (mfg_status & MAX34440_STATUS_OC_FAULT)
  103. ret |= PB_IOUT_OC_FAULT;
  104. break;
  105. case PMBUS_STATUS_TEMPERATURE:
  106. mfg_status = pmbus_read_word_data(client, 0,
  107. PMBUS_STATUS_MFR_SPECIFIC);
  108. if (mfg_status < 0)
  109. return mfg_status;
  110. if (mfg_status & MAX34440_STATUS_OT_WARN)
  111. ret |= PB_TEMP_OT_WARNING;
  112. if (mfg_status & MAX34440_STATUS_OT_FAULT)
  113. ret |= PB_TEMP_OT_FAULT;
  114. break;
  115. default:
  116. ret = -ENODATA;
  117. break;
  118. }
  119. return ret;
  120. }
  121. static struct pmbus_driver_info max34440_info[] = {
  122. [max34440] = {
  123. .pages = 14,
  124. .format[PSC_VOLTAGE_IN] = direct,
  125. .format[PSC_VOLTAGE_OUT] = direct,
  126. .format[PSC_TEMPERATURE] = direct,
  127. .format[PSC_CURRENT_OUT] = direct,
  128. .m[PSC_VOLTAGE_IN] = 1,
  129. .b[PSC_VOLTAGE_IN] = 0,
  130. .R[PSC_VOLTAGE_IN] = 3, /* R = 0 in datasheet reflects mV */
  131. .m[PSC_VOLTAGE_OUT] = 1,
  132. .b[PSC_VOLTAGE_OUT] = 0,
  133. .R[PSC_VOLTAGE_OUT] = 3, /* R = 0 in datasheet reflects mV */
  134. .m[PSC_CURRENT_OUT] = 1,
  135. .b[PSC_CURRENT_OUT] = 0,
  136. .R[PSC_CURRENT_OUT] = 3, /* R = 0 in datasheet reflects mA */
  137. .m[PSC_TEMPERATURE] = 1,
  138. .b[PSC_TEMPERATURE] = 0,
  139. .R[PSC_TEMPERATURE] = 2,
  140. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  141. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  142. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  143. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  144. .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  145. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  146. .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  147. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  148. .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  149. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  150. .func[5] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  151. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  152. .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  153. .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  154. .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  155. .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  156. .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  157. .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  158. .func[12] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  159. .func[13] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  160. .read_byte_data = max34440_read_byte_data,
  161. .read_word_data = max34440_read_word_data,
  162. .write_word_data = max34440_write_word_data,
  163. },
  164. [max34441] = {
  165. .pages = 12,
  166. .format[PSC_VOLTAGE_IN] = direct,
  167. .format[PSC_VOLTAGE_OUT] = direct,
  168. .format[PSC_TEMPERATURE] = direct,
  169. .format[PSC_CURRENT_OUT] = direct,
  170. .format[PSC_FAN] = direct,
  171. .m[PSC_VOLTAGE_IN] = 1,
  172. .b[PSC_VOLTAGE_IN] = 0,
  173. .R[PSC_VOLTAGE_IN] = 3,
  174. .m[PSC_VOLTAGE_OUT] = 1,
  175. .b[PSC_VOLTAGE_OUT] = 0,
  176. .R[PSC_VOLTAGE_OUT] = 3,
  177. .m[PSC_CURRENT_OUT] = 1,
  178. .b[PSC_CURRENT_OUT] = 0,
  179. .R[PSC_CURRENT_OUT] = 3,
  180. .m[PSC_TEMPERATURE] = 1,
  181. .b[PSC_TEMPERATURE] = 0,
  182. .R[PSC_TEMPERATURE] = 2,
  183. .m[PSC_FAN] = 1,
  184. .b[PSC_FAN] = 0,
  185. .R[PSC_FAN] = 0,
  186. .func[0] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  187. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  188. .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  189. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  190. .func[2] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  191. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  192. .func[3] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  193. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  194. .func[4] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  195. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT,
  196. .func[5] = PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12,
  197. .func[6] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  198. .func[7] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  199. .func[8] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  200. .func[9] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  201. .func[10] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  202. .func[11] = PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP,
  203. .read_byte_data = max34440_read_byte_data,
  204. .read_word_data = max34440_read_word_data,
  205. .write_word_data = max34440_write_word_data,
  206. },
  207. };
  208. static int max34440_probe(struct i2c_client *client,
  209. const struct i2c_device_id *id)
  210. {
  211. return pmbus_do_probe(client, id, &max34440_info[id->driver_data]);
  212. }
  213. static int max34440_remove(struct i2c_client *client)
  214. {
  215. pmbus_do_remove(client);
  216. return 0;
  217. }
  218. static const struct i2c_device_id max34440_id[] = {
  219. {"max34440", max34440},
  220. {"max34441", max34441},
  221. {}
  222. };
  223. MODULE_DEVICE_TABLE(i2c, max34440_id);
  224. /* This is the driver that will be inserted */
  225. static struct i2c_driver max34440_driver = {
  226. .driver = {
  227. .name = "max34440",
  228. },
  229. .probe = max34440_probe,
  230. .remove = max34440_remove,
  231. .id_table = max34440_id,
  232. };
  233. static int __init max34440_init(void)
  234. {
  235. return i2c_add_driver(&max34440_driver);
  236. }
  237. static void __exit max34440_exit(void)
  238. {
  239. i2c_del_driver(&max34440_driver);
  240. }
  241. MODULE_AUTHOR("Guenter Roeck");
  242. MODULE_DESCRIPTION("PMBus driver for Maxim MAX34440/MAX34441");
  243. MODULE_LICENSE("GPL");
  244. module_init(max34440_init);
  245. module_exit(max34440_exit);