lm25066.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Hardware monitoring driver for LM25066 / LM5064 / LM5066
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  5. * Copyright (c) 2013 Guenter Roeck
  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 "pmbus.h"
  28. enum chips { lm25066, lm5064, lm5066 };
  29. #define LM25066_READ_VAUX 0xd0
  30. #define LM25066_MFR_READ_IIN 0xd1
  31. #define LM25066_MFR_READ_PIN 0xd2
  32. #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
  33. #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
  34. #define LM25066_READ_PIN_PEAK 0xd5
  35. #define LM25066_CLEAR_PIN_PEAK 0xd6
  36. #define LM25066_DEVICE_SETUP 0xd9
  37. #define LM25066_READ_AVG_VIN 0xdc
  38. #define LM25066_READ_AVG_VOUT 0xdd
  39. #define LM25066_READ_AVG_IIN 0xde
  40. #define LM25066_READ_AVG_PIN 0xdf
  41. #define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
  42. struct lm25066_data {
  43. int id;
  44. struct pmbus_driver_info info;
  45. };
  46. #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
  47. static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
  48. {
  49. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  50. const struct lm25066_data *data = to_lm25066_data(info);
  51. int ret;
  52. switch (reg) {
  53. case PMBUS_VIRT_READ_VMON:
  54. ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
  55. if (ret < 0)
  56. break;
  57. /* Adjust returned value to match VIN coefficients */
  58. switch (data->id) {
  59. case lm25066:
  60. /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
  61. ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
  62. break;
  63. case lm5064:
  64. /* VIN: 4.53 mV VAUX: 700 uV LSB */
  65. ret = DIV_ROUND_CLOSEST(ret * 70, 453);
  66. break;
  67. case lm5066:
  68. /* VIN: 2.18 mV VAUX: 725 uV LSB */
  69. ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
  70. break;
  71. }
  72. break;
  73. case PMBUS_READ_IIN:
  74. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
  75. break;
  76. case PMBUS_READ_PIN:
  77. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
  78. break;
  79. case PMBUS_IIN_OC_WARN_LIMIT:
  80. ret = pmbus_read_word_data(client, 0,
  81. LM25066_MFR_IIN_OC_WARN_LIMIT);
  82. break;
  83. case PMBUS_PIN_OP_WARN_LIMIT:
  84. ret = pmbus_read_word_data(client, 0,
  85. LM25066_MFR_PIN_OP_WARN_LIMIT);
  86. break;
  87. case PMBUS_VIRT_READ_VIN_AVG:
  88. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
  89. break;
  90. case PMBUS_VIRT_READ_VOUT_AVG:
  91. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
  92. break;
  93. case PMBUS_VIRT_READ_IIN_AVG:
  94. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
  95. break;
  96. case PMBUS_VIRT_READ_PIN_AVG:
  97. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
  98. break;
  99. case PMBUS_VIRT_READ_PIN_MAX:
  100. ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
  101. break;
  102. case PMBUS_VIRT_RESET_PIN_HISTORY:
  103. ret = 0;
  104. break;
  105. default:
  106. ret = -ENODATA;
  107. break;
  108. }
  109. return ret;
  110. }
  111. static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
  112. u16 word)
  113. {
  114. int ret;
  115. switch (reg) {
  116. case PMBUS_IIN_OC_WARN_LIMIT:
  117. ret = pmbus_write_word_data(client, 0,
  118. LM25066_MFR_IIN_OC_WARN_LIMIT,
  119. word);
  120. break;
  121. case PMBUS_PIN_OP_WARN_LIMIT:
  122. ret = pmbus_write_word_data(client, 0,
  123. LM25066_MFR_PIN_OP_WARN_LIMIT,
  124. word);
  125. break;
  126. case PMBUS_VIRT_RESET_PIN_HISTORY:
  127. ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
  128. break;
  129. default:
  130. ret = -ENODATA;
  131. break;
  132. }
  133. return ret;
  134. }
  135. static int lm25066_probe(struct i2c_client *client,
  136. const struct i2c_device_id *id)
  137. {
  138. int config;
  139. struct lm25066_data *data;
  140. struct pmbus_driver_info *info;
  141. if (!i2c_check_functionality(client->adapter,
  142. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  143. return -ENODEV;
  144. data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
  145. GFP_KERNEL);
  146. if (!data)
  147. return -ENOMEM;
  148. config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
  149. if (config < 0)
  150. return config;
  151. data->id = id->driver_data;
  152. info = &data->info;
  153. info->pages = 1;
  154. info->format[PSC_VOLTAGE_IN] = direct;
  155. info->format[PSC_VOLTAGE_OUT] = direct;
  156. info->format[PSC_CURRENT_IN] = direct;
  157. info->format[PSC_TEMPERATURE] = direct;
  158. info->format[PSC_POWER] = direct;
  159. info->m[PSC_TEMPERATURE] = 16;
  160. info->b[PSC_TEMPERATURE] = 0;
  161. info->R[PSC_TEMPERATURE] = 0;
  162. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON | PMBUS_HAVE_VOUT
  163. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN
  164. | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  165. info->read_word_data = lm25066_read_word_data;
  166. info->write_word_data = lm25066_write_word_data;
  167. switch (id->driver_data) {
  168. case lm25066:
  169. info->m[PSC_VOLTAGE_IN] = 22070;
  170. info->b[PSC_VOLTAGE_IN] = 0;
  171. info->R[PSC_VOLTAGE_IN] = -2;
  172. info->m[PSC_VOLTAGE_OUT] = 22070;
  173. info->b[PSC_VOLTAGE_OUT] = 0;
  174. info->R[PSC_VOLTAGE_OUT] = -2;
  175. if (config & LM25066_DEV_SETUP_CL) {
  176. info->m[PSC_CURRENT_IN] = 6852;
  177. info->b[PSC_CURRENT_IN] = 0;
  178. info->R[PSC_CURRENT_IN] = -2;
  179. info->m[PSC_POWER] = 369;
  180. info->b[PSC_POWER] = 0;
  181. info->R[PSC_POWER] = -2;
  182. } else {
  183. info->m[PSC_CURRENT_IN] = 13661;
  184. info->b[PSC_CURRENT_IN] = 0;
  185. info->R[PSC_CURRENT_IN] = -2;
  186. info->m[PSC_POWER] = 736;
  187. info->b[PSC_POWER] = 0;
  188. info->R[PSC_POWER] = -2;
  189. }
  190. break;
  191. case lm5064:
  192. info->m[PSC_VOLTAGE_IN] = 22075;
  193. info->b[PSC_VOLTAGE_IN] = 0;
  194. info->R[PSC_VOLTAGE_IN] = -2;
  195. info->m[PSC_VOLTAGE_OUT] = 22075;
  196. info->b[PSC_VOLTAGE_OUT] = 0;
  197. info->R[PSC_VOLTAGE_OUT] = -2;
  198. if (config & LM25066_DEV_SETUP_CL) {
  199. info->m[PSC_CURRENT_IN] = 6713;
  200. info->b[PSC_CURRENT_IN] = 0;
  201. info->R[PSC_CURRENT_IN] = -2;
  202. info->m[PSC_POWER] = 3619;
  203. info->b[PSC_POWER] = 0;
  204. info->R[PSC_POWER] = -3;
  205. } else {
  206. info->m[PSC_CURRENT_IN] = 13426;
  207. info->b[PSC_CURRENT_IN] = 0;
  208. info->R[PSC_CURRENT_IN] = -2;
  209. info->m[PSC_POWER] = 7238;
  210. info->b[PSC_POWER] = 0;
  211. info->R[PSC_POWER] = -3;
  212. }
  213. break;
  214. case lm5066:
  215. info->m[PSC_VOLTAGE_IN] = 4587;
  216. info->b[PSC_VOLTAGE_IN] = 0;
  217. info->R[PSC_VOLTAGE_IN] = -2;
  218. info->m[PSC_VOLTAGE_OUT] = 4587;
  219. info->b[PSC_VOLTAGE_OUT] = 0;
  220. info->R[PSC_VOLTAGE_OUT] = -2;
  221. if (config & LM25066_DEV_SETUP_CL) {
  222. info->m[PSC_CURRENT_IN] = 10753;
  223. info->b[PSC_CURRENT_IN] = 0;
  224. info->R[PSC_CURRENT_IN] = -2;
  225. info->m[PSC_POWER] = 1204;
  226. info->b[PSC_POWER] = 0;
  227. info->R[PSC_POWER] = -3;
  228. } else {
  229. info->m[PSC_CURRENT_IN] = 5405;
  230. info->b[PSC_CURRENT_IN] = 0;
  231. info->R[PSC_CURRENT_IN] = -2;
  232. info->m[PSC_POWER] = 605;
  233. info->b[PSC_POWER] = 0;
  234. info->R[PSC_POWER] = -3;
  235. }
  236. break;
  237. default:
  238. return -ENODEV;
  239. }
  240. return pmbus_do_probe(client, id, info);
  241. }
  242. static const struct i2c_device_id lm25066_id[] = {
  243. {"lm25066", lm25066},
  244. {"lm5064", lm5064},
  245. {"lm5066", lm5066},
  246. { }
  247. };
  248. MODULE_DEVICE_TABLE(i2c, lm25066_id);
  249. /* This is the driver that will be inserted */
  250. static struct i2c_driver lm25066_driver = {
  251. .driver = {
  252. .name = "lm25066",
  253. },
  254. .probe = lm25066_probe,
  255. .remove = pmbus_do_remove,
  256. .id_table = lm25066_id,
  257. };
  258. module_i2c_driver(lm25066_driver);
  259. MODULE_AUTHOR("Guenter Roeck");
  260. MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
  261. MODULE_LICENSE("GPL");