Mike Frysinger | 71b2ef7 | 2022-09-12 18:54:36 | [diff] [blame] | 1 | /* Copyright 2016 The ChromiumOS Authors |
Mary Ruthven | 9b4f662 | 2016-02-23 22:56:28 | [diff] [blame] | 2 | * 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 | |
| 12 | static void enable_usb(void) |
| 13 | { |
| 14 | /* Enable USB device clock. */ |
| 15 | STM32_RCC_APB1ENR |= STM32_RCC_PB1_USB; |
| 16 | } |
| 17 | DECLARE_HOOK(HOOK_INIT, enable_usb, HOOK_PRIO_DEFAULT); |
| 18 | |
| 19 | static void disable_usb(void) |
| 20 | { |
| 21 | /* Disable USB device clock. */ |
| 22 | STM32_RCC_APB1ENR &= ~STM32_RCC_PB1_USB; |
| 23 | } |
| 24 | DECLARE_HOOK(HOOK_SYSJUMP, disable_usb, HOOK_PRIO_DEFAULT); |
| 25 | |
| 26 | static uint16_t detect_type(uint16_t det_type) |
| 27 | { |
| 28 | STM32_USB_BCDR &= 0; |
Patryk Duda | 85d7598 | 2024-04-10 13:10:59 | [diff] [blame] | 29 | crec_usleep(1); |
Mary Ruthven | 9b4f662 | 2016-02-23 22:56:28 | [diff] [blame] | 30 | STM32_USB_BCDR |= (STM32_USB_BCDR_BCDEN | det_type); |
Patryk Duda | 85d7598 | 2024-04-10 13:10:59 | [diff] [blame] | 31 | crec_usleep(1); |
Mary Ruthven | 9b4f662 | 2016-02-23 22:56:28 | [diff] [blame] | 32 | STM32_USB_BCDR &= ~(STM32_USB_BCDR_BCDEN | det_type); |
| 33 | return STM32_USB_BCDR; |
| 34 | } |
| 35 | |
Mary Ruthven | 9b4f662 | 2016-02-23 22:56:28 | [diff] [blame] | 36 | int 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 | } |