From: Javier Carrasco <javier.carrasco.cruz@gmail.com>
To: mgonellabolduc@dimonoff.com, Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Bill Wendling <morbo@google.com>,
Justin Stitt <justinstitt@google.com>
Cc: Mikael Gonella-Bolduc <m.gonella.bolduc@gmail.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
Hugo Villeneuve <hvilleneuve@dimonoff.com>
Subject: Re: [PATCH 2/2] iio: light: Add APDS9160 ALS & Proximity sensor driver
Date: Thu, 21 Nov 2024 13:12:15 +0100 [thread overview]
Message-ID: <a3bd83fb-589e-4814-9595-8f6343ecda83@gmail.com> (raw)
In-Reply-To: <20241119-apds9160-driver-v1-2-fa00675b4ea4@dimonoff.com>
Hi Mikael,
a few comments inline to add to what Krzysztof pointed out.
On 19/11/2024 21:36, Mikael Gonella-Bolduc via B4 Relay wrote:
> From: Mikael Gonella-Bolduc <mgonellabolduc@dimonoff.com>
>
> APDS9160 is a combination of ALS and proximity sensors.
>
> This patch add supports for:
> - Intensity clear data and illuminance data
> - Proximity data
> - Gain control, rate control
> - Event thresholds
>
> Signed-off-by: Mikael Gonella-Bolduc <mgonellabolduc@dimonoff.com>
> ---
> MAINTAINERS | 7 +
> drivers/iio/light/Kconfig | 13 +
> drivers/iio/light/Makefile | 1 +
> drivers/iio/light/apds9160.c | 1420 ++++++++++++++++++++++++++++++++++++++++++
> 4 files changed, 1441 insertions(+)
>
...
> +config APDS9160
> + tristate "APDS9160 combined als and proximity sensors"
> + select REGMAP_I2C
> + select IIO_BUFFER
> + select IIO_KFIFO_BUF
> + depends on I2C
> + help
> + Say Y here if you want to build a driver for Broadcom APDS9160
> + combined ambient light and proximity sensor chip.
> +
You can drop that "If unsure, say N here." as it is not common for such
drivers in IIO. There are a couple of entries in the whole subsystem
(not in this Kconfig, though) with this sentence, and some of them could
be dropped too.
> + To compile this driver as a module, choose M here: the
> + module will be called apds9160. If unsure, say N here.
> +
> config APDS9300
> tristate "APDS9300 ambient light sensor"
> depends on I2C
...
> +
> +static int apds9160_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan, int val,
> + int val2, long mask)
> +{
> + struct apds9160_chip *data = iio_priv(indio_dev);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_INT_TIME:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_INTENSITY:
> + return apds9160_set_als_int_time(data, val);
> + default:
> + return -EINVAL;
> + }
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + return apds9160_set_ps_rate(data, val);
> + default:
> + return -EINVAL;
> + }
> + case IIO_CHAN_INFO_HARDWAREGAIN:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_INTENSITY:
> + return apds9160_set_als_gain(data, val);
> + case IIO_PROXIMITY:
> + return apds9160_set_ps_gain(data, val);
> + default:
> + return -EINVAL;
> + }
> + case IIO_CHAN_INFO_CALIBSCALE:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + return apds9160_set_ps_cancellation_level(data, val);
> + default:
> + return -EINVAL;
> + }
> + case IIO_CHAN_INFO_CALIBBIAS:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + return apds9160_set_ps_analog_cancellation(data, val);
> + default:
> + return -EINVAL;
> + }
> + case IIO_CHAN_INFO_RAW:
> + if (val2 != 0)
> + return -EINVAL;
> + switch (chan->type) {
> + case IIO_CURRENT:
> + return apds9160_set_ps_current(data, val);
> + default:
> + return -EINVAL;
> + }
> + default:
> + return -EINVAL;
> + }
> +
If you only have a switch with a return in every path, this return 0
can't be reached.
> + return 0;
> +}
> +
> +static inline int apds9160_get_thres_reg(const struct iio_chan_spec *chan,
> + enum iio_event_direction dir, u8 *reg)
> +{
> + switch (dir) {
> + case IIO_EV_DIR_RISING:
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + *reg = APDS9160_REG_PS_THRES_HI_LSB;
> + break;
> + case IIO_INTENSITY:
> + *reg = APDS9160_REG_LS_THRES_UP_LSB;
> + break;
> + default:
> + return -EINVAL;
> + }
> + break;
> + case IIO_EV_DIR_FALLING:
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + *reg = APDS9160_REG_PS_THRES_LO_LSB;
> + break;
> + case IIO_INTENSITY:
> + *reg = APDS9160_REG_LS_THRES_LO_LSB;
> + break;
> + default:
> + return -EINVAL;
> + }
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
> +static int apds9160_read_event(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir,
> + enum iio_event_info info, int *val, int *val2)
> +{
> + u8 reg;
> +
> + int ret = 0;
> + struct apds9160_chip *data = iio_priv(indio_dev);
> +
> + if (info != IIO_EV_INFO_VALUE)
> + return -EINVAL;
> +
> + ret = apds9160_get_thres_reg(chan, dir, ®);
> + if (ret < 0)
> + return ret;
> +
> + if (chan->type == IIO_PROXIMITY) {
> + __le16 buf;
> +
> + ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
> + if (ret < 0)
> + return ret;
> + *val = le16_to_cpu(buf);
> + } else if (chan->type == IIO_INTENSITY) {
> + __le32 buf = 0;
> +
> + ret = regmap_bulk_read(data->regmap, reg, &buf, 3);
> + if (ret < 0)
> + return ret;
> + *val = le32_to_cpu(buf);
Missing braces for that else (use them in all arms if you need them in one).
> + } else
> + return -EINVAL;
> +
> + *val2 = 0;
> +
> + return IIO_VAL_INT;
> +}
> +
> +static int apds9160_write_event(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir,
> + enum iio_event_info info, int val, int val2)
> +{
> + u8 reg;
> + int ret = 0;
> + struct apds9160_chip *data = iio_priv(indio_dev);
> +
> + if (info != IIO_EV_INFO_VALUE)
> + return -EINVAL;
> +
> + ret = apds9160_get_thres_reg(chan, dir, ®);
> + if (ret < 0)
> + return ret;
> +
> + if (chan->type == IIO_PROXIMITY) {
> + if (val < 0 || val > APDS9160_PS_THRES_MAX)
> + return -EINVAL;
> + __le16 buf;
> +
> + buf = cpu_to_le16(val);
> + ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
> + if (ret < 0)
> + return ret;
> + } else if (chan->type == IIO_INTENSITY) {
> + if (val < 0 || val > APDS9160_LS_THRES_MAX)
> + return -EINVAL;
> + __le32 buf = 0;
> +
> + buf = cpu_to_le32(val);
> + ret = regmap_bulk_write(data->regmap, reg, &buf, 3);
> + if (ret < 0)
> +
Same here.
return ret;
> + } else
> + return -EINVAL;
> +
> + return 0;
> +}
> +
> +static int apds9160_read_event_config(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir)
> +{
> + struct apds9160_chip *data = iio_priv(indio_dev);
> +
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + return data->ps_int;
> + case IIO_INTENSITY:
> + return data->als_int;
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
> +
The 'state' argument is now a bool. To avoid issues, please rebase to
newer branches like linux-next, iio/testing iio/togreg. Otherwise it
will not compile with that modification. data->ps_int should then become
a bool too.
> +static int apds9160_write_event_config(struct iio_dev *indio_dev,
> + const struct iio_chan_spec *chan,
> + enum iio_event_type type,
> + enum iio_event_direction dir, int state)
> +{
> + struct apds9160_chip *data = iio_priv(indio_dev);
> + int ret;
> +
> + state = !!state;
> +
> + switch (chan->type) {
> + case IIO_PROXIMITY:
> + if (data->ps_int == state)
> + return -EINVAL;
> +
> + ret = regmap_field_write(data->reg_int_ps, state);
> + if (ret)
> + return ret;
> + data->ps_int = state;
> + break;
> + case IIO_INTENSITY:
> + if (data->als_int == state)
> + return -EINVAL;
> +
> + ret = regmap_field_write(data->reg_int_als, state);
> + if (ret)
> + return ret;
> + data->als_int = state;
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + return 0;
> +}
Best regards,
Javier Carrasco
next prev parent reply other threads:[~2024-11-21 12:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-19 20:36 [PATCH 0/2] Add support for Avago/Broadcom APDS9160 Mikael Gonella-Bolduc via B4 Relay
2024-11-19 20:36 ` [PATCH 1/2] dt-bindings: iio: light: Add APDS9160 binding Mikael Gonella-Bolduc via B4 Relay
2024-11-20 17:18 ` Conor Dooley
2024-11-20 17:23 ` Krzysztof Kozlowski
2024-11-20 20:26 ` Mikael Gonella-Bolduc
2024-11-21 7:52 ` Krzysztof Kozlowski
2024-11-20 17:22 ` Krzysztof Kozlowski
2024-11-24 19:26 ` Jonathan Cameron
2024-11-19 20:36 ` [PATCH 2/2] iio: light: Add APDS9160 ALS & Proximity sensor driver Mikael Gonella-Bolduc via B4 Relay
2024-11-20 17:33 ` Krzysztof Kozlowski
2024-11-21 12:12 ` Javier Carrasco [this message]
2024-11-22 15:09 ` kernel test robot
2024-11-24 21:15 ` Jonathan Cameron
2024-11-27 22:11 ` Mikael Gonella-Bolduc
2024-12-01 13:20 ` Jonathan Cameron
2024-12-02 8:22 ` Matti Vaittinen
2024-12-05 9:42 ` Matti Vaittinen
2024-12-06 16:20 ` Mikael Gonella-Bolduc
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a3bd83fb-589e-4814-9595-8f6343ecda83@gmail.com \
--to=javier.carrasco.cruz@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=hvilleneuve@dimonoff.com \
--cc=jic23@kernel.org \
--cc=justinstitt@google.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=llvm@lists.linux.dev \
--cc=m.gonella.bolduc@gmail.com \
--cc=mgonellabolduc@dimonoff.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).