blob: 37b621c69193a257831b642f43ef214dfaa2701b [file] [log] [blame]
Prasanna Prapanchambb84a542023-03-07 18:47:39 +00001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Platform data for the LN8411 battery charger driver.
4 */
5
6#ifndef _LN8411_CHARGER_H_
7#define _LN8411_CHARGER_H_
8
9#include <linux/mutex.h>
10#include <linux/module.h>
11#include <linux/debugfs.h>
12#include <linux/thermal.h>
13#include <linux/pm_runtime.h>
14#include <linux/kernel.h>
Prasanna Prapancham65997272023-03-31 17:46:30 +000015#include <linux/gpio/driver.h>
Prasanna Prapanchambb84a542023-03-07 18:47:39 +000016
17/* Google integration */
18#include "gbms_power_supply.h"
19#include "google_bms.h"
20#include "google_dc_pps.h"
21
22enum ln8411_modes {
23 LN8411_FWD4TO1 = 0,
24 LN8411_FWD2TO1,
25 LN8411_FWD1TO1,
26 LN8411_REV1TO4 = 4,
27 LN8411_REV1TO2,
28 LN8411_REV1TO1,
29};
30
31struct ln8411_platform_data {
32 s32 irq_gpio; /* GPIO pin that's connected to INT# */
33 u32 iin_cfg; /* Input Current Limit - uA unit */
34 u32 iin_cfg_max; /* from config/dt */
35 u32 v_float; /* V_Float Voltage - uV unit */
36 u32 v_float_dt; /* from config/dt */
37 u32 iin_topoff; /* Input Topoff current -uV unit */
38 s32 iin_max_offset;
39 s32 iin_cc_comp_offset;
Prasanna Prapanchambb84a542023-03-07 18:47:39 +000040 u32 ta_max_vol_2_1;
41 u32 ta_max_vol_4_1;
Prasanna Prapanchambb84a542023-03-07 18:47:39 +000042
43 /* irdrop */
44 s32 irdrop_limits[3];
45 s32 irdrop_limit_cnt;
46 u8 ln8411_irdrop;
47
48#if IS_ENABLED(CONFIG_THERMAL)
49 const char *usb_tz_name;
50#endif
51};
52
53/* - PPS Integration Shared definitions ---------------------------------- */
54
55/* AC[0] */
56#define LN8411_CHGS_VER 1
57#define LN8411_CHGS_VER_MASK 0xff
58/* AC[1] APDO */
59/* RS[0] */
60#define LN8411_CHGS_FLAG_SHIFT 0
61#define LN8411_CHGS_FLAG_MASK 0xff
62#define LN8411_CHGS_F_STBY BIT(0)
63#define LN8411_CHGS_F_SHDN BIT(1)
64#define LN8411_CHGS_F_DONE BIT(2)
65#define LN8411_CHGS_PRE_SHIFT 8
66#define LN8411_CHGS_PRE_MASK (0xff << LN8411_CHGS_PRE_SHIFT)
67#define LN8411_CHGS_RCPC_SHIFT 16
68#define LN8411_CHGS_RCPC_MASK (0xff << LN8411_CHGS_RCPC_SHIFT)
69#define LN8411_CHGS_NC_SHIFT 24
70#define LN8411_CHGS_NC_MASK (0xff << LN8411_CHGS_NC_SHIFT)
71/* RS[1] */
72#define LN8411_CHGS_OVCC_SHIFT 0
73#define LN8411_CHGS_OVCC_MASK (0xffff << LN8411_CHGS_OVCC_SHIFT)
74#define LN8411_CHGS_ADJ_SHIFT 16
75#define LN8411_CHGS_ADJ_MASK (0xffff << LN8411_CHGS_ADJ_MASK)
76/* RS[2] */
77#define LN8411_CHGS_CC_SHIFT 0
78#define LN8411_CHGS_CC_MASK (0xffff << LN8411_CHGS_CC_SHIFT)
79#define LN8411_CHGS_CV_SHIFT 16
80#define LN8411_CHGS_CV_MASK (0xffff << LN8411_CHGS_CV_SHIFT)
81/* RS[3] */
82#define LN8411_CHGS_CA_SHIFT 0
83#define LN8411_CHGS_CA_MASK (0xff << LN8411_CHGS_CA_SHIFT)
84
85
86struct ln8411_chg_stats {
87 u32 adapter_capabilities[2];
88 u32 receiver_state[5];
89
90 u8 valid;
91 u32 ovc_count;
92 u32 ovc_max_ibatt;
93 u32 ovc_max_delta;
94
95 u32 rcp_count;
96 u32 nc_count;
97 u32 pre_count;
98 u32 ca_count;
99 u32 cc_count;
100 u32 cv_count;
101 u32 adj_count;
102 u32 stby_count;
103};
104
105struct ln8411_chip_info {
106 u8 device_id;
107 u8 chip_rev;
108};
109
110#define ln8411_chg_stats_valid(chg_data) ((chg_data)->valid)
111
112static inline void ln8411_chg_stats_update_flags(struct ln8411_chg_stats *chg_data, u8 flags)
113{
114 chg_data->receiver_state[0] |= flags << LN8411_CHGS_FLAG_SHIFT;
115}
116
117static inline void ln8411_chg_stats_set_flags(struct ln8411_chg_stats *chg_data, u8 flags)
118{
119 chg_data->receiver_state[0] &= ~LN8411_CHGS_FLAG_MASK;
120 ln8411_chg_stats_update_flags(chg_data, flags);
121}
122
123static inline void ln8411_chg_stats_inc_ovcf(struct ln8411_chg_stats *chg_data,
124 s32 ibatt, s32 cc_max)
125{
126 const s32 delta = ibatt - cc_max;
127
128 chg_data->ovc_count++;
129 if (delta > chg_data->ovc_max_delta) {
130 chg_data->ovc_max_ibatt = ibatt;
131 chg_data->ovc_max_delta = delta;
132 }
133}
134
135/**
136 * struct ln8411_charger - ln8411 charger instance
137 * @monitor_wake_lock: lock to enter the suspend mode
138 * @lock: protects concurrent access to online variables
139 * @dev: pointer to device
140 * @regmap: pointer to driver regmap
141 * @mains: power_supply instance for AC/DC power
142 * @dc_wq: work queue for the algorithm and monitor timer
143 * @timer_work: timer work for charging
144 * @timer_id: timer id for timer_work
145 * @timer_period: timer period for timer_work
146 * @last_update_time: last update time after sleep
147 * @pps_index: psy index
148 * @tcpm_psy_name: name of TCPM power supply
149 * @tcpm_phandle: lookup for tcpm power supply
150 * @pps_work: pps work for PPS periodic time
151 * @pps_data: internal data for dc_pps
152 * @log: logbuffer
153 * @pd: phandle for qualcomm PMI usbpd-phy
154 * @wlc_psy_name: power supply for wlc DC
155 * @wlc_psy: wlc DC ps
156 * @mains_online: is AC/DC input connected
157 * @charging_state: direct charging state
158 * @ret_state: return direct charging state after DC_STATE_ADJUST_TAVOL is done
159 * @iin_cc: input current for the direct charging in cc mode, uA
160 * @ta_cur: AC/DC(TA) current, uA
161 * @ta_vol: AC/DC(TA) voltage, uV
162 * @ta_objpos: AC/DC(TA) PDO object position
163 * @ta_max_cur: TA maximum current of APDO, uA
164 * @ta_max_vol: TA maximum voltage for the direct charging, uV
165 * @ta_max_pwr: TA maximum power, uW
166 * @prev_iin: Previous IIN ADC of LN8411, uA
167 * @prev_inc: Previous TA voltage or current increment factor
168 * @fv_uv: requested float voltage
169 * @cc_max: requested charge current max
170 * @new_iin: New request input current limit, uA
171 * @new_vfloat: Request for new vfloat
172 * @adc_comp_gain: adc gain for compensation
173 * @retry_cnt: retry counter for re-starting charging if charging stop happens
174 * @ta_type: TA type for the direct charging, USBPD TA or Wireless Charger.
175 * @chg_mode: supported DC charging mode 2:1 or 4:1 mode
176 * @pdata: pointer to platform data
177 * @usb_tzd: device for thermal zone
178 * @debug_root: debug entry
179 * @debug_address: debug register address
180 * @debug_adc_channel: ADC channel to read
181 * @init_done: true when initialization is complete
182 * @dc_start_time: start time (sec since boot) of the DC session
183 */
184struct ln8411_charger {
185 struct wakeup_source *monitor_wake_lock;
186 struct mutex lock;
187 struct device *dev;
188 struct regmap *regmap;
189 struct power_supply *mains;
190
191 struct workqueue_struct *dc_wq;
192 struct delayed_work timer_work;
193 u32 timer_id;
194 unsigned long timer_period;
195 unsigned long last_update_time;
196
197 bool mains_online;
198 u32 charging_state;
199 u32 ret_state;
200
201 u32 iin_cc;
202
203 u32 ta_cur;
204 u32 ta_vol;
205 u32 ta_objpos;
206
207 /* same as pps_data */
208 u32 ta_max_cur;
209 u32 ta_max_vol;
210 unsigned long ta_max_pwr;
211
212 u32 prev_iin;
213 u32 prev_inc;
214
215 u32 new_iin;
216 s32 new_vfloat;
217
218 s32 adc_comp_gain;
219
220 s32 retry_cnt;
221
222 struct ln8411_platform_data *pdata;
223
224 /* Google Integration Start */
225 s32 pps_index; /* 0=disabled, 1=tcpm, 2=wireless */
226 bool init_done;
227 bool hw_init_done;
228
229 /* PPS_wireless */
230 const char *wlc_psy_name;
231 struct power_supply *wlc_psy;
232 /* PPS_wired with TCPM */
233 u32 tcpm_phandle;
234 const char *tcpm_psy_name;
235 struct power_supply *pd;
236 struct delayed_work pps_work;
237 struct pd_pps_data pps_data;
238 struct logbuffer *log;
239
240#if IS_ENABLED(CONFIG_THERMAL)
241 struct thermal_zone_device *usb_tzd;
242#endif
243
244 /* WIRELESS or WIRED */
245 s32 ta_type;
246 /*
247 * 0 - No direct charging
248 * 1 - 2:1 charging mode
249 * 2 - 4:1 charging mode
250 */
251 s32 chg_mode;
252
253 /* requested charging current and voltage */
254 s32 fv_uv;
255 s32 cc_max;
256 ktime_t dc_start_time;
257
258 /* monitoring */
259 struct power_supply *batt_psy;
260
261 /* debug */
262 struct dentry *debug_root;
263 u32 debug_address;
264 s32 debug_adc_channel;
265
266
267 bool wlc_ramp_out_iin;
268 u32 wlc_ramp_out_delay;
269 u32 wlc_ramp_out_vout_target;
270
271 struct ln8411_chg_stats chg_data;
272 struct gvotable_election *dc_avail;
273
274 u32 debug_count;
275 struct i2c_client *client;
276 struct ln8411_chip_info chip_info;
277 struct attribute_group attrs; /* SysFS attributes */
278 struct delayed_work init_hw_work;
Prasanna Prapancham65997272023-03-31 17:46:30 +0000279
280#if IS_ENABLED(CONFIG_GPIOLIB)
281 struct gpio_chip gpio;
282#endif
Prasanna Prapanchambb84a542023-03-07 18:47:39 +0000283 /* Google Integration END */
284
285 /* Temporary, only for A1 silicon */
286 u32 iin_reg;
287 u32 vfloat_reg;
288};
289
290/* Direct Charging State */
291enum {
292 DC_STATE_NO_CHARGING, /* No charging */
293 DC_STATE_CHECK_VBAT, /* Check min battery level */
294 DC_STATE_PRESET_DC, /* Preset TA voltage/current for DC */
295 DC_STATE_CHECK_ACTIVE, /* Check active status before Adjust CC mode */
296 DC_STATE_ADJUST_CC, /* Adjust CC mode */
297 DC_STATE_CC_MODE, /* Check CC mode status */
298 DC_STATE_START_CV, /* Start CV mode */
299 DC_STATE_CV_MODE, /* Check CV mode status */
300 DC_STATE_CHARGING_DONE, /* Charging Done */
301 DC_STATE_ADJUST_TAVOL, /* Adjust TA voltage, new TA current < 1000mA */
302 DC_STATE_ADJUST_TACUR, /* Adjust TA current, new TA current < 1000mA */
303 DC_STATE_MAX,
304};
305
306/* PD Message Type */
307enum {
308 PD_MSG_REQUEST_APDO,
309 MSG_REQUEST_FIXED_PDO,
310 WCRX_REQUEST_VOLTAGE,
311};
312
313/* TA Type for the direct charging */
314enum {
315 TA_TYPE_UNKNOWN,
316 TA_TYPE_USBPD,
317 TA_TYPE_WIRELESS,
318};
319
320/* Direct Charging Mode for the direct charging */
321enum {
322 CHG_NO_DC_MODE,
323 CHG_2TO1_DC_MODE,
324 CHG_4TO1_DC_MODE,
325 CHG_1TO2_DC_MODE,
326};
327
328/* PPS timers */
329#define LN8411_PDMSG_WAIT_T 250 /* 250ms */
330#define LN8411_PDMSG_RETRY_T 1000 /* 1000ms */
Prasanna Prapancham20f8efa2023-04-05 19:31:36 +0000331#define LN8411_PDMSG_WLC_WAIT_T 2000 /* 2000ms */
Prasanna Prapanchambb84a542023-03-07 18:47:39 +0000332#define LN8411_PPS_PERIODIC_T 10000 /* 10000ms */
333#define LN8411_TA_CONFIG_WAIT_T (4 * LN8411_PDMSG_WAIT_T)
334
335/* - Core driver ---------------------------- */
336
337s32 ln8411_input_current_limit(struct ln8411_charger *ln8411);
338
339/* - PPS Integration (move to a separate file) ---------------------------- */
340
341/* */
342enum {
343 PPS_INDEX_DISABLED = 0,
344 PPS_INDEX_TCPM = 1,
345 PPS_INDEX_WLC,
346 PPS_INDEX_MAX,
347};
348
349s32 ln8411_probe_pps(struct ln8411_charger *ln8411_chg);
350
351s32 ln8411_request_pdo(struct ln8411_charger *ln8411);
352s32 ln8411_usbpd_setup(struct ln8411_charger *ln8411);
353s32 ln8411_send_pd_message(struct ln8411_charger *ln8411, u32 msg_type);
354s32 ln8411_get_apdo_max_power(struct ln8411_charger *ln8411,
355 u32 ta_max_vol, u32 ta_max_cur);
356s32 ln8411_send_rx_voltage(struct ln8411_charger *ln8411, u32 msg_type);
357s32 ln8411_get_rx_max_power(struct ln8411_charger *ln8411);
358s32 ln8411_set_ta_type(struct ln8411_charger *ln8411, s32 pps_index);
359
360/* GBMS integration */
361struct power_supply *ln8411_get_rx_psy(struct ln8411_charger *ln8411);
362s32 ln8411_get_chg_chgr_state(struct ln8411_charger *ln8411,
363 union gbms_charger_state *chg_state);
364s32 ln8411_is_present(struct ln8411_charger *ln8411);
365s32 ln8411_get_status(struct ln8411_charger *ln8411);
366s32 ln8411_get_charge_type(struct ln8411_charger *ln8411);
367int ln8411_read_adc(struct ln8411_charger *ln8411,
368 const enum ln8411_adc_chan chan);
369int get_chip_info(struct ln8411_charger *chg);
370int ln8411_check_active(struct ln8411_charger *ln8411);
371
372extern s32 debug_printk_prlog;
373extern s32 debug_no_logbuffer;
374
375#define logbuffer_prlog(p, level, fmt, ...) \
376 gbms_logbuffer_prlog(p->log, level, debug_no_logbuffer, debug_printk_prlog, fmt, ##__VA_ARGS__)
377
378/* charge stats */
379void ln8411_chg_stats_init(struct ln8411_chg_stats *chg_data);
380s32 ln8411_chg_stats_update(struct ln8411_chg_stats *chg_data,
381 const struct ln8411_charger *ln8411);
382s32 ln8411_chg_stats_done(struct ln8411_chg_stats *chg_data,
383 const struct ln8411_charger *ln8411);
384void ln8411_chg_stats_dump(const struct ln8411_charger *ln8411);
385
386#endif