lm25066.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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, lm5066i };
  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[6][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. [lm5066i] = {
  202. [PSC_VOLTAGE_IN] = {
  203. .m = 4617,
  204. .b = -140,
  205. .R = -2,
  206. },
  207. [PSC_VOLTAGE_OUT] = {
  208. .m = 4602,
  209. .b = 500,
  210. .R = -2,
  211. },
  212. [PSC_CURRENT_IN] = {
  213. .m = 15076,
  214. .b = -504,
  215. .R = -2,
  216. },
  217. [PSC_CURRENT_IN_L] = {
  218. .m = 7645,
  219. .b = 100,
  220. .R = -2,
  221. },
  222. [PSC_POWER] = {
  223. .m = 1701,
  224. .b = -4000,
  225. .R = -3,
  226. },
  227. [PSC_POWER_L] = {
  228. .m = 861,
  229. .b = -965,
  230. .R = -3,
  231. },
  232. [PSC_TEMPERATURE] = {
  233. .m = 16,
  234. },
  235. },
  236. };
  237. struct lm25066_data {
  238. int id;
  239. u16 rlimit; /* Maximum register value */
  240. struct pmbus_driver_info info;
  241. };
  242. #define to_lm25066_data(x) container_of(x, struct lm25066_data, info)
  243. static int lm25066_read_word_data(struct i2c_client *client, int page, int reg)
  244. {
  245. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  246. const struct lm25066_data *data = to_lm25066_data(info);
  247. int ret;
  248. switch (reg) {
  249. case PMBUS_VIRT_READ_VMON:
  250. ret = pmbus_read_word_data(client, 0, LM25066_READ_VAUX);
  251. if (ret < 0)
  252. break;
  253. /* Adjust returned value to match VIN coefficients */
  254. switch (data->id) {
  255. case lm25056:
  256. /* VIN: 6.14 mV VAUX: 293 uV LSB */
  257. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  258. break;
  259. case lm25063:
  260. /* VIN: 6.25 mV VAUX: 200.0 uV LSB */
  261. ret = DIV_ROUND_CLOSEST(ret * 20, 625);
  262. break;
  263. case lm25066:
  264. /* VIN: 4.54 mV VAUX: 283.2 uV LSB */
  265. ret = DIV_ROUND_CLOSEST(ret * 2832, 45400);
  266. break;
  267. case lm5064:
  268. /* VIN: 4.53 mV VAUX: 700 uV LSB */
  269. ret = DIV_ROUND_CLOSEST(ret * 70, 453);
  270. break;
  271. case lm5066:
  272. case lm5066i:
  273. /* VIN: 2.18 mV VAUX: 725 uV LSB */
  274. ret = DIV_ROUND_CLOSEST(ret * 725, 2180);
  275. break;
  276. }
  277. break;
  278. case PMBUS_READ_IIN:
  279. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_IIN);
  280. break;
  281. case PMBUS_READ_PIN:
  282. ret = pmbus_read_word_data(client, 0, LM25066_MFR_READ_PIN);
  283. break;
  284. case PMBUS_IIN_OC_WARN_LIMIT:
  285. ret = pmbus_read_word_data(client, 0,
  286. LM25066_MFR_IIN_OC_WARN_LIMIT);
  287. break;
  288. case PMBUS_PIN_OP_WARN_LIMIT:
  289. ret = pmbus_read_word_data(client, 0,
  290. LM25066_MFR_PIN_OP_WARN_LIMIT);
  291. break;
  292. case PMBUS_VIRT_READ_VIN_AVG:
  293. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VIN);
  294. break;
  295. case PMBUS_VIRT_READ_VOUT_AVG:
  296. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_VOUT);
  297. break;
  298. case PMBUS_VIRT_READ_IIN_AVG:
  299. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_IIN);
  300. break;
  301. case PMBUS_VIRT_READ_PIN_AVG:
  302. ret = pmbus_read_word_data(client, 0, LM25066_READ_AVG_PIN);
  303. break;
  304. case PMBUS_VIRT_READ_PIN_MAX:
  305. ret = pmbus_read_word_data(client, 0, LM25066_READ_PIN_PEAK);
  306. break;
  307. case PMBUS_VIRT_RESET_PIN_HISTORY:
  308. ret = 0;
  309. break;
  310. default:
  311. ret = -ENODATA;
  312. break;
  313. }
  314. return ret;
  315. }
  316. static int lm25063_read_word_data(struct i2c_client *client, int page, int reg)
  317. {
  318. int ret;
  319. switch (reg) {
  320. case PMBUS_VIRT_READ_VOUT_MAX:
  321. ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MAX);
  322. break;
  323. case PMBUS_VIRT_READ_VOUT_MIN:
  324. ret = pmbus_read_word_data(client, 0, LM25063_READ_VOUT_MIN);
  325. break;
  326. default:
  327. ret = lm25066_read_word_data(client, page, reg);
  328. break;
  329. }
  330. return ret;
  331. }
  332. static int lm25056_read_word_data(struct i2c_client *client, int page, int reg)
  333. {
  334. int ret;
  335. switch (reg) {
  336. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  337. ret = pmbus_read_word_data(client, 0,
  338. LM25056_VAUX_UV_WARN_LIMIT);
  339. if (ret < 0)
  340. break;
  341. /* Adjust returned value to match VIN coefficients */
  342. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  343. break;
  344. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  345. ret = pmbus_read_word_data(client, 0,
  346. LM25056_VAUX_OV_WARN_LIMIT);
  347. if (ret < 0)
  348. break;
  349. /* Adjust returned value to match VIN coefficients */
  350. ret = DIV_ROUND_CLOSEST(ret * 293, 6140);
  351. break;
  352. default:
  353. ret = lm25066_read_word_data(client, page, reg);
  354. break;
  355. }
  356. return ret;
  357. }
  358. static int lm25056_read_byte_data(struct i2c_client *client, int page, int reg)
  359. {
  360. int ret, s;
  361. switch (reg) {
  362. case PMBUS_VIRT_STATUS_VMON:
  363. ret = pmbus_read_byte_data(client, 0,
  364. PMBUS_STATUS_MFR_SPECIFIC);
  365. if (ret < 0)
  366. break;
  367. s = 0;
  368. if (ret & LM25056_MFR_STS_VAUX_UV_WARN)
  369. s |= PB_VOLTAGE_UV_WARNING;
  370. if (ret & LM25056_MFR_STS_VAUX_OV_WARN)
  371. s |= PB_VOLTAGE_OV_WARNING;
  372. ret = s;
  373. break;
  374. default:
  375. ret = -ENODATA;
  376. break;
  377. }
  378. return ret;
  379. }
  380. static int lm25066_write_word_data(struct i2c_client *client, int page, int reg,
  381. u16 word)
  382. {
  383. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  384. const struct lm25066_data *data = to_lm25066_data(info);
  385. int ret;
  386. switch (reg) {
  387. case PMBUS_POUT_OP_FAULT_LIMIT:
  388. case PMBUS_POUT_OP_WARN_LIMIT:
  389. case PMBUS_VOUT_UV_WARN_LIMIT:
  390. case PMBUS_OT_FAULT_LIMIT:
  391. case PMBUS_OT_WARN_LIMIT:
  392. case PMBUS_IIN_OC_FAULT_LIMIT:
  393. case PMBUS_VIN_UV_WARN_LIMIT:
  394. case PMBUS_VIN_UV_FAULT_LIMIT:
  395. case PMBUS_VIN_OV_FAULT_LIMIT:
  396. case PMBUS_VIN_OV_WARN_LIMIT:
  397. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
  398. ret = pmbus_write_word_data(client, 0, reg, word);
  399. pmbus_clear_cache(client);
  400. break;
  401. case PMBUS_IIN_OC_WARN_LIMIT:
  402. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
  403. ret = pmbus_write_word_data(client, 0,
  404. LM25066_MFR_IIN_OC_WARN_LIMIT,
  405. word);
  406. pmbus_clear_cache(client);
  407. break;
  408. case PMBUS_PIN_OP_WARN_LIMIT:
  409. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
  410. ret = pmbus_write_word_data(client, 0,
  411. LM25066_MFR_PIN_OP_WARN_LIMIT,
  412. word);
  413. pmbus_clear_cache(client);
  414. break;
  415. case PMBUS_VIRT_VMON_UV_WARN_LIMIT:
  416. /* Adjust from VIN coefficients (for LM25056) */
  417. word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
  418. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
  419. ret = pmbus_write_word_data(client, 0,
  420. LM25056_VAUX_UV_WARN_LIMIT, word);
  421. pmbus_clear_cache(client);
  422. break;
  423. case PMBUS_VIRT_VMON_OV_WARN_LIMIT:
  424. /* Adjust from VIN coefficients (for LM25056) */
  425. word = DIV_ROUND_CLOSEST((int)word * 6140, 293);
  426. word = ((s16)word < 0) ? 0 : clamp_val(word, 0, data->rlimit);
  427. ret = pmbus_write_word_data(client, 0,
  428. LM25056_VAUX_OV_WARN_LIMIT, word);
  429. pmbus_clear_cache(client);
  430. break;
  431. case PMBUS_VIRT_RESET_PIN_HISTORY:
  432. ret = pmbus_write_byte(client, 0, LM25066_CLEAR_PIN_PEAK);
  433. break;
  434. default:
  435. ret = -ENODATA;
  436. break;
  437. }
  438. return ret;
  439. }
  440. static int lm25066_probe(struct i2c_client *client,
  441. const struct i2c_device_id *id)
  442. {
  443. int config;
  444. struct lm25066_data *data;
  445. struct pmbus_driver_info *info;
  446. struct __coeff *coeff;
  447. if (!i2c_check_functionality(client->adapter,
  448. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  449. return -ENODEV;
  450. data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
  451. GFP_KERNEL);
  452. if (!data)
  453. return -ENOMEM;
  454. config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
  455. if (config < 0)
  456. return config;
  457. data->id = id->driver_data;
  458. info = &data->info;
  459. info->pages = 1;
  460. info->format[PSC_VOLTAGE_IN] = direct;
  461. info->format[PSC_VOLTAGE_OUT] = direct;
  462. info->format[PSC_CURRENT_IN] = direct;
  463. info->format[PSC_TEMPERATURE] = direct;
  464. info->format[PSC_POWER] = direct;
  465. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_VMON
  466. | PMBUS_HAVE_PIN | PMBUS_HAVE_IIN | PMBUS_HAVE_STATUS_INPUT
  467. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  468. if (data->id == lm25056) {
  469. info->func[0] |= PMBUS_HAVE_STATUS_VMON;
  470. info->read_word_data = lm25056_read_word_data;
  471. info->read_byte_data = lm25056_read_byte_data;
  472. data->rlimit = 0x0fff;
  473. } else if (data->id == lm25063) {
  474. info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  475. | PMBUS_HAVE_POUT;
  476. info->read_word_data = lm25063_read_word_data;
  477. data->rlimit = 0xffff;
  478. } else {
  479. info->func[0] |= PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  480. info->read_word_data = lm25066_read_word_data;
  481. data->rlimit = 0x0fff;
  482. }
  483. info->write_word_data = lm25066_write_word_data;
  484. coeff = &lm25066_coeff[data->id][0];
  485. info->m[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].m;
  486. info->b[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].b;
  487. info->R[PSC_TEMPERATURE] = coeff[PSC_TEMPERATURE].R;
  488. info->m[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].m;
  489. info->b[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].b;
  490. info->R[PSC_VOLTAGE_IN] = coeff[PSC_VOLTAGE_IN].R;
  491. info->m[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].m;
  492. info->b[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].b;
  493. info->R[PSC_VOLTAGE_OUT] = coeff[PSC_VOLTAGE_OUT].R;
  494. info->R[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].R;
  495. info->R[PSC_POWER] = coeff[PSC_POWER].R;
  496. if (config & LM25066_DEV_SETUP_CL) {
  497. info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].m;
  498. info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN_L].b;
  499. info->m[PSC_POWER] = coeff[PSC_POWER_L].m;
  500. info->b[PSC_POWER] = coeff[PSC_POWER_L].b;
  501. } else {
  502. info->m[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].m;
  503. info->b[PSC_CURRENT_IN] = coeff[PSC_CURRENT_IN].b;
  504. info->m[PSC_POWER] = coeff[PSC_POWER].m;
  505. info->b[PSC_POWER] = coeff[PSC_POWER].b;
  506. }
  507. return pmbus_do_probe(client, id, info);
  508. }
  509. static const struct i2c_device_id lm25066_id[] = {
  510. {"lm25056", lm25056},
  511. {"lm25063", lm25063},
  512. {"lm25066", lm25066},
  513. {"lm5064", lm5064},
  514. {"lm5066", lm5066},
  515. {"lm5066i", lm5066i},
  516. { }
  517. };
  518. MODULE_DEVICE_TABLE(i2c, lm25066_id);
  519. /* This is the driver that will be inserted */
  520. static struct i2c_driver lm25066_driver = {
  521. .driver = {
  522. .name = "lm25066",
  523. },
  524. .probe = lm25066_probe,
  525. .remove = pmbus_do_remove,
  526. .id_table = lm25066_id,
  527. };
  528. module_i2c_driver(lm25066_driver);
  529. MODULE_AUTHOR("Guenter Roeck");
  530. MODULE_DESCRIPTION("PMBus driver for LM25066 and compatible chips");
  531. MODULE_LICENSE("GPL");