ucd9200.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Hardware monitoring driver for ucd9200 series Digital PWM System Controllers
  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/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/i2c/pmbus.h>
  27. #include "pmbus.h"
  28. #define UCD9200_PHASE_INFO 0xd2
  29. #define UCD9200_DEVICE_ID 0xfd
  30. enum chips { ucd9200, ucd9220, ucd9222, ucd9224, ucd9240, ucd9244, ucd9246,
  31. ucd9248 };
  32. static const struct i2c_device_id ucd9200_id[] = {
  33. {"ucd9200", ucd9200},
  34. {"ucd9220", ucd9220},
  35. {"ucd9222", ucd9222},
  36. {"ucd9224", ucd9224},
  37. {"ucd9240", ucd9240},
  38. {"ucd9244", ucd9244},
  39. {"ucd9246", ucd9246},
  40. {"ucd9248", ucd9248},
  41. {}
  42. };
  43. MODULE_DEVICE_TABLE(i2c, ucd9200_id);
  44. static int ucd9200_probe(struct i2c_client *client,
  45. const struct i2c_device_id *id)
  46. {
  47. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  48. struct pmbus_driver_info *info;
  49. const struct i2c_device_id *mid;
  50. int i, j, ret;
  51. if (!i2c_check_functionality(client->adapter,
  52. I2C_FUNC_SMBUS_BYTE_DATA |
  53. I2C_FUNC_SMBUS_BLOCK_DATA))
  54. return -ENODEV;
  55. ret = i2c_smbus_read_block_data(client, UCD9200_DEVICE_ID,
  56. block_buffer);
  57. if (ret < 0) {
  58. dev_err(&client->dev, "Failed to read device ID\n");
  59. return ret;
  60. }
  61. block_buffer[ret] = '\0';
  62. dev_info(&client->dev, "Device ID %s\n", block_buffer);
  63. for (mid = ucd9200_id; mid->name[0]; mid++) {
  64. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  65. break;
  66. }
  67. if (!mid->name[0]) {
  68. dev_err(&client->dev, "Unsupported device\n");
  69. return -ENODEV;
  70. }
  71. if (id->driver_data != ucd9200 && id->driver_data != mid->driver_data)
  72. dev_notice(&client->dev,
  73. "Device mismatch: Configured %s, detected %s\n",
  74. id->name, mid->name);
  75. info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
  76. GFP_KERNEL);
  77. if (!info)
  78. return -ENOMEM;
  79. ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO,
  80. block_buffer);
  81. if (ret < 0) {
  82. dev_err(&client->dev, "Failed to read phase information\n");
  83. return ret;
  84. }
  85. /*
  86. * Calculate number of configured pages (rails) from PHASE_INFO
  87. * register.
  88. * Rails have to be sequential, so we can abort after finding
  89. * the first unconfigured rail.
  90. */
  91. info->pages = 0;
  92. for (i = 0; i < ret; i++) {
  93. if (!block_buffer[i])
  94. break;
  95. info->pages++;
  96. }
  97. if (!info->pages) {
  98. dev_err(&client->dev, "No rails configured\n");
  99. return -ENODEV;
  100. }
  101. dev_info(&client->dev, "%d rails configured\n", info->pages);
  102. /*
  103. * Set PHASE registers on all pages to 0xff to ensure that phase
  104. * specific commands will apply to all phases of a given page (rail).
  105. * This only affects the READ_IOUT and READ_TEMPERATURE2 registers.
  106. * READ_IOUT will return the sum of currents of all phases of a rail,
  107. * and READ_TEMPERATURE2 will return the maximum temperature detected
  108. * for the the phases of the rail.
  109. */
  110. for (i = 0; i < info->pages; i++) {
  111. /*
  112. * Setting PAGE & PHASE fails once in a while for no obvious
  113. * reason, so we need to retry a couple of times.
  114. */
  115. for (j = 0; j < 3; j++) {
  116. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
  117. if (ret < 0)
  118. continue;
  119. ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
  120. 0xff);
  121. if (ret < 0)
  122. continue;
  123. break;
  124. }
  125. if (ret < 0) {
  126. dev_err(&client->dev,
  127. "Failed to initialize PHASE registers\n");
  128. return ret;
  129. }
  130. }
  131. if (info->pages > 1)
  132. i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
  133. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
  134. PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
  135. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  136. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  137. PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP |
  138. PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  139. for (i = 1; i < info->pages; i++)
  140. info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  141. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  142. PMBUS_HAVE_POUT |
  143. PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  144. /* ucd9240 supports a single fan */
  145. if (mid->driver_data == ucd9240)
  146. info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12;
  147. return pmbus_do_probe(client, mid, info);
  148. }
  149. /* This is the driver that will be inserted */
  150. static struct i2c_driver ucd9200_driver = {
  151. .driver = {
  152. .name = "ucd9200",
  153. },
  154. .probe = ucd9200_probe,
  155. .remove = pmbus_do_remove,
  156. .id_table = ucd9200_id,
  157. };
  158. module_i2c_driver(ucd9200_driver);
  159. MODULE_AUTHOR("Guenter Roeck");
  160. MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x");
  161. MODULE_LICENSE("GPL");