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