From nobody Mon Feb 9 14:03:20 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B5516C7EE26 for ; Mon, 22 May 2023 14:26:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233626AbjEVO0k (ORCPT ); Mon, 22 May 2023 10:26:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231695AbjEVO0c (ORCPT ); Mon, 22 May 2023 10:26:32 -0400 Received: from smtp1.axis.com (smtp1.axis.com [195.60.68.17]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BB75A3; Mon, 22 May 2023 07:26:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axis.com; q=dns/txt; s=axis-central1; t=1684765590; x=1716301590; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WSfkxVBv8f37msGKdmNCBmbjoy/DrcehpWEs6TZDZOI=; b=l3zAXsoFJX1cYU7PggwWJ+dp/KwrabHt1AwPm6oPagXBaNgpBR/mZG6u Hpf+I0Bpbv0isJB7hC7Cb63U7a2Vr76jflWGzj0pNqHNNg/+dDrMkHATo KNcF5of2/kwGlvW70BWXWxQqaxp0jnSVqbDkJOdtJqRqBqywOb+G4Mj3a +BRPnhCCeHRJlSG3D4z88jmai55lWgX1qbvRPi2uXFO6n/Nm0//Ud/e/J 5u3W1U9tdU8v2WGLsT3TsJ78GCmogrYINJziwLj68kuHDuD+ZdmGXPAQp 8h0fa7n1dKDYEneSRHGMXafmJeCKL3kBrP72cnsfZM6wRJQ9dsUAimJle A==; From: Astrid Rost To: Jonathan Cameron , Lars-Peter Clausen CC: , , , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Mathieu Othacehe , Astrid Rost Subject: [PATCH v4 3/7] iio: light: vcnl4000: Add als_it for vcnl4040/4200 Date: Mon, 22 May 2023 16:26:17 +0200 Message-ID: <20230522142621.1680563-4-astrid.rost@axis.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230522142621.1680563-1-astrid.rost@axis.com> References: <20230522142621.1680563-1-astrid.rost@axis.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add illuminance integration time for vcnl4040 and vcnl4200. Add read/write attribute for illuminance integration time and read attribute for available integration times. Set scale and sampling rate according to the integration time. Signed-off-by: Astrid Rost --- drivers/iio/light/vcnl4000.c | 121 ++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 8 deletions(-) diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c index 086a493fb6e5..285f1dd314d8 100644 --- a/drivers/iio/light/vcnl4000.c +++ b/drivers/iio/light/vcnl4000.c @@ -80,6 +80,7 @@ #define VCNL4000_SELF_TIMED_EN BIT(0) /* start self-timed measurement */ =20 #define VCNL4040_ALS_CONF_ALS_SHUTDOWN BIT(0) +#define VCNL4040_ALS_CONF_IT GENMASK(7, 6) /* Ambient integration time */ #define VCNL4040_PS_CONF1_PS_SHUTDOWN BIT(0) #define VCNL4040_PS_CONF2_PS_IT GENMASK(3, 1) /* Proximity integration tim= e */ #define VCNL4040_PS_CONF2_PS_INT GENMASK(9, 8) /* Proximity interrupt mode= */ @@ -133,6 +134,20 @@ static const int vcnl4200_ps_it_times[][2] =3D { {0, 864}, }; =20 +static const int vcnl4040_als_it_times[][2] =3D { + {0, 80000}, + {0, 160000}, + {0, 320000}, + {0, 640000}, +}; + +static const int vcnl4200_als_it_times[][2] =3D { + {0, 50000}, + {0, 100000}, + {0, 200000}, + {0, 400000}, +}; + #define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend= */ =20 enum vcnl4000_device_ids { @@ -177,6 +192,8 @@ struct vcnl4000_chip_spec { u8 int_reg; const int(*ps_it_times)[][2]; const int num_ps_it_times; + const int(*als_it_times)[][2]; + const int num_als_it_times; }; =20 static const struct i2c_device_id vcnl4000_id[] =3D { @@ -337,7 +354,7 @@ static int vcnl4200_init(struct vcnl4000_data *data) data->vcnl4200_al.sampling_rate =3D ktime_set(0, 96000 * 1000); /* Default wait time is 5ms, add 20% tolerance. */ data->vcnl4200_ps.sampling_rate =3D ktime_set(0, 6000 * 1000); - data->al_scale =3D 120000; + data->al_scale =3D 100000; break; } mutex_init(&data->vcnl4200_al.lock); @@ -509,6 +526,65 @@ static int vcnl4000_set_pm_runtime_state(struct vcnl40= 00_data *data, bool on) return ret; } =20 +static int vcnl4040_read_als_it(struct vcnl4000_data *data, int *val, int = *val2) +{ + int ret; + + ret =3D i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF); + if (ret < 0) + return ret; + + ret =3D FIELD_GET(VCNL4040_ALS_CONF_IT, ret); + + if (ret >=3D data->chip_spec->num_als_it_times) + return -EINVAL; + + *val =3D (*data->chip_spec->als_it_times)[ret][0]; + *val2 =3D (*data->chip_spec->als_it_times)[ret][1]; + + return 0; +} + +static ssize_t vcnl4040_write_als_it(struct vcnl4000_data *data, int val) +{ + unsigned int i; + int ret, index =3D -1; + u16 regval; + + for (i =3D 0; i < data->chip_spec->num_als_it_times; i++) { + if (val =3D=3D (*data->chip_spec->als_it_times)[i][1]) { + index =3D i; + break; + } + } + + if (index < 0) + return -EINVAL; + + data->vcnl4200_al.sampling_rate =3D ktime_set(0, val * 1200000); + if (data->id =3D=3D VCNL4200) + /* 24000 =C2=B5lux/step * 50000 =C2=B5s / val */ + data->al_scale =3D div_u64(1200000000, val); + else + /* 100000 =C2=B5lux/step * 80000 =C2=B5s / val */ + data->al_scale =3D div_u64(8000000000, val); + + mutex_lock(&data->vcnl4000_lock); + + ret =3D i2c_smbus_read_word_data(data->client, VCNL4200_AL_CONF); + if (ret < 0) + goto out; + + regval =3D (ret & ~VCNL4040_ALS_CONF_IT) | + FIELD_PREP(VCNL4040_ALS_CONF_IT, index); + ret =3D i2c_smbus_write_word_data(data->client, VCNL4200_AL_CONF, + regval); + +out: + mutex_unlock(&data->vcnl4000_lock); + return ret; +} + static int vcnl4040_read_ps_it(struct vcnl4000_data *data, int *val, int *= val2) { int ret; @@ -599,9 +675,16 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev, *val2 =3D data->al_scale; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_INT_TIME: - if (chan->type !=3D IIO_PROXIMITY) + switch (chan->type) { + case IIO_LIGHT: + ret =3D vcnl4040_read_als_it(data, val, val2); + break; + case IIO_PROXIMITY: + ret =3D vcnl4040_read_ps_it(data, val, val2); + break; + default: return -EINVAL; - ret =3D vcnl4040_read_ps_it(data, val, val2); + } if (ret < 0) return ret; return IIO_VAL_INT_PLUS_MICRO; @@ -620,9 +703,14 @@ static int vcnl4040_write_raw(struct iio_dev *indio_de= v, case IIO_CHAN_INFO_INT_TIME: if (val !=3D 0) return -EINVAL; - if (chan->type !=3D IIO_PROXIMITY) + switch (chan->type) { + case IIO_LIGHT: + return vcnl4040_write_als_it(data, val2); + case IIO_PROXIMITY: + return vcnl4040_write_ps_it(data, val2); + default: return -EINVAL; - return vcnl4040_write_ps_it(data, val2); + } default: return -EINVAL; } @@ -637,9 +725,20 @@ static int vcnl4040_read_avail(struct iio_dev *indio_d= ev, =20 switch (mask) { case IIO_CHAN_INFO_INT_TIME: - *vals =3D (int *)(*data->chip_spec->ps_it_times); + + switch (chan->type) { + case IIO_LIGHT: + *vals =3D (int *)(*data->chip_spec->als_it_times); + *length =3D 2 * data->chip_spec->num_als_it_times; + break; + case IIO_PROXIMITY: + *vals =3D (int *)(*data->chip_spec->ps_it_times); + *length =3D 2 * data->chip_spec->num_ps_it_times; + break; + default: + return -EINVAL; + } *type =3D IIO_VAL_INT_PLUS_MICRO; - *length =3D 2 * data->chip_spec->num_ps_it_times; return IIO_AVAIL_LIST; default: return -EINVAL; @@ -1261,7 +1360,9 @@ static const struct iio_chan_spec vcnl4040_channels[]= =3D { { .type =3D IIO_LIGHT, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | - BIT(IIO_CHAN_INFO_SCALE), + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_INT_TIME), + .info_mask_separate_available =3D BIT(IIO_CHAN_INFO_INT_TIME), }, { .type =3D IIO_PROXIMITY, .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | @@ -1334,6 +1435,8 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_= spec_cfg[] =3D { .int_reg =3D VCNL4040_INT_FLAGS, .ps_it_times =3D &vcnl4040_ps_it_times, .num_ps_it_times =3D ARRAY_SIZE(vcnl4040_ps_it_times), + .als_it_times =3D &vcnl4040_als_it_times, + .num_als_it_times =3D ARRAY_SIZE(vcnl4040_als_it_times), }, [VCNL4200] =3D { .prod =3D "VCNL4200", @@ -1348,6 +1451,8 @@ static const struct vcnl4000_chip_spec vcnl4000_chip_= spec_cfg[] =3D { .int_reg =3D VCNL4200_INT_FLAGS, .ps_it_times =3D &vcnl4200_ps_it_times, .num_ps_it_times =3D ARRAY_SIZE(vcnl4200_ps_it_times), + .als_it_times =3D &vcnl4200_als_it_times, + .num_als_it_times =3D ARRAY_SIZE(vcnl4200_als_it_times), }, }; =20 --=20 2.30.2