Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 1 | /* 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 Pelosi | 3289228 | 2020-09-11 02:29:20 -0700 | [diff] [blame] | 20 | #include <linux/device.h> |
AleX Pelosi | d71f585 | 2020-08-26 18:40:28 -0700 | [diff] [blame] | 21 | #include <misc/logbuffer.h> |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 22 | |
| 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 Pelosi | eeb023f | 2021-01-20 21:27:39 -0800 | [diff] [blame] | 30 | /* |
| 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 | */ |
| 36 | enum pps_psy_online_states { |
| 37 | PPS_PSY_OFFLINE = 0, |
| 38 | PPS_PSY_FIXED_ONLINE, |
| 39 | PPS_PSY_PROG_ONLINE, |
| 40 | }; |
| 41 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 42 | enum pd_pps_stage { |
AleX Pelosi | 330b996 | 2021-01-18 12:23:50 -0800 | [diff] [blame] | 43 | PPS_NOTSUPP = -1, /* tried and failed or disconnected */ |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 44 | PPS_DISABLED = 0, /* default state, never tried */ |
| 45 | PPS_NONE, /* try to enable */ |
| 46 | PPS_AVAILABLE, |
| 47 | PPS_ACTIVE, |
| 48 | }; |
| 49 | |
| 50 | enum 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 | |
| 59 | struct pd_pps_data { |
AleX Pelosi | 330b996 | 2021-01-18 12:23:50 -0800 | [diff] [blame] | 60 | struct power_supply *pps_psy; |
| 61 | void *port_data; |
| 62 | |
Ken Tsou | 5ecf2f4 | 2020-07-16 08:26:05 +0800 | [diff] [blame] | 63 | struct wakeup_source *pps_ws; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 64 | 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 Pelosi | ab0e9d4 | 2020-09-29 11:13:19 -0700 | [diff] [blame] | 75 | ktime_t last_update; |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 76 | |
| 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 Pelosi | 76a3e5b | 2022-01-06 13:57:18 -0800 | [diff] [blame] | 90 | /* TODO: device dependent */ |
| 91 | #define PD_SNK_MAX_MV 9000 |
| 92 | /* TODO: device dependent */ |
| 93 | #define PD_SNK_MIN_MV 5000 |
| 94 | |
| 95 | struct tcpm_port; |
| 96 | struct tcpm_port *chg_get_tcpm_port(struct power_supply *tcpm_psy); |
| 97 | |
| 98 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 99 | /* */ |
| 100 | #define pps_is_disabled(x) (((x) == PPS_NOTSUPP) || ((x) == PPS_DISABLED)) |
| 101 | |
AleX Pelosi | ac30499 | 2021-06-15 19:45:59 -0700 | [diff] [blame] | 102 | #define pps_name(pps_psy) \ |
| 103 | ((pps_psy) && (pps_psy)->desc && (pps_psy)->desc->name ? \ |
| 104 | (pps_psy)->desc->name : "<>") |
| 105 | |
| 106 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 107 | struct dentry; |
AleX Pelosi | f377662 | 2021-01-18 13:32:08 -0800 | [diff] [blame] | 108 | int pps_init(struct pd_pps_data *pps_data, struct device *dev, |
Wasb Liu | 6172071 | 2022-11-16 19:19:53 +0800 | [diff] [blame] | 109 | struct power_supply *pps_psy, const char *pps_ws_name); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 110 | int pps_init_fs(struct pd_pps_data *pps_data, struct dentry *de); |
| 111 | /* reset state and leave in DISABLED */ |
| 112 | void pps_init_state(struct pd_pps_data *pps_data); |
AleX Pelosi | 59aef95 | 2021-04-29 14:27:49 -0700 | [diff] [blame] | 113 | /* free resources */ |
| 114 | void pps_free(struct pd_pps_data *pps_data); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 115 | |
| 116 | /* Run the PPS state machine */ |
| 117 | int pps_work(struct pd_pps_data *pps, struct power_supply *tcpm_psy); |
| 118 | |
| 119 | /* rougly equivalent */ |
| 120 | int pps_ping(struct pd_pps_data *pps, struct power_supply *tcpm_psy); |
| 121 | int pps_keep_alive(struct pd_pps_data *pps, struct power_supply *tcpm_psy); |
| 122 | |
| 123 | /* update the PPS adapter */ |
| 124 | int pps_update_adapter(struct pd_pps_data *pps_data, |
| 125 | int pending_uv, int pending_ua, |
| 126 | struct power_supply *tcpm_psy); |
| 127 | int pps_check_adapter(struct pd_pps_data *pps, |
| 128 | int pending_uv, int pending_ua, |
| 129 | struct power_supply *tcpm_psy); |
| 130 | |
| 131 | /* */ |
| 132 | int pps_prog_offline(struct pd_pps_data *pps, struct power_supply *tcpm_psy); |
| 133 | |
| 134 | void pps_adjust_volt(struct pd_pps_data *pps, int mod); |
| 135 | |
AleX Pelosi | eeb023f | 2021-01-20 21:27:39 -0800 | [diff] [blame] | 136 | int chg_switch_profile(struct pd_pps_data *pps, struct power_supply *tcpm_psy, |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 137 | bool more_pwr); |
| 138 | |
| 139 | int 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 Pelosi | 43ed859 | 2021-06-17 17:54:13 -0700 | [diff] [blame] | 143 | bool pps_check_prog_online(struct pd_pps_data *pps_data); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 144 | bool pps_prog_check_online(struct pd_pps_data *pps_data, |
| 145 | struct power_supply *tcpm_psy); |
| 146 | |
| 147 | int pps_get_src_cap(struct pd_pps_data *pps, struct power_supply *tcpm_psy); |
| 148 | |
AleX Pelosi | d301b6d | 2021-04-27 20:18:32 -0700 | [diff] [blame] | 149 | void pps_set_logbuffer(struct pd_pps_data *pps_data, struct logbuffer *log); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 150 | void pps_log(struct pd_pps_data *pps, const char *fmt, ...); |
| 151 | |
| 152 | /* probe */ |
| 153 | struct power_supply *pps_get_tcpm_psy(struct device_node *node, size_t size); |
| 154 | |
| 155 | int pps_request_pdo(struct pd_pps_data *pps_data, unsigned int ta_idx, |
AleX Pelosi | eeb023f | 2021-01-20 21:27:39 -0800 | [diff] [blame] | 156 | unsigned int ta_max_vol, unsigned int ta_max_cur); |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 157 | |
AleX Pelosi | 76a3e5b | 2022-01-06 13:57:18 -0800 | [diff] [blame] | 158 | |
Ken Tsou | 8acade1 | 2020-07-09 03:17:35 +0800 | [diff] [blame] | 159 | |
Ken Tsou | 5ecf2f4 | 2020-07-16 08:26:05 +0800 | [diff] [blame] | 160 | #endif /* __GOOGLE_DC_PPS_H_ */ |