From patchwork Thu Sep 11 10:43:55 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pavel Machek X-Patchwork-Id: 388197 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-la0-x23f.google.com (mail-la0-x23f.google.com [IPv6:2a00:1450:4010:c03::23f]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47B861400D5 for ; Thu, 11 Sep 2014 20:43:59 +1000 (EST) Received: by mail-la0-f63.google.com with SMTP id el20sf199810lab.8 for ; Thu, 11 Sep 2014 03:43:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=date:from:to:cc:subject:message-id:mime-version:user-agent :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:list-post:list-help:list-archive :sender:list-subscribe:list-unsubscribe:content-type :content-disposition; bh=eFr47DgAy3aIOk6uVCduJOkIyMWHrzJruOSTvTHhb1Y=; b=NU4PYGMB88aCmdU6NRMr9V3AMgqxCcQzGehKhufIF7KySJNxgywLTxfDxfTdeM2R+Z KhuZVw10icJ1erSCFdb/xTNKl/9zx7pSXt+ACZMfZwv3Pi4FSi7QYQ82CeeXtdcWTleV MtJqcNSJDd0CfKO0n9nrIhoYQB5IzGbTgi26jRzbIkY9f4oonblifwaaBv2TmpQlWY1F SNpTKQ9wH7SlstAH237PprhgB4B4Kc9cFIjVaj5q2H44yz8dBjNUaXPJ6Tfx/87gUKst D+KD++mVg8Dw44mjDfVN2yV2AIh5e6ihmxPmdGWBoiOmWOdKDO/SkjEikbIBU46dIiHt S5nQ== X-Received: by 10.180.74.233 with SMTP id x9mr43797wiv.1.1410432236391; Thu, 11 Sep 2014 03:43:56 -0700 (PDT) X-BeenThere: rtc-linux@googlegroups.com Received: by 10.180.189.144 with SMTP id gi16ls838218wic.40.canary; Thu, 11 Sep 2014 03:43:56 -0700 (PDT) X-Received: by 10.180.73.79 with SMTP id j15mr786003wiv.4.1410432236010; Thu, 11 Sep 2014 03:43:56 -0700 (PDT) Received: from atrey.karlin.mff.cuni.cz (atrey.karlin.mff.cuni.cz. [195.113.26.193]) by gmr-mx.google.com with ESMTP id z15si44238wiv.3.2014.09.11.03.43.55 for ; Thu, 11 Sep 2014 03:43:55 -0700 (PDT) Received-SPF: none (google.com: pavel@ucw.cz does not designate permitted sender hosts) client-ip=195.113.26.193; Received: by atrey.karlin.mff.cuni.cz (Postfix, from userid 512) id 972DF81BCB; Thu, 11 Sep 2014 12:43:55 +0200 (CEST) Date: Thu, 11 Sep 2014 12:43:55 +0200 From: Pavel Machek To: kernel list , matti.vaittinen@nsn.com, robh+dt@kernel.org, akpm@linux-foundation.org, ijc+devicetree@hellion.org.uk, a.zummo@towertech.it, linux@roeck-us.net Cc: rtc-linux@googlegroups.com Subject: [rtc-linux] rtc: bq32000: add trickle charger option, with device tree binding Message-ID: <20140911104355.GA16588@amd> MIME-Version: 1.0 User-Agent: Mutt/1.5.23 (2014-03-12) X-Original-Sender: pavel@ucw.cz X-Original-Authentication-Results: gmr-mx.google.com; spf=neutral (google.com: pavel@ucw.cz does not designate permitted sender hosts) smtp.mail=pavel@ucw.cz Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Content-Disposition: inline BQ32000 devices have "trickle chargers". Introduce a code to enable the charger, based on device tree. Without charger, RTC does not keep time after power off. Signed-off-by: Pavel Machek diff --git a/drivers/rtc/rtc-bq32k.c b/drivers/rtc/rtc-bq32k.c index c74bf0d..4c24fb0 100644 --- a/drivers/rtc/rtc-bq32k.c +++ b/drivers/rtc/rtc-bq32k.c @@ -2,10 +2,14 @@ * Driver for TI BQ32000 RTC. * * Copyright (C) 2009 Semihalf. + * Copyright (C) 2014 Pavel Machek * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. + * + * You can get hardware description at + * http://www.ti.com/lit/ds/symlink/bq32000.pdf */ #include @@ -27,6 +31,10 @@ #define BQ32K_CENT 0x40 /* Century flag */ #define BQ32K_CENT_EN 0x80 /* Century flag enable bit */ +#define BQ32K_CALIBRATION 0x07 /* CAL_CFG1, calibration and control */ +#define BQ32K_TCH2 0x08 /* Trickle charge enable */ +#define BQ32K_CFG2 0x09 /* Trickle charger control */ + struct bq32k_regs { uint8_t seconds; uint8_t minutes; @@ -122,6 +130,59 @@ static const struct rtc_class_ops bq32k_rtc_ops = { .set_time = bq32k_rtc_set_time, }; +static int trickle_charger_of_init(struct device *dev, struct device_node *node) +{ + int plen = 0; + const uint32_t *setup; + const uint32_t *reg; + int error; + u32 ohms = 0; + + if (of_property_read_u32(node, "trickle-resistor-ohms" , &ohms)) + return 0; + + switch (ohms) { + case 180+940: + /* + * TCHE[3:0] == 0x05, TCH2 == 1, TCFE == 0 (charging + * over diode and 940ohm resistor) + */ + + if (of_property_read_bool(node, "trickle-diode-disable")) { + dev_err(dev, "diode and resistor mismatch\n"); + return -EINVAL; + } + reg = 0x05; + break; + + case 180+20000: + /* diode disabled */ + + if (!of_property_read_bool(node, "trickle-diode-disable")) { + dev_err(dev, "bq32k: diode and resistor mismatch\n"); + return -EINVAL; + } + reg = 0x25; + break; + + default: + dev_err(dev, "invalid resistor value (%d)\n", *setup); + return -EINVAL; + } + + error = bq32k_write(dev, ®, BQ32K_CFG2, 1); + if (error) + return error; + + reg = 0x20; + error = bq32k_write(dev, ®, BQ32K_TCH2, 1); + if (error) + return error; + + dev_info(dev, "Enabled trickle RTC battery charge.\n"); + return 0; +} + static int bq32k_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -153,6 +214,10 @@ static int bq32k_probe(struct i2c_client *client, if (error) return error; + if (client && client->dev.of_node) { + trickle_charger_of_init(dev, client->dev.of_node); + } + rtc = devm_rtc_device_register(&client->dev, bq32k_driver.driver.name, &bq32k_rtc_ops, THIS_MODULE); if (IS_ERR(rtc))