ucd9200.c 5.9 KB

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