adm1275.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hardware monitoring driver for Analog Devices ADM1275 Hot-Swap Controller
  4. * and Digital Power Monitor
  5. *
  6. * Copyright (c) 2011 Ericsson AB.
  7. * Copyright (c) 2018 Guenter Roeck
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/i2c.h>
  15. #include <linux/bitops.h>
  16. #include <linux/bitfield.h>
  17. #include <linux/log2.h>
  18. #include "pmbus.h"
  19. enum chips { adm1075, adm1272, adm1275, adm1276, adm1278, adm1293, adm1294 };
  20. #define ADM1275_MFR_STATUS_IOUT_WARN2 BIT(0)
  21. #define ADM1293_MFR_STATUS_VAUX_UV_WARN BIT(5)
  22. #define ADM1293_MFR_STATUS_VAUX_OV_WARN BIT(6)
  23. #define ADM1275_PEAK_IOUT 0xd0
  24. #define ADM1275_PEAK_VIN 0xd1
  25. #define ADM1275_PEAK_VOUT 0xd2
  26. #define ADM1275_PMON_CONFIG 0xd4
  27. #define ADM1275_VIN_VOUT_SELECT BIT(6)
  28. #define ADM1275_VRANGE BIT(5)
  29. #define ADM1075_IRANGE_50 BIT(4)
  30. #define ADM1075_IRANGE_25 BIT(3)
  31. #define ADM1075_IRANGE_MASK (BIT(3) | BIT(4))
  32. #define ADM1272_IRANGE BIT(0)
  33. #define ADM1278_TSFILT BIT(15)
  34. #define ADM1278_TEMP1_EN BIT(3)
  35. #define ADM1278_VIN_EN BIT(2)
  36. #define ADM1278_VOUT_EN BIT(1)
  37. #define ADM1278_PMON_DEFCONFIG (ADM1278_VOUT_EN | ADM1278_TEMP1_EN | ADM1278_TSFILT)
  38. #define ADM1293_IRANGE_25 0
  39. #define ADM1293_IRANGE_50 BIT(6)
  40. #define ADM1293_IRANGE_100 BIT(7)
  41. #define ADM1293_IRANGE_200 (BIT(6) | BIT(7))
  42. #define ADM1293_IRANGE_MASK (BIT(6) | BIT(7))
  43. #define ADM1293_VIN_SEL_012 BIT(2)
  44. #define ADM1293_VIN_SEL_074 BIT(3)
  45. #define ADM1293_VIN_SEL_210 (BIT(2) | BIT(3))
  46. #define ADM1293_VIN_SEL_MASK (BIT(2) | BIT(3))
  47. #define ADM1293_VAUX_EN BIT(1)
  48. #define ADM1278_PEAK_TEMP 0xd7
  49. #define ADM1275_IOUT_WARN2_LIMIT 0xd7
  50. #define ADM1275_DEVICE_CONFIG 0xd8
  51. #define ADM1275_IOUT_WARN2_SELECT BIT(4)
  52. #define ADM1276_PEAK_PIN 0xda
  53. #define ADM1075_READ_VAUX 0xdd
  54. #define ADM1075_VAUX_OV_WARN_LIMIT 0xde
  55. #define ADM1075_VAUX_UV_WARN_LIMIT 0xdf
  56. #define ADM1293_IOUT_MIN 0xe3
  57. #define ADM1293_PIN_MIN 0xe4
  58. #define ADM1075_VAUX_STATUS 0xf6
  59. #define ADM1075_VAUX_OV_WARN BIT(7)
  60. #define ADM1075_VAUX_UV_WARN BIT(6)
  61. #define ADM1275_VI_AVG_SHIFT 0
  62. #define ADM1275_VI_AVG_MASK GENMASK(ADM1275_VI_AVG_SHIFT + 2, \
  63. ADM1275_VI_AVG_SHIFT)
  64. #define ADM1275_SAMPLES_AVG_MAX 128
  65. #define ADM1278_PWR_AVG_SHIFT 11
  66. #define ADM1278_PWR_AVG_MASK GENMASK(ADM1278_PWR_AVG_SHIFT + 2, \
  67. ADM1278_PWR_AVG_SHIFT)
  68. #define ADM1278_VI_AVG_SHIFT 8
  69. #define ADM1278_VI_AVG_MASK GENMASK(ADM1278_VI_AVG_SHIFT + 2, \
  70. ADM1278_VI_AVG_SHIFT)
  71. struct adm1275_data {
  72. int id;
  73. bool have_oc_fault;
  74. bool have_uc_fault;
  75. bool have_vout;
  76. bool have_vaux_status;
  77. bool have_mfr_vaux_status;
  78. bool have_iout_min;
  79. bool have_pin_min;
  80. bool have_pin_max;
  81. bool have_temp_max;
  82. bool have_power_sampling;
  83. struct pmbus_driver_info info;
  84. };
  85. #define to_adm1275_data(x) container_of(x, struct adm1275_data, info)
  86. struct coefficients {
  87. s16 m;
  88. s16 b;
  89. s16 R;
  90. };
  91. static const struct coefficients adm1075_coefficients[] = {
  92. [0] = { 27169, 0, -1 }, /* voltage */
  93. [1] = { 806, 20475, -1 }, /* current, irange25 */
  94. [2] = { 404, 20475, -1 }, /* current, irange50 */
  95. [3] = { 8549, 0, -1 }, /* power, irange25 */
  96. [4] = { 4279, 0, -1 }, /* power, irange50 */
  97. };
  98. static const struct coefficients adm1272_coefficients[] = {
  99. [0] = { 6770, 0, -2 }, /* voltage, vrange 60V */
  100. [1] = { 4062, 0, -2 }, /* voltage, vrange 100V */
  101. [2] = { 1326, 20480, -1 }, /* current, vsense range 15mV */
  102. [3] = { 663, 20480, -1 }, /* current, vsense range 30mV */
  103. [4] = { 3512, 0, -2 }, /* power, vrange 60V, irange 15mV */
  104. [5] = { 21071, 0, -3 }, /* power, vrange 100V, irange 15mV */
  105. [6] = { 17561, 0, -3 }, /* power, vrange 60V, irange 30mV */
  106. [7] = { 10535, 0, -3 }, /* power, vrange 100V, irange 30mV */
  107. [8] = { 42, 31871, -1 }, /* temperature */
  108. };
  109. static const struct coefficients adm1275_coefficients[] = {
  110. [0] = { 19199, 0, -2 }, /* voltage, vrange set */
  111. [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
  112. [2] = { 807, 20475, -1 }, /* current */
  113. };
  114. static const struct coefficients adm1276_coefficients[] = {
  115. [0] = { 19199, 0, -2 }, /* voltage, vrange set */
  116. [1] = { 6720, 0, -1 }, /* voltage, vrange not set */
  117. [2] = { 807, 20475, -1 }, /* current */
  118. [3] = { 6043, 0, -2 }, /* power, vrange set */
  119. [4] = { 2115, 0, -1 }, /* power, vrange not set */
  120. };
  121. static const struct coefficients adm1278_coefficients[] = {
  122. [0] = { 19599, 0, -2 }, /* voltage */
  123. [1] = { 800, 20475, -1 }, /* current */
  124. [2] = { 6123, 0, -2 }, /* power */
  125. [3] = { 42, 31880, -1 }, /* temperature */
  126. };
  127. static const struct coefficients adm1293_coefficients[] = {
  128. [0] = { 3333, -1, 0 }, /* voltage, vrange 1.2V */
  129. [1] = { 5552, -5, -1 }, /* voltage, vrange 7.4V */
  130. [2] = { 19604, -50, -2 }, /* voltage, vrange 21V */
  131. [3] = { 8000, -100, -2 }, /* current, irange25 */
  132. [4] = { 4000, -100, -2 }, /* current, irange50 */
  133. [5] = { 20000, -1000, -3 }, /* current, irange100 */
  134. [6] = { 10000, -1000, -3 }, /* current, irange200 */
  135. [7] = { 10417, 0, -1 }, /* power, 1.2V, irange25 */
  136. [8] = { 5208, 0, -1 }, /* power, 1.2V, irange50 */
  137. [9] = { 26042, 0, -2 }, /* power, 1.2V, irange100 */
  138. [10] = { 13021, 0, -2 }, /* power, 1.2V, irange200 */
  139. [11] = { 17351, 0, -2 }, /* power, 7.4V, irange25 */
  140. [12] = { 8676, 0, -2 }, /* power, 7.4V, irange50 */
  141. [13] = { 4338, 0, -2 }, /* power, 7.4V, irange100 */
  142. [14] = { 21689, 0, -3 }, /* power, 7.4V, irange200 */
  143. [15] = { 6126, 0, -2 }, /* power, 21V, irange25 */
  144. [16] = { 30631, 0, -3 }, /* power, 21V, irange50 */
  145. [17] = { 15316, 0, -3 }, /* power, 21V, irange100 */
  146. [18] = { 7658, 0, -3 }, /* power, 21V, irange200 */
  147. };
  148. static int adm1275_read_pmon_config(const struct adm1275_data *data,
  149. struct i2c_client *client, bool is_power)
  150. {
  151. int shift, ret;
  152. u16 mask;
  153. /*
  154. * The PMON configuration register is a 16-bit register only on chips
  155. * supporting power average sampling. On other chips it is an 8-bit
  156. * register.
  157. */
  158. if (data->have_power_sampling) {
  159. ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
  160. mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
  161. shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
  162. } else {
  163. ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
  164. mask = ADM1275_VI_AVG_MASK;
  165. shift = ADM1275_VI_AVG_SHIFT;
  166. }
  167. if (ret < 0)
  168. return ret;
  169. return (ret & mask) >> shift;
  170. }
  171. static int adm1275_write_pmon_config(const struct adm1275_data *data,
  172. struct i2c_client *client,
  173. bool is_power, u16 word)
  174. {
  175. int shift, ret;
  176. u16 mask;
  177. if (data->have_power_sampling) {
  178. ret = i2c_smbus_read_word_data(client, ADM1275_PMON_CONFIG);
  179. mask = is_power ? ADM1278_PWR_AVG_MASK : ADM1278_VI_AVG_MASK;
  180. shift = is_power ? ADM1278_PWR_AVG_SHIFT : ADM1278_VI_AVG_SHIFT;
  181. } else {
  182. ret = i2c_smbus_read_byte_data(client, ADM1275_PMON_CONFIG);
  183. mask = ADM1275_VI_AVG_MASK;
  184. shift = ADM1275_VI_AVG_SHIFT;
  185. }
  186. if (ret < 0)
  187. return ret;
  188. word = (ret & ~mask) | ((word << shift) & mask);
  189. if (data->have_power_sampling)
  190. ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG,
  191. word);
  192. else
  193. ret = i2c_smbus_write_byte_data(client, ADM1275_PMON_CONFIG,
  194. word);
  195. return ret;
  196. }
  197. static int adm1275_read_word_data(struct i2c_client *client, int page,
  198. int phase, int reg)
  199. {
  200. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  201. const struct adm1275_data *data = to_adm1275_data(info);
  202. int ret = 0;
  203. if (page > 0)
  204. return -ENXIO;
  205. switch (reg) {
  206. case PMBUS_IOUT_UC_FAULT_LIMIT:
  207. if (!data->have_uc_fault)
  208. return -ENXIO;
  209. ret = pmbus_read_word_data(client, 0, 0xff,
  210. ADM1275_IOUT_WARN2_LIMIT);
  211. break;
  212. case PMBUS_IOUT_OC_FAULT_LIMIT:
  213. if (!data->have_oc_fault)
  214. return -ENXIO;
  215. ret = pmbus_read_word_data(client, 0, 0xff,
  216. ADM1275_IOUT_WARN2_LIMIT);
  217. break;
  218. case PMBUS_VOUT_OV_WARN_LIMIT:
  219. if (data->have_vout)
  220. return -ENODATA;
  221. ret = pmbus_read_word_data(client, 0, 0xff,
  222. ADM1075_VAUX_OV_WARN_LIMIT);
  223. break;
  224. case PMBUS_VOUT_UV_WARN_LIMIT:
  225. if (data->have_vout)
  226. return -ENODATA;
  227. ret = pmbus_read_word_data(client, 0, 0xff,
  228. ADM1075_VAUX_UV_WARN_LIMIT);
  229. break;
  230. case PMBUS_READ_VOUT:
  231. if (data->have_vout)
  232. return -ENODATA;
  233. ret = pmbus_read_word_data(client, 0, 0xff,
  234. ADM1075_READ_VAUX);
  235. break;
  236. case PMBUS_VIRT_READ_IOUT_MIN:
  237. if (!data->have_iout_min)
  238. return -ENXIO;
  239. ret = pmbus_read_word_data(client, 0, 0xff,
  240. ADM1293_IOUT_MIN);
  241. break;
  242. case PMBUS_VIRT_READ_IOUT_MAX:
  243. ret = pmbus_read_word_data(client, 0, 0xff,
  244. ADM1275_PEAK_IOUT);
  245. break;
  246. case PMBUS_VIRT_READ_VOUT_MAX:
  247. ret = pmbus_read_word_data(client, 0, 0xff,
  248. ADM1275_PEAK_VOUT);
  249. break;
  250. case PMBUS_VIRT_READ_VIN_MAX:
  251. ret = pmbus_read_word_data(client, 0, 0xff,
  252. ADM1275_PEAK_VIN);
  253. break;
  254. case PMBUS_VIRT_READ_PIN_MIN:
  255. if (!data->have_pin_min)
  256. return -ENXIO;
  257. ret = pmbus_read_word_data(client, 0, 0xff,
  258. ADM1293_PIN_MIN);
  259. break;
  260. case PMBUS_VIRT_READ_PIN_MAX:
  261. if (!data->have_pin_max)
  262. return -ENXIO;
  263. ret = pmbus_read_word_data(client, 0, 0xff,
  264. ADM1276_PEAK_PIN);
  265. break;
  266. case PMBUS_VIRT_READ_TEMP_MAX:
  267. if (!data->have_temp_max)
  268. return -ENXIO;
  269. ret = pmbus_read_word_data(client, 0, 0xff,
  270. ADM1278_PEAK_TEMP);
  271. break;
  272. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  273. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  274. case PMBUS_VIRT_RESET_VIN_HISTORY:
  275. break;
  276. case PMBUS_VIRT_RESET_PIN_HISTORY:
  277. if (!data->have_pin_max)
  278. return -ENXIO;
  279. break;
  280. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  281. if (!data->have_temp_max)
  282. return -ENXIO;
  283. break;
  284. case PMBUS_VIRT_POWER_SAMPLES:
  285. if (!data->have_power_sampling)
  286. return -ENXIO;
  287. ret = adm1275_read_pmon_config(data, client, true);
  288. if (ret < 0)
  289. break;
  290. ret = BIT(ret);
  291. break;
  292. case PMBUS_VIRT_IN_SAMPLES:
  293. case PMBUS_VIRT_CURR_SAMPLES:
  294. ret = adm1275_read_pmon_config(data, client, false);
  295. if (ret < 0)
  296. break;
  297. ret = BIT(ret);
  298. break;
  299. default:
  300. ret = -ENODATA;
  301. break;
  302. }
  303. return ret;
  304. }
  305. static int adm1275_write_word_data(struct i2c_client *client, int page, int reg,
  306. u16 word)
  307. {
  308. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  309. const struct adm1275_data *data = to_adm1275_data(info);
  310. int ret;
  311. if (page > 0)
  312. return -ENXIO;
  313. switch (reg) {
  314. case PMBUS_IOUT_UC_FAULT_LIMIT:
  315. case PMBUS_IOUT_OC_FAULT_LIMIT:
  316. ret = pmbus_write_word_data(client, 0, ADM1275_IOUT_WARN2_LIMIT,
  317. word);
  318. break;
  319. case PMBUS_VIRT_RESET_IOUT_HISTORY:
  320. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_IOUT, 0);
  321. if (!ret && data->have_iout_min)
  322. ret = pmbus_write_word_data(client, 0,
  323. ADM1293_IOUT_MIN, 0);
  324. break;
  325. case PMBUS_VIRT_RESET_VOUT_HISTORY:
  326. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VOUT, 0);
  327. break;
  328. case PMBUS_VIRT_RESET_VIN_HISTORY:
  329. ret = pmbus_write_word_data(client, 0, ADM1275_PEAK_VIN, 0);
  330. break;
  331. case PMBUS_VIRT_RESET_PIN_HISTORY:
  332. ret = pmbus_write_word_data(client, 0, ADM1276_PEAK_PIN, 0);
  333. if (!ret && data->have_pin_min)
  334. ret = pmbus_write_word_data(client, 0,
  335. ADM1293_PIN_MIN, 0);
  336. break;
  337. case PMBUS_VIRT_RESET_TEMP_HISTORY:
  338. ret = pmbus_write_word_data(client, 0, ADM1278_PEAK_TEMP, 0);
  339. break;
  340. case PMBUS_VIRT_POWER_SAMPLES:
  341. if (!data->have_power_sampling)
  342. return -ENXIO;
  343. word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
  344. ret = adm1275_write_pmon_config(data, client, true,
  345. ilog2(word));
  346. break;
  347. case PMBUS_VIRT_IN_SAMPLES:
  348. case PMBUS_VIRT_CURR_SAMPLES:
  349. word = clamp_val(word, 1, ADM1275_SAMPLES_AVG_MAX);
  350. ret = adm1275_write_pmon_config(data, client, false,
  351. ilog2(word));
  352. break;
  353. default:
  354. ret = -ENODATA;
  355. break;
  356. }
  357. return ret;
  358. }
  359. static int adm1275_read_byte_data(struct i2c_client *client, int page, int reg)
  360. {
  361. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  362. const struct adm1275_data *data = to_adm1275_data(info);
  363. int mfr_status, ret;
  364. if (page > 0)
  365. return -ENXIO;
  366. switch (reg) {
  367. case PMBUS_STATUS_IOUT:
  368. ret = pmbus_read_byte_data(client, page, PMBUS_STATUS_IOUT);
  369. if (ret < 0)
  370. break;
  371. if (!data->have_oc_fault && !data->have_uc_fault)
  372. break;
  373. mfr_status = pmbus_read_byte_data(client, page,
  374. PMBUS_STATUS_MFR_SPECIFIC);
  375. if (mfr_status < 0)
  376. return mfr_status;
  377. if (mfr_status & ADM1275_MFR_STATUS_IOUT_WARN2) {
  378. ret |= data->have_oc_fault ?
  379. PB_IOUT_OC_FAULT : PB_IOUT_UC_FAULT;
  380. }
  381. break;
  382. case PMBUS_STATUS_VOUT:
  383. if (data->have_vout)
  384. return -ENODATA;
  385. ret = 0;
  386. if (data->have_vaux_status) {
  387. mfr_status = pmbus_read_byte_data(client, 0,
  388. ADM1075_VAUX_STATUS);
  389. if (mfr_status < 0)
  390. return mfr_status;
  391. if (mfr_status & ADM1075_VAUX_OV_WARN)
  392. ret |= PB_VOLTAGE_OV_WARNING;
  393. if (mfr_status & ADM1075_VAUX_UV_WARN)
  394. ret |= PB_VOLTAGE_UV_WARNING;
  395. } else if (data->have_mfr_vaux_status) {
  396. mfr_status = pmbus_read_byte_data(client, page,
  397. PMBUS_STATUS_MFR_SPECIFIC);
  398. if (mfr_status < 0)
  399. return mfr_status;
  400. if (mfr_status & ADM1293_MFR_STATUS_VAUX_OV_WARN)
  401. ret |= PB_VOLTAGE_OV_WARNING;
  402. if (mfr_status & ADM1293_MFR_STATUS_VAUX_UV_WARN)
  403. ret |= PB_VOLTAGE_UV_WARNING;
  404. }
  405. break;
  406. default:
  407. ret = -ENODATA;
  408. break;
  409. }
  410. return ret;
  411. }
  412. static const struct i2c_device_id adm1275_id[] = {
  413. { "adm1075", adm1075 },
  414. { "adm1272", adm1272 },
  415. { "adm1275", adm1275 },
  416. { "adm1276", adm1276 },
  417. { "adm1278", adm1278 },
  418. { "adm1293", adm1293 },
  419. { "adm1294", adm1294 },
  420. { }
  421. };
  422. MODULE_DEVICE_TABLE(i2c, adm1275_id);
  423. /* Enable VOUT & TEMP1 if not enabled (disabled by default) */
  424. static int adm1275_enable_vout_temp(struct i2c_client *client, int config)
  425. {
  426. int ret;
  427. if ((config & ADM1278_PMON_DEFCONFIG) != ADM1278_PMON_DEFCONFIG) {
  428. config |= ADM1278_PMON_DEFCONFIG;
  429. ret = i2c_smbus_write_word_data(client, ADM1275_PMON_CONFIG, config);
  430. if (ret < 0) {
  431. dev_err(&client->dev, "Failed to enable VOUT/TEMP1 monitoring\n");
  432. return ret;
  433. }
  434. }
  435. return 0;
  436. }
  437. static int adm1275_probe(struct i2c_client *client)
  438. {
  439. s32 (*config_read_fn)(const struct i2c_client *client, u8 reg);
  440. u8 block_buffer[I2C_SMBUS_BLOCK_MAX + 1];
  441. int config, device_config;
  442. int ret;
  443. struct pmbus_driver_info *info;
  444. struct adm1275_data *data;
  445. const struct i2c_device_id *mid;
  446. const struct coefficients *coefficients;
  447. int vindex = -1, voindex = -1, cindex = -1, pindex = -1;
  448. int tindex = -1;
  449. u32 shunt;
  450. u32 avg;
  451. if (!i2c_check_functionality(client->adapter,
  452. I2C_FUNC_SMBUS_READ_BYTE_DATA
  453. | I2C_FUNC_SMBUS_BLOCK_DATA))
  454. return -ENODEV;
  455. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, block_buffer);
  456. if (ret < 0) {
  457. dev_err(&client->dev, "Failed to read Manufacturer ID\n");
  458. return ret;
  459. }
  460. if (ret != 3 || strncmp(block_buffer, "ADI", 3)) {
  461. dev_err(&client->dev, "Unsupported Manufacturer ID\n");
  462. return -ENODEV;
  463. }
  464. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, block_buffer);
  465. if (ret < 0) {
  466. dev_err(&client->dev, "Failed to read Manufacturer Model\n");
  467. return ret;
  468. }
  469. for (mid = adm1275_id; mid->name[0]; mid++) {
  470. if (!strncasecmp(mid->name, block_buffer, strlen(mid->name)))
  471. break;
  472. }
  473. if (!mid->name[0]) {
  474. dev_err(&client->dev, "Unsupported device\n");
  475. return -ENODEV;
  476. }
  477. if (strcmp(client->name, mid->name) != 0)
  478. dev_notice(&client->dev,
  479. "Device mismatch: Configured %s, detected %s\n",
  480. client->name, mid->name);
  481. if (mid->driver_data == adm1272 || mid->driver_data == adm1278 ||
  482. mid->driver_data == adm1293 || mid->driver_data == adm1294)
  483. config_read_fn = i2c_smbus_read_word_data;
  484. else
  485. config_read_fn = i2c_smbus_read_byte_data;
  486. config = config_read_fn(client, ADM1275_PMON_CONFIG);
  487. if (config < 0)
  488. return config;
  489. device_config = config_read_fn(client, ADM1275_DEVICE_CONFIG);
  490. if (device_config < 0)
  491. return device_config;
  492. data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
  493. GFP_KERNEL);
  494. if (!data)
  495. return -ENOMEM;
  496. if (of_property_read_u32(client->dev.of_node,
  497. "shunt-resistor-micro-ohms", &shunt))
  498. shunt = 1000; /* 1 mOhm if not set via DT */
  499. if (shunt == 0)
  500. return -EINVAL;
  501. data->id = mid->driver_data;
  502. info = &data->info;
  503. info->pages = 1;
  504. info->format[PSC_VOLTAGE_IN] = direct;
  505. info->format[PSC_VOLTAGE_OUT] = direct;
  506. info->format[PSC_CURRENT_OUT] = direct;
  507. info->format[PSC_POWER] = direct;
  508. info->format[PSC_TEMPERATURE] = direct;
  509. info->func[0] = PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  510. PMBUS_HAVE_SAMPLES;
  511. info->read_word_data = adm1275_read_word_data;
  512. info->read_byte_data = adm1275_read_byte_data;
  513. info->write_word_data = adm1275_write_word_data;
  514. switch (data->id) {
  515. case adm1075:
  516. if (device_config & ADM1275_IOUT_WARN2_SELECT)
  517. data->have_oc_fault = true;
  518. else
  519. data->have_uc_fault = true;
  520. data->have_pin_max = true;
  521. data->have_vaux_status = true;
  522. coefficients = adm1075_coefficients;
  523. vindex = 0;
  524. switch (config & ADM1075_IRANGE_MASK) {
  525. case ADM1075_IRANGE_25:
  526. cindex = 1;
  527. pindex = 3;
  528. break;
  529. case ADM1075_IRANGE_50:
  530. cindex = 2;
  531. pindex = 4;
  532. break;
  533. default:
  534. dev_err(&client->dev, "Invalid input current range");
  535. break;
  536. }
  537. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
  538. | PMBUS_HAVE_STATUS_INPUT;
  539. if (config & ADM1275_VIN_VOUT_SELECT)
  540. info->func[0] |=
  541. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  542. break;
  543. case adm1272:
  544. data->have_vout = true;
  545. data->have_pin_max = true;
  546. data->have_temp_max = true;
  547. data->have_power_sampling = true;
  548. coefficients = adm1272_coefficients;
  549. vindex = (config & ADM1275_VRANGE) ? 1 : 0;
  550. cindex = (config & ADM1272_IRANGE) ? 3 : 2;
  551. /* pindex depends on the combination of the above */
  552. switch (config & (ADM1275_VRANGE | ADM1272_IRANGE)) {
  553. case 0:
  554. default:
  555. pindex = 4;
  556. break;
  557. case ADM1275_VRANGE:
  558. pindex = 5;
  559. break;
  560. case ADM1272_IRANGE:
  561. pindex = 6;
  562. break;
  563. case ADM1275_VRANGE | ADM1272_IRANGE:
  564. pindex = 7;
  565. break;
  566. }
  567. tindex = 8;
  568. info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
  569. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  570. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  571. ret = adm1275_enable_vout_temp(client, config);
  572. if (ret)
  573. return ret;
  574. if (config & ADM1278_VIN_EN)
  575. info->func[0] |= PMBUS_HAVE_VIN;
  576. break;
  577. case adm1275:
  578. if (device_config & ADM1275_IOUT_WARN2_SELECT)
  579. data->have_oc_fault = true;
  580. else
  581. data->have_uc_fault = true;
  582. data->have_vout = true;
  583. coefficients = adm1275_coefficients;
  584. vindex = (config & ADM1275_VRANGE) ? 0 : 1;
  585. cindex = 2;
  586. if (config & ADM1275_VIN_VOUT_SELECT)
  587. info->func[0] |=
  588. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  589. else
  590. info->func[0] |=
  591. PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
  592. break;
  593. case adm1276:
  594. if (device_config & ADM1275_IOUT_WARN2_SELECT)
  595. data->have_oc_fault = true;
  596. else
  597. data->have_uc_fault = true;
  598. data->have_vout = true;
  599. data->have_pin_max = true;
  600. coefficients = adm1276_coefficients;
  601. vindex = (config & ADM1275_VRANGE) ? 0 : 1;
  602. cindex = 2;
  603. pindex = (config & ADM1275_VRANGE) ? 3 : 4;
  604. info->func[0] |= PMBUS_HAVE_VIN | PMBUS_HAVE_PIN
  605. | PMBUS_HAVE_STATUS_INPUT;
  606. if (config & ADM1275_VIN_VOUT_SELECT)
  607. info->func[0] |=
  608. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  609. break;
  610. case adm1278:
  611. data->have_vout = true;
  612. data->have_pin_max = true;
  613. data->have_temp_max = true;
  614. data->have_power_sampling = true;
  615. coefficients = adm1278_coefficients;
  616. vindex = 0;
  617. cindex = 1;
  618. pindex = 2;
  619. tindex = 3;
  620. info->func[0] |= PMBUS_HAVE_PIN | PMBUS_HAVE_STATUS_INPUT |
  621. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  622. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP;
  623. ret = adm1275_enable_vout_temp(client, config);
  624. if (ret)
  625. return ret;
  626. if (config & ADM1278_VIN_EN)
  627. info->func[0] |= PMBUS_HAVE_VIN;
  628. break;
  629. case adm1293:
  630. case adm1294:
  631. data->have_iout_min = true;
  632. data->have_pin_min = true;
  633. data->have_pin_max = true;
  634. data->have_mfr_vaux_status = true;
  635. data->have_power_sampling = true;
  636. coefficients = adm1293_coefficients;
  637. voindex = 0;
  638. switch (config & ADM1293_VIN_SEL_MASK) {
  639. case ADM1293_VIN_SEL_012: /* 1.2V */
  640. vindex = 0;
  641. break;
  642. case ADM1293_VIN_SEL_074: /* 7.4V */
  643. vindex = 1;
  644. break;
  645. case ADM1293_VIN_SEL_210: /* 21V */
  646. vindex = 2;
  647. break;
  648. default: /* disabled */
  649. break;
  650. }
  651. switch (config & ADM1293_IRANGE_MASK) {
  652. case ADM1293_IRANGE_25:
  653. cindex = 3;
  654. break;
  655. case ADM1293_IRANGE_50:
  656. cindex = 4;
  657. break;
  658. case ADM1293_IRANGE_100:
  659. cindex = 5;
  660. break;
  661. case ADM1293_IRANGE_200:
  662. cindex = 6;
  663. break;
  664. }
  665. if (vindex >= 0)
  666. pindex = 7 + vindex * 4 + (cindex - 3);
  667. if (config & ADM1293_VAUX_EN)
  668. info->func[0] |=
  669. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
  670. info->func[0] |= PMBUS_HAVE_PIN |
  671. PMBUS_HAVE_VIN | PMBUS_HAVE_STATUS_INPUT;
  672. break;
  673. default:
  674. dev_err(&client->dev, "Unsupported device\n");
  675. return -ENODEV;
  676. }
  677. if (data->have_power_sampling &&
  678. of_property_read_u32(client->dev.of_node,
  679. "adi,power-sample-average", &avg) == 0) {
  680. if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
  681. BIT(__fls(avg)) != avg) {
  682. dev_err(&client->dev,
  683. "Invalid number of power samples");
  684. return -EINVAL;
  685. }
  686. ret = adm1275_write_pmon_config(data, client, true,
  687. ilog2(avg));
  688. if (ret < 0) {
  689. dev_err(&client->dev,
  690. "Setting power sample averaging failed with error %d",
  691. ret);
  692. return ret;
  693. }
  694. }
  695. if (of_property_read_u32(client->dev.of_node,
  696. "adi,volt-curr-sample-average", &avg) == 0) {
  697. if (!avg || avg > ADM1275_SAMPLES_AVG_MAX ||
  698. BIT(__fls(avg)) != avg) {
  699. dev_err(&client->dev,
  700. "Invalid number of voltage/current samples");
  701. return -EINVAL;
  702. }
  703. ret = adm1275_write_pmon_config(data, client, false,
  704. ilog2(avg));
  705. if (ret < 0) {
  706. dev_err(&client->dev,
  707. "Setting voltage and current sample averaging failed with error %d",
  708. ret);
  709. return ret;
  710. }
  711. }
  712. if (voindex < 0)
  713. voindex = vindex;
  714. if (vindex >= 0) {
  715. info->m[PSC_VOLTAGE_IN] = coefficients[vindex].m;
  716. info->b[PSC_VOLTAGE_IN] = coefficients[vindex].b;
  717. info->R[PSC_VOLTAGE_IN] = coefficients[vindex].R;
  718. }
  719. if (voindex >= 0) {
  720. info->m[PSC_VOLTAGE_OUT] = coefficients[voindex].m;
  721. info->b[PSC_VOLTAGE_OUT] = coefficients[voindex].b;
  722. info->R[PSC_VOLTAGE_OUT] = coefficients[voindex].R;
  723. }
  724. if (cindex >= 0) {
  725. /* Scale current with sense resistor value */
  726. info->m[PSC_CURRENT_OUT] =
  727. coefficients[cindex].m * shunt / 1000;
  728. info->b[PSC_CURRENT_OUT] = coefficients[cindex].b;
  729. info->R[PSC_CURRENT_OUT] = coefficients[cindex].R;
  730. }
  731. if (pindex >= 0) {
  732. info->m[PSC_POWER] =
  733. coefficients[pindex].m * shunt / 1000;
  734. info->b[PSC_POWER] = coefficients[pindex].b;
  735. info->R[PSC_POWER] = coefficients[pindex].R;
  736. }
  737. if (tindex >= 0) {
  738. info->m[PSC_TEMPERATURE] = coefficients[tindex].m;
  739. info->b[PSC_TEMPERATURE] = coefficients[tindex].b;
  740. info->R[PSC_TEMPERATURE] = coefficients[tindex].R;
  741. }
  742. return pmbus_do_probe(client, info);
  743. }
  744. static struct i2c_driver adm1275_driver = {
  745. .driver = {
  746. .name = "adm1275",
  747. },
  748. .probe_new = adm1275_probe,
  749. .id_table = adm1275_id,
  750. };
  751. module_i2c_driver(adm1275_driver);
  752. MODULE_AUTHOR("Guenter Roeck");
  753. MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1275 and compatibles");
  754. MODULE_LICENSE("GPL");
  755. MODULE_IMPORT_NS(PMBUS);