Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp2422143rwl; Mon, 26 Dec 2022 14:51:04 -0800 (PST) X-Google-Smtp-Source: AMrXdXv6qGrDYe3OSJrixt5GF+hbrif7/S9Jqq0IqO+yXI7FgPmthP1dLqcVUmJEoM4eQ+78DhoN X-Received: by 2002:a17:902:6acb:b0:189:cdcc:5055 with SMTP id i11-20020a1709026acb00b00189cdcc5055mr19888206plt.0.1672095063933; Mon, 26 Dec 2022 14:51:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672095063; cv=none; d=google.com; s=arc-20160816; b=Xw3/HbgY62fZWAuwcF1Nz5Ve/BTBP/iwQfcB4zpxkalz1uAbZst2xJu2UOHoP+fhZm zoAAaeJak6fRHVSBN2gb34lYxtux4gEorIk+z72GqU3rSm/vgs0XM/d2SLdeNR5CS1+2 15MgbaMpiyP+Ek10EblRe8PRNHj9iZzm+oISTMOZLGri1Z4TJRsMY8mLOhgvnPg8jGMY r31SiXRX/tGfawB+I0CwLPqwRsnpruCwpOsakObmRT5o8Xl5jrKdh6JgzXUbpfmgDBII AFDXYCpA8WBmdz9jJgjohC9kFPDr+1t549jhb+wfOLxm2KeBGuorQIzTidLx66MmWuGv S1Jw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=oFvPTrAD2/mSiOxGgXH60G1/OioqgFym5utZnCnaAgo=; b=po9iRJvcj+O8vAYrmGwZgbm8lRB0Gr3dUzNx+3hpXP2lfnxMq3/SFC7QVTpgMkqUDD OL/xUcSmnBoVorVzRDGTbIvmAhUp0i9l4/qrhxz1Pf/MuFxFDzJMuQ+Jn4uhc5OXALOy VtEcPMvSp7ba+ilJ3KCxDzgePqSQpGWyJfreauKGPO9YeDGJC5T2Ayu5ED8CX4rqwOE/ LQKTZOQVsjibJsAH+noQ0hRwtg8SsYmb1wLlDNrIBSbBJ7n9X6mWk31VPkBMykhF/1AN Mw5RfnI95DmtMSZ7uQ15YXU9SoB622cbaeqN/2gURXoKg14pr23SYfxDx/GzmZP39RBl 09kQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=analog.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m16-20020a63ed50000000b00476990d057asi12514368pgk.472.2022.12.26.14.50.55; Mon, 26 Dec 2022 14:51:03 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=analog.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232481AbiLZWll (ORCPT + 66 others); Mon, 26 Dec 2022 17:41:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232403AbiLZWkh (ORCPT ); Mon, 26 Dec 2022 17:40:37 -0500 Received: from mx0a-00128a01.pphosted.com (mx0a-00128a01.pphosted.com [148.163.135.77]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A6F9F19; Mon, 26 Dec 2022 14:39:53 -0800 (PST) Received: from pps.filterd (m0167089.ppops.net [127.0.0.1]) by mx0a-00128a01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2BQIMKKL019565; Mon, 26 Dec 2022 17:39:39 -0500 Received: from nwd2mta4.analog.com ([137.71.173.58]) by mx0a-00128a01.pphosted.com (PPS) with ESMTPS id 3mny7a205d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 26 Dec 2022 17:39:39 -0500 Received: from ASHBMBX9.ad.analog.com (ASHBMBX9.ad.analog.com [10.64.17.10]) by nwd2mta4.analog.com (8.14.7/8.14.7) with ESMTP id 2BQMdc93000576 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 26 Dec 2022 17:39:38 -0500 Received: from ASHBMBX8.ad.analog.com (10.64.17.5) by ASHBMBX9.ad.analog.com (10.64.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.14; Mon, 26 Dec 2022 17:39:37 -0500 Received: from zeus.spd.analog.com (10.66.68.11) by ashbmbx8.ad.analog.com (10.64.17.5) with Microsoft SMTP Server id 15.2.986.14 via Frontend Transport; Mon, 26 Dec 2022 17:39:37 -0500 Received: from okan.localdomain ([10.158.19.61]) by zeus.spd.analog.com (8.15.1/8.15.1) with ESMTP id 2BQMcuAe032340; Mon, 26 Dec 2022 17:39:28 -0500 From: Okan Sahin To: CC: Jonathan Cameron , Lars-Peter Clausen , open list , "open list:IIO SUBSYSTEM AND DRIVERS" Subject: [PATCH v2 5/5] drivers: iio: adc: Add MAX77541 ADC Support Date: Tue, 27 Dec 2022 01:38:39 +0300 Message-ID: <20221226223839.103460-6-okan.sahin@analog.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221226223839.103460-1-okan.sahin@analog.com> References: <20221226223839.103460-1-okan.sahin@analog.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-ADIRuleOP-NewSCL: Rule Triggered X-Proofpoint-ORIG-GUID: IdZI3Uy7ccDsE7ttxrpP5PGbPX8tpLpF X-Proofpoint-GUID: IdZI3Uy7ccDsE7ttxrpP5PGbPX8tpLpF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.923,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-12-26_18,2022-12-23_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 mlxscore=0 phishscore=0 lowpriorityscore=0 impostorscore=0 suspectscore=0 priorityscore=1501 clxscore=1015 spamscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2212260192 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS 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 The MAX77541 has an 8-bit Successive Approximation Register (SAR) ADC with four multiplexers for supporting the telemetry feature Signed-off-by: Okan Sahin --- MAINTAINERS | 1 + drivers/iio/adc/Kconfig | 11 ++ drivers/iio/adc/Makefile | 1 + drivers/iio/adc/max77541-adc.c | 199 +++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+) create mode 100644 drivers/iio/adc/max77541-adc.c diff --git a/MAINTAINERS b/MAINTAINERS index 8e5572b28a8c..18ce4644cc75 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -12503,6 +12503,7 @@ L: linux-kernel@vger.kernel.org S: Maintained F: Documentation/devicetree/bindings/mfd/adi,max77541.yaml F: Documentation/devicetree/bindings/regulator/adi,max77541.yaml +F: drivers/iio/adc/max77541-adc.c F: drivers/mfd/max77541.c F: drivers/regulator/max77541-regulator.c F: include/linux/mfd/max77541.h diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 791612ca6012..9716225b50da 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -696,6 +696,17 @@ config MAX1363 To compile this driver as a module, choose M here: the module will be called max1363. +config MAX77541_ADC + tristate "Analog Devices MAX77541 ADC driver" + depends on MFD_MAX77541 + help + This driver controls a Analog Devices MAX77541 ADC + via I2C bus. This device has one adc. Say yes here to build + support for Analog Devices MAX77541 ADC interface. + + To compile this driver as a module, choose M here: + the module will be called max77541-adc. + config MAX9611 tristate "Maxim max9611/max9612 ADC driver" depends on I2C diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index 46caba7a010c..03774cccbb4b 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -64,6 +64,7 @@ obj-$(CONFIG_MAX1118) += max1118.o obj-$(CONFIG_MAX11205) += max11205.o obj-$(CONFIG_MAX1241) += max1241.o obj-$(CONFIG_MAX1363) += max1363.o +obj-$(CONFIG_MAX77541_ADC) += max77541-adc.o obj-$(CONFIG_MAX9611) += max9611.o obj-$(CONFIG_MCP320X) += mcp320x.o obj-$(CONFIG_MCP3422) += mcp3422.o diff --git a/drivers/iio/adc/max77541-adc.c b/drivers/iio/adc/max77541-adc.c new file mode 100644 index 000000000000..29adcdbd96ae --- /dev/null +++ b/drivers/iio/adc/max77541-adc.c @@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (c) 2022 Analog Devices, Inc. + * ADI MAX77541 ADC Driver with IIO interface + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX77541_ADC_CHANNEL(_channel, _name, _type, _reg) \ + { \ + .type = _type, \ + .indexed = 1, \ + .channel = _channel, \ + .address = _reg, \ + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ + BIT(IIO_CHAN_INFO_SCALE) |\ + BIT(IIO_CHAN_INFO_OFFSET),\ + .datasheet_name = _name, \ + } + +enum max77541_adc_range { + LOW_RANGE, + MID_RANGE, + HIGH_RANGE, +}; + +struct max77541_adc_iio { + struct regmap *regmap; +}; + +enum max77541_adc_channel { + MAX77541_ADC_VSYS_V = 0, + MAX77541_ADC_VOUT1_V, + MAX77541_ADC_VOUT2_V, + MAX77541_ADC_TEMP, +}; + +static int max77541_adc_offset(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2) +{ + switch (chan->channel) { + case MAX77541_ADC_VSYS_V: + case MAX77541_ADC_VOUT1_V: + case MAX77541_ADC_VOUT2_V: + *val = 0; + *val2 = 0; + return IIO_VAL_INT_PLUS_MICRO; + case MAX77541_ADC_TEMP: + *val = DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, + MILLIDEGREE_PER_DEGREE); + *val2 = 0; + return IIO_VAL_INT_PLUS_MICRO; + default: + return -EINVAL; + } +} + +static int max77541_adc_scale(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2) +{ + struct max77541_adc_iio *info = iio_priv(indio_dev); + unsigned int reg_val; + int ret; + + switch (chan->channel) { + case MAX77541_ADC_VSYS_V: + *val = 0; + *val2 = 25000; + return IIO_VAL_INT_PLUS_MICRO; + case MAX77541_ADC_VOUT1_V: + case MAX77541_ADC_VOUT2_V: + ret = regmap_read(info->regmap, MAX77541_REG_M2_CFG1, ®_val); + if (ret) + return ret; + reg_val = FIELD_GET(MAX77541_BITS_MX_CFG1_RNG, reg_val); + + *val = 0; + + switch (reg_val) { + case LOW_RANGE: + *val2 = 6250; + break; + case MID_RANGE: + *val2 = 12500; + break; + case HIGH_RANGE: + *val2 = 25000; + break; + default: + return -EINVAL; + } + + return IIO_VAL_INT_PLUS_MICRO; + case MAX77541_ADC_TEMP: + *val = 1; + *val2 = 725000; + return IIO_VAL_INT_PLUS_MICRO; + default: + return -EINVAL; + } +} + +static int max77541_adc_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val) +{ + struct max77541_adc_iio *info = iio_priv(indio_dev); + int ret; + + ret = regmap_read(info->regmap, chan->address, val); + if (ret) + return ret; + + return IIO_VAL_INT; +} + +static const struct iio_chan_spec max77541_adc_channels[] = { + MAX77541_ADC_CHANNEL(MAX77541_ADC_VSYS_V, "vsys_v", IIO_VOLTAGE, + MAX77541_REG_ADC_DATA_CH1), + MAX77541_ADC_CHANNEL(MAX77541_ADC_VOUT1_V, "vout1_v", IIO_VOLTAGE, + MAX77541_REG_ADC_DATA_CH2), + MAX77541_ADC_CHANNEL(MAX77541_ADC_VOUT2_V, "vout2_v", IIO_VOLTAGE, + MAX77541_REG_ADC_DATA_CH3), + MAX77541_ADC_CHANNEL(MAX77541_ADC_TEMP, "temp", IIO_TEMP, + MAX77541_REG_ADC_DATA_CH6), +}; + +static int max77541_adc_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + switch (mask) { + case IIO_CHAN_INFO_OFFSET: + return max77541_adc_offset(indio_dev, chan, val, val2); + case IIO_CHAN_INFO_SCALE: + return max77541_adc_scale(indio_dev, chan, val, val2); + case IIO_CHAN_INFO_RAW: + return max77541_adc_raw(indio_dev, chan, val); + default: + return -EINVAL; + } +} + +static const struct iio_info max77541_adc_info = { + .read_raw = max77541_adc_read_raw, +}; + +static int max77541_adc_probe(struct platform_device *pdev) +{ + struct max77541_dev *max77541 = dev_get_drvdata(pdev->dev.parent); + struct device *dev = &pdev->dev; + struct max77541_adc_iio *info; + struct iio_dev *indio_dev; + + indio_dev = devm_iio_device_alloc(dev, sizeof(*info)); + if (!indio_dev) + return -ENOMEM; + + info = iio_priv(indio_dev); + + info->regmap = max77541->regmap; + indio_dev->modes = INDIO_DIRECT_MODE; + + indio_dev->name = platform_get_device_id(pdev)->name; + indio_dev->info = &max77541_adc_info; + indio_dev->channels = max77541_adc_channels; + indio_dev->num_channels = ARRAY_SIZE(max77541_adc_channels); + + return devm_iio_device_register(dev, indio_dev); +} + +static const struct platform_device_id max77541_adc_platform_id[] = { + { "max77541-adc", }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(platform, max77541_adc_platform_id); + +static struct platform_driver max77541_adc_driver = { + .driver = { + .name = "max77541-adc", + }, + .probe = max77541_adc_probe, + .id_table = max77541_adc_platform_id, +}; +module_platform_driver(max77541_adc_driver); + +MODULE_AUTHOR("Okan Sahin "); +MODULE_DESCRIPTION("MAX77541 ADC driver"); +MODULE_LICENSE("GPL"); -- 2.30.2