From nobody Wed Nov 27 21:45:48 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 5B7421D88C1; Mon, 7 Oct 2024 15:20:16 +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=1728314418; cv=none; b=FWHNHUbOg/BmLafD6YDP0mpXWJztqhCuaOhyFWtrHTnsxrV1lfaG1aZhbzoRTROc3ctUeNCXky/QrWq2qaq6z7KXbR3hq+xfW8lGa/xWpLI5apq350esSrlujHRMzphfFfrdCJQ0yW3551VAsJFW8ZHJKAoRdGMLuVmiV5sE5LM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314418; c=relaxed/simple; bh=T+QYja7LUJzkV7RCgdEgVqVceSPVINzxqOOsbiHlAtM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MU1BwcW8JSRNNqsEldeKOQiTALbaxBgINrZyen7Ki8S7i5zCauxLmFNyXF98irz2LoGynmzORwefd01s236/RuCXFPcQAge7xKUE/tlD4PlOZG99ZRupsEptM5H5S2Tzpuytcnf6EmpOqZqj7SwCc397LgtxNbXjfItQ9e7FTFk= 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=j5axIN50; 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="j5axIN50" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-20b8be13cb1so48968385ad.1; Mon, 07 Oct 2024 08:20:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314415; x=1728919215; 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=yUspM/VMPEO1Kn2QXTO9UIPl/J9+OPnNBwBdWZtkysA=; b=j5axIN50mguAA6yZUAb0kv7TX6TOQL8bEjOoTX3FrFxR61pZRw7PYXay8gLVtV6+ST u5E6e3KGuzeHXgHKj1bdlhovIXML89yxElG4lZtUyZ1bxrPBF6po/ozrV/NSjeq7TFov 82Sq4lgkJ/buEUBnlBXMj+Vai0r9qYrDvYpstq+H0KqoudbjyBmMasvSAQTgHIjOhTQN m/hXryR4ZBzt89bUcSBk6d3hJafucDymyi+UwcB679BjUOyc4pE+VOdTVqPu7jLLRHLg bv68tBjcAxma6Do9xlFAUA8qniBOnCFr2piVjNGtEODN6atJd7SjsO64qTbiSULYIa1T voPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314415; x=1728919215; 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=yUspM/VMPEO1Kn2QXTO9UIPl/J9+OPnNBwBdWZtkysA=; b=CH0UlAHm1vfec9tvatRSq4iyTPoM8T2++/MCD0XDfTdde41no2Biw6/KONK6JYyNu0 79/RBBOk51C2PO3PJaZOoQDuMq3TJbyed8b5/zb+VSTxhfzk9nX3RHVEu36QhOj3nrG1 GAHjG40eT81abHzf+FoydI3/nui+lpmVo2VqXY3m32V7xe0ZGrU1KZBcNgEOSGCGrEY8 M2mEbgVYqAGPcAhh0KVgcGb5NOXB7f88vP72AJeKMDbsDkllABZ97LZxzwfvn6OFgJ38 2FWqfAo90IlmITmHADu68xdWA+dY5P5lPJv/SNcplQUBAamQ7IDzpTo+Q0qD8opUUiUl SThQ== X-Forwarded-Encrypted: i=1; AJvYcCXIPQ4kYEUlUa9jIkwOlBVraubcMKJnxcYCskCfR9K3jO3EqhbvxSVAZrZB46E3yLbARlOfDIdjHd4uLwg=@vger.kernel.org X-Gm-Message-State: AOJu0YxoDDmdPQO32jVokCIgqeB1a+cVtsBNrtd1ReSQ/meGZFNstN5W IBDM3CmwFzW6tsc4LLhUw1iNPiuNEq8NGSV1YmlZ9Cn5RKPMdFQ+/a0ftZNOyFQ= X-Google-Smtp-Source: AGHT+IGv6vbD0vJYQ8PyEEPLjhzoNFhVfuDsNUlr/rmEZWCXQq1pD8nx8hsECFkyRcsEWQedDMdfdA== X-Received: by 2002:a17:902:fc4e:b0:202:4666:f018 with SMTP id d9443c01a7336-20bfdfd2693mr202680265ad.15.1728314415059; Mon, 07 Oct 2024 08:20:15 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20c138b1395sm41000755ad.3.2024.10.07.08.20.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:20:14 -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 v3 1/3] iio: light: vl6180: Add configurable inter-measurement period support Date: Mon, 7 Oct 2024 20:49:45 +0530 Message-ID: <20241007151947.58828-2-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007151947.58828-1-abhashkumarjha123@gmail.com> References: <20241007151947.58828-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 milihertz. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 70 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a1b2b3c0b..67aa2f101 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,23 @@ static int vl6180_set_it(struct vl6180_data *data, in= t val, int val2) return ret; } =20 +static int vl6180_meas_reg_val_from_mhz(unsigned int mhz) +{ + unsigned int period =3D DIV_ROUND_CLOSEST(1000 * 1000, mhz); + 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 +457,28 @@ 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_mhz(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_mhz(val); + return vl6180_write_byte(data->client, + VL6180_ALS_INTER_MEAS_TIME, reg_val); + + default: + return -EINVAL; + } + } + default: return -EINVAL; } @@ -473,6 +525,20 @@ static int vl6180_init(struct vl6180_data *data) if (ret < 0) return ret; =20 + /* Default Range inter-measurement time: 50ms or 20000 mHz */ + ret =3D vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, + vl6180_meas_reg_val_from_mhz(20000)); + if (ret < 0) + return ret; + data->range_meas_rate =3D 20000; + + /* Default ALS inter-measurement time: 10ms or 100000 mHz */ + ret =3D vl6180_write_byte(client, VL6180_ALS_INTER_MEAS_TIME, + vl6180_meas_reg_val_from_mhz(100000)); + if (ret < 0) + return ret; + data->als_meas_rate =3D 100000; + /* 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 Wed Nov 27 21:45:48 2024 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 8037D1D89FF; Mon, 7 Oct 2024 15:20:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314421; cv=none; b=dZYpXvufwcY8FFWY22C9IB2U/Vbc9rVm+PTQHfF9rzosF9ZwB99RU2NJgVw26YPUwHmHAkkDRRedoo6M+cqydLZkmHSwNSwKJm5hrTRmhycbAnaoAAUKWCYLEWKtJI5+hldCA86K6fu12m4I+xH4zgT59F5vsOb2kK2YQXOdBFU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314421; c=relaxed/simple; bh=y8fpYG9T60OwQP0aXJxzsXaodmkYPpmnD99lrwUmUdo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TjW+DPHhDO6Jw44Elo7q7yJUnR+PgqBMKHNPFscjGaDBrI2dQbuKqpHnideGAeZq+TuwUWksYm/TuB9+L3K5uoVSiYvvFjdVKdPaQmYaS+w0ugClpZsHlO6+RgKdtUU0pOE6ZhWbgTuHwew7GZP1QEQUD/eqlkWA5fjeDGI9zVI= 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=TTwxfOOi; arc=none smtp.client-ip=209.85.214.180 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="TTwxfOOi" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-20b0b5cdb57so41329215ad.1; Mon, 07 Oct 2024 08:20:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314418; x=1728919218; 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=i6Q3pN7n5lzASa1iAB1h9SmIQ5Uey3Y3s1sa+1tGPWk=; b=TTwxfOOi9EU8i+irNiRRVyRKys2hw9Ae0eTQhx2HaUuJyvTp6JvA200nTuf6215xqK vRVWUz6fJna1It/lTi9OcSh0oTiAvkcIhxLfGTdjON7XOhp64swCLG0Q9m4SmZI0P44u jAStrWTT6lrZVb1gyN1Ts4xpW45/z6t/bNIFzP3uaB7RUtonnV8LaPp2INbhTnaduZpc YuxrwWSG8lF+VwPS5EgTUbKHDkt98TmUHA9UJUR2pktqFpFYiFItNKtrSt7RIhCvFw4b LcEt19BFvNerqrZtlMmBISv6RGMWACq/EZitei/HGNcmwagWV4nfrb+EcC3Pq/WlrHSC 87RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314418; x=1728919218; 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=i6Q3pN7n5lzASa1iAB1h9SmIQ5Uey3Y3s1sa+1tGPWk=; b=xKJrJ+ZlF2/bSJcqMv12R1ulTC8xs9XVy/Q8zPtlaNvpUmB4ppjohX1rqi0rI/FZLp JhAQVyQikwMGXi1LB9dGGWkWptbVOt/spaK8yqeiR7Iq8cGeJA+i3mvqXDxULRwG13RD 4+DSm0vu3QaDax6Oub/AezsA02WX8A3coey4+yN6qntaPTz9dSEAonOACOu0coxAhOjI rls3RoRifmhzseNy1LmPIndPAhv6WNknqXU7qqfj6ZbDF/7ZQS5OpWEW9+MsuR9nqgyS ehWdN7fxummeY8CXYongQHN8ep1vlPNqXOUK+E4PvpismKetRLxkUyxONtS7qxAq5kGw vyrg== X-Forwarded-Encrypted: i=1; AJvYcCVYk8F3eScqU3Mbg2iwLRHmKeqrdQhuMMSLHEoln0uiBKGicO0cZbNdwu9LqBuQL3khV/nktPjKsEH1jOg=@vger.kernel.org X-Gm-Message-State: AOJu0YxwluVOXyf5vjsOUB1/zwaGD3/LV8leQz8docsdWXZ5KsMoFXEL cTHkwBhl2t6Y7sqytMBQfDbhje3vfNZqq1Obe8uafGf6RLQQHULHk43gB0D9XQk= X-Google-Smtp-Source: AGHT+IGP30QjcEuP6lWtQgkymsNYOcb0WCuG8oqw5rvdNOtuthT3T4Y6C+jpxhDkdvQ472jsadiBUw== X-Received: by 2002:a17:902:d50d:b0:205:59b7:69c2 with SMTP id d9443c01a7336-20bff3912e8mr144356195ad.7.1728314418218; Mon, 07 Oct 2024 08:20:18 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20c138b1395sm41000755ad.3.2024.10.07.08.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:20:17 -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 v3 2/3] iio: light: vl6180: Added Interrupt support for single shot access Date: Mon, 7 Oct 2024 20:49:46 +0530 Message-ID: <20241007151947.58828-3-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007151947.58828-1-abhashkumarjha123@gmail.com> References: <20241007151947.58828-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" The interrupts are serviced in the `vl6180_measure` function when the irq_handler signals that the reading is complete. We now can read asynchronously if `client->irq` is set. Signed-off-by: Abhash Jha --- drivers/iio/light/vl6180.c | 54 +++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index 67aa2f101..a747501b0 100644 --- a/drivers/iio/light/vl6180.c +++ b/drivers/iio/light/vl6180.c @@ -86,6 +86,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,29 +212,38 @@ static int vl6180_write_word(struct i2c_client *clien= t, 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 mutex_lock(&data->lock); + reinit_completion(&data->completion); + /* Start single shot measurement */ ret =3D vl6180_write_byte(client, vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); 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) { + 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 */ @@ -484,6 +494,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, @@ -583,6 +602,17 @@ 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) + return dev_err_probe(&client->dev, ret, "devm_request_irq error \n"); + + init_completion(&data->completion); + } + return devm_iio_device_register(&client->dev, indio_dev); } =20 --=20 2.43.0 From nobody Wed Nov 27 21:45:48 2024 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 661031D8E08; Mon, 7 Oct 2024 15:20:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314424; cv=none; b=pp7AyC78x/QpMXR1HP8s4TmLfZMZMIoDO8iS7QF0WW7D3DwILxPKD7fPTwUU9r43aRzrvthlVVf9avClOoP/Wanr7qVOjwFLe95nSh/j3qiG1qQZL+ArpebDXCc+gxfRmOYWqpgrDWDZ5GTCbA6CIZZMCGgFmk/1+qf5NcA3mVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728314424; c=relaxed/simple; bh=kahuhEbWhljSN1XdX+Glr547jKdvkKJ3VPKL2ysqdQE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Pm22zVs4ymUfCx2T4eAijPRGzdh8pMpO850sSrmCLsXk8HZRSSQpJjnYHBx/8kzru0Fkg43sNBj69hcMuoXUu56iEN5Nf5KKiA9sh3tyik0Vq2ok6RPg7aulZIo/dRCj2y7LpFj4s2rtzuX7eqNxQKZttEuMlRD07dVDglYTfHI= 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=Sr61Mszv; arc=none smtp.client-ip=209.85.214.176 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="Sr61Mszv" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-20b6458ee37so52820905ad.1; Mon, 07 Oct 2024 08:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728314421; x=1728919221; 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=NGXYLWa9PfVlmMvYF4HK1fhAmnNokMXHI7JdaUOAE80=; b=Sr61MszvwdQPLIMJL4nSOefe9ffo4+mIPwnXK+/qYC/itWxXk8AW9mT2dhstK5qhHj LfHUi9bM0kY/xKs9mR38fSS5/DnhA4fm4PpneXsx9YZ5Y6wWWc8Okmy/GECvQf82qvq2 N9klVIxq9CLZQnuZP6/KO40QWxVyDi50nLTJUZ8dpQuNjSywAAXSdgNLBME+lin8eoRs H41ZNeAyZPLNMctmFf6yGSJsDcSbpCtTi2faPEvPSxRsx9NZq0fDWk0Wvg2txnQhCcXA yhTBkc92PTCMdNn4yriuLEtu9tln/1gz9iatjoNxlW7wMxDgxm+o22YTl+WpE1vSu6+0 r+wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728314421; x=1728919221; 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=NGXYLWa9PfVlmMvYF4HK1fhAmnNokMXHI7JdaUOAE80=; b=SwQiCz9Y2sip65wFlwb2xKPj3FuNSZ5I8794d29IZzq2vSKzCsHq1ttJCPcwll8K2c LRbZ+Ku15lJaYxkHLOzsYb0MvTT2wzA0ASlR9C1N1AtYlKcwPUExTUx7IeBnv9RjpHld MTwn2EgRPFKnKtabDpGUXTJtkUorMIppfuvikFOMX8+xtp+2NAP+c7m4UyezlClRszQf P3DV3v0DpMG8Js6AFbJ4vnRTiHVs6fPltrmzGQxDaHs98zON1YuqGFq63xfWf9vN43cJ gykQwfMZJhASx5DOgHRywnnklcVg5Hnvp2WDzXb2BNjNtOOm2j3B8hdPLvU38x1KyA/g ql6A== X-Forwarded-Encrypted: i=1; AJvYcCX9Q6EMTIWn341v8+wWW3mNeND4/IanbRz/H6yNXs6S9KvA9djJoMwqNvElCZz0OMRdQi5LzVQy0t/zSag=@vger.kernel.org X-Gm-Message-State: AOJu0YzFRmMXw7Qnlv2oX5Vw3vMujLY5nLP5oyi1/in9OebKLdfQ7RX/ K+kjPz6rAUYnyfCqGbhIOj7N7TBMeIIeM/hdJoJvJnjp7ZwA4wH+cILOj0BWieE= X-Google-Smtp-Source: AGHT+IF/adgye/9imrxRdWOmsrXvNj+RaMAm6X+oSoTCeiTWPfZ627OqTUq0FI6wwgutoU63E72YIw== X-Received: by 2002:a17:902:d50c:b0:20b:6145:d0d5 with SMTP id d9443c01a7336-20bfde5fed4mr179128415ad.10.1728314421511; Mon, 07 Oct 2024 08:20:21 -0700 (PDT) Received: from abhash-IdeaPad-L340-15IRH-Gaming.. ([136.233.9.100]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-20c138b1395sm41000755ad.3.2024.10.07.08.20.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Oct 2024 08:20:21 -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 v3 3/3] iio: light: vl6180: Add support for Continuous Mode Date: Mon, 7 Oct 2024 20:49:47 +0530 Message-ID: <20241007151947.58828-4-abhashkumarjha123@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241007151947.58828-1-abhashkumarjha123@gmail.com> References: <20241007151947.58828-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 | 133 +++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 4 deletions(-) diff --git a/drivers/iio/light/vl6180.c b/drivers/iio/light/vl6180.c index a747501b0..f0c7a13a0 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 @@ -87,10 +91,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[2]; + aligned_s64 timestamp; + } scan; }; =20 enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; @@ -272,6 +282,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) | @@ -280,14 +296,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 /* @@ -499,7 +528,48 @@ 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); + s64 time_ns =3D iio_get_time_ns(indio_dev); + int ret, bit, i =3D 0; + + iio_for_each_active_channel(indio_dev, bit) { + if (vl6180_chan_regs_table[bit].word) + ret =3D vl6180_read_word(data->client, + vl6180_chan_regs_table[bit].value_reg); + else + ret =3D vl6180_read_byte(data->client, + vl6180_chan_regs_table[bit].value_reg); + + if (ret < 0) { + dev_err(&data->client->dev, + "failed to read from value regs: %d\n", ret); + return IRQ_HANDLED; + } + + data->scan.chan[i++] =3D ret; + } + + iio_push_to_buffers_with_timestamp(indio_dev, &data->scan, time_ns); + 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 @@ -507,9 +577,45 @@ 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 iio_validate_own_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); + int bit; + + iio_for_each_active_channel(indio_dev, bit) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[bit].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); + int bit; + + iio_for_each_active_channel(indio_dev, bit) + return vl6180_write_byte(data->client, + vl6180_chan_regs_table[bit].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; @@ -544,6 +650,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 or 20000 mHz */ ret =3D vl6180_write_byte(client, VL6180_RANGE_INTER_MEAS_TIME, vl6180_meas_reg_val_from_mhz(20000)); @@ -598,7 +710,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 @@ -611,6 +723,19 @@ static int vl6180_probe(struct i2c_client *client) return dev_err_probe(&client->dev, ret, "devm_request_irq error \n"); =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