Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mfd: sc27xx: Add USB charger type detection support

The Spreadtrum SC27XX series PMICs supply the USB charger type detection
function, and related registers are located on the PMIC global registers
region, thus we implement and export this function in the MFD driver for
users to get the USB charger type.

Signed-off-by: Baolin Wang <baolin.wang7@gmail.com>
Signed-off-by: Lee Jones <lee.jones@linaro.org>

authored by

Baolin Wang and committed by
Lee Jones
2a7e7274 ba583693

+59
+52
drivers/mfd/sprd-sc27xx-spi.c
··· 10 10 #include <linux/of_device.h> 11 11 #include <linux/regmap.h> 12 12 #include <linux/spi/spi.h> 13 + #include <uapi/linux/usb/charger.h> 13 14 14 15 #define SPRD_PMIC_INT_MASK_STATUS 0x0 15 16 #define SPRD_PMIC_INT_RAW_STATUS 0x4 ··· 18 17 19 18 #define SPRD_SC2731_IRQ_BASE 0x140 20 19 #define SPRD_SC2731_IRQ_NUMS 16 20 + #define SPRD_SC2731_CHG_DET 0xedc 21 + 22 + /* PMIC charger detection definition */ 23 + #define SPRD_PMIC_CHG_DET_DELAY_US 200000 24 + #define SPRD_PMIC_CHG_DET_TIMEOUT 2000000 25 + #define SPRD_PMIC_CHG_DET_DONE BIT(11) 26 + #define SPRD_PMIC_SDP_TYPE BIT(7) 27 + #define SPRD_PMIC_DCP_TYPE BIT(6) 28 + #define SPRD_PMIC_CDP_TYPE BIT(5) 29 + #define SPRD_PMIC_CHG_TYPE_MASK GENMASK(7, 5) 21 30 22 31 struct sprd_pmic { 23 32 struct regmap *regmap; ··· 35 24 struct regmap_irq *irqs; 36 25 struct regmap_irq_chip irq_chip; 37 26 struct regmap_irq_chip_data *irq_data; 27 + const struct sprd_pmic_data *pdata; 38 28 int irq; 39 29 }; 40 30 41 31 struct sprd_pmic_data { 42 32 u32 irq_base; 43 33 u32 num_irqs; 34 + u32 charger_det; 44 35 }; 45 36 46 37 /* ··· 53 40 static const struct sprd_pmic_data sc2731_data = { 54 41 .irq_base = SPRD_SC2731_IRQ_BASE, 55 42 .num_irqs = SPRD_SC2731_IRQ_NUMS, 43 + .charger_det = SPRD_SC2731_CHG_DET, 56 44 }; 45 + 46 + enum usb_charger_type sprd_pmic_detect_charger_type(struct device *dev) 47 + { 48 + struct spi_device *spi = to_spi_device(dev); 49 + struct sprd_pmic *ddata = spi_get_drvdata(spi); 50 + const struct sprd_pmic_data *pdata = ddata->pdata; 51 + enum usb_charger_type type; 52 + u32 val; 53 + int ret; 54 + 55 + ret = regmap_read_poll_timeout(ddata->regmap, pdata->charger_det, val, 56 + (val & SPRD_PMIC_CHG_DET_DONE), 57 + SPRD_PMIC_CHG_DET_DELAY_US, 58 + SPRD_PMIC_CHG_DET_TIMEOUT); 59 + if (ret) { 60 + dev_err(&spi->dev, "failed to detect charger type\n"); 61 + return UNKNOWN_TYPE; 62 + } 63 + 64 + switch (val & SPRD_PMIC_CHG_TYPE_MASK) { 65 + case SPRD_PMIC_CDP_TYPE: 66 + type = CDP_TYPE; 67 + break; 68 + case SPRD_PMIC_DCP_TYPE: 69 + type = DCP_TYPE; 70 + break; 71 + case SPRD_PMIC_SDP_TYPE: 72 + type = SDP_TYPE; 73 + break; 74 + default: 75 + type = UNKNOWN_TYPE; 76 + break; 77 + } 78 + 79 + return type; 80 + } 81 + EXPORT_SYMBOL_GPL(sprd_pmic_detect_charger_type); 57 82 58 83 static const struct mfd_cell sprd_pmic_devs[] = { 59 84 { ··· 232 181 spi_set_drvdata(spi, ddata); 233 182 ddata->dev = &spi->dev; 234 183 ddata->irq = spi->irq; 184 + ddata->pdata = pdata; 235 185 236 186 ddata->irq_chip.name = dev_name(&spi->dev); 237 187 ddata->irq_chip.status_base =
+7
include/linux/mfd/sc27xx-pmic.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #ifndef __LINUX_MFD_SC27XX_PMIC_H 3 + #define __LINUX_MFD_SC27XX_PMIC_H 4 + 5 + extern enum usb_charger_type sprd_pmic_detect_charger_type(struct device *dev); 6 + 7 + #endif /* __LINUX_MFD_SC27XX_PMIC_H */