1 // SPDX-License-Identifier: GPL-2.0
3 * extcon driver for Basin Cove PMIC
5 * Copyright (c) 2019, Intel Corporation.
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
9 #include <linux/extcon-provider.h>
10 #include <linux/interrupt.h>
11 #include <linux/mfd/intel_soc_pmic.h>
12 #include <linux/mfd/intel_soc_pmic_mrfld.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
18 #include "extcon-intel.h"
20 #define BCOVE_USBIDCTRL 0x19
21 #define BCOVE_USBIDCTRL_ID BIT(0)
22 #define BCOVE_USBIDCTRL_ACA BIT(1)
23 #define BCOVE_USBIDCTRL_ALL (BCOVE_USBIDCTRL_ID | BCOVE_USBIDCTRL_ACA)
25 #define BCOVE_USBIDSTS 0x1a
26 #define BCOVE_USBIDSTS_GND BIT(0)
27 #define BCOVE_USBIDSTS_RARBRC_MASK GENMASK(2, 1)
28 #define BCOVE_USBIDSTS_RARBRC_SHIFT 1
29 #define BCOVE_USBIDSTS_NO_ACA 0
30 #define BCOVE_USBIDSTS_R_ID_A 1
31 #define BCOVE_USBIDSTS_R_ID_B 2
32 #define BCOVE_USBIDSTS_R_ID_C 3
33 #define BCOVE_USBIDSTS_FLOAT BIT(3)
34 #define BCOVE_USBIDSTS_SHORT BIT(4)
36 #define BCOVE_CHGRIRQ_ALL (BCOVE_CHGRIRQ_VBUSDET | BCOVE_CHGRIRQ_DCDET | \
37 BCOVE_CHGRIRQ_BATTDET | BCOVE_CHGRIRQ_USBIDDET)
39 #define BCOVE_CHGRCTRL0 0x4b
40 #define BCOVE_CHGRCTRL0_CHGRRESET BIT(0)
41 #define BCOVE_CHGRCTRL0_EMRGCHREN BIT(1)
42 #define BCOVE_CHGRCTRL0_EXTCHRDIS BIT(2)
43 #define BCOVE_CHGRCTRL0_SWCONTROL BIT(3)
44 #define BCOVE_CHGRCTRL0_TTLCK BIT(4)
45 #define BCOVE_CHGRCTRL0_BIT_5 BIT(5)
46 #define BCOVE_CHGRCTRL0_BIT_6 BIT(6)
47 #define BCOVE_CHGRCTRL0_CHR_WDT_NOKICK BIT(7)
49 struct mrfld_extcon_data
{
51 struct regmap
*regmap
;
52 struct extcon_dev
*edev
;
57 static const unsigned int mrfld_extcon_cable
[] = {
67 static int mrfld_extcon_clear(struct mrfld_extcon_data
*data
, unsigned int reg
,
70 return regmap_update_bits(data
->regmap
, reg
, mask
, 0x00);
73 static int mrfld_extcon_set(struct mrfld_extcon_data
*data
, unsigned int reg
,
76 return regmap_update_bits(data
->regmap
, reg
, mask
, 0xff);
79 static int mrfld_extcon_sw_control(struct mrfld_extcon_data
*data
, bool enable
)
81 unsigned int mask
= BCOVE_CHGRCTRL0_SWCONTROL
;
82 struct device
*dev
= data
->dev
;
86 ret
= mrfld_extcon_set(data
, BCOVE_CHGRCTRL0
, mask
);
88 ret
= mrfld_extcon_clear(data
, BCOVE_CHGRCTRL0
, mask
);
90 dev_err(dev
, "can't set SW control: %d\n", ret
);
94 static int mrfld_extcon_get_id(struct mrfld_extcon_data
*data
)
96 struct regmap
*regmap
= data
->regmap
;
101 ret
= regmap_read(regmap
, BCOVE_USBIDSTS
, &id
);
105 if (id
& BCOVE_USBIDSTS_FLOAT
)
106 return INTEL_USB_ID_FLOAT
;
108 switch ((id
& BCOVE_USBIDSTS_RARBRC_MASK
) >> BCOVE_USBIDSTS_RARBRC_SHIFT
) {
109 case BCOVE_USBIDSTS_R_ID_A
:
110 return INTEL_USB_RID_A
;
111 case BCOVE_USBIDSTS_R_ID_B
:
112 return INTEL_USB_RID_B
;
113 case BCOVE_USBIDSTS_R_ID_C
:
114 return INTEL_USB_RID_C
;
118 * PMIC A0 reports USBIDSTS_GND = 1 for ID_GND,
119 * but PMIC B0 reports USBIDSTS_GND = 0 for ID_GND.
120 * Thus we must check this bit at last.
122 ground
= id
& BCOVE_USBIDSTS_GND
;
123 switch ('A' + BCOVE_MAJOR(data
->id
)) {
125 return ground
? INTEL_USB_ID_GND
: INTEL_USB_ID_FLOAT
;
127 return ground
? INTEL_USB_ID_FLOAT
: INTEL_USB_ID_GND
;
130 /* Unknown or unsupported type */
131 return INTEL_USB_ID_FLOAT
;
134 static int mrfld_extcon_role_detect(struct mrfld_extcon_data
*data
)
140 ret
= mrfld_extcon_get_id(data
);
146 usb_host
= (id
== INTEL_USB_ID_GND
) || (id
== INTEL_USB_RID_A
);
147 extcon_set_state_sync(data
->edev
, EXTCON_USB_HOST
, usb_host
);
152 static int mrfld_extcon_cable_detect(struct mrfld_extcon_data
*data
)
154 struct regmap
*regmap
= data
->regmap
;
155 unsigned int status
, change
;
159 * It seems SCU firmware clears the content of BCOVE_CHGRIRQ1
160 * and makes it useless for OS. Instead we compare a previously
161 * stored status to the current one, provided by BCOVE_SCHGRIRQ1.
163 ret
= regmap_read(regmap
, BCOVE_SCHGRIRQ1
, &status
);
167 change
= status
^ data
->status
;
171 if (change
& BCOVE_CHGRIRQ_USBIDDET
) {
172 ret
= mrfld_extcon_role_detect(data
);
177 data
->status
= status
;
182 static irqreturn_t
mrfld_extcon_interrupt(int irq
, void *dev_id
)
184 struct mrfld_extcon_data
*data
= dev_id
;
187 ret
= mrfld_extcon_cable_detect(data
);
189 mrfld_extcon_clear(data
, BCOVE_MIRQLVL1
, BCOVE_LVL1_CHGR
);
191 return ret
? IRQ_NONE
: IRQ_HANDLED
;
194 static int mrfld_extcon_probe(struct platform_device
*pdev
)
196 struct device
*dev
= &pdev
->dev
;
197 struct intel_soc_pmic
*pmic
= dev_get_drvdata(dev
->parent
);
198 struct regmap
*regmap
= pmic
->regmap
;
199 struct mrfld_extcon_data
*data
;
204 irq
= platform_get_irq(pdev
, 0);
208 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
213 data
->regmap
= regmap
;
215 data
->edev
= devm_extcon_dev_allocate(dev
, mrfld_extcon_cable
);
216 if (IS_ERR(data
->edev
))
217 return PTR_ERR(data
->edev
);
219 ret
= devm_extcon_dev_register(dev
, data
->edev
);
221 return dev_err_probe(dev
, ret
, "can't register extcon device\n");
223 ret
= devm_request_threaded_irq(dev
, irq
, NULL
, mrfld_extcon_interrupt
,
224 IRQF_ONESHOT
| IRQF_SHARED
, pdev
->name
,
227 return dev_err_probe(dev
, ret
, "can't register IRQ handler\n");
229 ret
= regmap_read(regmap
, BCOVE_ID
, &id
);
231 return dev_err_probe(dev
, ret
, "can't read PMIC ID\n");
235 ret
= mrfld_extcon_sw_control(data
, true);
239 /* Get initial state */
240 mrfld_extcon_role_detect(data
);
243 * Cached status value is used for cable detection, see comments
244 * in mrfld_extcon_cable_detect(), we need to sync cached value
245 * with a real state of the hardware.
247 regmap_read(regmap
, BCOVE_SCHGRIRQ1
, &status
);
248 data
->status
= status
;
250 mrfld_extcon_clear(data
, BCOVE_MIRQLVL1
, BCOVE_LVL1_CHGR
);
251 mrfld_extcon_clear(data
, BCOVE_MCHGRIRQ1
, BCOVE_CHGRIRQ_ALL
);
253 mrfld_extcon_set(data
, BCOVE_USBIDCTRL
, BCOVE_USBIDCTRL_ALL
);
255 platform_set_drvdata(pdev
, data
);
260 static void mrfld_extcon_remove(struct platform_device
*pdev
)
262 struct mrfld_extcon_data
*data
= platform_get_drvdata(pdev
);
264 mrfld_extcon_sw_control(data
, false);
267 static const struct platform_device_id mrfld_extcon_id_table
[] = {
268 { .name
= "mrfld_bcove_pwrsrc" },
271 MODULE_DEVICE_TABLE(platform
, mrfld_extcon_id_table
);
273 static struct platform_driver mrfld_extcon_driver
= {
275 .name
= "mrfld_bcove_pwrsrc",
277 .probe
= mrfld_extcon_probe
,
278 .remove_new
= mrfld_extcon_remove
,
279 .id_table
= mrfld_extcon_id_table
,
281 module_platform_driver(mrfld_extcon_driver
);
283 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
284 MODULE_DESCRIPTION("extcon driver for Intel Merrifield Basin Cove PMIC");
285 MODULE_LICENSE("GPL v2");