lm25066.c 13 KB

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