From nobody Thu Nov 28 01:25:16 2024 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C217C156C71; Sat, 5 Oct 2024 15:53:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143584; cv=none; b=mC8gc3ExjyUe2q2NzmYomr35fFk5VDyfgOmpbgmChTsQG3Nu6jfexdUR5gEx1R6S06LLJg93U4/qHsTudFUNOmUraZbyrKpoRUmRIt7Fx3dO4Fha2Dc+41XSqVb6rEdAQMFZxxAHG1v3N3wghLCzQvPE7qu9Ohq0bN9jJdwJR8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143584; c=relaxed/simple; bh=YM+iaVcsEE3ZAS7IL3uC3MHg8FPw65HssdDPUopdi3g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MPTR1Ur07Dl6ZSV6sQG6UJ0UsYGd0lmUsnsKDTLnVA/SxBNrz/7zS2a8tIZ7xLMTFUGLAyEV7RuF8lnAydqJU+uZlzUutkmBn2WnS98RFkL/2gCs1vZ6K0E+vlaObftJbUJHi/WPB8IN+V3zjOdzCoWflAj2hFbnmFbN88hB4kk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=WaL2tGqy; arc=none smtp.client-ip=209.85.214.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WaL2tGqy" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20b7463dd89so34009935ad.2; Sat, 05 Oct 2024 08:53:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728143581; x=1728748381; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nlFeKsIuSxzj6ce1JKc3eZHCtZGbmTVgezzcVvunKfA=; b=WaL2tGqykQZ3OCmanzXyUk6khSyiu0kX4Q/IHLqDgV/KNNpVmA6XrRcUMrsn3G1TnN wbs6txZ1cfups+Ljqa2ZffUjr+lXPBlUdPZoJ0qrtVtx2CPZVCgaE3WZo3hfHZQN7l97 vBx0BPWAJFgiPqqsuy4uf0G1xFBgNfctu3eAB/7IPf5XF3oX97TrGQDLg79GkVoKOdCA chWlKc88yrsuJimwDhF7GbbWStbiVmW6sMxOlK3nBw9S08+BIsxW5Z1XDIIZJBvNjnPX o3jgtBI4IdL9Gsk04w9cDGOKiwAzgw6zDLasc6aF4mMDO4E0tWkhV8jxMmrPtxOqT4fJ 1GnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728143581; x=1728748381; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nlFeKsIuSxzj6ce1JKc3eZHCtZGbmTVgezzcVvunKfA=; b=o8aIWIVe+NzwXVSlIRckZWhRuokCu9rBNdNtyRl8tHvcc5deQuJFxLpUmOR8ZoYxHU pJs/JbKYzt1nlfBe1ankJ9yrvTRKUvrI5vzOIabXnsZmY4KTmPcEgv7iYCr4isOamws4 7Y9qgiOTgiuMEwWCGvkMNuX5rRakFuG8vEph7lOgejRfVAlNfXMdhx48rdt145OqrLjB 3Komv4GjhiM0YUOxZoINHTFM1dW2uYDTghAGNhQUUZFsM89Dyxb48xPLwU1gQUF2ic2W foL8i+QMJJ3mCMx3PsGS8mvDbG8HJoQJgXC+mRX0cqZmqsuiAfiiuXi5VXp7sXSn7krO aHyw== X-Forwarded-Encrypted: i=1; AJvYcCVkGjAwWlALOOVkmVqNzI/qVexmlyl4vkxIm+m/Umw8Mv84MXSeWY+eG7dE0iC3eLNihlBEAAHdqz6RT4E=@vger.kernel.org X-Gm-Message-State: AOJu0YzQmkS1XDYHspDnzuFAcshcfVQHACmzRAOcAcNBsttNrJ4zUe05 +OG9RcJFGWP5EJEkP9X4dSSHzS7rSH1Tk8zvxzaUxGhw5tZlWJNSunyuBhVzUeI= X-Google-Smtp-Source: AGHT+IHpbyCu6rFkjCEMdh9DbxkkhaEuterkVGU7E7W9/k0mzj66nhE7f/8g1m/RBBD9U4aVMBrUhQ== X-Received: by 2002:a17:902:f68d:b0:20b:bad4:5b95 with SMTP id d9443c01a7336-20bfdfb10b7mr88305535ad.13.1728143580813; Sat, 05 Oct 2024 08:53:00 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20aebc40csm2008370a91.23.2024.10.05.08.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 08:53:00 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 1/3] iio: light: vl6180: Add configurable inter-measurement period support Date: Sat, 5 Oct 2024 21:22:23 +0530 Message-ID: <20241005155225.9239-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005155225.9239-1-abhashkumarjha123@gmail.com> References: <20241005155225.9239-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose the IIO_CHAN_INFO_SAMP_FREQ attribute as a way to configure the inter-measurement period for both the IIO_DISTANCE and IIO_LIGHT channels. The inter-measurement period must be given in miliseconds. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a1b2b3c0b..401f13411 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -38,7 +38,9 @@ #define VL6180_OUT_OF_RESET 0x016 #define VL6180_HOLD 0x017 #define VL6180_RANGE_START 0x018 +#define VL6180_RANGE_INTER_MEAS_TIME 0x01b #define VL6180_ALS_START 0x038 +#define VL6180_ALS_INTER_MEAS_TIME 0x03e #define VL6180_ALS_GAIN 0x03f #define VL6180_ALS_IT 0x040 =20 @@ -86,6 +88,8 @@ struct vl6180_data { struct mutex lock; unsigned int als_gain_milli; unsigned int als_it_ms; + unsigned int als_meas_rate; + unsigned int range_meas_rate; }; =20 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; @@ -261,12 +265,14 @@ static const struct iio_chan_spec vl6180_channels[] = =3D { .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | - BIT(IIO_CHAN_INFO_HARDWAREGAIN), + BIT(IIO_CHAN_INFO_HARDWAREGAIN) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type =3D IIO_DISTANCE, .address =3D VL6180_RANGE, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type =3D IIO_PROXIMITY, .address =3D VL6180_PROX, @@ -333,6 +339,18 @@ static int vl6180_read_raw(struct iio_dev *indio_dev, =20 return IIO_VAL_FRACTIONAL; =20 + case IIO_CHAN_INFO_SAMP_FREQ: + switch (chan->type) { + case IIO_DISTANCE: + *val =3D data->range_meas_rate; + return IIO_VAL_INT; + case IIO_LIGHT: + *val =3D data->als_meas_rate; + return IIO_VAL_INT; + default: + return -EINVAL; + } + default: return -EINVAL; } @@ -412,12 +430,24 @@ static int vl6180_set_it(struct vl6180_data *data, in= t val, int val2) return ret; } =20 +static int vl6180_meas_reg_val_from_ms(unsigned int period) +{ + unsigned int reg_val =3D 0; + + if (period > 10) + reg_val =3D period < 2550 ? (DIV_ROUND_CLOSEST(period, 10) - 1) : 254; + + return reg_val; +} + static int vl6180_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask) { struct vl6180_data *data =3D iio_priv(indio_dev); + unsigned int reg_val; =20 + guard(mutex)(&data->lock); switch (mask) { case IIO_CHAN_INFO_INT_TIME: return vl6180_set_it(data, val, val2); @@ -427,6 +457,22 @@ static int vl6180_write_raw(struct iio_dev *indio_dev, return -EINVAL; =20 return vl6180_set_als_gain(data, val, val2); + + case IIO_CHAN_INFO_SAMP_FREQ: + switch (chan->type) { + case IIO_DISTANCE: + data->range_meas_rate =3D val; + reg_val =3D vl6180_meas_reg_val_from_ms(val); + return vl6180_write_byte(data->client, + VL6180_RANGE_INTER_MEAS_TIME, reg_val); + + case IIO_LIGHT: + data->als_meas_rate =3D val; + reg_val =3D vl6180_meas_reg_val_from_ms(val); + return vl6180_write_byte(data->client, + VL6180_ALS_INTER_MEAS_TIME, reg_val); + } + default: return -EINVAL; } @@ -473,6 +519,22 @@ static int vl6180_init(struct vl6180_data *data) if (ret < 0) return ret; =20 + /* Default Range inter-measurement time: 50ms + * reg_val =3D (50 / 10 - 1) =3D 4 + */ + ret =3D vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, 4); + if (ret < 0) + return ret; + data->range_meas_rate =3D 50; + + /* Default ALS inter-measurement time: 10ms + * reg_val =3D (10 / 10 - 1) =3D 0 + */ + ret =3D vl6180_write_byte(client, VL6180_ALS_INTER_MEAS_TIME, 0); + if (ret < 0) + return ret; + data->als_meas_rate =3D 10; + /* ALS integration time: 100ms */ data->als_it_ms =3D 100; ret =3D vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); --=20 2.43.0 From nobody Thu Nov 28 01:25:16 2024 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BC63156F54; Sat, 5 Oct 2024 15:53:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143586; cv=none; b=FsC4xv8iz4Rjtxrgo21Ufs65tZF/zbi6HXChpe7NSGgGXFpiRF5Lp93wwMu24mqyAlee2uEDVnxJWMQWh9UTD++0am8BCl7B7uvW5rg9fQqnA8tUEQMkL97HzZL6UQbS9xB6ktyFV+hzEeyXqNU70ESPHJbS2GyZRF3ccpC6mNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143586; c=relaxed/simple; bh=JBqlXJK+sYEsYqPlK4tM6IeWeNFc+TGZbPwndM1P41g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rUrYfMGwBLt2LncaYeDkAmHBorUvOL+rPivhMhYmxhtXdIo2+8mQVUIlyVVOAf47I90nX2D3MSI143WILl6hr3tJpp0wbZBvZkjBD0gpifBuQV9pff9KxF6tLVRM6z2YUfc89VlGafoyEq/kJ3doBxaqOTcSpX8vZ0N/U+JG+gk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=lwtymgmc; arc=none smtp.client-ip=209.85.215.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="lwtymgmc" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7e9e38dd5f1so1932800a12.0; Sat, 05 Oct 2024 08:53:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728143584; x=1728748384; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7SqI97S0ywB/WLJhr+9ChW9idec52eD6V6moNdqzpdU=; b=lwtymgmcj637X9wbix2jq8j1Nscgb2RFuvmGlQwUaVnS9kbyp9NGhBp9jRXokFHfqT gredEaDli7Rw8+GC7sewW2viQzIopa38ujWeAKXEKgZ8/5hfAXZHU40kr/jSgL/5yWfp U7WqcptVtB1HGESPcSBzpvVL5TRGhU9aUorUgixoUauv6Hfmh43kGp2xne1pOjKJuXpI HfYnGALcdyav4M5W4esFlXTwkiRgNvqcAtDVF47J4hlfdKUA3Y2N8dd4I5HJ5djQCV88 ZPK6pCtC0YhzSGNvGyfH4VF5zIjzGRVnfTP5CJksXETRxDoC+CeLO8DrUPDzLwSIc4zh 3gLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728143584; x=1728748384; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7SqI97S0ywB/WLJhr+9ChW9idec52eD6V6moNdqzpdU=; b=MDhGFoAylPUkMvXv1wGrnaWNhsqVom+V1HaKRuW+aNR3ZUF1cAHBz5ikjx1E0HP3xu s1ZI+fPdUVUyzTKY72wv3/X3rREVOBSA2aCmGkFEVknJPmGo+dUm4s2Dzh3RgREpgHEG koVmXCvqSAsnpDAJHJk5RVmd5xtp4RCobURU4QCYDpaUvpo5JDYli9rIG/KxylLZWHDu PnimSlcfZub5+4y/WCmtb96CYbY+aT3jQuzUBbnWBlRFuIIclO4bog1P/URJyG8/VcV7 2+S++7rFSl8xULVWp8LV4v45OvfMNXw8h2kM1/NdzHMM/5NZBLQbxE1rZrEAxhACjQLE bGQA== X-Forwarded-Encrypted: i=1; AJvYcCU3/HcxWJP2gAAU+cNf/pEXgF+gxJGGOlC8vjtpWf/NLXu9lwj/jnZvX9qYAgLCVK9UoXUKgrJPwg1NoSw=@vger.kernel.org X-Gm-Message-State: AOJu0YwaVet8ehaMtY7XGbOmyHgluXkg2+d2YtTKssbYy6nH0S/kAK1p zqEiMRmr2NpbH88ZCcnp4q8PsAvp2jD0tLissYNyhtPXC4FV0B3yXSgA2duHlj4= X-Google-Smtp-Source: AGHT+IGmX/Y5Nn/D+9Dgd1fi5rbYMn6bnYjPv+H8S9zdxrz4eeR4wEDPd9VTtVytIy0D9wSIAHXZSw== X-Received: by 2002:a17:90b:4b50:b0:2e0:d693:7884 with SMTP id 98e67ed59e1d1-2e1e620ea80mr9793642a91.5.1728143583780; Sat, 05 Oct 2024 08:53:03 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20aebc40csm2008370a91.23.2024.10.05.08.53.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 08:53:03 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 2/3] iio: light: vl6180: Added Interrupt support for single shot access Date: Sat, 5 Oct 2024 21:22:24 +0530 Message-ID: <20241005155225.9239-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005155225.9239-1-abhashkumarjha123@gmail.com> References: <20241005155225.9239-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Configured the GPIO1 pin to provide output interrupts. And then the interrupts are serviced in the `vl6180_measure` function when the irq_handler signals that the reading is complete. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 64 +++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index 401f13411..7630a7503 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -33,6 +33,7 @@ #define VL6180_MODEL_ID_VAL 0xb4 =20 /* Configuration registers */ +#define VL6180_MODE_GPIO1 0x011 #define VL6180_INTR_CONFIG 0x014 #define VL6180_INTR_CLEAR 0x015 #define VL6180_OUT_OF_RESET 0x016 @@ -70,6 +71,9 @@ /* bits of the HOLD register */ #define VL6180_HOLD_ON BIT(0) =20 +/* bit for enabling GPIO1 Interrupt Output */ +#define VL6180_GPIO1_INTR_OUT BIT(4) + /* default value for the ALS_IT register */ #define VL6180_ALS_IT_100 0x63 /* 100 ms */ =20 @@ -86,6 +90,7 @@ struct vl6180_data { struct i2c_client *client; struct mutex lock; + struct completion completion; unsigned int als_gain_milli; unsigned int als_it_ms; unsigned int als_meas_rate; @@ -211,6 +216,7 @@ static int vl6180_write_word(struct i2c_client *client,= u16 cmd, u16 val) static int vl6180_measure(struct vl6180_data *data, int addr) { struct i2c_client *client =3D data->client; + unsigned long time_left; int tries =3D 20, ret; u16 value; =20 @@ -221,19 +227,26 @@ static int vl6180_measure(struct vl6180_data *data, i= nt addr) if (ret < 0) goto fail; =20 - while (tries--) { - ret =3D vl6180_read_byte(client, VL6180_INTR_STATUS); - if (ret < 0) - goto fail; - - if (ret & vl6180_chan_regs_table[addr].drdy_mask) - break; - msleep(20); - } + if (client->irq) { + reinit_completion(&data->completion); + time_left =3D wait_for_completion_timeout(&data->completion, HZ / 10); + if (time_left =3D=3D 0) + return -ETIMEDOUT; + } else { + while (tries--) { + ret =3D vl6180_read_byte(client, VL6180_INTR_STATUS); + if (ret < 0) + goto fail; + + if (ret & vl6180_chan_regs_table[addr].drdy_mask) + break; + msleep(20); + } =20 - if (tries < 0) { - ret =3D -EIO; - goto fail; + if (tries < 0) { + ret =3D -EIO; + goto fail; + } } =20 /* Read result value from appropriate registers */ @@ -478,6 +491,15 @@ static int vl6180_write_raw(struct iio_dev *indio_dev, } } =20 +static irqreturn_t vl6180_threaded_irq(int irq, void *priv) +{ + struct iio_dev *indio_dev =3D priv; + struct vl6180_data *data =3D iio_priv(indio_dev); + + complete(&data->completion); + return IRQ_HANDLED; +} + static const struct iio_info vl6180_info =3D { .read_raw =3D vl6180_read_raw, .write_raw =3D vl6180_write_raw, @@ -513,6 +535,11 @@ static int vl6180_init(struct vl6180_data *data) if (ret !=3D 0x01) dev_info(&client->dev, "device is not fresh out of reset\n"); =20 + ret =3D vl6180_write_byte(client, VL6180_MODE_GPIO1, + VL6180_GPIO1_INTR_OUT); + if (ret < 0) + return ret; + /* Enable ALS and Range ready interrupts */ ret =3D vl6180_write_byte(client, VL6180_INTR_CONFIG, VL6180_ALS_READY | VL6180_RANGE_READY); @@ -579,6 +606,19 @@ static int vl6180_probe(struct i2c_client *client) if (ret < 0) return ret; =20 + if (client->irq) { + ret =3D devm_request_threaded_irq(&client->dev, client->irq, + NULL, vl6180_threaded_irq, + IRQF_ONESHOT, + indio_dev->name, indio_dev); + if (ret) { + dev_err(&client->dev, "devm_request_irq error: %d\n", ret); + return ret; + } + + init_completion(&data->completion); + } + return devm_iio_device_register(&client->dev, indio_dev); } =20 --=20 2.43.0 From nobody Thu Nov 28 01:25:16 2024 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C76D615854F; Sat, 5 Oct 2024 15:53:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143590; cv=none; b=UZWz0VUj+CRfa97lDTJv7s5ktco3d/aYy6CpIG9WRuLunsQlrubwKrNIMBteyKgDDXjVa/KsUBwIX+eb4Wcwr+EOaUDMwXeFFFkeltoVKITIVdWYIHs/1vodqBZbb5ezi6bBLkZnhkO4mvGL+HwfY5E/U40VF7g6bNTxVFIJlVk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728143590; c=relaxed/simple; bh=Fit/7+70VUBcPnUV9WaI7RcQB219ex1h4vG8en2Yru0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lJPa5aP6fN1mZcv87MkoQDWixcyaqeHe8W5j0Cp5Q4gJSxwuIkXfhgo+IYZYdzdEEvnKVx+T0o0U+xt+7b+MeX3B24mtkOjqL0kC5e7Q2QClJMomP0gu7R7cMN/Sch/nPXEcqa+n80s5y6RcGHUBQew8gThm4AXEexsr6mfh4F0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=NB+LmiuG; arc=none smtp.client-ip=209.85.216.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NB+LmiuG" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2e078d28fe9so2201971a91.2; Sat, 05 Oct 2024 08:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728143587; x=1728748387; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TE9IGHKdaTgYIuAo7IgiEznxfsxfMWkmrqG2JFBMfBo=; b=NB+LmiuG3F82GZnb8Tzc04O/ZP5J1kmAztaX3HLqpeb0h0BGHLQZAU0rOno8r3I9cK TNzJVbIFnhNvhF3+A4fl1ZuQQpbtpBUoPbubtFL81AIQSzwonmOzL7yw+dhdpeJlNfHj 15obzZ5TGdHHx+TE+EXGAn1/KWiDQ6zq7jlkgtYFMhaT4kO3NNTc6z5qV6a07xKtONxb X9KUGfovIHQ1h6XOD8OzHqALvcTZzNPyhlBWX5zB4VdTUKC3TtN4E0zbIEkmgFmXe36S zxg65DcN9Mj37zEnSXMKNFcfN+Ue4hLDDuOhuWSn6ljIv6uzxc6mvhwBglxgTvuD9xYf EU3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728143587; x=1728748387; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TE9IGHKdaTgYIuAo7IgiEznxfsxfMWkmrqG2JFBMfBo=; b=pYG6sjeYW+UaGaMUaDokSHgzyqSdVJHmHezz4cwfppiGvsx2ZQ68bYnujBxudlKT0v 1SgYT2X0/GFstpp7vqTgcodGCq2GOI89c1LpQk2Z9ATf93QjR0BaFBCdedXLyjLIVEAN 1tUPdmrjUUePf/ZEkK5yz6c1cfxozCxQoh1I1hNwoW6t6X2NEM7vIh4DGKaEKL4iF0N7 ew3booNNhg1xCZcFFKBfxLwHW6JX1t9pwklhXQLmIZ1H0ZWTfWm9mUVMpe2iwUMGH+1Z PJj3JX67CiAWlbzAWpoSheQcAGKPchRHVNYVOw/6llizk61M0G0btxOgARfSjyS/I9tL 7Z5A== X-Forwarded-Encrypted: i=1; AJvYcCX/h9Skn2Rmp0orHQ307OzZxtWWIpSMqwieI7MkMin/aen2rpJFhtYcjXbKDT1VZJPlOJO4EQ7s0UiRh2Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyfnObF45TGMX10Hgd4or6bfDv7kP0+lDNvCcjWbSJkB/E6T2B+ Tb2K8bS/az315ij67eijbVtdyFzHw82osZ5/mrW3jLoCLsXJbR7iuzrK3337qUk= X-Google-Smtp-Source: AGHT+IGAKi0OtMMtNkrRd7Q7988lPCmzLMrqb3e2UAyznKK5l57XDJ83GpLhVet/SipoVFuZq7iOmg== X-Received: by 2002:a17:90b:151:b0:2d3:c892:9607 with SMTP id 98e67ed59e1d1-2e1e621e995mr7497453a91.12.1728143586944; Sat, 05 Oct 2024 08:53:06 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 98e67ed59e1d1-2e20aebc40csm2008370a91.23.2024.10.05.08.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 08:53:06 -0700 (PDT) From: Abhash Jha To: linux-iio@vger.kernel.org Cc: jic23@kernel.org, lars@metafoo.de, linux-kernel@vger.kernel.org, Abhash Jha Subject: [PATCH v2 3/3] iio: light: vl6180: Add support for Continuous Mode Date: Sat, 5 Oct 2024 21:22:25 +0530 Message-ID: <20241005155225.9239-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005155225.9239-1-abhashkumarjha123@gmail.com> References: <20241005155225.9239-1-abhashkumarjha123@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Added support for getting continuous readings from vl6180 using triggered buffer approach. The continuous mode can be enabled by enabling the buffer. Also added a trigger and appropriate checks to see that it is used with this device. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 138 +++++++++++++++++++++++++++++++++++-- 1 file changed, 134 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index 7630a7503..74be3e6dc 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -25,6 +25,10 @@ =20 #include #include +#include +#include +#include +#include =20 #define VL6180_DRV_NAME "vl6180" =20 @@ -91,10 +95,16 @@ struct vl6180_data { struct i2c_client *client; struct mutex lock; struct completion completion; + struct iio_trigger *trig; unsigned int als_gain_milli; unsigned int als_it_ms; unsigned int als_meas_rate; unsigned int range_meas_rate; + + struct { + u16 chan; + aligned_u64 timestamp; + } scan; }; =20 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; @@ -275,6 +285,12 @@ static const struct iio_chan_spec vl6180_channels[] = =3D { { .type =3D IIO_LIGHT, .address =3D VL6180_ALS, + .scan_index =3D VL6180_ALS, + .scan_type =3D { + .sign =3D 'u', + .realbits =3D 16, + .storagebits =3D 16, + }, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) | @@ -283,14 +299,27 @@ static const struct iio_chan_spec vl6180_channels[] = =3D { }, { .type =3D IIO_DISTANCE, .address =3D VL6180_RANGE, + .scan_index =3D VL6180_RANGE, + .scan_type =3D { + .sign =3D 'u', + .realbits =3D 8, + .storagebits =3D 8, + }, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | BIT(IIO_CHAN_INFO_SAMP_FREQ), }, { .type =3D IIO_PROXIMITY, .address =3D VL6180_PROX, + .scan_index =3D VL6180_PROX, + .scan_type =3D { + .sign =3D 'u', + .realbits =3D 16, + .storagebits =3D 16, + }, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW), - } + }, + IIO_CHAN_SOFT_TIMESTAMP(3), }; =20 /* @@ -496,17 +525,99 @@ static irqreturn_t vl6180_threaded_irq(int irq, void = *priv) struct iio_dev *indio_dev =3D priv; struct vl6180_data *data =3D iio_priv(indio_dev); =20 - complete(&data->completion); + if (iio_buffer_enabled(indio_dev)) + iio_trigger_poll_nested(indio_dev->trig); + else + complete(&data->completion); + return IRQ_HANDLED; } =20 +static irqreturn_t vl6180_trigger_handler(int irq, void *priv) +{ + struct iio_poll_func *pf =3D priv; + struct iio_dev *indio_dev =3D pf->indio_dev; + struct vl6180_data *data =3D iio_priv(indio_dev); + int ret; + + for (int i =3D 0; i < indio_dev->masklength; i++) { + if (test_bit(i, indio_dev->active_scan_mask)) { + + ret =3D vl6180_chan_regs_table[i].word ? + vl6180_read_word(data->client, vl6180_chan_regs_table[i].value_reg) : + vl6180_read_byte(data->client, vl6180_chan_regs_table[i].value_reg); + if (ret < 0) + dev_err(&data->client->dev, "failed to read from value regs: %d\n", ret= ); + + data->scan.chan =3D ret; + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, + iio_get_time_ns(indio_dev)); + } + } + + iio_trigger_notify_done(indio_dev->trig); + + /* Clear the interrupt flag after data read */ + ret =3D vl6180_write_byte(data->client, VL6180_INTR_CLEAR, + VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); + if (ret < 0) + dev_err(&data->client->dev, "failed to clear irq: %d\n", ret); + + return IRQ_HANDLED; +} + +static int vl6180_validate_trigger(struct iio_dev *indio_dev, struct iio_t= rigger *trig) +{ + struct vl6180_data *data =3D iio_priv(indio_dev); + + return data->trig =3D=3D trig ? 0 : -EINVAL; +} + static const struct iio_info vl6180_info =3D { .read_raw =3D vl6180_read_raw, .write_raw =3D vl6180_write_raw, .attrs =3D &vl6180_attribute_group, + .validate_trigger =3D vl6180_validate_trigger, }; =20 -static int vl6180_init(struct vl6180_data *data) +static int vl6180_buffer_postenable(struct iio_dev *indio_dev) +{ + struct vl6180_data *data =3D iio_priv(indio_dev); + + for (int i =3D 0; i < indio_dev->masklength; i++) { + if (test_bit(i, indio_dev->active_scan_mask)) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[i].start_reg, + VL6180_MODE_CONT | VL6180_STARTSTOP); + } + + return -EINVAL; +} + +static int vl6180_buffer_postdisable(struct iio_dev *indio_dev) +{ + struct vl6180_data *data =3D iio_priv(indio_dev); + + for (int i =3D 0; i < indio_dev->masklength; i++) { + if (test_bit(i, indio_dev->active_scan_mask)) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[i].start_reg, + VL6180_STARTSTOP); + } + + return -EINVAL; +} + +static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = =3D { + .postenable =3D &vl6180_buffer_postenable, + .postdisable =3D &vl6180_buffer_postdisable, +}; + +static const struct iio_trigger_ops vl6180_trigger_ops =3D { + .validate_device =3D iio_trigger_validate_own_device, +}; + +static int vl6180_init(struct vl6180_data *data, struct iio_dev *indio_dev) { struct i2c_client *client =3D data->client; int ret; @@ -546,6 +657,12 @@ static int vl6180_init(struct vl6180_data *data) if (ret < 0) return ret; =20 + ret =3D devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, + &vl6180_trigger_handler, + &iio_triggered_buffer_setup_ops); + if (ret) + return ret; + /* Default Range inter-measurement time: 50ms * reg_val =3D (50 / 10 - 1) =3D 4 */ @@ -602,7 +719,7 @@ static int vl6180_probe(struct i2c_client *client) indio_dev->name =3D VL6180_DRV_NAME; indio_dev->modes =3D INDIO_DIRECT_MODE; =20 - ret =3D vl6180_init(data); + ret =3D vl6180_init(data, indio_dev); if (ret < 0) return ret; =20 @@ -617,6 +734,19 @@ static int vl6180_probe(struct i2c_client *client) } =20 init_completion(&data->completion); + + data->trig =3D devm_iio_trigger_alloc(&client->dev, "%s-dev%d", + indio_dev->name, iio_device_id(indio_dev)); + if (!data->trig) + return -ENOMEM; + + data->trig->ops =3D &vl6180_trigger_ops; + iio_trigger_set_drvdata(data->trig, indio_dev); + ret =3D devm_iio_trigger_register(&client->dev, data->trig); + if (ret) + return ret; + + indio_dev->trig =3D iio_trigger_get(data->trig); } =20 return devm_iio_device_register(&client->dev, indio_dev); --=20 2.43.0