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