From patchwork Sun Jan 24 15:22:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 572318 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-qk0-x23f.google.com (mail-qk0-x23f.google.com [IPv6:2607:f8b0:400d:c09::23f]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 1E68D140BF6 for ; Mon, 25 Jan 2016 02:22:32 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b=bangjHs+; dkim-atps=neutral Received: by mail-qk0-x23f.google.com with SMTP id e124sf34306760qkc.0 for ; Sun, 24 Jan 2016 07:22:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20120806; h=mime-version:from:to:cc:subject:date:message-id:x-original-sender :x-original-authentication-results:reply-to:content-type:precedence :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help :list-archive:sender:list-subscribe:list-unsubscribe; bh=m1F5V5AIySLtw0uNsWMcGywA3pRm525NnHnPLP5RdeU=; b=bangjHs+seG+B9uRN3h3+hHOOK1nr/DQo0+0uZDEEuPjFg8mxJZA+wejfYY2/yX1d+ j4JpDhtIRHMw9l5QId0Uml+7Ytxp/07UrfkXj1mTVgwIxeSKp1pwcOtYcMghsgsv/vPA Y5ppGmrpmeX+LB4jzZ0JoZ3hOykQ7GYHIyeedbYxgPRi49yanJDy/1ABoOfXrC3fqE78 jCgyZ1MzjUBYCbCNr/WixAb5ixYjb98IVixyiFXqJt3w4vLbtMzXzWGx9KlHB0AF85Yu ebazmh4sMs1JQSkmN5XCLGKRWOCecPDx5TSsvRH1oTMszllhIKxFYAJ9RZk3C/9D7/UI Vo1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:to:cc:subject:date:message-id :x-original-sender:x-original-authentication-results:reply-to :content-type:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive:sender :list-subscribe:list-unsubscribe; bh=m1F5V5AIySLtw0uNsWMcGywA3pRm525NnHnPLP5RdeU=; b=GK3d2y3YIsIUCKOGlrMaSzScJO2UMGbRoEOZkM+85Fy5AYX442SUsSCjQPhmpULzlg Sv2/NQbNIJkXQKZ283pHUjzxfR3PX2rf9ODoL87IU51p8KviXNkqB9fT6MZ9AxSd0Nbk cREcu5H0nXvtEC9in1qLdBOEjkgiiYak2z4X4ycxA1whGD4LwEF0ZEl0OTv/sswp5XDj rYVavGhpLMSEAFTprZ7+0yI5slqubSpySbg0uPWxH6qqPazgrCJUSdAcFhQNvE9N9Tpj 34+JDUwxv+xp2OTGnvdFZqEjK00WY31DWtu2f7FmHzzYqNsIWprs6R54HtO68KpAkDFo XGPA== X-Gm-Message-State: AG10YORG8UTT4Sxjd0QMpgiaB12RWtZeBzF0iQ9ZMD1dt4y692ED9KyvvxngQJOJe5mU4Q== X-Received: by 10.140.92.213 with SMTP id b79mr240912qge.11.1453648950314; Sun, 24 Jan 2016 07:22:30 -0800 (PST) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.140.97.180 with SMTP id m49ls1469910qge.97.gmail; Sun, 24 Jan 2016 07:22:29 -0800 (PST) X-Received: by 10.31.160.209 with SMTP id j200mr12373479vke.6.1453648949142; Sun, 24 Jan 2016 07:22:29 -0800 (PST) Received: from mail-pf0-x241.google.com (mail-pf0-x241.google.com. [2607:f8b0:400e:c00::241]) by gmr-mx.google.com with ESMTPS id ui7si1547811pab.0.2016.01.24.07.22.29 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 24 Jan 2016 07:22:29 -0800 (PST) Received-SPF: pass (google.com: domain of akinobu.mita@gmail.com designates 2607:f8b0:400e:c00::241 as permitted sender) client-ip=2607:f8b0:400e:c00::241; Received: by mail-pf0-x241.google.com with SMTP id n128so5664941pfn.3 for ; Sun, 24 Jan 2016 07:22:29 -0800 (PST) X-Received: by 10.98.68.152 with SMTP id m24mr19094342pfi.78.1453648948972; Sun, 24 Jan 2016 07:22:28 -0800 (PST) Received: from localhost.localdomain (KD113159139091.ppp-bb.dion.ne.jp. [113.159.139.91]) by smtp.gmail.com with ESMTPSA id dg12sm22331170pac.47.2016.01.24.07.22.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 24 Jan 2016 07:22:27 -0800 (PST) From: Akinobu Mita To: rtc-linux@googlegroups.com, lm-sensors@lm-sensors.org Cc: Akinobu Mita , Alessandro Zummo , Alexandre Belloni , Jean Delvare , Guenter Roeck Subject: [rtc-linux] [PATCH v4] rtc: rtc-ds1307: add temperature sensor support for ds3231 Date: Mon, 25 Jan 2016 00:22:16 +0900 Message-Id: <1453648936-17017-1-git-send-email-akinobu.mita@gmail.com> X-Mailer: git-send-email 2.5.0 X-Original-Sender: Akinobu.Mita@gmail.com X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of akinobu.mita@gmail.com designates 2607:f8b0:400e:c00::241 as permitted sender) smtp.mailfrom=akinobu.mita@gmail.com; dmarc=pass (p=NONE dis=NONE) header.from=gmail.com Reply-To: rtc-linux@googlegroups.com Precedence: list Mailing-list: list rtc-linux@googlegroups.com; contact rtc-linux+owners@googlegroups.com List-ID: X-Spam-Checked-In-Group: rtc-linux@googlegroups.com X-Google-Group-Id: 712029733259 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , DS3231 has the temperature registers with a resolution of 0.25 degree celsius. This enables to get the value through hwmon. # cat /sys/class/i2c-adapter/i2c-2/2-0068/hwmon/hwmon0/temp1_input 21000 Signed-off-by: Akinobu Mita Acked-by: Guenter Roeck Cc: Alessandro Zummo Cc: Alexandre Belloni Cc: Jean Delvare Cc: Guenter Roeck --- * v4 (all changes are suggested by Guenter Roeck) - drop the Cc: mailing lists in commit log - add Acked-by line - remove unnecessary header include - s/s32/int/ - make ds1307_hwmon_register() a void function as the return value is not used drivers/rtc/Kconfig | 9 +++++ drivers/rtc/rtc-ds1307.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 2a52424..73a7e51 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -212,6 +212,15 @@ config RTC_DRV_DS1307 This driver can also be built as a module. If so, the module will be called rtc-ds1307. +config RTC_DRV_DS1307_HWMON + bool "HWMON support for rtc-ds1307" + depends on RTC_DRV_DS1307 && HWMON + depends on !(RTC_DRV_DS1307=y && HWMON=m) + default y + help + Say Y here if you want to expose temperature sensor data on + rtc-ds1307 (only DS3231) + config RTC_DRV_DS1374 tristate "Dallas/Maxim DS1374" help diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c index aa705bb..34d92a0 100644 --- a/drivers/rtc/rtc-ds1307.c +++ b/drivers/rtc/rtc-ds1307.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include /* * We can't determine type by probing, but if we expect pre-Linux code @@ -851,6 +853,90 @@ out: return; } +/*----------------------------------------------------------------------*/ + +#ifdef CONFIG_RTC_DRV_DS1307_HWMON + +/* + * Temperature sensor support for ds3231 devices. + */ + +#define DS3231_REG_TEMPERATURE 0x11 + +/* + * A user-initiated temperature conversion is not started by this function, + * so the temperature is updated once every 64 seconds. + */ +static int ds3231_hwmon_read_temp(struct device *dev, s16 *mC) +{ + struct ds1307 *ds1307 = dev_get_drvdata(dev); + u8 temp_buf[2]; + s16 temp; + int ret; + + ret = ds1307->read_block_data(ds1307->client, DS3231_REG_TEMPERATURE, + sizeof(temp_buf), temp_buf); + if (ret < 0) + return ret; + if (ret != sizeof(temp_buf)) + return -EIO; + + /* + * Temperature is represented as a 10-bit code with a resolution of + * 0.25 degree celsius and encoded in two's complement format. + */ + temp = (temp_buf[0] << 8) | temp_buf[1]; + temp >>= 6; + *mC = temp * 250; + + return 0; +} + +static ssize_t ds3231_hwmon_show_temp(struct device *dev, + struct device_attribute *attr, char *buf) +{ + int ret; + s16 temp; + + ret = ds3231_hwmon_read_temp(dev, &temp); + if (ret) + return ret; + + return sprintf(buf, "%d\n", temp); +} +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, ds3231_hwmon_show_temp, + NULL, 0); + +static struct attribute *ds3231_hwmon_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + NULL, +}; +ATTRIBUTE_GROUPS(ds3231_hwmon); + +static void ds1307_hwmon_register(struct ds1307 *ds1307) +{ + struct device *dev; + + if (ds1307->type != ds_3231) + return; + + dev = devm_hwmon_device_register_with_groups(&ds1307->client->dev, + ds1307->client->name, + ds1307, ds3231_hwmon_groups); + if (IS_ERR(dev)) { + dev_warn(&ds1307->client->dev, + "unable to register hwmon device %ld\n", PTR_ERR(dev)); + } +} + +#else + +static void ds1307_hwmon_register(struct ds1307 *ds1307) +{ +} + +#endif + static int ds1307_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -1191,6 +1277,8 @@ read_rtc: } } + ds1307_hwmon_register(ds1307); + return 0; exit: