From patchwork Fri Jan 22 17:57:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akinobu Mita X-Patchwork-Id: 571763 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail-pf0-x23a.google.com (mail-pf0-x23a.google.com [IPv6:2607:f8b0:400e:c00::23a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id E5FE11402DD for ; Sat, 23 Jan 2016 04:57:41 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=googlegroups.com header.i=@googlegroups.com header.b=l1vUz0eE; dkim-atps=neutral Received: by mail-pf0-x23a.google.com with SMTP id q63sf24707052pfb.0 for ; Fri, 22 Jan 2016 09:57:41 -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=d0VkRh/8N97RZfyFEaFNHF3mQmddUE83a+bnzqAQzLY=; b=l1vUz0eE71JhwFbPaMsLO5tA+8XLKl/73sGG0iHaZcTAwtrrWz0A4AZl/twELaMK7R 1cCd6salxIjAZSmihaiLSBpnOWrB+/YOCCpolutySCxhiYtmu6/D7Tjk1rlPaMat/EQF cpbuQ1gwGqBQAGS597QkECrnfvm3twb4xvKiJFhV32VI2RiEk16I4hSAJkHfi9N+zKQw faRlG6e6QdoJOoTMUYb5EGPWkh1kwhr6GB3dNiRygrQXlVSLkZC5+x6mospk+87/Ny6x Sb9xgvaBd9qlM8dX5OJipNGdSzB7wVx7qdSjcAZXIIe2XoqV31ex4rl/b7PA8qFKIujn 4SUg== 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=d0VkRh/8N97RZfyFEaFNHF3mQmddUE83a+bnzqAQzLY=; b=k0nea3t0lw2o/mNMBDuR2EC67uLhHkfppxzJrXWqFi13btItgP6W76QTb5QbMh08Kz 7gFKfWd4JvXZlr8C/K8WhhcPRN3zzswTt0euka2dvIG/zzddVGTy3I5LqJUSRclwGTo6 LZcQ4szG9iQZODARL232jsBwNKX2vNC8DGPIJvYQ9COpTYxluiLRX6Ks9LhRXa4Ma2+/ 2wabzTaFqyt3bl5HvJiuWe3Yk95AcK7qd+aipYHlBaS3AlSwTsqDEXFWhnu2eLjRwwVe 7CCvRjnlohkGd2OUKI7fpfF/R1pqoqKImWMwbHY8XLvZx9evxlOfJbwf2O1rZJ6CiZIl iPKg== X-Gm-Message-State: AG10YOSl/wFuV3VBo5Fnz/ogOiV5Rg54slI0DrPqDq6Vs0/LQSoB8HMSKlz9Hsl+IDPItA== X-Received: by 10.140.92.213 with SMTP id b79mr122537qge.11.1453485459661; Fri, 22 Jan 2016 09:57:39 -0800 (PST) MIME-Version: 1.0 X-BeenThere: rtc-linux@googlegroups.com Received: by 10.140.43.247 with SMTP id e110ls905880qga.44.gmail; Fri, 22 Jan 2016 09:57:38 -0800 (PST) X-Received: by 10.129.80.137 with SMTP id e131mr3805475ywb.37.1453485458947; Fri, 22 Jan 2016 09:57:38 -0800 (PST) Received: from mail-pa0-x244.google.com (mail-pa0-x244.google.com. [2607:f8b0:400e:c03::244]) by gmr-mx.google.com with ESMTPS id i25si789381pfj.2.2016.01.22.09.57.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 22 Jan 2016 09:57:38 -0800 (PST) Received-SPF: pass (google.com: domain of akinobu.mita@gmail.com designates 2607:f8b0:400e:c03::244 as permitted sender) client-ip=2607:f8b0:400e:c03::244; Received: by mail-pa0-x244.google.com with SMTP id gi1so3528596pac.2 for ; Fri, 22 Jan 2016 09:57:38 -0800 (PST) X-Received: by 10.66.121.136 with SMTP id lk8mr6281828pab.1.1453485458794; Fri, 22 Jan 2016 09:57:38 -0800 (PST) Received: from localhost.localdomain (KD113159139091.ppp-bb.dion.ne.jp. [113.159.139.91]) by smtp.gmail.com with ESMTPSA id p66sm11173476pfi.34.2016.01.22.09.57.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 22 Jan 2016 09:57:37 -0800 (PST) From: Akinobu Mita To: rtc-linux@googlegroups.com Cc: Akinobu Mita , Alessandro Zummo , Alexandre Belloni , Jean Delvare , Guenter Roeck , lm-sensors@lm-sensors.org Subject: [rtc-linux] [PATCH v2] rtc: rtc-ds1307: add temperature sensor support for ds3231 Date: Sat, 23 Jan 2016 02:57:21 +0900 Message-Id: <1453485441-11667-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:c03::244 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 Cc: Alessandro Zummo Cc: Alexandre Belloni Cc: rtc-linux@googlegroups.com Cc: Jean Delvare Cc: Guenter Roeck Cc: lm-sensors@lm-sensors.org --- * v2 - convert to use hwmon framework, suggested by Alexandre Belloni drivers/rtc/rtc-ds1307.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c index aa705bb..e0d4ce5 100644 --- a/drivers/rtc/rtc-ds1307.c +++ b/drivers/rtc/rtc-ds1307.c @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include /* * We can't determine type by probing, but if we expect pre-Linux code @@ -75,6 +78,7 @@ enum ds_type { # define DS1337_BIT_nEOSC 0x80 # define DS1339_BIT_BBSQI 0x20 # define DS3231_BIT_BBSQW 0x40 /* same as BBSQI */ +# define DS3231_BIT_CONV 0x20 # define DS1337_BIT_RS2 0x10 # define DS1337_BIT_RS1 0x08 # define DS1337_BIT_INTCN 0x04 @@ -89,6 +93,7 @@ enum ds_type { # define DS1340_BIT_OSF 0x80 #define DS1337_REG_STATUS 0x0f # define DS1337_BIT_OSF 0x80 +# define DS3231_BIT_BSY 0x04 # define DS1337_BIT_A2I 0x02 # define DS1337_BIT_A1I 0x01 #define DS1339_REG_ALARM1_SECS 0x07 @@ -851,6 +856,121 @@ out: return; } +/*----------------------------------------------------------------------*/ + +#if IS_ENABLED(CONFIG_HWMON) + +/* + * Temperature sensor support for ds3231 devices. + */ + +#define DS3231_REG_TEMPERATURE 0x11 + +static ssize_t ds3231_hwmon_show_temp(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct ds1307 *ds1307 = dev_get_drvdata(dev); + struct i2c_client *client = ds1307->client; + u8 temp_buf[2]; + s16 temp; + int control; + int status; + s32 ret; + unsigned long timeout; + + status = i2c_smbus_read_byte_data(client, DS1337_REG_STATUS); + if (status < 0) + return status; + + /* + * Start user-initiated temperature conversion + */ + if (!(status & DS3231_BIT_BSY)) { + struct mutex *lock = &ds1307->rtc->ops_lock; + + mutex_lock(lock); + + control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); + if (control < 0) { + mutex_unlock(lock); + return control; + } + ret = i2c_smbus_write_byte_data(client, DS1337_REG_CONTROL, + control | DS3231_BIT_CONV); + if (ret) + return ret; + + mutex_unlock(lock); + + /* + * A user-initiated temperature conversion does not affect + * the BSY bit for approximately 2ms. + */ + usleep_range(2000, 3000); + } + + /* + * Wait until the conversion is finished + */ + timeout = jiffies + HZ; + + do { + control = i2c_smbus_read_byte_data(client, DS1337_REG_CONTROL); + if (control < 0) + return control; + if (!(control & DS3231_BIT_CONV)) + break; + if (time_after(jiffies, timeout)) + return -EIO; + usleep_range(2000, 3000); + } while (1); + + 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; + + return sprintf(buf, "%d\n", temp * 250); +} +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 int ds1307_hwmon_register(struct ds1307 *ds1307) +{ + if (ds1307->type != ds_3231) + return 0; + + devm_hwmon_device_register_with_groups(&ds1307->client->dev, + ds1307->client->name, + ds1307, ds3231_hwmon_groups); + + return 0; +} + +#else + +static int ds1307_hwmon_register(struct ds1307 *ds1307) +{ + return 0; +} + +#endif + static int ds1307_probe(struct i2c_client *client, const struct i2c_device_id *id) { @@ -1191,6 +1311,8 @@ read_rtc: } } + ds1307_hwmon_register(ds1307); + return 0; exit: