Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755094AbcDFJBw (ORCPT ); Wed, 6 Apr 2016 05:01:52 -0400 Received: from mail-am1on0083.outbound.protection.outlook.com ([157.56.112.83]:6325 "EHLO emea01-am1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753160AbcDFJBs convert rfc822-to-8bit (ORCPT ); Wed, 6 Apr 2016 05:01:48 -0400 From: Jun Li To: Baolin Wang , "balbi@kernel.org" , "gregkh@linuxfoundation.org" , "sre@kernel.org" , "dbaryshkov@gmail.com" , "dwmw2@infradead.org" CC: "peter.chen@freescale.com" , "stern@rowland.harvard.edu" , "r.baldyga@samsung.com" , "yoshihiro.shimoda.uh@renesas.com" , "lee.jones@linaro.org" , "broonie@kernel.org" , "ckeepax@opensource.wolfsonmicro.com" , "patches@opensource.wolfsonmicro.com" , "linux-pm@vger.kernel.org" , "linux-usb@vger.kernel.org" , "device-mainlining@lists.linuxfoundation.org" , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH v9 1/4] gadget: Introduce the usb charger framework Thread-Topic: [PATCH v9 1/4] gadget: Introduce the usb charger framework Thread-Index: AQHRi+d3gu1IGZmEqUKwuqyIMYkUm598m1sQ Date: Wed, 6 Apr 2016 08:26:45 +0000 Message-ID: References: <6c594cc66fd06b575b04cc8bb0fe0374d0501d4d.1459494744.git.baolin.wang@linaro.org> In-Reply-To: <6c594cc66fd06b575b04cc8bb0fe0374d0501d4d.1459494744.git.baolin.wang@linaro.org> Accept-Language: zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: linaro.org; dkim=none (message not signed) header.d=none;linaro.org; dmarc=none action=none header.from=nxp.com; x-originating-ip: [199.59.225.131] x-ms-office365-filtering-correlation-id: a3a0dd5d-764d-40a4-929b-08d35df53114 x-microsoft-exchange-diagnostics: 1;AM4PR04MB2132;5:h2jnTbquKjCYjEEzXotiOEZA9A7FyymXNH18loufKRzjGH/b6kJqpzgllfKRdc4KKjFlxR9sTAB/5VtCg/xWT9EE02eoZhshz29YWt4zENztt4sV4AyiIUfsXckEdoPMt642s8Ywy4Zgv8xnSoaWbg==;24:dpV9sstiDRx36dNdt7JNfPkENuluI/uowiSV4f4pJI57lhPhm9v2HAU6b/70eGmM6EhFpf/7Ffq87o1cbZ/SfBKA+2ljO+VSCCZtdob0q2g= x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AM4PR04MB2132; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:; x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026);SRVR:AM4PR04MB2132;BCL:0;PCL:0;RULEID:;SRVR:AM4PR04MB2132; x-forefront-prvs: 0904004ECB x-forefront-antispam-report: SFV:NSPM;SFS:(10009020)(6009001)(377454003)(13464003)(4326007)(87936001)(2906002)(19580405001)(19580395003)(86362001)(1096002)(54356999)(106116001)(81166005)(122556002)(50986999)(76176999)(5008740100001)(33656002)(11100500001)(3660700001)(76576001)(66066001)(3280700002)(74316001)(5004730100002)(92566002)(2950100001)(2900100001)(1220700001)(15975445007)(586003)(5002640100001)(2201001)(10400500002)(2501003)(5003600100002)(102836003)(6116002)(5001770100001)(189998001)(77096005)(3846002)(7059030)(422495003);DIR:OUT;SFP:1101;SCL:1;SRVR:AM4PR04MB2132;H:AM4PR04MB2130.eurprd04.prod.outlook.com;FPR:;SPF:None;MLV:sfv;LANG:en; spamdiagnosticoutput: 1:23 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-originalarrivaltime: 06 Apr 2016 08:26:45.9611 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR04MB2132 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4385 Lines: 142 Hi > -----Original Message----- > From: linux-usb-owner@vger.kernel.org [mailto:linux-usb- > owner@vger.kernel.org] On Behalf Of Baolin Wang > Sent: Friday, April 01, 2016 3:22 PM > To: balbi@kernel.org; gregkh@linuxfoundation.org; sre@kernel.org; > dbaryshkov@gmail.com; dwmw2@infradead.org > Cc: peter.chen@freescale.com; stern@rowland.harvard.edu; > r.baldyga@samsung.com; yoshihiro.shimoda.uh@renesas.com; > lee.jones@linaro.org; broonie@kernel.org; > ckeepax@opensource.wolfsonmicro.com; patches@opensource.wolfsonmicro.com; > baolin.wang@linaro.org; linux-pm@vger.kernel.org; linux- > usb@vger.kernel.org; device-mainlining@lists.linuxfoundation.org; linux- > kernel@vger.kernel.org > Subject: [PATCH v9 1/4] gadget: Introduce the usb charger framework > ... > +/* > + * usb_charger_get_type() - get the usb charger type with lock protection. > + * @uchger - usb charger device > + * > + * Users can get the charger type by this safe API, rather than using > +the > + * usb_charger structure directly. > + */ > +enum usb_charger_type usb_charger_get_type(struct usb_charger *uchger) > +{ > + enum usb_charger_type type; > + > + mutex_lock(&uchger->lock); > + type = uchger->type; > + mutex_unlock(&uchger->lock); > + > + return type; > +} > +EXPORT_SYMBOL_GPL(usb_charger_get_type); > + > +/* > + * usb_charger_detect_type() - detect the charger type manually. > + * @uchger - usb charger device > + * > + * Note: You should ensure you need to detect the charger type manually > +on your > + * platform. > + * You should call it at the right gadget state to avoid affecting > +gadget > + * enumeration. > + */ > +int usb_charger_detect_type(struct usb_charger *uchger) { > + enum usb_charger_type type; > + > + if (WARN(!uchger->charger_detect, > + "charger detection method should not be NULL")) > + return -EINVAL; > + > + type = uchger->charger_detect(uchger); > + > + mutex_lock(&uchger->lock); > + uchger->type = type; > + mutex_unlock(&uchger->lock); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(usb_charger_detect_type); > + > +/* > + * usb_charger_get_type_by_others() - Get the usb charger type by the > +callback > + * which is implemented by users. > + * @uchger - the usb charger device. > + * > + * Note: This function is just used for getting the charger type, not > +for > + * detecting charger type which might affect the DP/DM line when gadget > +is on > + * enumeration state. > + */ > +static enum usb_charger_type > +usb_charger_get_type_by_others(struct usb_charger *uchger) { > + if (uchger->type != UNKNOWN_TYPE) > + return uchger->type; > + > + if (uchger->psy) { > + union power_supply_propval val; > + > + power_supply_get_property(uchger->psy, > + POWER_SUPPLY_PROP_CHARGE_TYPE, > + &val); > + switch (val.intval) { > + case POWER_SUPPLY_TYPE_USB: > + uchger->type = SDP_TYPE; > + break; > + case POWER_SUPPLY_TYPE_USB_DCP: > + uchger->type = DCP_TYPE; > + break; > + case POWER_SUPPLY_TYPE_USB_CDP: > + uchger->type = CDP_TYPE; > + break; > + case POWER_SUPPLY_TYPE_USB_ACA: > + uchger->type = ACA_TYPE; > + break; > + default: > + uchger->type = UNKNOWN_TYPE; > + break; > + } > + } else if (uchger->get_charger_type) { > + uchger->type = uchger->get_charger_type(uchger); > + } else { > + uchger->type = UNKNOWN_TYPE; > + } > + > + return uchger->type; > +} > + I think we may don't need this usb_charger_get_type_by_others(). "uchger->type" is set in one place is enough, that is: by uchger->charger_detect() in usb_charger_detect_type(), then usb_charger_get_type_by_others() is replaced by usb_charger_get_type(). uchger->charger_detect() can have diff implementations no matter what kind of mechanism is used, for your PMIC case, you can just directly get the type value by power_supply_get_property(); with that, we can have one central place to set uchger->type. After uchger->type is set, charger type detection is no need to be involved until charger type changes. Then next question is where is to call usb_charger_detect_type(), We need make sure it finished before usb gadget connect. Ideal is with your framework, diff users only need implement uchger->charger_detect(). :) Li Jun > -- > To unsubscribe from this list: send the line "unsubscribe linux-usb" in > the body of a message to majordomo@vger.kernel.org More majordomo info at > http://vger.kernel.org/majordomo-info.html