Linux核心中USB充電器的解決方案

來源:互聯網
上載者:User

Linux核心中USB充電器的解決方案 當前最新的核心(v3.5)對USB充電器的整體方案支援的不是太好,這裡討論的USB充電器的方案僅指軟體 方案,即充電器的檢測需要由軟體幹預(比如讀取USB PHY的寄存器), 同時電池的充電值根據 充電器的不同類型,需要由軟體來設定。 硬體檢測充電器及硬體自動選擇充電電流不在此討論之列。 軟體的方案的好處是: 可以省去一個USB充電器的檢測晶片以及使用更簡單(便宜)的電池管理晶片,  www.2cto.com   壞處是要由軟體幹預(可能有bug)以及不能實現低電量時高充電電流的充電方式。 一個整套的USB充電器方案,應該包括: 1. USB充電器的識別, 判斷出該USB Charger是如下USB Charger中的一種: - SDP (Standard Downstream Port) - DCP (Dedicated Charging Port) - CDP (Charging Downstream Port) - ACA (Accessory Charger Adapters) 通常識別的比較多的是: SDP (接PC) 和 DCP (專用charger, DP和DM短接)。 2. USB充電器的識別與電池管理晶片的介面 即如何把充電結果告知電池管理晶片的驅動, 這裡需要用到核心Power Supply的架構。 在軟體實現USB充電的方案中,通常會有2個驅動,因為充電器識別模組和電池充電模組是 2個硬體模組。根據我對核心對應代碼的理解,有如下兩種方法可以實現Linux USB充電器的解決方案。  www.2cto.com   方法一:1. 1.1 在USB充電器的識別驅動裡(在USB PHY裡或單獨的充電器驅動)設定一個notifier, 當 充電器識別完成後,將對應的事件和值傳遞給對應註冊在這個notifier上的使用者。 probe函數中調用:
 ATOMIC_INIT_NOTIFIER_HEAD(&notifier); 當檢測完成後調用: atomic_notifier_call_chain(&notifier, event, v); 1.2 同時還要實現: psy->external_power_changed (psy為struct power_supply的指標),這樣當 充電器識別完成後,調用【1】power_supply_changed(psy)就可以改變相應的屬性值, 然後使用者程式通過/sys/class/power_supply/就可以知道所有的屬性值。 2. 2.1 在電池驅動裡註冊一個notifier,以及對應的notifier的函數 probe函數中: usb_phy = usb_get_transceiver(); usb_register_notifier(usb_phy, nb); nb.notifier_call = set_charging_current; 2.2 set_charger_current函數中: 將充電電流的值設定到電池管理晶片中去,通常用I2C或SPI介面完成。(根據電池管理晶片的 串列介面不同而不同)。  www.2cto.com   方案二: 無需在2個driver裡實現notifier的介面。 1. 在電池管理晶片驅動裡要實現,方案一中的1.2部分。 2. 在電池管理晶片驅動裡通過power_supply_get_by_name(name)得到對應power supply的指標。 這裡的name可以由platform data傳入或寫在device tree的對應的node裡。 這個name必須和 USB充電器的識別驅動註冊的power supply的名字相同。 【1】要實現此功能,還需要如下patch: Subject: [PATCH] power_supply: add get_supplier_property Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>--- drivers/power/power_supply_core.c |   32 ++++++++++++++++++++++++++++++++ include/linux/power_supply.h      |    3 +++ 2 files changed, 35 insertions(+), 0 deletions(-) diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.cindex 329b46b..fb7843b 100644--- a/drivers/power/power_supply_core.c+++ b/drivers/power/power_supply_core.c@@ -94,6 +94,38 @@ int power_supply_am_i_supplied(struct power_supply *psy) } EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); +static int power_supply_find_supplier(struct device *dev, void *data)+{+    struct power_supply *psy = (struct power_supply *)data;+    struct power_supply *epsy = dev_get_drvdata(dev);+    int i;  www.2cto.com  ++    for (i = 0; i < epsy->num_supplicants; i++)+        if (!strcmp(epsy->supplied_to[i], psy->name))+            return 1;+            +    return 0;+}++int power_supply_get_supplier_property(struct power_supply *psy,+                    enum power_supply_property psp,+                    union power_supply_propval *val)+{+    struct power_supply *epsy;+    struct device *dev;++    dev = class_find_device(power_supply_class, NULL, psy,+                power_supply_find_supplier);+    if (!dev)+        return 1;++    epsy = dev_get_drvdata(dev);+    put_device(dev);++    return epsy->get_property(epsy, psp, val);+}+EXPORT_SYMBOL_GPL(power_supply_get_supplier_property);+ static int __power_supply_is_system_supplied(struct device *dev, void *data) {     union power_supply_propval ret = {0,};diff --git a/include/linux/power_supply.h b/include/linux/power_supply.hindex 204c18d..0dd89bf 100644  www.2cto.com  --- a/include/linux/power_supply.h+++ b/include/linux/power_supply.h@@ -201,6 +201,9 @@ extern struct power_supply *power_supply_get_by_name(char *name); extern void power_supply_changed(struct power_supply *psy); extern int power_supply_am_i_supplied(struct power_supply *psy); extern int power_supply_set_battery_charged(struct power_supply *psy);+extern int power_supply_get_supplier_property(struct power_supply *psy,+                    enum power_supply_property psp,+                    union power_supply_propval *val);  #if defined(CONFIG_POWER_SUPPLY) || defined(CONFIG_POWER_SUPPLY_MODULE) extern int power_supply_is_system_supplied(void);  作者 hzpeterchen

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.