ucd9200.c 5.2 KB

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