lm25066.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_VOUT_UV_WARN_LIMIT:
  117. case PMBUS_OT_FAULT_LIMIT:
  118. case PMBUS_OT_WARN_LIMIT:
  119. case PMBUS_VIN_UV_WARN_LIMIT:
  120. case PMBUS_VIN_OV_WARN_LIMIT:
  121. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  122. ret = pmbus_write_word_data(client, 0, reg, word);
  123. pmbus_clear_cache(client);
  124. break;
  125. case PMBUS_IIN_OC_WARN_LIMIT:
  126. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  127. ret = pmbus_write_word_data(client, 0,
  128. LM25066_MFR_IIN_OC_WARN_LIMIT,
  129. word);
  130. pmbus_clear_cache(client);
  131. break;
  132. case PMBUS_PIN_OP_WARN_LIMIT:
  133. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  134. ret = pmbus_write_word_data(client, 0,
  135. LM25066_MFR_PIN_OP_WARN_LIMIT,
  136. word);
  137. pmbus_clear_cache(client);
  138. break;
  139. case PMBUS_VIRT_RESET_PIN_HISTORY:
  140. ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
  141. break;
  142. default:
  143. ret = -ENODATA;
  144. break;
  145. }
  146. return ret;
  147. }
  148. static int lm25066_probe(struct i2c_client *client,
  149. const struct i2c_device_id *id)
  150. {
  151. int config;
  152. struct lm25066_data *data;
  153. struct pmbus_driver_info *info;
  154. if (!i2c_check_functionality(client->adapter,
  155. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  156. return -ENODEV;
  157. data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
  158. GFP_KERNEL);
  159. if (!data)
  160. return -ENOMEM;
  161. config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
  162. if (config < 0)
  163. return config;
  164. data->id = id->driver_data;
  165. info = &data->info;
  166. info->pages = 1;
  167. info->format[PSC_VOLTAGE_IN] = direct;
  168. info->format[PSC_VOLTAGE_OUT] = direct;
  169. info->format[PSC_CURRENT_IN] = direct;
  170. info->format[PSC_TEMPERATURE] = direct;
  171. info->format[PSC_POWER] = direct;
  172. info->m[PSC_TEMPERATURE] = 16;
  173. info->b[PSC_TEMPERATURE] = 0;
  174. info->R[PSC_TEMPERATURE] = 0;
  175. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON | PMBUS_HAVE_VOUT
  176. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN
  177. | PMBUS_HAVE_STATUS_INPUT | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  178. info->read_word_data = lm25066_read_word_data;
  179. info->write_word_data = lm25066_write_word_data;
  180. switch (id->driver_data) {
  181. case lm25066:
  182. info->m[PSC_VOLTAGE_IN] = 22070;
  183. info->b[PSC_VOLTAGE_IN] = 0;
  184. info->R[PSC_VOLTAGE_IN] = -2;
  185. info->m[PSC_VOLTAGE_OUT] = 22070;
  186. info->b[PSC_VOLTAGE_OUT] = 0;
  187. info->R[PSC_VOLTAGE_OUT] = -2;
  188. if (config & LM25066_DEV_SETUP_CL) {
  189. info->m[PSC_CURRENT_IN] = 6852;
  190. info->b[PSC_CURRENT_IN] = 0;
  191. info->R[PSC_CURRENT_IN] = -2;
  192. info->m[PSC_POWER] = 369;
  193. info->b[PSC_POWER] = 0;
  194. info->R[PSC_POWER] = -2;
  195. } else {
  196. info->m[PSC_CURRENT_IN] = 13661;
  197. info->b[PSC_CURRENT_IN] = 0;
  198. info->R[PSC_CURRENT_IN] = -2;
  199. info->m[PSC_POWER] = 736;
  200. info->b[PSC_POWER] = 0;
  201. info->R[PSC_POWER] = -2;
  202. }
  203. break;
  204. case lm5064:
  205. info->m[PSC_VOLTAGE_IN] = 22075;
  206. info->b[PSC_VOLTAGE_IN] = 0;
  207. info->R[PSC_VOLTAGE_IN] = -2;
  208. info->m[PSC_VOLTAGE_OUT] = 22075;
  209. info->b[PSC_VOLTAGE_OUT] = 0;
  210. info->R[PSC_VOLTAGE_OUT] = -2;
  211. if (config & LM25066_DEV_SETUP_CL) {
  212. info->m[PSC_CURRENT_IN] = 6713;
  213. info->b[PSC_CURRENT_IN] = 0;
  214. info->R[PSC_CURRENT_IN] = -2;
  215. info->m[PSC_POWER] = 3619;
  216. info->b[PSC_POWER] = 0;
  217. info->R[PSC_POWER] = -3;
  218. } else {
  219. info->m[PSC_CURRENT_IN] = 13426;
  220. info->b[PSC_CURRENT_IN] = 0;
  221. info->R[PSC_CURRENT_IN] = -2;
  222. info->m[PSC_POWER] = 7238;
  223. info->b[PSC_POWER] = 0;
  224. info->R[PSC_POWER] = -3;
  225. }
  226. break;
  227. case lm5066:
  228. info->m[PSC_VOLTAGE_IN] = 4587;
  229. info->b[PSC_VOLTAGE_IN] = 0;
  230. info->R[PSC_VOLTAGE_IN] = -2;
  231. info->m[PSC_VOLTAGE_OUT] = 4587;
  232. info->b[PSC_VOLTAGE_OUT] = 0;
  233. info->R[PSC_VOLTAGE_OUT] = -2;
  234. if (config & LM25066_DEV_SETUP_CL) {
  235. info->m[PSC_CURRENT_IN] = 10753;
  236. info->b[PSC_CURRENT_IN] = 0;
  237. info->R[PSC_CURRENT_IN] = -2;
  238. info->m[PSC_POWER] = 1204;
  239. info->b[PSC_POWER] = 0;
  240. info->R[PSC_POWER] = -3;
  241. } else {
  242. info->m[PSC_CURRENT_IN] = 5405;
  243. info->b[PSC_CURRENT_IN] = 0;
  244. info->R[PSC_CURRENT_IN] = -2;
  245. info->m[PSC_POWER] = 605;
  246. info->b[PSC_POWER] = 0;
  247. info->R[PSC_POWER] = -3;
  248. }
  249. break;
  250. default:
  251. return -ENODEV;
  252. }
  253. return pmbus_do_probe(client, id, info);
  254. }
  255. static const struct i2c_device_id lm25066_id[] = {
  256. {"lm25066", lm25066},
  257. {"lm5064", lm5064},
  258. {"lm5066", lm5066},
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(i2c, lm25066_id);
  262. /* This is the driver that will be inserted */
  263. static struct i2c_driver lm25066_driver = {
  264. .driver = {
  265. .name = "lm25066",
  266. },
  267. .probe = lm25066_probe,
  268. .remove = pmbus_do_remove,
  269. .id_table = lm25066_id,
  270. };
  271. module_i2c_driver(lm25066_driver);
  272. MODULE_AUTHOR("Guenter Roeck");
  273. MODULE_DESCRIPTION("PMBus driver for LM25066/LM5064/LM5066");
  274. MODULE_LICENSE("GPL");