^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) // ChromeOS Embedded Controller extcon
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) // Copyright (C) 2017 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Author: Benson Leung <bleung@chromium.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/extcon-provider.h>
^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/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_data/cros_ec_commands.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/platform_data/cros_ec_proto.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/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct cros_ec_extcon_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct cros_ec_device *ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct notifier_block notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned int dr; /* data role */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) bool pr; /* power role (true if VBUS enabled) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bool dp; /* DisplayPort enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bool mux; /* SuperSpeed (usb3) enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int power_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const unsigned int usb_type_c_cable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) EXTCON_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) enum usb_data_roles {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) DR_NONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) DR_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) DR_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) };
^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) * cros_ec_pd_command() - Send a command to the EC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @info: pointer to struct cros_ec_extcon_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @command: EC command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @version: EC command version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @outdata: EC command output data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @outsize: Size of outdata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @indata: EC command input data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @insize: Size of indata
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Return: 0 on success, <0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static int cros_ec_pd_command(struct cros_ec_extcon_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned int command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned int version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void *outdata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int outsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) void *indata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) unsigned int insize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct cros_ec_command *msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) msg = kzalloc(sizeof(*msg) + max(outsize, insize), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) msg->version = version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) msg->command = command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) msg->outsize = outsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) msg->insize = insize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (outsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) memcpy(msg->data, outdata, outsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ret = cros_ec_cmd_xfer_status(info->ec, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ret >= 0 && insize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) memcpy(indata, msg->data, insize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) kfree(msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^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) * cros_ec_usb_get_power_type() - Get power type info about PD device attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * to given port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @info: pointer to struct cros_ec_extcon_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Return: power type on success, <0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int cros_ec_usb_get_power_type(struct cros_ec_extcon_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct ec_params_usb_pd_power_info req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct ec_response_usb_pd_power_info resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) req.port = info->port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ret = cros_ec_pd_command(info, EC_CMD_USB_PD_POWER_INFO, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) &req, sizeof(req), &resp, sizeof(resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return resp.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * cros_ec_usb_get_pd_mux_state() - Get PD mux state for given port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @info: pointer to struct cros_ec_extcon_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Return: PD mux state on success, <0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int cros_ec_usb_get_pd_mux_state(struct cros_ec_extcon_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct ec_params_usb_pd_mux_info req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct ec_response_usb_pd_mux_info resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) req.port = info->port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = cros_ec_pd_command(info, EC_CMD_USB_PD_MUX_INFO, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) &resp, sizeof(resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return resp.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * cros_ec_usb_get_role() - Get role info about possible PD device attached to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * given port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * @info: pointer to struct cros_ec_extcon_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * @polarity: pointer to cable polarity (return value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * Return: role info on success, -ENOTCONN if no cable is connected, <0 on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static int cros_ec_usb_get_role(struct cros_ec_extcon_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bool *polarity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct ec_params_usb_pd_control pd_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct ec_response_usb_pd_control_v1 resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pd_control.port = info->port_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pd_control.role = USB_PD_CTRL_ROLE_NO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) pd_control.mux = USB_PD_CTRL_MUX_NO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) pd_control.swap = USB_PD_CTRL_SWAP_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ret = cros_ec_pd_command(info, EC_CMD_USB_PD_CONTROL, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) &pd_control, sizeof(pd_control),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) &resp, sizeof(resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!(resp.enabled & PD_CTRL_RESP_ENABLED_CONNECTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) *polarity = resp.polarity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return resp.role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * cros_ec_pd_get_num_ports() - Get number of EC charge ports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) * @info: pointer to struct cros_ec_extcon_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Return: number of ports on success, <0 on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int cros_ec_pd_get_num_ports(struct cros_ec_extcon_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct ec_response_usb_pd_ports resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = cros_ec_pd_command(info, EC_CMD_USB_PD_PORTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 0, NULL, 0, &resp, sizeof(resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return resp.num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const char *cros_ec_usb_role_string(unsigned int role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return role == DR_NONE ? "DISCONNECTED" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) (role == DR_HOST ? "DFP" : "UFP");
^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) static const char *cros_ec_usb_power_type_string(unsigned int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) case USB_CHG_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return "USB_CHG_TYPE_NONE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case USB_CHG_TYPE_PD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return "USB_CHG_TYPE_PD";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) case USB_CHG_TYPE_PROPRIETARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return "USB_CHG_TYPE_PROPRIETARY";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) case USB_CHG_TYPE_C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return "USB_CHG_TYPE_C";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) case USB_CHG_TYPE_BC12_DCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return "USB_CHG_TYPE_BC12_DCP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) case USB_CHG_TYPE_BC12_CDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return "USB_CHG_TYPE_BC12_CDP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) case USB_CHG_TYPE_BC12_SDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return "USB_CHG_TYPE_BC12_SDP";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) case USB_CHG_TYPE_OTHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return "USB_CHG_TYPE_OTHER";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) case USB_CHG_TYPE_VBUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return "USB_CHG_TYPE_VBUS";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) case USB_CHG_TYPE_UNKNOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return "USB_CHG_TYPE_UNKNOWN";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return "USB_CHG_TYPE_UNKNOWN";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^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) static bool cros_ec_usb_power_type_is_wall_wart(unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) unsigned int role)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* FIXME : Guppy, Donnettes, and other chargers will be miscategorized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * because they identify with USB_CHG_TYPE_C, but we can't return true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * here from that code because that breaks Suzy-Q and other kinds of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * USB Type-C cables and peripherals.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) case USB_CHG_TYPE_PROPRIETARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) case USB_CHG_TYPE_BC12_DCP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) case USB_CHG_TYPE_PD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) case USB_CHG_TYPE_C:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case USB_CHG_TYPE_BC12_CDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) case USB_CHG_TYPE_BC12_SDP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) case USB_CHG_TYPE_OTHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) case USB_CHG_TYPE_VBUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) case USB_CHG_TYPE_UNKNOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) case USB_CHG_TYPE_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int extcon_cros_ec_detect_cable(struct cros_ec_extcon_info *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) bool force)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct device *dev = info->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int role, power_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) unsigned int dr = DR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) bool pr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) bool polarity = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) bool dp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) bool mux = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) bool hpd = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) power_type = cros_ec_usb_get_power_type(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (power_type < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) dev_err(dev, "failed getting power type err = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) power_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return power_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) role = cros_ec_usb_get_role(info, &polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) if (role < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (role != -ENOTCONN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dev_err(dev, "failed getting role err = %d\n", role);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return role;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) dev_dbg(dev, "disconnected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) int pd_mux_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dr = (role & PD_CTRL_RESP_ROLE_DATA) ? DR_HOST : DR_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pr = (role & PD_CTRL_RESP_ROLE_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) pd_mux_state = cros_ec_usb_get_pd_mux_state(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (pd_mux_state < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) pd_mux_state = USB_PD_MUX_USB_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) dp = pd_mux_state & USB_PD_MUX_DP_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) mux = pd_mux_state & USB_PD_MUX_USB_ENABLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) hpd = pd_mux_state & USB_PD_MUX_HPD_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) dev_dbg(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) "connected role 0x%x pwr type %d dr %d pr %d pol %d mux %d dp %d hpd %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) role, power_type, dr, pr, polarity, mux, dp, hpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^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) * When there is no USB host (e.g. USB PD charger),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * we are not really a UFP for the AP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (dr == DR_DEVICE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) cros_ec_usb_power_type_is_wall_wart(power_type, role))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) dr = DR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (force || info->dr != dr || info->pr != pr || info->dp != dp ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) info->mux != mux || info->power_type != power_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) bool host_connected = false, device_connected = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) dev_dbg(dev, "Type/Role switch! type = %s role = %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) cros_ec_usb_power_type_string(power_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) cros_ec_usb_role_string(dr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) info->dr = dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) info->pr = pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) info->dp = dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) info->mux = mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) info->power_type = power_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (dr == DR_DEVICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) device_connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) else if (dr == DR_HOST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) host_connected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) extcon_set_state(info->edev, EXTCON_USB, device_connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) extcon_set_state(info->edev, EXTCON_USB_HOST, host_connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) extcon_set_state(info->edev, EXTCON_DISP_DP, dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) extcon_set_property(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) EXTCON_PROP_USB_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) (union extcon_property_value)(int)pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) extcon_set_property(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) EXTCON_PROP_USB_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) (union extcon_property_value)(int)pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) extcon_set_property(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) EXTCON_PROP_USB_TYPEC_POLARITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) (union extcon_property_value)(int)polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) extcon_set_property(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) EXTCON_PROP_USB_TYPEC_POLARITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) (union extcon_property_value)(int)polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) extcon_set_property(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) EXTCON_PROP_USB_TYPEC_POLARITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) (union extcon_property_value)(int)polarity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) extcon_set_property(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) EXTCON_PROP_USB_SS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) (union extcon_property_value)(int)mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) extcon_set_property(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) EXTCON_PROP_USB_SS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) (union extcon_property_value)(int)mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) extcon_set_property(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) EXTCON_PROP_USB_SS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) (union extcon_property_value)(int)mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) extcon_set_property(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) EXTCON_PROP_DISP_HPD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) (union extcon_property_value)(int)hpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) extcon_sync(info->edev, EXTCON_USB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) extcon_sync(info->edev, EXTCON_USB_HOST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) extcon_sync(info->edev, EXTCON_DISP_DP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) } else if (hpd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) extcon_set_property(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) EXTCON_PROP_DISP_HPD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) (union extcon_property_value)(int)hpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) extcon_sync(info->edev, EXTCON_DISP_DP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static int extcon_cros_ec_event(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) unsigned long queued_during_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) void *_notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) struct cros_ec_extcon_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct cros_ec_device *ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) u32 host_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) info = container_of(nb, struct cros_ec_extcon_info, notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ec = info->ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) host_event = cros_ec_get_host_event(ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (host_event & (EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) EC_HOST_EVENT_MASK(EC_HOST_EVENT_USB_MUX))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) extcon_cros_ec_detect_cable(info, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return NOTIFY_OK;
^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) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static int extcon_cros_ec_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) struct cros_ec_extcon_info *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct device_node *np = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) int numports, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) info->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) info->ec = ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) u32 port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) ret = of_property_read_u32(np, "google,usb-port-id", &port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) dev_err(dev, "Missing google,usb-port-id property\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) info->port_id = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) info->port_id = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) numports = cros_ec_pd_get_num_ports(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (numports < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) dev_err(dev, "failed getting number of ports! ret = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) numports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return numports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (info->port_id >= numports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) dev_err(dev, "This system only supports %d ports\n", numports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) return -ENODEV;
^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) info->edev = devm_extcon_dev_allocate(dev, usb_type_c_cable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (IS_ERR(info->edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_err(dev, "failed to allocate extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) ret = devm_extcon_dev_register(dev, info->edev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) dev_err(dev, "failed to register extcon device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) extcon_set_property_capability(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) EXTCON_PROP_USB_VBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) EXTCON_PROP_USB_VBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) extcon_set_property_capability(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) EXTCON_PROP_USB_TYPEC_POLARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) EXTCON_PROP_USB_TYPEC_POLARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) extcon_set_property_capability(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) EXTCON_PROP_USB_TYPEC_POLARITY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) extcon_set_property_capability(info->edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) EXTCON_PROP_USB_SS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) extcon_set_property_capability(info->edev, EXTCON_USB_HOST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) EXTCON_PROP_USB_SS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) extcon_set_property_capability(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) EXTCON_PROP_USB_SS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) extcon_set_property_capability(info->edev, EXTCON_DISP_DP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) EXTCON_PROP_DISP_HPD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) info->dr = DR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) info->pr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) platform_set_drvdata(pdev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /* Get PD events from the EC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) info->notifier.notifier_call = extcon_cros_ec_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) ret = blocking_notifier_chain_register(&info->ec->event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) &info->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) dev_err(dev, "failed to register notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /* Perform initial detection */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) ret = extcon_cros_ec_detect_cable(info, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) dev_err(dev, "failed to detect initial cable state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) goto unregister_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) unregister_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) blocking_notifier_chain_unregister(&info->ec->event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) &info->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static int extcon_cros_ec_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct cros_ec_extcon_info *info = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) blocking_notifier_chain_unregister(&info->ec->event_notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) &info->notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) static int extcon_cros_ec_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) return 0;
^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) static int extcon_cros_ec_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) struct cros_ec_extcon_info *info = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) ret = extcon_cros_ec_detect_cable(info, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) dev_err(dev, "failed to detect cable state on resume\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static const struct dev_pm_ops extcon_cros_ec_dev_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) SET_SYSTEM_SLEEP_PM_OPS(extcon_cros_ec_suspend, extcon_cros_ec_resume)
^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) #define DEV_PM_OPS (&extcon_cros_ec_dev_pm_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) #define DEV_PM_OPS NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) #endif /* CONFIG_PM_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static const struct of_device_id extcon_cros_ec_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) { .compatible = "google,extcon-usbc-cros-ec" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) MODULE_DEVICE_TABLE(of, extcon_cros_ec_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) #endif /* CONFIG_OF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) static struct platform_driver extcon_cros_ec_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) .name = "extcon-usbc-cros-ec",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) .of_match_table = of_match_ptr(extcon_cros_ec_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) .pm = DEV_PM_OPS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) .remove = extcon_cros_ec_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) .probe = extcon_cros_ec_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) module_platform_driver(extcon_cros_ec_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) MODULE_DESCRIPTION("ChromeOS Embedded Controller extcon driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) MODULE_AUTHOR("Benson Leung <bleung@chromium.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) MODULE_LICENSE("GPL v2");