Support ROHM BU27008 RGB sensor
Subsystems (2)
cover Support ROHM BU27008 RGB sensor
Add support for ROHM BU27008 RGB sensor.
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
This series supports reading the RGBC and IR channels using IIO frameeork. However, only two of the BC+IR can be enabled at the same time. Series adds also support for scale and integration time configuration, where scale consists of impact of both the integration time and hardware gain. The gain and time support is backed by the newly introduced IIO GTS helper. This series depends on GTS helper patches added in BU27034 support series which is already merged in iio/togreg which this series is based on.
The hardware allows configuring gain setting by writing a 5-bit gain selector value to a register. Part of the gain setting is common for all channels (RGBC + IR) but part of the selector value can be set separately for RGBC and IR:
MODE_CONTROL2 REG:
bit 7 6 5 4 3 2 1 0
-----------------------------------------------------------------
| RGB selector | +---------------------------------------+ ----------------------------------------------------------------- | high bits IR | | low bits IR selector | +---------------+ +-----------------------+
In theory it would be possible to set certain separate gain values for RGBC and IR channels, but this gets pretty confusing because there are a few 'unsupported' selector values. If only RGBC or IR was set, some extra handling should be done to prevent the other channel from getting unsupported value due to change in high-bits. Furthermore, allowing the channels to be set different gain values (in some cases when gains are such the HW supports it) would make the cases where also integration time is changed to achieve correct scale ... interesting. It might also be confusing for user to try predicting when setting different scales succeeds and when it does not. Furthermore, if for example the scale setting for RGBC caused IR selector to be invalid - it could also cause the IR scale to "jump" very far from previous value.
To make the code simpler and more predictable for users, the current logic is as follows:
1. Prevent setting IR scale. (My assumption is IR is less used than RGBC)
2. When RGBC scale is set, set also the IR-selector to the same value. This prevents unsupported selector values and makes the IR scale changes predictable.
The 2) could mean we effectively have the same scale for all channels. Unfortunately, the HW design is slightly peculiar and selector 0 means gain 1X on RGBC but gain 2X on IR. Rest of the selectors equal same gain values on RGBC and IR. The result is that while changin selector from 0 => 1 causes RGBC gain to go from 1X => 4X, it causes IR gain to go from 2X => 4X.
So, the driver provides separate scale entries for all channels (also RGB and C will have separate gain entries because these channels are of same type as IR channel). This makes it possible for user applications to go read the scales for all channels after setting scale for one (in order to detect the IR scale difference).
Having the separate IR scale entry which applications can read to detect "arbitrary scale changes" makes it possible for applications to be written so they can cope if we need to implement the 'allow setting some different gains for IR and RGBC' - later.
Finally, the scales_available is also provided for all other channels except the IR channel, which does not allow the scale to be changed.
The sensor provides a data-ready IRQ and the driver implements a triggered buffer mode using this IRQ as a trigger.
---
Matti Vaittinen (3):
dt-bindings: iio: light: ROHM BU27008
iio: light: ROHM BU27008 color sensor
MAINTAINERS: Add ROHM BU27008
.../bindings/iio/light/rohm-bu27008.yaml | 49 + MAINTAINERS | 3 +- drivers/iio/light/Kconfig | 14 + drivers/iio/light/Makefile | 1 + drivers/iio/light/rohm-bu27008.c | 1028 +++++++++++++++++ 5 files changed, 1094 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml create mode 100644 drivers/iio/light/rohm-bu27008.c
base-commit: 52cc189b4fc6af6accc45fe7b7053d76d8724059
Signature · 14 lines
2.40.0 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
PATCH 1/3 dt-bindings: iio: light: ROHM BU27008
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add BU27008 dt-bindings.
--- .../bindings/iio/light/rohm-bu27008.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +49 -0
@@ -0,0 +1,49 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/light/bu27008.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ROHM BU27008 color sensor + +maintainers: + - Matti Vaittinen <[email protected]> + +description: | + The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear + and IR) with four configurable channels. Red and green being always + available and two out of the rest three (blue, clear, IR) can be + selected to be simultaneously measured. Typical application is adjusting + LCD backlight of TVs, mobile phones and tablet PCs. + +properties: + compatible: + const: rohm,bu27008 + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + vdd-supply: true + +required: + - compatible + - reg + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + + light-sensor@38 { + compatible = "rohm,bu27008"; + reg = <0x38>; + }; + }; + +...
Signature · 14 lines
2.40.0 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
PATCH 2/3 iio: light: ROHM BU27008 color sensor
In reply to Matti VaittinenThe ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
- raw_read() of RGB and clear channels
- triggered buffer w/ DRDY interrtupt
--- drivers/iio/light/Kconfig | 14 + drivers/iio/light/Makefile | 1 + drivers/iio/light/rohm-bu27008.c | 1028 ++++++++++++++++++++++++++++++ 3 files changed, 1043 insertions(+) create mode 100644 drivers/iio/light/rohm-bu27008.c
drivers/iio/light/Kconfig +14 -0
@@ -289,6 +289,20 @@ config JSA1212 To compile this driver as a module, choose M here: the module will be called jsa1212. +config ROHM_BU27008 + tristate "ROHM BU27008 color (RGB+C/IR) sensor" + depends on I2C + select REGMAP_I2C + select IIO_GTS_HELPER + help + Enable support for the ROHM BU27008 color sensor. + The ROHM BU27008 is a sensor with 5 photodiodes (red, green, + blue, clear and IR) with four configurable channels. Red and + green being always available and two out of the rest three + (blue, clear, IR) can be selected to be simultaneously measured. + Typical application is adjusting LCD backlight of TVs, + mobile phones and tablet PCs. + config ROHM_BU27034 tristate "ROHM BU27034 ambient light sensor" depends on I2C
drivers/iio/light/Makefile +1 -0
@@ -39,6 +39,7 @@ obj-$(CONFIG_NOA1305) += noa1305.o obj-$(CONFIG_OPT3001) += opt3001.o obj-$(CONFIG_PA12203001) += pa12203001.o obj-$(CONFIG_ROHM_BU27034) += rohm-bu27034.o +obj-$(CONFIG_ROHM_BU27008) += rohm-bu27008.o obj-$(CONFIG_RPR0521) += rpr0521.o obj-$(CONFIG_SI1133) += si1133.o obj-$(CONFIG_SI1145) += si1145.o
drivers/iio/light/rohm-bu27008.c new file +1028 -0
1,028 lines not on this page · raw message
Signature · 14 lines
2.40.0 -- Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =]
Re: [PATCH v1 3/3] MAINTAINERS: Add ROHM BU27008
In reply to a message not in the archiveQuoted 2 lines · Matti Vaittinen
It appears part of the intended recipient addresses ended up in the commit message. (CC'd the missed people/lists here). Sorry folks, I'll fix this in the next version.
Quoted 24 lines
Add myself as a maintainer for ROHM BU27008 color sensor driver.
Signed-off-by Matti Vaittinen <[email protected]>--- MAINTAINERS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)MAINTAINERS +2
-1@@ -18099,10 +18099,11 @@ S: Maintained F: Documentation/devicetree/bindings/iio/light/bh1750.yaml F: drivers/iio/light/bh1750.c -ROHM BU27034 AMBIENT LIGHT SENSOR DRIVER +ROHM BU270xx LIGHT SENSOR DRIVERs M: Matti Vaittinen <[email protected]> L: [email protected] S: Supported +F: drivers/iio/light/rohm-bu27008.c F: drivers/iio/light/rohm-bu27034.c ROHM MULTIFUNCTION BD9571MWV-M PMIC DEVICE DRIVERS
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Jonathan Cameron
maintainerQuoted 10 lines · On Fri, 21 Apr 2023 12:37:30 +0300 Matti Vaittinen <[email protected]>…
Add support for ROHM BU27008 RGB sensor.
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
This series supports reading the RGBC and IR channels using IIO frameeork. However, only two of the BC+IR can be enabled at the same
framework
Quoted 56 lines
time. Series adds also support for scale and integration time configuration, where scale consists of impact of both the integration time and hardware gain. The gain and time support is backed by the newly introduced IIO GTS helper. This series depends on GTS helper patches added in BU27034 support series which is already merged in iio/togreg which this series is based on.
The hardware allows configuring gain setting by writing a 5-bit gain selector value to a register. Part of the gain setting is common for all channels (RGBC + IR) but part of the selector value can be set separately for RGBC and IR:
MODE_CONTROL2REG:
bit 7 6 5 4 3 2 1 0
-----------------------------------------------------------------| RGB selector | +---------------------------------------+ ----------------------------------------------------------------- | high bits IR | | low bits IR selector | +---------------+ +-----------------------+In theory it would be possible to set certain separate gain values for RGBC and IR channels, but this gets pretty confusing because there are a few 'unsupported' selector values. If only RGBC or IR was set, some extra handling should be done to prevent the other channel from getting unsupported value due to change in high-bits. Furthermore, allowing the channels to be set different gain values (in some cases when gains are such the HW supports it) would make the cases where also integration time is changed to achieve correct scale ... interesting. It might also be confusing for user to try predicting when setting different scales succeeds and when it does not. Furthermore, if for example the scale setting for RGBC caused IR selector to be invalid - it could also cause the IR scale to "jump" very far from previous value.
To make the code simpler and more predictable for users, the current logic is as follows:
1. Prevent setting IR scale. (My assumption is IR is less used than RGBC)
2. When RGBC scale is set, set also the IR-selector to the same value. This prevents unsupported selector values and makes the IR scale changes predictable.The 2) could mean we effectively have the same scale for all channels. Unfortunately, the HW design is slightly peculiar and selector 0 means gain 1X on RGBC but gain 2X on IR. Rest of the selectors equal same gain values on RGBC and IR. The result is that while changin selector from 0 => 1 causes RGBC gain to go from 1X => 4X, it causes IR gain to go from 2X => 4X.
So, the driver provides separate scale entries for all channels (also RGB and C will have separate gain entries because these channels are of same type as IR channel). This makes it possible for user applications to go read the scales for all channels after setting scale for one (in order to detect the IR scale difference).
Quoted 4 lines
Having the separate IR scale entry which applications can read to detect "arbitrary scale changes" makes it possible for applications to be written so they can cope if we need to implement the 'allow setting some different gains for IR and RGBC' - later.
Hmm. I'm not sure preventing it is the best approach. That makes for an interface that is perhaps even less intuitive than having it affect the scales of the other channels. Still having it configurable at all is optional from an ABI point of view, so we could go with what you have here and see if anyone shouts about it in future.
Jonathan
Quoted 25 lines
Finally, the
scales_availableis also provided for all other channels except the IR channel, which does not allow the scale to be changed.The sensor provides a data-ready IRQ and the driver implements a triggered buffer mode using this IRQ as a trigger.
---
Matti Vaittinen (3):
dt-bindings: iio: light: ROHM BU27008
iio: light: ROHM BU27008 color sensor
MAINTAINERS: Add ROHM BU27008.../bindings/iio/light/rohm-bu27008.yaml | 49 + MAINTAINERS | 3 +- drivers/iio/light/Kconfig | 14 + drivers/iio/light/Makefile | 1 + drivers/iio/light/rohm-bu27008.c | 1028 +++++++++++++++++ 5 files changed, 1094 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml create mode 100644 drivers/iio/light/rohm-bu27008.cbase-commit:
52cc189b4fc6af6accc45fe7b7053d76d8724059
Jonathan Cameron
maintainerRe: [PATCH v1 2/3] iio: light: ROHM BU27008 color sensor
In reply to Matti VaittinenQuoted 11 lines · On Fri, 21 Apr 2023 12:39:36 +0300 Matti Vaittinen <[email protected]>…
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
-raw_read()of RGB and clear channels
- triggered buffer w/ DRDY interrtuptSigned-off-by Matti Vaittinen <[email protected]>
Hi Matti,
Biggest issue in here is some confusion over data packing when some channels are enabled. The driver must pack the channels that are enabled (seen from active_scan_mask) according to general IIO rules. So naturally aligned buffers. Thus for this device it should always be packed into
struct scan {
__le16 chans[4];
s64 ts __aligned(8); /*it's aligned anyway but better not to make reviewers think ;) */
};Even though there are 5 possible channels. If one in the middle isnt' enabled (e.g. blue) then clear and IR shift to lower words. of the buffer.
The demux code in the IIO core then deals with userspace requesting less than this
by repacking the data if needed. That allows it to present different views to different
consumers (e.g. userspace IIO access, and an inkernel buffer user - though there aren't any of those for light sensors AFAIK.
I'd not thought about the issue of the weird scales interacting with someone changing the integration time. That's nasty and I don't have a better suggestion than what you have currently.
Otherwise some trivial stuff inline.
Jonathan
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +0
-0@@ -0,0 +1,1028 @@
...
Quoted 13 lines
drivers/iio/light/rohm-bu27008.c new file +13
-0+ +enum { +BU27008_RED ,/* Always data0 */ +BU27008_GREEN ,/* Always data1 */ +BU27008_BLUE ,/* data2, configurable (blue / clear) */ +BU27008_CLEAR ,/* data2 or data3 */ +BU27008_IR ,/* data3 */ +BU27008_NUM_CHANS +}; + +enum { +BU27008_DATA0 ,/* Always RED */ +BU27008_DATA1 ,/* Always GREEN */
drivers/iio/light/rohm-bu27008.c line 76
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+BU27008_DATA2 ,/* Blue or Clear */ +BU27008_DATA3 ,/* IR or Clear */ + BU27008_NUM_HW_CHANS +}; + +/* We can always measure red and green at same time */ +#define ALWAYS_SCANNABLE (BIT(BU27008_RED) | BIT(BU27008_GREEN)) + +#define BU27008_CHAN_DATA_SIZE 2/* Each channel has 16bits of data */ +#define BU27008_BUF_DATA_SIZE (BU27008_NUM_CHANS * BU27008_CHAN_DATA_SIZE)
Buffer should be same size as the number you can grab in one read. So it should be same as BU27008_HW_DATA_SIZE. The data is packed when only some channels are enabled.
drivers/iio/light/rohm-bu27008.c line 83
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7
-0+#define BU27008_HW_DATA_SIZE (BU27008_NUM_HW_CHANS * BU27008_CHAN_DATA_SIZE) +#define NUM_U16_IN_TSTAMP (sizeof(s64) / sizeof(u16)) + +static const unsigned long bu27008_scan_masks[] = { +ALWAYS_SCANNABLE |BIT (BU27008_CLEAR ) |BIT (BU27008_IR ), +ALWAYS_SCANNABLE |BIT (BU27008_CLEAR ) |BIT (BU27008_BLUE ), +ALWAYS_SCANNABLE |BIT (BU27008_BLUE ) |BIT (BU27008_IR ),
Good, this looks correct for allowing the IIO core to deal with demuxing the data as necessary. Note that this is saying that if one of these modes is active scan mask it will be backed into lowers set of __le16 buffer elements.
drivers/iio/light/rohm-bu27008.c line 199
Quoted 12 lines
drivers/iio/light/rohm-bu27008.c new file +11
-0+ 0 +}; + + +struct bu27008_data { +struct regmap *regmap; +struct iio_trigger *trig; +struct device *dev; +struct iio_gts gts; +struct iio_gts gts_ir; +int64_t timestamp, old_timestamp;
As noted later, not sure what old_timestamp is for.
drivers/iio/light/rohm-bu27008.c line 204
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+int irq; + +/* +* Prevent changing gain/time config when scale is read/written. +* Prevent changing gain/time when raw data is read.
Does more than that. Probably need a bit more detail here.
drivers/iio/light/rohm-bu27008.c line 209
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+*/ +struct mutex mutex; +bool trigger_enabled; + +__le16 buffer[BU27008_NUM_CHANS ];
As below, you should be able to always read directly into the element you also use to push to the iio buffer.
drivers/iio/light/rohm-bu27008.c line 214
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+ +struct { +__le16 channels[BU27008_NUM_CHANS ]; +s64 ts__aligned(8) ; + } scan;
As noted below. This has write structure for a buffer scan, but you don't use it. Mind you, no need to have it in here that I can see. I'd just put it on the stack.
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +4
-0+}; + +static int bu27008_get_gain (struct bu27008_data *data,struct iio_gts *gts,int *gain) +{
drivers/iio/light/rohm-bu27008.c line 294
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+int ret, sel; + + ret =regmap_read (data->regmap,BU27008_REG_MODE_CONTROL2 , &sel); +if (ret) +return ret; + + sel =FIELD_GET (BU27008_MASK_RGBC_GAIN , sel); + + ret =iio_gts_find_gain_by_sel (gts, sel); +
No blank line here. Keep call and it's error handler in one block of code.
drivers/iio/light/rohm-bu27008.c line 297
Quoted 3 lines
drivers/iio/light/rohm-bu27008.c new file +3
-0+if (ret <0 ) { +dev_err (data->dev,"unknown gain value 0x%x\n" , sel); +
Blank line here doesn't add anything. Drop it.
Quoted 20 lines
drivers/iio/light/rohm-bu27008.c new file +20
-0+return ret; + } + + *gain = ret; + +return 0 ; +} + +static int bu27008_write_gain_sel (struct bu27008_data *data,int sel) +{ +int regval; + + regval =FIELD_PREP (BU27008_MASK_RGBC_GAIN , sel); + +/* +* We do always set also the LOW bits of IR-gain because othervice we +* would risk resulting an invalid GAIN register value. +* +* We could allow setting separate gains for RGBC and IR when the +* values were such that HW could support both gain settings.
drivers/iio/light/rohm-bu27008.c line 327
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+* Eg, when the shared bits were same for both gain values. +* +* This, however, has a negligible benefit compared to the increased +* software complexity when we would need to go through the gains +* for both channels separately when the integration time changes. +* This would end up with nasty logic for computing gain values for +* both channels - and rejecting them if shared bits changed. +* +* We should then build the logic by guessing what a user prefers. +* RGBC or IR gains correctly set while other jumps to odd value?
Ah. This is fiddly because integration time is shared. I'd missed that detail. We could break that up, so that which they care about is dependent on which one they wrote but it's a bit ugly.
Quoted 18 lines
drivers/iio/light/rohm-bu27008.c new file +18
-0+* Maybe look-up a value where both gains are somehow optimized +* <what this somehow is, is ATM unknown to us>. Or maybe user would +* expect us to reject changes when optimal gains can't be set to both +* channels w/given integration time. At best that would result +* solution that works well for a very specific subset of +* configurations but causes unexpected corner-cases. +* +* So, we keep it simple. Always set same selector to IR and RGBC. +* We disallow setting IR (as I expect that most of the users are +* interested in RGBC). This way we can show the user that the scales +* for RGBC and IR channels are different (1X Vs 2X with sel 0) while +* still keeping the operation deterministic. +*/ + regval |=FIELD_PREP (BU27008_MASK_IR_GAIN_LO , sel); + +return regmap_update_bits (data->regmap,BU27008_REG_MODE_CONTROL2 , +BU27008_MASK_RGBC_GAIN , regval); +}
drivers/iio/light/rohm-bu27008.c
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +9
-0+ +static int bu27008_meas_set (struct bu27008_data *data,bool enable) +{ +if (enable) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN ); + +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN );
Might be cleaner with regmap_update_bits()
drivers/iio/light/rohm-bu27008.c line 697
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +7
-0+} +static int bu27008_validate_trigger (struct iio_dev *idev, +struct iio_trigger *trig) +{ +struct bu27008_data *data =iio_priv (idev); + +if (data->trig != trig)
Hmm. I thought we had a standard function for this, but turns out we only have the one that validates in the other direction. Perhaps it's wroth something generic like iio_device_validate_own_trigger which would be similar to iio_trigger_validate_own_device in that it would use the common parentage of the trigger and iio_dev to check the match.
drivers/iio/light/rohm-bu27008.c line 733
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +9
-0+return -EINVAL ; + +return 0 ; +} +static int bu27008_chip_init (struct bu27008_data *data) +{ +int ret; + +/* Reset */
Kind of obvious from the code, I'd drop the comment as something that might rot over time.
drivers/iio/light/rohm-bu27008.c line 739
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6
-0+ ret =regmap_update_bits (data->regmap,BU27008_REG_SYSTEM_CONTROL , +BU27008_MASK_SW_RESET ,BU27008_MASK_SW_RESET ); +if (ret) +return dev_err_probe (data->dev, ret,"Sensor reset failed\n" ); + +msleep (1 );
Good to state what reset time min is from datasheet.
drivers/iio/light/rohm-bu27008.c line 750
Quoted 11 lines
drivers/iio/light/rohm-bu27008.c new file +11
-0+ +return ret; +} + +static int bu27008_set_drdy_irq (struct bu27008_data *data,bool state) +{ +if (state) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN ); +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN );
regmap_update_bits() maybe with the mask and value supplied.
drivers/iio/light/rohm-bu27008.c line 781
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +8
-0+} + +static irqreturn_t bu27008_irq_handler (int irq,void *private) +{ +struct iio_dev *idev = private; +struct bu27008_data *data =iio_priv (idev); + + data->old_timestamp = data->timestamp;
What is old_timestamp for? Without out setting that, this is the same as iio_pollfunc_store_time() with the timestamp stored in a slightly difference place and always waking the thread (which probably doesn't matter)
Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4
-0+ data->timestamp =iio_get_time_ns (idev); + +if (data->trigger_enabled) +return IRQ_WAKE_THREAD ;
drivers/iio/light/rohm-bu27008.c line 795
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+ +return IRQ_NONE ; +} + +static irqreturn_t bu27008_trigger_handler (int irq,void *p) +{ +struct iio_poll_func *pf = p; +struct iio_dev *idev = pf->indio_dev; +struct bu27008_data *data =iio_priv (idev); +__le16 raw[BU27008_NUM_CHANS +NUM_U16_IN_TSTAMP ];
Not immediately obvious this is correct.
I'm fairly sure it's not...
Timestamp needs to be aligned to 8 bytes. Here it's 10 bytes
in. That's why we often use a structure to make that explicit though even then you need to force alignment of the s64 (as it would be 32 bit aligned on x86_32 otherwise.) Note that you only actually read 4 channels so once you've fixed the code below, this all gets simpler. Even then, you should use a structure to make it more obviously correct for a reviewer.
drivers/iio/light/rohm-bu27008.c line 798
Quoted 3 lines
drivers/iio/light/rohm-bu27008.c new file +3
-0+int ret, dummy; + +memset (&raw,0 ,sizeof (raw));
You'll need this after you make it a structure as suggested as then it will have holes and memset is needed to fill them. As code stands ] = {}; would have been cleaner but that only assigns the elements of a structure so you'll need the memset if you change to one.
drivers/iio/light/rohm-bu27008.c line 802
Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4
-0+ +/* +* After some measurements, it seems reading the +* BU27008_REG_MODE_CONTROL3 debounces the IRQ line
'it seems' worries me. In docs? Not that we have them but would be nice if this statement was stronger!
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+*/ + ret =regmap_read (data->regmap,BU27008_REG_MODE_CONTROL3 , &dummy); +if (ret <0 ) + goto err_read; +
drivers/iio/light/rohm-bu27008.c line 817
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+ ret =regmap_bulk_read (data->regmap,BU27008_REG_DATA0_LO , data->buffer, +BU27008_HW_DATA_SIZE ); +if (ret <0 ) + goto err_read; + +/* Red and green are always in dedicated channels. */ +if (*idev->active_scan_mask &BIT (BU27008_RED )) + raw[BU27008_RED ] = data->buffer[BU27008_RED ]; +if (*idev->active_scan_mask &BIT (BU27008_GREEN )) + raw[BU27008_GREEN ] = data->buffer[BU27008_GREEN ];
Write them in even if disabled. The IIO core will deal with any data shuffling needed to drop any that aren't used. That's what the avail_masks stuff is used for.
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7
-0+ +/* +* We need to check the scan mask to determine which of the +* BLUE/CLEAR/IR are enabled so we know which channel is used to +* measure which data. +*/ +if (*idev->active_scan_mask &BIT (BU27008_BLUE )) {
drivers/iio/light/rohm-bu27008.c line 834
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+ raw[BU27008_BLUE ] = data->buffer[BU27008_DATA2 ]; + +if (*idev->active_scan_mask &BIT (BU27008_CLEAR )) + raw[BU27008_CLEAR ] = data->buffer[BU27008_DATA3 ]; + }else { +if (*idev->active_scan_mask &BIT (BU27008_CLEAR )) + raw[BU27008_CLEAR ] = data->buffer[BU27008_DATA2 ]; + } +if (*idev->active_scan_mask &BIT (BU27008_IR )) + raw[BU27008_IR ] = data->buffer[BU27008_DATA3 ];
This dance doesn't seem right. If a channel is disabled, then we should pack the data in the the buffer passed to iio_push_to_buffers_with_timestamp() not attempt to have it at a fixed offset. Hopefully if the scan_index etc all match up in the right order you should be able to just do the regmap_bulk_read directly into the buffer you are going to push out later (that buffer needs to be a bit larger to take the timestamp.) You have a nice scan element in your idev, but you don't use it.
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7
-0+ +iio_push_to_buffers_with_timestamp (idev, raw, pf->timestamp); +err_read: +iio_trigger_notify_done (idev->trig); + +return IRQ_HANDLED ; +}
drivers/iio/light/rohm-bu27008.c line 851
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+ +static irqreturn_t bu27008_irq_thread_handler (int irq,void *private) +{ +struct iio_dev *idev = private; +struct bu27008_data *data =iio_priv (idev); +irqreturn_t ret =IRQ_NONE ; + +mutex_lock (&data->mutex); +if (data->trigger_enabled) { +iio_trigger_poll_nested (data->trig);
Add a comment here on why it makes sense to hold the mutex whilst calling this.
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+ ret =IRQ_HANDLED ; + } +mutex_unlock (&data->mutex); + +return ret;
drivers/iio/light/rohm-bu27008.c line 866
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+} + +static int bu27008_buffer_preenable (struct iio_dev *idev) +{ +struct bu27008_data *data =iio_priv (idev); +int chan_sel, ret; + +/* Configure channel selection */ +if (*idev->active_scan_mask &BIT (BU27008_BLUE )) { +if (*idev->active_scan_mask &BIT (BU27008_CLEAR ))
Whilst in this case active_scan_mask is a single long, you should treat it as if it were a bitmap and use the bitmap accessors as per comments in linux/bitmap.htest_bit() is probably appropriate here.
Quoted 32 lines
drivers/iio/light/rohm-bu27008.c new file +32
-0+ chan_sel =BU27008_BLUE2_CLEAR3 ; + else + chan_sel =BU27008_BLUE2_IR3 ; + }else { + chan_sel =BU27008_CLEAR2_IR3 ; + } + + chan_sel =FIELD_PREP (BU27008_MASK_CHAN_SEL , chan_sel); + + ret =regmap_update_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_CHAN_SEL , chan_sel); +if (ret) +return ret; + +return bu27008_meas_set (data, true); +} + +static int bu27008_buffer_postdisable (struct iio_dev *idev) +{ +struct bu27008_data *data =iio_priv (idev); + +return bu27008_meas_set (data, false); +} + +static const struct iio_buffer_setup_ops bu27008_buffer_ops = { + .preenable = bu27008_buffer_preenable, + .postdisable = bu27008_buffer_postdisable, +}; + +static int bu27008_probe (struct i2c_client *i2c) +{ +struct device *dev = &i2c->dev;
drivers/iio/light/rohm-bu27008.c line 908
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+struct iio_trigger *indio_trig; +struct bu27008_data *data; +struct regmap *regmap; +unsigned int part_id, reg; +struct iio_dev *idev; +char *name; +int ret; + +if (!i2c->irq) { +dev_err (dev,"No IRQ configured\n" );
If it's possible to relax this requirement for an IRQ you should definitely do so, even if you loose a lot of functionality. It's annoyingly common for board designers to think it's not necessary to wire up IRQs.
Quoted 39 lines
drivers/iio/light/rohm-bu27008.c new file +39
-0+return -EINVAL ; + } + + regmap =devm_regmap_init_i2c (i2c, &bu27008_regmap); +if (IS_ERR (regmap)) +return dev_err_probe (dev,PTR_ERR (regmap), +"Failed to initialize Regmap\n" ); + + idev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!idev) +return -ENOMEM ; + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"Failed to get regulator\n" ); + + data =iio_priv (idev); + + ret =regmap_read (regmap,BU27008_REG_SYSTEM_CONTROL , ®); +if (ret) +return dev_err_probe (dev, ret,"Failed to access sensor\n" ); + + part_id =FIELD_GET (BU27008_MASK_PART_ID , reg); + +if (part_id !=BU27008_ID ) +dev_warn (dev,"unknown device 0x%x\n" , part_id); + + ret =devm_iio_init_iio_gts (dev,BU27008_SCALE_1X ,0 , bu27008_gains, +ARRAY_SIZE (bu27008_gains), bu27008_itimes, +ARRAY_SIZE (bu27008_itimes), &data->gts); +if (ret) +return ret; + + ret =devm_iio_init_iio_gts (dev,BU27008_SCALE_1X ,0 , bu27008_gains_ir, +ARRAY_SIZE (bu27008_gains_ir), bu27008_itimes, +ARRAY_SIZE (bu27008_itimes), &data->gts_ir); +if (ret) +return ret; +
drivers/iio/light/rohm-bu27008.c line 957
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+mutex_init (&data->mutex); + data->regmap = regmap; + data->dev = dev; + data->irq = i2c->irq; + + idev->channels = bu27008_channels; + idev->num_channels =ARRAY_SIZE (bu27008_channels); + idev->name ="bu27008" ; + idev->info = &bu27008_info; + idev->modes =INDIO_DIRECT_MODE |INDIO_BUFFER_SOFTWARE ;
Should be INDIO_BUFFER_TRIGGERRED I think. which is set by devm_iio_triggered_buffer_setup() for you. So just DIRECT_MODE here.
drivers/iio/light/rohm-bu27008.c line 962
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+ idev->available_scan_masks = bu27008_scan_masks; + + ret =bu27008_chip_init (data); +if (ret) +return ret;
Blank line here to separate the blocks of connected code from those that are unrelated.
drivers/iio/light/rohm-bu27008.c line 968
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6
-0+ ret =devm_iio_triggered_buffer_setup_ext (dev, idev, + &iio_pollfunc_store_time, + bu27008_trigger_handler, +IIO_BUFFER_DIRECTION_IN , + &bu27008_buffer_ops, +NULL );
I'm guessing this is left from a point in development where last parameter wasn't NULL. Given it is you can just use the more common devm_iio_triggered_buffer_setup() that also assumes it's an input buffers (which this is)
drivers/iio/light/rohm-bu27008.c line 974
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6
-0+if (ret) +return dev_err_probe (data->dev, ret, +"iio_triggered_buffer_setup_ext FAIL\n" ); + + indio_trig =devm_iio_trigger_alloc (dev,"%sdata-rdy-dev%d" , idev->name, +iio_device_id (idev));
Bit of a mix of naming conventions in here. Both occur in various drivers, but not normally mixed up. Either
idev, itrig
orindio_dev, indio_trig
drivers/iio/light/rohm-bu27008.c line 985
Quoted 11 lines
drivers/iio/light/rohm-bu27008.c new file +11
-0+if (!indio_trig) +return -ENOMEM ; + + data->trig = indio_trig; + + indio_trig->ops = &bu27008_trigger_ops; +iio_trigger_set_drvdata (indio_trig, data); + +/* +* No need to check for NULL. request_threaded_irq() defaults to +* dev_name() should the alloc fail.
Whilst true, it's very unlikely we'll get a failure here and
the code to check is shorter than the comment. Hence I'd just check and error out on failure anyway in interests of readability and simplicity.
drivers/iio/light/rohm-bu27008.c line 990
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+*/ + name =devm_kasprintf (data->dev,GFP_KERNEL ,"%s-bu27008" , +dev_name (data->dev)); + + ret = devm_request_threaded_irq(data->dev, i2c->irq, bu27008_irq_handler,
(dev, ...
in all of these.
drivers/iio/light/rohm-bu27008.c line 999
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +9
-0+ &bu27008_irq_thread_handler, + IRQF_ONESHOT, name, idev); +if (ret) +return dev_err_probe (data->dev, ret,"Could not request IRQ\n" ); + + + ret =devm_iio_trigger_register (dev, indio_trig); +if (ret) + return dev_err_probe(data->dev, ret,
Be consistent on data->dev vs dev. I'd prefer dev as it's shorter.
drivers/iio/light/rohm-bu27008.c line 1003
Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4
-0+ "Trigger registration failed\n"); + + ret =devm_iio_device_register (data->dev, idev); +if (ret <0 )
if (ret) for consistency and as it's documented as never taking a postive value anyway.
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+return dev_err_probe (dev, ret, +"Unable to register iio device\n" ); + +return ret; +}
Quoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Fri, 21 Apr 2023 12:37:30 +0300 Matti Vaittinen <[email protected]>…
Add support for ROHM BU27008 RGB sensor.
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Quoted 4 lines
Quoted 2 lines
This series supports reading the RGBC and IR channels using IIO frameeork. However, only two of the BC+IR can be enabled at the same
framework
Thanks!
Quoted 62 lines
Quoted 57 lines
time. Series adds also support for scale and integration time configuration, where scale consists of impact of both the integration time and hardware gain. The gain and time support is backed by the newly introduced IIO GTS helper. This series depends on GTS helper patches added in BU27034 support series which is already merged in iio/togreg which this series is based on.
The hardware allows configuring gain setting by writing a 5-bit gain selector value to a register. Part of the gain setting is common for all channels (RGBC + IR) but part of the selector value can be set separately for RGBC and IR:
MODE_CONTROL2REG:
bit 7 6 5 4 3 2 1 0
-----------------------------------------------------------------| RGB selector | +---------------------------------------+ ----------------------------------------------------------------- | high bits IR | | low bits IR selector | +---------------+ +-----------------------+In theory it would be possible to set certain separate gain values for RGBC and IR channels, but this gets pretty confusing because there are a few 'unsupported' selector values. If only RGBC or IR was set, some extra handling should be done to prevent the other channel from getting unsupported value due to change in high-bits. Furthermore, allowing the channels to be set different gain values (in some cases when gains are such the HW supports it) would make the cases where also integration time is changed to achieve correct scale ... interesting. It might also be confusing for user to try predicting when setting different scales succeeds and when it does not. Furthermore, if for example the scale setting for RGBC caused IR selector to be invalid - it could also cause the IR scale to "jump" very far from previous value.
To make the code simpler and more predictable for users, the current logic is as follows:
1. Prevent setting IR scale. (My assumption is IR is less used than RGBC)
2. When RGBC scale is set, set also the IR-selector to the same value. This prevents unsupported selector values and makes the IR scale changes predictable.The 2) could mean we effectively have the same scale for all channels. Unfortunately, the HW design is slightly peculiar and selector 0 means gain 1X on RGBC but gain 2X on IR. Rest of the selectors equal same gain values on RGBC and IR. The result is that while changin selector from 0 => 1 causes RGBC gain to go from 1X => 4X, it causes IR gain to go from 2X => 4X.
So, the driver provides separate scale entries for all channels (also RGB and C will have separate gain entries because these channels are of same type as IR channel). This makes it possible for user applications to go read the scales for all channels after setting scale for one (in order to detect the IR scale difference).
Quoted 4 lines
Having the separate IR scale entry which applications can read to detect "arbitrary scale changes" makes it possible for applications to be written so they can cope if we need to implement the 'allow setting some different gains for IR and RGBC' - later.
Quoted 5 lines
Hmm. I'm not sure preventing it is the best approach. That makes for an interface that is perhaps even less intuitive than having it affect the scales of the other channels. Still having it configurable at all is optional from an ABI point of view, so we could go with what you have here and see if anyone shouts about it in future.
I am not sure either. This is really a dance between making driver which is still understandable and maintainable, ABI which allows us to improve things if needed and providing users the features they need.
So. let's begin with this - and as you say, if it appears to be suboptimal, then we can re-work it :)
Yours,
-- Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Re: [PATCH v1 2/3] iio: light: ROHM BU27008 color sensor
In reply to Jonathan CameronQuoted 22 lines · Jonathan Cameron
Quoted 11 lines · On Fri, 21 Apr 2023 12:39:36 +0300 Matti Vaittinen <[email protected]>…
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
-raw_read()of RGB and clear channels
- triggered buffer w/ DRDY interrtuptSigned-off-by Matti Vaittinen <[email protected]>Hi Matti,
Biggest issue in here is some confusion over data packing when some channels are enabled. The driver must pack the channels that are enabled (seen from
active_scan_mask) according to general IIO rules. So naturally aligned buffers. Thus for this device it should always be packed into
Quoted 7 lines
struct scan { __le16 chans[4]; s64 ts __aligned(8); /*it's aligned anyway but better not to make reviewers think ;) */ };Even though there are 5 possible channels. If one in the middle isnt' enabled (e.g. blue) then clear and IR shift to lower words. of the buffer.
Ah, right. So I had misunderstood how the buffer works. I thought the scan_mask was only used to disallow unsupported channel-enabling configurations. If I understand your statement correctly, the scan_mask is used to determine the 'place of data' in the buffer when certain configuration is used. (I'll check this from the code but if the IIO handles data as I now think - that's cool! It should indeed simplify the buffer in driver side!).
Quoted 11 lines
The demux code in the IIO core then deals with userspace requesting less than this
by repacking the data if needed. That allows it to present different views to different
consumers (e.g. userspace IIO access, and an inkernel buffer user - though there aren't any of those for light sensors AFAIK.I'd not thought about the issue of the weird scales interacting with someone changing the integration time. That's nasty and I don't have a better suggestion than what you have currently.
Otherwise some trivial stuff inline.
Thanks for the non-trivial and trivial stuff! I'll re-work this driver based on your input - which is really invaluable - during this week. Your support is _very much_ appreciated!
Quoted 42 lines
Jonathan
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +0
-0@@ -0,0 +1,1028 @@...
Quoted 13 lines
drivers/iio/light/rohm-bu27008.c new file +13
-0+ +enum { +BU27008_RED ,/* Always data0 */ +BU27008_GREEN ,/* Always data1 */ +BU27008_BLUE ,/* data2, configurable (blue / clear) */ +BU27008_CLEAR ,/* data2 or data3 */ +BU27008_IR ,/* data3 */ +BU27008_NUM_CHANS +}; + +enum { +BU27008_DATA0 ,/* Always RED */ +BU27008_DATA1 ,/* Always GREEN */ drivers/iio/light/rohm-bu27008.c line 76
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+BU27008_DATA2 ,/* Blue or Clear */ +BU27008_DATA3 ,/* IR or Clear */ + BU27008_NUM_HW_CHANS +}; + +/* We can always measure red and green at same time */ +#define ALWAYS_SCANNABLE (BIT(BU27008_RED) | BIT(BU27008_GREEN)) + +#define BU27008_CHAN_DATA_SIZE 2/* Each channel has 16bits of data */ +#define BU27008_BUF_DATA_SIZE (BU27008_NUM_CHANS * BU27008_CHAN_DATA_SIZE)Buffer should be same size as the number you can grab in one read. So it should be same as
BU27008_HW_DATA_SIZE. The data is packed when only some channels are enabled.
Quoted 12 lines
drivers/iio/light/rohm-bu27008.c line 83
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c new file +7
-0+#define BU27008_HW_DATA_SIZE (BU27008_NUM_HW_CHANS * BU27008_CHAN_DATA_SIZE) +#define NUM_U16_IN_TSTAMP (sizeof(s64) / sizeof(u16)) + +static const unsigned long bu27008_scan_masks[] = { +ALWAYS_SCANNABLE |BIT (BU27008_CLEAR ) |BIT (BU27008_IR ), +ALWAYS_SCANNABLE |BIT (BU27008_CLEAR ) |BIT (BU27008_BLUE ), +ALWAYS_SCANNABLE |BIT (BU27008_BLUE ) |BIT (BU27008_IR ),Good, this looks correct for allowing the IIO core to deal with demuxing the data as necessary. Note that this is saying that if one of these modes is active scan mask it will be backed into lowers set of
__le16buffer elements.
So basically, what I need to ensure is that the data in the buffer will be in the same order we tell here for "IIO demuxer". I think I got this now, thanks!
Quoted 14 lines
drivers/iio/light/rohm-bu27008.c line 697
Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +6
-0+static int bu27008_validate_trigger (struct iio_dev *idev, +struct iio_trigger *trig) +{ +struct bu27008_data *data =iio_priv (idev); + +if (data->trig != trig)Hmm. I thought we had a standard function for this, but turns out we only have the one that validates in the other direction. Perhaps it's wroth something generic like
iio_device_validate_own_triggerwhich would be similar toiio_trigger_validate_own_devicein that it would use the common parentage of the trigger andiio_devto check the match.
I'll see if I can come up with something generic based on your suggestion :)
Quoted 8 lines
drivers/iio/light/rohm-bu27008.c line 739
Quoted 6 lines
drivers/iio/light/rohm-bu27008.c new file +6
-0+ ret =regmap_update_bits (data->regmap,BU27008_REG_SYSTEM_CONTROL , +BU27008_MASK_SW_RESET ,BU27008_MASK_SW_RESET ); +if (ret) +return dev_err_probe (data->dev, ret,"Sensor reset failed\n" ); + +msleep (1 );Good to state what reset time min is from datasheet.
I don't remember seeing that in datasheet, but I'll recheck.
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c line 802
Quoted 4 lines
drivers/iio/light/rohm-bu27008.c new file +4
-0+ +/* +* After some measurements, it seems reading the +* BU27008_REG_MODE_CONTROL3 debounces the IRQ line 'it seems' worries me. In docs? Not that we have them but would be nice if this statement was stronger!
I have the - or at least a - datasheet. Unfortunately the datasheet does not bother explaining too much about how things are working... The 'seems' here is based on my observation that without reading the BU27008_REG_MODE_CONTROL3 all the samples are read immediately regardless the integration time. Also, tracing the lines with saleae does not show the IRQ line being debounced when samples are read. This behaviour changes when I read the BU27008_REG_MODE_CONTROL3 - which contains the 'VALID'-bit. Also saleae shows the IRQ debounced when this reg is read. Hence, I am fairly convinced the IC works like this - and I am somewhat convinced the IC is meant to work like this - but no, I haven't seen this documented (nor did I receive confirmation from the hardware colleagues when I asked about this). So, this comment just states my current understanding - HW 'seems' to work like this :)
Quoted 7 lines
drivers/iio/light/rohm-bu27008.c line 908
Quoted 3 lines
drivers/iio/light/rohm-bu27008.c new file +3
-0+ +if (!i2c->irq) { +dev_err (dev,"No IRQ configured\n" );If it's possible to relax this requirement for an IRQ you should definitely do so, even if you loose a lot of functionality. It's annoyingly common for board designers to think it's not necessary to wire up IRQs.
Well, knowing how IC designers implement the IRQs... I am not at all sure not wiring the IRQs is a bad thing XD
And yes, a light sensor is probably very much usable with plain polling. I'll just drop the buffer/trigger stuff and provide only the read/write_raw() if IRQ is not given.
All in all, thanks a lot for the review!
Yours,
-- Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Re: [PATCH v1 1/3] dt-bindings: iio: light: ROHM BU27008
In reply to Matti VaittinenQuoted 6 lines · Matti Vaittinen
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Quoted 7 lines
Add BU27008 dt-bindings.
Signed-off-by Matti Vaittinen <[email protected]>--- .../bindings/iio/light/rohm-bu27008.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml
Filename like compatible, so rohm,bu27008.yaml
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml line 4
Quoted 11 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +4
-0@@ -0,0 +1,49 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/light/bu27008.yaml#
Does not look like you tested the bindings. Please run make dt_binding_check (see Documentation/devicetree/bindings/writing-schema.rst for instructions).
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml line 12
Quoted 8 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +8
-0+$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ROHM BU27008 color sensor + +maintainers: + - Matti Vaittinen <[email protected]> + +description: |
Do not need '|' unless you need to preserve formatting.
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml line 17
Quoted 5 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +5
-0+ The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear + and IR) with four configurable channels. Red and green being always + available and two out of the rest three (blue, clear, IR) can be + selected to be simultaneously measured. Typical application is adjusting + LCD backlight of TVs, mobile phones and tablet PCs.
Best regards,
Krzysztof
Re: [PATCH v1 2/3] iio: light: ROHM BU27008 color sensor
In reply to Jonathan CameronQuoted 13 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 21 Apr 2023 12:39:36 +0300 Matti Vaittinen <[email protected]>…
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
-raw_read()of RGB and clear channels
- triggered buffer w/ DRDY interrtupt
Quoted 14 lines
drivers/iio/light/rohm-bu27008.c line 514
Quoted 10 lines
Signed-off-by Matti Vaittinen <[email protected]>drivers/iio/light/rohm-bu27008.c new file +9
-0+ +static int bu27008_meas_set (struct bu27008_data *data,bool enable) +{ +if (enable) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN ); + +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN );Might be cleaner with
regmap_update_bits()Quoted 1 line
+}
Hm. I need to disagree on this although I think it depends on what one is used to :)
For me adding a variable for value to be used is slightly more complex than just using clear or set function depending on the enable/disable. I remember thinking the same as you and preferring the update_bits also on enable/disable cases - until I wrote my first power-supply driver and Sebasian Reichel told me to not do:
int val;
if (foo)
val = mask; else val = 0;
return regmap_update_bits(regmap, reg, mask, val);
but use set/clear bits. This allows killing the 'int val;'. I remember I had to sleep over night on it but I later started seeing the set/clear bits as a simpler thing.
Sure we could also do
if (foo)
return regmap_update_bits(map, reg, mask, mask); else return regmap_update_bits(map, reg, mask, 0);
- but here we just replace:
regmap_set_bits(map, reg, mask) withregmap_update_bits(map, reg, mask, mask)
and
regmap_clear_bits(map, reg, mask)regmap_update_bits(map, reg, mask, 0)
with longer but functionally same variants - which kind of says "I think the "regmap_set_bits() and regmap_clear_bits()" are useless ;)
After saying this - I can use the regmap_update_bits() if you insist, but in my (not always so) humble opinion this does not improve the function.
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c line 750
Quoted 8 lines
drivers/iio/light/rohm-bu27008.c new file +8
-0+ +static int bu27008_set_drdy_irq (struct bu27008_data *data,bool state) +{ +if (state) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN ); +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN );
regmap_update_bits()maybe with the mask and value supplied.
Same weak objection here as was with the bu27008_meas_set(). Eg, can change if required but please reconsider :)
Quoted 14 lines
drivers/iio/light/rohm-bu27008.c line 781
Quoted 9 lines
drivers/iio/light/rohm-bu27008.c new file +8
-0+} + +static irqreturn_t bu27008_irq_handler (int irq,void *private) +{ +struct iio_dev *idev = private; +struct bu27008_data *data =iio_priv (idev); + + data->old_timestamp = data->timestamp;What is
old_timestampfor? Without out setting that, this is the same asiio_pollfunc_store_time()with the timestamp stored in a slightly difference place and always waking the thread (which probably doesn't matter)
Thanks. I just re-used the logic from a driver which had some other options but the data-ready IRQ as well. As we don't have any such in bu27008, I think we can drop the custom stuff as you suggest and clean up this for quite a bit :) Thanks!
Quoted 5 lines
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+ +static irqreturn_t bu27008_irq_thread_handler (int irq,void *private) +{ +struct iio_dev *idev = private; +struct bu27008_data *data =iio_priv (idev);
Quoted 8 lines
drivers/iio/light/rohm-bu27008.c line 851
Quoted 5 lines
drivers/iio/light/rohm-bu27008.c new file +5
-0+irqreturn_t ret =IRQ_NONE ; + +mutex_lock (&data->mutex); +if (data->trigger_enabled) { +iio_trigger_poll_nested (data->trig);Add a comment here on why it makes sense to hold the mutex whilst calling this.
After revising this - I don't think it makes. Nor do I think we need the trigger_enable flag so we don't propably need the mutex in buffer enable either as all raw-write configs are claiming the direct mode.
I'll cook the v2 soon(ish). Thanks!
Yours,
--Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Re: [PATCH v1 1/3] dt-bindings: iio: light: ROHM BU27008
In reply to Krzysztof KozlowskiQuoted 9 lines · Krzysztof Kozlowski
Quoted 8 lines · Matti Vaittinen
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add BU27008 dt-bindings.
Quoted 7 lines
Quoted 5 lines
Signed-off-by Matti Vaittinen <[email protected]>--- .../bindings/iio/light/rohm-bu27008.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+)create mode 100644
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yamlFilename like compatible, so rohm,bu27008.yaml
Thanks Krzysztof. I should've remembered this as you told me the same thing during the bu27034 review. Feel free to kick me atthe ELC-E if I do the same mistake with bu27010 as well ^^;
Quoted 17 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml line 4
Quoted 12 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +4
-0@@ -0,0 +1,49 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/light/bu27008.yaml#Does not look like you tested the bindings. Please run
make dt_binding_check(seeDocumentation/devicetree/bindings/writing-schema.rstfor instructions).
I actually did. But I did first run the dt_binding_check without filename - causing it to check all the in-tree bindings - which took a while. So, I went to have a lunch. When I came back I re-ran the check with the filename (DT_SCHEMA_FILES=...) - which gave me no errors.
I _assume_ this is because running the check for all bindings had already done <add step here> generating the warning, while re-running the check with the filename omitted the <add step here> and no longer displayed the warning.
In any case, I missed warning from full-check, and checker missed the warning when re-ran.
Quoted 11 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml line 12
Quoted 9 lines
Documentation/devicetree/bindings/iio/light/rohm-bu27008.yaml new file +8
-0+$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: ROHM BU27008 color sensor + +maintainers: + - Matti Vaittinen <[email protected]> + +description: |Do not need '|' unless you need to preserve formatting.
Ok, thanks.
I'll fix these for v2 :)
Yours,
-- Matti
Signature · 5 lines
Matti Vaittinen Linux kernel developer at ROHM Semiconductors Oulu Finland ~~ When things go utterly wrong vim users can always type :help! ~~
Jonathan Cameron
maintainerRe: [PATCH v1 2/3] iio: light: ROHM BU27008 color sensor
In reply to Vaittinen, MattiQuoted 31 lines · Matti
Quoted 22 lines · Jonathan Cameron
Quoted 11 lines · On Fri, 21 Apr 2023 12:39:36 +0300 Matti Vaittinen <[email protected]>…
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
-raw_read()of RGB and clear channels
- triggered buffer w/ DRDY interrtuptSigned-off-by Matti Vaittinen <[email protected]>Hi Matti,
Biggest issue in here is some confusion over data packing when some channels are enabled. The driver must pack the channels that are enabled (seen from
active_scan_mask) according to general IIO rules. So naturally aligned buffers. Thus for this device it should always be packed intoQuoted 7 lines
struct scan { __le16 chans[4]; s64 ts __aligned(8); /*it's aligned anyway but better not to make reviewers think ;) */ };Even though there are 5 possible channels. If one in the middle isnt' enabled (e.g. blue) then clear and IR shift to lower words. of the buffer.
Quoted 7 lines
Ah, right. So I had misunderstood how the buffer works. I thought the
scan_maskwas only used to disallow unsupported channel-enabling configurations. If I understand your statement correctly, thescan_maskis used to determine the 'place of data' in the buffer when certain configuration is used. (I'll check this from the code but if the IIO handles data as I now think - that's cool! It should indeed simplify the buffer in driver side!).
'Place' is a little confusing (English is imprecise sometimes). Order is perhaps more precise.
Place can mean same as order - as in 1st place in a race, but it can also mean a specific location such as a place at a table where only some seats are full.
The handling for this came about as part of the multiple consumer support for SoC ADCs but it was also useful for ripping out a whole load of driver specific handling that did similar repacking and letting the generic code handle repacking data. It is fairly optimal and does things like larger memcpys if there are a set of channels that need moving, and cleanly makes it a noop if there is nothing to do at all. All those tricks came IIRC from individual drivers that had previously been doing this magic.
Jonathan
Jonathan Cameron
maintainerRe: [PATCH v1 2/3] iio: light: ROHM BU27008 color sensor
In reply to Matti VaittinenQuoted 65 lines · On Mon, 24 Apr 2023 13:14:56 +0300 Matti Vaittinen <[email protected]>…
Quoted 13 lines · Jonathan Cameron
Quoted 10 lines · On Fri, 21 Apr 2023 12:39:36 +0300 Matti Vaittinen <[email protected]>…
The ROHM BU27008 is a sensor with 5 photodiodes (red, green, blue, clear and IR) with four configurable channels. Red and green being always available and two out of the rest three (blue, clear, IR) can be selected to be simultaneously measured. Typical application is adjusting LCD backlight of TVs, mobile phones and tablet PCs.
Add initial support for the ROHM BU27008 color sensor.
-raw_read()of RGB and clear channels
- triggered buffer w/ DRDY interrtuptQuoted 14 lines
drivers/iio/light/rohm-bu27008.c line 514
Quoted 10 lines
Signed-off-by Matti Vaittinen <[email protected]>drivers/iio/light/rohm-bu27008.c new file +9
-0+ +static int bu27008_meas_set (struct bu27008_data *data,bool enable) +{ +if (enable) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN ); + +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_MEAS_EN );Might be cleaner with
regmap_update_bits()Quoted 1 line
+}
Hm. I need to disagree on this although I think it depends on what one is used to :)
For me adding a variable for value to be used is slightly more complex than just using clear or set function depending on the enable/disable. I remember thinking the same as you and preferring the
update_bitsalso on enable/disable cases - until I wrote my first power-supply driver and Sebasian Reichel told me to not do:int val;
if (foo)
val = mask; else val = 0;return
regmap_update_bits(regmap, reg, mask, val);but use set/clear bits. This allows killing the 'int val;'. I remember I had to sleep over night on it but I later started seeing the set/clear bits as a simpler thing.
Sure we could also do
if (foo)
return regmap_update_bits(map, reg, mask, mask); else return regmap_update_bits(map, reg, mask, 0);- but here we just replace:
regmap_set_bits(map, reg, mask)withregmap_update_bits(map, reg, mask, mask)and
Quoted 8 lines
regmap_clear_bits(map, reg, mask)regmap_update_bits(map, reg, mask, 0)with longer but functionally same variants - which kind of says "I think the "
regmap_set_bits()andregmap_clear_bits()" are useless ;)After saying this - I can use the
regmap_update_bits()if you insist, but in my (not always so) humble opinion this does not improve the function.
Makes sense. Leave it as it stands.
Quoted 12 lines
Quoted 11 lines
drivers/iio/light/rohm-bu27008.c line 750
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +8
-0+ +static int bu27008_set_drdy_irq (struct bu27008_data *data,bool state) +{ +if (state) +return regmap_set_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN ); +return regmap_clear_bits (data->regmap,BU27008_REG_MODE_CONTROL3 , +BU27008_MASK_INT_EN );
regmap_update_bits()maybe with the mask and value supplied.
Quoted 2 lines
Same weak objection here as was with the
bu27008_meas_set(). Eg, can change if required but please reconsider :)
Sure. Was a 'maybe' :)
Quoted 19 lines
Quoted 5 lines
Quoted 3 lines
(quoted diff) +2
-0+} +Quoted 13 lines
drivers/iio/light/rohm-bu27008.c line 851
Quoted 10 lines
drivers/iio/light/rohm-bu27008.c new file +10
-0+ +static irqreturn_t bu27008_irq_thread_handler (int irq,void *private) +{ +struct iio_dev *idev = private; +struct bu27008_data *data =iio_priv (idev); +irqreturn_t ret =IRQ_NONE ; + +mutex_lock (&data->mutex); +if (data->trigger_enabled) { +iio_trigger_poll_nested (data->trig);Add a comment here on why it makes sense to hold the mutex whilst calling this.
Quoted 3 lines
After revising this - I don't think it makes. Nor do I think we need the
trigger_enableflag so we don't propably need the mutex in buffer enable either as all raw-write configs are claiming the direct mode.
Clearing this out meant I noticed the oddity of doing this in the thread at all. So all good in the end ;)
Jonathan