summaryrefslogtreecommitdiffstats
path: root/drivers/extcon/extcon-axp288.c
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2018-03-20 08:57:13 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2018-03-22 08:49:28 -0400
commitd54f063cdbe414590c97d990111ddff25c6f9593 (patch)
tree3d5a6e845143ff9b882f28e240b2f8e7a926ac94 /drivers/extcon/extcon-axp288.c
parent29b4aec2f407341963b7e3b15e8756ce815abf8a (diff)
extcon: axp288: Set USB role where necessary
The AXP288 BC1.2 charger detection / extcon code may seem like a strange place to add code to control the USB role-switch on devices with an AXP288, but there are 2 reasons to do this inside the axp288 extcon code: 1) On many devices the USB role is controlled by ACPI AML code, but the AML code only switches between the host and none roles, because of Windows not really using device mode. To make device mode work we need to toggle between the none/device roles based on Vbus presence, and the axp288 extcon gets interrupts on Vbus insertion / removal. 2) In order for our BC1.2 charger detection to work properly the role mux must be properly set to device mode before we do the detection. Also note the Kconfig help-text / obsolete depends on USB_PHY which are remnants from older never upstreamed code also controlling the mux from the axp288 extcon code. This commit also adds code to get notifications from the INT3496 extcon device, which is used on some devices to notify the kernel about id-pin changes instead of them being handled through AML code. This fixes: -Device mode not working on most CHT devices with an AXP288 -Host mode not working on devices with an INT3496 ACPI device -Charger-type misdetection (always SDP) on devices with an INT3496 when the USB role (always) gets initialized as host Signed-off-by: Hans de Goede <hdegoede@redhat.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/extcon/extcon-axp288.c')
-rw-r--r--drivers/extcon/extcon-axp288.c176
1 files changed, 168 insertions, 8 deletions
diff --git a/drivers/extcon/extcon-axp288.c b/drivers/extcon/extcon-axp288.c
index 3ec4c715e240..a983708b77a6 100644
--- a/drivers/extcon/extcon-axp288.c
+++ b/drivers/extcon/extcon-axp288.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver 2 * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver
3 * 3 *
4 * Copyright (c) 2017-2018 Hans de Goede <hdegoede@redhat.com>
4 * Copyright (C) 2015 Intel Corporation 5 * Copyright (C) 2015 Intel Corporation
5 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com> 6 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
6 * 7 *
@@ -14,6 +15,7 @@
14 * GNU General Public License for more details. 15 * GNU General Public License for more details.
15 */ 16 */
16 17
18#include <linux/acpi.h>
17#include <linux/module.h> 19#include <linux/module.h>
18#include <linux/kernel.h> 20#include <linux/kernel.h>
19#include <linux/io.h> 21#include <linux/io.h>
@@ -25,6 +27,11 @@
25#include <linux/extcon-provider.h> 27#include <linux/extcon-provider.h>
26#include <linux/regmap.h> 28#include <linux/regmap.h>
27#include <linux/mfd/axp20x.h> 29#include <linux/mfd/axp20x.h>
30#include <linux/usb/role.h>
31#include <linux/workqueue.h>
32
33#include <asm/cpu_device_id.h>
34#include <asm/intel-family.h>
28 35
29/* Power source status register */ 36/* Power source status register */
30#define PS_STAT_VBUS_TRIGGER BIT(0) 37#define PS_STAT_VBUS_TRIGGER BIT(0)
@@ -97,9 +104,19 @@ struct axp288_extcon_info {
97 struct device *dev; 104 struct device *dev;
98 struct regmap *regmap; 105 struct regmap *regmap;
99 struct regmap_irq_chip_data *regmap_irqc; 106 struct regmap_irq_chip_data *regmap_irqc;
107 struct usb_role_switch *role_sw;
108 struct work_struct role_work;
100 int irq[EXTCON_IRQ_END]; 109 int irq[EXTCON_IRQ_END];
101 struct extcon_dev *edev; 110 struct extcon_dev *edev;
111 struct extcon_dev *id_extcon;
112 struct notifier_block id_nb;
102 unsigned int previous_cable; 113 unsigned int previous_cable;
114 bool vbus_attach;
115};
116
117static const struct x86_cpu_id cherry_trail_cpu_ids[] = {
118 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT, X86_FEATURE_ANY },
119 {}
103}; 120};
104 121
105/* Power up/down reason string array */ 122/* Power up/down reason string array */
@@ -137,20 +154,74 @@ static void axp288_extcon_log_rsi(struct axp288_extcon_info *info)
137 regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask); 154 regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask);
138} 155}
139 156
140static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info) 157/*
158 * The below code to control the USB role-switch on devices with an AXP288
159 * may seem out of place, but there are 2 reasons why this is the best place
160 * to control the USB role-switch on such devices:
161 * 1) On many devices the USB role is controlled by AML code, but the AML code
162 * only switches between the host and none roles, because of Windows not
163 * really using device mode. To make device mode work we need to toggle
164 * between the none/device roles based on Vbus presence, and this driver
165 * gets interrupts on Vbus insertion / removal.
166 * 2) In order for our BC1.2 charger detection to work properly the role
167 * mux must be properly set to device mode before we do the detection.
168 */
169
170/* Returns the id-pin value, note pulled low / false == host-mode */
171static bool axp288_get_id_pin(struct axp288_extcon_info *info)
141{ 172{
142 int ret, stat, cfg, pwr_stat; 173 enum usb_role role;
143 u8 chrg_type; 174
144 unsigned int cable = info->previous_cable; 175 if (info->id_extcon)
145 bool vbus_attach = false; 176 return extcon_get_state(info->id_extcon, EXTCON_USB_HOST) <= 0;
177
178 /* We cannot access the id-pin, see what mode the AML code has set */
179 role = usb_role_switch_get_role(info->role_sw);
180 return role != USB_ROLE_HOST;
181}
182
183static void axp288_usb_role_work(struct work_struct *work)
184{
185 struct axp288_extcon_info *info =
186 container_of(work, struct axp288_extcon_info, role_work);
187 enum usb_role role;
188 bool id_pin;
189 int ret;
190
191 id_pin = axp288_get_id_pin(info);
192 if (!id_pin)
193 role = USB_ROLE_HOST;
194 else if (info->vbus_attach)
195 role = USB_ROLE_DEVICE;
196 else
197 role = USB_ROLE_NONE;
198
199 ret = usb_role_switch_set_role(info->role_sw, role);
200 if (ret)
201 dev_err(info->dev, "failed to set role: %d\n", ret);
202}
203
204static bool axp288_get_vbus_attach(struct axp288_extcon_info *info)
205{
206 int ret, pwr_stat;
146 207
147 ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat); 208 ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat);
148 if (ret < 0) { 209 if (ret < 0) {
149 dev_err(info->dev, "failed to read vbus status\n"); 210 dev_err(info->dev, "failed to read vbus status\n");
150 return ret; 211 return false;
151 } 212 }
152 213
153 vbus_attach = (pwr_stat & PS_STAT_VBUS_VALID); 214 return !!(pwr_stat & PS_STAT_VBUS_VALID);
215}
216
217static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info)
218{
219 int ret, stat, cfg;
220 u8 chrg_type;
221 unsigned int cable = info->previous_cable;
222 bool vbus_attach = false;
223
224 vbus_attach = axp288_get_vbus_attach(info);
154 if (!vbus_attach) 225 if (!vbus_attach)
155 goto no_vbus; 226 goto no_vbus;
156 227
@@ -201,6 +272,12 @@ no_vbus:
201 info->previous_cable = cable; 272 info->previous_cable = cable;
202 } 273 }
203 274
275 if (info->role_sw && info->vbus_attach != vbus_attach) {
276 info->vbus_attach = vbus_attach;
277 /* Setting the role can take a while */
278 queue_work(system_long_wq, &info->role_work);
279 }
280
204 return 0; 281 return 0;
205 282
206dev_det_ret: 283dev_det_ret:
@@ -210,6 +287,18 @@ dev_det_ret:
210 return ret; 287 return ret;
211} 288}
212 289
290static int axp288_extcon_id_evt(struct notifier_block *nb,
291 unsigned long event, void *param)
292{
293 struct axp288_extcon_info *info =
294 container_of(nb, struct axp288_extcon_info, id_nb);
295
296 /* We may not sleep and setting the role can take a while */
297 queue_work(system_long_wq, &info->role_work);
298
299 return NOTIFY_OK;
300}
301
213static irqreturn_t axp288_extcon_isr(int irq, void *data) 302static irqreturn_t axp288_extcon_isr(int irq, void *data)
214{ 303{
215 struct axp288_extcon_info *info = data; 304 struct axp288_extcon_info *info = data;
@@ -231,10 +320,20 @@ static void axp288_extcon_enable(struct axp288_extcon_info *info)
231 BC_GLOBAL_RUN, BC_GLOBAL_RUN); 320 BC_GLOBAL_RUN, BC_GLOBAL_RUN);
232} 321}
233 322
323static void axp288_put_role_sw(void *data)
324{
325 struct axp288_extcon_info *info = data;
326
327 cancel_work_sync(&info->role_work);
328 usb_role_switch_put(info->role_sw);
329}
330
234static int axp288_extcon_probe(struct platform_device *pdev) 331static int axp288_extcon_probe(struct platform_device *pdev)
235{ 332{
236 struct axp288_extcon_info *info; 333 struct axp288_extcon_info *info;
237 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent); 334 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
335 struct device *dev = &pdev->dev;
336 const char *name;
238 int ret, i, pirq; 337 int ret, i, pirq;
239 338
240 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL); 339 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
@@ -245,9 +344,33 @@ static int axp288_extcon_probe(struct platform_device *pdev)
245 info->regmap = axp20x->regmap; 344 info->regmap = axp20x->regmap;
246 info->regmap_irqc = axp20x->regmap_irqc; 345 info->regmap_irqc = axp20x->regmap_irqc;
247 info->previous_cable = EXTCON_NONE; 346 info->previous_cable = EXTCON_NONE;
347 INIT_WORK(&info->role_work, axp288_usb_role_work);
348 info->id_nb.notifier_call = axp288_extcon_id_evt;
248 349
249 platform_set_drvdata(pdev, info); 350 platform_set_drvdata(pdev, info);
250 351
352 info->role_sw = usb_role_switch_get(dev);
353 if (IS_ERR(info->role_sw))
354 return PTR_ERR(info->role_sw);
355 if (info->role_sw) {
356 ret = devm_add_action_or_reset(dev, axp288_put_role_sw, info);
357 if (ret)
358 return ret;
359
360 name = acpi_dev_get_first_match_name("INT3496", NULL, -1);
361 if (name) {
362 info->id_extcon = extcon_get_extcon_dev(name);
363 if (!info->id_extcon)
364 return -EPROBE_DEFER;
365
366 dev_info(dev, "controlling USB role\n");
367 } else {
368 dev_info(dev, "controlling USB role based on Vbus presence\n");
369 }
370 }
371
372 info->vbus_attach = axp288_get_vbus_attach(info);
373
251 axp288_extcon_log_rsi(info); 374 axp288_extcon_log_rsi(info);
252 375
253 /* Initialize extcon device */ 376 /* Initialize extcon device */
@@ -289,6 +412,19 @@ static int axp288_extcon_probe(struct platform_device *pdev)
289 } 412 }
290 } 413 }
291 414
415 if (info->id_extcon) {
416 ret = devm_extcon_register_notifier_all(dev, info->id_extcon,
417 &info->id_nb);
418 if (ret)
419 return ret;
420 }
421
422 /* Make sure the role-sw is set correctly before doing BC detection */
423 if (info->role_sw) {
424 queue_work(system_long_wq, &info->role_work);
425 flush_work(&info->role_work);
426 }
427
292 /* Start charger cable type detection */ 428 /* Start charger cable type detection */
293 axp288_extcon_enable(info); 429 axp288_extcon_enable(info);
294 430
@@ -308,8 +444,32 @@ static struct platform_driver axp288_extcon_driver = {
308 .name = "axp288_extcon", 444 .name = "axp288_extcon",
309 }, 445 },
310}; 446};
311module_platform_driver(axp288_extcon_driver); 447
448static struct device_connection axp288_extcon_role_sw_conn = {
449 .endpoint[0] = "axp288_extcon",
450 .endpoint[1] = "intel_xhci_usb_sw-role-switch",
451 .id = "usb-role-switch",
452};
453
454static int __init axp288_extcon_init(void)
455{
456 if (x86_match_cpu(cherry_trail_cpu_ids))
457 device_connection_add(&axp288_extcon_role_sw_conn);
458
459 return platform_driver_register(&axp288_extcon_driver);
460}
461module_init(axp288_extcon_init);
462
463static void __exit axp288_extcon_exit(void)
464{
465 if (x86_match_cpu(cherry_trail_cpu_ids))
466 device_connection_remove(&axp288_extcon_role_sw_conn);
467
468 platform_driver_unregister(&axp288_extcon_driver);
469}
470module_exit(axp288_extcon_exit);
312 471
313MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>"); 472MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
473MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
314MODULE_DESCRIPTION("X-Powers AXP288 extcon driver"); 474MODULE_DESCRIPTION("X-Powers AXP288 extcon driver");
315MODULE_LICENSE("GPL v2"); 475MODULE_LICENSE("GPL v2");