From nobody Thu Nov 28 03:39:25 2024 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 7AFF41AA786; Fri, 4 Oct 2024 15:02:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054155; cv=none; b=J9k1nnUHn7uBinwfdKny62yfxwhn7+LT0e3sZOZKpuX4AdZDtRzN6plmkDiXlsyXPuvCKlxI34ymBnlWTtsFpK4KL6ZPrnSTq1lcnvJcD9MYjdnSs1IkihzVD8PKB41t7nVFzfNa8sdazr+s6MmV9Xi7st66YFSNsJKIScGFCdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054155; c=relaxed/simple; bh=8/jtRzU96krvO9eHA+xFxGTA0v868jE3gyBfKdrRvus=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nw8wDWwnCbIhDPOi33dAepU2yKng1V31NYmzUEDYfyltTS8SdqxzO1z6egayobEJfj/6DtEG+0qS53FRwv3upEPD1VHAEQVh7pPTOfCXfiNbaD+sCSQyTVonVoJKFsnmjm0HASQfdFjynbv5jR5vBfhwoOT5HzGweD83qe372W8= 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=VnOIpLCF; arc=none smtp.client-ip=209.85.216.52 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="VnOIpLCF" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e091682cfbso1735607a91.0; Fri, 04 Oct 2024 08:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728054153; x=1728658953; 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=hV/Shy1FZi7iJzQidTCMmnMOcmxQOiJp03mXnsnRSps=; b=VnOIpLCF7QZQdFQO1SJw51omFVQIaTYASormddjP2wh9LL7dLicokhfW1/+0NjdQ2T D+dKfYshBLxMi9+ogNzp/FMX1j+6jKYUGPmxKh6rhcQNmImNQqgKCj7cRZjpkWDQBpD4 WfLtSa3JnUg5e0CrN5qq5rTCEtPXLeEQyLMXeUZo6ufkocl7KSvdW1aOh13m0pzF48sd ujGNIx/+qgugD23xZen1yw6meT/cqrHM+7khRekt9TKfsK09AJLYTGC+fceE/lS7+IqE kME/dhjCKWykNgFQZdzJNtIb7YEMlatHgqs0cekq/Mv2wTxRQp0D8zlAAx2nUNr7R2vv 5bAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728054153; x=1728658953; 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=hV/Shy1FZi7iJzQidTCMmnMOcmxQOiJp03mXnsnRSps=; b=rBubNfRjnvH3Uq4lQk/JkWmwkNVDiqukWM6nmhHZAq51XF26Qm59jAd5r3aECGJofU ZtObg4isG0f1YtDRIsagVDUhMoTkYqrg0/X1+M+EgbYbD6Sw2rncb9cFgQLLWfLl0ANE 9bkl+hokN/iQqvjLnv2MGxqwEvcEYjb/nSSKztNKJTtVB+lzXXeiFtWddKz8AgfHNqOX juJqCB6nkf8xjsosvIazgQnG6aAdeaEKI1xaGeIppMWkxv9YaRO8wkfe+zy7bo3RyYlG AWvJErYngXbcbj8b3ak0pabHGc7MskapFAHkiExVf45KKIbF+82SVVGnuwSl/hepJn5L xbzw== X-Forwarded-Encrypted: i=1; AJvYcCWgkcs7hgix2KxHdO6aufIrzUciadwuVZGeOvSXkZ/NT7NH7hTIoVd8X6Y6z7mCU7KQJKrL6AEoHQYVCSI=@vger.kernel.org X-Gm-Message-State: AOJu0YxWVhcCDZzOLMRXsPn84am8bJjwbZGCC/5G4V5uZowL5mQHqiZC XC1YzGevaQ7W75f1HlyFu9t76vYG9GRnkO7ss/m5mnsDajxA3Kx7+3ZFFucL5DU= X-Google-Smtp-Source: AGHT+IHXipJ4bjOkO8pnstOlhRfaqvic/Ya/BiH+1xmQ7oSN1dLW23K6+PVztgtZoVvN9CuOBds/Yg== X-Received: by 2002:a17:90a:bf0c:b0:2e0:8536:4f53 with SMTP id 98e67ed59e1d1-2e1e621151amr3899667a91.3.1728054153079; Fri, 04 Oct 2024 08:02:33 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-7e9f6c34127sm19979a12.67.2024.10.04.08.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2024 08:02:32 -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 1/3] iio: light: vl6180: Add configurable inter-measurement period support Date: Fri, 4 Oct 2024 20:31:46 +0530 Message-ID: <20241004150148.14033-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241004150148.14033-1-abhashkumarjha123@gmail.com> References: <20241004150148.14033-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 | 67 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a1b2b3c0b..52a837644 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,11 +430,22 @@ 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 switch (mask) { case IIO_CHAN_INFO_INT_TIME: @@ -427,6 +456,24 @@ 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: + guard(mutex)(&data->lock); + 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, re= g_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; + } + default: return -EINVAL; } @@ -473,6 +520,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 03:39:25 2024 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 2F2231C3053; Fri, 4 Oct 2024 15:02:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054159; cv=none; b=uv4OjgHszFHWAhS87WLCIMXlBTBIhtvh39E2P85hz5IHhAFmBLgCRlwdWRPiHrcX5hqqE9nEwBxUNhkTzx8WrAoIScW0DCtqiRNsYV4PqAgOkGiyNCaQAx/7/J9Qd1PzCpWqwYEYf1SWyrvikcsIGcR2qRfz7KkrTxKYsO7bKKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054159; c=relaxed/simple; bh=1QPDLs3MAqbXspdBmK3r2AssVDmePiOsOZc33gczOvg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DyGyolC6OfKD+zJygWaEskT2NcU91Oq1TF/JuSDaYltcodFj2DH+xvVYgI96ULhpSJe0nzxZbL510nil0z+gb71EqRhAuEUsbiy6jLGmDF6K2g7xB6gGvE+vfNSnadNtOae59pq9cHnypsYBcLzW7a4YtyAxtclrKz8HLF1WnYk= 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=TA64ujtq; arc=none smtp.client-ip=209.85.210.182 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="TA64ujtq" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7178df70f28so1981875b3a.2; Fri, 04 Oct 2024 08:02:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728054157; x=1728658957; 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=juxTMhYI61rMzzi75wKN/Dv12hzV7/kE6Qe6wu1IjGM=; b=TA64ujtqr0EKm1CFeMnnLIDLofbhveqdVhArtuUW6MCCiebzpg+Ux89NbJ7zZ9ow9W 5thnDy3LhKiEo4wd+dtOKsnaxdx7vXf1Fe+QPeflSI1IKJrlIw+L3Y+9rA3uGPAJ6L6G z6zL8mildW6ez3VmPIU8mkhgaN9el5f4+oYDcAVg3Gr9OElnLpMG1/tACe90qEiN6Iw9 57HmUNoV0aVPo/EawEZ+PPTvA9Q7Y6FTjjZqQgZIqp0MPhQOzNb3aZOmyAKFF7LDcVKj zCqr/6OtL3Rt/OIGkqvfpK/O1WRS5ieV+rDYEAfrmt2uN4rNdjSg8ZFRN309tChDj9Sl L+Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728054157; x=1728658957; 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=juxTMhYI61rMzzi75wKN/Dv12hzV7/kE6Qe6wu1IjGM=; b=pSfyolDqjmxOQNHR1O1EmSjj3xM/sxZUQKsSF+nhKOM0rLj7b4J703nBVMfzLhyXFB yIPZephyll2ga43zahu15jlhbofACUXRTeVIBsdl9DWzSKqRTBOTl67F7dxJnYNLZoQJ nTWghpFUiF30xQ1NugwoKhyrnEwI6qNjX4TtW8BLAxCX7tFU7OKRav5egkxZ++w7ahH1 CaRlND8mxBCAJgxn41tqbilXyPY2YXkNc9iQiUlGoIBW4Pnjw3qI1OOskWUlEERQtDd+ KOdt+rA6atu2i195kn0H3iCsI0nKPFAyKYAd6ZjBjSmVYWtDGmlMj7jk0qam1Jk0oSAq znrA== X-Forwarded-Encrypted: i=1; AJvYcCXdN6Roobbt03/KTrFPkZ/bbUWd/z6vkW8B99ggA/mz/Jxu096q41TM1as+sx/CNRLvBWeoxRQHPdyfh1w=@vger.kernel.org X-Gm-Message-State: AOJu0YyWZCHh7d9E2AtN0hH5nZTdOEKO/2LZeDNMcFCRBIBkXAxNQDpU Ploy9r4bzg0Z0ERn2dHM0gFsYOjf2WBlTz8oVnxDBg7FoGgKrXJK46DMojMXNtI= X-Google-Smtp-Source: AGHT+IHIbhwida7fs1g89tC4oLSfuYKdy15rti6QzwxlcjO3KsxZofa15kj7Gl3wLKtapAsw9OtHCA== X-Received: by 2002:a05:6a00:4fd3:b0:714:3de8:a616 with SMTP id d2e1a72fcca58-71de2449f43mr4365380b3a.19.1728054156668; Fri, 04 Oct 2024 08:02:36 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-7e9f6c34127sm19979a12.67.2024.10.04.08.02.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2024 08:02:36 -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 2/3] iio: light: vl6180: Added Interrupt support for single shot access Date: Fri, 4 Oct 2024 20:31:47 +0530 Message-ID: <20241004150148.14033-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241004150148.14033-1-abhashkumarjha123@gmail.com> References: <20241004150148.14033-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 52a837644..4c2b486e2 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 */ @@ -479,6 +492,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, @@ -514,6 +536,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); @@ -580,6 +607,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 03:39:25 2024 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 9A3251C75F6; Fri, 4 Oct 2024 15:02:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054164; cv=none; b=fOyt29EezeSsw3oRNAly3wxq/0Rlpo4RZbakOj9I0i8adOWy42CEqGCUxFr+eRXEG4YkOVl/lHUUoUi6fGspMgAts3Iipw6NemmZaddRXWbetmMJEvzYjNha29128d+UOCk+P5oAoZVCAYCiEfYn1Lhyc9hTpqteK7tqvmQwg1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728054164; c=relaxed/simple; bh=Fjx/l2xdHysAK/CJdTUGFN8OKKZLV0AyFZ/ZqSxNnI0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hjov4sauLxQjY5CknWVGg9Rh2hHGonztNjDvZ2YagOVLw4u5CEEwI+870oDpUkV0qoQdu3B0GIm3QD2dzgi40+YiRkm+7EM2nJKmwb2CPiC0nTAb5iQ4mxkb6rHye2U5mZnELG21S5llr/nBoo57GPbQrZxr370BrajsnHWfEp8= 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=VNu6gwlT; arc=none smtp.client-ip=209.85.210.173 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="VNu6gwlT" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-718e2855479so1855507b3a.1; Fri, 04 Oct 2024 08:02:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728054160; x=1728658960; 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=0ySn74GfdRjrISJyyXSwTVcF7lv1Nr6YC2yCmv/pj+A=; b=VNu6gwlTVy+y5TGia/kyRmgmTrVR09l8IWGkvRkW4/2kGvOzMn+2dqnYY2aKuWM+3P k3w/8UT7P6bGX0Yn0KepOlv88cXDL3vDCb1VAAeUjJYhCNP2IBNwICzWe8yGnzrg1UDT rpPKX9wN0hYaCh4WJKQoiKl4HsxMZ8iWaRgjT1Mxei0/Hp+RCDwOYFpuOOpT8hGvRIwQ 0JCrIeJPbzru/LTadBhirnSUT2UwkHgO5DBcxhbhPSn9XzFSOEWdhivA9Gcd+UepViHE cj37Y1A2hexp9kqwa9uPBYoa1HXhodUzXPy5WXoy6CKyuQ6XJDdo2exzNjwldwa7klKl owug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728054160; x=1728658960; 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=0ySn74GfdRjrISJyyXSwTVcF7lv1Nr6YC2yCmv/pj+A=; b=OZqIE2Ny9o3KPA034eHZu9ZBNGqy6USC/p+Kf7pgJEon4Dlyt3iM8vJeNfXmLLrNyW MrMUVzbdUkN8RS7bCmbyDhbxFefFDYyFBIM3YyPxpvqFgPsCQ02zDAKSv6s5iIsx2OdA jEO9BSJ3j7tffr8+LZO/zExmwCwxkmJ3b3++KsFwGDVCAbhZf1Ro+EYvUNkPOF6rLHuW zqb0lxby4MMRxNt5I5YB5Sjdo7qfK4/3xuHC7NQ7/ls6K+KjpSlw3+kOBLQgiVk7iu7Q Vrri+OGMBX/KBmhtKb2pPRM+qmrem9W5U2/nLwgHCFBUnUg6Z544QlDo178WvMtqqRA2 DSqw== X-Forwarded-Encrypted: i=1; AJvYcCWlmA2SutohMd1+BFl1D30bCE3j5+C9adK6Uii9QydCZmdYSJhQfvDe9EFbLetqMQyUcirj5750XcHrMtc=@vger.kernel.org X-Gm-Message-State: AOJu0YwgG25Ce7Fu9E/2kP2huFNexLu+s8PVR0RacDDWQJV4EFJ1G8Sm 4/kAL/uq7tA7uuzIy+pS71Ke+N1DjPEy2o1nwjesryEyP8Nh3uHcd7rw7W9Cc/E= X-Google-Smtp-Source: AGHT+IFHC8xL8+nWqUauFWJMNcz4vp8AIZyiJMaiZHrJZGOQlLhVWB/zbpUeaTjU3fZx701yF0/i+A== X-Received: by 2002:aa7:88c9:0:b0:710:50c8:ddcb with SMTP id d2e1a72fcca58-71de23a57efmr4622308b3a.5.1728054160108; Fri, 04 Oct 2024 08:02:40 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-7e9f6c34127sm19979a12.67.2024.10.04.08.02.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Oct 2024 08:02:39 -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 3/3] iio: light: vl6180: Add support for Continuous Mode Date: Fri, 4 Oct 2024 20:31:48 +0530 Message-ID: <20241004150148.14033-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241004150148.14033-1-abhashkumarjha123@gmail.com> References: <20241004150148.14033-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 4c2b486e2..e724e752e 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 /* @@ -497,17 +526,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; +} + +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; } =20 +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; @@ -547,6 +658,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 */ @@ -603,7 +720,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 @@ -618,6 +735,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