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