hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/mfd/sprd-sc27xx-spi.c
....@@ -1,23 +1,18 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2017 Spreadtrum Communications Inc.
3
- *
4
- * This software is licensed under the terms of the GNU General Public
5
- * License version 2, as published by the Free Software Foundation, and
6
- * may be copied, distributed, and modified under those terms.
7
- *
8
- * This program is distributed in the hope that it will be useful,
9
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- * GNU General Public License for more details.
124 */
135
146 #include <linux/interrupt.h>
157 #include <linux/kernel.h>
168 #include <linux/module.h>
179 #include <linux/mfd/core.h>
10
+#include <linux/mfd/sc27xx-pmic.h>
1811 #include <linux/of_device.h>
12
+#include <linux/of_platform.h>
1913 #include <linux/regmap.h>
2014 #include <linux/spi/spi.h>
15
+#include <uapi/linux/usb/charger.h>
2116
2217 #define SPRD_PMIC_INT_MASK_STATUS 0x0
2318 #define SPRD_PMIC_INT_RAW_STATUS 0x4
....@@ -25,6 +20,16 @@
2520
2621 #define SPRD_SC2731_IRQ_BASE 0x140
2722 #define SPRD_SC2731_IRQ_NUMS 16
23
+#define SPRD_SC2731_CHG_DET 0xedc
24
+
25
+/* PMIC charger detection definition */
26
+#define SPRD_PMIC_CHG_DET_DELAY_US 200000
27
+#define SPRD_PMIC_CHG_DET_TIMEOUT 2000000
28
+#define SPRD_PMIC_CHG_DET_DONE BIT(11)
29
+#define SPRD_PMIC_SDP_TYPE BIT(7)
30
+#define SPRD_PMIC_DCP_TYPE BIT(6)
31
+#define SPRD_PMIC_CDP_TYPE BIT(5)
32
+#define SPRD_PMIC_CHG_TYPE_MASK GENMASK(7, 5)
2833
2934 struct sprd_pmic {
3035 struct regmap *regmap;
....@@ -32,12 +37,14 @@
3237 struct regmap_irq *irqs;
3338 struct regmap_irq_chip irq_chip;
3439 struct regmap_irq_chip_data *irq_data;
40
+ const struct sprd_pmic_data *pdata;
3541 int irq;
3642 };
3743
3844 struct sprd_pmic_data {
3945 u32 irq_base;
4046 u32 num_irqs;
47
+ u32 charger_det;
4148 };
4249
4350 /*
....@@ -48,74 +55,45 @@
4855 static const struct sprd_pmic_data sc2731_data = {
4956 .irq_base = SPRD_SC2731_IRQ_BASE,
5057 .num_irqs = SPRD_SC2731_IRQ_NUMS,
58
+ .charger_det = SPRD_SC2731_CHG_DET,
5159 };
5260
53
-static const struct mfd_cell sprd_pmic_devs[] = {
54
- {
55
- .name = "sc27xx-wdt",
56
- .of_compatible = "sprd,sc27xx-wdt",
57
- }, {
58
- .name = "sc27xx-rtc",
59
- .of_compatible = "sprd,sc27xx-rtc",
60
- }, {
61
- .name = "sc27xx-charger",
62
- .of_compatible = "sprd,sc27xx-charger",
63
- }, {
64
- .name = "sc27xx-chg-timer",
65
- .of_compatible = "sprd,sc27xx-chg-timer",
66
- }, {
67
- .name = "sc27xx-fast-chg",
68
- .of_compatible = "sprd,sc27xx-fast-chg",
69
- }, {
70
- .name = "sc27xx-chg-wdt",
71
- .of_compatible = "sprd,sc27xx-chg-wdt",
72
- }, {
73
- .name = "sc27xx-typec",
74
- .of_compatible = "sprd,sc27xx-typec",
75
- }, {
76
- .name = "sc27xx-flash",
77
- .of_compatible = "sprd,sc27xx-flash",
78
- }, {
79
- .name = "sc27xx-eic",
80
- .of_compatible = "sprd,sc27xx-eic",
81
- }, {
82
- .name = "sc27xx-efuse",
83
- .of_compatible = "sprd,sc27xx-efuse",
84
- }, {
85
- .name = "sc27xx-thermal",
86
- .of_compatible = "sprd,sc27xx-thermal",
87
- }, {
88
- .name = "sc27xx-adc",
89
- .of_compatible = "sprd,sc27xx-adc",
90
- }, {
91
- .name = "sc27xx-audio-codec",
92
- .of_compatible = "sprd,sc27xx-audio-codec",
93
- }, {
94
- .name = "sc27xx-regulator",
95
- .of_compatible = "sprd,sc27xx-regulator",
96
- }, {
97
- .name = "sc27xx-vibrator",
98
- .of_compatible = "sprd,sc27xx-vibrator",
99
- }, {
100
- .name = "sc27xx-keypad-led",
101
- .of_compatible = "sprd,sc27xx-keypad-led",
102
- }, {
103
- .name = "sc27xx-bltc",
104
- .of_compatible = "sprd,sc27xx-bltc",
105
- }, {
106
- .name = "sc27xx-fgu",
107
- .of_compatible = "sprd,sc27xx-fgu",
108
- }, {
109
- .name = "sc27xx-7sreset",
110
- .of_compatible = "sprd,sc27xx-7sreset",
111
- }, {
112
- .name = "sc27xx-poweroff",
113
- .of_compatible = "sprd,sc27xx-poweroff",
114
- }, {
115
- .name = "sc27xx-syscon",
116
- .of_compatible = "sprd,sc27xx-syscon",
117
- },
118
-};
61
+enum usb_charger_type sprd_pmic_detect_charger_type(struct device *dev)
62
+{
63
+ struct spi_device *spi = to_spi_device(dev);
64
+ struct sprd_pmic *ddata = spi_get_drvdata(spi);
65
+ const struct sprd_pmic_data *pdata = ddata->pdata;
66
+ enum usb_charger_type type;
67
+ u32 val;
68
+ int ret;
69
+
70
+ ret = regmap_read_poll_timeout(ddata->regmap, pdata->charger_det, val,
71
+ (val & SPRD_PMIC_CHG_DET_DONE),
72
+ SPRD_PMIC_CHG_DET_DELAY_US,
73
+ SPRD_PMIC_CHG_DET_TIMEOUT);
74
+ if (ret) {
75
+ dev_err(&spi->dev, "failed to detect charger type\n");
76
+ return UNKNOWN_TYPE;
77
+ }
78
+
79
+ switch (val & SPRD_PMIC_CHG_TYPE_MASK) {
80
+ case SPRD_PMIC_CDP_TYPE:
81
+ type = CDP_TYPE;
82
+ break;
83
+ case SPRD_PMIC_DCP_TYPE:
84
+ type = DCP_TYPE;
85
+ break;
86
+ case SPRD_PMIC_SDP_TYPE:
87
+ type = SDP_TYPE;
88
+ break;
89
+ default:
90
+ type = UNKNOWN_TYPE;
91
+ break;
92
+ }
93
+
94
+ return type;
95
+}
96
+EXPORT_SYMBOL_GPL(sprd_pmic_detect_charger_type);
11997
12098 static int sprd_pmic_spi_write(void *context, const void *data, size_t count)
12199 {
....@@ -189,6 +167,7 @@
189167 spi_set_drvdata(spi, ddata);
190168 ddata->dev = &spi->dev;
191169 ddata->irq = spi->irq;
170
+ ddata->pdata = pdata;
192171
193172 ddata->irq_chip.name = dev_name(&spi->dev);
194173 ddata->irq_chip.status_base =
....@@ -206,10 +185,8 @@
206185 return -ENOMEM;
207186
208187 ddata->irq_chip.irqs = ddata->irqs;
209
- for (i = 0; i < pdata->num_irqs; i++) {
210
- ddata->irqs[i].reg_offset = i / pdata->num_irqs;
211
- ddata->irqs[i].mask = BIT(i % pdata->num_irqs);
212
- }
188
+ for (i = 0; i < pdata->num_irqs; i++)
189
+ ddata->irqs[i].mask = BIT(i);
213190
214191 ret = devm_regmap_add_irq_chip(&spi->dev, ddata->regmap, ddata->irq,
215192 IRQF_ONESHOT, 0,
....@@ -219,12 +196,9 @@
219196 return ret;
220197 }
221198
222
- ret = devm_mfd_add_devices(&spi->dev, PLATFORM_DEVID_AUTO,
223
- sprd_pmic_devs, ARRAY_SIZE(sprd_pmic_devs),
224
- NULL, 0,
225
- regmap_irq_get_domain(ddata->irq_data));
199
+ ret = devm_of_platform_populate(&spi->dev);
226200 if (ret) {
227
- dev_err(&spi->dev, "Failed to register device %d\n", ret);
201
+ dev_err(&spi->dev, "Failed to populate sub-devices %d\n", ret);
228202 return ret;
229203 }
230204
....@@ -265,7 +239,6 @@
265239 static struct spi_driver sprd_pmic_driver = {
266240 .driver = {
267241 .name = "sc27xx-pmic",
268
- .bus = &spi_bus_type,
269242 .of_match_table = sprd_pmic_match,
270243 .pm = &sprd_pmic_pm_ops,
271244 },