ucd9000.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/init.h>
  24. #include <linux/err.h>
  25. #include <linux/slab.h>
  26. #include <linux/i2c.h>
  27. #include <linux/i2c/pmbus.h>
  28. #include "pmbus.h"
  29. enum chips { ucd9000, ucd90120, ucd90124, ucd9090, ucd90910 };
  30. #define UCD9000_MONITOR_CONFIG 0xd5
  31. #define UCD9000_NUM_PAGES 0xd6
  32. #define UCD9000_FAN_CONFIG_INDEX 0xe7
  33. #define UCD9000_FAN_CONFIG 0xe8
  34. #define UCD9000_DEVICE_ID 0xfd
  35. #define UCD9000_MON_TYPE(x) (((x) >> 5) & 0x07)
  36. #define UCD9000_MON_PAGE(x) ((x) & 0x0f)
  37. #define UCD9000_MON_VOLTAGE 1
  38. #define UCD9000_MON_TEMPERATURE 2
  39. #define UCD9000_MON_CURRENT 3
  40. #define UCD9000_MON_VOLTAGE_HW 4
  41. #define UCD9000_NUM_FAN 4
  42. struct ucd9000_data {
  43. u8 fan_data[UCD9000_NUM_FAN][I2C_SMBUS_BLOCK_MAX];
  44. struct pmbus_driver_info info;
  45. };
  46. #define to_ucd9000_data(_info) container_of(_info, struct ucd9000_data, info)
  47. static int ucd9000_get_fan_config(struct i2c_client *client, int fan)
  48. {
  49. int fan_config = 0;
  50. struct ucd9000_data *data
  51. = to_ucd9000_data(pmbus_get_driver_info(client));
  52. if (data->fan_data[fan][3] & 1)
  53. fan_config |= PB_FAN_2_INSTALLED; /* Use lower bit position */
  54. /* Pulses/revolution */
  55. fan_config |= (data->fan_data[fan][3] & 0x06) >> 1;
  56. return fan_config;
  57. }
  58. static int ucd9000_read_byte_data(struct i2c_client *client, int page, int reg)
  59. {
  60. int ret = 0;
  61. int fan_config;
  62. switch (reg) {
  63. case PMBUS_FAN_CONFIG_12:
  64. if (page > 0)
  65. return -ENXIO;
  66. ret = ucd9000_get_fan_config(client, 0);
  67. if (ret < 0)
  68. return ret;
  69. fan_config = ret << 4;
  70. ret = ucd9000_get_fan_config(client, 1);
  71. if (ret < 0)
  72. return ret;
  73. fan_config |= ret;
  74. ret = fan_config;
  75. break;
  76. case PMBUS_FAN_CONFIG_34:
  77. if (page > 0)
  78. return -ENXIO;
  79. ret = ucd9000_get_fan_config(client, 2);
  80. if (ret < 0)
  81. return ret;
  82. fan_config = ret << 4;
  83. ret = ucd9000_get_fan_config(client, 3);
  84. if (ret < 0)
  85. return ret;
  86. fan_config |= ret;
  87. ret = fan_config;
  88. break;
  89. default:
  90. ret = -ENODATA;
  91. break;
  92. }
  93. return ret;
  94. }
  95. static const struct i2c_device_id ucd9000_id[] = {
  96. {"ucd9000", ucd9000},
  97. {"ucd90120", ucd90120},
  98. {"ucd90124", ucd90124},
  99. {"ucd9090", ucd9090},
  100. {"ucd90910", ucd90910},
  101. {}
  102. };
  103. MODULE_DEVICE_TABLE(i2c, ucd9000_id);
  104. static int ucd9000_probe(struct i2c_client *client,
  105. const struct i2c_device_id *id)
  106. {
  107. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  108. struct ucd9000_data *data;
  109. struct pmbus_driver_info *info;
  110. const struct i2c_device_id *mid;
  111. int i, ret;
  112. if (!i2c_check_functionality(client->adapter,
  113. I2C_FUNC_SMBUS_BYTE_DATA |
  114. I2C_FUNC_SMBUS_BLOCK_DATA))
  115. return -ENODEV;
  116. ret = i2c_smbus_read_block_data(client, UCD9000_DEVICE_ID,
  117. block_buffer);
  118. if (ret < 0) {
  119. dev_err(&client->dev, "Failed to read device ID\n");
  120. return ret;
  121. }
  122. block_buffer[ret] = '\0';
  123. dev_info(&client->dev, "Device ID %s\n", block_buffer);
  124. for (mid = ucd9000_id; mid->name[0]; mid++) {
  125. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  126. break;
  127. }
  128. if (!mid->name[0]) {
  129. dev_err(&client->dev, "Unsupported device\n");
  130. return -ENODEV;
  131. }
  132. if (id->driver_data != ucd9000 && id->driver_data != mid->driver_data)
  133. dev_notice(&client->dev,
  134. "Device mismatch: Configured %s, detected %s\n",
  135. id->name, mid->name);
  136. data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
  137. GFP_KERNEL);
  138. if (!data)
  139. return -ENOMEM;
  140. info = &data->info;
  141. ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
  142. if (ret < 0) {
  143. dev_err(&client->dev,
  144. "Failed to read number of active pages\n");
  145. return ret;
  146. }
  147. info->pages = ret;
  148. if (!info->pages) {
  149. dev_err(&client->dev, "No pages configured\n");
  150. return -ENODEV;
  151. }
  152. /* The internal temperature sensor is always active */
  153. info->func[0] = PMBUS_HAVE_TEMP;
  154. /* Everything else is configurable */
  155. ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
  156. block_buffer);
  157. if (ret <= 0) {
  158. dev_err(&client->dev, "Failed to read configuration data\n");
  159. return -ENODEV;
  160. }
  161. for (i = 0; i < ret; i++) {
  162. int page = UCD9000_MON_PAGE(block_buffer[i]);
  163. if (page >= info->pages)
  164. continue;
  165. switch (UCD9000_MON_TYPE(block_buffer[i])) {
  166. case UCD9000_MON_VOLTAGE:
  167. case UCD9000_MON_VOLTAGE_HW:
  168. info->func[page] |= PMBUS_HAVE_VOUT
  169. | PMBUS_HAVE_STATUS_VOUT;
  170. break;
  171. case UCD9000_MON_TEMPERATURE:
  172. info->func[page] |= PMBUS_HAVE_TEMP2
  173. | PMBUS_HAVE_STATUS_TEMP;
  174. break;
  175. case UCD9000_MON_CURRENT:
  176. info->func[page] |= PMBUS_HAVE_IOUT
  177. | PMBUS_HAVE_STATUS_IOUT;
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. /* Fan configuration */
  184. if (mid->driver_data == ucd90124) {
  185. for (i = 0; i < UCD9000_NUM_FAN; i++) {
  186. i2c_smbus_write_byte_data(client,
  187. UCD9000_FAN_CONFIG_INDEX, i);
  188. ret = i2c_smbus_read_block_data(client,
  189. UCD9000_FAN_CONFIG,
  190. data->fan_data[i]);
  191. if (ret < 0)
  192. return ret;
  193. }
  194. i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
  195. info->read_byte_data = ucd9000_read_byte_data;
  196. info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
  197. | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
  198. }
  199. return pmbus_do_probe(client, mid, info);
  200. }
  201. /* This is the driver that will be inserted */
  202. static struct i2c_driver ucd9000_driver = {
  203. .driver = {
  204. .name = "ucd9000",
  205. },
  206. .probe = ucd9000_probe,
  207. .remove = pmbus_do_remove,
  208. .id_table = ucd9000_id,
  209. };
  210. module_i2c_driver(ucd9000_driver);
  211. MODULE_AUTHOR("Guenter Roeck");
  212. MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
  213. MODULE_LICENSE("GPL");