1280 lines · 36.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+//// extcon-max77693.c - MAX77693 extcon driver to support MAX77693 MUIC//// Copyright (C) 2012 Samsung Electrnoics// Chanwoo Choi <cw00.choi@samsung.com> #include <linux/devm-helpers.h>#include <linux/kernel.h>#include <linux/module.h>#include <linux/i2c.h>#include <linux/slab.h>#include <linux/input.h>#include <linux/interrupt.h>#include <linux/err.h>#include <linux/platform_device.h>#include <linux/mfd/max77693.h>#include <linux/mfd/max77693-common.h>#include <linux/mfd/max77693-private.h>#include <linux/extcon-provider.h>#include <linux/regmap.h>#include <linux/irqdomain.h> #define	DEV_NAME			"max77693-muic"#define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */ /* * Default value of MAX77693 register to bring up MUIC device. * If user don't set some initial value for MUIC device through platform data, * extcon-max77693 driver use 'default_init_data' to bring up base operation * of MAX77693 MUIC device. */static struct max77693_reg_data default_init_data[] = {	{		/* STATUS2 - [3]ChgDetRun */		.addr = MAX77693_MUIC_REG_STATUS2,		.data = MAX77693_STATUS2_CHGDETRUN_MASK,	}, {		/* INTMASK1 - Unmask [3]ADC1KM,[0]ADCM */		.addr = MAX77693_MUIC_REG_INTMASK1,		.data = INTMASK1_ADC1K_MASK			| INTMASK1_ADC_MASK,	}, {		/* INTMASK2 - Unmask [0]ChgTypM */		.addr = MAX77693_MUIC_REG_INTMASK2,		.data = INTMASK2_CHGTYP_MASK,	}, {		/* INTMASK3 - Mask all of interrupts */		.addr = MAX77693_MUIC_REG_INTMASK3,		.data = 0x0,	}, {		/* CDETCTRL2 */		.addr = MAX77693_MUIC_REG_CDETCTRL2,		.data = CDETCTRL2_VIDRMEN_MASK			| CDETCTRL2_DXOVPEN_MASK,	},}; enum max77693_muic_adc_debounce_time {	ADC_DEBOUNCE_TIME_5MS = 0,	ADC_DEBOUNCE_TIME_10MS,	ADC_DEBOUNCE_TIME_25MS,	ADC_DEBOUNCE_TIME_38_62MS,}; struct max77693_muic_info {	struct device *dev;	struct max77693_dev *max77693;	struct extcon_dev *edev;	int prev_cable_type;	int prev_cable_type_gnd;	int prev_chg_type;	int prev_button_type;	u8 status[2]; 	int irq;	struct work_struct irq_work;	struct mutex mutex; 	/*	 * Use delayed workqueue to detect cable state and then	 * notify cable state to notifiee/platform through uevent.	 * After completing the booting of platform, the extcon provider	 * driver should notify cable state to upper layer.	 */	struct delayed_work wq_detcable; 	/* Button of dock device */	struct input_dev *dock; 	/*	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB	 * h/w path of COMP2/COMN1 on CONTROL1 register.	 */	int path_usb;	int path_uart;}; enum max77693_muic_cable_group {	MAX77693_CABLE_GROUP_ADC = 0,	MAX77693_CABLE_GROUP_ADC_GND,	MAX77693_CABLE_GROUP_CHG,	MAX77693_CABLE_GROUP_VBVOLT,}; enum max77693_muic_charger_type {	MAX77693_CHARGER_TYPE_NONE = 0,	MAX77693_CHARGER_TYPE_USB,	MAX77693_CHARGER_TYPE_DOWNSTREAM_PORT,	MAX77693_CHARGER_TYPE_DEDICATED_CHG,	MAX77693_CHARGER_TYPE_APPLE_500MA,	MAX77693_CHARGER_TYPE_APPLE_1A_2A,	MAX77693_CHARGER_TYPE_DEAD_BATTERY = 7,}; /** * struct max77693_muic_irq * @irq: the index of irq list of MUIC device. * @name: the name of irq. * @virq: the virtual irq to use irq domain */struct max77693_muic_irq {	unsigned int irq;	const char *name;	unsigned int virq;}; static struct max77693_muic_irq muic_irqs[] = {	{ MAX77693_MUIC_IRQ_INT1_ADC,		"muic-ADC" },	{ MAX77693_MUIC_IRQ_INT1_ADC_LOW,	"muic-ADCLOW" },	{ MAX77693_MUIC_IRQ_INT1_ADC_ERR,	"muic-ADCError" },	{ MAX77693_MUIC_IRQ_INT1_ADC1K,		"muic-ADC1K" },	{ MAX77693_MUIC_IRQ_INT2_CHGTYP,	"muic-CHGTYP" },	{ MAX77693_MUIC_IRQ_INT2_CHGDETREUN,	"muic-CHGDETREUN" },	{ MAX77693_MUIC_IRQ_INT2_DCDTMR,	"muic-DCDTMR" },	{ MAX77693_MUIC_IRQ_INT2_DXOVP,		"muic-DXOVP" },	{ MAX77693_MUIC_IRQ_INT2_VBVOLT,	"muic-VBVOLT" },	{ MAX77693_MUIC_IRQ_INT2_VIDRM,		"muic-VIDRM" },	{ MAX77693_MUIC_IRQ_INT3_EOC,		"muic-EOC" },	{ MAX77693_MUIC_IRQ_INT3_CGMBC,		"muic-CGMBC" },	{ MAX77693_MUIC_IRQ_INT3_OVP,		"muic-OVP" },	{ MAX77693_MUIC_IRQ_INT3_MBCCHG_ERR,	"muic-MBCCHG_ERR" },	{ MAX77693_MUIC_IRQ_INT3_CHG_ENABLED,	"muic-CHG_ENABLED" },	{ MAX77693_MUIC_IRQ_INT3_BAT_DET,	"muic-BAT_DET" },}; /* Define supported accessory type */enum max77693_muic_acc_type {	MAX77693_MUIC_ADC_GROUND = 0x0,	MAX77693_MUIC_ADC_SEND_END_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S1_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S2_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S3_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S4_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S5_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S6_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S7_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S8_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S9_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S10_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S11_BUTTON,	MAX77693_MUIC_ADC_REMOTE_S12_BUTTON,	MAX77693_MUIC_ADC_RESERVED_ACC_1,	MAX77693_MUIC_ADC_RESERVED_ACC_2,	MAX77693_MUIC_ADC_RESERVED_ACC_3,	MAX77693_MUIC_ADC_RESERVED_ACC_4,	MAX77693_MUIC_ADC_RESERVED_ACC_5,	MAX77693_MUIC_ADC_CEA936_AUDIO,	MAX77693_MUIC_ADC_PHONE_POWERED_DEV,	MAX77693_MUIC_ADC_TTY_CONVERTER,	MAX77693_MUIC_ADC_UART_CABLE,	MAX77693_MUIC_ADC_CEA936A_TYPE1_CHG,	MAX77693_MUIC_ADC_FACTORY_MODE_USB_OFF,	MAX77693_MUIC_ADC_FACTORY_MODE_USB_ON,	MAX77693_MUIC_ADC_AV_CABLE_NOLOAD,	MAX77693_MUIC_ADC_CEA936A_TYPE2_CHG,	MAX77693_MUIC_ADC_FACTORY_MODE_UART_OFF,	MAX77693_MUIC_ADC_FACTORY_MODE_UART_ON,	MAX77693_MUIC_ADC_AUDIO_MODE_REMOTE,	MAX77693_MUIC_ADC_OPEN, 	/*	 * The below accessories have same ADC value so ADCLow and	 * ADC1K bit is used to separate specific accessory.	 */						/* ADC|VBVolot|ADCLow|ADC1K| */	MAX77693_MUIC_GND_USB_HOST = 0x100,	/* 0x0|      0|     0|    0| */	MAX77693_MUIC_GND_USB_HOST_VB = 0x104,	/* 0x0|      1|     0|    0| */	MAX77693_MUIC_GND_AV_CABLE_LOAD = 0x102,/* 0x0|      0|     1|    0| */	MAX77693_MUIC_GND_MHL = 0x103,		/* 0x0|      0|     1|    1| */	MAX77693_MUIC_GND_MHL_VB = 0x107,	/* 0x0|      1|     1|    1| */}; /* * MAX77693 MUIC device support below list of accessories(external connector) */static const unsigned int max77693_extcon_cable[] = {	EXTCON_USB,	EXTCON_USB_HOST,	EXTCON_CHG_USB_SDP,	EXTCON_CHG_USB_DCP,	EXTCON_CHG_USB_FAST,	EXTCON_CHG_USB_SLOW,	EXTCON_CHG_USB_CDP,	EXTCON_DISP_MHL,	EXTCON_JIG,	EXTCON_DOCK,	EXTCON_NONE,}; /* * max77693_muic_set_debounce_time - Set the debounce time of ADC * @info: the instance including private data of max77693 MUIC * @time: the debounce time of ADC */static int max77693_muic_set_debounce_time(struct max77693_muic_info *info,		enum max77693_muic_adc_debounce_time time){	int ret; 	switch (time) {	case ADC_DEBOUNCE_TIME_5MS:	case ADC_DEBOUNCE_TIME_10MS:	case ADC_DEBOUNCE_TIME_25MS:	case ADC_DEBOUNCE_TIME_38_62MS:		/*		 * Don't touch BTLDset, JIGset when you want to change adc		 * debounce time. If it writes other than 0 to BTLDset, JIGset		 * muic device will be reset and loose current state.		 */		ret = regmap_write(info->max77693->regmap_muic,				  MAX77693_MUIC_REG_CTRL3,				  time << MAX77693_CONTROL3_ADCDBSET_SHIFT);		if (ret) {			dev_err(info->dev, "failed to set ADC debounce time\n");			return ret;		}		break;	default:		dev_err(info->dev, "invalid ADC debounce time\n");		return -EINVAL;	} 	return 0;}; /* * max77693_muic_set_path - Set hardware line according to attached cable * @info: the instance including private data of max77693 MUIC * @value: the path according to attached cable * @attached: the state of cable (true:attached, false:detached) * * The max77693 MUIC device share outside H/W line among a varity of cables * so, this function set internal path of H/W line according to the type of * attached cable. */static int max77693_muic_set_path(struct max77693_muic_info *info,		u8 val, bool attached){	int ret;	unsigned int ctrl1, ctrl2 = 0; 	if (attached)		ctrl1 = val;	else		ctrl1 = MAX77693_CONTROL1_SW_OPEN; 	ret = regmap_update_bits(info->max77693->regmap_muic,			MAX77693_MUIC_REG_CTRL1, COMP_SW_MASK, ctrl1);	if (ret < 0) {		dev_err(info->dev, "failed to update MUIC register\n");		return ret;	} 	if (attached)		ctrl2 |= MAX77693_CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */	else		ctrl2 |= MAX77693_CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */ 	ret = regmap_update_bits(info->max77693->regmap_muic,			MAX77693_MUIC_REG_CTRL2,			MAX77693_CONTROL2_LOWPWR_MASK | MAX77693_CONTROL2_CPEN_MASK,			ctrl2);	if (ret < 0) {		dev_err(info->dev, "failed to update MUIC register\n");		return ret;	} 	dev_info(info->dev,		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",		ctrl1, ctrl2, attached ? "attached" : "detached"); 	return 0;} /* * max77693_muic_get_cable_type - Return cable type and check cable state * @info: the instance including private data of max77693 MUIC * @group: the path according to attached cable * @attached: store cable state and return * * This function check the cable state either attached or detached, * and then divide precise type of cable according to cable group. *	- MAX77693_CABLE_GROUP_ADC *	- MAX77693_CABLE_GROUP_ADC_GND *	- MAX77693_CABLE_GROUP_CHG *	- MAX77693_CABLE_GROUP_VBVOLT */static int max77693_muic_get_cable_type(struct max77693_muic_info *info,		enum max77693_muic_cable_group group, bool *attached){	int cable_type = 0;	int adc;	int adc1k;	int adclow;	int vbvolt;	int chg_type; 	switch (group) {	case MAX77693_CABLE_GROUP_ADC:		/*		 * Read ADC value to check cable type and decide cable state		 * according to cable type		 */		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;		adc >>= MAX77693_STATUS1_ADC_SHIFT; 		/*		 * Check current cable state/cable type and store cable type		 * (info->prev_cable_type) for handling cable when cable is		 * detached.		 */		if (adc == MAX77693_MUIC_ADC_OPEN) {			*attached = false; 			cable_type = info->prev_cable_type;			info->prev_cable_type = MAX77693_MUIC_ADC_OPEN;		} else {			*attached = true; 			cable_type = info->prev_cable_type = adc;		}		break;	case MAX77693_CABLE_GROUP_ADC_GND:		/*		 * Read ADC value to check cable type and decide cable state		 * according to cable type		 */		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;		adc >>= MAX77693_STATUS1_ADC_SHIFT; 		/*		 * Check current cable state/cable type and store cable type		 * (info->prev_cable_type/_gnd) for handling cable when cable		 * is detached.		 */		if (adc == MAX77693_MUIC_ADC_OPEN) {			*attached = false; 			cable_type = info->prev_cable_type_gnd;			info->prev_cable_type_gnd = MAX77693_MUIC_ADC_OPEN;		} else {			*attached = true; 			adclow = info->status[0] & MAX77693_STATUS1_ADCLOW_MASK;			adclow >>= MAX77693_STATUS1_ADCLOW_SHIFT;			adc1k = info->status[0] & MAX77693_STATUS1_ADC1K_MASK;			adc1k >>= MAX77693_STATUS1_ADC1K_SHIFT; 			vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;			vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT; 			/**			 * [0x1|VBVolt|ADCLow|ADC1K]			 * [0x1|     0|     0|    0] USB_HOST			 * [0x1|     1|     0|    0] USB_HSOT_VB			 * [0x1|     0|     1|    0] Audio Video cable with load			 * [0x1|     0|     1|    1] MHL without charging cable			 * [0x1|     1|     1|    1] MHL with charging cable			 */			cable_type = ((0x1 << 8)					| (vbvolt << 2)					| (adclow << 1)					| adc1k); 			info->prev_cable_type = adc;			info->prev_cable_type_gnd = cable_type;		} 		break;	case MAX77693_CABLE_GROUP_CHG:		/*		 * Read charger type to check cable type and decide cable state		 * according to type of charger cable.		 */		chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;		chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT; 		if (chg_type == MAX77693_CHARGER_TYPE_NONE) {			*attached = false; 			cable_type = info->prev_chg_type;			info->prev_chg_type = MAX77693_CHARGER_TYPE_NONE;		} else {			*attached = true; 			/*			 * Check current cable state/cable type and store cable			 * type(info->prev_chg_type) for handling cable when			 * charger cable is detached.			 */			cable_type = info->prev_chg_type = chg_type;		} 		break;	case MAX77693_CABLE_GROUP_VBVOLT:		/*		 * Read ADC value to check cable type and decide cable state		 * according to cable type		 */		adc = info->status[0] & MAX77693_STATUS1_ADC_MASK;		adc >>= MAX77693_STATUS1_ADC_SHIFT;		chg_type = info->status[1] & MAX77693_STATUS2_CHGTYP_MASK;		chg_type >>= MAX77693_STATUS2_CHGTYP_SHIFT; 		if (adc == MAX77693_MUIC_ADC_OPEN				&& chg_type == MAX77693_CHARGER_TYPE_NONE)			*attached = false;		else			*attached = true; 		/*		 * Read vbvolt field, if vbvolt is 1,		 * this cable is used for charging.		 */		vbvolt = info->status[1] & MAX77693_STATUS2_VBVOLT_MASK;		vbvolt >>= MAX77693_STATUS2_VBVOLT_SHIFT; 		cable_type = vbvolt;		break;	default:		dev_err(info->dev, "Unknown cable group (%d)\n", group);		cable_type = -EINVAL;		break;	} 	return cable_type;} static int max77693_muic_dock_handler(struct max77693_muic_info *info,		int cable_type, bool attached){	int ret = 0;	int vbvolt;	bool cable_attached;	unsigned int dock_id; 	dev_info(info->dev,		"external connector is %s (adc:0x%02x)\n",		attached ? "attached" : "detached", cable_type); 	switch (cable_type) {	case MAX77693_MUIC_ADC_RESERVED_ACC_3:		/* Dock-Smart */		/*		 * Check power cable whether attached or detached state.		 * The Dock-Smart device need surely external power supply.		 * If power cable(USB/TA) isn't connected to Dock device,		 * user can't use Dock-Smart for desktop mode.		 */		vbvolt = max77693_muic_get_cable_type(info,				MAX77693_CABLE_GROUP_VBVOLT, &cable_attached);		if (attached && !vbvolt) {			dev_warn(info->dev,				"Cannot detect external power supply\n");			return 0;		} 		/*		 * Notify Dock/MHL state.		 * - Dock device include three type of cable which		 * are HDMI, USB for mouse/keyboard and micro-usb port		 * for USB/TA cable. Dock device need always exteranl		 * power supply(USB/TA cable through micro-usb cable). Dock		 * device support screen output of target to separate		 * monitor and mouse/keyboard for desktop mode.		 *		 * Features of 'USB/TA cable with Dock device'		 * - Support MHL		 * - Support external output feature of audio		 * - Support charging through micro-usb port without data		 *	     connection if TA cable is connected to target.		 * - Support charging and data connection through micro-usb port		 *           if USB cable is connected between target and host		 *	     device.		 * - Support OTG(On-The-Go) device (Ex: Mouse/Keyboard)		 */		ret = max77693_muic_set_path(info, info->path_usb, attached);		if (ret < 0)			return ret; 

Lines 1–500 of 1280.