iio: devm helpers and Murata zpa2326 barometer support
Subsystems (3)
v2 cover iio: devm helpers and Murata zpa2326 barometer support
Introduce support for the Murata ZPA2326 pressure and temperature sensor. Also bring in resource managed versions for triggered buffer setup helpers and trigger registration which the above mentioned driver depends on.
Driver features :
* I2C and SPI buses
* interrupt and polling based operations
* direct, triggered and software buffered IIO sampling modes
* hardware sampling frequency handling
* trigger consumers notification (plugged on interrupt)
* runtime and system power states
Changes since v1 :
* cosmetic and spelling corrections
* remove useless header
* remove useless u8 cast from register macro definitions
* fix uninitialized pressure word for continuous sampling operation
* rework of zpa_dequeue_pressure error handling flow for enhanced readability
* merge zpa_acknowledge_irq() into zpa_handle_threaded_irq() for enhanced readability
* remove useless pm_runtime_mark_last_busy() invocation
* remove useless channel initializers
* add VRef regulator support
* switch to regmap based bus operations (save ~100 lines of code)
* make symbol prefixes consistent with source file name (zpa_ -> zpa2326_)
* get rid of BUG() usage
* protect sampling frequency reads from userspace with mutex
* remove DEBUG macro (rely on dynamic debug or a futur DEBUG Kconfig option defined at IIO level)
* rename ZPA2326_CONVERSION_TIMEOUT macros to ZPA2326_CONVERSION_JIFFIES
* simplify runtime power management error handling
* rework ring setup operations for better separation of concerns
* fix module removal / devm_iio_device_register() race condition
* factor out hardware detection sequence
Regards,
gregor.
Gregor Boirie (3):
iio:trigger: add resource managed (un)register
iio: add resource managed triggered buffer init helpers iio:pressure: initial zpa2326 barometer support
.../devicetree/bindings/iio/pressure/zpa2326.txt | 23 + Documentation/driver-model/devres.txt | 4 + drivers/iio/buffer/industrialio-triggered-buffer.c | 42 + drivers/iio/industrialio-core.c | 3 +- drivers/iio/industrialio-trigger.c | 59 + drivers/iio/pressure/Kconfig | 35 + drivers/iio/pressure/Makefile | 3 + drivers/iio/pressure/zpa2326.c | 1736 ++++++++++++++++++++ drivers/iio/pressure/zpa2326.h | 93 ++ drivers/iio/pressure/zpa2326_i2c.c | 97 ++ drivers/iio/pressure/zpa2326_spi.c | 101 ++ include/linux/iio/iio.h | 1 + include/linux/iio/trigger.h | 6 + include/linux/iio/triggered_buffer.h | 8 + 14 files changed, 2210 insertions(+), 1 deletion(-) create mode 100644 Documentation/devicetree/bindings/iio/pressure/zpa2326.txt create mode 100644 drivers/iio/pressure/zpa2326.c create mode 100644 drivers/iio/pressure/zpa2326.h create mode 100644 drivers/iio/pressure/zpa2326_i2c.c create mode 100644 drivers/iio/pressure/zpa2326_spi.c
Signature · 1 line
2.1.4
PATCH v2 1/3 iio:trigger: add resource managed (un)register
Add resource managed devm_iio_trigger_register() and devm_iio_triger_unregister() to automatically clean up registered triggers allocated by IIO drivers, thus leading to simplified IIO drivers code.
--- Documentation/driver-model/devres.txt | 2 ++ drivers/iio/industrialio-trigger.c | 59 +++++++++++++++++++++++++++++++++++ include/linux/iio/trigger.h | 6 ++++ 3 files changed, 67 insertions(+)
Documentation/driver-model/devres.txt +2 -0
@@ -268,6 +268,8 @@ IIO devm_iio_kfifo_free() devm_iio_trigger_alloc() devm_iio_trigger_free() + devm_iio_trigger_register() + devm_iio_trigger_unregister() devm_iio_channel_get() devm_iio_channel_release() devm_iio_channel_get_all()
drivers/iio/industrialio-trigger.c +59 -0
65 lines not on this page · raw message
include/linux/iio/trigger.h +6 -0
@@ -125,12 +125,18 @@ static inline void *iio_trigger_get_drvdata(struct iio_trigger *trig)**/ int iio_trigger_register (struct iio_trigger *trig_info); +int devm_iio_trigger_register (struct device *dev, +struct iio_trigger *trig_info); +/** * iio_trigger_unregister() - unregister a trigger from the core * @trig_info: trigger to be unregistered **/ void iio_trigger_unregister (struct iio_trigger *trig_info); +void devm_iio_trigger_unregister (struct device *dev, +struct iio_trigger *trig_info); +/** * iio_trigger_poll() - called on a trigger occurring * @trig: trigger which occurred
Signature · 1 line
2.1.4
PATCH v2 2/3 iio: add resource managed triggered buffer init helpers
In reply to Gregor BoirieAdd resource managed devm_iio_triggered_buffer_setup() and devm_iio_triggered_buffer_cleanup() to automatically clean up triggered buffers setup by IIO drivers, thus leading to simplified IIO drivers code.
--- Documentation/driver-model/devres.txt | 2 ++ drivers/iio/buffer/industrialio-triggered-buffer.c | 42 ++++++++++++++++++++++ drivers/iio/industrialio-core.c | 3 +- include/linux/iio/iio.h | 1 + include/linux/iio/triggered_buffer.h | 8 +++++ 5 files changed, 55 insertions(+), 1 deletion(-)
Documentation/driver-model/devres.txt +2 -0
@@ -266,6 +266,8 @@ IIO devm_iio_device_unregister() devm_iio_kfifo_allocate() devm_iio_kfifo_free() + devm_iio_triggered_buffer_setup() + devm_iio_triggered_buffer_cleanup() devm_iio_trigger_alloc() devm_iio_trigger_free() devm_iio_trigger_register()
drivers/iio/buffer/industrialio-triggered-buffer.c +42 -0
48 lines not on this page · raw message
drivers/iio/industrialio-core.c +2 -1
@@ -1309,7 +1309,7 @@ static void devm_iio_device_release(struct device *dev, void *res)iio_device_free (*(struct iio_dev **)res); } -static int devm_iio_device_match (struct device *dev,void *res,void *data) +int devm_iio_device_match (struct device *dev,void *res,void *data) {struct iio_dev **r = res;if (!r || !*r) {
@@ -1318,6 +1318,7 @@ static int devm_iio_device_match(struct device *dev, void *res, void *data) }return *r == data; } +EXPORT_SYMBOL_GPL (devm_iio_device_match);/** * devm_iio_device_alloc - Resource-managed iio_device_alloc()
include/linux/iio/iio.h +1 -0
@@ -642,6 +642,7 @@ static inline struct iio_dev *iio_priv_to_dev(void *priv) }void iio_device_free (struct iio_dev *indio_dev); +int devm_iio_device_match (struct device *dev,void *res,void *data);struct iio_dev *devm_iio_device_alloc (struct device *dev,int sizeof_priv);void devm_iio_device_free (struct device *dev,struct iio_dev *indio_dev); struct iio_trigger *devm_iio_trigger_alloc(struct device *dev,
include/linux/iio/triggered_buffer.h +8 -0
@@ -12,4 +12,12 @@ int iio_triggered_buffer_setup(struct iio_dev *indio_dev, const struct iio_buffer_setup_ops *setup_ops);void iio_triggered_buffer_cleanup (struct iio_dev *indio_dev); +int devm_iio_triggered_buffer_setup (struct device *dev, +struct iio_dev *indio_dev, +irqreturn_t (*h)(int irq,void *p), +irqreturn_t (*thread)(int irq,void *p), +const struct iio_buffer_setup_ops *ops); +void devm_iio_triggered_buffer_cleanup (struct device *dev, +struct iio_dev *indio_dev); +#endif
Signature · 1 line
2.1.4
PATCH v2 3/3 iio:pressure: initial zpa2326 barometer support
In reply to Gregor BoirieIntroduce driver for Murata ZPA2326 pressure and temperature sensor:
http://www.murata.com/en-us/products/productdetail?partno=ZPA2326-0311A-R
--- .../devicetree/bindings/iio/pressure/zpa2326.txt | 23 + drivers/iio/pressure/Kconfig | 35 + drivers/iio/pressure/Makefile | 3 + drivers/iio/pressure/zpa2326.c | 1736 ++++++++++++++++++++ drivers/iio/pressure/zpa2326.h | 93 ++ drivers/iio/pressure/zpa2326_i2c.c | 97 ++ drivers/iio/pressure/zpa2326_spi.c | 101 ++ 7 files changed, 2088 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/pressure/zpa2326.txt create mode 100644 drivers/iio/pressure/zpa2326.c create mode 100644 drivers/iio/pressure/zpa2326.h create mode 100644 drivers/iio/pressure/zpa2326_i2c.c create mode 100644 drivers/iio/pressure/zpa2326_spi.c
Documentation/devicetree/bindings/iio/pressure/zpa2326.txt new file +23 -0
@@ -0,0 +1,23 @@ +Murata ZPA2326 pressure sensor + +Pressure sensor from Murata with SPI and I2C bus interfaces. + +Required properties: +- compatible: "murata,zpa2326" +- reg: the I2C address or SPI chip select the device will respond to + +Optional properties: +- interrupt-parent: should be the phandle for the interrupt controller +- interrupts: interrupt mapping for IRQ +- vdd-supply: an optional regulator that needs to be on to provide VDD + power to the sensor + +Example: + +zpa2326@5c { + compatible = "murata,zpa2326"; + reg = <0x5c>; + interrupt-parent = <&gpio>; + interrupts = <12>; + vdd-supply = <&ldo_1v8_gnss>; +};
drivers/iio/pressure/Kconfig +35 -0
@@ -187,4 +187,39 @@ config HP206C This driver can also be built as a module. If so, the module will be called hp206c. +config ZPA2326 + tristate "Murata ZPA2326 pressure sensor driver" + depends on I2C || SPI_MASTER + select REGMAP + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say Y here to build support for the Murata ZPA2326 pressure and + temperature sensor. + + To compile this driver as a module, choose M here: the module will + be called zpa2326. + +config ZPA2326_I2C + tristate "support I2C bus connection" + depends on I2C && ZPA2326 + select REGMAP_I2C + help + Say Y here to build I2C bus support for the Murata ZPA2326 pressure + and temperature sensor. + + To compile this driver as a module, choose M here: the module will + be called zpa2326_i2c. + +config ZPA2326_SPI + tristate "support SPI bus connection" + depends on SPI_MASTER && ZPA2326 + select REGMAP_SPI + help + Say Y here to build SPI bus support for the Murata ZPA2326 pressure + and temperature sensor. + + To compile this driver as a module, choose M here: the module will + be called zpa2326_spi. + endmenu
drivers/iio/pressure/Makefile +3 -0
@@ -22,6 +22,9 @@ st_pressure-y := st_pressure_core.o st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o obj-$(CONFIG_T5403) += t5403.o obj-$(CONFIG_HP206C) += hp206c.o +obj-$(CONFIG_ZPA2326) += zpa2326.o +obj-$(CONFIG_ZPA2326_I2C) += zpa2326_i2c.o +obj-$(CONFIG_ZPA2326_SPI) += zpa2326_spi.o obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o
drivers/iio/pressure/zpa2326.c new file +1736 -0
1,736 lines not on this page · raw message
drivers/iio/pressure/zpa2326.h new file +93 -0
93 lines not on this page · raw message
drivers/iio/pressure/zpa2326_i2c.c new file +97 -0
97 lines not on this page · raw message
drivers/iio/pressure/zpa2326_spi.c new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
2.1.4
Re: [PATCH v2 3/3] iio:pressure: initial zpa2326 barometer support
Quoted 4 lines · Gregor Boirie
Introduce driver for Murata ZPA2326 pressure and temperature sensor:
http://www.murata.com/en-us/products/productdetail?partno=ZPA2326-0311A-RSigned-off-by Gregor Boirie <[email protected]>
Looking nice!
Documentation/devicetree/bindings/iio/pressure/zpa2326.txt line 14
Quoted 6 lines
Documentation/devicetree/bindings/iio/pressure/zpa2326.txt new file +6
-0+Optional properties: +- interrupt-parent: should be the phandle for the interrupt controller +- interrupts: interrupt mapping for IRQ +- vdd-supply: an optional regulator that needs to be on to provide VDD + power to the sensor +
vref-supply?
drivers/iio/pressure/zpa2326.c line 89
Quoted 7 lines
drivers/iio/pressure/zpa2326.c new file +7
-0+/* Hardware sampling frequency descriptor. */ +struct zpa2326_frequency { +/* Stringified frequency in Hertz. */ +const char *hz; +/* Output Data Rate word as expected by ZPA2326_CTRL_REG3_REG. */ +u16 odr; +};
I still prefer the kerneldoc style of documenting, even if we're not building the
documentation from this file. But OK it's a matter of taste maybe.
drivers/iio/pressure/zpa2326.c line 110
Quoted 2 lines
drivers/iio/pressure/zpa2326.c new file +2
-0+/* Per-device internal private state. */ +struct zpa2326_private {
That opinion applies also for this struct.
drivers/iio/pressure/zpa2326.c line 170
Quoted 8 lines
drivers/iio/pressure/zpa2326.c new file +8
-0+/* +* Fetch single byte from slave register. +* indio_dev: IIO device the slave is attached to. +* address: address of register to read from. +* +* Returns the fetched byte when successful, a negative error code otherwise. +*/ +static int zpa2326_read_byte (const struct iio_dev *indio_dev,u8 address)
I also prefer functions to use kerneldoc style. Even if they are not built into documents. For uniformness.
drivers/iio/pressure/zpa2326.c line 287
Quoted 4 lines
drivers/iio/pressure/zpa2326.c new file +4
-0+static int zpa2326_enable_device (const struct iio_dev *indio_dev) +{ +int err =zpa2326_write_byte (indio_dev,ZPA2326_CTRL_REG0_REG , +ZPA2326_CTRL_REG0_ENABLE );
Argh declaring a variable and immediately performing an operation assigning it a value scares me. I can't say why, it's just that I want it like:
int ret;
ret = zpa...
I also use "ret" over "err". The rationale is that it is a return code, not an error code. It is only an error code if it is negative or != 0. Again that is a personal preference I guess, at least I have an explanation for it.
Quoted 20 lines
drivers/iio/pressure/zpa2326.c new file +20
-0+static int zpa2326_power_on (const struct iio_dev *indio_dev, +const struct zpa2326_private *private) +{ +int err =regulator_enable (private->vref); + +if (err) +return err; + + err =regulator_enable (private->vdd); +if (err) + goto vref; + +zpa2326_dbg (indio_dev,"powered on" ); + + err =zpa2326_enable_device (indio_dev); +if (err) + goto vdd; + + err =zpa2326_reset_device (indio_dev); +if (err)
drivers/iio/pressure/zpa2326.c line 395
Quoted 10 lines
drivers/iio/pressure/zpa2326.c new file +10
-0+ goto disable; + +return 0 ; + +disable: +zpa2326_disable_device (indio_dev); +vdd: +regulator_disable (private->vdd); +vref: +regulator_disable (private->vref);
I would personally name the labels "out_disable_device" "out_disable_vdd", "out_disable_vref" but maybe I have a bit of talkative style.
drivers/iio/pressure/zpa2326.c line 409
Quoted 9 lines
drivers/iio/pressure/zpa2326.c new file +9
-0+/* Power off device, i.e. disable attached power regulators. */ +static void zpa2326_power_off (const struct iio_dev *indio_dev, +const struct zpa2326_private *private) +{ +regulator_disable (private->vdd); +regulator_disable (private->vref); + +zpa2326_dbg (indio_dev,"powered off" ); +}
Why is zpa2326_disable_device() called on the error path but not in the power off function?
Quoted 16 lines
drivers/iio/pressure/zpa2326.c new file +16
-0+static int zpa2326_dequeue_pressure (const struct iio_dev *indio_dev, +u32 *pressure) +{ +int err =zpa2326_read_byte (indio_dev,ZPA2326_STATUS_REG ); +int cleared =-1 ; + +if (err <0 ) +return err; + + *pressure =0 ; + +if (err &ZPA2326_STATUS_P_OR ) { +/* +* Fifo overrun : first sample dequeued from fifo is the +* newest. +*/
drivers/iio/pressure/zpa2326.c line 538
Quoted 10 lines
drivers/iio/pressure/zpa2326.c new file +10
-0+zpa2326_warn (indio_dev,"fifo overflow" ); + + err =zpa2326_read_block (indio_dev,ZPA2326_PRESS_OUT_XL_REG ,3 , + (u8 *)pressure); +if (err) +return err; + +#define ZPA2326_FIFO_DEPTH (16U) +/* Hardware fifo may hold no more than 16 pressure samples. */ +return zpa2326_clear_fifo (indio_dev,ZPA2326_FIFO_DEPTH -1 );
I would not put a #define in the middle of the code like that, but in the top of the file. But I've seen others do this so it's no strong opinion.
drivers/iio/pressure/zpa2326.c line 1446
Quoted 7 lines
drivers/iio/pressure/zpa2326.c new file +7
-0+/* +* Pressure resolution is 1/64 Pascal. Scale to kPascal +* as required by IIO ABI. +*/ + *val =0 ; + *val2 =1000000 /64 ; +return IIO_VAL_INT_PLUS_NANO ;
If what you return is a fraction why not:
*val = 1000000;
*val2 = 64;
return IIO_VAL_FRACTIONAL;
?
If you insist on performing a division in the code, use DIV_ROUND_CLOSEST().
drivers/iio/pressure/zpa2326.c line 1476
Quoted 6 lines
drivers/iio/pressure/zpa2326.c new file +6
-0+case IIO_CHAN_INFO_OFFSET : +switch (chan->type) { +case IIO_TEMP : + *val =-17683000 /649 ; + *val2 = ((17683000 %649 ) *1000000000ULL ) /649ULL ; +return IIO_VAL_INT_PLUS_NANO ;
Same here. Rewrite that equation as a fraction and it becomes much prettier I think.
drivers/iio/pressure/zpa2326.c line 1539
Quoted 12 lines
drivers/iio/pressure/zpa2326.c new file +12
-0+static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, + zpa2326_show_frequency, + zpa2326_store_frequency); + +/* Expose supported hardware sampling frequencies (Hz) through sysfs. */ +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23"); + +static struct attribute *zpa2326_attributes[] = { + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, +NULL +};
I was asked to supply these through the raw read/write functions in my driver by using IIO_CHAN_INFO_SAMP_FREQ. It works like a charm! If you want it to apply to the whole device, use the .info_mask_shared_by_all = = BIT(IIO_CHAN_INFO_SAMP_FREQ),
(See my MPU-3050 driver patch for an example.)
Yours,
Linus Walleij
Re: [PATCH v2 3/3] iio:pressure: initial zpa2326 barometer support
Quoted 8 lines · Linus Walleij
Quoted 4 lines · Gregor Boirie
Introduce driver for Murata ZPA2326 pressure and temperature sensor:
http://www.murata.com/en-us/products/productdetail?partno=ZPA2326-0311A-RSigned-off-by Gregor Boirie <[email protected]>Looking nice!
Quoted 7 lines
Documentation/devicetree/bindings/iio/pressure/zpa2326.txt line 14
Quoted 6 lines
Documentation/devicetree/bindings/iio/pressure/zpa2326.txt new file +6
-0+Optional properties: +- interrupt-parent: should be the phandle for the interrupt controller +- interrupts: interrupt mapping for IRQ +- vdd-supply: an optional regulator that needs to be on to provide VDD + power to the sensor +vref-supply?
done
Quoted 10 lines
drivers/iio/pressure/zpa2326.c line 89
Quoted 7 lines
drivers/iio/pressure/zpa2326.c new file +7
-0+/* Hardware sampling frequency descriptor. */ +struct zpa2326_frequency { +/* Stringified frequency in Hertz. */ +const char *hz; +/* Output Data Rate word as expected by ZPA2326_CTRL_REG3_REG. */ +u16 odr; +};I still prefer the kerneldoc style of documenting, even if we're not building the
documentation from this file. But OK it's a matter of taste maybe.
ok. let's beautify these
Quoted 23 lines
drivers/iio/pressure/zpa2326.c line 110
Quoted 2 lines
drivers/iio/pressure/zpa2326.c new file +2
-0+/* Per-device internal private state. */ +struct zpa2326_private {That opinion applies also for this struct.
drivers/iio/pressure/zpa2326.c line 170
Quoted 8 lines
drivers/iio/pressure/zpa2326.c new file +8
-0+/* +* Fetch single byte from slave register. +* indio_dev: IIO device the slave is attached to. +* address: address of register to read from. +* +* Returns the fetched byte when successful, a negative error code otherwise. +*/ +static int zpa2326_read_byte (const struct iio_dev *indio_dev,u8 address)I also prefer functions to use kerneldoc style. Even if they are not built into documents. For uniformness.
drivers/iio/pressure/zpa2326.c line 287
Quoted 4 lines
drivers/iio/pressure/zpa2326.c new file +4
-0+static int zpa2326_enable_device (const struct iio_dev *indio_dev) +{ +int err =zpa2326_write_byte (indio_dev,ZPA2326_CTRL_REG0_REG , +ZPA2326_CTRL_REG0_ENABLE );Argh declaring a variable and immediately performing an operation assigning it a value scares me. I can't say why, it's just that I want it like:
Quoted 8 lines
int ret;
ret = zpa...
I also use "ret" over "err". The rationale is that it is a return code, not an error code. It is only an error code if it is negative or != 0. Again that is a personal preference I guess, at least I have an explanation for it.
Agreed. However, in this precise case, the variable is used for error handling purpose only.
Renamed quite a few of them in the whole driver.
Quoted 34 lines
Quoted 20 lines
drivers/iio/pressure/zpa2326.c new file +20
-0+static int zpa2326_power_on (const struct iio_dev *indio_dev, +const struct zpa2326_private *private) +{ +int err =regulator_enable (private->vref); + +if (err) +return err; + + err =regulator_enable (private->vdd); +if (err) + goto vref; + +zpa2326_dbg (indio_dev,"powered on" ); + + err =zpa2326_enable_device (indio_dev); +if (err) + goto vdd; + + err =zpa2326_reset_device (indio_dev); +if (err)drivers/iio/pressure/zpa2326.c line 395
Quoted 10 lines
drivers/iio/pressure/zpa2326.c new file +10
-0+ goto disable; + +return 0 ; + +disable: +zpa2326_disable_device (indio_dev); +vdd: +regulator_disable (private->vdd); +vref: +regulator_disable (private->vref);I would personally name the labels "
out_disable_device" "out_disable_vdd", "out_disable_vref" but maybe I have a bit of talkative style.
Quoted 11 lines
drivers/iio/pressure/zpa2326.c line 409
Quoted 9 lines
drivers/iio/pressure/zpa2326.c new file +9
-0+/* Power off device, i.e. disable attached power regulators. */ +static void zpa2326_power_off (const struct iio_dev *indio_dev, +const struct zpa2326_private *private) +{ +regulator_disable (private->vdd); +regulator_disable (private->vref); + +zpa2326_dbg (indio_dev,"powered off" ); +}Why is
zpa2326_disable_device()called on the error path but not in the power off function?
zpa2326_disable_device() is used to switch hardware to a low power state. It is used in many locations to implement one-shot/on demand sampling without switching regulators off.
The reason is that getting out of hardware low power states (zpa2326_enable_device()) is short enough (1 ms) so that we can afford switching back and forth all the time. However, we do have no control over regulators power up time...
Hence :
* regulators related operations are integrated within pm_runtime, initial power up and final power off things,
* whereas zpa2326_enable_device() and zpa2326_disable_device() are used along with sampling machinery.
Hope this is clear enough.
Quoted 21 lines
Quoted 21 lines
drivers/iio/pressure/zpa2326.c new file +21
-0+static int zpa2326_dequeue_pressure (const struct iio_dev *indio_dev, +u32 *pressure) +{ +int err =zpa2326_read_byte (indio_dev,ZPA2326_STATUS_REG ); +int cleared =-1 ; + +if (err <0 ) +return err; + + *pressure =0 ; + +if (err &ZPA2326_STATUS_P_OR ) { +/* +* Fifo overrun : first sample dequeued from fifo is the +* newest. +*/ +zpa2326_warn (indio_dev,"fifo overflow" ); + + err =zpa2326_read_block (indio_dev,ZPA2326_PRESS_OUT_XL_REG ,3 , + (u8 *)pressure); +if (err)
Quoted 8 lines
drivers/iio/pressure/zpa2326.c line 538
Quoted 5 lines
drivers/iio/pressure/zpa2326.c new file +5
-0+return err; + +#define ZPA2326_FIFO_DEPTH (16U) +/* Hardware fifo may hold no more than 16 pressure samples. */ +return zpa2326_clear_fifo (indio_dev,ZPA2326_FIFO_DEPTH -1 );I would not put a #define in the middle of the code like that, but in the top of the file. But I've seen others do this so it's no strong opinion.
The idea here is to prevent reader from getting back to the top of file to see macro definition (when used in a single location). Makes things much easier to read provided it doesn't pollute the natural instruction flow (to me).
Quoted 18 lines
drivers/iio/pressure/zpa2326.c line 1446
Quoted 7 lines
drivers/iio/pressure/zpa2326.c new file +7
-0+/* +* Pressure resolution is 1/64 Pascal. Scale to kPascal +* as required by IIO ABI. +*/ + *val =0 ; + *val2 =1000000 /64 ; +return IIO_VAL_INT_PLUS_NANO ;If what you return is a fraction why not:
*val = 1000000;
*val2 = 64;
returnIIO_VAL_FRACTIONAL;?
If you insist on performing a division in the code, use
DIV_ROUND_CLOSEST().
Quoted 8 lines
drivers/iio/pressure/zpa2326.c line 1476
Quoted 6 lines
drivers/iio/pressure/zpa2326.c new file +6
-0+case IIO_CHAN_INFO_OFFSET : +switch (chan->type) { +case IIO_TEMP : + *val =-17683000 /649 ; + *val2 = ((17683000 %649 ) *1000000000ULL ) /649ULL ; +return IIO_VAL_INT_PLUS_NANO ;Same here. Rewrite that equation as a fraction and it becomes much prettier I think.
That's an interesting one ! I must admit this looks *really* ugly.
I initially used IIO_VAL_FRACTIONAL. But on my Raspberry it returned rubbish to userspace. I supposed I hit some kind of corner case but had no time to investigate this : in the advent of a real bug, this would need some extended regression testing to prevent from breaking multiple drivers.
In addition, these will be constant folded (at least with my toolchains). I decided a good comment would propably do the job...
Quoted 17 lines
drivers/iio/pressure/zpa2326.c line 1539
Quoted 12 lines
drivers/iio/pressure/zpa2326.c new file +12
-0+static IIO_DEV_ATTR_SAMP_FREQ(S_IWUSR | S_IRUGO, + zpa2326_show_frequency, + zpa2326_store_frequency); + +/* Expose supported hardware sampling frequencies (Hz) through sysfs. */ +static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23"); + +static struct attribute *zpa2326_attributes[] = { + &iio_dev_attr_sampling_frequency.dev_attr.attr, + &iio_const_attr_sampling_frequency_available.dev_attr.attr, +NULL +};I was asked to supply these through the raw read/write functions in my driver by using
IIO_CHAN_INFO_SAMP_FREQ. It works like a charm! If you want it to apply to the whole device, use the .info_mask_shared_by_all = =BIT(IIO_CHAN_INFO_SAMP_FREQ),
Quoted 1 line
(See my MPU-3050 driver patch for an example.)
ongoing.
Thanks,
Grégor.
Quoted 2 lines
Yours,
Linus Walleij