^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-ptn5150.c - PTN5150 CC logic extcon driver to support USB detection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Based on extcon-sm5502.c driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) // Copyright (c) 2018-2019 by Vijai Kumar K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Author: Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // Copyright (c) 2020 Krzysztof Kozlowski <krzk@kernel.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/bitfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.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/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* PTN5150 registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define PTN5150_REG_DEVICE_ID 0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define PTN5150_REG_CONTROL 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define PTN5150_REG_INT_STATUS 0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define PTN5150_REG_CC_STATUS 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define PTN5150_REG_CON_DET 0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define PTN5150_REG_VCONN_STATUS 0x0a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define PTN5150_REG_RESET 0x0b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define PTN5150_REG_INT_MASK 0x18
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define PTN5150_REG_INT_REG_STATUS 0x19
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define PTN5150_REG_END PTN5150_REG_INT_REG_STATUS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define PTN5150_DFP_ATTACHED 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define PTN5150_UFP_ATTACHED 0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* Define PTN5150 MASK/SHIFT constant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define PTN5150_REG_DEVICE_ID_VERSION GENMASK(7, 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define PTN5150_REG_DEVICE_ID_VENDOR GENMASK(2, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define PTN5150_REG_CC_PORT_ATTACHMENT GENMASK(4, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define PTN5150_REG_CC_VBUS_DETECTION BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define PTN5150_REG_INT_CABLE_ATTACH_MASK BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define PTN5150_REG_INT_CABLE_DETACH_MASK BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct ptn5150_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct i2c_client *i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct gpio_desc *int_gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct gpio_desc *vbus_gpiod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct work_struct irq_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* List of detectable cables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const unsigned int ptn5150_extcon_cable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) EXTCON_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static const struct regmap_config ptn5150_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .reg_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .max_register = PTN5150_REG_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static void ptn5150_check_state(struct ptn5150_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned int port_status, reg_data, vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) ret = regmap_read(info->regmap, PTN5150_REG_CC_STATUS, ®_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dev_err(info->dev, "failed to read CC STATUS %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) port_status = FIELD_GET(PTN5150_REG_CC_PORT_ATTACHMENT, reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) switch (port_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case PTN5150_DFP_ATTACHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) extcon_set_state_sync(info->edev, EXTCON_USB_HOST, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) gpiod_set_value_cansleep(info->vbus_gpiod, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) extcon_set_state_sync(info->edev, EXTCON_USB, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case PTN5150_UFP_ATTACHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) extcon_set_state_sync(info->edev, EXTCON_USB, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) vbus = FIELD_GET(PTN5150_REG_CC_VBUS_DETECTION, reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (vbus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) gpiod_set_value_cansleep(info->vbus_gpiod, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) gpiod_set_value_cansleep(info->vbus_gpiod, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) extcon_set_state_sync(info->edev, EXTCON_USB_HOST, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static void ptn5150_irq_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct ptn5150_info *info = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct ptn5150_info, irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned int int_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!info->edev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Clear interrupt. Read would clear the register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, &int_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_err(info->dev, "failed to read INT STATUS %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (int_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) unsigned int cable_attach;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) cable_attach = int_status & PTN5150_REG_INT_CABLE_ATTACH_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (cable_attach) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ptn5150_check_state(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) extcon_set_state_sync(info->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) EXTCON_USB_HOST, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) extcon_set_state_sync(info->edev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) EXTCON_USB, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) gpiod_set_value_cansleep(info->vbus_gpiod, 0);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Clear interrupt. Read would clear the register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) &int_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) "failed to read INT REG STATUS %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_unlock(&info->mutex);
^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) static irqreturn_t ptn5150_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct ptn5150_info *info = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) schedule_work(&info->irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int ptn5150_init_dev_type(struct ptn5150_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned int reg_data, vendor_id, version_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ret = regmap_read(info->regmap, PTN5150_REG_DEVICE_ID, ®_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_err(info->dev, "failed to read DEVICE_ID %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) vendor_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VENDOR, reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) version_id = FIELD_GET(PTN5150_REG_DEVICE_ID_VERSION, reg_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dev_dbg(info->dev, "Device type: version: 0x%x, vendor: 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) version_id, vendor_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /* Clear any existing interrupts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ret = regmap_read(info->regmap, PTN5150_REG_INT_STATUS, ®_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) "failed to read PTN5150_REG_INT_STATUS %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = regmap_read(info->regmap, PTN5150_REG_INT_REG_STATUS, ®_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_err(info->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) "failed to read PTN5150_REG_INT_REG_STATUS %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int ptn5150_i2c_probe(struct i2c_client *i2c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct device *dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct device_node *np = i2c->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct ptn5150_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) info = devm_kzalloc(&i2c->dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) i2c_set_clientdata(i2c, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) info->dev = &i2c->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) info->i2c = i2c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) info->vbus_gpiod = devm_gpiod_get(&i2c->dev, "vbus", GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (IS_ERR(info->vbus_gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ret = PTR_ERR(info->vbus_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ret == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_info(dev, "No VBUS GPIO, ignoring VBUS control\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) info->vbus_gpiod = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return dev_err_probe(dev, ret, "failed to get VBUS GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^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) mutex_init(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) INIT_WORK(&info->irq_work, ptn5150_irq_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) info->regmap = devm_regmap_init_i2c(i2c, &ptn5150_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (IS_ERR(info->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return dev_err_probe(info->dev, PTR_ERR(info->regmap),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) "failed to allocate register map\n");
^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) if (i2c->irq > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) info->irq = i2c->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) info->int_gpiod = devm_gpiod_get(&i2c->dev, "int", GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (IS_ERR(info->int_gpiod)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return dev_err_probe(dev, PTR_ERR(info->int_gpiod),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) "failed to get INT GPIO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) info->irq = gpiod_to_irq(info->int_gpiod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (info->irq < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_err(dev, "failed to get INTB IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return info->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) ret = devm_request_threaded_irq(dev, info->irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) ptn5150_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) IRQF_TRIGGER_FALLING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) i2c->name, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) dev_err(dev, "failed to request handler for INTB IRQ\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /* Allocate extcon device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) info->edev = devm_extcon_dev_allocate(info->dev, ptn5150_extcon_cable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (IS_ERR(info->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) dev_err(info->dev, "failed to allocate memory for extcon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Register extcon device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) ret = devm_extcon_dev_register(info->dev, info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) dev_err(info->dev, "failed to register extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) extcon_set_property_capability(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) EXTCON_PROP_USB_VBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) EXTCON_PROP_USB_VBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) EXTCON_PROP_USB_TYPEC_POLARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /* Initialize PTN5150 device and print vendor id and version id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ret = ptn5150_init_dev_type(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Update current extcon state if for example OTG connection was there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * before the probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) mutex_lock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ptn5150_check_state(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) mutex_unlock(&info->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static const struct of_device_id ptn5150_dt_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) { .compatible = "nxp,ptn5150" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) MODULE_DEVICE_TABLE(of, ptn5150_dt_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static const struct i2c_device_id ptn5150_i2c_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) { "ptn5150", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) MODULE_DEVICE_TABLE(i2c, ptn5150_i2c_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static struct i2c_driver ptn5150_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .name = "ptn5150",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) .of_match_table = ptn5150_dt_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .probe_new = ptn5150_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .id_table = ptn5150_i2c_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) module_i2c_driver(ptn5150_i2c_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) MODULE_DESCRIPTION("NXP PTN5150 CC logic Extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) MODULE_AUTHOR("Vijai Kumar K <vijaikumar.kanagarajan@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) MODULE_AUTHOR("Krzysztof Kozlowski <krzk@kernel.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) MODULE_LICENSE("GPL v2");