| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * 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. |
|---|
| 12 | 4 | */ |
|---|
| 13 | 5 | |
|---|
| 14 | 6 | #include <linux/interrupt.h> |
|---|
| 15 | 7 | #include <linux/kernel.h> |
|---|
| 16 | 8 | #include <linux/module.h> |
|---|
| 17 | 9 | #include <linux/mfd/core.h> |
|---|
| 10 | +#include <linux/mfd/sc27xx-pmic.h> |
|---|
| 18 | 11 | #include <linux/of_device.h> |
|---|
| 12 | +#include <linux/of_platform.h> |
|---|
| 19 | 13 | #include <linux/regmap.h> |
|---|
| 20 | 14 | #include <linux/spi/spi.h> |
|---|
| 15 | +#include <uapi/linux/usb/charger.h> |
|---|
| 21 | 16 | |
|---|
| 22 | 17 | #define SPRD_PMIC_INT_MASK_STATUS 0x0 |
|---|
| 23 | 18 | #define SPRD_PMIC_INT_RAW_STATUS 0x4 |
|---|
| .. | .. |
|---|
| 25 | 20 | |
|---|
| 26 | 21 | #define SPRD_SC2731_IRQ_BASE 0x140 |
|---|
| 27 | 22 | #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) |
|---|
| 28 | 33 | |
|---|
| 29 | 34 | struct sprd_pmic { |
|---|
| 30 | 35 | struct regmap *regmap; |
|---|
| .. | .. |
|---|
| 32 | 37 | struct regmap_irq *irqs; |
|---|
| 33 | 38 | struct regmap_irq_chip irq_chip; |
|---|
| 34 | 39 | struct regmap_irq_chip_data *irq_data; |
|---|
| 40 | + const struct sprd_pmic_data *pdata; |
|---|
| 35 | 41 | int irq; |
|---|
| 36 | 42 | }; |
|---|
| 37 | 43 | |
|---|
| 38 | 44 | struct sprd_pmic_data { |
|---|
| 39 | 45 | u32 irq_base; |
|---|
| 40 | 46 | u32 num_irqs; |
|---|
| 47 | + u32 charger_det; |
|---|
| 41 | 48 | }; |
|---|
| 42 | 49 | |
|---|
| 43 | 50 | /* |
|---|
| .. | .. |
|---|
| 48 | 55 | static const struct sprd_pmic_data sc2731_data = { |
|---|
| 49 | 56 | .irq_base = SPRD_SC2731_IRQ_BASE, |
|---|
| 50 | 57 | .num_irqs = SPRD_SC2731_IRQ_NUMS, |
|---|
| 58 | + .charger_det = SPRD_SC2731_CHG_DET, |
|---|
| 51 | 59 | }; |
|---|
| 52 | 60 | |
|---|
| 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); |
|---|
| 119 | 97 | |
|---|
| 120 | 98 | static int sprd_pmic_spi_write(void *context, const void *data, size_t count) |
|---|
| 121 | 99 | { |
|---|
| .. | .. |
|---|
| 189 | 167 | spi_set_drvdata(spi, ddata); |
|---|
| 190 | 168 | ddata->dev = &spi->dev; |
|---|
| 191 | 169 | ddata->irq = spi->irq; |
|---|
| 170 | + ddata->pdata = pdata; |
|---|
| 192 | 171 | |
|---|
| 193 | 172 | ddata->irq_chip.name = dev_name(&spi->dev); |
|---|
| 194 | 173 | ddata->irq_chip.status_base = |
|---|
| .. | .. |
|---|
| 206 | 185 | return -ENOMEM; |
|---|
| 207 | 186 | |
|---|
| 208 | 187 | 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); |
|---|
| 213 | 190 | |
|---|
| 214 | 191 | ret = devm_regmap_add_irq_chip(&spi->dev, ddata->regmap, ddata->irq, |
|---|
| 215 | 192 | IRQF_ONESHOT, 0, |
|---|
| .. | .. |
|---|
| 219 | 196 | return ret; |
|---|
| 220 | 197 | } |
|---|
| 221 | 198 | |
|---|
| 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); |
|---|
| 226 | 200 | 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); |
|---|
| 228 | 202 | return ret; |
|---|
| 229 | 203 | } |
|---|
| 230 | 204 | |
|---|
| .. | .. |
|---|
| 265 | 239 | static struct spi_driver sprd_pmic_driver = { |
|---|
| 266 | 240 | .driver = { |
|---|
| 267 | 241 | .name = "sc27xx-pmic", |
|---|
| 268 | | - .bus = &spi_bus_type, |
|---|
| 269 | 242 | .of_match_table = sprd_pmic_match, |
|---|
| 270 | 243 | .pm = &sprd_pmic_pm_ops, |
|---|
| 271 | 244 | }, |
|---|