summaryrefslogtreecommitdiff
path: root/include/peripheral_charger.h
blob: e4b4993d4711d65d444bb0f465cc169cf61e282a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef __CROS_EC_PERIPHERAL_CHARGER_H
#define __CROS_EC_PERIPHERAL_CHARGER_H

#include "atomic.h"
#include "common.h"
#include "ec_commands.h"
#include "gpio.h"
#include "queue.h"
#include "stdbool.h"
#include "task.h"

/*
 * Peripheral charge manager
 *
 * Peripheral charge manager (PCHG) is a state machine (SM), which manages
 * charge ports to charge peripheral devices. Events can be generated
 * externally (by a charger chip) or internally (by a host command or the SM
 * itself). Events are queued and handled first-come-first-serve basis.
 *
 * Peripheral charger drivers should implement struct pchg_drv. Each operation
 * can be synchronous or asynchronous depending on the chip. If a function
 * works synchronously, it should return EC_SUCCESS. That'll make the SM
 * immediately queue the next event (if applicable) and transition to the next
 * state. If a function works asynchronously, it should return
 * EC_SUCCESS_IN_PROGRESS. That'll make the SM stay in the same state. The SM
 * is expected to receive IRQ for further information about the operation,
 * which may or may not make the SM transition to the next state.
 *
 * Roughly speaking the SM looks as follows:
 *
 *                  +---------------+
 *                  |     RESET     |
 *                  +-------+-------+
 *                          |
 *                          | INITIALIZED
 *                          v
 *                  +---------------+
 *                  |  INITIALIZED  |<--------------+
 *                  +------+--------+               |
 *                         | ^                      |
 *                 ENABLED | | DISABLED             |
 *                         v |                      |
 *                  +--------+------+               |
 *   +------------->|    ENABLED    |               |
 *   |              +-----+------+--+               |
 *   |                    |      |                  |
 *   |    DEVICE_DETECTED |      | DEVICE_CONNECTED |
 *   |                    v      |                  |
 *   | DEVICE_LOST  +----------+ |                  |
 *   +--------------+ DETECTED +-|------------------+
 *   |              +-----+----+ |     ERROR        |
 *   |                    |      |                  |
 *   |    DEVICE_CONNECTED|      |                  |
 *   |                    v      v                  |
 *   |              +---------------+               |
 *   +--------------+   CONNECTED   +---------------+
 *   | DEVICE_LOST  +------+--------+  ERROR        |
 *   |                     | ^                      |
 *   |      CHARGE_STARTED | | CHARGE_ENDED         |
 *   |                     | | CHARGE_STOPPED       |
 *   |                     v |                      |
 *   |              +--------+------+               |
 *   +--------------+   CHARGING    +---------------+
 *     DEVICE_LOST  +---------------+  ERROR
 *
 *
 * In download (firmware update) mode, the state machine transitions as follows:
 *
 *                  +---------------+
 *                  |   DOWNLOAD    |
 *                  +------+--------+
 *                         | ^
 *             UPDATE_OPEN | |
 *                         | | UPDATE_CLOSE
 *                         v |
 *                  +--------+------+
 *              +-->|  DOWNLOADING  |
 *              |   +------+--------+
 *              |          |
 *              +----------+
 *              UPDATE_WRITE
 */

/*
 * BIST Mode
 *
 * BIST mode is implemented as follows. Note == means comparison (i.e. mode
 * check) and = means assignment (i.e. mode change).
 *
 *
 *                  +---------------+
 *   +------------->|     RESET     |<-----------------------+
 *   |              +-------+-------+                        |
 *   |                      |                                |
 *   |                      | INITIALIZED                    |
 *   |                      v                                |
 *   |              +---------------+  mode==BIST            |
 *   |              |  INITIALIZED  |---------------+        |
 *   |              +-------+-------+               |        |
 *   |                      |                       |        |
 *   |                      | mode==NORMAL          |        | mode=NORMAL
 *   |                      |                       |        | bist_cmd=NONE
 *   | DEVICE_DETECTED      |                       |        |
 *   | && bist_cmd!=NONE    V                       V        |
 *   | mode=BIST    +---------------+         +-----------+  |
 *   +--------------|   ENABLED(*1) |         |    BIST   |--+
 *   +------------->+------+--------+         +-----------+
 *   |                     |
 *   |                     | DEVICE_DETECTED
 *   |                     |
 *   | DEVICE_LOST         |
 *   | bist_cmd=RF_CHARGE  V
 *   | (*2)          +----------+
 *   +---------------+ DETECTED |
 *                   +----------+
 *
 * *1 BIST mode is entered on device detection when a BIST command is already
 *    requested (bist_cmd!=NONE).
 * *2 Whenever a device is lost, we reset bist_cmd to RF_CHARGE. This makes next
 *    device detection trigger BIST mode.
 */

/* Size of event queue. Use it to initialize struct pchg.events. */
#define PCHG_EVENT_QUEUE_SIZE 8

enum pchg_event {
	/* No event */
	PCHG_EVENT_NONE = 0,

	/* IRQ is pending. */
	PCHG_EVENT_IRQ,

	/* External Events */
	PCHG_EVENT_RESET,
	PCHG_EVENT_INITIALIZED,
	PCHG_EVENT_ENABLED,
	PCHG_EVENT_DISABLED,
	PCHG_EVENT_DEVICE_DETECTED,
	PCHG_EVENT_DEVICE_CONNECTED,
	PCHG_EVENT_DEVICE_LOST,
	PCHG_EVENT_CHARGE_STARTED,
	PCHG_EVENT_CHARGE_UPDATE,
	PCHG_EVENT_CHARGE_ENDED,
	PCHG_EVENT_CHARGE_STOPPED,
	PCHG_EVENT_UPDATE_OPENED,
	PCHG_EVENT_UPDATE_CLOSED,
	PCHG_EVENT_UPDATE_WRITTEN,
	PCHG_EVENT_IN_NORMAL,

	/* Errors */
	PCHG_EVENT_ERROR,

	/* Internal (a.k.a. Host) Events */
	PCHG_EVENT_ENABLE,
	PCHG_EVENT_DISABLE,
	PCHG_EVENT_BIST_RUN,
	PCHG_EVENT_BIST_DONE,
	PCHG_EVENT_UPDATE_OPEN,
	PCHG_EVENT_UPDATE_WRITE,
	PCHG_EVENT_UPDATE_CLOSE,
	PCHG_EVENT_UPDATE_ERROR,

	/* Counter. Add new entry above. */
	PCHG_EVENT_COUNT
};
BUILD_ASSERT(PCHG_EVENT_COUNT <= sizeof(uint32_t) * 8);

enum pchg_error {
	/* Communication error in link layer (i2c, spi, etc.). */
	PCHG_ERROR_COMMUNICATION,

	PCHG_ERROR_OVER_TEMPERATURE,
	PCHG_ERROR_OVER_CURRENT,
	PCHG_ERROR_FOREIGN_OBJECT,

	/* Protocol error (e.g. NACK returned from a chip). */
	PCHG_ERROR_RESPONSE,
	/* Other errors reported by a chip. */
	PCHG_ERROR_FW_VERSION,
	PCHG_ERROR_INVALID_FW,
	PCHG_ERROR_WRITE_FLASH,

	/* All other errors */
	PCHG_ERROR_OTHER,

	/* Add no entries below here. */
	PCHG_ERROR_COUNT
};

#define PCHG_ERROR_MASK(e) BIT(e)

enum pchg_mode {
	PCHG_MODE_NORMAL = 0,
	PCHG_MODE_DOWNLOAD,
	PCHG_MODE_PASSTHRU,
	PCHG_MODE_BIST,
	/* Add no more entries below here. */
	PCHG_MODE_COUNT
};

enum pchg_bist_cmd {
	PCHG_BIST_CMD_ANTENNA = 0x00,
	PCHG_BIST_CMD_RF_CHARGE_ON = 0x01,
	PCHG_BIST_CMD_RF_CHARGE_OFF = 0x02,

	/* Add no more entries below here. */
	PCHG_BIST_CMD_NONE = 0xff
};

enum pchg_chipset_state {
	PCHG_CHIPSET_STATE_ON = 0,
	PCHG_CHIPSET_STATE_SUSPEND,
	/* No more new entries below here */
	PCHG_CHIPSET_STATE_COUNT
};

/**
 * Data struct describing the configuration of a peripheral charging port.
 */
struct pchg_config {
	/* Charger driver */
	struct pchg_drv *drv;
	/* I2C port number */
	int i2c_port;
	/* GPIO pin used for IRQ */
	enum gpio_signal irq_pin;
	/* Full battery percentage */
	uint8_t full_percent;
	/* Update block size */
	uint32_t block_size;
	/* RF charge duration in msec. Set it to 0 to disable RF charge. */
	uint16_t rf_charge_msec;
};

struct pchg_update {
	/* Version of new firmware. Usually used by EC_PCHG_UPDATE_CMD_OPEN. */
	uint32_t version;
	/* CRC32 of new firmware. Usually used by EC_PCHG_UPDATE_CMD_CLOSE. */
	uint32_t crc32;
	/* Address which <data> will be written to. */
	uint32_t addr;
	/* Size of <data> */
	uint32_t size;
	/* 0: No data. 1: Data is ready for write. */
	uint8_t data_ready;
	/* Partial data of new firmware */
	uint8_t data[128];
};

struct pchg_policy_t {
	uint32_t evt_mask;
	uint32_t err_mask;
};

extern struct pchg_policy_t pchg_policy_on;
extern struct pchg_policy_t pchg_policy_suspend;

/**
 * Data struct describing the status of a peripheral charging port. It provides
 * the state machine and a charger driver with a context to work on.
 */
struct pchg {
	/* Static configuration */
	const struct pchg_config *const cfg;
	/* Event & error report policy. */
	struct pchg_policy_t *policy[PCHG_CHIPSET_STATE_COUNT];
	/* Current state of the port */
	enum pchg_state state;
	/* Event queue */
	struct queue const events;
	/* Event queue mutex */
	struct mutex mtx;
	/* 1:Pending IRQ 0:No pending IRQ */
	atomic_t irq;
	/* Event currently being handled */
	enum pchg_event event;
	/* Error (enum pchg_error). Port is disabled until it's cleared. */
	uint32_t error;
	/* Battery percentage (0% ~ 100%) of the connected peripheral device */
	uint8_t battery_percent;
	/* Number of dropped events (due to queue overflow) */
	uint32_t dropped_event_count;
	/* Number of dropped host events (due to queue overflow) */
	uint32_t dropped_host_event_count;
	/* enum pchg_mode */
	uint8_t mode;
	/* enum pchg_bist_cmd */
	uint8_t bist_cmd;
	/* FW version */
	uint32_t fw_version;
	/* Context related to FW update */
	struct pchg_update update;
};

/**
 * Peripheral charger driver
 *
 * These functions shall return only communication errors (e.g. i2c error). If
 * the error is internal to PCHG, they should return EC_SUCCESS and set
 * ctx->event to PCHG_EVENT_ERROR and set a flag in ctx->error.
 */
struct pchg_drv {
	/*
	 * Reset charger chip. External reset (e.g by GPIO). No
	 * communication or data access is expected (e.g. no I2C access).
	 */
	int (*reset)(struct pchg *ctx);
	/*
	 * Initialize the charger. Run setup needed only once per reset
	 * (e.g. enable I2C, unlock I2C).
	 */
	int (*init)(struct pchg *ctx);
	/* Enable/disable the charger. */
	int (*enable)(struct pchg *ctx, bool enable);
	/*
	 * Get chip info, identify chip and setup function pointers
	 * (e.g. I2C read function). It needs to work without IRQ.
	 */
	int (*get_chip_info)(struct pchg *ctx);
	/* Get event info. */
	int (*get_event)(struct pchg *ctx);
	/* Get battery level. */
	int (*get_soc)(struct pchg *ctx);
	/* open update session */
	int (*update_open)(struct pchg *ctx);
	/* write update image */
	int (*update_write)(struct pchg *ctx);
	/* close update session */
	int (*update_close)(struct pchg *ctx);
	/* Toggle pass-through mode. */
	int (*passthru)(struct pchg *ctx, bool enable);
	/* Control BIST commands. */
	int (*bist)(struct pchg *ctx, uint8_t test_id);
};

/**
 * Array storing configs and states of all the peripheral charging ports.
 * Should be defined in board.c.
 */
extern struct pchg pchgs[];
extern const int pchg_count;

/* Utility macro converting port config to port number. */
#define PCHG_CTX_TO_PORT(ctx) ((ctx) - &pchgs[0])

/**
 * Interrupt handler for a peripheral charger.
 *
 * @param signal
 */
void pchg_irq(enum gpio_signal signal);

/**
 * Task running a state machine for charging peripheral devices.
 */
void pchg_task(void *u);

/**
 * Turn on/off power for a PCHG charger.
 *
 * @param port  Port number of the PCHG charger.
 * @param on
 */
__override_proto void board_pchg_power_on(int port, bool on);

#endif /* __CROS_EC_PERIPHERAL_CHARGER_H */