lm25066.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Hardware monitoring driver for LM25066 / LM5064 / LM5066
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include "pmbus.h"
  27. enum chips { lm25066, lm5064, lm5066 };
  28. #define LM25066_READ_VAUX 0xd0
  29. #define LM25066_MFR_READ_IIN 0xd1
  30. #define LM25066_MFR_READ_PIN 0xd2
  31. #define LM25066_MFR_IIN_OC_WARN_LIMIT 0xd3
  32. #define LM25066_MFR_PIN_OP_WARN_LIMIT 0xd4
  33. #define LM25066_READ_PIN_PEAK 0xd5
  34. #define LM25066_CLEAR_PIN_PEAK 0xd6
  35. #define LM25066_DEVICE_SETUP 0xd9
  36. #define LM25066_READ_AVG_VIN 0xdc
  37. #define LM25066_READ_AVG_VOUT 0xdd
  38. #define LM25066_READ_AVG_IIN 0xde
  39. #define LM25066_READ_AVG_PIN 0xdf
  40. #define LM25066_DEV_SETUP_CL (1 << 4) /* Current limit */
  41. struct lm25066_data {
  42. int id;
  43. struct pmbus_driver_info info;
  44. };
  45. #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
  46. static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
  47. {
  48. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  49. const struct lm25066_data *data = to_lm25066_data(info);
  50. int ret;
  51. if (page > 1)
  52. return -ENXIO;
  53. /* Map READ_VAUX into READ_VOUT register on page 1 */
  54. if (page == 1) {
  55. switch (reg) {
  56. case PMBUS_READ_VOUT:
  57. ret = pmbus_read_word_data(client, 0,
  58. LM25066_READ_VAUX);
  59. if (ret < 0)
  60. break;
  61. /* Adjust returned value to match VOUT coefficients */
  62. switch (data->id) {
  63. case lm25066:
  64. /* VOUT: 4.54 mV VAUX: 283.2 uV LSB */
  65. ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
  66. break;
  67. case lm5064:
  68. /* VOUT: 4.53 mV VAUX: 700 uV LSB */
  69. ret = DIV_ROUND_CLOSEST(ret * 70, 453);
  70. break;
  71. case lm5066:
  72. /* VOUT: 2.18 mV VAUX: 725 uV LSB */
  73. ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
  74. break;
  75. }
  76. break;
  77. default:
  78. /* No other valid registers on page 1 */
  79. ret = -ENXIO;
  80. break;
  81. }
  82. goto done;
  83. }
  84. switch (reg) {
  85. case PMBUS_READ_IIN:
  86. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
  87. break;
  88. case PMBUS_READ_PIN:
  89. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
  90. break;
  91. case PMBUS_IIN_OC_WARN_LIMIT:
  92. ret = pmbus_read_word_data(client, 0,
  93. LM25066_MFR_IIN_OC_WARN_LIMIT);
  94. break;
  95. case PMBUS_PIN_OP_WARN_LIMIT:
  96. ret = pmbus_read_word_data(client, 0,
  97. LM25066_MFR_PIN_OP_WARN_LIMIT);
  98. break;
  99. case PMBUS_VIRT_READ_VIN_AVG:
  100. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
  101. break;
  102. case PMBUS_VIRT_READ_VOUT_AVG:
  103. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
  104. break;
  105. case PMBUS_VIRT_READ_IIN_AVG:
  106. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
  107. break;
  108. case PMBUS_VIRT_READ_PIN_AVG:
  109. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
  110. break;
  111. case PMBUS_VIRT_READ_PIN_MAX:
  112. ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
  113. break;
  114. case PMBUS_VIRT_RESET_PIN_HISTORY:
  115. ret = 0;
  116. break;
  117. default:
  118. ret = -ENODATA;
  119. break;
  120. }
  121. done:
  122. return ret;
  123. }
  124. static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
  125. u16 word)
  126. {
  127. int ret;
  128. if (page > 1)
  129. return -ENXIO;
  130. switch (reg) {
  131. case PMBUS_IIN_OC_WARN_LIMIT:
  132. ret = pmbus_write_word_data(client, 0,
  133. LM25066_MFR_IIN_OC_WARN_LIMIT,
  134. word);
  135. break;
  136. case PMBUS_PIN_OP_WARN_LIMIT:
  137. ret = pmbus_write_word_data(client, 0,
  138. LM25066_MFR_PIN_OP_WARN_LIMIT,
  139. word);
  140. break;
  141. case PMBUS_VIRT_RESET_PIN_HISTORY:
  142. ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
  143. break;
  144. default:
  145. ret = -ENODATA;
  146. break;
  147. }
  148. return ret;
  149. }
  150. static int lm25066_write_byte(struct i2c_client *client, int page, u8 value)
  151. {
  152. if (page > 1)
  153. return -ENXIO;
  154. if (page <= 0)
  155. return pmbus_write_byte(client, page, value);
  156. return 0;
  157. }
  158. static int lm25066_probe(struct i2c_client *client,
  159. const struct i2c_device_id *id)
  160. {
  161. int config;
  162. struct lm25066_data *data;
  163. struct pmbus_driver_info *info;
  164. if (!i2c_check_functionality(client->adapter,
  165. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  166. return -ENODEV;
  167. data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
  168. GFP_KERNEL);
  169. if (!data)
  170. return -ENOMEM;
  171. config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
  172. if (config < 0)
  173. return config;
  174. data->id = id->driver_data;
  175. info = &data->info;
  176. info->pages = 2;
  177. info->format[PSC_VOLTAGE_IN] = direct;
  178. info->format[PSC_VOLTAGE_OUT] = direct;
  179. info->format[PSC_CURRENT_IN] = direct;
  180. info->format[PSC_TEMPERATURE] = direct;
  181. info->format[PSC_POWER] = direct;
  182. info->m[PSC_TEMPERATURE] = 16;
  183. info->b[PSC_TEMPERATURE] = 0;
  184. info->R[PSC_TEMPERATURE] = 0;
  185. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT
  186. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN
  187. | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  188. info->func[1] = PMBUS_HAVE_VOUT;
  189. info->read_word_data = lm25066_read_word_data;
  190. info->write_word_data = lm25066_write_word_data;
  191. info->write_byte = lm25066_write_byte;
  192. switch (id->driver_data) {
  193. case lm25066:
  194. info->m[PSC_VOLTAGE_IN] = 22070;
  195. info->b[PSC_VOLTAGE_IN] = 0;
  196. info->R[PSC_VOLTAGE_IN] = -2;
  197. info->m[PSC_VOLTAGE_OUT] = 22070;
  198. info->b[PSC_VOLTAGE_OUT] = 0;
  199. info->R[PSC_VOLTAGE_OUT] = -2;
  200. if (config & LM25066_DEV_SETUP_CL) {
  201. info->m[PSC_CURRENT_IN] = 6852;
  202. info->b[PSC_CURRENT_IN] = 0;
  203. info->R[PSC_CURRENT_IN] = -2;
  204. info->m[PSC_POWER] = 369;
  205. info->b[PSC_POWER] = 0;
  206. info->R[PSC_POWER] = -2;
  207. } else {
  208. info->m[PSC_CURRENT_IN] = 13661;
  209. info->b[PSC_CURRENT_IN] = 0;
  210. info->R[PSC_CURRENT_IN] = -2;
  211. info->m[PSC_POWER] = 736;
  212. info->b[PSC_POWER] = 0;
  213. info->R[PSC_POWER] = -2;
  214. }
  215. break;
  216. case lm5064:
  217. info->m[PSC_VOLTAGE_IN] = 22075;
  218. info->b[PSC_VOLTAGE_IN] = 0;
  219. info->R[PSC_VOLTAGE_IN] = -2;
  220. info->m[PSC_VOLTAGE_OUT] = 22075;
  221. info->b[PSC_VOLTAGE_OUT] = 0;
  222. info->R[PSC_VOLTAGE_OUT] = -2;
  223. if (config & LM25066_DEV_SETUP_CL) {
  224. info->m[PSC_CURRENT_IN] = 6713;
  225. info->b[PSC_CURRENT_IN] = 0;
  226. info->R[PSC_CURRENT_IN] = -2;
  227. info->m[PSC_POWER] = 3619;
  228. info->b[PSC_POWER] = 0;
  229. info->R[PSC_POWER] = -3;
  230. } else {
  231. info->m[PSC_CURRENT_IN] = 13426;
  232. info->b[PSC_CURRENT_IN] = 0;
  233. info->R[PSC_CURRENT_IN] = -2;
  234. info->m[PSC_POWER] = 7238;
  235. info->b[PSC_POWER] = 0;
  236. info->R[PSC_POWER] = -3;
  237. }
  238. break;
  239. case lm5066:
  240. info->m[PSC_VOLTAGE_IN] = 4587;
  241. info->b[PSC_VOLTAGE_IN] = 0;
  242. info->R[PSC_VOLTAGE_IN] = -2;
  243. info->m[PSC_VOLTAGE_OUT] = 4587;
  244. info->b[PSC_VOLTAGE_OUT] = 0;
  245. info->R[PSC_VOLTAGE_OUT] = -2;
  246. if (config & LM25066_DEV_SETUP_CL) {
  247. info->m[PSC_CURRENT_IN] = 10753;
  248. info->b[PSC_CURRENT_IN] = 0;
  249. info->R[PSC_CURRENT_IN] = -2;
  250. info->m[PSC_POWER] = 1204;
  251. info->b[PSC_POWER] = 0;
  252. info->R[PSC_POWER] = -3;
  253. } else {
  254. info->m[PSC_CURRENT_IN] = 5405;
  255. info->b[PSC_CURRENT_IN] = 0;
  256. info->R[PSC_CURRENT_IN] = -2;
  257. info->m[PSC_POWER] = 605;
  258. info->b[PSC_POWER] = 0;
  259. info->R[PSC_POWER] = -3;
  260. }
  261. break;
  262. default:
  263. return -ENODEV;
  264. }
  265. return pmbus_do_probe(client, id, info);
  266. }
  267. static const struct i2c_device_id lm25066_id[] = {
  268. {"lm25066", lm25066},
  269. {"lm5064", lm5064},
  270. {"lm5066", lm5066},
  271. { }
  272. };
  273. MODULE_DEVICE_TABLE(i2c, lm25066_id);
  274. /* This is the driver that will be inserted */
  275. static struct i2c_driver lm25066_driver = {
  276. .driver = {
  277. .name = "lm25066",
  278. },
  279. .probe = lm25066_probe,
  280. .remove = pmbus_do_remove,
  281. .id_table = lm25066_id,
  282. };
  283. module_i2c_driver(lm25066_driver);
  284. MODULE_AUTHOR("Guenter Roeck");
  285. MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
  286. MODULE_LICENSE("GPL");