pmbus.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "pmbus.h"
  28. /*
  29. * Find sensor groups and status registers on each page.
  30. */
  31. static void pmbus_find_sensor_groups(struct i2c_client *client,
  32. struct pmbus_driver_info *info)
  33. {
  34. int page;
  35. /* Sensors detected on page 0 only */
  36. if (pmbus_check_word_register(client, 0, PMBUS_READ_VIN))
  37. info->func[0] |= PMBUS_HAVE_VIN;
  38. if (pmbus_check_word_register(client, 0, PMBUS_READ_VCAP))
  39. info->func[0] |= PMBUS_HAVE_VCAP;
  40. if (pmbus_check_word_register(client, 0, PMBUS_READ_IIN))
  41. info->func[0] |= PMBUS_HAVE_IIN;
  42. if (pmbus_check_word_register(client, 0, PMBUS_READ_PIN))
  43. info->func[0] |= PMBUS_HAVE_PIN;
  44. if (info->func[0]
  45. && pmbus_check_byte_register(client, 0, PMBUS_STATUS_INPUT))
  46. info->func[0] |= PMBUS_HAVE_STATUS_INPUT;
  47. if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_12) &&
  48. pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_1)) {
  49. info->func[0] |= PMBUS_HAVE_FAN12;
  50. if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_12))
  51. info->func[0] |= PMBUS_HAVE_STATUS_FAN12;
  52. }
  53. if (pmbus_check_byte_register(client, 0, PMBUS_FAN_CONFIG_34) &&
  54. pmbus_check_word_register(client, 0, PMBUS_READ_FAN_SPEED_3)) {
  55. info->func[0] |= PMBUS_HAVE_FAN34;
  56. if (pmbus_check_byte_register(client, 0, PMBUS_STATUS_FAN_34))
  57. info->func[0] |= PMBUS_HAVE_STATUS_FAN34;
  58. }
  59. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_1))
  60. info->func[0] |= PMBUS_HAVE_TEMP;
  61. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_2))
  62. info->func[0] |= PMBUS_HAVE_TEMP2;
  63. if (pmbus_check_word_register(client, 0, PMBUS_READ_TEMPERATURE_3))
  64. info->func[0] |= PMBUS_HAVE_TEMP3;
  65. if (info->func[0] & (PMBUS_HAVE_TEMP | PMBUS_HAVE_TEMP2
  66. | PMBUS_HAVE_TEMP3)
  67. && pmbus_check_byte_register(client, 0,
  68. PMBUS_STATUS_TEMPERATURE))
  69. info->func[0] |= PMBUS_HAVE_STATUS_TEMP;
  70. /* Sensors detected on all pages */
  71. for (page = 0; page < info->pages; page++) {
  72. if (pmbus_check_word_register(client, page, PMBUS_READ_VOUT)) {
  73. info->func[page] |= PMBUS_HAVE_VOUT;
  74. if (pmbus_check_byte_register(client, page,
  75. PMBUS_STATUS_VOUT))
  76. info->func[page] |= PMBUS_HAVE_STATUS_VOUT;
  77. }
  78. if (pmbus_check_word_register(client, page, PMBUS_READ_IOUT)) {
  79. info->func[page] |= PMBUS_HAVE_IOUT;
  80. if (pmbus_check_byte_register(client, 0,
  81. PMBUS_STATUS_IOUT))
  82. info->func[page] |= PMBUS_HAVE_STATUS_IOUT;
  83. }
  84. if (pmbus_check_word_register(client, page, PMBUS_READ_POUT))
  85. info->func[page] |= PMBUS_HAVE_POUT;
  86. }
  87. }
  88. /*
  89. * Identify chip parameters.
  90. */
  91. static int pmbus_identify(struct i2c_client *client,
  92. struct pmbus_driver_info *info)
  93. {
  94. if (!info->pages) {
  95. /*
  96. * Check if the PAGE command is supported. If it is,
  97. * keep setting the page number until it fails or until the
  98. * maximum number of pages has been reached. Assume that
  99. * this is the number of pages supported by the chip.
  100. */
  101. if (pmbus_check_byte_register(client, 0, PMBUS_PAGE)) {
  102. int page;
  103. for (page = 1; page < PMBUS_PAGES; page++) {
  104. if (pmbus_set_page(client, page) < 0)
  105. break;
  106. }
  107. pmbus_set_page(client, 0);
  108. info->pages = page;
  109. } else {
  110. info->pages = 1;
  111. }
  112. }
  113. /*
  114. * We should check if the COEFFICIENTS register is supported.
  115. * If it is, and the chip is configured for direct mode, we can read
  116. * the coefficients from the chip, one set per group of sensor
  117. * registers.
  118. *
  119. * To do this, we will need access to a chip which actually supports the
  120. * COEFFICIENTS command, since the command is too complex to implement
  121. * without testing it.
  122. */
  123. /* Try to find sensor groups */
  124. pmbus_find_sensor_groups(client, info);
  125. return 0;
  126. }
  127. static int pmbus_probe(struct i2c_client *client,
  128. const struct i2c_device_id *id)
  129. {
  130. struct pmbus_driver_info *info;
  131. int ret;
  132. info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
  133. if (!info)
  134. return -ENOMEM;
  135. info->pages = id->driver_data;
  136. info->identify = pmbus_identify;
  137. ret = pmbus_do_probe(client, id, info);
  138. if (ret < 0)
  139. goto out;
  140. return 0;
  141. out:
  142. kfree(info);
  143. return ret;
  144. }
  145. static int pmbus_remove(struct i2c_client *client)
  146. {
  147. int ret;
  148. const struct pmbus_driver_info *info;
  149. info = pmbus_get_driver_info(client);
  150. ret = pmbus_do_remove(client);
  151. kfree(info);
  152. return ret;
  153. }
  154. /*
  155. * Use driver_data to set the number of pages supported by the chip.
  156. */
  157. static const struct i2c_device_id pmbus_id[] = {
  158. {"bmr450", 1},
  159. {"bmr451", 1},
  160. {"bmr453", 1},
  161. {"bmr454", 1},
  162. {"ltc2978", 8},
  163. {"pmbus", 0},
  164. {}
  165. };
  166. MODULE_DEVICE_TABLE(i2c, pmbus_id);
  167. /* This is the driver that will be inserted */
  168. static struct i2c_driver pmbus_driver = {
  169. .driver = {
  170. .name = "pmbus",
  171. },
  172. .probe = pmbus_probe,
  173. .remove = pmbus_remove,
  174. .id_table = pmbus_id,
  175. };
  176. static int __init pmbus_init(void)
  177. {
  178. return i2c_add_driver(&pmbus_driver);
  179. }
  180. static void __exit pmbus_exit(void)
  181. {
  182. i2c_del_driver(&pmbus_driver);
  183. }
  184. MODULE_AUTHOR("Guenter Roeck");
  185. MODULE_DESCRIPTION("Generic PMBus driver");
  186. MODULE_LICENSE("GPL");
  187. module_init(pmbus_init);
  188. module_exit(pmbus_exit);