dt-bindings: iio: light: ROHM BH1745
Subsystems (3)
Mudit Sharma
maintainerPATCH v7 1/2 dt-bindings: iio: light: ROHM BH1745
Add ROHM BH1745 - 4 channel I2C colour sensor's dt-bindings.
---v6->v7:
- no changev5->v6:
- no changev4->v5:
- add power supply infov3->v4:
- no changesv2->v3:
- move 'additionalproperties' after 'required' block
- remove block style indicator '|' from description v1->v2:
- fix yaml issue: make maintainers a list
.../bindings/iio/light/rohm,bh1745.yaml | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm,bh1745.yaml
Documentation/devicetree/bindings/iio/light/rohm,bh1745.yaml new file +53 -0
@@ -0,0 +1,53 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/light/rohm,bh1745.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ROHM BH1745 colour sensor + +maintainers: + - Mudit Sharma <[email protected]> + +description: + BH1745 is an I2C colour sensor with red, green, blue and clear + channels. It has a programmable active low interrupt pin. + Interrupt occurs when the signal from the selected interrupt + source channel crosses set interrupt threshold high/low level. + +properties: + compatible: + const: rohm,bh1745 + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + vdd-supply: true + +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + i2c { + #address-cells = <1>; + #size-cells = <0>; + + colour-sensor@38 { + compatible = "rohm,bh1745"; + reg = <0x38>; + interrupt-parent = <&gpio>; + interrupts = <19 IRQ_TYPE_LEVEL_LOW>; + vdd-supply = <&vdd>; + }; + }; + +...
Signature · 1 line
2.43.0
Mudit Sharma
maintainerPATCH v7 2/2 iio: light: ROHM BH1745 colour sensor
Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.
Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.
Add myself as the maintainer for this driver in MAINTAINERS.
---v6->v7:
- Remove trigger registration
- Code format fixes
- Organise register information
- Simply return 'dev_err_probe()' in probe functions
- Use '/**/' for comments
- Clearly label 'readable' and 'writeable' for readable and writeable regmap ranges
- Drop gain and integration time LU tables and use functions from GTS helper
- Fix logic in 'bh1745_read_event_config()' and 'bh1745_write_event_config()'
- Better utilise regmap APIv5->v6:
- Fix typo
- Fix Indentation
- Drop read in bh1745_set_trigger_state() as configuring all the value v4->v5:
- Provide scale instead of HW gain
- Use GTS helpers
- Code style fixes
- Add check for part ID during probe
- Always enable latch when enabling interrupt
- Use devm_add_action_or_reset() and drop bh1745_remove() function
- Drop custom DEVICE_ATTR and provide related read_avail and setup interrupt source with event_config
- Make buffer support independent of IRQ
- Add power regulator handing with devm_regulator_get_enable()
- Drop read and write wrappers and use regmap functions directly
- Add MODULE_DEVICE_TABLE for of_device_idv3->v4:
- Fix formatting:
- Remove redundant new line
- Use '0x' rather than '0X'v2->v3:
- Squash commit for addition to MAINTAINERS
- Fix code style for consistency:
- New line before last 'return'
- Use variable name 'value' instead of 'val' in
'bh1745_set_trigger_state()'
- Align function parameters to match parenthesis
- Avoid use of magic number
- Use named enum instead of anonymous enum
- Use 'guard(mutex)(&data->lock)' instead of 'mutex_lock()' 'mutex_unlock()'
- Only initialize 'ret' and 'value' when necessary
- Fix and optimize logic for in_interrupt_latch_store()
- Fix error handling in irq , trigger handlers and dev attribute for latchv1->v2:
- No changes
MAINTAINERS | 6 + drivers/iio/light/Kconfig | 13 + drivers/iio/light/Makefile | 1 + drivers/iio/light/bh1745.c | 877 +++++++++++++++++++++++++++++++++++++ 4 files changed, 897 insertions(+) create mode 100644 drivers/iio/light/bh1745.c
MAINTAINERS +6 -0
@@ -19404,6 +19404,12 @@ S: Supported F: drivers/power/supply/bd99954-charger.c F: drivers/power/supply/bd99954-charger.h +ROHM BH1745 COLOUR SENSOR +M: Mudit Sharma <[email protected]> +L: [email protected] +S: Maintained +F: drivers/iio/light/bh1745.c + ROHM BH1750 AMBIENT LIGHT SENSOR DRIVER M: Tomasz Duszynski <[email protected]> S: Maintained
drivers/iio/light/Kconfig +13 -0
@@ -114,6 +114,19 @@ config AS73211 This driver can also be built as a module. If so, the module will be called as73211. +config BH1745 + tristate "ROHM BH1745 colour sensor" + depends on I2C + select REGMAP_I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + select IIO_GTS_HELPER + help + Say Y here to build support for the ROHM bh1745 colour sensor. + + To compile this driver as a module, choose M here: the module will + be called bh1745. + config BH1750 tristate "ROHM BH1750 ambient light sensor" depends on I2C
drivers/iio/light/Makefile +1 -0
@@ -13,6 +13,7 @@ obj-$(CONFIG_APDS9300) += apds9300.o obj-$(CONFIG_APDS9306) += apds9306.o obj-$(CONFIG_APDS9960) += apds9960.o obj-$(CONFIG_AS73211) += as73211.o +obj-$(CONFIG_BH1745) += bh1745.o obj-$(CONFIG_BH1750) += bh1750.o obj-$(CONFIG_BH1780) += bh1780.o obj-$(CONFIG_CM32181) += cm32181.o
drivers/iio/light/bh1745.c new file +877 -0
877 lines not on this page · raw message
Signature · 1 line
2.43.0
Jonathan Cameron
maintainerRe: [PATCH v7 2/2] iio: light: ROHM BH1745 colour sensor
Quoted 9 lines · On Fri, 5 Jul 2024 22:59:08 +0100 Mudit Sharma <[email protected]>…
Add support for BH1745, which is an I2C colour sensor with red, green, blue and clear channels. It has a programmable active low interrupt pin. Interrupt occurs when the signal from the selected interrupt source channel crosses set interrupt threshold high or low level.
Interrupt source for the device can be configured by enabling the corresponding event. Interrupt latch is always enabled when setting up interrupt.
Quoted 5 lines
Add myself as the maintainer for this driver in
MAINTAINERS.Signed-off-by Mudit Sharma <[email protected]>Reviewed-by Ivan Orlov <[email protected]>Reviewed-by Javier Carrasco <[email protected]>
One ordering bug introduced in this version + some minor other comments. I'd like Matti to check the GTS stuff though as I still find it hard to get my head around!
Jonathan
Quoted 39 lines
drivers/iio/light/bh1745.c new file +33
-0@@ -0,0 +1,877 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* ROHM BH1745 digital colour sensor driver +* +* Copyright (C) Mudit Sharma <[email protected]> +* +* 7-bit I2C slave addresses: +* 0x38 (ADDR pin low) +* 0x39 (ADDR pin high) +*/ + +#include <linux/i2c.h> +#include <linux/mutex.h> +#include <linux/util_macros.h> +#include <linux/iio/events.h> +#include <linux/regmap.h> +#include <linux/bits.h> +#include <linux/bitfield.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/iio/iio-gts-helper.h> + +/* BH1745 configuration registers */ + +/* System control */ +#define BH1745_SYS_CTRL 0x40 +#define BH1745_SW_RESET BIT(7) +#define BH1745_INTR_RESET BIT(6) +#define BH1745_PART_ID_MASK GENMASK(5, 0)
drivers/iio/light/bh1745.c line 43
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+#define BH1745_PART_ID 0x0B + +/* Mode control 1 */ +#define BH1745_MODE_CTRL_1 0x41 +#define BH1745_MEASUREMENT_TIME_MASK GENMASK(2, 0) + +/* Mode control 2 */ +#define BH1745_MODE_CTRL_2 0x42 +#define BH1745_RGBC_EN BIT(4) +#define BH1745_ADC_GAIN_MASK GENMASK(1, 0)
Name the fields so it's obvious what register they are in. #define BH1745_CTRL2_RGBC_EN
etc
Then it is much easier to check this set of defines against the datasheet once and then see inline if register fields are being written to the correct registers etc.
drivers/iio/light/bh1745.c line 121
Quoted 12 lines
drivers/iio/light/bh1745.c new file +12
-0+ +static const struct iio_itime_sel_mul bh1745_itimes[] = { +GAIN_SCALE_ITIME_US (5120000 ,BH1745_MEASUREMENT_TIME_5120MS ,32 ), +GAIN_SCALE_ITIME_US (2560000 ,BH1745_MEASUREMENT_TIME_2560MS ,16 ), +GAIN_SCALE_ITIME_US (1280000 ,BH1745_MEASUREMENT_TIME_1280MS ,8 ), +GAIN_SCALE_ITIME_US (640000 ,BH1745_MEASUREMENT_TIME_640MS ,4 ), +GAIN_SCALE_ITIME_US (320000 ,BH1745_MEASUREMENT_TIME_320MS ,2 ), +GAIN_SCALE_ITIME_US (160000 ,BH1745_MEASUREMENT_TIME_160MS ,1 ), +}; + +struct bh1745_data { +/* Lock to prevent device setting update or read before
/* * Lock
is the comment syntax used in IIO (and most, not not all :(, of the rest of the kernel)
Quoted 7 lines
drivers/iio/light/bh1745.c new file +7
-0+* related calculations are completed +*/ +struct mutex lock; +struct regmap *regmap; +struct device *dev; +struct iio_trigger *trig; +struct iio_gts gts;
drivers/iio/light/bh1745.c
Quoted 10 lines
drivers/iio/light/bh1745.c new file +9
-0+int irq; +}; + +static int bh1745_get_int_time (struct bh1745_data *data,int *val) +{ +int ret; +int int_time; + + ret =regmap_read (data->regmap,BH1745_MODE_CTRL_1 , &int_time);
Trivial but as a general rule, more readable to not change the meaning of a variable during a function. Here it starts out as the register value then becomes the field.
Better to use 2 local variables to make the difference clear. The compiler can do the same thing in either case anyway (use same storage or different storage) so we might as well go for readability.
There are other similar cases that would be nice to clean up
Quoted 29 lines
drivers/iio/light/bh1745.c new file +28
-0+if (ret) +return ret; + + int_time =FIELD_GET (BH1745_MEASUREMENT_TIME_MASK , int_time); + ret =iio_gts_find_int_time_by_sel (&data->gts, int_time); +if (ret <0 ) +return ret; + + *val = ret; + +return 0 ; +} +static int bh1745_write_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int val,int val2,long mask) +{ +struct bh1745_data *data =iio_priv (indio_dev); + +guard (mutex)(&data->lock); +switch (mask) { +case IIO_CHAN_INFO_SCALE : +return bh1745_set_scale (data, val, val2); + +case IIO_CHAN_INFO_INT_TIME : +return bh1745_set_int_time (data, val, val2); + +default : +return -EINVAL ;
drivers/iio/light/bh1745.c line 475
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+ } +} + +static int bh1745_write_raw_get_fmt (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +long mask) +{ +switch (mask) { +case IIO_CHAN_INFO_SCALE : +return IIO_VAL_INT ;
If you are forcing the type for scale to be integer, why does bh1745_set_scale() take val2? That will always be zero.
Quoted 15 lines
drivers/iio/light/bh1745.c new file +11
-0+ +case IIO_CHAN_INFO_INT_TIME : +return IIO_VAL_INT_PLUS_MICRO ; + +default : +return -EINVAL ; + } +} + +static irqreturn_t bh1745_trigger_handler (int interrupt,void *p) +{
drivers/iio/light/bh1745.c line 730
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+struct iio_poll_func *pf = p; +struct iio_dev *indio_dev = pf->indio_dev; +struct bh1745_data *data =iio_priv (indio_dev); +struct { +u16 chans[4 ]; +s64 timestamp__aligned(8) ; + } scan; +u16 value; +int ret; +int i, j =0 ;
Prefer that you don't mix variable declarations that set up a value with those that don't as in general it hurts readability.
int ret, i; int j = 0;
Quoted 21 lines
drivers/iio/light/bh1745.c new file +21
-0+ +for_each_set_bit (i, indio_dev->active_scan_mask, indio_dev->masklength) { + ret =regmap_bulk_read (data->regmap,BH1745_RED_LSB +2 * i, &value,2 ); +if (ret) + goto err; + scan.chans[j++] = value; + } + +iio_push_to_buffers_with_timestamp (indio_dev, &scan,iio_get_time_ns (indio_dev)); + +err: +iio_trigger_notify_done (indio_dev->trig); + +return IRQ_HANDLED ; +} + +static int bh1745_setup_triggered_buffer (struct iio_dev *indio_dev,struct device *parent) +{ +struct bh1745_data *data =iio_priv (indio_dev); +struct device *dev = data->dev; +int ret;
drivers/iio/light/bh1745.c line 761
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+ + ret =devm_iio_triggered_buffer_setup (parent, indio_dev,NULL , + bh1745_trigger_handler,NULL ); +if (ret) +return dev_err_probe (dev, ret,"Triggered buffer setup failed\n" ); + +if (data->irq) { + ret = devm_request_threaded_irq(dev, data->irq, NULL, + bh1745_interrupt_handler, + IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
Type of interrupt is a question for firmware - not the driver. Should only have IRQF_ONESHOT in here.
Note we have this wrong in a lot of old drivers, but can't fix them as we have no way to know if there is a broken firmware out there on a board where the driver overriding it is the only way it works. However, that doesn't mean to say we will introduce new cases!
Quoted 20 lines
drivers/iio/light/bh1745.c new file +19
-0+ "bh1745_interrupt", indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Request for IRQ failed\n" ); + } + +return 0 ; +} + +static int bh1745_probe (struct i2c_client *client) +{ +int ret; +int part_id; +struct bh1745_data *data; +struct iio_dev *indio_dev; +struct device *dev = &client->dev; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ;
drivers/iio/light/bh1745.c line 815
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+ +i2c_set_clientdata (client, indio_dev); + indio_dev->info = &bh1745_info; + indio_dev->name ="bh1745" ; + indio_dev->channels = bh1745_channels; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->num_channels =ARRAY_SIZE (bh1745_channels); + data =iio_priv (indio_dev); + data->dev = &client->dev; + data->irq = client->irq;
Pass the irq directly to the function that needs it as a pameter. There is very rarely a reason to keep a copy of it after probe() and there doesn't seem to be one here.
Quoted 18 lines
drivers/iio/light/bh1745.c new file +18
-0+ data->regmap =devm_regmap_init_i2c (client, &bh1745_regmap); +if (IS_ERR (data->regmap)) +return dev_err_probe (dev,PTR_ERR (data->regmap), +"Failed to initialize Regmap\n" ); + + ret =regmap_read (data->regmap,BH1745_SYS_CTRL , &part_id); +if (ret) +return ret; + + part_id =FIELD_GET (BH1745_PART_ID_MASK , part_id); +if (part_id !=BH1745_PART_ID ) +dev_warn (dev,"Unknown part ID 0x%x\n" , part_id); + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to get and enable regulator\n" ); + + ret =bh1745_init (data);
drivers/iio/light/bh1745.c line 843
Quoted 10 lines
drivers/iio/light/bh1745.c new file +10
-0+if (ret) +return ret; + + ret =bh1745_setup_triggered_buffer (indio_dev, indio_dev->dev.parent); +if (ret) +return ret; + + ret =devm_add_action_or_reset (dev, bh1745_power_off, data); +if (ret) +return dev_err_probe (dev, ret,"Failed to add action or reset\n" );
This block must be immediately after bh1745_init() block above. It's undoing an action in there. As you have it here and error from bh1745_setup_triggered_buffer() will result in the device being left turned on.
A better solution that would prevent this getting broken again would be to move it into the bh1745_init() function.
That would put it right next to the power_on call and make everything more obviously correct.
Quoted 7 lines
drivers/iio/light/bh1745.c new file +7
-0+ + ret =devm_iio_device_register (dev, indio_dev); +if (ret) +return dev_err_probe (dev, ret,"Failed to register device\n" ); + +return 0 ; +}