pmbus.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Hardware monitoring driver for PMBus devices
  3. *
  4. * Copyright (c) 2010, 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/slab.h>
  25. #include <linux/mutex.h>
  26. #include <linux/i2c.h>
  27. #include <linux/pmbus.h>
  28. #include "pmbus.h"
  29. /*
  30. * Find sensor groups and status registers on each page.
  31. */
  32. static void pmbus_find_sensor_groups(struct i2c_client *client,
  33. struct pmbus_driver_info *info)
  34. {
  35. int page;
  36. /* Sensors detected on page 0 only */
  37. if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
  38. info->func[0] |= PMBUS_HAVE_VIN;
  39. if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
  40. info->func[0] |= PMBUS_HAVE_VCAP;
  41. if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
  42. info->func[0] |= PMBUS_HAVE_IIN;
  43. if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
  44. info->func[0] |= PMBUS_HAVE_PIN;
  45. if (info->func[0]
  46. && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
  47. info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
  48. if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
  49. pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
  50. info->func[0] |= PMBUS_HAVE_FAN12;
  51. if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
  52. info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
  53. }
  54. if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
  55. pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
  56. info->func[0] |= PMBUS_HAVE_FAN34;
  57. if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
  58. info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
  59. }
  60. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
  61. info->func[0] |= PMBUS_HAVE_TEMP;
  62. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
  63. info->func[0] |= PMBUS_HAVE_TEMP2;
  64. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
  65. info->func[0] |= PMBUS_HAVE_TEMP3;
  66. if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
  67. | PMBUS_HAVE_TEMP3)
  68. && pmbus_check_byte_register(client, 0,
  69. PMBUS_STATUS_TEMPERATURE))
  70. info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
  71. /* Sensors detected on all pages */
  72. for (page = 0; page < info->pages; page++) {
  73. if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
  74. info->func[page] |= PMBUS_HAVE_VOUT;
  75. if (pmbus_check_byte_register(client, page,
  76. PMBUS_STATUS_VOUT))
  77. info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
  78. }
  79. if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
  80. info->func[page] |= PMBUS_HAVE_IOUT;
  81. if (pmbus_check_byte_register(client, 0,
  82. PMBUS_STATUS_IOUT))
  83. info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
  84. }
  85. if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
  86. info->func[page] |= PMBUS_HAVE_POUT;
  87. }
  88. }
  89. /*
  90. * Identify chip parameters.
  91. */
  92. static int pmbus_identify(struct i2c_client *client,
  93. struct pmbus_driver_info *info)
  94. {
  95. int ret = 0;
  96. if (!info->pages) {
  97. /*
  98. * Check if the PAGE command is supported. If it is,
  99. * keep setting the page number until it fails or until the
  100. * maximum number of pages has been reached. Assume that
  101. * this is the number of pages supported by the chip.
  102. */
  103. if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
  104. int page;
  105. for (page = 1; page < PMBUS_PAGES; page++) {
  106. if (pmbus_set_page(client, page) < 0)
  107. break;
  108. }
  109. pmbus_set_page(client, 0);
  110. info->pages = page;
  111. } else {
  112. info->pages = 1;
  113. }
  114. pmbus_clear_faults(client);
  115. }
  116. if (pmbus_check_byte_register(client, 0, PMBUS_VOUT_MODE)) {
  117. int vout_mode;
  118. vout_mode = pmbus_read_byte_data(client, 0, PMBUS_VOUT_MODE);
  119. if (vout_mode >= 0 && vout_mode != 0xff) {
  120. switch (vout_mode >> 5) {
  121. case 0:
  122. break;
  123. case 1:
  124. info->format[PSC_VOLTAGE_OUT] = vid;
  125. info->vrm_version = vr11;
  126. break;
  127. case 2:
  128. info->format[PSC_VOLTAGE_OUT] = direct;
  129. break;
  130. default:
  131. ret = -ENODEV;
  132. goto abort;
  133. }
  134. }
  135. }
  136. /*
  137. * We should check if the COEFFICIENTS register is supported.
  138. * If it is, and the chip is configured for direct mode, we can read
  139. * the coefficients from the chip, one set per group of sensor
  140. * registers.
  141. *
  142. * To do this, we will need access to a chip which actually supports the
  143. * COEFFICIENTS command, since the command is too complex to implement
  144. * without testing it. Until then, abort if a chip configured for direct
  145. * mode was detected.
  146. */
  147. if (info->format[PSC_VOLTAGE_OUT] == direct) {
  148. ret = -ENODEV;
  149. goto abort;
  150. }
  151. /* Try to find sensor groups */
  152. pmbus_find_sensor_groups(client, info);
  153. abort:
  154. return ret;
  155. }
  156. static int pmbus_probe(struct i2c_client *client,
  157. const struct i2c_device_id *id)
  158. {
  159. struct pmbus_driver_info *info;
  160. struct pmbus_platform_data *pdata = NULL;
  161. struct device *dev = &client->dev;
  162. info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
  163. if (!info)
  164. return -ENOMEM;
  165. if (!strcmp(id->name, "dps460") || !strcmp(id->name, "dps800") ||
  166. !strcmp(id->name, "sgd009")) {
  167. pdata = devm_kzalloc(dev, sizeof(struct pmbus_platform_data),
  168. GFP_KERNEL);
  169. if (!pdata)
  170. return -ENOMEM;
  171. pdata->flags = PMBUS_SKIP_STATUS_CHECK;
  172. }
  173. info->pages = id->driver_data;
  174. info->identify = pmbus_identify;
  175. dev->platform_data = pdata;
  176. return pmbus_do_probe(client, id, info);
  177. }
  178. /*
  179. * Use driver_data to set the number of pages supported by the chip.
  180. */
  181. static const struct i2c_device_id pmbus_id[] = {
  182. {"adp4000", 1},
  183. {"bmr453", 1},
  184. {"bmr454", 1},
  185. {"dps460", 1},
  186. {"dps800", 1},
  187. {"mdt040", 1},
  188. {"ncp4200", 1},
  189. {"ncp4208", 1},
  190. {"pdt003", 1},
  191. {"pdt006", 1},
  192. {"pdt012", 1},
  193. {"pmbus", 0},
  194. {"sgd009", 1},
  195. {"tps40400", 1},
  196. {"tps544b20", 1},
  197. {"tps544b25", 1},
  198. {"tps544c20", 1},
  199. {"tps544c25", 1},
  200. {"udt020", 1},
  201. {}
  202. };
  203. MODULE_DEVICE_TABLE(i2c, pmbus_id);
  204. /* This is the driver that will be inserted */
  205. static struct i2c_driver pmbus_driver = {
  206. .driver = {
  207. .name = "pmbus",
  208. },
  209. .probe = pmbus_probe,
  210. .remove = pmbus_do_remove,
  211. .id_table = pmbus_id,
  212. };
  213. module_i2c_driver(pmbus_driver);
  214. MODULE_AUTHOR("Guenter Roeck");
  215. MODULE_DESCRIPTION("Generic PMBus driver");
  216. MODULE_LICENSE("GPL");