^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-max14577.c - MAX14577/77836 extcon driver to support MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Copyright (C) 2013,2014 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Chanwoo Choi <cw00.choi@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Krzysztof Kozlowski <krzk@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/i2c.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/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/mfd/max14577.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/max14577-private.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/extcon-provider.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define DELAY_MS_DEFAULT 17000 /* unit: millisecond */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) enum max14577_muic_adc_debounce_time {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) ADC_DEBOUNCE_TIME_5MS = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) ADC_DEBOUNCE_TIME_10MS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) ADC_DEBOUNCE_TIME_25MS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) ADC_DEBOUNCE_TIME_38_62MS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) enum max14577_muic_status {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) MAX14577_MUIC_STATUS1 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) MAX14577_MUIC_STATUS2 = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MAX14577_MUIC_STATUS_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * struct max14577_muic_irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @irq: the index of irq list of MUIC device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @name: the name of irq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @virq: the virtual irq to use irq domain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct max14577_muic_irq {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned int virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct max14577_muic_irq max14577_muic_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static struct max14577_muic_irq max77836_muic_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) { MAX14577_IRQ_INT1_ADC, "muic-ADC" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) { MAX14577_IRQ_INT1_ADCLOW, "muic-ADCLOW" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) { MAX14577_IRQ_INT1_ADCERR, "muic-ADCError" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) { MAX77836_IRQ_INT1_ADC1K, "muic-ADC1K" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) { MAX14577_IRQ_INT2_CHGTYP, "muic-CHGTYP" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) { MAX14577_IRQ_INT2_CHGDETRUN, "muic-CHGDETRUN" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) { MAX14577_IRQ_INT2_DCDTMR, "muic-DCDTMR" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) { MAX14577_IRQ_INT2_DBCHG, "muic-DBCHG" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) { MAX14577_IRQ_INT2_VBVOLT, "muic-VBVOLT" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) { MAX77836_IRQ_INT2_VIDRM, "muic-VIDRM" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct max14577_muic_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct max14577 *max14577;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int prev_cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int prev_chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) u8 status[MAX14577_MUIC_STATUS_END];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct max14577_muic_irq *muic_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) unsigned int muic_irqs_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) bool irq_adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) bool irq_chg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct work_struct irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct mutex mutex;
^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) * Use delayed workqueue to detect cable state and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * notify cable state to notifiee/platform through uevent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * After completing the booting of platform, the extcon provider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * driver should notify cable state to upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct delayed_work wq_detcable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Default usb/uart path whether UART/USB or AUX_UART/AUX_USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * h/w path of COMP2/COMN1 on CONTROL1 register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) int path_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int path_uart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) enum max14577_muic_cable_group {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) MAX14577_CABLE_GROUP_ADC = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) MAX14577_CABLE_GROUP_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* Define supported accessory type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) enum max14577_muic_acc_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) MAX14577_MUIC_ADC_GROUND = 0x0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) MAX14577_MUIC_ADC_SEND_END_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) MAX14577_MUIC_ADC_REMOTE_S1_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) MAX14577_MUIC_ADC_REMOTE_S2_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) MAX14577_MUIC_ADC_REMOTE_S3_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) MAX14577_MUIC_ADC_REMOTE_S4_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) MAX14577_MUIC_ADC_REMOTE_S5_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) MAX14577_MUIC_ADC_REMOTE_S6_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) MAX14577_MUIC_ADC_REMOTE_S7_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) MAX14577_MUIC_ADC_REMOTE_S8_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) MAX14577_MUIC_ADC_REMOTE_S9_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) MAX14577_MUIC_ADC_REMOTE_S10_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) MAX14577_MUIC_ADC_REMOTE_S11_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) MAX14577_MUIC_ADC_REMOTE_S12_BUTTON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) MAX14577_MUIC_ADC_RESERVED_ACC_1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) MAX14577_MUIC_ADC_RESERVED_ACC_2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) MAX14577_MUIC_ADC_RESERVED_ACC_3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) MAX14577_MUIC_ADC_RESERVED_ACC_4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) MAX14577_MUIC_ADC_RESERVED_ACC_5,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MAX14577_MUIC_ADC_PHONE_POWERED_DEV,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) MAX14577_MUIC_ADC_TTY_CONVERTER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MAX14577_MUIC_ADC_UART_CABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) MAX14577_MUIC_ADC_AV_CABLE_NOLOAD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1, /* with Remote and Simple Ctrl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MAX14577_MUIC_ADC_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const unsigned int max14577_extcon_cable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) EXTCON_CHG_USB_FAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) EXTCON_CHG_USB_SLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) EXTCON_CHG_USB_CDP,
^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) * max14577_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 max14577 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 max14577_muic_set_debounce_time(struct max14577_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) enum max14577_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) u8 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_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 = max14577_update_reg(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MAX14577_MUIC_REG_CONTROL3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) CTRL3_ADCDBSET_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) time << CTRL3_ADCDBSET_SHIFT);
^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) * max14577_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 max14577 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 max14577 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 max14577_muic_set_path(struct max14577_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) u8 ctrl1, ctrl2 = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* Set open state to path before changing hw path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = max14577_update_reg(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) MAX14577_MUIC_REG_CONTROL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) CLEAR_IDBEN_MICEN_MASK, CTRL1_SW_OPEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_err(info->dev, "failed to update MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) ctrl1 = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ctrl1 = CTRL1_SW_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) ret = max14577_update_reg(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) MAX14577_MUIC_REG_CONTROL1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) CLEAR_IDBEN_MICEN_MASK, ctrl1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_err(info->dev, "failed to update MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ctrl2 |= CTRL2_CPEN_MASK; /* LowPwr=0, CPEn=1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ctrl2 |= CTRL2_LOWPWR_MASK; /* LowPwr=1, CPEn=0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) ret = max14577_update_reg(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) MAX14577_REG_CONTROL2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) CTRL2_LOWPWR_MASK | CTRL2_CPEN_MASK, ctrl2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dev_err(info->dev, "failed to update MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) dev_dbg(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) "CONTROL1 : 0x%02x, CONTROL2 : 0x%02x, state : %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ctrl1, ctrl2, attached ? "attached" : "detached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * max14577_muic_get_cable_type - Return cable type and check cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @info: the instance including private data of max14577 MUIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * @group: the path according to attached cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * @attached: store cable state and return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * This function check the cable state either attached or detached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * and then divide precise type of cable according to cable group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * - max14577_CABLE_GROUP_ADC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * - max14577_CABLE_GROUP_CHG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static int max14577_muic_get_cable_type(struct max14577_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) enum max14577_muic_cable_group group, bool *attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int cable_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) switch (group) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) case MAX14577_CABLE_GROUP_ADC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * Read ADC value to check cable type and decide cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * according to cable type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) adc = info->status[MAX14577_MUIC_STATUS1] & STATUS1_ADC_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) adc >>= STATUS1_ADC_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * Check current cable state/cable type and store cable type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * (info->prev_cable_type) for handling cable when cable is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (adc == MAX14577_MUIC_ADC_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) *attached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cable_type = info->prev_cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) info->prev_cable_type = MAX14577_MUIC_ADC_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *attached = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) cable_type = info->prev_cable_type = adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) case MAX14577_CABLE_GROUP_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * Read charger type to check cable type and decide cable state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * according to type of charger cable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) chg_type = info->status[MAX14577_MUIC_STATUS2] &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) STATUS2_CHGTYP_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) chg_type >>= STATUS2_CHGTYP_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (chg_type == MAX14577_CHARGER_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *attached = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) cable_type = info->prev_chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) info->prev_chg_type = MAX14577_CHARGER_TYPE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) *attached = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * Check current cable state/cable type and store cable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * type(info->prev_chg_type) for handling cable when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * charger cable is detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) cable_type = info->prev_chg_type = chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) dev_err(info->dev, "Unknown cable group (%d)\n", group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) cable_type = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return cable_type;
^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) static int max14577_muic_jig_handler(struct max14577_muic_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int cable_type, bool attached)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) u8 path = CTRL1_SW_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dev_dbg(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) "external connector is %s (adc:0x%02x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) attached ? "attached" : "detached", cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) switch (cable_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF: /* ADC_JIG_USB_OFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON: /* ADC_JIG_USB_ON */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* PATH:AP_USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) path = CTRL1_SW_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF: /* ADC_JIG_UART_OFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* PATH:AP_UART */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) path = CTRL1_SW_UART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) dev_err(info->dev, "failed to detect %s jig cable\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) attached ? "attached" : "detached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ret = max14577_muic_set_path(info, path, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) extcon_set_state_sync(info->edev, EXTCON_JIG, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int max14577_muic_adc_handler(struct max14577_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) int cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* Check accessory state which is either detached or attached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) cable_type = max14577_muic_get_cable_type(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) MAX14577_CABLE_GROUP_ADC, &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) dev_dbg(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) "external connector is %s (adc:0x%02x, prev_adc:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) attached ? "attached" : "detached", cable_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) info->prev_cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) switch (cable_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) case MAX14577_MUIC_ADC_FACTORY_MODE_USB_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) case MAX14577_MUIC_ADC_FACTORY_MODE_USB_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) case MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /* JIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ret = max14577_muic_jig_handler(info, cable_type, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) case MAX14577_MUIC_ADC_GROUND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) case MAX14577_MUIC_ADC_SEND_END_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) case MAX14577_MUIC_ADC_REMOTE_S1_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) case MAX14577_MUIC_ADC_REMOTE_S2_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) case MAX14577_MUIC_ADC_REMOTE_S3_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) case MAX14577_MUIC_ADC_REMOTE_S4_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) case MAX14577_MUIC_ADC_REMOTE_S5_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) case MAX14577_MUIC_ADC_REMOTE_S6_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) case MAX14577_MUIC_ADC_REMOTE_S7_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) case MAX14577_MUIC_ADC_REMOTE_S8_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) case MAX14577_MUIC_ADC_REMOTE_S9_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) case MAX14577_MUIC_ADC_REMOTE_S10_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) case MAX14577_MUIC_ADC_REMOTE_S11_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) case MAX14577_MUIC_ADC_REMOTE_S12_BUTTON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) case MAX14577_MUIC_ADC_RESERVED_ACC_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) case MAX14577_MUIC_ADC_RESERVED_ACC_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) case MAX14577_MUIC_ADC_RESERVED_ACC_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) case MAX14577_MUIC_ADC_RESERVED_ACC_4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) case MAX14577_MUIC_ADC_RESERVED_ACC_5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) case MAX14577_MUIC_ADC_PHONE_POWERED_DEV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) case MAX14577_MUIC_ADC_TTY_CONVERTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) case MAX14577_MUIC_ADC_UART_CABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) case MAX14577_MUIC_ADC_CEA936A_TYPE1_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) case MAX14577_MUIC_ADC_AV_CABLE_NOLOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) case MAX14577_MUIC_ADC_CEA936A_TYPE2_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) case MAX14577_MUIC_ADC_FACTORY_MODE_UART_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) case MAX14577_MUIC_ADC_AUDIO_DEVICE_TYPE1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * This accessory isn't used in general case if it is specially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * needed to detect additional accessory, should implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * proper operation when this accessory is attached/detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) dev_info(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) "accessory is %s but it isn't used (adc:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) attached ? "attached" : "detached", cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) "failed to detect %s accessory (adc:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) attached ? "attached" : "detached", cable_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static int max14577_muic_chg_handler(struct max14577_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) chg_type = max14577_muic_get_cable_type(info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) MAX14577_CABLE_GROUP_CHG, &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) dev_dbg(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) "external connector is %s(chg_type:0x%x, prev_chg_type:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) attached ? "attached" : "detached",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) chg_type, info->prev_chg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) switch (chg_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) case MAX14577_CHARGER_TYPE_USB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) /* PATH:AP_USB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ret = max14577_muic_set_path(info, info->path_usb, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) extcon_set_state_sync(info->edev, EXTCON_USB, attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) case MAX14577_CHARGER_TYPE_DEDICATED_CHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) extcon_set_state_sync(info->edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) case MAX14577_CHARGER_TYPE_DOWNSTREAM_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) extcon_set_state_sync(info->edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) case MAX14577_CHARGER_TYPE_SPECIAL_500MA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) extcon_set_state_sync(info->edev, EXTCON_CHG_USB_SLOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) case MAX14577_CHARGER_TYPE_SPECIAL_1A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) extcon_set_state_sync(info->edev, EXTCON_CHG_USB_FAST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) case MAX14577_CHARGER_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) case MAX14577_CHARGER_TYPE_DEAD_BATTERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) "failed to detect %s accessory (chg_type:0x%x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) attached ? "attached" : "detached", chg_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) static void max14577_muic_irq_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct max14577_muic_info *info = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) struct max14577_muic_info, irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (!info->edev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) ret = max14577_bulk_read(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) MAX14577_MUIC_REG_STATUS1, info->status, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) dev_err(info->dev, "failed to read MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (info->irq_adc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) ret = max14577_muic_adc_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) info->irq_adc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (info->irq_chg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) ret = max14577_muic_chg_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) info->irq_chg = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) dev_err(info->dev, "failed to handle MUIC interrupt\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * Returns 0 if irq_type does not match registered IRQ for this device type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static int max14577_parse_irq(struct max14577_muic_info *info, int irq_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) switch (irq_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) case MAX14577_IRQ_INT1_ADC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) case MAX14577_IRQ_INT1_ADCLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) case MAX14577_IRQ_INT1_ADCERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * Handle all of accessory except for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * type of charger accessory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) info->irq_adc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) case MAX14577_IRQ_INT2_CHGTYP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) case MAX14577_IRQ_INT2_CHGDETRUN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) case MAX14577_IRQ_INT2_DCDTMR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) case MAX14577_IRQ_INT2_DBCHG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) case MAX14577_IRQ_INT2_VBVOLT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* Handle charger accessory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) info->irq_chg = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * Sets irq_adc or irq_chg in max14577_muic_info and returns 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * Returns 0 if irq_type does not match registered IRQ for this device type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int max77836_parse_irq(struct max14577_muic_info *info, int irq_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* First check common max14577 interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (max14577_parse_irq(info, irq_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) switch (irq_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) case MAX77836_IRQ_INT1_ADC1K:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) info->irq_adc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) case MAX77836_IRQ_INT2_VIDRM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /* Handle charger accessory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) info->irq_chg = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static irqreturn_t max14577_muic_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct max14577_muic_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) int i, irq_type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) bool irq_parsed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * We may be called multiple times for different nested IRQ-s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * Including changes in INT1_ADC and INT2_CGHTYP at once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * However we only need to know whether it was ADC, charger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * or both interrupts so decode IRQ and turn on proper flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) for (i = 0; i < info->muic_irqs_num; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (irq == info->muic_irqs[i].virq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) irq_type = info->muic_irqs[i].irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) switch (info->max14577->dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) case MAXIM_DEVICE_TYPE_MAX77836:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) irq_parsed = max77836_parse_irq(info, irq_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) case MAXIM_DEVICE_TYPE_MAX14577:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) irq_parsed = max14577_parse_irq(info, irq_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (!irq_parsed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) dev_err(info->dev, "muic interrupt: irq %d occurred, skipped\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) irq_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) schedule_work(&info->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) static int max14577_muic_detect_accessory(struct max14577_muic_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) int adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) int chg_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* Read STATUSx register to detect accessory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) ret = max14577_bulk_read(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) MAX14577_MUIC_REG_STATUS1, info->status, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) dev_err(info->dev, "failed to read MUIC register\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) adc = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (attached && adc != MAX14577_MUIC_ADC_OPEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) ret = max14577_muic_adc_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) dev_err(info->dev, "Cannot detect accessory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) chg_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_CHG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (attached && chg_type != MAX14577_CHARGER_TYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ret = max14577_muic_chg_handler(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) dev_err(info->dev, "Cannot detect charger accessory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static void max14577_muic_detect_cable_wq(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) struct max14577_muic_info *info = container_of(to_delayed_work(work),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct max14577_muic_info, wq_detcable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) max14577_muic_detect_accessory(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) static int max14577_muic_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) struct max14577 *max14577 = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) struct max14577_muic_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) int delay_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) int cable_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) bool attached;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) u8 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) info->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) info->max14577 = max14577;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) mutex_init(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) INIT_WORK(&info->irq_work, max14577_muic_irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) switch (max14577->dev_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) case MAXIM_DEVICE_TYPE_MAX77836:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) info->muic_irqs = max77836_muic_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) info->muic_irqs_num = ARRAY_SIZE(max77836_muic_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) case MAXIM_DEVICE_TYPE_MAX14577:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) info->muic_irqs = max14577_muic_irqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) info->muic_irqs_num = ARRAY_SIZE(max14577_muic_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* Support irq domain for max14577 MUIC device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) for (i = 0; i < info->muic_irqs_num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) struct max14577_muic_irq *muic_irq = &info->muic_irqs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) int virq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) virq = regmap_irq_get_virq(max14577->irq_data, muic_irq->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (virq <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) muic_irq->virq = virq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ret = devm_request_threaded_irq(&pdev->dev, virq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) max14577_muic_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) IRQF_NO_SUSPEND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) muic_irq->name, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) "failed: irq request (IRQ: %d, error :%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) muic_irq->irq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* Initialize extcon device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) info->edev = devm_extcon_dev_allocate(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) max14577_extcon_cable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (IS_ERR(info->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) return PTR_ERR(info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) ret = devm_extcon_dev_register(&pdev->dev, info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) dev_err(&pdev->dev, "failed to register extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /* Default h/w line path */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) info->path_usb = CTRL1_SW_USB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) info->path_uart = CTRL1_SW_UART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) delay_jiffies = msecs_to_jiffies(DELAY_MS_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) /* Set initial path for UART when JIG is connected to get serial logs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) ret = max14577_bulk_read(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) MAX14577_MUIC_REG_STATUS1, info->status, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) dev_err(info->dev, "Cannot read STATUS registers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) cable_type = max14577_muic_get_cable_type(info, MAX14577_CABLE_GROUP_ADC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) &attached);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (attached && cable_type == MAX14577_MUIC_ADC_FACTORY_MODE_UART_OFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) max14577_muic_set_path(info, info->path_uart, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) /* Check revision number of MUIC device*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) ret = max14577_read_reg(info->max14577->regmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) MAX14577_REG_DEVICEID, &id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) dev_err(&pdev->dev, "failed to read revision number\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) dev_info(info->dev, "device ID : 0x%x\n", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) /* Set ADC debounce time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) max14577_muic_set_debounce_time(info, ADC_DEBOUNCE_TIME_25MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) * Detect accessory after completing the initialization of platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * - Use delayed workqueue to detect cable state and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * notify cable state to notifiee/platform through uevent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * After completing the booting of platform, the extcon provider
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * driver should notify cable state to upper layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) INIT_DELAYED_WORK(&info->wq_detcable, max14577_muic_detect_cable_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) queue_delayed_work(system_power_efficient_wq, &info->wq_detcable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) delay_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static int max14577_muic_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct max14577_muic_info *info = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) cancel_work_sync(&info->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) static const struct platform_device_id max14577_muic_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) { "max14577-muic", MAXIM_DEVICE_TYPE_MAX14577, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) { "max77836-muic", MAXIM_DEVICE_TYPE_MAX77836, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) MODULE_DEVICE_TABLE(platform, max14577_muic_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) static const struct of_device_id of_max14577_muic_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) { .compatible = "maxim,max14577-muic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) .data = (void *)MAXIM_DEVICE_TYPE_MAX14577, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) { .compatible = "maxim,max77836-muic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) .data = (void *)MAXIM_DEVICE_TYPE_MAX77836, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) MODULE_DEVICE_TABLE(of, of_max14577_muic_dt_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) static struct platform_driver max14577_muic_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) .name = "max14577-muic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) .of_match_table = of_max14577_muic_dt_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) .probe = max14577_muic_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) .remove = max14577_muic_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) .id_table = max14577_muic_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) module_platform_driver(max14577_muic_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) MODULE_DESCRIPTION("Maxim 14577/77836 Extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <krzk@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) MODULE_ALIAS("platform:extcon-max14577");