adm1275.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #define ADM1275_PEAK_IOUT 0xd0
  25. #define ADM1275_PEAK_VIN 0xd1
  26. #define ADM1275_PEAK_VOUT 0xd2
  27. #define ADM1275_PMON_CONFIG 0xd4
  28. #define ADM1275_VIN_VOUT_SELECT (1 << 6)
  29. #define ADM1275_VRANGE (1 << 5)
  30. static int adm1275_read_word_data(struct i2c_client *client, int page, int reg)
  31. {
  32. int ret;
  33. if (page)
  34. return -EINVAL;
  35. switch (reg) {
  36. case PMBUS_VIRT_READ_IOUT_MAX:
  37. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_IOUT);
  38. break;
  39. case PMBUS_VIRT_READ_VOUT_MAX:
  40. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VOUT);
  41. break;
  42. case PMBUS_VIRT_READ_VIN_MAX:
  43. ret = pmbus_read_word_data(client, 0, ADM1275_PEAK_VIN);
  44. break;
  45. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  46. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  47. case PMBUS_VIRT_RESET_VIN_HISTORY:
  48. ret = 0;
  49. break;
  50. default:
  51. ret = -ENODATA;
  52. break;
  53. }
  54. return ret;
  55. }
  56. static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
  57. u16 word)
  58. {
  59. int ret;
  60. if (page)
  61. return -EINVAL;
  62. switch (reg) {
  63. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  64. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
  65. break;
  66. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  67. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
  68. break;
  69. case PMBUS_VIRT_RESET_VIN_HISTORY:
  70. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
  71. break;
  72. default:
  73. ret = -ENODATA;
  74. break;
  75. }
  76. return ret;
  77. }
  78. static int adm1275_probe(struct i2c_client *client,
  79. const struct i2c_device_id *id)
  80. {
  81. int config;
  82. int ret;
  83. struct pmbus_driver_info *info;
  84. if (!i2c_check_functionality(client->adapter,
  85. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  86. return -ENODEV;
  87. info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL);
  88. if (!info)
  89. return -ENOMEM;
  90. config = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
  91. if (config < 0) {
  92. ret = config;
  93. goto err_mem;
  94. }
  95. info->pages = 1;
  96. info->format[PSC_VOLTAGE_IN] = direct;
  97. info->format[PSC_VOLTAGE_OUT] = direct;
  98. info->format[PSC_CURRENT_OUT] = direct;
  99. info->m[PSC_CURRENT_OUT] = 807;
  100. info->b[PSC_CURRENT_OUT] = 20475;
  101. info->R[PSC_CURRENT_OUT] = -1;
  102. info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
  103. info->read_word_data = adm1275_read_word_data;
  104. info->write_word_data = adm1275_write_word_data;
  105. if (config & ADM1275_VRANGE) {
  106. info->m[PSC_VOLTAGE_IN] = 19199;
  107. info->b[PSC_VOLTAGE_IN] = 0;
  108. info->R[PSC_VOLTAGE_IN] = -2;
  109. info->m[PSC_VOLTAGE_OUT] = 19199;
  110. info->b[PSC_VOLTAGE_OUT] = 0;
  111. info->R[PSC_VOLTAGE_OUT] = -2;
  112. } else {
  113. info->m[PSC_VOLTAGE_IN] = 6720;
  114. info->b[PSC_VOLTAGE_IN] = 0;
  115. info->R[PSC_VOLTAGE_IN] = -1;
  116. info->m[PSC_VOLTAGE_OUT] = 6720;
  117. info->b[PSC_VOLTAGE_OUT] = 0;
  118. info->R[PSC_VOLTAGE_OUT] = -1;
  119. }
  120. if (config & ADM1275_VIN_VOUT_SELECT)
  121. info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  122. else
  123. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
  124. ret = pmbus_do_probe(client, id, info);
  125. if (ret)
  126. goto err_mem;
  127. return 0;
  128. err_mem:
  129. kfree(info);
  130. return ret;
  131. }
  132. static int adm1275_remove(struct i2c_client *client)
  133. {
  134. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  135. int ret;
  136. ret = pmbus_do_remove(client);
  137. kfree(info);
  138. return ret;
  139. }
  140. static const struct i2c_device_id adm1275_id[] = {
  141. {"adm1275", 0},
  142. { }
  143. };
  144. MODULE_DEVICE_TABLE(i2c, adm1275_id);
  145. static struct i2c_driver adm1275_driver = {
  146. .driver = {
  147. .name = "adm1275",
  148. },
  149. .probe = adm1275_probe,
  150. .remove = adm1275_remove,
  151. .id_table = adm1275_id,
  152. };
  153. static int __init adm1275_init(void)
  154. {
  155. return i2c_add_driver(&adm1275_driver);
  156. }
  157. static void __exit adm1275_exit(void)
  158. {
  159. i2c_del_driver(&adm1275_driver);
  160. }
  161. MODULE_AUTHOR("Guenter Roeck");
  162. MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275");
  163. MODULE_LICENSE("GPL");
  164. module_init(adm1275_init);
  165. module_exit(adm1275_exit);