From patchwork Tue Aug 13 05:40:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 13761335 Received: from mta-65-228.siemens.flowmailer.net (mta-65-228.siemens.flowmailer.net [185.136.65.228]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 03D1E28E37 for ; Tue, 13 Aug 2024 05:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.136.65.228 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527660; cv=none; b=VbBexC7IrDJBOJEvRwaOOd1rESC57oPho8e98jLpN2ExABuoO7QiUCifChubmQOd4avOBLdpKUnzBDzTNgXM1iQ7DdGeX98gbDpl3UDObiB32Xbj+1lAXCFF/bnU4d2LB4MnaFZdMhPumL5RpgAmuako1YYBwihBe8min4u3wcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527660; c=relaxed/simple; bh=zFEUd2pMUFqc0l2cFhXTA/vcslU9ZSjDvoVcTnst/r4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=k6GSsHByqHX6JmXEbph3+SX9VfuYunXngKfFJOA4dfMriUfxPxgEcQGOc6FBtsRksS4MGb970e3YhEo04zTy+xLS6EwVY5NX9So8xzRd3jHhA1GU63AOT4UBRGJdJ3J3bKnVKsL7PO0WR+equ5ZuTyCuu04yQijaPPHz3AQgk6w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b=VJOuKDzw; arc=none smtp.client-ip=185.136.65.228 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b="VJOuKDzw" Received: by mta-65-228.siemens.flowmailer.net with ESMTPSA id 2024081305405388560a319dc6d81327 for ; Tue, 13 Aug 2024 07:40:53 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=jan.kiszka@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc:References:In-Reply-To; bh=kLxXWKJeSf3UekWLu1x2/pCppvnWMzHoBEKo5ap7lj0=; b=VJOuKDzwHufpOhs/aRMbQiN8gZR0YU0laF/YR7TdwxfUUdgGe3o8dHTz+s3kZ6TB9MgeeU IOFsx6YONsblRcuphd32QIIoOR8jJeU3Dd3SI2gPXT3+oWgQCMvT+IC10tIb3JGRdfQNiv8F VLI7jdPcMRBgmDznhM1hW6Ch1Ek6po9SJCOX7IlIa43QfKLWED5ubbayg8JlNgo0J2xaIrYT AvZa1YGq5vIFgyyFDF8riHeCmUTVuNIVfU6PlDTTHvtJjB95wXEt3bJ30W7AHzJd613B2fww ADz9XWVuF7nBHcIitslUM9oyA0K3zmQjzFtQvY+1Wk0LEpf38lIXn2UA==; From: Jan Kiszka To: Jonathan Cameron , linux-iio@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-kernel@vger.kernel.org, Bao Cheng Su , Chao Zeng , devicetree@vger.kernel.org Subject: [PATCH 1/3] dt-bindings: vendor-prefixes: Add EVERLIGHT Date: Tue, 13 Aug 2024 07:40:40 +0200 Message-ID: <1c79a109a7e91927a9380d2aee91fae32848d7f7.1723527641.git.jan.kiszka@siemens.com> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-294854:519-21489:flowmailer From: Baocheng Su Add vendor prefix for EVERLIGHT Electronics Co., Ltd. Signed-off-by: Baocheng Su Acked-by: Conor Dooley --- Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml index a70ce43b3dc0..1d2bf326fe91 100644 --- a/Documentation/devicetree/bindings/vendor-prefixes.yaml +++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml @@ -486,6 +486,8 @@ patternProperties: description: Eukréa Electromatique "^everest,.*": description: Everest Semiconductor Co. Ltd. + "^everlight,.*": + description: EVERLIGHT Electronics Co., Ltd. "^everspin,.*": description: Everspin Technologies, Inc. "^evervision,.*": From patchwork Tue Aug 13 05:40:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 13761333 Received: from mta-64-227.siemens.flowmailer.net (mta-64-227.siemens.flowmailer.net [185.136.64.227]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F310828DA5 for ; Tue, 13 Aug 2024 05:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.136.64.227 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527659; cv=none; b=YiI61eAr9zMbzjfESwQF9ayhwsWPUk5JAqbgiFEepkfiExywuYiqLC22J3951OCY51dyvVQnTWd0S2gDIah6TPWiH5vgabq+igi6+k9eXVlbGO0B8QS7z3gH/xAFi1AIAedfpmKM6CVwEooFMtTJhyKOSiXVCFV0OvLkfUquLOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527659; c=relaxed/simple; bh=+Sl9js/FAvukhdw5pD5G5fro6DzgQKGz+ecKiCcQwqE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fh5uAwSKrBbT3C82vRAsLz43++mDj1RZrhqfniiiPwdEdqMbj0W7+tf/K5KRdtuksCwJfPvivAC0OnVjx2qST+aiQEKtXEIiakV9/Wx0j/Zw1RxTFBsIAbrUaJiDDFFe47wbZ2HGusftwmwY1wyhNqF3Saa3ZsjXHOdHk22u65A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b=QhFGWnWA; arc=none smtp.client-ip=185.136.64.227 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b="QhFGWnWA" Received: by mta-64-227.siemens.flowmailer.net with ESMTPSA id 202408130540533d9c99f68ca363c30e for ; Tue, 13 Aug 2024 07:40:53 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=jan.kiszka@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc:References:In-Reply-To; bh=mrt4p7/B6H9XYp1wTap2ACfGRhZiMZXU+/jlyXlj66w=; b=QhFGWnWAN6Y4QoLtKfwKnqfnzPRm1nabQAN3+FjUDNJ25utXlcFw0N16i1gviVU+M6+obx VeXTtUyZ1mBjV+a6ksVhybo6heKZXlyE5j90/mvDbhiCPPWl+R0Fx2JWSqwgOCZsOjNxqdrX g1UpzTYE7uzDbWQ92aV5K8OQrZCa3z5g3fXKmUI0wn05Sj4qLBwaHT5ajriDmJm9Q6mauKAL dQbc7A8v18JOJH8/vnNWy5QcDHE6GwZMW9fS1ZuTHVKFwvG2DG7ZtkDvXNHWOA7vQH4PQjwr 1w+OOFIO2pOFiiq00TqX6cZVa3ZilYtxe6nm6InQVpXv2P7sibF6q33Q==; From: Jan Kiszka To: Jonathan Cameron , linux-iio@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-kernel@vger.kernel.org, Bao Cheng Su , Chao Zeng , devicetree@vger.kernel.org Subject: [PATCH 2/3] dt-bindings: iio: Add everlight pm16d17 binding Date: Tue, 13 Aug 2024 07:40:41 +0200 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-294854:519-21489:flowmailer From: Chao Zeng Add the binding document for the everlight pm16d17 sensor. Signed-off-by: Chao Zeng Co-developed-by: Baocheng Su Signed-off-by: Baocheng Su --- .../iio/proximity/everlight,pm16d17.yaml | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/proximity/everlight,pm16d17.yaml diff --git a/Documentation/devicetree/bindings/iio/proximity/everlight,pm16d17.yaml b/Documentation/devicetree/bindings/iio/proximity/everlight,pm16d17.yaml new file mode 100644 index 000000000000..fadc3075181a --- /dev/null +++ b/Documentation/devicetree/bindings/iio/proximity/everlight,pm16d17.yaml @@ -0,0 +1,95 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/proximity/everlight,pm16d17.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Everlight PM-16D17 Ambient Light & Proximity Sensor + +maintainers: + - Chao Zeng + +description: | + This sensor uses standard I2C interface. Interrupt function is not covered. + Datasheet: https://en.everlight.com/sensor/category-proximity_sensor/digital_proximity_sensor/ + +properties: + compatible: + enum: + - everlight,pm16d17 + + reg: + maxItems: 1 + + ps-gain: + description: Receiver gain of proximity sensor + $ref: /schemas/types.yaml#/definitions/uint32 + enum: [1, 2, 4, 8] + default: 1 + + ps-itime: + description: Conversion time for proximity sensor [ms] + $ref: /schemas/types.yaml#/definitions/string + enum: + - "0.4" + - "0.8" + - "1.6" + - "3.2" + - "6.3" + - "12.6" + - "25.2" + default: "0.4" + + ps-wtime: + description: Waiting time for proximity sensor [ms] + $ref: /schemas/types.yaml#/definitions/string + enum: + - "12.5" + - "25" + - "50" + - "100" + - "200" + - "400" + - "800" + - "1600" + default: "12.5" + + ps-ir-led-pulse-count: + description: IR LED drive pulse count + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 256 + default: 1 + + ps-offset-cancel: + description: | + When PS offset cancel function is enabled, the result of subtracting any + value specified by the PS offset cancel register from the internal PS + output data is written to the PS output data register. + $ref: /schemas/types.yaml#/definitions/uint32 + default: 0 + maximum: 65535 + +required: + - compatible + - reg + +unevaluatedProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + lightsensor: pm16d17@44 { + compatible = "everlight,pm16d17"; + reg = <0x44>; + + ps-gain = <1>; + ps-itime = "0.4"; + ps-wtime = "12.5"; + ps-ir-led-pulse-count = <1>; + ps-offset-cancel = <280>; + }; + }; From patchwork Tue Aug 13 05:40:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kiszka X-Patchwork-Id: 13761334 Received: from mta-64-225.siemens.flowmailer.net (mta-64-225.siemens.flowmailer.net [185.136.64.225]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69CC32C19E for ; Tue, 13 Aug 2024 05:40:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.136.64.225 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527659; cv=none; b=myXgIdJctYPbKn7v4hgG1YzH+LX5RPF50d26lxKFvEAdw3SlCbRo7zuX1YNfM5tBdL4KZCOmmWdWQUipI0E6FGw3k7xHF6zviusX0cVNjN1wFPlCrC/VfiwjasYt1cBCLzMdXUIW6+C5wBRYOkVLzE5qXIwY0lu1wQCHVpU6j10= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723527659; c=relaxed/simple; bh=lqrNRFOGEIqChEHT8COg3Ajkibxc3Cly4DTf3GL9xj0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rf9UpeGA6qzKmGWPGQMrNEcJn5B+bPyNNoQJkNy9eQUHuPH8WC9sBbNt1wl+q/KBetp/bHmfbM9d8C5C1/rSvD2ZUnzRQhcItQPYk/wVZoLB6z55r4INTKiIiqqmRHKJEdayeNEvBtRfW9N349pEXAjnHRSvuBMeM+to5mZZSWE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b=W+Yc50RD; arc=none smtp.client-ip=185.136.64.225 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=siemens.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rts-flowmailer.siemens.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=siemens.com header.i=jan.kiszka@siemens.com header.b="W+Yc50RD" Received: by mta-64-225.siemens.flowmailer.net with ESMTPSA id 20240813054054f911ea2106586132ab for ; Tue, 13 Aug 2024 07:40:54 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=fm1; d=siemens.com; i=jan.kiszka@siemens.com; h=Date:From:Subject:To:Message-ID:MIME-Version:Content-Type:Content-Transfer-Encoding:Cc:References:In-Reply-To; bh=P5fnWxXIzhRO02nPudc2IAQGQdZVKSGoTLHiEj4IpHQ=; b=W+Yc50RD4AyEogFGWiZm3lemsqRQyisBGl8kL7cP48QZJUiDTZLEuSa1c+oJHumaDQ0drj W1K3SzX+FuMFp0w1NqxEUg7d8UjnZVgwAfwOf8Ok1pz6sgZRKruaRVHLG8DT7ZkvyBnRpUvb yCHf6vn+vyU9W8iIiiUA5/9NtR+Tmm2hZ3aWbURGvp3JCuP8gORtyu8ii1naNlO+f5d8h3RA 6Iw1j8h1wioKO6XEeAfbTbC4hvD/xBeBhCIUBEYjhlJ6F9F6Qek4yFur4sXat727T0+aHBaX RhRzesbfbWqTRG/zvAm1er7xJjroYUpIn7P+E8V9Yt0AG4Jybl2PNdIg==; From: Jan Kiszka To: Jonathan Cameron , linux-iio@vger.kernel.org, Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-kernel@vger.kernel.org, Bao Cheng Su , Chao Zeng , devicetree@vger.kernel.org Subject: [PATCH 3/3] iio: proximity: Add support for everlight pmd16d17 sensor Date: Tue, 13 Aug 2024 07:40:42 +0200 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Flowmailer-Platform: Siemens Feedback-ID: 519:519-294854:519-21489:flowmailer From: Chao Zeng Add initial support for everlight pm16d17 proximity sensor. Signed-off-by: Chao Zeng Co-developed-by: Baocheng Su Signed-off-by: Baocheng Su Signed-off-by: Jan Kiszka --- drivers/iio/proximity/Kconfig | 11 ++ drivers/iio/proximity/Makefile | 1 + drivers/iio/proximity/pm16d17.c | 324 ++++++++++++++++++++++++++++++++ 3 files changed, 336 insertions(+) create mode 100644 drivers/iio/proximity/pm16d17.c diff --git a/drivers/iio/proximity/Kconfig b/drivers/iio/proximity/Kconfig index 2ca3b0bc5eba..4c26bc3a0390 100644 --- a/drivers/iio/proximity/Kconfig +++ b/drivers/iio/proximity/Kconfig @@ -96,6 +96,17 @@ config PING To compile this driver as a module, choose M here: the module will be called ping. +config PM16D17 + tristate "PM16D17 proximity sensor" + select REGMAP_I2C + depends on I2C + help + Say Y here to build a driver for the Everlight Devices + PM16D17 proximity sensor. + + To compile this driver as a module, choose M here: the + module will be called pm16d17. + config RFD77402 tristate "RFD77402 ToF sensor" depends on I2C diff --git a/drivers/iio/proximity/Makefile b/drivers/iio/proximity/Makefile index f36598380446..e41bba9c7cd3 100644 --- a/drivers/iio/proximity/Makefile +++ b/drivers/iio/proximity/Makefile @@ -11,6 +11,7 @@ obj-$(CONFIG_ISL29501) += isl29501.o obj-$(CONFIG_LIDAR_LITE_V2) += pulsedlight-lidar-lite-v2.o obj-$(CONFIG_MB1232) += mb1232.o obj-$(CONFIG_PING) += ping.o +obj-$(CONFIG_PM16D17) += pm16d17.o obj-$(CONFIG_RFD77402) += rfd77402.o obj-$(CONFIG_SRF04) += srf04.o obj-$(CONFIG_SRF08) += srf08.o diff --git a/drivers/iio/proximity/pm16d17.c b/drivers/iio/proximity/pm16d17.c new file mode 100644 index 000000000000..94f21fc5e2fb --- /dev/null +++ b/drivers/iio/proximity/pm16d17.c @@ -0,0 +1,324 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) Siemens AG, 2023-2024 + * + * Driver for Everlight PM-16d17 proximity sensor + * + * Author: Chao Zeng + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PM16D17_DRV_NAME "pm16d17" +#define PM16D17_REGMAP_NAME "pm16d17_regmap" + +/* Registers Address */ +#define PM16D17_OP_MODE 0x00 +#define PM16D17_INTERRUPT_FLAG 0x01 +#define PM16D17_PS_SETTING 0x0A +#define PM16D17_VCSEL_DRIVE_CURRENT 0x0B +#define PM16D17_VCSEL_DRIVE_PULSE 0x0C +#define PM16D17_PS_INTUPT_LTHD_L 0x0D +#define PM16D17_PS_INTUPT_LTHD_H 0x0E +#define PM16D17_PS_INTUPT_HTHD_L 0x0F +#define PM16D17_PS_INTUPT_HTHD_H 0x10 +#define PM16D17_PS_DATA_L 0x11 +#define PM16D17_PS_DATA_H 0x12 +#define PM16D17_PS_SETTING2 0x13 +#define PM16D17_PS_OFFSET_CANCEL_L 0x14 +#define PM16D17_PS_OFFSET_CANCEL_H 0x15 +#define PM16D17_DEV_ID 0x18 + +#define DEVICE_ID 0x11 + +#define ENABLE_PS_FUNCTION BIT(3) +#define PS_GAIN_MASK GENMASK(7, 6) +#define PS_ITIME_MASK GENMASK(5, 3) +#define PS_WTIME_MASK GENMASK(2, 0) +#define OFFSET_CANCEL_ENABLE BIT(7) +#define PS_OFFSET_CANCEL_LSB_MASK GENMASK(7, 0) +#define PS_OFFSET_CANCEL_MSB_MASK GENMASK(15, 8) + +enum { + PITIME_0_POINT_4_MS = (0 << 3), + PITIME_0_POINT_8_MS = (1 << 3), + PITIME_1_POINT_6_MS = (2 << 3), + PITIME_3_POINT_2_MS = (3 << 3), + PITIME_6_POINT_3_MS = (4 << 3), + PITIME_12_POINT_6_MS = (5 << 3), + PITIME_25_POINT_2_MS = (6 << 3), +}; + +enum { + PWTIME_12_POINT_5_MS = 0, + PWTIME_25_MS, + PWTIME_50_MS, + PWTIME_100_MS, + PWTIME_200_MS, + PWTIME_400_MS, + PWTIME_800_MS, + PWTIME_1600_MS, +}; + +struct pm16d17_data { + struct i2c_client *client; + struct regmap *regmap; +}; + +static const struct regmap_config pm16d17_regmap_config = { + .name = PM16D17_REGMAP_NAME, + .reg_bits = 8, + .val_bits = 8, + .cache_type = REGCACHE_NONE, +}; + +static const struct iio_chan_spec pm16d17_channels[] = { + { + .type = IIO_PROXIMITY, + .indexed = 1, + .channel = 0, + .scan_index = -1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + } +}; + +static inline int pm16d17_write_reg(struct pm16d17_data *data, + unsigned int reg, + unsigned int value) +{ + return regmap_write(data->regmap, reg, value); +} + +static inline unsigned int pm16d17_read_reg(struct pm16d17_data *data, + unsigned int reg, + unsigned int *reg_val) +{ + return regmap_read(data->regmap, reg, reg_val); +} + +static int pm16d17_read_raw(struct iio_dev *indio_dev, + struct iio_chan_spec const *chan, + int *val, int *val2, long mask) +{ + struct pm16d17_data *data = iio_priv(indio_dev); + unsigned int ps_data_l; + unsigned int ps_data_h; + uint16_t ps_data; + int ret = -EINVAL; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + switch (chan->type) { + case IIO_PROXIMITY: + ret = pm16d17_read_reg(data, PM16D17_PS_DATA_L, &ps_data_l); + if (ret < 0) + return ret; + + ret = pm16d17_read_reg(data, PM16D17_PS_DATA_H, &ps_data_h); + if (ret < 0) + return ret; + + ps_data = (ps_data_h << 8) | ps_data_l; + + dev_dbg(&data->client->dev, "PS data: %x\n", ps_data); + + *val = ps_data; + ret = IIO_VAL_INT; + break; + default: + break; + } + default: + break; + } + + return ret; +} + +static const struct iio_info pm16d17_info = { + .read_raw = pm16d17_read_raw, +}; + +static int pm16d17_chip_init(struct pm16d17_data *data) +{ + struct i2c_client *client = data->client; + struct device_node *np = client->dev.of_node; + const char *conv_time = NULL; + const char *wait_time = NULL; + uint8_t op_mode_setting_val; + uint32_t ps_offset_cancel; + uint8_t offset_lsb; + uint8_t offset_msb; + uint32_t pulse_count; + uint32_t pgain; + unsigned int val; + int ret; + + ret = pm16d17_read_reg(data, PM16D17_DEV_ID, &val); + + if (ret < 0 || (val != DEVICE_ID)) { + dev_err(&client->dev, "Invalid chip id 0x%04x\n", val); + return -ENODEV; + } + + dev_dbg(&client->dev, "Detected PM16D17 with chip id: 0x%04x\n", val); + + ret = pm16d17_write_reg(data, PM16D17_OP_MODE, ENABLE_PS_FUNCTION); + if (ret < 0) + return ret; + + of_property_read_u32(np, "ps-gain", &pgain); + switch (pgain) { + case 1: + case 2: + case 4: + case 8: + op_mode_setting_val |= (ilog2(pgain) << 6) & PS_GAIN_MASK; + break; + default: + break; + } + + of_property_read_string(np, "ps-itime", &conv_time); + if (strcmp(conv_time, "0.4") == 0) + op_mode_setting_val |= PITIME_0_POINT_4_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "0.8") == 0) + op_mode_setting_val |= PITIME_0_POINT_8_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "1.6") == 0) + op_mode_setting_val |= PITIME_1_POINT_6_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "3.2") == 0) + op_mode_setting_val |= PITIME_3_POINT_2_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "6.3") == 0) + op_mode_setting_val |= PITIME_6_POINT_3_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "12.6") == 0) + op_mode_setting_val |= PITIME_12_POINT_6_MS & PS_ITIME_MASK; + else if (strcmp(conv_time, "25.2") == 0) + op_mode_setting_val |= PITIME_25_POINT_2_MS & PS_ITIME_MASK; + else { + dev_info(&client->dev, "Using default ps itime value\n"); + op_mode_setting_val |= PITIME_0_POINT_4_MS & PS_ITIME_MASK; + } + + of_property_read_string(np, "ps-wtime", &wait_time); + if (strcmp(wait_time, "12.5") == 0) + op_mode_setting_val |= PWTIME_12_POINT_5_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "25") == 0) + op_mode_setting_val |= PWTIME_25_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "50") == 0) + op_mode_setting_val |= PWTIME_50_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "100") == 0) + op_mode_setting_val |= PWTIME_100_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "200") == 0) + op_mode_setting_val |= PWTIME_200_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "400") == 0) + op_mode_setting_val |= PWTIME_400_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "800") == 0) + op_mode_setting_val |= PWTIME_800_MS & PS_WTIME_MASK; + else if (strcmp(wait_time, "1600") == 0) + op_mode_setting_val |= PWTIME_1600_MS & PS_WTIME_MASK; + else { + dev_info(&client->dev, "Using default ps wtime value\n"); + op_mode_setting_val |= PWTIME_12_POINT_5_MS & PS_WTIME_MASK; + } + + ret = pm16d17_write_reg(data, PM16D17_PS_SETTING, op_mode_setting_val); + if (ret < 0) + return ret; + + of_property_read_u32(np, "ps-ir-led-pulse-count", &pulse_count); + if (pulse_count > 256) + pulse_count = 256; + ret = pm16d17_write_reg(data, PM16D17_VCSEL_DRIVE_PULSE, pulse_count - 1); + if (ret < 0) + return ret; + + of_property_read_u32(np, "ps-offset-cancel", &ps_offset_cancel); + if (ps_offset_cancel != 0) { + ret = pm16d17_write_reg(data, PM16D17_PS_SETTING2, OFFSET_CANCEL_ENABLE); + if (ret < 0) + return ret; + + offset_lsb = ps_offset_cancel & PS_OFFSET_CANCEL_LSB_MASK; + offset_msb = (ps_offset_cancel & PS_OFFSET_CANCEL_MSB_MASK) >> 8; + + ret = pm16d17_write_reg(data, PM16D17_PS_OFFSET_CANCEL_L, offset_lsb); + if (ret < 0) + return ret; + + ret = pm16d17_write_reg(data, PM16D17_PS_OFFSET_CANCEL_H, offset_msb); + if (ret < 0) + return ret; + } + + return 0; +} + +static int pm16d17_probe(struct i2c_client *client) +{ + struct pm16d17_data *data; + struct iio_dev *indio_dev; + int ret; + + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); + if (!indio_dev) + return -ENOMEM; + + indio_dev->dev.parent = &client->dev; + indio_dev->info = &pm16d17_info; + indio_dev->name = PM16D17_DRV_NAME; + indio_dev->channels = pm16d17_channels; + indio_dev->num_channels = ARRAY_SIZE(pm16d17_channels); + indio_dev->modes = INDIO_DIRECT_MODE; + + data = iio_priv(indio_dev); + data->client = client; + + data->regmap = devm_regmap_init_i2c(client, &pm16d17_regmap_config); + if (IS_ERR(data->regmap)) { + dev_err(&client->dev, "regmap initialization failed.\n"); + return PTR_ERR(data->regmap); + } + + ret = pm16d17_chip_init(data); + if (ret) + return ret; + + return devm_iio_device_register(&client->dev, indio_dev); +} + +static const struct i2c_device_id pm16d17_id[] = { + {"pm16d17", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, pm16d17_id); + +static const struct of_device_id pm16d17_of_match[] = { + { .compatible = "everlight,pm16d17" }, + {} +}; +MODULE_DEVICE_TABLE(of, pm16d17_of_match); + +static struct i2c_driver pm16d17_driver = { + .driver = { + .name = PM16D17_DRV_NAME, + .of_match_table = pm16d17_of_match, + }, + .probe = pm16d17_probe, + .id_table = pm16d17_id, +}; +module_i2c_driver(pm16d17_driver); + +MODULE_AUTHOR("Chao Zeng "); +MODULE_DESCRIPTION("PM16D17 proximity sensor"); +MODULE_LICENSE("GPL");