From patchwork Mon Sep 23 12:17:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11156665 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3F48E17D4 for ; Mon, 23 Sep 2019 12:17:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1E0E4214DA for ; Mon, 23 Sep 2019 12:17:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YlLIxetR" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732114AbfIWMRu (ORCPT ); Mon, 23 Sep 2019 08:17:50 -0400 Received: from mail-wr1-f65.google.com ([209.85.221.65]:34537 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732154AbfIWMRu (ORCPT ); Mon, 23 Sep 2019 08:17:50 -0400 Received: by mail-wr1-f65.google.com with SMTP id a11so13734893wrx.1 for ; Mon, 23 Sep 2019 05:17:48 -0700 (PDT) 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=tuV3QwoICkxsxXj6MRCa5YPaGE02cRujJRq5EjeTc48=; b=YlLIxetRqPNhHoOE5I9RCGCWOtjZ+cKWOL+PJWEQtF+eN+FadhWpBsZTIb6/lyvfSw f7jpawcytfDP+47riAdSKBpbqRysMlUvLdNAM8qHzWAtODdSw1LUepuk1ohQVthJQ9x0 t6Ws6oRerRSjp+JhJWjgK8eGEo1FdouM3cZr2ynS4CJJ9NbDnwcsB2tEtoXeStPlypug 2VIZ4dOo8g7t71Pm7TgUhygPVzRGH8rv0l1UNnU5M7gSjCnnLF9ALf4UGtJqRDrWz5vw t81PgE+GelISzO7se72YTwU4WmudItIY+Moib2FOjIH3wwwqe2eS7AMIOHcOt98DcrAQ U9Jw== 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=tuV3QwoICkxsxXj6MRCa5YPaGE02cRujJRq5EjeTc48=; b=Q4wQgdcfwM73lpWTDsNdVknHCytKOeSwHkvQl6xelmy5gW8Bs5RKgr0NyxqhvjwOhq 28eaO4z2hYg1Nkb2Zk6YTwqY80VjgdWWmGtBtf9RZ8opK55M2wtGPNcXaCAc111IsSnD SwS5GjJj82RMSYSeo+zaxHDeiTJ0nAiIBW2Qt1AJlMBHDiAPLPrOIxU9OGDCqqLunURo AkjNvs/vxDNs8dEssjyOeKm1L/G5nHBU/5mv2Cvq9+w34MGks11lCKb8f13BDauDeIR7 ZGIcwR7yJQX1SthoZ8pGKJBxg3zPRm1qPftc8Tugtz1FDmASay/v5YZeD1aRlnE7uSbQ 23wg== X-Gm-Message-State: APjAAAUhXpWEFAvmJmvOfhLZ0JM+oga4XtS/ooMAtPo1ih3TWsRelIZe n4TYhAHsEylPfuq0xG9ao4M= X-Google-Smtp-Source: APXvYqysgyKmGI6lMl+vW8RghRiq24OIIX5NDvdzEBFX1zBrsL5zbWEMPUV1VY/kQFevZJg9gZAsCw== X-Received: by 2002:adf:e390:: with SMTP id e16mr21570355wrm.29.1569241067423; Mon, 23 Sep 2019 05:17:47 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id a7sm11342836wra.43.2019.09.23.05.17.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 05:17:46 -0700 (PDT) From: Andrea Merello To: jic23@kernel.org, patrick.havelange@essensium.com, paresh.chaudhary@rockwellcollins.com, pmeerw@pmeerw.net, lars@metafoo.de, knaack.h@gmx.de, matthew.weber@rockwellcollins.com Cc: colin.king@canonical.com, linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 1/3] iio: max31856: add option for setting mains filter rejection frequency Date: Mon, 23 Sep 2019 14:17:12 +0200 Message-Id: <20190923121714.13672-2-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190923121714.13672-1-andrea.merello@gmail.com> References: <20190923121714.13672-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This sensor has an embedded notch filter for reducing interferences caused by the power mains. This filter can be tuned to reject either 50Hz or 60Hz (and harmonics). Currently the said setting is left alone (the sensor defaults to 60Hz). This patch introduces a IIO attribute that allows the user to set the said filter to the desired frequency. NOTE: this has been intentionally not tied to any DT property to allow the configuration of this setting from userspace, e.g. with a GUI or by reading a configuration file, or maybe reading a GPIO tied to a physical switch or accessing some device that can autodetect the line frequency. Signed-off-by: Andrea Merello --- drivers/iio/temperature/max31856.c | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/drivers/iio/temperature/max31856.c b/drivers/iio/temperature/max31856.c index 73ed550e3fc9..d12613f7ba3c 100644 --- a/drivers/iio/temperature/max31856.c +++ b/drivers/iio/temperature/max31856.c @@ -23,6 +23,7 @@ #define MAX31856_CR0_1SHOT BIT(6) #define MAX31856_CR0_OCFAULT BIT(4) #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4) +#define MAX31856_CR0_FILTER_50HZ BIT(0) #define MAX31856_TC_TYPE_MASK GENMASK(3, 0) #define MAX31856_FAULT_OVUV BIT(1) #define MAX31856_FAULT_OPEN BIT(0) @@ -63,6 +64,7 @@ static const struct iio_chan_spec max31856_channels[] = { struct max31856_data { struct spi_device *spi; u32 thermocouple_type; + bool filter_50hz; }; static int max31856_read(struct max31856_data *data, u8 reg, @@ -123,6 +125,11 @@ static int max31856_init(struct max31856_data *data) reg_cr0_val &= ~MAX31856_CR0_1SHOT; reg_cr0_val |= MAX31856_CR0_AUTOCONVERT; + if (data->filter_50hz) + reg_cr0_val |= MAX31856_CR0_FILTER_50HZ; + else + reg_cr0_val &= ~MAX31856_CR0_FILTER_50HZ; + return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); } @@ -249,12 +256,53 @@ static ssize_t show_fault_oc(struct device *dev, return show_fault(dev, MAX31856_FAULT_OPEN, buf); } +static ssize_t show_filter(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + + return sprintf(buf, "%d\n", data->filter_50hz ? 50 : 60); +} + +static ssize_t set_filter(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + unsigned int freq; + int ret; + + ret = kstrtouint(buf, 10, &freq); + if (ret) + return ret; + + switch (freq) { + case 50: + data->filter_50hz = true; + break; + case 60: + data->filter_50hz = false; + break; + default: + return -EINVAL; + } + + max31856_init(data); + return len; +} + static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0); static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0); +static IIO_DEVICE_ATTR(filter, 0644, show_filter, set_filter, 0); static struct attribute *max31856_attributes[] = { &iio_dev_attr_fault_ovuv.dev_attr.attr, &iio_dev_attr_fault_oc.dev_attr.attr, + &iio_dev_attr_filter.dev_attr.attr, NULL, }; @@ -280,6 +328,7 @@ static int max31856_probe(struct spi_device *spi) data = iio_priv(indio_dev); data->spi = spi; + data->filter_50hz = false; spi_set_drvdata(spi, indio_dev); From patchwork Mon Sep 23 12:17:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11156667 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 915D017EE for ; Mon, 23 Sep 2019 12:17:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 66551214DA for ; Mon, 23 Sep 2019 12:17:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="G/8H5BBk" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732154AbfIWMRv (ORCPT ); Mon, 23 Sep 2019 08:17:51 -0400 Received: from mail-wm1-f68.google.com ([209.85.128.68]:54851 "EHLO mail-wm1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726372AbfIWMRu (ORCPT ); Mon, 23 Sep 2019 08:17:50 -0400 Received: by mail-wm1-f68.google.com with SMTP id p7so9689924wmp.4 for ; Mon, 23 Sep 2019 05:17:49 -0700 (PDT) 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=vkw+yBgCh3Duibf7O+61ekGUivzVg0RWWljsVrDdd80=; b=G/8H5BBk0dWbBcjVRH3ufQGX+az+p5UDGYiLQ51AF5vQIEytxjQ/LGksSmMkg7b+KM KxoaCyO/XnLWlPns7IzKv0Uon8lfIOs2g87eKc+l89k0mqTiMDB2fFJpExnW9uW31xoN B6ev4g5FB09PwjDmxsdT25EArE4uoc3QiV+aacmqX/YHm15Bx9ADdKzKGpg52Sp3XEqi OIOnZqV3MOzsxViSGExt8aMj6GG13W9u1jzrvURzF+RSE97M1KV6nN+2IQQ8v/jj/p1+ aJfMm2YsGnzrcXKOBR+Xf3/f4046J0pEbnbj/CPCP9rXDk73AWvCuVHlvjKE+aeKN5Xc Vy7Q== 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=vkw+yBgCh3Duibf7O+61ekGUivzVg0RWWljsVrDdd80=; b=cQjl2fk2DcYbFAwzPupzDt9QPAyo7TQEkNSQb2vGiWaNh8KfdJSoRInHJxZk9eAY4a GC3P/Q2HjlFUrNCeox4cVfe0biMqZCXRumaJO8xQrpSdeGkaKMM3J6DU6qtlouTXc8oF gtp1mFVD0Q8YgeJ+tQiArZCeK+2vEZkoGRPL/FQ8FsQ7cyYiqsZJksu8FyaCDttR8xD0 7mW7149xetJQ36xSH8bGyp3U2eiNJiI32HAhksIZi/WVMSPCHqBpIsx7vVuzJMurVbh9 u2Q4eYnzf4MEnPo0mCDKq5fEn5nfZFH+1985WHRSFPx+61XtxBhhMJGUWA8Mle/QgQ61 gIUA== X-Gm-Message-State: APjAAAXIyfQg7voMaX9LCrjYqWFHfFnpVNySC+W8vPt+gbZyWM7dd1uc P+y+yJzVsKbj9tHCcJmmzcNhYN0R18s= X-Google-Smtp-Source: APXvYqwow9KEvVfcEZ4XYDBzvdS3E2/MtCCJDPRVJDLgYeef1J3jG0jLmZmC5UY0buWAPMYPvbolMg== X-Received: by 2002:a1c:4e01:: with SMTP id g1mr12452827wmh.134.1569241068492; Mon, 23 Sep 2019 05:17:48 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id a7sm11342836wra.43.2019.09.23.05.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 05:17:47 -0700 (PDT) From: Andrea Merello To: jic23@kernel.org, patrick.havelange@essensium.com, paresh.chaudhary@rockwellcollins.com, pmeerw@pmeerw.net, lars@metafoo.de, knaack.h@gmx.de, matthew.weber@rockwellcollins.com Cc: colin.king@canonical.com, linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 2/3] iio: max31856: add support for configuring the HW averaging Date: Mon, 23 Sep 2019 14:17:13 +0200 Message-Id: <20190923121714.13672-3-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190923121714.13672-1-andrea.merello@gmail.com> References: <20190923121714.13672-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This sensor can perform samples averaging in hardware, but currently the driver leaves this setting alone (default is no averaging). This patch introduces a new IIO attribute that allows the user to set the averaging as desired (the HW supports averaging of 2, 5, 8 or 16 samples) Signed-off-by: Andrea Merello --- drivers/iio/temperature/max31856.c | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/drivers/iio/temperature/max31856.c b/drivers/iio/temperature/max31856.c index d12613f7ba3c..8b2e0102fa5c 100644 --- a/drivers/iio/temperature/max31856.c +++ b/drivers/iio/temperature/max31856.c @@ -24,6 +24,8 @@ #define MAX31856_CR0_OCFAULT BIT(4) #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4) #define MAX31856_CR0_FILTER_50HZ BIT(0) +#define MAX31856_AVERAGING_MASK GENMASK(6, 4) +#define MAX31856_AVERAGING_SHIFT 4 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0) #define MAX31856_FAULT_OVUV BIT(1) #define MAX31856_FAULT_OPEN BIT(0) @@ -65,6 +67,7 @@ struct max31856_data { struct spi_device *spi; u32 thermocouple_type; bool filter_50hz; + int averaging; }; static int max31856_read(struct max31856_data *data, u8 reg, @@ -109,6 +112,10 @@ static int max31856_init(struct max31856_data *data) reg_cr1_val &= ~MAX31856_TC_TYPE_MASK; reg_cr1_val |= data->thermocouple_type; + + reg_cr1_val &= ~MAX31856_AVERAGING_MASK; + reg_cr1_val |= data->averaging << MAX31856_AVERAGING_SHIFT; + ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val); if (ret) return ret; @@ -295,14 +302,50 @@ static ssize_t set_filter(struct device *dev, return len; } +static ssize_t show_averaging(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + + return sprintf(buf, "%d\n", 1 << data->averaging); +} + +static ssize_t set_averaging(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + unsigned int nsamples; + int shift; + int ret; + + ret = kstrtouint(buf, 10, &nsamples); + if (ret) + return ret; + + shift = fls(nsamples) - 1; + if (nsamples > 16 || BIT(shift) != nsamples) + return -EINVAL; + + data->averaging = shift; + max31856_init(data); + return len; +} + static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0); static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0); static IIO_DEVICE_ATTR(filter, 0644, show_filter, set_filter, 0); +static IIO_DEVICE_ATTR(averaging, 0644, show_averaging, set_averaging, 0); static struct attribute *max31856_attributes[] = { &iio_dev_attr_fault_ovuv.dev_attr.attr, &iio_dev_attr_fault_oc.dev_attr.attr, &iio_dev_attr_filter.dev_attr.attr, + &iio_dev_attr_averaging.dev_attr.attr, NULL, }; From patchwork Mon Sep 23 12:17:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Merello X-Patchwork-Id: 11156669 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A1A8B13B1 for ; Mon, 23 Sep 2019 12:17:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8114C2089F for ; Mon, 23 Sep 2019 12:17:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="tzaJLYjx" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725913AbfIWMRw (ORCPT ); Mon, 23 Sep 2019 08:17:52 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:39413 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732101AbfIWMRv (ORCPT ); Mon, 23 Sep 2019 08:17:51 -0400 Received: by mail-wm1-f66.google.com with SMTP id v17so9052767wml.4 for ; Mon, 23 Sep 2019 05:17:50 -0700 (PDT) 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=QSsgq7cJwjjmwGU+j1IcvwyHO3CVCMDJ3zX6AShKDGM=; b=tzaJLYjxxVdzYPDA8luK/gxQV0sW6xM6NjJzrJeo99SOPNTQu3G8xhqnoM8PmzqzbM 6YAKmjQXgJX3JRPKQfJTv6KmbwQDk3uke76gjoc86n39VZ6CjPHqlyAo95zVM33WluVK J3i/4G/M09eB6ulj7Yh9Fv3CmCIDUdxIj6JMWrteLgEnZdf0DXDCawQBEyDQ+xrIL7xB URRD+Qio9gSe+nQ42tOLU6Nfl7JUwH+ruqJlYvTJWlclN87gAnCGWsnCEsijBFk/1aRh jldrbHLQwRFpVzrrteLMujhPWDKd3NUjHoDVtZesAqV+erPvAn8F56al98Ft7Bbj3jJs rUNw== 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=QSsgq7cJwjjmwGU+j1IcvwyHO3CVCMDJ3zX6AShKDGM=; b=WTczO3ODsH18dqL9AMiEZ/IPM5RzdrqOYaNfLkM10bsBl38ieyrR1R/zIdJTmxXk3N UcjncG8H0hFm9vDgZxU8Us2Wvh6AsROc6GFy7gPyM67Ns7iwjMcZlDQRi47GcGdLcIek Jzx1nHsPLZmlHGuoRrobq/cIM6w2Y5l6gYV0TcNadDaVo84TnnC0+af3kNU+XwoN1UYB DAF45zig2yxQfLb0FScZ3AhgWbqIjqW+5u+tctf1ScX8NWQsM+9K8R5CJfY9MF5aI8GD iQEMouoGHBY3OZDuJHl4Q4bb8zjt7T7f8wXYnDacWc+cDXhiKm3HVZLS7q4vFoBCBuXL /pPw== X-Gm-Message-State: APjAAAUq/XMCPm+7y64NyOwIjqxQEHF7tcFZ7Cr/XrFNfgunNxVs5OI8 1jUr0CFnsxGMoVY8Hh7OVls= X-Google-Smtp-Source: APXvYqzOgKAsem8Fp43vjeDlxDg/z7AJhaIwYROlIMId9UWN1Tl6eBibEOrwyJRNWcoVm1X7wTZuDw== X-Received: by 2002:a1c:720b:: with SMTP id n11mr12770650wmc.23.1569241069456; Mon, 23 Sep 2019 05:17:49 -0700 (PDT) Received: from NewMoon.iit.local ([90.147.180.254]) by smtp.gmail.com with ESMTPSA id a7sm11342836wra.43.2019.09.23.05.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Sep 2019 05:17:48 -0700 (PDT) From: Andrea Merello To: jic23@kernel.org, patrick.havelange@essensium.com, paresh.chaudhary@rockwellcollins.com, pmeerw@pmeerw.net, lars@metafoo.de, knaack.h@gmx.de, matthew.weber@rockwellcollins.com Cc: colin.king@canonical.com, linux-iio@vger.kernel.org, Andrea Merello Subject: [PATCH 3/3] iio: max31856: add support for runtime-configuring the thermocouple type Date: Mon, 23 Sep 2019 14:17:14 +0200 Message-Id: <20190923121714.13672-4-andrea.merello@gmail.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190923121714.13672-1-andrea.merello@gmail.com> References: <20190923121714.13672-1-andrea.merello@gmail.com> Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org The sensor support various thermocouple types (e.g. J, K, N, ...). The driver allows to configure this parameter using a DT property. This is useful when i.e. the thermocouple is physically tied to the sensor and it is usually not removed, or when it is at least known in advace which sensor will be connected to the circuit. However, if the user can randomly connect any kind of thermocouples (i.e. the device exposes a connector, and the user is free to connect its own sensors), it would be more appropriate to provide a mechanism to dynamically switch from one thermocouple type to another. This can be i.e. handled in userspace by a GUI, a configuration file or a program that detects the thermocouple type by reading a GPIO, or a eeprom on the probe, or whatever. This patch adds a IIO attribute that can be used to override, at run-time, the DT-provided setting (which serves as default). Signed-off-by: Andrea Merello --- drivers/iio/temperature/max31856.c | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/drivers/iio/temperature/max31856.c b/drivers/iio/temperature/max31856.c index 8b2e0102fa5c..588e791c79a3 100644 --- a/drivers/iio/temperature/max31856.c +++ b/drivers/iio/temperature/max31856.c @@ -6,6 +6,7 @@ * Copyright (C) 2018-2019 Rockwell Collins */ +#include #include #include #include @@ -70,6 +71,10 @@ struct max31856_data { int averaging; }; +const char max31856_tc_types[] = { + 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T' +}; + static int max31856_read(struct max31856_data *data, u8 reg, u8 val[], unsigned int read_size) { @@ -336,16 +341,55 @@ static ssize_t set_averaging(struct device *dev, return len; } +static ssize_t show_tc_type(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + + return sprintf(buf, "%c\n", max31856_tc_types[data->thermocouple_type]); +} + +static ssize_t set_tc_type(struct device *dev, + struct device_attribute *attr, + const char *buf, + size_t len) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct max31856_data *data = iio_priv(indio_dev); + char tmp; + int tc_type = -1; + int i; + + if (sscanf(buf, "%c\n", &tmp) != 1) + return -EINVAL; + + for (i = 0; i < ARRAY_SIZE(max31856_tc_types); i++) { + if (max31856_tc_types[i] == toupper(tmp)) { + tc_type = i; + break; + } + } + if (tc_type < 0) + return -EINVAL; + data->thermocouple_type = tc_type; + max31856_init(data); + return len; +} + static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0); static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0); static IIO_DEVICE_ATTR(filter, 0644, show_filter, set_filter, 0); static IIO_DEVICE_ATTR(averaging, 0644, show_averaging, set_averaging, 0); +static IIO_DEVICE_ATTR(thermocouple_type, 0644, show_tc_type, set_tc_type, 0); static struct attribute *max31856_attributes[] = { &iio_dev_attr_fault_ovuv.dev_attr.attr, &iio_dev_attr_fault_oc.dev_attr.attr, &iio_dev_attr_filter.dev_attr.attr, &iio_dev_attr_averaging.dev_attr.attr, + &iio_dev_attr_thermocouple_type.dev_attr.attr, NULL, };