Received: by 10.223.164.202 with SMTP id h10csp80330wrb; Tue, 7 Nov 2017 03:15:10 -0800 (PST) X-Google-Smtp-Source: ABhQp+QspcErgL4AuwsbNRbcm8CI1BkhTJPOFlrga3+HFrFxuMphrHRM5638Eqi0y7BWigmqtIqz X-Received: by 10.98.209.8 with SMTP id z8mr20066187pfg.184.1510053310756; Tue, 07 Nov 2017 03:15:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510053310; cv=none; d=google.com; s=arc-20160816; b=cQw5vSQkG6I6CDZ85y3DuabatNiOT5IJO0Fl6xUtwSllnIX2LU7Db8lYaWvNkgSHFU nnoF/IWUfBFGFUuOC809bVuZQASKyrPpq572IrysuAR/dQZypXjduCM2sL6K3tdE2KBp LFa8zu5VOkr51ttHKCZJr7hv7mWjt9gomN1pXGsVVSmvjLisAvxsFJy4g916IOnrxnA5 qu4dljDxA2yHm6Z7thL8PGVfkWh/SMBuTqS14lmPAp8fs+n6bEDGAr2TBBfsaJo4RKr8 7wKHUdDRxzrqA8tENkQ/OBmGlleJM+lVKCxu1cRvWMSCGuDXHAOHKiU4zcgBg6RiuwzZ 1jUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=okA/06G66ZlLHrx25suEIRkfCKhcC6iXXqOQnA2bS94=; b=z9xiW14VOWj3pp0cCws+1si2a4MlkjYYQaewQcbQg8d5XpCwTmB12y0zWaQTqG9RNW 5qyq9FoQkWqC17ZHtzh71Us34zPdtGdmIoJf99m3QvFCGLiJrhzO4jArpMG4Z+nE2n1D gFRrTsPoM4alo998JRIfixm9jH8LImfpdYQ4nYEMB13JJdlL9E8YA8Fd+3hioDhm/XQB kX/hDzrmNQIIvwVLoUozkcvItpfF9sRVtMQt3Qs0MDdOa9ZKKVFuBBLcRZZkOHEFpcxA OmTGMqzvE3zzSP40hYDPVmRWl3BLB7qT2Lus/rWG5/Pi+CQ7N4WUo1oPg4J37SKQT7Xx kUBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WX9jDvPK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d20si963619plr.763.2017.11.07.03.14.57; Tue, 07 Nov 2017 03:15:10 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=WX9jDvPK; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933384AbdKGI2t (ORCPT + 91 others); Tue, 7 Nov 2017 03:28:49 -0500 Received: from mail-pg0-f68.google.com ([74.125.83.68]:47612 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933039AbdKGI2o (ORCPT ); Tue, 7 Nov 2017 03:28:44 -0500 Received: by mail-pg0-f68.google.com with SMTP id o7so1618958pgc.4; Tue, 07 Nov 2017 00:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=okA/06G66ZlLHrx25suEIRkfCKhcC6iXXqOQnA2bS94=; b=WX9jDvPKcbpIyrptPKna9wkP9aeB0zhRS8FCXFt6FsBbhOz+OHpihk3IIYTEBg+fZZ thELJzcpdjU9ouT2S3AIumGNsvxu6orh2YgtCC0EQvt9yo+vHn42qWSiCjYcerrECwaS R8TUGJTltgURxIv3vXDYMfacjVlCpSjYYyvTtyg66q80gT6lUU/1NCLKu4KlO3x16XIz FKjUUZUGRjE4+BbEuynojgv9Jz4W/cDo8e+XaX/HXVJ2MqvmecDJDtET4fDutMXtteU+ IbCqoVom4GJVeMO93hW48v1A4DIvKI+b1N4hJWCv421VUsJP6D7qyuAQR/jc/rG+IS57 E9Ew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=okA/06G66ZlLHrx25suEIRkfCKhcC6iXXqOQnA2bS94=; b=szPEqPn6KyKuA35wf+t2n+WSUt21FZDy05hn2WFtBhk2k5PPR3MUk/o5ViyXHYa3ch olO9z/cQDu02vCK5aqa06d+ng6BItGoesa7IszHHBcZkuPZpGC+B81mxWRWcIYgo8xIh xDumchH+QkAGhCvN8C3MKDXQYEO0tAsqqqaFHZ3s1KV/kwUiFkwn2KJn6v6YvYxxmazE 8KCHbfCj3YhiYCI9/dgsaH434DeUICKnaASaC1z7rBgDLRF32SEUSa25aApu/xcbRsI1 JsBfmMFIWJPLDvAejpx8sLjIe6bu9Db1eC15TQTT6eA5otnLsWPFv1ofIJA9cd8Uyp6s 8vUw== X-Gm-Message-State: AMCzsaVu/LP6Z7xYgbNOecYPOOCbbmMtkI6EQyOxe92HCnOvPTlke+SC Xb5bxZe7Xf7f2b65WAGfUiM= X-Received: by 10.84.214.136 with SMTP id j8mr18001519pli.422.1510043323764; Tue, 07 Nov 2017 00:28:43 -0800 (PST) Received: from localhost ([159.203.222.251]) by smtp.gmail.com with ESMTPSA id r22sm2192099pfl.15.2017.11.07.00.28.41 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Tue, 07 Nov 2017 00:28:43 -0800 (PST) From: Lei YU To: Mark Rutland , Jean Delvare , Guenter Roeck , Jonathan Corbet , Jiri Kosina Cc: Lei YU , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hwmon@vger.kernel.org, Joel Stanley , Andrew Jeffery Subject: [PATCH v2 2/3] drivers: hwmon: Add W83773G driver Date: Tue, 7 Nov 2017 16:27:53 +0800 Message-Id: <1510043274-4778-3-git-send-email-mine260309@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1510043274-4778-1-git-send-email-mine260309@gmail.com> References: <1510043274-4778-1-git-send-email-mine260309@gmail.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Nuvoton W83773G is a hardware monitor IC providing one local temperature and two remote temperature sensors. Signed-off-by: Lei YU --- v2: - Rewrite the driver using regmap - Add offset and update_interval --- drivers/hwmon/Kconfig | 10 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/w83773g.c | 281 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 drivers/hwmon/w83773g.c diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index d654314..11c6248 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1710,6 +1710,16 @@ config SENSORS_VT8231 This driver can also be built as a module. If so, the module will be called vt8231. +config SENSORS_W83773G + tristate "Nuvoton W83773G" + depends on I2C + help + If you say yes here you get support for the Nuvoton W83773G hardware + monitoring chip. + + This driver can also be built as a module. If so, the module + will be called w83773g. + config SENSORS_W83781D tristate "Winbond W83781D, W83782D, W83783S, Asus AS99127F" depends on I2C diff --git a/drivers/hwmon/Makefile b/drivers/hwmon/Makefile index c84d978..0649ad8 100644 --- a/drivers/hwmon/Makefile +++ b/drivers/hwmon/Makefile @@ -13,6 +13,7 @@ obj-$(CONFIG_SENSORS_ATK0110) += asus_atk0110.o # asb100, then w83781d go first, as they can override other drivers' addresses. obj-$(CONFIG_SENSORS_ASB100) += asb100.o obj-$(CONFIG_SENSORS_W83627HF) += w83627hf.o +obj-$(CONFIG_SENSORS_W83773G) += w83773g.o obj-$(CONFIG_SENSORS_W83792D) += w83792d.o obj-$(CONFIG_SENSORS_W83793) += w83793.o obj-$(CONFIG_SENSORS_W83795) += w83795.o diff --git a/drivers/hwmon/w83773g.c b/drivers/hwmon/w83773g.c new file mode 100644 index 0000000..d352bac --- /dev/null +++ b/drivers/hwmon/w83773g.c @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2017 IBM Corp. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Driver for the Nuvoton W83773G SMBus temperature sensor IC. + * Supported models: W83773G + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Addresses to scan */ +static const unsigned short normal_i2c[] = { 0x4c, 0x4d, I2C_CLIENT_END }; + +/* The W83773 registers */ +#define W83773_CONVERSION_RATE_REG_READ 0x04 +#define W83773_CONVERSION_RATE_REG_WRITE 0x0A +#define W83773_MANUFACTURER_ID_REG 0xFE +#define W83773_LOCAL_TEMP 0x00 + +static const u8 W83773_STATUS[2] = { 0x02, 0x17 }; + +static const u8 W83773_TEMP_LSB[2] = { 0x10, 0x25 }; +static const u8 W83773_TEMP_MSB[2] = { 0x01, 0x24 }; + +static const u8 W83773_OFFSET_LSB[2] = { 0x12, 0x16 }; +static const u8 W83773_OFFSET_MSB[2] = { 0x11, 0x15 }; + + +/* this is the number of sensors in the device */ +static const struct i2c_device_id w83773_id[] = { + { "w83773g" }, + { } +}; + +MODULE_DEVICE_TABLE(i2c, w83773_id); + +static const struct of_device_id w83773_of_match[] = { + { + .compatible = "nuvoton,w83773g" + }, + { }, +}; +MODULE_DEVICE_TABLE(of, w83773_of_match); + +static inline int temp_of_local(s8 reg) +{ + return reg * 1000; +} + +static inline int temp_of_remote(s8 hb, u8 lb) +{ + return (hb << 3 | lb >> 5) * 125; +} + +static ssize_t show_local_temp(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); + struct regmap *regmap = dev_get_drvdata(dev); + unsigned int regval; + int ret; + + ret = regmap_read(regmap, sda->index, ®val); + if (ret < 0) + return ret; + + return sprintf(buf, "%d\n", temp_of_local(regval)); +} + +static ssize_t show_remote_temp(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); + struct regmap *regmap = dev_get_drvdata(dev); + unsigned int regval_high; + unsigned int regval_low; + int ret; + + ret = regmap_read(regmap, W83773_TEMP_MSB[sda->index], ®val_high); + ret |= regmap_read(regmap, W83773_TEMP_LSB[sda->index], ®val_low); + if (ret < 0) + return ret; + + return sprintf(buf, "%d\n", temp_of_remote(regval_high, regval_low)); +} + +static ssize_t show_status(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); + struct regmap *regmap = dev_get_drvdata(dev); + unsigned int regval; + int ret; + + ret = regmap_read(regmap, W83773_STATUS[sda->index], ®val); + + if (ret < 0) + return ret; + + return sprintf(buf, "%d\n", (u8)regval & 0x04 >> 2); +} + +static ssize_t show_offset(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); + struct regmap *regmap = dev_get_drvdata(dev); + unsigned int regval_high; + unsigned int regval_low; + int ret; + + ret = regmap_read(regmap, W83773_OFFSET_MSB[sda->index], ®val_high); + ret |= regmap_read(regmap, W83773_OFFSET_LSB[sda->index], ®val_low); + if (ret < 0) + return ret; + + return sprintf(buf, "%d\n", temp_of_remote(regval_high, regval_low)); +} + +static ssize_t set_offset(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); + struct regmap *regmap = dev_get_drvdata(dev); + long val; + int ret; + int err; + u8 high_byte; + u8 low_byte; + + err = kstrtol(buf, 10, &val); + if (err) + return err; + + val = clamp_val(val, -127825, 127825); + /* offset value equals to (high_byte << 3 | low_byte >> 5) * 125 */ + val /= 125; + high_byte = val >> 3; + low_byte = (val & 0x07) << 5; + + ret = regmap_write(regmap, W83773_OFFSET_MSB[sda->index], high_byte); + ret |= regmap_write(regmap, W83773_OFFSET_LSB[sda->index], low_byte); + + if (ret < 0) + return ret; + return count; +} + +static ssize_t show_update_interval(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct regmap *regmap = dev_get_drvdata(dev); + unsigned int regval; + int ret; + + ret = regmap_read(regmap, W83773_CONVERSION_RATE_REG_READ, ®val); + if (ret < 0) + return ret; + + return sprintf(buf, "%u\n", 16000 >> regval); +} + +static ssize_t set_update_interval(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct regmap *regmap = dev_get_drvdata(dev); + unsigned long val; + int ret; + int err, rate; + + err = kstrtoul(buf, 10, &val); + if (err) + return err; + + /* + * For valid rates, interval can be calculated as + * interval = (1 << (8 - rate)) * 62.5; + * Rounded rate is therefore + * rate = 8 - __fls(interval * 8 / (62.5 * 7)); + * Use clamp_val() to avoid overflows, and to ensure valid input + * for __fls. + */ + val = clamp_val(val, 62, 16000) * 10; + rate = 8 - __fls((val * 8 / (625 * 7))); + ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, rate); + if (ret < 0) + return ret; + return count; +} + +static SENSOR_DEVICE_ATTR(temp1_input, 0444, show_local_temp, NULL, + W83773_LOCAL_TEMP); +static SENSOR_DEVICE_ATTR(temp2_input, 0444, show_remote_temp, NULL, 0); +static SENSOR_DEVICE_ATTR(temp2_fault, 0444, show_status, NULL, 0); +static SENSOR_DEVICE_ATTR(temp2_offset, 0644, show_offset, set_offset, 0); +static SENSOR_DEVICE_ATTR(temp3_input, 0444, show_remote_temp, NULL, 1); +static SENSOR_DEVICE_ATTR(temp3_fault, 0444, show_status, NULL, 1); +static SENSOR_DEVICE_ATTR(temp3_offset, 0644, show_offset, set_offset, 1); +static DEVICE_ATTR(update_interval, 0644, show_update_interval, + set_update_interval); + +static struct attribute *w83773g_attrs[] = { + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_temp2_fault.dev_attr.attr, + &sensor_dev_attr_temp2_offset.dev_attr.attr, + &sensor_dev_attr_temp3_input.dev_attr.attr, + &sensor_dev_attr_temp3_fault.dev_attr.attr, + &sensor_dev_attr_temp3_offset.dev_attr.attr, + &dev_attr_update_interval.attr, + NULL +}; +ATTRIBUTE_GROUPS(w83773g); + +static const struct regmap_config w83773g_regmap_config = { + .reg_bits = 8, + .val_bits = 8, +}; + +static int w83773_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + + struct device *dev = &client->dev; + struct device *hwmon_dev; + struct regmap *regmap; + int ret; + + regmap = devm_regmap_init_i2c(client, &w83773g_regmap_config); + if (IS_ERR(regmap)) { + dev_err(dev, "failed to allocate register map\n"); + return PTR_ERR(regmap); + } + + /* Set the conversion rate to 2 Hz */ + ret = regmap_write(regmap, W83773_CONVERSION_RATE_REG_WRITE, 0x05); + if (ret < 0) { + dev_err(&client->dev, "error writing config rate register\n"); + return ret; + } + + i2c_set_clientdata(client, regmap); + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, + regmap, w83773g_groups); + return PTR_ERR_OR_ZERO(hwmon_dev); +} + +static struct i2c_driver w83773_driver = { + .class = I2C_CLASS_HWMON, + .driver = { + .name = "w83773g", + .of_match_table = of_match_ptr(w83773_of_match), + }, + .probe = w83773_probe, + .id_table = w83773_id, + .address_list = normal_i2c, +}; + +module_i2c_driver(w83773_driver); + +MODULE_AUTHOR("Lei YU "); +MODULE_DESCRIPTION("W83773G temperature sensor driver"); +MODULE_LICENSE("GPL"); -- 1.9.1 From 1583574482190922813@xxx Thu Nov 09 07:58:31 +0000 2017 X-GM-THRID: 1583574482190922813 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread