ucd9200.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. mid = NULL;
  64. for (i = 0; i < ARRAY_SIZE(ucd9200_id); i++) {
  65. mid = &ucd9200_id[i];
  66. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  67. break;
  68. }
  69. if (!mid || !strlen(mid->name)) {
  70. dev_err(&client->dev, "Unsupported device\n");
  71. return -ENODEV;
  72. }
  73. if (id->driver_data != ucd9200 && id->driver_data != mid->driver_data)
  74. dev_notice(&client->dev,
  75. "Device mismatch: Configured %s, detected %s\n",
  76. id->name, mid->name);
  77. info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
  78. if (!info)
  79. return -ENOMEM;
  80. ret = i2c_smbus_read_block_data(client, UCD9200_PHASE_INFO,
  81. block_buffer);
  82. if (ret < 0) {
  83. dev_err(&client->dev, "Failed to read phase information\n");
  84. goto out;
  85. }
  86. /*
  87. * Calculate number of configured pages (rails) from PHASE_INFO
  88. * register.
  89. * Rails have to be sequential, so we can abort after finding
  90. * the first unconfigured rail.
  91. */
  92. info->pages = 0;
  93. for (i = 0; i < ret; i++) {
  94. if (!block_buffer[i])
  95. break;
  96. info->pages++;
  97. }
  98. if (!info->pages) {
  99. dev_err(&client->dev, "No rails configured\n");
  100. ret = -ENODEV;
  101. goto out;
  102. }
  103. dev_info(&client->dev, "%d rails configured\n", info->pages);
  104. /*
  105. * Set PHASE registers on all pages to 0xff to ensure that phase
  106. * specific commands will apply to all phases of a given page (rail).
  107. * This only affects the READ_IOUT and READ_TEMPERATURE2 registers.
  108. * READ_IOUT will return the sum of currents of all phases of a rail,
  109. * and READ_TEMPERATURE2 will return the maximum temperature detected
  110. * for the the phases of the rail.
  111. */
  112. for (i = 0; i < info->pages; i++) {
  113. /*
  114. * Setting PAGE & PHASE fails once in a while for no obvious
  115. * reason, so we need to retry a couple of times.
  116. */
  117. for (j = 0; j < 3; j++) {
  118. ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
  119. if (ret < 0)
  120. continue;
  121. ret = i2c_smbus_write_byte_data(client, PMBUS_PHASE,
  122. 0xff);
  123. if (ret < 0)
  124. continue;
  125. break;
  126. }
  127. if (ret < 0) {
  128. dev_err(&client->dev,
  129. "Failed to initialize PHASE registers\n");
  130. goto out;
  131. }
  132. }
  133. if (info->pages > 1)
  134. i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
  135. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT |
  136. PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
  137. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  138. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  139. PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP |
  140. PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  141. for (i = 1; i < info->pages; i++)
  142. info->func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  143. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  144. PMBUS_HAVE_POUT |
  145. PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  146. /* ucd9240 supports a single fan */
  147. if (mid->driver_data == ucd9240)
  148. info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12;
  149. ret = pmbus_do_probe(client, mid, info);
  150. if (ret < 0)
  151. goto out;
  152. return 0;
  153. out:
  154. kfree(info);
  155. return ret;
  156. }
  157. static int ucd9200_remove(struct i2c_client *client)
  158. {
  159. int ret;
  160. const struct pmbus_driver_info *info;
  161. info = pmbus_get_driver_info(client);
  162. ret = pmbus_do_remove(client);
  163. kfree(info);
  164. return ret;
  165. }
  166. /* This is the driver that will be inserted */
  167. static struct i2c_driver ucd9200_driver = {
  168. .driver = {
  169. .name = "ucd9200",
  170. },
  171. .probe = ucd9200_probe,
  172. .remove = ucd9200_remove,
  173. .id_table = ucd9200_id,
  174. };
  175. static int __init ucd9200_init(void)
  176. {
  177. return i2c_add_driver(&ucd9200_driver);
  178. }
  179. static void __exit ucd9200_exit(void)
  180. {
  181. i2c_del_driver(&ucd9200_driver);
  182. }
  183. MODULE_AUTHOR("Guenter Roeck");
  184. MODULE_DESCRIPTION("PMBus driver for TI UCD922x, UCD924x");
  185. MODULE_LICENSE("GPL");
  186. module_init(ucd9200_init);
  187. module_exit(ucd9200_exit);