From patchwork Mon Aug 22 06:49:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wenyou Yang X-Patchwork-Id: 9293051 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 37881607FF for ; Mon, 22 Aug 2016 06:53:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 289CF287E1 for ; Mon, 22 Aug 2016 06:53:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1D2AD28932; Mon, 22 Aug 2016 06:53:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9A167287E1 for ; Mon, 22 Aug 2016 06:53:48 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bbj5n-0005Xa-EO; Mon, 22 Aug 2016 06:52:19 +0000 Received: from eusmtp01.atmel.com ([212.144.249.243]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bbj4n-0004Vo-Nc for linux-arm-kernel@lists.infradead.org; Mon, 22 Aug 2016 06:51:23 +0000 Received: from apsmtp01.atmel.com (10.168.254.31) by eusmtp01.atmel.com (10.145.145.31) with Microsoft SMTP Server id 14.3.235.1; Mon, 22 Aug 2016 08:50:55 +0200 Received: from shaarm01.corp.atmel.com (10.168.254.13) by apsmtp01.atmel.com (10.168.254.31) with Microsoft SMTP Server id 14.3.235.1; Mon, 22 Aug 2016 14:58:12 +0800 From: Wenyou Yang To: Sebastian Reichel , Dmitry Eremin-Solenikov , David Woodhouse , Rob Herring , Pawel Moll , Mark Brown , Ian Campbell , "Kumar Gala" Subject: [PATCH v7 3/7] power: act8945a_charger: Add status change update support Date: Mon, 22 Aug 2016 14:49:13 +0800 Message-ID: <1471848557-27278-4-git-send-email-wenyou.yang@atmel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1471848557-27278-1-git-send-email-wenyou.yang@atmel.com> References: <1471848557-27278-1-git-send-email-wenyou.yang@atmel.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160821_235118_498509_1CDA0732 X-CRM114-Status: GOOD ( 15.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: devicetree@vger.kernel.org, linux-pm@vger.kernel.org, Nicolas Ferre , linux-kernel@vger.kernel.org, Wenyou Yang , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add the charger status change interrupt support, it will report the power supply changed event. This interrupt is generated by one of the conditions as below: - the state machine jumps out of or into the EOC state - the CHGIN input voltage goes out of or into the valid range. - the battery temperature goes out of or into the valid range. - the PRECHARGE time-out occurs. - the total charge time-out occurs. Signed-off-by: Wenyou Yang --- Changes in v7: - Fix the wrong using devm_request_irq() with with parent device as *dev. Changes in v6: - Add .remove callback function. - Fix the 'dev' argument of devm_request_irq() to pdev->dev.parent. Changes in v5: None Changes in v4: - Use "interrupts" property, instead of "active-semi,irq-gpios" for irq. Changes in v3: None Changes in v2: None drivers/power/supply/act8945a_charger.c | 95 ++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 7 deletions(-) diff --git a/drivers/power/supply/act8945a_charger.c b/drivers/power/supply/act8945a_charger.c index cee8f75..fd99739 100644 --- a/drivers/power/supply/act8945a_charger.c +++ b/drivers/power/supply/act8945a_charger.c @@ -10,9 +10,11 @@ * published by the Free Software Foundation. * */ +#include #include #include #include +#include #include #include #include @@ -75,7 +77,11 @@ static const char *act8945a_charger_manufacturer = "Active-semi"; #define APCH_STATE_CSTATE_PRE 0x03 struct act8945a_charger { + struct power_supply *psy; struct regmap *regmap; + struct work_struct work; + + bool init_done; }; static int act8945a_get_charger_state(struct regmap *regmap, int *val) @@ -252,6 +258,47 @@ static const struct power_supply_desc act8945a_charger_desc = { .num_properties = ARRAY_SIZE(act8945a_charger_props), }; +static int act8945a_enable_interrupt(struct act8945a_charger *charger) +{ + struct regmap *regmap = charger->regmap; + unsigned char ctrl; + int ret; + + ctrl = APCH_CTRL_CHGEOCOUT | APCH_CTRL_CHGEOCIN | + APCH_CTRL_INDIS | APCH_CTRL_INCON | + APCH_CTRL_TEMPOUT | APCH_CTRL_TEMPIN | + APCH_CTRL_TIMRPRE | APCH_CTRL_TIMRTOT; + ret = regmap_write(regmap, ACT8945A_APCH_CTRL, ctrl); + if (ret) + return ret; + + ctrl = APCH_STATUS_CHGSTAT | APCH_STATUS_INSTAT | + APCH_STATUS_TEMPSTAT | APCH_STATUS_TIMRSTAT; + ret = regmap_write(regmap, ACT8945A_APCH_STATUS, ctrl); + if (ret) + return ret; + + return 0; +} + +static void act8945a_work(struct work_struct *work) +{ + struct act8945a_charger *charger = + container_of(work, struct act8945a_charger, work); + + power_supply_changed(charger->psy); +} + +static irqreturn_t act8945a_status_changed(int irq, void *dev_id) +{ + struct act8945a_charger *charger = dev_id; + + if (charger->init_done) + schedule_work(&charger->work); + + return IRQ_HANDLED; +} + #define DEFAULT_TOTAL_TIME_OUT 3 #define DEFAULT_PRE_TIME_OUT 40 #define DEFAULT_INPUT_OVP_THRESHOLD 6600 @@ -362,9 +409,8 @@ static int act8945a_charger_config(struct device *dev, static int act8945a_charger_probe(struct platform_device *pdev) { struct act8945a_charger *charger; - struct power_supply *psy; struct power_supply_config psy_cfg = {}; - int ret; + int irq, ret; charger = devm_kzalloc(&pdev->dev, sizeof(*charger), GFP_KERNEL); if (!charger) @@ -380,17 +426,51 @@ static int act8945a_charger_probe(struct platform_device *pdev) if (ret) return ret; + irq = of_irq_get(pdev->dev.parent->of_node, 0); + if (irq == -EPROBE_DEFER) { + dev_err(&pdev->dev, "failed to find IRQ number\n"); + return -EPROBE_DEFER; + } + + ret = devm_request_irq(&pdev->dev, irq, act8945a_status_changed, + IRQF_TRIGGER_FALLING, "act8945a_interrupt", + charger); + if (ret) { + dev_err(&pdev->dev, "failed to request nIRQ pin IRQ\n"); + return ret; + } + psy_cfg.of_node = pdev->dev.parent->of_node; psy_cfg.drv_data = charger; - psy = devm_power_supply_register(&pdev->dev, - &act8945a_charger_desc, - &psy_cfg); - if (IS_ERR(psy)) { + charger->psy = devm_power_supply_register(&pdev->dev, + &act8945a_charger_desc, + &psy_cfg); + if (IS_ERR(charger->psy)) { dev_err(&pdev->dev, "failed to register power supply\n"); - return PTR_ERR(psy); + return PTR_ERR(charger->psy); } + platform_set_drvdata(pdev, charger); + + INIT_WORK(&charger->work, act8945a_work); + + ret = act8945a_enable_interrupt(charger); + if (ret) + return -EIO; + + charger->init_done = true; + + return 0; +} + +static int act8945a_charger_remove(struct platform_device *pdev) +{ + struct act8945a_charger *charger = platform_get_drvdata(pdev); + + charger->init_done = false; + cancel_work_sync(&charger->work); + return 0; } @@ -399,6 +479,7 @@ static struct platform_driver act8945a_charger_driver = { .name = "act8945a-charger", }, .probe = act8945a_charger_probe, + .remove = act8945a_charger_remove, }; module_platform_driver(act8945a_charger_driver);