ltc2978.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * Hardware monitoring driver for LTC2978 and compatible chips.
  3. *
  4. * Copyright (c) 2011 Ericsson AB.
  5. * Copyright (c) 2013, 2014, 2015 Guenter Roeck
  6. * Copyright (c) 2015 Linear Technology
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/err.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/regulator/driver.h>
  27. #include "pmbus.h"
  28. enum chips { ltc2974, ltc2975, ltc2977, ltc2978, ltc2980, ltc3880, ltc3882,
  29. ltc3883, ltc3886, ltc3887, ltm2987, ltm4675, ltm4676 };
  30. /* Common for all chips */
  31. #define LTC2978_MFR_VOUT_PEAK 0xdd
  32. #define LTC2978_MFR_VIN_PEAK 0xde
  33. #define LTC2978_MFR_TEMPERATURE_PEAK 0xdf
  34. #define LTC2978_MFR_SPECIAL_ID 0xe7 /* Undocumented on LTC3882 */
  35. #define LTC2978_MFR_COMMON 0xef
  36. /* LTC2974, LTC2975, LCT2977, LTC2980, LTC2978, and LTM2987 */
  37. #define LTC2978_MFR_VOUT_MIN 0xfb
  38. #define LTC2978_MFR_VIN_MIN 0xfc
  39. #define LTC2978_MFR_TEMPERATURE_MIN 0xfd
  40. /* LTC2974, LTC2975 */
  41. #define LTC2974_MFR_IOUT_PEAK 0xd7
  42. #define LTC2974_MFR_IOUT_MIN 0xd8
  43. /* LTC3880, LTC3882, LTC3883, LTC3887, LTM4675, and LTM4676 */
  44. #define LTC3880_MFR_IOUT_PEAK 0xd7
  45. #define LTC3880_MFR_CLEAR_PEAKS 0xe3
  46. #define LTC3880_MFR_TEMPERATURE2_PEAK 0xf4
  47. /* LTC3883 and LTC3886 only */
  48. #define LTC3883_MFR_IIN_PEAK 0xe1
  49. /* LTC2975 only */
  50. #define LTC2975_MFR_IIN_PEAK 0xc4
  51. #define LTC2975_MFR_IIN_MIN 0xc5
  52. #define LTC2975_MFR_PIN_PEAK 0xc6
  53. #define LTC2975_MFR_PIN_MIN 0xc7
  54. #define LTC2978_ID_MASK 0xfff0
  55. #define LTC2974_ID 0x0210
  56. #define LTC2975_ID 0x0220
  57. #define LTC2977_ID 0x0130
  58. #define LTC2978_ID_REV1 0x0110 /* Early revision */
  59. #define LTC2978_ID_REV2 0x0120
  60. #define LTC2980_ID_A 0x8030 /* A/B for two die IDs */
  61. #define LTC2980_ID_B 0x8040
  62. #define LTC3880_ID 0x4020
  63. #define LTC3882_ID 0x4200
  64. #define LTC3882_ID_D1 0x4240 /* Dash 1 */
  65. #define LTC3883_ID 0x4300
  66. #define LTC3886_ID 0x4600
  67. #define LTC3887_ID 0x4700
  68. #define LTM2987_ID_A 0x8010 /* A/B for two die IDs */
  69. #define LTM2987_ID_B 0x8020
  70. #define LTM4675_ID 0x47a0
  71. #define LTM4676_ID_REV1 0x4400
  72. #define LTM4676_ID_REV2 0x4480
  73. #define LTM4676A_ID 0x47e0
  74. #define LTC2974_NUM_PAGES 4
  75. #define LTC2978_NUM_PAGES 8
  76. #define LTC3880_NUM_PAGES 2
  77. #define LTC3883_NUM_PAGES 1
  78. #define LTC_POLL_TIMEOUT 100 /* in milli-seconds */
  79. #define LTC_NOT_BUSY BIT(5)
  80. #define LTC_NOT_PENDING BIT(4)
  81. /*
  82. * LTC2978 clears peak data whenever the CLEAR_FAULTS command is executed, which
  83. * happens pretty much each time chip data is updated. Raw peak data therefore
  84. * does not provide much value. To be able to provide useful peak data, keep an
  85. * internal cache of measured peak data, which is only cleared if an explicit
  86. * "clear peak" command is executed for the sensor in question.
  87. */
  88. struct ltc2978_data {
  89. enum chips id;
  90. u16 vin_min, vin_max;
  91. u16 temp_min[LTC2974_NUM_PAGES], temp_max[LTC2974_NUM_PAGES];
  92. u16 vout_min[LTC2978_NUM_PAGES], vout_max[LTC2978_NUM_PAGES];
  93. u16 iout_min[LTC2974_NUM_PAGES], iout_max[LTC2974_NUM_PAGES];
  94. u16 iin_min, iin_max;
  95. u16 pin_min, pin_max;
  96. u16 temp2_max;
  97. struct pmbus_driver_info info;
  98. u32 features;
  99. };
  100. #define to_ltc2978_data(x) container_of(x, struct ltc2978_data, info)
  101. #define FEAT_CLEAR_PEAKS BIT(0)
  102. #define FEAT_NEEDS_POLLING BIT(1)
  103. #define has_clear_peaks(d) ((d)->features & FEAT_CLEAR_PEAKS)
  104. #define needs_polling(d) ((d)->features & FEAT_NEEDS_POLLING)
  105. static int ltc_wait_ready(struct i2c_client *client)
  106. {
  107. unsigned long timeout = jiffies + msecs_to_jiffies(LTC_POLL_TIMEOUT);
  108. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  109. struct ltc2978_data *data = to_ltc2978_data(info);
  110. int status;
  111. u8 mask;
  112. if (!needs_polling(data))
  113. return 0;
  114. /*
  115. * LTC3883 does not support LTC_NOT_PENDING, even though
  116. * the datasheet claims that it does.
  117. */
  118. mask = LTC_NOT_BUSY;
  119. if (data->id != ltc3883)
  120. mask |= LTC_NOT_PENDING;
  121. do {
  122. status = pmbus_read_byte_data(client, 0, LTC2978_MFR_COMMON);
  123. if (status == -EBADMSG || status == -ENXIO) {
  124. /* PEC error or NACK: chip may be busy, try again */
  125. usleep_range(50, 100);
  126. continue;
  127. }
  128. if (status < 0)
  129. return status;
  130. if ((status & mask) == mask)
  131. return 0;
  132. usleep_range(50, 100);
  133. } while (time_before(jiffies, timeout));
  134. return -ETIMEDOUT;
  135. }
  136. static int ltc_read_word_data(struct i2c_client *client, int page, int reg)
  137. {
  138. int ret;
  139. ret = ltc_wait_ready(client);
  140. if (ret < 0)
  141. return ret;
  142. return pmbus_read_word_data(client, page, reg);
  143. }
  144. static int ltc_read_byte_data(struct i2c_client *client, int page, int reg)
  145. {
  146. int ret;
  147. ret = ltc_wait_ready(client);
  148. if (ret < 0)
  149. return ret;
  150. return pmbus_read_byte_data(client, page, reg);
  151. }
  152. static int ltc_write_byte(struct i2c_client *client, int page, u8 byte)
  153. {
  154. int ret;
  155. ret = ltc_wait_ready(client);
  156. if (ret < 0)
  157. return ret;
  158. return pmbus_write_byte(client, page, byte);
  159. }
  160. static inline int lin11_to_val(int data)
  161. {
  162. s16 e = ((s16)data) >> 11;
  163. s32 m = (((s16)(data << 5)) >> 5);
  164. /*
  165. * mantissa is 10 bit + sign, exponent adds up to 15 bit.
  166. * Add 6 bit to exponent for maximum accuracy (10 + 15 + 6 = 31).
  167. */
  168. e += 6;
  169. return (e < 0 ? m >> -e : m << e);
  170. }
  171. static int ltc_get_max(struct ltc2978_data *data, struct i2c_client *client,
  172. int page, int reg, u16 *pmax)
  173. {
  174. int ret;
  175. ret = ltc_read_word_data(client, page, reg);
  176. if (ret >= 0) {
  177. if (lin11_to_val(ret) > lin11_to_val(*pmax))
  178. *pmax = ret;
  179. ret = *pmax;
  180. }
  181. return ret;
  182. }
  183. static int ltc_get_min(struct ltc2978_data *data, struct i2c_client *client,
  184. int page, int reg, u16 *pmin)
  185. {
  186. int ret;
  187. ret = ltc_read_word_data(client, page, reg);
  188. if (ret >= 0) {
  189. if (lin11_to_val(ret) < lin11_to_val(*pmin))
  190. *pmin = ret;
  191. ret = *pmin;
  192. }
  193. return ret;
  194. }
  195. static int ltc2978_read_word_data_common(struct i2c_client *client, int page,
  196. int reg)
  197. {
  198. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  199. struct ltc2978_data *data = to_ltc2978_data(info);
  200. int ret;
  201. switch (reg) {
  202. case PMBUS_VIRT_READ_VIN_MAX:
  203. ret = ltc_get_max(data, client, page, LTC2978_MFR_VIN_PEAK,
  204. &data->vin_max);
  205. break;
  206. case PMBUS_VIRT_READ_VOUT_MAX:
  207. ret = ltc_read_word_data(client, page, LTC2978_MFR_VOUT_PEAK);
  208. if (ret >= 0) {
  209. /*
  210. * VOUT is 16 bit unsigned with fixed exponent,
  211. * so we can compare it directly
  212. */
  213. if (ret > data->vout_max[page])
  214. data->vout_max[page] = ret;
  215. ret = data->vout_max[page];
  216. }
  217. break;
  218. case PMBUS_VIRT_READ_TEMP_MAX:
  219. ret = ltc_get_max(data, client, page,
  220. LTC2978_MFR_TEMPERATURE_PEAK,
  221. &data->temp_max[page]);
  222. break;
  223. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  224. case PMBUS_VIRT_RESET_VIN_HISTORY:
  225. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  226. ret = 0;
  227. break;
  228. default:
  229. ret = ltc_wait_ready(client);
  230. if (ret < 0)
  231. return ret;
  232. ret = -ENODATA;
  233. break;
  234. }
  235. return ret;
  236. }
  237. static int ltc2978_read_word_data(struct i2c_client *client, int page, int reg)
  238. {
  239. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  240. struct ltc2978_data *data = to_ltc2978_data(info);
  241. int ret;
  242. switch (reg) {
  243. case PMBUS_VIRT_READ_VIN_MIN:
  244. ret = ltc_get_min(data, client, page, LTC2978_MFR_VIN_MIN,
  245. &data->vin_min);
  246. break;
  247. case PMBUS_VIRT_READ_VOUT_MIN:
  248. ret = ltc_read_word_data(client, page, LTC2978_MFR_VOUT_MIN);
  249. if (ret >= 0) {
  250. /*
  251. * VOUT_MIN is known to not be supported on some lots
  252. * of LTC2978 revision 1, and will return the maximum
  253. * possible voltage if read. If VOUT_MAX is valid and
  254. * lower than the reading of VOUT_MIN, use it instead.
  255. */
  256. if (data->vout_max[page] && ret > data->vout_max[page])
  257. ret = data->vout_max[page];
  258. if (ret < data->vout_min[page])
  259. data->vout_min[page] = ret;
  260. ret = data->vout_min[page];
  261. }
  262. break;
  263. case PMBUS_VIRT_READ_TEMP_MIN:
  264. ret = ltc_get_min(data, client, page,
  265. LTC2978_MFR_TEMPERATURE_MIN,
  266. &data->temp_min[page]);
  267. break;
  268. case PMBUS_VIRT_READ_IOUT_MAX:
  269. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  270. case PMBUS_VIRT_READ_TEMP2_MAX:
  271. case PMBUS_VIRT_RESET_TEMP2_HISTORY:
  272. ret = -ENXIO;
  273. break;
  274. default:
  275. ret = ltc2978_read_word_data_common(client, page, reg);
  276. break;
  277. }
  278. return ret;
  279. }
  280. static int ltc2974_read_word_data(struct i2c_client *client, int page, int reg)
  281. {
  282. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  283. struct ltc2978_data *data = to_ltc2978_data(info);
  284. int ret;
  285. switch (reg) {
  286. case PMBUS_VIRT_READ_IOUT_MAX:
  287. ret = ltc_get_max(data, client, page, LTC2974_MFR_IOUT_PEAK,
  288. &data->iout_max[page]);
  289. break;
  290. case PMBUS_VIRT_READ_IOUT_MIN:
  291. ret = ltc_get_min(data, client, page, LTC2974_MFR_IOUT_MIN,
  292. &data->iout_min[page]);
  293. break;
  294. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  295. ret = 0;
  296. break;
  297. default:
  298. ret = ltc2978_read_word_data(client, page, reg);
  299. break;
  300. }
  301. return ret;
  302. }
  303. static int ltc2975_read_word_data(struct i2c_client *client, int page, int reg)
  304. {
  305. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  306. struct ltc2978_data *data = to_ltc2978_data(info);
  307. int ret;
  308. switch (reg) {
  309. case PMBUS_VIRT_READ_IIN_MAX:
  310. ret = ltc_get_max(data, client, page, LTC2975_MFR_IIN_PEAK,
  311. &data->iin_max);
  312. break;
  313. case PMBUS_VIRT_READ_IIN_MIN:
  314. ret = ltc_get_min(data, client, page, LTC2975_MFR_IIN_MIN,
  315. &data->iin_min);
  316. break;
  317. case PMBUS_VIRT_READ_PIN_MAX:
  318. ret = ltc_get_max(data, client, page, LTC2975_MFR_PIN_PEAK,
  319. &data->pin_max);
  320. break;
  321. case PMBUS_VIRT_READ_PIN_MIN:
  322. ret = ltc_get_min(data, client, page, LTC2975_MFR_PIN_MIN,
  323. &data->pin_min);
  324. break;
  325. case PMBUS_VIRT_RESET_IIN_HISTORY:
  326. case PMBUS_VIRT_RESET_PIN_HISTORY:
  327. ret = 0;
  328. break;
  329. default:
  330. ret = ltc2978_read_word_data(client, page, reg);
  331. break;
  332. }
  333. return ret;
  334. }
  335. static int ltc3880_read_word_data(struct i2c_client *client, int page, int reg)
  336. {
  337. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  338. struct ltc2978_data *data = to_ltc2978_data(info);
  339. int ret;
  340. switch (reg) {
  341. case PMBUS_VIRT_READ_IOUT_MAX:
  342. ret = ltc_get_max(data, client, page, LTC3880_MFR_IOUT_PEAK,
  343. &data->iout_max[page]);
  344. break;
  345. case PMBUS_VIRT_READ_TEMP2_MAX:
  346. ret = ltc_get_max(data, client, page,
  347. LTC3880_MFR_TEMPERATURE2_PEAK,
  348. &data->temp2_max);
  349. break;
  350. case PMBUS_VIRT_READ_VIN_MIN:
  351. case PMBUS_VIRT_READ_VOUT_MIN:
  352. case PMBUS_VIRT_READ_TEMP_MIN:
  353. ret = -ENXIO;
  354. break;
  355. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  356. case PMBUS_VIRT_RESET_TEMP2_HISTORY:
  357. ret = 0;
  358. break;
  359. default:
  360. ret = ltc2978_read_word_data_common(client, page, reg);
  361. break;
  362. }
  363. return ret;
  364. }
  365. static int ltc3883_read_word_data(struct i2c_client *client, int page, int reg)
  366. {
  367. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  368. struct ltc2978_data *data = to_ltc2978_data(info);
  369. int ret;
  370. switch (reg) {
  371. case PMBUS_VIRT_READ_IIN_MAX:
  372. ret = ltc_get_max(data, client, page, LTC3883_MFR_IIN_PEAK,
  373. &data->iin_max);
  374. break;
  375. case PMBUS_VIRT_RESET_IIN_HISTORY:
  376. ret = 0;
  377. break;
  378. default:
  379. ret = ltc3880_read_word_data(client, page, reg);
  380. break;
  381. }
  382. return ret;
  383. }
  384. static int ltc2978_clear_peaks(struct ltc2978_data *data,
  385. struct i2c_client *client, int page)
  386. {
  387. int ret;
  388. if (has_clear_peaks(data))
  389. ret = ltc_write_byte(client, 0, LTC3880_MFR_CLEAR_PEAKS);
  390. else
  391. ret = ltc_write_byte(client, page, PMBUS_CLEAR_FAULTS);
  392. return ret;
  393. }
  394. static int ltc2978_write_word_data(struct i2c_client *client, int page,
  395. int reg, u16 word)
  396. {
  397. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  398. struct ltc2978_data *data = to_ltc2978_data(info);
  399. int ret;
  400. switch (reg) {
  401. case PMBUS_VIRT_RESET_IIN_HISTORY:
  402. data->iin_max = 0x7c00;
  403. data->iin_min = 0x7bff;
  404. ret = ltc2978_clear_peaks(data, client, 0);
  405. break;
  406. case PMBUS_VIRT_RESET_PIN_HISTORY:
  407. data->pin_max = 0x7c00;
  408. data->pin_min = 0x7bff;
  409. ret = ltc2978_clear_peaks(data, client, 0);
  410. break;
  411. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  412. data->iout_max[page] = 0x7c00;
  413. data->iout_min[page] = 0xfbff;
  414. ret = ltc2978_clear_peaks(data, client, page);
  415. break;
  416. case PMBUS_VIRT_RESET_TEMP2_HISTORY:
  417. data->temp2_max = 0x7c00;
  418. ret = ltc2978_clear_peaks(data, client, page);
  419. break;
  420. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  421. data->vout_min[page] = 0xffff;
  422. data->vout_max[page] = 0;
  423. ret = ltc2978_clear_peaks(data, client, page);
  424. break;
  425. case PMBUS_VIRT_RESET_VIN_HISTORY:
  426. data->vin_min = 0x7bff;
  427. data->vin_max = 0x7c00;
  428. ret = ltc2978_clear_peaks(data, client, page);
  429. break;
  430. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  431. data->temp_min[page] = 0x7bff;
  432. data->temp_max[page] = 0x7c00;
  433. ret = ltc2978_clear_peaks(data, client, page);
  434. break;
  435. default:
  436. ret = ltc_wait_ready(client);
  437. if (ret < 0)
  438. return ret;
  439. ret = -ENODATA;
  440. break;
  441. }
  442. return ret;
  443. }
  444. static const struct i2c_device_id ltc2978_id[] = {
  445. {"ltc2974", ltc2974},
  446. {"ltc2975", ltc2975},
  447. {"ltc2977", ltc2977},
  448. {"ltc2978", ltc2978},
  449. {"ltc2980", ltc2980},
  450. {"ltc3880", ltc3880},
  451. {"ltc3882", ltc3882},
  452. {"ltc3883", ltc3883},
  453. {"ltc3886", ltc3886},
  454. {"ltc3887", ltc3887},
  455. {"ltm2987", ltm2987},
  456. {"ltm4675", ltm4675},
  457. {"ltm4676", ltm4676},
  458. {}
  459. };
  460. MODULE_DEVICE_TABLE(i2c, ltc2978_id);
  461. #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
  462. static const struct regulator_desc ltc2978_reg_desc[] = {
  463. PMBUS_REGULATOR("vout", 0),
  464. PMBUS_REGULATOR("vout", 1),
  465. PMBUS_REGULATOR("vout", 2),
  466. PMBUS_REGULATOR("vout", 3),
  467. PMBUS_REGULATOR("vout", 4),
  468. PMBUS_REGULATOR("vout", 5),
  469. PMBUS_REGULATOR("vout", 6),
  470. PMBUS_REGULATOR("vout", 7),
  471. };
  472. #endif /* CONFIG_SENSORS_LTC2978_REGULATOR */
  473. static int ltc2978_get_id(struct i2c_client *client)
  474. {
  475. int chip_id;
  476. chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
  477. if (chip_id < 0) {
  478. const struct i2c_device_id *id;
  479. u8 buf[I2C_SMBUS_BLOCK_MAX];
  480. int ret;
  481. if (!i2c_check_functionality(client->adapter,
  482. I2C_FUNC_SMBUS_READ_BLOCK_DATA))
  483. return -ENODEV;
  484. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
  485. if (ret < 0)
  486. return ret;
  487. if (ret < 3 || strncmp(buf, "LTC", 3))
  488. return -ENODEV;
  489. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
  490. if (ret < 0)
  491. return ret;
  492. for (id = &ltc2978_id[0]; strlen(id->name); id++) {
  493. if (!strncasecmp(id->name, buf, strlen(id->name)))
  494. return (int)id->driver_data;
  495. }
  496. return -ENODEV;
  497. }
  498. chip_id &= LTC2978_ID_MASK;
  499. if (chip_id == LTC2974_ID)
  500. return ltc2974;
  501. else if (chip_id == LTC2975_ID)
  502. return ltc2975;
  503. else if (chip_id == LTC2977_ID)
  504. return ltc2977;
  505. else if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2)
  506. return ltc2978;
  507. else if (chip_id == LTC2980_ID_A || chip_id == LTC2980_ID_B)
  508. return ltc2980;
  509. else if (chip_id == LTC3880_ID)
  510. return ltc3880;
  511. else if (chip_id == LTC3882_ID || chip_id == LTC3882_ID_D1)
  512. return ltc3882;
  513. else if (chip_id == LTC3883_ID)
  514. return ltc3883;
  515. else if (chip_id == LTC3886_ID)
  516. return ltc3886;
  517. else if (chip_id == LTC3887_ID)
  518. return ltc3887;
  519. else if (chip_id == LTM2987_ID_A || chip_id == LTM2987_ID_B)
  520. return ltm2987;
  521. else if (chip_id == LTM4675_ID)
  522. return ltm4675;
  523. else if (chip_id == LTM4676_ID_REV1 || chip_id == LTM4676_ID_REV2 ||
  524. chip_id == LTM4676A_ID)
  525. return ltm4676;
  526. dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
  527. return -ENODEV;
  528. }
  529. static int ltc2978_probe(struct i2c_client *client,
  530. const struct i2c_device_id *id)
  531. {
  532. int i, chip_id;
  533. struct ltc2978_data *data;
  534. struct pmbus_driver_info *info;
  535. if (!i2c_check_functionality(client->adapter,
  536. I2C_FUNC_SMBUS_READ_WORD_DATA))
  537. return -ENODEV;
  538. data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
  539. GFP_KERNEL);
  540. if (!data)
  541. return -ENOMEM;
  542. chip_id = ltc2978_get_id(client);
  543. if (chip_id < 0)
  544. return chip_id;
  545. data->id = chip_id;
  546. if (data->id != id->driver_data)
  547. dev_warn(&client->dev,
  548. "Device mismatch: Configured %s, detected %s\n",
  549. id->name,
  550. ltc2978_id[data->id].name);
  551. info = &data->info;
  552. info->write_word_data = ltc2978_write_word_data;
  553. info->write_byte = ltc_write_byte;
  554. info->read_word_data = ltc_read_word_data;
  555. info->read_byte_data = ltc_read_byte_data;
  556. data->vin_min = 0x7bff;
  557. data->vin_max = 0x7c00;
  558. for (i = 0; i < ARRAY_SIZE(data->vout_min); i++)
  559. data->vout_min[i] = 0xffff;
  560. for (i = 0; i < ARRAY_SIZE(data->iout_min); i++)
  561. data->iout_min[i] = 0xfbff;
  562. for (i = 0; i < ARRAY_SIZE(data->iout_max); i++)
  563. data->iout_max[i] = 0x7c00;
  564. for (i = 0; i < ARRAY_SIZE(data->temp_min); i++)
  565. data->temp_min[i] = 0x7bff;
  566. for (i = 0; i < ARRAY_SIZE(data->temp_max); i++)
  567. data->temp_max[i] = 0x7c00;
  568. data->temp2_max = 0x7c00;
  569. switch (data->id) {
  570. case ltc2974:
  571. info->read_word_data = ltc2974_read_word_data;
  572. info->pages = LTC2974_NUM_PAGES;
  573. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  574. | PMBUS_HAVE_TEMP2;
  575. for (i = 0; i < info->pages; i++) {
  576. info->func[i] |= PMBUS_HAVE_VOUT
  577. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
  578. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
  579. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
  580. }
  581. break;
  582. case ltc2975:
  583. info->read_word_data = ltc2975_read_word_data;
  584. info->pages = LTC2974_NUM_PAGES;
  585. info->func[0] = PMBUS_HAVE_IIN | PMBUS_HAVE_PIN
  586. | PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  587. | PMBUS_HAVE_TEMP2;
  588. for (i = 0; i < info->pages; i++) {
  589. info->func[i] |= PMBUS_HAVE_VOUT
  590. | PMBUS_HAVE_STATUS_VOUT | PMBUS_HAVE_POUT
  591. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP
  592. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT;
  593. }
  594. break;
  595. case ltc2977:
  596. case ltc2978:
  597. case ltc2980:
  598. case ltm2987:
  599. info->read_word_data = ltc2978_read_word_data;
  600. info->pages = LTC2978_NUM_PAGES;
  601. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT
  602. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  603. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  604. for (i = 1; i < LTC2978_NUM_PAGES; i++) {
  605. info->func[i] = PMBUS_HAVE_VOUT
  606. | PMBUS_HAVE_STATUS_VOUT;
  607. }
  608. break;
  609. case ltc3880:
  610. case ltc3887:
  611. case ltm4675:
  612. case ltm4676:
  613. data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
  614. info->read_word_data = ltc3880_read_word_data;
  615. info->pages = LTC3880_NUM_PAGES;
  616. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
  617. | PMBUS_HAVE_STATUS_INPUT
  618. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  619. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  620. | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
  621. | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  622. info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  623. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  624. | PMBUS_HAVE_POUT
  625. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  626. break;
  627. case ltc3882:
  628. data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
  629. info->read_word_data = ltc3880_read_word_data;
  630. info->pages = LTC3880_NUM_PAGES;
  631. info->func[0] = PMBUS_HAVE_VIN
  632. | PMBUS_HAVE_STATUS_INPUT
  633. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  634. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  635. | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
  636. | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  637. info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  638. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  639. | PMBUS_HAVE_POUT
  640. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  641. break;
  642. case ltc3883:
  643. data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
  644. info->read_word_data = ltc3883_read_word_data;
  645. info->pages = LTC3883_NUM_PAGES;
  646. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
  647. | PMBUS_HAVE_STATUS_INPUT
  648. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  649. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  650. | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
  651. | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  652. break;
  653. case ltc3886:
  654. data->features |= FEAT_CLEAR_PEAKS | FEAT_NEEDS_POLLING;
  655. info->read_word_data = ltc3883_read_word_data;
  656. info->pages = LTC3880_NUM_PAGES;
  657. info->func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN
  658. | PMBUS_HAVE_STATUS_INPUT
  659. | PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  660. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  661. | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT | PMBUS_HAVE_TEMP
  662. | PMBUS_HAVE_TEMP2 | PMBUS_HAVE_STATUS_TEMP;
  663. info->func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT
  664. | PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT
  665. | PMBUS_HAVE_POUT
  666. | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  667. break;
  668. default:
  669. return -ENODEV;
  670. }
  671. #if IS_ENABLED(CONFIG_SENSORS_LTC2978_REGULATOR)
  672. info->num_regulators = info->pages;
  673. info->reg_desc = ltc2978_reg_desc;
  674. if (info->num_regulators > ARRAY_SIZE(ltc2978_reg_desc)) {
  675. dev_err(&client->dev, "num_regulators too large!");
  676. info->num_regulators = ARRAY_SIZE(ltc2978_reg_desc);
  677. }
  678. #endif
  679. return pmbus_do_probe(client, id, info);
  680. }
  681. #ifdef CONFIG_OF
  682. static const struct of_device_id ltc2978_of_match[] = {
  683. { .compatible = "lltc,ltc2974" },
  684. { .compatible = "lltc,ltc2975" },
  685. { .compatible = "lltc,ltc2977" },
  686. { .compatible = "lltc,ltc2978" },
  687. { .compatible = "lltc,ltc2980" },
  688. { .compatible = "lltc,ltc3880" },
  689. { .compatible = "lltc,ltc3882" },
  690. { .compatible = "lltc,ltc3883" },
  691. { .compatible = "lltc,ltc3886" },
  692. { .compatible = "lltc,ltc3887" },
  693. { .compatible = "lltc,ltm2987" },
  694. { .compatible = "lltc,ltm4675" },
  695. { .compatible = "lltc,ltm4676" },
  696. { }
  697. };
  698. MODULE_DEVICE_TABLE(of, ltc2978_of_match);
  699. #endif
  700. static struct i2c_driver ltc2978_driver = {
  701. .driver = {
  702. .name = "ltc2978",
  703. .of_match_table = of_match_ptr(ltc2978_of_match),
  704. },
  705. .probe = ltc2978_probe,
  706. .remove = pmbus_do_remove,
  707. .id_table = ltc2978_id,
  708. };
  709. module_i2c_driver(ltc2978_driver);
  710. MODULE_AUTHOR("Guenter Roeck");
  711. MODULE_DESCRIPTION("PMBus driver for LTC2978 and comppatible chips");
  712. MODULE_LICENSE("GPL");