0058-v6.0-mfd-qcom-spmi-pmic-expose-the-PMIC-revid-information.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. From 231f6a9f24a5e9b6e7af801ca2377970474cdf59 Mon Sep 17 00:00:00 2001
  2. From: Caleb Connolly <[email protected]>
  3. Date: Fri, 29 Apr 2022 23:08:57 +0100
  4. Subject: [PATCH] mfd: qcom-spmi-pmic: expose the PMIC revid information to
  5. clients
  6. Some PMIC functions such as the RRADC need to be aware of the PMIC
  7. chip revision information to implement errata or otherwise adjust
  8. behaviour, export the PMIC information to enable this.
  9. This is specifically required to enable the RRADC to adjust
  10. coefficients based on which chip fab the PMIC was produced in,
  11. this can vary per unique device and therefore has to be read at
  12. runtime.
  13. Signed-off-by: Caleb Connolly <[email protected]>
  14. Reviewed-by: Dmitry Baryshkov <[email protected]>
  15. Tested-by: Dmitry Baryshkov <[email protected]>
  16. Acked-by: Lee Jones <[email protected]>
  17. Link: https://lore.kernel.org/r/[email protected]
  18. Signed-off-by: Jonathan Cameron <[email protected]>
  19. ---
  20. drivers/mfd/qcom-spmi-pmic.c | 265 ++++++++++++++++++++----------
  21. include/soc/qcom/qcom-spmi-pmic.h | 60 +++++++
  22. 2 files changed, 235 insertions(+), 90 deletions(-)
  23. create mode 100644 include/soc/qcom/qcom-spmi-pmic.h
  24. --- a/drivers/mfd/qcom-spmi-pmic.c
  25. +++ b/drivers/mfd/qcom-spmi-pmic.c
  26. @@ -3,11 +3,16 @@
  27. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  28. */
  29. +#include <linux/device.h>
  30. +#include <linux/errno.h>
  31. +#include <linux/gfp.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/spmi.h>
  35. +#include <linux/types.h>
  36. #include <linux/regmap.h>
  37. #include <linux/of_platform.h>
  38. +#include <soc/qcom/qcom-spmi-pmic.h>
  39. #define PMIC_REV2 0x101
  40. #define PMIC_REV3 0x102
  41. @@ -17,106 +22,140 @@
  42. #define PMIC_TYPE_VALUE 0x51
  43. -#define COMMON_SUBTYPE 0x00
  44. -#define PM8941_SUBTYPE 0x01
  45. -#define PM8841_SUBTYPE 0x02
  46. -#define PM8019_SUBTYPE 0x03
  47. -#define PM8226_SUBTYPE 0x04
  48. -#define PM8110_SUBTYPE 0x05
  49. -#define PMA8084_SUBTYPE 0x06
  50. -#define PMI8962_SUBTYPE 0x07
  51. -#define PMD9635_SUBTYPE 0x08
  52. -#define PM8994_SUBTYPE 0x09
  53. -#define PMI8994_SUBTYPE 0x0a
  54. -#define PM8916_SUBTYPE 0x0b
  55. -#define PM8004_SUBTYPE 0x0c
  56. -#define PM8909_SUBTYPE 0x0d
  57. -#define PM8028_SUBTYPE 0x0e
  58. -#define PM8901_SUBTYPE 0x0f
  59. -#define PM8950_SUBTYPE 0x10
  60. -#define PMI8950_SUBTYPE 0x11
  61. -#define PM8998_SUBTYPE 0x14
  62. -#define PMI8998_SUBTYPE 0x15
  63. -#define PM8005_SUBTYPE 0x18
  64. -#define PM660L_SUBTYPE 0x1A
  65. -#define PM660_SUBTYPE 0x1B
  66. -#define PM8150_SUBTYPE 0x1E
  67. -#define PM8150L_SUBTYPE 0x1f
  68. -#define PM8150B_SUBTYPE 0x20
  69. -#define PMK8002_SUBTYPE 0x21
  70. -#define PM8009_SUBTYPE 0x24
  71. -#define PM8150C_SUBTYPE 0x26
  72. -#define SMB2351_SUBTYPE 0x29
  73. +#define PMIC_REV4_V2 0x02
  74. +
  75. +struct qcom_spmi_dev {
  76. + int num_usids;
  77. + struct qcom_spmi_pmic pmic;
  78. +};
  79. +
  80. +#define N_USIDS(n) ((void *)n)
  81. static const struct of_device_id pmic_spmi_id_table[] = {
  82. - { .compatible = "qcom,pm660", .data = (void *)PM660_SUBTYPE },
  83. - { .compatible = "qcom,pm660l", .data = (void *)PM660L_SUBTYPE },
  84. - { .compatible = "qcom,pm8004", .data = (void *)PM8004_SUBTYPE },
  85. - { .compatible = "qcom,pm8005", .data = (void *)PM8005_SUBTYPE },
  86. - { .compatible = "qcom,pm8019", .data = (void *)PM8019_SUBTYPE },
  87. - { .compatible = "qcom,pm8028", .data = (void *)PM8028_SUBTYPE },
  88. - { .compatible = "qcom,pm8110", .data = (void *)PM8110_SUBTYPE },
  89. - { .compatible = "qcom,pm8150", .data = (void *)PM8150_SUBTYPE },
  90. - { .compatible = "qcom,pm8150b", .data = (void *)PM8150B_SUBTYPE },
  91. - { .compatible = "qcom,pm8150c", .data = (void *)PM8150C_SUBTYPE },
  92. - { .compatible = "qcom,pm8150l", .data = (void *)PM8150L_SUBTYPE },
  93. - { .compatible = "qcom,pm8226", .data = (void *)PM8226_SUBTYPE },
  94. - { .compatible = "qcom,pm8841", .data = (void *)PM8841_SUBTYPE },
  95. - { .compatible = "qcom,pm8901", .data = (void *)PM8901_SUBTYPE },
  96. - { .compatible = "qcom,pm8909", .data = (void *)PM8909_SUBTYPE },
  97. - { .compatible = "qcom,pm8916", .data = (void *)PM8916_SUBTYPE },
  98. - { .compatible = "qcom,pm8941", .data = (void *)PM8941_SUBTYPE },
  99. - { .compatible = "qcom,pm8950", .data = (void *)PM8950_SUBTYPE },
  100. - { .compatible = "qcom,pm8994", .data = (void *)PM8994_SUBTYPE },
  101. - { .compatible = "qcom,pm8998", .data = (void *)PM8998_SUBTYPE },
  102. - { .compatible = "qcom,pma8084", .data = (void *)PMA8084_SUBTYPE },
  103. - { .compatible = "qcom,pmd9635", .data = (void *)PMD9635_SUBTYPE },
  104. - { .compatible = "qcom,pmi8950", .data = (void *)PMI8950_SUBTYPE },
  105. - { .compatible = "qcom,pmi8962", .data = (void *)PMI8962_SUBTYPE },
  106. - { .compatible = "qcom,pmi8994", .data = (void *)PMI8994_SUBTYPE },
  107. - { .compatible = "qcom,pmi8998", .data = (void *)PMI8998_SUBTYPE },
  108. - { .compatible = "qcom,pmk8002", .data = (void *)PMK8002_SUBTYPE },
  109. - { .compatible = "qcom,smb2351", .data = (void *)SMB2351_SUBTYPE },
  110. - { .compatible = "qcom,spmi-pmic", .data = (void *)COMMON_SUBTYPE },
  111. + { .compatible = "qcom,pm660", .data = N_USIDS(2) },
  112. + { .compatible = "qcom,pm660l", .data = N_USIDS(2) },
  113. + { .compatible = "qcom,pm8004", .data = N_USIDS(2) },
  114. + { .compatible = "qcom,pm8005", .data = N_USIDS(2) },
  115. + { .compatible = "qcom,pm8019", .data = N_USIDS(2) },
  116. + { .compatible = "qcom,pm8028", .data = N_USIDS(2) },
  117. + { .compatible = "qcom,pm8110", .data = N_USIDS(2) },
  118. + { .compatible = "qcom,pm8150", .data = N_USIDS(2) },
  119. + { .compatible = "qcom,pm8150b", .data = N_USIDS(2) },
  120. + { .compatible = "qcom,pm8150c", .data = N_USIDS(2) },
  121. + { .compatible = "qcom,pm8150l", .data = N_USIDS(2) },
  122. + { .compatible = "qcom,pm8226", .data = N_USIDS(2) },
  123. + { .compatible = "qcom,pm8841", .data = N_USIDS(2) },
  124. + { .compatible = "qcom,pm8901", .data = N_USIDS(2) },
  125. + { .compatible = "qcom,pm8909", .data = N_USIDS(2) },
  126. + { .compatible = "qcom,pm8916", .data = N_USIDS(2) },
  127. + { .compatible = "qcom,pm8941", .data = N_USIDS(2) },
  128. + { .compatible = "qcom,pm8950", .data = N_USIDS(2) },
  129. + { .compatible = "qcom,pm8994", .data = N_USIDS(2) },
  130. + { .compatible = "qcom,pm8998", .data = N_USIDS(2) },
  131. + { .compatible = "qcom,pma8084", .data = N_USIDS(2) },
  132. + { .compatible = "qcom,pmd9635", .data = N_USIDS(2) },
  133. + { .compatible = "qcom,pmi8950", .data = N_USIDS(2) },
  134. + { .compatible = "qcom,pmi8962", .data = N_USIDS(2) },
  135. + { .compatible = "qcom,pmi8994", .data = N_USIDS(2) },
  136. + { .compatible = "qcom,pmi8998", .data = N_USIDS(2) },
  137. + { .compatible = "qcom,pmk8002", .data = N_USIDS(2) },
  138. + { .compatible = "qcom,smb2351", .data = N_USIDS(2) },
  139. + { .compatible = "qcom,spmi-pmic", .data = N_USIDS(1) },
  140. { }
  141. };
  142. -static void pmic_spmi_show_revid(struct regmap *map, struct device *dev)
  143. +/*
  144. + * A PMIC can be represented by multiple SPMI devices, but
  145. + * only the base PMIC device will contain a reference to
  146. + * the revision information.
  147. + *
  148. + * This function takes a pointer to a pmic device and
  149. + * returns a pointer to the base PMIC device.
  150. + *
  151. + * This only supports PMICs with 1 or 2 USIDs.
  152. + */
  153. +static struct spmi_device *qcom_pmic_get_base_usid(struct device *dev)
  154. {
  155. - unsigned int rev2, minor, major, type, subtype;
  156. - const char *name = "unknown";
  157. - int ret, i;
  158. + struct spmi_device *sdev;
  159. + struct qcom_spmi_dev *ctx;
  160. + struct device_node *spmi_bus;
  161. + struct device_node *other_usid = NULL;
  162. + int function_parent_usid, ret;
  163. + u32 pmic_addr;
  164. - ret = regmap_read(map, PMIC_TYPE, &type);
  165. - if (ret < 0)
  166. - return;
  167. + sdev = to_spmi_device(dev);
  168. + ctx = dev_get_drvdata(&sdev->dev);
  169. - if (type != PMIC_TYPE_VALUE)
  170. - return;
  171. + /*
  172. + * Quick return if the function device is already in the base
  173. + * USID. This will always be hit for PMICs with only 1 USID.
  174. + */
  175. + if (sdev->usid % ctx->num_usids == 0)
  176. + return sdev;
  177. - ret = regmap_read(map, PMIC_SUBTYPE, &subtype);
  178. + function_parent_usid = sdev->usid;
  179. +
  180. + /*
  181. + * Walk through the list of PMICs until we find the sibling USID.
  182. + * The goal is to find the first USID which is less than the
  183. + * number of USIDs in the PMIC array, e.g. for a PMIC with 2 USIDs
  184. + * where the function device is under USID 3, we want to find the
  185. + * device for USID 2.
  186. + */
  187. + spmi_bus = of_get_parent(sdev->dev.of_node);
  188. + do {
  189. + other_usid = of_get_next_child(spmi_bus, other_usid);
  190. +
  191. + ret = of_property_read_u32_index(other_usid, "reg", 0, &pmic_addr);
  192. + if (ret)
  193. + return ERR_PTR(ret);
  194. +
  195. + sdev = spmi_device_from_of(other_usid);
  196. + if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) {
  197. + if (!sdev)
  198. + /*
  199. + * If the base USID for this PMIC hasn't probed yet
  200. + * but the secondary USID has, then we need to defer
  201. + * the function driver so that it will attempt to
  202. + * probe again when the base USID is ready.
  203. + */
  204. + return ERR_PTR(-EPROBE_DEFER);
  205. + return sdev;
  206. + }
  207. + } while (other_usid->sibling);
  208. +
  209. + return ERR_PTR(-ENODATA);
  210. +}
  211. +
  212. +static int pmic_spmi_load_revid(struct regmap *map, struct device *dev,
  213. + struct qcom_spmi_pmic *pmic)
  214. +{
  215. + int ret;
  216. +
  217. + ret = regmap_read(map, PMIC_TYPE, &pmic->type);
  218. if (ret < 0)
  219. - return;
  220. + return ret;
  221. - for (i = 0; i < ARRAY_SIZE(pmic_spmi_id_table); i++) {
  222. - if (subtype == (unsigned long)pmic_spmi_id_table[i].data)
  223. - break;
  224. - }
  225. + if (pmic->type != PMIC_TYPE_VALUE)
  226. + return ret;
  227. - if (i != ARRAY_SIZE(pmic_spmi_id_table))
  228. - name = pmic_spmi_id_table[i].compatible;
  229. + ret = regmap_read(map, PMIC_SUBTYPE, &pmic->subtype);
  230. + if (ret < 0)
  231. + return ret;
  232. - ret = regmap_read(map, PMIC_REV2, &rev2);
  233. + pmic->name = of_match_device(pmic_spmi_id_table, dev)->compatible;
  234. +
  235. + ret = regmap_read(map, PMIC_REV2, &pmic->rev2);
  236. if (ret < 0)
  237. - return;
  238. + return ret;
  239. - ret = regmap_read(map, PMIC_REV3, &minor);
  240. + ret = regmap_read(map, PMIC_REV3, &pmic->minor);
  241. if (ret < 0)
  242. - return;
  243. + return ret;
  244. - ret = regmap_read(map, PMIC_REV4, &major);
  245. + ret = regmap_read(map, PMIC_REV4, &pmic->major);
  246. if (ret < 0)
  247. - return;
  248. + return ret;
  249. /*
  250. * In early versions of PM8941 and PM8226, the major revision number
  251. @@ -124,15 +163,49 @@ static void pmic_spmi_show_revid(struct
  252. * Increment the major revision number here if the chip is an early
  253. * version of PM8941 or PM8226.
  254. */
  255. - if ((subtype == PM8941_SUBTYPE || subtype == PM8226_SUBTYPE) &&
  256. - major < 0x02)
  257. - major++;
  258. + if ((pmic->subtype == PM8941_SUBTYPE || pmic->subtype == PM8226_SUBTYPE) &&
  259. + pmic->major < PMIC_REV4_V2)
  260. + pmic->major++;
  261. +
  262. + if (pmic->subtype == PM8110_SUBTYPE)
  263. + pmic->minor = pmic->rev2;
  264. +
  265. + dev_dbg(dev, "%x: %s v%d.%d\n",
  266. + pmic->subtype, pmic->name, pmic->major, pmic->minor);
  267. +
  268. + return 0;
  269. +}
  270. +
  271. +/**
  272. + * qcom_pmic_get() - Get a pointer to the base PMIC device
  273. + *
  274. + * This function takes a struct device for a driver which is a child of a PMIC.
  275. + * And locates the PMIC revision information for it.
  276. + *
  277. + * @dev: the pmic function device
  278. + * @return: the struct qcom_spmi_pmic* pointer associated with the function device
  279. + */
  280. +const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev)
  281. +{
  282. + struct spmi_device *sdev;
  283. + struct qcom_spmi_dev *spmi;
  284. +
  285. + /*
  286. + * Make sure the device is actually a child of a PMIC
  287. + */
  288. + if (!of_match_device(pmic_spmi_id_table, dev->parent))
  289. + return ERR_PTR(-EINVAL);
  290. +
  291. + sdev = qcom_pmic_get_base_usid(dev->parent);
  292. - if (subtype == PM8110_SUBTYPE)
  293. - minor = rev2;
  294. + if (IS_ERR(sdev))
  295. + return ERR_CAST(sdev);
  296. - dev_dbg(dev, "%x: %s v%d.%d\n", subtype, name, major, minor);
  297. + spmi = dev_get_drvdata(&sdev->dev);
  298. +
  299. + return &spmi->pmic;
  300. }
  301. +EXPORT_SYMBOL(qcom_pmic_get);
  302. static const struct regmap_config spmi_regmap_config = {
  303. .reg_bits = 16,
  304. @@ -144,14 +217,26 @@ static const struct regmap_config spmi_r
  305. static int pmic_spmi_probe(struct spmi_device *sdev)
  306. {
  307. struct regmap *regmap;
  308. + struct qcom_spmi_dev *ctx;
  309. + int ret;
  310. regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
  311. if (IS_ERR(regmap))
  312. return PTR_ERR(regmap);
  313. + ctx = devm_kzalloc(&sdev->dev, sizeof(*ctx), GFP_KERNEL);
  314. + if (!ctx)
  315. + return -ENOMEM;
  316. +
  317. + ctx->num_usids = (uintptr_t)of_device_get_match_data(&sdev->dev);
  318. +
  319. /* Only the first slave id for a PMIC contains this information */
  320. - if (sdev->usid % 2 == 0)
  321. - pmic_spmi_show_revid(regmap, &sdev->dev);
  322. + if (sdev->usid % ctx->num_usids == 0) {
  323. + ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic);
  324. + if (ret < 0)
  325. + return ret;
  326. + }
  327. + spmi_device_set_drvdata(sdev, ctx);
  328. return devm_of_platform_populate(&sdev->dev);
  329. }
  330. --- /dev/null
  331. +++ b/include/soc/qcom/qcom-spmi-pmic.h
  332. @@ -0,0 +1,60 @@
  333. +/* SPDX-License-Identifier: GPL-2.0-only */
  334. +/* Copyright (c) 2022 Linaro. All rights reserved.
  335. + * Author: Caleb Connolly <[email protected]>
  336. + */
  337. +
  338. +#ifndef __QCOM_SPMI_PMIC_H__
  339. +#define __QCOM_SPMI_PMIC_H__
  340. +
  341. +#include <linux/device.h>
  342. +
  343. +#define COMMON_SUBTYPE 0x00
  344. +#define PM8941_SUBTYPE 0x01
  345. +#define PM8841_SUBTYPE 0x02
  346. +#define PM8019_SUBTYPE 0x03
  347. +#define PM8226_SUBTYPE 0x04
  348. +#define PM8110_SUBTYPE 0x05
  349. +#define PMA8084_SUBTYPE 0x06
  350. +#define PMI8962_SUBTYPE 0x07
  351. +#define PMD9635_SUBTYPE 0x08
  352. +#define PM8994_SUBTYPE 0x09
  353. +#define PMI8994_SUBTYPE 0x0a
  354. +#define PM8916_SUBTYPE 0x0b
  355. +#define PM8004_SUBTYPE 0x0c
  356. +#define PM8909_SUBTYPE 0x0d
  357. +#define PM8028_SUBTYPE 0x0e
  358. +#define PM8901_SUBTYPE 0x0f
  359. +#define PM8950_SUBTYPE 0x10
  360. +#define PMI8950_SUBTYPE 0x11
  361. +#define PM8998_SUBTYPE 0x14
  362. +#define PMI8998_SUBTYPE 0x15
  363. +#define PM8005_SUBTYPE 0x18
  364. +#define PM660L_SUBTYPE 0x1A
  365. +#define PM660_SUBTYPE 0x1B
  366. +#define PM8150_SUBTYPE 0x1E
  367. +#define PM8150L_SUBTYPE 0x1f
  368. +#define PM8150B_SUBTYPE 0x20
  369. +#define PMK8002_SUBTYPE 0x21
  370. +#define PM8009_SUBTYPE 0x24
  371. +#define PM8150C_SUBTYPE 0x26
  372. +#define SMB2351_SUBTYPE 0x29
  373. +
  374. +#define PMI8998_FAB_ID_SMIC 0x11
  375. +#define PMI8998_FAB_ID_GF 0x30
  376. +
  377. +#define PM660_FAB_ID_GF 0x0
  378. +#define PM660_FAB_ID_TSMC 0x2
  379. +#define PM660_FAB_ID_MX 0x3
  380. +
  381. +struct qcom_spmi_pmic {
  382. + unsigned int type;
  383. + unsigned int subtype;
  384. + unsigned int major;
  385. + unsigned int minor;
  386. + unsigned int rev2;
  387. + const char *name;
  388. +};
  389. +
  390. +const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev);
  391. +
  392. +#endif /* __QCOM_SPMI_PMIC_H__ */