From patchwork Sun Aug 14 05:51:45 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 9278681 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 1379F600CB for ; Sun, 14 Aug 2016 10:07:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04D9428946 for ; Sun, 14 Aug 2016 10:07:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC5202894B; Sun, 14 Aug 2016 10:07:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F01928946 for ; Sun, 14 Aug 2016 10:07:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752895AbcHNKHM (ORCPT ); Sun, 14 Aug 2016 06:07:12 -0400 Received: from bh-25.webhostbox.net ([208.91.199.152]:38136 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752821AbcHNKHL (ORCPT ); Sun, 14 Aug 2016 06:07:11 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=roeck-us.net; s=default; h=References:In-Reply-To:Message-Id:Date:Subject: Cc:To:From; bh=sSul9tP7o2Q417n5FFPWXKqmtAXSUAy+PUvp2Tg9zo8=; b=Cw70KOdawnYSOj hcM/Xlo5Yadi9W860gvP61wEVlo4fwKCdXPz05KMUPjcBR3xjpie/xg9+r70J0n6nqAEXtQZTjx+K 72ez9IV+cK6MUf+ZRNAdyOlIRFXYQZimwp0GtwocZz7m7WrXAwOZVRmoyMSRcmmWhILUy4vuTVLt/ NDMxhcQOQHfAqUl557KjL4KZa6xWxOHfu7vaSXa3GoYIzJ95CGwVvRWpc458kcDpBXApR6/eyjfKd UgqkkNNwpbp9M/mnxzHzEMl5TLrKsFHzuyh4EP9TK+830TNSIzWn5EIpOB5z0IX/CsixVmTmY8I/t YA+oAN5Fqg7S5U4XFnXw==; Received: from 108-223-40-66.lightspeed.sntcca.sbcglobal.net ([108.223.40.66]:34262 helo=localhost) by bh-25.webhostbox.net with esmtpa (Exim 4.86_1) (envelope-from ) id 1bYoKy-0011hA-QC; Sun, 14 Aug 2016 05:51:57 +0000 From: Guenter Roeck To: Hardware Monitoring Cc: Jean Delvare , Guenter Roeck Subject: [PATCH 3/9] hwmon: (tmp102) Convert to use new hwmon registration API Date: Sat, 13 Aug 2016 22:51:45 -0700 Message-Id: <1471153911-6536-3-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1471153911-6536-1-git-send-email-linux@roeck-us.net> References: <1471153911-6536-1-git-send-email-linux@roeck-us.net> X-Authenticated_sender: guenter@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: guenter@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: guenter@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Simplify code and reduce code size by using the new hwmon registration API. Signed-off-by: Guenter Roeck --- drivers/hwmon/Kconfig | 1 - drivers/hwmon/tmp102.c | 152 ++++++++++++++++++++++++++++--------------------- 2 files changed, 88 insertions(+), 65 deletions(-) diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig index fcdaf6773238..6c82ee240c4c 100644 --- a/drivers/hwmon/Kconfig +++ b/drivers/hwmon/Kconfig @@ -1571,7 +1571,6 @@ config SENSORS_THMC50 config SENSORS_TMP102 tristate "Texas Instruments TMP102" depends on I2C - depends on THERMAL || !THERMAL_OF select REGMAP_I2C help If you say yes here you get support for Texas Instruments TMP102 diff --git a/drivers/hwmon/tmp102.c b/drivers/hwmon/tmp102.c index 8479ac5eb853..0874d77acec6 100644 --- a/drivers/hwmon/tmp102.c +++ b/drivers/hwmon/tmp102.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #define DRIVER_NAME "tmp102" @@ -79,84 +78,111 @@ static inline u16 tmp102_mC_to_reg(int val) return (val * 128) / 1000; } -static int tmp102_read_temp(void *dev, int *temp) +static int tmp102_read(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long *temp) { struct tmp102 *tmp102 = dev_get_drvdata(dev); - unsigned int reg; - int ret; - - if (time_before(jiffies, tmp102->ready_time)) { - dev_dbg(dev, "%s: Conversion not ready yet..\n", __func__); - return -EAGAIN; + unsigned int regval; + int err, reg; + + switch (attr) { + case hwmon_temp_input: + /* Is it too early to return a conversion ? */ + if (time_before(jiffies, tmp102->ready_time)) + return -EAGAIN; + reg = TMP102_TEMP_REG; + break; + case hwmon_temp_max_hyst: + reg = TMP102_TLOW_REG; + break; + case hwmon_temp_max: + reg = TMP102_THIGH_REG; + break; + default: + return -EOPNOTSUPP; } - ret = regmap_read(tmp102->regmap, TMP102_TEMP_REG, ®); - if (ret < 0) - return ret; - - *temp = tmp102_reg_to_mC(reg); + err = regmap_read(tmp102->regmap, reg, ®val); + if (err < 0) + return err; + *temp = tmp102_reg_to_mC(regval); return 0; } -static ssize_t tmp102_show_temp(struct device *dev, - struct device_attribute *attr, - char *buf) +static int tmp102_write(struct device *dev, enum hwmon_sensor_types type, + u32 attr, int channel, long temp) { - struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); struct tmp102 *tmp102 = dev_get_drvdata(dev); - int regaddr = sda->index; - unsigned int reg; - int err; - - if (regaddr == TMP102_TEMP_REG && - time_before(jiffies, tmp102->ready_time)) - return -EAGAIN; - - err = regmap_read(tmp102->regmap, regaddr, ®); - if (err < 0) - return err; + int reg; + + switch (attr) { + case hwmon_temp_max_hyst: + reg = TMP102_TLOW_REG; + break; + case hwmon_temp_max: + reg = TMP102_THIGH_REG; + break; + default: + return -EOPNOTSUPP; + } - return sprintf(buf, "%d\n", tmp102_reg_to_mC(reg)); + temp = clamp_val(temp, -256000, 255000); + return regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(temp)); } -static ssize_t tmp102_set_temp(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) +static umode_t tmp102_is_visible(const void *data, enum hwmon_sensor_types type, + u32 attr, int channel) { - struct sensor_device_attribute *sda = to_sensor_dev_attr(attr); - struct tmp102 *tmp102 = dev_get_drvdata(dev); - int reg = sda->index; - long val; - int err; - - if (kstrtol(buf, 10, &val) < 0) - return -EINVAL; - val = clamp_val(val, -256000, 255000); - - err = regmap_write(tmp102->regmap, reg, tmp102_mC_to_reg(val)); - return err ? : count; + if (type != hwmon_temp) + return 0; + + switch (attr) { + case hwmon_temp_input: + return S_IRUGO; + case hwmon_temp_max_hyst: + case hwmon_temp_max: + return S_IRUGO | S_IWUSR; + default: + return 0; + } } -static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, tmp102_show_temp, NULL, - TMP102_TEMP_REG); +static u32 tmp102_chip_config[] = { + HWMON_C_REGISTER_TZ, + 0 +}; + +static const struct hwmon_channel_info tmp102_chip = { + .type = hwmon_chip, + .config = tmp102_chip_config, +}; -static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO, tmp102_show_temp, - tmp102_set_temp, TMP102_TLOW_REG); +static u32 tmp102_temp_config[] = { + HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST, + 0 +}; -static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, tmp102_show_temp, - tmp102_set_temp, TMP102_THIGH_REG); +static const struct hwmon_channel_info tmp102_temp = { + .type = hwmon_temp, + .config = tmp102_temp_config, +}; -static struct attribute *tmp102_attrs[] = { - &sensor_dev_attr_temp1_input.dev_attr.attr, - &sensor_dev_attr_temp1_max_hyst.dev_attr.attr, - &sensor_dev_attr_temp1_max.dev_attr.attr, +static const struct hwmon_channel_info *tmp102_info[] = { + &tmp102_chip, + &tmp102_temp, NULL }; -ATTRIBUTE_GROUPS(tmp102); -static const struct thermal_zone_of_device_ops tmp102_of_thermal_ops = { - .get_temp = tmp102_read_temp, +static const struct hwmon_ops tmp102_hwmon_ops = { + .is_visible = tmp102_is_visible, + .read = tmp102_read, + .write = tmp102_write, +}; + +static const struct hwmon_chip_info tmp102_chip_info = { + .ops = &tmp102_hwmon_ops, + .info = tmp102_info, }; static void tmp102_restore_config(void *data) @@ -188,7 +214,7 @@ static const struct regmap_config tmp102_regmap_config = { }; static int tmp102_probe(struct i2c_client *client, - const struct i2c_device_id *id) + const struct i2c_device_id *id) { struct device *dev = &client->dev; struct device *hwmon_dev; @@ -249,16 +275,14 @@ static int tmp102_probe(struct i2c_client *client, tmp102->ready_time += msecs_to_jiffies(CONVERSION_TIME_MS); } - hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, - tmp102, - tmp102_groups); + hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name, + tmp102, + &tmp102_chip_info, + NULL); if (IS_ERR(hwmon_dev)) { dev_dbg(dev, "unable to register hwmon device\n"); return PTR_ERR(hwmon_dev); } - devm_thermal_zone_of_sensor_register(hwmon_dev, 0, hwmon_dev, - &tmp102_of_thermal_ops); - dev_info(dev, "initialized\n"); return 0;