summaryrefslogtreecommitdiff
path: root/driver/bc12/pi3usb9281.c
blob: ee7f81433d16954256fd244196c9a2153153f315 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Pericom PI3USB3281 USB port switch driver.
 */

#include "charge_manager.h"
#include "common.h"
#include "console.h"
#include "ec_commands.h"
#include "gpio.h"
#include "hooks.h"
#include "i2c.h"
#include "pi3usb9281.h"
#include "task.h"
#include "timer.h"
#include "usb_charge.h"
#include "usb_pd.h"
#include "util.h"

 /* Console output macros */
#define CPUTS(outstr) cputs(CC_USBCHARGE, outstr)
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)

/* 8-bit I2C address */
#define PI3USB9281_I2C_ADDR (0x25 << 1)

/* Delay values */
#define PI3USB9281_SW_RESET_DELAY 20

/* Wait after a charger is detected to debounce pin contact order */
#define PI3USB9281_DETECT_DEBOUNCE_MS			1000
#define PI3USB9281_RESET_DEBOUNCE_MS			100
#define PI3USB9281_RESET_STARTUP_DELAY			(200 * MSEC)
#define PI3USB9281_RESET_STARTUP_DELAY_INTERVAL_MS	40

/* Store the state of our USB data switches so that they can be restored. */
static int usb_switch_state[CONFIG_USB_PD_PORT_COUNT];

static int pi3usb9281_reset(int port);
static int pi3usb9281_get_interrupts(int port);

static void select_chip(int port)
{
	struct pi3usb9281_config *chip = &pi3usb9281_chips[port];
	ASSERT(port < CONFIG_BC12_DETECT_PI3USB9281_CHIP_COUNT);

	if (chip->mux_lock) {
		mutex_lock(chip->mux_lock);
		gpio_set_level(chip->mux_gpio, chip->mux_gpio_level);
	}
}

static void unselect_chip(int port)
{
	struct pi3usb9281_config *chip = &pi3usb9281_chips[port];

	if (chip->mux_lock)
		/* Just release the mutex, no need to change the mux gpio */
		mutex_unlock(chip->mux_lock);
}

static uint8_t pi3usb9281_do_read(int port, uint8_t reg, int with_lock)
{
	struct pi3usb9281_config *chip = &pi3usb9281_chips[port];
	int res, val;

	if (with_lock)
		select_chip(port);

	res = i2c_read8(chip->i2c_port, PI3USB9281_I2C_ADDR, reg, &val);

	if (with_lock)
		unselect_chip(port);

	if (res)
		return 0xee;

	return val;
}

static uint8_t pi3usb9281_read_u(int port, uint8_t reg)
{
	return pi3usb9281_do_read(port, reg, 0);
}

static uint8_t pi3usb9281_read(int port, uint8_t reg)
{
	return pi3usb9281_do_read(port, reg, 1);
}

static int pi3usb9281_do_write(
	int port, uint8_t reg, uint8_t val, int with_lock)
{
	struct pi3usb9281_config *chip = &pi3usb9281_chips[port];
	int res;

	if (with_lock)
		select_chip(port);

	res = i2c_write8(chip->i2c_port, PI3USB9281_I2C_ADDR, reg, val);

	if (with_lock)
		unselect_chip(port);

	if (res)
		CPRINTS("PI3USB9281 I2C write failed");
	return res;
}

static int pi3usb9281_write(int port, uint8_t reg, uint8_t val)
{
	return pi3usb9281_do_write(port, reg, val, 1);
}

/* Write control register, taking care to correctly set reserved bits. */
static int pi3usb9281_do_write_ctrl(int port, uint8_t ctrl, int with_lock)
{
	return pi3usb9281_do_write(port, PI3USB9281_REG_CONTROL,
				(ctrl & PI3USB9281_CTRL_MASK) |
				PI3USB9281_CTRL_RSVD_1, with_lock);
}

static int pi3usb9281_write_ctrl(int port, uint8_t ctrl)
{
	return pi3usb9281_do_write_ctrl(port, ctrl, 1);
}

static int pi3usb9281_write_ctrl_u(int port, uint8_t ctrl)
{
	return pi3usb9281_do_write_ctrl(port, ctrl, 0);
}

/*
 * Mask particular interrupts (e.g. attach, detach, ovp, ocp).
 * 1: UnMask (enable). 0: Mask (disable)
 */
static int pi3usb9281_set_interrupt_mask(int port, uint8_t mask)
{
	return pi3usb9281_write(port, PI3USB9281_REG_INT_MASK, ~mask);
}

static void pi3usb9281_init(int port)
{
	uint8_t dev_id;

	dev_id = pi3usb9281_read(port, PI3USB9281_REG_DEV_ID);

	if (dev_id != PI3USB9281_DEV_ID && dev_id != PI3USB9281_DEV_ID_A)
		CPRINTS("PI3USB9281 invalid ID 0x%02x", dev_id);

	pi3usb9281_reset(port);
	pi3usb9281_enable_interrupts(port);
}


int pi3usb9281_enable_interrupts(int port)
{
	uint8_t ctrl;
	pi3usb9281_set_interrupt_mask(port, PI3USB9281_INT_ATTACH_DETACH);
	ctrl = pi3usb9281_read(port, PI3USB9281_REG_CONTROL);
	if (ctrl == 0xee)
		return EC_ERROR_UNKNOWN;

	return pi3usb9281_write_ctrl(port, ctrl & ~PI3USB9281_CTRL_INT_DIS);
}

static int pi3usb9281_disable_interrupts(int port)
{
	uint8_t ctrl = pi3usb9281_read(port, PI3USB9281_REG_CONTROL);
	int rv;

	if (ctrl == 0xee)
		return EC_ERROR_UNKNOWN;

	rv = pi3usb9281_write_ctrl(port, ctrl | PI3USB9281_CTRL_INT_DIS);
	pi3usb9281_get_interrupts(port);
	return rv;
}

static int pi3usb9281_get_interrupts(int port)
{
	return pi3usb9281_read(port, PI3USB9281_REG_INT);
}

int pi3usb9281_get_device_type(int port)
{
	return pi3usb9281_read(port, PI3USB9281_REG_DEV_TYPE) & 0x77;
}

static int pi3usb9281_get_charger_status(int port)
{
	return pi3usb9281_read(port, PI3USB9281_REG_CHG_STATUS) & 0x1f;
}

static int pi3usb9281_get_ilim(int device_type, int charger_status)
{
	/* Limit USB port current. 500mA for not listed types. */
	int current_limit_ma = 500;

	if (charger_status & PI3USB9281_CHG_CAR_TYPE1 ||
	    charger_status & PI3USB9281_CHG_CAR_TYPE2)
		current_limit_ma = 3000;
	else if (charger_status & PI3USB9281_CHG_APPLE_1A)
		current_limit_ma = 1000;
	else if (charger_status & PI3USB9281_CHG_APPLE_2A)
		current_limit_ma = 2000;
	else if (charger_status & PI3USB9281_CHG_APPLE_2_4A)
		current_limit_ma = 2400;
	else if (device_type & PI3USB9281_TYPE_CDP)
		current_limit_ma = 1500;
	else if (device_type & PI3USB9281_TYPE_DCP)
		current_limit_ma = 500;

	return current_limit_ma;
}

static int pi3usb9281_reset(int port)
{
	int rv = pi3usb9281_write(port, PI3USB9281_REG_RESET, 0x1);

	if (!rv)
		/* Reset takes ~15ms. Wait for 20ms to be safe. */
		msleep(PI3USB9281_SW_RESET_DELAY);

	return rv;
}

static int pi3usb9281_set_switch_manual(int port, int val)
{
	int res = EC_ERROR_UNKNOWN;
	uint8_t ctrl;

	select_chip(port);
	ctrl = pi3usb9281_read_u(port, PI3USB9281_REG_CONTROL);

	if (ctrl != 0xee) {
		if (val)
			ctrl &= ~PI3USB9281_CTRL_AUTO;
		else
			ctrl |= PI3USB9281_CTRL_AUTO;
		res = pi3usb9281_write_ctrl_u(port, ctrl);
	}

	unselect_chip(port);
	return res;
}

static int pi3usb9281_set_pins(int port, uint8_t val)
{
	return pi3usb9281_write(port, PI3USB9281_REG_MANUAL, val);
}

static int pi3usb9281_set_switches(int port, int open)
{
	int res = EC_ERROR_UNKNOWN;
	uint8_t ctrl;

	select_chip(port);
	ctrl = pi3usb9281_read_u(port, PI3USB9281_REG_CONTROL);

	if (ctrl != 0xee) {
		if (open)
			ctrl &= ~PI3USB9281_CTRL_SWITCH_AUTO;
		else
			ctrl |= PI3USB9281_CTRL_SWITCH_AUTO;
		res = pi3usb9281_write_ctrl_u(port, ctrl);
	}

	unselect_chip(port);
	return res;
}

void usb_charger_set_switches(int port, enum usb_switch setting)
{
	/* If switch is not changing then return */
	if (setting == usb_switch_state[port])
		return;
	if (setting != USB_SWITCH_RESTORE)
		usb_switch_state[port] = setting;
	CPRINTS("USB MUX %d", usb_switch_state[port]);
	task_set_event(TASK_ID_USB_CHG_P0 + port, USB_CHG_EVENT_MUX, 0);
}

static int pc3usb9281_read_interrupt(int port)
{
	timestamp_t timeout;
	timeout.val = get_time().val + PI3USB9281_RESET_STARTUP_DELAY;
	do {
		/* Read (& clear) possible attach & detach interrupt */
		if (pi3usb9281_get_interrupts(port) &
				PI3USB9281_INT_ATTACH_DETACH)
			return EC_SUCCESS;
		msleep(PI3USB9281_RESET_STARTUP_DELAY_INTERVAL_MS);
	} while (get_time().val < timeout.val);
	return EC_ERROR_TIMEOUT;
}

/*
 * Handle BC 1.2 attach & detach event
 *
 * On attach, it resets pi3usb9281 for debounce. This reset should immediately
 * trigger another attach or detach interrupt. If other (unexpected) event is
 * observed, it forwards the event so that the caller can handle it.
 */
static uint32_t bc12_detect(int port)
{
	int device_type, chg_status;
	uint32_t evt = 0;

	if (usb_charger_port_is_sourcing_vbus(port)) {
		/* If we're sourcing VBUS then we're not charging */
		device_type = PI3USB9281_TYPE_NONE;
		chg_status = PI3USB9281_CHG_NONE;
	} else {
		/* Set device type */
		device_type = pi3usb9281_get_device_type(port);
		chg_status = pi3usb9281_get_charger_status(port);
	}

	/* Debounce pin plug order if we detect a charger */
	if (device_type || PI3USB9281_CHG_STATUS_ANY(chg_status)) {
		/* next operation might trigger a detach interrupt */
		pi3usb9281_disable_interrupts(port);
		/*
		 * Ensure D+/D- are open before resetting
		 * Note: we can't simply call pi3usb9281_set_switches() because
		 * another task might override it and set the switches closed.
		 */
		pi3usb9281_set_switch_manual(port, 1);
		pi3usb9281_set_pins(port, 0);

		/* Delay to debounce pin attach order */
		msleep(PI3USB9281_DETECT_DEBOUNCE_MS);

		/*
		 * Reset PI3USB9281 to refresh detection registers. After reset,
		 * - Interrupt is globally disabled
		 * - All interrupts are unmasked (=enabled)
		 *
		 * WARNING: This reset is acceptable for samus_pd,
		 * but may not be acceptable for devices that have
		 * an OTG / device mode, as we may be interrupting
		 * the connection.
		 */
		pi3usb9281_reset(port);

		/*
		 * Restore data switch settings - switches return to
		 * closed on reset until restored.
		 */
		usb_charger_set_switches(port, USB_SWITCH_RESTORE);

		/*
		 * Wait after reset, before re-enabling interrupt, so that
		 * spurious interrupts from this port are ignored.
		 */
		msleep(PI3USB9281_RESET_DEBOUNCE_MS);

		/* Re-enable interrupts */
		pi3usb9281_enable_interrupts(port);

		/*
		 * Consume interrupt (expectedly) triggered by the reset.
		 * If it's other event (e.g. VBUS), return immediately.
		 */
		evt = task_wait_event(PI3USB9281_RESET_DEBOUNCE_MS * MSEC);
		if (evt & USB_CHG_EVENT_BC12)
			evt &= ~USB_CHG_EVENT_BC12;
		else if (evt & USB_CHG_EVENT_INTR)
			evt &= ~USB_CHG_EVENT_INTR;
		else
			return evt;

		/* Debounce is done. Registers should have trustworthy values */
		device_type = PI3USB9281_TYPE_NONE;
		chg_status = PI3USB9281_CHG_NONE;
		if (pc3usb9281_read_interrupt(port) == EC_SUCCESS) {
			device_type = pi3usb9281_get_device_type(port);
			chg_status = pi3usb9281_get_charger_status(port);
		}
	}

	/* Attachment: decode + update available charge */
	if (device_type || PI3USB9281_CHG_STATUS_ANY(chg_status)) {
		struct charge_port_info chg;
		int type;

		if (PI3USB9281_CHG_STATUS_ANY(chg_status))
			type = CHARGE_SUPPLIER_PROPRIETARY;
		else if (device_type & PI3USB9281_TYPE_CDP)
			type = CHARGE_SUPPLIER_BC12_CDP;
		else if (device_type & PI3USB9281_TYPE_DCP)
			type = CHARGE_SUPPLIER_BC12_DCP;
		else if (device_type & PI3USB9281_TYPE_SDP)
			type = CHARGE_SUPPLIER_BC12_SDP;
		else
			type = CHARGE_SUPPLIER_OTHER;

		chg.voltage = USB_CHARGER_VOLTAGE_MV;
		chg.current = pi3usb9281_get_ilim(device_type, chg_status);
		charge_manager_update_charge(type, port, &chg);
	} else {
		/* Detachment: update available charge to 0 */
		usb_charger_reset_charge(port);
	}

	return evt;
}

void usb_charger_task(void *u)
{
	int port = (task_get_current() == TASK_ID_USB_CHG_P0 ? 0 : 1);
	uint32_t evt;

	/* Initialize chip and enable interrupts */
	pi3usb9281_init(port);

	evt = bc12_detect(port);

	while (1) {
		/* Interrupt from the Pericom chip, determine charger type */
		if (evt & USB_CHG_EVENT_BC12) {
			/* Read interrupt register to clear on chip */
			pi3usb9281_get_interrupts(port);
			evt = bc12_detect(port);
		} else if (evt & USB_CHG_EVENT_INTR) {
			/* USB_CHG_EVENT_INTR & _BC12 are mutually exclusive */
			/* Check the interrupt register, and clear on chip */
			if (pi3usb9281_get_interrupts(port) &
					PI3USB9281_INT_ATTACH_DETACH)
				evt = bc12_detect(port);
		}

		if (evt & USB_CHG_EVENT_MUX)
			pi3usb9281_set_switches(port, usb_switch_state[port]);

		/*
		 * Re-enable interrupts on pericom charger detector since the
		 * chip may periodically reset itself, and come back up with
		 * registers in default state. TODO(crosbug.com/p/33823): Fix
		 * these unwanted resets.
		 */
		if (evt & USB_CHG_EVENT_VBUS) {
			pi3usb9281_enable_interrupts(port);
#ifndef CONFIG_USB_PD_VBUS_DETECT_TCPC
			CPRINTS("VBUS p%d %d", port,
				pd_snk_is_vbus_provided(port));
#endif
		}

		evt = task_wait_event(-1);
	}
}

#if defined(CONFIG_CHARGE_RAMP_SW) || defined(CONFIG_CHARGE_RAMP_HW)
int usb_charger_ramp_allowed(int supplier)
{
	return supplier == CHARGE_SUPPLIER_BC12_DCP ||
	       supplier == CHARGE_SUPPLIER_BC12_SDP ||
	       supplier == CHARGE_SUPPLIER_BC12_CDP ||
	       supplier == CHARGE_SUPPLIER_PROPRIETARY;
}

int usb_charger_ramp_max(int supplier, int sup_curr)
{
	switch (supplier) {
	case CHARGE_SUPPLIER_BC12_DCP:
		return 2000;
	case CHARGE_SUPPLIER_BC12_SDP:
		return 1000;
	case CHARGE_SUPPLIER_BC12_CDP:
	case CHARGE_SUPPLIER_PROPRIETARY:
		return sup_curr;
	default:
		return 500;
	}
}
#endif /* CONFIG_CHARGE_RAMP_SW || CONFIG_CHARGE_RAMP_HW */