ucd9000.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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)
  65. return -EINVAL;
  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)
  78. return -EINVAL;
  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. mid = NULL;
  125. for (i = 0; i < ARRAY_SIZE(ucd9000_id); i++) {
  126. mid = &ucd9000_id[i];
  127. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  128. break;
  129. }
  130. if (!mid || !strlen(mid->name)) {
  131. dev_err(&client->dev, "Unsupported device\n");
  132. return -ENODEV;
  133. }
  134. if (id->driver_data != ucd9000 && id->driver_data != mid->driver_data)
  135. dev_notice(&client->dev,
  136. "Device mismatch: Configured %s, detected %s\n",
  137. id->name, mid->name);
  138. data = kzalloc(sizeof(struct ucd9000_data), GFP_KERNEL);
  139. if (!data)
  140. return -ENOMEM;
  141. info = &data->info;
  142. ret = i2c_smbus_read_byte_data(client, UCD9000_NUM_PAGES);
  143. if (ret < 0) {
  144. dev_err(&client->dev,
  145. "Failed to read number of active pages\n");
  146. goto out;
  147. }
  148. info->pages = ret;
  149. if (!info->pages) {
  150. dev_err(&client->dev, "No pages configured\n");
  151. ret = -ENODEV;
  152. goto out;
  153. }
  154. /* The internal temperature sensor is always active */
  155. info->func[0] = PMBUS_HAVE_TEMP;
  156. /* Everything else is configurable */
  157. ret = i2c_smbus_read_block_data(client, UCD9000_MONITOR_CONFIG,
  158. block_buffer);
  159. if (ret <= 0) {
  160. dev_err(&client->dev, "Failed to read configuration data\n");
  161. ret = -ENODEV;
  162. goto out;
  163. }
  164. for (i = 0; i < ret; i++) {
  165. int page = UCD9000_MON_PAGE(block_buffer[i]);
  166. if (page >= info->pages)
  167. continue;
  168. switch (UCD9000_MON_TYPE(block_buffer[i])) {
  169. case UCD9000_MON_VOLTAGE:
  170. case UCD9000_MON_VOLTAGE_HW:
  171. info->func[page] |= PMBUS_HAVE_VOUT
  172. | PMBUS_HAVE_STATUS_VOUT;
  173. break;
  174. case UCD9000_MON_TEMPERATURE:
  175. info->func[page] |= PMBUS_HAVE_TEMP2
  176. | PMBUS_HAVE_STATUS_TEMP;
  177. break;
  178. case UCD9000_MON_CURRENT:
  179. info->func[page] |= PMBUS_HAVE_IOUT
  180. | PMBUS_HAVE_STATUS_IOUT;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. /* Fan configuration */
  187. if (mid->driver_data == ucd90124) {
  188. for (i = 0; i < UCD9000_NUM_FAN; i++) {
  189. i2c_smbus_write_byte_data(client,
  190. UCD9000_FAN_CONFIG_INDEX, i);
  191. ret = i2c_smbus_read_block_data(client,
  192. UCD9000_FAN_CONFIG,
  193. data->fan_data[i]);
  194. if (ret < 0)
  195. goto out;
  196. }
  197. i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
  198. info->read_byte_data = ucd9000_read_byte_data;
  199. info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12
  200. | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
  201. }
  202. ret = pmbus_do_probe(client, mid, info);
  203. if (ret < 0)
  204. goto out;
  205. return 0;
  206. out:
  207. kfree(data);
  208. return ret;
  209. }
  210. static int ucd9000_remove(struct i2c_client *client)
  211. {
  212. int ret;
  213. struct ucd9000_data *data;
  214. data = to_ucd9000_data(pmbus_get_driver_info(client));
  215. ret = pmbus_do_remove(client);
  216. kfree(data);
  217. return ret;
  218. }
  219. /* This is the driver that will be inserted */
  220. static struct i2c_driver ucd9000_driver = {
  221. .driver = {
  222. .name = "ucd9000",
  223. },
  224. .probe = ucd9000_probe,
  225. .remove = ucd9000_remove,
  226. .id_table = ucd9000_id,
  227. };
  228. static int __init ucd9000_init(void)
  229. {
  230. return i2c_add_driver(&ucd9000_driver);
  231. }
  232. static void __exit ucd9000_exit(void)
  233. {
  234. i2c_del_driver(&ucd9000_driver);
  235. }
  236. MODULE_AUTHOR("Guenter Roeck");
  237. MODULE_DESCRIPTION("PMBus driver for TI UCD90xxx");
  238. MODULE_LICENSE("GPL");
  239. module_init(ucd9000_init);
  240. module_exit(ucd9000_exit);