adm1275.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 { 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 ADM1275_IOUT_WARN2_LIMIT 0xd7
  32. #define ADM1275_DEVICE_CONFIG 0xd8
  33. #define ADM1275_IOUT_WARN2_SELECT (1 << 4)
  34. #define ADM1276_PEAK_PIN 0xda
  35. #define ADM1275_MFR_STATUS_IOUT_WARN2 (1 << 0)
  36. struct adm1275_data {
  37. int id;
  38. bool have_oc_fault;
  39. struct pmbus_driver_info info;
  40. };
  41. #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
  42. static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
  43. {
  44. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  45. const struct adm1275_data *data = to_adm1275_data(info);
  46. int ret = 0;
  47. if (page)
  48. return -ENXIO;
  49. switch (reg) {
  50. case PMBUS_IOUT_UC_FAULT_LIMIT:
  51. if (data->have_oc_fault) {
  52. ret = -ENXIO;
  53. break;
  54. }
  55. ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
  56. break;
  57. case PMBUS_IOUT_OC_FAULT_LIMIT:
  58. if (!data->have_oc_fault) {
  59. ret = -ENXIO;
  60. break;
  61. }
  62. ret = pmbus_read_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT);
  63. break;
  64. case PMBUS_VIRT_READ_IOUT_MAX:
  65. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
  66. break;
  67. case PMBUS_VIRT_READ_VOUT_MAX:
  68. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
  69. break;
  70. case PMBUS_VIRT_READ_VIN_MAX:
  71. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
  72. break;
  73. case PMBUS_VIRT_READ_PIN_MAX:
  74. if (data->id != adm1276) {
  75. ret = -ENXIO;
  76. break;
  77. }
  78. ret = pmbus_read_word_data(client, 0, ADM1276_PEAK_PIN);
  79. break;
  80. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  81. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  82. case PMBUS_VIRT_RESET_VIN_HISTORY:
  83. break;
  84. case PMBUS_VIRT_RESET_PIN_HISTORY:
  85. if (data->id != adm1276)
  86. ret = -ENXIO;
  87. break;
  88. default:
  89. ret = -ENODATA;
  90. break;
  91. }
  92. return ret;
  93. }
  94. static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
  95. u16 word)
  96. {
  97. int ret;
  98. if (page)
  99. return -ENXIO;
  100. switch (reg) {
  101. case PMBUS_IOUT_UC_FAULT_LIMIT:
  102. case PMBUS_IOUT_OC_FAULT_LIMIT:
  103. ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
  104. word);
  105. break;
  106. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  107. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
  108. break;
  109. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  110. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
  111. break;
  112. case PMBUS_VIRT_RESET_VIN_HISTORY:
  113. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
  114. break;
  115. case PMBUS_VIRT_RESET_PIN_HISTORY:
  116. ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
  117. break;
  118. default:
  119. ret = -ENODATA;
  120. break;
  121. }
  122. return ret;
  123. }
  124. static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
  125. {
  126. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  127. const struct adm1275_data *data = to_adm1275_data(info);
  128. int mfr_status, ret;
  129. if (page > 0)
  130. return -ENXIO;
  131. switch (reg) {
  132. case PMBUS_STATUS_IOUT:
  133. ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
  134. if (ret < 0)
  135. break;
  136. mfr_status = pmbus_read_byte_data(client, page,
  137. PMBUS_STATUS_MFR_SPECIFIC);
  138. if (mfr_status < 0) {
  139. ret = mfr_status;
  140. break;
  141. }
  142. if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
  143. ret |= data->have_oc_fault ?
  144. PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
  145. }
  146. break;
  147. default:
  148. ret = -ENODATA;
  149. break;
  150. }
  151. return ret;
  152. }
  153. static int adm1275_probe(struct i2c_client *client,
  154. const struct i2c_device_id *id)
  155. {
  156. int config, device_config;
  157. int ret;
  158. struct pmbus_driver_info *info;
  159. struct adm1275_data *data;
  160. if (!i2c_check_functionality(client->adapter,
  161. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  162. return -ENODEV;
  163. data = kzalloc(sizeof(struct adm1275_data), GFP_KERNEL);
  164. if (!data)
  165. return -ENOMEM;
  166. config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
  167. if (config < 0) {
  168. ret = config;
  169. goto err_mem;
  170. }
  171. device_config = i2c_smbus_read_byte_data(client, ADM1275_DEVICE_CONFIG);
  172. if (device_config < 0) {
  173. ret = device_config;
  174. goto err_mem;
  175. }
  176. data->id = id->driver_data;
  177. info = &data->info;
  178. info->pages = 1;
  179. info->format[PSC_VOLTAGE_IN] = direct;
  180. info->format[PSC_VOLTAGE_OUT] = direct;
  181. info->format[PSC_CURRENT_OUT] = direct;
  182. info->m[PSC_CURRENT_OUT] = 807;
  183. info->b[PSC_CURRENT_OUT] = 20475;
  184. info->R[PSC_CURRENT_OUT] = -1;
  185. info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
  186. info->read_word_data = adm1275_read_word_data;
  187. info->read_byte_data = adm1275_read_byte_data;
  188. info->write_word_data = adm1275_write_word_data;
  189. if (config & ADM1275_VRANGE) {
  190. info->m[PSC_VOLTAGE_IN] = 19199;
  191. info->b[PSC_VOLTAGE_IN] = 0;
  192. info->R[PSC_VOLTAGE_IN] = -2;
  193. info->m[PSC_VOLTAGE_OUT] = 19199;
  194. info->b[PSC_VOLTAGE_OUT] = 0;
  195. info->R[PSC_VOLTAGE_OUT] = -2;
  196. } else {
  197. info->m[PSC_VOLTAGE_IN] = 6720;
  198. info->b[PSC_VOLTAGE_IN] = 0;
  199. info->R[PSC_VOLTAGE_IN] = -1;
  200. info->m[PSC_VOLTAGE_OUT] = 6720;
  201. info->b[PSC_VOLTAGE_OUT] = 0;
  202. info->R[PSC_VOLTAGE_OUT] = -1;
  203. }
  204. if (device_config & ADM1275_IOUT_WARN2_SELECT)
  205. data->have_oc_fault = true;
  206. switch (id->driver_data) {
  207. case adm1275:
  208. if (config & ADM1275_VIN_VOUT_SELECT)
  209. info->func[0] |=
  210. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  211. else
  212. info->func[0] |=
  213. PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
  214. break;
  215. case adm1276:
  216. info->format[PSC_POWER] = direct;
  217. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
  218. | PMBUS_HAVE_STATUS_INPUT;
  219. if (config & ADM1275_VIN_VOUT_SELECT)
  220. info->func[0] |=
  221. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  222. if (config & ADM1275_VRANGE) {
  223. info->m[PSC_POWER] = 6043;
  224. info->b[PSC_POWER] = 0;
  225. info->R[PSC_POWER] = -2;
  226. } else {
  227. info->m[PSC_POWER] = 2115;
  228. info->b[PSC_POWER] = 0;
  229. info->R[PSC_POWER] = -1;
  230. }
  231. break;
  232. }
  233. ret = pmbus_do_probe(client, id, info);
  234. if (ret)
  235. goto err_mem;
  236. return 0;
  237. err_mem:
  238. kfree(data);
  239. return ret;
  240. }
  241. static int adm1275_remove(struct i2c_client *client)
  242. {
  243. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  244. const struct adm1275_data *data = to_adm1275_data(info);
  245. pmbus_do_remove(client);
  246. kfree(data);
  247. return 0;
  248. }
  249. static const struct i2c_device_id adm1275_id[] = {
  250. { "adm1275", adm1275 },
  251. { "adm1276", adm1276 },
  252. { }
  253. };
  254. MODULE_DEVICE_TABLE(i2c, adm1275_id);
  255. static struct i2c_driver adm1275_driver = {
  256. .driver = {
  257. .name = "adm1275",
  258. },
  259. .probe = adm1275_probe,
  260. .remove = adm1275_remove,
  261. .id_table = adm1275_id,
  262. };
  263. static int __init adm1275_init(void)
  264. {
  265. return i2c_add_driver(&adm1275_driver);
  266. }
  267. static void __exit adm1275_exit(void)
  268. {
  269. i2c_del_driver(&adm1275_driver);
  270. }
  271. MODULE_AUTHOR("Guenter Roeck");
  272. MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
  273. MODULE_LICENSE("GPL");
  274. module_init(adm1275_init);
  275. module_exit(adm1275_exit);