Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver
  4 *
  5 * Copyright (c) 2017-2018 Hans de Goede <hdegoede@redhat.com>
  6 * Copyright (C) 2015 Intel Corporation
  7 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/acpi.h>
 11#include <linux/module.h>
 12#include <linux/kernel.h>
 13#include <linux/io.h>
 14#include <linux/slab.h>
 15#include <linux/interrupt.h>
 16#include <linux/platform_device.h>
 17#include <linux/property.h>
 
 18#include <linux/notifier.h>
 19#include <linux/extcon-provider.h>
 20#include <linux/regmap.h>
 
 
 21#include <linux/mfd/axp20x.h>
 22#include <linux/usb/role.h>
 23#include <linux/workqueue.h>
 24
 25#include <asm/cpu_device_id.h>
 26#include <asm/intel-family.h>
 27#include <asm/iosf_mbi.h>
 28
 29/* Power source status register */
 30#define PS_STAT_VBUS_TRIGGER		BIT(0)
 31#define PS_STAT_BAT_CHRG_DIR		BIT(2)
 32#define PS_STAT_VBUS_ABOVE_VHOLD	BIT(3)
 33#define PS_STAT_VBUS_VALID		BIT(4)
 34#define PS_STAT_VBUS_PRESENT		BIT(5)
 35
 36/* BC module global register */
 37#define BC_GLOBAL_RUN			BIT(0)
 38#define BC_GLOBAL_DET_STAT		BIT(2)
 39#define BC_GLOBAL_DBP_TOUT		BIT(3)
 40#define BC_GLOBAL_VLGC_COM_SEL		BIT(4)
 41#define BC_GLOBAL_DCD_TOUT_MASK		(BIT(6)|BIT(5))
 42#define BC_GLOBAL_DCD_TOUT_300MS	0
 43#define BC_GLOBAL_DCD_TOUT_100MS	1
 44#define BC_GLOBAL_DCD_TOUT_500MS	2
 45#define BC_GLOBAL_DCD_TOUT_900MS	3
 46#define BC_GLOBAL_DCD_DET_SEL		BIT(7)
 47
 48/* BC module vbus control and status register */
 49#define VBUS_CNTL_DPDM_PD_EN		BIT(4)
 50#define VBUS_CNTL_DPDM_FD_EN		BIT(5)
 51#define VBUS_CNTL_FIRST_PO_STAT		BIT(6)
 52
 53/* BC USB status register */
 54#define USB_STAT_BUS_STAT_MASK		(BIT(3)|BIT(2)|BIT(1)|BIT(0))
 55#define USB_STAT_BUS_STAT_SHIFT		0
 56#define USB_STAT_BUS_STAT_ATHD		0
 57#define USB_STAT_BUS_STAT_CONN		1
 58#define USB_STAT_BUS_STAT_SUSP		2
 59#define USB_STAT_BUS_STAT_CONF		3
 60#define USB_STAT_USB_SS_MODE		BIT(4)
 61#define USB_STAT_DEAD_BAT_DET		BIT(6)
 62#define USB_STAT_DBP_UNCFG		BIT(7)
 63
 64/* BC detect status register */
 65#define DET_STAT_MASK			(BIT(7)|BIT(6)|BIT(5))
 66#define DET_STAT_SHIFT			5
 67#define DET_STAT_SDP			1
 68#define DET_STAT_CDP			2
 69#define DET_STAT_DCP			3
 70
 
 
 
 
 
 
 71enum axp288_extcon_reg {
 72	AXP288_PS_STAT_REG		= 0x00,
 73	AXP288_PS_BOOT_REASON_REG	= 0x02,
 74	AXP288_BC_GLOBAL_REG		= 0x2c,
 75	AXP288_BC_VBUS_CNTL_REG		= 0x2d,
 76	AXP288_BC_USB_STAT_REG		= 0x2e,
 77	AXP288_BC_DET_STAT_REG		= 0x2f,
 
 
 
 
 
 
 
 78};
 79
 80enum axp288_extcon_irq {
 81	VBUS_FALLING_IRQ = 0,
 82	VBUS_RISING_IRQ,
 83	MV_CHNG_IRQ,
 84	BC_USB_CHNG_IRQ,
 85	EXTCON_IRQ_END,
 86};
 87
 88static const unsigned int axp288_extcon_cables[] = {
 89	EXTCON_CHG_USB_SDP,
 90	EXTCON_CHG_USB_CDP,
 91	EXTCON_CHG_USB_DCP,
 92	EXTCON_USB,
 93	EXTCON_NONE,
 94};
 95
 96struct axp288_extcon_info {
 97	struct device *dev;
 98	struct regmap *regmap;
 99	struct regmap_irq_chip_data *regmap_irqc;
100	struct usb_role_switch *role_sw;
101	struct work_struct role_work;
102	int irq[EXTCON_IRQ_END];
103	struct extcon_dev *edev;
104	struct extcon_dev *id_extcon;
105	struct notifier_block id_nb;
106	unsigned int previous_cable;
107	bool vbus_attach;
108};
109
110static const struct x86_cpu_id cherry_trail_cpu_ids[] = {
111	X86_MATCH_INTEL_FAM6_MODEL(ATOM_AIRMONT,	NULL),
112	{}
113};
114
115/* Power up/down reason string array */
116static const char * const axp288_pwr_up_down_info[] = {
117	"Last wake caused by user pressing the power button",
118	"Last wake caused by a charger insertion",
119	"Last wake caused by a battery insertion",
120	"Last wake caused by SOC initiated global reset",
121	"Last wake caused by cold reset",
122	"Last shutdown caused by PMIC UVLO threshold",
123	"Last shutdown caused by SOC initiated cold off",
124	"Last shutdown caused by user pressing the power button",
 
125};
126
127/*
128 * Decode and log the given "reset source indicator" (rsi)
129 * register and then clear it.
130 */
131static void axp288_extcon_log_rsi(struct axp288_extcon_info *info)
132{
 
133	unsigned int val, i, clear_mask = 0;
134	unsigned long bits;
135	int ret;
136
137	ret = regmap_read(info->regmap, AXP288_PS_BOOT_REASON_REG, &val);
138	if (ret < 0) {
139		dev_err(info->dev, "failed to read reset source indicator\n");
140		return;
 
 
141	}
142
143	bits = val & GENMASK(ARRAY_SIZE(axp288_pwr_up_down_info) - 1, 0);
144	for_each_set_bit(i, &bits, ARRAY_SIZE(axp288_pwr_up_down_info))
145		dev_dbg(info->dev, "%s\n", axp288_pwr_up_down_info[i]);
146	clear_mask = bits;
147
148	/* Clear the register value for next reboot (write 1 to clear bit) */
149	regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask);
150}
151
152/*
153 * The below code to control the USB role-switch on devices with an AXP288
154 * may seem out of place, but there are 2 reasons why this is the best place
155 * to control the USB role-switch on such devices:
156 * 1) On many devices the USB role is controlled by AML code, but the AML code
157 *    only switches between the host and none roles, because of Windows not
158 *    really using device mode. To make device mode work we need to toggle
159 *    between the none/device roles based on Vbus presence, and this driver
160 *    gets interrupts on Vbus insertion / removal.
161 * 2) In order for our BC1.2 charger detection to work properly the role
162 *    mux must be properly set to device mode before we do the detection.
163 */
164
165/* Returns the id-pin value, note pulled low / false == host-mode */
166static bool axp288_get_id_pin(struct axp288_extcon_info *info)
167{
168	enum usb_role role;
169
170	if (info->id_extcon)
171		return extcon_get_state(info->id_extcon, EXTCON_USB_HOST) <= 0;
172
173	/* We cannot access the id-pin, see what mode the AML code has set */
174	role = usb_role_switch_get_role(info->role_sw);
175	return role != USB_ROLE_HOST;
176}
177
178static void axp288_usb_role_work(struct work_struct *work)
179{
180	struct axp288_extcon_info *info =
181		container_of(work, struct axp288_extcon_info, role_work);
182	enum usb_role role;
183	bool id_pin;
184	int ret;
185
186	id_pin = axp288_get_id_pin(info);
187	if (!id_pin)
188		role = USB_ROLE_HOST;
189	else if (info->vbus_attach)
190		role = USB_ROLE_DEVICE;
191	else
192		role = USB_ROLE_NONE;
193
194	ret = usb_role_switch_set_role(info->role_sw, role);
195	if (ret)
196		dev_err(info->dev, "failed to set role: %d\n", ret);
197}
198
199static bool axp288_get_vbus_attach(struct axp288_extcon_info *info)
200{
201	int ret, pwr_stat;
202
203	ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat);
204	if (ret < 0) {
205		dev_err(info->dev, "failed to read vbus status\n");
206		return false;
207	}
208
209	return !!(pwr_stat & PS_STAT_VBUS_VALID);
210}
211
212static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info)
213{
214	int ret, stat, cfg;
 
 
215	u8 chrg_type;
216	unsigned int cable = info->previous_cable;
217	bool vbus_attach = false;
218
219	ret = iosf_mbi_block_punit_i2c_access();
220	if (ret < 0)
 
221		return ret;
 
222
223	vbus_attach = axp288_get_vbus_attach(info);
224	if (!vbus_attach)
225		goto no_vbus;
226
227	/* Check charger detection completion status */
228	ret = regmap_read(info->regmap, AXP288_BC_GLOBAL_REG, &cfg);
229	if (ret < 0)
230		goto dev_det_ret;
231	if (cfg & BC_GLOBAL_DET_STAT) {
232		dev_dbg(info->dev, "can't complete the charger detection\n");
233		goto dev_det_ret;
234	}
235
236	ret = regmap_read(info->regmap, AXP288_BC_DET_STAT_REG, &stat);
237	if (ret < 0)
238		goto dev_det_ret;
239
240	chrg_type = (stat & DET_STAT_MASK) >> DET_STAT_SHIFT;
241
242	switch (chrg_type) {
243	case DET_STAT_SDP:
244		dev_dbg(info->dev, "sdp cable is connected\n");
 
 
245		cable = EXTCON_CHG_USB_SDP;
246		break;
247	case DET_STAT_CDP:
248		dev_dbg(info->dev, "cdp cable is connected\n");
 
 
249		cable = EXTCON_CHG_USB_CDP;
250		break;
251	case DET_STAT_DCP:
252		dev_dbg(info->dev, "dcp cable is connected\n");
 
253		cable = EXTCON_CHG_USB_DCP;
254		break;
255	default:
256		dev_warn(info->dev, "unknown (reserved) bc detect result\n");
257		cable = EXTCON_CHG_USB_SDP;
258	}
259
260no_vbus:
261	iosf_mbi_unblock_punit_i2c_access();
 
 
 
 
 
 
 
 
262
263	extcon_set_state_sync(info->edev, info->previous_cable, false);
264	if (info->previous_cable == EXTCON_CHG_USB_SDP)
265		extcon_set_state_sync(info->edev, EXTCON_USB, false);
266
267	if (vbus_attach) {
268		extcon_set_state_sync(info->edev, cable, vbus_attach);
269		if (cable == EXTCON_CHG_USB_SDP)
270			extcon_set_state_sync(info->edev, EXTCON_USB,
271						vbus_attach);
272
273		info->previous_cable = cable;
274	}
275
276	if (info->role_sw && info->vbus_attach != vbus_attach) {
277		info->vbus_attach = vbus_attach;
278		/* Setting the role can take a while */
279		queue_work(system_long_wq, &info->role_work);
280	}
281
 
 
 
 
 
 
 
282	return 0;
283
284dev_det_ret:
285	iosf_mbi_unblock_punit_i2c_access();
286
287	if (ret < 0)
288		dev_err(info->dev, "failed to detect BC Mod\n");
289
290	return ret;
291}
292
293static int axp288_extcon_id_evt(struct notifier_block *nb,
294				unsigned long event, void *param)
295{
296	struct axp288_extcon_info *info =
297		container_of(nb, struct axp288_extcon_info, id_nb);
298
299	/* We may not sleep and setting the role can take a while */
300	queue_work(system_long_wq, &info->role_work);
301
302	return NOTIFY_OK;
303}
304
305static irqreturn_t axp288_extcon_isr(int irq, void *data)
306{
307	struct axp288_extcon_info *info = data;
308	int ret;
309
310	ret = axp288_handle_chrg_det_event(info);
311	if (ret < 0)
312		dev_err(info->dev, "failed to handle the interrupt\n");
313
314	return IRQ_HANDLED;
315}
316
317static int axp288_extcon_enable(struct axp288_extcon_info *info)
318{
319	int ret = 0;
320
321	ret = iosf_mbi_block_punit_i2c_access();
322	if (ret < 0)
323		return ret;
324
325	regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
326						BC_GLOBAL_RUN, 0);
 
 
327	/* Enable the charger detection logic */
328	regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
329					BC_GLOBAL_RUN, BC_GLOBAL_RUN);
330
331	iosf_mbi_unblock_punit_i2c_access();
332
333	return ret;
334}
335
336static void axp288_put_role_sw(void *data)
337{
338	struct axp288_extcon_info *info = data;
339
340	cancel_work_sync(&info->role_work);
341	usb_role_switch_put(info->role_sw);
342}
343
344static int axp288_extcon_find_role_sw(struct axp288_extcon_info *info)
345{
346	const struct software_node *swnode;
347	struct fwnode_handle *fwnode;
348
349	if (!x86_match_cpu(cherry_trail_cpu_ids))
350		return 0;
351
352	swnode = software_node_find_by_name(NULL, "intel-xhci-usb-sw");
353	if (!swnode)
354		return -EPROBE_DEFER;
355
356	fwnode = software_node_fwnode(swnode);
357	info->role_sw = usb_role_switch_find_by_fwnode(fwnode);
358	fwnode_handle_put(fwnode);
359
360	return info->role_sw ? 0 : -EPROBE_DEFER;
361}
362
363static int axp288_extcon_probe(struct platform_device *pdev)
364{
365	struct axp288_extcon_info *info;
366	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
367	struct device *dev = &pdev->dev;
368	struct acpi_device *adev;
369	int ret, i, pirq;
370
371	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
372	if (!info)
373		return -ENOMEM;
374
375	info->dev = &pdev->dev;
376	info->regmap = axp20x->regmap;
377	info->regmap_irqc = axp20x->regmap_irqc;
378	info->previous_cable = EXTCON_NONE;
379	INIT_WORK(&info->role_work, axp288_usb_role_work);
380	info->id_nb.notifier_call = axp288_extcon_id_evt;
381
382	platform_set_drvdata(pdev, info);
383
384	ret = axp288_extcon_find_role_sw(info);
385	if (ret)
386		return ret;
387
388	if (info->role_sw) {
389		ret = devm_add_action_or_reset(dev, axp288_put_role_sw, info);
390		if (ret)
391			return ret;
392
393		adev = acpi_dev_get_first_match_dev("INT3496", NULL, -1);
394		if (adev) {
395			info->id_extcon = extcon_get_extcon_dev(acpi_dev_name(adev));
396			put_device(&adev->dev);
397			if (IS_ERR(info->id_extcon))
398				return PTR_ERR(info->id_extcon);
399
400			dev_info(dev, "controlling USB role\n");
401		} else {
402			dev_info(dev, "controlling USB role based on Vbus presence\n");
403		}
404	}
405
406	ret = iosf_mbi_block_punit_i2c_access();
407	if (ret < 0)
408		return ret;
409
410	info->vbus_attach = axp288_get_vbus_attach(info);
411
412	axp288_extcon_log_rsi(info);
413
414	iosf_mbi_unblock_punit_i2c_access();
415
416	/* Initialize extcon device */
417	info->edev = devm_extcon_dev_allocate(&pdev->dev,
418					      axp288_extcon_cables);
419	if (IS_ERR(info->edev)) {
420		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
421		return PTR_ERR(info->edev);
422	}
423
424	/* Register extcon device */
425	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
426	if (ret) {
427		dev_err(&pdev->dev, "failed to register extcon device\n");
428		return ret;
429	}
430
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
431	for (i = 0; i < EXTCON_IRQ_END; i++) {
432		pirq = platform_get_irq(pdev, i);
433		if (pirq < 0)
434			return pirq;
435
436		info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
437		if (info->irq[i] < 0) {
438			dev_err(&pdev->dev,
439				"failed to get virtual interrupt=%d\n", pirq);
440			ret = info->irq[i];
441			return ret;
442		}
443
444		ret = devm_request_threaded_irq(&pdev->dev, info->irq[i],
445				NULL, axp288_extcon_isr,
446				IRQF_ONESHOT | IRQF_NO_SUSPEND,
447				pdev->name, info);
448		if (ret) {
449			dev_err(&pdev->dev, "failed to request interrupt=%d\n",
450							info->irq[i]);
451			return ret;
452		}
453	}
454
455	if (info->id_extcon) {
456		ret = devm_extcon_register_notifier_all(dev, info->id_extcon,
457							&info->id_nb);
458		if (ret)
459			return ret;
460	}
461
462	/* Make sure the role-sw is set correctly before doing BC detection */
463	if (info->role_sw) {
464		queue_work(system_long_wq, &info->role_work);
465		flush_work(&info->role_work);
466	}
467
468	/* Start charger cable type detection */
469	ret = axp288_extcon_enable(info);
470	if (ret < 0)
471		return ret;
472
473	device_init_wakeup(dev, true);
474	platform_set_drvdata(pdev, info);
475
476	return 0;
477}
478
479static int __maybe_unused axp288_extcon_suspend(struct device *dev)
480{
481	struct axp288_extcon_info *info = dev_get_drvdata(dev);
482
483	if (device_may_wakeup(dev))
484		enable_irq_wake(info->irq[VBUS_RISING_IRQ]);
485
486	return 0;
487}
488
489static int __maybe_unused axp288_extcon_resume(struct device *dev)
490{
491	struct axp288_extcon_info *info = dev_get_drvdata(dev);
492
493	/*
494	 * Wakeup when a charger is connected to do charger-type
495	 * connection and generate an extcon event which makes the
496	 * axp288 charger driver set the input current limit.
497	 */
498	if (device_may_wakeup(dev))
499		disable_irq_wake(info->irq[VBUS_RISING_IRQ]);
500
501	return 0;
502}
503
504static SIMPLE_DEV_PM_OPS(axp288_extcon_pm_ops, axp288_extcon_suspend,
505			 axp288_extcon_resume);
506
507static const struct platform_device_id axp288_extcon_table[] = {
508	{ .name = "axp288_extcon" },
509	{},
510};
511MODULE_DEVICE_TABLE(platform, axp288_extcon_table);
512
513static struct platform_driver axp288_extcon_driver = {
514	.probe = axp288_extcon_probe,
515	.id_table = axp288_extcon_table,
516	.driver = {
517		.name = "axp288_extcon",
518		.pm = &axp288_extcon_pm_ops,
519	},
520};
521module_platform_driver(axp288_extcon_driver);
522
523MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
524MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
525MODULE_DESCRIPTION("X-Powers AXP288 extcon driver");
526MODULE_LICENSE("GPL v2");
v4.6
 
  1/*
  2 * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver
  3 *
 
  4 * Copyright (C) 2015 Intel Corporation
  5 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify
  8 * it under the terms of the GNU General Public License version 2 as
  9 * published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope that it will be useful,
 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14 * GNU General Public License for more details.
 15 */
 16
 
 17#include <linux/module.h>
 18#include <linux/kernel.h>
 19#include <linux/io.h>
 20#include <linux/slab.h>
 21#include <linux/interrupt.h>
 22#include <linux/platform_device.h>
 23#include <linux/property.h>
 24#include <linux/usb/phy.h>
 25#include <linux/notifier.h>
 26#include <linux/extcon.h>
 27#include <linux/regmap.h>
 28#include <linux/gpio.h>
 29#include <linux/gpio/consumer.h>
 30#include <linux/mfd/axp20x.h>
 
 
 
 
 
 
 31
 32/* Power source status register */
 33#define PS_STAT_VBUS_TRIGGER		BIT(0)
 34#define PS_STAT_BAT_CHRG_DIR		BIT(2)
 35#define PS_STAT_VBUS_ABOVE_VHOLD	BIT(3)
 36#define PS_STAT_VBUS_VALID		BIT(4)
 37#define PS_STAT_VBUS_PRESENT		BIT(5)
 38
 39/* BC module global register */
 40#define BC_GLOBAL_RUN			BIT(0)
 41#define BC_GLOBAL_DET_STAT		BIT(2)
 42#define BC_GLOBAL_DBP_TOUT		BIT(3)
 43#define BC_GLOBAL_VLGC_COM_SEL		BIT(4)
 44#define BC_GLOBAL_DCD_TOUT_MASK		(BIT(6)|BIT(5))
 45#define BC_GLOBAL_DCD_TOUT_300MS	0
 46#define BC_GLOBAL_DCD_TOUT_100MS	1
 47#define BC_GLOBAL_DCD_TOUT_500MS	2
 48#define BC_GLOBAL_DCD_TOUT_900MS	3
 49#define BC_GLOBAL_DCD_DET_SEL		BIT(7)
 50
 51/* BC module vbus control and status register */
 52#define VBUS_CNTL_DPDM_PD_EN		BIT(4)
 53#define VBUS_CNTL_DPDM_FD_EN		BIT(5)
 54#define VBUS_CNTL_FIRST_PO_STAT		BIT(6)
 55
 56/* BC USB status register */
 57#define USB_STAT_BUS_STAT_MASK		(BIT(3)|BIT(2)|BIT(1)|BIT(0))
 58#define USB_STAT_BUS_STAT_SHIFT		0
 59#define USB_STAT_BUS_STAT_ATHD		0
 60#define USB_STAT_BUS_STAT_CONN		1
 61#define USB_STAT_BUS_STAT_SUSP		2
 62#define USB_STAT_BUS_STAT_CONF		3
 63#define USB_STAT_USB_SS_MODE		BIT(4)
 64#define USB_STAT_DEAD_BAT_DET		BIT(6)
 65#define USB_STAT_DBP_UNCFG		BIT(7)
 66
 67/* BC detect status register */
 68#define DET_STAT_MASK			(BIT(7)|BIT(6)|BIT(5))
 69#define DET_STAT_SHIFT			5
 70#define DET_STAT_SDP			1
 71#define DET_STAT_CDP			2
 72#define DET_STAT_DCP			3
 73
 74/* IRQ enable-1 register */
 75#define PWRSRC_IRQ_CFG_MASK		(BIT(4)|BIT(3)|BIT(2))
 76
 77/* IRQ enable-6 register */
 78#define BC12_IRQ_CFG_MASK		BIT(1)
 79
 80enum axp288_extcon_reg {
 81	AXP288_PS_STAT_REG		= 0x00,
 82	AXP288_PS_BOOT_REASON_REG	= 0x02,
 83	AXP288_BC_GLOBAL_REG		= 0x2c,
 84	AXP288_BC_VBUS_CNTL_REG		= 0x2d,
 85	AXP288_BC_USB_STAT_REG		= 0x2e,
 86	AXP288_BC_DET_STAT_REG		= 0x2f,
 87	AXP288_PWRSRC_IRQ_CFG_REG	= 0x40,
 88	AXP288_BC12_IRQ_CFG_REG		= 0x45,
 89};
 90
 91enum axp288_mux_select {
 92	EXTCON_GPIO_MUX_SEL_PMIC = 0,
 93	EXTCON_GPIO_MUX_SEL_SOC,
 94};
 95
 96enum axp288_extcon_irq {
 97	VBUS_FALLING_IRQ = 0,
 98	VBUS_RISING_IRQ,
 99	MV_CHNG_IRQ,
100	BC_USB_CHNG_IRQ,
101	EXTCON_IRQ_END,
102};
103
104static const unsigned int axp288_extcon_cables[] = {
105	EXTCON_CHG_USB_SDP,
106	EXTCON_CHG_USB_CDP,
107	EXTCON_CHG_USB_DCP,
 
108	EXTCON_NONE,
109};
110
111struct axp288_extcon_info {
112	struct device *dev;
113	struct regmap *regmap;
114	struct regmap_irq_chip_data *regmap_irqc;
115	struct axp288_extcon_pdata *pdata;
 
116	int irq[EXTCON_IRQ_END];
117	struct extcon_dev *edev;
118	struct notifier_block extcon_nb;
119	struct usb_phy *otg;
 
 
 
 
 
 
 
120};
121
122/* Power up/down reason string array */
123static char *axp288_pwr_up_down_info[] = {
124	"Last wake caused by user pressing the power button",
125	"Last wake caused by a charger insertion",
126	"Last wake caused by a battery insertion",
127	"Last wake caused by SOC initiated global reset",
128	"Last wake caused by cold reset",
129	"Last shutdown caused by PMIC UVLO threshold",
130	"Last shutdown caused by SOC initiated cold off",
131	"Last shutdown caused by user pressing the power button",
132	NULL,
133};
134
135/*
136 * Decode and log the given "reset source indicator" (rsi)
137 * register and then clear it.
138 */
139static void axp288_extcon_log_rsi(struct axp288_extcon_info *info)
140{
141	char **rsi;
142	unsigned int val, i, clear_mask = 0;
 
143	int ret;
144
145	ret = regmap_read(info->regmap, AXP288_PS_BOOT_REASON_REG, &val);
146	for (i = 0, rsi = axp288_pwr_up_down_info; *rsi; rsi++, i++) {
147		if (val & BIT(i)) {
148			dev_dbg(info->dev, "%s\n", *rsi);
149			clear_mask |= BIT(i);
150		}
151	}
152
 
 
 
 
 
153	/* Clear the register value for next reboot (write 1 to clear bit) */
154	regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask);
155}
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info)
158{
159	static bool notify_otg, notify_charger;
160	static unsigned int cable;
161	int ret, stat, cfg, pwr_stat;
162	u8 chrg_type;
 
163	bool vbus_attach = false;
164
165	ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat);
166	if (ret < 0) {
167		dev_err(info->dev, "failed to read vbus status\n");
168		return ret;
169	}
170
171	vbus_attach = (pwr_stat & PS_STAT_VBUS_PRESENT);
172	if (!vbus_attach)
173		goto notify_otg;
174
175	/* Check charger detection completion status */
176	ret = regmap_read(info->regmap, AXP288_BC_GLOBAL_REG, &cfg);
177	if (ret < 0)
178		goto dev_det_ret;
179	if (cfg & BC_GLOBAL_DET_STAT) {
180		dev_dbg(info->dev, "can't complete the charger detection\n");
181		goto dev_det_ret;
182	}
183
184	ret = regmap_read(info->regmap, AXP288_BC_DET_STAT_REG, &stat);
185	if (ret < 0)
186		goto dev_det_ret;
187
188	chrg_type = (stat & DET_STAT_MASK) >> DET_STAT_SHIFT;
189
190	switch (chrg_type) {
191	case DET_STAT_SDP:
192		dev_dbg(info->dev, "sdp cable is connecetd\n");
193		notify_otg = true;
194		notify_charger = true;
195		cable = EXTCON_CHG_USB_SDP;
196		break;
197	case DET_STAT_CDP:
198		dev_dbg(info->dev, "cdp cable is connecetd\n");
199		notify_otg = true;
200		notify_charger = true;
201		cable = EXTCON_CHG_USB_CDP;
202		break;
203	case DET_STAT_DCP:
204		dev_dbg(info->dev, "dcp cable is connecetd\n");
205		notify_charger = true;
206		cable = EXTCON_CHG_USB_DCP;
207		break;
208	default:
209		dev_warn(info->dev,
210			"disconnect or unknown or ID event\n");
211	}
212
213notify_otg:
214	if (notify_otg) {
215		/*
216		 * If VBUS is absent Connect D+/D- lines to PMIC for BC
217		 * detection. Else connect them to SOC for USB communication.
218		 */
219		if (info->pdata->gpio_mux_cntl)
220			gpiod_set_value(info->pdata->gpio_mux_cntl,
221				vbus_attach ? EXTCON_GPIO_MUX_SEL_SOC
222						: EXTCON_GPIO_MUX_SEL_PMIC);
223
224		atomic_notifier_call_chain(&info->otg->notifier,
225			vbus_attach ? USB_EVENT_VBUS : USB_EVENT_NONE, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226	}
227
228	if (notify_charger)
229		extcon_set_cable_state_(info->edev, cable, vbus_attach);
230
231	/* Clear the flags on disconnect event */
232	if (!vbus_attach)
233		notify_otg = notify_charger = false;
234
235	return 0;
236
237dev_det_ret:
 
 
238	if (ret < 0)
239		dev_err(info->dev, "failed to detect BC Mod\n");
240
241	return ret;
242}
243
 
 
 
 
 
 
 
 
 
 
 
 
244static irqreturn_t axp288_extcon_isr(int irq, void *data)
245{
246	struct axp288_extcon_info *info = data;
247	int ret;
248
249	ret = axp288_handle_chrg_det_event(info);
250	if (ret < 0)
251		dev_err(info->dev, "failed to handle the interrupt\n");
252
253	return IRQ_HANDLED;
254}
255
256static void axp288_extcon_enable_irq(struct axp288_extcon_info *info)
257{
258	/* Unmask VBUS interrupt */
259	regmap_write(info->regmap, AXP288_PWRSRC_IRQ_CFG_REG,
260						PWRSRC_IRQ_CFG_MASK);
 
 
 
261	regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
262						BC_GLOBAL_RUN, 0);
263	/* Unmask the BC1.2 complete interrupts */
264	regmap_write(info->regmap, AXP288_BC12_IRQ_CFG_REG, BC12_IRQ_CFG_MASK);
265	/* Enable the charger detection logic */
266	regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
267					BC_GLOBAL_RUN, BC_GLOBAL_RUN);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268}
269
270static int axp288_extcon_probe(struct platform_device *pdev)
271{
272	struct axp288_extcon_info *info;
273	struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
274	int ret, i, pirq, gpio;
 
 
275
276	info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
277	if (!info)
278		return -ENOMEM;
279
280	info->dev = &pdev->dev;
281	info->regmap = axp20x->regmap;
282	info->regmap_irqc = axp20x->regmap_irqc;
283	info->pdata = pdev->dev.platform_data;
 
 
 
 
 
 
 
 
 
 
 
 
 
284
285	if (!info->pdata) {
286		/* Try ACPI provided pdata via device properties */
287		if (!device_property_present(&pdev->dev,
288					"axp288_extcon_data\n"))
289			dev_err(&pdev->dev, "failed to get platform data\n");
290		return -ENODEV;
 
 
 
 
 
291	}
292	platform_set_drvdata(pdev, info);
 
 
 
 
 
293
294	axp288_extcon_log_rsi(info);
295
 
 
296	/* Initialize extcon device */
297	info->edev = devm_extcon_dev_allocate(&pdev->dev,
298					      axp288_extcon_cables);
299	if (IS_ERR(info->edev)) {
300		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
301		return PTR_ERR(info->edev);
302	}
303
304	/* Register extcon device */
305	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
306	if (ret) {
307		dev_err(&pdev->dev, "failed to register extcon device\n");
308		return ret;
309	}
310
311	/* Get otg transceiver phy */
312	info->otg = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
313	if (IS_ERR(info->otg)) {
314		dev_err(&pdev->dev, "failed to get otg transceiver\n");
315		return PTR_ERR(info->otg);
316	}
317
318	/* Set up gpio control for USB Mux */
319	if (info->pdata->gpio_mux_cntl) {
320		gpio = desc_to_gpio(info->pdata->gpio_mux_cntl);
321		ret = devm_gpio_request(&pdev->dev, gpio, "USB_MUX");
322		if (ret < 0) {
323			dev_err(&pdev->dev,
324				"failed to request the gpio=%d\n", gpio);
325			return ret;
326		}
327		gpiod_direction_output(info->pdata->gpio_mux_cntl,
328						EXTCON_GPIO_MUX_SEL_PMIC);
329	}
330
331	for (i = 0; i < EXTCON_IRQ_END; i++) {
332		pirq = platform_get_irq(pdev, i);
 
 
 
333		info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
334		if (info->irq[i] < 0) {
335			dev_err(&pdev->dev,
336				"failed to get virtual interrupt=%d\n", pirq);
337			ret = info->irq[i];
338			return ret;
339		}
340
341		ret = devm_request_threaded_irq(&pdev->dev, info->irq[i],
342				NULL, axp288_extcon_isr,
343				IRQF_ONESHOT | IRQF_NO_SUSPEND,
344				pdev->name, info);
345		if (ret) {
346			dev_err(&pdev->dev, "failed to request interrupt=%d\n",
347							info->irq[i]);
348			return ret;
349		}
350	}
351
352	/* Enable interrupts */
353	axp288_extcon_enable_irq(info);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
355	return 0;
356}
357
 
 
 
 
 
 
 
 
 
358static struct platform_driver axp288_extcon_driver = {
359	.probe = axp288_extcon_probe,
 
360	.driver = {
361		.name = "axp288_extcon",
 
362	},
363};
364module_platform_driver(axp288_extcon_driver);
365
366MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
 
367MODULE_DESCRIPTION("X-Powers AXP288 extcon driver");
368MODULE_LICENSE("GPL v2");