From patchwork Fri Nov 18 15:37:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Rajat Khandelwal X-Patchwork-Id: 21747 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp469093wrr; Thu, 17 Nov 2022 07:40:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf7UoJGGY83rUNsuywIYH5fl6xviovYsSpi8vRMnsqrPtOKPQwFTmxBZDWeVvtYwbnzrUiL0 X-Received: by 2002:a17:906:348b:b0:78d:9e04:d8c2 with SMTP id g11-20020a170906348b00b0078d9e04d8c2mr2505926ejb.614.1668699637333; Thu, 17 Nov 2022 07:40:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668699637; cv=none; d=google.com; s=arc-20160816; b=gH6Nd9S96BalNQHn2LMYzmep7oZWgzHfBzaQCq+s1o+GZi7KqNbaN09t1iD6mYtysH ozoqWnhpsEq958zMS1oE61z9Oo1qG6D6r7raYDmLMvy6MPj/UmdC+ySSEE7MT6IiLgzC ruGFmPUf0OhmsLLPsp+gt0L0MTQ+41CsSaFFBIHXgazWTx8Dtm7qAcged98NdFQ/jaMH jaB2ucuTDWi7gQV/idqlMSSgQNEfFBR/InyVS9lz9FWHUijTIp/9Jhw1N5a3WgeJIDth eOq2DhkR57lbLdyWMbRYhgbL0QKsrY6aa1D+xk38N+OBjflATmQ0hzaadtIokIX298Wn ayeA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=5Gl+ncM/ASmma1FR9VfuAkZt1Nud9R1yjWHbjOAnMnk=; b=j0lm4GfHjsrWfzqVbEyw12NjzZvG+I1oghL5xotOgEC8iRr7MSJe0IPSbWsge6BU0o gnzjtPZJ9S3KYpQ+1Cos7EKEwEjC8NRrLgSvBXTxVCLQeNYoTPAOidDwgiyasNcec+mJ QS+fjzhjsUXBEb3O1TMEx4OuWGAPY+ZkmqN9Gl+6RG+FFTK7bDI95xNSUZtUlKDScEXh tvmKpeOWH7ppWHqufnw8JX/2EUN0Fgcgonscj6rbiGxFVW9L8UgkMd/GFu/8Yhq7GXPp fxrDun6Zm+GFGefIrfwXG7SnOE6ZL0/efKCXKl2buQXrpm6vGGGHqiiClEwJUHkY7/8G jVQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QEc2JAFI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oz13-20020a170906cd0d00b007a46fa50b26si651205ejb.517.2022.11.17.07.40.12; Thu, 17 Nov 2022 07:40:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=QEc2JAFI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239456AbiKQPiT (ORCPT + 99 others); Thu, 17 Nov 2022 10:38:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234934AbiKQPiB (ORCPT ); Thu, 17 Nov 2022 10:38:01 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5584E59859; Thu, 17 Nov 2022 07:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668699480; x=1700235480; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=U03TPz/uuXhwbVKrgsgYAMUrnhL2sOKZzWfzmrmv1LU=; b=QEc2JAFIml1Tr0LwXTwdXFq9MupLO20vUa8QZslcQwtA4gY+FwxFlg+W lPW8gaJ2DnaaCGoNbtXTL4k5HLg4dGDzjnllKcMv+/eLTVbR9fB0QXqBS dJJ9+VrWqESs0u9yshguRbP0lRUAMecFHQFsMHlg7dJRODXM+KonlC5HS S8haXlHMIKq6QEcAHN2hr15yvLNYnUFDol7VQPyrLvpcG+f94w1T1r0/x Oo3VcApBofDCd1BglrvgAvNBpBCQtLtkrFe1PgXruCn5O71L28h7S2jI+ GeYbRv1IXDPP2RPntK7Yl6Gvtz2E2gTGDvVnLl7cZWs+fuipQu8FmnxAF Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="312895845" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="312895845" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 07:38:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="634090448" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="634090448" Received: from unknown (HELO rajath-NUC10i7FNH..) ([10.223.165.88]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 07:37:56 -0800 From: Rajat Khandelwal To: jic23@kernel.org, lars@metafoo.de Cc: linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, jdelvare@suse.com, linux@roeck-us.net, linux-hwmon@vger.kernel.org, rajat.khandelwal@intel.com, Rajat Khandelwal Subject: [PATCH v10] iio: temperature: Add driver support for Maxim MAX30208 Date: Fri, 18 Nov 2022 21:07:29 +0530 Message-Id: <20221118153729.762018-1-rajat.khandelwal@linux.intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3.8 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_12_24, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749758390971117644?= X-GMAIL-MSGID: =?utf-8?q?1749758390971117644?= Maxim MAX30208 is a digital temperature sensor with 0.1°C accuracy. Add support for max30208 driver in iio subsystem. Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX30208.pdf Signed-off-by: Rajat Khandelwal --- v10: Correcting the overflow logic v9: Repositioning register data v8: 1. Returning time out if conversion fails to happen 2. Setting rollover bit to '1' to allow FIFO overwriting 3. Dropping ACPI_PTR v7: 1. Dropped GPIOs use for now 2. Driver name string directly used 3. Mutex lock description added 4. Removed noisy errors and only kept errors on larger code blocks 5. dev_warn -> dev_err for temperature conversion failure 6. Improvised the logic of popping out values 7. Fixed line breaks 8. module_i2c_driver v6: Converted usleep_range to msleep as delay is quite large v5: 1. Fixed comment position in max30208_request 2. Use of local u8 variable to build register values 3. Using u8 instead of s8 in data_count 4. Removed global MAX30208_RES_MILLICELCIUS 5. Removed 'comma' on NULL terminators v4: Version comments go below line separator of signed-off-by v3: Release the mutex lock after error gets returned v2: 1. Removed TODO 2. Removed unnecessary blank spaces 3. Corrected MC->MILLICELCIUS 4. Comments added wherever required 5. dev_err on i2c fails 6. Rearranged some flows 7. Removed PROCESSED 8. int error return on gpio setup 9. device_register at the end of probe 10. Return on unsuccessful reset 11. acpi_match_table and of_match_table added 12. Minor quirks MAINTAINERS | 6 + drivers/iio/temperature/Kconfig | 10 ++ drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/max30208.c | 252 +++++++++++++++++++++++++++++ 4 files changed, 269 insertions(+) create mode 100644 drivers/iio/temperature/max30208.c diff --git a/MAINTAINERS b/MAINTAINERS index f1390b8270b2..7f1fd2e31b94 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -12373,6 +12373,12 @@ S: Maintained F: Documentation/devicetree/bindings/regulator/maxim,max20086.yaml F: drivers/regulator/max20086-regulator.c +MAXIM MAX30208 TEMPERATURE SENSOR DRIVER +M: Rajat Khandelwal +L: linux-iio@vger.kernel.org +S: Maintained +F: drivers/iio/temperature/max30208.c + MAXIM MAX77650 PMIC MFD DRIVER M: Bartosz Golaszewski L: linux-kernel@vger.kernel.org diff --git a/drivers/iio/temperature/Kconfig b/drivers/iio/temperature/Kconfig index e8ed849e3b76..ed384f33e0c7 100644 --- a/drivers/iio/temperature/Kconfig +++ b/drivers/iio/temperature/Kconfig @@ -128,6 +128,16 @@ config TSYS02D This driver can also be built as a module. If so, the module will be called tsys02d. +config MAX30208 + tristate "Maxim MAX30208 digital temperature sensor" + depends on I2C + help + If you say yes here you get support for Maxim MAX30208 + digital temperature sensor connected via I2C. + + This driver can also be built as a module. If so, the module + will be called max30208. + config MAX31856 tristate "MAX31856 thermocouple sensor" depends on SPI diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile index dd08e562ffe0..dfec8c6d3019 100644 --- a/drivers/iio/temperature/Makefile +++ b/drivers/iio/temperature/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_IQS620AT_TEMP) += iqs620at-temp.o obj-$(CONFIG_LTC2983) += ltc2983.o obj-$(CONFIG_HID_SENSOR_TEMP) += hid-sensor-temperature.o obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.o +obj-$(CONFIG_MAX30208) += max30208.o obj-$(CONFIG_MAX31856) += max31856.o obj-$(CONFIG_MAX31865) += max31865.o obj-$(CONFIG_MLX90614) += mlx90614.o diff --git a/drivers/iio/temperature/max30208.c b/drivers/iio/temperature/max30208.c new file mode 100644 index 000000000000..c85c21474711 --- /dev/null +++ b/drivers/iio/temperature/max30208.c @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* + * Copyright (c) Rajat Khandelwal + * + * Maxim MAX30208 digital temperature sensor with 0.1°C accuracy + * (7-bit I2C slave address (0x50 - 0x53)) + */ + +#include +#include +#include +#include +#include +#include + +#define MAX30208_STATUS 0x00 +#define MAX30208_STATUS_TEMP_RDY BIT(0) +#define MAX30208_INT_ENABLE 0x01 +#define MAX30208_INT_ENABLE_TEMP_RDY BIT(0) + +#define MAX30208_FIFO_OVF_CNTR 0x06 +#define MAX30208_FIFO_DATA_CNTR 0x07 +#define MAX30208_FIFO_DATA 0x08 + +#define MAX30208_FIFO_CONFIG 0x0a +#define MAX30208_FIFO_CONFIG_RO BIT(1) + +#define MAX30208_SYSTEM_CTRL 0x0c +#define MAX30208_SYSTEM_CTRL_RESET 0x01 + +#define MAX30208_TEMP_SENSOR_SETUP 0x14 +#define MAX30208_TEMP_SENSOR_SETUP_CONV BIT(0) + +struct max30208_data { + struct i2c_client *client; + struct iio_dev *indio_dev; + struct mutex lock; /* Lock to prevent concurrent reads of temperature readings */ +}; + +static const struct iio_chan_spec max30208_channels[] = { + { + .type = IIO_TEMP, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), + }, +}; + +/** + * max30208_request() - Request a reading + * @data: Struct comprising member elements of the device + * + * Requests a reading from the device and waits until the conversion is ready. + */ +static int max30208_request(struct max30208_data *data) +{ + /* + * Sensor can take up to 500 ms to respond so execute a total of + * 10 retries to give the device sufficient time. + */ + int retries = 10; + u8 regval; + int ret; + + ret = i2c_smbus_read_byte_data(data->client, MAX30208_TEMP_SENSOR_SETUP); + if (ret < 0) + return ret; + + regval = ret | MAX30208_TEMP_SENSOR_SETUP_CONV; + + ret = i2c_smbus_write_byte_data(data->client, MAX30208_TEMP_SENSOR_SETUP, regval); + if (ret) + return ret; + + while (retries--) { + ret = i2c_smbus_read_byte_data(data->client, MAX30208_STATUS); + if (ret < 0) + return ret; + + if (ret & MAX30208_STATUS_TEMP_RDY) + return 0; + + msleep(50); + } + dev_err(&data->client->dev, "Temperature conversion failed\n"); + + return -ETIMEDOUT; +} + +static int max30208_update_temp(struct max30208_data *data) +{ + u8 data_count; + int ret; + + mutex_lock(&data->lock); + + ret = max30208_request(data); + if (ret) + goto unlock; + + ret = i2c_smbus_read_byte_data(data->client, MAX30208_FIFO_OVF_CNTR); + if (ret < 0) + goto unlock; + else if (!ret) { + ret = i2c_smbus_read_byte_data(data->client, MAX30208_FIFO_DATA_CNTR); + if (ret < 0) + goto unlock; + + data_count = ret; + } else + data_count = 1; + + while (data_count) { + ret = i2c_smbus_read_word_swapped(data->client, MAX30208_FIFO_DATA); + if (ret < 0) + goto unlock; + + data_count--; + } + +unlock: + mutex_unlock(&data->lock); + return ret; +} + +/** + * max30208_config_setup() - Set up FIFO configuration register + * @data: Struct comprising member elements of the device + * + * Sets the rollover bit to '1' to enable overwriting FIFO during overflow. + */ +static int max30208_config_setup(struct max30208_data *data) +{ + u8 regval; + int ret; + + ret = i2c_smbus_read_byte_data(data->client, MAX30208_FIFO_CONFIG); + if (ret < 0) + return ret; + + regval = ret | MAX30208_FIFO_CONFIG_RO; + + ret = i2c_smbus_write_byte_data(data->client, MAX30208_FIFO_CONFIG, regval); + if (ret) + return ret; + + return 0; +} + +static int max30208_read(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct max30208_data *data = iio_priv(indio_dev); + int ret; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + ret = max30208_update_temp(data); + if (ret < 0) + return ret; + + *val = sign_extend32(ret, 15); + return IIO_VAL_INT; + + case IIO_CHAN_INFO_SCALE: + *val = 5; + return IIO_VAL_INT; + + default: + return -EINVAL; + } +} + +static const struct iio_info max30208_info = { + .read_raw = max30208_read, +}; + +static int max30208_probe(struct i2c_client *i2c) +{ + struct device *dev = &i2c->dev; + struct max30208_data *data; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); + if (!indio_dev) + return -ENOMEM; + + data = iio_priv(indio_dev); + data->client = i2c; + mutex_init(&data->lock); + + indio_dev->name = "max30208"; + indio_dev->channels = max30208_channels; + indio_dev->num_channels = ARRAY_SIZE(max30208_channels); + indio_dev->info = &max30208_info; + indio_dev->modes = INDIO_DIRECT_MODE; + + ret = i2c_smbus_write_byte_data(data->client, MAX30208_SYSTEM_CTRL, + MAX30208_SYSTEM_CTRL_RESET); + if (ret) { + dev_err(dev, "Failure in performing reset\n"); + return ret; + } + + msleep(50); + + ret = max30208_config_setup(data); + if (ret) + return ret; + + ret = devm_iio_device_register(dev, indio_dev); + if (ret) { + dev_err(dev, "Failed to register IIO device\n"); + return ret; + } + + return 0; +} + +static const struct i2c_device_id max30208_id_table[] = { + { "max30208" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, max30208_id_table); + +static const struct acpi_device_id max30208_acpi_match[] = { + { "MAX30208" }, + { } +}; +MODULE_DEVICE_TABLE(acpi, max30208_acpi_match); + +static const struct of_device_id max30208_of_match[] = { + { .compatible = "maxim,max30208" }, + { } +}; +MODULE_DEVICE_TABLE(of, max30208_of_match); + +static struct i2c_driver max30208_driver = { + .driver = { + .name = "max30208", + .of_match_table = max30208_of_match, + .acpi_match_table = max30208_acpi_match, + }, + .probe_new = max30208_probe, + .id_table = max30208_id_table, +}; +module_i2c_driver(max30208_driver); + +MODULE_AUTHOR("Rajat Khandelwal "); +MODULE_DESCRIPTION("Maxim MAX30208 digital temperature sensor"); +MODULE_LICENSE("GPL");