From patchwork Tue Mar 10 21:27:23 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Belisko X-Patchwork-Id: 5980791 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D1AD39F318 for ; Tue, 10 Mar 2015 21:31:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E8C202024D for ; Tue, 10 Mar 2015 21:31:19 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F36EF20221 for ; Tue, 10 Mar 2015 21:31:18 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YVRhr-0007NN-2X; Tue, 10 Mar 2015 21:28:51 +0000 Received: from mail-we0-x235.google.com ([2a00:1450:400c:c03::235]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YVRhI-0006z8-Ab for linux-arm-kernel@lists.infradead.org; Tue, 10 Mar 2015 21:28:17 +0000 Received: by wevl61 with SMTP id l61so4750755wev.10 for ; Tue, 10 Mar 2015 14:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=G0oY+iWEK8Oa5UKU/rcf/fxooSqD8T0T5fz37NiYcdw=; b=vp+3sXh4FqYpp26hx/7ciWCzTiPFulnXmWRioExFBgTKms9LiNSnO2gvnqfOg3CbWD a8rvOH9NxHVydJjynMmrRYwvdqzscJuV6gDWvQ7pRXUIDfC4AGeAqe7qTrzm4pbib7hR qzMdVUNNtEIGRArTZFysOZVcS49yffWZgTcN/zODjgtCCIiHsEPPBofTs41/uSxmpriK FkUiGpbCf3Fz7pkkwvuyCg8nNS9O0nD5/UPY/ezpTT7D6QADlfyZ9jch52M+L57o9bTY Woa6/wKYIPUwdEAbhcpKTpwvuNlZBPOtqWTkQkDSyu07WNEnJgIzq2jaJ0cUZNADSE0K ZDUA== X-Received: by 10.180.12.84 with SMTP id w20mr74716307wib.9.1426022872597; Tue, 10 Mar 2015 14:27:52 -0700 (PDT) Received: from marek.lan (ip-85.163.101.103.o2inet.sk. [85.163.101.103]) by mx.google.com with ESMTPSA id nb18sm3311653wic.3.2015.03.10.14.27.48 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 10 Mar 2015 14:27:52 -0700 (PDT) From: Marek Belisko To: bcousson@baylibre.com, tony@atomide.com, sre@kernel.org, dbaryshkov@gmail.com, dwmw2@infradead.org Subject: [PATCH v4 2/6] power: twl4030_madc_battery: Add device tree support Date: Tue, 10 Mar 2015 22:27:23 +0100 Message-Id: <1426022847-30912-3-git-send-email-marek@goldelico.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1426022847-30912-1-git-send-email-marek@goldelico.com> References: <1426022847-30912-1-git-send-email-marek@goldelico.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150310_142816_545601_559F290D X-CRM114-Status: GOOD ( 15.47 ) X-Spam-Score: -0.5 (/) Cc: mark.rutland@arm.com, devicetree@vger.kernel.org, pawel.moll@arm.com, ijc+devicetree@hellion.org.uk, hns@goldelico.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, robh+dt@kernel.org, galak@codeaurora.org, Marek Belisko , linux-omap@vger.kernel.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Signed-off-by: Marek Belisko --- drivers/power/twl4030_madc_battery.c | 81 ++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/drivers/power/twl4030_madc_battery.c b/drivers/power/twl4030_madc_battery.c index 6af28b5..4bcb4a9 100644 --- a/drivers/power/twl4030_madc_battery.c +++ b/drivers/power/twl4030_madc_battery.c @@ -20,6 +20,7 @@ #include #include #include +#include struct twl4030_madc_battery { struct power_supply psy; @@ -183,6 +184,82 @@ static int twl4030_cmp(const void *a, const void *b) ((struct twl4030_madc_bat_calibration *)a)->voltage; } +#ifdef CONFIG_OF +static struct twl4030_madc_bat_platform_data * + twl4030_madc_dt_probe(struct platform_device *pdev) +{ + struct twl4030_madc_bat_platform_data *pdata; + struct device_node *np = pdev->dev.of_node; + int ret; + int i, proplen; + + pdata = devm_kzalloc(&pdev->dev, + sizeof(struct twl4030_madc_bat_platform_data), + GFP_KERNEL); + if (!pdata) + return ERR_PTR(-ENOMEM); + + ret = of_property_read_u32(np, "capacity-uah", &pdata->capacity); + if (ret != 0) + return ERR_PTR(-EINVAL); + + /* parse and prepare charging data */ + proplen = of_property_count_u32_elems(np, "ti,volt-to-capacity-charging-map"); + if (proplen < 0) { + dev_warn(&pdev->dev, "No 'ti,volt-to-capacity-charging-map' property found\n"); + return ERR_PTR(-EINVAL); + } + + pdata->charging = devm_kzalloc(&pdev->dev, + sizeof(struct twl4030_madc_bat_calibration) * (proplen / 2), + GFP_KERNEL); + + for (i = 0; i < proplen / 2; i++) { + of_property_read_u32_index(np, "ti,volt-to-capacity-charging-map", + i * 2, + (u32 *)&pdata->charging[i].voltage); + of_property_read_u32_index(np, "ti,volt-to-capacity-charging-map", + i * 2 + 1, + (u32 *)&pdata->charging[i].level); + } + + /* parse and prepare discharging data */ + proplen = of_property_count_u32_elems(np, + "ti,volt-to-capacity-discharging-map"); + if (proplen < 0) { + dev_warn(&pdev->dev, "No 'ti,volt-to-capacity-discharging-map' property found\n"); + return ERR_PTR(-EINVAL); + } + + pdata->discharging = devm_kzalloc(&pdev->dev, + sizeof(struct twl4030_madc_bat_calibration) * (proplen / 2), + GFP_KERNEL); + + for (i = 0; i < proplen / 2; i++) { + of_property_read_u32_index(np, "ti,volt-to-capacity-discharging-map", + i * 2, + (u32 *)&pdata->discharging[i].voltage); + of_property_read_u32_index(np, "ti,volt-to-capacity-discharging-map", + i * 2 + 1, + (u32 *)&pdata->discharging[i].level); + } + + return pdata; +} + +static const struct of_device_id of_twl4030_madc_match[] = { + { .compatible = "ti,twl4030-madc-battery", }, + {}, +}; + +#else +static struct twl4030_madc_bat_platform_data * + twl4030_madc_dt_probe(struct platform_device *pdev) +{ + return ERR_PTR(-ENODEV); +} +#endif + static int twl4030_madc_battery_probe(struct platform_device *pdev) { struct twl4030_madc_battery *twl4030_madc_bat; @@ -194,6 +271,9 @@ static int twl4030_madc_battery_probe(struct platform_device *pdev) if (!twl4030_madc_bat) return -ENOMEM; + if (!pdata) + pdata = twl4030_madc_dt_probe(pdev); + twl4030_madc_bat->psy.name = "twl4030_battery"; twl4030_madc_bat->psy.type = POWER_SUPPLY_TYPE_BATTERY; twl4030_madc_bat->psy.properties = twl4030_madc_bat_props; @@ -263,6 +343,7 @@ static int twl4030_madc_battery_remove(struct platform_device *pdev) static struct platform_driver twl4030_madc_battery_driver = { .driver = { .name = "twl4030_madc_battery", + .of_match_table = of_match_ptr(of_twl4030_madc_match), }, .probe = twl4030_madc_battery_probe, .remove = twl4030_madc_battery_remove,