Linux-libre 5.4.49-gnu
[librecmc/linux-libre.git] / drivers / mfd / qcom-spmi-pmic.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014, The Linux Foundation. All rights reserved.
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/spmi.h>
9 #include <linux/regmap.h>
10 #include <linux/of_platform.h>
11
12 #define PMIC_REV2               0x101
13 #define PMIC_REV3               0x102
14 #define PMIC_REV4               0x103
15 #define PMIC_TYPE               0x104
16 #define PMIC_SUBTYPE            0x105
17
18 #define PMIC_TYPE_VALUE         0x51
19
20 #define COMMON_SUBTYPE          0x00
21 #define PM8941_SUBTYPE          0x01
22 #define PM8841_SUBTYPE          0x02
23 #define PM8019_SUBTYPE          0x03
24 #define PM8226_SUBTYPE          0x04
25 #define PM8110_SUBTYPE          0x05
26 #define PMA8084_SUBTYPE         0x06
27 #define PMI8962_SUBTYPE         0x07
28 #define PMD9635_SUBTYPE         0x08
29 #define PM8994_SUBTYPE          0x09
30 #define PMI8994_SUBTYPE         0x0a
31 #define PM8916_SUBTYPE          0x0b
32 #define PM8004_SUBTYPE          0x0c
33 #define PM8909_SUBTYPE          0x0d
34 #define PM8998_SUBTYPE          0x14
35 #define PMI8998_SUBTYPE         0x15
36 #define PM8005_SUBTYPE          0x18
37
38 static const struct of_device_id pmic_spmi_id_table[] = {
39         { .compatible = "qcom,spmi-pmic", .data = (void *)COMMON_SUBTYPE },
40         { .compatible = "qcom,pm8941",    .data = (void *)PM8941_SUBTYPE },
41         { .compatible = "qcom,pm8841",    .data = (void *)PM8841_SUBTYPE },
42         { .compatible = "qcom,pm8019",    .data = (void *)PM8019_SUBTYPE },
43         { .compatible = "qcom,pm8226",    .data = (void *)PM8226_SUBTYPE },
44         { .compatible = "qcom,pm8110",    .data = (void *)PM8110_SUBTYPE },
45         { .compatible = "qcom,pma8084",   .data = (void *)PMA8084_SUBTYPE },
46         { .compatible = "qcom,pmi8962",   .data = (void *)PMI8962_SUBTYPE },
47         { .compatible = "qcom,pmd9635",   .data = (void *)PMD9635_SUBTYPE },
48         { .compatible = "qcom,pm8994",    .data = (void *)PM8994_SUBTYPE },
49         { .compatible = "qcom,pmi8994",   .data = (void *)PMI8994_SUBTYPE },
50         { .compatible = "qcom,pm8916",    .data = (void *)PM8916_SUBTYPE },
51         { .compatible = "qcom,pm8004",    .data = (void *)PM8004_SUBTYPE },
52         { .compatible = "qcom,pm8909",    .data = (void *)PM8909_SUBTYPE },
53         { .compatible = "qcom,pm8998",    .data = (void *)PM8998_SUBTYPE },
54         { .compatible = "qcom,pmi8998",   .data = (void *)PMI8998_SUBTYPE },
55         { .compatible = "qcom,pm8005",    .data = (void *)PM8005_SUBTYPE },
56         { }
57 };
58
59 static void pmic_spmi_show_revid(struct regmap *map, struct device *dev)
60 {
61         unsigned int rev2, minor, major, type, subtype;
62         const char *name = "unknown";
63         int ret, i;
64
65         ret = regmap_read(map, PMIC_TYPE, &type);
66         if (ret < 0)
67                 return;
68
69         if (type != PMIC_TYPE_VALUE)
70                 return;
71
72         ret = regmap_read(map, PMIC_SUBTYPE, &subtype);
73         if (ret < 0)
74                 return;
75
76         for (i = 0; i < ARRAY_SIZE(pmic_spmi_id_table); i++) {
77                 if (subtype == (unsigned long)pmic_spmi_id_table[i].data)
78                         break;
79         }
80
81         if (i != ARRAY_SIZE(pmic_spmi_id_table))
82                 name = pmic_spmi_id_table[i].compatible;
83
84         ret = regmap_read(map, PMIC_REV2, &rev2);
85         if (ret < 0)
86                 return;
87
88         ret = regmap_read(map, PMIC_REV3, &minor);
89         if (ret < 0)
90                 return;
91
92         ret = regmap_read(map, PMIC_REV4, &major);
93         if (ret < 0)
94                 return;
95
96         /*
97          * In early versions of PM8941 and PM8226, the major revision number
98          * started incrementing from 0 (eg 0 = v1.0, 1 = v2.0).
99          * Increment the major revision number here if the chip is an early
100          * version of PM8941 or PM8226.
101          */
102         if ((subtype == PM8941_SUBTYPE || subtype == PM8226_SUBTYPE) &&
103             major < 0x02)
104                 major++;
105
106         if (subtype == PM8110_SUBTYPE)
107                 minor = rev2;
108
109         dev_dbg(dev, "%x: %s v%d.%d\n", subtype, name, major, minor);
110 }
111
112 static const struct regmap_config spmi_regmap_config = {
113         .reg_bits       = 16,
114         .val_bits       = 8,
115         .max_register   = 0xffff,
116         .fast_io        = true,
117 };
118
119 static int pmic_spmi_probe(struct spmi_device *sdev)
120 {
121         struct regmap *regmap;
122
123         regmap = devm_regmap_init_spmi_ext(sdev, &spmi_regmap_config);
124         if (IS_ERR(regmap))
125                 return PTR_ERR(regmap);
126
127         /* Only the first slave id for a PMIC contains this information */
128         if (sdev->usid % 2 == 0)
129                 pmic_spmi_show_revid(regmap, &sdev->dev);
130
131         return devm_of_platform_populate(&sdev->dev);
132 }
133
134 MODULE_DEVICE_TABLE(of, pmic_spmi_id_table);
135
136 static struct spmi_driver pmic_spmi_driver = {
137         .probe = pmic_spmi_probe,
138         .driver = {
139                 .name = "pmic-spmi",
140                 .of_match_table = pmic_spmi_id_table,
141         },
142 };
143 module_spmi_driver(pmic_spmi_driver);
144
145 MODULE_DESCRIPTION("Qualcomm SPMI PMIC driver");
146 MODULE_ALIAS("spmi:spmi-pmic");
147 MODULE_LICENSE("GPL v2");
148 MODULE_AUTHOR("Josh Cartwright <joshc@codeaurora.org>");
149 MODULE_AUTHOR("Stanimir Varbanov <svarbanov@mm-sol.com>");