blob: be1b86d352a0c4c587f96e28d77f14999a5d249f [file] [log] [blame]
Mike Frysinger71b2ef72022-09-12 18:54:361/* Copyright 2016 The ChromiumOS Authors
Mary Ruthven9b4f6622016-02-23 22:56:282 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5/* Detect what adapter is connected */
6
7#include "charge_manager.h"
8#include "hooks.h"
9#include "registers.h"
10#include "timer.h"
11
12static void enable_usb(void)
13{
14 /* Enable USB device clock. */
15 STM32_RCC_APB1ENR |= STM32_RCC_PB1_USB;
16}
17DECLARE_HOOK(HOOK_INIT, enable_usb, HOOK_PRIO_DEFAULT);
18
19static void disable_usb(void)
20{
21 /* Disable USB device clock. */
22 STM32_RCC_APB1ENR &= ~STM32_RCC_PB1_USB;
23}
24DECLARE_HOOK(HOOK_SYSJUMP, disable_usb, HOOK_PRIO_DEFAULT);
25
26static uint16_t detect_type(uint16_t det_type)
27{
28 STM32_USB_BCDR &= 0;
Patryk Duda85d75982024-04-10 13:10:5929 crec_usleep(1);
Mary Ruthven9b4f6622016-02-23 22:56:2830 STM32_USB_BCDR |= (STM32_USB_BCDR_BCDEN | det_type);
Patryk Duda85d75982024-04-10 13:10:5931 crec_usleep(1);
Mary Ruthven9b4f6622016-02-23 22:56:2832 STM32_USB_BCDR &= ~(STM32_USB_BCDR_BCDEN | det_type);
33 return STM32_USB_BCDR;
34}
35
Mary Ruthven9b4f6622016-02-23 22:56:2836int charger_detect_get_device_type(void)
37{
38 uint16_t pdet_result;
39
40 if (!(detect_type(STM32_USB_BCDR_DCDEN) & STM32_USB_BCDR_DCDET))
41 return CHARGE_SUPPLIER_PD;
42
43 pdet_result = detect_type(STM32_USB_BCDR_PDEN);
44 /* TODO: add support for detecting proprietary chargers. */
45 if (pdet_result & STM32_USB_BCDR_PDET) {
46 if (detect_type(STM32_USB_BCDR_SDEN) & STM32_USB_BCDR_SDET)
47 return CHARGE_SUPPLIER_BC12_DCP;
48 else
49 return CHARGE_SUPPLIER_BC12_CDP;
50 } else if (pdet_result & STM32_USB_BCDR_PS2DET)
51 return CHARGE_SUPPLIER_PROPRIETARY;
52 else
53 return CHARGE_SUPPLIER_BC12_SDP;
54}