blob: f68f413dc60c9fc18874b6cbc2b76d8eebe0f6d4 [file] [log] [blame]
Jack Phamf4baeb12017-02-03 19:01:48 -08001/* Copyright (c) 2016-2017, Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/completion.h>
14#include <linux/delay.h>
15#include <linux/hrtimer.h>
16#include <linux/ipc_logging.h>
17#include <linux/kernel.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_platform.h>
22#include <linux/power_supply.h>
23#include <linux/regulator/consumer.h>
24#include <linux/slab.h>
25#include <linux/spinlock.h>
26#include <linux/workqueue.h>
27#include <linux/extcon.h>
28#include <linux/usb/class-dual-role.h>
29#include <linux/usb/usbpd.h>
30#include "usbpd.h"
31
Vijayavardhan Vennapusa34a1f1d2017-05-31 12:00:37 +053032/* To start USB stack for USB3.1 complaince testing */
33static bool usb_compliance_mode;
34module_param(usb_compliance_mode, bool, 0644);
35MODULE_PARM_DESC(usb_compliance_mode, "Start USB stack for USB3.1 compliance testing");
36
Vijayavardhan Vennapusa49f21ce2017-06-05 14:47:56 +053037static bool disable_usb_pd;
38module_param(disable_usb_pd, bool, 0644);
39MODULE_PARM_DESC(disable_usb_pd, "Disable USB PD for USB3.1 compliance testing");
40
Hemant Kumarbe746222017-07-20 13:51:49 -070041static bool rev3_sink_only;
42module_param(rev3_sink_only, bool, 0644);
43MODULE_PARM_DESC(rev3_sink_only, "Enable power delivery rev3.0 sink only mode");
44
Jack Phamf4baeb12017-02-03 19:01:48 -080045enum usbpd_state {
46 PE_UNKNOWN,
47 PE_ERROR_RECOVERY,
48 PE_SRC_DISABLED,
49 PE_SRC_STARTUP,
50 PE_SRC_SEND_CAPABILITIES,
51 PE_SRC_SEND_CAPABILITIES_WAIT, /* substate to wait for Request */
52 PE_SRC_NEGOTIATE_CAPABILITY,
53 PE_SRC_TRANSITION_SUPPLY,
54 PE_SRC_READY,
55 PE_SRC_HARD_RESET,
56 PE_SRC_SOFT_RESET,
57 PE_SRC_SEND_SOFT_RESET,
58 PE_SRC_DISCOVERY,
59 PE_SRC_TRANSITION_TO_DEFAULT,
60 PE_SNK_STARTUP,
61 PE_SNK_DISCOVERY,
62 PE_SNK_WAIT_FOR_CAPABILITIES,
63 PE_SNK_EVALUATE_CAPABILITY,
64 PE_SNK_SELECT_CAPABILITY,
65 PE_SNK_TRANSITION_SINK,
66 PE_SNK_READY,
67 PE_SNK_HARD_RESET,
68 PE_SNK_SOFT_RESET,
69 PE_SNK_SEND_SOFT_RESET,
70 PE_SNK_TRANSITION_TO_DEFAULT,
71 PE_DRS_SEND_DR_SWAP,
72 PE_PRS_SNK_SRC_SEND_SWAP,
73 PE_PRS_SNK_SRC_TRANSITION_TO_OFF,
74 PE_PRS_SNK_SRC_SOURCE_ON,
75 PE_PRS_SRC_SNK_SEND_SWAP,
76 PE_PRS_SRC_SNK_TRANSITION_TO_OFF,
77 PE_PRS_SRC_SNK_WAIT_SOURCE_ON,
78 PE_VCS_WAIT_FOR_VCONN,
79};
80
81static const char * const usbpd_state_strings[] = {
82 "UNKNOWN",
83 "ERROR_RECOVERY",
84 "SRC_Disabled",
85 "SRC_Startup",
86 "SRC_Send_Capabilities",
87 "SRC_Send_Capabilities (Wait for Request)",
88 "SRC_Negotiate_Capability",
89 "SRC_Transition_Supply",
90 "SRC_Ready",
91 "SRC_Hard_Reset",
92 "SRC_Soft_Reset",
93 "SRC_Send_Soft_Reset",
94 "SRC_Discovery",
95 "SRC_Transition_to_default",
96 "SNK_Startup",
97 "SNK_Discovery",
98 "SNK_Wait_for_Capabilities",
99 "SNK_Evaluate_Capability",
100 "SNK_Select_Capability",
101 "SNK_Transition_Sink",
102 "SNK_Ready",
103 "SNK_Hard_Reset",
104 "SNK_Soft_Reset",
105 "SNK_Send_Soft_Reset",
106 "SNK_Transition_to_default",
107 "DRS_Send_DR_Swap",
108 "PRS_SNK_SRC_Send_Swap",
109 "PRS_SNK_SRC_Transition_to_off",
110 "PRS_SNK_SRC_Source_on",
111 "PRS_SRC_SNK_Send_Swap",
112 "PRS_SRC_SNK_Transition_to_off",
113 "PRS_SRC_SNK_Wait_Source_on",
114 "VCS_Wait_for_VCONN",
115};
116
117enum usbpd_control_msg_type {
118 MSG_RESERVED = 0,
119 MSG_GOODCRC,
120 MSG_GOTOMIN,
121 MSG_ACCEPT,
122 MSG_REJECT,
123 MSG_PING,
124 MSG_PS_RDY,
125 MSG_GET_SOURCE_CAP,
126 MSG_GET_SINK_CAP,
127 MSG_DR_SWAP,
128 MSG_PR_SWAP,
129 MSG_VCONN_SWAP,
130 MSG_WAIT,
131 MSG_SOFT_RESET,
Jack Phamf3c1bd32017-08-02 18:32:23 -0700132 MSG_NOT_SUPPORTED = 0x10,
133 MSG_GET_SOURCE_CAP_EXTENDED,
134 MSG_GET_STATUS,
135 MSG_FR_SWAP,
136 MSG_GET_PPS_STATUS,
137 MSG_GET_COUNTRY_CODES,
Jack Phamf4baeb12017-02-03 19:01:48 -0800138};
139
140enum usbpd_data_msg_type {
141 MSG_SOURCE_CAPABILITIES = 1,
142 MSG_REQUEST,
143 MSG_BIST,
144 MSG_SINK_CAPABILITIES,
Jack Phamf3c1bd32017-08-02 18:32:23 -0700145 MSG_BATTERY_STATUS,
146 MSG_ALERT,
147 MSG_GET_COUNTRY_INFO,
Jack Phamf4baeb12017-02-03 19:01:48 -0800148 MSG_VDM = 0xF,
149};
150
Jack Phamf3c1bd32017-08-02 18:32:23 -0700151enum usbpd_ext_msg_type {
152 MSG_SOURCE_CAPABILITIES_EXTENDED = 1,
153 MSG_STATUS,
154 MSG_GET_BATTERY_CAP,
155 MSG_GET_BATTERY_STATUS,
156 MSG_BATTERY_CAPABILITIES,
157 MSG_GET_MANUFACTURER_INFO,
158 MSG_MANUFACTURER_INFO,
159 MSG_SECURITY_REQUEST,
160 MSG_SECURITY_RESPONSE,
161 MSG_FIRMWARE_UPDATE_REQUEST,
162 MSG_FIRMWARE_UPDATE_RESPONSE,
163 MSG_PPS_STATUS,
164 MSG_COUNTRY_INFO,
165 MSG_COUNTRY_CODES,
166};
167
Jack Phamf4baeb12017-02-03 19:01:48 -0800168enum vdm_state {
169 VDM_NONE,
170 DISCOVERED_ID,
171 DISCOVERED_SVIDS,
172 DISCOVERED_MODES,
173 MODE_ENTERED,
174 MODE_EXITED,
175};
176
177static void *usbpd_ipc_log;
178#define usbpd_dbg(dev, fmt, ...) do { \
179 ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \
180 ##__VA_ARGS__); \
181 dev_dbg(dev, fmt, ##__VA_ARGS__); \
182 } while (0)
183
184#define usbpd_info(dev, fmt, ...) do { \
185 ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \
186 ##__VA_ARGS__); \
187 dev_info(dev, fmt, ##__VA_ARGS__); \
188 } while (0)
189
190#define usbpd_warn(dev, fmt, ...) do { \
191 ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \
192 ##__VA_ARGS__); \
193 dev_warn(dev, fmt, ##__VA_ARGS__); \
194 } while (0)
195
196#define usbpd_err(dev, fmt, ...) do { \
197 ipc_log_string(usbpd_ipc_log, "%s: %s: " fmt, dev_name(dev), __func__, \
198 ##__VA_ARGS__); \
199 dev_err(dev, fmt, ##__VA_ARGS__); \
200 } while (0)
201
202#define NUM_LOG_PAGES 10
203
204/* Timeouts (in ms) */
205#define ERROR_RECOVERY_TIME 25
206#define SENDER_RESPONSE_TIME 26
207#define SINK_WAIT_CAP_TIME 500
208#define PS_TRANSITION_TIME 450
209#define SRC_CAP_TIME 120
210#define SRC_TRANSITION_TIME 25
211#define SRC_RECOVER_TIME 750
212#define PS_HARD_RESET_TIME 25
213#define PS_SOURCE_ON 400
214#define PS_SOURCE_OFF 750
Jack Pham35121342017-07-06 00:12:15 -0700215#define FIRST_SOURCE_CAP_TIME 200
Jack Phamf4baeb12017-02-03 19:01:48 -0800216#define VDM_BUSY_TIME 50
217#define VCONN_ON_TIME 100
218
219/* tPSHardReset + tSafe0V */
220#define SNK_HARD_RESET_VBUS_OFF_TIME (35 + 650)
221
222/* tSrcRecover + tSrcTurnOn */
223#define SNK_HARD_RESET_VBUS_ON_TIME (1000 + 275)
224
225#define PD_CAPS_COUNT 50
226
227#define PD_MAX_MSG_ID 7
228
Hemant Kumar796534e2017-05-30 15:54:55 -0700229#define PD_MAX_DATA_OBJ 7
230
Hemant Kumar018b5982017-08-09 14:14:37 -0700231#define PD_SRC_CAP_EXT_DB_LEN 24
Hemant Kumara1875942017-08-09 16:50:14 -0700232#define PD_STATUS_DB_LEN 5
Hemant Kumar51ded972017-08-09 17:57:24 -0700233#define PD_BATTERY_CAP_DB_LEN 9
Hemant Kumar018b5982017-08-09 14:14:37 -0700234
Jack Phamf3c1bd32017-08-02 18:32:23 -0700235#define PD_MAX_EXT_MSG_LEN 260
236#define PD_MAX_EXT_MSG_LEGACY_LEN 26
237
Jack Phamf4baeb12017-02-03 19:01:48 -0800238#define PD_MSG_HDR(type, dr, pr, id, cnt, rev) \
Jack Phamf3c1bd32017-08-02 18:32:23 -0700239 (((type) & 0x1F) | ((dr) << 5) | (rev << 6) | \
Jack Phamf4baeb12017-02-03 19:01:48 -0800240 ((pr) << 8) | ((id) << 9) | ((cnt) << 12))
Jack Phamf3c1bd32017-08-02 18:32:23 -0700241#define PD_MSG_HDR_COUNT(hdr) (((hdr) >> 12) & 7)
242#define PD_MSG_HDR_TYPE(hdr) ((hdr) & 0x1F)
243#define PD_MSG_HDR_ID(hdr) (((hdr) >> 9) & 7)
244#define PD_MSG_HDR_REV(hdr) (((hdr) >> 6) & 3)
245#define PD_MSG_HDR_EXTENDED BIT(15)
246#define PD_MSG_HDR_IS_EXTENDED(hdr) ((hdr) & PD_MSG_HDR_EXTENDED)
247
248#define PD_MSG_EXT_HDR(chunked, num, req, size) \
249 (((chunked) << 15) | (((num) & 0xF) << 11) | \
250 ((req) << 10) | ((size) & 0x1FF))
251#define PD_MSG_EXT_HDR_IS_CHUNKED(ehdr) ((ehdr) & 0x8000)
252#define PD_MSG_EXT_HDR_CHUNK_NUM(ehdr) (((ehdr) >> 11) & 0xF)
253#define PD_MSG_EXT_HDR_REQ_CHUNK(ehdr) ((ehdr) & 0x400)
254#define PD_MSG_EXT_HDR_DATA_SIZE(ehdr) ((ehdr) & 0x1FF)
Jack Phamf4baeb12017-02-03 19:01:48 -0800255
256#define PD_RDO_FIXED(obj, gb, mismatch, usb_comm, no_usb_susp, curr1, curr2) \
257 (((obj) << 28) | ((gb) << 27) | ((mismatch) << 26) | \
258 ((usb_comm) << 25) | ((no_usb_susp) << 24) | \
259 ((curr1) << 10) | (curr2))
260
261#define PD_RDO_AUGMENTED(obj, mismatch, usb_comm, no_usb_susp, volt, curr) \
262 (((obj) << 28) | ((mismatch) << 26) | ((usb_comm) << 25) | \
263 ((no_usb_susp) << 24) | ((volt) << 9) | (curr))
264
265#define PD_RDO_OBJ_POS(rdo) ((rdo) >> 28 & 7)
266#define PD_RDO_GIVEBACK(rdo) ((rdo) >> 27 & 1)
267#define PD_RDO_MISMATCH(rdo) ((rdo) >> 26 & 1)
268#define PD_RDO_USB_COMM(rdo) ((rdo) >> 25 & 1)
269#define PD_RDO_NO_USB_SUSP(rdo) ((rdo) >> 24 & 1)
270#define PD_RDO_FIXED_CURR(rdo) ((rdo) >> 10 & 0x3FF)
271#define PD_RDO_FIXED_CURR_MINMAX(rdo) ((rdo) & 0x3FF)
272#define PD_RDO_PROG_VOLTAGE(rdo) ((rdo) >> 9 & 0x7FF)
273#define PD_RDO_PROG_CURR(rdo) ((rdo) & 0x7F)
274
275#define PD_SRC_PDO_TYPE(pdo) (((pdo) >> 30) & 3)
276#define PD_SRC_PDO_TYPE_FIXED 0
277#define PD_SRC_PDO_TYPE_BATTERY 1
278#define PD_SRC_PDO_TYPE_VARIABLE 2
279#define PD_SRC_PDO_TYPE_AUGMENTED 3
280
281#define PD_SRC_PDO_FIXED_PR_SWAP(pdo) (((pdo) >> 29) & 1)
282#define PD_SRC_PDO_FIXED_USB_SUSP(pdo) (((pdo) >> 28) & 1)
283#define PD_SRC_PDO_FIXED_EXT_POWERED(pdo) (((pdo) >> 27) & 1)
284#define PD_SRC_PDO_FIXED_USB_COMM(pdo) (((pdo) >> 26) & 1)
285#define PD_SRC_PDO_FIXED_DR_SWAP(pdo) (((pdo) >> 25) & 1)
286#define PD_SRC_PDO_FIXED_PEAK_CURR(pdo) (((pdo) >> 20) & 3)
287#define PD_SRC_PDO_FIXED_VOLTAGE(pdo) (((pdo) >> 10) & 0x3FF)
288#define PD_SRC_PDO_FIXED_MAX_CURR(pdo) ((pdo) & 0x3FF)
289
290#define PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) (((pdo) >> 20) & 0x3FF)
291#define PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) (((pdo) >> 10) & 0x3FF)
292#define PD_SRC_PDO_VAR_BATT_MAX(pdo) ((pdo) & 0x3FF)
293
294#define PD_APDO_PPS(pdo) (((pdo) >> 28) & 3)
295#define PD_APDO_MAX_VOLT(pdo) (((pdo) >> 17) & 0xFF)
296#define PD_APDO_MIN_VOLT(pdo) (((pdo) >> 8) & 0xFF)
297#define PD_APDO_MAX_CURR(pdo) ((pdo) & 0x7F)
298
299/* Vendor Defined Messages */
300#define MAX_CRC_RECEIVE_TIME 9 /* ~(2 * tReceive_max(1.1ms) * # retry 4) */
301#define MAX_VDM_RESPONSE_TIME 60 /* 2 * tVDMSenderResponse_max(30ms) */
302#define MAX_VDM_BUSY_TIME 100 /* 2 * tVDMBusy (50ms) */
303
Jack Phamee1f9052017-01-26 12:27:07 -0800304#define PD_SNK_PDO_FIXED(prs, hc, uc, usb_comm, drs, volt, curr) \
305 (((prs) << 29) | ((hc) << 28) | ((uc) << 27) | ((usb_comm) << 26) | \
306 ((drs) << 25) | ((volt) << 10) | (curr))
307
Jack Phamf4baeb12017-02-03 19:01:48 -0800308/* VDM header is the first 32-bit object following the 16-bit PD header */
309#define VDM_HDR_SVID(hdr) ((hdr) >> 16)
310#define VDM_IS_SVDM(hdr) ((hdr) & 0x8000)
311#define SVDM_HDR_OBJ_POS(hdr) (((hdr) >> 8) & 0x7)
312#define SVDM_HDR_CMD_TYPE(hdr) (((hdr) >> 6) & 0x3)
313#define SVDM_HDR_CMD(hdr) ((hdr) & 0x1f)
314
315#define SVDM_HDR(svid, ver, obj, cmd_type, cmd) \
316 (((svid) << 16) | (1 << 15) | ((ver) << 13) \
317 | ((obj) << 8) | ((cmd_type) << 6) | (cmd))
318
319/* discover id response vdo bit fields */
320#define ID_HDR_USB_HOST BIT(31)
321#define ID_HDR_USB_DEVICE BIT(30)
322#define ID_HDR_MODAL_OPR BIT(26)
323#define ID_HDR_PRODUCT_TYPE(n) ((n) >> 27)
324#define ID_HDR_PRODUCT_PER_MASK (2 << 27)
325#define ID_HDR_PRODUCT_HUB 1
326#define ID_HDR_PRODUCT_PER 2
327#define ID_HDR_PRODUCT_AMA 5
328#define ID_HDR_VID 0x05c6 /* qcom */
329#define PROD_VDO_PID 0x0a00 /* TBD */
330
331static bool check_vsafe0v = true;
332module_param(check_vsafe0v, bool, 0600);
333
334static int min_sink_current = 900;
335module_param(min_sink_current, int, 0600);
336
337static const u32 default_src_caps[] = { 0x36019096 }; /* VSafe5V @ 1.5A */
Jack Phamee1f9052017-01-26 12:27:07 -0800338static const u32 default_snk_caps[] = { 0x2601912C }; /* VSafe5V @ 3A */
Jack Phamf4baeb12017-02-03 19:01:48 -0800339
340struct vdm_tx {
Jack Phamf3c1bd32017-08-02 18:32:23 -0700341 u32 data[PD_MAX_DATA_OBJ];
Jack Phamf4baeb12017-02-03 19:01:48 -0800342 int size;
343};
344
345struct rx_msg {
Jack Phamf3c1bd32017-08-02 18:32:23 -0700346 u16 hdr;
347 u16 data_len; /* size of payload in bytes */
Jack Phamf4baeb12017-02-03 19:01:48 -0800348 struct list_head entry;
Jack Phamf3c1bd32017-08-02 18:32:23 -0700349 u8 payload[];
Jack Phamf4baeb12017-02-03 19:01:48 -0800350};
351
Jack Phamf3c1bd32017-08-02 18:32:23 -0700352#define IS_DATA(m, t) ((m) && !PD_MSG_HDR_IS_EXTENDED((m)->hdr) && \
353 PD_MSG_HDR_COUNT((m)->hdr) && \
354 (PD_MSG_HDR_TYPE((m)->hdr) == (t)))
355#define IS_CTRL(m, t) ((m) && !PD_MSG_HDR_COUNT((m)->hdr) && \
356 (PD_MSG_HDR_TYPE((m)->hdr) == (t)))
357#define IS_EXT(m, t) ((m) && PD_MSG_HDR_IS_EXTENDED((m)->hdr) && \
358 (PD_MSG_HDR_TYPE((m)->hdr) == (t)))
Jack Phamf4baeb12017-02-03 19:01:48 -0800359
360struct usbpd {
361 struct device dev;
362 struct workqueue_struct *wq;
363 struct work_struct sm_work;
364 struct hrtimer timer;
365 bool sm_queued;
366
367 struct extcon_dev *extcon;
368
369 enum usbpd_state current_state;
370 bool hard_reset_recvd;
Jack Pham4b323282017-11-03 12:24:59 -0700371 ktime_t hard_reset_recvd_time;
Jack Phamf4baeb12017-02-03 19:01:48 -0800372 struct list_head rx_q;
373 spinlock_t rx_lock;
Jack Phamf3c1bd32017-08-02 18:32:23 -0700374 struct rx_msg *rx_ext_msg;
Jack Phamf4baeb12017-02-03 19:01:48 -0800375
Hemant Kumar796534e2017-05-30 15:54:55 -0700376 u32 received_pdos[PD_MAX_DATA_OBJ];
Hemant Kumara1875942017-08-09 16:50:14 -0700377 u32 received_ado;
Jack Phamf6c02da2017-01-31 15:23:56 -0800378 u16 src_cap_id;
Jack Phamf4baeb12017-02-03 19:01:48 -0800379 u8 selected_pdo;
380 u8 requested_pdo;
381 u32 rdo; /* can be either source or sink */
382 int current_voltage; /* uV */
383 int requested_voltage; /* uV */
384 int requested_current; /* mA */
385 bool pd_connected;
386 bool in_explicit_contract;
387 bool peer_usb_comm;
388 bool peer_pr_swap;
389 bool peer_dr_swap;
390
Jack Phamee1f9052017-01-26 12:27:07 -0800391 u32 sink_caps[7];
392 int num_sink_caps;
393
Jack Phamf4baeb12017-02-03 19:01:48 -0800394 struct power_supply *usb_psy;
395 struct notifier_block psy_nb;
396
397 enum power_supply_typec_mode typec_mode;
398 enum power_supply_type psy_type;
399 enum power_supply_typec_power_role forced_pr;
400 bool vbus_present;
401
402 enum pd_spec_rev spec_rev;
403 enum data_role current_dr;
404 enum power_role current_pr;
405 bool in_pr_swap;
406 bool pd_phy_opened;
Jack Phamaf7d3842017-01-26 13:28:19 -0800407 bool send_request;
408 struct completion is_ready;
Jack Phamf3c1bd32017-08-02 18:32:23 -0700409 struct completion tx_chunk_request;
410 u8 next_tx_chunk;
Jack Phamf4baeb12017-02-03 19:01:48 -0800411
Jack Phamaf7d3842017-01-26 13:28:19 -0800412 struct mutex swap_lock;
Jack Phamf4baeb12017-02-03 19:01:48 -0800413 struct dual_role_phy_instance *dual_role;
414 struct dual_role_phy_desc dr_desc;
415 bool send_pr_swap;
416 bool send_dr_swap;
417
418 struct regulator *vbus;
419 struct regulator *vconn;
420 bool vbus_enabled;
421 bool vconn_enabled;
422 bool vconn_is_external;
423
424 u8 tx_msgid;
425 u8 rx_msgid;
426 int caps_count;
427 int hard_reset_count;
428
429 enum vdm_state vdm_state;
430 u16 *discovered_svids;
431 int num_svids;
432 struct vdm_tx *vdm_tx;
433 struct vdm_tx *vdm_tx_retry;
Mayank Rana83443202017-08-31 15:38:03 -0700434 struct mutex svid_handler_lock;
Jack Phamf4baeb12017-02-03 19:01:48 -0800435 struct list_head svid_handlers;
436
437 struct list_head instance;
Mayank Rana6af43422017-07-18 12:09:02 -0700438
439 u16 ss_lane_svid;
Hemant Kumar018b5982017-08-09 14:14:37 -0700440
441 /* ext msg support */
442 bool send_get_src_cap_ext;
443 u8 src_cap_ext_db[PD_SRC_CAP_EXT_DB_LEN];
444 bool send_get_pps_status;
445 u32 pps_status_db;
Hemant Kumara1875942017-08-09 16:50:14 -0700446 u8 status_db[PD_STATUS_DB_LEN];
Hemant Kumar51ded972017-08-09 17:57:24 -0700447 bool send_get_battery_cap;
448 u8 get_battery_cap_db;
449 u8 battery_cap_db[PD_BATTERY_CAP_DB_LEN];
450 u8 get_battery_status_db;
451 bool send_get_battery_status;
452 u32 battery_sts_dobj;
Jack Phamf4baeb12017-02-03 19:01:48 -0800453};
454
455static LIST_HEAD(_usbpd); /* useful for debugging */
456
457static const unsigned int usbpd_extcon_cable[] = {
458 EXTCON_USB,
459 EXTCON_USB_HOST,
Jack Phamf4baeb12017-02-03 19:01:48 -0800460 EXTCON_NONE,
461};
462
463/* EXTCON_USB and EXTCON_USB_HOST are mutually exclusive */
464static const u32 usbpd_extcon_exclusive[] = {0x3, 0};
465
466enum plug_orientation usbpd_get_plug_orientation(struct usbpd *pd)
467{
468 int ret;
469 union power_supply_propval val;
470
471 ret = power_supply_get_property(pd->usb_psy,
472 POWER_SUPPLY_PROP_TYPEC_CC_ORIENTATION, &val);
473 if (ret)
474 return ORIENTATION_NONE;
475
476 return val.intval;
477}
478EXPORT_SYMBOL(usbpd_get_plug_orientation);
479
Pratham Pratap033a2d92017-11-14 20:57:05 +0530480static unsigned int get_connector_type(struct usbpd *pd)
481{
482 int ret;
483 union power_supply_propval val;
484
485 ret = power_supply_get_property(pd->usb_psy,
486 POWER_SUPPLY_PROP_CONNECTOR_TYPE, &val);
487
488 if (ret) {
489 dev_err(&pd->dev, "Unable to read CONNECTOR TYPE: %d\n", ret);
490 return ret;
491 }
492 return val.intval;
493}
494
Jack Phamf4baeb12017-02-03 19:01:48 -0800495static inline void stop_usb_host(struct usbpd *pd)
496{
Jack Pham4e9dff72017-04-04 18:05:53 -0700497 extcon_set_state_sync(pd->extcon, EXTCON_USB_HOST, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -0800498}
499
500static inline void start_usb_host(struct usbpd *pd, bool ss)
501{
502 enum plug_orientation cc = usbpd_get_plug_orientation(pd);
Jack Pham4e9dff72017-04-04 18:05:53 -0700503 union extcon_property_value val;
Jack Phamf4baeb12017-02-03 19:01:48 -0800504
Jack Pham4e9dff72017-04-04 18:05:53 -0700505 val.intval = (cc == ORIENTATION_CC2);
506 extcon_set_property(pd->extcon, EXTCON_USB_HOST,
507 EXTCON_PROP_USB_TYPEC_POLARITY, val);
508
509 val.intval = ss;
510 extcon_set_property(pd->extcon, EXTCON_USB_HOST,
511 EXTCON_PROP_USB_SS, val);
512
513 extcon_set_state_sync(pd->extcon, EXTCON_USB_HOST, 1);
Jack Phamf4baeb12017-02-03 19:01:48 -0800514}
515
516static inline void stop_usb_peripheral(struct usbpd *pd)
517{
Jack Pham4e9dff72017-04-04 18:05:53 -0700518 extcon_set_state_sync(pd->extcon, EXTCON_USB, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -0800519}
520
521static inline void start_usb_peripheral(struct usbpd *pd)
522{
523 enum plug_orientation cc = usbpd_get_plug_orientation(pd);
Jack Pham4e9dff72017-04-04 18:05:53 -0700524 union extcon_property_value val;
Jack Phamf4baeb12017-02-03 19:01:48 -0800525
Jack Pham4e9dff72017-04-04 18:05:53 -0700526 val.intval = (cc == ORIENTATION_CC2);
527 extcon_set_property(pd->extcon, EXTCON_USB,
528 EXTCON_PROP_USB_TYPEC_POLARITY, val);
529
530 val.intval = 1;
531 extcon_set_property(pd->extcon, EXTCON_USB, EXTCON_PROP_USB_SS, val);
532
533 extcon_set_state_sync(pd->extcon, EXTCON_USB, 1);
Jack Phamf4baeb12017-02-03 19:01:48 -0800534}
535
Mayank Rana6af43422017-07-18 12:09:02 -0700536/**
537 * This API allows client driver to request for releasing SS lanes. It should
538 * not be called from atomic context.
539 *
540 * @pd - USBPD handler
541 * @hdlr - client's handler
542 *
543 * @returns int - Success - 0, else negative error code
544 */
545static int usbpd_release_ss_lane(struct usbpd *pd,
546 struct usbpd_svid_handler *hdlr)
547{
548 int ret = 0;
549
550 if (!hdlr || !pd)
551 return -EINVAL;
552
553 usbpd_dbg(&pd->dev, "hdlr:%pK svid:%d", hdlr, hdlr->svid);
554 /*
555 * If USB SS lanes are already used by one client, and other client is
556 * requesting for same or same client requesting again, return -EBUSY.
557 */
558 if (pd->ss_lane_svid) {
559 usbpd_dbg(&pd->dev, "-EBUSY: ss_lanes are already used by(%d)",
560 pd->ss_lane_svid);
561 ret = -EBUSY;
562 goto err_exit;
563 }
564
565 ret = extcon_blocking_sync(pd->extcon, EXTCON_USB_HOST, 0);
566 if (ret) {
567 usbpd_err(&pd->dev, "err(%d) for releasing ss lane", ret);
568 goto err_exit;
569 }
570
571 pd->ss_lane_svid = hdlr->svid;
572
573err_exit:
574 return ret;
575}
576
Jack Phamf4baeb12017-02-03 19:01:48 -0800577static int set_power_role(struct usbpd *pd, enum power_role pr)
578{
579 union power_supply_propval val = {0};
580
581 switch (pr) {
582 case PR_NONE:
583 val.intval = POWER_SUPPLY_TYPEC_PR_NONE;
584 break;
585 case PR_SINK:
586 val.intval = POWER_SUPPLY_TYPEC_PR_SINK;
587 break;
588 case PR_SRC:
589 val.intval = POWER_SUPPLY_TYPEC_PR_SOURCE;
590 break;
591 }
592
593 return power_supply_set_property(pd->usb_psy,
594 POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val);
595}
596
597static struct usbpd_svid_handler *find_svid_handler(struct usbpd *pd, u16 svid)
598{
599 struct usbpd_svid_handler *handler;
600
Mayank Rana83443202017-08-31 15:38:03 -0700601 mutex_lock(&pd->svid_handler_lock);
602 list_for_each_entry(handler, &pd->svid_handlers, entry) {
603 if (svid == handler->svid) {
604 mutex_unlock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -0800605 return handler;
Mayank Rana83443202017-08-31 15:38:03 -0700606 }
607 }
Jack Phamf4baeb12017-02-03 19:01:48 -0800608
Mayank Rana83443202017-08-31 15:38:03 -0700609 mutex_unlock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -0800610 return NULL;
611}
612
613/* Reset protocol layer */
614static inline void pd_reset_protocol(struct usbpd *pd)
615{
616 /*
617 * first Rx ID should be 0; set this to a sentinel of -1 so that in
618 * phy_msg_received() we can check if we had seen it before.
619 */
620 pd->rx_msgid = -1;
621 pd->tx_msgid = 0;
Jack Phamaf7d3842017-01-26 13:28:19 -0800622 pd->send_request = false;
623 pd->send_pr_swap = false;
624 pd->send_dr_swap = false;
Jack Phamf4baeb12017-02-03 19:01:48 -0800625}
626
Jack Phame95cfc72017-08-01 17:36:50 -0700627static int pd_send_msg(struct usbpd *pd, u8 msg_type, const u32 *data,
628 size_t num_data, enum pd_sop_type sop)
Jack Phamf4baeb12017-02-03 19:01:48 -0800629{
630 int ret;
631 u16 hdr;
632
Jack Pham4b323282017-11-03 12:24:59 -0700633 if (pd->hard_reset_recvd)
634 return -EBUSY;
635
Jack Phame95cfc72017-08-01 17:36:50 -0700636 hdr = PD_MSG_HDR(msg_type, pd->current_dr, pd->current_pr,
Jack Phamf4baeb12017-02-03 19:01:48 -0800637 pd->tx_msgid, num_data, pd->spec_rev);
Jack Phamf4baeb12017-02-03 19:01:48 -0800638
Jack Pham7dfd7612017-08-01 18:04:13 -0700639 ret = pd_phy_write(hdr, (u8 *)data, num_data * sizeof(u32), sop);
640 if (ret)
Jack Phamf4baeb12017-02-03 19:01:48 -0800641 return ret;
Jack Phamcc119752017-06-07 15:35:57 -0700642
643 pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID;
Jack Phamf4baeb12017-02-03 19:01:48 -0800644 return 0;
645}
646
Hemant Kumar51ded972017-08-09 17:57:24 -0700647static int pd_send_ext_msg(struct usbpd *pd, u8 msg_type,
648 const u8 *data, size_t data_len, enum pd_sop_type sop)
649{
650 int ret;
651 size_t len_remain, chunk_len;
652 u8 chunked_payload[PD_MAX_DATA_OBJ * sizeof(u32)] = {0};
653 u16 hdr;
654 u16 ext_hdr;
655 u8 num_objs;
656
657 if (data_len > PD_MAX_EXT_MSG_LEN) {
658 usbpd_warn(&pd->dev, "Extended message length exceeds max, truncating...\n");
659 data_len = PD_MAX_EXT_MSG_LEN;
660 }
661
662 pd->next_tx_chunk = 0;
663 len_remain = data_len;
664 do {
665 ext_hdr = PD_MSG_EXT_HDR(1, pd->next_tx_chunk++, 0, data_len);
666 memcpy(chunked_payload, &ext_hdr, sizeof(ext_hdr));
667
668 chunk_len = min_t(size_t, len_remain,
669 PD_MAX_EXT_MSG_LEGACY_LEN);
670 memcpy(chunked_payload + sizeof(ext_hdr), data, chunk_len);
671
672 num_objs = DIV_ROUND_UP(chunk_len + sizeof(u16), sizeof(u32));
673 len_remain -= chunk_len;
674
675 reinit_completion(&pd->tx_chunk_request);
676 hdr = PD_MSG_HDR(msg_type, pd->current_dr, pd->current_pr,
677 pd->tx_msgid, num_objs, pd->spec_rev) |
678 PD_MSG_HDR_EXTENDED;
679 ret = pd_phy_write(hdr, chunked_payload,
680 num_objs * sizeof(u32), sop);
681 if (ret)
682 return ret;
683
684 pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID;
685
686 /* Wait for request chunk */
687 if (len_remain &&
688 !wait_for_completion_timeout(&pd->tx_chunk_request,
689 msecs_to_jiffies(SENDER_RESPONSE_TIME))) {
690 usbpd_err(&pd->dev, "Timed out waiting for chunk request\n");
691 return -EPROTO;
692 }
693 } while (len_remain);
694
695 return 0;
696}
697
Jack Phamf4baeb12017-02-03 19:01:48 -0800698static int pd_select_pdo(struct usbpd *pd, int pdo_pos, int uv, int ua)
699{
700 int curr;
701 int max_current;
702 bool mismatch = false;
703 u8 type;
704 u32 pdo = pd->received_pdos[pdo_pos - 1];
705
706 type = PD_SRC_PDO_TYPE(pdo);
707 if (type == PD_SRC_PDO_TYPE_FIXED) {
708 curr = max_current = PD_SRC_PDO_FIXED_MAX_CURR(pdo) * 10;
709
710 /*
711 * Check if the PDO has enough current, otherwise set the
712 * Capability Mismatch flag
713 */
714 if (curr < min_sink_current) {
715 mismatch = true;
716 max_current = min_sink_current;
717 }
718
719 pd->requested_voltage =
720 PD_SRC_PDO_FIXED_VOLTAGE(pdo) * 50 * 1000;
721 pd->rdo = PD_RDO_FIXED(pdo_pos, 0, mismatch, 1, 1, curr / 10,
722 max_current / 10);
723 } else if (type == PD_SRC_PDO_TYPE_AUGMENTED) {
724 if ((uv / 100000) > PD_APDO_MAX_VOLT(pdo) ||
725 (uv / 100000) < PD_APDO_MIN_VOLT(pdo) ||
726 (ua / 50000) > PD_APDO_MAX_CURR(pdo) || (ua < 0)) {
727 usbpd_err(&pd->dev, "uv (%d) and ua (%d) out of range of APDO\n",
728 uv, ua);
729 return -EINVAL;
730 }
731
732 curr = ua / 1000;
733 pd->requested_voltage = uv;
734 pd->rdo = PD_RDO_AUGMENTED(pdo_pos, mismatch, 1, 1,
735 uv / 20000, ua / 50000);
736 } else {
737 usbpd_err(&pd->dev, "Only Fixed or Programmable PDOs supported\n");
738 return -ENOTSUPP;
739 }
740
741 /* Can't sink more than 5V if VCONN is sourced from the VBUS input */
742 if (pd->vconn_enabled && !pd->vconn_is_external &&
743 pd->requested_voltage > 5000000)
744 return -ENOTSUPP;
745
746 pd->requested_current = curr;
747 pd->requested_pdo = pdo_pos;
748
749 return 0;
750}
751
752static int pd_eval_src_caps(struct usbpd *pd)
753{
Hemant Kumarbe746222017-07-20 13:51:49 -0700754 int i;
Jack Phamf4baeb12017-02-03 19:01:48 -0800755 union power_supply_propval val;
756 u32 first_pdo = pd->received_pdos[0];
757
758 if (PD_SRC_PDO_TYPE(first_pdo) != PD_SRC_PDO_TYPE_FIXED) {
759 usbpd_err(&pd->dev, "First src_cap invalid! %08x\n", first_pdo);
760 return -EINVAL;
761 }
762
763 pd->peer_usb_comm = PD_SRC_PDO_FIXED_USB_COMM(first_pdo);
764 pd->peer_pr_swap = PD_SRC_PDO_FIXED_PR_SWAP(first_pdo);
765 pd->peer_dr_swap = PD_SRC_PDO_FIXED_DR_SWAP(first_pdo);
766
767 val.intval = PD_SRC_PDO_FIXED_USB_SUSP(first_pdo);
768 power_supply_set_property(pd->usb_psy,
769 POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED, &val);
770
Hemant Kumarbe746222017-07-20 13:51:49 -0700771 if (pd->spec_rev == USBPD_REV_30 && !rev3_sink_only) {
772 bool pps_found = false;
773
774 /* downgrade to 2.0 if no PPS */
775 for (i = 1; i < PD_MAX_DATA_OBJ; i++) {
776 if ((PD_SRC_PDO_TYPE(pd->received_pdos[i]) ==
Hemant Kumar796534e2017-05-30 15:54:55 -0700777 PD_SRC_PDO_TYPE_AUGMENTED) &&
Hemant Kumarbe746222017-07-20 13:51:49 -0700778 !PD_APDO_PPS(pd->received_pdos[i])) {
779 pps_found = true;
780 break;
781 }
782 }
783 if (!pps_found)
784 pd->spec_rev = USBPD_REV_20;
Hemant Kumar796534e2017-05-30 15:54:55 -0700785 }
786
Jack Phamf4baeb12017-02-03 19:01:48 -0800787 /* Select the first PDO (vSafe5V) immediately. */
788 pd_select_pdo(pd, 1, 0, 0);
789
790 return 0;
791}
792
793static void pd_send_hard_reset(struct usbpd *pd)
794{
Jack Pham857ee682017-05-25 11:53:36 -0700795 union power_supply_propval val = {0};
796
Jack Phamf4baeb12017-02-03 19:01:48 -0800797 usbpd_dbg(&pd->dev, "send hard reset");
798
799 /* Force CC logic to source/sink to keep Rp/Rd unchanged */
800 set_power_role(pd, pd->current_pr);
801 pd->hard_reset_count++;
Jack Pham7dfd7612017-08-01 18:04:13 -0700802 pd_phy_signal(HARD_RESET_SIG);
Jack Phamf4baeb12017-02-03 19:01:48 -0800803 pd->in_pr_swap = false;
Jack Pham857ee682017-05-25 11:53:36 -0700804 power_supply_set_property(pd->usb_psy, POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -0800805}
806
807static void kick_sm(struct usbpd *pd, int ms)
808{
809 pm_stay_awake(&pd->dev);
810 pd->sm_queued = true;
811
812 if (ms)
813 hrtimer_start(&pd->timer, ms_to_ktime(ms), HRTIMER_MODE_REL);
814 else
815 queue_work(pd->wq, &pd->sm_work);
816}
817
Jack Phame95cfc72017-08-01 17:36:50 -0700818static void phy_sig_received(struct usbpd *pd, enum pd_sig_type sig)
Jack Phamf4baeb12017-02-03 19:01:48 -0800819{
Jack Pham9411cbb2017-06-06 11:10:03 -0700820 union power_supply_propval val = {1};
821
Jack Phame95cfc72017-08-01 17:36:50 -0700822 if (sig != HARD_RESET_SIG) {
823 usbpd_err(&pd->dev, "invalid signal (%d) received\n", sig);
Jack Phamf4baeb12017-02-03 19:01:48 -0800824 return;
825 }
826
Jack Pham4b323282017-11-03 12:24:59 -0700827 pd->hard_reset_recvd = true;
828 pd->hard_reset_recvd_time = ktime_get();
829
830 usbpd_err(&pd->dev, "hard reset received\n");
Jack Phamf4baeb12017-02-03 19:01:48 -0800831
832 /* Force CC logic to source/sink to keep Rp/Rd unchanged */
833 set_power_role(pd, pd->current_pr);
Jack Pham9411cbb2017-06-06 11:10:03 -0700834 power_supply_set_property(pd->usb_psy,
835 POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val);
836
Jack Phamf4baeb12017-02-03 19:01:48 -0800837 kick_sm(pd, 0);
838}
839
Jack Phamf3c1bd32017-08-02 18:32:23 -0700840struct pd_request_chunk {
841 struct work_struct w;
842 struct usbpd *pd;
843 u8 msg_type;
844 u8 chunk_num;
845 enum pd_sop_type sop;
846};
847
848static void pd_request_chunk_work(struct work_struct *w)
849{
850 struct pd_request_chunk *req =
851 container_of(w, struct pd_request_chunk, w);
852 struct usbpd *pd = req->pd;
853 unsigned long flags;
854 int ret;
855 u8 payload[4] = {0}; /* ext_hdr + padding */
856 u16 hdr = PD_MSG_HDR(req->msg_type, pd->current_dr, pd->current_pr,
857 pd->tx_msgid, 1, pd->spec_rev) | PD_MSG_HDR_EXTENDED;
858
859 *(u16 *)payload = PD_MSG_EXT_HDR(1, req->chunk_num, 1, 0);
860
861 ret = pd_phy_write(hdr, payload, sizeof(payload), req->sop);
862 if (!ret) {
863 pd->tx_msgid = (pd->tx_msgid + 1) & PD_MAX_MSG_ID;
864 } else {
865 usbpd_err(&pd->dev, "could not send chunk request\n");
866
867 /* queue what we have anyway */
868 spin_lock_irqsave(&pd->rx_lock, flags);
869 list_add_tail(&pd->rx_ext_msg->entry, &pd->rx_q);
870 spin_unlock_irqrestore(&pd->rx_lock, flags);
871
872 pd->rx_ext_msg = NULL;
873 }
874
875 kfree(req);
876}
877
878static struct rx_msg *pd_ext_msg_received(struct usbpd *pd, u16 header, u8 *buf,
879 size_t len, enum pd_sop_type sop)
880{
881 struct rx_msg *rx_msg;
882 u16 bytes_to_copy;
883 u16 ext_hdr = *(u16 *)buf;
884 u8 chunk_num;
885
886 if (!PD_MSG_EXT_HDR_IS_CHUNKED(ext_hdr)) {
887 usbpd_err(&pd->dev, "unchunked extended messages unsupported\n");
888 return NULL;
889 }
890
891 /* request for next Tx chunk */
892 if (PD_MSG_EXT_HDR_REQ_CHUNK(ext_hdr)) {
893 if (PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr) ||
894 PD_MSG_EXT_HDR_CHUNK_NUM(ext_hdr) !=
895 pd->next_tx_chunk) {
896 usbpd_err(&pd->dev, "invalid request chunk ext header 0x%02x\n",
897 ext_hdr);
898 return NULL;
899 }
900
901 if (!completion_done(&pd->tx_chunk_request))
902 complete(&pd->tx_chunk_request);
903
904 return NULL;
905 }
906
907 chunk_num = PD_MSG_EXT_HDR_CHUNK_NUM(ext_hdr);
908 if (!chunk_num) {
909 /* allocate new message if first chunk */
910 rx_msg = kzalloc(sizeof(*rx_msg) +
911 PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr),
Jack Phama27be8f2017-11-02 10:51:48 -0700912 GFP_ATOMIC);
Jack Phamf3c1bd32017-08-02 18:32:23 -0700913 if (!rx_msg)
914 return NULL;
915
916 rx_msg->hdr = header;
917 rx_msg->data_len = PD_MSG_EXT_HDR_DATA_SIZE(ext_hdr);
918
919 if (rx_msg->data_len > PD_MAX_EXT_MSG_LEN) {
920 usbpd_warn(&pd->dev, "Extended message length exceeds max, truncating...\n");
921 rx_msg->data_len = PD_MAX_EXT_MSG_LEN;
922 }
923 } else {
924 if (!pd->rx_ext_msg) {
925 usbpd_err(&pd->dev, "missing first rx_ext_msg chunk\n");
926 return NULL;
927 }
928
929 rx_msg = pd->rx_ext_msg;
930 }
931
932 /*
933 * The amount to copy is derived as follows:
934 *
935 * - if extended data_len < 26, then copy data_len bytes
936 * - for chunks 0..N-2, copy 26 bytes
937 * - for the last chunk (N-1), copy the remainder
938 */
939 bytes_to_copy =
940 min((rx_msg->data_len - chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN),
941 PD_MAX_EXT_MSG_LEGACY_LEN);
942
943 /* check against received length to avoid overrun */
944 if (bytes_to_copy > len - sizeof(ext_hdr)) {
945 usbpd_warn(&pd->dev, "not enough bytes in chunk, expected:%u received:%lu\n",
946 bytes_to_copy, len - sizeof(ext_hdr));
947 bytes_to_copy = len - sizeof(ext_hdr);
948 }
949
950 memcpy(rx_msg->payload + chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN, buf + 2,
951 bytes_to_copy);
952
953 /* request next chunk? */
954 if ((rx_msg->data_len - chunk_num * PD_MAX_EXT_MSG_LEGACY_LEN) >
955 PD_MAX_EXT_MSG_LEGACY_LEN) {
956 struct pd_request_chunk *req;
957
958 if (pd->rx_ext_msg && pd->rx_ext_msg != rx_msg) {
959 usbpd_dbg(&pd->dev, "stale previous rx_ext_msg?\n");
960 kfree(pd->rx_ext_msg);
961 }
962
963 pd->rx_ext_msg = rx_msg;
964
Jack Phama27be8f2017-11-02 10:51:48 -0700965 req = kzalloc(sizeof(*req), GFP_ATOMIC);
Jack Phamf3c1bd32017-08-02 18:32:23 -0700966 if (!req)
967 goto queue_rx; /* return what we have anyway */
968
969 INIT_WORK(&req->w, pd_request_chunk_work);
970 req->pd = pd;
971 req->msg_type = PD_MSG_HDR_TYPE(header);
972 req->chunk_num = chunk_num + 1;
973 req->sop = sop;
974 queue_work(pd->wq, &req->w);
975
976 return NULL;
977 }
978
979queue_rx:
980 pd->rx_ext_msg = NULL;
981 return rx_msg; /* queue it for usbpd_sm */
982}
983
Jack Phame95cfc72017-08-01 17:36:50 -0700984static void phy_msg_received(struct usbpd *pd, enum pd_sop_type sop,
Jack Phamf4baeb12017-02-03 19:01:48 -0800985 u8 *buf, size_t len)
986{
987 struct rx_msg *rx_msg;
988 unsigned long flags;
989 u16 header;
990
Jack Phame95cfc72017-08-01 17:36:50 -0700991 if (sop != SOP_MSG) {
Jack Phamf4baeb12017-02-03 19:01:48 -0800992 usbpd_err(&pd->dev, "invalid msg type (%d) received; only SOP supported\n",
Jack Phame95cfc72017-08-01 17:36:50 -0700993 sop);
Jack Phamf4baeb12017-02-03 19:01:48 -0800994 return;
995 }
996
997 if (len < 2) {
998 usbpd_err(&pd->dev, "invalid message received, len=%zd\n", len);
999 return;
1000 }
1001
1002 header = *((u16 *)buf);
1003 buf += sizeof(u16);
1004 len -= sizeof(u16);
1005
1006 if (len % 4 != 0) {
1007 usbpd_err(&pd->dev, "len=%zd not multiple of 4\n", len);
1008 return;
1009 }
1010
1011 /* if MSGID already seen, discard */
1012 if (PD_MSG_HDR_ID(header) == pd->rx_msgid &&
1013 PD_MSG_HDR_TYPE(header) != MSG_SOFT_RESET) {
1014 usbpd_dbg(&pd->dev, "MessageID already seen, discarding\n");
1015 return;
1016 }
1017
1018 pd->rx_msgid = PD_MSG_HDR_ID(header);
1019
1020 /* discard Pings */
1021 if (PD_MSG_HDR_TYPE(header) == MSG_PING && !len)
1022 return;
1023
1024 /* check header's count field to see if it matches len */
1025 if (PD_MSG_HDR_COUNT(header) != (len / 4)) {
1026 usbpd_err(&pd->dev, "header count (%d) mismatch, len=%zd\n",
1027 PD_MSG_HDR_COUNT(header), len);
1028 return;
1029 }
1030
Hemant Kumarbe746222017-07-20 13:51:49 -07001031 /* if spec rev differs (i.e. is older), update PHY */
1032 if (PD_MSG_HDR_REV(header) < pd->spec_rev)
1033 pd->spec_rev = PD_MSG_HDR_REV(header);
1034
Jack Phamf3c1bd32017-08-02 18:32:23 -07001035 usbpd_dbg(&pd->dev, "received message: type(%d) num_objs(%d)\n",
1036 PD_MSG_HDR_TYPE(header), PD_MSG_HDR_COUNT(header));
Jack Phamf4baeb12017-02-03 19:01:48 -08001037
Jack Phamf3c1bd32017-08-02 18:32:23 -07001038 if (!PD_MSG_HDR_IS_EXTENDED(header)) {
Jack Phama27be8f2017-11-02 10:51:48 -07001039 rx_msg = kzalloc(sizeof(*rx_msg) + len, GFP_ATOMIC);
Jack Phamf3c1bd32017-08-02 18:32:23 -07001040 if (!rx_msg)
1041 return;
1042
1043 rx_msg->hdr = header;
1044 rx_msg->data_len = len;
1045 memcpy(rx_msg->payload, buf, len);
1046 } else {
1047 rx_msg = pd_ext_msg_received(pd, header, buf, len, sop);
1048 if (!rx_msg)
1049 return;
1050 }
Jack Phamf4baeb12017-02-03 19:01:48 -08001051
1052 spin_lock_irqsave(&pd->rx_lock, flags);
1053 list_add_tail(&rx_msg->entry, &pd->rx_q);
1054 spin_unlock_irqrestore(&pd->rx_lock, flags);
1055
Jack Phamf4baeb12017-02-03 19:01:48 -08001056 kick_sm(pd, 0);
1057}
1058
1059static void phy_shutdown(struct usbpd *pd)
1060{
1061 usbpd_dbg(&pd->dev, "shutdown");
Jack Pham0ab02962017-10-12 15:33:50 -07001062
1063 if (pd->vconn_enabled) {
1064 regulator_disable(pd->vconn);
1065 pd->vconn_enabled = false;
1066 }
1067
1068 if (pd->vbus_enabled) {
1069 regulator_disable(pd->vbus);
1070 pd->vbus_enabled = false;
1071 }
Jack Phamf4baeb12017-02-03 19:01:48 -08001072}
1073
1074static enum hrtimer_restart pd_timeout(struct hrtimer *timer)
1075{
1076 struct usbpd *pd = container_of(timer, struct usbpd, timer);
1077
1078 usbpd_dbg(&pd->dev, "timeout");
1079 queue_work(pd->wq, &pd->sm_work);
1080
1081 return HRTIMER_NORESTART;
1082}
1083
1084/* Enters new state and executes actions on entry */
1085static void usbpd_set_state(struct usbpd *pd, enum usbpd_state next_state)
1086{
1087 struct pd_phy_params phy_params = {
1088 .signal_cb = phy_sig_received,
1089 .msg_rx_cb = phy_msg_received,
1090 .shutdown_cb = phy_shutdown,
1091 .frame_filter_val = FRAME_FILTER_EN_SOP |
1092 FRAME_FILTER_EN_HARD_RESET,
Jack Phamf4baeb12017-02-03 19:01:48 -08001093 };
1094 union power_supply_propval val = {0};
1095 unsigned long flags;
1096 int ret;
1097
Jack Pham4b323282017-11-03 12:24:59 -07001098 if (pd->hard_reset_recvd) /* let usbpd_sm handle it */
1099 return;
1100
Jack Phamf4baeb12017-02-03 19:01:48 -08001101 usbpd_dbg(&pd->dev, "%s -> %s\n",
1102 usbpd_state_strings[pd->current_state],
1103 usbpd_state_strings[next_state]);
1104
1105 pd->current_state = next_state;
1106
1107 switch (next_state) {
1108 case PE_ERROR_RECOVERY: /* perform hard disconnect/reconnect */
1109 pd->in_pr_swap = false;
1110 pd->current_pr = PR_NONE;
1111 set_power_role(pd, PR_NONE);
1112 pd->typec_mode = POWER_SUPPLY_TYPEC_NONE;
1113 kick_sm(pd, 0);
1114 break;
1115
1116 /* Source states */
Jack Phamb9200bb2017-04-19 00:25:32 -07001117 case PE_SRC_DISABLED:
1118 /* are we still connected? */
1119 if (pd->typec_mode == POWER_SUPPLY_TYPEC_NONE) {
1120 pd->current_pr = PR_NONE;
1121 kick_sm(pd, 0);
1122 }
1123
1124 break;
1125
Jack Phamf4baeb12017-02-03 19:01:48 -08001126 case PE_SRC_STARTUP:
1127 if (pd->current_dr == DR_NONE) {
1128 pd->current_dr = DR_DFP;
Mayank Rana38e9b252017-03-23 12:35:57 -07001129 start_usb_host(pd, true);
Mayank Rana6af43422017-07-18 12:09:02 -07001130 pd->ss_lane_svid = 0x0;
Jack Phamf4baeb12017-02-03 19:01:48 -08001131 }
1132
1133 dual_role_instance_changed(pd->dual_role);
1134
1135 /* Set CC back to DRP toggle for the next disconnect */
1136 val.intval = POWER_SUPPLY_TYPEC_PR_DUAL;
1137 power_supply_set_property(pd->usb_psy,
1138 POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val);
1139
Hemant Kumarbe746222017-07-20 13:51:49 -07001140 /* support only PD 2.0 as a source */
1141 pd->spec_rev = USBPD_REV_20;
Jack Phamf4baeb12017-02-03 19:01:48 -08001142 pd_reset_protocol(pd);
1143
1144 if (!pd->in_pr_swap) {
1145 if (pd->pd_phy_opened) {
1146 pd_phy_close();
1147 pd->pd_phy_opened = false;
1148 }
1149
1150 phy_params.data_role = pd->current_dr;
1151 phy_params.power_role = pd->current_pr;
Jack Phamf4baeb12017-02-03 19:01:48 -08001152
1153 ret = pd_phy_open(&phy_params);
1154 if (ret) {
1155 WARN_ON_ONCE(1);
1156 usbpd_err(&pd->dev, "error opening PD PHY %d\n",
1157 ret);
1158 pd->current_state = PE_UNKNOWN;
1159 return;
1160 }
1161
1162 pd->pd_phy_opened = true;
Jack Phamf4baeb12017-02-03 19:01:48 -08001163 }
1164
Jack Phamf4baeb12017-02-03 19:01:48 -08001165 if (pd->in_pr_swap) {
Jack Phamf4baeb12017-02-03 19:01:48 -08001166 pd->in_pr_swap = false;
Jack Pham857ee682017-05-25 11:53:36 -07001167 val.intval = 0;
1168 power_supply_set_property(pd->usb_psy,
1169 POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08001170 }
1171
Jack Pham35121342017-07-06 00:12:15 -07001172 /*
1173 * A sink might remove its terminations (during some Type-C
1174 * compliance tests or a sink attempting to do Try.SRC)
1175 * at this point just after we enabled VBUS. Sending PD
1176 * messages now would delay detecting the detach beyond the
1177 * required timing. Instead, delay sending out the first
1178 * source capabilities to allow for the other side to
1179 * completely settle CC debounce and allow HW to detect detach
1180 * sooner in the meantime. PD spec allows up to
1181 * tFirstSourceCap (250ms).
1182 */
1183 pd->current_state = PE_SRC_SEND_CAPABILITIES;
1184 kick_sm(pd, FIRST_SOURCE_CAP_TIME);
1185 break;
Jack Phamf4baeb12017-02-03 19:01:48 -08001186
1187 case PE_SRC_SEND_CAPABILITIES:
1188 kick_sm(pd, 0);
1189 break;
1190
1191 case PE_SRC_NEGOTIATE_CAPABILITY:
1192 if (PD_RDO_OBJ_POS(pd->rdo) != 1 ||
1193 PD_RDO_FIXED_CURR(pd->rdo) >
1194 PD_SRC_PDO_FIXED_MAX_CURR(*default_src_caps) ||
1195 PD_RDO_FIXED_CURR_MINMAX(pd->rdo) >
1196 PD_SRC_PDO_FIXED_MAX_CURR(*default_src_caps)) {
1197 /* send Reject */
1198 ret = pd_send_msg(pd, MSG_REJECT, NULL, 0, SOP_MSG);
1199 if (ret) {
1200 usbpd_err(&pd->dev, "Error sending Reject\n");
1201 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
1202 break;
1203 }
1204
1205 usbpd_err(&pd->dev, "Invalid request: %08x\n", pd->rdo);
1206
1207 if (pd->in_explicit_contract)
1208 usbpd_set_state(pd, PE_SRC_READY);
1209 else
1210 /*
1211 * bypass PE_SRC_Capability_Response and
1212 * PE_SRC_Wait_New_Capabilities in this
1213 * implementation for simplicity.
1214 */
1215 usbpd_set_state(pd, PE_SRC_SEND_CAPABILITIES);
1216 break;
1217 }
1218
1219 /* PE_SRC_TRANSITION_SUPPLY pseudo-state */
1220 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
1221 if (ret) {
1222 usbpd_err(&pd->dev, "Error sending Accept\n");
1223 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
1224 break;
1225 }
1226
1227 /* tSrcTransition required after ACCEPT */
1228 usleep_range(SRC_TRANSITION_TIME * USEC_PER_MSEC,
1229 (SRC_TRANSITION_TIME + 5) * USEC_PER_MSEC);
1230
1231 /*
1232 * Normally a voltage change should occur within tSrcReady
1233 * but since we only support VSafe5V there is nothing more to
1234 * prepare from the power supply so send PS_RDY right away.
1235 */
1236 ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG);
1237 if (ret) {
1238 usbpd_err(&pd->dev, "Error sending PS_RDY\n");
1239 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
1240 break;
1241 }
1242
1243 usbpd_set_state(pd, PE_SRC_READY);
1244 break;
1245
1246 case PE_SRC_READY:
1247 pd->in_explicit_contract = true;
Jack Phambe0f20c2017-10-05 12:51:26 -07001248
1249 if (pd->vdm_tx)
1250 kick_sm(pd, 0);
1251 else if (pd->current_dr == DR_DFP && pd->vdm_state == VDM_NONE)
1252 usbpd_send_svdm(pd, USBPD_SID,
1253 USBPD_SVDM_DISCOVER_IDENTITY,
1254 SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -08001255
1256 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Jack Phamaf7d3842017-01-26 13:28:19 -08001257 complete(&pd->is_ready);
Jack Phamf4baeb12017-02-03 19:01:48 -08001258 dual_role_instance_changed(pd->dual_role);
1259 break;
1260
1261 case PE_SRC_HARD_RESET:
1262 case PE_SNK_HARD_RESET:
Jack Phamb9200bb2017-04-19 00:25:32 -07001263 /* are we still connected? */
1264 if (pd->typec_mode == POWER_SUPPLY_TYPEC_NONE)
1265 pd->current_pr = PR_NONE;
1266
Jack Phamf4baeb12017-02-03 19:01:48 -08001267 /* hard reset may sleep; handle it in the workqueue */
1268 kick_sm(pd, 0);
1269 break;
1270
1271 case PE_SRC_SEND_SOFT_RESET:
1272 case PE_SNK_SEND_SOFT_RESET:
1273 pd_reset_protocol(pd);
1274
1275 ret = pd_send_msg(pd, MSG_SOFT_RESET, NULL, 0, SOP_MSG);
1276 if (ret) {
1277 usbpd_err(&pd->dev, "Error sending Soft Reset, do Hard Reset\n");
1278 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
1279 PE_SRC_HARD_RESET : PE_SNK_HARD_RESET);
1280 break;
1281 }
1282
1283 /* wait for ACCEPT */
1284 kick_sm(pd, SENDER_RESPONSE_TIME);
1285 break;
1286
1287 /* Sink states */
1288 case PE_SNK_STARTUP:
1289 if (pd->current_dr == DR_NONE || pd->current_dr == DR_UFP) {
1290 pd->current_dr = DR_UFP;
1291
1292 if (pd->psy_type == POWER_SUPPLY_TYPE_USB ||
Vijayavardhan Vennapusa34a1f1d2017-05-31 12:00:37 +05301293 pd->psy_type == POWER_SUPPLY_TYPE_USB_CDP ||
Hemant Kumarbbf7d052017-07-13 12:08:49 -07001294 pd->psy_type == POWER_SUPPLY_TYPE_USB_FLOAT ||
Vijayavardhan Vennapusa34a1f1d2017-05-31 12:00:37 +05301295 usb_compliance_mode)
Jack Phamf4baeb12017-02-03 19:01:48 -08001296 start_usb_peripheral(pd);
1297 }
1298
1299 dual_role_instance_changed(pd->dual_role);
1300
1301 ret = power_supply_get_property(pd->usb_psy,
1302 POWER_SUPPLY_PROP_PD_ALLOWED, &val);
1303 if (ret) {
1304 usbpd_err(&pd->dev, "Unable to read USB PROP_PD_ALLOWED: %d\n",
1305 ret);
1306 break;
1307 }
1308
Vijayavardhan Vennapusa49f21ce2017-06-05 14:47:56 +05301309 if (!val.intval || disable_usb_pd)
Jack Phamf4baeb12017-02-03 19:01:48 -08001310 break;
1311
Hemant Kumarbe746222017-07-20 13:51:49 -07001312 /*
1313 * support up to PD 3.0 as a sink; if source is 2.0
1314 * phy_msg_received() will handle the downgrade.
1315 */
1316 pd->spec_rev = USBPD_REV_30;
Jack Phamf4baeb12017-02-03 19:01:48 -08001317 pd_reset_protocol(pd);
1318
1319 if (!pd->in_pr_swap) {
1320 if (pd->pd_phy_opened) {
1321 pd_phy_close();
1322 pd->pd_phy_opened = false;
1323 }
1324
1325 phy_params.data_role = pd->current_dr;
1326 phy_params.power_role = pd->current_pr;
Jack Phamf4baeb12017-02-03 19:01:48 -08001327
1328 ret = pd_phy_open(&phy_params);
1329 if (ret) {
1330 WARN_ON_ONCE(1);
1331 usbpd_err(&pd->dev, "error opening PD PHY %d\n",
1332 ret);
1333 pd->current_state = PE_UNKNOWN;
1334 return;
1335 }
1336
1337 pd->pd_phy_opened = true;
Jack Phamf4baeb12017-02-03 19:01:48 -08001338 }
1339
1340 pd->current_voltage = pd->requested_voltage = 5000000;
1341 val.intval = pd->requested_voltage; /* set max range to 5V */
1342 power_supply_set_property(pd->usb_psy,
Nicholas Troast7f55c922017-07-25 13:18:03 -07001343 POWER_SUPPLY_PROP_PD_VOLTAGE_MAX, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08001344
1345 if (!pd->vbus_present) {
1346 pd->current_state = PE_SNK_DISCOVERY;
1347 /* max time for hard reset to turn vbus back on */
1348 kick_sm(pd, SNK_HARD_RESET_VBUS_ON_TIME);
1349 break;
1350 }
1351
1352 pd->current_state = PE_SNK_WAIT_FOR_CAPABILITIES;
1353 /* fall-through */
1354
1355 case PE_SNK_WAIT_FOR_CAPABILITIES:
1356 spin_lock_irqsave(&pd->rx_lock, flags);
1357 if (list_empty(&pd->rx_q))
1358 kick_sm(pd, SINK_WAIT_CAP_TIME);
1359 spin_unlock_irqrestore(&pd->rx_lock, flags);
1360 break;
1361
1362 case PE_SNK_EVALUATE_CAPABILITY:
1363 pd->pd_connected = true; /* we know peer is PD capable */
1364 pd->hard_reset_count = 0;
1365
1366 /* evaluate PDOs and select one */
1367 ret = pd_eval_src_caps(pd);
1368 if (ret < 0) {
1369 usbpd_err(&pd->dev, "Invalid src_caps received. Skipping request\n");
1370 break;
1371 }
1372 pd->current_state = PE_SNK_SELECT_CAPABILITY;
1373 /* fall-through */
1374
1375 case PE_SNK_SELECT_CAPABILITY:
1376 ret = pd_send_msg(pd, MSG_REQUEST, &pd->rdo, 1, SOP_MSG);
1377 if (ret) {
1378 usbpd_err(&pd->dev, "Error sending Request\n");
1379 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
1380 break;
1381 }
1382
1383 /* wait for ACCEPT */
1384 kick_sm(pd, SENDER_RESPONSE_TIME);
1385 break;
1386
1387 case PE_SNK_TRANSITION_SINK:
1388 /* wait for PS_RDY */
1389 kick_sm(pd, PS_TRANSITION_TIME);
1390 break;
1391
1392 case PE_SNK_READY:
1393 pd->in_explicit_contract = true;
Jack Phambe0f20c2017-10-05 12:51:26 -07001394
1395 if (pd->vdm_tx)
1396 kick_sm(pd, 0);
1397 else if (pd->current_dr == DR_DFP && pd->vdm_state == VDM_NONE)
1398 usbpd_send_svdm(pd, USBPD_SID,
1399 USBPD_SVDM_DISCOVER_IDENTITY,
1400 SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0);
1401
Jack Phamf4baeb12017-02-03 19:01:48 -08001402 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Jack Phamaf7d3842017-01-26 13:28:19 -08001403 complete(&pd->is_ready);
Jack Phamf4baeb12017-02-03 19:01:48 -08001404 dual_role_instance_changed(pd->dual_role);
1405 break;
1406
1407 case PE_SNK_TRANSITION_TO_DEFAULT:
1408 if (pd->current_dr != DR_UFP) {
1409 stop_usb_host(pd);
1410 start_usb_peripheral(pd);
1411 pd->current_dr = DR_UFP;
1412 pd_phy_update_roles(pd->current_dr, pd->current_pr);
1413 }
1414 if (pd->vconn_enabled) {
1415 regulator_disable(pd->vconn);
1416 pd->vconn_enabled = false;
1417 }
1418
1419 /* max time for hard reset to turn vbus off */
1420 kick_sm(pd, SNK_HARD_RESET_VBUS_OFF_TIME);
1421 break;
1422
1423 case PE_PRS_SNK_SRC_TRANSITION_TO_OFF:
1424 val.intval = pd->requested_current = 0; /* suspend charging */
1425 power_supply_set_property(pd->usb_psy,
1426 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
1427
1428 pd->in_explicit_contract = false;
1429
1430 /*
1431 * need to update PR bit in message header so that
1432 * proper GoodCRC is sent when receiving next PS_RDY
1433 */
1434 pd_phy_update_roles(pd->current_dr, PR_SRC);
1435
1436 /* wait for PS_RDY */
1437 kick_sm(pd, PS_SOURCE_OFF);
1438 break;
1439
1440 default:
1441 usbpd_dbg(&pd->dev, "No action for state %s\n",
1442 usbpd_state_strings[pd->current_state]);
1443 break;
1444 }
1445}
1446
1447int usbpd_register_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr)
1448{
1449 if (find_svid_handler(pd, hdlr->svid)) {
1450 usbpd_err(&pd->dev, "SVID 0x%04x already registered\n",
1451 hdlr->svid);
1452 return -EINVAL;
1453 }
1454
1455 /* require connect/disconnect callbacks be implemented */
1456 if (!hdlr->connect || !hdlr->disconnect) {
1457 usbpd_err(&pd->dev, "SVID 0x%04x connect/disconnect must be non-NULL\n",
1458 hdlr->svid);
1459 return -EINVAL;
1460 }
1461
Mayank Rana83443202017-08-31 15:38:03 -07001462 usbpd_dbg(&pd->dev, "registered handler(%pK) for SVID 0x%04x\n",
1463 hdlr, hdlr->svid);
1464 mutex_lock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08001465 list_add_tail(&hdlr->entry, &pd->svid_handlers);
Mayank Rana83443202017-08-31 15:38:03 -07001466 mutex_unlock(&pd->svid_handler_lock);
Mayank Rana6af43422017-07-18 12:09:02 -07001467 hdlr->request_usb_ss_lane = usbpd_release_ss_lane;
Jack Phamf4baeb12017-02-03 19:01:48 -08001468
1469 /* already connected with this SVID discovered? */
1470 if (pd->vdm_state >= DISCOVERED_SVIDS) {
1471 int i;
1472
1473 for (i = 0; i < pd->num_svids; i++) {
1474 if (pd->discovered_svids[i] == hdlr->svid) {
1475 hdlr->connect(hdlr);
1476 hdlr->discovered = true;
1477 break;
1478 }
1479 }
1480 }
1481
1482 return 0;
1483}
1484EXPORT_SYMBOL(usbpd_register_svid);
1485
1486void usbpd_unregister_svid(struct usbpd *pd, struct usbpd_svid_handler *hdlr)
1487{
Mayank Rana83443202017-08-31 15:38:03 -07001488
1489 usbpd_dbg(&pd->dev, "unregistered handler(%pK) for SVID 0x%04x\n",
1490 hdlr, hdlr->svid);
1491 mutex_lock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08001492 list_del_init(&hdlr->entry);
Mayank Rana83443202017-08-31 15:38:03 -07001493 mutex_unlock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08001494}
1495EXPORT_SYMBOL(usbpd_unregister_svid);
1496
1497int usbpd_send_vdm(struct usbpd *pd, u32 vdm_hdr, const u32 *vdos, int num_vdos)
1498{
1499 struct vdm_tx *vdm_tx;
1500
1501 if (!pd->in_explicit_contract || pd->vdm_tx)
1502 return -EBUSY;
1503
1504 vdm_tx = kzalloc(sizeof(*vdm_tx), GFP_KERNEL);
1505 if (!vdm_tx)
1506 return -ENOMEM;
1507
1508 vdm_tx->data[0] = vdm_hdr;
1509 if (vdos && num_vdos)
1510 memcpy(&vdm_tx->data[1], vdos, num_vdos * sizeof(u32));
1511 vdm_tx->size = num_vdos + 1; /* include the header */
1512
1513 /* VDM will get sent in PE_SRC/SNK_READY state handling */
1514 pd->vdm_tx = vdm_tx;
1515
1516 /* slight delay before queuing to prioritize handling of incoming VDM */
1517 kick_sm(pd, 2);
1518
1519 return 0;
1520}
1521EXPORT_SYMBOL(usbpd_send_vdm);
1522
1523int usbpd_send_svdm(struct usbpd *pd, u16 svid, u8 cmd,
1524 enum usbpd_svdm_cmd_type cmd_type, int obj_pos,
1525 const u32 *vdos, int num_vdos)
1526{
1527 u32 svdm_hdr = SVDM_HDR(svid, 0, obj_pos, cmd_type, cmd);
1528
1529 usbpd_dbg(&pd->dev, "VDM tx: svid:%x cmd:%x cmd_type:%x svdm_hdr:%x\n",
1530 svid, cmd, cmd_type, svdm_hdr);
1531
1532 return usbpd_send_vdm(pd, svdm_hdr, vdos, num_vdos);
1533}
1534EXPORT_SYMBOL(usbpd_send_svdm);
1535
1536static void handle_vdm_rx(struct usbpd *pd, struct rx_msg *rx_msg)
1537{
Jack Phamf3c1bd32017-08-02 18:32:23 -07001538 u32 vdm_hdr =
1539 rx_msg->data_len >= sizeof(u32) ? ((u32 *)rx_msg->payload)[0] : 0;
1540
1541 u32 *vdos = (u32 *)&rx_msg->payload[sizeof(u32)];
Jack Phamf4baeb12017-02-03 19:01:48 -08001542 u16 svid = VDM_HDR_SVID(vdm_hdr);
1543 u16 *psvid;
Jack Phamf3c1bd32017-08-02 18:32:23 -07001544 u8 i, num_vdos = PD_MSG_HDR_COUNT(rx_msg->hdr) - 1;
Jack Phamf4baeb12017-02-03 19:01:48 -08001545 u8 cmd = SVDM_HDR_CMD(vdm_hdr);
1546 u8 cmd_type = SVDM_HDR_CMD_TYPE(vdm_hdr);
1547 bool has_dp = false;
1548 struct usbpd_svid_handler *handler;
1549
1550 usbpd_dbg(&pd->dev, "VDM rx: svid:%x cmd:%x cmd_type:%x vdm_hdr:%x\n",
1551 svid, cmd, cmd_type, vdm_hdr);
1552
1553 /* if it's a supported SVID, pass the message to the handler */
1554 handler = find_svid_handler(pd, svid);
1555
1556 /* Unstructured VDM */
1557 if (!VDM_IS_SVDM(vdm_hdr)) {
1558 if (handler && handler->vdm_received)
1559 handler->vdm_received(handler, vdm_hdr, vdos, num_vdos);
1560 return;
1561 }
1562
1563 /* if this interrupts a previous exchange, abort queued response */
1564 if (cmd_type == SVDM_CMD_TYPE_INITIATOR && pd->vdm_tx) {
1565 usbpd_dbg(&pd->dev, "Discarding previously queued SVDM tx (SVID:0x%04x)\n",
1566 VDM_HDR_SVID(pd->vdm_tx->data[0]));
1567
1568 kfree(pd->vdm_tx);
1569 pd->vdm_tx = NULL;
1570 }
1571
1572 if (handler && handler->svdm_received) {
1573 handler->svdm_received(handler, cmd, cmd_type, vdos, num_vdos);
1574 return;
1575 }
1576
1577 /* Standard Discovery or unhandled messages go here */
1578 switch (cmd_type) {
1579 case SVDM_CMD_TYPE_INITIATOR:
1580 if (svid == USBPD_SID && cmd == USBPD_SVDM_DISCOVER_IDENTITY) {
1581 u32 tx_vdos[3] = {
1582 ID_HDR_USB_HOST | ID_HDR_USB_DEVICE |
1583 ID_HDR_PRODUCT_PER_MASK | ID_HDR_VID,
1584 0x0, /* TBD: Cert Stat VDO */
1585 (PROD_VDO_PID << 16),
1586 /* TBD: Get these from gadget */
1587 };
1588
1589 usbpd_send_svdm(pd, USBPD_SID, cmd,
1590 SVDM_CMD_TYPE_RESP_ACK, 0, tx_vdos, 3);
1591 } else if (cmd != USBPD_SVDM_ATTENTION) {
1592 usbpd_send_svdm(pd, svid, cmd, SVDM_CMD_TYPE_RESP_NAK,
1593 SVDM_HDR_OBJ_POS(vdm_hdr), NULL, 0);
1594 }
1595 break;
1596
1597 case SVDM_CMD_TYPE_RESP_ACK:
1598 if (svid != USBPD_SID) {
1599 usbpd_err(&pd->dev, "unhandled ACK for SVID:0x%x\n",
1600 svid);
1601 break;
1602 }
1603
1604 switch (cmd) {
1605 case USBPD_SVDM_DISCOVER_IDENTITY:
1606 kfree(pd->vdm_tx_retry);
1607 pd->vdm_tx_retry = NULL;
1608
1609 pd->vdm_state = DISCOVERED_ID;
1610 usbpd_send_svdm(pd, USBPD_SID,
1611 USBPD_SVDM_DISCOVER_SVIDS,
1612 SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0);
1613 break;
1614
1615 case USBPD_SVDM_DISCOVER_SVIDS:
1616 pd->vdm_state = DISCOVERED_SVIDS;
1617
1618 kfree(pd->vdm_tx_retry);
1619 pd->vdm_tx_retry = NULL;
1620
1621 if (!pd->discovered_svids) {
1622 pd->num_svids = 2 * num_vdos;
1623 pd->discovered_svids = kcalloc(pd->num_svids,
1624 sizeof(u16),
1625 GFP_KERNEL);
1626 if (!pd->discovered_svids) {
1627 usbpd_err(&pd->dev, "unable to allocate SVIDs\n");
1628 break;
1629 }
1630
1631 psvid = pd->discovered_svids;
1632 } else { /* handle > 12 SVIDs */
1633 void *ptr;
1634 size_t oldsize = pd->num_svids * sizeof(u16);
1635 size_t newsize = oldsize +
1636 (2 * num_vdos * sizeof(u16));
1637
1638 ptr = krealloc(pd->discovered_svids, newsize,
1639 GFP_KERNEL);
1640 if (!ptr) {
1641 usbpd_err(&pd->dev, "unable to realloc SVIDs\n");
1642 break;
1643 }
1644
1645 pd->discovered_svids = ptr;
1646 psvid = pd->discovered_svids + pd->num_svids;
1647 memset(psvid, 0, (2 * num_vdos));
1648 pd->num_svids += 2 * num_vdos;
1649 }
1650
1651 /* convert 32-bit VDOs to list of 16-bit SVIDs */
1652 for (i = 0; i < num_vdos * 2; i++) {
1653 /*
1654 * Within each 32-bit VDO,
1655 * SVID[i]: upper 16-bits
1656 * SVID[i+1]: lower 16-bits
1657 * where i is even.
1658 */
1659 if (!(i & 1))
1660 svid = vdos[i >> 1] >> 16;
1661 else
1662 svid = vdos[i >> 1] & 0xFFFF;
1663
1664 /*
1665 * There are some devices that incorrectly
1666 * swap the order of SVIDs within a VDO. So in
1667 * case of an odd-number of SVIDs it could end
1668 * up with SVID[i] as 0 while SVID[i+1] is
1669 * non-zero. Just skip over the zero ones.
1670 */
1671 if (svid) {
1672 usbpd_dbg(&pd->dev, "Discovered SVID: 0x%04x\n",
1673 svid);
1674 *psvid++ = svid;
1675 }
1676 }
1677
1678 /* if more than 12 SVIDs, resend the request */
1679 if (num_vdos == 6 && vdos[5] != 0) {
1680 usbpd_send_svdm(pd, USBPD_SID,
1681 USBPD_SVDM_DISCOVER_SVIDS,
1682 SVDM_CMD_TYPE_INITIATOR, 0,
1683 NULL, 0);
1684 break;
1685 }
1686
1687 /* now that all SVIDs are discovered, notify handlers */
1688 for (i = 0; i < pd->num_svids; i++) {
1689 svid = pd->discovered_svids[i];
1690 if (svid) {
1691 handler = find_svid_handler(pd, svid);
1692 if (handler) {
1693 handler->connect(handler);
1694 handler->discovered = true;
1695 }
1696 }
1697
1698 if (svid == 0xFF01)
1699 has_dp = true;
1700 }
Jack Phamf4baeb12017-02-03 19:01:48 -08001701 break;
1702
1703 default:
1704 usbpd_dbg(&pd->dev, "unhandled ACK for command:0x%x\n",
1705 cmd);
1706 break;
1707 }
1708 break;
1709
1710 case SVDM_CMD_TYPE_RESP_NAK:
1711 usbpd_info(&pd->dev, "VDM NAK received for SVID:0x%04x command:0x%x\n",
1712 svid, cmd);
1713
1714 switch (cmd) {
1715 case USBPD_SVDM_DISCOVER_IDENTITY:
1716 case USBPD_SVDM_DISCOVER_SVIDS:
Jack Phamf4baeb12017-02-03 19:01:48 -08001717 break;
1718 default:
1719 break;
1720 }
1721
1722 break;
1723
1724 case SVDM_CMD_TYPE_RESP_BUSY:
1725 switch (cmd) {
1726 case USBPD_SVDM_DISCOVER_IDENTITY:
1727 case USBPD_SVDM_DISCOVER_SVIDS:
1728 if (!pd->vdm_tx_retry) {
1729 usbpd_err(&pd->dev, "Discover command %d VDM was unexpectedly freed\n",
1730 cmd);
1731 break;
1732 }
1733
1734 /* wait tVDMBusy, then retry */
1735 pd->vdm_tx = pd->vdm_tx_retry;
1736 pd->vdm_tx_retry = NULL;
1737 kick_sm(pd, VDM_BUSY_TIME);
1738 break;
1739 default:
1740 break;
1741 }
1742 break;
1743 }
1744}
1745
1746static void handle_vdm_tx(struct usbpd *pd)
1747{
1748 int ret;
1749 unsigned long flags;
1750
1751 /* only send one VDM at a time */
1752 if (pd->vdm_tx) {
1753 u32 vdm_hdr = pd->vdm_tx->data[0];
1754
1755 /* bail out and try again later if a message just arrived */
1756 spin_lock_irqsave(&pd->rx_lock, flags);
1757 if (!list_empty(&pd->rx_q)) {
1758 spin_unlock_irqrestore(&pd->rx_lock, flags);
1759 return;
1760 }
1761 spin_unlock_irqrestore(&pd->rx_lock, flags);
1762
1763 ret = pd_send_msg(pd, MSG_VDM, pd->vdm_tx->data,
1764 pd->vdm_tx->size, SOP_MSG);
1765 if (ret) {
1766 usbpd_err(&pd->dev, "Error (%d) sending VDM command %d\n",
1767 ret, SVDM_HDR_CMD(pd->vdm_tx->data[0]));
1768
1769 /* retry when hitting PE_SRC/SNK_Ready again */
1770 if (ret != -EBUSY)
1771 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
1772 PE_SRC_SEND_SOFT_RESET :
1773 PE_SNK_SEND_SOFT_RESET);
1774
1775 return;
1776 }
1777
1778 /*
1779 * special case: keep initiated Discover ID/SVIDs
1780 * around in case we need to re-try when receiving BUSY
1781 */
1782 if (VDM_IS_SVDM(vdm_hdr) &&
1783 SVDM_HDR_CMD_TYPE(vdm_hdr) == SVDM_CMD_TYPE_INITIATOR &&
1784 SVDM_HDR_CMD(vdm_hdr) <= USBPD_SVDM_DISCOVER_SVIDS) {
1785 if (pd->vdm_tx_retry) {
1786 usbpd_dbg(&pd->dev, "Previous Discover VDM command %d not ACKed/NAKed\n",
1787 SVDM_HDR_CMD(
1788 pd->vdm_tx_retry->data[0]));
1789 kfree(pd->vdm_tx_retry);
1790 }
1791 pd->vdm_tx_retry = pd->vdm_tx;
1792 } else {
1793 kfree(pd->vdm_tx);
1794 }
1795
1796 pd->vdm_tx = NULL;
1797 }
1798}
1799
1800static void reset_vdm_state(struct usbpd *pd)
1801{
1802 struct usbpd_svid_handler *handler;
1803
Mayank Rana83443202017-08-31 15:38:03 -07001804 mutex_lock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08001805 list_for_each_entry(handler, &pd->svid_handlers, entry) {
1806 if (handler->discovered) {
1807 handler->disconnect(handler);
1808 handler->discovered = false;
1809 }
1810 }
1811
Mayank Rana83443202017-08-31 15:38:03 -07001812 mutex_unlock(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08001813 pd->vdm_state = VDM_NONE;
1814 kfree(pd->vdm_tx_retry);
1815 pd->vdm_tx_retry = NULL;
1816 kfree(pd->discovered_svids);
1817 pd->discovered_svids = NULL;
1818 pd->num_svids = 0;
1819 kfree(pd->vdm_tx);
1820 pd->vdm_tx = NULL;
Vijayavardhan Vennapusadea32272018-04-05 14:25:39 +05301821 pd->ss_lane_svid = 0x0;
Jack Phamf4baeb12017-02-03 19:01:48 -08001822}
1823
1824static void dr_swap(struct usbpd *pd)
1825{
1826 reset_vdm_state(pd);
Mayank Rana45b0bc42017-07-25 15:38:05 -07001827 usbpd_dbg(&pd->dev, "dr_swap: current_dr(%d)\n", pd->current_dr);
Jack Phamf4baeb12017-02-03 19:01:48 -08001828
1829 if (pd->current_dr == DR_DFP) {
1830 stop_usb_host(pd);
1831 start_usb_peripheral(pd);
1832 pd->current_dr = DR_UFP;
1833 } else if (pd->current_dr == DR_UFP) {
1834 stop_usb_peripheral(pd);
Mayank Rana45b0bc42017-07-25 15:38:05 -07001835 start_usb_host(pd, true);
Jack Phamf4baeb12017-02-03 19:01:48 -08001836 pd->current_dr = DR_DFP;
1837
Jack Phamf4baeb12017-02-03 19:01:48 -08001838 usbpd_send_svdm(pd, USBPD_SID, USBPD_SVDM_DISCOVER_IDENTITY,
1839 SVDM_CMD_TYPE_INITIATOR, 0, NULL, 0);
1840 }
1841
1842 pd_phy_update_roles(pd->current_dr, pd->current_pr);
Jack Phame232bde2017-03-02 11:37:00 -08001843 dual_role_instance_changed(pd->dual_role);
Jack Phamf4baeb12017-02-03 19:01:48 -08001844}
1845
1846
1847static void vconn_swap(struct usbpd *pd)
1848{
1849 int ret;
1850
1851 if (pd->vconn_enabled) {
1852 pd->current_state = PE_VCS_WAIT_FOR_VCONN;
1853 kick_sm(pd, VCONN_ON_TIME);
1854 } else {
1855 ret = regulator_enable(pd->vconn);
1856 if (ret) {
1857 usbpd_err(&pd->dev, "Unable to enable vconn\n");
1858 return;
1859 }
1860
1861 pd->vconn_enabled = true;
1862
1863 /*
1864 * Small delay to ensure Vconn has ramped up. This is well
1865 * below tVCONNSourceOn (100ms) so we still send PS_RDY within
1866 * the allowed time.
1867 */
1868 usleep_range(5000, 10000);
1869
1870 ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG);
1871 if (ret) {
1872 usbpd_err(&pd->dev, "Error sending PS_RDY\n");
1873 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
1874 PE_SRC_SEND_SOFT_RESET :
1875 PE_SNK_SEND_SOFT_RESET);
1876 return;
1877 }
1878 }
1879}
1880
1881static int enable_vbus(struct usbpd *pd)
1882{
1883 union power_supply_propval val = {0};
1884 int count = 100;
1885 int ret;
1886
1887 if (!check_vsafe0v)
1888 goto enable_reg;
1889
1890 /*
1891 * Check to make sure there's no lingering charge on
1892 * VBUS before enabling it as a source. If so poll here
1893 * until it goes below VSafe0V (0.8V) before proceeding.
1894 */
1895 while (count--) {
1896 ret = power_supply_get_property(pd->usb_psy,
1897 POWER_SUPPLY_PROP_VOLTAGE_NOW, &val);
1898 if (ret || val.intval <= 800000)
1899 break;
1900 usleep_range(20000, 30000);
1901 }
1902
1903 if (count < 99)
1904 msleep(100); /* need to wait an additional tCCDebounce */
1905
1906enable_reg:
1907 ret = regulator_enable(pd->vbus);
1908 if (ret)
1909 usbpd_err(&pd->dev, "Unable to enable vbus (%d)\n", ret);
1910 else
1911 pd->vbus_enabled = true;
1912
1913 return ret;
1914}
1915
1916static inline void rx_msg_cleanup(struct usbpd *pd)
1917{
1918 struct rx_msg *msg, *tmp;
1919 unsigned long flags;
1920
1921 spin_lock_irqsave(&pd->rx_lock, flags);
1922 list_for_each_entry_safe(msg, tmp, &pd->rx_q, entry) {
1923 list_del(&msg->entry);
1924 kfree(msg);
1925 }
1926 spin_unlock_irqrestore(&pd->rx_lock, flags);
1927}
1928
1929/* For PD 3.0, check SinkTxOk before allowing initiating AMS */
1930static inline bool is_sink_tx_ok(struct usbpd *pd)
1931{
1932 if (pd->spec_rev == USBPD_REV_30)
1933 return pd->typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH;
1934
1935 return true;
1936}
1937
1938/* Handles current state and determines transitions */
1939static void usbpd_sm(struct work_struct *w)
1940{
1941 struct usbpd *pd = container_of(w, struct usbpd, sm_work);
1942 union power_supply_propval val = {0};
1943 int ret;
1944 struct rx_msg *rx_msg = NULL;
1945 unsigned long flags;
1946
1947 usbpd_dbg(&pd->dev, "handle state %s\n",
1948 usbpd_state_strings[pd->current_state]);
1949
1950 hrtimer_cancel(&pd->timer);
1951 pd->sm_queued = false;
1952
1953 spin_lock_irqsave(&pd->rx_lock, flags);
1954 if (!list_empty(&pd->rx_q)) {
1955 rx_msg = list_first_entry(&pd->rx_q, struct rx_msg, entry);
1956 list_del(&rx_msg->entry);
1957 }
1958 spin_unlock_irqrestore(&pd->rx_lock, flags);
1959
1960 /* Disconnect? */
1961 if (pd->current_pr == PR_NONE) {
1962 if (pd->current_state == PE_UNKNOWN)
1963 goto sm_done;
1964
Hemant Kumar86bd10f2017-05-24 12:25:15 -07001965 if (pd->vconn_enabled) {
1966 regulator_disable(pd->vconn);
1967 pd->vconn_enabled = false;
1968 }
1969
Jack Phamf4baeb12017-02-03 19:01:48 -08001970 usbpd_info(&pd->dev, "USB Type-C disconnect\n");
1971
1972 if (pd->pd_phy_opened) {
1973 pd_phy_close();
1974 pd->pd_phy_opened = false;
1975 }
1976
1977 pd->in_pr_swap = false;
1978 pd->pd_connected = false;
1979 pd->in_explicit_contract = false;
1980 pd->hard_reset_recvd = false;
1981 pd->caps_count = 0;
1982 pd->hard_reset_count = 0;
Jack Phamf4baeb12017-02-03 19:01:48 -08001983 pd->requested_voltage = 0;
1984 pd->requested_current = 0;
Jack Phamf6c02da2017-01-31 15:23:56 -08001985 pd->selected_pdo = pd->requested_pdo = 0;
Jack Phamf4baeb12017-02-03 19:01:48 -08001986 memset(&pd->received_pdos, 0, sizeof(pd->received_pdos));
1987 rx_msg_cleanup(pd);
1988
Jack Phamf4baeb12017-02-03 19:01:48 -08001989 power_supply_set_property(pd->usb_psy,
1990 POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val);
1991
1992 power_supply_set_property(pd->usb_psy,
1993 POWER_SUPPLY_PROP_PD_USB_SUSPEND_SUPPORTED,
1994 &val);
1995
1996 power_supply_set_property(pd->usb_psy,
1997 POWER_SUPPLY_PROP_PD_ACTIVE, &val);
1998
1999 if (pd->vbus_enabled) {
2000 regulator_disable(pd->vbus);
2001 pd->vbus_enabled = false;
2002 }
2003
Mayank Rana86cb20292017-06-01 11:36:07 -07002004 reset_vdm_state(pd);
Jack Phamf4baeb12017-02-03 19:01:48 -08002005 if (pd->current_dr == DR_UFP)
2006 stop_usb_peripheral(pd);
2007 else if (pd->current_dr == DR_DFP)
2008 stop_usb_host(pd);
2009
Jack Phamf4baeb12017-02-03 19:01:48 -08002010 pd->current_dr = DR_NONE;
2011
Jack Phamf4baeb12017-02-03 19:01:48 -08002012 if (pd->current_state == PE_ERROR_RECOVERY)
2013 /* forced disconnect, wait before resetting to DRP */
2014 usleep_range(ERROR_RECOVERY_TIME * USEC_PER_MSEC,
2015 (ERROR_RECOVERY_TIME + 5) * USEC_PER_MSEC);
2016
Jack Pham01c5cfd2017-06-06 22:22:18 -07002017 val.intval = 0;
2018 power_supply_set_property(pd->usb_psy,
2019 POWER_SUPPLY_PROP_PR_SWAP, &val);
2020
Jack Phamf4baeb12017-02-03 19:01:48 -08002021 /* set due to dual_role class "mode" change */
2022 if (pd->forced_pr != POWER_SUPPLY_TYPEC_PR_NONE)
2023 val.intval = pd->forced_pr;
Hemant Kumarbe746222017-07-20 13:51:49 -07002024 else if (rev3_sink_only)
2025 val.intval = POWER_SUPPLY_TYPEC_PR_SINK;
Jack Phamf4baeb12017-02-03 19:01:48 -08002026 else
2027 /* Set CC back to DRP toggle */
2028 val.intval = POWER_SUPPLY_TYPEC_PR_DUAL;
2029
2030 power_supply_set_property(pd->usb_psy,
2031 POWER_SUPPLY_PROP_TYPEC_POWER_ROLE, &val);
2032 pd->forced_pr = POWER_SUPPLY_TYPEC_PR_NONE;
2033
2034 pd->current_state = PE_UNKNOWN;
2035
2036 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
2037 dual_role_instance_changed(pd->dual_role);
2038
2039 goto sm_done;
2040 }
2041
2042 /* Hard reset? */
2043 if (pd->hard_reset_recvd) {
2044 pd->hard_reset_recvd = false;
2045
Jack Pham6de20802017-05-24 13:44:56 -07002046 if (pd->requested_current) {
2047 val.intval = pd->requested_current = 0;
2048 power_supply_set_property(pd->usb_psy,
2049 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
2050 }
2051
2052 pd->requested_voltage = 5000000;
2053 val.intval = pd->requested_voltage;
2054 power_supply_set_property(pd->usb_psy,
Nicholas Troast7f55c922017-07-25 13:18:03 -07002055 POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, &val);
Jack Pham6de20802017-05-24 13:44:56 -07002056
Jack Phamf4baeb12017-02-03 19:01:48 -08002057 pd->in_pr_swap = false;
Jack Pham857ee682017-05-25 11:53:36 -07002058 val.intval = 0;
2059 power_supply_set_property(pd->usb_psy,
2060 POWER_SUPPLY_PROP_PR_SWAP, &val);
2061
Jack Phamf6c02da2017-01-31 15:23:56 -08002062 pd->in_explicit_contract = false;
2063 pd->selected_pdo = pd->requested_pdo = 0;
2064 pd->rdo = 0;
Jack Phamf4baeb12017-02-03 19:01:48 -08002065 rx_msg_cleanup(pd);
2066 reset_vdm_state(pd);
Jack Phamf6c02da2017-01-31 15:23:56 -08002067 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Jack Phamf4baeb12017-02-03 19:01:48 -08002068
2069 if (pd->current_pr == PR_SINK) {
2070 usbpd_set_state(pd, PE_SNK_TRANSITION_TO_DEFAULT);
2071 } else {
Jack Pham4b323282017-11-03 12:24:59 -07002072 s64 delta = ktime_ms_delta(ktime_get(),
2073 pd->hard_reset_recvd_time);
Jack Phamf4baeb12017-02-03 19:01:48 -08002074 pd->current_state = PE_SRC_TRANSITION_TO_DEFAULT;
Jack Pham4b323282017-11-03 12:24:59 -07002075 if (delta >= PS_HARD_RESET_TIME)
2076 kick_sm(pd, 0);
2077 else
2078 kick_sm(pd, PS_HARD_RESET_TIME - (int)delta);
Jack Phamf4baeb12017-02-03 19:01:48 -08002079 }
2080
2081 goto sm_done;
2082 }
2083
2084 /* Soft reset? */
2085 if (IS_CTRL(rx_msg, MSG_SOFT_RESET)) {
2086 usbpd_dbg(&pd->dev, "Handle soft reset\n");
2087
2088 if (pd->current_pr == PR_SRC)
2089 pd->current_state = PE_SRC_SOFT_RESET;
2090 else if (pd->current_pr == PR_SINK)
2091 pd->current_state = PE_SNK_SOFT_RESET;
2092 }
2093
2094 switch (pd->current_state) {
2095 case PE_UNKNOWN:
Jack Phamc4be61c2017-10-10 16:59:32 -07002096 val.intval = 0;
2097 power_supply_set_property(pd->usb_psy,
2098 POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val);
2099
Jack Phamf4baeb12017-02-03 19:01:48 -08002100 if (pd->current_pr == PR_SINK) {
2101 usbpd_set_state(pd, PE_SNK_STARTUP);
2102 } else if (pd->current_pr == PR_SRC) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002103 if (!pd->vconn_enabled &&
2104 pd->typec_mode ==
2105 POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE) {
2106 ret = regulator_enable(pd->vconn);
2107 if (ret)
2108 usbpd_err(&pd->dev, "Unable to enable vconn\n");
2109 else
2110 pd->vconn_enabled = true;
2111 }
Vijayavardhan Vennapusa326b4792017-07-13 12:10:50 +05302112 enable_vbus(pd);
Jack Phamf4baeb12017-02-03 19:01:48 -08002113
2114 usbpd_set_state(pd, PE_SRC_STARTUP);
2115 }
2116 break;
2117
2118 case PE_SRC_STARTUP:
2119 usbpd_set_state(pd, PE_SRC_STARTUP);
2120 break;
2121
2122 case PE_SRC_SEND_CAPABILITIES:
2123 ret = pd_send_msg(pd, MSG_SOURCE_CAPABILITIES, default_src_caps,
2124 ARRAY_SIZE(default_src_caps), SOP_MSG);
2125 if (ret) {
2126 pd->caps_count++;
Mayank Rana38e9b252017-03-23 12:35:57 -07002127 if (pd->caps_count >= PD_CAPS_COUNT) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002128 usbpd_dbg(&pd->dev, "Src CapsCounter exceeded, disabling PD\n");
2129 usbpd_set_state(pd, PE_SRC_DISABLED);
2130
2131 val.intval = 0;
2132 power_supply_set_property(pd->usb_psy,
2133 POWER_SUPPLY_PROP_PD_ACTIVE,
2134 &val);
2135 break;
2136 }
2137
2138 kick_sm(pd, SRC_CAP_TIME);
2139 break;
2140 }
2141
2142 /* transmit was successful if GoodCRC was received */
2143 pd->caps_count = 0;
2144 pd->hard_reset_count = 0;
2145 pd->pd_connected = true; /* we know peer is PD capable */
2146
2147 /* wait for REQUEST */
2148 pd->current_state = PE_SRC_SEND_CAPABILITIES_WAIT;
2149 kick_sm(pd, SENDER_RESPONSE_TIME);
2150
2151 val.intval = 1;
2152 power_supply_set_property(pd->usb_psy,
2153 POWER_SUPPLY_PROP_PD_ACTIVE, &val);
2154 break;
2155
2156 case PE_SRC_SEND_CAPABILITIES_WAIT:
2157 if (IS_DATA(rx_msg, MSG_REQUEST)) {
Jack Phamf3c1bd32017-08-02 18:32:23 -07002158 pd->rdo = *(u32 *)rx_msg->payload;
Jack Phamf4baeb12017-02-03 19:01:48 -08002159 usbpd_set_state(pd, PE_SRC_NEGOTIATE_CAPABILITY);
2160 } else if (rx_msg) {
2161 usbpd_err(&pd->dev, "Unexpected message received\n");
2162 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2163 } else {
2164 usbpd_set_state(pd, PE_SRC_HARD_RESET);
2165 }
2166 break;
2167
2168 case PE_SRC_READY:
2169 if (IS_CTRL(rx_msg, MSG_GET_SOURCE_CAP)) {
Hemant Kumaree2db9b2017-05-30 14:52:18 -07002170 pd->current_state = PE_SRC_SEND_CAPABILITIES;
2171 kick_sm(pd, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -08002172 } else if (IS_CTRL(rx_msg, MSG_GET_SINK_CAP)) {
2173 ret = pd_send_msg(pd, MSG_SINK_CAPABILITIES,
Jack Phamee1f9052017-01-26 12:27:07 -08002174 pd->sink_caps, pd->num_sink_caps,
2175 SOP_MSG);
Jack Phamf4baeb12017-02-03 19:01:48 -08002176 if (ret) {
2177 usbpd_err(&pd->dev, "Error sending Sink Caps\n");
2178 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2179 }
2180 } else if (IS_DATA(rx_msg, MSG_REQUEST)) {
Jack Phamf3c1bd32017-08-02 18:32:23 -07002181 pd->rdo = *(u32 *)rx_msg->payload;
Jack Phamf4baeb12017-02-03 19:01:48 -08002182 usbpd_set_state(pd, PE_SRC_NEGOTIATE_CAPABILITY);
2183 } else if (IS_CTRL(rx_msg, MSG_DR_SWAP)) {
2184 if (pd->vdm_state == MODE_ENTERED) {
2185 usbpd_set_state(pd, PE_SRC_HARD_RESET);
2186 break;
2187 }
2188
2189 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2190 if (ret) {
2191 usbpd_err(&pd->dev, "Error sending Accept\n");
2192 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2193 break;
2194 }
2195
2196 dr_swap(pd);
2197 } else if (IS_CTRL(rx_msg, MSG_PR_SWAP)) {
2198 /* lock in current mode */
2199 set_power_role(pd, pd->current_pr);
2200
2201 /* we'll happily accept Src->Sink requests anytime */
2202 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2203 if (ret) {
2204 usbpd_err(&pd->dev, "Error sending Accept\n");
2205 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2206 break;
2207 }
2208
2209 pd->current_state = PE_PRS_SRC_SNK_TRANSITION_TO_OFF;
2210 kick_sm(pd, SRC_TRANSITION_TIME);
2211 break;
2212 } else if (IS_CTRL(rx_msg, MSG_VCONN_SWAP)) {
2213 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2214 if (ret) {
2215 usbpd_err(&pd->dev, "Error sending Accept\n");
2216 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2217 break;
2218 }
2219
2220 vconn_swap(pd);
2221 } else if (IS_DATA(rx_msg, MSG_VDM)) {
2222 handle_vdm_rx(pd, rx_msg);
Jack Pham69a427e2017-08-04 12:26:51 -07002223 } else if (rx_msg && pd->spec_rev == USBPD_REV_30) {
2224 /* unhandled messages */
2225 ret = pd_send_msg(pd, MSG_NOT_SUPPORTED, NULL, 0,
2226 SOP_MSG);
2227 if (ret) {
2228 usbpd_err(&pd->dev, "Error sending Not supported\n");
2229 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2230 }
2231 break;
Jack Phamf4baeb12017-02-03 19:01:48 -08002232 } else if (pd->send_pr_swap) {
2233 pd->send_pr_swap = false;
2234 ret = pd_send_msg(pd, MSG_PR_SWAP, NULL, 0, SOP_MSG);
2235 if (ret) {
2236 dev_err(&pd->dev, "Error sending PR Swap\n");
2237 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2238 break;
2239 }
2240
2241 pd->current_state = PE_PRS_SRC_SNK_SEND_SWAP;
2242 kick_sm(pd, SENDER_RESPONSE_TIME);
2243 } else if (pd->send_dr_swap) {
2244 pd->send_dr_swap = false;
2245 ret = pd_send_msg(pd, MSG_DR_SWAP, NULL, 0, SOP_MSG);
2246 if (ret) {
2247 dev_err(&pd->dev, "Error sending DR Swap\n");
2248 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2249 break;
2250 }
2251
2252 pd->current_state = PE_DRS_SEND_DR_SWAP;
2253 kick_sm(pd, SENDER_RESPONSE_TIME);
2254 } else {
2255 handle_vdm_tx(pd);
2256 }
2257 break;
2258
2259 case PE_SRC_TRANSITION_TO_DEFAULT:
2260 if (pd->vconn_enabled)
2261 regulator_disable(pd->vconn);
Jack Phamc4be61c2017-10-10 16:59:32 -07002262 pd->vconn_enabled = false;
2263
Jack Phamf4baeb12017-02-03 19:01:48 -08002264 if (pd->vbus_enabled)
2265 regulator_disable(pd->vbus);
Jack Phamc4be61c2017-10-10 16:59:32 -07002266 pd->vbus_enabled = false;
Jack Phamf4baeb12017-02-03 19:01:48 -08002267
2268 if (pd->current_dr != DR_DFP) {
Jack Pham4e9dff72017-04-04 18:05:53 -07002269 extcon_set_state_sync(pd->extcon, EXTCON_USB, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -08002270 pd->current_dr = DR_DFP;
2271 pd_phy_update_roles(pd->current_dr, pd->current_pr);
2272 }
2273
Jack Phamc4be61c2017-10-10 16:59:32 -07002274 /* PE_UNKNOWN will turn on VBUS and go back to PE_SRC_STARTUP */
2275 pd->current_state = PE_UNKNOWN;
2276 kick_sm(pd, SRC_RECOVER_TIME);
Jack Phamf4baeb12017-02-03 19:01:48 -08002277 break;
2278
2279 case PE_SRC_HARD_RESET:
2280 val.intval = 1;
2281 power_supply_set_property(pd->usb_psy,
2282 POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val);
2283
2284 pd_send_hard_reset(pd);
2285 pd->in_explicit_contract = false;
Jack Phamf6c02da2017-01-31 15:23:56 -08002286 pd->rdo = 0;
Jack Phamf4baeb12017-02-03 19:01:48 -08002287 rx_msg_cleanup(pd);
2288 reset_vdm_state(pd);
Jack Phamf6c02da2017-01-31 15:23:56 -08002289 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Jack Phamf4baeb12017-02-03 19:01:48 -08002290
2291 pd->current_state = PE_SRC_TRANSITION_TO_DEFAULT;
2292 kick_sm(pd, PS_HARD_RESET_TIME);
2293 break;
2294
2295 case PE_SNK_STARTUP:
2296 usbpd_set_state(pd, PE_SNK_STARTUP);
2297 break;
2298
2299 case PE_SNK_DISCOVERY:
2300 if (!rx_msg) {
2301 if (pd->vbus_present)
2302 usbpd_set_state(pd,
2303 PE_SNK_WAIT_FOR_CAPABILITIES);
2304
2305 /*
2306 * Handle disconnection in the middle of PR_Swap.
2307 * Since in psy_changed() if pd->in_pr_swap is true
2308 * we ignore the typec_mode==NONE change since that is
2309 * expected to happen. However if the cable really did
2310 * get disconnected we need to check for it here after
2311 * waiting for VBUS presence times out.
2312 */
2313 if (!pd->typec_mode) {
2314 pd->current_pr = PR_NONE;
2315 kick_sm(pd, 0);
2316 }
2317
2318 break;
2319 }
2320 /* else fall-through */
2321
2322 case PE_SNK_WAIT_FOR_CAPABILITIES:
2323 pd->in_pr_swap = false;
Jack Pham857ee682017-05-25 11:53:36 -07002324 val.intval = 0;
2325 power_supply_set_property(pd->usb_psy,
2326 POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002327
2328 if (IS_DATA(rx_msg, MSG_SOURCE_CAPABILITIES)) {
2329 val.intval = 0;
2330 power_supply_set_property(pd->usb_psy,
2331 POWER_SUPPLY_PROP_PD_IN_HARD_RESET,
2332 &val);
2333
2334 /* save the PDOs so userspace can further evaluate */
Jack Phame9e1f132017-10-23 09:47:49 -07002335 memset(&pd->received_pdos, 0,
Jack Phamf4baeb12017-02-03 19:01:48 -08002336 sizeof(pd->received_pdos));
Jack Phame9e1f132017-10-23 09:47:49 -07002337 memcpy(&pd->received_pdos, rx_msg->payload,
2338 min_t(size_t, rx_msg->data_len,
2339 sizeof(pd->received_pdos)));
Jack Phamf4baeb12017-02-03 19:01:48 -08002340 pd->src_cap_id++;
2341
2342 usbpd_set_state(pd, PE_SNK_EVALUATE_CAPABILITY);
2343
2344 val.intval = 1;
2345 power_supply_set_property(pd->usb_psy,
2346 POWER_SUPPLY_PROP_PD_ACTIVE, &val);
2347 } else if (pd->hard_reset_count < 3) {
2348 usbpd_set_state(pd, PE_SNK_HARD_RESET);
Jack Phamf4baeb12017-02-03 19:01:48 -08002349 } else {
2350 usbpd_dbg(&pd->dev, "Sink hard reset count exceeded, disabling PD\n");
2351
2352 val.intval = 0;
2353 power_supply_set_property(pd->usb_psy,
2354 POWER_SUPPLY_PROP_PD_IN_HARD_RESET,
2355 &val);
2356
2357 val.intval = 0;
2358 power_supply_set_property(pd->usb_psy,
2359 POWER_SUPPLY_PROP_PD_ACTIVE, &val);
2360 }
2361 break;
2362
2363 case PE_SNK_SELECT_CAPABILITY:
2364 if (IS_CTRL(rx_msg, MSG_ACCEPT)) {
Jack Pham78c869a2017-02-14 16:10:28 -08002365 u32 pdo = pd->received_pdos[pd->requested_pdo - 1];
2366 bool same_pps = (pd->selected_pdo == pd->requested_pdo)
2367 && (PD_SRC_PDO_TYPE(pdo) ==
2368 PD_SRC_PDO_TYPE_AUGMENTED);
2369
Jack Phamf4baeb12017-02-03 19:01:48 -08002370 usbpd_set_state(pd, PE_SNK_TRANSITION_SINK);
2371
2372 /* prepare for voltage increase/decrease */
2373 val.intval = pd->requested_voltage;
2374 power_supply_set_property(pd->usb_psy,
2375 pd->requested_voltage >= pd->current_voltage ?
Nicholas Troast7f55c922017-07-25 13:18:03 -07002376 POWER_SUPPLY_PROP_PD_VOLTAGE_MAX :
2377 POWER_SUPPLY_PROP_PD_VOLTAGE_MIN,
Jack Phamf4baeb12017-02-03 19:01:48 -08002378 &val);
2379
2380 /*
Jack Pham78c869a2017-02-14 16:10:28 -08002381 * if changing voltages (not within the same PPS PDO),
2382 * we must lower input current to pSnkStdby (2.5W).
2383 * Calculate it and set PD_CURRENT_MAX accordingly.
Jack Phamf4baeb12017-02-03 19:01:48 -08002384 */
Jack Pham78c869a2017-02-14 16:10:28 -08002385 if (!same_pps &&
2386 pd->requested_voltage != pd->current_voltage) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002387 int mv = max(pd->requested_voltage,
2388 pd->current_voltage) / 1000;
2389 val.intval = (2500000 / mv) * 1000;
2390 power_supply_set_property(pd->usb_psy,
2391 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
2392 } else {
2393 /* decreasing current? */
2394 ret = power_supply_get_property(pd->usb_psy,
2395 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
2396 if (!ret &&
2397 pd->requested_current < val.intval) {
2398 val.intval =
2399 pd->requested_current * 1000;
2400 power_supply_set_property(pd->usb_psy,
2401 POWER_SUPPLY_PROP_PD_CURRENT_MAX,
2402 &val);
2403 }
2404 }
2405
2406 pd->selected_pdo = pd->requested_pdo;
2407 } else if (IS_CTRL(rx_msg, MSG_REJECT) ||
2408 IS_CTRL(rx_msg, MSG_WAIT)) {
2409 if (pd->in_explicit_contract)
2410 usbpd_set_state(pd, PE_SNK_READY);
2411 else
2412 usbpd_set_state(pd,
2413 PE_SNK_WAIT_FOR_CAPABILITIES);
2414 } else if (rx_msg) {
2415 usbpd_err(&pd->dev, "Invalid response to sink request\n");
2416 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2417 } else {
2418 /* timed out; go to hard reset */
2419 usbpd_set_state(pd, PE_SNK_HARD_RESET);
2420 }
2421 break;
2422
2423 case PE_SNK_TRANSITION_SINK:
2424 if (IS_CTRL(rx_msg, MSG_PS_RDY)) {
2425 val.intval = pd->requested_voltage;
2426 power_supply_set_property(pd->usb_psy,
2427 pd->requested_voltage >= pd->current_voltage ?
Nicholas Troast7f55c922017-07-25 13:18:03 -07002428 POWER_SUPPLY_PROP_PD_VOLTAGE_MIN :
2429 POWER_SUPPLY_PROP_PD_VOLTAGE_MAX, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002430 pd->current_voltage = pd->requested_voltage;
2431
2432 /* resume charging */
2433 val.intval = pd->requested_current * 1000; /* mA->uA */
2434 power_supply_set_property(pd->usb_psy,
2435 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
2436
2437 usbpd_set_state(pd, PE_SNK_READY);
2438 } else {
2439 /* timed out; go to hard reset */
2440 usbpd_set_state(pd, PE_SNK_HARD_RESET);
2441 }
2442 break;
2443
2444 case PE_SNK_READY:
2445 if (IS_DATA(rx_msg, MSG_SOURCE_CAPABILITIES)) {
2446 /* save the PDOs so userspace can further evaluate */
Jack Phame9e1f132017-10-23 09:47:49 -07002447 memset(&pd->received_pdos, 0,
Jack Phamf4baeb12017-02-03 19:01:48 -08002448 sizeof(pd->received_pdos));
Jack Phame9e1f132017-10-23 09:47:49 -07002449 memcpy(&pd->received_pdos, rx_msg->payload,
2450 min_t(size_t, rx_msg->data_len,
2451 sizeof(pd->received_pdos)));
Jack Phamf4baeb12017-02-03 19:01:48 -08002452 pd->src_cap_id++;
2453
2454 usbpd_set_state(pd, PE_SNK_EVALUATE_CAPABILITY);
2455 } else if (IS_CTRL(rx_msg, MSG_GET_SINK_CAP)) {
2456 ret = pd_send_msg(pd, MSG_SINK_CAPABILITIES,
Jack Phamee1f9052017-01-26 12:27:07 -08002457 pd->sink_caps, pd->num_sink_caps,
2458 SOP_MSG);
Jack Phamf4baeb12017-02-03 19:01:48 -08002459 if (ret) {
2460 usbpd_err(&pd->dev, "Error sending Sink Caps\n");
2461 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2462 }
Jack Pham69a427e2017-08-04 12:26:51 -07002463 } else if (IS_CTRL(rx_msg, MSG_GET_SOURCE_CAP) &&
2464 pd->spec_rev == USBPD_REV_20) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002465 ret = pd_send_msg(pd, MSG_SOURCE_CAPABILITIES,
2466 default_src_caps,
2467 ARRAY_SIZE(default_src_caps), SOP_MSG);
2468 if (ret) {
2469 usbpd_err(&pd->dev, "Error sending SRC CAPs\n");
2470 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2471 break;
2472 }
2473 } else if (IS_CTRL(rx_msg, MSG_DR_SWAP)) {
2474 if (pd->vdm_state == MODE_ENTERED) {
2475 usbpd_set_state(pd, PE_SNK_HARD_RESET);
2476 break;
2477 }
2478
2479 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2480 if (ret) {
2481 usbpd_err(&pd->dev, "Error sending Accept\n");
2482 usbpd_set_state(pd, PE_SRC_SEND_SOFT_RESET);
2483 break;
2484 }
2485
2486 dr_swap(pd);
Jack Pham69a427e2017-08-04 12:26:51 -07002487 } else if (IS_CTRL(rx_msg, MSG_PR_SWAP) &&
2488 pd->spec_rev == USBPD_REV_20) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002489 /* lock in current mode */
2490 set_power_role(pd, pd->current_pr);
2491
2492 /* TODO: should we Reject in certain circumstances? */
2493 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2494 if (ret) {
2495 usbpd_err(&pd->dev, "Error sending Accept\n");
2496 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2497 break;
2498 }
2499
2500 pd->in_pr_swap = true;
Jack Pham857ee682017-05-25 11:53:36 -07002501 val.intval = 1;
2502 power_supply_set_property(pd->usb_psy,
2503 POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002504 usbpd_set_state(pd, PE_PRS_SNK_SRC_TRANSITION_TO_OFF);
2505 break;
Jack Pham69a427e2017-08-04 12:26:51 -07002506 } else if (IS_CTRL(rx_msg, MSG_VCONN_SWAP) &&
2507 pd->spec_rev == USBPD_REV_20) {
Jack Phamf4baeb12017-02-03 19:01:48 -08002508 /*
2509 * if VCONN is connected to VBUS, make sure we are
2510 * not in high voltage contract, otherwise reject.
2511 */
2512 if (!pd->vconn_is_external &&
2513 (pd->requested_voltage > 5000000)) {
2514 ret = pd_send_msg(pd, MSG_REJECT, NULL, 0,
2515 SOP_MSG);
2516 if (ret) {
2517 usbpd_err(&pd->dev, "Error sending Reject\n");
2518 usbpd_set_state(pd,
2519 PE_SNK_SEND_SOFT_RESET);
2520 }
2521
2522 break;
2523 }
2524
2525 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2526 if (ret) {
2527 usbpd_err(&pd->dev, "Error sending Accept\n");
2528 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2529 break;
2530 }
2531
2532 vconn_swap(pd);
2533 } else if (IS_DATA(rx_msg, MSG_VDM)) {
2534 handle_vdm_rx(pd, rx_msg);
Hemant Kumar018b5982017-08-09 14:14:37 -07002535 } else if (pd->send_get_src_cap_ext && is_sink_tx_ok(pd)) {
2536 pd->send_get_src_cap_ext = false;
2537 ret = pd_send_msg(pd, MSG_GET_SOURCE_CAP_EXTENDED, NULL,
2538 0, SOP_MSG);
2539 if (ret) {
2540 dev_err(&pd->dev,
2541 "Error sending get_src_cap_ext\n");
2542 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2543 break;
2544 }
2545 kick_sm(pd, SENDER_RESPONSE_TIME);
2546 } else if (rx_msg &&
2547 IS_EXT(rx_msg, MSG_SOURCE_CAPABILITIES_EXTENDED)) {
2548 if (rx_msg->data_len != PD_SRC_CAP_EXT_DB_LEN) {
2549 usbpd_err(&pd->dev, "Invalid src cap ext db\n");
2550 break;
2551 }
2552 memcpy(&pd->src_cap_ext_db, rx_msg->payload,
2553 sizeof(pd->src_cap_ext_db));
2554 complete(&pd->is_ready);
2555 } else if (pd->send_get_pps_status && is_sink_tx_ok(pd)) {
2556 pd->send_get_pps_status = false;
2557 ret = pd_send_msg(pd, MSG_GET_PPS_STATUS, NULL,
2558 0, SOP_MSG);
2559 if (ret) {
2560 dev_err(&pd->dev,
2561 "Error sending get_pps_status\n");
2562 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2563 break;
2564 }
2565 kick_sm(pd, SENDER_RESPONSE_TIME);
2566 } else if (rx_msg &&
2567 IS_EXT(rx_msg, MSG_PPS_STATUS)) {
2568 if (rx_msg->data_len != sizeof(pd->pps_status_db)) {
2569 usbpd_err(&pd->dev, "Invalid pps status db\n");
2570 break;
2571 }
2572 memcpy(&pd->pps_status_db, rx_msg->payload,
2573 sizeof(pd->pps_status_db));
2574 complete(&pd->is_ready);
Hemant Kumara1875942017-08-09 16:50:14 -07002575 } else if (IS_DATA(rx_msg, MSG_ALERT)) {
2576 if (rx_msg->data_len != sizeof(pd->received_ado)) {
2577 usbpd_err(&pd->dev, "Invalid ado\n");
2578 break;
2579 }
2580 memcpy(&pd->received_ado, rx_msg->payload,
2581 sizeof(pd->received_ado));
2582 ret = pd_send_msg(pd, MSG_GET_STATUS, NULL,
2583 0, SOP_MSG);
2584 if (ret) {
2585 dev_err(&pd->dev,
2586 "Error sending get_status\n");
2587 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2588 break;
2589 }
2590 kick_sm(pd, SENDER_RESPONSE_TIME);
2591 } else if (rx_msg &&
2592 IS_EXT(rx_msg, MSG_STATUS)) {
2593 if (rx_msg->data_len != PD_STATUS_DB_LEN) {
2594 usbpd_err(&pd->dev, "Invalid status db\n");
2595 break;
2596 }
2597 memcpy(&pd->status_db, rx_msg->payload,
2598 sizeof(pd->status_db));
2599 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Hemant Kumar51ded972017-08-09 17:57:24 -07002600 } else if (pd->send_get_battery_cap && is_sink_tx_ok(pd)) {
2601 pd->send_get_battery_cap = false;
2602 ret = pd_send_ext_msg(pd, MSG_GET_BATTERY_CAP,
2603 &pd->get_battery_cap_db, 1, SOP_MSG);
2604 if (ret) {
2605 dev_err(&pd->dev,
2606 "Error sending get_battery_cap\n");
2607 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2608 break;
2609 }
2610 kick_sm(pd, SENDER_RESPONSE_TIME);
2611 } else if (rx_msg &&
2612 IS_EXT(rx_msg, MSG_BATTERY_CAPABILITIES)) {
2613 if (rx_msg->data_len != PD_BATTERY_CAP_DB_LEN) {
2614 usbpd_err(&pd->dev, "Invalid battery cap db\n");
2615 break;
2616 }
2617 memcpy(&pd->battery_cap_db, rx_msg->payload,
2618 sizeof(pd->battery_cap_db));
2619 complete(&pd->is_ready);
2620 } else if (pd->send_get_battery_status && is_sink_tx_ok(pd)) {
2621 pd->send_get_battery_status = false;
2622 ret = pd_send_ext_msg(pd, MSG_GET_BATTERY_STATUS,
2623 &pd->get_battery_status_db, 1, SOP_MSG);
2624 if (ret) {
2625 dev_err(&pd->dev,
2626 "Error sending get_battery_status\n");
2627 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2628 break;
2629 }
2630 kick_sm(pd, SENDER_RESPONSE_TIME);
2631 } else if (rx_msg &&
2632 IS_EXT(rx_msg, MSG_BATTERY_STATUS)) {
2633 if (rx_msg->data_len != sizeof(pd->battery_sts_dobj)) {
2634 usbpd_err(&pd->dev, "Invalid bat sts dobj\n");
2635 break;
2636 }
2637 memcpy(&pd->battery_sts_dobj, rx_msg->payload,
2638 sizeof(pd->battery_sts_dobj));
2639 complete(&pd->is_ready);
Jack Pham69a427e2017-08-04 12:26:51 -07002640 } else if (rx_msg && pd->spec_rev == USBPD_REV_30) {
2641 /* unhandled messages */
2642 ret = pd_send_msg(pd, MSG_NOT_SUPPORTED, NULL, 0,
2643 SOP_MSG);
2644 if (ret) {
2645 usbpd_err(&pd->dev, "Error sending Not supported\n");
2646 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2647 }
2648 break;
Jack Phamaf7d3842017-01-26 13:28:19 -08002649 } else if (pd->send_request) {
2650 pd->send_request = false;
2651 usbpd_set_state(pd, PE_SNK_SELECT_CAPABILITY);
Jack Phamf4baeb12017-02-03 19:01:48 -08002652 } else if (pd->send_pr_swap && is_sink_tx_ok(pd)) {
2653 pd->send_pr_swap = false;
2654 ret = pd_send_msg(pd, MSG_PR_SWAP, NULL, 0, SOP_MSG);
2655 if (ret) {
2656 dev_err(&pd->dev, "Error sending PR Swap\n");
2657 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2658 break;
2659 }
2660
2661 pd->current_state = PE_PRS_SNK_SRC_SEND_SWAP;
2662 kick_sm(pd, SENDER_RESPONSE_TIME);
2663 } else if (pd->send_dr_swap && is_sink_tx_ok(pd)) {
2664 pd->send_dr_swap = false;
2665 ret = pd_send_msg(pd, MSG_DR_SWAP, NULL, 0, SOP_MSG);
2666 if (ret) {
2667 dev_err(&pd->dev, "Error sending DR Swap\n");
2668 usbpd_set_state(pd, PE_SNK_SEND_SOFT_RESET);
2669 break;
2670 }
2671
2672 pd->current_state = PE_DRS_SEND_DR_SWAP;
2673 kick_sm(pd, SENDER_RESPONSE_TIME);
2674 } else if (is_sink_tx_ok(pd)) {
2675 handle_vdm_tx(pd);
2676 }
2677 break;
2678
2679 case PE_SNK_TRANSITION_TO_DEFAULT:
2680 usbpd_set_state(pd, PE_SNK_STARTUP);
2681 break;
2682
2683 case PE_SRC_SOFT_RESET:
2684 case PE_SNK_SOFT_RESET:
2685 pd_reset_protocol(pd);
2686
2687 ret = pd_send_msg(pd, MSG_ACCEPT, NULL, 0, SOP_MSG);
2688 if (ret) {
2689 usbpd_err(&pd->dev, "%s: Error sending Accept, do Hard Reset\n",
2690 usbpd_state_strings[pd->current_state]);
2691 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2692 PE_SRC_HARD_RESET : PE_SNK_HARD_RESET);
2693 break;
2694 }
2695
2696 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2697 PE_SRC_SEND_CAPABILITIES :
2698 PE_SNK_WAIT_FOR_CAPABILITIES);
2699 break;
2700
2701 case PE_SRC_SEND_SOFT_RESET:
2702 case PE_SNK_SEND_SOFT_RESET:
2703 if (IS_CTRL(rx_msg, MSG_ACCEPT)) {
2704 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2705 PE_SRC_SEND_CAPABILITIES :
2706 PE_SNK_WAIT_FOR_CAPABILITIES);
2707 } else {
2708 usbpd_err(&pd->dev, "%s: Did not see Accept, do Hard Reset\n",
2709 usbpd_state_strings[pd->current_state]);
2710 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2711 PE_SRC_HARD_RESET : PE_SNK_HARD_RESET);
2712 }
2713 break;
2714
2715 case PE_SNK_HARD_RESET:
2716 /* prepare charger for VBUS change */
2717 val.intval = 1;
2718 power_supply_set_property(pd->usb_psy,
2719 POWER_SUPPLY_PROP_PD_IN_HARD_RESET, &val);
2720
2721 pd->requested_voltage = 5000000;
2722
2723 if (pd->requested_current) {
2724 val.intval = pd->requested_current = 0;
2725 power_supply_set_property(pd->usb_psy,
2726 POWER_SUPPLY_PROP_PD_CURRENT_MAX, &val);
2727 }
2728
2729 val.intval = pd->requested_voltage;
2730 power_supply_set_property(pd->usb_psy,
Nicholas Troast7f55c922017-07-25 13:18:03 -07002731 POWER_SUPPLY_PROP_PD_VOLTAGE_MIN, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002732
2733 pd_send_hard_reset(pd);
2734 pd->in_explicit_contract = false;
Jack Phamf6c02da2017-01-31 15:23:56 -08002735 pd->selected_pdo = pd->requested_pdo = 0;
2736 pd->rdo = 0;
Jack Phamf4baeb12017-02-03 19:01:48 -08002737 reset_vdm_state(pd);
Jack Phamf6c02da2017-01-31 15:23:56 -08002738 kobject_uevent(&pd->dev.kobj, KOBJ_CHANGE);
Jack Phamf4baeb12017-02-03 19:01:48 -08002739 usbpd_set_state(pd, PE_SNK_TRANSITION_TO_DEFAULT);
2740 break;
2741
2742 case PE_DRS_SEND_DR_SWAP:
2743 if (IS_CTRL(rx_msg, MSG_ACCEPT))
2744 dr_swap(pd);
2745
2746 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2747 PE_SRC_READY : PE_SNK_READY);
2748 break;
2749
2750 case PE_PRS_SRC_SNK_SEND_SWAP:
2751 if (!IS_CTRL(rx_msg, MSG_ACCEPT)) {
2752 pd->current_state = PE_SRC_READY;
2753 break;
2754 }
2755
2756 pd->current_state = PE_PRS_SRC_SNK_TRANSITION_TO_OFF;
2757 kick_sm(pd, SRC_TRANSITION_TIME);
2758 break;
2759
2760 case PE_PRS_SRC_SNK_TRANSITION_TO_OFF:
2761 pd->in_pr_swap = true;
Jack Pham857ee682017-05-25 11:53:36 -07002762 val.intval = 1;
2763 power_supply_set_property(pd->usb_psy,
2764 POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002765 pd->in_explicit_contract = false;
2766
2767 if (pd->vbus_enabled) {
2768 regulator_disable(pd->vbus);
2769 pd->vbus_enabled = false;
2770 }
2771
2772 /* PE_PRS_SRC_SNK_Assert_Rd */
2773 pd->current_pr = PR_SINK;
2774 set_power_role(pd, pd->current_pr);
2775 pd_phy_update_roles(pd->current_dr, pd->current_pr);
2776
2777 /* allow time for Vbus discharge, must be < tSrcSwapStdby */
2778 msleep(500);
2779
2780 ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG);
2781 if (ret) {
2782 usbpd_err(&pd->dev, "Error sending PS_RDY\n");
2783 usbpd_set_state(pd, PE_ERROR_RECOVERY);
2784 break;
2785 }
2786
2787 pd->current_state = PE_PRS_SRC_SNK_WAIT_SOURCE_ON;
2788 kick_sm(pd, PS_SOURCE_ON);
2789 break;
2790
2791 case PE_PRS_SRC_SNK_WAIT_SOURCE_ON:
2792 if (IS_CTRL(rx_msg, MSG_PS_RDY))
2793 usbpd_set_state(pd, PE_SNK_STARTUP);
2794 else
2795 usbpd_set_state(pd, PE_ERROR_RECOVERY);
2796 break;
2797
2798 case PE_PRS_SNK_SRC_SEND_SWAP:
2799 if (!IS_CTRL(rx_msg, MSG_ACCEPT)) {
2800 pd->current_state = PE_SNK_READY;
2801 break;
2802 }
2803
2804 pd->in_pr_swap = true;
Jack Pham857ee682017-05-25 11:53:36 -07002805 val.intval = 1;
2806 power_supply_set_property(pd->usb_psy,
2807 POWER_SUPPLY_PROP_PR_SWAP, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002808 usbpd_set_state(pd, PE_PRS_SNK_SRC_TRANSITION_TO_OFF);
2809 break;
2810
2811 case PE_PRS_SNK_SRC_TRANSITION_TO_OFF:
2812 if (!IS_CTRL(rx_msg, MSG_PS_RDY)) {
2813 usbpd_set_state(pd, PE_ERROR_RECOVERY);
2814 break;
2815 }
2816
2817 /* PE_PRS_SNK_SRC_Assert_Rp */
2818 pd->current_pr = PR_SRC;
2819 set_power_role(pd, pd->current_pr);
2820 pd->current_state = PE_PRS_SNK_SRC_SOURCE_ON;
2821
2822 /* fall-through */
2823
2824 case PE_PRS_SNK_SRC_SOURCE_ON:
2825 enable_vbus(pd);
2826 msleep(200); /* allow time VBUS ramp-up, must be < tNewSrc */
2827
2828 ret = pd_send_msg(pd, MSG_PS_RDY, NULL, 0, SOP_MSG);
2829 if (ret) {
2830 usbpd_err(&pd->dev, "Error sending PS_RDY\n");
2831 usbpd_set_state(pd, PE_ERROR_RECOVERY);
2832 break;
2833 }
2834
2835 usbpd_set_state(pd, PE_SRC_STARTUP);
2836 break;
2837
2838 case PE_VCS_WAIT_FOR_VCONN:
2839 if (IS_CTRL(rx_msg, MSG_PS_RDY)) {
2840 /*
2841 * hopefully redundant check but in case not enabled
2842 * avoids unbalanced regulator disable count
2843 */
2844 if (pd->vconn_enabled)
2845 regulator_disable(pd->vconn);
2846 pd->vconn_enabled = false;
2847
2848 pd->current_state = pd->current_pr == PR_SRC ?
2849 PE_SRC_READY : PE_SNK_READY;
2850 } else {
2851 /* timed out; go to hard reset */
2852 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
2853 PE_SRC_HARD_RESET : PE_SNK_HARD_RESET);
2854 }
2855
2856 break;
2857
2858 default:
2859 usbpd_err(&pd->dev, "Unhandled state %s\n",
2860 usbpd_state_strings[pd->current_state]);
2861 break;
2862 }
2863
2864sm_done:
2865 kfree(rx_msg);
2866
Jack Pham564b4172017-05-24 18:41:53 -07002867 spin_lock_irqsave(&pd->rx_lock, flags);
2868 ret = list_empty(&pd->rx_q);
2869 spin_unlock_irqrestore(&pd->rx_lock, flags);
2870
2871 /* requeue if there are any new/pending RX messages */
2872 if (!ret)
2873 kick_sm(pd, 0);
2874
Jack Phamf4baeb12017-02-03 19:01:48 -08002875 if (!pd->sm_queued)
2876 pm_relax(&pd->dev);
2877}
2878
2879static inline const char *src_current(enum power_supply_typec_mode typec_mode)
2880{
2881 switch (typec_mode) {
2882 case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
2883 return "default";
2884 case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
2885 return "medium - 1.5A";
2886 case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
2887 return "high - 3.0A";
2888 default:
2889 return "";
2890 }
2891}
2892
2893static int psy_changed(struct notifier_block *nb, unsigned long evt, void *ptr)
2894{
2895 struct usbpd *pd = container_of(nb, struct usbpd, psy_nb);
2896 union power_supply_propval val;
2897 enum power_supply_typec_mode typec_mode;
2898 int ret;
2899
2900 if (ptr != pd->usb_psy || evt != PSY_EVENT_PROP_CHANGED)
2901 return 0;
2902
2903 ret = power_supply_get_property(pd->usb_psy,
2904 POWER_SUPPLY_PROP_TYPEC_MODE, &val);
2905 if (ret) {
2906 usbpd_err(&pd->dev, "Unable to read USB TYPEC_MODE: %d\n", ret);
2907 return ret;
2908 }
2909
2910 typec_mode = val.intval;
2911
2912 ret = power_supply_get_property(pd->usb_psy,
2913 POWER_SUPPLY_PROP_PE_START, &val);
2914 if (ret) {
2915 usbpd_err(&pd->dev, "Unable to read USB PROP_PE_START: %d\n",
2916 ret);
2917 return ret;
2918 }
2919
2920 /* Don't proceed if PE_START=0 as other props may still change */
2921 if (!val.intval && !pd->pd_connected &&
2922 typec_mode != POWER_SUPPLY_TYPEC_NONE)
2923 return 0;
2924
2925 ret = power_supply_get_property(pd->usb_psy,
2926 POWER_SUPPLY_PROP_PRESENT, &val);
2927 if (ret) {
2928 usbpd_err(&pd->dev, "Unable to read USB PRESENT: %d\n", ret);
2929 return ret;
2930 }
2931
2932 pd->vbus_present = val.intval;
2933
2934 ret = power_supply_get_property(pd->usb_psy,
Fenglin Wu80826e02017-04-25 21:45:08 +08002935 POWER_SUPPLY_PROP_REAL_TYPE, &val);
Jack Phamf4baeb12017-02-03 19:01:48 -08002936 if (ret) {
2937 usbpd_err(&pd->dev, "Unable to read USB TYPE: %d\n", ret);
2938 return ret;
2939 }
2940
2941 pd->psy_type = val.intval;
2942
2943 /*
2944 * For sink hard reset, state machine needs to know when VBUS changes
2945 * - when in PE_SNK_TRANSITION_TO_DEFAULT, notify when VBUS falls
2946 * - when in PE_SNK_DISCOVERY, notify when VBUS rises
2947 */
2948 if (typec_mode && ((!pd->vbus_present &&
2949 pd->current_state == PE_SNK_TRANSITION_TO_DEFAULT) ||
2950 (pd->vbus_present && pd->current_state == PE_SNK_DISCOVERY))) {
2951 usbpd_dbg(&pd->dev, "hard reset: typec mode:%d present:%d\n",
2952 typec_mode, pd->vbus_present);
2953 pd->typec_mode = typec_mode;
2954 kick_sm(pd, 0);
2955 return 0;
2956 }
2957
2958 if (pd->typec_mode == typec_mode)
2959 return 0;
2960
2961 pd->typec_mode = typec_mode;
2962
2963 usbpd_dbg(&pd->dev, "typec mode:%d present:%d type:%d orientation:%d\n",
2964 typec_mode, pd->vbus_present, pd->psy_type,
2965 usbpd_get_plug_orientation(pd));
2966
2967 switch (typec_mode) {
2968 /* Disconnect */
2969 case POWER_SUPPLY_TYPEC_NONE:
2970 if (pd->in_pr_swap) {
2971 usbpd_dbg(&pd->dev, "Ignoring disconnect due to PR swap\n");
2972 return 0;
2973 }
2974
2975 pd->current_pr = PR_NONE;
2976 break;
2977
2978 /* Sink states */
2979 case POWER_SUPPLY_TYPEC_SOURCE_DEFAULT:
2980 case POWER_SUPPLY_TYPEC_SOURCE_MEDIUM:
2981 case POWER_SUPPLY_TYPEC_SOURCE_HIGH:
2982 usbpd_info(&pd->dev, "Type-C Source (%s) connected\n",
2983 src_current(typec_mode));
2984
2985 /* if waiting for SinkTxOk to start an AMS */
2986 if (pd->spec_rev == USBPD_REV_30 &&
2987 typec_mode == POWER_SUPPLY_TYPEC_SOURCE_HIGH &&
2988 (pd->send_pr_swap || pd->send_dr_swap || pd->vdm_tx))
2989 break;
2990
2991 if (pd->current_pr == PR_SINK)
2992 return 0;
2993
Jack Phamd434a412017-07-24 10:05:05 -07002994 /*
2995 * Unexpected if not in PR swap; need to force disconnect from
2996 * source so we can turn off VBUS, Vconn, PD PHY etc.
2997 */
2998 if (pd->current_pr == PR_SRC) {
2999 usbpd_info(&pd->dev, "Forcing disconnect from source mode\n");
3000 pd->current_pr = PR_NONE;
3001 break;
3002 }
3003
Jack Phamf4baeb12017-02-03 19:01:48 -08003004 pd->current_pr = PR_SINK;
3005 break;
3006
3007 /* Source states */
3008 case POWER_SUPPLY_TYPEC_SINK_POWERED_CABLE:
3009 case POWER_SUPPLY_TYPEC_SINK:
3010 usbpd_info(&pd->dev, "Type-C Sink%s connected\n",
3011 typec_mode == POWER_SUPPLY_TYPEC_SINK ?
3012 "" : " (powered)");
3013
3014 if (pd->current_pr == PR_SRC)
3015 return 0;
3016
3017 pd->current_pr = PR_SRC;
3018 break;
3019
3020 case POWER_SUPPLY_TYPEC_SINK_DEBUG_ACCESSORY:
3021 usbpd_info(&pd->dev, "Type-C Debug Accessory connected\n");
3022 break;
3023 case POWER_SUPPLY_TYPEC_SINK_AUDIO_ADAPTER:
3024 usbpd_info(&pd->dev, "Type-C Analog Audio Adapter connected\n");
3025 break;
3026 default:
3027 usbpd_warn(&pd->dev, "Unsupported typec mode:%d\n",
3028 typec_mode);
3029 break;
3030 }
3031
3032 /* queue state machine due to CC state change */
3033 kick_sm(pd, 0);
3034 return 0;
3035}
3036
3037static enum dual_role_property usbpd_dr_properties[] = {
3038 DUAL_ROLE_PROP_SUPPORTED_MODES,
3039 DUAL_ROLE_PROP_MODE,
3040 DUAL_ROLE_PROP_PR,
3041 DUAL_ROLE_PROP_DR,
3042};
3043
3044static int usbpd_dr_get_property(struct dual_role_phy_instance *dual_role,
3045 enum dual_role_property prop, unsigned int *val)
3046{
3047 struct usbpd *pd = dual_role_get_drvdata(dual_role);
3048
3049 if (!pd)
3050 return -ENODEV;
3051
3052 switch (prop) {
3053 case DUAL_ROLE_PROP_MODE:
3054 /* For now associate UFP/DFP with data role only */
3055 if (pd->current_dr == DR_UFP)
3056 *val = DUAL_ROLE_PROP_MODE_UFP;
3057 else if (pd->current_dr == DR_DFP)
3058 *val = DUAL_ROLE_PROP_MODE_DFP;
3059 else
3060 *val = DUAL_ROLE_PROP_MODE_NONE;
3061 break;
3062 case DUAL_ROLE_PROP_PR:
3063 if (pd->current_pr == PR_SRC)
3064 *val = DUAL_ROLE_PROP_PR_SRC;
3065 else if (pd->current_pr == PR_SINK)
3066 *val = DUAL_ROLE_PROP_PR_SNK;
3067 else
3068 *val = DUAL_ROLE_PROP_PR_NONE;
3069 break;
3070 case DUAL_ROLE_PROP_DR:
3071 if (pd->current_dr == DR_UFP)
3072 *val = DUAL_ROLE_PROP_DR_DEVICE;
3073 else if (pd->current_dr == DR_DFP)
3074 *val = DUAL_ROLE_PROP_DR_HOST;
3075 else
3076 *val = DUAL_ROLE_PROP_DR_NONE;
3077 break;
3078 default:
3079 usbpd_warn(&pd->dev, "unsupported property %d\n", prop);
3080 return -ENODATA;
3081 }
3082
3083 return 0;
3084}
3085
3086static int usbpd_dr_set_property(struct dual_role_phy_instance *dual_role,
3087 enum dual_role_property prop, const unsigned int *val)
3088{
3089 struct usbpd *pd = dual_role_get_drvdata(dual_role);
3090 bool do_swap = false;
3091
3092 if (!pd)
3093 return -ENODEV;
3094
3095 switch (prop) {
3096 case DUAL_ROLE_PROP_MODE:
3097 usbpd_dbg(&pd->dev, "Setting mode to %d\n", *val);
3098
3099 /*
3100 * Forces disconnect on CC and re-establishes connection.
3101 * This does not use PD-based PR/DR swap
3102 */
3103 if (*val == DUAL_ROLE_PROP_MODE_UFP)
3104 pd->forced_pr = POWER_SUPPLY_TYPEC_PR_SINK;
3105 else if (*val == DUAL_ROLE_PROP_MODE_DFP)
3106 pd->forced_pr = POWER_SUPPLY_TYPEC_PR_SOURCE;
3107
3108 /* new mode will be applied in disconnect handler */
3109 set_power_role(pd, PR_NONE);
3110
3111 /* wait until it takes effect */
3112 while (pd->forced_pr != POWER_SUPPLY_TYPEC_PR_NONE)
3113 msleep(20);
3114
3115 break;
3116
3117 case DUAL_ROLE_PROP_DR:
3118 usbpd_dbg(&pd->dev, "Setting data_role to %d\n", *val);
3119
3120 if (*val == DUAL_ROLE_PROP_DR_HOST) {
3121 if (pd->current_dr == DR_UFP)
3122 do_swap = true;
3123 } else if (*val == DUAL_ROLE_PROP_DR_DEVICE) {
3124 if (pd->current_dr == DR_DFP)
3125 do_swap = true;
3126 } else {
3127 usbpd_warn(&pd->dev, "setting data_role to 'none' unsupported\n");
3128 return -ENOTSUPP;
3129 }
3130
3131 if (do_swap) {
3132 if (pd->current_state != PE_SRC_READY &&
3133 pd->current_state != PE_SNK_READY) {
3134 usbpd_err(&pd->dev, "data_role swap not allowed: PD not in Ready state\n");
3135 return -EAGAIN;
3136 }
3137
3138 if (pd->current_state == PE_SNK_READY &&
3139 !is_sink_tx_ok(pd)) {
3140 usbpd_err(&pd->dev, "Rp indicates SinkTxNG\n");
3141 return -EAGAIN;
3142 }
3143
Jack Phamaf7d3842017-01-26 13:28:19 -08003144 mutex_lock(&pd->swap_lock);
3145 reinit_completion(&pd->is_ready);
Jack Phamf4baeb12017-02-03 19:01:48 -08003146 pd->send_dr_swap = true;
3147 kick_sm(pd, 0);
3148
3149 /* wait for operation to complete */
Jack Phamaf7d3842017-01-26 13:28:19 -08003150 if (!wait_for_completion_timeout(&pd->is_ready,
Jack Phamf4baeb12017-02-03 19:01:48 -08003151 msecs_to_jiffies(100))) {
3152 usbpd_err(&pd->dev, "data_role swap timed out\n");
Jack Phamaf7d3842017-01-26 13:28:19 -08003153 mutex_unlock(&pd->swap_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08003154 return -ETIMEDOUT;
3155 }
3156
Jack Phamaf7d3842017-01-26 13:28:19 -08003157 mutex_unlock(&pd->swap_lock);
3158
Jack Phamf4baeb12017-02-03 19:01:48 -08003159 if ((*val == DUAL_ROLE_PROP_DR_HOST &&
3160 pd->current_dr != DR_DFP) ||
3161 (*val == DUAL_ROLE_PROP_DR_DEVICE &&
3162 pd->current_dr != DR_UFP)) {
3163 usbpd_err(&pd->dev, "incorrect state (%s) after data_role swap\n",
3164 pd->current_dr == DR_DFP ?
3165 "dfp" : "ufp");
3166 return -EPROTO;
3167 }
3168 }
3169
3170 break;
3171
3172 case DUAL_ROLE_PROP_PR:
3173 usbpd_dbg(&pd->dev, "Setting power_role to %d\n", *val);
3174
3175 if (*val == DUAL_ROLE_PROP_PR_SRC) {
3176 if (pd->current_pr == PR_SINK)
3177 do_swap = true;
3178 } else if (*val == DUAL_ROLE_PROP_PR_SNK) {
3179 if (pd->current_pr == PR_SRC)
3180 do_swap = true;
3181 } else {
3182 usbpd_warn(&pd->dev, "setting power_role to 'none' unsupported\n");
3183 return -ENOTSUPP;
3184 }
3185
3186 if (do_swap) {
3187 if (pd->current_state != PE_SRC_READY &&
3188 pd->current_state != PE_SNK_READY) {
3189 usbpd_err(&pd->dev, "power_role swap not allowed: PD not in Ready state\n");
3190 return -EAGAIN;
3191 }
3192
3193 if (pd->current_state == PE_SNK_READY &&
3194 !is_sink_tx_ok(pd)) {
3195 usbpd_err(&pd->dev, "Rp indicates SinkTxNG\n");
3196 return -EAGAIN;
3197 }
3198
Jack Phamaf7d3842017-01-26 13:28:19 -08003199 mutex_lock(&pd->swap_lock);
3200 reinit_completion(&pd->is_ready);
Jack Phamf4baeb12017-02-03 19:01:48 -08003201 pd->send_pr_swap = true;
3202 kick_sm(pd, 0);
3203
3204 /* wait for operation to complete */
Jack Phamaf7d3842017-01-26 13:28:19 -08003205 if (!wait_for_completion_timeout(&pd->is_ready,
Jack Phamf4baeb12017-02-03 19:01:48 -08003206 msecs_to_jiffies(2000))) {
3207 usbpd_err(&pd->dev, "power_role swap timed out\n");
Jack Phamaf7d3842017-01-26 13:28:19 -08003208 mutex_unlock(&pd->swap_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08003209 return -ETIMEDOUT;
3210 }
3211
Jack Phamaf7d3842017-01-26 13:28:19 -08003212 mutex_unlock(&pd->swap_lock);
3213
Jack Phamf4baeb12017-02-03 19:01:48 -08003214 if ((*val == DUAL_ROLE_PROP_PR_SRC &&
3215 pd->current_pr != PR_SRC) ||
3216 (*val == DUAL_ROLE_PROP_PR_SNK &&
3217 pd->current_pr != PR_SINK)) {
3218 usbpd_err(&pd->dev, "incorrect state (%s) after power_role swap\n",
3219 pd->current_pr == PR_SRC ?
3220 "source" : "sink");
3221 return -EPROTO;
3222 }
3223 }
3224 break;
3225
3226 default:
3227 usbpd_warn(&pd->dev, "unsupported property %d\n", prop);
3228 return -ENOTSUPP;
3229 }
3230
3231 return 0;
3232}
3233
3234static int usbpd_dr_prop_writeable(struct dual_role_phy_instance *dual_role,
3235 enum dual_role_property prop)
3236{
Jack Phame232bde2017-03-02 11:37:00 -08003237 struct usbpd *pd = dual_role_get_drvdata(dual_role);
3238
Jack Phamf4baeb12017-02-03 19:01:48 -08003239 switch (prop) {
3240 case DUAL_ROLE_PROP_MODE:
Jack Phame232bde2017-03-02 11:37:00 -08003241 return 1;
Jack Phamf4baeb12017-02-03 19:01:48 -08003242 case DUAL_ROLE_PROP_DR:
3243 case DUAL_ROLE_PROP_PR:
Jack Phame232bde2017-03-02 11:37:00 -08003244 if (pd)
3245 return pd->current_state == PE_SNK_READY ||
3246 pd->current_state == PE_SRC_READY;
3247 break;
Jack Phamf4baeb12017-02-03 19:01:48 -08003248 default:
3249 break;
3250 }
3251
3252 return 0;
3253}
3254
3255static int usbpd_uevent(struct device *dev, struct kobj_uevent_env *env)
3256{
3257 struct usbpd *pd = dev_get_drvdata(dev);
3258 int i;
3259
3260 add_uevent_var(env, "DATA_ROLE=%s", pd->current_dr == DR_DFP ?
3261 "dfp" : "ufp");
3262
3263 if (pd->current_pr == PR_SINK) {
3264 add_uevent_var(env, "POWER_ROLE=sink");
3265 add_uevent_var(env, "SRC_CAP_ID=%d", pd->src_cap_id);
3266
3267 for (i = 0; i < ARRAY_SIZE(pd->received_pdos); i++)
3268 add_uevent_var(env, "PDO%d=%08x", i,
3269 pd->received_pdos[i]);
3270
3271 add_uevent_var(env, "REQUESTED_PDO=%d", pd->requested_pdo);
3272 add_uevent_var(env, "SELECTED_PDO=%d", pd->selected_pdo);
3273 } else {
3274 add_uevent_var(env, "POWER_ROLE=source");
3275 for (i = 0; i < ARRAY_SIZE(default_src_caps); i++)
3276 add_uevent_var(env, "PDO%d=%08x", i,
3277 default_src_caps[i]);
3278 }
3279
3280 add_uevent_var(env, "RDO=%08x", pd->rdo);
3281 add_uevent_var(env, "CONTRACT=%s", pd->in_explicit_contract ?
3282 "explicit" : "implicit");
3283 add_uevent_var(env, "ALT_MODE=%d", pd->vdm_state == MODE_ENTERED);
3284
Hemant Kumara1875942017-08-09 16:50:14 -07003285 add_uevent_var(env, "ADO=%08x", pd->received_ado);
3286 for (i = 0; i < PD_STATUS_DB_LEN; i++)
3287 add_uevent_var(env, "SDB%d=%08x", i, pd->status_db[i]);
3288
Jack Phamf4baeb12017-02-03 19:01:48 -08003289 return 0;
3290}
3291
3292static ssize_t contract_show(struct device *dev, struct device_attribute *attr,
3293 char *buf)
3294{
3295 struct usbpd *pd = dev_get_drvdata(dev);
3296
3297 return snprintf(buf, PAGE_SIZE, "%s\n",
3298 pd->in_explicit_contract ? "explicit" : "implicit");
3299}
3300static DEVICE_ATTR_RO(contract);
3301
3302static ssize_t current_pr_show(struct device *dev,
3303 struct device_attribute *attr, char *buf)
3304{
3305 struct usbpd *pd = dev_get_drvdata(dev);
3306 const char *pr = "none";
3307
3308 if (pd->current_pr == PR_SINK)
3309 pr = "sink";
3310 else if (pd->current_pr == PR_SRC)
3311 pr = "source";
3312
3313 return snprintf(buf, PAGE_SIZE, "%s\n", pr);
3314}
3315static DEVICE_ATTR_RO(current_pr);
3316
3317static ssize_t initial_pr_show(struct device *dev,
3318 struct device_attribute *attr, char *buf)
3319{
3320 struct usbpd *pd = dev_get_drvdata(dev);
3321 const char *pr = "none";
3322
3323 if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SOURCE_DEFAULT)
3324 pr = "sink";
3325 else if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SINK)
3326 pr = "source";
3327
3328 return snprintf(buf, PAGE_SIZE, "%s\n", pr);
3329}
3330static DEVICE_ATTR_RO(initial_pr);
3331
3332static ssize_t current_dr_show(struct device *dev,
3333 struct device_attribute *attr, char *buf)
3334{
3335 struct usbpd *pd = dev_get_drvdata(dev);
3336 const char *dr = "none";
3337
3338 if (pd->current_dr == DR_UFP)
3339 dr = "ufp";
3340 else if (pd->current_dr == DR_DFP)
3341 dr = "dfp";
3342
3343 return snprintf(buf, PAGE_SIZE, "%s\n", dr);
3344}
3345static DEVICE_ATTR_RO(current_dr);
3346
3347static ssize_t initial_dr_show(struct device *dev,
3348 struct device_attribute *attr, char *buf)
3349{
3350 struct usbpd *pd = dev_get_drvdata(dev);
3351 const char *dr = "none";
3352
3353 if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SOURCE_DEFAULT)
3354 dr = "ufp";
3355 else if (pd->typec_mode >= POWER_SUPPLY_TYPEC_SINK)
3356 dr = "dfp";
3357
3358 return snprintf(buf, PAGE_SIZE, "%s\n", dr);
3359}
3360static DEVICE_ATTR_RO(initial_dr);
3361
3362static ssize_t src_cap_id_show(struct device *dev,
3363 struct device_attribute *attr, char *buf)
3364{
3365 struct usbpd *pd = dev_get_drvdata(dev);
3366
3367 return snprintf(buf, PAGE_SIZE, "%d\n", pd->src_cap_id);
3368}
3369static DEVICE_ATTR_RO(src_cap_id);
3370
3371/* Dump received source PDOs in human-readable format */
3372static ssize_t pdo_h_show(struct device *dev, struct device_attribute *attr,
3373 char *buf)
3374{
3375 struct usbpd *pd = dev_get_drvdata(dev);
3376 int i;
3377 ssize_t cnt = 0;
3378
3379 for (i = 0; i < ARRAY_SIZE(pd->received_pdos); i++) {
3380 u32 pdo = pd->received_pdos[i];
3381
3382 if (pdo == 0)
3383 break;
3384
3385 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt, "PDO %d\n", i + 1);
3386
3387 if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_FIXED) {
3388 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt,
3389 "\tFixed supply\n"
3390 "\tDual-Role Power:%d\n"
3391 "\tUSB Suspend Supported:%d\n"
3392 "\tExternally Powered:%d\n"
3393 "\tUSB Communications Capable:%d\n"
3394 "\tData Role Swap:%d\n"
3395 "\tPeak Current:%d\n"
3396 "\tVoltage:%d (mV)\n"
3397 "\tMax Current:%d (mA)\n",
3398 PD_SRC_PDO_FIXED_PR_SWAP(pdo),
3399 PD_SRC_PDO_FIXED_USB_SUSP(pdo),
3400 PD_SRC_PDO_FIXED_EXT_POWERED(pdo),
3401 PD_SRC_PDO_FIXED_USB_COMM(pdo),
3402 PD_SRC_PDO_FIXED_DR_SWAP(pdo),
3403 PD_SRC_PDO_FIXED_PEAK_CURR(pdo),
3404 PD_SRC_PDO_FIXED_VOLTAGE(pdo) * 50,
3405 PD_SRC_PDO_FIXED_MAX_CURR(pdo) * 10);
3406 } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_BATTERY) {
3407 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt,
3408 "\tBattery supply\n"
3409 "\tMax Voltage:%d (mV)\n"
3410 "\tMin Voltage:%d (mV)\n"
3411 "\tMax Power:%d (mW)\n",
3412 PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) * 50,
3413 PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) * 50,
3414 PD_SRC_PDO_VAR_BATT_MAX(pdo) * 250);
3415 } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_VARIABLE) {
3416 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt,
3417 "\tVariable supply\n"
3418 "\tMax Voltage:%d (mV)\n"
3419 "\tMin Voltage:%d (mV)\n"
3420 "\tMax Current:%d (mA)\n",
3421 PD_SRC_PDO_VAR_BATT_MAX_VOLT(pdo) * 50,
3422 PD_SRC_PDO_VAR_BATT_MIN_VOLT(pdo) * 50,
3423 PD_SRC_PDO_VAR_BATT_MAX(pdo) * 10);
3424 } else if (PD_SRC_PDO_TYPE(pdo) == PD_SRC_PDO_TYPE_AUGMENTED) {
3425 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt,
3426 "\tProgrammable Power supply\n"
3427 "\tMax Voltage:%d (mV)\n"
3428 "\tMin Voltage:%d (mV)\n"
3429 "\tMax Current:%d (mA)\n",
3430 PD_APDO_MAX_VOLT(pdo) * 100,
3431 PD_APDO_MIN_VOLT(pdo) * 100,
3432 PD_APDO_MAX_CURR(pdo) * 50);
3433 } else {
3434 cnt += scnprintf(&buf[cnt], PAGE_SIZE - cnt,
3435 "Invalid PDO\n");
3436 }
3437
3438 buf[cnt++] = '\n';
3439 }
3440
3441 return cnt;
3442}
3443static DEVICE_ATTR_RO(pdo_h);
3444
3445static ssize_t pdo_n_show(struct device *dev, struct device_attribute *attr,
3446 char *buf);
3447
3448#define PDO_ATTR(n) { \
3449 .attr = { .name = __stringify(pdo##n), .mode = 0444 }, \
3450 .show = pdo_n_show, \
3451}
3452static struct device_attribute dev_attr_pdos[] = {
3453 PDO_ATTR(1),
3454 PDO_ATTR(2),
3455 PDO_ATTR(3),
3456 PDO_ATTR(4),
3457 PDO_ATTR(5),
3458 PDO_ATTR(6),
3459 PDO_ATTR(7),
3460};
3461
3462static ssize_t pdo_n_show(struct device *dev, struct device_attribute *attr,
3463 char *buf)
3464{
3465 struct usbpd *pd = dev_get_drvdata(dev);
3466 int i;
3467
3468 for (i = 0; i < ARRAY_SIZE(dev_attr_pdos); i++)
3469 if (attr == &dev_attr_pdos[i])
3470 /* dump the PDO as a hex string */
3471 return snprintf(buf, PAGE_SIZE, "%08x\n",
3472 pd->received_pdos[i]);
3473
3474 usbpd_err(&pd->dev, "Invalid PDO index\n");
3475 return -EINVAL;
3476}
3477
3478static ssize_t select_pdo_store(struct device *dev,
3479 struct device_attribute *attr, const char *buf, size_t size)
3480{
3481 struct usbpd *pd = dev_get_drvdata(dev);
3482 int src_cap_id;
3483 int pdo, uv = 0, ua = 0;
3484 int ret;
3485
Jack Phamaf7d3842017-01-26 13:28:19 -08003486 mutex_lock(&pd->swap_lock);
3487
Jack Phamf4baeb12017-02-03 19:01:48 -08003488 /* Only allowed if we are already in explicit sink contract */
3489 if (pd->current_state != PE_SNK_READY || !is_sink_tx_ok(pd)) {
3490 usbpd_err(&pd->dev, "select_pdo: Cannot select new PDO yet\n");
Jack Phamaf7d3842017-01-26 13:28:19 -08003491 ret = -EBUSY;
3492 goto out;
Jack Phamf4baeb12017-02-03 19:01:48 -08003493 }
3494
3495 ret = sscanf(buf, "%d %d %d %d", &src_cap_id, &pdo, &uv, &ua);
3496 if (ret != 2 && ret != 4) {
3497 usbpd_err(&pd->dev, "select_pdo: Must specify <src cap id> <PDO> [<uV> <uA>]\n");
Jack Phamaf7d3842017-01-26 13:28:19 -08003498 ret = -EINVAL;
3499 goto out;
Jack Phamf4baeb12017-02-03 19:01:48 -08003500 }
3501
3502 if (src_cap_id != pd->src_cap_id) {
3503 usbpd_err(&pd->dev, "select_pdo: src_cap_id mismatch. Requested:%d, current:%d\n",
3504 src_cap_id, pd->src_cap_id);
Jack Phamaf7d3842017-01-26 13:28:19 -08003505 ret = -EINVAL;
3506 goto out;
Jack Phamf4baeb12017-02-03 19:01:48 -08003507 }
3508
3509 if (pdo < 1 || pdo > 7) {
3510 usbpd_err(&pd->dev, "select_pdo: invalid PDO:%d\n", pdo);
Jack Phamaf7d3842017-01-26 13:28:19 -08003511 ret = -EINVAL;
3512 goto out;
Jack Phamf4baeb12017-02-03 19:01:48 -08003513 }
3514
3515 ret = pd_select_pdo(pd, pdo, uv, ua);
3516 if (ret)
Jack Phamaf7d3842017-01-26 13:28:19 -08003517 goto out;
Jack Phamf4baeb12017-02-03 19:01:48 -08003518
Jack Phamaf7d3842017-01-26 13:28:19 -08003519 reinit_completion(&pd->is_ready);
3520 pd->send_request = true;
3521 kick_sm(pd, 0);
Jack Phamf4baeb12017-02-03 19:01:48 -08003522
Jack Phamaf7d3842017-01-26 13:28:19 -08003523 /* wait for operation to complete */
3524 if (!wait_for_completion_timeout(&pd->is_ready,
3525 msecs_to_jiffies(1000))) {
3526 usbpd_err(&pd->dev, "select_pdo: request timed out\n");
3527 ret = -ETIMEDOUT;
3528 goto out;
3529 }
3530
3531 /* determine if request was accepted/rejected */
3532 if (pd->selected_pdo != pd->requested_pdo ||
3533 pd->current_voltage != pd->requested_voltage) {
3534 usbpd_err(&pd->dev, "select_pdo: request rejected\n");
3535 ret = -EINVAL;
3536 }
3537
3538out:
3539 pd->send_request = false;
3540 mutex_unlock(&pd->swap_lock);
3541 return ret ? ret : size;
Jack Phamf4baeb12017-02-03 19:01:48 -08003542}
3543
3544static ssize_t select_pdo_show(struct device *dev,
3545 struct device_attribute *attr, char *buf)
3546{
3547 struct usbpd *pd = dev_get_drvdata(dev);
3548
3549 return snprintf(buf, PAGE_SIZE, "%d\n", pd->selected_pdo);
3550}
3551static DEVICE_ATTR_RW(select_pdo);
3552
3553static ssize_t rdo_show(struct device *dev, struct device_attribute *attr,
3554 char *buf)
3555{
3556 struct usbpd *pd = dev_get_drvdata(dev);
3557
3558 /* dump the RDO as a hex string */
3559 return snprintf(buf, PAGE_SIZE, "%08x\n", pd->rdo);
3560}
3561static DEVICE_ATTR_RO(rdo);
3562
3563static ssize_t rdo_h_show(struct device *dev, struct device_attribute *attr,
3564 char *buf)
3565{
3566 struct usbpd *pd = dev_get_drvdata(dev);
3567 int pos = PD_RDO_OBJ_POS(pd->rdo);
3568 int type = PD_SRC_PDO_TYPE(pd->received_pdos[pos]);
3569 int len;
3570
3571 len = scnprintf(buf, PAGE_SIZE, "Request Data Object\n"
3572 "\tObj Pos:%d\n"
3573 "\tGiveback:%d\n"
3574 "\tCapability Mismatch:%d\n"
3575 "\tUSB Communications Capable:%d\n"
3576 "\tNo USB Suspend:%d\n",
3577 PD_RDO_OBJ_POS(pd->rdo),
3578 PD_RDO_GIVEBACK(pd->rdo),
3579 PD_RDO_MISMATCH(pd->rdo),
3580 PD_RDO_USB_COMM(pd->rdo),
3581 PD_RDO_NO_USB_SUSP(pd->rdo));
3582
3583 switch (type) {
3584 case PD_SRC_PDO_TYPE_FIXED:
3585 case PD_SRC_PDO_TYPE_VARIABLE:
3586 len += scnprintf(buf + len, PAGE_SIZE - len,
3587 "(Fixed/Variable)\n"
3588 "\tOperating Current:%d (mA)\n"
3589 "\t%s Current:%d (mA)\n",
3590 PD_RDO_FIXED_CURR(pd->rdo) * 10,
3591 PD_RDO_GIVEBACK(pd->rdo) ? "Min" : "Max",
3592 PD_RDO_FIXED_CURR_MINMAX(pd->rdo) * 10);
3593 break;
3594
3595 case PD_SRC_PDO_TYPE_BATTERY:
3596 len += scnprintf(buf + len, PAGE_SIZE - len,
3597 "(Battery)\n"
3598 "\tOperating Power:%d (mW)\n"
3599 "\t%s Power:%d (mW)\n",
3600 PD_RDO_FIXED_CURR(pd->rdo) * 250,
3601 PD_RDO_GIVEBACK(pd->rdo) ? "Min" : "Max",
3602 PD_RDO_FIXED_CURR_MINMAX(pd->rdo) * 250);
3603 break;
3604
3605 case PD_SRC_PDO_TYPE_AUGMENTED:
3606 len += scnprintf(buf + len, PAGE_SIZE - len,
3607 "(Programmable)\n"
3608 "\tOutput Voltage:%d (mV)\n"
3609 "\tOperating Current:%d (mA)\n",
3610 PD_RDO_PROG_VOLTAGE(pd->rdo) * 20,
3611 PD_RDO_PROG_CURR(pd->rdo) * 50);
3612 break;
3613 }
3614
3615 return len;
3616}
3617static DEVICE_ATTR_RO(rdo_h);
3618
3619static ssize_t hard_reset_store(struct device *dev,
3620 struct device_attribute *attr, const char *buf, size_t size)
3621{
3622 struct usbpd *pd = dev_get_drvdata(dev);
3623 int val = 0;
3624
3625 if (sscanf(buf, "%d\n", &val) != 1)
3626 return -EINVAL;
3627
3628 if (val)
3629 usbpd_set_state(pd, pd->current_pr == PR_SRC ?
3630 PE_SRC_HARD_RESET : PE_SNK_HARD_RESET);
3631
3632 return size;
3633}
3634static DEVICE_ATTR_WO(hard_reset);
3635
Hemant Kumar018b5982017-08-09 14:14:37 -07003636static int trigger_tx_msg(struct usbpd *pd, bool *msg_tx_flag)
3637{
3638 int ret = 0;
3639
3640 /* Only allowed if we are already in explicit sink contract */
3641 if (pd->current_state != PE_SNK_READY || !is_sink_tx_ok(pd)) {
3642 usbpd_err(&pd->dev, "%s: Cannot send msg\n", __func__);
3643 ret = -EBUSY;
3644 goto out;
3645 }
3646
3647 reinit_completion(&pd->is_ready);
3648 *msg_tx_flag = true;
3649 kick_sm(pd, 0);
3650
3651 /* wait for operation to complete */
3652 if (!wait_for_completion_timeout(&pd->is_ready,
3653 msecs_to_jiffies(1000))) {
3654 usbpd_err(&pd->dev, "%s: request timed out\n", __func__);
3655 ret = -ETIMEDOUT;
3656 }
3657
3658out:
3659 *msg_tx_flag = false;
3660 return ret;
3661
3662}
3663
3664static ssize_t get_src_cap_ext_show(struct device *dev,
3665 struct device_attribute *attr, char *buf)
3666{
3667 int i, ret, len = 0;
3668 struct usbpd *pd = dev_get_drvdata(dev);
3669
3670 if (pd->spec_rev == USBPD_REV_20)
3671 return -EINVAL;
3672
3673 ret = trigger_tx_msg(pd, &pd->send_get_src_cap_ext);
3674 if (ret)
3675 return ret;
3676
3677 for (i = 0; i < PD_SRC_CAP_EXT_DB_LEN; i++)
3678 len += snprintf(buf + len, PAGE_SIZE - len, "%d\n",
3679 pd->src_cap_ext_db[i]);
3680 return len;
3681}
3682static DEVICE_ATTR_RO(get_src_cap_ext);
3683
3684static ssize_t get_pps_status_show(struct device *dev,
3685 struct device_attribute *attr, char *buf)
3686{
3687 int ret;
3688 struct usbpd *pd = dev_get_drvdata(dev);
3689
3690 if (pd->spec_rev == USBPD_REV_20)
3691 return -EINVAL;
3692
3693 ret = trigger_tx_msg(pd, &pd->send_get_pps_status);
3694 if (ret)
3695 return ret;
3696
3697 return snprintf(buf, PAGE_SIZE, "%d\n", pd->pps_status_db);
3698}
3699static DEVICE_ATTR_RO(get_pps_status);
3700
Hemant Kumara1875942017-08-09 16:50:14 -07003701static ssize_t rx_ado_show(struct device *dev, struct device_attribute *attr,
3702 char *buf)
3703{
3704 struct usbpd *pd = dev_get_drvdata(dev);
3705
3706 /* dump the ADO as a hex string */
3707 return snprintf(buf, PAGE_SIZE, "%08x\n", pd->received_ado);
3708}
3709static DEVICE_ATTR_RO(rx_ado);
3710
Hemant Kumar51ded972017-08-09 17:57:24 -07003711static ssize_t get_battery_cap_store(struct device *dev,
3712 struct device_attribute *attr, const char *buf, size_t size)
3713{
3714 struct usbpd *pd = dev_get_drvdata(dev);
3715 int val, ret;
3716
3717 if (pd->spec_rev == USBPD_REV_20 || sscanf(buf, "%d\n", &val) != 1) {
3718 pd->get_battery_cap_db = -EINVAL;
3719 return -EINVAL;
3720 }
3721
3722 pd->get_battery_cap_db = val;
3723
3724 ret = trigger_tx_msg(pd, &pd->send_get_battery_cap);
3725
3726 return ret ? ret : size;
3727}
3728
3729static ssize_t get_battery_cap_show(struct device *dev,
3730 struct device_attribute *attr, char *buf)
3731{
3732 int i, len = 0;
3733 struct usbpd *pd = dev_get_drvdata(dev);
3734
3735 if (pd->get_battery_cap_db == -EINVAL)
3736 return -EINVAL;
3737
3738 for (i = 0; i < PD_BATTERY_CAP_DB_LEN; i++)
3739 len += snprintf(buf + len, PAGE_SIZE - len, "%d\n",
3740 pd->battery_cap_db[i]);
3741 return len;
3742}
3743static DEVICE_ATTR_RW(get_battery_cap);
3744
3745static ssize_t get_battery_status_store(struct device *dev,
3746 struct device_attribute *attr, const char *buf, size_t size)
3747{
3748 struct usbpd *pd = dev_get_drvdata(dev);
3749 int val, ret;
3750
3751 if (pd->spec_rev == USBPD_REV_20 || sscanf(buf, "%d\n", &val) != 1) {
3752 pd->get_battery_status_db = -EINVAL;
3753 return -EINVAL;
3754 }
3755
3756 pd->get_battery_status_db = val;
3757
3758 ret = trigger_tx_msg(pd, &pd->send_get_battery_status);
3759
3760 return ret ? ret : size;
3761}
3762
3763static ssize_t get_battery_status_show(struct device *dev,
3764 struct device_attribute *attr, char *buf)
3765{
3766 struct usbpd *pd = dev_get_drvdata(dev);
3767
3768 if (pd->get_battery_status_db == -EINVAL)
3769 return -EINVAL;
3770
3771 return snprintf(buf, PAGE_SIZE, "%d\n", pd->battery_sts_dobj);
3772}
3773static DEVICE_ATTR_RW(get_battery_status);
3774
Jack Phamf4baeb12017-02-03 19:01:48 -08003775static struct attribute *usbpd_attrs[] = {
3776 &dev_attr_contract.attr,
3777 &dev_attr_initial_pr.attr,
3778 &dev_attr_current_pr.attr,
3779 &dev_attr_initial_dr.attr,
3780 &dev_attr_current_dr.attr,
3781 &dev_attr_src_cap_id.attr,
3782 &dev_attr_pdo_h.attr,
3783 &dev_attr_pdos[0].attr,
3784 &dev_attr_pdos[1].attr,
3785 &dev_attr_pdos[2].attr,
3786 &dev_attr_pdos[3].attr,
3787 &dev_attr_pdos[4].attr,
3788 &dev_attr_pdos[5].attr,
3789 &dev_attr_pdos[6].attr,
3790 &dev_attr_select_pdo.attr,
3791 &dev_attr_rdo.attr,
3792 &dev_attr_rdo_h.attr,
3793 &dev_attr_hard_reset.attr,
Hemant Kumar018b5982017-08-09 14:14:37 -07003794 &dev_attr_get_src_cap_ext.attr,
3795 &dev_attr_get_pps_status.attr,
Hemant Kumara1875942017-08-09 16:50:14 -07003796 &dev_attr_rx_ado.attr,
Hemant Kumar51ded972017-08-09 17:57:24 -07003797 &dev_attr_get_battery_cap.attr,
3798 &dev_attr_get_battery_status.attr,
Jack Phamf4baeb12017-02-03 19:01:48 -08003799 NULL,
3800};
3801ATTRIBUTE_GROUPS(usbpd);
3802
3803static struct class usbpd_class = {
3804 .name = "usbpd",
3805 .owner = THIS_MODULE,
3806 .dev_uevent = usbpd_uevent,
3807 .dev_groups = usbpd_groups,
3808};
3809
3810static int match_usbpd_device(struct device *dev, const void *data)
3811{
3812 return dev->parent == data;
3813}
3814
3815static void devm_usbpd_put(struct device *dev, void *res)
3816{
3817 struct usbpd **ppd = res;
3818
3819 put_device(&(*ppd)->dev);
3820}
3821
3822struct usbpd *devm_usbpd_get_by_phandle(struct device *dev, const char *phandle)
3823{
3824 struct usbpd **ptr, *pd = NULL;
3825 struct device_node *pd_np;
3826 struct platform_device *pdev;
3827 struct device *pd_dev;
3828
3829 if (!usbpd_class.p) /* usbpd_init() not yet called */
3830 return ERR_PTR(-EAGAIN);
3831
3832 if (!dev->of_node)
3833 return ERR_PTR(-EINVAL);
3834
3835 pd_np = of_parse_phandle(dev->of_node, phandle, 0);
3836 if (!pd_np)
3837 return ERR_PTR(-ENXIO);
3838
3839 pdev = of_find_device_by_node(pd_np);
3840 if (!pdev)
3841 return ERR_PTR(-ENODEV);
3842
3843 pd_dev = class_find_device(&usbpd_class, NULL, &pdev->dev,
3844 match_usbpd_device);
3845 if (!pd_dev) {
3846 platform_device_put(pdev);
3847 /* device was found but maybe hadn't probed yet, so defer */
3848 return ERR_PTR(-EPROBE_DEFER);
3849 }
3850
3851 ptr = devres_alloc(devm_usbpd_put, sizeof(*ptr), GFP_KERNEL);
3852 if (!ptr) {
3853 put_device(pd_dev);
3854 platform_device_put(pdev);
3855 return ERR_PTR(-ENOMEM);
3856 }
3857
3858 pd = dev_get_drvdata(pd_dev);
3859 if (!pd)
3860 return ERR_PTR(-EPROBE_DEFER);
3861
3862 *ptr = pd;
3863 devres_add(dev, ptr);
3864
3865 return pd;
3866}
3867EXPORT_SYMBOL(devm_usbpd_get_by_phandle);
3868
3869static int num_pd_instances;
3870
3871/**
3872 * usbpd_create - Create a new instance of USB PD protocol/policy engine
3873 * @parent - parent device to associate with
3874 *
3875 * This creates a new usbpd class device which manages the state of a
3876 * USB PD-capable port. The parent device that is passed in should be
3877 * associated with the physical device port, e.g. a PD PHY.
3878 *
3879 * Return: struct usbpd pointer, or an ERR_PTR value
3880 */
3881struct usbpd *usbpd_create(struct device *parent)
3882{
3883 int ret;
3884 struct usbpd *pd;
3885
3886 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
3887 if (!pd)
3888 return ERR_PTR(-ENOMEM);
3889
3890 device_initialize(&pd->dev);
3891 pd->dev.class = &usbpd_class;
3892 pd->dev.parent = parent;
3893 dev_set_drvdata(&pd->dev, pd);
3894
3895 ret = dev_set_name(&pd->dev, "usbpd%d", num_pd_instances++);
3896 if (ret)
3897 goto free_pd;
3898
3899 ret = device_init_wakeup(&pd->dev, true);
3900 if (ret)
3901 goto free_pd;
3902
3903 ret = device_add(&pd->dev);
3904 if (ret)
3905 goto free_pd;
3906
Hemant Kumar86bd10f2017-05-24 12:25:15 -07003907 pd->wq = alloc_ordered_workqueue("usbpd_wq", WQ_FREEZABLE | WQ_HIGHPRI);
Jack Phamf4baeb12017-02-03 19:01:48 -08003908 if (!pd->wq) {
3909 ret = -ENOMEM;
3910 goto del_pd;
3911 }
3912 INIT_WORK(&pd->sm_work, usbpd_sm);
3913 hrtimer_init(&pd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
3914 pd->timer.function = pd_timeout;
Jack Phamaf7d3842017-01-26 13:28:19 -08003915 mutex_init(&pd->swap_lock);
Mayank Rana83443202017-08-31 15:38:03 -07003916 mutex_init(&pd->svid_handler_lock);
Jack Phamf4baeb12017-02-03 19:01:48 -08003917
3918 pd->usb_psy = power_supply_get_by_name("usb");
3919 if (!pd->usb_psy) {
3920 usbpd_dbg(&pd->dev, "Could not get USB power_supply, deferring probe\n");
3921 ret = -EPROBE_DEFER;
3922 goto destroy_wq;
3923 }
3924
Pratham Pratap033a2d92017-11-14 20:57:05 +05303925 if (get_connector_type(pd) == POWER_SUPPLY_CONNECTOR_MICRO_USB) {
3926 usbpd_dbg(&pd->dev, "USB connector is microAB hence failing pdphy_probe\n");
3927 ret = -EINVAL;
3928 goto put_psy;
3929 }
Jack Phamf4baeb12017-02-03 19:01:48 -08003930 /*
3931 * associate extcon with the parent dev as it could have a DT
3932 * node which will be useful for extcon_get_edev_by_phandle()
3933 */
3934 pd->extcon = devm_extcon_dev_allocate(parent, usbpd_extcon_cable);
3935 if (IS_ERR(pd->extcon)) {
3936 usbpd_err(&pd->dev, "failed to allocate extcon device\n");
3937 ret = PTR_ERR(pd->extcon);
3938 goto put_psy;
3939 }
3940
3941 pd->extcon->mutually_exclusive = usbpd_extcon_exclusive;
3942 ret = devm_extcon_dev_register(parent, pd->extcon);
3943 if (ret) {
3944 usbpd_err(&pd->dev, "failed to register extcon device\n");
3945 goto put_psy;
3946 }
3947
Jack Pham4e9dff72017-04-04 18:05:53 -07003948 /* Support reporting polarity and speed via properties */
3949 extcon_set_property_capability(pd->extcon, EXTCON_USB,
3950 EXTCON_PROP_USB_TYPEC_POLARITY);
3951 extcon_set_property_capability(pd->extcon, EXTCON_USB,
3952 EXTCON_PROP_USB_SS);
3953 extcon_set_property_capability(pd->extcon, EXTCON_USB_HOST,
3954 EXTCON_PROP_USB_TYPEC_POLARITY);
3955 extcon_set_property_capability(pd->extcon, EXTCON_USB_HOST,
3956 EXTCON_PROP_USB_SS);
3957
Jack Phamf4baeb12017-02-03 19:01:48 -08003958 pd->vbus = devm_regulator_get(parent, "vbus");
3959 if (IS_ERR(pd->vbus)) {
3960 ret = PTR_ERR(pd->vbus);
3961 goto put_psy;
3962 }
3963
3964 pd->vconn = devm_regulator_get(parent, "vconn");
3965 if (IS_ERR(pd->vconn)) {
3966 ret = PTR_ERR(pd->vconn);
3967 goto put_psy;
3968 }
3969
3970 pd->vconn_is_external = device_property_present(parent,
3971 "qcom,vconn-uses-external-source");
3972
Jack Phamee1f9052017-01-26 12:27:07 -08003973 pd->num_sink_caps = device_property_read_u32_array(parent,
3974 "qcom,default-sink-caps", NULL, 0);
Vijayavardhan Vennapusa248d7232017-02-14 15:32:07 +05303975 if (pd->num_sink_caps > 0) {
Jack Phamee1f9052017-01-26 12:27:07 -08003976 int i;
3977 u32 sink_caps[14];
3978
3979 if (pd->num_sink_caps % 2 || pd->num_sink_caps > 14) {
3980 ret = -EINVAL;
3981 usbpd_err(&pd->dev, "default-sink-caps must be be specified as voltage/current, max 7 pairs\n");
3982 goto put_psy;
3983 }
3984
3985 ret = device_property_read_u32_array(parent,
3986 "qcom,default-sink-caps", sink_caps,
3987 pd->num_sink_caps);
3988 if (ret) {
3989 usbpd_err(&pd->dev, "Error reading default-sink-caps\n");
3990 goto put_psy;
3991 }
3992
3993 pd->num_sink_caps /= 2;
3994
3995 for (i = 0; i < pd->num_sink_caps; i++) {
3996 int v = sink_caps[i * 2] / 50;
3997 int c = sink_caps[i * 2 + 1] / 10;
3998
3999 pd->sink_caps[i] =
4000 PD_SNK_PDO_FIXED(0, 0, 0, 0, 0, v, c);
4001 }
4002
4003 /* First PDO includes additional capabilities */
4004 pd->sink_caps[0] |= PD_SNK_PDO_FIXED(1, 0, 0, 1, 1, 0, 0);
4005 } else {
4006 memcpy(pd->sink_caps, default_snk_caps,
4007 sizeof(default_snk_caps));
4008 pd->num_sink_caps = ARRAY_SIZE(default_snk_caps);
4009 }
4010
Jack Phamf4baeb12017-02-03 19:01:48 -08004011 /*
4012 * Register the Android dual-role class (/sys/class/dual_role_usb/).
4013 * The first instance should be named "otg_default" as that's what
4014 * Android expects.
4015 * Note this is different than the /sys/class/usbpd/ created above.
4016 */
4017 pd->dr_desc.name = (num_pd_instances == 1) ?
4018 "otg_default" : dev_name(&pd->dev);
4019 pd->dr_desc.supported_modes = DUAL_ROLE_SUPPORTED_MODES_DFP_AND_UFP;
4020 pd->dr_desc.properties = usbpd_dr_properties;
4021 pd->dr_desc.num_properties = ARRAY_SIZE(usbpd_dr_properties);
4022 pd->dr_desc.get_property = usbpd_dr_get_property;
4023 pd->dr_desc.set_property = usbpd_dr_set_property;
4024 pd->dr_desc.property_is_writeable = usbpd_dr_prop_writeable;
4025
4026 pd->dual_role = devm_dual_role_instance_register(&pd->dev,
4027 &pd->dr_desc);
4028 if (IS_ERR(pd->dual_role)) {
4029 usbpd_err(&pd->dev, "could not register dual_role instance\n");
4030 goto put_psy;
4031 } else {
4032 pd->dual_role->drv_data = pd;
4033 }
4034
4035 pd->current_pr = PR_NONE;
4036 pd->current_dr = DR_NONE;
4037 list_add_tail(&pd->instance, &_usbpd);
4038
4039 spin_lock_init(&pd->rx_lock);
4040 INIT_LIST_HEAD(&pd->rx_q);
4041 INIT_LIST_HEAD(&pd->svid_handlers);
Jack Phamaf7d3842017-01-26 13:28:19 -08004042 init_completion(&pd->is_ready);
Jack Phamf3c1bd32017-08-02 18:32:23 -07004043 init_completion(&pd->tx_chunk_request);
Jack Phamf4baeb12017-02-03 19:01:48 -08004044
4045 pd->psy_nb.notifier_call = psy_changed;
4046 ret = power_supply_reg_notifier(&pd->psy_nb);
4047 if (ret)
4048 goto del_inst;
4049
4050 /* force read initial power_supply values */
4051 psy_changed(&pd->psy_nb, PSY_EVENT_PROP_CHANGED, pd->usb_psy);
4052
4053 return pd;
4054
4055del_inst:
4056 list_del(&pd->instance);
4057put_psy:
4058 power_supply_put(pd->usb_psy);
4059destroy_wq:
4060 destroy_workqueue(pd->wq);
4061del_pd:
4062 device_del(&pd->dev);
4063free_pd:
4064 num_pd_instances--;
4065 kfree(pd);
4066 return ERR_PTR(ret);
4067}
4068EXPORT_SYMBOL(usbpd_create);
4069
4070/**
4071 * usbpd_destroy - Removes and frees a usbpd instance
4072 * @pd: the instance to destroy
4073 */
4074void usbpd_destroy(struct usbpd *pd)
4075{
4076 if (!pd)
4077 return;
4078
4079 list_del(&pd->instance);
4080 power_supply_unreg_notifier(&pd->psy_nb);
4081 power_supply_put(pd->usb_psy);
4082 destroy_workqueue(pd->wq);
4083 device_del(&pd->dev);
4084 kfree(pd);
4085}
4086EXPORT_SYMBOL(usbpd_destroy);
4087
4088static int __init usbpd_init(void)
4089{
4090 usbpd_ipc_log = ipc_log_context_create(NUM_LOG_PAGES, "usb_pd", 0);
4091 return class_register(&usbpd_class);
4092}
4093module_init(usbpd_init);
4094
4095static void __exit usbpd_exit(void)
4096{
4097 class_unregister(&usbpd_class);
4098}
4099module_exit(usbpd_exit);
4100
4101MODULE_DESCRIPTION("USB Power Delivery Policy Engine");
4102MODULE_LICENSE("GPL v2");