1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/device.h> 7 #include <linux/errno.h> 8 #include <linux/gfp.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/of.h> 12 #include <linux/of_device.h> 13 #include <linux/of_platform.h> 14 #include <linux/spmi.h> 15 #include <linux/types.h> 16 #include <linux/regmap.h> 17 #include <soc/qcom/qcom-spmi-pmic.h> 18 19 #define PMIC_REV2 0x101 20 #define PMIC_REV3 0x102 21 #define PMIC_REV4 0x103 22 #define PMIC_TYPE 0x104 23 #define PMIC_SUBTYPE 0x105 24 #define PMIC_FAB_ID 0x1f2 25 26 #define PMIC_TYPE_VALUE 0x51 27 28 #define PMIC_REV4_V2 0x02 29 30 struct qcom_spmi_dev { 31 int num_usids; 32 struct qcom_spmi_pmic pmic; 33 }; 34 35 static DEFINE_MUTEX(pmic_spmi_revid_lock); 36 37 #define N_USIDS(n) ((void *)n) 38 39 static const struct of_device_id pmic_spmi_id_table[] = { 40 { .compatible = "qcom,pm660", .data = N_USIDS(2) }, 41 { .compatible = "qcom,pm660l", .data = N_USIDS(2) }, 42 { .compatible = "qcom,pm8004", .data = N_USIDS(2) }, 43 { .compatible = "qcom,pm8005", .data = N_USIDS(2) }, 44 { .compatible = "qcom,pm8019", .data = N_USIDS(2) }, 45 { .compatible = "qcom,pm8028", .data = N_USIDS(2) }, 46 { .compatible = "qcom,pm8110", .data = N_USIDS(2) }, 47 { .compatible = "qcom,pm8150", .data = N_USIDS(2) }, 48 { .compatible = "qcom,pm8150b", .data = N_USIDS(2) }, 49 { .compatible = "qcom,pm8150c", .data = N_USIDS(2) }, 50 { .compatible = "qcom,pm8150l", .data = N_USIDS(2) }, 51 { .compatible = "qcom,pm8226", .data = N_USIDS(2) }, 52 { .compatible = "qcom,pm8841", .data = N_USIDS(2) }, 53 { .compatible = "qcom,pm8901", .data = N_USIDS(2) }, 54 { .compatible = "qcom,pm8909", .data = N_USIDS(2) }, 55 { .compatible = "qcom,pm8916", .data = N_USIDS(2) }, 56 { .compatible = "qcom,pm8941", .data = N_USIDS(2) }, 57 { .compatible = "qcom,pm8950", .data = N_USIDS(2) }, 58 { .compatible = "qcom,pm8994", .data = N_USIDS(2) }, 59 { .compatible = "qcom,pm8998", .data = N_USIDS(2) }, 60 { .compatible = "qcom,pma8084", .data = N_USIDS(2) }, 61 { .compatible = "qcom,pmd9635", .data = N_USIDS(2) }, 62 { .compatible = "qcom,pmi8950", .data = N_USIDS(2) }, 63 { .compatible = "qcom,pmi8962", .data = N_USIDS(2) }, 64 { .compatible = "qcom,pmi8994", .data = N_USIDS(2) }, 65 { .compatible = "qcom,pmi8998", .data = N_USIDS(2) }, 66 { .compatible = "qcom,pmk8002", .data = N_USIDS(2) }, 67 { .compatible = "qcom,pmp8074", .data = N_USIDS(2) }, 68 { .compatible = "qcom,smb2351", .data = N_USIDS(2) }, 69 { .compatible = "qcom,spmi-pmic", .data = N_USIDS(1) }, 70 { } 71 }; 72 73 /* 74 * A PMIC can be represented by multiple SPMI devices, but 75 * only the base PMIC device will contain a reference to 76 * the revision information. 77 * 78 * This function takes a pointer to a pmic device and 79 * returns a pointer to the base PMIC device. 80 * 81 * This only supports PMICs with 1 or 2 USIDs. 82 */ 83 static struct spmi_device *qcom_pmic_get_base_usid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx) 84 { 85 struct device_node *spmi_bus; 86 struct device_node *child; 87 int function_parent_usid, ret; 88 u32 pmic_addr; 89 90 /* 91 * Quick return if the function device is already in the base 92 * USID. This will always be hit for PMICs with only 1 USID. 93 */ 94 if (sdev->usid % ctx->num_usids == 0) { 95 get_device(&sdev->dev); 96 return sdev; 97 } 98 99 function_parent_usid = sdev->usid; 100 101 /* 102 * Walk through the list of PMICs until we find the sibling USID. 103 * The goal is to find the first USID which is less than the 104 * number of USIDs in the PMIC array, e.g. for a PMIC with 2 USIDs 105 * where the function device is under USID 3, we want to find the 106 * device for USID 2. 107 */ 108 spmi_bus = of_get_parent(sdev->dev.of_node); 109 sdev = ERR_PTR(-ENODATA); 110 for_each_child_of_node(spmi_bus, child) { 111 ret = of_property_read_u32_index(child, "reg", 0, &pmic_addr); 112 if (ret) { 113 of_node_put(child); 114 sdev = ERR_PTR(ret); 115 break; 116 } 117 118 if (pmic_addr == function_parent_usid - (ctx->num_usids - 1)) { 119 sdev = spmi_find_device_by_of_node(child); 120 if (!sdev) { 121 /* 122 * If the base USID for this PMIC hasn't been 123 * registered yet then we need to defer. 124 */ 125 sdev = ERR_PTR(-EPROBE_DEFER); 126 } 127 of_node_put(child); 128 break; 129 } 130 } 131 132 of_node_put(spmi_bus); 133 134 return sdev; 135 } 136 137 static int pmic_spmi_get_base_revid(struct spmi_device *sdev, struct qcom_spmi_dev *ctx) 138 { 139 struct qcom_spmi_dev *base_ctx; 140 struct spmi_device *base; 141 int ret = 0; 142 143 base = qcom_pmic_get_base_usid(sdev, ctx); 144 if (IS_ERR(base)) 145 return PTR_ERR(base); 146 147 /* 148 * Copy revid info from base device if it has probed and is still 149 * bound to its driver. 150 */ 151 mutex_lock(&pmic_spmi_revid_lock); 152 base_ctx = spmi_device_get_drvdata(base); 153 if (!base_ctx) { 154 ret = -EPROBE_DEFER; 155 goto out_unlock; 156 } 157 memcpy(&ctx->pmic, &base_ctx->pmic, sizeof(ctx->pmic)); 158 out_unlock: 159 mutex_unlock(&pmic_spmi_revid_lock); 160 161 put_device(&base->dev); 162 163 return ret; 164 } 165 166 static int pmic_spmi_load_revid(struct regmap *map, struct device *dev, 167 struct qcom_spmi_pmic *pmic) 168 { 169 int ret; 170 171 ret = regmap_read(map, PMIC_TYPE, &pmic->type); 172 if (ret < 0) 173 return ret; 174 175 if (pmic->type != PMIC_TYPE_VALUE) 176 return ret; 177 178 ret = regmap_read(map, PMIC_SUBTYPE, &pmic->subtype); 179 if (ret < 0) 180 return ret; 181 182 pmic->name = of_match_device(pmic_spmi_id_table, dev)->compatible; 183 184 ret = regmap_read(map, PMIC_REV2, &pmic->rev2); 185 if (ret < 0) 186 return ret; 187 188 ret = regmap_read(map, PMIC_REV3, &pmic->minor); 189 if (ret < 0) 190 return ret; 191 192 ret = regmap_read(map, PMIC_REV4, &pmic->major); 193 if (ret < 0) 194 return ret; 195 196 if (pmic->subtype == PMI8998_SUBTYPE || pmic->subtype == PM660_SUBTYPE) { 197 ret = regmap_read(map, PMIC_FAB_ID, &pmic->fab_id); 198 if (ret < 0) 199 return ret; 200 } 201 202 /* 203 * In early versions of PM8941 and PM8226, the major revision number 204 * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0). 205 * Increment the major revision number here if the chip is an early 206 * version of PM8941 or PM8226. 207 */ 208 if ((pmic->subtype == PM8941_SUBTYPE || pmic->subtype == PM8226_SUBTYPE) && 209 pmic->major < PMIC_REV4_V2) 210 pmic->major++; 211 212 if (pmic->subtype == PM8110_SUBTYPE) 213 pmic->minor = pmic->rev2; 214 215 dev_dbg(dev, "%x: %s v%d.%d\n", 216 pmic->subtype, pmic->name, pmic->major, pmic->minor); 217 218 return 0; 219 } 220 221 /** 222 * qcom_pmic_get() - Get a pointer to the base PMIC device 223 * 224 * This function takes a struct device for a driver which is a child of a PMIC. 225 * And locates the PMIC revision information for it. 226 * 227 * @dev: the pmic function device 228 * @return: the struct qcom_spmi_pmic* pointer associated with the function device 229 */ 230 const struct qcom_spmi_pmic *qcom_pmic_get(struct device *dev) 231 { 232 struct spmi_device *sdev; 233 struct qcom_spmi_dev *spmi; 234 235 /* 236 * Make sure the device is actually a child of a PMIC 237 */ 238 if (!of_match_device(pmic_spmi_id_table, dev->parent)) 239 return ERR_PTR(-EINVAL); 240 241 sdev = to_spmi_device(dev->parent); 242 spmi = dev_get_drvdata(&sdev->dev); 243 244 return &spmi->pmic; 245 } 246 EXPORT_SYMBOL_GPL(qcom_pmic_get); 247 248 static const struct regmap_config spmi_regmap_config = { 249 .reg_bits = 16, 250 .val_bits = 8, 251 .max_register = 0xffff, 252 .fast_io = true, 253 }; 254 255 static int pmic_spmi_probe(struct spmi_device *sdev) 256 { 257 struct regmap *regmap; 258 struct qcom_spmi_dev *ctx; 259 int ret; 260 261 regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config); 262 if (IS_ERR(regmap)) 263 return PTR_ERR(regmap); 264 265 ctx = devm_kzalloc(&sdev->dev, sizeof(*ctx), GFP_KERNEL); 266 if (!ctx) 267 return -ENOMEM; 268 269 ctx->num_usids = (uintptr_t)device_get_match_data(&sdev->dev); 270 271 /* Only the first slave id for a PMIC contains this information */ 272 if (sdev->usid % ctx->num_usids == 0) { 273 ret = pmic_spmi_load_revid(regmap, &sdev->dev, &ctx->pmic); 274 if (ret < 0) 275 return ret; 276 } else { 277 ret = pmic_spmi_get_base_revid(sdev, ctx); 278 if (ret) 279 return ret; 280 } 281 282 mutex_lock(&pmic_spmi_revid_lock); 283 spmi_device_set_drvdata(sdev, ctx); 284 mutex_unlock(&pmic_spmi_revid_lock); 285 286 return devm_of_platform_populate(&sdev->dev); 287 } 288 289 static void pmic_spmi_remove(struct spmi_device *sdev) 290 { 291 mutex_lock(&pmic_spmi_revid_lock); 292 spmi_device_set_drvdata(sdev, NULL); 293 mutex_unlock(&pmic_spmi_revid_lock); 294 } 295 296 MODULE_DEVICE_TABLE(of, pmic_spmi_id_table); 297 298 static struct spmi_driver pmic_spmi_driver = { 299 .probe = pmic_spmi_probe, 300 .remove = pmic_spmi_remove, 301 .driver = { 302 .name = "pmic-spmi", 303 .of_match_table = pmic_spmi_id_table, 304 }, 305 }; 306 module_spmi_driver(pmic_spmi_driver); 307 308 MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver"); 309 MODULE_ALIAS("spmi:spmi-pmic"); 310 MODULE_LICENSE("GPL v2"); 311 MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>"); 312 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>"); 313