adm1275.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
  3. * and Digital Power Monitor
  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. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. #include "pmbus.h"
  24. enum chips { adm1075, adm1275, adm1276 };
  25. #define ADM1275_PEAK_IOUT 0xd0
  26. #define ADM1275_PEAK_VIN 0xd1
  27. #define ADM1275_PEAK_VOUT 0xd2
  28. #define ADM1275_PMON_CONFIG 0xd4
  29. #define ADM1275_VIN_VOUT_SELECT (1 << 6)
  30. #define ADM1275_VRANGE (1 << 5)
  31. #define ADM1075_IRANGE_50 (1 << 4)
  32. #define ADM1075_IRANGE_25 (1 << 3)
  33. #define ADM1075_IRANGE_MASK ((1 << 3) | (1 << 4))
  34. #define ADM1275_IOUT_WARN2_LIMIT 0xd7
  35. #define ADM1275_DEVICE_CONFIG 0xd8
  36. #define ADM1275_IOUT_WARN2_SELECT (1 << 4)
  37. #define ADM1276_PEAK_PIN 0xda
  38. #define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
  39. #define ADM1075_READ_VAUX 0xdd
  40. #define ADM1075_VAUX_OV_WARN_LIMIT 0xde
  41. #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
  42. #define ADM1075_VAUX_STATUS 0xf6
  43. #define ADM1075_VAUX_OV_WARN (1<<7)
  44. #define ADM1075_VAUX_UV_WARN (1<<6)
  45. struct adm1275_data {
  46. int id;
  47. bool have_oc_fault;
  48. struct pmbus_driver_info info;
  49. };
  50. #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
  51. static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
  52. {
  53. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  54. const struct adm1275_data *data = to_adm1275_data(info);
  55. int ret = 0;
  56. if (page)
  57. return -ENXIO;
  58. switch (reg) {
  59. case PMBUS_IOUT_UC_FAULT_LIMIT:
  60. if (data->have_oc_fault) {
  61. ret = -ENXIO;
  62. break;
  63. }
  64. ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
  65. break;
  66. case PMBUS_IOUT_OC_FAULT_LIMIT:
  67. if (!data->have_oc_fault) {
  68. ret = -ENXIO;
  69. break;
  70. }
  71. ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
  72. break;
  73. case PMBUS_VOUT_OV_WARN_LIMIT:
  74. if (data->id != adm1075) {
  75. ret = -ENODATA;
  76. break;
  77. }
  78. ret = pmbus_read_word_data(client, 0,
  79. ADM1075_VAUX_OV_WARN_LIMIT);
  80. break;
  81. case PMBUS_VOUT_UV_WARN_LIMIT:
  82. if (data->id != adm1075) {
  83. ret = -ENODATA;
  84. break;
  85. }
  86. ret = pmbus_read_word_data(client, 0,
  87. ADM1075_VAUX_UV_WARN_LIMIT);
  88. break;
  89. case PMBUS_READ_VOUT:
  90. if (data->id != adm1075) {
  91. ret = -ENODATA;
  92. break;
  93. }
  94. ret = pmbus_read_word_data(client, 0, ADM1075_READ_VAUX);
  95. break;
  96. case PMBUS_VIRT_READ_IOUT_MAX:
  97. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
  98. break;
  99. case PMBUS_VIRT_READ_VOUT_MAX:
  100. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
  101. break;
  102. case PMBUS_VIRT_READ_VIN_MAX:
  103. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
  104. break;
  105. case PMBUS_VIRT_READ_PIN_MAX:
  106. if (data->id == adm1275) {
  107. ret = -ENXIO;
  108. break;
  109. }
  110. ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
  111. break;
  112. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  113. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  114. case PMBUS_VIRT_RESET_VIN_HISTORY:
  115. break;
  116. case PMBUS_VIRT_RESET_PIN_HISTORY:
  117. if (data->id == adm1275)
  118. ret = -ENXIO;
  119. break;
  120. default:
  121. ret = -ENODATA;
  122. break;
  123. }
  124. return ret;
  125. }
  126. static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
  127. u16 word)
  128. {
  129. int ret;
  130. if (page)
  131. return -ENXIO;
  132. switch (reg) {
  133. case PMBUS_IOUT_UC_FAULT_LIMIT:
  134. case PMBUS_IOUT_OC_FAULT_LIMIT:
  135. ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
  136. word);
  137. break;
  138. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  139. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
  140. break;
  141. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  142. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
  143. break;
  144. case PMBUS_VIRT_RESET_VIN_HISTORY:
  145. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
  146. break;
  147. case PMBUS_VIRT_RESET_PIN_HISTORY:
  148. ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
  149. break;
  150. default:
  151. ret = -ENODATA;
  152. break;
  153. }
  154. return ret;
  155. }
  156. static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
  157. {
  158. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  159. const struct adm1275_data *data = to_adm1275_data(info);
  160. int mfr_status, ret;
  161. if (page > 0)
  162. return -ENXIO;
  163. switch (reg) {
  164. case PMBUS_STATUS_IOUT:
  165. ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
  166. if (ret < 0)
  167. break;
  168. mfr_status = pmbus_read_byte_data(client, page,
  169. PMBUS_STATUS_MFR_SPECIFIC);
  170. if (mfr_status < 0) {
  171. ret = mfr_status;
  172. break;
  173. }
  174. if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
  175. ret |= data->have_oc_fault ?
  176. PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
  177. }
  178. break;
  179. case PMBUS_STATUS_VOUT:
  180. if (data->id != adm1075) {
  181. ret = -ENODATA;
  182. break;
  183. }
  184. ret = 0;
  185. mfr_status = pmbus_read_byte_data(client, 0,
  186. ADM1075_VAUX_STATUS);
  187. if (mfr_status & ADM1075_VAUX_OV_WARN)
  188. ret |= PB_VOLTAGE_OV_WARNING;
  189. if (mfr_status & ADM1075_VAUX_UV_WARN)
  190. ret |= PB_VOLTAGE_UV_WARNING;
  191. break;
  192. default:
  193. ret = -ENODATA;
  194. break;
  195. }
  196. return ret;
  197. }
  198. static const struct i2c_device_id adm1275_id[] = {
  199. { "adm1075", adm1075 },
  200. { "adm1275", adm1275 },
  201. { "adm1276", adm1276 },
  202. { }
  203. };
  204. MODULE_DEVICE_TABLE(i2c, adm1275_id);
  205. static int adm1275_probe(struct i2c_client *client,
  206. const struct i2c_device_id *id)
  207. {
  208. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  209. int config, device_config;
  210. int ret;
  211. struct pmbus_driver_info *info;
  212. struct adm1275_data *data;
  213. const struct i2c_device_id *mid;
  214. if (!i2c_check_functionality(client->adapter,
  215. I2C_FUNC_SMBUS_READ_BYTE_DATA
  216. | I2C_FUNC_SMBUS_BLOCK_DATA))
  217. return -ENODEV;
  218. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
  219. if (ret < 0) {
  220. dev_err(&client->dev, "Failed to read Manufacturer ID\n");
  221. return ret;
  222. }
  223. if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
  224. dev_err(&client->dev, "Unsupported Manufacturer ID\n");
  225. return -ENODEV;
  226. }
  227. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
  228. if (ret < 0) {
  229. dev_err(&client->dev, "Failed to read Manufacturer Model\n");
  230. return ret;
  231. }
  232. for (mid = adm1275_id; mid->name[0]; mid++) {
  233. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  234. break;
  235. }
  236. if (!mid->name[0]) {
  237. dev_err(&client->dev, "Unsupported device\n");
  238. return -ENODEV;
  239. }
  240. if (id->driver_data != mid->driver_data)
  241. dev_notice(&client->dev,
  242. "Device mismatch: Configured %s, detected %s\n",
  243. id->name, mid->name);
  244. config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
  245. if (config < 0)
  246. return config;
  247. device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
  248. if (device_config < 0)
  249. return device_config;
  250. data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
  251. GFP_KERNEL);
  252. if (!data)
  253. return -ENOMEM;
  254. data->id = mid->driver_data;
  255. info = &data->info;
  256. info->pages = 1;
  257. info->format[PSC_VOLTAGE_IN] = direct;
  258. info->format[PSC_VOLTAGE_OUT] = direct;
  259. info->format[PSC_CURRENT_OUT] = direct;
  260. info->m[PSC_CURRENT_OUT] = 807;
  261. info->b[PSC_CURRENT_OUT] = 20475;
  262. info->R[PSC_CURRENT_OUT] = -1;
  263. info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
  264. info->read_word_data = adm1275_read_word_data;
  265. info->read_byte_data = adm1275_read_byte_data;
  266. info->write_word_data = adm1275_write_word_data;
  267. if (data->id == adm1075) {
  268. info->m[PSC_VOLTAGE_IN] = 27169;
  269. info->b[PSC_VOLTAGE_IN] = 0;
  270. info->R[PSC_VOLTAGE_IN] = -1;
  271. info->m[PSC_VOLTAGE_OUT] = 27169;
  272. info->b[PSC_VOLTAGE_OUT] = 0;
  273. info->R[PSC_VOLTAGE_OUT] = -1;
  274. } else if (config & ADM1275_VRANGE) {
  275. info->m[PSC_VOLTAGE_IN] = 19199;
  276. info->b[PSC_VOLTAGE_IN] = 0;
  277. info->R[PSC_VOLTAGE_IN] = -2;
  278. info->m[PSC_VOLTAGE_OUT] = 19199;
  279. info->b[PSC_VOLTAGE_OUT] = 0;
  280. info->R[PSC_VOLTAGE_OUT] = -2;
  281. } else {
  282. info->m[PSC_VOLTAGE_IN] = 6720;
  283. info->b[PSC_VOLTAGE_IN] = 0;
  284. info->R[PSC_VOLTAGE_IN] = -1;
  285. info->m[PSC_VOLTAGE_OUT] = 6720;
  286. info->b[PSC_VOLTAGE_OUT] = 0;
  287. info->R[PSC_VOLTAGE_OUT] = -1;
  288. }
  289. if (device_config & ADM1275_IOUT_WARN2_SELECT)
  290. data->have_oc_fault = true;
  291. switch (data->id) {
  292. case adm1075:
  293. info->format[PSC_POWER] = direct;
  294. info->b[PSC_POWER] = 0;
  295. info->R[PSC_POWER] = -1;
  296. switch (config & ADM1075_IRANGE_MASK) {
  297. case ADM1075_IRANGE_25:
  298. info->m[PSC_POWER] = 8549;
  299. info->m[PSC_CURRENT_OUT] = 806;
  300. break;
  301. case ADM1075_IRANGE_50:
  302. info->m[PSC_POWER] = 4279;
  303. info->m[PSC_CURRENT_OUT] = 404;
  304. break;
  305. default:
  306. dev_err(&client->dev, "Invalid input current range");
  307. info->m[PSC_POWER] = 0;
  308. info->m[PSC_CURRENT_OUT] = 0;
  309. break;
  310. }
  311. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
  312. | PMBUS_HAVE_STATUS_INPUT;
  313. if (config & ADM1275_VIN_VOUT_SELECT)
  314. info->func[0] |=
  315. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  316. break;
  317. case adm1275:
  318. if (config & ADM1275_VIN_VOUT_SELECT)
  319. info->func[0] |=
  320. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  321. else
  322. info->func[0] |=
  323. PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
  324. break;
  325. case adm1276:
  326. info->format[PSC_POWER] = direct;
  327. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
  328. | PMBUS_HAVE_STATUS_INPUT;
  329. if (config & ADM1275_VIN_VOUT_SELECT)
  330. info->func[0] |=
  331. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  332. if (config & ADM1275_VRANGE) {
  333. info->m[PSC_POWER] = 6043;
  334. info->b[PSC_POWER] = 0;
  335. info->R[PSC_POWER] = -2;
  336. } else {
  337. info->m[PSC_POWER] = 2115;
  338. info->b[PSC_POWER] = 0;
  339. info->R[PSC_POWER] = -1;
  340. }
  341. break;
  342. }
  343. return pmbus_do_probe(client, id, info);
  344. }
  345. static struct i2c_driver adm1275_driver = {
  346. .driver = {
  347. .name = "adm1275",
  348. },
  349. .probe = adm1275_probe,
  350. .remove = pmbus_do_remove,
  351. .id_table = adm1275_id,
  352. };
  353. module_i2c_driver(adm1275_driver);
  354. MODULE_AUTHOR("Guenter Roeck");
  355. MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
  356. MODULE_LICENSE("GPL");