blob: d74798849e7658889019e621a3e4774f585a2b12 [file] [log] [blame]
Ken Tsou8acade12020-07-09 03:17:35 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright 2020 Google, LLC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#ifndef __GOOGLE_DC_PPS_H_
17#define __GOOGLE_DC_PPS_H_
18
19#include <linux/usb/pd.h>
AleX Pelosi32892282020-09-11 02:29:20 -070020#include <linux/device.h>
AleX Pelosid71f5852020-08-26 18:40:28 -070021#include <misc/logbuffer.h>
Ken Tsou8acade12020-07-09 03:17:35 +080022
23#define PD_T_PPS_TIMEOUT 9000 /* Maximum of 10 seconds */
24#define PD_T_PPS_DEADLINE_S 7
25
26#define PPS_KEEP_ALIVE_MAX 3
27#define PPS_ERROR_RETRY_MS 1000
28#define CHG_PPS_VOTER "pps_chg"
29
AleX Pelosieeb023f2021-01-20 21:27:39 -080030/*
31 * There is a similar one in tcpm.c
32 * NOTE: we don't really need to replicate the values in tcpm.c in the
33 * internal state, we just need to know to set 2 to the TCPM power supply
34 * to enable PPS.
35 */
36enum pps_psy_online_states {
37 PPS_PSY_OFFLINE = 0,
38 PPS_PSY_FIXED_ONLINE,
39 PPS_PSY_PROG_ONLINE,
40};
41
Ken Tsou8acade12020-07-09 03:17:35 +080042enum pd_pps_stage {
AleX Pelosi330b9962021-01-18 12:23:50 -080043 PPS_NOTSUPP = -1, /* tried and failed or disconnected */
Ken Tsou8acade12020-07-09 03:17:35 +080044 PPS_DISABLED = 0, /* default state, never tried */
45 PPS_NONE, /* try to enable */
46 PPS_AVAILABLE,
47 PPS_ACTIVE,
48};
49
50enum pd_nr_pdo {
51 PDO_FIXED_5V = 1,
52 PDO_FIXED_HIGH_VOLTAGE,
53 PDO_PPS,
54
55 PDO_MAX_SUPP = PDO_PPS,
56 PDO_MAX = PDO_MAX_OBJECTS, /* 7 */
57};
58
59struct pd_pps_data {
AleX Pelosi330b9962021-01-18 12:23:50 -080060 struct power_supply *pps_psy;
61 void *port_data;
62
Ken Tsou5ecf2f42020-07-16 08:26:05 +080063 struct wakeup_source *pps_ws;
Ken Tsou8acade12020-07-09 03:17:35 +080064 bool stay_awake;
65
66 int nr_src_cap;
67 u32 *src_caps;
68 u32 snk_pdo[PDO_MAX_OBJECTS];
69 unsigned int nr_snk_pdo;
70 u32 default_pps_pdo;
71
72 int pd_online;
73 enum pd_pps_stage stage;
74 unsigned int keep_alive_cnt;
AleX Pelosiab0e9d42020-09-29 11:13:19 -070075 ktime_t last_update;
Ken Tsou8acade12020-07-09 03:17:35 +080076
77 /* from TA */
78 int min_uv;
79 int max_uv;
80 int max_ua;
81
82 /* to TA */
83 int out_uv;
84 int op_ua;
85
86 /* logging client */
87 struct logbuffer *log;
88};
89
AleX Pelosi76a3e5b2022-01-06 13:57:18 -080090/* TODO: device dependent */
91#define PD_SNK_MAX_MV 9000
92/* TODO: device dependent */
93#define PD_SNK_MIN_MV 5000
94
95struct tcpm_port;
96struct tcpm_port *chg_get_tcpm_port(struct power_supply *tcpm_psy);
97
98
Ken Tsou8acade12020-07-09 03:17:35 +080099/* */
100#define pps_is_disabled(x) (((x) == PPS_NOTSUPP) || ((x) == PPS_DISABLED))
101
AleX Pelosiac304992021-06-15 19:45:59 -0700102#define pps_name(pps_psy) \
103 ((pps_psy) && (pps_psy)->desc && (pps_psy)->desc->name ? \
104 (pps_psy)->desc->name : "<>")
105
106
Ken Tsou8acade12020-07-09 03:17:35 +0800107struct dentry;
AleX Pelosif3776622021-01-18 13:32:08 -0800108int pps_init(struct pd_pps_data *pps_data, struct device *dev,
Wasb Liu61720712022-11-16 19:19:53 +0800109 struct power_supply *pps_psy, const char *pps_ws_name);
Ken Tsou8acade12020-07-09 03:17:35 +0800110int pps_init_fs(struct pd_pps_data *pps_data, struct dentry *de);
111/* reset state and leave in DISABLED */
112void pps_init_state(struct pd_pps_data *pps_data);
AleX Pelosi59aef952021-04-29 14:27:49 -0700113/* free resources */
114void pps_free(struct pd_pps_data *pps_data);
Ken Tsou8acade12020-07-09 03:17:35 +0800115
116/* Run the PPS state machine */
117int pps_work(struct pd_pps_data *pps, struct power_supply *tcpm_psy);
118
119/* rougly equivalent */
120int pps_ping(struct pd_pps_data *pps, struct power_supply *tcpm_psy);
121int pps_keep_alive(struct pd_pps_data *pps, struct power_supply *tcpm_psy);
122
123/* update the PPS adapter */
124int pps_update_adapter(struct pd_pps_data *pps_data,
125 int pending_uv, int pending_ua,
126 struct power_supply *tcpm_psy);
127int pps_check_adapter(struct pd_pps_data *pps,
128 int pending_uv, int pending_ua,
129 struct power_supply *tcpm_psy);
130
131/* */
132int pps_prog_offline(struct pd_pps_data *pps, struct power_supply *tcpm_psy);
133
134void pps_adjust_volt(struct pd_pps_data *pps, int mod);
135
AleX Pelosieeb023f2021-01-20 21:27:39 -0800136int chg_switch_profile(struct pd_pps_data *pps, struct power_supply *tcpm_psy,
Ken Tsou8acade12020-07-09 03:17:35 +0800137 bool more_pwr);
138
139int pps_get_apdo_max_power(struct pd_pps_data *pps, unsigned int *ta_idx,
140 unsigned int *ta_max_vol, unsigned int *ta_max_cur,
141 unsigned long *ta_max_pwr);
142
AleX Pelosi43ed8592021-06-17 17:54:13 -0700143bool pps_check_prog_online(struct pd_pps_data *pps_data);
Ken Tsou8acade12020-07-09 03:17:35 +0800144bool pps_prog_check_online(struct pd_pps_data *pps_data,
145 struct power_supply *tcpm_psy);
146
147int pps_get_src_cap(struct pd_pps_data *pps, struct power_supply *tcpm_psy);
148
AleX Pelosid301b6d2021-04-27 20:18:32 -0700149void pps_set_logbuffer(struct pd_pps_data *pps_data, struct logbuffer *log);
Ken Tsou8acade12020-07-09 03:17:35 +0800150void pps_log(struct pd_pps_data *pps, const char *fmt, ...);
151
152/* probe */
153struct power_supply *pps_get_tcpm_psy(struct device_node *node, size_t size);
154
155int pps_request_pdo(struct pd_pps_data *pps_data, unsigned int ta_idx,
AleX Pelosieeb023f2021-01-20 21:27:39 -0800156 unsigned int ta_max_vol, unsigned int ta_max_cur);
Ken Tsou8acade12020-07-09 03:17:35 +0800157
AleX Pelosi76a3e5b2022-01-06 13:57:18 -0800158
Ken Tsou8acade12020-07-09 03:17:35 +0800159
Ken Tsou5ecf2f42020-07-16 08:26:05 +0800160#endif /* __GOOGLE_DC_PPS_H_ */