summaryrefslogtreecommitdiffstats
path: root/drivers/iio/adc/ina2xx-adc.c
AgeCommit message (Collapse)Author
2020-09-17iio:adc:ina2xx Fix timestamp alignment issue.Jonathan Cameron
commit f8cd222feb82ecd82dcf610fcc15186f55f9c2b5 upstream. One of a class of bugs pointed out by Lars in a recent review. iio_push_to_buffers_with_timestamp assumes the buffer used is aligned to the size of the timestamp (8 bytes). This is not guaranteed in this driver which uses a 32 byte array of smaller elements on the stack. As Lars also noted this anti pattern can involve a leak of data to userspace and that indeed can happen here. We close both issues by moving to a suitable structure in the iio_priv() data with alignment explicitly requested. This data is allocated with kzalloc so no data can leak apart from previous readings. The explicit alignment isn't technically needed here, but it reduced fragility and avoids cut and paste into drivers where it will be needed. If we want this in older stables will need manual backport due to driver reworks. Fixes: c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monitors") Reported-by: Lars-Peter Clausen <lars@metafoo.de> Cc: Stefan Brüns <stefan.bruens@rwth-aachen.de> Cc: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Cc: <Stable@vger.kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-07-07iio: adc: ina2xx: avoid kthread_stop() with stale task_structAkinobu Mita
When the buffer is enabled for ina2xx driver, a dedicated kthread is invoked to capture mesurement data. When the buffer is disabled, the kthread is stopped. However if the kthread gets register access errors, it immediately exits and when the malfunctional buffer is disabled, the stale task_struct pointer is accessed as there is no kthread to be stopped. A similar issue in the usbip driver is prevented by kthread_get_run and kthread_stop_put helpers by increasing usage count of the task_struct. This change applies the same solution. Cc: Stefan Brüns <stefan.bruens@rwth-aachen.de> Cc: Jonathan Cameron <jic23@kernel.org> Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Fixes: c43a102e67db ("iio: ina2xx: add support for TI INA2xx Power Monitors") Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2018-01-08iio: adc: ina2xx: Actually align the loop with the conversion ready flagStefan Brüns
Currently, the registers are read out once per conversion interval. If the reading is delayed as the conversion has not yet finished, this extra time is treated as being part of the readout, although it should delay the start of the poll interval. This results in the interval starting slightly earlier in each iteration, until all time between reads is spent polling the status registers instead of sleeping. To fix this, the delay has to account for the state of the conversion ready flag. Whenever the conversion is already finished, schedule the next read on the regular interval, otherwise schedule it one interval after the flag bit has been set. Split the work function in two functions, one for the status poll and one for reading the values, to be able to note down the time when the flag bit is raised. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Align timestamp with conversion ready flagStefan Brüns
As the timestamp is no longer (ab-)used to measure the function run time, it can be taken at the correct time, i.e. when the conversion has finished. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Use a monotonic clock for delay calculationStefan Brüns
The iio timestamp clock is user selectable and may be non-monotonic. Also, only part of the acquisition time is measured, thus the delay was longer than intended. Use a monotonic timestamp to track the time for the next poll iteration. The timestamp is advanced by the sampling interval each iteration. In case the conversion overrruns the register readout (i.e. fast sampling combined with a slow bus), one or multiple samples will be dropped. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Make calibration register value fixedMaciej Purski
Calibration register is used for calculating current register in hardware according to datasheet: current = shunt_volt * calib_register / 2048 (ina 226) current = shunt_volt * calib_register / 4096 (ina 219) Fix calib_register value to 2048 for ina226 and 4096 for ina 219 in order to avoid truncation error and provide best precision allowed by shunt_voltage measurement. Make current scale value follow changes of shunt_resistor from sysfs as calib_register value is now fixed. Power_lsb value should also follow shunt_resistor changes as stated in datasheet: power_lsb = 25 * current_lsb (ina 226) power_lsb = 20 * current_lsb (ina 219) This is a part of the patchset: https://lkml.org/lkml/2017/11/22/394 Signed-off-by: Maciej Purski <m.purski@samsung.com> Reviewed-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Do not udelay for several secondsStefan Brüns
The conversion time can be up to 16 seconds (8 ms per channel, 2 channels, 1024 times averaging). Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Remove unneeded dummy read to clear CNVR flagStefan Brüns
Although the datasheet states the CNVR flag is cleared by reading the BUS_VOLTAGE register, it is actually cleared by reading any of the voltage/current/power registers. The behaviour has been confirmed by TI support: http://e2e.ti.com/support/amplifiers/current-shunt-monitors/f/931/p/647053/2378282 Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Clarify size requirement for data bufferStefan Brüns
The timestamp is inserted into the buffer after the sample data by iio_push_to_buffers_with_timestamp, document the space requirement for the timestamp. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2018-01-08iio: adc: ina2xx: Remove bogus cast for data argumentStefan Brüns
iio_push_to_buffers_with_timestamp expects a void pointer, so the cast is both unnecessary and misleading. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-12-02iio: adc: ina2xx: Allow setting Shunt Voltage PGA gain and Bus Voltage rangeStefan Brüns
Reducing shunt and bus voltage range improves the accuracy, so allow altering the default settings. Both settings are exposed as gain values. While for the shunt voltage this is straightforward, the bus range settings of 32V (default) and 16V are mapped to gain values of 1 resp. 2, to provide a uniform API to userspace. As the gain settings are incorporated into the raw values by the sensor itself, adjusting of the scale attributes is not necessary. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2017-12-02iio: adc: ina2xx: Use LSB specifier instead of divider in configStefan Brüns
While the config uses the physical value corresponding to the LSB for both the power and the bus voltage register, the shunt voltage is specified as parts of 1 mV. Use the LSB physical value for all registers. No functional change. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2017-12-02iio: adc: ina2xx: Shift bus voltage register to mask flag bitsStefan Brüns
Lower bits of the INA219/220 bus voltage register are conversion status flags, properly shift the value. When reading via IIO buffer, the value is passed on unaltered, shifting is the responsibility of the user. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2017-10-09iio: adc: ina2xx: Adhere to documented ABI, use Ohm instead of uOhmStefan Brüns
According to the ABI documentation, the shunt resistor value should be specificied in Ohm. As this is also used/documented for the MAX9611, use the same for the INA2xx driver. This poses an ABI break for anyone actually altering the shunt value through the sysfs interface, it does not alter the default value nor a value set from the devicetree. Minor change: Fix comment, 1mA is 10^-3A. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2017-09-25Merge tag 'iio-for-4.15a' of ↵Greg Kroah-Hartman
git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next Jonathan writes: Round one of new device support, features and cleanup for IIO in the 4.15 cycle. Note there is a misc driver drop in here given we have support in IIO and the feeling is no one will care. A large part of this series is a boiler plate removal series avoiding the need to explicitly provide THIS_MODULE in various locations. It's very dull but touches all drivers. New device support * ad5446 - add ids to support compatible parts DAC081S101, DAC101S101, DAC121S101. - add the dac7512 id and drop the misc driver as feeling is no one is using it (was introduced for a board that is long obsolete) * mt6577 - add bindings for mt2712 which is fully compatible with other supported parts. * st_pressure - add support for LPS33HW and LPS35HW with bindings (ids mostly). New features * ccs811 - Add support for the data ready trigger. * mma8452 - remove artifical restriction on supporting multiple event types at the same time. * tcs3472 - support out of threshold events Core and tree wide cleanup * Use macro magic to remove the need to provide THIS_MODULE as part of struct iio_info or struct iio_trigger_ops. This is similar to work done in a number of other subsystems (e.g. i2c, spi). All drivers are fixed and then the fields in these structures are removed. This will cause build failures for out of tree drivers and any new drivers that cross with this work going into the kernel. Note mostly done with a coccinelle patch, included in the series on the mailing list but not merged as the fields no longer exist in the structures so the any hold outs will cause a build failure. Cleanups * ads1015 - avoid writing config register when it doesn't change. - add 10% to conversion wait time as it seems it is sometimes a little small. * ade7753 - replace use of core mlock with a local lock. This is part of a long term effort to make the use of mlock opaque and single purpose. * ade7759 - expand the use of buf_lock to cover previous mlock cases. This is a slightly nicer solution to the same issue as in ade7753. * cros_ec - drop an unused variable * inv_mpu6050 - add a missing break in a switch for consistency - not actual bug, - make some local arrays static to save on object code size. * max5481 - drop manual setting of the spi module owner as handled by the spi core. * max5487 - drop manual setting of the spi module owner as handled by the spi core. * max9611 - drop explicit setting of the i2c module owner as handled by the i2c core. * mcp320x - speed up reads on single channel devices, - drop unused of_device_id data elements, - document the struct mcp320x, - improve binding docs to reflect restrictions on spi setup and to make it explicit that the reference regulator is needed. * mma8452 - symbolic to octal permissions, - unsigned to unsigned int. * st_lsm6dsx - avoid setting odr values multiple times, - drop config of LIR as it is only ever set to the existing defaults, - drop rounding configuration as it only ever matches the defaults. * ti-ads8688 - drop manual setting of the spi module owner as handled by the spi core. * tsl2x7x - constify the i2c_device_id, - cleanup limit checks to avoid static checker warnings (and generally have nicer code).
2017-08-28Merge 4.13-rc7 into staging-nextGreg Kroah-Hartman
We want the staging and iio fixes in here to handle the merge issues. Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-08-22iio:adc: drop assign iio_info.driver_module and iio_trigger_ops.ownerJonathan Cameron
The equivalent of both of these are now done via macro magic when the relevant register calls are made. The actual structure elements will shortly go away. Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
2017-07-30iio: adc: ina219: Avoid underflow for sleeping timeStefan Brüns
Proper support for the INA219 lowered the minimum sampling period from 2*140us to 2*84us. Subtracting 200us later leads to an underflow and an almost infinite udelay later. Using a signed int for the sampling period provides sufficient range (at most 2*8640*1024us), but catches the underflow when comparing with buffer_us. Fixes: 18edac2e22f4 ("iio: adc: Fix integration time/averaging for INA219/220") Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
2017-07-01iio: adc: Fix polling of INA219 conversion ready flagStefan Brüns
While the INA226 has a conversion ready flag (CVRF) in the R/W Mask/Enable register with read-to-clear semantics, the corresponding bit of the INA219 (CNVR) is part of the bus voltage register. The flag is cleared by reading the power register. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2017-06-03iio: adc: Fix integration time/averaging for INA219/220Stefan Brüns
INA226/230/231 has integration times per voltage channel and common averaging setting for both channels, while the INA219/220 only has a combined integration time/averaging setting per channel. Only expose the averaging attribute for the INA226, and expose the correct integration times for the INA219. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2017-06-03iio: adc: ina2xx: Make use of attribute flags consistent on all channelsStefan Brüns
Flags for shared channel attributes should be set on all channels of a channel set. I.e. SAMP_FREQUENCY and OVERSAMPLING_RATIO are set on the in_voltage{0,1} channels, thus should be set on in_power, in_current. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2017-05-07iio: adc: Fix bad GENMASK use, typos, whitespaceStefan Brüns
Commit 7906dd52c5a0 ("iio: ina2xx: Fix whitespace and re-order code") changed the register number of the MASK_ENABLE register from 0x06 to the value equivalent GENMASK(2,1), although its no mask. Also fix a typo (INA2_6_6 instead of INA2_2_6), and use the datasheet name ("Mask/Enable") for the register number define. Fix bad indentation for channel attributes. Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de> Acked-by: Andrew F. Davis <afd@ti.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2017-03-19iio: adc: ina2xx: Add OF device ID tableJavier Martinez Canillas
The driver doesn't have a struct of_device_id table but supported devices are registered via Device Trees. This is working on the assumption that a I2C device registered via OF will always match a legacy I2C device ID and that the MODALIAS reported will always be of the form i2c:<device>. But this could change in the future so the correct approach is to have an OF device ID table if the devices are registered via OF. Signed-off-by: Javier Martinez Canillas <javier@osg.samsung.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2017-01-10iio:kfifo_buf header include push down.Jonathan Cameron
As a precursor to splitting buffer.h, lets make sure all drivers include the relevant headers rather than relying on picking them up from kfifo_buf.h. Signed-off-by: Jonathan Cameron <jic23@kernel.org> Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
2016-09-03iio: adc: ina2xx: remove unused debug field from chip global dataAlison Schofield
commit 1961bce76452 "iio: ina2xx: Remove trace_printk debug statements" removed the code that used the chip->prev_ns field. This patch cleans it up further by removing the unused field and assignments. Signed-off-by: Alison Schofield <amsfield22@gmail.com> Cc: Daniel Baluta <daniel.baluta@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-07-03iio: adc: add missing of_node references to iio_devMatt Ranostay
Adding missing indio_dev->dev.of_node references to allow iio consumers to access the device channels. Signed-off-by: Matt Ranostay <mranostay@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-06-30iio:core: timestamping clock selection supportGregor Boirie
Adds a new per-device sysfs attribute "current_timestamp_clock" to allow userspace to select a particular POSIX clock for buffered samples and events timestamping. Following clocks, as listed in clock_gettime(2), are supported: CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_MONOTONIC_RAW, CLOCK_REALTIME_COARSE, CLOCK_MONOTONIC_COARSE, CLOCK_BOOTTIME and CLOCK_TAI. Signed-off-by: Gregor Boirie <gregor.boirie@parrot.com> Acked-by: Sanchayan Maity <maitysanchayan@gmail.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-04-03iio: ina2xx-adc: fix scale for VShuntMarc Titinger
The scale would result in uV instead of expected mV. Mostly cosmetic, since the value of 'Power' was computed OK. Signed-off-by: Marc Titinger <marc.titinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-03-21iio: ina2xx-adc: update the CALIB. register when RShunt changesMarc Titinger
The user (or an init script) may setup RShunt via sysfs after the driver was initialized, for instance based on the EEPROM contents of a modular probe. The calibration register must be set accordingly. Signed-off-by: Marc Titinger <marc.titinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-02-24iio: ina2xx: Remove trace_printk debug statmentsAndrew F. Davis
These are generally for devlopment use only, remove these from performance-critical code, convert to dev_dbg elswhere. Signed-off-by: Andrew F. Davis <afd@ti.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2016-02-24iio: ina2xx: Fix whitespace and re-order codeAndrew F. Davis
Group of probably overly rigorous whitespace and code cleanups. - Alphabetize includes - Assign to variables in the order they are defined - Alignment issues - Group alike statements together - Use helper macros Signed-off-by: Andrew F. Davis <afd@ti.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-22iio: adc: ina2xx: Fix incorrect report of data endianness to userspace.Jonathan Cameron
This was extracted from a reposting of the driver after it had been applied to the IIO tree. I have fast tracked it as the driver will be in 4.5 and it would be nice to fix this trivial issue before it is. Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-19iio: ina2xx: fix channel order in software bufferMarc Titinger
POWER and CURRENT were swapped out in the buffer: was current2 and power3, correct order is power2 and current3. Signed-off-by: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-12iio: ina2xx: give the capture kthread a more useful name string.Marc Titinger
PID PPID USER STAT VSZ %VSZ %CPU COMMAND 144 2 root DW 0 0% 33% [ina226:1-8800us] 141 2 root DW 0 0% 25% [ina226:0-8800us] 40 2 root SW 0 0% 15% [irq/156-4802a00] 147 2 root DW 0 0% 7% [ina226:2-8800us] 145 1 root S 1236 0% 6% dd if /dev/iio:device1 of /dev/null 148 1 root S 1236 0% 4% dd if /dev/iio:device2 of /dev/null 149 137 root R 1244 0% 3% top -d 1 142 1 root S 1236 0% 2% dd if /dev/iio:device0 of /dev/null Signed-off-by: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-12iio: ina2xx: re-instate a sysfs show/store for the shunt resistor valueMarc Titinger
Different probe modules use different resistor values. The front-end application may read a probe ID (from eeprom) and set the shunt value accordingly. Signed-off-by: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-12iio: ina2xx: provide a sysfs parameter to allow async readout of the ADCsMarc Titinger
This can lead to repeated or skipped samples depending on the clock beat between the capture thread and the chip sampling clock, but will also spare reading/waiting for the Capture Ready Flag and improve the available i2c bandwidth for reading measurements. Output of iio_info: ...snip... 4 device-specific attributes found: attr 0: in_oversampling_ratio value: 4 attr 1: in_allow_async_readout value: 0 attr 2: integration_time_available value: 140 204 332 588 1100 2116... attr 3: in_sampling_frequency value: 114 Signed-off-by: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
2015-12-12iio: ina2xx: add support for TI INA2xx Power MonitorsMarc Titinger
in SOFTWARE buffer mode, a kthread will capture the active scan_elements into a kfifo, then compute the remaining time until the next capture tick and do an active wait (udelay). This will produce a stream of up to fours channels plus a 64bits timestamps (ns). Tested with ina226, on BeagleBoneBlack. Datasheet: http://www.ti.com/lit/gpn/ina226 Signed-off-by: Marc Titinger <mtitinger@baylibre.com> Signed-off-by: Jonathan Cameron <jic23@kernel.org>