iio: light: add driver support for MAX44009
Subsystems (2)
PATCH 1/1 iio: light: add driver support for MAX44009
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
events
* Illuminance triggered buffers
* Integration time
https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
--- .../bindings/iio/light/max44009.txt | 25 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/max44009.c | 696 ++++++++++++++++++ 4 files changed, 735 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/max44009.txt create mode 100644 drivers/iio/light/max44009.c
Documentation/devicetree/bindings/iio/light/max44009.txt new file +25 -0
@@ -0,0 +1,25 @@ +* MAX44009 Ambient Light Sensor + +Required properties: + +- compatible: should be "maxim,max44009" +- reg: the I2C address of the device (default is <0x4a>) + +Optional properties: + +- interrupts : interrupt mapping for GPIO IRQ. Should be configured with + IRQ_TYPE_EDGE_FALLING. + +Refer to interrupt-controller/interrupts.txt for generic interrupt client +node bindings. + +Example: + +max44009: max44009@4a { + compatible = "maxim,max44009"; + reg = <0x4a>; + + interrupt-parent = <&gpio1>; + interrupts = <17 IRQ_TYPE_EDGE_FALLING>; +}; +
drivers/iio/light/Kconfig +13 -0
@@ -299,6 +299,19 @@ config MAX44000 To compile this driver as a module, choose M here: the module will be called max44000. +config MAX44009 + tristate "MAX44009 Ambient Light Sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_TRIGGERED_EVENT + help + Say Y here if you want to build support for Maxim Integrated's + MAX44009 ambient light sensor device. + + To compile this driver as a module, choose M here: + the module will be called max44009. + config OPT3001 tristate "Texas Instruments OPT3001 Light Sensor" depends on I2C
drivers/iio/light/Makefile +1 -0
@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_LV0104CS) += lv0104cs.o obj-$(CONFIG_MAX44000) += max44000.o +obj-$(CONFIG_MAX44009) += max44009.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_RPR0521) += rpr0521.o
drivers/iio/light/max44009.c new file +696 -0
696 lines not on this page · raw message
Signature · 1 line
2.20.1
Jonathan Cameron
maintainerOn Wed, 16 Jan 2019 22:56:23 -0800
Robert Eshleman <[email protected]> wrote:
Hi Robert,
Note I review drivers backwards, so comments may make more sense that way around.
Quoted 6 lines
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Quoted 6 lines
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
events
Not really on this one. You support using them as a trigger, which is not how threshold events should be handled in IIO. Please
report them as events. This device doesn't seem to have
a trigger that can be used in general, so you shouldn't provide one.
Userspace can use the event to decide to do a read if it wants to follow the classic move the thresholds so as to detect big 'changes' in light intensity.
Various other comments inline. Quite a bit of style cleanup needed as well, please check the kernel docs for coding style
https://www.kernel.org/doc/Documentation/process/coding-style.rst
Also take a look at other IIO drivers for the bits that are noted in there as varying across the kernel.
Whilst there is quite a bit to work on in here yet, great to see support for this new part! Looking forward to v2.
Jonathan
Quoted 91 lines
* Illuminance triggered buffers
* Integration time
https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
Signed-off-by Robert Eshleman <[email protected]>--- .../bindings/iio/light/max44009.txt | 25 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/max44009.c | 696 ++++++++++++++++++ 4 files changed, 735 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/max44009.txt create mode 100644 drivers/iio/light/max44009.cDocumentation/devicetree/bindings/iio/light/max44009.txt new file +25
-0@@ -0,0 +1,25 @@ +* MAX44009 Ambient Light Sensor + +Required properties: + +- compatible: should be "maxim,max44009" +- reg: the I2C address of the device (default is <0x4a>) + +Optional properties: + +- interrupts : interrupt mapping for GPIO IRQ. Should be configured with + IRQ_TYPE_EDGE_FALLING. + +Refer to interrupt-controller/interrupts.txt for generic interrupt client +node bindings. + +Example: + +max44009: max44009@4a { + compatible = "maxim,max44009"; + reg = <0x4a>; + + interrupt-parent = <&gpio1>; + interrupts = <17 IRQ_TYPE_EDGE_FALLING>; +}; +drivers/iio/light/Kconfig +13
-0@@ -299,6 +299,19 @@ config MAX44000 To compile this driver as a module, choose M here: the module will be called max44000. +config MAX44009 + tristate "MAX44009 Ambient Light Sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_TRIGGERED_EVENT + help + Say Y here if you want to build support for Maxim Integrated's + MAX44009 ambient light sensor device. + + To compile this driver as a module, choose M here: + the module will be called max44009. + config OPT3001 tristate "Texas Instruments OPT3001 Light Sensor" depends on I2Cdrivers/iio/light/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_LV0104CS) += lv0104cs.o obj-$(CONFIG_MAX44000) += max44000.o +obj-$(CONFIG_MAX44009) += max44009.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_RPR0521) += rpr0521.odrivers/iio/light/max44009.c new file +0
-0
drivers/iio/light/max44009.c line 9
Quoted 9 lines
drivers/iio/light/max44009.c new file +9
-0@@ -0,0 +1,696 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* max44009.c - Support for MAX44009 Ambient Light Sensor +* +* Copyright (c) 2019 Robert Eshleman <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation.
If you have SPDX, no need to have a license statement.
drivers/iio/light/max44009.c line 17
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+* +* +* Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf +* +* TODO: Support continuous mode and processed event value (IIO_EV_INFO_VALUE) +* +* Default I2C address: 0x4a +*
No point in this blank line.
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+*/ + +#include <linux/bits.h> +#include <linux/i2c.h> +#include <linux/iio/buffer.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h>
drivers/iio/light/max44009.c line 37
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+#include <linux/iio/triggered_buffer.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/regmap.h> +#include <linux/util_macros.h> + +#define MAX44009_DRV_NAME "max44009" +#define MAX44009_IRQ_NAME "max44009_event"
Unless used more than once, no point in defines like this. Just put the string inline.
drivers/iio/light/max44009.c line 49
Quoted 12 lines
drivers/iio/light/max44009.c new file +12
-0+ +/* Registers in datasheet order */ +#define MAX44009_REG_STATUS 0x0 +#define MAX44009_REG_ENABLE 0x1 +#define MAX44009_REG_CFG 0x2 +#define MAX44009_REG_LUX_HI 0x3 +#define MAX44009_REG_LUX_LO 0x4 +#define MAX44009_REG_UPPER_THR 0x5 +#define MAX44009_REG_LOWER_THR 0x6 +#define MAX44009_REG_THR_TIMER 0x7 + +#define MAX44009_INT_TIME_MASK (BIT(2) | BIT(1) | BIT(0))
GENMASK
Quoted 7 lines
drivers/iio/light/max44009.c new file +7
-0+#define MAX44009_INT_TIME_SHIFT (0) + +#define MAX44009_MANUAL_MODE_MASK BIT(6) + +/* The maxmimum raw rising threshold for the max44009 */ +#define MAX44009_MAXIMUM_THRESHOLD 8355840 +
drivers/iio/light/max44009.c line 66
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+#define MAX44009_HI_NIBBLE (reg) (((reg) >> 4) & 0xf) +#define MAX44009_LO_NIBBLE (reg) ((reg) & 0xf) + +#define MAX44009_EXP_MASK 0xf00 +#define MAX44009_EXP_RSHIFT 8 +#define MAX44009_LUX_EXP (reg) \ + (1 << (((reg) & MAX44009_EXP_MASK) >> MAX44009_EXP_RSHIFT)) +#define MAX44009_LUX_MANT (reg) ((reg) & 0xff) + +#define MAX44009_LUX (reg) (MAX44009_LUX_EXP(reg) * MAX44009_LUX_MANT(reg))
These are complex enough that a few small functions with documentation would be better than macros.
Quoted 19 lines
drivers/iio/light/max44009.c new file +18
-0+ +#define MAX44009_THRESH_MANT (reg) ((MAX44009_LO_NIBBLE(reg) << 4) + 15) +#define MAX44009_THRESHOLD (reg) \ + ((1 << MAX44009_HI_NIBBLE(reg)) * MAX44009_THRESH_MANT(reg)) + +static const u32 max44009_int_time_ns_array[] = { +800000000 , +400000000 , +200000000 , +100000000 , +50000000 ,/* Manual mode only */ +25000000 ,/* Manual mode only */ +12500000 ,/* Manual mode only */ +6250000 ,/* Manual mode only */ +}; + +static const char max44009_int_time_str[] = +"0.8 "
drivers/iio/light/max44009.c line 94
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+"0.4 " +"0.2 " +"0.1 " +"0.05 " +"0.025 " +"0.0125 " +"0.00625" ; + +static const u8 max44009_scale_avail_ulux_array[] = {45 }; +static const char max44009_scale_avail_str[] ="0.045" ;
If there is only one value, don't provide the avail stuff and just put the value in directly where used.
Quoted 38 lines
drivers/iio/light/max44009.c new file +38
-0+ +struct max44009_data { +struct i2c_client *client; +struct iio_trigger *trigger; +struct mutex lock; +int64_t timestamp; +}; + +static const struct iio_event_spec max44009_event_spec[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec max44009_channels[] = { + { + .type =IIO_LIGHT , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_INT_TIME ), + .scan_index =0 , + .scan_type = { + .sign ='u' , + .realbits =24 , + .storagebits =32 , + }, + .event_spec = max44009_event_spec, + .num_event_specs =ARRAY_SIZE (max44009_event_spec), + }, +IIO_CHAN_SOFT_TIMESTAMP (1 ),
drivers/iio/light/max44009.c line 142
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+}; + +static int max44009_read_reg (struct max44009_data *data,char reg) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock); + ret =i2c_smbus_read_byte_data (client, reg); +if (ret <0 ) {
Same as below.
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+dev_err (&client->dev, +"failed to read reg 0x%0x, err: %d\n" , reg, ret); + goto err; + } + +err:
drivers/iio/light/max44009.c line 158
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+mutex_unlock (&data->lock); +return ret; +} + +static int max44009_write_reg (struct max44009_data *data,char reg,char buf) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock);
What is this mutex protecting?
drivers/iio/light/max44009.c
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+ ret =i2c_smbus_write_byte_data (client, reg, buf); +if (ret <0 ) {
returns 0 on success. So do
if (ret) as it'll prove simpler to follow for handling later.
Quoted 45 lines
drivers/iio/light/max44009.c new file +45
-0+dev_err (&client->dev, +"failed to write reg 0x%0x, err: %d\n" , + reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_read_int_time (struct max44009_data *data) +{ +int ret =max44009_read_reg (data,MAX44009_REG_CFG ); + +if (ret <0 ) { +return ret; + } + +return max44009_int_time_ns_array[ret &MAX44009_INT_TIME_MASK ]; +} + +static int max44009_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret, int_time; +s64 ns; + +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) { + ns = val *NSEC_PER_SEC + val2; + int_time =find_closest_descending ( + ns, + max44009_int_time_ns_array, +ARRAY_SIZE (max44009_int_time_ns_array)); + + ret =max44009_read_reg (data,MAX44009_REG_CFG ); +if (ret <0 ) +return ret; + + ret &= ~MAX44009_INT_TIME_MASK ; + ret |= (int_time <<MAX44009_INT_TIME_SHIFT ); + ret |=MAX44009_MANUAL_MODE_MASK ; +
drivers/iio/light/max44009.c line 215
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return max44009_write_reg (data,MAX44009_REG_CFG , ret); + } +return -EINVAL ; +} + +static int max44009_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) {
Make sure to get rid of all the brackets where kernel style doesn't have them. Basically any single statements if or else blocks (or simiilar).
drivers/iio/light/max44009.c line 217
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+return IIO_VAL_INT_PLUS_NANO ; + }else {
You don't have any others...
Quoted 242 lines
drivers/iio/light/max44009.c new file +241
-0+return IIO_VAL_INT ; + } +} + +#define READ_LUX_XFER_LEN (4) + +static int max44009_read_lux_raw (struct max44009_data *data) +{ +int ret; +struct i2c_msg xfer[READ_LUX_XFER_LEN ]; +u8 luxhireg[1 ] = {MAX44009_REG_LUX_HI }; +u8 luxloreg[1 ] = {MAX44009_REG_LUX_LO }; +u8 lo =0 ; +u8 hi =0 ; +u16 reg =0 ; + + xfer[0 ].addr = data->client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =1 ; + xfer[0 ].buf = luxhireg; + + xfer[1 ].addr = data->client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =1 ; + xfer[1 ].buf = &hi; + + xfer[2 ].addr = data->client->addr; + xfer[2 ].flags =0 ; + xfer[2 ].len =1 ; + xfer[2 ].buf = luxloreg; + + xfer[3 ].addr = data->client->addr; + xfer[3 ].flags =I2C_M_RD ; + xfer[3 ].len =1 ; + xfer[3 ].buf = &lo; + +/* +* Use i2c_transfer instead of smbus read because i2c_transfer +* does NOT use a stop bit between address write and data read. +* Using a stop bit causes disjoint upper/lower byte reads and +* reduces accuracy +*/ +mutex_lock (&data->lock); + ret =i2c_transfer (data->client->adapter, xfer,READ_LUX_XFER_LEN ); +mutex_unlock (&data->lock); +if (ret !=READ_LUX_XFER_LEN ) { +return -EIO ; + } + + reg = (((u16 )hi) <<4 ) | (lo &0xf ); + +return MAX44009_LUX (reg); +} + +static int max44009_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : { +switch (chan->type) { +case IIO_LIGHT : { + ret =max44009_read_lux_raw (data); +if (ret <0 ) { +return ret; + } + *val = ret; + *val2 =0 ; +return IIO_VAL_INT ; + } +default : +return -EINVAL ; + } +break ; + } + +case IIO_CHAN_INFO_INT_TIME : { + ret =max44009_read_int_time (data); +if (ret <0 ) { +return ret; + } + + *val2 = ret; + *val =0 ; +return IIO_VAL_INT_PLUS_NANO ; + } + +default : +return -EINVAL ; + } +} + +static IIO_CONST_ATTR(illuminance_scale_available, max44009_scale_avail_str); +static IIO_CONST_ATTR(illuminance_integration_time_available, + max44009_int_time_str); + +static struct attribute *max44009_attributes[] = { + &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, + &iio_const_attr_illuminance_scale_available.dev_attr.attr,NULL +}; + +static const struct attribute_group max44009_attribute_group = { + .attrs = max44009_attributes, +}; + +static int max44009_thresh_byte_from_int (int thresh) +{ +int mantissa, exp; + +if (thresh <0 || thresh >MAX44009_MAXIMUM_THRESHOLD ) { +return -EINVAL ; + } + +for (mantissa = thresh, exp =0 ; mantissa >0xff ; exp++) { + mantissa >>=1 ; + } + + mantissa >>=4 ; + mantissa &=0xf ; + exp <<=4 ; + +return exp | mantissa; +} + +static int max44009_get_thr_reg (enum iio_event_direction dir) +{ +switch (dir) { +case IIO_EV_DIR_RISING : +return MAX44009_REG_UPPER_THR ; +case IIO_EV_DIR_FALLING : +return MAX44009_REG_LOWER_THR ; +default : +return -EINVAL ; + } +} + +static int max44009_write_thresh (struct iio_dev *indio_dev, +enum iio_event_direction dir,int val) +{ +struct max44009_data *data =iio_priv (indio_dev); +int thresh; +int reg; + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_thresh_byte_from_int (val); +if (thresh <0 ) { +return thresh; + } + +return max44009_write_reg (data, reg, thresh); +} + +static int max44009_write_event_value (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) +{ +if (info !=IIO_EV_INFO_VALUE || chan->type !=IIO_LIGHT || val2 !=0 ) { +return -EINVAL ; + } + +return max44009_write_thresh (indio_dev, dir, val); +} + +static int max44009_read_event_value (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) +{ +int thresh, reg; +struct max44009_data *data =iio_priv (indio_dev); + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_read_reg (data, reg); +if (thresh <0 ) { +return thresh; + } + + *val =MAX44009_THRESHOLD (thresh); + +return IIO_VAL_INT ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , state); +if (ret <0 ) { +return ret; + } + +/* +* Set device to trigger interrupt immediately upon exceeding +* the threshold limit +*/ + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) { +return ret; + } + +return 0 ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) {
drivers/iio/light/max44009.c line 468
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return -EINVAL ; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <0 ) { +return ret; + } + +return ret; +}
We have lots of event config, but they are actually controlling the trigger. The device doesn't generate any iio events at all that I can see.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +static const struct iio_info max44009_info = { + .read_raw = max44009_read_raw, + .write_raw = max44009_write_raw, + .write_raw_get_fmt = max44009_write_raw_get_fmt, + .read_event_value = max44009_read_event_value, + .read_event_config = max44009_read_event_config, + .write_event_value = max44009_write_event_value, + .write_event_config = max44009_write_event_config, + .attrs = &max44009_attribute_group, +}; + +static int max44009_set_trigger_state (struct iio_trigger *trigger, +bool enable) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trigger);
drivers/iio/light/max44009.c line 494
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+struct max44009_data *data =iio_priv (indio_dev); +int ret; + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , enable); +if (ret <0 ) { +return ret; + } + + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) {
As suggested above, write_reg should clearly only return 0 or negative. you can just do
return max440..
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return ret; + } + +return ret; +} + +static const struct iio_trigger_ops max44009_trigger_ops = { + .set_trigger_state = max44009_set_trigger_state, +}; +
drivers/iio/light/max44009.c line 514
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+static irqreturn_t max44009_trigger_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); +int lux, upper, lower; +int ret; +enum iio_event_direction direction; + +/* 32-bit for lux and 64-bit for timestamp */ +u32 buf[3 ] = {0 };
Except the timestamp needs to be 64 bit aligned so this isn't big enough. All elements in IIO buffers are 'naturally' aligned. so 32 bit is aligned to 32 bits, 64 to 64 bits.
drivers/iio/light/max44009.c line 524
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <=0 ) { + goto err; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <=0 ) { + goto err; + }
If these reads are necessary, then add comments on why.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +/* Clear interrupt by disabling interrupt (see datasheet) */ + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,0 ); +if (ret <0 ) { + goto err; + } + + lux =max44009_read_lux_raw (data); +if (lux <0 ) { + goto err; + } + + upper =max44009_read_reg (data,MAX44009_REG_UPPER_THR ); +if (upper <0 ) { + goto err; + }
drivers/iio/light/max44009.c line 550
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ upper =MAX44009_THRESHOLD (upper); + + lower =max44009_read_reg (data,MAX44009_REG_LOWER_THR ); +if (lower <0 ) { + goto err; + } + lower =MAX44009_THRESHOLD (lower); + +/* If lux is NOT out-of-bounds then the interrupt was not triggered +* by this device
Multiline comment syntax in IIO (and most of the kernel) is /*
* If...
*/
Do we support shared interrupt lines? Doesn't look like it, which means it 'probably was' generate by this device, but we don't know why because the value has changed.
Returning IRQ_NONE is a bad idea unless we are pretty sure it is a spurious interrupt, or we have a shared interrupt line. It will ultimately result in your interrupt being disable by the kernel and all activity breaking.
There is also a perfectly good register to tell use if we generated the interrupt. Read that one and use that, not this racey approach.
Quoted 32 lines
drivers/iio/light/max44009.c new file +32
-0+*/ +if (lux < upper && lux > lower) { + goto err; + } + +/* Get event for correct thresh direction */ +if (lux >= upper) { + direction =IIO_EV_DIR_RISING ; + }else if (lux <= lower) { + direction =IIO_EV_DIR_FALLING ; + }else { + goto err; + } + + buf[0 ] = lux; +iio_push_to_buffers_with_timestamp (indio_dev, &buf, data->timestamp); +iio_trigger_notify_done (data->trigger); + +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , direction), + data->timestamp); + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +if (ret <0 ) { + goto err; + } + +return IRQ_HANDLED ; + +err: +/* Re-enable interrupt */
drivers/iio/light/max44009.c line 592
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +return IRQ_NONE ; +} + +static irqreturn_t max44009_irq_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); + + data->timestamp =iio_get_time_ns (indio_dev);
We have a standard core function to do this..iio_pollfunc_store_time.
There 'might' be a reason to do it differently depending on whether you really need it to be that good if you can't identify whether it is your interrupt until the interrupt thread.
Quoted 24 lines
drivers/iio/light/max44009.c new file +24
-0+return IRQ_WAKE_THREAD ; +} + +static int max44009_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct max44009_data *data; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) { +return -ENOMEM ; + } + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev); + data->client = client; + indio_dev->dev.parent = &client->dev; + indio_dev->info = &max44009_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name =MAX44009_DRV_NAME ; + indio_dev->channels = max44009_channels; + indio_dev->num_channels =ARRAY_SIZE (max44009_channels); +mutex_init (&data->lock);
drivers/iio/light/max44009.c line 626
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +/* Clear stale interrupt bit */ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <0 ) { + goto err; + } + +if (client->irq >0 ) { + ret = devm_request_threaded_irq(&client->dev, client->irq, + max44009_irq_handler,
This is wrong. The interrupt handler should call the iio_trigger_poll functions to cause the trigger handlers for all attached devices to be called.
If for some reason the trigger is only suitable for use by this device then things get more blurred, but if not, the interrupt handler itself should establish that the interrupt is the data ready signal and call iio_trigger_poll. If it is in a thread, call iio_trigger_poll_chained.
The interrupt should be cleared (if it hasn't naturally happened for some other reason, in the trigger try_reenable callback.
drivers/iio/light/max44009.c line 639
Quoted 13 lines
drivers/iio/light/max44009.c new file +13
-0+ max44009_trigger_handler, +IRQF_TRIGGER_FALLING | +IRQF_ONESHOT , + MAX44009_IRQ_NAME, indio_dev); +if (ret <0 ) { + goto err; + } + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + max44009_irq_handler, + max44009_trigger_handler, +NULL ); +if (ret <0 ) {
It's going to change anyway (see below) but note kernel style is no brackets when only one item in an block like this.
Quoted 14 lines
drivers/iio/light/max44009.c new file +14
-0+ goto err; + } + + data->trigger =devm_iio_trigger_alloc (indio_dev->dev.parent, +"%s-dev%d" , + indio_dev->name, + indio_dev->id); +if (!data->trigger) { + ret = -ENOMEM ; + goto err; + } + data->trigger->dev.parent = indio_dev->dev.parent; + data->trigger->ops = &max44009_trigger_ops; +iio_trigger_set_drvdata (data->trigger, indio_dev);
drivers/iio/light/max44009.c line 663
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =devm_iio_trigger_register (&client->dev, data->trigger); +if (ret <0 ) { + goto err; + } + } + + ret =devm_iio_device_register (&client->dev, indio_dev); +if (ret <0 ) { + goto err;
Without the mutex destroy these all just become return ret;
drivers/iio/light/max44009.c line 668
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+ } + +return 0 ; +err: +mutex_destroy (&data->lock);
mutex destroy is only really useful for lock debugging. Given we don't leave anything behind here anyway, it just makes the flow more complex for no gain. We very rarely bother with it as a result.
Quoted 17 lines
drivers/iio/light/max44009.c new file +17
-0+return ret; +} + +static const struct i2c_device_id max44009_id[] = { + {"max44009" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (i2c, max44009_id); + +static struct i2c_driver max44009_driver = { + .driver = { + .name =MAX44009_DRV_NAME , + }, + .probe = max44009_probe, + .id_table = max44009_id, +}; +module_i2c_driver (max44009_driver);
drivers/iio/light/max44009.c line 695
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +static const struct of_device_id max44009_of_match[] = { + { .compatible ="maxim,max44009" }, + { } +}; +MODULE_DEVICE_TABLE (of, max44009_of_match); + +MODULE_AUTHOR ("Robert Eshleman <[email protected]>" ); +MODULE_LICENSE ("GPL v2" ); +MODULE_VERSION ("1.0.0" );
Please drop. MODULE_VERSION provides very little useful info and tends to lead to people assuming it does.
Userspace has no obligation to ever look at it and we can't break userspace that doesn't so it doesn't provide any value.
Quoted 1 line
+MODULE_DESCRIPTION("MAX44009 ambient light sensor driver");
On Sat, 19 Jan 2019, Jonathan Cameron wrote:
some more comments from my side below...
Quoted 61 lines
On Wed, 16 Jan 2019 22:56:23 -0800
Robert Eshleman <[email protected]> wrote:Hi Robert,
Note I review drivers backwards, so comments may make more sense that way around.
Quoted 6 lines
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Quoted 6 lines
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
eventsNot really on this one. You support using them as a trigger, which is not how threshold events should be handled in IIO. Please
report them as events. This device doesn't seem to have
a trigger that can be used in general, so you shouldn't provide one.Userspace can use the event to decide to do a read if it wants to follow the classic move the thresholds so as to detect big 'changes' in light intensity.
Various other comments inline. Quite a bit of style cleanup needed as well, please check the kernel docs for coding style
https://www.kernel.org/doc/Documentation/process/coding-style.rstAlso take a look at other IIO drivers for the bits that are noted in there as varying across the kernel.
Whilst there is quite a bit to work on in here yet, great to see support for this new part! Looking forward to v2.
Jonathan
Quoted 18 lines
* Illuminance triggered buffers
* Integration time
https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
Signed-off-by Robert Eshleman <[email protected]>--- .../bindings/iio/light/max44009.txt | 25 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/max44009.c | 696 ++++++++++++++++++ 4 files changed, 735 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/max44009.txt create mode 100644 drivers/iio/light/max44009.c
Quoted 16 lines
Quoted 6 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +0
-0Documentation/devicetree/bindings/iio/light/max44009.txt line 10
Quoted 10 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +10
-0@@ -0,0 +1,25 @@ +* MAX44009 Ambient Light Sensor + +Required properties: + +- compatible: should be "maxim,max44009" +- reg: the I2C address of the device (default is <0x4a>) + +Optional properties: + +- interrupts : interrupt mapping for GPIO IRQ. Should be configured with
the space before the colon hurts my eyes
Quoted 101 lines
Quoted 57 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +15
-0+ IRQ_TYPE_EDGE_FALLING. + +Refer to interrupt-controller/interrupts.txt for generic interrupt client +node bindings. + +Example: + +max44009: max44009@4a { + compatible = "maxim,max44009"; + reg = <0x4a>; + + interrupt-parent = <&gpio1>; + interrupts = <17 IRQ_TYPE_EDGE_FALLING>; +}; +drivers/iio/light/Kconfig +13
-0@@ -299,6 +299,19 @@ config MAX44000 To compile this driver as a module, choose M here: the module will be called max44000. +config MAX44009 + tristate "MAX44009 Ambient Light Sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_TRIGGERED_EVENT + help + Say Y here if you want to build support for Maxim Integrated's + MAX44009 ambient light sensor device. + + To compile this driver as a module, choose M here: + the module will be called max44009. + config OPT3001 tristate "Texas Instruments OPT3001 Light Sensor" depends on I2Cdrivers/iio/light/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_LV0104CS) += lv0104cs.o obj-$(CONFIG_MAX44000) += max44000.o +obj-$(CONFIG_MAX44009) += max44009.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_RPR0521) += rpr0521.odrivers/iio/light/max44009.c new file +0
-0drivers/iio/light/max44009.c line 9
Quoted 9 lines
drivers/iio/light/max44009.c new file +9
-0@@ -0,0 +1,696 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* max44009.c - Support for MAX44009 Ambient Light Sensor +* +* Copyright (c) 2019 Robert Eshleman <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. If you have SPDX, no need to have a license statement.
drivers/iio/light/max44009.c line 17
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+* +* +* Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf +* +* TODO: Support continuous mode and processed event value (IIO_EV_INFO_VALUE) +* +* Default I2C address: 0x4a +* No point in this blank line.
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+*/ + +#include <linux/bits.h> +#include <linux/i2c.h> +#include <linux/iio/buffer.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> drivers/iio/light/max44009.c line 37
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+#include <linux/iio/triggered_buffer.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/regmap.h> +#include <linux/util_macros.h> + +#define MAX44009_DRV_NAME "max44009" +#define MAX44009_IRQ_NAME "max44009_event"Unless used more than once, no point in defines like this. Just put the string inline.
Quoted 16 lines
drivers/iio/light/max44009.c line 49
Quoted 12 lines
drivers/iio/light/max44009.c new file +12
-0+ +/* Registers in datasheet order */ +#define MAX44009_REG_STATUS 0x0 +#define MAX44009_REG_ENABLE 0x1 +#define MAX44009_REG_CFG 0x2 +#define MAX44009_REG_LUX_HI 0x3 +#define MAX44009_REG_LUX_LO 0x4 +#define MAX44009_REG_UPPER_THR 0x5 +#define MAX44009_REG_LOWER_THR 0x6 +#define MAX44009_REG_THR_TIMER 0x7 + +#define MAX44009_INT_TIME_MASK (BIT(2) | BIT(1) | BIT(0))GENMASK
Quoted 1 line
drivers/iio/light/max44009.c new file +1
-0+#define MAX44009_INT_TIME_SHIFT (0)
shift 0 needed?
Quoted 4 lines
drivers/iio/light/max44009.c line 54
Quoted 4 lines
drivers/iio/light/max44009.c new file +4
-0+ +#define MAX44009_MANUAL_MODE_MASK BIT(6) + +/* The maxmimum raw rising threshold for the max44009 */
maximum
Quoted 40 lines
drivers/iio/light/max44009.c line 66
Quoted 12 lines
drivers/iio/light/max44009.c new file +12
-0+#define MAX44009_MAXIMUM_THRESHOLD 8355840 + +#define MAX44009_HI_NIBBLE (reg) (((reg) >> 4) & 0xf) +#define MAX44009_LO_NIBBLE (reg) ((reg) & 0xf) + +#define MAX44009_EXP_MASK 0xf00 +#define MAX44009_EXP_RSHIFT 8 +#define MAX44009_LUX_EXP (reg) \ + (1 << (((reg) & MAX44009_EXP_MASK) >> MAX44009_EXP_RSHIFT)) +#define MAX44009_LUX_MANT (reg) ((reg) & 0xff) + +#define MAX44009_LUX (reg) (MAX44009_LUX_EXP(reg) * MAX44009_LUX_MANT(reg))These are complex enough that a few small functions with documentation would be better than macros.
Quoted 24 lines
drivers/iio/light/max44009.c new file +23
-0+ +#define MAX44009_THRESH_MANT (reg) ((MAX44009_LO_NIBBLE(reg) << 4) + 15) +#define MAX44009_THRESHOLD (reg) \ + ((1 << MAX44009_HI_NIBBLE(reg)) * MAX44009_THRESH_MANT(reg)) + +static const u32 max44009_int_time_ns_array[] = { +800000000 , +400000000 , +200000000 , +100000000 , +50000000 ,/* Manual mode only */ +25000000 ,/* Manual mode only */ +12500000 ,/* Manual mode only */ +6250000 ,/* Manual mode only */ +}; + +static const char max44009_int_time_str[] = +"0.8 " +"0.4 " +"0.2 " +"0.1 " +"0.05 " +"0.025 "
Quoted 8 lines
drivers/iio/light/max44009.c line 94
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+"0.0125 " +"0.00625" ; + +static const u8 max44009_scale_avail_ulux_array[] = {45 }; +static const char max44009_scale_avail_str[] ="0.045" ;If there is only one value, don't provide the avail stuff and just put the value in directly where used.
CHAN_INFO_SCALE is not exposed but should be to express the 0.045 multiplier
Quoted 105 lines
Quoted 38 lines
drivers/iio/light/max44009.c new file +38
-0+ +struct max44009_data { +struct i2c_client *client; +struct iio_trigger *trigger; +struct mutex lock; +int64_t timestamp; +}; + +static const struct iio_event_spec max44009_event_spec[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec max44009_channels[] = { + { + .type =IIO_LIGHT , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_INT_TIME ), + .scan_index =0 , + .scan_type = { + .sign ='u' , + .realbits =24 , + .storagebits =32 , + }, + .event_spec = max44009_event_spec, + .num_event_specs =ARRAY_SIZE (max44009_event_spec), + }, +IIO_CHAN_SOFT_TIMESTAMP (1 ),drivers/iio/light/max44009.c line 142
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+}; + +static int max44009_read_reg (struct max44009_data *data,char reg) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock); + ret =i2c_smbus_read_byte_data (client, reg); +if (ret <0 ) {Same as below.
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+dev_err (&client->dev, +"failed to read reg 0x%0x, err: %d\n" , reg, ret); + goto err; + } + +err:drivers/iio/light/max44009.c line 158
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+mutex_unlock (&data->lock); +return ret; +} + +static int max44009_write_reg (struct max44009_data *data,char reg,char buf) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock);What is this mutex protecting?
drivers/iio/light/max44009.c
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+ ret =i2c_smbus_write_byte_data (client, reg, buf); +if (ret <0 ) {returns 0 on success. So do
if (ret) as it'll prove simpler to follow for handling later.Quoted 29 lines
drivers/iio/light/max44009.c new file +29
-0+dev_err (&client->dev, +"failed to write reg 0x%0x, err: %d\n" , + reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_read_int_time (struct max44009_data *data) +{ +int ret =max44009_read_reg (data,MAX44009_REG_CFG ); + +if (ret <0 ) { +return ret; + } + +return max44009_int_time_ns_array[ret &MAX44009_INT_TIME_MASK ]; +} + +static int max44009_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret, int_time; +s64 ns;
Quoted 8 lines
drivers/iio/light/max44009.c line 197
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) { + ns = val *NSEC_PER_SEC + val2; + int_time =find_closest_descending ( + ns, + max44009_int_time_ns_array, +ARRAY_SIZE (max44009_int_time_ns_array)); +
a mutex might make sense here to protect the read/write combo
Quoted 27 lines
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ ret =max44009_read_reg (data,MAX44009_REG_CFG ); +if (ret <0 ) +return ret; + + ret &= ~MAX44009_INT_TIME_MASK ; + ret |= (int_time <<MAX44009_INT_TIME_SHIFT ); + ret |=MAX44009_MANUAL_MODE_MASK ; +drivers/iio/light/max44009.c line 215
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return max44009_write_reg (data,MAX44009_REG_CFG , ret); + } +return -EINVAL ; +} + +static int max44009_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) {Make sure to get rid of all the brackets where kernel style doesn't have them. Basically any single statements if or else blocks (or simiilar).
drivers/iio/light/max44009.c line 217
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+return IIO_VAL_INT_PLUS_NANO ; + }else {You don't have any others...
Quoted 5 lines
drivers/iio/light/max44009.c line 222
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+return IIO_VAL_INT ; + } +} + +#define READ_LUX_XFER_LEN (4)
MAX44009_ prefix please, no need for ()
Quoted 7 lines
drivers/iio/light/max44009.c line 229
Quoted 7 lines
drivers/iio/light/max44009.c new file +7
-0+ +static int max44009_read_lux_raw (struct max44009_data *data) +{ +int ret; +struct i2c_msg xfer[READ_LUX_XFER_LEN ]; +u8 luxhireg[1 ] = {MAX44009_REG_LUX_HI }; +u8 luxloreg[1 ] = {MAX44009_REG_LUX_LO };
a simple u8 would work as well, no array
Quoted 7 lines
drivers/iio/light/max44009.c line 236
Quoted 7 lines
drivers/iio/light/max44009.c new file +7
-0+u8 lo =0 ; +u8 hi =0 ; +u16 reg =0 ; + + xfer[0 ].addr = data->client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =1 ;
sizeof(luxhireg)?
Quoted 5 lines
drivers/iio/light/max44009.c line 241
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+ xfer[0 ].buf = luxhireg; + + xfer[1 ].addr = data->client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =1 ;
sizeof(&hi)?
Quoted 11 lines
Quoted 11 lines
drivers/iio/light/max44009.c new file +11
-0+ xfer[1 ].buf = &hi; + + xfer[2 ].addr = data->client->addr; + xfer[2 ].flags =0 ; + xfer[2 ].len =1 ; + xfer[2 ].buf = luxloreg; + + xfer[3 ].addr = data->client->addr; + xfer[3 ].flags =I2C_M_RD ; + xfer[3 ].len =1 ; + xfer[3 ].buf = &lo;
Quoted 8 lines
drivers/iio/light/max44009.c line 260
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ +/* +* Use i2c_transfer instead of smbus read because i2c_transfer +* does NOT use a stop bit between address write and data read. +* Using a stop bit causes disjoint upper/lower byte reads and +* reduces accuracy +*/ +mutex_lock (&data->lock);
mutex needed?
Quoted 46 lines
Quoted 46 lines
drivers/iio/light/max44009.c new file +45
-0+ ret =i2c_transfer (data->client->adapter, xfer,READ_LUX_XFER_LEN ); +mutex_unlock (&data->lock); +if (ret !=READ_LUX_XFER_LEN ) { +return -EIO ; + } + + reg = (((u16 )hi) <<4 ) | (lo &0xf ); + +return MAX44009_LUX (reg); +} + +static int max44009_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : { +switch (chan->type) { +case IIO_LIGHT : { + ret =max44009_read_lux_raw (data); +if (ret <0 ) { +return ret; + } + *val = ret; + *val2 =0 ; +return IIO_VAL_INT ; + } +default : +return -EINVAL ; + } +break ; + } + +case IIO_CHAN_INFO_INT_TIME : { + ret =max44009_read_int_time (data); +if (ret <0 ) { +return ret; + } + + *val2 = ret; + *val =0 ; +return IIO_VAL_INT_PLUS_NANO ;
Quoted 8 lines
drivers/iio/light/max44009.c line 313
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ } + +default : +return -EINVAL ; + } +} + +static IIO_CONST_ATTR(illuminance_scale_available, max44009_scale_avail_str);
channels do not have CHAN_INFO_SCALE, so why expose scale_available?
Quoted 458 lines
Quoted 147 lines
drivers/iio/light/max44009.c new file +145
-0+static IIO_CONST_ATTR(illuminance_integration_time_available, + max44009_int_time_str); + +static struct attribute *max44009_attributes[] = { + &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, + &iio_const_attr_illuminance_scale_available.dev_attr.attr,NULL +}; + +static const struct attribute_group max44009_attribute_group = { + .attrs = max44009_attributes, +}; + +static int max44009_thresh_byte_from_int (int thresh) +{ +int mantissa, exp; + +if (thresh <0 || thresh >MAX44009_MAXIMUM_THRESHOLD ) { +return -EINVAL ; + } + +for (mantissa = thresh, exp =0 ; mantissa >0xff ; exp++) { + mantissa >>=1 ; + } + + mantissa >>=4 ; + mantissa &=0xf ; + exp <<=4 ; + +return exp | mantissa; +} + +static int max44009_get_thr_reg (enum iio_event_direction dir) +{ +switch (dir) { +case IIO_EV_DIR_RISING : +return MAX44009_REG_UPPER_THR ; +case IIO_EV_DIR_FALLING : +return MAX44009_REG_LOWER_THR ; +default : +return -EINVAL ; + } +} + +static int max44009_write_thresh (struct iio_dev *indio_dev, +enum iio_event_direction dir,int val) +{ +struct max44009_data *data =iio_priv (indio_dev); +int thresh; +int reg; + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_thresh_byte_from_int (val); +if (thresh <0 ) { +return thresh; + } + +return max44009_write_reg (data, reg, thresh); +} + +static int max44009_write_event_value (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) +{ +if (info !=IIO_EV_INFO_VALUE || chan->type !=IIO_LIGHT || val2 !=0 ) { +return -EINVAL ; + } + +return max44009_write_thresh (indio_dev, dir, val); +} + +static int max44009_read_event_value (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) +{ +int thresh, reg; +struct max44009_data *data =iio_priv (indio_dev); + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_read_reg (data, reg); +if (thresh <0 ) { +return thresh; + } + + *val =MAX44009_THRESHOLD (thresh); + +return IIO_VAL_INT ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , state); +if (ret <0 ) { +return ret; + } + +/* +* Set device to trigger interrupt immediately upon exceeding +* the threshold limit +*/ + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) { +return ret; + } + +return 0 ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) {drivers/iio/light/max44009.c line 468
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return -EINVAL ; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <0 ) { +return ret; + } + +return ret; +}We have lots of event config, but they are actually controlling the trigger. The device doesn't generate any iio events at all that I can see.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +static const struct iio_info max44009_info = { + .read_raw = max44009_read_raw, + .write_raw = max44009_write_raw, + .write_raw_get_fmt = max44009_write_raw_get_fmt, + .read_event_value = max44009_read_event_value, + .read_event_config = max44009_read_event_config, + .write_event_value = max44009_write_event_value, + .write_event_config = max44009_write_event_config, + .attrs = &max44009_attribute_group, +}; + +static int max44009_set_trigger_state (struct iio_trigger *trigger, +bool enable) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trigger);drivers/iio/light/max44009.c line 494
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+struct max44009_data *data =iio_priv (indio_dev); +int ret; + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , enable); +if (ret <0 ) { +return ret; + } + + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) {As suggested above,
write_regshould clearly only return 0 or negative. you can just do
return max440..Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return ret; + } + +return ret; +} + +static const struct iio_trigger_ops max44009_trigger_ops = { + .set_trigger_state = max44009_set_trigger_state, +}; +drivers/iio/light/max44009.c line 514
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+static irqreturn_t max44009_trigger_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); +int lux, upper, lower; +int ret; +enum iio_event_direction direction; + +/* 32-bit for lux and 64-bit for timestamp */ +u32 buf[3 ] = {0 };Except the timestamp needs to be 64 bit aligned so this isn't big enough. All elements in IIO buffers are 'naturally' aligned. so 32 bit is aligned to 32 bits, 64 to 64 bits.
drivers/iio/light/max44009.c line 524
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <=0 ) { + goto err; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <=0 ) { + goto err; + }If these reads are necessary, then add comments on why.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +/* Clear interrupt by disabling interrupt (see datasheet) */ + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,0 ); +if (ret <0 ) { + goto err; + } + + lux =max44009_read_lux_raw (data); +if (lux <0 ) { + goto err; + } + + upper =max44009_read_reg (data,MAX44009_REG_UPPER_THR ); +if (upper <0 ) { + goto err; + }drivers/iio/light/max44009.c line 550
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ upper =MAX44009_THRESHOLD (upper); + + lower =max44009_read_reg (data,MAX44009_REG_LOWER_THR ); +if (lower <0 ) { + goto err; + } + lower =MAX44009_THRESHOLD (lower); + +/* If lux is NOT out-of-bounds then the interrupt was not triggered +* by this device Multiline comment syntax in IIO (and most of the kernel) is /*
* If...
*/Do we support shared interrupt lines? Doesn't look like it, which means it 'probably was' generate by this device, but we don't know why because the value has changed.
Returning
IRQ_NONEis a bad idea unless we are pretty sure it is a spurious interrupt, or we have a shared interrupt line. It will ultimately result in your interrupt being disable by the kernel and all activity breaking.There is also a perfectly good register to tell use if we generated the interrupt. Read that one and use that, not this racey approach.
Quoted 32 lines
drivers/iio/light/max44009.c new file +32
-0+*/ +if (lux < upper && lux > lower) { + goto err; + } + +/* Get event for correct thresh direction */ +if (lux >= upper) { + direction =IIO_EV_DIR_RISING ; + }else if (lux <= lower) { + direction =IIO_EV_DIR_FALLING ; + }else { + goto err; + } + + buf[0 ] = lux; +iio_push_to_buffers_with_timestamp (indio_dev, &buf, data->timestamp); +iio_trigger_notify_done (data->trigger); + +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , direction), + data->timestamp); + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +if (ret <0 ) { + goto err; + } + +return IRQ_HANDLED ; + +err: +/* Re-enable interrupt */ drivers/iio/light/max44009.c line 592
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +return IRQ_NONE ; +} + +static irqreturn_t max44009_irq_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); + + data->timestamp =iio_get_time_ns (indio_dev);We have a standard core function to do this..
iio_pollfunc_store_time.There 'might' be a reason to do it differently depending on whether you really need it to be that good if you can't identify whether it is your interrupt until the interrupt thread.
Quoted 24 lines
drivers/iio/light/max44009.c new file +24
-0+return IRQ_WAKE_THREAD ; +} + +static int max44009_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct max44009_data *data; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) { +return -ENOMEM ; + } + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev); + data->client = client; + indio_dev->dev.parent = &client->dev; + indio_dev->info = &max44009_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name =MAX44009_DRV_NAME ; + indio_dev->channels = max44009_channels; + indio_dev->num_channels =ARRAY_SIZE (max44009_channels); +mutex_init (&data->lock);drivers/iio/light/max44009.c line 626
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +/* Clear stale interrupt bit */ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <0 ) { + goto err; + } + +if (client->irq >0 ) { + ret = devm_request_threaded_irq(&client->dev, client->irq, + max44009_irq_handler,This is wrong. The interrupt handler should call the
iio_trigger_pollfunctions to cause the trigger handlers for all attached devices to be called.If for some reason the trigger is only suitable for use by this device then things get more blurred, but if not, the interrupt handler itself should establish that the interrupt is the data ready signal and call
iio_trigger_poll. If it is in a thread, calliio_trigger_poll_chained.The interrupt should be cleared (if it hasn't naturally happened for some other reason, in the trigger
try_reenablecallback.drivers/iio/light/max44009.c line 639
Quoted 13 lines
drivers/iio/light/max44009.c new file +13
-0+ max44009_trigger_handler, +IRQF_TRIGGER_FALLING | +IRQF_ONESHOT , + MAX44009_IRQ_NAME, indio_dev); +if (ret <0 ) { + goto err; + } + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + max44009_irq_handler, + max44009_trigger_handler, +NULL ); +if (ret <0 ) {It's going to change anyway (see below) but note kernel style is no brackets when only one item in an block like this.
Quoted 14 lines
drivers/iio/light/max44009.c new file +14
-0+ goto err; + } + + data->trigger =devm_iio_trigger_alloc (indio_dev->dev.parent, +"%s-dev%d" , + indio_dev->name, + indio_dev->id); +if (!data->trigger) { + ret = -ENOMEM ; + goto err; + } + data->trigger->dev.parent = indio_dev->dev.parent; + data->trigger->ops = &max44009_trigger_ops; +iio_trigger_set_drvdata (data->trigger, indio_dev);drivers/iio/light/max44009.c line 663
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =devm_iio_trigger_register (&client->dev, data->trigger); +if (ret <0 ) { + goto err; + } + } + + ret =devm_iio_device_register (&client->dev, indio_dev); +if (ret <0 ) { + goto err;Without the mutex destroy these all just become return ret;
drivers/iio/light/max44009.c line 668
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+ } + +return 0 ; +err: +mutex_destroy (&data->lock);mutex destroy is only really useful for lock debugging. Given we don't leave anything behind here anyway, it just makes the flow more complex for no gain. We very rarely bother with it as a result.
Quoted 17 lines
drivers/iio/light/max44009.c new file +17
-0+return ret; +} + +static const struct i2c_device_id max44009_id[] = { + {"max44009" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (i2c, max44009_id); + +static struct i2c_driver max44009_driver = { + .driver = { + .name =MAX44009_DRV_NAME , + }, + .probe = max44009_probe, + .id_table = max44009_id, +}; +module_i2c_driver (max44009_driver);drivers/iio/light/max44009.c line 695
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +static const struct of_device_id max44009_of_match[] = { + { .compatible ="maxim,max44009" }, + { } +}; +MODULE_DEVICE_TABLE (of, max44009_of_match); + +MODULE_AUTHOR ("Robert Eshleman <[email protected]>" ); +MODULE_LICENSE ("GPL v2" ); +MODULE_VERSION ("1.0.0" );Please drop.
MODULE_VERSIONprovides very little useful info and tends to lead to people assuming it does.Userspace has no obligation to ever look at it and we can't break userspace that doesn't so it doesn't provide any value.
Quoted 1 line
+MODULE_DESCRIPTION("MAX44009 ambient light sensor driver");
Signature · 2 lines
Peter Meerwald-Stadler Mobile: +43 664 24 44 418
On Sat, Jan 19, 2019 at 08:41:46PM +0100, Peter Meerwald-Stadler wrote:
Hey Jonathon and Peter,
First, thank you for the constructive and in-depth feedback.
I have a question below regarding a section of code that will need to be protected against a race condition if future features are added.
Mostly this email is just an ack that I've started working on the changes.
Thanks again,
Robert
Quoted 47 lines · Jonathan Cameron
some more comments from my side below...
Quoted 43 lines
On Wed, 16 Jan 2019 22:56:23 -0800
Robert Eshleman <[email protected]> wrote:Hi Robert,
Note I review drivers backwards, so comments may make more sense that way around.
Quoted 6 lines
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Quoted 6 lines
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
eventsNot really on this one. You support using them as a trigger, which is not how threshold events should be handled in IIO. Please
report them as events. This device doesn't seem to have
a trigger that can be used in general, so you shouldn't provide one.Userspace can use the event to decide to do a read if it wants to follow the classic move the thresholds so as to detect big 'changes' in light intensity.
Various other comments inline. Quite a bit of style cleanup needed as well, please check the kernel docs for coding style
https://www.kernel.org/doc/Documentation/process/coding-style.rstAlso take a look at other IIO drivers for the bits that are noted in there as varying across the kernel.
Whilst there is quite a bit to work on in here yet, great to see support for this new part! Looking forward to v2.
Jonathan
After seeing your notes and looking closer at how userspace leverages triggers, I can see now how it does not make sense to let this device supply one. It is, as you mentioned, events that are the right fit for this device.
Quoted 213 lines
Quoted 18 lines
Quoted 18 lines
* Illuminance triggered buffers
* Integration time
https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
Signed-off-by Robert Eshleman <[email protected]>--- .../bindings/iio/light/max44009.txt | 25 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/max44009.c | 696 ++++++++++++++++++ 4 files changed, 735 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/max44009.txt create mode 100644 drivers/iio/light/max44009.cQuoted 16 lines
Quoted 6 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +0
-0Documentation/devicetree/bindings/iio/light/max44009.txt line 10
Quoted 10 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +10
-0@@ -0,0 +1,25 @@ +* MAX44009 Ambient Light Sensor + +Required properties: + +- compatible: should be "maxim,max44009" +- reg: the I2C address of the device (default is <0x4a>) + +Optional properties: + +- interrupts : interrupt mapping for GPIO IRQ. Should be configured withthe space before the colon hurts my eyes
Quoted 101 lines
Quoted 57 lines
Documentation/devicetree/bindings/iio/light/max44009.txt new file +15
-0+ IRQ_TYPE_EDGE_FALLING. + +Refer to interrupt-controller/interrupts.txt for generic interrupt client +node bindings. + +Example: + +max44009: max44009@4a { + compatible = "maxim,max44009"; + reg = <0x4a>; + + interrupt-parent = <&gpio1>; + interrupts = <17 IRQ_TYPE_EDGE_FALLING>; +}; +drivers/iio/light/Kconfig +13
-0@@ -299,6 +299,19 @@ config MAX44000 To compile this driver as a module, choose M here: the module will be called max44000. +config MAX44009 + tristate "MAX44009 Ambient Light Sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_TRIGGERED_EVENT + help + Say Y here if you want to build support for Maxim Integrated's + MAX44009 ambient light sensor device. + + To compile this driver as a module, choose M here: + the module will be called max44009. + config OPT3001 tristate "Texas Instruments OPT3001 Light Sensor" depends on I2Cdrivers/iio/light/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_LV0104CS) += lv0104cs.o obj-$(CONFIG_MAX44000) += max44000.o +obj-$(CONFIG_MAX44009) += max44009.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_RPR0521) += rpr0521.odrivers/iio/light/max44009.c new file +0
-0drivers/iio/light/max44009.c line 9
Quoted 9 lines
drivers/iio/light/max44009.c new file +9
-0@@ -0,0 +1,696 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* max44009.c - Support for MAX44009 Ambient Light Sensor +* +* Copyright (c) 2019 Robert Eshleman <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. If you have SPDX, no need to have a license statement.
drivers/iio/light/max44009.c line 17
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+* +* +* Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf +* +* TODO: Support continuous mode and processed event value (IIO_EV_INFO_VALUE) +* +* Default I2C address: 0x4a +* No point in this blank line.
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+*/ + +#include <linux/bits.h> +#include <linux/i2c.h> +#include <linux/iio/buffer.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> drivers/iio/light/max44009.c line 37
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+#include <linux/iio/triggered_buffer.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/regmap.h> +#include <linux/util_macros.h> + +#define MAX44009_DRV_NAME "max44009" +#define MAX44009_IRQ_NAME "max44009_event"Unless used more than once, no point in defines like this. Just put the string inline.
Quoted 16 lines
drivers/iio/light/max44009.c line 49
Quoted 12 lines
drivers/iio/light/max44009.c new file +12
-0+ +/* Registers in datasheet order */ +#define MAX44009_REG_STATUS 0x0 +#define MAX44009_REG_ENABLE 0x1 +#define MAX44009_REG_CFG 0x2 +#define MAX44009_REG_LUX_HI 0x3 +#define MAX44009_REG_LUX_LO 0x4 +#define MAX44009_REG_UPPER_THR 0x5 +#define MAX44009_REG_LOWER_THR 0x6 +#define MAX44009_REG_THR_TIMER 0x7 + +#define MAX44009_INT_TIME_MASK (BIT(2) | BIT(1) | BIT(0))GENMASK
Quoted 1 line
drivers/iio/light/max44009.c new file +1
-0+#define MAX44009_INT_TIME_SHIFT (0)shift 0 needed?
Quoted 4 lines
drivers/iio/light/max44009.c line 54
Quoted 4 lines
drivers/iio/light/max44009.c new file +4
-0+ +#define MAX44009_MANUAL_MODE_MASK BIT(6) + +/* The maxmimum raw rising threshold for the max44009 */ maximum
Quoted 40 lines
drivers/iio/light/max44009.c line 66
Quoted 12 lines
drivers/iio/light/max44009.c new file +12
-0+#define MAX44009_MAXIMUM_THRESHOLD 8355840 + +#define MAX44009_HI_NIBBLE (reg) (((reg) >> 4) & 0xf) +#define MAX44009_LO_NIBBLE (reg) ((reg) & 0xf) + +#define MAX44009_EXP_MASK 0xf00 +#define MAX44009_EXP_RSHIFT 8 +#define MAX44009_LUX_EXP (reg) \ + (1 << (((reg) & MAX44009_EXP_MASK) >> MAX44009_EXP_RSHIFT)) +#define MAX44009_LUX_MANT (reg) ((reg) & 0xff) + +#define MAX44009_LUX (reg) (MAX44009_LUX_EXP(reg) * MAX44009_LUX_MANT(reg))These are complex enough that a few small functions with documentation would be better than macros.
Quoted 24 lines
drivers/iio/light/max44009.c new file +23
-0+ +#define MAX44009_THRESH_MANT (reg) ((MAX44009_LO_NIBBLE(reg) << 4) + 15) +#define MAX44009_THRESHOLD (reg) \ + ((1 << MAX44009_HI_NIBBLE(reg)) * MAX44009_THRESH_MANT(reg)) + +static const u32 max44009_int_time_ns_array[] = { +800000000 , +400000000 , +200000000 , +100000000 , +50000000 ,/* Manual mode only */ +25000000 ,/* Manual mode only */ +12500000 ,/* Manual mode only */ +6250000 ,/* Manual mode only */ +}; + +static const char max44009_int_time_str[] = +"0.8 " +"0.4 " +"0.2 " +"0.1 " +"0.05 " +"0.025 " Quoted 8 lines
drivers/iio/light/max44009.c line 94
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+"0.0125 " +"0.00625" ; + +static const u8 max44009_scale_avail_ulux_array[] = {45 }; +static const char max44009_scale_avail_str[] ="0.045" ;If there is only one value, don't provide the avail stuff and just put the value in directly where used.
Quoted 3 lines
CHAN_INFO_SCALEis not exposed but should be to express the 0.045 multiplier
Got it, totally makes sense.
Quoted 107 lines
Quoted 107 lines
Quoted 38 lines
drivers/iio/light/max44009.c new file +38
-0+ +struct max44009_data { +struct i2c_client *client; +struct iio_trigger *trigger; +struct mutex lock; +int64_t timestamp; +}; + +static const struct iio_event_spec max44009_event_spec[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec max44009_channels[] = { + { + .type =IIO_LIGHT , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_INT_TIME ), + .scan_index =0 , + .scan_type = { + .sign ='u' , + .realbits =24 , + .storagebits =32 , + }, + .event_spec = max44009_event_spec, + .num_event_specs =ARRAY_SIZE (max44009_event_spec), + }, +IIO_CHAN_SOFT_TIMESTAMP (1 ),drivers/iio/light/max44009.c line 142
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+}; + +static int max44009_read_reg (struct max44009_data *data,char reg) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock); + ret =i2c_smbus_read_byte_data (client, reg); +if (ret <0 ) {Same as below.
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+dev_err (&client->dev, +"failed to read reg 0x%0x, err: %d\n" , reg, ret); + goto err; + } + +err:drivers/iio/light/max44009.c line 158
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+mutex_unlock (&data->lock); +return ret; +} + +static int max44009_write_reg (struct max44009_data *data,char reg,char buf) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock);What is this mutex protecting?
drivers/iio/light/max44009.c
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+ ret =i2c_smbus_write_byte_data (client, reg, buf); +if (ret <0 ) {returns 0 on success. So do
if (ret) as it'll prove simpler to follow for handling later.Quoted 31 lines
drivers/iio/light/max44009.c new file +31
-0+dev_err (&client->dev, +"failed to write reg 0x%0x, err: %d\n" , + reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_read_int_time (struct max44009_data *data) +{ +int ret =max44009_read_reg (data,MAX44009_REG_CFG ); + +if (ret <0 ) { +return ret; + } + +return max44009_int_time_ns_array[ret &MAX44009_INT_TIME_MASK ]; +} + +static int max44009_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret, int_time; +s64 ns; + +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) {
Quoted 9 lines
Quoted 6 lines
drivers/iio/light/max44009.c line 197
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+ ns = val *NSEC_PER_SEC + val2; + int_time =find_closest_descending ( + ns, + max44009_int_time_ns_array, +ARRAY_SIZE (max44009_int_time_ns_array)); +a mutex might make sense here to protect the read/write combo
This device supports other configuration, via MAX44009_REG_CFG, that is not yet supported by this driver. If those configurations are supported in the future, then there will be a race condition that leads to a failure to update the register with both configurations (the most recent write will win). Currently, this is the only section that modifies this register with a read/update/write. Should this be future-proofed now (with a mutex), or deferred to when/if those features are supported in the future?
As an aside, my current revision of this driver removed the unnecessary mutex locks, which led to not needing a mutex at all.
Quoted 55 lines
Quoted 27 lines
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ ret =max44009_read_reg (data,MAX44009_REG_CFG ); +if (ret <0 ) +return ret; + + ret &= ~MAX44009_INT_TIME_MASK ; + ret |= (int_time <<MAX44009_INT_TIME_SHIFT ); + ret |=MAX44009_MANUAL_MODE_MASK ; +drivers/iio/light/max44009.c line 215
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return max44009_write_reg (data,MAX44009_REG_CFG , ret); + } +return -EINVAL ; +} + +static int max44009_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) {Make sure to get rid of all the brackets where kernel style doesn't have them. Basically any single statements if or else blocks (or simiilar).
drivers/iio/light/max44009.c line 217
Quoted 2 lines
drivers/iio/light/max44009.c new file +2
-0+return IIO_VAL_INT_PLUS_NANO ; + }else {You don't have any others...
Quoted 5 lines
drivers/iio/light/max44009.c line 222
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+return IIO_VAL_INT ; + } +} + +#define READ_LUX_XFER_LEN (4)
MAX44009_prefix please, no need for ()Quoted 7 lines
drivers/iio/light/max44009.c line 229
Quoted 7 lines
drivers/iio/light/max44009.c new file +7
-0+ +static int max44009_read_lux_raw (struct max44009_data *data) +{ +int ret; +struct i2c_msg xfer[READ_LUX_XFER_LEN ]; +u8 luxhireg[1 ] = {MAX44009_REG_LUX_HI }; +u8 luxloreg[1 ] = {MAX44009_REG_LUX_LO };a simple u8 would work as well, no array
Quoted 7 lines
drivers/iio/light/max44009.c line 236
Quoted 7 lines
drivers/iio/light/max44009.c new file +7
-0+u8 lo =0 ; +u8 hi =0 ; +u16 reg =0 ; + + xfer[0 ].addr = data->client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =1 ;sizeof(luxhireg)?
Quoted 8 lines
Quoted 5 lines
drivers/iio/light/max44009.c line 241
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+ xfer[0 ].buf = luxhireg; + + xfer[1 ].addr = data->client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =1 ;
sizeof(&hi)?
Got it. I'll use sizeof() in v2 (also changing this assignment to be the more idiomatic style seen in other IIO drivers for arrays of structs).
Quoted 70 lines
Quoted 11 lines
Quoted 11 lines
drivers/iio/light/max44009.c new file +11
-0+ xfer[1 ].buf = &hi; + + xfer[2 ].addr = data->client->addr; + xfer[2 ].flags =0 ; + xfer[2 ].len =1 ; + xfer[2 ].buf = luxloreg; + + xfer[3 ].addr = data->client->addr; + xfer[3 ].flags =I2C_M_RD ; + xfer[3 ].len =1 ; + xfer[3 ].buf = &lo;Quoted 8 lines
drivers/iio/light/max44009.c line 260
Quoted 8 lines
drivers/iio/light/max44009.c new file +8
-0+ +/* +* Use i2c_transfer instead of smbus read because i2c_transfer +* does NOT use a stop bit between address write and data read. +* Using a stop bit causes disjoint upper/lower byte reads and +* reduces accuracy +*/ +mutex_lock (&data->lock);mutex needed?
Quoted 48 lines
Quoted 48 lines
drivers/iio/light/max44009.c new file +47
-0+ ret =i2c_transfer (data->client->adapter, xfer,READ_LUX_XFER_LEN ); +mutex_unlock (&data->lock); +if (ret !=READ_LUX_XFER_LEN ) { +return -EIO ; + } + + reg = (((u16 )hi) <<4 ) | (lo &0xf ); + +return MAX44009_LUX (reg); +} + +static int max44009_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : { +switch (chan->type) { +case IIO_LIGHT : { + ret =max44009_read_lux_raw (data); +if (ret <0 ) { +return ret; + } + *val = ret; + *val2 =0 ; +return IIO_VAL_INT ; + } +default : +return -EINVAL ; + } +break ; + } + +case IIO_CHAN_INFO_INT_TIME : { + ret =max44009_read_int_time (data); +if (ret <0 ) { +return ret; + } + + *val2 = ret; + *val =0 ; +return IIO_VAL_INT_PLUS_NANO ; + } +
Quoted 279 lines
Quoted 6 lines
drivers/iio/light/max44009.c line 313
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+default : +return -EINVAL ; + } +} + +static IIO_CONST_ATTR(illuminance_scale_available, max44009_scale_avail_str);channels do not have
CHAN_INFO_SCALE, so why exposescale_available?Quoted 270 lines
Quoted 147 lines
drivers/iio/light/max44009.c new file +145
-0+static IIO_CONST_ATTR(illuminance_integration_time_available, + max44009_int_time_str); + +static struct attribute *max44009_attributes[] = { + &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, + &iio_const_attr_illuminance_scale_available.dev_attr.attr,NULL +}; + +static const struct attribute_group max44009_attribute_group = { + .attrs = max44009_attributes, +}; + +static int max44009_thresh_byte_from_int (int thresh) +{ +int mantissa, exp; + +if (thresh <0 || thresh >MAX44009_MAXIMUM_THRESHOLD ) { +return -EINVAL ; + } + +for (mantissa = thresh, exp =0 ; mantissa >0xff ; exp++) { + mantissa >>=1 ; + } + + mantissa >>=4 ; + mantissa &=0xf ; + exp <<=4 ; + +return exp | mantissa; +} + +static int max44009_get_thr_reg (enum iio_event_direction dir) +{ +switch (dir) { +case IIO_EV_DIR_RISING : +return MAX44009_REG_UPPER_THR ; +case IIO_EV_DIR_FALLING : +return MAX44009_REG_LOWER_THR ; +default : +return -EINVAL ; + } +} + +static int max44009_write_thresh (struct iio_dev *indio_dev, +enum iio_event_direction dir,int val) +{ +struct max44009_data *data =iio_priv (indio_dev); +int thresh; +int reg; + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_thresh_byte_from_int (val); +if (thresh <0 ) { +return thresh; + } + +return max44009_write_reg (data, reg, thresh); +} + +static int max44009_write_event_value (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) +{ +if (info !=IIO_EV_INFO_VALUE || chan->type !=IIO_LIGHT || val2 !=0 ) { +return -EINVAL ; + } + +return max44009_write_thresh (indio_dev, dir, val); +} + +static int max44009_read_event_value (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) +{ +int thresh, reg; +struct max44009_data *data =iio_priv (indio_dev); + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_read_reg (data, reg); +if (thresh <0 ) { +return thresh; + } + + *val =MAX44009_THRESHOLD (thresh); + +return IIO_VAL_INT ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , state); +if (ret <0 ) { +return ret; + } + +/* +* Set device to trigger interrupt immediately upon exceeding +* the threshold limit +*/ + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) { +return ret; + } + +return 0 ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) {drivers/iio/light/max44009.c line 468
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return -EINVAL ; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <0 ) { +return ret; + } + +return ret; +}We have lots of event config, but they are actually controlling the trigger. The device doesn't generate any iio events at all that I can see.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +static const struct iio_info max44009_info = { + .read_raw = max44009_read_raw, + .write_raw = max44009_write_raw, + .write_raw_get_fmt = max44009_write_raw_get_fmt, + .read_event_value = max44009_read_event_value, + .read_event_config = max44009_read_event_config, + .write_event_value = max44009_write_event_value, + .write_event_config = max44009_write_event_config, + .attrs = &max44009_attribute_group, +}; + +static int max44009_set_trigger_state (struct iio_trigger *trigger, +bool enable) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trigger);drivers/iio/light/max44009.c line 494
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+struct max44009_data *data =iio_priv (indio_dev); +int ret; + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , enable); +if (ret <0 ) { +return ret; + } + + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) {As suggested above,
write_regshould clearly only return 0 or negative. you can just do
return max440..Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+return ret; + } + +return ret; +} + +static const struct iio_trigger_ops max44009_trigger_ops = { + .set_trigger_state = max44009_set_trigger_state, +}; +drivers/iio/light/max44009.c line 514
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+static irqreturn_t max44009_trigger_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); +int lux, upper, lower; +int ret; +enum iio_event_direction direction; + +/* 32-bit for lux and 64-bit for timestamp */ +u32 buf[3 ] = {0 };Except the timestamp needs to be 64 bit aligned so this isn't big enough. All elements in IIO buffers are 'naturally' aligned. so 32 bit is aligned to 32 bits, 64 to 64 bits.
drivers/iio/light/max44009.c line 524
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <=0 ) { + goto err; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <=0 ) { + goto err; + }If these reads are necessary, then add comments on why.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +/* Clear interrupt by disabling interrupt (see datasheet) */ + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,0 ); +if (ret <0 ) { + goto err; + } + + lux =max44009_read_lux_raw (data); +if (lux <0 ) { + goto err; + } + + upper =max44009_read_reg (data,MAX44009_REG_UPPER_THR ); +if (upper <0 ) { + goto err; + }drivers/iio/light/max44009.c line 550
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ upper =MAX44009_THRESHOLD (upper); + + lower =max44009_read_reg (data,MAX44009_REG_LOWER_THR ); +if (lower <0 ) { + goto err; + } + lower =MAX44009_THRESHOLD (lower); + +/* If lux is NOT out-of-bounds then the interrupt was not triggered +* by this device Multiline comment syntax in IIO (and most of the kernel) is /*
* If...
*/Do we support shared interrupt lines? Doesn't look like it, which means it 'probably was' generate by this device, but we don't know why because the value has changed.
Returning
IRQ_NONEis a bad idea unless we are pretty sure it is a spurious interrupt, or we have a shared interrupt line. It will ultimately result in your interrupt being disable by the kernel and all activity breaking.There is also a perfectly good register to tell use if we generated the interrupt. Read that one and use that, not this racey approach.
That is very good to know how IRQ_NONE is handled in this context. After revisiting this function and better understanding events, it became clear that this function could basically be reduced down to a read of the status register and a call to iio_push_event.
Quoted 193 lines
Quoted 188 lines
Quoted 32 lines
drivers/iio/light/max44009.c new file +32
-0+*/ +if (lux < upper && lux > lower) { + goto err; + } + +/* Get event for correct thresh direction */ +if (lux >= upper) { + direction =IIO_EV_DIR_RISING ; + }else if (lux <= lower) { + direction =IIO_EV_DIR_FALLING ; + }else { + goto err; + } + + buf[0 ] = lux; +iio_push_to_buffers_with_timestamp (indio_dev, &buf, data->timestamp); +iio_trigger_notify_done (data->trigger); + +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , direction), + data->timestamp); + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +if (ret <0 ) { + goto err; + } + +return IRQ_HANDLED ; + +err: +/* Re-enable interrupt */ drivers/iio/light/max44009.c line 592
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +return IRQ_NONE ; +} + +static irqreturn_t max44009_irq_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); + + data->timestamp =iio_get_time_ns (indio_dev);We have a standard core function to do this..
iio_pollfunc_store_time.There 'might' be a reason to do it differently depending on whether you really need it to be that good if you can't identify whether it is your interrupt until the interrupt thread.
Quoted 24 lines
drivers/iio/light/max44009.c new file +24
-0+return IRQ_WAKE_THREAD ; +} + +static int max44009_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct max44009_data *data; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) { +return -ENOMEM ; + } + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev); + data->client = client; + indio_dev->dev.parent = &client->dev; + indio_dev->info = &max44009_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name =MAX44009_DRV_NAME ; + indio_dev->channels = max44009_channels; + indio_dev->num_channels =ARRAY_SIZE (max44009_channels); +mutex_init (&data->lock);drivers/iio/light/max44009.c line 626
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +/* Clear stale interrupt bit */ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <0 ) { + goto err; + } + +if (client->irq >0 ) { + ret = devm_request_threaded_irq(&client->dev, client->irq, + max44009_irq_handler,This is wrong. The interrupt handler should call the
iio_trigger_pollfunctions to cause the trigger handlers for all attached devices to be called.If for some reason the trigger is only suitable for use by this device then things get more blurred, but if not, the interrupt handler itself should establish that the interrupt is the data ready signal and call
iio_trigger_poll. If it is in a thread, calliio_trigger_poll_chained.The interrupt should be cleared (if it hasn't naturally happened for some other reason, in the trigger
try_reenablecallback.drivers/iio/light/max44009.c line 639
Quoted 13 lines
drivers/iio/light/max44009.c new file +13
-0+ max44009_trigger_handler, +IRQF_TRIGGER_FALLING | +IRQF_ONESHOT , + MAX44009_IRQ_NAME, indio_dev); +if (ret <0 ) { + goto err; + } + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + max44009_irq_handler, + max44009_trigger_handler, +NULL ); +if (ret <0 ) {It's going to change anyway (see below) but note kernel style is no brackets when only one item in an block like this.
Quoted 14 lines
drivers/iio/light/max44009.c new file +14
-0+ goto err; + } + + data->trigger =devm_iio_trigger_alloc (indio_dev->dev.parent, +"%s-dev%d" , + indio_dev->name, + indio_dev->id); +if (!data->trigger) { + ret = -ENOMEM ; + goto err; + } + data->trigger->dev.parent = indio_dev->dev.parent; + data->trigger->ops = &max44009_trigger_ops; +iio_trigger_set_drvdata (data->trigger, indio_dev);drivers/iio/light/max44009.c line 663
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =devm_iio_trigger_register (&client->dev, data->trigger); +if (ret <0 ) { + goto err; + } + } + + ret =devm_iio_device_register (&client->dev, indio_dev); +if (ret <0 ) { + goto err;Without the mutex destroy these all just become return ret;
drivers/iio/light/max44009.c line 668
Quoted 5 lines
drivers/iio/light/max44009.c new file +5
-0+ } + +return 0 ; +err: +mutex_destroy (&data->lock);mutex destroy is only really useful for lock debugging. Given we don't leave anything behind here anyway, it just makes the flow more complex for no gain. We very rarely bother with it as a result.
Quoted 17 lines
drivers/iio/light/max44009.c new file +17
-0+return ret; +} + +static const struct i2c_device_id max44009_id[] = { + {"max44009" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (i2c, max44009_id); + +static struct i2c_driver max44009_driver = { + .driver = { + .name =MAX44009_DRV_NAME , + }, + .probe = max44009_probe, + .id_table = max44009_id, +}; +module_i2c_driver (max44009_driver);drivers/iio/light/max44009.c line 695
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ +static const struct of_device_id max44009_of_match[] = { + { .compatible ="maxim,max44009" }, + { } +}; +MODULE_DEVICE_TABLE (of, max44009_of_match); + +MODULE_AUTHOR ("Robert Eshleman <[email protected]>" ); +MODULE_LICENSE ("GPL v2" ); +MODULE_VERSION ("1.0.0" );Please drop.
MODULE_VERSIONprovides very little useful info and tends to lead to people assuming it does.Userspace has no obligation to ever look at it and we can't break userspace that doesn't so it doesn't provide any value.
Quoted 1 line
+MODULE_DESCRIPTION("MAX44009 ambient light sensor driver");
Signature · 2 lines
Peter Meerwald-Stadler Mobile: +43 664 24 44 418
Quoted 8 lines · On Sun, 20 Jan 2019 22:39:14 -0800 Robert Eshleman <[email protected]>…
On Sat, Jan 19, 2019 at 08:41:46PM +0100, Peter Meerwald-Stadler wrote:
Hey Jonathon and Peter,
First, thank you for the constructive and in-depth feedback.
I have a question below regarding a section of code that will need to be protected against a race condition if future features are added.
Comments inline. I would be paranoid now and add the lock. Less likely that we'll accidentally forget it at some later stage!
Quoted 52 lines
Mostly this email is just an ack that I've started working on the changes.
Thanks again,
RobertQuoted 45 lines · Jonathan Cameron
some more comments from my side below...
Quoted 41 lines
On Wed, 16 Jan 2019 22:56:23 -0800
Robert Eshleman <[email protected]> wrote:Hi Robert,
Note I review drivers backwards, so comments may make more sense that way around.
Quoted 6 lines
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Quoted 6 lines
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
eventsNot really on this one. You support using them as a trigger, which is not how threshold events should be handled in IIO. Please
report them as events. This device doesn't seem to have
a trigger that can be used in general, so you shouldn't provide one.Userspace can use the event to decide to do a read if it wants to follow the classic move the thresholds so as to detect big 'changes' in light intensity.
Various other comments inline. Quite a bit of style cleanup needed as well, please check the kernel docs for coding style
https://www.kernel.org/doc/Documentation/process/coding-style.rstAlso take a look at other IIO drivers for the bits that are noted in there as varying across the kernel.
Whilst there is quite a bit to work on in here yet, great to see support for this new part! Looking forward to v2.
Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Jonathan
After seeing your notes and looking closer at how userspace leverages triggers, I can see now how it does not make sense to let this device supply one. It is, as you mentioned, events that are the right fit for this device.
Great.
..
Quoted 76 lines
Quoted 57 lines
Quoted 57 lines
Quoted 7 lines
drivers/iio/light/max44009.c new file +6
-0+dev_err (&client->dev, +"failed to read reg 0x%0x, err: %d\n" , reg, ret); + goto err; + } + +err:drivers/iio/light/max44009.c line 158
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+mutex_unlock (&data->lock); +return ret; +} + +static int max44009_write_reg (struct max44009_data *data,char reg,char buf) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock);What is this mutex protecting?
Review
Quoted 2 lines
(quoted diff) +2
-0+ ret = i2c_smbus_write_byte_data(client, reg, buf); + if (ret < 0) {returns 0 on success. So do
if (ret) as it'll prove simpler to follow for handling later.Quoted 31 lines
drivers/iio/light/max44009.c new file +31
-0+dev_err (&client->dev, +"failed to write reg 0x%0x, err: %d\n" , + reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_read_int_time (struct max44009_data *data) +{ +int ret =max44009_read_reg (data,MAX44009_REG_CFG ); + +if (ret <0 ) { +return ret; + } + +return max44009_int_time_ns_array[ret &MAX44009_INT_TIME_MASK ]; +} + +static int max44009_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret, int_time; +s64 ns; + +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) {Quoted 9 lines
Quoted 6 lines
drivers/iio/light/max44009.c line 197
Quoted 6 lines
drivers/iio/light/max44009.c new file +6
-0+ ns = val *NSEC_PER_SEC + val2; + int_time =find_closest_descending ( + ns, + max44009_int_time_ns_array, +ARRAY_SIZE (max44009_int_time_ns_array)); +a mutex might make sense here to protect the read/write combo
This device supports other configuration, via
MAX44009_REG_CFG, that is not yet supported by this driver. If those configurations are supported in the future, then there will be a race condition that leads to a failure to update the register with both configurations (the most recent write will win). Currently, this is the only section that modifies this register with a read/update/write. Should this be future-proofed now (with a mutex), or deferred to when/if those features are supported in the future?
Quoted 2 lines
As an aside, my current revision of this driver removed the unnecessary mutex locks, which led to not needing a mutex at all.
I would add them now. It's far too likely they'll get forgotten at some later stage.
Quoted 86 lines
Quoted 86 lines
Quoted 86 lines
Quoted 19 lines
drivers/iio/light/max44009.c new file +17
-0+ ret =max44009_read_reg (data,MAX44009_REG_CFG ); +if (ret <0 ) +return ret; + + ret &= ~MAX44009_INT_TIME_MASK ; + ret |= (int_time <<MAX44009_INT_TIME_SHIFT ); + ret |=MAX44009_MANUAL_MODE_MASK ; + +return max44009_write_reg (data,MAX44009_REG_CFG , ret); + } +return -EINVAL ; +} + +static const struct iio_trigger_ops max44009_trigger_ops = { + .set_trigger_state = max44009_set_trigger_state, +}; +drivers/iio/light/max44009.c
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+static irqreturn_t max44009_trigger_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); +int lux, upper, lower; +int ret; +enum iio_event_direction direction; + +/* 32-bit for lux and 64-bit for timestamp */ +u32 buf[3 ] = {0 };Except the timestamp needs to be 64 bit aligned so this isn't big enough. All elements in IIO buffers are 'naturally' aligned. so 32 bit is aligned to 32 bits, 64 to 64 bits.
drivers/iio/light/max44009.c line 524
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <=0 ) { + goto err; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <=0 ) { + goto err; + }If these reads are necessary, then add comments on why.
Quoted 16 lines
drivers/iio/light/max44009.c new file +16
-0+ +/* Clear interrupt by disabling interrupt (see datasheet) */ + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,0 ); +if (ret <0 ) { + goto err; + } + + lux =max44009_read_lux_raw (data); +if (lux <0 ) { + goto err; + } + + upper =max44009_read_reg (data,MAX44009_REG_UPPER_THR ); +if (upper <0 ) { + goto err; + }drivers/iio/light/max44009.c line 550
Quoted 10 lines
drivers/iio/light/max44009.c new file +10
-0+ upper =MAX44009_THRESHOLD (upper); + + lower =max44009_read_reg (data,MAX44009_REG_LOWER_THR ); +if (lower <0 ) { + goto err; + } + lower =MAX44009_THRESHOLD (lower); + +/* If lux is NOT out-of-bounds then the interrupt was not triggered +* by this device Multiline comment syntax in IIO (and most of the kernel) is /*
* If...
*/Do we support shared interrupt lines? Doesn't look like it, which means it 'probably was' generate by this device, but we don't know why because the value has changed.
Returning
IRQ_NONEis a bad idea unless we are pretty sure it is a spurious interrupt, or we have a shared interrupt line. It will ultimately result in your interrupt being disable by the kernel and all activity breaking.
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
There is also a perfectly good register to tell use if we generated the interrupt. Read that one and use that, not this racey approach.
That is very good to know how
IRQ_NONEis handled in this context. After revisiting this function and better understanding events, it became clear that this function could basically be reduced down to a read of the status register and a call toiio_push_event.
Sounds about right.
Jonathan
Rob Herring
maintainerQuoted 15 lines · Robert Eshleman
The MAX44009 is a low-power ambient light sensor from Maxim Integrated. It differs from the MAX44000 in that it doesn't have proximity sensing and that it requires far less current (1 micro-amp vs 5 micro-amps). The register mapping and feature set between the two are different enough to require a new driver for the MAX44009.
Developed and tested with a BeagleBone Black and UDOO Neo (i.MX6SX)
Supported features:
* Rising and falling illuminance threshold
events* Illuminance triggered buffers
Quoted 7 lines
* Integration time
https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf
Signed-off-by Robert Eshleman <[email protected]>--- .../bindings/iio/light/max44009.txt | 25 +
Please split bindings to a separate patch.
Quoted 779 lines
drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/max44009.c | 696 ++++++++++++++++++ 4 files changed, 735 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/max44009.txt create mode 100644 drivers/iio/light/max44009.cDocumentation/devicetree/bindings/iio/light/max44009.txt new file +25
-0@@ -0,0 +1,25 @@ +* MAX44009 Ambient Light Sensor + +Required properties: + +- compatible: should be "maxim,max44009" +- reg: the I2C address of the device (default is <0x4a>) + +Optional properties: + +- interrupts : interrupt mapping for GPIO IRQ. Should be configured with + IRQ_TYPE_EDGE_FALLING. + +Refer to interrupt-controller/interrupts.txt for generic interrupt client +node bindings. + +Example: + +max44009: max44009@4a { + compatible = "maxim,max44009"; + reg = <0x4a>; + + interrupt-parent = <&gpio1>; + interrupts = <17 IRQ_TYPE_EDGE_FALLING>; +}; +drivers/iio/light/Kconfig +13
-0@@ -299,6 +299,19 @@ config MAX44000 To compile this driver as a module, choose M here: the module will be called max44000. +config MAX44009 + tristate "MAX44009 Ambient Light Sensor" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_TRIGGERED_EVENT + help + Say Y here if you want to build support for Maxim Integrated's + MAX44009 ambient light sensor device. + + To compile this driver as a module, choose M here: + the module will be called max44009. + config OPT3001 tristate "Texas Instruments OPT3001 Light Sensor" depends on I2Cdrivers/iio/light/Makefile +1
-0@@ -28,6 +28,7 @@ obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o obj-$(CONFIG_LTR501) += ltr501.o obj-$(CONFIG_LV0104CS) += lv0104cs.o obj-$(CONFIG_MAX44000) += max44000.o +obj-$(CONFIG_MAX44009) += max44009.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_RPR0521) += rpr0521.odrivers/iio/light/max44009.c new file +696
-0@@ -0,0 +1,696 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* max44009.c - Support for MAX44009 Ambient Light Sensor +* +* Copyright (c) 2019 Robert Eshleman <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +* +* Datasheet: https://datasheets.maximintegrated.com/en/ds/MAX44009.pdf +* +* TODO: Support continuous mode and processed event value (IIO_EV_INFO_VALUE) +* +* Default I2C address: 0x4a +* +*/ + +#include <linux/bits.h> +#include <linux/i2c.h> +#include <linux/iio/buffer.h> +#include <linux/iio/events.h> +#include <linux/iio/iio.h> +#include <linux/iio/sysfs.h> +#include <linux/iio/trigger.h> +#include <linux/iio/trigger_consumer.h> +#include <linux/iio/triggered_buffer.h> +#include <linux/init.h> +#include <linux/interrupt.h> +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/regmap.h> +#include <linux/util_macros.h> + +#define MAX44009_DRV_NAME "max44009" +#define MAX44009_IRQ_NAME "max44009_event" + +/* Registers in datasheet order */ +#define MAX44009_REG_STATUS 0x0 +#define MAX44009_REG_ENABLE 0x1 +#define MAX44009_REG_CFG 0x2 +#define MAX44009_REG_LUX_HI 0x3 +#define MAX44009_REG_LUX_LO 0x4 +#define MAX44009_REG_UPPER_THR 0x5 +#define MAX44009_REG_LOWER_THR 0x6 +#define MAX44009_REG_THR_TIMER 0x7 + +#define MAX44009_INT_TIME_MASK (BIT(2) | BIT(1) | BIT(0)) +#define MAX44009_INT_TIME_SHIFT (0) + +#define MAX44009_MANUAL_MODE_MASK BIT(6) + +/* The maxmimum raw rising threshold for the max44009 */ +#define MAX44009_MAXIMUM_THRESHOLD 8355840 + +#define MAX44009_HI_NIBBLE (reg) (((reg) >> 4) & 0xf) +#define MAX44009_LO_NIBBLE (reg) ((reg) & 0xf) + +#define MAX44009_EXP_MASK 0xf00 +#define MAX44009_EXP_RSHIFT 8 +#define MAX44009_LUX_EXP (reg) \ + (1 << (((reg) & MAX44009_EXP_MASK) >> MAX44009_EXP_RSHIFT)) +#define MAX44009_LUX_MANT (reg) ((reg) & 0xff) + +#define MAX44009_LUX (reg) (MAX44009_LUX_EXP(reg) * MAX44009_LUX_MANT(reg)) + +#define MAX44009_THRESH_MANT (reg) ((MAX44009_LO_NIBBLE(reg) << 4) + 15) +#define MAX44009_THRESHOLD (reg) \ + ((1 << MAX44009_HI_NIBBLE(reg)) * MAX44009_THRESH_MANT(reg)) + +static const u32 max44009_int_time_ns_array[] = { +800000000 , +400000000 , +200000000 , +100000000 , +50000000 ,/* Manual mode only */ +25000000 ,/* Manual mode only */ +12500000 ,/* Manual mode only */ +6250000 ,/* Manual mode only */ +}; + +static const char max44009_int_time_str[] = +"0.8 " +"0.4 " +"0.2 " +"0.1 " +"0.05 " +"0.025 " +"0.0125 " +"0.00625" ; + +static const u8 max44009_scale_avail_ulux_array[] = {45 }; +static const char max44009_scale_avail_str[] ="0.045" ; + +struct max44009_data { +struct i2c_client *client; +struct iio_trigger *trigger; +struct mutex lock; +int64_t timestamp; +}; + +static const struct iio_event_spec max44009_event_spec[] = { + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_RISING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, + { + .type =IIO_EV_TYPE_THRESH , + .dir =IIO_EV_DIR_FALLING , + .mask_separate =BIT (IIO_EV_INFO_VALUE ) | +BIT (IIO_EV_INFO_ENABLE ), + }, +}; + +static const struct iio_chan_spec max44009_channels[] = { + { + .type =IIO_LIGHT , + .info_mask_separate =BIT (IIO_CHAN_INFO_RAW ), + .info_mask_shared_by_type =BIT (IIO_CHAN_INFO_INT_TIME ), + .scan_index =0 , + .scan_type = { + .sign ='u' , + .realbits =24 , + .storagebits =32 , + }, + .event_spec = max44009_event_spec, + .num_event_specs =ARRAY_SIZE (max44009_event_spec), + }, +IIO_CHAN_SOFT_TIMESTAMP (1 ), +}; + +static int max44009_read_reg (struct max44009_data *data,char reg) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock); + ret =i2c_smbus_read_byte_data (client, reg); +if (ret <0 ) { +dev_err (&client->dev, +"failed to read reg 0x%0x, err: %d\n" , reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_write_reg (struct max44009_data *data,char reg,char buf) +{ +struct i2c_client *client = data->client; +int ret; + +mutex_lock (&data->lock); + ret =i2c_smbus_write_byte_data (client, reg, buf); +if (ret <0 ) { +dev_err (&client->dev, +"failed to write reg 0x%0x, err: %d\n" , + reg, ret); + goto err; + } + +err: +mutex_unlock (&data->lock); +return ret; +} + +static int max44009_read_int_time (struct max44009_data *data) +{ +int ret =max44009_read_reg (data,MAX44009_REG_CFG ); + +if (ret <0 ) { +return ret; + } + +return max44009_int_time_ns_array[ret &MAX44009_INT_TIME_MASK ]; +} + +static int max44009_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int val, +int val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret, int_time; +s64 ns; + +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) { + ns = val *NSEC_PER_SEC + val2; + int_time =find_closest_descending ( + ns, + max44009_int_time_ns_array, +ARRAY_SIZE (max44009_int_time_ns_array)); + + ret =max44009_read_reg (data,MAX44009_REG_CFG ); +if (ret <0 ) +return ret; + + ret &= ~MAX44009_INT_TIME_MASK ; + ret |= (int_time <<MAX44009_INT_TIME_SHIFT ); + ret |=MAX44009_MANUAL_MODE_MASK ; + +return max44009_write_reg (data,MAX44009_REG_CFG , ret); + } +return -EINVAL ; +} + +static int max44009_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +if (mask ==IIO_CHAN_INFO_INT_TIME && chan->type ==IIO_LIGHT ) { +return IIO_VAL_INT_PLUS_NANO ; + }else { +return IIO_VAL_INT ; + } +} + +#define READ_LUX_XFER_LEN (4) + +static int max44009_read_lux_raw (struct max44009_data *data) +{ +int ret; +struct i2c_msg xfer[READ_LUX_XFER_LEN ]; +u8 luxhireg[1 ] = {MAX44009_REG_LUX_HI }; +u8 luxloreg[1 ] = {MAX44009_REG_LUX_LO }; +u8 lo =0 ; +u8 hi =0 ; +u16 reg =0 ; + + xfer[0 ].addr = data->client->addr; + xfer[0 ].flags =0 ; + xfer[0 ].len =1 ; + xfer[0 ].buf = luxhireg; + + xfer[1 ].addr = data->client->addr; + xfer[1 ].flags =I2C_M_RD ; + xfer[1 ].len =1 ; + xfer[1 ].buf = &hi; + + xfer[2 ].addr = data->client->addr; + xfer[2 ].flags =0 ; + xfer[2 ].len =1 ; + xfer[2 ].buf = luxloreg; + + xfer[3 ].addr = data->client->addr; + xfer[3 ].flags =I2C_M_RD ; + xfer[3 ].len =1 ; + xfer[3 ].buf = &lo; + +/* +* Use i2c_transfer instead of smbus read because i2c_transfer +* does NOT use a stop bit between address write and data read. +* Using a stop bit causes disjoint upper/lower byte reads and +* reduces accuracy +*/ +mutex_lock (&data->lock); + ret =i2c_transfer (data->client->adapter, xfer,READ_LUX_XFER_LEN ); +mutex_unlock (&data->lock); +if (ret !=READ_LUX_XFER_LEN ) { +return -EIO ; + } + + reg = (((u16 )hi) <<4 ) | (lo &0xf ); + +return MAX44009_LUX (reg); +} + +static int max44009_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan,int *val, +int *val2,long mask) +{ +struct max44009_data *data =iio_priv (indio_dev); +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : { +switch (chan->type) { +case IIO_LIGHT : { + ret =max44009_read_lux_raw (data); +if (ret <0 ) { +return ret; + } + *val = ret; + *val2 =0 ; +return IIO_VAL_INT ; + } +default : +return -EINVAL ; + } +break ; + } + +case IIO_CHAN_INFO_INT_TIME : { + ret =max44009_read_int_time (data); +if (ret <0 ) { +return ret; + } + + *val2 = ret; + *val =0 ; +return IIO_VAL_INT_PLUS_NANO ; + } + +default : +return -EINVAL ; + } +} + +static IIO_CONST_ATTR(illuminance_scale_available, max44009_scale_avail_str); +static IIO_CONST_ATTR(illuminance_integration_time_available, + max44009_int_time_str); + +static struct attribute *max44009_attributes[] = { + &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, + &iio_const_attr_illuminance_scale_available.dev_attr.attr,NULL +}; + +static const struct attribute_group max44009_attribute_group = { + .attrs = max44009_attributes, +}; + +static int max44009_thresh_byte_from_int (int thresh) +{ +int mantissa, exp; + +if (thresh <0 || thresh >MAX44009_MAXIMUM_THRESHOLD ) { +return -EINVAL ; + } + +for (mantissa = thresh, exp =0 ; mantissa >0xff ; exp++) { + mantissa >>=1 ; + } + + mantissa >>=4 ; + mantissa &=0xf ; + exp <<=4 ; + +return exp | mantissa; +} + +static int max44009_get_thr_reg (enum iio_event_direction dir) +{ +switch (dir) { +case IIO_EV_DIR_RISING : +return MAX44009_REG_UPPER_THR ; +case IIO_EV_DIR_FALLING : +return MAX44009_REG_LOWER_THR ; +default : +return -EINVAL ; + } +} + +static int max44009_write_thresh (struct iio_dev *indio_dev, +enum iio_event_direction dir,int val) +{ +struct max44009_data *data =iio_priv (indio_dev); +int thresh; +int reg; + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_thresh_byte_from_int (val); +if (thresh <0 ) { +return thresh; + } + +return max44009_write_reg (data, reg, thresh); +} + +static int max44009_write_event_value (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) +{ +if (info !=IIO_EV_INFO_VALUE || chan->type !=IIO_LIGHT || val2 !=0 ) { +return -EINVAL ; + } + +return max44009_write_thresh (indio_dev, dir, val); +} + +static int max44009_read_event_value (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) +{ +int thresh, reg; +struct max44009_data *data =iio_priv (indio_dev); + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + reg =max44009_get_thr_reg (dir); +if (reg <0 ) { +return reg; + } + + thresh =max44009_read_reg (data, reg); +if (thresh <0 ) { +return thresh; + } + + *val =MAX44009_THRESHOLD (thresh); + +return IIO_VAL_INT ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , state); +if (ret <0 ) { +return ret; + } + +/* +* Set device to trigger interrupt immediately upon exceeding +* the threshold limit +*/ + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) { +return ret; + } + +return 0 ; +} + +static int max44009_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 max44009_data *data =iio_priv (indio_dev); +int ret; + +if (chan->type !=IIO_LIGHT || type !=IIO_EV_TYPE_THRESH ) { +return -EINVAL ; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <0 ) { +return ret; + } + +return ret; +} + +static const struct iio_info max44009_info = { + .read_raw = max44009_read_raw, + .write_raw = max44009_write_raw, + .write_raw_get_fmt = max44009_write_raw_get_fmt, + .read_event_value = max44009_read_event_value, + .read_event_config = max44009_read_event_config, + .write_event_value = max44009_write_event_value, + .write_event_config = max44009_write_event_config, + .attrs = &max44009_attribute_group, +}; + +static int max44009_set_trigger_state (struct iio_trigger *trigger, +bool enable) +{ +struct iio_dev *indio_dev =iio_trigger_get_drvdata (trigger); +struct max44009_data *data =iio_priv (indio_dev); +int ret; + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE , enable); +if (ret <0 ) { +return ret; + } + + ret =max44009_write_reg (data,MAX44009_REG_THR_TIMER ,0 ); +if (ret <0 ) { +return ret; + } + +return ret; +} + +static const struct iio_trigger_ops max44009_trigger_ops = { + .set_trigger_state = max44009_set_trigger_state, +}; + +static irqreturn_t max44009_trigger_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); +int lux, upper, lower; +int ret; +enum iio_event_direction direction; + +/* 32-bit for lux and 64-bit for timestamp */ +u32 buf[3 ] = {0 }; + + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <=0 ) { + goto err; + } + + ret =max44009_read_reg (data,MAX44009_REG_ENABLE ); +if (ret <=0 ) { + goto err; + } + +/* Clear interrupt by disabling interrupt (see datasheet) */ + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,0 ); +if (ret <0 ) { + goto err; + } + + lux =max44009_read_lux_raw (data); +if (lux <0 ) { + goto err; + } + + upper =max44009_read_reg (data,MAX44009_REG_UPPER_THR ); +if (upper <0 ) { + goto err; + } + upper =MAX44009_THRESHOLD (upper); + + lower =max44009_read_reg (data,MAX44009_REG_LOWER_THR ); +if (lower <0 ) { + goto err; + } + lower =MAX44009_THRESHOLD (lower); + +/* If lux is NOT out-of-bounds then the interrupt was not triggered +* by this device +*/ +if (lux < upper && lux > lower) { + goto err; + } + +/* Get event for correct thresh direction */ +if (lux >= upper) { + direction =IIO_EV_DIR_RISING ; + }else if (lux <= lower) { + direction =IIO_EV_DIR_FALLING ; + }else { + goto err; + } + + buf[0 ] = lux; +iio_push_to_buffers_with_timestamp (indio_dev, &buf, data->timestamp); +iio_trigger_notify_done (data->trigger); + +iio_push_event (indio_dev, +IIO_UNMOD_EVENT_CODE (IIO_LIGHT ,0 , +IIO_EV_TYPE_THRESH , direction), + data->timestamp); + + ret =max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +if (ret <0 ) { + goto err; + } + +return IRQ_HANDLED ; + +err: +/* Re-enable interrupt */ +max44009_write_reg (data,MAX44009_REG_ENABLE ,1 ); +return IRQ_NONE ; +} + +static irqreturn_t max44009_irq_handler (int irq,void *p) +{ +struct iio_dev *indio_dev = p; +struct max44009_data *data =iio_priv (indio_dev); + + data->timestamp =iio_get_time_ns (indio_dev); +return IRQ_WAKE_THREAD ; +} + +static int max44009_probe (struct i2c_client *client, +const struct i2c_device_id *id) +{ +struct max44009_data *data; +struct iio_dev *indio_dev; +int ret; + + indio_dev =devm_iio_device_alloc (&client->dev,sizeof (*data)); +if (!indio_dev) { +return -ENOMEM ; + } + data =iio_priv (indio_dev); +i2c_set_clientdata (client, indio_dev); + data->client = client; + indio_dev->dev.parent = &client->dev; + indio_dev->info = &max44009_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->name =MAX44009_DRV_NAME ; + indio_dev->channels = max44009_channels; + indio_dev->num_channels =ARRAY_SIZE (max44009_channels); +mutex_init (&data->lock); + +/* Clear stale interrupt bit */ + ret =max44009_read_reg (data,MAX44009_REG_STATUS ); +if (ret <0 ) { + goto err; + } + +if (client->irq >0 ) { + ret =devm_request_threaded_irq (&client->dev, client->irq, + max44009_irq_handler, + max44009_trigger_handler, +IRQF_TRIGGER_FALLING | +IRQF_ONESHOT , +MAX44009_IRQ_NAME , indio_dev); +if (ret <0 ) { + goto err; + } + + ret =devm_iio_triggered_buffer_setup (&client->dev, indio_dev, + max44009_irq_handler, + max44009_trigger_handler, +NULL ); +if (ret <0 ) { + goto err; + } + + data->trigger =devm_iio_trigger_alloc (indio_dev->dev.parent, +"%s-dev%d" , + indio_dev->name, + indio_dev->id); +if (!data->trigger) { + ret = -ENOMEM ; + goto err; + } + data->trigger->dev.parent = indio_dev->dev.parent; + data->trigger->ops = &max44009_trigger_ops; +iio_trigger_set_drvdata (data->trigger, indio_dev); + + ret =devm_iio_trigger_register (&client->dev, data->trigger); +if (ret <0 ) { + goto err; + } + } + + ret =devm_iio_device_register (&client->dev, indio_dev); +if (ret <0 ) { + goto err; + } + +return 0 ; +err: +mutex_destroy (&data->lock); +return ret; +} + +static const struct i2c_device_id max44009_id[] = { + {"max44009" ,0 }, + { } +}; +MODULE_DEVICE_TABLE (i2c, max44009_id); + +static struct i2c_driver max44009_driver = { + .driver = { + .name =MAX44009_DRV_NAME , + }, + .probe = max44009_probe, + .id_table = max44009_id, +}; +module_i2c_driver (max44009_driver); + +static const struct of_device_id max44009_of_match[] = { + { .compatible ="maxim,max44009" }, + { } +}; +MODULE_DEVICE_TABLE (of, max44009_of_match); + +MODULE_AUTHOR ("Robert Eshleman <[email protected]>" ); +MODULE_LICENSE ("GPL v2" ); +MODULE_VERSION ("1.0.0" ); +MODULE_DESCRIPTION ("MAX44009 ambient light sensor driver" );Signature · 1 line
2.20.1