lm25066.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Hardware monitoring driver for LM25056 / 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 { lm25056, 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. /* LM25056 only */
  43. #define LM25056_VAUX_OV_WARN_LIMIT 0xe3
  44. #define LM25056_VAUX_UV_WARN_LIMIT 0xe4
  45. #define LM25056_MFR_STS_VAUX_OV_WARN (1 << 1)
  46. #define LM25056_MFR_STS_VAUX_UV_WARN (1 << 0)
  47. struct __coeff {
  48. short m, b, R;
  49. };
  50. #define PSC_CURRENT_IN_L (PSC_NUM_CLASSES)
  51. #define PSC_POWER_L (PSC_NUM_CLASSES + 1)
  52. static struct __coeff lm25066_coeff[4][PSC_NUM_CLASSES + 2] = {
  53. [lm25056] = {
  54. [PSC_VOLTAGE_IN] = {
  55. .m = 16296,
  56. .R = -2,
  57. },
  58. [PSC_CURRENT_IN] = {
  59. .m = 13797,
  60. .R = -2,
  61. },
  62. [PSC_CURRENT_IN_L] = {
  63. .m = 6726,
  64. .R = -2,
  65. },
  66. [PSC_POWER] = {
  67. .m = 5501,
  68. .R = -3,
  69. },
  70. [PSC_POWER_L] = {
  71. .m = 26882,
  72. .R = -4,
  73. },
  74. [PSC_TEMPERATURE] = {
  75. .m = 1580,
  76. .b = -14500,
  77. .R = -2,
  78. },
  79. },
  80. [lm25066] = {
  81. [PSC_VOLTAGE_IN] = {
  82. .m = 22070,
  83. .R = -2,
  84. },
  85. [PSC_VOLTAGE_OUT] = {
  86. .m = 22070,
  87. .R = -2,
  88. },
  89. [PSC_CURRENT_IN] = {
  90. .m = 13661,
  91. .R = -2,
  92. },
  93. [PSC_CURRENT_IN_L] = {
  94. .m = 6852,
  95. .R = -2,
  96. },
  97. [PSC_POWER] = {
  98. .m = 736,
  99. .R = -2,
  100. },
  101. [PSC_POWER_L] = {
  102. .m = 369,
  103. .R = -2,
  104. },
  105. [PSC_TEMPERATURE] = {
  106. .m = 16,
  107. },
  108. },
  109. [lm5064] = {
  110. [PSC_VOLTAGE_IN] = {
  111. .m = 4611,
  112. .R = -2,
  113. },
  114. [PSC_VOLTAGE_OUT] = {
  115. .m = 4621,
  116. .R = -2,
  117. },
  118. [PSC_CURRENT_IN] = {
  119. .m = 10742,
  120. .R = -2,
  121. },
  122. [PSC_CURRENT_IN_L] = {
  123. .m = 5456,
  124. .R = -2,
  125. },
  126. [PSC_POWER] = {
  127. .m = 1204,
  128. .R = -3,
  129. },
  130. [PSC_POWER_L] = {
  131. .m = 612,
  132. .R = -3,
  133. },
  134. [PSC_TEMPERATURE] = {
  135. .m = 16,
  136. },
  137. },
  138. [lm5066] = {
  139. [PSC_VOLTAGE_IN] = {
  140. .m = 4587,
  141. .R = -2,
  142. },
  143. [PSC_VOLTAGE_OUT] = {
  144. .m = 4587,
  145. .R = -2,
  146. },
  147. [PSC_CURRENT_IN] = {
  148. .m = 10753,
  149. .R = -2,
  150. },
  151. [PSC_CURRENT_IN_L] = {
  152. .m = 5405,
  153. .R = -2,
  154. },
  155. [PSC_POWER] = {
  156. .m = 1204,
  157. .R = -3,
  158. },
  159. [PSC_POWER_L] = {
  160. .m = 605,
  161. .R = -3,
  162. },
  163. [PSC_TEMPERATURE] = {
  164. .m = 16,
  165. },
  166. },
  167. };
  168. struct lm25066_data {
  169. int id;
  170. struct pmbus_driver_info info;
  171. };
  172. #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
  173. static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
  174. {
  175. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  176. const struct lm25066_data *data = to_lm25066_data(info);
  177. int ret;
  178. switch (reg) {
  179. case PMBUS_VIRT_READ_VMON:
  180. ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
  181. if (ret < 0)
  182. break;
  183. /* Adjust returned value to match VIN coefficients */
  184. switch (data->id) {
  185. case lm25056:
  186. /* VIN: 6.14 mV VAUX: 293 uV LSB */
  187. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  188. break;
  189. case lm25066:
  190. /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
  191. ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
  192. break;
  193. case lm5064:
  194. /* VIN: 4.53 mV VAUX: 700 uV LSB */
  195. ret = DIV_ROUND_CLOSEST(ret * 70, 453);
  196. break;
  197. case lm5066:
  198. /* VIN: 2.18 mV VAUX: 725 uV LSB */
  199. ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
  200. break;
  201. }
  202. break;
  203. case PMBUS_READ_IIN:
  204. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
  205. break;
  206. case PMBUS_READ_PIN:
  207. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
  208. break;
  209. case PMBUS_IIN_OC_WARN_LIMIT:
  210. ret = pmbus_read_word_data(client, 0,
  211. LM25066_MFR_IIN_OC_WARN_LIMIT);
  212. break;
  213. case PMBUS_PIN_OP_WARN_LIMIT:
  214. ret = pmbus_read_word_data(client, 0,
  215. LM25066_MFR_PIN_OP_WARN_LIMIT);
  216. break;
  217. case PMBUS_VIRT_READ_VIN_AVG:
  218. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
  219. break;
  220. case PMBUS_VIRT_READ_VOUT_AVG:
  221. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
  222. break;
  223. case PMBUS_VIRT_READ_IIN_AVG:
  224. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
  225. break;
  226. case PMBUS_VIRT_READ_PIN_AVG:
  227. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
  228. break;
  229. case PMBUS_VIRT_READ_PIN_MAX:
  230. ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
  231. break;
  232. case PMBUS_VIRT_RESET_PIN_HISTORY:
  233. ret = 0;
  234. break;
  235. default:
  236. ret = -ENODATA;
  237. break;
  238. }
  239. return ret;
  240. }
  241. static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
  242. {
  243. int ret;
  244. switch (reg) {
  245. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  246. ret = pmbus_read_word_data(client, 0,
  247. LM25056_VAUX_UV_WARN_LIMIT);
  248. if (ret < 0)
  249. break;
  250. /* Adjust returned value to match VIN coefficients */
  251. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  252. break;
  253. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  254. ret = pmbus_read_word_data(client, 0,
  255. LM25056_VAUX_OV_WARN_LIMIT);
  256. if (ret < 0)
  257. break;
  258. /* Adjust returned value to match VIN coefficients */
  259. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  260. break;
  261. default:
  262. ret = lm25066_read_word_data(client, page, reg);
  263. break;
  264. }
  265. return ret;
  266. }
  267. static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
  268. {
  269. int ret, s;
  270. switch (reg) {
  271. case PMBUS_VIRT_STATUS_VMON:
  272. ret = pmbus_read_byte_data(client, 0,
  273. PMBUS_STATUS_MFR_SPECIFIC);
  274. if (ret < 0)
  275. break;
  276. s = 0;
  277. if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
  278. s |= PB_VOLTAGE_UV_WARNING;
  279. if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
  280. s |= PB_VOLTAGE_OV_WARNING;
  281. ret = s;
  282. break;
  283. default:
  284. ret = -ENODATA;
  285. break;
  286. }
  287. return ret;
  288. }
  289. static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
  290. u16 word)
  291. {
  292. int ret;
  293. switch (reg) {
  294. case PMBUS_VOUT_UV_WARN_LIMIT:
  295. case PMBUS_OT_FAULT_LIMIT:
  296. case PMBUS_OT_WARN_LIMIT:
  297. case PMBUS_VIN_UV_WARN_LIMIT:
  298. case PMBUS_VIN_OV_WARN_LIMIT:
  299. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  300. ret = pmbus_write_word_data(client, 0, reg, word);
  301. pmbus_clear_cache(client);
  302. break;
  303. case PMBUS_IIN_OC_WARN_LIMIT:
  304. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  305. ret = pmbus_write_word_data(client, 0,
  306. LM25066_MFR_IIN_OC_WARN_LIMIT,
  307. word);
  308. pmbus_clear_cache(client);
  309. break;
  310. case PMBUS_PIN_OP_WARN_LIMIT:
  311. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  312. ret = pmbus_write_word_data(client, 0,
  313. LM25066_MFR_PIN_OP_WARN_LIMIT,
  314. word);
  315. pmbus_clear_cache(client);
  316. break;
  317. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  318. /* Adjust from VIN coefficients (for LM25056) */
  319. word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
  320. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  321. ret = pmbus_write_word_data(client, 0,
  322. LM25056_VAUX_UV_WARN_LIMIT, word);
  323. pmbus_clear_cache(client);
  324. break;
  325. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  326. /* Adjust from VIN coefficients (for LM25056) */
  327. word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
  328. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, 0x0fff);
  329. ret = pmbus_write_word_data(client, 0,
  330. LM25056_VAUX_OV_WARN_LIMIT, word);
  331. pmbus_clear_cache(client);
  332. break;
  333. case PMBUS_VIRT_RESET_PIN_HISTORY:
  334. ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
  335. break;
  336. default:
  337. ret = -ENODATA;
  338. break;
  339. }
  340. return ret;
  341. }
  342. static int lm25066_probe(struct i2c_client *client,
  343. const struct i2c_device_id *id)
  344. {
  345. int config;
  346. struct lm25066_data *data;
  347. struct pmbus_driver_info *info;
  348. struct __coeff *coeff;
  349. if (!i2c_check_functionality(client->adapter,
  350. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  351. return -ENODEV;
  352. data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
  353. GFP_KERNEL);
  354. if (!data)
  355. return -ENOMEM;
  356. config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
  357. if (config < 0)
  358. return config;
  359. data->id = id->driver_data;
  360. info = &data->info;
  361. info->pages = 1;
  362. info->format[PSC_VOLTAGE_IN] = direct;
  363. info->format[PSC_VOLTAGE_OUT] = direct;
  364. info->format[PSC_CURRENT_IN] = direct;
  365. info->format[PSC_TEMPERATURE] = direct;
  366. info->format[PSC_POWER] = direct;
  367. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
  368. | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
  369. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  370. if (data->id == lm25056) {
  371. info->func[0] |= PMBUS_HAVE_STATUS_VMON;
  372. info->read_word_data = lm25056_read_word_data;
  373. info->read_byte_data = lm25056_read_byte_data;
  374. } else {
  375. info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  376. info->read_word_data = lm25066_read_word_data;
  377. }
  378. info->write_word_data = lm25066_write_word_data;
  379. coeff = &lm25066_coeff[data->id][0];
  380. info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
  381. info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
  382. info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
  383. info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
  384. info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
  385. info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
  386. info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
  387. info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
  388. info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
  389. info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
  390. info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
  391. info->b[PSC_POWER] = coeff[PSC_POWER].b;
  392. info->R[PSC_POWER] = coeff[PSC_POWER].R;
  393. if (config & LM25066_DEV_SETUP_CL) {
  394. info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
  395. info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
  396. } else {
  397. info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
  398. info->m[PSC_POWER] = coeff[PSC_POWER].m;
  399. }
  400. return pmbus_do_probe(client, id, info);
  401. }
  402. static const struct i2c_device_id lm25066_id[] = {
  403. {"lm25056", lm25056},
  404. {"lm25066", lm25066},
  405. {"lm5064", lm5064},
  406. {"lm5066", lm5066},
  407. { }
  408. };
  409. MODULE_DEVICE_TABLE(i2c, lm25066_id);
  410. /* This is the driver that will be inserted */
  411. static struct i2c_driver lm25066_driver = {
  412. .driver = {
  413. .name = "lm25066",
  414. },
  415. .probe = lm25066_probe,
  416. .remove = pmbus_do_remove,
  417. .id_table = lm25066_id,
  418. };
  419. module_i2c_driver(lm25066_driver);
  420. MODULE_AUTHOR("Guenter Roeck");
  421. MODULE_DESCRIPTION("PMBus driver for LM25056/LM25066/LM5064/LM5066");
  422. MODULE_LICENSE("GPL");