max20730.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for MAX20710, MAX20730, MAX20734, and MAX20743 Integrated,
  4. * Step-Down Switching Regulators
  5. *
  6. * Copyright 2019 Google LLC.
  7. * Copyright 2020 Maxim Integrated
  8. */
  9. #include <linux/bits.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/err.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/mutex.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pmbus.h>
  19. #include <linux/util_macros.h>
  20. #include "pmbus.h"
  21. enum chips {
  22. max20710,
  23. max20730,
  24. max20734,
  25. max20743
  26. };
  27. enum {
  28. MAX20730_DEBUGFS_VOUT_MIN = 0,
  29. MAX20730_DEBUGFS_FREQUENCY,
  30. MAX20730_DEBUGFS_PG_DELAY,
  31. MAX20730_DEBUGFS_INTERNAL_GAIN,
  32. MAX20730_DEBUGFS_BOOT_VOLTAGE,
  33. MAX20730_DEBUGFS_OUT_V_RAMP_RATE,
  34. MAX20730_DEBUGFS_OC_PROTECT_MODE,
  35. MAX20730_DEBUGFS_SS_TIMING,
  36. MAX20730_DEBUGFS_IMAX,
  37. MAX20730_DEBUGFS_OPERATION,
  38. MAX20730_DEBUGFS_ON_OFF_CONFIG,
  39. MAX20730_DEBUGFS_SMBALERT_MASK,
  40. MAX20730_DEBUGFS_VOUT_MODE,
  41. MAX20730_DEBUGFS_VOUT_COMMAND,
  42. MAX20730_DEBUGFS_VOUT_MAX,
  43. MAX20730_DEBUGFS_NUM_ENTRIES
  44. };
  45. struct max20730_data {
  46. enum chips id;
  47. struct pmbus_driver_info info;
  48. struct mutex lock; /* Used to protect against parallel writes */
  49. u16 mfr_devset1;
  50. u16 mfr_devset2;
  51. u16 mfr_voutmin;
  52. u32 vout_voltage_divider[2];
  53. };
  54. #define to_max20730_data(x) container_of(x, struct max20730_data, info)
  55. #define VOLT_FROM_REG(val) DIV_ROUND_CLOSEST((val), 1 << 9)
  56. #define PMBUS_SMB_ALERT_MASK 0x1B
  57. #define MAX20730_MFR_VOUT_MIN 0xd1
  58. #define MAX20730_MFR_DEVSET1 0xd2
  59. #define MAX20730_MFR_DEVSET2 0xd3
  60. #define MAX20730_MFR_VOUT_MIN_MASK GENMASK(9, 0)
  61. #define MAX20730_MFR_VOUT_MIN_BIT_POS 0
  62. #define MAX20730_MFR_DEVSET1_RGAIN_MASK (BIT(13) | BIT(14))
  63. #define MAX20730_MFR_DEVSET1_OTP_MASK (BIT(11) | BIT(12))
  64. #define MAX20730_MFR_DEVSET1_VBOOT_MASK (BIT(8) | BIT(9))
  65. #define MAX20730_MFR_DEVSET1_OCP_MASK (BIT(5) | BIT(6))
  66. #define MAX20730_MFR_DEVSET1_FSW_MASK GENMASK(4, 2)
  67. #define MAX20730_MFR_DEVSET1_TSTAT_MASK (BIT(0) | BIT(1))
  68. #define MAX20730_MFR_DEVSET1_RGAIN_BIT_POS 13
  69. #define MAX20730_MFR_DEVSET1_OTP_BIT_POS 11
  70. #define MAX20730_MFR_DEVSET1_VBOOT_BIT_POS 8
  71. #define MAX20730_MFR_DEVSET1_OCP_BIT_POS 5
  72. #define MAX20730_MFR_DEVSET1_FSW_BIT_POS 2
  73. #define MAX20730_MFR_DEVSET1_TSTAT_BIT_POS 0
  74. #define MAX20730_MFR_DEVSET2_IMAX_MASK GENMASK(10, 8)
  75. #define MAX20730_MFR_DEVSET2_VRATE (BIT(6) | BIT(7))
  76. #define MAX20730_MFR_DEVSET2_OCPM_MASK BIT(5)
  77. #define MAX20730_MFR_DEVSET2_SS_MASK (BIT(0) | BIT(1))
  78. #define MAX20730_MFR_DEVSET2_IMAX_BIT_POS 8
  79. #define MAX20730_MFR_DEVSET2_VRATE_BIT_POS 6
  80. #define MAX20730_MFR_DEVSET2_OCPM_BIT_POS 5
  81. #define MAX20730_MFR_DEVSET2_SS_BIT_POS 0
  82. #define DEBUG_FS_DATA_MAX 16
  83. struct max20730_debugfs_data {
  84. struct i2c_client *client;
  85. int debugfs_entries[MAX20730_DEBUGFS_NUM_ENTRIES];
  86. };
  87. #define to_psu(x, y) container_of((x), \
  88. struct max20730_debugfs_data, debugfs_entries[(y)])
  89. #ifdef CONFIG_DEBUG_FS
  90. static ssize_t max20730_debugfs_read(struct file *file, char __user *buf,
  91. size_t count, loff_t *ppos)
  92. {
  93. int ret, len;
  94. int *idxp = file->private_data;
  95. int idx = *idxp;
  96. struct max20730_debugfs_data *psu = to_psu(idxp, idx);
  97. const struct pmbus_driver_info *info;
  98. const struct max20730_data *data;
  99. char tbuf[DEBUG_FS_DATA_MAX] = { 0 };
  100. u16 val;
  101. info = pmbus_get_driver_info(psu->client);
  102. data = to_max20730_data(info);
  103. switch (idx) {
  104. case MAX20730_DEBUGFS_VOUT_MIN:
  105. ret = VOLT_FROM_REG(data->mfr_voutmin * 10000);
  106. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d.%d\n",
  107. ret / 10000, ret % 10000);
  108. break;
  109. case MAX20730_DEBUGFS_FREQUENCY:
  110. val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_FSW_MASK)
  111. >> MAX20730_MFR_DEVSET1_FSW_BIT_POS;
  112. if (val == 0)
  113. ret = 400;
  114. else if (val == 1)
  115. ret = 500;
  116. else if (val == 2 || val == 3)
  117. ret = 600;
  118. else if (val == 4)
  119. ret = 700;
  120. else if (val == 5)
  121. ret = 800;
  122. else
  123. ret = 900;
  124. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  125. break;
  126. case MAX20730_DEBUGFS_PG_DELAY:
  127. val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_TSTAT_MASK)
  128. >> MAX20730_MFR_DEVSET1_TSTAT_BIT_POS;
  129. if (val == 0)
  130. len = strlcpy(tbuf, "2000\n", DEBUG_FS_DATA_MAX);
  131. else if (val == 1)
  132. len = strlcpy(tbuf, "125\n", DEBUG_FS_DATA_MAX);
  133. else if (val == 2)
  134. len = strlcpy(tbuf, "62.5\n", DEBUG_FS_DATA_MAX);
  135. else
  136. len = strlcpy(tbuf, "32\n", DEBUG_FS_DATA_MAX);
  137. break;
  138. case MAX20730_DEBUGFS_INTERNAL_GAIN:
  139. val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_RGAIN_MASK)
  140. >> MAX20730_MFR_DEVSET1_RGAIN_BIT_POS;
  141. if (data->id == max20734) {
  142. /* AN6209 */
  143. if (val == 0)
  144. len = strlcpy(tbuf, "0.8\n", DEBUG_FS_DATA_MAX);
  145. else if (val == 1)
  146. len = strlcpy(tbuf, "3.2\n", DEBUG_FS_DATA_MAX);
  147. else if (val == 2)
  148. len = strlcpy(tbuf, "1.6\n", DEBUG_FS_DATA_MAX);
  149. else
  150. len = strlcpy(tbuf, "6.4\n", DEBUG_FS_DATA_MAX);
  151. } else if (data->id == max20730 || data->id == max20710) {
  152. /* AN6042 or AN6140 */
  153. if (val == 0)
  154. len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
  155. else if (val == 1)
  156. len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
  157. else if (val == 2)
  158. len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
  159. else
  160. len = strlcpy(tbuf, "7.2\n", DEBUG_FS_DATA_MAX);
  161. } else if (data->id == max20743) {
  162. /* AN6042 */
  163. if (val == 0)
  164. len = strlcpy(tbuf, "0.45\n", DEBUG_FS_DATA_MAX);
  165. else if (val == 1)
  166. len = strlcpy(tbuf, "1.8\n", DEBUG_FS_DATA_MAX);
  167. else if (val == 2)
  168. len = strlcpy(tbuf, "0.9\n", DEBUG_FS_DATA_MAX);
  169. else
  170. len = strlcpy(tbuf, "3.6\n", DEBUG_FS_DATA_MAX);
  171. } else {
  172. len = strlcpy(tbuf, "Not supported\n", DEBUG_FS_DATA_MAX);
  173. }
  174. break;
  175. case MAX20730_DEBUGFS_BOOT_VOLTAGE:
  176. val = (data->mfr_devset1 & MAX20730_MFR_DEVSET1_VBOOT_MASK)
  177. >> MAX20730_MFR_DEVSET1_VBOOT_BIT_POS;
  178. if (val == 0)
  179. len = strlcpy(tbuf, "0.6484\n", DEBUG_FS_DATA_MAX);
  180. else if (val == 1)
  181. len = strlcpy(tbuf, "0.8984\n", DEBUG_FS_DATA_MAX);
  182. else if (val == 2)
  183. len = strlcpy(tbuf, "1.0\n", DEBUG_FS_DATA_MAX);
  184. else
  185. len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
  186. break;
  187. case MAX20730_DEBUGFS_OUT_V_RAMP_RATE:
  188. val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_VRATE)
  189. >> MAX20730_MFR_DEVSET2_VRATE_BIT_POS;
  190. if (val == 0)
  191. len = strlcpy(tbuf, "4\n", DEBUG_FS_DATA_MAX);
  192. else if (val == 1)
  193. len = strlcpy(tbuf, "2\n", DEBUG_FS_DATA_MAX);
  194. else if (val == 2)
  195. len = strlcpy(tbuf, "1\n", DEBUG_FS_DATA_MAX);
  196. else
  197. len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
  198. break;
  199. case MAX20730_DEBUGFS_OC_PROTECT_MODE:
  200. ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_OCPM_MASK)
  201. >> MAX20730_MFR_DEVSET2_OCPM_BIT_POS;
  202. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  203. break;
  204. case MAX20730_DEBUGFS_SS_TIMING:
  205. val = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_SS_MASK)
  206. >> MAX20730_MFR_DEVSET2_SS_BIT_POS;
  207. if (val == 0)
  208. len = strlcpy(tbuf, "0.75\n", DEBUG_FS_DATA_MAX);
  209. else if (val == 1)
  210. len = strlcpy(tbuf, "1.5\n", DEBUG_FS_DATA_MAX);
  211. else if (val == 2)
  212. len = strlcpy(tbuf, "3\n", DEBUG_FS_DATA_MAX);
  213. else
  214. len = strlcpy(tbuf, "6\n", DEBUG_FS_DATA_MAX);
  215. break;
  216. case MAX20730_DEBUGFS_IMAX:
  217. ret = (data->mfr_devset2 & MAX20730_MFR_DEVSET2_IMAX_MASK)
  218. >> MAX20730_MFR_DEVSET2_IMAX_BIT_POS;
  219. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  220. break;
  221. case MAX20730_DEBUGFS_OPERATION:
  222. ret = i2c_smbus_read_byte_data(psu->client, PMBUS_OPERATION);
  223. if (ret < 0)
  224. return ret;
  225. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  226. break;
  227. case MAX20730_DEBUGFS_ON_OFF_CONFIG:
  228. ret = i2c_smbus_read_byte_data(psu->client, PMBUS_ON_OFF_CONFIG);
  229. if (ret < 0)
  230. return ret;
  231. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  232. break;
  233. case MAX20730_DEBUGFS_SMBALERT_MASK:
  234. ret = i2c_smbus_read_word_data(psu->client,
  235. PMBUS_SMB_ALERT_MASK);
  236. if (ret < 0)
  237. return ret;
  238. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  239. break;
  240. case MAX20730_DEBUGFS_VOUT_MODE:
  241. ret = i2c_smbus_read_byte_data(psu->client, PMBUS_VOUT_MODE);
  242. if (ret < 0)
  243. return ret;
  244. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX, "%d\n", ret);
  245. break;
  246. case MAX20730_DEBUGFS_VOUT_COMMAND:
  247. ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_COMMAND);
  248. if (ret < 0)
  249. return ret;
  250. ret = VOLT_FROM_REG(ret * 10000);
  251. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
  252. "%d.%d\n", ret / 10000, ret % 10000);
  253. break;
  254. case MAX20730_DEBUGFS_VOUT_MAX:
  255. ret = i2c_smbus_read_word_data(psu->client, PMBUS_VOUT_MAX);
  256. if (ret < 0)
  257. return ret;
  258. ret = VOLT_FROM_REG(ret * 10000);
  259. len = scnprintf(tbuf, DEBUG_FS_DATA_MAX,
  260. "%d.%d\n", ret / 10000, ret % 10000);
  261. break;
  262. default:
  263. len = strlcpy(tbuf, "Invalid\n", DEBUG_FS_DATA_MAX);
  264. }
  265. return simple_read_from_buffer(buf, count, ppos, tbuf, len);
  266. }
  267. static const struct file_operations max20730_fops = {
  268. .llseek = noop_llseek,
  269. .read = max20730_debugfs_read,
  270. .write = NULL,
  271. .open = simple_open,
  272. };
  273. static int max20730_init_debugfs(struct i2c_client *client,
  274. struct max20730_data *data)
  275. {
  276. int ret, i;
  277. struct dentry *debugfs;
  278. struct dentry *max20730_dir;
  279. struct max20730_debugfs_data *psu;
  280. ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET2);
  281. if (ret < 0)
  282. return ret;
  283. data->mfr_devset2 = ret;
  284. ret = i2c_smbus_read_word_data(client, MAX20730_MFR_VOUT_MIN);
  285. if (ret < 0)
  286. return ret;
  287. data->mfr_voutmin = ret;
  288. psu = devm_kzalloc(&client->dev, sizeof(*psu), GFP_KERNEL);
  289. if (!psu)
  290. return -ENOMEM;
  291. psu->client = client;
  292. debugfs = pmbus_get_debugfs_dir(client);
  293. if (!debugfs)
  294. return -ENOENT;
  295. max20730_dir = debugfs_create_dir(client->name, debugfs);
  296. for (i = 0; i < MAX20730_DEBUGFS_NUM_ENTRIES; ++i)
  297. psu->debugfs_entries[i] = i;
  298. debugfs_create_file("vout_min", 0444, max20730_dir,
  299. &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MIN],
  300. &max20730_fops);
  301. debugfs_create_file("frequency", 0444, max20730_dir,
  302. &psu->debugfs_entries[MAX20730_DEBUGFS_FREQUENCY],
  303. &max20730_fops);
  304. debugfs_create_file("power_good_delay", 0444, max20730_dir,
  305. &psu->debugfs_entries[MAX20730_DEBUGFS_PG_DELAY],
  306. &max20730_fops);
  307. debugfs_create_file("internal_gain", 0444, max20730_dir,
  308. &psu->debugfs_entries[MAX20730_DEBUGFS_INTERNAL_GAIN],
  309. &max20730_fops);
  310. debugfs_create_file("boot_voltage", 0444, max20730_dir,
  311. &psu->debugfs_entries[MAX20730_DEBUGFS_BOOT_VOLTAGE],
  312. &max20730_fops);
  313. debugfs_create_file("out_voltage_ramp_rate", 0444, max20730_dir,
  314. &psu->debugfs_entries[MAX20730_DEBUGFS_OUT_V_RAMP_RATE],
  315. &max20730_fops);
  316. debugfs_create_file("oc_protection_mode", 0444, max20730_dir,
  317. &psu->debugfs_entries[MAX20730_DEBUGFS_OC_PROTECT_MODE],
  318. &max20730_fops);
  319. debugfs_create_file("soft_start_timing", 0444, max20730_dir,
  320. &psu->debugfs_entries[MAX20730_DEBUGFS_SS_TIMING],
  321. &max20730_fops);
  322. debugfs_create_file("imax", 0444, max20730_dir,
  323. &psu->debugfs_entries[MAX20730_DEBUGFS_IMAX],
  324. &max20730_fops);
  325. debugfs_create_file("operation", 0444, max20730_dir,
  326. &psu->debugfs_entries[MAX20730_DEBUGFS_OPERATION],
  327. &max20730_fops);
  328. debugfs_create_file("on_off_config", 0444, max20730_dir,
  329. &psu->debugfs_entries[MAX20730_DEBUGFS_ON_OFF_CONFIG],
  330. &max20730_fops);
  331. debugfs_create_file("smbalert_mask", 0444, max20730_dir,
  332. &psu->debugfs_entries[MAX20730_DEBUGFS_SMBALERT_MASK],
  333. &max20730_fops);
  334. debugfs_create_file("vout_mode", 0444, max20730_dir,
  335. &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MODE],
  336. &max20730_fops);
  337. debugfs_create_file("vout_command", 0444, max20730_dir,
  338. &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_COMMAND],
  339. &max20730_fops);
  340. debugfs_create_file("vout_max", 0444, max20730_dir,
  341. &psu->debugfs_entries[MAX20730_DEBUGFS_VOUT_MAX],
  342. &max20730_fops);
  343. return 0;
  344. }
  345. #else
  346. static int max20730_init_debugfs(struct i2c_client *client,
  347. struct max20730_data *data)
  348. {
  349. return 0;
  350. }
  351. #endif /* CONFIG_DEBUG_FS */
  352. static const struct i2c_device_id max20730_id[];
  353. /*
  354. * Convert discreet value to direct data format. Strictly speaking, all passed
  355. * values are constants, so we could do that calculation manually. On the
  356. * downside, that would make the driver more difficult to maintain, so lets
  357. * use this approach.
  358. */
  359. static u16 val_to_direct(int v, enum pmbus_sensor_classes class,
  360. const struct pmbus_driver_info *info)
  361. {
  362. int R = info->R[class] - 3; /* take milli-units into account */
  363. int b = info->b[class] * 1000;
  364. long d;
  365. d = v * info->m[class] + b;
  366. /*
  367. * R < 0 is true for all callers, so we don't need to bother
  368. * about the R > 0 case.
  369. */
  370. while (R < 0) {
  371. d = DIV_ROUND_CLOSEST(d, 10);
  372. R++;
  373. }
  374. return (u16)d;
  375. }
  376. static long direct_to_val(u16 w, enum pmbus_sensor_classes class,
  377. const struct pmbus_driver_info *info)
  378. {
  379. int R = info->R[class] - 3;
  380. int b = info->b[class] * 1000;
  381. int m = info->m[class];
  382. long d = (s16)w;
  383. if (m == 0)
  384. return 0;
  385. while (R < 0) {
  386. d *= 10;
  387. R++;
  388. }
  389. d = (d - b) / m;
  390. return d;
  391. }
  392. static u32 max_current[][5] = {
  393. [max20710] = { 6200, 8000, 9700, 11600 },
  394. [max20730] = { 13000, 16600, 20100, 23600 },
  395. [max20734] = { 21000, 27000, 32000, 38000 },
  396. [max20743] = { 18900, 24100, 29200, 34100 },
  397. };
  398. static int max20730_read_word_data(struct i2c_client *client, int page,
  399. int phase, int reg)
  400. {
  401. const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
  402. const struct max20730_data *data = to_max20730_data(info);
  403. int ret = 0;
  404. u32 max_c;
  405. switch (reg) {
  406. case PMBUS_OT_FAULT_LIMIT:
  407. switch ((data->mfr_devset1 >> 11) & 0x3) {
  408. case 0x0:
  409. ret = val_to_direct(150000, PSC_TEMPERATURE, info);
  410. break;
  411. case 0x1:
  412. ret = val_to_direct(130000, PSC_TEMPERATURE, info);
  413. break;
  414. default:
  415. ret = -ENODATA;
  416. break;
  417. }
  418. break;
  419. case PMBUS_IOUT_OC_FAULT_LIMIT:
  420. max_c = max_current[data->id][(data->mfr_devset1 >> 5) & 0x3];
  421. ret = val_to_direct(max_c, PSC_CURRENT_OUT, info);
  422. break;
  423. case PMBUS_READ_VOUT:
  424. ret = pmbus_read_word_data(client, page, phase, reg);
  425. if (ret > 0 && data->vout_voltage_divider[0] && data->vout_voltage_divider[1]) {
  426. u64 temp = DIV_ROUND_CLOSEST_ULL((u64)ret * data->vout_voltage_divider[1],
  427. data->vout_voltage_divider[0]);
  428. ret = clamp_val(temp, 0, 0xffff);
  429. }
  430. break;
  431. default:
  432. ret = -ENODATA;
  433. break;
  434. }
  435. return ret;
  436. }
  437. static int max20730_write_word_data(struct i2c_client *client, int page,
  438. int reg, u16 word)
  439. {
  440. struct pmbus_driver_info *info;
  441. struct max20730_data *data;
  442. u16 devset1;
  443. int ret = 0;
  444. int idx;
  445. info = (struct pmbus_driver_info *)pmbus_get_driver_info(client);
  446. data = to_max20730_data(info);
  447. mutex_lock(&data->lock);
  448. devset1 = data->mfr_devset1;
  449. switch (reg) {
  450. case PMBUS_OT_FAULT_LIMIT:
  451. devset1 &= ~(BIT(11) | BIT(12));
  452. if (direct_to_val(word, PSC_TEMPERATURE, info) < 140000)
  453. devset1 |= BIT(11);
  454. break;
  455. case PMBUS_IOUT_OC_FAULT_LIMIT:
  456. devset1 &= ~(BIT(5) | BIT(6));
  457. idx = find_closest(direct_to_val(word, PSC_CURRENT_OUT, info),
  458. max_current[data->id], 4);
  459. devset1 |= (idx << 5);
  460. break;
  461. default:
  462. ret = -ENODATA;
  463. break;
  464. }
  465. if (!ret && devset1 != data->mfr_devset1) {
  466. ret = i2c_smbus_write_word_data(client, MAX20730_MFR_DEVSET1,
  467. devset1);
  468. if (!ret) {
  469. data->mfr_devset1 = devset1;
  470. pmbus_clear_cache(client);
  471. }
  472. }
  473. mutex_unlock(&data->lock);
  474. return ret;
  475. }
  476. static const struct pmbus_driver_info max20730_info[] = {
  477. [max20710] = {
  478. .pages = 1,
  479. .read_word_data = max20730_read_word_data,
  480. .write_word_data = max20730_write_word_data,
  481. /* Source : Maxim AN6140 and AN6042 */
  482. .format[PSC_TEMPERATURE] = direct,
  483. .m[PSC_TEMPERATURE] = 21,
  484. .b[PSC_TEMPERATURE] = 5887,
  485. .R[PSC_TEMPERATURE] = -1,
  486. .format[PSC_VOLTAGE_IN] = direct,
  487. .m[PSC_VOLTAGE_IN] = 3609,
  488. .b[PSC_VOLTAGE_IN] = 0,
  489. .R[PSC_VOLTAGE_IN] = -2,
  490. .format[PSC_CURRENT_OUT] = direct,
  491. .m[PSC_CURRENT_OUT] = 153,
  492. .b[PSC_CURRENT_OUT] = 4976,
  493. .R[PSC_CURRENT_OUT] = -1,
  494. .format[PSC_VOLTAGE_OUT] = linear,
  495. .func[0] = PMBUS_HAVE_VIN |
  496. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  497. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  498. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  499. PMBUS_HAVE_STATUS_INPUT,
  500. },
  501. [max20730] = {
  502. .pages = 1,
  503. .read_word_data = max20730_read_word_data,
  504. .write_word_data = max20730_write_word_data,
  505. /* Source : Maxim AN6042 */
  506. .format[PSC_TEMPERATURE] = direct,
  507. .m[PSC_TEMPERATURE] = 21,
  508. .b[PSC_TEMPERATURE] = 5887,
  509. .R[PSC_TEMPERATURE] = -1,
  510. .format[PSC_VOLTAGE_IN] = direct,
  511. .m[PSC_VOLTAGE_IN] = 3609,
  512. .b[PSC_VOLTAGE_IN] = 0,
  513. .R[PSC_VOLTAGE_IN] = -2,
  514. /*
  515. * Values in the datasheet are adjusted for temperature and
  516. * for the relationship between Vin and Vout.
  517. * Unfortunately, the data sheet suggests that Vout measurement
  518. * may be scaled with a resistor array. This is indeed the case
  519. * at least on the evaulation boards. As a result, any in-driver
  520. * adjustments would either be wrong or require elaborate means
  521. * to configure the scaling. Instead of doing that, just report
  522. * raw values and let userspace handle adjustments.
  523. */
  524. .format[PSC_CURRENT_OUT] = direct,
  525. .m[PSC_CURRENT_OUT] = 153,
  526. .b[PSC_CURRENT_OUT] = 4976,
  527. .R[PSC_CURRENT_OUT] = -1,
  528. .format[PSC_VOLTAGE_OUT] = linear,
  529. .func[0] = PMBUS_HAVE_VIN |
  530. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  531. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  532. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  533. PMBUS_HAVE_STATUS_INPUT,
  534. },
  535. [max20734] = {
  536. .pages = 1,
  537. .read_word_data = max20730_read_word_data,
  538. .write_word_data = max20730_write_word_data,
  539. /* Source : Maxim AN6209 */
  540. .format[PSC_TEMPERATURE] = direct,
  541. .m[PSC_TEMPERATURE] = 21,
  542. .b[PSC_TEMPERATURE] = 5887,
  543. .R[PSC_TEMPERATURE] = -1,
  544. .format[PSC_VOLTAGE_IN] = direct,
  545. .m[PSC_VOLTAGE_IN] = 3592,
  546. .b[PSC_VOLTAGE_IN] = 0,
  547. .R[PSC_VOLTAGE_IN] = -2,
  548. .format[PSC_CURRENT_OUT] = direct,
  549. .m[PSC_CURRENT_OUT] = 111,
  550. .b[PSC_CURRENT_OUT] = 3461,
  551. .R[PSC_CURRENT_OUT] = -1,
  552. .format[PSC_VOLTAGE_OUT] = linear,
  553. .func[0] = PMBUS_HAVE_VIN |
  554. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  555. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  556. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  557. PMBUS_HAVE_STATUS_INPUT,
  558. },
  559. [max20743] = {
  560. .pages = 1,
  561. .read_word_data = max20730_read_word_data,
  562. .write_word_data = max20730_write_word_data,
  563. /* Source : Maxim AN6042 */
  564. .format[PSC_TEMPERATURE] = direct,
  565. .m[PSC_TEMPERATURE] = 21,
  566. .b[PSC_TEMPERATURE] = 5887,
  567. .R[PSC_TEMPERATURE] = -1,
  568. .format[PSC_VOLTAGE_IN] = direct,
  569. .m[PSC_VOLTAGE_IN] = 3597,
  570. .b[PSC_VOLTAGE_IN] = 0,
  571. .R[PSC_VOLTAGE_IN] = -2,
  572. .format[PSC_CURRENT_OUT] = direct,
  573. .m[PSC_CURRENT_OUT] = 95,
  574. .b[PSC_CURRENT_OUT] = 5014,
  575. .R[PSC_CURRENT_OUT] = -1,
  576. .format[PSC_VOLTAGE_OUT] = linear,
  577. .func[0] = PMBUS_HAVE_VIN |
  578. PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
  579. PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
  580. PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
  581. PMBUS_HAVE_STATUS_INPUT,
  582. },
  583. };
  584. static int max20730_probe(struct i2c_client *client)
  585. {
  586. struct device *dev = &client->dev;
  587. u8 buf[I2C_SMBUS_BLOCK_MAX + 1];
  588. struct max20730_data *data;
  589. enum chips chip_id;
  590. int ret;
  591. if (!i2c_check_functionality(client->adapter,
  592. I2C_FUNC_SMBUS_READ_BYTE_DATA |
  593. I2C_FUNC_SMBUS_READ_WORD_DATA |
  594. I2C_FUNC_SMBUS_BLOCK_DATA))
  595. return -ENODEV;
  596. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_ID, buf);
  597. if (ret < 0) {
  598. dev_err(&client->dev, "Failed to read Manufacturer ID\n");
  599. return ret;
  600. }
  601. if (ret != 5 || strncmp(buf, "MAXIM", 5)) {
  602. buf[ret] = '\0';
  603. dev_err(dev, "Unsupported Manufacturer ID '%s'\n", buf);
  604. return -ENODEV;
  605. }
  606. /*
  607. * The chips support reading PMBUS_MFR_MODEL. On both MAX20730
  608. * and MAX20734, reading it returns M20743. Presumably that is
  609. * the reason why the command is not documented. Unfortunately,
  610. * that means that there is no reliable means to detect the chip.
  611. * However, we can at least detect the chip series. Compare
  612. * the returned value against 'M20743' and bail out if there is
  613. * a mismatch. If that doesn't work for all chips, we may have
  614. * to remove this check.
  615. */
  616. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_MODEL, buf);
  617. if (ret < 0) {
  618. dev_err(dev, "Failed to read Manufacturer Model\n");
  619. return ret;
  620. }
  621. if (ret != 6 || strncmp(buf, "M20743", 6)) {
  622. buf[ret] = '\0';
  623. dev_err(dev, "Unsupported Manufacturer Model '%s'\n", buf);
  624. return -ENODEV;
  625. }
  626. ret = i2c_smbus_read_block_data(client, PMBUS_MFR_REVISION, buf);
  627. if (ret < 0) {
  628. dev_err(dev, "Failed to read Manufacturer Revision\n");
  629. return ret;
  630. }
  631. if (ret != 1 || buf[0] != 'F') {
  632. buf[ret] = '\0';
  633. dev_err(dev, "Unsupported Manufacturer Revision '%s'\n", buf);
  634. return -ENODEV;
  635. }
  636. if (client->dev.of_node)
  637. chip_id = (enum chips)of_device_get_match_data(dev);
  638. else
  639. chip_id = i2c_match_id(max20730_id, client)->driver_data;
  640. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  641. if (!data)
  642. return -ENOMEM;
  643. data->id = chip_id;
  644. mutex_init(&data->lock);
  645. memcpy(&data->info, &max20730_info[chip_id], sizeof(data->info));
  646. if (of_property_read_u32_array(client->dev.of_node, "vout-voltage-divider",
  647. data->vout_voltage_divider,
  648. ARRAY_SIZE(data->vout_voltage_divider)) != 0)
  649. memset(data->vout_voltage_divider, 0, sizeof(data->vout_voltage_divider));
  650. if (data->vout_voltage_divider[1] < data->vout_voltage_divider[0]) {
  651. dev_err(dev,
  652. "The total resistance of voltage divider is less than output resistance\n");
  653. return -EINVAL;
  654. }
  655. ret = i2c_smbus_read_word_data(client, MAX20730_MFR_DEVSET1);
  656. if (ret < 0)
  657. return ret;
  658. data->mfr_devset1 = ret;
  659. ret = pmbus_do_probe(client, &data->info);
  660. if (ret < 0)
  661. return ret;
  662. ret = max20730_init_debugfs(client, data);
  663. if (ret)
  664. dev_warn(dev, "Failed to register debugfs: %d\n",
  665. ret);
  666. return 0;
  667. }
  668. static const struct i2c_device_id max20730_id[] = {
  669. { "max20710", max20710 },
  670. { "max20730", max20730 },
  671. { "max20734", max20734 },
  672. { "max20743", max20743 },
  673. { },
  674. };
  675. MODULE_DEVICE_TABLE(i2c, max20730_id);
  676. static const struct of_device_id max20730_of_match[] = {
  677. { .compatible = "maxim,max20710", .data = (void *)max20710 },
  678. { .compatible = "maxim,max20730", .data = (void *)max20730 },
  679. { .compatible = "maxim,max20734", .data = (void *)max20734 },
  680. { .compatible = "maxim,max20743", .data = (void *)max20743 },
  681. { },
  682. };
  683. MODULE_DEVICE_TABLE(of, max20730_of_match);
  684. static struct i2c_driver max20730_driver = {
  685. .driver = {
  686. .name = "max20730",
  687. .of_match_table = max20730_of_match,
  688. },
  689. .probe_new = max20730_probe,
  690. .id_table = max20730_id,
  691. };
  692. module_i2c_driver(max20730_driver);
  693. MODULE_AUTHOR("Guenter Roeck <[email protected]>");
  694. MODULE_DESCRIPTION("PMBus driver for Maxim MAX20710 / MAX20730 / MAX20734 / MAX20743");
  695. MODULE_LICENSE("GPL");
  696. MODULE_IMPORT_NS(PMBUS);