Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) // extcon-max8997.c - MAX8997 extcon driver to support MAX8997 MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) //  Copyright (C) 2012 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) //  Donggeun Kim <dg77.kim@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/mfd/max8997.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/mfd/max8997-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/extcon-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/irqdomain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define	DEV_NAME			"max8997-muic"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define	DELAY_MS_DEFAULT		20000		/* unit: millisecond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) enum max8997_muic_adc_debounce_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	ADC_DEBOUNCE_TIME_0_5MS = 0,	/* 0.5ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	ADC_DEBOUNCE_TIME_10MS,		/* 10ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	ADC_DEBOUNCE_TIME_25MS,		/* 25ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	ADC_DEBOUNCE_TIME_38_62MS,	/* 38.62ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct max8997_muic_irq {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	unsigned int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct max8997_muic_irq muic_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	{ MAX8997_MUICIRQ_ADCError,	"muic-ADCERROR" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	{ MAX8997_MUICIRQ_ADCLow,	"muic-ADCLOW" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	{ MAX8997_MUICIRQ_ADC,		"muic-ADC" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	{ MAX8997_MUICIRQ_VBVolt,	"muic-VBVOLT" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	{ MAX8997_MUICIRQ_DBChg,	"muic-DBCHG" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	{ MAX8997_MUICIRQ_DCDTmr,	"muic-DCDTMR" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	{ MAX8997_MUICIRQ_ChgDetRun,	"muic-CHGDETRUN" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	{ MAX8997_MUICIRQ_ChgTyp,	"muic-CHGTYP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	{ MAX8997_MUICIRQ_OVP,		"muic-OVP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) /* Define supported cable type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) enum max8997_muic_acc_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	MAX8997_MUIC_ADC_GROUND = 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	MAX8997_MUIC_ADC_MHL,			/* MHL*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	MAX8997_MUIC_ADC_REMOTE_S1_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	MAX8997_MUIC_ADC_REMOTE_S2_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	MAX8997_MUIC_ADC_REMOTE_S3_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	MAX8997_MUIC_ADC_REMOTE_S4_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	MAX8997_MUIC_ADC_REMOTE_S5_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	MAX8997_MUIC_ADC_REMOTE_S6_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	MAX8997_MUIC_ADC_REMOTE_S7_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	MAX8997_MUIC_ADC_REMOTE_S8_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	MAX8997_MUIC_ADC_REMOTE_S9_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	MAX8997_MUIC_ADC_REMOTE_S10_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	MAX8997_MUIC_ADC_REMOTE_S11_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	MAX8997_MUIC_ADC_REMOTE_S12_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	MAX8997_MUIC_ADC_RESERVED_ACC_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	MAX8997_MUIC_ADC_RESERVED_ACC_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	MAX8997_MUIC_ADC_RESERVED_ACC_3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	MAX8997_MUIC_ADC_RESERVED_ACC_4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	MAX8997_MUIC_ADC_RESERVED_ACC_5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	MAX8997_MUIC_ADC_CEA936_AUDIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	MAX8997_MUIC_ADC_PHONE_POWERED_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	MAX8997_MUIC_ADC_TTY_CONVERTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	MAX8997_MUIC_ADC_UART_CABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF,	/* JIG-USB-OFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON,	/* JIG-USB-ON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	MAX8997_MUIC_ADC_AV_CABLE_NOLOAD,	/* DESKDOCK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF,	/* JIG-UART */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON,	/* CARDOCK */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	MAX8997_MUIC_ADC_OPEN,			/* OPEN */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) enum max8997_muic_cable_group {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	MAX8997_CABLE_GROUP_ADC = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	MAX8997_CABLE_GROUP_ADC_GND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	MAX8997_CABLE_GROUP_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	MAX8997_CABLE_GROUP_VBVOLT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) enum max8997_muic_usb_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	MAX8997_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	MAX8997_USB_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) enum max8997_muic_charger_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	MAX8997_CHARGER_TYPE_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	MAX8997_CHARGER_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	MAX8997_CHARGER_TYPE_DEDICATED_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	MAX8997_CHARGER_TYPE_500MA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	MAX8997_CHARGER_TYPE_1A,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	MAX8997_CHARGER_TYPE_DEAD_BATTERY = 7,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct max8997_muic_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct i2c_client *muic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int prev_cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int prev_chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	u8 status[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	struct work_struct irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct max8997_muic_platform_data *muic_pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	enum max8997_muic_charger_type pre_charger_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	 * Use delayed workqueue to detect cable state and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	 * notify cable state to notifiee/platform through uevent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	 * After completing the booting of platform, the extcon provider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * driver should notify cable state to upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	struct delayed_work wq_detcable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * h/w path of COMP2/COMN1 on CONTROL1 register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	int path_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	int path_uart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static const unsigned int max8997_extcon_cable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	EXTCON_CHG_USB_FAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	EXTCON_CHG_USB_SLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	EXTCON_DISP_MHL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	EXTCON_DOCK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	EXTCON_JIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	EXTCON_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * max8997_muic_set_debounce_time - Set the debounce time of ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * @info: the instance including private data of max8997 MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * @time: the debounce time of ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static int max8997_muic_set_debounce_time(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		enum max8997_muic_adc_debounce_time time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	switch (time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	case ADC_DEBOUNCE_TIME_0_5MS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	case ADC_DEBOUNCE_TIME_10MS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	case ADC_DEBOUNCE_TIME_25MS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	case ADC_DEBOUNCE_TIME_38_62MS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		ret = max8997_update_reg(info->muic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 					  MAX8997_MUIC_REG_CONTROL3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					  time << CONTROL3_ADCDBSET_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 					  CONTROL3_ADCDBSET_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			dev_err(info->dev, "failed to set ADC debounce time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		dev_err(info->dev, "invalid ADC debounce time\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * max8997_muic_set_path - Set hardware line according to attached cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * @info: the instance including private data of max8997 MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * @value: the path according to attached cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * @attached: the state of cable (true:attached, false:detached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * The max8997 MUIC device share outside H/W line among a varity of cables,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * so this function set internal path of H/W line according to the type of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * attached cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static int max8997_muic_set_path(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		u8 val, bool attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	u8 ctrl1, ctrl2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		ctrl1 = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		ctrl1 = CONTROL1_SW_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	ret = max8997_update_reg(info->muic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			MAX8997_MUIC_REG_CONTROL1, ctrl1, COMP_SW_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		dev_err(info->dev, "failed to update MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		ctrl2 |= CONTROL2_CPEN_MASK;	/* LowPwr=0, CPEn=1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		ctrl2 |= CONTROL2_LOWPWR_MASK;	/* LowPwr=1, CPEn=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	ret = max8997_update_reg(info->muic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			MAX8997_MUIC_REG_CONTROL2, ctrl2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			CONTROL2_LOWPWR_MASK | CONTROL2_CPEN_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		dev_err(info->dev, "failed to update MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	dev_info(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		"CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		ctrl1, ctrl2, attached ? "attached" : "detached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * max8997_muic_get_cable_type - Return cable type and check cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * @info: the instance including private data of max8997 MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * @group: the path according to attached cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  * @attached: store cable state and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * This function check the cable state either attached or detached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * and then divide precise type of cable according to cable group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  *	- MAX8997_CABLE_GROUP_ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)  *	- MAX8997_CABLE_GROUP_CHG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int max8997_muic_get_cable_type(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		enum max8997_muic_cable_group group, bool *attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	int cable_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	switch (group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	case MAX8997_CABLE_GROUP_ADC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		 * Read ADC value to check cable type and decide cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		 * according to cable type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		adc = info->status[0] & STATUS1_ADC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		adc >>= STATUS1_ADC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		 * Check current cable state/cable type and store cable type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * (info->prev_cable_type) for handling cable when cable is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		if (adc == MAX8997_MUIC_ADC_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			*attached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			cable_type = info->prev_cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			info->prev_cable_type = MAX8997_MUIC_ADC_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			*attached = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			cable_type = info->prev_cable_type = adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	case MAX8997_CABLE_GROUP_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		 * Read charger type to check cable type and decide cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		 * according to type of charger cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		chg_type = info->status[1] & STATUS2_CHGTYP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		chg_type >>= STATUS2_CHGTYP_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if (chg_type == MAX8997_CHARGER_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			*attached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			cable_type = info->prev_chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 			info->prev_chg_type = MAX8997_CHARGER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			*attached = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			 * Check current cable state/cable type and store cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			 * type(info->prev_chg_type) for handling cable when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			 * charger cable is detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			cable_type = info->prev_chg_type = chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		dev_err(info->dev, "Unknown cable group (%d)\n", group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		cable_type = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	return cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static int max8997_muic_handle_usb(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			enum max8997_muic_usb_type usb_type, bool attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	ret = max8997_muic_set_path(info, info->path_usb, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		dev_err(info->dev, "failed to update muic register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	switch (usb_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	case MAX8997_USB_HOST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		extcon_set_state_sync(info->edev, EXTCON_USB_HOST, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	case MAX8997_USB_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		extcon_set_state_sync(info->edev, EXTCON_USB, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 					attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		dev_err(info->dev, "failed to detect %s usb cable\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			attached ? "attached" : "detached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static int max8997_muic_handle_dock(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 			int cable_type, bool attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ret = max8997_muic_set_path(info, CONTROL1_SW_AUDIO, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		dev_err(info->dev, "failed to update muic register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	switch (cable_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		extcon_set_state_sync(info->edev, EXTCON_DOCK, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		dev_err(info->dev, "failed to detect %s dock device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			attached ? "attached" : "detached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int max8997_muic_handle_jig_uart(struct max8997_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			bool attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	/* switch to UART */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	ret = max8997_muic_set_path(info, info->path_uart, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		dev_err(info->dev, "failed to update muic register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int max8997_muic_adc_handler(struct max8997_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	int cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	/* Check cable state which is either detached or attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	cable_type = max8997_muic_get_cable_type(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				MAX8997_CABLE_GROUP_ADC, &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	switch (cable_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	case MAX8997_MUIC_ADC_GROUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ret = max8997_muic_handle_usb(info, MAX8997_USB_HOST, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	case MAX8997_MUIC_ADC_MHL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		extcon_set_state_sync(info->edev, EXTCON_DISP_MHL, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	case MAX8997_MUIC_ADC_FACTORY_MODE_USB_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		ret = max8997_muic_handle_usb(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 					     MAX8997_USB_DEVICE, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	case MAX8997_MUIC_ADC_AV_CABLE_NOLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		ret = max8997_muic_handle_dock(info, cable_type, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	case MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		ret = max8997_muic_handle_jig_uart(info, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	case MAX8997_MUIC_ADC_REMOTE_S1_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	case MAX8997_MUIC_ADC_REMOTE_S2_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	case MAX8997_MUIC_ADC_REMOTE_S3_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	case MAX8997_MUIC_ADC_REMOTE_S4_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	case MAX8997_MUIC_ADC_REMOTE_S5_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	case MAX8997_MUIC_ADC_REMOTE_S6_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	case MAX8997_MUIC_ADC_REMOTE_S7_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	case MAX8997_MUIC_ADC_REMOTE_S8_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	case MAX8997_MUIC_ADC_REMOTE_S9_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	case MAX8997_MUIC_ADC_REMOTE_S10_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	case MAX8997_MUIC_ADC_REMOTE_S11_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	case MAX8997_MUIC_ADC_REMOTE_S12_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	case MAX8997_MUIC_ADC_RESERVED_ACC_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	case MAX8997_MUIC_ADC_RESERVED_ACC_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	case MAX8997_MUIC_ADC_RESERVED_ACC_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	case MAX8997_MUIC_ADC_RESERVED_ACC_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	case MAX8997_MUIC_ADC_RESERVED_ACC_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	case MAX8997_MUIC_ADC_CEA936_AUDIO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	case MAX8997_MUIC_ADC_PHONE_POWERED_DEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	case MAX8997_MUIC_ADC_TTY_CONVERTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	case MAX8997_MUIC_ADC_UART_CABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	case MAX8997_MUIC_ADC_CEA936A_TYPE1_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	case MAX8997_MUIC_ADC_CEA936A_TYPE2_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	case MAX8997_MUIC_ADC_AUDIO_MODE_REMOTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		 * This cable isn't used in general case if it is specially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		 * needed to detect additional cable, should implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		 * proper operation when this cable is attached/detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		dev_info(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			"cable is %s but it isn't used (type:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			attached ? "attached" : "detached", cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 		dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 			"failed to detect %s unknown cable (type:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			attached ? "attached" : "detached", cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static int max8997_muic_chg_handler(struct max8997_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	chg_type = max8997_muic_get_cable_type(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 				MAX8997_CABLE_GROUP_CHG, &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	switch (chg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	case MAX8997_CHARGER_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	case MAX8997_CHARGER_TYPE_USB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		adc = info->status[0] & STATUS1_ADC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		adc >>= STATUS1_ADC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		if ((adc & STATUS1_ADC_MASK) == MAX8997_MUIC_ADC_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			max8997_muic_handle_usb(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 					MAX8997_USB_DEVICE, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	case MAX8997_CHARGER_TYPE_DOWNSTREAM_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 					attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	case MAX8997_CHARGER_TYPE_DEDICATED_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 					attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	case MAX8997_CHARGER_TYPE_500MA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 					attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	case MAX8997_CHARGER_TYPE_1A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 					attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 		dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			"failed to detect %s unknown chg cable (type:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			attached ? "attached" : "detached", chg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static void max8997_muic_irq_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	struct max8997_muic_info *info = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			struct max8997_muic_info, irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	int irq_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (!info->edev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		if (info->irq == muic_irqs[i].virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			irq_type = muic_irqs[i].irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 				2, info->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		dev_err(info->dev, "failed to read muic register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	switch (irq_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	case MAX8997_MUICIRQ_ADCError:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	case MAX8997_MUICIRQ_ADCLow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	case MAX8997_MUICIRQ_ADC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		/* Handle all of cable except for charger cable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		ret = max8997_muic_adc_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	case MAX8997_MUICIRQ_VBVolt:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	case MAX8997_MUICIRQ_DBChg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	case MAX8997_MUICIRQ_DCDTmr:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	case MAX8997_MUICIRQ_ChgDetRun:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	case MAX8997_MUICIRQ_ChgTyp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		/* Handle charger cable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		ret = max8997_muic_chg_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	case MAX8997_MUICIRQ_OVP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		dev_info(info->dev, "misc interrupt: irq %d occurred\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 				irq_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		dev_err(info->dev, "failed to handle MUIC interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) static irqreturn_t max8997_muic_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	struct max8997_muic_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	dev_dbg(info->dev, "irq:%d\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	info->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	schedule_work(&info->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static int max8997_muic_detect_dev(struct max8997_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	int adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	/* Read STATUSx register to detect accessory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	ret = max8997_bulk_read(info->muic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			MAX8997_MUIC_REG_STATUS1, 2, info->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		dev_err(info->dev, "failed to read MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	adc = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_ADC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 					&attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (attached && adc != MAX8997_MUIC_ADC_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		ret = max8997_muic_adc_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			dev_err(info->dev, "Cannot detect ADC cable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	chg_type = max8997_muic_get_cable_type(info, MAX8997_CABLE_GROUP_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 					&attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	if (attached && chg_type != MAX8997_CHARGER_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		ret = max8997_muic_chg_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			dev_err(info->dev, "Cannot detect charger cable\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static void max8997_muic_detect_cable_wq(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	struct max8997_muic_info *info = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				struct max8997_muic_info, wq_detcable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	ret = max8997_muic_detect_dev(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		dev_err(info->dev, "failed to detect cable type\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static int max8997_muic_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	struct max8997_dev *max8997 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct max8997_platform_data *pdata = dev_get_platdata(max8997->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	struct max8997_muic_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	int delay_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	int cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	info = devm_kzalloc(&pdev->dev, sizeof(struct max8997_muic_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	info->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	info->muic = max8997->muic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	mutex_init(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	INIT_WORK(&info->irq_work, max8997_muic_irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		struct max8997_muic_irq *muic_irq = &muic_irqs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		unsigned int virq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		virq = irq_create_mapping(max8997->irq_domain, muic_irq->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 		if (!virq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		muic_irq->virq = virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		ret = request_threaded_irq(virq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 				max8997_muic_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				IRQF_NO_SUSPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 				muic_irq->name, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 			dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 				"failed: irq request (IRQ: %d, error :%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 				muic_irq->irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 			goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	/* External connector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	info->edev = devm_extcon_dev_allocate(&pdev->dev, max8997_extcon_cable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	if (IS_ERR(info->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		ret = PTR_ERR(info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	ret = devm_extcon_dev_register(&pdev->dev, info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 		dev_err(&pdev->dev, "failed to register extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	if (pdata && pdata->muic_pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 		struct max8997_muic_platform_data *muic_pdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 			= pdata->muic_pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		/* Initialize registers according to platform data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		for (i = 0; i < muic_pdata->num_init_data; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 			max8997_write_reg(info->muic,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 					muic_pdata->init_data[i].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 					muic_pdata->init_data[i].data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		 * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 		 * h/w path of COMP2/COMN1 on CONTROL1 register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		if (muic_pdata->path_uart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 			info->path_uart = muic_pdata->path_uart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 			info->path_uart = CONTROL1_SW_UART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		if (muic_pdata->path_usb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 			info->path_usb = muic_pdata->path_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 			info->path_usb = CONTROL1_SW_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		 * Default delay time for detecting cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 		 * after certain time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 		if (muic_pdata->detcable_delay_ms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 			delay_jiffies =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 				msecs_to_jiffies(muic_pdata->detcable_delay_ms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 			delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 		info->path_uart = CONTROL1_SW_UART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		info->path_usb = CONTROL1_SW_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	/* Set initial path for UART when JIG is connected to get serial logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	ret = max8997_bulk_read(info->muic, MAX8997_MUIC_REG_STATUS1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 				2, info->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		dev_err(info->dev, "failed to read MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		goto err_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	cable_type = max8997_muic_get_cable_type(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 					   MAX8997_CABLE_GROUP_ADC, &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	if (attached && cable_type == MAX8997_MUIC_ADC_FACTORY_MODE_UART_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		max8997_muic_set_path(info, info->path_uart, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	/* Set ADC debounce time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	max8997_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	 * Detect accessory after completing the initialization of platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	 * - Use delayed workqueue to detect cable state and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	 * notify cable state to notifiee/platform through uevent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	 * After completing the booting of platform, the extcon provider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	 * driver should notify cable state to upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	INIT_DELAYED_WORK(&info->wq_detcable, max8997_muic_detect_cable_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 			delay_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) err_irq:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		free_irq(muic_irqs[i].virq, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) static int max8997_muic_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	struct max8997_muic_info *info = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	for (i = 0; i < ARRAY_SIZE(muic_irqs); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		free_irq(muic_irqs[i].virq, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	cancel_work_sync(&info->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static struct platform_driver max8997_muic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 		.name	= DEV_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	.probe		= max8997_muic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	.remove		= max8997_muic_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) module_platform_driver(max8997_muic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) MODULE_DESCRIPTION("Maxim MAX8997 Extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) MODULE_ALIAS("platform:max8997-muic");