ucd9000.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Hardware monitoring driver for UCD90xxx Sequencer and System Health
  3. * Controller series
  4. *
  5. * Copyright (C) 2011 Ericsson AB.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/pmbus.h>
  29. #include "pmbus.h"
  30. enum chips { ucd9000, ucd90120, ucd90124, ucd90160, ucd9090, ucd90910 };
  31. #define UCD9000_MONITOR_CONFIG 0xd5
  32. #define UCD9000_NUM_PAGES 0xd6
  33. #define UCD9000_FAN_CONFIG_INDEX 0xe7
  34. #define UCD9000_FAN_CONFIG 0xe8
  35. #define UCD9000_DEVICE_ID 0xfd
  36. #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
  37. #define UCD9000_MON_PAGE(x) ((x) & 0x0f)
  38. #define UCD9000_MON_VOLTAGE 1
  39. #define UCD9000_MON_TEMPERATURE 2
  40. #define UCD9000_MON_CURRENT 3
  41. #define UCD9000_MON_VOLTAGE_HW 4
  42. #define UCD9000_NUM_FAN 4
  43. struct ucd9000_data {
  44. u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
  45. struct pmbus_driver_info info;
  46. };
  47. #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
  48. static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
  49. {
  50. int fan_config = 0;
  51. struct ucd9000_data *data
  52. = to_ucd9000_data(pmbus_get_driver_info(client));
  53. if (data->fan_data[fan][3] & 1)
  54. fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */
  55. /* Pulses/revolution */
  56. fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
  57. return fan_config;
  58. }
  59. static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
  60. {
  61. int ret = 0;
  62. int fan_config;
  63. switch (reg) {
  64. case PMBUS_FAN_CONFIG_12:
  65. if (page > 0)
  66. return -ENXIO;
  67. ret = ucd9000_get_fan_config(client, 0);
  68. if (ret < 0)
  69. return ret;
  70. fan_config = ret << 4;
  71. ret = ucd9000_get_fan_config(client, 1);
  72. if (ret < 0)
  73. return ret;
  74. fan_config |= ret;
  75. ret = fan_config;
  76. break;
  77. case PMBUS_FAN_CONFIG_34:
  78. if (page > 0)
  79. return -ENXIO;
  80. ret = ucd9000_get_fan_config(client, 2);
  81. if (ret < 0)
  82. return ret;
  83. fan_config = ret << 4;
  84. ret = ucd9000_get_fan_config(client, 3);
  85. if (ret < 0)
  86. return ret;
  87. fan_config |= ret;
  88. ret = fan_config;
  89. break;
  90. default:
  91. ret = -ENODATA;
  92. break;
  93. }
  94. return ret;
  95. }
  96. static const struct i2c_device_id ucd9000_id[] = {
  97. {"ucd9000", ucd9000},
  98. {"ucd90120", ucd90120},
  99. {"ucd90124", ucd90124},
  100. {"ucd90160", ucd90160},
  101. {"ucd9090", ucd9090},
  102. {"ucd90910", ucd90910},
  103. {}
  104. };
  105. MODULE_DEVICE_TABLE(i2c, ucd9000_id);
  106. static const struct of_device_id ucd9000_of_match[] = {
  107. {
  108. .compatible = "ti,ucd9000",
  109. .data = (void *)ucd9000
  110. },
  111. {
  112. .compatible = "ti,ucd90120",
  113. .data = (void *)ucd90120
  114. },
  115. {
  116. .compatible = "ti,ucd90124",
  117. .data = (void *)ucd90124
  118. },
  119. {
  120. .compatible = "ti,ucd90160",
  121. .data = (void *)ucd90160
  122. },
  123. {
  124. .compatible = "ti,ucd9090",
  125. .data = (void *)ucd9090
  126. },
  127. {
  128. .compatible = "ti,ucd90910",
  129. .data = (void *)ucd90910
  130. },
  131. { },
  132. };
  133. MODULE_DEVICE_TABLE(of, ucd9000_of_match);
  134. static int ucd9000_probe(struct i2c_client *client,
  135. const struct i2c_device_id *id)
  136. {
  137. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  138. struct ucd9000_data *data;
  139. struct pmbus_driver_info *info;
  140. const struct i2c_device_id *mid;
  141. enum chips chip;
  142. int i, ret;
  143. if (!i2c_check_functionality(client->adapter,
  144. I2C_FUNC_SMBUS_BYTE_DATA |
  145. I2C_FUNC_SMBUS_BLOCK_DATA))
  146. return -ENODEV;
  147. ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
  148. block_buffer);
  149. if (ret < 0) {
  150. dev_err(&client->dev, "Failed to read device ID\n");
  151. return ret;
  152. }
  153. block_buffer[ret] = '\0';
  154. dev_info(&client->dev, "Device ID %s\n", block_buffer);
  155. for (mid = ucd9000_id; mid->name[0]; mid++) {
  156. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  157. break;
  158. }
  159. if (!mid->name[0]) {
  160. dev_err(&client->dev, "Unsupported device\n");
  161. return -ENODEV;
  162. }
  163. if (client->dev.of_node)
  164. chip = (enum chips)of_device_get_match_data(&client->dev);
  165. else
  166. chip = id->driver_data;
  167. if (chip != ucd9000 && chip != mid->driver_data)
  168. dev_notice(&client->dev,
  169. "Device mismatch: Configured %s, detected %s\n",
  170. id->name, mid->name);
  171. data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
  172. GFP_KERNEL);
  173. if (!data)
  174. return -ENOMEM;
  175. info = &data->info;
  176. ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
  177. if (ret < 0) {
  178. dev_err(&client->dev,
  179. "Failed to read number of active pages\n");
  180. return ret;
  181. }
  182. info->pages = ret;
  183. if (!info->pages) {
  184. dev_err(&client->dev, "No pages configured\n");
  185. return -ENODEV;
  186. }
  187. /* The internal temperature sensor is always active */
  188. info->func[0] = PMBUS_HAVE_TEMP;
  189. /* Everything else is configurable */
  190. ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
  191. block_buffer);
  192. if (ret <= 0) {
  193. dev_err(&client->dev, "Failed to read configuration data\n");
  194. return -ENODEV;
  195. }
  196. for (i = 0; i < ret; i++) {
  197. int page = UCD9000_MON_PAGE(block_buffer[i]);
  198. if (page >= info->pages)
  199. continue;
  200. switch (UCD9000_MON_TYPE(block_buffer[i])) {
  201. case UCD9000_MON_VOLTAGE:
  202. case UCD9000_MON_VOLTAGE_HW:
  203. info->func[page] |= PMBUS_HAVE_VOUT
  204. | PMBUS_HAVE_STATUS_VOUT;
  205. break;
  206. case UCD9000_MON_TEMPERATURE:
  207. info->func[page] |= PMBUS_HAVE_TEMP2
  208. | PMBUS_HAVE_STATUS_TEMP;
  209. break;
  210. case UCD9000_MON_CURRENT:
  211. info->func[page] |= PMBUS_HAVE_IOUT
  212. | PMBUS_HAVE_STATUS_IOUT;
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. /* Fan configuration */
  219. if (mid->driver_data == ucd90124) {
  220. for (i = 0; i < UCD9000_NUM_FAN; i++) {
  221. i2c_smbus_write_byte_data(client,
  222. UCD9000_FAN_CONFIG_INDEX, i);
  223. ret = i2c_smbus_read_block_data(client,
  224. UCD9000_FAN_CONFIG,
  225. data->fan_data[i]);
  226. if (ret < 0)
  227. return ret;
  228. }
  229. i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
  230. info->read_byte_data = ucd9000_read_byte_data;
  231. info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
  232. | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
  233. }
  234. return pmbus_do_probe(client, mid, info);
  235. }
  236. /* This is the driver that will be inserted */
  237. static struct i2c_driver ucd9000_driver = {
  238. .driver = {
  239. .name = "ucd9000",
  240. .of_match_table = of_match_ptr(ucd9000_of_match),
  241. },
  242. .probe = ucd9000_probe,
  243. .remove = pmbus_do_remove,
  244. .id_table = ucd9000_id,
  245. };
  246. module_i2c_driver(ucd9000_driver);
  247. MODULE_AUTHOR("Guenter Roeck");
  248. MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
  249. MODULE_LICENSE("GPL");