Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'iio-for-6.12a' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/jic23/iio into char-misc-testing

Jonathan writes:

IIO: 1st set of new device support, features and cleanup for 6.12

Includes a merge of spi-mos-config branch from spi.git that brings
support needed for the AD4000 driver.

Lots of new device support this time including 9 new drivers and substantial
changes to add new support to several more.

New device support
------------------

Given we have a lot of new support, I've subcategorized them:

Substantial changes, or new driver
**********************************

adi,ad4000
- New driver for this high speed ADC.
adi,ad4695
- New driver supporting AD4690, AD4696, AD4697 and AD4698 ADCs.
- Follow up series added triggered buffer support.
adi,ad7380
- Add support for single ended parts, AD7386, ADC7387, AD7388 and -4 variants.
(driver previously only support differential parts).
These variants have an additional front end MUX so only half the channels
can be sampled efficiently.
adi,ad9467
- Refactor and extend driver to support ad9643, ad9449 and ad9652 high speed
ADCs.
adi,adxl380
- New driver for this low power accelerometer.
adi,ltc2664
- New driver supporting LTC2664 and LTC2672 DACs.
microchip,pac1921
- New driver for this power/current monitor chip.
rohm,bh1745
- New driver for this RGBC colour sensor.
rohm,bu27034anuc
- The original bu27034 was canceled before mass production, so the
driver is modified to support the BU27034ANUC which had some significant
differences. DT compatible changed to avoid chance of old driver ever
binding to real hardware.
sciosense,ens210
- New driver for ens210, ens210a, ens211, ens212, ens213a, and ens215
temperature and humidity sensors (all register compatible up to some
conversion time differences)
sensiron,sdp500
- New driver for this differential pressure sensor.
tyhx,hx9023s
- New driver to support this capacitive proximity sensor.

Minor changes to support new devices
************************************

adi,adf4377
- Add support for the single output adf4378.
kionix,kxcjk-1013
- Add support for KX022-1020 accelerometer (binding and ID table only)
liteon,ltrf216a
- Add support for ltr-308. A few minor differences in features set
rockchip,saradc
- Add ID for rk3576-saradc
sensortek,stk3310
- Add ID for stk3013 proximity sensor which (despite documentation) has
an ambient light sensor and is compatible with existing parts.

Documentation updates
---------------------

Generalize ABI docs for shunt resistor attribute
Improve calibscale and calibbias related documentation. A couple of follow
up patches to resolve duplicate documentation that resulted.

New core features
-----------------

backend
- Add option for debugfs - useful for test pattern control
- Use this for both adi-axi-adc and adi-axi-dac
trigger suspend
- Add functions to allow triggers to be suspended. This avoids problems
when a device enters suspend to idle with a sysfs trigger. Use it for now
in the bmi323 only.

New driver features
-------------------

adi,ad7192
- Add option to be a clock provider (+ additional clock config options)
adi,ad7380
- Add documentation for this fairly new driver.
adi,ad9461
- Provide control of test modes and backend validation blocks used
to identify problems (via debugfs)
adi,ad9739
- Add backend debugfs and docs for what is provided via adi-axi-dac
avago,apds9960
- Add proximity and gesture calibration offset control
bosch,bmp280
- Triggered buffer support including adding raw+scale output for sysfs.
liteon,ltr390
- Add configuration of integration time and scale.
stm,dfsdm
- Convert this SD modulator driver to backend framework and add support
for channel scaling + modern channel bindings.

Treewide cleanup
----------------

iio_dev->masklength: Making it private.
- Provide access function to read the core compute channel mask length
and a macro to iterate over elements in the active_scan_mask.
- Enables marking masklength __private preventing drivers from
writing it without triggering a build warning whilst minimizing overhead
in what are typically hot paths.
- Convert all drivers and finally mark it private.
Merge conflicts resolved in drivers applied after this point.
Constify regmap_bus
- These are never modified, so mark them const.

Core cleanup
------------

backend
- A few late breaking bits of feedback (unused variable, error messages)
dma-buffer
- Namespace exports.
core
- Drop unused assignment.

Driver cleanup
--------------

adi,ad4695
- Fixing binding to reflect that common-mode-channel is a scalar.
adi,ad7280a
- Use __free(kfree) to simplify freeing of receive buffer.
adi,ad7606
- Various dt-binding cleanup and improvements.
- Fix oversampling related gpio handling.
- Make polarity of standby gpio match documentation.
- use guard() to simplify lock handling.
adi,ad7768
- Use device_for_each_child_node_scoped() instead of fwnode equivalent.
adi,ad7124
- Reduce SPI transfers by avoiding separate writes to different fields
in the same register.
- Start the ADC in idle mode.
adi,adis
- Drop ifdefs in favor of IS_ENABLED.
adi,admv8818
- Fix wrong ABI docs.
asahi-kasei,ak8975
- Drop a prefix free compatible accidentally added recently.
aspeed,adc
- Use of_property_present() instead of of_find_property() to see if the
property is there or not.
atmel,at91,
- Use __free(kfree) to simplify freeing of channel related array.
bosch,bma400
- Use __free(kfree) to simplify freeing a locally allocated string.
bosch,bmc150
- Add missing mount-matrix binding docs.
bosch,bme680
- Fix read/write to ensure multiple necessary sequential reads without
device configuration change.
- Drop unnecessary type casts and use more appropriate data types.
- Drop some left over ACPI code as ACPI support was removed due to invalid
IDs (and no known users).
- Sort headers consistently.
- Avoid unnecessary duplicate read and redundant read of gas config.
- Use bulk reads to get calibration data.
- Reorder allocation of IIO device to be prior to device init.
- Add remaining read/write buffers to the union used already for all others.
- Tidy up error checks for consistency of style, including dev_err_probe()
- Bring the device startup procedure inline with the vendor code.
- Reorder code so mode forcing is more obvious occurring where needed.
- Tidy up data locality in reading functions so no magic data is stored
in state structures just to get it across function calls.
- Make a local lookup table static to avoid placing it on the stack.
bosch,bmp280
- Fix BME280 regmap to not include registers it doesn't have.
- Wait a little longer after config to allow for maximum possible necessary
wait.
- Reorganize headers.
- Make conversion_time_max array static to avoid placing it on the stack.
maxim,max1363
- Use __free(kfree) to simplify freeing transmission buffer.
microchip,mcp3964
- Use devm_regulator_get_enable_read_voltage()
microchip,mcp3911
- Use devm_regulator_get_enable_read_voltage()
microchip,mcp4728
- Use devm_regulator_get_enable_read_voltage()
microchip,mcp4922
- Use devm_regulator_get_enable_read_voltage() and devm_* to allow
dropping of explicit remove() callback.
onnn,noa1305
- Various tidy up.
- Provide available scale values.
- Make integration time configurable.
- Fix up integration time look up (/2 error)
ti,dac7311
- Check if spi_setup() succeeded.
ti,tsc2046
- Use __free(kfree) to simplify freeing rx and tx buffers.
- Use devm_regulator_get_enable_read_voltage()

Various minor fixes not called out explicitly.

* tag 'iio-for-6.12a' of ssh://gitolite.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (250 commits)
drivers:iio:Fix the NULL vs IS_ERR() bug for debugfs_create_dir()
iio: sgp40: retain documentation in driver
iio: ABI: remove duplicate in_resistance_calibbias
dt-bindings: iio: st,stm32-adc: add top-level constraints
iio: ABI: add missing calibbias attributes
iio: ABI: add missing calibscale attributes
iio: ABI: sort calibscale attributes
iio: ABI: document calibscale_available attributes
iio: light: ltr390: Calculate 'counts_per_uvi' dynamically
iio: light: ltr390: Add ALS channel and support for gain and resolution
doc: iio: ad4695: document buffered read
iio: adc: ad4695: implement triggered buffer
iio: proximity: hx9023s: Fix error code in hx9023s_property_get()
iio: light: noa1305: Fix up integration time look up
iio: humidity: Add support for ENS210
dt-bindings: iio: humidity: add ENS210 sensor family
iio: imu: adis16460: drop ifdef around CONFIG_DEBUG_FS
iio: imu: adis16400: drop ifdef around CONFIG_DEBUG_FS
iio: imu: adis16480: drop ifdef around CONFIG_DEBUG_FS
iio: imu: adis16475: drop ifdef around CONFIG_DEBUG_FS
...

+14744 -1806
+39
Documentation/ABI/testing/debugfs-iio-ad9467
··· 1 + What: /sys/kernel/debug/iio/iio:deviceX/calibration_table_dump 2 + KernelVersion: 6.11 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + This dumps the calibration table that was filled during the 6 + digital interface tuning process. 7 + 8 + What: /sys/kernel/debug/iio/iio:deviceX/in_voltage_test_mode_available 9 + KernelVersion: 6.11 10 + Contact: linux-iio@vger.kernel.org 11 + Description: 12 + List all the available test tones: 13 + - off 14 + - midscale_short 15 + - pos_fullscale 16 + - neg_fullscale 17 + - checkerboard 18 + - prbs23 19 + - prbs9 20 + - one_zero_toggle 21 + - user 22 + - bit_toggle 23 + - sync 24 + - one_bit_high 25 + - mixed_bit_frequency 26 + - ramp 27 + 28 + Note that depending on the actual device being used, some of the 29 + above might not be available (and they won't be listed when 30 + reading the file). 31 + 32 + What: /sys/kernel/debug/iio/iio:deviceX/in_voltageY_test_mode 33 + KernelVersion: 6.11 34 + Contact: linux-iio@vger.kernel.org 35 + Description: 36 + Writing to this file will initiate one of available test tone on 37 + channel Y. Reading it, shows which test is running. In cases 38 + where an IIO backend is available and supports the test tone, 39 + additional information about the data correctness is given.
+20
Documentation/ABI/testing/debugfs-iio-backend
··· 1 + What: /sys/kernel/debug/iio/iio:deviceX/backendY/name 2 + KernelVersion: 6.11 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + Name of Backend Y connected to device X. 6 + 7 + What: /sys/kernel/debug/iio/iio:deviceX/backendY/direct_reg_access 8 + KernelVersion: 6.11 9 + Contact: linux-iio@vger.kernel.org 10 + Description: 11 + Directly access the registers of backend Y. Typical usage is: 12 + 13 + Reading address 0x50 14 + echo 0x50 > direct_reg_access 15 + cat direct_reg_access 16 + 17 + Writing address 0x50 18 + echo 0x50 0x3 > direct_reg_access 19 + //readback address 0x50 20 + cat direct_reg_access
+61 -14
Documentation/ABI/testing/sysfs-bus-iio
··· 523 523 What: /sys/bus/iio/devices/iio:deviceX/in_accel_x_calibbias 524 524 What: /sys/bus/iio/devices/iio:deviceX/in_accel_y_calibbias 525 525 What: /sys/bus/iio/devices/iio:deviceX/in_accel_z_calibbias 526 + What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_i_calibbias 527 + What: /sys/bus/iio/devices/iio:deviceX/in_altvoltageY_q_calibbias 526 528 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_x_calibbias 527 529 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_y_calibbias 528 530 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_z_calibbias 531 + What: /sys/bus/iio/devices/iio:deviceX/in_capacitance_calibbias 532 + What: /sys/bus/iio/devices/iio:deviceX/in_illuminance_calibbias 529 533 What: /sys/bus/iio/devices/iio:deviceX/in_illuminance0_calibbias 530 - What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calibbias 531 - What: /sys/bus/iio/devices/iio:deviceX/in_pressureY_calibbias 534 + What: /sys/bus/iio/devices/iio:deviceX/in_intensityY_calibbias 535 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_x_calibbias 536 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_y_calibbias 537 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_z_calibbias 532 538 What: /sys/bus/iio/devices/iio:deviceX/in_pressure_calibbias 539 + What: /sys/bus/iio/devices/iio:deviceX/in_pressureY_calibbias 540 + What: /sys/bus/iio/devices/iio:deviceX/in_proximity_calibbias 541 + What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calibbias 542 + What: /sys/bus/iio/devices/iio:deviceX/in_resistance_calibbias 543 + What: /sys/bus/iio/devices/iio:deviceX/in_temp_calibbias 544 + What: /sys/bus/iio/devices/iio:deviceX/out_currentY_calibbias 545 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_calibbias 533 546 KernelVersion: 2.6.35 534 547 Contact: linux-iio@vger.kernel.org 535 548 Description: ··· 554 541 555 542 What: /sys/bus/iio/devices/iio:deviceX/in_accel_calibbias_available 556 543 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_calibbias_available 544 + What: /sys/bus/iio/devices/iio:deviceX/in_temp_calibbias_available 545 + What: /sys/bus/iio/devices/iio:deviceX/in_proximity_calibbias_available 546 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_calibbias_available 557 547 KernelVersion: 5.8 558 548 Contact: linux-iio@vger.kernel.org 559 549 Description: ··· 565 549 - a small discrete set of values like "0 2 4 6 8" 566 550 - a range specified as "[min step max]" 567 551 568 - What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_calibscale 569 - What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_supply_calibscale 570 - What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_calibscale 571 - What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_calibscale 572 - What: /sys/bus/iio/devices/iio:deviceX/in_voltage_i_calibscale 573 - What: /sys/bus/iio/devices/iio:deviceX/in_voltage_q_calibscale 574 - What: /sys/bus/iio/devices/iio:deviceX/in_altvoltage_calibscale 575 - What: /sys/bus/iio/devices/iio:deviceX/in_voltage_calibscale 576 552 What: /sys/bus/iio/devices/iio:deviceX/in_accel_x_calibscale 577 553 What: /sys/bus/iio/devices/iio:deviceX/in_accel_y_calibscale 578 554 What: /sys/bus/iio/devices/iio:deviceX/in_accel_z_calibscale 555 + What: /sys/bus/iio/devices/iio:deviceX/in_altvoltage_calibscale 579 556 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_x_calibscale 580 557 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_y_calibscale 581 558 What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_z_calibscale 582 - What: /sys/bus/iio/devices/iio:deviceX/in_illuminance0_calibscale 583 - What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calibscale 584 - What: /sys/bus/iio/devices/iio:deviceX/in_pressureY_calibscale 585 - What: /sys/bus/iio/devices/iio:deviceX/in_pressure_calibscale 559 + What: /sys/bus/iio/devices/iio:deviceX/in_capacitance_calibscale 586 560 What: /sys/bus/iio/devices/iio:deviceX/in_illuminance_calibscale 561 + What: /sys/bus/iio/devices/iio:deviceX/in_illuminance0_calibscale 562 + What: /sys/bus/iio/devices/iio:deviceX/in_intensity_both_calibscale 563 + What: /sys/bus/iio/devices/iio:deviceX/in_intensity_calibscale 564 + What: /sys/bus/iio/devices/iio:deviceX/in_intensity_ir_calibscale 565 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_x_calibscale 566 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_y_calibscale 567 + What: /sys/bus/iio/devices/iio:deviceX/in_magn_z_calibscale 568 + What: /sys/bus/iio/devices/iio:deviceX/in_pressure_calibscale 569 + What: /sys/bus/iio/devices/iio:deviceX/in_pressureY_calibscale 570 + What: /sys/bus/iio/devices/iio:deviceX/in_proximity0_calibscale 571 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_calibscale 572 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_i_calibscale 573 + What: /sys/bus/iio/devices/iio:deviceX/in_voltage_q_calibscale 574 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_calibscale 575 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_i_calibscale 576 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_q_calibscale 577 + What: /sys/bus/iio/devices/iio:deviceX/in_voltageY_supply_calibscale 578 + What: /sys/bus/iio/devices/iio:deviceX/out_currentY_calibscale 579 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_calibscale 587 580 KernelVersion: 2.6.35 588 581 Contact: linux-iio@vger.kernel.org 589 582 Description: 590 583 Hardware applied calibration scale factor (assumed to fix 591 584 production inaccuracies). If shared across all channels, 592 585 <type>_calibscale is used. 586 + 587 + What: /sys/bus/iio/devices/iio:deviceX/in_illuminanceY_calibscale_available 588 + What: /sys/bus/iio/devices/iio:deviceX/in_intensityY_calibscale_available 589 + What: /sys/bus/iio/devices/iio:deviceX/in_proximityY_calibscale_available 590 + KernelVersion: 4.8 591 + Contact: linux-iio@vger.kernel.org 592 + Description: 593 + Available values of calibscale. Maybe expressed as either of: 594 + 595 + - a small discrete set of values like "1 8 16" 596 + - a range specified as "[min step max]" 597 + 598 + If shared across all channels, <type>_calibscale_available is used. 593 599 594 600 What: /sys/bus/iio/devices/iio:deviceX/in_activity_calibgender 595 601 What: /sys/bus/iio/devices/iio:deviceX/in_energy_calibgender ··· 746 708 2.5kohm_to_gnd: connected to ground via a 2.5kOhm resistor, 747 709 6kohm_to_gnd: connected to ground via a 6kOhm resistor, 748 710 20kohm_to_gnd: connected to ground via a 20kOhm resistor, 711 + 42kohm_to_gnd: connected to ground via a 42kOhm resistor, 749 712 90kohm_to_gnd: connected to ground via a 90kOhm resistor, 750 713 100kohm_to_gnd: connected to ground via an 100kOhm resistor, 751 714 125kohm_to_gnd: connected to ground via an 125kOhm resistor, ··· 2328 2289 Contact: linux-iio@vger.kernel.org 2329 2290 Description: 2330 2291 List of available timeout value for tap gesture confirmation. 2292 + 2293 + What: /sys/.../iio:deviceX/in_shunt_resistor 2294 + What: /sys/.../iio:deviceX/in_current_shunt_resistor 2295 + What: /sys/.../iio:deviceX/in_power_shunt_resistor 2296 + KernelVersion: 6.10 2297 + Contact: linux-iio@vger.kernel.org 2298 + Description: 2299 + The value of current sense resistor in Ohms.
-17
Documentation/ABI/testing/sysfs-bus-iio-adc-max9611
··· 1 - What: /sys/bus/iio/devices/iio:deviceX/in_power_shunt_resistor 2 - Date: March 2017 3 - KernelVersion: 4.12 4 - Contact: linux-iio@vger.kernel.org 5 - Description: The value of the shunt resistor used to compute power drain on 6 - common input voltage pin (RS+). In Ohms. 7 - 8 - What: /sys/bus/iio/devices/iio:deviceX/in_current_shunt_resistor 9 - Date: March 2017 10 - KernelVersion: 4.12 11 - Contact: linux-iio@vger.kernel.org 12 - Description: The value of the shunt resistor used to compute current flowing 13 - between RS+ and RS- voltage sense inputs. In Ohms. 14 - 15 - These attributes describe a single physical component, exposed as two distinct 16 - attributes as it is used to calculate two different values: power load and 17 - current flowing between RS+ and RS- inputs.
-14
Documentation/ABI/testing/sysfs-bus-iio-chemical-sgp40
··· 15 15 Set the relative humidity. This value is sent to the sensor for 16 16 humidity compensation. 17 17 Default value: 50000 (50 % relative humidity) 18 - 19 - What: /sys/bus/iio/devices/iio:deviceX/in_resistance_calibbias 20 - Date: August 2021 21 - KernelVersion: 5.15 22 - Contact: Andreas Klinger <ak@it-klinger.de> 23 - Description: 24 - Set the bias value for the resistance which is used for 25 - calculation of in_concentration_input as follows: 26 - 27 - x = (in_resistance_raw - in_resistance_calibbias) * 0.65 28 - 29 - in_concentration_input = 500 / (1 + e^x) 30 - 31 - Default value: 30000
+61
Documentation/ABI/testing/sysfs-bus-iio-dac
··· 1 + What: /sys/bus/iio/devices/iio:deviceX/out_currentY_toggle_en 2 + KernelVersion: 5.18 3 + Contact: linux-iio@vger.kernel.org 4 + Description: 5 + Toggle enable. Write 1 to enable toggle or 0 to disable it. This 6 + is useful when one wants to change the DAC output codes. For 7 + autonomous toggling, the way it should be done is: 8 + 9 + - disable toggle operation; 10 + - change out_currentY_rawN, where N is the integer value of the symbol; 11 + - enable toggle operation. 12 + 13 + What: /sys/bus/iio/devices/iio:deviceX/out_currentY_rawN 14 + KernelVersion: 5.18 15 + Contact: linux-iio@vger.kernel.org 16 + Description: 17 + This attribute has the same meaning as out_currentY_raw. It is 18 + specific to toggle enabled channels and refers to the DAC output 19 + code in INPUT_N (_rawN), where N is the integer value of the symbol. 20 + The same scale and offset as in out_currentY_raw applies. 21 + 22 + What: /sys/bus/iio/devices/iio:deviceX/out_currentY_symbol 23 + KernelVersion: 5.18 24 + Contact: linux-iio@vger.kernel.org 25 + Description: 26 + Performs a SW switch to a predefined output symbol. This attribute 27 + is specific to toggle enabled channels and allows switching between 28 + multiple predefined symbols. Each symbol corresponds to a different 29 + output, denoted as out_currentY_rawN, where N is the integer value 30 + of the symbol. Writing an integer value N will select out_currentY_rawN. 31 + 32 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_toggle_en 33 + KernelVersion: 5.18 34 + Contact: linux-iio@vger.kernel.org 35 + Description: 36 + Toggle enable. Write 1 to enable toggle or 0 to disable it. This 37 + is useful when one wants to change the DAC output codes. For 38 + autonomous toggling, the way it should be done is: 39 + 40 + - disable toggle operation; 41 + - change out_voltageY_rawN, where N is the integer value of the symbol; 42 + - enable toggle operation. 43 + 44 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_rawN 45 + KernelVersion: 5.18 46 + Contact: linux-iio@vger.kernel.org 47 + Description: 48 + This attribute has the same meaning as out_currentY_raw. It is 49 + specific to toggle enabled channels and refers to the DAC output 50 + code in INPUT_N (_rawN), where N is the integer value of the symbol. 51 + The same scale and offset as in out_currentY_raw applies. 52 + 53 + What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_symbol 54 + KernelVersion: 5.18 55 + Contact: linux-iio@vger.kernel.org 56 + Description: 57 + Performs a SW switch to a predefined output symbol. This attribute 58 + is specific to toggle enabled channels and allows switching between 59 + multiple predefined symbols. Each symbol corresponds to a different 60 + output, denoted as out_voltageY_rawN, where N is the integer value 61 + of the symbol. Writing an integer value N will select out_voltageY_rawN.
-31
Documentation/ABI/testing/sysfs-bus-iio-dac-ltc2688
··· 53 53 Contact: linux-iio@vger.kernel.org 54 54 Description: 55 55 Returns the available values for the dither phase. 56 - 57 - What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_toggle_en 58 - KernelVersion: 5.18 59 - Contact: linux-iio@vger.kernel.org 60 - Description: 61 - Toggle enable. Write 1 to enable toggle or 0 to disable it. This is 62 - useful when one wants to change the DAC output codes. The way it should 63 - be done is: 64 - 65 - - disable toggle operation; 66 - - change out_voltageY_raw0 and out_voltageY_raw1; 67 - - enable toggle operation. 68 - 69 - What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_raw0 70 - What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_raw1 71 - KernelVersion: 5.18 72 - Contact: linux-iio@vger.kernel.org 73 - Description: 74 - It has the same meaning as out_voltageY_raw. This attribute is 75 - specific to toggle enabled channels and refers to the DAC output 76 - code in INPUT_A (_raw0) and INPUT_B (_raw1). The same scale and offset 77 - as in out_voltageY_raw applies. 78 - 79 - What: /sys/bus/iio/devices/iio:deviceX/out_voltageY_symbol 80 - KernelVersion: 5.18 81 - Contact: linux-iio@vger.kernel.org 82 - Description: 83 - Performs a SW toggle. This attribute is specific to toggle 84 - enabled channels and allows to toggle between out_voltageY_raw0 85 - and out_voltageY_raw1 through software. Writing 0 will select 86 - out_voltageY_raw0 while 1 selects out_voltageY_raw1.
+1 -1
Documentation/ABI/testing/sysfs-bus-iio-filter-admv8818
··· 3 3 Contact: linux-iio@vger.kernel.org 4 4 Description: 5 5 Reading this returns the valid values that can be written to the 6 - on_altvoltage0_mode attribute: 6 + filter_mode attribute: 7 7 8 8 - auto -> Adjust bandpass filter to track changes in input clock rate. 9 9 - manual -> disable/unregister the clock rate notifier / input clock tracking.
-9
Documentation/ABI/testing/sysfs-bus-iio-ina2xx-adc
··· 13 13 available for reading data. However, samples can be occasionally skipped 14 14 or repeated, depending on the beat between the capture and conversion 15 15 rates. 16 - 17 - What: /sys/bus/iio/devices/iio:deviceX/in_shunt_resistor 18 - Date: December 2015 19 - KernelVersion: 4.4 20 - Contact: linux-iio@vger.kernel.org 21 - Description: 22 - The value of the shunt resistor may be known only at runtime fom an 23 - eeprom content read by a client application. This attribute allows to 24 - set its value in ohms.
+92
Documentation/devicetree/bindings/iio/accel/adi,adxl380.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/accel/adi,adxl380.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices ADXL380/382 3-Axis Digital Accelerometer 8 + 9 + maintainers: 10 + - Ramona Gradinariu <ramona.gradinariu@analog.com> 11 + - Antoniu Miclaus <antoniu.miclaus@analog.com> 12 + 13 + description: | 14 + The ADXL380/ADXL382 is a low noise density, low power, 3-axis 15 + accelerometer with selectable measurement ranges. The ADXL380 16 + supports the ±4 g, ±8 g, and ±16 g ranges, and the ADXL382 supports 17 + ±15 g, ±30 g, and ±60 g ranges. 18 + 19 + https://www.analog.com/en/products/adxl380.html 20 + 21 + properties: 22 + compatible: 23 + enum: 24 + - adi,adxl380 25 + - adi,adxl382 26 + 27 + reg: 28 + maxItems: 1 29 + 30 + interrupts: 31 + minItems: 1 32 + maxItems: 2 33 + 34 + interrupt-names: 35 + minItems: 1 36 + items: 37 + - enum: [INT0, INT1] 38 + - const: INT1 39 + 40 + vddio-supply: true 41 + 42 + vsupply-supply: true 43 + 44 + required: 45 + - compatible 46 + - reg 47 + - interrupts 48 + - interrupt-names 49 + - vddio-supply 50 + - vsupply-supply 51 + 52 + allOf: 53 + - $ref: /schemas/spi/spi-peripheral-props.yaml# 54 + 55 + unevaluatedProperties: false 56 + 57 + examples: 58 + - | 59 + #include <dt-bindings/interrupt-controller/irq.h> 60 + 61 + i2c { 62 + #address-cells = <1>; 63 + #size-cells = <0>; 64 + 65 + accelerometer@54 { 66 + compatible = "adi,adxl380"; 67 + reg = <0x54>; 68 + vddio-supply = <&vddio>; 69 + vsupply-supply = <&vsupply>; 70 + interrupt-parent = <&gpio>; 71 + interrupts = <25 IRQ_TYPE_LEVEL_HIGH>; 72 + interrupt-names = "INT0"; 73 + }; 74 + }; 75 + - | 76 + #include <dt-bindings/interrupt-controller/irq.h> 77 + 78 + spi { 79 + #address-cells = <1>; 80 + #size-cells = <0>; 81 + 82 + accelerometer@0 { 83 + compatible = "adi,adxl380"; 84 + reg = <0>; 85 + spi-max-frequency = <8000000>; 86 + vddio-supply = <&vddio>; 87 + vsupply-supply = <&vsupply>; 88 + interrupt-parent = <&gpio>; 89 + interrupts = <25 IRQ_TYPE_LEVEL_HIGH>; 90 + interrupt-names = "INT0"; 91 + }; 92 + };
+1
Documentation/devicetree/bindings/iio/accel/kionix,kxcjk1013.yaml
··· 16 16 - kionix,kxcj91008 17 17 - kionix,kxtj21009 18 18 - kionix,kxtf9 19 + - kionix,kx022-1020 19 20 - kionix,kx023-1025 20 21 21 22 reg:
+197
Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/adi,ad4000.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices AD4000 and similar Analog to Digital Converters 8 + 9 + maintainers: 10 + - Marcelo Schmitt <marcelo.schmitt@analog.com> 11 + 12 + description: | 13 + Analog Devices AD4000 family of Analog to Digital Converters with SPI support. 14 + Specifications can be found at: 15 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4000-4004-4008.pdf 16 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4001-4005.pdf 17 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4002-4006-4010.pdf 18 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4003-4007-4011.pdf 19 + https://www.analog.com/media/en/technical-documentation/data-sheets/ad4020-4021-4022.pdf 20 + https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4001.pdf 21 + https://www.analog.com/media/en/technical-documentation/data-sheets/adaq4003.pdf 22 + 23 + $ref: /schemas/spi/spi-peripheral-props.yaml# 24 + 25 + properties: 26 + compatible: 27 + oneOf: 28 + - const: adi,ad4000 29 + - items: 30 + - enum: 31 + - adi,ad4004 32 + - adi,ad4008 33 + - const: adi,ad4000 34 + 35 + - const: adi,ad4001 36 + - items: 37 + - enum: 38 + - adi,ad4005 39 + - const: adi,ad4001 40 + 41 + - const: adi,ad4002 42 + - items: 43 + - enum: 44 + - adi,ad4006 45 + - adi,ad4010 46 + - const: adi,ad4002 47 + 48 + - const: adi,ad4003 49 + - items: 50 + - enum: 51 + - adi,ad4007 52 + - adi,ad4011 53 + - const: adi,ad4003 54 + 55 + - const: adi,ad4020 56 + - items: 57 + - enum: 58 + - adi,ad4021 59 + - adi,ad4022 60 + - const: adi,ad4020 61 + 62 + - const: adi,adaq4001 63 + 64 + - const: adi,adaq4003 65 + 66 + reg: 67 + maxItems: 1 68 + 69 + spi-max-frequency: 70 + maximum: 102040816 # for VIO > 2.7 V, 81300813 for VIO > 1.7 V 71 + 72 + adi,sdi-pin: 73 + $ref: /schemas/types.yaml#/definitions/string 74 + enum: [ high, low, cs, sdi ] 75 + default: sdi 76 + description: 77 + Describes how the ADC SDI pin is wired. A value of "sdi" indicates that 78 + the ADC SDI is connected to host SDO. "high" indicates that the ADC SDI 79 + pin is hard-wired to logic high (VIO). "low" indicates that it is 80 + hard-wired low (GND). "cs" indicates that the ADC SDI pin is connected to 81 + the host CS line. 82 + 83 + '#daisy-chained-devices': true 84 + 85 + vdd-supply: 86 + description: A 1.8V supply that powers the chip (VDD). 87 + 88 + vio-supply: 89 + description: 90 + A 1.8V to 5.5V supply for the digital inputs and outputs (VIO). 91 + 92 + ref-supply: 93 + description: 94 + A 2.5 to 5V supply for the external reference voltage (REF). 95 + 96 + cnv-gpios: 97 + description: 98 + When provided, this property indicates the GPIO that is connected to the 99 + CNV pin. 100 + maxItems: 1 101 + 102 + adi,high-z-input: 103 + type: boolean 104 + description: 105 + High-Z mode allows the amplifier and RC filter in front of the ADC to be 106 + chosen based on the signal bandwidth of interest, rather than the settling 107 + requirements of the switched capacitor SAR ADC inputs. 108 + 109 + adi,gain-milli: 110 + description: | 111 + The hardware gain applied to the ADC input (in milli units). 112 + The gain provided by the ADC input scaler is defined by the hardware 113 + connections between chip pins OUT+, R1K-, R1K1-, R1K+, R1K1+, and OUT-. 114 + If not present, default to 1000 (no actual gain applied). 115 + $ref: /schemas/types.yaml#/definitions/uint16 116 + enum: [454, 909, 1000, 1900] 117 + default: 1000 118 + 119 + interrupts: 120 + description: 121 + The SDO pin can also function as a busy indicator. This node should be 122 + connected to an interrupt that is triggered when the SDO line goes low 123 + while the SDI line is high and the CNV line is low ("3-wire" mode) or the 124 + SDI line is low and the CNV line is high ("4-wire" mode); or when the SDO 125 + line goes high while the SDI and CNV lines are high (chain mode), 126 + maxItems: 1 127 + 128 + required: 129 + - compatible 130 + - reg 131 + - vdd-supply 132 + - vio-supply 133 + - ref-supply 134 + 135 + allOf: 136 + # The configuration register can only be accessed if SDI is connected to MOSI 137 + - if: 138 + required: 139 + - adi,sdi-pin 140 + then: 141 + properties: 142 + adi,high-z-input: false 143 + # chain mode has lower SCLK max rate 144 + - if: 145 + required: 146 + - '#daisy-chained-devices' 147 + then: 148 + properties: 149 + spi-max-frequency: 150 + maximum: 50000000 # for VIO > 2.7 V, 40000000 for VIO > 1.7 V 151 + # Gain property only applies to ADAQ devices 152 + - if: 153 + properties: 154 + compatible: 155 + not: 156 + contains: 157 + enum: 158 + - adi,adaq4001 159 + - adi,adaq4003 160 + then: 161 + properties: 162 + adi,gain-milli: false 163 + 164 + unevaluatedProperties: false 165 + 166 + examples: 167 + - | 168 + #include <dt-bindings/gpio/gpio.h> 169 + spi { 170 + #address-cells = <1>; 171 + #size-cells = <0>; 172 + adc@0 { 173 + compatible = "adi,ad4020"; 174 + reg = <0>; 175 + spi-max-frequency = <71000000>; 176 + vdd-supply = <&supply_1_8V>; 177 + vio-supply = <&supply_1_8V>; 178 + ref-supply = <&supply_5V>; 179 + adi,sdi-pin = "cs"; 180 + cnv-gpios = <&gpio0 88 GPIO_ACTIVE_HIGH>; 181 + }; 182 + }; 183 + - | 184 + spi { 185 + #address-cells = <1>; 186 + #size-cells = <0>; 187 + adc@0 { 188 + compatible = "adi,adaq4003"; 189 + reg = <0>; 190 + spi-max-frequency = <80000000>; 191 + vdd-supply = <&supply_1_8V>; 192 + vio-supply = <&supply_1_8V>; 193 + ref-supply = <&supply_5V>; 194 + adi,high-z-input; 195 + adi,gain-milli = /bits/ 16 <454>; 196 + }; 197 + };
+254
Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/adi,ad4695.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices Easy Drive Multiplexed SAR Analog to Digital Converters 8 + 9 + maintainers: 10 + - Michael Hennerich <Michael.Hennerich@analog.com> 11 + - Nuno Sá <nuno.sa@analog.com> 12 + 13 + description: | 14 + A family of similar multi-channel analog to digital converters with SPI bus. 15 + 16 + * https://www.analog.com/en/products/ad4695.html 17 + * https://www.analog.com/en/products/ad4696.html 18 + * https://www.analog.com/en/products/ad4697.html 19 + * https://www.analog.com/en/products/ad4698.html 20 + 21 + $ref: /schemas/spi/spi-peripheral-props.yaml# 22 + 23 + properties: 24 + compatible: 25 + enum: 26 + - adi,ad4695 27 + - adi,ad4696 28 + - adi,ad4697 29 + - adi,ad4698 30 + 31 + reg: 32 + maxItems: 1 33 + 34 + spi-max-frequency: 35 + maximum: 80000000 36 + 37 + spi-cpol: true 38 + spi-cpha: true 39 + 40 + spi-rx-bus-width: 41 + minimum: 1 42 + maximum: 4 43 + 44 + avdd-supply: 45 + description: Analog power supply. 46 + 47 + vio-supply: 48 + description: I/O pin power supply. 49 + 50 + ldo-in-supply: 51 + description: Internal LDO Input. Mutually exclusive with vdd-supply. 52 + 53 + vdd-supply: 54 + description: Core power supply. Mutually exclusive with ldo-in-supply. 55 + 56 + ref-supply: 57 + description: 58 + External reference voltage. Mutually exclusive with refin-supply. 59 + 60 + refin-supply: 61 + description: 62 + Internal reference buffer input. Mutually exclusive with ref-supply. 63 + 64 + com-supply: 65 + description: Common voltage supply for pseudo-differential analog inputs. 66 + 67 + adi,no-ref-current-limit: 68 + $ref: /schemas/types.yaml#/definitions/flag 69 + description: 70 + When this flag is present, the REF Overvoltage Reduced Current protection 71 + is disabled. 72 + 73 + adi,no-ref-high-z: 74 + $ref: /schemas/types.yaml#/definitions/flag 75 + description: 76 + Enable this flag if the ref-supply requires Reference Input High-Z Mode 77 + to be disabled for proper operation. 78 + 79 + cnv-gpios: 80 + description: The Convert Input (CNV). If omitted, CNV is tied to SPI CS. 81 + maxItems: 1 82 + 83 + reset-gpios: 84 + description: The Reset Input (RESET). Should be configured GPIO_ACTIVE_LOW. 85 + maxItems: 1 86 + 87 + interrupts: 88 + minItems: 1 89 + items: 90 + - description: Signal coming from the BSY_ALT_GP0 pin (ALERT or BUSY). 91 + - description: Signal coming from the GP2 pin (ALERT). 92 + - description: Signal coming from the GP3 pin (BUSY). 93 + 94 + interrupt-names: 95 + minItems: 1 96 + items: 97 + - const: gp0 98 + - const: gp2 99 + - const: gp3 100 + 101 + gpio-controller: true 102 + 103 + "#gpio-cells": 104 + const: 2 105 + description: | 106 + The first cell is the GPn number: 0 to 3. 107 + The second cell takes standard GPIO flags. 108 + 109 + "#address-cells": 110 + const: 1 111 + 112 + "#size-cells": 113 + const: 0 114 + 115 + patternProperties: 116 + "^in(?:[13579]|1[135])-supply$": 117 + description: 118 + Optional voltage supply for odd numbered channels when they are used as 119 + the negative input for a pseudo-differential channel. 120 + 121 + "^channel@[0-9a-f]$": 122 + type: object 123 + $ref: adc.yaml 124 + unevaluatedProperties: false 125 + description: 126 + Describes each individual channel. In addition the properties defined 127 + below, bipolar from adc.yaml is also supported. 128 + 129 + properties: 130 + reg: 131 + maximum: 15 132 + 133 + common-mode-channel: 134 + description: 135 + Describes the common mode channel for single channels. 0xFF is REFGND 136 + and OxFE is COM. Macros are available for these values in 137 + dt-bindings/iio/adi,ad4695.h. Values 1 to 15 correspond to INx inputs. 138 + Only odd numbered INx inputs can be used as common mode channels. 139 + enum: [1, 3, 5, 7, 9, 11, 13, 15, 0xFE, 0xFF] 140 + default: 0xFF 141 + 142 + adi,no-high-z: 143 + $ref: /schemas/types.yaml#/definitions/flag 144 + description: 145 + Enable this flag if the input pin requires the Analog Input High-Z 146 + Mode to be disabled for proper operation. 147 + 148 + required: 149 + - reg 150 + 151 + allOf: 152 + # bipolar mode can't be used with REFGND 153 + - if: 154 + properties: 155 + common-mode-channel: 156 + const: 0xFF 157 + then: 158 + properties: 159 + bipolar: false 160 + 161 + required: 162 + - compatible 163 + - reg 164 + - avdd-supply 165 + - vio-supply 166 + 167 + allOf: 168 + - oneOf: 169 + - required: 170 + - ldo-in-supply 171 + - required: 172 + - vdd-supply 173 + 174 + - oneOf: 175 + - required: 176 + - ref-supply 177 + - required: 178 + - refin-supply 179 + 180 + # the internal reference buffer always requires high-z mode 181 + - if: 182 + required: 183 + - refin-supply 184 + then: 185 + properties: 186 + adi,no-ref-high-z: false 187 + 188 + # limit channels for 8-channel chips 189 + - if: 190 + properties: 191 + compatible: 192 + contains: 193 + enum: 194 + - adi,ad4697 195 + - adi,ad4698 196 + then: 197 + patternProperties: 198 + "^in(?:9|1[135])-supply$": false 199 + "^channel@[0-7]$": 200 + properties: 201 + reg: 202 + maximum: 7 203 + common-mode-channel: 204 + enum: [1, 3, 5, 7, 0xFE, 0xFF] 205 + "^channel@[8-9a-f]$": false 206 + 207 + unevaluatedProperties: false 208 + 209 + examples: 210 + - | 211 + #include <dt-bindings/gpio/gpio.h> 212 + #include <dt-bindings/iio/adi,ad4695.h> 213 + 214 + spi { 215 + #address-cells = <1>; 216 + #size-cells = <0>; 217 + 218 + adc@0 { 219 + compatible = "adi,ad4695"; 220 + reg = <0>; 221 + spi-cpol; 222 + spi-cpha; 223 + spi-max-frequency = <80000000>; 224 + avdd-supply = <&power_supply>; 225 + ldo-in-supply = <&power_supply>; 226 + vio-supply = <&io_supply>; 227 + refin-supply = <&supply_5V>; 228 + com-supply = <&supply_2V5>; 229 + in3-supply = <&supply_2V5>; 230 + reset-gpios = <&gpio 1 GPIO_ACTIVE_LOW>; 231 + 232 + #address-cells = <1>; 233 + #size-cells = <0>; 234 + 235 + /* Pseudo-differential channel between IN0 and REFGND. */ 236 + channel@0 { 237 + reg = <0>; 238 + }; 239 + 240 + /* Pseudo-differential channel between IN1 and COM. */ 241 + channel@1 { 242 + reg = <1>; 243 + common-mode-channel = <AD4695_COMMON_MODE_COM>; 244 + bipolar; 245 + }; 246 + 247 + /* Pseudo-differential channel between IN2 and IN3. */ 248 + channel@2 { 249 + reg = <2>; 250 + common-mode-channel = <3>; 251 + bipolar; 252 + }; 253 + }; 254 + };
+26 -7
Documentation/devicetree/bindings/iio/adc/adi,ad7192.yaml
··· 39 39 40 40 clocks: 41 41 maxItems: 1 42 - description: phandle to the master clock (mclk) 42 + description: 43 + Optionally, either a crystal can be attached externally between MCLK1 and 44 + MCLK2 pins, or an external CMOS-compatible clock can drive the MCLK2 45 + pin. If absent, internal 4.92MHz clock is used, which can be made 46 + available on MCLK2 pin. 43 47 44 48 clock-names: 45 - items: 46 - - const: mclk 49 + enum: 50 + - xtal 51 + - mclk 52 + 53 + "#clock-cells": 54 + const: 0 55 + description: 56 + If present when internal clock is used, configured as clock provider. 47 57 48 58 interrupts: 49 59 maxItems: 1 ··· 144 134 required: 145 135 - compatible 146 136 - reg 147 - - clocks 148 - - clock-names 149 137 - interrupts 150 138 - dvdd-supply 151 139 - avdd-supply ··· 164 156 then: 165 157 patternProperties: 166 158 "^channel@[0-9a-f]+$": false 159 + - if: 160 + anyOf: 161 + - required: 162 + - clocks 163 + - required: 164 + - clock-names 165 + then: 166 + properties: 167 + "#clock-cells": false 168 + required: 169 + - clocks 170 + - clock-names 167 171 168 172 unevaluatedProperties: false 169 173 ··· 221 201 spi-max-frequency = <1000000>; 222 202 spi-cpol; 223 203 spi-cpha; 224 - clocks = <&ad7192_mclk>; 225 - clock-names = "mclk"; 204 + #clock-cells = <0>; 226 205 interrupts = <25 0x2>; 227 206 interrupt-parent = <&gpio>; 228 207 aincom-supply = <&aincom>;
+13
Documentation/devicetree/bindings/iio/adc/adi,ad7380.yaml
··· 15 15 * https://www.analog.com/en/products/ad7381.html 16 16 * https://www.analog.com/en/products/ad7383.html 17 17 * https://www.analog.com/en/products/ad7384.html 18 + * https://www.analog.com/en/products/ad7386.html 19 + * https://www.analog.com/en/products/ad7387.html 20 + * https://www.analog.com/en/products/ad7388.html 18 21 * https://www.analog.com/en/products/ad7380-4.html 19 22 * https://www.analog.com/en/products/ad7381-4.html 20 23 * https://www.analog.com/en/products/ad7383-4.html 21 24 * https://www.analog.com/en/products/ad7384-4.html 25 + * https://www.analog.com/en/products/ad7386-4.html 26 + * https://www.analog.com/en/products/ad7387-4.html 27 + * https://www.analog.com/en/products/ad7388-4.html 28 + 22 29 23 30 $ref: /schemas/spi/spi-peripheral-props.yaml# 24 31 ··· 36 29 - adi,ad7381 37 30 - adi,ad7383 38 31 - adi,ad7384 32 + - adi,ad7386 33 + - adi,ad7387 34 + - adi,ad7388 39 35 - adi,ad7380-4 40 36 - adi,ad7381-4 41 37 - adi,ad7383-4 42 38 - adi,ad7384-4 39 + - adi,ad7386-4 40 + - adi,ad7387-4 41 + - adi,ad7388-4 43 42 44 43 reg: 45 44 maxItems: 1
+93 -30
Documentation/devicetree/bindings/iio/adc/adi,ad7606.yaml
··· 35 35 36 36 avcc-supply: true 37 37 38 + vdrive-supply: 39 + description: 40 + Determines the voltage level at which the interface logic pins will 41 + operate. 42 + 43 + refin-supply: 44 + description: 45 + The voltage supply for optional external reference voltage. 46 + 38 47 interrupts: 48 + description: 49 + The BUSY pin falling edge indicates that the conversion is over, and thus 50 + new data is available. 39 51 maxItems: 1 40 52 41 53 adi,conversion-start-gpios: 42 54 description: 43 - Must be the device tree identifier of the CONVST pin. 44 - This logic input is used to initiate conversions on the analog 45 - input channels. As the line is active high, it should be marked 46 - GPIO_ACTIVE_HIGH. 47 - maxItems: 1 55 + Must be the device tree identifier of the CONVST pin(s). This logic input 56 + is used to initiate conversions on the analog input channels. As the line 57 + is active high, it should be marked GPIO_ACTIVE_HIGH. 58 + minItems: 1 59 + maxItems: 2 48 60 49 61 reset-gpios: 50 62 description: 51 - Must be the device tree identifier of the RESET pin. If specified, 52 - it will be asserted during driver probe. As the line is active high, 53 - it should be marked GPIO_ACTIVE_HIGH. 63 + Must be the device tree identifier of the RESET pin. If specified, it will 64 + be asserted during driver probe. On the AD7606x, as the line is active 65 + high, it should be marked GPIO_ACTIVE_HIGH. On the AD7616, as the line is 66 + active low, it should be marked GPIO_ACTIVE_LOW. 54 67 maxItems: 1 55 68 56 69 standby-gpios: 57 70 description: 58 - Must be the device tree identifier of the STBY pin. This pin is used 59 - to place the AD7606 into one of two power-down modes, Standby mode or 71 + Must be the device tree identifier of the STBY pin. This pin is used to 72 + place the AD7606 into one of two power-down modes, Standby mode or 60 73 Shutdown mode. As the line is active low, it should be marked 61 74 GPIO_ACTIVE_LOW. 62 75 maxItems: 1 63 76 64 77 adi,first-data-gpios: 65 78 description: 66 - Must be the device tree identifier of the FRSTDATA pin. 67 - The FRSTDATA output indicates when the first channel, V1, is 68 - being read back on either the parallel, byte or serial interface. 69 - As the line is active high, it should be marked GPIO_ACTIVE_HIGH. 79 + Must be the device tree identifier of the FRSTDATA pin. The FRSTDATA 80 + output indicates when the first channel, V1, is being read back on either 81 + the parallel, byte or serial interface. As the line is active high, it 82 + should be marked GPIO_ACTIVE_HIGH. 70 83 maxItems: 1 71 84 72 85 adi,range-gpios: 73 86 description: 74 - Must be the device tree identifier of the RANGE pin. The polarity on 75 - this pin determines the input range of the analog input channels. If 76 - this pin is tied to a logic high, the analog input range is ±10V for 77 - all channels. If this pin is tied to a logic low, the analog input range 87 + Must be the device tree identifier of the RANGE pin. The state on this 88 + pin determines the input range of the analog input channels. If this pin 89 + is tied to a logic high, the analog input range is ±10V for all channels. 90 + On the AD760X, if this pin is tied to a logic low, the analog input range 78 91 is ±5V for all channels. As the line is active high, it should be marked 79 - GPIO_ACTIVE_HIGH. 80 - maxItems: 1 92 + GPIO_ACTIVE_HIGH. On the AD7616, there are 2 pins, and if the 2 pins are 93 + tied to a logic high, software mode is enabled, otherwise one of the 3 94 + possible range values is selected. 95 + minItems: 1 96 + maxItems: 2 81 97 82 98 adi,oversampling-ratio-gpios: 83 99 description: 84 - Must be the device tree identifier of the over-sampling 85 - mode pins. As the line is active high, it should be marked 86 - GPIO_ACTIVE_HIGH. 100 + Must be the device tree identifier of the over-sampling mode pins. As the 101 + line is active high, it should be marked GPIO_ACTIVE_HIGH. On the AD7606X 102 + parts that support it, if all 3 pins are tied to a logic high, software 103 + mode is enabled. 87 104 maxItems: 3 88 105 89 106 adi,sw-mode: 90 107 description: 91 - Software mode of operation, so far available only for ad7616 and ad7606b. 92 - It is enabled when all three oversampling mode pins are connected to 93 - high level. The device is configured by the corresponding registers. If the 94 - adi,oversampling-ratio-gpios property is defined, then the driver will set the 95 - oversampling gpios to high. Otherwise, it is assumed that the pins are hardwired 96 - to VDD. 108 + Software mode of operation, so far available only for AD7616 and AD7606B. 109 + It is enabled when all three oversampling mode pins are connected to high 110 + level for the AD7606B, or both the range selection are connected to high 111 + level for the AD7616. The device is configured by the corresponding 112 + registers. If the adi,oversampling-ratio-gpios property is defined, then 113 + the driver will set the oversampling gpios to high. Otherwise, it is 114 + assumed that the pins are hardwired to VDD. 97 115 type: boolean 98 116 99 117 required: ··· 119 101 - reg 120 102 - spi-cpha 121 103 - avcc-supply 104 + - vdrive-supply 122 105 - interrupts 123 106 - adi,conversion-start-gpios 124 107 125 108 allOf: 126 109 - $ref: /schemas/spi/spi-peripheral-props.yaml# 110 + 111 + - if: 112 + properties: 113 + compatible: 114 + contains: 115 + const: adi,ad7616 116 + then: 117 + properties: 118 + adi,first-data-gpios: false 119 + standby-gpios: false 120 + adi,range-gpios: 121 + maxItems: 2 122 + else: 123 + properties: 124 + adi,range-gpios: 125 + maxItems: 1 126 + 127 + - if: 128 + properties: 129 + compatible: 130 + contains: 131 + enum: 132 + - adi,ad7605-4 133 + - adi,ad7616 134 + then: 135 + properties: 136 + adi,oversampling-ratio-gpios: false 137 + 138 + - if: 139 + properties: 140 + compatible: 141 + contains: 142 + enum: 143 + - adi,ad7605-4 144 + - adi,ad7606-4 145 + - adi,ad7606-6 146 + - adi,ad7606-8 147 + then: 148 + properties: 149 + adi,sw-mode: false 150 + else: 151 + properties: 152 + adi,conversion-start-gpios: 153 + maxItems: 1 127 154 128 155 unevaluatedProperties: false 129 156 ··· 188 125 spi-cpha; 189 126 190 127 avcc-supply = <&adc_vref>; 128 + vdrive-supply = <&vdd_supply>; 191 129 192 130 interrupts = <25 IRQ_TYPE_EDGE_FALLING>; 193 131 interrupt-parent = <&gpio>; ··· 200 136 <&gpio 23 GPIO_ACTIVE_HIGH>, 201 137 <&gpio 26 GPIO_ACTIVE_HIGH>; 202 138 standby-gpios = <&gpio 24 GPIO_ACTIVE_LOW>; 203 - adi,sw-mode; 204 139 }; 205 140 }; 206 141 ...
+3
Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml
··· 28 28 - adi,ad9265 29 29 - adi,ad9434 30 30 - adi,ad9467 31 + - adi,ad9643 32 + - adi,ad9649 33 + - adi,ad9652 31 34 32 35 reg: 33 36 maxItems: 1
+71
Documentation/devicetree/bindings/iio/adc/microchip,pac1921.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/adc/microchip,pac1921.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Microchip PAC1921 High-Side Power/Current Monitor with Anaog Output 8 + 9 + maintainers: 10 + - Matteo Martelli <matteomartelli3@gmail.com> 11 + 12 + description: | 13 + The PAC1921 is a power/current monitoring device with an analog output 14 + and I2C/SMBus interface. 15 + 16 + Datasheet can be found here: 17 + https://ww1.microchip.com/downloads/en/DeviceDoc/PAC1921-Data-Sheet-DS20005293E.pdf 18 + 19 + properties: 20 + compatible: 21 + const: microchip,pac1921 22 + 23 + reg: 24 + maxItems: 1 25 + 26 + vdd-supply: true 27 + 28 + "#io-channel-cells": 29 + const: 1 30 + 31 + shunt-resistor-micro-ohms: 32 + description: 33 + Value in micro Ohms of the shunt resistor connected between 34 + the SENSE+ and SENSE- inputs, across which the current is measured. 35 + Value is needed to compute the scaling of the measured current. 36 + 37 + label: 38 + description: Unique name to identify which device this is. 39 + 40 + read-integrate-gpios: 41 + description: 42 + READ/INT input pin to control the current state of the device, either in 43 + the INTEGRATE state when driven high, or in the READ state when driven low. 44 + When not connected the pin is floating and it can be overridden by the 45 + INT_EN register bit after asserting the READ/INT_OVR register bit. 46 + maxItems: 1 47 + 48 + required: 49 + - compatible 50 + - reg 51 + - vdd-supply 52 + - shunt-resistor-micro-ohms 53 + 54 + additionalProperties: false 55 + 56 + examples: 57 + - | 58 + i2c { 59 + #address-cells = <1>; 60 + #size-cells = <0>; 61 + 62 + adc@4c { 63 + compatible = "microchip,pac1921"; 64 + reg = <0x4c>; 65 + vdd-supply = <&vdd>; 66 + #io-channel-cells = <1>; 67 + label = "vbat"; 68 + shunt-resistor-micro-ohms = <10000>; 69 + }; 70 + }; 71 + ...
+3
Documentation/devicetree/bindings/iio/adc/rockchip-saradc.yaml
··· 17 17 - const: rockchip,rk3399-saradc 18 18 - const: rockchip,rk3588-saradc 19 19 - items: 20 + - const: rockchip,rk3576-saradc 21 + - const: rockchip,rk3588-saradc 22 + - items: 20 23 - enum: 21 24 - rockchip,px30-saradc 22 25 - rockchip,rk3308-saradc
+23 -2
Documentation/devicetree/bindings/iio/adc/sigma-delta-modulator.yaml
··· 18 18 - sd-modulator 19 19 - ads1201 20 20 21 + '#io-backend-cells': 22 + const: 0 23 + 21 24 '#io-channel-cells': 22 25 const: 0 23 26 27 + vref-supply: 28 + description: Phandle to the vref input analog reference voltage. 29 + 30 + dependencies: 31 + vref-supply: [ '#io-backend-cells' ] 32 + 24 33 required: 25 34 - compatible 26 - - '#io-channel-cells' 35 + 36 + anyOf: 37 + - required: ['#io-backend-cells'] 38 + - required: ['#io-channel-cells'] 27 39 28 40 additionalProperties: false 29 41 30 42 examples: 31 43 - | 32 - ads1202: adc { 44 + // Backend binding example. SD modulator configured as an IIO backend device 45 + ads1201_0: adc { 46 + compatible = "sd-modulator"; 47 + vref-supply = <&vdd_adc>; 48 + #io-backend-cells = <0>; 49 + }; 50 + 51 + - | 52 + // Legacy binding example. SD modulator configured as an IIO channel provider 53 + ads1201_1: adc { 33 54 compatible = "sd-modulator"; 34 55 #io-channel-cells = <0>; 35 56 };
+3 -1
Documentation/devicetree/bindings/iio/adc/st,stm32-adc.yaml
··· 54 54 It's not present on stm32f4. 55 55 It's required on stm32h7 and stm32mp1. 56 56 57 - clock-names: true 57 + clock-names: 58 + minItems: 1 59 + maxItems: 2 58 60 59 61 st,max-clk-rate-hz: 60 62 description:
+107 -15
Documentation/devicetree/bindings/iio/adc/st,stm32-dfsdm-adc.yaml
··· 102 102 items: 103 103 minimum: 0 104 104 maximum: 7 105 + deprecated: true 105 106 106 107 st,adc-channel-names: 107 108 description: List of single-ended channel names. 109 + deprecated: true 108 110 109 111 st,filter-order: 110 112 description: | ··· 120 118 "#io-channel-cells": 121 119 const: 1 122 120 121 + '#address-cells': 122 + const: 1 123 + 124 + '#size-cells': 125 + const: 0 126 + 123 127 st,adc-channel-types: 124 128 description: | 125 129 Single-ended channel input type. ··· 136 128 items: 137 129 enum: [ SPI_R, SPI_F, MANCH_R, MANCH_F ] 138 130 $ref: /schemas/types.yaml#/definitions/non-unique-string-array 131 + deprecated: true 139 132 140 133 st,adc-channel-clk-src: 141 134 description: | ··· 148 139 items: 149 140 enum: [ CLKIN, CLKOUT, CLKOUT_F, CLKOUT_R ] 150 141 $ref: /schemas/types.yaml#/definitions/non-unique-string-array 142 + deprecated: true 151 143 152 144 st,adc-alt-channel: 153 145 description: ··· 157 147 If not set, channel n is connected to SPI input n. 158 148 If set, channel n is connected to SPI input n + 1. 159 149 type: boolean 150 + deprecated: true 160 151 161 152 st,filter0-sync: 162 153 description: ··· 176 165 - compatible 177 166 - reg 178 167 - interrupts 179 - - st,adc-channels 180 - - st,adc-channel-names 181 168 - st,filter-order 182 169 - "#io-channel-cells" 170 + 171 + patternProperties: 172 + "^channel@[0-7]$": 173 + type: object 174 + $ref: adc.yaml 175 + unevaluatedProperties: false 176 + description: Represents the external channels which are connected to the DFSDM. 177 + 178 + properties: 179 + reg: 180 + maximum: 7 181 + 182 + label: 183 + description: 184 + Unique name to identify which channel this is. 185 + 186 + st,adc-channel-type: 187 + description: | 188 + Single-ended channel input type. 189 + - "SPI_R": SPI with data on rising edge (default) 190 + - "SPI_F": SPI with data on falling edge 191 + - "MANCH_R": manchester codec, rising edge = logic 0, falling edge = logic 1 192 + - "MANCH_F": manchester codec, rising edge = logic 1, falling edge = logic 0 193 + $ref: /schemas/types.yaml#/definitions/string 194 + enum: [ SPI_R, SPI_F, MANCH_R, MANCH_F ] 195 + 196 + st,adc-channel-clk-src: 197 + description: | 198 + Conversion clock source. 199 + - "CLKIN": external SPI clock (CLKIN x) 200 + - "CLKOUT": internal SPI clock (CLKOUT) (default) 201 + - "CLKOUT_F": internal SPI clock divided by 2 (falling edge). 202 + - "CLKOUT_R": internal SPI clock divided by 2 (rising edge). 203 + $ref: /schemas/types.yaml#/definitions/string 204 + enum: [ CLKIN, CLKOUT, CLKOUT_F, CLKOUT_R ] 205 + 206 + st,adc-alt-channel: 207 + description: 208 + Must be defined if two sigma delta modulators are 209 + connected on same SPI input. 210 + If not set, channel n is connected to SPI input n. 211 + If set, channel n is connected to SPI input n + 1. 212 + type: boolean 213 + 214 + io-backends: 215 + description: 216 + Used to pipe external sigma delta modulator or internal ADC backend to DFSDM channel. 217 + maxItems: 1 218 + 219 + required: 220 + - reg 183 221 184 222 allOf: 185 223 - if: ··· 259 199 description: 260 200 From common IIO binding. Used to pipe external sigma delta 261 201 modulator or internal ADC output to DFSDM channel. 202 + deprecated: true 262 203 263 - required: 264 - - io-channels 204 + if: 205 + required: 206 + - st,adc-channels 207 + then: 208 + required: 209 + - io-channels 210 + 211 + patternProperties: 212 + "^channel@[0-7]$": 213 + required: 214 + - io-backends 265 215 266 216 - if: 267 217 properties: ··· 368 298 #address-cells = <1>; 369 299 #size-cells = <0>; 370 300 301 + // Example 1: Audio use case with generic binding 371 302 dfsdm0: filter@0 { 372 303 compatible = "st,stm32-dfsdm-dmic"; 373 304 reg = <0>; ··· 376 305 dmas = <&dmamux1 101 0x400 0x01>; 377 306 dma-names = "rx"; 378 307 #io-channel-cells = <1>; 379 - st,adc-channels = <1>; 380 - st,adc-channel-names = "dmic0"; 381 - st,adc-channel-types = "SPI_R"; 382 - st,adc-channel-clk-src = "CLKOUT"; 308 + #address-cells = <1>; 309 + #size-cells = <0>; 383 310 st,filter-order = <5>; 311 + 312 + channel@1 { 313 + reg = <1>; 314 + label = "dmic0"; 315 + st,adc-channel-type = "SPI_R"; 316 + st,adc-channel-clk-src = "CLKOUT"; 317 + st,adc-alt-channel; 318 + }; 384 319 385 320 asoc_pdm0: dfsdm-dai { 386 321 compatible = "st,stm32h7-dfsdm-dai"; ··· 395 318 }; 396 319 }; 397 320 398 - dfsdm_pdm1: filter@1 { 321 + // Example 2: Analog use case with generic binding 322 + dfsdm1: filter@1 { 399 323 compatible = "st,stm32-dfsdm-adc"; 400 324 reg = <1>; 401 325 interrupts = <GIC_SPI 111 IRQ_TYPE_LEVEL_HIGH>; 402 326 dmas = <&dmamux1 102 0x400 0x01>; 403 327 dma-names = "rx"; 404 - #io-channel-cells = <1>; 405 - st,adc-channels = <2 3>; 406 - st,adc-channel-names = "in2", "in3"; 407 - st,adc-channel-types = "SPI_R", "SPI_R"; 408 - st,adc-channel-clk-src = "CLKOUT_F", "CLKOUT_F"; 409 - io-channels = <&sd_adc2 &sd_adc3>; 410 328 st,filter-order = <1>; 329 + #io-channel-cells = <1>; 330 + #address-cells = <1>; 331 + #size-cells = <0>; 332 + 333 + channel@2 { 334 + reg = <2>; 335 + label = "in2"; 336 + st,adc-channel-type = "SPI_F"; 337 + st,adc-channel-clk-src = "CLKOUT"; 338 + st,adc-alt-channel; 339 + io-backends = <&sd_adc2>; 340 + }; 341 + 342 + channel@3 { 343 + reg = <3>; 344 + label = "in3"; 345 + st,adc-channel-type = "SPI_R"; 346 + st,adc-channel-clk-src = "CLKOUT"; 347 + io-backends = <&sd_adc3>; 348 + }; 411 349 }; 412 350 }; 413 351
+181
Documentation/devicetree/bindings/iio/dac/adi,ltc2664.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/dac/adi,ltc2664.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices LTC2664 DAC 8 + 9 + maintainers: 10 + - Michael Hennerich <michael.hennerich@analog.com> 11 + - Kim Seer Paller <kimseer.paller@analog.com> 12 + 13 + description: | 14 + Analog Devices LTC2664 4 channel, 12-/16-Bit, +-10V DAC 15 + https://www.analog.com/media/en/technical-documentation/data-sheets/2664fa.pdf 16 + 17 + properties: 18 + compatible: 19 + enum: 20 + - adi,ltc2664 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + spi-max-frequency: 26 + maximum: 50000000 27 + 28 + vcc-supply: 29 + description: Analog Supply Voltage Input. 30 + 31 + v-pos-supply: 32 + description: Positive Supply Voltage Input. 33 + 34 + v-neg-supply: 35 + description: Negative Supply Voltage Input. 36 + 37 + iovcc-supply: 38 + description: Digital Input/Output Supply Voltage. 39 + 40 + ref-supply: 41 + description: 42 + Reference Input/Output. The voltage at the REF pin sets the full-scale 43 + range of all channels. If not provided the internal reference is used and 44 + also provided on the VREF pin. 45 + 46 + reset-gpios: 47 + description: 48 + Active-low Asynchronous Clear Input. A logic low at this level-triggered 49 + input clears the part to the reset code and range determined by the 50 + hardwired option chosen using the MSPAN pins. The control registers are 51 + cleared to zero. 52 + maxItems: 1 53 + 54 + adi,manual-span-operation-config: 55 + description: 56 + This property must mimic the MSPAN pin configurations. By tying the MSPAN 57 + pins (MSP2, MSP1 and MSP0) to GND and/or VCC, any output range can be 58 + hardware-configured with different mid-scale or zero-scale reset options. 59 + The hardware configuration is latched during power on reset for proper 60 + operation. 61 + 0 - MPS2=GND, MPS1=GND, MSP0=GND (+-10V, reset to 0V) 62 + 1 - MPS2=GND, MPS1=GND, MSP0=VCC (+-5V, reset to 0V) 63 + 2 - MPS2=GND, MPS1=VCC, MSP0=GND (+-2.5V, reset to 0V) 64 + 3 - MPS2=GND, MPS1=VCC, MSP0=VCC (0V to 10, reset to 0V) 65 + 4 - MPS2=VCC, MPS1=GND, MSP0=GND (0V to 10V, reset to 5V) 66 + 5 - MPS2=VCC, MPS1=GND, MSP0=VCC (0V to 5V, reset to 0V) 67 + 6 - MPS2=VCC, MPS1=VCC, MSP0=GND (0V to 5V, reset to 2.5V) 68 + 7 - MPS2=VCC, MPS1=VCC, MSP0=VCC (0V to 5V, reset to 0V, enables SoftSpan) 69 + $ref: /schemas/types.yaml#/definitions/uint32 70 + enum: [0, 1, 2, 3, 4, 5, 6, 7] 71 + default: 7 72 + 73 + io-channels: 74 + description: 75 + ADC channel to monitor voltages and temperature at the MUXOUT pin. 76 + maxItems: 1 77 + 78 + '#address-cells': 79 + const: 1 80 + 81 + '#size-cells': 82 + const: 0 83 + 84 + patternProperties: 85 + "^channel@[0-3]$": 86 + $ref: dac.yaml 87 + type: object 88 + additionalProperties: false 89 + 90 + properties: 91 + reg: 92 + description: The channel number representing the DAC output channel. 93 + maximum: 3 94 + 95 + adi,toggle-mode: 96 + description: 97 + Set the channel as a toggle enabled channel. Toggle operation enables 98 + fast switching of a DAC output between two different DAC codes without 99 + any SPI transaction. 100 + type: boolean 101 + 102 + output-range-microvolt: 103 + description: 104 + This property is only allowed when SoftSpan is enabled. If not present, 105 + [0, 5000000] is the default output range. 106 + oneOf: 107 + - items: 108 + - const: 0 109 + - enum: [5000000, 10000000] 110 + - items: 111 + - const: -5000000 112 + - const: 5000000 113 + - items: 114 + - const: -10000000 115 + - const: 10000000 116 + - items: 117 + - const: -2500000 118 + - const: 2500000 119 + 120 + required: 121 + - reg 122 + 123 + allOf: 124 + - if: 125 + not: 126 + properties: 127 + adi,manual-span-operation-config: 128 + const: 7 129 + then: 130 + patternProperties: 131 + "^channel@[0-3]$": 132 + properties: 133 + output-range-microvolt: false 134 + 135 + required: 136 + - compatible 137 + - reg 138 + - spi-max-frequency 139 + - vcc-supply 140 + - iovcc-supply 141 + - v-pos-supply 142 + - v-neg-supply 143 + 144 + allOf: 145 + - $ref: /schemas/spi/spi-peripheral-props.yaml# 146 + 147 + additionalProperties: false 148 + 149 + examples: 150 + - | 151 + spi { 152 + #address-cells = <1>; 153 + #size-cells = <0>; 154 + dac@0 { 155 + compatible = "adi,ltc2664"; 156 + reg = <0>; 157 + spi-max-frequency = <10000000>; 158 + 159 + vcc-supply = <&vcc>; 160 + iovcc-supply = <&vcc>; 161 + ref-supply = <&vref>; 162 + v-pos-supply = <&vpos>; 163 + v-neg-supply = <&vneg>; 164 + 165 + io-channels = <&adc 0>; 166 + 167 + #address-cells = <1>; 168 + #size-cells = <0>; 169 + channel@0 { 170 + reg = <0>; 171 + adi,toggle-mode; 172 + output-range-microvolt = <(-10000000) 10000000>; 173 + }; 174 + 175 + channel@1 { 176 + reg = <1>; 177 + output-range-microvolt= <0 10000000>; 178 + }; 179 + }; 180 + }; 181 + ...
+160
Documentation/devicetree/bindings/iio/dac/adi,ltc2672.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/dac/adi,ltc2672.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices LTC2672 DAC 8 + 9 + maintainers: 10 + - Michael Hennerich <michael.hennerich@analog.com> 11 + - Kim Seer Paller <kimseer.paller@analog.com> 12 + 13 + description: | 14 + Analog Devices LTC2672 5 channel, 12-/16-Bit, 300mA DAC 15 + https://www.analog.com/media/en/technical-documentation/data-sheets/ltc2672.pdf 16 + 17 + properties: 18 + compatible: 19 + enum: 20 + - adi,ltc2672 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + spi-max-frequency: 26 + maximum: 50000000 27 + 28 + vcc-supply: 29 + description: Analog Supply Voltage Input. 30 + 31 + v-neg-supply: 32 + description: Negative Supply Voltage Input. 33 + 34 + vdd0-supply: 35 + description: Positive Supply Voltage Input for DAC OUT0. 36 + 37 + vdd1-supply: 38 + description: Positive Supply Voltage Input for DAC OUT1. 39 + 40 + vdd2-supply: 41 + description: Positive Supply Voltage Input for DAC OUT2. 42 + 43 + vdd3-supply: 44 + description: Positive Supply Voltage Input for DAC OUT3. 45 + 46 + vdd4-supply: 47 + description: Positive Supply Voltage Input for DAC OUT4. 48 + 49 + iovcc-supply: 50 + description: Digital Input/Output Supply Voltage. 51 + 52 + ref-supply: 53 + description: 54 + Reference Input/Output. The voltage at the REF pin sets the full-scale 55 + range of all channels. If not provided the internal reference is used and 56 + also provided on the VREF pin. 57 + 58 + reset-gpios: 59 + description: 60 + Active Low Asynchronous Clear Input. A logic low at this level triggered 61 + input clears the device to the default reset code and output range, which 62 + is zero-scale with the outputs off. The control registers are cleared to 63 + zero. 64 + maxItems: 1 65 + 66 + adi,rfsadj-ohms: 67 + description: 68 + If FSADJ is tied to VCC, an internal RFSADJ (20 kΩ) is selected, which 69 + results in nominal output ranges. When an external resistor of 19 kΩ to 70 + 41 kΩ can be used instead by connecting the resistor between FSADJ and GND 71 + it controls the scaling of the ranges, and the internal resistor is 72 + automatically disconnected. 73 + minimum: 19000 74 + maximum: 41000 75 + default: 20000 76 + 77 + io-channels: 78 + description: 79 + ADC channel to monitor voltages and currents at the MUX pin. 80 + maxItems: 1 81 + 82 + '#address-cells': 83 + const: 1 84 + 85 + '#size-cells': 86 + const: 0 87 + 88 + patternProperties: 89 + "^channel@[0-4]$": 90 + $ref: dac.yaml 91 + type: object 92 + additionalProperties: false 93 + 94 + properties: 95 + reg: 96 + description: The channel number representing the DAC output channel. 97 + maximum: 4 98 + 99 + adi,toggle-mode: 100 + description: 101 + Set the channel as a toggle enabled channel. Toggle operation enables 102 + fast switching of a DAC output between two different DAC codes without 103 + any SPI transaction. 104 + type: boolean 105 + 106 + output-range-microamp: 107 + items: 108 + - const: 0 109 + - enum: [3125000, 6250000, 12500000, 25000000, 50000000, 100000000, 110 + 200000000, 300000000] 111 + 112 + required: 113 + - reg 114 + - output-range-microamp 115 + 116 + required: 117 + - compatible 118 + - reg 119 + - spi-max-frequency 120 + - vcc-supply 121 + - iovcc-supply 122 + - v-neg-supply 123 + 124 + allOf: 125 + - $ref: /schemas/spi/spi-peripheral-props.yaml# 126 + 127 + additionalProperties: false 128 + 129 + examples: 130 + - | 131 + spi { 132 + #address-cells = <1>; 133 + #size-cells = <0>; 134 + dac@0 { 135 + compatible = "adi,ltc2672"; 136 + reg = <0>; 137 + spi-max-frequency = <10000000>; 138 + 139 + vcc-supply = <&vcc>; 140 + iovcc-supply = <&vcc>; 141 + ref-supply = <&vref>; 142 + v-neg-supply = <&vneg>; 143 + 144 + io-channels = <&adc 0>; 145 + 146 + #address-cells = <1>; 147 + #size-cells = <0>; 148 + channel@0 { 149 + reg = <0>; 150 + adi,toggle-mode; 151 + output-range-microamp = <0 3125000>; 152 + }; 153 + 154 + channel@1 { 155 + reg = <1>; 156 + output-range-microamp = <0 6250000>; 157 + }; 158 + }; 159 + }; 160 + ...
+50
Documentation/devicetree/bindings/iio/dac/dac.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/dac/dac.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: IIO Common Properties for DAC Channels 8 + 9 + maintainers: 10 + - Jonathan Cameron <jic23@kernel.org> 11 + 12 + description: 13 + A few properties are defined in a common way for DAC channels. 14 + 15 + properties: 16 + $nodename: 17 + pattern: "^channel(@[0-9a-f]+)?$" 18 + description: 19 + A channel index should match reg. 20 + 21 + reg: 22 + maxItems: 1 23 + 24 + label: 25 + description: Unique name to identify which channel this is. 26 + 27 + output-range-microamp: 28 + maxItems: 2 29 + minItems: 2 30 + description: 31 + Specify the channel output full scale range in microamperes. 32 + 33 + output-range-microvolt: 34 + maxItems: 2 35 + minItems: 2 36 + description: 37 + Specify the channel output full scale range in microvolts. 38 + 39 + anyOf: 40 + - oneOf: 41 + - required: 42 + - reg 43 + - output-range-microamp 44 + - required: 45 + - reg 46 + - output-range-microvolt 47 + - required: 48 + - reg 49 + 50 + additionalProperties: true
+10
Documentation/devicetree/bindings/iio/frequency/adi,adf4377.yaml
··· 17 17 applications. 18 18 19 19 https://www.analog.com/en/products/adf4377.html 20 + https://www.analog.com/en/products/adf4378.html 20 21 21 22 properties: 22 23 compatible: ··· 74 73 75 74 allOf: 76 75 - $ref: /schemas/spi/spi-peripheral-props.yaml# 76 + - if: 77 + properties: 78 + compatible: 79 + contains: 80 + enum: 81 + - adi,adf4378 82 + then: 83 + properties: 84 + clk2-enable-gpios: false 77 85 78 86 unevaluatedProperties: false 79 87
+55
Documentation/devicetree/bindings/iio/humidity/sciosense,ens210.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/humidity/sciosense,ens210.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: ScioSense ENS210 temperature and humidity sensor 8 + 9 + maintainers: 10 + - Joshua Felmeden <jfelmeden@thegoodpenguin.co.uk> 11 + 12 + description: | 13 + Temperature and Humidity sensor. 14 + 15 + Datasheet: 16 + https://www.sciosense.com/wp-content/uploads/2024/04/ENS21x-Datasheet.pdf 17 + https://www.sciosense.com/wp-content/uploads/2023/12/ENS210-Datasheet.pdf 18 + 19 + properties: 20 + compatible: 21 + oneOf: 22 + - items: 23 + - enum: 24 + - sciosense,ens210a 25 + - sciosense,ens211 26 + - sciosense,ens212 27 + - sciosense,ens213a 28 + - sciosense,ens215 29 + - const: sciosense,ens210 30 + - const: sciosense,ens210 31 + 32 + reg: 33 + maxItems: 1 34 + 35 + vdd-supply: true 36 + 37 + required: 38 + - compatible 39 + - reg 40 + 41 + additionalProperties: false 42 + 43 + examples: 44 + - | 45 + i2c { 46 + #address-cells = <1>; 47 + #size-cells = <0>; 48 + 49 + temperature-sensor@43 { 50 + compatible = "sciosense,ens210"; 51 + reg = <0x43>; 52 + }; 53 + }; 54 + ... 55 +
+3 -1
Documentation/devicetree/bindings/iio/light/liteon,ltrf216a.yaml
··· 14 14 15 15 properties: 16 16 compatible: 17 - const: liteon,ltrf216a 17 + enum: 18 + - liteon,ltr308 19 + - liteon,ltrf216a 18 20 19 21 reg: 20 22 maxItems: 1
+53
Documentation/devicetree/bindings/iio/light/rohm,bh1745.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/light/rohm,bh1745.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: ROHM BH1745 colour sensor 8 + 9 + maintainers: 10 + - Mudit Sharma <muditsharma.info@gmail.com> 11 + 12 + description: 13 + BH1745 is an I2C colour sensor with red, green, blue and clear 14 + channels. It has a programmable active low interrupt pin. 15 + Interrupt occurs when the signal from the selected interrupt 16 + source channel crosses set interrupt threshold high/low level. 17 + 18 + properties: 19 + compatible: 20 + const: rohm,bh1745 21 + 22 + reg: 23 + maxItems: 1 24 + 25 + interrupts: 26 + maxItems: 1 27 + 28 + vdd-supply: true 29 + 30 + required: 31 + - compatible 32 + - reg 33 + - vdd-supply 34 + 35 + additionalProperties: false 36 + 37 + examples: 38 + - | 39 + #include <dt-bindings/interrupt-controller/irq.h> 40 + i2c { 41 + #address-cells = <1>; 42 + #size-cells = <0>; 43 + 44 + colour-sensor@38 { 45 + compatible = "rohm,bh1745"; 46 + reg = <0x38>; 47 + interrupt-parent = <&gpio>; 48 + interrupts = <19 IRQ_TYPE_LEVEL_LOW>; 49 + vdd-supply = <&vdd>; 50 + }; 51 + }; 52 + 53 + ...
+5 -6
Documentation/devicetree/bindings/iio/light/rohm,bu27034.yaml Documentation/devicetree/bindings/iio/light/rohm,bu27034anuc.yaml
··· 1 1 # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 2 %YAML 1.2 3 3 --- 4 - $id: http://devicetree.org/schemas/iio/light/rohm,bu27034.yaml# 4 + $id: http://devicetree.org/schemas/iio/light/rohm,bu27034anuc.yaml# 5 5 $schema: http://devicetree.org/meta-schemas/core.yaml# 6 6 7 - title: ROHM BU27034 ambient light sensor 7 + title: ROHM BU27034ANUC ambient light sensor 8 8 9 9 maintainers: 10 10 - Matti Vaittinen <mazziesaccount@gmail.com> 11 11 12 12 description: | 13 - ROHM BU27034 is an ambient light sesnor with 3 channels and 3 photo diodes 13 + ROHM BU27034ANUC is an ambient light sensor with 2 channels and 2 photo diodes 14 14 capable of detecting a very wide range of illuminance. Typical application 15 15 is adjusting LCD and backlight power of TVs and mobile phones. 16 - https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/light/bu27034nuc-e.pdf 17 16 18 17 properties: 19 18 compatible: 20 - const: rohm,bu27034 19 + const: rohm,bu27034anuc 21 20 22 21 reg: 23 22 maxItems: 1 ··· 36 37 #size-cells = <0>; 37 38 38 39 light-sensor@38 { 39 - compatible = "rohm,bu27034"; 40 + compatible = "rohm,bu27034anuc"; 40 41 reg = <0x38>; 41 42 vdd-supply = <&vdd>; 42 43 };
+9 -4
Documentation/devicetree/bindings/iio/light/stk33xx.yaml
··· 18 18 19 19 properties: 20 20 compatible: 21 - enum: 22 - - sensortek,stk3310 23 - - sensortek,stk3311 24 - - sensortek,stk3335 21 + oneOf: 22 + - enum: 23 + - sensortek,stk3310 24 + - sensortek,stk3311 25 + - sensortek,stk3335 26 + - items: 27 + - enum: 28 + - sensortek,stk3013 29 + - const: sensortek,stk3310 25 30 26 31 reg: 27 32 maxItems: 1
-1
Documentation/devicetree/bindings/iio/magnetometer/asahi-kasei,ak8975.yaml
··· 23 23 - ak8963 24 24 - ak09911 25 25 - ak09912 26 - - ak09916 27 26 deprecated: true 28 27 29 28 reg:
+3
Documentation/devicetree/bindings/iio/magnetometer/bosch,bmc150_magn.yaml
··· 36 36 interrupts: 37 37 maxItems: 1 38 38 39 + mount-matrix: 40 + description: an optional 3x3 mounting rotation matrix. 41 + 39 42 additionalProperties: false 40 43 41 44 required:
+46
Documentation/devicetree/bindings/iio/pressure/sensirion,sdp500.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/pressure/sensirion,sdp500.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: sdp500/sdp510 pressure sensor with I2C bus interface 8 + 9 + maintainers: 10 + - Petar Stoykov <petar.stoykov@prodrive-technologies.com> 11 + 12 + description: | 13 + Pressure sensor from Sensirion with I2C bus interface. 14 + There is no software difference between sdp500 and sdp510. 15 + 16 + properties: 17 + compatible: 18 + oneOf: 19 + - items: 20 + - const: sensirion,sdp510 21 + - const: sensirion,sdp500 22 + - const: sensirion,sdp500 23 + 24 + reg: 25 + maxItems: 1 26 + 27 + vdd-supply: true 28 + 29 + required: 30 + - compatible 31 + - reg 32 + - vdd-supply 33 + 34 + additionalProperties: false 35 + 36 + examples: 37 + - | 38 + i2c { 39 + #address-cells = <1>; 40 + #size-cells = <0>; 41 + pressure@40 { 42 + compatible = "sensirion,sdp500"; 43 + reg = <0x40>; 44 + vdd-supply = <&foo>; 45 + }; 46 + };
+93
Documentation/devicetree/bindings/iio/proximity/tyhx,hx9023s.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/proximity/tyhx,hx9023s.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: TYHX HX9023S capacitive proximity sensor 8 + 9 + maintainers: 10 + - Yasin Lee <yasin.lee.x@gmail.com> 11 + 12 + description: | 13 + TYHX HX9023S proximity sensor. Datasheet can be found here: 14 + http://www.tianyihexin.com/ueditor/php/upload/file/20240614/1718336303992081.pdf 15 + 16 + properties: 17 + compatible: 18 + const: tyhx,hx9023s 19 + 20 + reg: 21 + maxItems: 1 22 + 23 + interrupts: 24 + description: 25 + Generated by device to announce preceding read request has finished 26 + and data is available or that a close/far proximity event has happened. 27 + maxItems: 1 28 + 29 + vdd-supply: true 30 + 31 + "#address-cells": 32 + const: 1 33 + 34 + "#size-cells": 35 + const: 0 36 + 37 + patternProperties: 38 + "^channel@[0-4]$": 39 + $ref: /schemas/iio/adc/adc.yaml 40 + type: object 41 + unevaluatedProperties: false 42 + 43 + properties: 44 + reg: 45 + minimum: 0 46 + maximum: 4 47 + description: The channel number. 48 + 49 + required: 50 + - compatible 51 + - reg 52 + - vdd-supply 53 + 54 + unevaluatedProperties: false 55 + 56 + examples: 57 + - | 58 + #include <dt-bindings/interrupt-controller/irq.h> 59 + i2c { 60 + #address-cells = <1>; 61 + #size-cells = <0>; 62 + proximity@2a { 63 + compatible = "tyhx,hx9023s"; 64 + reg = <0x2a>; 65 + interrupt-parent = <&pio>; 66 + interrupts = <16 IRQ_TYPE_EDGE_FALLING>; 67 + vdd-supply = <&pp1800_prox>; 68 + 69 + #address-cells = <1>; 70 + #size-cells = <0>; 71 + 72 + channel@0 { 73 + reg = <0>; 74 + single-channel = <0>; 75 + }; 76 + channel@1 { 77 + reg = <1>; 78 + single-channel = <1>; 79 + }; 80 + channel@2 { 81 + reg = <2>; 82 + single-channel = <2>; 83 + }; 84 + channel@3 { 85 + reg = <3>; 86 + diff-channels = <1 0>; 87 + }; 88 + channel@4 { 89 + reg = <4>; 90 + diff-channels = <2 0>; 91 + }; 92 + }; 93 + };
+2
Documentation/devicetree/bindings/vendor-prefixes.yaml
··· 1535 1535 description: Turing Machines, Inc. 1536 1536 "^tyan,.*": 1537 1537 description: Tyan Computer Corporation 1538 + "^tyhx,.*": 1539 + description: NanjingTianyihexin Electronics Ltd. 1538 1540 "^u-blox,.*": 1539 1541 description: u-blox 1540 1542 "^u-boot,.*":
+131
Documentation/iio/ad4000.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ============= 4 + AD4000 driver 5 + ============= 6 + 7 + Device driver for Analog Devices Inc. AD4000 series of ADCs. 8 + 9 + Supported devices 10 + ================= 11 + 12 + * `AD4000 <https://www.analog.com/AD4000>`_ 13 + * `AD4001 <https://www.analog.com/AD4001>`_ 14 + * `AD4002 <https://www.analog.com/AD4002>`_ 15 + * `AD4003 <https://www.analog.com/AD4003>`_ 16 + * `AD4004 <https://www.analog.com/AD4004>`_ 17 + * `AD4005 <https://www.analog.com/AD4005>`_ 18 + * `AD4006 <https://www.analog.com/AD4006>`_ 19 + * `AD4007 <https://www.analog.com/AD4007>`_ 20 + * `AD4008 <https://www.analog.com/AD4008>`_ 21 + * `AD4010 <https://www.analog.com/AD4010>`_ 22 + * `AD4011 <https://www.analog.com/AD4011>`_ 23 + * `AD4020 <https://www.analog.com/AD4020>`_ 24 + * `AD4021 <https://www.analog.com/AD4021>`_ 25 + * `AD4022 <https://www.analog.com/AD4022>`_ 26 + * `ADAQ4001 <https://www.analog.com/ADAQ4001>`_ 27 + * `ADAQ4003 <https://www.analog.com/ADAQ4003>`_ 28 + 29 + Wiring connections 30 + ------------------ 31 + 32 + Devices of the AD4000 series can be connected to the SPI host controller in a 33 + few different modes. 34 + 35 + CS mode, 3-wire turbo mode 36 + ^^^^^^^^^^^^^^^^^^^^^^^^^^ 37 + 38 + Datasheet "3-wire" mode is what most resembles standard SPI connection which, 39 + for these devices, comprises of connecting the controller CS line to device CNV 40 + pin and other SPI lines as usual. This configuration is (misleadingly) called 41 + "CS Mode, 3-Wire Turbo Mode" connection in datasheets. 42 + NOTE: The datasheet definition of 3-wire mode for the AD4000 series is NOT the 43 + same of standard spi-3wire mode. 44 + This is the only connection mode that allows configuration register access but 45 + it requires the SPI controller to support the ``SPI_MOSI_IDLE_HIGH`` feature. 46 + 47 + Omit the ``adi,sdi-pin`` property in device tree to select this mode. 48 + 49 + :: 50 + 51 + +-------------+ 52 + + ----------------------------------| SDO | 53 + | | | 54 + | +-------------------| CS | 55 + | v | | 56 + | +--------------------+ | HOST | 57 + | | CNV | | | 58 + +--->| SDI AD4000 SDO |-------->| SDI | 59 + | SCK | | | 60 + +--------------------+ | | 61 + ^ | | 62 + +--------------------| SCLK | 63 + +-------------+ 64 + 65 + CS mode, 3-wire, without busy indicator 66 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 67 + 68 + Another wiring configuration supported as "3-wire" mode has the SDI pin 69 + hard-wired to digital input/output interface supply (VIO). In this setup, the 70 + controller is not required to support ``SPI_MOSI_IDLE_HIGH`` but register access 71 + is not possible. This connection mode saves one wire and works with any SPI 72 + controller. 73 + 74 + Set the ``adi,sdi-pin`` device tree property to ``"high"`` to select this mode. 75 + 76 + :: 77 + 78 + +-------------+ 79 + +--------------------| CS | 80 + v | | 81 + VIO +--------------------+ | HOST | 82 + | | CNV | | | 83 + +--->| SDI AD4000 SDO |-------->| SDI | 84 + | SCK | | | 85 + +--------------------+ | | 86 + ^ | | 87 + +--------------------| SCLK | 88 + +-------------+ 89 + 90 + Alternatively, a GPIO may be connected to the device CNV pin. This is similar to 91 + the previous wiring configuration but saves the use of a CS line. 92 + 93 + :: 94 + 95 + +-------------+ 96 + +--------------------| GPIO | 97 + v | | 98 + VIO +--------------------+ | HOST | 99 + | | CNV | | | 100 + +--->| SDI AD4000 SDO |-------->| SDI | 101 + | SCK | | | 102 + +--------------------+ | | 103 + ^ | | 104 + +--------------------| SCLK | 105 + +-------------+ 106 + 107 + CS mode, 4-wire without busy indicator 108 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 109 + 110 + In datasheet "4-wire" mode, the controller CS line is connected to the ADC SDI 111 + pin and a GPIO is connected to the ADC CNV pin. This connection mode may better 112 + suit scenarios where multiple ADCs can share one CNV trigger. 113 + 114 + Set ``adi,sdi-pin`` to ``"cs"`` to select this mode. 115 + 116 + 117 + :: 118 + 119 + +-------------+ 120 + + ----------------------------------| CS | 121 + | | | 122 + | +-------------------| GPIO | 123 + | v | | 124 + | +--------------------+ | HOST | 125 + | | CNV | | | 126 + +--->| SDI AD4000 SDO |-------->| SDI | 127 + | SCK | | | 128 + +--------------------+ | | 129 + ^ | | 130 + +--------------------| SCLK | 131 + +-------------+
+162
Documentation/iio/ad4695.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ============= 4 + AD4695 driver 5 + ============= 6 + 7 + ADC driver for Analog Devices Inc. AD4695 and similar devices. The module name 8 + is ``ad4695``. 9 + 10 + 11 + Supported devices 12 + ================= 13 + 14 + The following chips are supported by this driver: 15 + 16 + * `AD4695 <https://www.analog.com/AD4695>`_ 17 + * `AD4696 <https://www.analog.com/AD4696>`_ 18 + * `AD4697 <https://www.analog.com/AD4697>`_ 19 + * `AD4698 <https://www.analog.com/AD4698>`_ 20 + 21 + 22 + Supported features 23 + ================== 24 + 25 + SPI wiring modes 26 + ---------------- 27 + 28 + The driver currently supports the following SPI wiring configuration: 29 + 30 + 4-wire mode 31 + ^^^^^^^^^^^ 32 + 33 + In this mode, CNV and CS are tied together and there is a single SDO line. 34 + 35 + .. code-block:: 36 + 37 + +-------------+ +-------------+ 38 + | CS |<-+------| CS | 39 + | CNV |<-+ | | 40 + | ADC | | HOST | 41 + | | | | 42 + | SDI |<--------| SDO | 43 + | SDO |-------->| SDI | 44 + | SCLK |<--------| SCLK | 45 + +-------------+ +-------------+ 46 + 47 + To use this mode, in the device tree, omit the ``cnv-gpios`` and 48 + ``spi-rx-bus-width`` properties. 49 + 50 + Channel configuration 51 + --------------------- 52 + 53 + Since the chip supports multiple ways to configure each channel, this must be 54 + described in the device tree based on what is actually wired up to the inputs. 55 + 56 + There are three typical configurations: 57 + 58 + An ``INx`` pin is used as the positive input with the ``REFGND``, ``COM`` or 59 + the next ``INx`` pin as the negative input. 60 + 61 + Pairing with REFGND 62 + ^^^^^^^^^^^^^^^^^^^ 63 + 64 + Each ``INx`` pin can be used as a pseudo-differential input in conjunction with 65 + the ``REFGND`` pin. The device tree will look like this: 66 + 67 + .. code-block:: 68 + 69 + channel@0 { 70 + reg = <0>; /* IN0 */ 71 + }; 72 + 73 + If no other channel properties are needed (e.g. ``adi,no-high-z``), the channel 74 + node can be omitted entirely. 75 + 76 + This will appear on the IIO bus as the ``voltage0`` channel. The processed value 77 + (*raw × scale*) will be the voltage present on the ``IN0`` pin relative to 78 + ``REFGND``. (Offset is always 0 when pairing with ``REFGND``.) 79 + 80 + Pairing with COM 81 + ^^^^^^^^^^^^^^^^ 82 + 83 + Each ``INx`` pin can be used as a pseudo-differential input in conjunction with 84 + the ``COM`` pin. The device tree will look like this: 85 + 86 + .. code-block:: 87 + 88 + com-supply = <&vref_div_2>; 89 + 90 + channel@1 { 91 + reg = <1>; /* IN1 */ 92 + common-mode-channel = <AD4695_COMMON_MODE_COM>; 93 + bipolar; 94 + }; 95 + 96 + This will appear on the IIO bus as the ``voltage1`` channel. The processed value 97 + (*(raw + offset) × scale*) will be the voltage measured on the ``IN1`` pin 98 + relative to ``REFGND``. (The offset is determined by the ``com-supply`` voltage.) 99 + 100 + The macro comes from: 101 + 102 + .. code-block:: 103 + 104 + #include <dt-bindings/iio/adi,ad4695.h> 105 + 106 + Pairing two INx pins 107 + ^^^^^^^^^^^^^^^^^^^^ 108 + 109 + An even-numbered ``INx`` pin and the following odd-numbered ``INx`` pin can be 110 + used as a pseudo-differential input. The device tree for using ``IN2`` as the 111 + positive input and ``IN3`` as the negative input will look like this: 112 + 113 + .. code-block:: 114 + 115 + in3-supply = <&vref_div_2>; 116 + 117 + channel@2 { 118 + reg = <2>; /* IN2 */ 119 + common-mode-channel = <3>; /* IN3 */ 120 + bipolar; 121 + }; 122 + 123 + This will appear on the IIO bus as the ``voltage2`` channel. The processed value 124 + (*(raw + offset) × scale*) will be the voltage measured on the ``IN1`` pin 125 + relative to ``REFGND``. (Offset is determined by the ``in3-supply`` voltage.) 126 + 127 + VCC supply 128 + ---------- 129 + 130 + The chip supports being powered by an external LDO via the ``VCC`` input or an 131 + internal LDO via the ``LDO_IN`` input. The driver looks at the device tree to 132 + determine which is being used. If ``ldo-supply`` is present, then the internal 133 + LDO is used. If ``vcc-supply`` is present, then the external LDO is used and 134 + the internal LDO is disabled. 135 + 136 + Reference voltage 137 + ----------------- 138 + 139 + The chip supports an external reference voltage via the ``REF`` input or an 140 + internal buffered reference voltage via the ``REFIN`` input. The driver looks 141 + at the device tree to determine which is being used. If ``ref-supply`` is 142 + present, then the external reference voltage is used and the internal buffer is 143 + disabled. If ``refin-supply`` is present, then the internal buffered reference 144 + voltage is used. 145 + 146 + Unimplemented features 147 + ---------------------- 148 + 149 + - Additional wiring modes 150 + - Threshold events 151 + - Oversampling 152 + - Gain/offset calibration 153 + - GPIO support 154 + - CRC support 155 + 156 + Device buffers 157 + ============== 158 + 159 + This driver supports hardware triggered buffers. This uses the "advanced 160 + sequencer" feature of the chip to trigger a burst of conversions. 161 + 162 + Also see :doc:`iio_devbuf` for more general information.
+130
Documentation/iio/ad7380.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0-only 2 + 3 + ============= 4 + AD7380 driver 5 + ============= 6 + 7 + ADC driver for Analog Devices Inc. AD7380 and similar devices. The module name 8 + is ``ad7380``. 9 + 10 + 11 + Supported devices 12 + ================= 13 + 14 + The following chips are supported by this driver: 15 + 16 + * `AD7380 <https://www.analog.com/en/products/ad7380.html>`_ 17 + * `AD7381 <https://www.analog.com/en/products/ad7381.html>`_ 18 + * `AD7383 <https://www.analog.com/en/products/ad7383.html>`_ 19 + * `AD7384 <https://www.analog.com/en/products/ad7384.html>`_ 20 + * `AD7386 <https://www.analog.com/en/products/ad7386.html>`_ 21 + * `AD7387 <https://www.analog.com/en/products/ad7387.html>`_ 22 + * `AD7388 <https://www.analog.com/en/products/ad7388.html>`_ 23 + * `AD7380-4 <https://www.analog.com/en/products/ad7380-4.html>`_ 24 + * `AD7381-4 <https://www.analog.com/en/products/ad7381-4.html>`_ 25 + * `AD7383-4 <https://www.analog.com/en/products/ad7383-4.html>`_ 26 + * `AD7384-4 <https://www.analog.com/en/products/ad7384-4.html>`_ 27 + * `AD7386-4 <https://www.analog.com/en/products/ad7386-4.html>`_ 28 + * `AD7387-4 <https://www.analog.com/en/products/ad7387-4.html>`_ 29 + * `AD7388-4 <https://www.analog.com/en/products/ad7388-4.html>`_ 30 + 31 + 32 + Supported features 33 + ================== 34 + 35 + SPI wiring modes 36 + ---------------- 37 + 38 + ad738x ADCs can output data on several SDO lines (1/2/4). The driver currently 39 + supports only 1 SDO line. 40 + 41 + Reference voltage 42 + ----------------- 43 + 44 + 2 possible reference voltage sources are supported: 45 + 46 + - Internal reference (2.5V) 47 + - External reference (2.5V to 3.3V) 48 + 49 + The source is determined by the device tree. If ``refio-supply`` is present, 50 + then the external reference is used, else the internal reference is used. 51 + 52 + Oversampling and resolution boost 53 + --------------------------------- 54 + 55 + This family supports 2 types of oversampling: normal average and rolling 56 + average. Only normal average is supported by the driver, as rolling average can 57 + be achieved by processing a captured data buffer. The following ratios are 58 + available: 1 (oversampling disabled)/2/4/8/16/32. 59 + 60 + When the on-chip oversampling function is enabled the performance of the ADC can 61 + exceed the default resolution. To accommodate the performance boost achievable, 62 + it is possible to enable an additional two bits of resolution. Because the 63 + resolution boost feature can only be enabled when oversampling is enabled and 64 + oversampling is not as useful without the resolution boost, the driver 65 + automatically enables the resolution boost if and only if oversampling is 66 + enabled. 67 + 68 + Since the resolution boost feature causes 16-bit chips to now have 18-bit data 69 + which means the storagebits has to change from 16 to 32 bits, we use the new 70 + ext_scan_type feature to allow changing the scan_type at runtime. Unfortunately 71 + libiio does not support it. So when enabling or disabling oversampling, user 72 + must restart iiod using the following command: 73 + 74 + .. code-block:: bash 75 + 76 + root:~# systemctl restart iiod 77 + 78 + Channel selection and sequencer (single-end chips only) 79 + ------------------------------------------------------- 80 + 81 + Single-ended chips of this family (ad7386/7/8(-4)) have a 2:1 multiplexer in 82 + front of each ADC. They also include additional configuration registers that 83 + allow for either manual selection or automatic switching (sequencer mode), of 84 + the multiplexer inputs. 85 + 86 + From an IIO point of view, all inputs are exported, i.e ad7386/7/8 87 + export 4 channels and ad7386-4/7-4/8-4 export 8 channels. 88 + 89 + Inputs ``AinX0`` of multiplexers correspond to the first half of IIO channels (i.e 90 + 0-1 or 0-3) and inputs ``AinX1`` correspond to second half (i.e 2-3 or 4-7). 91 + Example for AD7386/7/8 (2 channels parts): 92 + 93 + .. code-block:: 94 + 95 + IIO | AD7386/7/8 96 + | +---------------------------- 97 + | | _____ ______ 98 + | | | | | | 99 + voltage0 | AinA0 --|--->| | | | 100 + | | | mux |----->| ADCA |--- 101 + voltage2 | AinA1 --|--->| | | | 102 + | | |_____| |_____ | 103 + | | _____ ______ 104 + | | | | | | 105 + voltage1 | AinB0 --|--->| | | | 106 + | | | mux |----->| ADCB |--- 107 + voltage3 | AinB1 --|--->| | | | 108 + | | |_____| |______| 109 + | | 110 + | +---------------------------- 111 + 112 + 113 + When enabling sequencer mode, the effective sampling rate is divided by two. 114 + 115 + Unimplemented features 116 + ---------------------- 117 + 118 + - 2/4 SDO lines 119 + - Rolling average oversampling 120 + - Power down mode 121 + - CRC indication 122 + - Alert 123 + 124 + 125 + Device buffers 126 + ============== 127 + 128 + This driver supports IIO triggered buffers. 129 + 130 + See :doc:`iio_devbuf` for more information.
+233
Documentation/iio/adxl380.rst
··· 1 + .. SPDX-License-Identifier: GPL-2.0 2 + 3 + =============== 4 + ADXL380 driver 5 + =============== 6 + 7 + This driver supports Analog Device's ADXL380/382 on SPI/I2C bus. 8 + 9 + 1. Supported devices 10 + ==================== 11 + 12 + * `ADXL380 <https://www.analog.com/ADXL380>`_ 13 + * `ADXL382 <https://www.analog.com/ADXL382>`_ 14 + 15 + The ADXL380/ADXL382 is a low noise density, low power, 3-axis accelerometer with 16 + selectable measurement ranges. The ADXL380 supports the ±4 g, ±8 g, and ±16 g 17 + ranges, and the ADXL382 supports ±15 g, ±30 g, and ±60 g ranges. 18 + 19 + 2. Device attributes 20 + ==================== 21 + 22 + Accelerometer measurements are always provided. 23 + 24 + Temperature data are also provided. This data can be used to monitor the 25 + internal system temperature or to improve the temperature stability of the 26 + device via calibration. 27 + 28 + Each IIO device, has a device folder under ``/sys/bus/iio/devices/iio:deviceX``, 29 + where X is the IIO index of the device. Under these folders reside a set of 30 + device files, depending on the characteristics and features of the hardware 31 + device in questions. These files are consistently generalized and documented in 32 + the IIO ABI documentation. 33 + 34 + The following tables show the adxl380 related device files, found in the 35 + specific device folder path ``/sys/bus/iio/devices/iio:deviceX``. 36 + 37 + +---------------------------------------------------+----------------------------------------------------------+ 38 + | 3-Axis Accelerometer related device files | Description | 39 + +---------------------------------------------------+----------------------------------------------------------+ 40 + | in_accel_scale | Scale for the accelerometer channels. | 41 + +---------------------------------------------------+----------------------------------------------------------+ 42 + | in_accel_filter_high_pass_3db_frequency | Low pass filter bandwidth. | 43 + +---------------------------------------------------+----------------------------------------------------------+ 44 + | in_accel_filter_high_pass_3db_frequency_available | Available low pass filter bandwidth configurations. | 45 + +---------------------------------------------------+----------------------------------------------------------+ 46 + | in_accel_filter_low_pass_3db_frequency | High pass filter bandwidth. | 47 + +---------------------------------------------------+----------------------------------------------------------+ 48 + | in_accel_filter_low_pass_3db_frequency_available | Available high pass filter bandwidth configurations. | 49 + +---------------------------------------------------+----------------------------------------------------------+ 50 + | in_accel_x_calibbias | Calibration offset for the X-axis accelerometer channel. | 51 + +---------------------------------------------------+----------------------------------------------------------+ 52 + | in_accel_x_raw | Raw X-axis accelerometer channel value. | 53 + +---------------------------------------------------+----------------------------------------------------------+ 54 + | in_accel_y_calibbias | y-axis acceleration offset correction | 55 + +---------------------------------------------------+----------------------------------------------------------+ 56 + | in_accel_y_raw | Raw Y-axis accelerometer channel value. | 57 + +---------------------------------------------------+----------------------------------------------------------+ 58 + | in_accel_z_calibbias | Calibration offset for the Z-axis accelerometer channel. | 59 + +---------------------------------------------------+----------------------------------------------------------+ 60 + | in_accel_z_raw | Raw Z-axis accelerometer channel value. | 61 + +---------------------------------------------------+----------------------------------------------------------+ 62 + 63 + +----------------------------------+--------------------------------------------+ 64 + | Temperature sensor related files | Description | 65 + +----------------------------------+--------------------------------------------+ 66 + | in_temp_raw | Raw temperature channel value. | 67 + +----------------------------------+--------------------------------------------+ 68 + | in_temp_offset | Offset for the temperature sensor channel. | 69 + +----------------------------------+--------------------------------------------+ 70 + | in_temp_scale | Scale for the temperature sensor channel. | 71 + +----------------------------------+--------------------------------------------+ 72 + 73 + +------------------------------+----------------------------------------------+ 74 + | Miscellaneous device files | Description | 75 + +------------------------------+----------------------------------------------+ 76 + | name | Name of the IIO device. | 77 + +------------------------------+----------------------------------------------+ 78 + | sampling_frequency | Currently selected sample rate. | 79 + +------------------------------+----------------------------------------------+ 80 + | sampling_frequency_available | Available sampling frequency configurations. | 81 + +------------------------------+----------------------------------------------+ 82 + 83 + Channels processed values 84 + ------------------------- 85 + 86 + A channel value can be read from its _raw attribute. The value returned is the 87 + raw value as reported by the devices. To get the processed value of the channel, 88 + apply the following formula: 89 + 90 + .. code-block:: bash 91 + 92 + processed value = (_raw + _offset) * _scale 93 + 94 + Where _offset and _scale are device attributes. If no _offset attribute is 95 + present, simply assume its value is 0. 96 + 97 + The adis16475 driver offers data for 2 types of channels, the table below shows 98 + the measurement units for the processed value, which are defined by the IIO 99 + framework: 100 + 101 + +-------------------------------------+---------------------------+ 102 + | Channel type | Measurement unit | 103 + +-------------------------------------+---------------------------+ 104 + | Acceleration on X, Y, and Z axis | Meters per Second squared | 105 + +-------------------------------------+---------------------------+ 106 + | Temperature | Millidegrees Celsius | 107 + +-------------------------------------+---------------------------+ 108 + 109 + Usage examples 110 + -------------- 111 + 112 + Show device name: 113 + 114 + .. code-block:: bash 115 + 116 + root:/sys/bus/iio/devices/iio:device0> cat name 117 + adxl382 118 + 119 + Show accelerometer channels value: 120 + 121 + .. code-block:: bash 122 + 123 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_x_raw 124 + -1771 125 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_y_raw 126 + 282 127 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_z_raw 128 + -1523 129 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_scale 130 + 0.004903325 131 + 132 + - X-axis acceleration = in_accel_x_raw * in_accel_scale = −8.683788575 m/s^2 133 + - Y-axis acceleration = in_accel_y_raw * in_accel_scale = 1.38273765 m/s^2 134 + - Z-axis acceleration = in_accel_z_raw * in_accel_scale = -7.467763975 m/s^2 135 + 136 + Set calibration offset for accelerometer channels: 137 + 138 + .. code-block:: bash 139 + 140 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_x_calibbias 141 + 0 142 + 143 + root:/sys/bus/iio/devices/iio:device0> echo 50 > in_accel_x_calibbias 144 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_x_calibbias 145 + 50 146 + 147 + Set sampling frequency: 148 + 149 + .. code-block:: bash 150 + 151 + root:/sys/bus/iio/devices/iio:device0> cat sampling_frequency 152 + 16000 153 + root:/sys/bus/iio/devices/iio:device0> cat sampling_frequency_available 154 + 16000 32000 64000 155 + 156 + root:/sys/bus/iio/devices/iio:device0> echo 32000 > sampling_frequency 157 + root:/sys/bus/iio/devices/iio:device0> cat sampling_frequency 158 + 32000 159 + 160 + Set low pass filter bandwidth for accelerometer channels: 161 + 162 + .. code-block:: bash 163 + 164 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_filter_low_pass_3db_frequency 165 + 32000 166 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_filter_low_pass_3db_frequency_available 167 + 32000 8000 4000 2000 168 + 169 + root:/sys/bus/iio/devices/iio:device0> echo 2000 > in_accel_filter_low_pass_3db_frequency 170 + root:/sys/bus/iio/devices/iio:device0> cat in_accel_filter_low_pass_3db_frequency 171 + 2000 172 + 173 + 3. Device buffers 174 + ================= 175 + 176 + This driver supports IIO buffers. 177 + 178 + All devices support retrieving the raw acceleration and temperature measurements 179 + using buffers. 180 + 181 + Usage examples 182 + -------------- 183 + 184 + Select channels for buffer read: 185 + 186 + .. code-block:: bash 187 + 188 + root:/sys/bus/iio/devices/iio:device0> echo 1 > scan_elements/in_accel_x_en 189 + root:/sys/bus/iio/devices/iio:device0> echo 1 > scan_elements/in_accel_y_en 190 + root:/sys/bus/iio/devices/iio:device0> echo 1 > scan_elements/in_accel_z_en 191 + root:/sys/bus/iio/devices/iio:device0> echo 1 > scan_elements/in_temp_en 192 + 193 + Set the number of samples to be stored in the buffer: 194 + 195 + .. code-block:: bash 196 + 197 + root:/sys/bus/iio/devices/iio:device0> echo 10 > buffer/length 198 + 199 + Enable buffer readings: 200 + 201 + .. code-block:: bash 202 + 203 + root:/sys/bus/iio/devices/iio:device0> echo 1 > buffer/enable 204 + 205 + Obtain buffered data: 206 + 207 + .. code-block:: bash 208 + 209 + root:/sys/bus/iio/devices/iio:device0> hexdump -C /dev/iio\:device0 210 + ... 211 + 002bc300 f7 e7 00 a8 fb c5 24 80 f7 e7 01 04 fb d6 24 80 |......$.......$.| 212 + 002bc310 f7 f9 00 ab fb dc 24 80 f7 c3 00 b8 fb e2 24 80 |......$.......$.| 213 + 002bc320 f7 fb 00 bb fb d1 24 80 f7 b1 00 5f fb d1 24 80 |......$...._..$.| 214 + 002bc330 f7 c4 00 c6 fb a6 24 80 f7 a6 00 68 fb f1 24 80 |......$....h..$.| 215 + 002bc340 f7 b8 00 a3 fb e7 24 80 f7 9a 00 b1 fb af 24 80 |......$.......$.| 216 + 002bc350 f7 b1 00 67 fb ee 24 80 f7 96 00 be fb 92 24 80 |...g..$.......$.| 217 + 002bc360 f7 ab 00 7a fc 1b 24 80 f7 b6 00 ae fb 76 24 80 |...z..$......v$.| 218 + 002bc370 f7 ce 00 a3 fc 02 24 80 f7 c0 00 be fb 8b 24 80 |......$.......$.| 219 + 002bc380 f7 c3 00 93 fb d0 24 80 f7 ce 00 d8 fb c8 24 80 |......$.......$.| 220 + 002bc390 f7 bd 00 c0 fb 82 24 80 f8 00 00 e8 fb db 24 80 |......$.......$.| 221 + 002bc3a0 f7 d8 00 d3 fb b4 24 80 f8 0b 00 e5 fb c3 24 80 |......$.......$.| 222 + 002bc3b0 f7 eb 00 c8 fb 92 24 80 f7 e7 00 ea fb cb 24 80 |......$.......$.| 223 + 002bc3c0 f7 fd 00 cb fb 94 24 80 f7 e3 00 f2 fb b8 24 80 |......$.......$.| 224 + ... 225 + 226 + See ``Documentation/iio/iio_devbuf.rst`` for more information about how buffered 227 + data is structured. 228 + 229 + 4. IIO Interfacing Tools 230 + ======================== 231 + 232 + See ``Documentation/iio/iio_tools.rst`` for the description of the available IIO 233 + interfacing tools.
+4
Documentation/iio/index.rst
··· 18 18 .. toctree:: 19 19 :maxdepth: 1 20 20 21 + ad4000 22 + ad4695 23 + ad7380 21 24 ad7944 22 25 adis16475 23 26 adis16480 27 + adxl380 24 28 bno055 25 29 ep93xx_adc
+83
Documentation/spi/spi-summary.rst
··· 614 614 already running). 615 615 616 616 617 + Extensions to the SPI protocol 618 + ------------------------------ 619 + The fact that SPI doesn't have a formal specification or standard permits chip 620 + manufacturers to implement the SPI protocol in slightly different ways. In most 621 + cases, SPI protocol implementations from different vendors are compatible among 622 + each other. For example, in SPI mode 0 (CPOL=0, CPHA=0) the bus lines may behave 623 + like the following: 624 + 625 + :: 626 + 627 + nCSx ___ ___ 628 + \_________________________________________________________________/ 629 + • • 630 + • • 631 + SCLK ___ ___ ___ ___ ___ ___ ___ ___ 632 + _______/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \_____ 633 + • : ; : ; : ; : ; : ; : ; : ; : ; • 634 + • : ; : ; : ; : ; : ; : ; : ; : ; • 635 + MOSI XXX__________ _______ _______ ________XXX 636 + 0xA5 XXX__/ 1 \_0_____/ 1 \_0_______0_____/ 1 \_0_____/ 1 \_XXX 637 + • ; ; ; ; ; ; ; ; • 638 + • ; ; ; ; ; ; ; ; • 639 + MISO XXX__________ _______________________ _______ XXX 640 + 0xBA XXX__/ 1 \_____0_/ 1 1 1 \_____0__/ 1 \____0__XXX 641 + 642 + Legend:: 643 + 644 + • marks the start/end of transmission; 645 + : marks when data is clocked into the peripheral; 646 + ; marks when data is clocked into the controller; 647 + X marks when line states are not specified. 648 + 649 + In some few cases, chips extend the SPI protocol by specifying line behaviors 650 + that other SPI protocols don't (e.g. data line state for when CS is not 651 + asserted). Those distinct SPI protocols, modes, and configurations are supported 652 + by different SPI mode flags. 653 + 654 + MOSI idle state configuration 655 + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 656 + 657 + Common SPI protocol implementations don't specify any state or behavior for the 658 + MOSI line when the controller is not clocking out data. However, there do exist 659 + peripherals that require specific MOSI line state when data is not being clocked 660 + out. For example, if the peripheral expects the MOSI line to be high when the 661 + controller is not clocking out data (``SPI_MOSI_IDLE_HIGH``), then a transfer in 662 + SPI mode 0 would look like the following: 663 + 664 + :: 665 + 666 + nCSx ___ ___ 667 + \_________________________________________________________________/ 668 + • • 669 + • • 670 + SCLK ___ ___ ___ ___ ___ ___ ___ ___ 671 + _______/ \___/ \___/ \___/ \___/ \___/ \___/ \___/ \_____ 672 + • : ; : ; : ; : ; : ; : ; : ; : ; • 673 + • : ; : ; : ; : ; : ; : ; : ; : ; • 674 + MOSI _____ _______ _______ _______________ ___ 675 + 0x56 \_0_____/ 1 \_0_____/ 1 \_0_____/ 1 1 \_0_____/ 676 + • ; ; ; ; ; ; ; ; • 677 + • ; ; ; ; ; ; ; ; • 678 + MISO XXX__________ _______________________ _______ XXX 679 + 0xBA XXX__/ 1 \_____0_/ 1 1 1 \_____0__/ 1 \____0__XXX 680 + 681 + Legend:: 682 + 683 + • marks the start/end of transmission; 684 + : marks when data is clocked into the peripheral; 685 + ; marks when data is clocked into the controller; 686 + X marks when line states are not specified. 687 + 688 + In this extension to the usual SPI protocol, the MOSI line state is specified to 689 + be kept high when CS is asserted but the controller is not clocking out data to 690 + the peripheral and also when CS is not asserted. 691 + 692 + Peripherals that require this extension must request it by setting the 693 + ``SPI_MOSI_IDLE_HIGH`` bit into the mode attribute of their ``struct 694 + spi_device`` and call spi_setup(). Controllers that support this extension 695 + should indicate it by setting ``SPI_MOSI_IDLE_HIGH`` in the mode_bits attribute 696 + of their ``struct spi_controller``. The configuration to idle MOSI low is 697 + analogous but uses the ``SPI_MOSI_IDLE_LOW`` mode bit. 698 + 699 + 617 700 THANKS TO 618 701 --------- 619 702 Contributors to Linux-SPI discussions include (in alphabetical order,
+73
MAINTAINERS
··· 448 448 W: https://wiki.analog.com/resources/tools-software/linux-drivers/iio-adc/ad738x 449 449 W: https://ez.analog.com/linux-software-drivers 450 450 F: Documentation/devicetree/bindings/iio/adc/adi,ad7380.yaml 451 + F: Documentation/iio/ad7380.rst 451 452 F: drivers/iio/adc/ad7380.c 452 453 453 454 AD7877 TOUCHSCREEN DRIVER ··· 619 618 F: drivers/iio/accel/adxl372.c 620 619 F: drivers/iio/accel/adxl372_i2c.c 621 620 F: drivers/iio/accel/adxl372_spi.c 621 + 622 + ADXL380 THREE-AXIS DIGITAL ACCELEROMETER DRIVER 623 + M: Ramona Gradinariu <ramona.gradinariu@analog.com> 624 + M: Antoniu Miclaus <antoniu.miclaus@analog.com> 625 + S: Supported 626 + W: https://ez.analog.com/linux-software-drivers 627 + F: Documentation/devicetree/bindings/iio/accel/adi,adxl380.yaml 628 + F: drivers/iio/accel/adxl380.c 629 + F: drivers/iio/accel/adxl380.h 630 + F: drivers/iio/accel/adxl380_i2c.c 631 + F: drivers/iio/accel/adxl380_spi.c 622 632 623 633 AF8133J THREE-AXIS MAGNETOMETER DRIVER 624 634 M: Ondřej Jirman <megi@xff.cz> ··· 1214 1202 F: Documentation/devicetree/bindings/iio/dac/adi,ad3552r.yaml 1215 1203 F: drivers/iio/dac/ad3552r.c 1216 1204 1205 + ANALOG DEVICES INC AD4000 DRIVER 1206 + M: Marcelo Schmitt <marcelo.schmitt@analog.com> 1207 + L: linux-iio@vger.kernel.org 1208 + S: Supported 1209 + W: https://ez.analog.com/linux-software-drivers 1210 + F: Documentation/devicetree/bindings/iio/adc/adi,ad4000.yaml 1211 + F: Documentation/iio/ad4000.rst 1212 + F: drivers/iio/adc/ad4000.c 1213 + 1217 1214 ANALOG DEVICES INC AD4130 DRIVER 1218 1215 M: Cosmin Tanislav <cosmin.tanislav@analog.com> 1219 1216 L: linux-iio@vger.kernel.org ··· 1231 1210 F: Documentation/ABI/testing/sysfs-bus-iio-adc-ad4130 1232 1211 F: Documentation/devicetree/bindings/iio/adc/adi,ad4130.yaml 1233 1212 F: drivers/iio/adc/ad4130.c 1213 + 1214 + ANALOG DEVICES INC AD4695 DRIVER 1215 + M: Michael Hennerich <michael.hennerich@analog.com> 1216 + M: Nuno Sá <nuno.sa@analog.com> 1217 + R: David Lechner <dlechner@baylibre.com> 1218 + L: linux-iio@vger.kernel.org 1219 + S: Supported 1220 + W: https://ez.analog.com/linux-software-drivers 1221 + F: Documentation/devicetree/bindings/iio/adc/adi,ad4695.yaml 1222 + F: Documentation/iio/ad4695.rst 1223 + F: drivers/iio/adc/ad4695.c 1224 + F: include/dt-bindings/iio/adi,ad4695.h 1234 1225 1235 1226 ANALOG DEVICES INC AD7091R DRIVER 1236 1227 M: Marcelo Schmitt <marcelo.schmitt@analog.com> ··· 1309 1276 W: https://ez.analog.com/linux-software-drivers 1310 1277 F: Documentation/devicetree/bindings/iio/adc/adi,ad7780.yaml 1311 1278 F: drivers/iio/adc/ad7780.c 1279 + 1280 + ANALOG DEVICES INC AD9467 DRIVER 1281 + M: Michael Hennerich <Michael.Hennerich@analog.com> 1282 + M: Nuno Sa <nuno.sa@analog.com> 1283 + L: linux-iio@vger.kernel.org 1284 + S: Supported 1285 + W: https://ez.analog.com/linux-software-drivers 1286 + F: Documentation/ABI/testing/debugfs-iio-ad9467 1287 + F: Documentation/devicetree/bindings/iio/adc/adi,ad9467.yaml 1288 + F: drivers/iio/adc/ad9467.c 1312 1289 1313 1290 ANALOG DEVICES INC AD9739a DRIVER 1314 1291 M: Nuno Sa <nuno.sa@analog.com> ··· 10904 10861 R: Olivier Moysan <olivier.moysan@foss.st.com> 10905 10862 L: linux-iio@vger.kernel.org 10906 10863 S: Maintained 10864 + F: Documentation/ABI/testing/debugfs-iio-backend 10907 10865 F: drivers/iio/industrialio-backend.c 10908 10866 F: include/linux/iio/backend.h 10909 10867 ··· 13305 13261 F: Documentation/devicetree/bindings/iio/dac/lltc,ltc1660.yaml 13306 13262 F: drivers/iio/dac/ltc1660.c 13307 13263 13264 + LTC2664 IIO DAC DRIVER 13265 + M: Michael Hennerich <michael.hennerich@analog.com> 13266 + M: Kim Seer Paller <kimseer.paller@analog.com> 13267 + L: linux-iio@vger.kernel.org 13268 + S: Supported 13269 + W: https://ez.analog.com/linux-software-drivers 13270 + F: Documentation/devicetree/bindings/iio/dac/adi,ltc2664.yaml 13271 + F: Documentation/devicetree/bindings/iio/dac/adi,ltc2672.yaml 13272 + F: drivers/iio/dac/ltc2664.c 13273 + 13308 13274 LTC2688 IIO DAC DRIVER 13309 13275 M: Nuno Sá <nuno.sa@analog.com> 13310 13276 L: linux-iio@vger.kernel.org ··· 15075 15021 F: Documentation/devicetree/bindings/nvmem/microchip,sama7g5-otpc.yaml 15076 15022 F: drivers/nvmem/microchip-otpc.c 15077 15023 F: include/dt-bindings/nvmem/microchip,sama7g5-otpc.h 15024 + 15025 + MICROCHIP PAC1921 POWER/CURRENT MONITOR DRIVER 15026 + M: Matteo Martelli <matteomartelli3@gmail.com> 15027 + L: linux-iio@vger.kernel.org 15028 + S: Supported 15029 + F: Documentation/devicetree/bindings/iio/adc/microchip,pac1921.yaml 15030 + F: drivers/iio/adc/pac1921.c 15078 15031 15079 15032 MICROCHIP PAC1934 POWER/ENERGY MONITOR DRIVER 15080 15033 M: Marius Cristea <marius.cristea@microchip.com> ··· 19828 19767 F: drivers/power/supply/bd99954-charger.c 19829 19768 F: drivers/power/supply/bd99954-charger.h 19830 19769 19770 + ROHM BH1745 COLOUR SENSOR 19771 + M: Mudit Sharma <muditsharma.info@gmail.com> 19772 + L: linux-iio@vger.kernel.org 19773 + S: Maintained 19774 + F: drivers/iio/light/bh1745.c 19775 + 19831 19776 ROHM BH1750 AMBIENT LIGHT SENSOR DRIVER 19832 19777 M: Tomasz Duszynski <tduszyns@gmail.com> 19833 19778 S: Maintained ··· 20648 20581 S: Maintained 20649 20582 F: Documentation/devicetree/bindings/iio/chemical/sensirion,scd4x.yaml 20650 20583 F: drivers/iio/chemical/scd4x.c 20584 + 20585 + SENSIRION SDP500 DIFFERENTIAL PRESSURE SENSOR DRIVER 20586 + M: Petar Stoykov <petar.stoykov@prodrive-technologies.com> 20587 + S: Maintained 20588 + F: Documentation/devicetree/bindings/iio/pressure/sensirion,sdp500.yaml 20589 + F: drivers/iio/pressure/sdp500.c 20651 20590 20652 20591 SENSIRION SGP40 GAS SENSOR DRIVER 20653 20592 M: Andreas Klinger <ak@it-klinger.de>
+27
drivers/iio/accel/Kconfig
··· 177 177 To compile this driver as a module, choose M here: the 178 178 module will be called adxl372_i2c. 179 179 180 + config ADXL380 181 + tristate 182 + select IIO_BUFFER 183 + select IIO_TRIGGERED_BUFFER 184 + 185 + config ADXL380_SPI 186 + tristate "Analog Devices ADXL380 3-Axis Accelerometer SPI Driver" 187 + depends on SPI 188 + select ADXL380 189 + select REGMAP_SPI 190 + help 191 + Say yes here to add support for the Analog Devices ADXL380 triaxial 192 + acceleration sensor. 193 + To compile this driver as a module, choose M here: the 194 + module will be called adxl380_spi. 195 + 196 + config ADXL380_I2C 197 + tristate "Analog Devices ADXL380 3-Axis Accelerometer I2C Driver" 198 + depends on I2C 199 + select ADXL380 200 + select REGMAP_I2C 201 + help 202 + Say yes here to add support for the Analog Devices ADXL380 triaxial 203 + acceleration sensor. 204 + To compile this driver as a module, choose M here: the 205 + module will be called adxl380_i2c. 206 + 180 207 config BMA180 181 208 tristate "Bosch BMA023/BMA1x0/BMA250 3-Axis Accelerometer Driver" 182 209 depends on I2C && INPUT_BMA150=n
+3
drivers/iio/accel/Makefile
··· 21 21 obj-$(CONFIG_ADXL372) += adxl372.o 22 22 obj-$(CONFIG_ADXL372_I2C) += adxl372_i2c.o 23 23 obj-$(CONFIG_ADXL372_SPI) += adxl372_spi.o 24 + obj-$(CONFIG_ADXL380) += adxl380.o 25 + obj-$(CONFIG_ADXL380_I2C) += adxl380_i2c.o 26 + obj-$(CONFIG_ADXL380_SPI) += adxl380_spi.o 24 27 obj-$(CONFIG_BMA180) += bma180.o 25 28 obj-$(CONFIG_BMA220) += bma220_spi.o 26 29 obj-$(CONFIG_BMA400) += bma400_core.o
+1 -1
drivers/iio/accel/adxl367.c
··· 1220 1220 return ret; 1221 1221 1222 1222 st->fifo_set_size = bitmap_weight(active_scan_mask, 1223 - indio_dev->masklength); 1223 + iio_get_masklength(indio_dev)); 1224 1224 1225 1225 return 0; 1226 1226 }
+1 -1
drivers/iio/accel/adxl367_spi.c
··· 72 72 return spi_sync(st->spi, &st->reg_write_msg); 73 73 } 74 74 75 - static struct regmap_bus adxl367_spi_regmap_bus = { 75 + static const struct regmap_bus adxl367_spi_regmap_bus = { 76 76 .read = adxl367_read, 77 77 .write = adxl367_write, 78 78 };
+1 -1
drivers/iio/accel/adxl372.c
··· 1050 1050 st->fifo_format = adxl372_axis_lookup_table[i].fifo_format; 1051 1051 st->fifo_axis_mask = adxl372_axis_lookup_table[i].bits; 1052 1052 st->fifo_set_size = bitmap_weight(indio_dev->active_scan_mask, 1053 - indio_dev->masklength); 1053 + iio_get_masklength(indio_dev)); 1054 1054 1055 1055 /* Configure the FIFO to store sets of impact event peak. */ 1056 1056 if (st->peak_fifo_mode_en) {
+1905
drivers/iio/accel/adxl380.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * ADXL380 3-Axis Digital Accelerometer core driver 4 + * 5 + * Copyright 2024 Analog Devices Inc. 6 + */ 7 + 8 + #include <linux/bitfield.h> 9 + #include <linux/interrupt.h> 10 + #include <linux/irq.h> 11 + #include <linux/module.h> 12 + #include <linux/property.h> 13 + #include <linux/regmap.h> 14 + #include <linux/units.h> 15 + 16 + #include <asm/unaligned.h> 17 + 18 + #include <linux/iio/buffer.h> 19 + #include <linux/iio/events.h> 20 + #include <linux/iio/iio.h> 21 + #include <linux/iio/kfifo_buf.h> 22 + #include <linux/iio/sysfs.h> 23 + 24 + #include <linux/regulator/consumer.h> 25 + 26 + #include "adxl380.h" 27 + 28 + #define ADXL380_ID_VAL 380 29 + #define ADXL382_ID_VAL 382 30 + 31 + #define ADXL380_DEVID_AD_REG 0x00 32 + #define ADLX380_PART_ID_REG 0x02 33 + 34 + #define ADXL380_X_DATA_H_REG 0x15 35 + #define ADXL380_Y_DATA_H_REG 0x17 36 + #define ADXL380_Z_DATA_H_REG 0x19 37 + #define ADXL380_T_DATA_H_REG 0x1B 38 + 39 + #define ADXL380_MISC_0_REG 0x20 40 + #define ADXL380_XL382_MSK BIT(7) 41 + 42 + #define ADXL380_MISC_1_REG 0x21 43 + 44 + #define ADXL380_X_DSM_OFFSET_REG 0x4D 45 + 46 + #define ADXL380_ACT_INACT_CTL_REG 0x37 47 + #define ADXL380_INACT_EN_MSK BIT(2) 48 + #define ADXL380_ACT_EN_MSK BIT(0) 49 + 50 + #define ADXL380_SNSR_AXIS_EN_REG 0x38 51 + #define ADXL380_ACT_INACT_AXIS_EN_MSK GENMASK(2, 0) 52 + 53 + #define ADXL380_THRESH_ACT_H_REG 0x39 54 + #define ADXL380_TIME_ACT_H_REG 0x3B 55 + #define ADXL380_THRESH_INACT_H_REG 0x3E 56 + #define ADXL380_TIME_INACT_H_REG 0x40 57 + #define ADXL380_THRESH_MAX GENMASK(12, 0) 58 + #define ADXL380_TIME_MAX GENMASK(24, 0) 59 + 60 + #define ADXL380_FIFO_CONFIG_0_REG 0x30 61 + #define ADXL380_FIFO_SAMPLES_8_MSK BIT(0) 62 + #define ADXL380_FIFO_MODE_MSK GENMASK(5, 4) 63 + 64 + #define ADXL380_FIFO_DISABLED 0 65 + #define ADXL380_FIFO_NORMAL 1 66 + #define ADXL380_FIFO_STREAMED 2 67 + #define ADXL380_FIFO_TRIGGERED 3 68 + 69 + #define ADXL380_FIFO_CONFIG_1_REG 0x31 70 + #define ADXL380_FIFO_STATUS_0_REG 0x1E 71 + 72 + #define ADXL380_TAP_THRESH_REG 0x43 73 + #define ADXL380_TAP_DUR_REG 0x44 74 + #define ADXL380_TAP_LATENT_REG 0x45 75 + #define ADXL380_TAP_WINDOW_REG 0x46 76 + #define ADXL380_TAP_TIME_MAX GENMASK(7, 0) 77 + 78 + #define ADXL380_TAP_CFG_REG 0x47 79 + #define ADXL380_TAP_AXIS_MSK GENMASK(1, 0) 80 + 81 + #define ADXL380_TRIG_CFG_REG 0x49 82 + #define ADXL380_TRIG_CFG_DEC_2X_MSK BIT(7) 83 + #define ADXL380_TRIG_CFG_SINC_RATE_MSK BIT(6) 84 + 85 + #define ADXL380_FILTER_REG 0x50 86 + #define ADXL380_FILTER_EQ_FILT_MSK BIT(6) 87 + #define ADXL380_FILTER_LPF_MODE_MSK GENMASK(5, 4) 88 + #define ADXL380_FILTER_HPF_PATH_MSK BIT(3) 89 + #define ADXL380_FILTER_HPF_CORNER_MSK GENMASK(2, 0) 90 + 91 + #define ADXL380_OP_MODE_REG 0x26 92 + #define ADXL380_OP_MODE_RANGE_MSK GENMASK(7, 6) 93 + #define ADXL380_OP_MODE_MSK GENMASK(3, 0) 94 + #define ADXL380_OP_MODE_STANDBY 0 95 + #define ADXL380_OP_MODE_HEART_SOUND 1 96 + #define ADXL380_OP_MODE_ULP 2 97 + #define ADXL380_OP_MODE_VLP 3 98 + #define ADXL380_OP_MODE_LP 4 99 + #define ADXL380_OP_MODE_LP_ULP 6 100 + #define ADXL380_OP_MODE_LP_VLP 7 101 + #define ADXL380_OP_MODE_RBW 8 102 + #define ADXL380_OP_MODE_RBW_ULP 10 103 + #define ADXL380_OP_MODE_RBW_VLP 11 104 + #define ADXL380_OP_MODE_HP 12 105 + #define ADXL380_OP_MODE_HP_ULP 14 106 + #define ADXL380_OP_MODE_HP_VLP 15 107 + 108 + #define ADXL380_OP_MODE_4G_RANGE 0 109 + #define ADXL382_OP_MODE_15G_RANGE 0 110 + #define ADXL380_OP_MODE_8G_RANGE 1 111 + #define ADXL382_OP_MODE_30G_RANGE 1 112 + #define ADXL380_OP_MODE_16G_RANGE 2 113 + #define ADXL382_OP_MODE_60G_RANGE 2 114 + 115 + #define ADXL380_DIG_EN_REG 0x27 116 + #define ADXL380_CHAN_EN_MSK(chan) BIT(4 + (chan)) 117 + #define ADXL380_FIFO_EN_MSK BIT(3) 118 + 119 + #define ADXL380_INT0_MAP0_REG 0x2B 120 + #define ADXL380_INT1_MAP0_REG 0x2D 121 + #define ADXL380_INT_MAP0_INACT_INT0_MSK BIT(6) 122 + #define ADXL380_INT_MAP0_ACT_INT0_MSK BIT(5) 123 + #define ADXL380_INT_MAP0_FIFO_WM_INT0_MSK BIT(3) 124 + 125 + #define ADXL380_INT0_MAP1_REG 0x2C 126 + #define ADXL380_INT1_MAP1_REG 0x2E 127 + #define ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK BIT(1) 128 + #define ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK BIT(0) 129 + 130 + #define ADXL380_INT0_REG 0x5D 131 + #define ADXL380_INT0_POL_MSK BIT(7) 132 + 133 + #define ADXL380_RESET_REG 0x2A 134 + #define ADXL380_FIFO_DATA 0x1D 135 + 136 + #define ADXL380_DEVID_AD_VAL 0xAD 137 + #define ADXL380_RESET_CODE 0x52 138 + 139 + #define ADXL380_STATUS_0_REG 0x11 140 + #define ADXL380_STATUS_0_FIFO_FULL_MSK BIT(1) 141 + #define ADXL380_STATUS_0_FIFO_WM_MSK BIT(3) 142 + 143 + #define ADXL380_STATUS_1_INACT_MSK BIT(6) 144 + #define ADXL380_STATUS_1_ACT_MSK BIT(5) 145 + #define ADXL380_STATUS_1_DOUBLE_TAP_MSK BIT(1) 146 + #define ADXL380_STATUS_1_SINGLE_TAP_MSK BIT(0) 147 + 148 + #define ADXL380_FIFO_SAMPLES 315UL 149 + 150 + enum adxl380_channels { 151 + ADXL380_ACCEL_X, 152 + ADXL380_ACCEL_Y, 153 + ADXL380_ACCEL_Z, 154 + ADXL380_TEMP, 155 + ADXL380_CH_NUM 156 + }; 157 + 158 + enum adxl380_axis { 159 + ADXL380_X_AXIS, 160 + ADXL380_Y_AXIS, 161 + ADXL380_Z_AXIS, 162 + }; 163 + 164 + enum adxl380_activity_type { 165 + ADXL380_ACTIVITY, 166 + ADXL380_INACTIVITY, 167 + }; 168 + 169 + enum adxl380_tap_type { 170 + ADXL380_SINGLE_TAP, 171 + ADXL380_DOUBLE_TAP, 172 + }; 173 + 174 + enum adxl380_tap_time_type { 175 + ADXL380_TAP_TIME_LATENT, 176 + ADXL380_TAP_TIME_WINDOW, 177 + }; 178 + 179 + static const int adxl380_range_scale_factor_tbl[] = { 1, 2, 4 }; 180 + 181 + const struct adxl380_chip_info adxl380_chip_info = { 182 + .name = "adxl380", 183 + .chip_id = ADXL380_ID_VAL, 184 + .scale_tbl = { 185 + [ADXL380_OP_MODE_4G_RANGE] = { 0, 1307226 }, 186 + [ADXL380_OP_MODE_8G_RANGE] = { 0, 2615434 }, 187 + [ADXL380_OP_MODE_16G_RANGE] = { 0, 5229886 }, 188 + }, 189 + .samp_freq_tbl = { 8000, 16000, 32000 }, 190 + /* 191 + * The datasheet defines an intercept of 470 LSB at 25 degC 192 + * and a sensitivity of 10.2 LSB/C. 193 + */ 194 + .temp_offset = 25 * 102 / 10 - 470, 195 + 196 + }; 197 + EXPORT_SYMBOL_NS_GPL(adxl380_chip_info, IIO_ADXL380); 198 + 199 + const struct adxl380_chip_info adxl382_chip_info = { 200 + .name = "adxl382", 201 + .chip_id = ADXL382_ID_VAL, 202 + .scale_tbl = { 203 + [ADXL382_OP_MODE_15G_RANGE] = { 0, 4903325 }, 204 + [ADXL382_OP_MODE_30G_RANGE] = { 0, 9806650 }, 205 + [ADXL382_OP_MODE_60G_RANGE] = { 0, 19613300 }, 206 + }, 207 + .samp_freq_tbl = { 16000, 32000, 64000 }, 208 + /* 209 + * The datasheet defines an intercept of 570 LSB at 25 degC 210 + * and a sensitivity of 10.2 LSB/C. 211 + */ 212 + .temp_offset = 25 * 102 / 10 - 570, 213 + }; 214 + EXPORT_SYMBOL_NS_GPL(adxl382_chip_info, IIO_ADXL380); 215 + 216 + static const unsigned int adxl380_th_reg_high_addr[2] = { 217 + [ADXL380_ACTIVITY] = ADXL380_THRESH_ACT_H_REG, 218 + [ADXL380_INACTIVITY] = ADXL380_THRESH_INACT_H_REG, 219 + }; 220 + 221 + static const unsigned int adxl380_time_reg_high_addr[2] = { 222 + [ADXL380_ACTIVITY] = ADXL380_TIME_ACT_H_REG, 223 + [ADXL380_INACTIVITY] = ADXL380_TIME_INACT_H_REG, 224 + }; 225 + 226 + static const unsigned int adxl380_tap_time_reg[2] = { 227 + [ADXL380_TAP_TIME_LATENT] = ADXL380_TAP_LATENT_REG, 228 + [ADXL380_TAP_TIME_WINDOW] = ADXL380_TAP_WINDOW_REG, 229 + }; 230 + 231 + struct adxl380_state { 232 + struct regmap *regmap; 233 + struct device *dev; 234 + const struct adxl380_chip_info *chip_info; 235 + /* 236 + * Synchronize access to members of driver state, and ensure atomicity 237 + * of consecutive regmap operations. 238 + */ 239 + struct mutex lock; 240 + enum adxl380_axis tap_axis_en; 241 + u8 range; 242 + u8 odr; 243 + u8 fifo_set_size; 244 + u8 transf_buf[3]; 245 + u16 watermark; 246 + u32 act_time_ms; 247 + u32 act_threshold; 248 + u32 inact_time_ms; 249 + u32 inact_threshold; 250 + u32 tap_latent_us; 251 + u32 tap_window_us; 252 + u32 tap_duration_us; 253 + u32 tap_threshold; 254 + int irq; 255 + int int_map[2]; 256 + int lpf_tbl[4]; 257 + int hpf_tbl[7][2]; 258 + 259 + __be16 fifo_buf[ADXL380_FIFO_SAMPLES] __aligned(IIO_DMA_MINALIGN); 260 + }; 261 + 262 + bool adxl380_readable_noinc_reg(struct device *dev, unsigned int reg) 263 + { 264 + return reg == ADXL380_FIFO_DATA; 265 + } 266 + EXPORT_SYMBOL_NS_GPL(adxl380_readable_noinc_reg, IIO_ADXL380); 267 + 268 + static int adxl380_set_measure_en(struct adxl380_state *st, bool en) 269 + { 270 + int ret; 271 + unsigned int act_inact_ctl; 272 + u8 op_mode = ADXL380_OP_MODE_STANDBY; 273 + 274 + if (en) { 275 + ret = regmap_read(st->regmap, ADXL380_ACT_INACT_CTL_REG, &act_inact_ctl); 276 + if (ret) 277 + return ret; 278 + 279 + /* Activity/ Inactivity detection available only in VLP/ULP mode */ 280 + if (FIELD_GET(ADXL380_ACT_EN_MSK, act_inact_ctl) || 281 + FIELD_GET(ADXL380_INACT_EN_MSK, act_inact_ctl)) 282 + op_mode = ADXL380_OP_MODE_VLP; 283 + else 284 + op_mode = ADXL380_OP_MODE_HP; 285 + } 286 + 287 + return regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG, 288 + ADXL380_OP_MODE_MSK, 289 + FIELD_PREP(ADXL380_OP_MODE_MSK, op_mode)); 290 + } 291 + 292 + static void adxl380_scale_act_inact_thresholds(struct adxl380_state *st, 293 + u8 old_range, 294 + u8 new_range) 295 + { 296 + st->act_threshold = mult_frac(st->act_threshold, 297 + adxl380_range_scale_factor_tbl[old_range], 298 + adxl380_range_scale_factor_tbl[new_range]); 299 + st->inact_threshold = mult_frac(st->inact_threshold, 300 + adxl380_range_scale_factor_tbl[old_range], 301 + adxl380_range_scale_factor_tbl[new_range]); 302 + } 303 + 304 + static int adxl380_write_act_inact_threshold(struct adxl380_state *st, 305 + enum adxl380_activity_type act, 306 + unsigned int th) 307 + { 308 + int ret; 309 + u8 reg = adxl380_th_reg_high_addr[act]; 310 + 311 + if (th > ADXL380_THRESH_MAX) 312 + return -EINVAL; 313 + 314 + ret = regmap_write(st->regmap, reg + 1, th & GENMASK(7, 0)); 315 + if (ret) 316 + return ret; 317 + 318 + ret = regmap_update_bits(st->regmap, reg, GENMASK(2, 0), th >> 8); 319 + if (ret) 320 + return ret; 321 + 322 + if (act == ADXL380_ACTIVITY) 323 + st->act_threshold = th; 324 + else 325 + st->inact_threshold = th; 326 + 327 + return 0; 328 + } 329 + 330 + static int adxl380_set_act_inact_threshold(struct iio_dev *indio_dev, 331 + enum adxl380_activity_type act, 332 + u16 th) 333 + { 334 + struct adxl380_state *st = iio_priv(indio_dev); 335 + int ret; 336 + 337 + guard(mutex)(&st->lock); 338 + 339 + ret = adxl380_set_measure_en(st, false); 340 + if (ret) 341 + return ret; 342 + 343 + ret = adxl380_write_act_inact_threshold(st, act, th); 344 + if (ret) 345 + return ret; 346 + 347 + return adxl380_set_measure_en(st, true); 348 + } 349 + 350 + static int adxl380_set_tap_threshold_value(struct iio_dev *indio_dev, u8 th) 351 + { 352 + int ret; 353 + struct adxl380_state *st = iio_priv(indio_dev); 354 + 355 + guard(mutex)(&st->lock); 356 + 357 + ret = adxl380_set_measure_en(st, false); 358 + if (ret) 359 + return ret; 360 + 361 + ret = regmap_write(st->regmap, ADXL380_TAP_THRESH_REG, th); 362 + if (ret) 363 + return ret; 364 + 365 + st->tap_threshold = th; 366 + 367 + return adxl380_set_measure_en(st, true); 368 + } 369 + 370 + static int _adxl380_write_tap_time_us(struct adxl380_state *st, 371 + enum adxl380_tap_time_type tap_time_type, 372 + u32 us) 373 + { 374 + u8 reg = adxl380_tap_time_reg[tap_time_type]; 375 + unsigned int reg_val; 376 + int ret; 377 + 378 + /* scale factor for tap window is 1250us / LSB */ 379 + reg_val = DIV_ROUND_CLOSEST(us, 1250); 380 + if (reg_val > ADXL380_TAP_TIME_MAX) 381 + reg_val = ADXL380_TAP_TIME_MAX; 382 + 383 + ret = regmap_write(st->regmap, reg, reg_val); 384 + if (ret) 385 + return ret; 386 + 387 + if (tap_time_type == ADXL380_TAP_TIME_WINDOW) 388 + st->tap_window_us = us; 389 + else 390 + st->tap_latent_us = us; 391 + 392 + return 0; 393 + } 394 + 395 + static int adxl380_write_tap_time_us(struct adxl380_state *st, 396 + enum adxl380_tap_time_type tap_time_type, u32 us) 397 + { 398 + int ret; 399 + 400 + guard(mutex)(&st->lock); 401 + 402 + ret = adxl380_set_measure_en(st, false); 403 + if (ret) 404 + return ret; 405 + 406 + ret = _adxl380_write_tap_time_us(st, tap_time_type, us); 407 + if (ret) 408 + return ret; 409 + 410 + return adxl380_set_measure_en(st, true); 411 + } 412 + 413 + static int adxl380_write_tap_dur_us(struct iio_dev *indio_dev, u32 us) 414 + { 415 + int ret; 416 + unsigned int reg_val; 417 + struct adxl380_state *st = iio_priv(indio_dev); 418 + 419 + /* 625us per code is the scale factor of TAP_DUR register */ 420 + reg_val = DIV_ROUND_CLOSEST(us, 625); 421 + 422 + ret = adxl380_set_measure_en(st, false); 423 + if (ret) 424 + return ret; 425 + 426 + ret = regmap_write(st->regmap, ADXL380_TAP_DUR_REG, reg_val); 427 + if (ret) 428 + return ret; 429 + 430 + return adxl380_set_measure_en(st, true); 431 + } 432 + 433 + static int adxl380_read_chn(struct adxl380_state *st, u8 addr) 434 + { 435 + int ret; 436 + 437 + guard(mutex)(&st->lock); 438 + 439 + ret = regmap_bulk_read(st->regmap, addr, &st->transf_buf, 2); 440 + if (ret) 441 + return ret; 442 + 443 + return get_unaligned_be16(st->transf_buf); 444 + } 445 + 446 + static int adxl380_get_odr(struct adxl380_state *st, int *odr) 447 + { 448 + int ret; 449 + unsigned int trig_cfg, odr_idx; 450 + 451 + ret = regmap_read(st->regmap, ADXL380_TRIG_CFG_REG, &trig_cfg); 452 + if (ret) 453 + return ret; 454 + 455 + odr_idx = (FIELD_GET(ADXL380_TRIG_CFG_SINC_RATE_MSK, trig_cfg) << 1) | 456 + (FIELD_GET(ADXL380_TRIG_CFG_DEC_2X_MSK, trig_cfg) & 1); 457 + 458 + *odr = st->chip_info->samp_freq_tbl[odr_idx]; 459 + 460 + return 0; 461 + } 462 + 463 + static const int adxl380_lpf_div[] = { 464 + 1, 4, 8, 16, 465 + }; 466 + 467 + static int adxl380_fill_lpf_tbl(struct adxl380_state *st) 468 + { 469 + int ret, i; 470 + int odr; 471 + 472 + ret = adxl380_get_odr(st, &odr); 473 + if (ret) 474 + return ret; 475 + 476 + for (i = 0; i < ARRAY_SIZE(st->lpf_tbl); i++) 477 + st->lpf_tbl[i] = DIV_ROUND_CLOSEST(odr, adxl380_lpf_div[i]); 478 + 479 + return 0; 480 + } 481 + 482 + static const int adxl380_hpf_mul[] = { 483 + 0, 247000, 62084, 15545, 3862, 954, 238, 484 + }; 485 + 486 + static int adxl380_fill_hpf_tbl(struct adxl380_state *st) 487 + { 488 + int i, ret, odr_hz; 489 + u32 multiplier; 490 + u64 div, rem, odr; 491 + 492 + ret = adxl380_get_odr(st, &odr_hz); 493 + if (ret) 494 + return ret; 495 + 496 + for (i = 0; i < ARRAY_SIZE(adxl380_hpf_mul); i++) { 497 + odr = mul_u64_u32_shr(odr_hz, MEGA, 0); 498 + multiplier = adxl380_hpf_mul[i]; 499 + div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0), 500 + TERA * 100, &rem); 501 + 502 + st->hpf_tbl[i][0] = div; 503 + st->hpf_tbl[i][1] = div_u64(rem, MEGA * 100); 504 + } 505 + 506 + return 0; 507 + } 508 + 509 + static int adxl380_set_odr(struct adxl380_state *st, u8 odr) 510 + { 511 + int ret; 512 + 513 + guard(mutex)(&st->lock); 514 + 515 + ret = adxl380_set_measure_en(st, false); 516 + if (ret) 517 + return ret; 518 + 519 + ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG, 520 + ADXL380_TRIG_CFG_DEC_2X_MSK, 521 + FIELD_PREP(ADXL380_TRIG_CFG_DEC_2X_MSK, odr & 1)); 522 + if (ret) 523 + return ret; 524 + 525 + ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG, 526 + ADXL380_TRIG_CFG_SINC_RATE_MSK, 527 + FIELD_PREP(ADXL380_TRIG_CFG_SINC_RATE_MSK, odr >> 1)); 528 + if (ret) 529 + return ret; 530 + 531 + ret = adxl380_set_measure_en(st, true); 532 + if (ret) 533 + return ret; 534 + 535 + ret = adxl380_fill_lpf_tbl(st); 536 + if (ret) 537 + return ret; 538 + 539 + return adxl380_fill_hpf_tbl(st); 540 + } 541 + 542 + static int adxl380_find_match_1d_tbl(const int *array, unsigned int size, 543 + int val) 544 + { 545 + int i; 546 + 547 + for (i = 0; i < size; i++) { 548 + if (val == array[i]) 549 + return i; 550 + } 551 + 552 + return size - 1; 553 + } 554 + 555 + static int adxl380_find_match_2d_tbl(const int (*freq_tbl)[2], int n, int val, int val2) 556 + { 557 + int i; 558 + 559 + for (i = 0; i < n; i++) { 560 + if (freq_tbl[i][0] == val && freq_tbl[i][1] == val2) 561 + return i; 562 + } 563 + 564 + return -EINVAL; 565 + } 566 + 567 + static int adxl380_get_lpf(struct adxl380_state *st, int *lpf) 568 + { 569 + int ret; 570 + unsigned int trig_cfg, lpf_idx; 571 + 572 + guard(mutex)(&st->lock); 573 + 574 + ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg); 575 + if (ret) 576 + return ret; 577 + 578 + lpf_idx = FIELD_GET(ADXL380_FILTER_LPF_MODE_MSK, trig_cfg); 579 + 580 + *lpf = st->lpf_tbl[lpf_idx]; 581 + 582 + return 0; 583 + } 584 + 585 + static int adxl380_set_lpf(struct adxl380_state *st, u8 lpf) 586 + { 587 + int ret; 588 + u8 eq_bypass = 0; 589 + 590 + guard(mutex)(&st->lock); 591 + 592 + ret = adxl380_set_measure_en(st, false); 593 + if (ret) 594 + return ret; 595 + 596 + if (lpf) 597 + eq_bypass = 1; 598 + 599 + ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG, 600 + ADXL380_FILTER_EQ_FILT_MSK, 601 + FIELD_PREP(ADXL380_FILTER_EQ_FILT_MSK, eq_bypass)); 602 + if (ret) 603 + return ret; 604 + 605 + ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG, 606 + ADXL380_FILTER_LPF_MODE_MSK, 607 + FIELD_PREP(ADXL380_FILTER_LPF_MODE_MSK, lpf)); 608 + if (ret) 609 + return ret; 610 + 611 + return adxl380_set_measure_en(st, true); 612 + } 613 + 614 + static int adxl380_get_hpf(struct adxl380_state *st, int *hpf_int, int *hpf_frac) 615 + { 616 + int ret; 617 + unsigned int trig_cfg, hpf_idx; 618 + 619 + guard(mutex)(&st->lock); 620 + 621 + ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg); 622 + if (ret) 623 + return ret; 624 + 625 + hpf_idx = FIELD_GET(ADXL380_FILTER_HPF_CORNER_MSK, trig_cfg); 626 + 627 + *hpf_int = st->hpf_tbl[hpf_idx][0]; 628 + *hpf_frac = st->hpf_tbl[hpf_idx][1]; 629 + 630 + return 0; 631 + } 632 + 633 + static int adxl380_set_hpf(struct adxl380_state *st, u8 hpf) 634 + { 635 + int ret; 636 + u8 hpf_path = 0; 637 + 638 + guard(mutex)(&st->lock); 639 + 640 + ret = adxl380_set_measure_en(st, false); 641 + if (ret) 642 + return ret; 643 + 644 + if (hpf) 645 + hpf_path = 1; 646 + 647 + ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG, 648 + ADXL380_FILTER_HPF_PATH_MSK, 649 + FIELD_PREP(ADXL380_FILTER_HPF_PATH_MSK, hpf_path)); 650 + if (ret) 651 + return ret; 652 + 653 + ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG, 654 + ADXL380_FILTER_HPF_CORNER_MSK, 655 + FIELD_PREP(ADXL380_FILTER_HPF_CORNER_MSK, hpf)); 656 + if (ret) 657 + return ret; 658 + 659 + return adxl380_set_measure_en(st, true); 660 + } 661 + 662 + static int _adxl380_set_act_inact_time_ms(struct adxl380_state *st, 663 + enum adxl380_activity_type act, 664 + u32 ms) 665 + { 666 + u8 reg = adxl380_time_reg_high_addr[act]; 667 + unsigned int reg_val; 668 + int ret; 669 + 670 + /* 500us per code is the scale factor of TIME_ACT / TIME_INACT registers */ 671 + reg_val = min(DIV_ROUND_CLOSEST(ms * 1000, 500), ADXL380_TIME_MAX); 672 + 673 + put_unaligned_be24(reg_val, &st->transf_buf[0]); 674 + 675 + ret = regmap_bulk_write(st->regmap, reg, st->transf_buf, sizeof(st->transf_buf)); 676 + if (ret) 677 + return ret; 678 + 679 + if (act == ADXL380_ACTIVITY) 680 + st->act_time_ms = ms; 681 + else 682 + st->inact_time_ms = ms; 683 + 684 + return 0; 685 + } 686 + 687 + static int adxl380_set_act_inact_time_ms(struct adxl380_state *st, 688 + enum adxl380_activity_type act, 689 + u32 ms) 690 + { 691 + int ret; 692 + 693 + guard(mutex)(&st->lock); 694 + 695 + ret = adxl380_set_measure_en(st, false); 696 + if (ret) 697 + return ret; 698 + 699 + ret = _adxl380_set_act_inact_time_ms(st, act, ms); 700 + if (ret) 701 + return ret; 702 + 703 + return adxl380_set_measure_en(st, true); 704 + } 705 + 706 + static int adxl380_set_range(struct adxl380_state *st, u8 range) 707 + { 708 + int ret; 709 + 710 + guard(mutex)(&st->lock); 711 + 712 + ret = adxl380_set_measure_en(st, false); 713 + if (ret) 714 + return ret; 715 + 716 + ret = regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG, 717 + ADXL380_OP_MODE_RANGE_MSK, 718 + FIELD_PREP(ADXL380_OP_MODE_RANGE_MSK, range)); 719 + 720 + if (ret) 721 + return ret; 722 + 723 + adxl380_scale_act_inact_thresholds(st, st->range, range); 724 + 725 + /* Activity thresholds depend on range */ 726 + ret = adxl380_write_act_inact_threshold(st, ADXL380_ACTIVITY, 727 + st->act_threshold); 728 + if (ret) 729 + return ret; 730 + 731 + ret = adxl380_write_act_inact_threshold(st, ADXL380_INACTIVITY, 732 + st->inact_threshold); 733 + if (ret) 734 + return ret; 735 + 736 + st->range = range; 737 + 738 + return adxl380_set_measure_en(st, true); 739 + } 740 + 741 + static int adxl380_write_act_inact_en(struct adxl380_state *st, 742 + enum adxl380_activity_type type, 743 + bool en) 744 + { 745 + if (type == ADXL380_ACTIVITY) 746 + return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG, 747 + ADXL380_ACT_EN_MSK, 748 + FIELD_PREP(ADXL380_ACT_EN_MSK, en)); 749 + 750 + return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG, 751 + ADXL380_INACT_EN_MSK, 752 + FIELD_PREP(ADXL380_INACT_EN_MSK, en)); 753 + } 754 + 755 + static int adxl380_read_act_inact_int(struct adxl380_state *st, 756 + enum adxl380_activity_type type, 757 + bool *en) 758 + { 759 + int ret; 760 + unsigned int reg_val; 761 + 762 + guard(mutex)(&st->lock); 763 + 764 + ret = regmap_read(st->regmap, st->int_map[0], &reg_val); 765 + if (ret) 766 + return ret; 767 + 768 + if (type == ADXL380_ACTIVITY) 769 + *en = FIELD_GET(ADXL380_INT_MAP0_ACT_INT0_MSK, reg_val); 770 + else 771 + *en = FIELD_GET(ADXL380_INT_MAP0_INACT_INT0_MSK, reg_val); 772 + 773 + return 0; 774 + } 775 + 776 + static int adxl380_write_act_inact_int(struct adxl380_state *st, 777 + enum adxl380_activity_type act, 778 + bool en) 779 + { 780 + if (act == ADXL380_ACTIVITY) 781 + return regmap_update_bits(st->regmap, st->int_map[0], 782 + ADXL380_INT_MAP0_ACT_INT0_MSK, 783 + FIELD_PREP(ADXL380_INT_MAP0_ACT_INT0_MSK, en)); 784 + 785 + return regmap_update_bits(st->regmap, st->int_map[0], 786 + ADXL380_INT_MAP0_INACT_INT0_MSK, 787 + FIELD_PREP(ADXL380_INT_MAP0_INACT_INT0_MSK, en)); 788 + } 789 + 790 + static int adxl380_act_inact_config(struct adxl380_state *st, 791 + enum adxl380_activity_type type, 792 + bool en) 793 + { 794 + int ret; 795 + 796 + guard(mutex)(&st->lock); 797 + 798 + ret = adxl380_set_measure_en(st, false); 799 + if (ret) 800 + return ret; 801 + 802 + ret = adxl380_write_act_inact_en(st, type, en); 803 + if (ret) 804 + return ret; 805 + 806 + ret = adxl380_write_act_inact_int(st, type, en); 807 + if (ret) 808 + return ret; 809 + 810 + return adxl380_set_measure_en(st, true); 811 + } 812 + 813 + static int adxl380_write_tap_axis(struct adxl380_state *st, 814 + enum adxl380_axis axis) 815 + { 816 + int ret; 817 + 818 + ret = regmap_update_bits(st->regmap, ADXL380_TAP_CFG_REG, 819 + ADXL380_TAP_AXIS_MSK, 820 + FIELD_PREP(ADXL380_TAP_AXIS_MSK, axis)); 821 + 822 + if (ret) 823 + return ret; 824 + 825 + st->tap_axis_en = axis; 826 + 827 + return 0; 828 + } 829 + 830 + static int adxl380_read_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool *en) 831 + { 832 + int ret; 833 + unsigned int reg_val; 834 + 835 + ret = regmap_read(st->regmap, st->int_map[1], &reg_val); 836 + if (ret) 837 + return ret; 838 + 839 + if (type == ADXL380_SINGLE_TAP) 840 + *en = FIELD_GET(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, reg_val); 841 + else 842 + *en = FIELD_GET(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, reg_val); 843 + 844 + return 0; 845 + } 846 + 847 + static int adxl380_write_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool en) 848 + { 849 + if (type == ADXL380_SINGLE_TAP) 850 + return regmap_update_bits(st->regmap, st->int_map[1], 851 + ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, 852 + FIELD_PREP(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, en)); 853 + 854 + return regmap_update_bits(st->regmap, st->int_map[1], 855 + ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, 856 + FIELD_PREP(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, en)); 857 + } 858 + 859 + static int adxl380_tap_config(struct adxl380_state *st, 860 + enum adxl380_axis axis, 861 + enum adxl380_tap_type type, 862 + bool en) 863 + { 864 + int ret; 865 + 866 + guard(mutex)(&st->lock); 867 + 868 + ret = adxl380_set_measure_en(st, false); 869 + if (ret) 870 + return ret; 871 + 872 + ret = adxl380_write_tap_axis(st, axis); 873 + if (ret) 874 + return ret; 875 + 876 + ret = adxl380_write_tap_int(st, type, en); 877 + if (ret) 878 + return ret; 879 + 880 + return adxl380_set_measure_en(st, true); 881 + } 882 + 883 + static int adxl380_set_fifo_samples(struct adxl380_state *st) 884 + { 885 + int ret; 886 + u16 fifo_samples = st->watermark * st->fifo_set_size; 887 + 888 + ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG, 889 + ADXL380_FIFO_SAMPLES_8_MSK, 890 + FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK, 891 + (fifo_samples & BIT(8)))); 892 + if (ret) 893 + return ret; 894 + 895 + return regmap_write(st->regmap, ADXL380_FIFO_CONFIG_1_REG, 896 + fifo_samples & 0xFF); 897 + } 898 + 899 + static int adxl380_get_status(struct adxl380_state *st, u8 *status0, u8 *status1) 900 + { 901 + int ret; 902 + 903 + /* STATUS0, STATUS1 are adjacent regs */ 904 + ret = regmap_bulk_read(st->regmap, ADXL380_STATUS_0_REG, 905 + &st->transf_buf, 2); 906 + if (ret) 907 + return ret; 908 + 909 + *status0 = st->transf_buf[0]; 910 + *status1 = st->transf_buf[1]; 911 + 912 + return 0; 913 + } 914 + 915 + static int adxl380_get_fifo_entries(struct adxl380_state *st, u16 *fifo_entries) 916 + { 917 + int ret; 918 + 919 + ret = regmap_bulk_read(st->regmap, ADXL380_FIFO_STATUS_0_REG, 920 + &st->transf_buf, 2); 921 + if (ret) 922 + return ret; 923 + 924 + *fifo_entries = st->transf_buf[0] | ((BIT(0) & st->transf_buf[1]) << 8); 925 + 926 + return 0; 927 + } 928 + 929 + static void adxl380_push_event(struct iio_dev *indio_dev, s64 timestamp, 930 + u8 status1) 931 + { 932 + if (FIELD_GET(ADXL380_STATUS_1_ACT_MSK, status1)) 933 + iio_push_event(indio_dev, 934 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 935 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 936 + timestamp); 937 + 938 + if (FIELD_GET(ADXL380_STATUS_1_INACT_MSK, status1)) 939 + iio_push_event(indio_dev, 940 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 941 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING), 942 + timestamp); 943 + if (FIELD_GET(ADXL380_STATUS_1_SINGLE_TAP_MSK, status1)) 944 + iio_push_event(indio_dev, 945 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 946 + IIO_EV_TYPE_GESTURE, IIO_EV_DIR_SINGLETAP), 947 + timestamp); 948 + 949 + if (FIELD_GET(ADXL380_STATUS_1_DOUBLE_TAP_MSK, status1)) 950 + iio_push_event(indio_dev, 951 + IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z, 952 + IIO_EV_TYPE_GESTURE, IIO_EV_DIR_DOUBLETAP), 953 + timestamp); 954 + } 955 + 956 + static irqreturn_t adxl380_irq_handler(int irq, void *p) 957 + { 958 + struct iio_dev *indio_dev = p; 959 + struct adxl380_state *st = iio_priv(indio_dev); 960 + u8 status0, status1; 961 + u16 fifo_entries; 962 + int i; 963 + int ret; 964 + 965 + guard(mutex)(&st->lock); 966 + 967 + ret = adxl380_get_status(st, &status0, &status1); 968 + if (ret) 969 + return IRQ_HANDLED; 970 + 971 + adxl380_push_event(indio_dev, iio_get_time_ns(indio_dev), status1); 972 + 973 + if (!FIELD_GET(ADXL380_STATUS_0_FIFO_WM_MSK, status0)) 974 + return IRQ_HANDLED; 975 + 976 + ret = adxl380_get_fifo_entries(st, &fifo_entries); 977 + if (ret) 978 + return IRQ_HANDLED; 979 + 980 + for (i = 0; i < fifo_entries; i += st->fifo_set_size) { 981 + ret = regmap_noinc_read(st->regmap, ADXL380_FIFO_DATA, 982 + &st->fifo_buf[i], 983 + 2 * st->fifo_set_size); 984 + if (ret) 985 + return IRQ_HANDLED; 986 + iio_push_to_buffers(indio_dev, &st->fifo_buf[i]); 987 + } 988 + 989 + return IRQ_HANDLED; 990 + } 991 + 992 + static int adxl380_write_calibbias_value(struct adxl380_state *st, 993 + unsigned long chan_addr, 994 + s8 calibbias) 995 + { 996 + int ret; 997 + 998 + guard(mutex)(&st->lock); 999 + 1000 + ret = adxl380_set_measure_en(st, false); 1001 + if (ret) 1002 + return ret; 1003 + 1004 + ret = regmap_write(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, calibbias); 1005 + if (ret) 1006 + return ret; 1007 + 1008 + return adxl380_set_measure_en(st, true); 1009 + } 1010 + 1011 + static int adxl380_read_calibbias_value(struct adxl380_state *st, 1012 + unsigned long chan_addr, 1013 + int *calibbias) 1014 + { 1015 + int ret; 1016 + unsigned int reg_val; 1017 + 1018 + guard(mutex)(&st->lock); 1019 + 1020 + ret = regmap_read(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, &reg_val); 1021 + if (ret) 1022 + return ret; 1023 + 1024 + *calibbias = sign_extend32(reg_val, 7); 1025 + 1026 + return 0; 1027 + } 1028 + 1029 + static ssize_t hwfifo_watermark_min_show(struct device *dev, 1030 + struct device_attribute *attr, 1031 + char *buf) 1032 + { 1033 + return sysfs_emit(buf, "1\n"); 1034 + } 1035 + 1036 + static ssize_t hwfifo_watermark_max_show(struct device *dev, 1037 + struct device_attribute *attr, 1038 + char *buf) 1039 + { 1040 + return sysfs_emit(buf, "%lu\n", ADXL380_FIFO_SAMPLES); 1041 + } 1042 + 1043 + static ssize_t adxl380_get_fifo_watermark(struct device *dev, 1044 + struct device_attribute *attr, 1045 + char *buf) 1046 + { 1047 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1048 + struct adxl380_state *st = iio_priv(indio_dev); 1049 + 1050 + return sysfs_emit(buf, "%d\n", st->watermark); 1051 + } 1052 + 1053 + static ssize_t adxl380_get_fifo_enabled(struct device *dev, 1054 + struct device_attribute *attr, 1055 + char *buf) 1056 + { 1057 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1058 + struct adxl380_state *st = iio_priv(indio_dev); 1059 + int ret; 1060 + unsigned int reg_val; 1061 + 1062 + ret = regmap_read(st->regmap, ADXL380_DIG_EN_REG, &reg_val); 1063 + if (ret) 1064 + return ret; 1065 + 1066 + return sysfs_emit(buf, "%lu\n", 1067 + FIELD_GET(ADXL380_FIFO_EN_MSK, reg_val)); 1068 + } 1069 + 1070 + static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0); 1071 + static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0); 1072 + static IIO_DEVICE_ATTR(hwfifo_watermark, 0444, 1073 + adxl380_get_fifo_watermark, NULL, 0); 1074 + static IIO_DEVICE_ATTR(hwfifo_enabled, 0444, 1075 + adxl380_get_fifo_enabled, NULL, 0); 1076 + 1077 + static const struct iio_dev_attr *adxl380_fifo_attributes[] = { 1078 + &iio_dev_attr_hwfifo_watermark_min, 1079 + &iio_dev_attr_hwfifo_watermark_max, 1080 + &iio_dev_attr_hwfifo_watermark, 1081 + &iio_dev_attr_hwfifo_enabled, 1082 + NULL 1083 + }; 1084 + 1085 + static int adxl380_buffer_postenable(struct iio_dev *indio_dev) 1086 + { 1087 + struct adxl380_state *st = iio_priv(indio_dev); 1088 + int i; 1089 + int ret; 1090 + 1091 + guard(mutex)(&st->lock); 1092 + 1093 + ret = adxl380_set_measure_en(st, false); 1094 + if (ret) 1095 + return ret; 1096 + 1097 + ret = regmap_update_bits(st->regmap, 1098 + st->int_map[0], 1099 + ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 1100 + FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 1)); 1101 + if (ret) 1102 + return ret; 1103 + 1104 + for_each_clear_bit(i, indio_dev->active_scan_mask, ADXL380_CH_NUM) { 1105 + ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, 1106 + ADXL380_CHAN_EN_MSK(i), 1107 + 0 << (4 + i)); 1108 + if (ret) 1109 + return ret; 1110 + } 1111 + 1112 + st->fifo_set_size = bitmap_weight(indio_dev->active_scan_mask, 1113 + iio_get_masklength(indio_dev)); 1114 + 1115 + if ((st->watermark * st->fifo_set_size) > ADXL380_FIFO_SAMPLES) 1116 + st->watermark = (ADXL380_FIFO_SAMPLES / st->fifo_set_size); 1117 + 1118 + ret = adxl380_set_fifo_samples(st); 1119 + if (ret) 1120 + return ret; 1121 + 1122 + ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK, 1123 + FIELD_PREP(ADXL380_FIFO_EN_MSK, 1)); 1124 + if (ret) 1125 + return ret; 1126 + 1127 + return adxl380_set_measure_en(st, true); 1128 + } 1129 + 1130 + static int adxl380_buffer_predisable(struct iio_dev *indio_dev) 1131 + { 1132 + struct adxl380_state *st = iio_priv(indio_dev); 1133 + int ret, i; 1134 + 1135 + guard(mutex)(&st->lock); 1136 + 1137 + ret = adxl380_set_measure_en(st, false); 1138 + if (ret) 1139 + return ret; 1140 + 1141 + ret = regmap_update_bits(st->regmap, 1142 + st->int_map[0], 1143 + ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 1144 + FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 0)); 1145 + if (ret) 1146 + return ret; 1147 + 1148 + for (i = 0; i < indio_dev->num_channels; i++) { 1149 + ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, 1150 + ADXL380_CHAN_EN_MSK(i), 1151 + 1 << (4 + i)); 1152 + if (ret) 1153 + return ret; 1154 + } 1155 + 1156 + ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK, 1157 + FIELD_PREP(ADXL380_FIFO_EN_MSK, 0)); 1158 + if (ret) 1159 + return ret; 1160 + 1161 + return adxl380_set_measure_en(st, true); 1162 + } 1163 + 1164 + static const struct iio_buffer_setup_ops adxl380_buffer_ops = { 1165 + .postenable = adxl380_buffer_postenable, 1166 + .predisable = adxl380_buffer_predisable, 1167 + }; 1168 + 1169 + static int adxl380_read_raw(struct iio_dev *indio_dev, 1170 + struct iio_chan_spec const *chan, 1171 + int *val, int *val2, long info) 1172 + { 1173 + struct adxl380_state *st = iio_priv(indio_dev); 1174 + int ret; 1175 + 1176 + switch (info) { 1177 + case IIO_CHAN_INFO_RAW: 1178 + ret = iio_device_claim_direct_mode(indio_dev); 1179 + if (ret) 1180 + return ret; 1181 + 1182 + ret = adxl380_read_chn(st, chan->address); 1183 + iio_device_release_direct_mode(indio_dev); 1184 + if (ret) 1185 + return ret; 1186 + 1187 + *val = sign_extend32(ret >> chan->scan_type.shift, 1188 + chan->scan_type.realbits - 1); 1189 + return IIO_VAL_INT; 1190 + case IIO_CHAN_INFO_SCALE: 1191 + switch (chan->type) { 1192 + case IIO_ACCEL: 1193 + scoped_guard(mutex, &st->lock) { 1194 + *val = st->chip_info->scale_tbl[st->range][0]; 1195 + *val2 = st->chip_info->scale_tbl[st->range][1]; 1196 + } 1197 + return IIO_VAL_INT_PLUS_NANO; 1198 + case IIO_TEMP: 1199 + /* 10.2 LSB / Degree Celsius */ 1200 + *val = 10000; 1201 + *val2 = 102; 1202 + return IIO_VAL_FRACTIONAL; 1203 + default: 1204 + return -EINVAL; 1205 + } 1206 + case IIO_CHAN_INFO_OFFSET: 1207 + switch (chan->type) { 1208 + case IIO_TEMP: 1209 + *val = st->chip_info->temp_offset; 1210 + return IIO_VAL_INT; 1211 + default: 1212 + return -EINVAL; 1213 + } 1214 + case IIO_CHAN_INFO_CALIBBIAS: 1215 + switch (chan->type) { 1216 + case IIO_ACCEL: 1217 + ret = adxl380_read_calibbias_value(st, chan->scan_index, val); 1218 + if (ret) 1219 + return ret; 1220 + return IIO_VAL_INT; 1221 + default: 1222 + return -EINVAL; 1223 + } 1224 + case IIO_CHAN_INFO_SAMP_FREQ: 1225 + ret = adxl380_get_odr(st, val); 1226 + if (ret) 1227 + return ret; 1228 + return IIO_VAL_INT; 1229 + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 1230 + ret = adxl380_get_lpf(st, val); 1231 + if (ret) 1232 + return ret; 1233 + return IIO_VAL_INT; 1234 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 1235 + ret = adxl380_get_hpf(st, val, val2); 1236 + if (ret) 1237 + return ret; 1238 + return IIO_VAL_INT_PLUS_MICRO; 1239 + } 1240 + 1241 + return -EINVAL; 1242 + } 1243 + 1244 + static int adxl380_read_avail(struct iio_dev *indio_dev, 1245 + struct iio_chan_spec const *chan, 1246 + const int **vals, int *type, int *length, 1247 + long mask) 1248 + { 1249 + struct adxl380_state *st = iio_priv(indio_dev); 1250 + 1251 + if (chan->type != IIO_ACCEL) 1252 + return -EINVAL; 1253 + 1254 + switch (mask) { 1255 + case IIO_CHAN_INFO_SCALE: 1256 + *vals = (const int *)st->chip_info->scale_tbl; 1257 + *type = IIO_VAL_INT_PLUS_NANO; 1258 + *length = ARRAY_SIZE(st->chip_info->scale_tbl) * 2; 1259 + return IIO_AVAIL_LIST; 1260 + case IIO_CHAN_INFO_SAMP_FREQ: 1261 + *vals = (const int *)st->chip_info->samp_freq_tbl; 1262 + *type = IIO_VAL_INT; 1263 + *length = ARRAY_SIZE(st->chip_info->samp_freq_tbl); 1264 + return IIO_AVAIL_LIST; 1265 + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 1266 + *vals = (const int *)st->lpf_tbl; 1267 + *type = IIO_VAL_INT; 1268 + *length = ARRAY_SIZE(st->lpf_tbl); 1269 + return IIO_AVAIL_LIST; 1270 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 1271 + *vals = (const int *)st->hpf_tbl; 1272 + *type = IIO_VAL_INT_PLUS_MICRO; 1273 + /* Values are stored in a 2D matrix */ 1274 + *length = ARRAY_SIZE(st->hpf_tbl) * 2; 1275 + return IIO_AVAIL_LIST; 1276 + default: 1277 + return -EINVAL; 1278 + } 1279 + } 1280 + 1281 + static int adxl380_write_raw(struct iio_dev *indio_dev, 1282 + struct iio_chan_spec const *chan, 1283 + int val, int val2, long info) 1284 + { 1285 + struct adxl380_state *st = iio_priv(indio_dev); 1286 + int odr_index, lpf_index, hpf_index, range_index; 1287 + 1288 + switch (info) { 1289 + case IIO_CHAN_INFO_SAMP_FREQ: 1290 + odr_index = adxl380_find_match_1d_tbl(st->chip_info->samp_freq_tbl, 1291 + ARRAY_SIZE(st->chip_info->samp_freq_tbl), 1292 + val); 1293 + return adxl380_set_odr(st, odr_index); 1294 + case IIO_CHAN_INFO_CALIBBIAS: 1295 + return adxl380_write_calibbias_value(st, chan->scan_index, val); 1296 + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 1297 + lpf_index = adxl380_find_match_1d_tbl(st->lpf_tbl, 1298 + ARRAY_SIZE(st->lpf_tbl), 1299 + val); 1300 + return adxl380_set_lpf(st, lpf_index); 1301 + case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY: 1302 + hpf_index = adxl380_find_match_2d_tbl(st->hpf_tbl, 1303 + ARRAY_SIZE(st->hpf_tbl), 1304 + val, val2); 1305 + if (hpf_index < 0) 1306 + return hpf_index; 1307 + return adxl380_set_hpf(st, hpf_index); 1308 + case IIO_CHAN_INFO_SCALE: 1309 + range_index = adxl380_find_match_2d_tbl(st->chip_info->scale_tbl, 1310 + ARRAY_SIZE(st->chip_info->scale_tbl), 1311 + val, val2); 1312 + if (range_index < 0) 1313 + return range_index; 1314 + return adxl380_set_range(st, range_index); 1315 + default: 1316 + return -EINVAL; 1317 + } 1318 + } 1319 + 1320 + static int adxl380_write_raw_get_fmt(struct iio_dev *indio_dev, 1321 + struct iio_chan_spec const *chan, 1322 + long info) 1323 + { 1324 + switch (info) { 1325 + case IIO_CHAN_INFO_SCALE: 1326 + if (chan->type != IIO_ACCEL) 1327 + return -EINVAL; 1328 + 1329 + return IIO_VAL_INT_PLUS_NANO; 1330 + default: 1331 + return IIO_VAL_INT_PLUS_MICRO; 1332 + } 1333 + } 1334 + 1335 + static int adxl380_read_event_config(struct iio_dev *indio_dev, 1336 + const struct iio_chan_spec *chan, 1337 + enum iio_event_type type, 1338 + enum iio_event_direction dir) 1339 + { 1340 + struct adxl380_state *st = iio_priv(indio_dev); 1341 + int ret; 1342 + bool int_en; 1343 + bool tap_axis_en = false; 1344 + 1345 + switch (chan->channel2) { 1346 + case IIO_MOD_X: 1347 + tap_axis_en = st->tap_axis_en == ADXL380_X_AXIS; 1348 + break; 1349 + case IIO_MOD_Y: 1350 + tap_axis_en = st->tap_axis_en == ADXL380_Y_AXIS; 1351 + break; 1352 + case IIO_MOD_Z: 1353 + tap_axis_en = st->tap_axis_en == ADXL380_Z_AXIS; 1354 + break; 1355 + default: 1356 + return -EINVAL; 1357 + } 1358 + 1359 + switch (dir) { 1360 + case IIO_EV_DIR_RISING: 1361 + ret = adxl380_read_act_inact_int(st, ADXL380_ACTIVITY, &int_en); 1362 + if (ret) 1363 + return ret; 1364 + return int_en; 1365 + case IIO_EV_DIR_FALLING: 1366 + ret = adxl380_read_act_inact_int(st, ADXL380_INACTIVITY, &int_en); 1367 + if (ret) 1368 + return ret; 1369 + return int_en; 1370 + case IIO_EV_DIR_SINGLETAP: 1371 + ret = adxl380_read_tap_int(st, ADXL380_SINGLE_TAP, &int_en); 1372 + if (ret) 1373 + return ret; 1374 + return int_en && tap_axis_en; 1375 + case IIO_EV_DIR_DOUBLETAP: 1376 + ret = adxl380_read_tap_int(st, ADXL380_DOUBLE_TAP, &int_en); 1377 + if (ret) 1378 + return ret; 1379 + return int_en && tap_axis_en; 1380 + default: 1381 + return -EINVAL; 1382 + } 1383 + } 1384 + 1385 + static int adxl380_write_event_config(struct iio_dev *indio_dev, 1386 + const struct iio_chan_spec *chan, 1387 + enum iio_event_type type, 1388 + enum iio_event_direction dir, 1389 + int state) 1390 + { 1391 + struct adxl380_state *st = iio_priv(indio_dev); 1392 + enum adxl380_axis axis; 1393 + 1394 + switch (chan->channel2) { 1395 + case IIO_MOD_X: 1396 + axis = ADXL380_X_AXIS; 1397 + break; 1398 + case IIO_MOD_Y: 1399 + axis = ADXL380_Y_AXIS; 1400 + break; 1401 + case IIO_MOD_Z: 1402 + axis = ADXL380_Z_AXIS; 1403 + break; 1404 + default: 1405 + return -EINVAL; 1406 + } 1407 + 1408 + switch (dir) { 1409 + case IIO_EV_DIR_RISING: 1410 + return adxl380_act_inact_config(st, ADXL380_ACTIVITY, state); 1411 + case IIO_EV_DIR_FALLING: 1412 + return adxl380_act_inact_config(st, ADXL380_INACTIVITY, state); 1413 + case IIO_EV_DIR_SINGLETAP: 1414 + return adxl380_tap_config(st, axis, ADXL380_SINGLE_TAP, state); 1415 + case IIO_EV_DIR_DOUBLETAP: 1416 + return adxl380_tap_config(st, axis, ADXL380_DOUBLE_TAP, state); 1417 + default: 1418 + return -EINVAL; 1419 + } 1420 + } 1421 + 1422 + static int adxl380_read_event_value(struct iio_dev *indio_dev, 1423 + const struct iio_chan_spec *chan, 1424 + enum iio_event_type type, 1425 + enum iio_event_direction dir, 1426 + enum iio_event_info info, 1427 + int *val, int *val2) 1428 + { 1429 + struct adxl380_state *st = iio_priv(indio_dev); 1430 + 1431 + guard(mutex)(&st->lock); 1432 + 1433 + switch (type) { 1434 + case IIO_EV_TYPE_THRESH: 1435 + switch (info) { 1436 + case IIO_EV_INFO_VALUE: { 1437 + switch (dir) { 1438 + case IIO_EV_DIR_RISING: 1439 + *val = st->act_threshold; 1440 + return IIO_VAL_INT; 1441 + case IIO_EV_DIR_FALLING: 1442 + *val = st->inact_threshold; 1443 + return IIO_VAL_INT; 1444 + default: 1445 + return -EINVAL; 1446 + } 1447 + } 1448 + case IIO_EV_INFO_PERIOD: 1449 + switch (dir) { 1450 + case IIO_EV_DIR_RISING: 1451 + *val = st->act_time_ms; 1452 + *val2 = 1000; 1453 + return IIO_VAL_FRACTIONAL; 1454 + case IIO_EV_DIR_FALLING: 1455 + *val = st->inact_time_ms; 1456 + *val2 = 1000; 1457 + return IIO_VAL_FRACTIONAL; 1458 + default: 1459 + return -EINVAL; 1460 + } 1461 + default: 1462 + return -EINVAL; 1463 + } 1464 + case IIO_EV_TYPE_GESTURE: 1465 + switch (info) { 1466 + case IIO_EV_INFO_VALUE: 1467 + *val = st->tap_threshold; 1468 + return IIO_VAL_INT; 1469 + case IIO_EV_INFO_RESET_TIMEOUT: 1470 + *val = st->tap_window_us; 1471 + *val2 = 1000000; 1472 + return IIO_VAL_FRACTIONAL; 1473 + case IIO_EV_INFO_TAP2_MIN_DELAY: 1474 + *val = st->tap_latent_us; 1475 + *val2 = 1000000; 1476 + return IIO_VAL_FRACTIONAL; 1477 + default: 1478 + return -EINVAL; 1479 + } 1480 + default: 1481 + return -EINVAL; 1482 + } 1483 + } 1484 + 1485 + static int adxl380_write_event_value(struct iio_dev *indio_dev, 1486 + const struct iio_chan_spec *chan, 1487 + enum iio_event_type type, enum iio_event_direction dir, 1488 + enum iio_event_info info, int val, int val2) 1489 + { 1490 + struct adxl380_state *st = iio_priv(indio_dev); 1491 + u32 val_ms, val_us; 1492 + 1493 + if (chan->type != IIO_ACCEL) 1494 + return -EINVAL; 1495 + 1496 + switch (type) { 1497 + case IIO_EV_TYPE_THRESH: 1498 + switch (info) { 1499 + case IIO_EV_INFO_VALUE: 1500 + switch (dir) { 1501 + case IIO_EV_DIR_RISING: 1502 + return adxl380_set_act_inact_threshold(indio_dev, 1503 + ADXL380_ACTIVITY, val); 1504 + case IIO_EV_DIR_FALLING: 1505 + return adxl380_set_act_inact_threshold(indio_dev, 1506 + ADXL380_INACTIVITY, val); 1507 + default: 1508 + return -EINVAL; 1509 + } 1510 + case IIO_EV_INFO_PERIOD: 1511 + val_ms = val * 1000 + DIV_ROUND_UP(val2, 1000); 1512 + switch (dir) { 1513 + case IIO_EV_DIR_RISING: 1514 + return adxl380_set_act_inact_time_ms(st, 1515 + ADXL380_ACTIVITY, val_ms); 1516 + case IIO_EV_DIR_FALLING: 1517 + return adxl380_set_act_inact_time_ms(st, 1518 + ADXL380_INACTIVITY, val_ms); 1519 + default: 1520 + return -EINVAL; 1521 + } 1522 + 1523 + default: 1524 + return -EINVAL; 1525 + } 1526 + case IIO_EV_TYPE_GESTURE: 1527 + switch (info) { 1528 + case IIO_EV_INFO_VALUE: 1529 + return adxl380_set_tap_threshold_value(indio_dev, val); 1530 + case IIO_EV_INFO_RESET_TIMEOUT: 1531 + val_us = val * 1000000 + val2; 1532 + return adxl380_write_tap_time_us(st, 1533 + ADXL380_TAP_TIME_WINDOW, 1534 + val_us); 1535 + case IIO_EV_INFO_TAP2_MIN_DELAY: 1536 + val_us = val * 1000000 + val2; 1537 + return adxl380_write_tap_time_us(st, 1538 + ADXL380_TAP_TIME_LATENT, 1539 + val_us); 1540 + default: 1541 + return -EINVAL; 1542 + } 1543 + default: 1544 + return -EINVAL; 1545 + } 1546 + } 1547 + 1548 + static ssize_t in_accel_gesture_tap_maxtomin_time_show(struct device *dev, 1549 + struct device_attribute *attr, 1550 + char *buf) 1551 + { 1552 + int vals[2]; 1553 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1554 + struct adxl380_state *st = iio_priv(indio_dev); 1555 + 1556 + guard(mutex)(&st->lock); 1557 + 1558 + vals[0] = st->tap_duration_us; 1559 + vals[1] = MICRO; 1560 + 1561 + return iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, vals); 1562 + } 1563 + 1564 + static ssize_t in_accel_gesture_tap_maxtomin_time_store(struct device *dev, 1565 + struct device_attribute *attr, 1566 + const char *buf, size_t len) 1567 + { 1568 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1569 + struct adxl380_state *st = iio_priv(indio_dev); 1570 + int ret, val_int, val_fract_us; 1571 + 1572 + guard(mutex)(&st->lock); 1573 + 1574 + ret = iio_str_to_fixpoint(buf, 100000, &val_int, &val_fract_us); 1575 + if (ret) 1576 + return ret; 1577 + 1578 + /* maximum value is 255 * 625 us = 0.159375 seconds */ 1579 + if (val_int || val_fract_us > 159375 || val_fract_us < 0) 1580 + return -EINVAL; 1581 + 1582 + ret = adxl380_write_tap_dur_us(indio_dev, val_fract_us); 1583 + if (ret) 1584 + return ret; 1585 + 1586 + return len; 1587 + } 1588 + 1589 + static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time, 0); 1590 + 1591 + static struct attribute *adxl380_event_attributes[] = { 1592 + &iio_dev_attr_in_accel_gesture_tap_maxtomin_time.dev_attr.attr, 1593 + NULL 1594 + }; 1595 + 1596 + static const struct attribute_group adxl380_event_attribute_group = { 1597 + .attrs = adxl380_event_attributes, 1598 + }; 1599 + 1600 + static int adxl380_reg_access(struct iio_dev *indio_dev, 1601 + unsigned int reg, 1602 + unsigned int writeval, 1603 + unsigned int *readval) 1604 + { 1605 + struct adxl380_state *st = iio_priv(indio_dev); 1606 + 1607 + if (readval) 1608 + return regmap_read(st->regmap, reg, readval); 1609 + 1610 + return regmap_write(st->regmap, reg, writeval); 1611 + } 1612 + 1613 + static int adxl380_set_watermark(struct iio_dev *indio_dev, unsigned int val) 1614 + { 1615 + struct adxl380_state *st = iio_priv(indio_dev); 1616 + 1617 + st->watermark = min(val, ADXL380_FIFO_SAMPLES); 1618 + 1619 + return 0; 1620 + } 1621 + 1622 + static const struct iio_info adxl380_info = { 1623 + .read_raw = adxl380_read_raw, 1624 + .read_avail = &adxl380_read_avail, 1625 + .write_raw = adxl380_write_raw, 1626 + .write_raw_get_fmt = adxl380_write_raw_get_fmt, 1627 + .read_event_config = adxl380_read_event_config, 1628 + .write_event_config = adxl380_write_event_config, 1629 + .read_event_value = adxl380_read_event_value, 1630 + .write_event_value = adxl380_write_event_value, 1631 + .event_attrs = &adxl380_event_attribute_group, 1632 + .debugfs_reg_access = &adxl380_reg_access, 1633 + .hwfifo_set_watermark = adxl380_set_watermark, 1634 + }; 1635 + 1636 + static const struct iio_event_spec adxl380_events[] = { 1637 + { 1638 + .type = IIO_EV_TYPE_THRESH, 1639 + .dir = IIO_EV_DIR_RISING, 1640 + .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | 1641 + BIT(IIO_EV_INFO_VALUE) | 1642 + BIT(IIO_EV_INFO_PERIOD), 1643 + }, 1644 + { 1645 + .type = IIO_EV_TYPE_THRESH, 1646 + .dir = IIO_EV_DIR_FALLING, 1647 + .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | 1648 + BIT(IIO_EV_INFO_VALUE) | 1649 + BIT(IIO_EV_INFO_PERIOD), 1650 + }, 1651 + { 1652 + .type = IIO_EV_TYPE_GESTURE, 1653 + .dir = IIO_EV_DIR_SINGLETAP, 1654 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 1655 + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | 1656 + BIT(IIO_EV_INFO_RESET_TIMEOUT), 1657 + }, 1658 + { 1659 + .type = IIO_EV_TYPE_GESTURE, 1660 + .dir = IIO_EV_DIR_DOUBLETAP, 1661 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 1662 + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) | 1663 + BIT(IIO_EV_INFO_RESET_TIMEOUT) | 1664 + BIT(IIO_EV_INFO_TAP2_MIN_DELAY), 1665 + }, 1666 + }; 1667 + 1668 + #define ADXL380_ACCEL_CHANNEL(index, reg, axis) { \ 1669 + .type = IIO_ACCEL, \ 1670 + .address = reg, \ 1671 + .modified = 1, \ 1672 + .channel2 = IIO_MOD_##axis, \ 1673 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 1674 + BIT(IIO_CHAN_INFO_CALIBBIAS), \ 1675 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1676 + .info_mask_shared_by_all_available = \ 1677 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 1678 + .info_mask_shared_by_type = \ 1679 + BIT(IIO_CHAN_INFO_SCALE) | \ 1680 + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \ 1681 + BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \ 1682 + .info_mask_shared_by_type_available = \ 1683 + BIT(IIO_CHAN_INFO_SCALE) | \ 1684 + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \ 1685 + BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \ 1686 + .scan_index = index, \ 1687 + .scan_type = { \ 1688 + .sign = 's', \ 1689 + .realbits = 16, \ 1690 + .storagebits = 16, \ 1691 + .endianness = IIO_BE, \ 1692 + }, \ 1693 + .event_spec = adxl380_events, \ 1694 + .num_event_specs = ARRAY_SIZE(adxl380_events) \ 1695 + } 1696 + 1697 + static const struct iio_chan_spec adxl380_channels[] = { 1698 + ADXL380_ACCEL_CHANNEL(0, ADXL380_X_DATA_H_REG, X), 1699 + ADXL380_ACCEL_CHANNEL(1, ADXL380_Y_DATA_H_REG, Y), 1700 + ADXL380_ACCEL_CHANNEL(2, ADXL380_Z_DATA_H_REG, Z), 1701 + { 1702 + .type = IIO_TEMP, 1703 + .address = ADXL380_T_DATA_H_REG, 1704 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1705 + BIT(IIO_CHAN_INFO_SCALE) | 1706 + BIT(IIO_CHAN_INFO_OFFSET), 1707 + .scan_index = 3, 1708 + .scan_type = { 1709 + .sign = 's', 1710 + .realbits = 12, 1711 + .storagebits = 16, 1712 + .shift = 4, 1713 + .endianness = IIO_BE, 1714 + }, 1715 + }, 1716 + }; 1717 + 1718 + static int adxl380_config_irq(struct iio_dev *indio_dev) 1719 + { 1720 + struct adxl380_state *st = iio_priv(indio_dev); 1721 + unsigned long irq_flag; 1722 + struct irq_data *desc; 1723 + u32 irq_type; 1724 + u8 polarity; 1725 + int ret; 1726 + 1727 + st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT0"); 1728 + if (st->irq > 0) { 1729 + st->int_map[0] = ADXL380_INT0_MAP0_REG; 1730 + st->int_map[1] = ADXL380_INT0_MAP1_REG; 1731 + } else { 1732 + st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT1"); 1733 + if (st->irq > 0) 1734 + return dev_err_probe(st->dev, -ENODEV, 1735 + "no interrupt name specified"); 1736 + st->int_map[0] = ADXL380_INT1_MAP0_REG; 1737 + st->int_map[1] = ADXL380_INT1_MAP1_REG; 1738 + } 1739 + 1740 + desc = irq_get_irq_data(st->irq); 1741 + if (!desc) 1742 + return dev_err_probe(st->dev, -EINVAL, "Could not find IRQ %d\n", st->irq); 1743 + 1744 + irq_type = irqd_get_trigger_type(desc); 1745 + if (irq_type == IRQ_TYPE_LEVEL_HIGH) { 1746 + polarity = 0; 1747 + irq_flag = IRQF_TRIGGER_HIGH | IRQF_ONESHOT; 1748 + } else if (irq_type == IRQ_TYPE_LEVEL_LOW) { 1749 + polarity = 1; 1750 + irq_flag = IRQF_TRIGGER_LOW | IRQF_ONESHOT; 1751 + } else { 1752 + return dev_err_probe(st->dev, -EINVAL, 1753 + "Invalid interrupt 0x%x. Only level interrupts supported\n", 1754 + irq_type); 1755 + } 1756 + 1757 + ret = regmap_update_bits(st->regmap, ADXL380_INT0_REG, 1758 + ADXL380_INT0_POL_MSK, 1759 + FIELD_PREP(ADXL380_INT0_POL_MSK, polarity)); 1760 + if (ret) 1761 + return ret; 1762 + 1763 + return devm_request_threaded_irq(st->dev, st->irq, NULL, 1764 + adxl380_irq_handler, irq_flag, 1765 + indio_dev->name, indio_dev); 1766 + } 1767 + 1768 + static int adxl380_setup(struct iio_dev *indio_dev) 1769 + { 1770 + unsigned int reg_val; 1771 + u16 part_id, chip_id; 1772 + int ret, i; 1773 + struct adxl380_state *st = iio_priv(indio_dev); 1774 + 1775 + ret = regmap_read(st->regmap, ADXL380_DEVID_AD_REG, &reg_val); 1776 + if (ret) 1777 + return ret; 1778 + 1779 + if (reg_val != ADXL380_DEVID_AD_VAL) 1780 + dev_warn(st->dev, "Unknown chip id %x\n", reg_val); 1781 + 1782 + ret = regmap_bulk_read(st->regmap, ADLX380_PART_ID_REG, 1783 + &st->transf_buf, 2); 1784 + if (ret) 1785 + return ret; 1786 + 1787 + part_id = get_unaligned_be16(st->transf_buf); 1788 + part_id >>= 4; 1789 + 1790 + if (part_id != ADXL380_ID_VAL) 1791 + dev_warn(st->dev, "Unknown part id %x\n", part_id); 1792 + 1793 + ret = regmap_read(st->regmap, ADXL380_MISC_0_REG, &reg_val); 1794 + if (ret) 1795 + return ret; 1796 + 1797 + /* Bit to differentiate between ADXL380/382. */ 1798 + if (reg_val & ADXL380_XL382_MSK) 1799 + chip_id = ADXL382_ID_VAL; 1800 + else 1801 + chip_id = ADXL380_ID_VAL; 1802 + 1803 + if (chip_id != st->chip_info->chip_id) 1804 + dev_warn(st->dev, "Unknown chip id %x\n", chip_id); 1805 + 1806 + ret = regmap_write(st->regmap, ADXL380_RESET_REG, ADXL380_RESET_CODE); 1807 + if (ret) 1808 + return ret; 1809 + 1810 + /* 1811 + * A latency of approximately 0.5 ms is required after soft reset. 1812 + * Stated in the register REG_RESET description. 1813 + */ 1814 + fsleep(500); 1815 + 1816 + for (i = 0; i < indio_dev->num_channels; i++) { 1817 + ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, 1818 + ADXL380_CHAN_EN_MSK(i), 1819 + 1 << (4 + i)); 1820 + if (ret) 1821 + return ret; 1822 + } 1823 + 1824 + ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG, 1825 + ADXL380_FIFO_MODE_MSK, 1826 + FIELD_PREP(ADXL380_FIFO_MODE_MSK, ADXL380_FIFO_STREAMED)); 1827 + if (ret) 1828 + return ret; 1829 + 1830 + /* Select all 3 axis for act/inact detection. */ 1831 + ret = regmap_update_bits(st->regmap, ADXL380_SNSR_AXIS_EN_REG, 1832 + ADXL380_ACT_INACT_AXIS_EN_MSK, 1833 + FIELD_PREP(ADXL380_ACT_INACT_AXIS_EN_MSK, 1834 + ADXL380_ACT_INACT_AXIS_EN_MSK)); 1835 + if (ret) 1836 + return ret; 1837 + 1838 + ret = adxl380_config_irq(indio_dev); 1839 + if (ret) 1840 + return ret; 1841 + 1842 + ret = adxl380_fill_lpf_tbl(st); 1843 + if (ret) 1844 + return ret; 1845 + 1846 + ret = adxl380_fill_hpf_tbl(st); 1847 + if (ret) 1848 + return ret; 1849 + 1850 + return adxl380_set_measure_en(st, true); 1851 + } 1852 + 1853 + int adxl380_probe(struct device *dev, struct regmap *regmap, 1854 + const struct adxl380_chip_info *chip_info) 1855 + { 1856 + struct iio_dev *indio_dev; 1857 + struct adxl380_state *st; 1858 + int ret; 1859 + 1860 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 1861 + if (!indio_dev) 1862 + return -ENOMEM; 1863 + 1864 + st = iio_priv(indio_dev); 1865 + 1866 + st->dev = dev; 1867 + st->regmap = regmap; 1868 + st->chip_info = chip_info; 1869 + 1870 + mutex_init(&st->lock); 1871 + 1872 + indio_dev->channels = adxl380_channels; 1873 + indio_dev->num_channels = ARRAY_SIZE(adxl380_channels); 1874 + indio_dev->name = chip_info->name; 1875 + indio_dev->info = &adxl380_info; 1876 + indio_dev->modes = INDIO_DIRECT_MODE; 1877 + 1878 + ret = devm_regulator_get_enable(dev, "vddio"); 1879 + if (ret) 1880 + return dev_err_probe(st->dev, ret, 1881 + "Failed to get vddio regulator\n"); 1882 + 1883 + ret = devm_regulator_get_enable(st->dev, "vsupply"); 1884 + if (ret) 1885 + return dev_err_probe(st->dev, ret, 1886 + "Failed to get vsupply regulator\n"); 1887 + 1888 + ret = adxl380_setup(indio_dev); 1889 + if (ret) 1890 + return ret; 1891 + 1892 + ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev, 1893 + &adxl380_buffer_ops, 1894 + adxl380_fifo_attributes); 1895 + if (ret) 1896 + return ret; 1897 + 1898 + return devm_iio_device_register(dev, indio_dev); 1899 + } 1900 + EXPORT_SYMBOL_NS_GPL(adxl380_probe, IIO_ADXL380); 1901 + 1902 + MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>"); 1903 + MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>"); 1904 + MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer driver"); 1905 + MODULE_LICENSE("GPL");
+26
drivers/iio/accel/adxl380.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ */ 2 + /* 3 + * ADXL380 3-Axis Digital Accelerometer 4 + * 5 + * Copyright 2024 Analog Devices Inc. 6 + */ 7 + 8 + #ifndef _ADXL380_H_ 9 + #define _ADXL380_H_ 10 + 11 + struct adxl380_chip_info { 12 + const char *name; 13 + const int scale_tbl[3][2]; 14 + const int samp_freq_tbl[3]; 15 + const int temp_offset; 16 + const u16 chip_id; 17 + }; 18 + 19 + extern const struct adxl380_chip_info adxl380_chip_info; 20 + extern const struct adxl380_chip_info adxl382_chip_info; 21 + 22 + int adxl380_probe(struct device *dev, struct regmap *regmap, 23 + const struct adxl380_chip_info *chip_info); 24 + bool adxl380_readable_noinc_reg(struct device *dev, unsigned int reg); 25 + 26 + #endif /* _ADXL380_H_ */
+64
drivers/iio/accel/adxl380_i2c.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * ADXL380 3-Axis Digital Accelerometer I2C driver 4 + * 5 + * Copyright 2024 Analog Devices Inc. 6 + */ 7 + 8 + #include <linux/i2c.h> 9 + #include <linux/mod_devicetable.h> 10 + #include <linux/module.h> 11 + #include <linux/regmap.h> 12 + 13 + #include "adxl380.h" 14 + 15 + static const struct regmap_config adxl380_regmap_config = { 16 + .reg_bits = 8, 17 + .val_bits = 8, 18 + .readable_noinc_reg = adxl380_readable_noinc_reg, 19 + }; 20 + 21 + static int adxl380_i2c_probe(struct i2c_client *client) 22 + { 23 + struct regmap *regmap; 24 + const struct adxl380_chip_info *chip_data; 25 + 26 + chip_data = i2c_get_match_data(client); 27 + 28 + regmap = devm_regmap_init_i2c(client, &adxl380_regmap_config); 29 + if (IS_ERR(regmap)) 30 + return PTR_ERR(regmap); 31 + 32 + return adxl380_probe(&client->dev, regmap, chip_data); 33 + } 34 + 35 + static const struct i2c_device_id adxl380_i2c_id[] = { 36 + { "adxl380", (kernel_ulong_t)&adxl380_chip_info }, 37 + { "adxl382", (kernel_ulong_t)&adxl382_chip_info }, 38 + { } 39 + }; 40 + MODULE_DEVICE_TABLE(i2c, adxl380_i2c_id); 41 + 42 + static const struct of_device_id adxl380_of_match[] = { 43 + { .compatible = "adi,adxl380", .data = &adxl380_chip_info }, 44 + { .compatible = "adi,adxl382", .data = &adxl382_chip_info }, 45 + { } 46 + }; 47 + MODULE_DEVICE_TABLE(of, adxl380_of_match); 48 + 49 + static struct i2c_driver adxl380_i2c_driver = { 50 + .driver = { 51 + .name = "adxl380_i2c", 52 + .of_match_table = adxl380_of_match, 53 + }, 54 + .probe = adxl380_i2c_probe, 55 + .id_table = adxl380_i2c_id, 56 + }; 57 + 58 + module_i2c_driver(adxl380_i2c_driver); 59 + 60 + MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>"); 61 + MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>"); 62 + MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer I2C driver"); 63 + MODULE_LICENSE("GPL"); 64 + MODULE_IMPORT_NS(IIO_ADXL380);
+66
drivers/iio/accel/adxl380_spi.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * ADXL380 3-Axis Digital Accelerometer SPI driver 4 + * 5 + * Copyright 2024 Analog Devices Inc. 6 + */ 7 + 8 + #include <linux/mod_devicetable.h> 9 + #include <linux/module.h> 10 + #include <linux/regmap.h> 11 + #include <linux/spi/spi.h> 12 + 13 + #include "adxl380.h" 14 + 15 + static const struct regmap_config adxl380_spi_regmap_config = { 16 + .reg_bits = 7, 17 + .pad_bits = 1, 18 + .val_bits = 8, 19 + .read_flag_mask = BIT(0), 20 + .readable_noinc_reg = adxl380_readable_noinc_reg, 21 + }; 22 + 23 + static int adxl380_spi_probe(struct spi_device *spi) 24 + { 25 + const struct adxl380_chip_info *chip_data; 26 + struct regmap *regmap; 27 + 28 + chip_data = spi_get_device_match_data(spi); 29 + 30 + regmap = devm_regmap_init_spi(spi, &adxl380_spi_regmap_config); 31 + if (IS_ERR(regmap)) 32 + return PTR_ERR(regmap); 33 + 34 + return adxl380_probe(&spi->dev, regmap, chip_data); 35 + } 36 + 37 + static const struct spi_device_id adxl380_spi_id[] = { 38 + { "adxl380", (kernel_ulong_t)&adxl380_chip_info }, 39 + { "adxl382", (kernel_ulong_t)&adxl382_chip_info }, 40 + { } 41 + }; 42 + MODULE_DEVICE_TABLE(spi, adxl380_spi_id); 43 + 44 + static const struct of_device_id adxl380_of_match[] = { 45 + { .compatible = "adi,adxl380", .data = &adxl380_chip_info }, 46 + { .compatible = "adi,adxl382", .data = &adxl382_chip_info }, 47 + { } 48 + }; 49 + MODULE_DEVICE_TABLE(of, adxl380_of_match); 50 + 51 + static struct spi_driver adxl380_spi_driver = { 52 + .driver = { 53 + .name = "adxl380_spi", 54 + .of_match_table = adxl380_of_match, 55 + }, 56 + .probe = adxl380_spi_probe, 57 + .id_table = adxl380_spi_id, 58 + }; 59 + 60 + module_spi_driver(adxl380_spi_driver); 61 + 62 + MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>"); 63 + MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>"); 64 + MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer SPI driver"); 65 + MODULE_LICENSE("GPL"); 66 + MODULE_IMPORT_NS(IIO_ADXL380);
+1 -2
drivers/iio/accel/bma180.c
··· 876 876 877 877 mutex_lock(&data->mutex); 878 878 879 - for_each_set_bit(bit, indio_dev->active_scan_mask, 880 - indio_dev->masklength) { 879 + iio_for_each_active_channel(indio_dev, bit) { 881 880 ret = bma180_get_data_reg(data, bit); 882 881 if (ret < 0) { 883 882 mutex_unlock(&data->mutex);
+5 -6
drivers/iio/accel/bma400_core.c
··· 13 13 14 14 #include <linux/bitfield.h> 15 15 #include <linux/bitops.h> 16 + #include <linux/cleanup.h> 16 17 #include <linux/device.h> 17 18 #include <linux/kernel.h> 18 19 #include <linux/module.h> ··· 796 795 797 796 static int bma400_get_steps_reg(struct bma400_data *data, int *val) 798 797 { 799 - u8 *steps_raw; 800 798 int ret; 801 799 802 - steps_raw = kmalloc(BMA400_STEP_RAW_LEN, GFP_KERNEL); 800 + u8 *steps_raw __free(kfree) = kmalloc(BMA400_STEP_RAW_LEN, GFP_KERNEL); 803 801 if (!steps_raw) 804 802 return -ENOMEM; 805 803 806 804 ret = regmap_bulk_read(data->regmap, BMA400_STEP_CNT0_REG, 807 805 steps_raw, BMA400_STEP_RAW_LEN); 808 - if (ret) { 809 - kfree(steps_raw); 806 + if (ret) 810 807 return ret; 811 - } 808 + 812 809 *val = get_unaligned_le24(steps_raw); 813 - kfree(steps_raw); 810 + 814 811 return IIO_VAL_INT; 815 812 } 816 813
+1 -1
drivers/iio/accel/bma400_spi.c
··· 53 53 return spi_write(spi, data, count); 54 54 } 55 55 56 - static struct regmap_bus bma400_regmap_bus = { 56 + static const struct regmap_bus bma400_regmap_bus = { 57 57 .read = bma400_regmap_spi_read, 58 58 .write = bma400_regmap_spi_write, 59 59 .read_flag_mask = BIT(7),
+1 -2
drivers/iio/accel/bmc150-accel-core.c
··· 1007 1007 int j, bit; 1008 1008 1009 1009 j = 0; 1010 - for_each_set_bit(bit, indio_dev->active_scan_mask, 1011 - indio_dev->masklength) 1010 + iio_for_each_active_channel(indio_dev, bit) 1012 1011 memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit], 1013 1012 sizeof(data->scan.channels[0])); 1014 1013
+1 -1
drivers/iio/accel/bmi088-accel-spi.c
··· 36 36 return spi_write_then_read(spi, addr, sizeof(addr), val, val_size); 37 37 } 38 38 39 - static struct regmap_bus bmi088_regmap_bus = { 39 + static const struct regmap_bus bmi088_regmap_bus = { 40 40 .write = bmi088_regmap_spi_write, 41 41 .read = bmi088_regmap_spi_read, 42 42 };
+1 -1
drivers/iio/accel/cros_ec_accel_legacy.c
··· 62 62 return ret; 63 63 } 64 64 65 - for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 65 + for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { 66 66 *data = st->resp->dump.sensor[sensor_num].data[i] * 67 67 st->sign[i]; 68 68 data++;
+1 -2
drivers/iio/accel/fxls8962af-core.c
··· 966 966 int j, bit; 967 967 968 968 j = 0; 969 - for_each_set_bit(bit, indio_dev->active_scan_mask, 970 - indio_dev->masklength) { 969 + iio_for_each_active_channel(indio_dev, bit) { 971 970 memcpy(&data->scan.channels[j++], &buffer[i * 3 + bit], 972 971 sizeof(data->scan.channels[0])); 973 972 }
+6 -2
drivers/iio/accel/kxcjk-1013.c
··· 173 173 KXCJ91008, 174 174 KXTJ21009, 175 175 KXTF9, 176 + KX0221020, 176 177 KX0231025, 177 178 KX_MAX_CHIPS /* this must be last */ 178 179 }; ··· 581 580 return ret; 582 581 } 583 582 584 - /* On KX023, route all used interrupts to INT1 for now */ 585 - if (data->chipset == KX0231025 && data->client->irq > 0) { 583 + /* On KX023 and KX022, route all used interrupts to INT1 for now */ 584 + if ((data->chipset == KX0231025 || data->chipset == KX0221020) && data->client->irq > 0) { 586 585 ret = i2c_smbus_write_byte_data(data->client, KX023_REG_INC4, 587 586 KX023_REG_INC4_DRDY1 | 588 587 KX023_REG_INC4_WUFI1); ··· 1508 1507 case KXTF9: 1509 1508 data->regs = &kxtf9_regs; 1510 1509 break; 1510 + case KX0221020: 1511 1511 case KX0231025: 1512 1512 data->regs = &kx0231025_regs; 1513 1513 break; ··· 1714 1712 {"kxcj91008", KXCJ91008}, 1715 1713 {"kxtj21009", KXTJ21009}, 1716 1714 {"kxtf9", KXTF9}, 1715 + {"kx022-1020", KX0221020}, 1717 1716 {"kx023-1025", KX0231025}, 1718 1717 {"SMO8500", KXCJ91008}, 1719 1718 {} ··· 1727 1724 { .compatible = "kionix,kxcj91008", }, 1728 1725 { .compatible = "kionix,kxtj21009", }, 1729 1726 { .compatible = "kionix,kxtf9", }, 1727 + { .compatible = "kionix,kx022-1020", }, 1730 1728 { .compatible = "kionix,kx023-1025", }, 1731 1729 { } 1732 1730 };
+1 -2
drivers/iio/accel/msa311.c
··· 900 900 901 901 mutex_lock(&msa311->lock); 902 902 903 - for_each_set_bit(bit, indio_dev->active_scan_mask, 904 - indio_dev->masklength) { 903 + iio_for_each_active_channel(indio_dev, bit) { 905 904 chan = &msa311_channels[bit]; 906 905 907 906 err = msa311_get_axis(msa311, chan, &axis);
+1 -2
drivers/iio/accel/sca3300.c
··· 494 494 int bit, ret, val, i = 0; 495 495 s16 *channels = (s16 *)data->buffer; 496 496 497 - for_each_set_bit(bit, indio_dev->active_scan_mask, 498 - indio_dev->masklength) { 497 + iio_for_each_active_channel(indio_dev, bit) { 499 498 ret = sca3300_read_reg(data, indio_dev->channels[bit].address, &val); 500 499 if (ret) { 501 500 dev_err_ratelimited(&data->spi->dev,
+1 -2
drivers/iio/accel/stk8312.c
··· 448 448 goto err; 449 449 } 450 450 } else { 451 - for_each_set_bit(bit, indio_dev->active_scan_mask, 452 - indio_dev->masklength) { 451 + iio_for_each_active_channel(indio_dev, bit) { 453 452 ret = stk8312_read_accel(data, bit); 454 453 if (ret < 0) { 455 454 mutex_unlock(&data->lock);
+1 -2
drivers/iio/accel/stk8ba50.c
··· 330 330 goto err; 331 331 } 332 332 } else { 333 - for_each_set_bit(bit, indio_dev->active_scan_mask, 334 - indio_dev->masklength) { 333 + iio_for_each_active_channel(indio_dev, bit) { 335 334 ret = stk8ba50_read_accel(data, 336 335 stk8ba50_channel_table[bit]); 337 336 if (ret < 0)
+38
drivers/iio/adc/Kconfig
··· 21 21 select IIO_BUFFER 22 22 select IIO_TRIGGERED_BUFFER 23 23 24 + config AD4000 25 + tristate "Analog Devices AD4000 ADC Driver" 26 + depends on SPI 27 + select IIO_BUFFER 28 + select IIO_TRIGGERED_BUFFER 29 + help 30 + Say yes here to build support for Analog Devices AD4000 high speed 31 + SPI analog to digital converters (ADC). 32 + 33 + To compile this driver as a module, choose M here: the module will be 34 + called ad4000. 35 + 24 36 config AD4130 25 37 tristate "Analog Device AD4130 ADC Driver" 26 38 depends on SPI ··· 47 35 48 36 To compile this driver as a module, choose M here: the module will be 49 37 called ad4130. 38 + 39 + config AD4695 40 + tristate "Analog Device AD4695 ADC Driver" 41 + depends on SPI 42 + select REGMAP_SPI 43 + help 44 + Say yes here to build support for Analog Devices AD4695 and similar 45 + analog to digital converters (ADC). 46 + 47 + To compile this driver as a module, choose M here: the module will be 48 + called ad4695. 50 49 51 50 config AD7091R 52 51 tristate ··· 1014 991 This driver can also be built as a module. If so, the module 1015 992 will be called npcm_adc. 1016 993 994 + config PAC1921 995 + tristate "Microchip Technology PAC1921 driver" 996 + depends on I2C 997 + select REGMAP_I2C 998 + select IIO_BUFFER 999 + select IIO_TRIGGERED_BUFFER 1000 + help 1001 + Say yes here to build support for Microchip Technology's PAC1921 1002 + High-Side Power/Current Monitor with Analog Output. 1003 + 1004 + This driver can also be built as a module. If so, the module 1005 + will be called pac1921. 1006 + 1017 1007 config PAC1934 1018 1008 tristate "Microchip Technology PAC1934 driver" 1019 1009 depends on I2C ··· 1207 1171 tristate "Generic sigma delta modulator" 1208 1172 select IIO_BUFFER 1209 1173 select IIO_TRIGGERED_BUFFER 1174 + select IIO_BACKEND 1210 1175 help 1211 1176 Select this option to enables sigma delta modulator. This driver can 1212 1177 support generic sigma delta modulators. ··· 1262 1225 select IIO_BUFFER 1263 1226 select IIO_BUFFER_HW_CONSUMER 1264 1227 select IIO_TRIGGERED_BUFFER 1228 + select IIO_BACKEND 1265 1229 help 1266 1230 Select this option to support ADCSigma delta modulator for 1267 1231 STMicroelectronics STM32 digital filter for sigma delta converter.
+3
drivers/iio/adc/Makefile
··· 6 6 # When adding new entries keep the list in alphabetical order 7 7 obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o 8 8 obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o 9 + obj-$(CONFIG_AD4000) += ad4000.o 9 10 obj-$(CONFIG_AD4130) += ad4130.o 11 + obj-$(CONFIG_AD4695) += ad4695.o 10 12 obj-$(CONFIG_AD7091R) += ad7091r-base.o 11 13 obj-$(CONFIG_AD7091R5) += ad7091r5.o 12 14 obj-$(CONFIG_AD7091R8) += ad7091r8.o ··· 92 90 obj-$(CONFIG_MXS_LRADC_ADC) += mxs-lradc-adc.o 93 91 obj-$(CONFIG_NAU7802) += nau7802.o 94 92 obj-$(CONFIG_NPCM_ADC) += npcm_adc.o 93 + obj-$(CONFIG_PAC1921) += pac1921.o 95 94 obj-$(CONFIG_PAC1934) += pac1934.o 96 95 obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o 97 96 obj-$(CONFIG_QCOM_PM8XXX_XOADC) += qcom-pm8xxx-xoadc.o
+722
drivers/iio/adc/ad4000.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * AD4000 SPI ADC driver 4 + * 5 + * Copyright 2024 Analog Devices Inc. 6 + */ 7 + #include <linux/bits.h> 8 + #include <linux/bitfield.h> 9 + #include <linux/byteorder/generic.h> 10 + #include <linux/cleanup.h> 11 + #include <linux/device.h> 12 + #include <linux/err.h> 13 + #include <linux/math.h> 14 + #include <linux/module.h> 15 + #include <linux/mod_devicetable.h> 16 + #include <linux/gpio/consumer.h> 17 + #include <linux/regulator/consumer.h> 18 + #include <linux/spi/spi.h> 19 + #include <linux/units.h> 20 + #include <linux/util_macros.h> 21 + #include <linux/iio/iio.h> 22 + 23 + #include <linux/iio/buffer.h> 24 + #include <linux/iio/triggered_buffer.h> 25 + #include <linux/iio/trigger_consumer.h> 26 + 27 + #define AD4000_READ_COMMAND 0x54 28 + #define AD4000_WRITE_COMMAND 0x14 29 + 30 + #define AD4000_CONFIG_REG_DEFAULT 0xE1 31 + 32 + /* AD4000 Configuration Register programmable bits */ 33 + #define AD4000_CFG_SPAN_COMP BIT(3) /* Input span compression */ 34 + #define AD4000_CFG_HIGHZ BIT(2) /* High impedance mode */ 35 + 36 + #define AD4000_SCALE_OPTIONS 2 37 + 38 + #define AD4000_TQUIET1_NS 190 39 + #define AD4000_TQUIET2_NS 60 40 + #define AD4000_TCONV_NS 320 41 + 42 + #define __AD4000_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, _reg_access) \ 43 + { \ 44 + .type = IIO_VOLTAGE, \ 45 + .indexed = 1, \ 46 + .differential = 1, \ 47 + .channel = 0, \ 48 + .channel2 = 1, \ 49 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 50 + BIT(IIO_CHAN_INFO_SCALE), \ 51 + .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\ 52 + .scan_type = { \ 53 + .sign = _sign, \ 54 + .realbits = _real_bits, \ 55 + .storagebits = _storage_bits, \ 56 + .shift = _storage_bits - _real_bits, \ 57 + .endianness = IIO_BE, \ 58 + }, \ 59 + } 60 + 61 + #define AD4000_DIFF_CHANNEL(_sign, _real_bits, _reg_access) \ 62 + __AD4000_DIFF_CHANNEL((_sign), (_real_bits), \ 63 + ((_real_bits) > 16 ? 32 : 16), (_reg_access)) 64 + 65 + #define __AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _storage_bits, _reg_access)\ 66 + { \ 67 + .type = IIO_VOLTAGE, \ 68 + .indexed = 1, \ 69 + .channel = 0, \ 70 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 71 + BIT(IIO_CHAN_INFO_SCALE) | \ 72 + BIT(IIO_CHAN_INFO_OFFSET), \ 73 + .info_mask_separate_available = _reg_access ? BIT(IIO_CHAN_INFO_SCALE) : 0,\ 74 + .scan_type = { \ 75 + .sign = _sign, \ 76 + .realbits = _real_bits, \ 77 + .storagebits = _storage_bits, \ 78 + .shift = _storage_bits - _real_bits, \ 79 + .endianness = IIO_BE, \ 80 + }, \ 81 + } 82 + 83 + #define AD4000_PSEUDO_DIFF_CHANNEL(_sign, _real_bits, _reg_access) \ 84 + __AD4000_PSEUDO_DIFF_CHANNEL((_sign), (_real_bits), \ 85 + ((_real_bits) > 16 ? 32 : 16), (_reg_access)) 86 + 87 + static const char * const ad4000_power_supplies[] = { 88 + "vdd", "vio" 89 + }; 90 + 91 + enum ad4000_sdi { 92 + AD4000_SDI_MOSI, 93 + AD4000_SDI_VIO, 94 + AD4000_SDI_CS, 95 + AD4000_SDI_GND, 96 + }; 97 + 98 + /* maps adi,sdi-pin property value to enum */ 99 + static const char * const ad4000_sdi_pin[] = { 100 + [AD4000_SDI_MOSI] = "sdi", 101 + [AD4000_SDI_VIO] = "high", 102 + [AD4000_SDI_CS] = "cs", 103 + [AD4000_SDI_GND] = "low", 104 + }; 105 + 106 + /* Gains stored as fractions of 1000 so they can be expressed by integers. */ 107 + static const int ad4000_gains[] = { 108 + 454, 909, 1000, 1900, 109 + }; 110 + 111 + struct ad4000_chip_info { 112 + const char *dev_name; 113 + struct iio_chan_spec chan_spec; 114 + struct iio_chan_spec reg_access_chan_spec; 115 + bool has_hardware_gain; 116 + }; 117 + 118 + static const struct ad4000_chip_info ad4000_chip_info = { 119 + .dev_name = "ad4000", 120 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0), 121 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1), 122 + }; 123 + 124 + static const struct ad4000_chip_info ad4001_chip_info = { 125 + .dev_name = "ad4001", 126 + .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0), 127 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1), 128 + }; 129 + 130 + static const struct ad4000_chip_info ad4002_chip_info = { 131 + .dev_name = "ad4002", 132 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0), 133 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1), 134 + }; 135 + 136 + static const struct ad4000_chip_info ad4003_chip_info = { 137 + .dev_name = "ad4003", 138 + .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0), 139 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1), 140 + }; 141 + 142 + static const struct ad4000_chip_info ad4004_chip_info = { 143 + .dev_name = "ad4004", 144 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0), 145 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1), 146 + }; 147 + 148 + static const struct ad4000_chip_info ad4005_chip_info = { 149 + .dev_name = "ad4005", 150 + .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0), 151 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1), 152 + }; 153 + 154 + static const struct ad4000_chip_info ad4006_chip_info = { 155 + .dev_name = "ad4006", 156 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0), 157 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1), 158 + }; 159 + 160 + static const struct ad4000_chip_info ad4007_chip_info = { 161 + .dev_name = "ad4007", 162 + .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0), 163 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1), 164 + }; 165 + 166 + static const struct ad4000_chip_info ad4008_chip_info = { 167 + .dev_name = "ad4008", 168 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 0), 169 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 16, 1), 170 + }; 171 + 172 + static const struct ad4000_chip_info ad4010_chip_info = { 173 + .dev_name = "ad4010", 174 + .chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 0), 175 + .reg_access_chan_spec = AD4000_PSEUDO_DIFF_CHANNEL('u', 18, 1), 176 + }; 177 + 178 + static const struct ad4000_chip_info ad4011_chip_info = { 179 + .dev_name = "ad4011", 180 + .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0), 181 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1), 182 + }; 183 + 184 + static const struct ad4000_chip_info ad4020_chip_info = { 185 + .dev_name = "ad4020", 186 + .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0), 187 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1), 188 + }; 189 + 190 + static const struct ad4000_chip_info ad4021_chip_info = { 191 + .dev_name = "ad4021", 192 + .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0), 193 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1), 194 + }; 195 + 196 + static const struct ad4000_chip_info ad4022_chip_info = { 197 + .dev_name = "ad4022", 198 + .chan_spec = AD4000_DIFF_CHANNEL('s', 20, 0), 199 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 20, 1), 200 + }; 201 + 202 + static const struct ad4000_chip_info adaq4001_chip_info = { 203 + .dev_name = "adaq4001", 204 + .chan_spec = AD4000_DIFF_CHANNEL('s', 16, 0), 205 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 16, 1), 206 + .has_hardware_gain = true, 207 + }; 208 + 209 + static const struct ad4000_chip_info adaq4003_chip_info = { 210 + .dev_name = "adaq4003", 211 + .chan_spec = AD4000_DIFF_CHANNEL('s', 18, 0), 212 + .reg_access_chan_spec = AD4000_DIFF_CHANNEL('s', 18, 1), 213 + .has_hardware_gain = true, 214 + }; 215 + 216 + struct ad4000_state { 217 + struct spi_device *spi; 218 + struct gpio_desc *cnv_gpio; 219 + struct spi_transfer xfers[2]; 220 + struct spi_message msg; 221 + struct mutex lock; /* Protect read modify write cycle */ 222 + int vref_mv; 223 + enum ad4000_sdi sdi_pin; 224 + bool span_comp; 225 + u16 gain_milli; 226 + int scale_tbl[AD4000_SCALE_OPTIONS][2]; 227 + 228 + /* 229 + * DMA (thus cache coherency maintenance) requires the transfer buffers 230 + * to live in their own cache lines. 231 + */ 232 + struct { 233 + union { 234 + __be16 sample_buf16; 235 + __be32 sample_buf32; 236 + } data; 237 + s64 timestamp __aligned(8); 238 + } scan __aligned(IIO_DMA_MINALIGN); 239 + u8 tx_buf[2]; 240 + u8 rx_buf[2]; 241 + }; 242 + 243 + static void ad4000_fill_scale_tbl(struct ad4000_state *st, 244 + struct iio_chan_spec const *chan) 245 + { 246 + int val, tmp0, tmp1; 247 + int scale_bits; 248 + u64 tmp2; 249 + 250 + /* 251 + * ADCs that output two's complement code have one less bit to express 252 + * voltage magnitude. 253 + */ 254 + if (chan->scan_type.sign == 's') 255 + scale_bits = chan->scan_type.realbits - 1; 256 + else 257 + scale_bits = chan->scan_type.realbits; 258 + 259 + /* 260 + * The gain is stored as a fraction of 1000 and, as we need to 261 + * divide vref_mv by the gain, we invert the gain/1000 fraction. 262 + * Also multiply by an extra MILLI to preserve precision. 263 + * Thus, we have MILLI * MILLI equals MICRO as fraction numerator. 264 + */ 265 + val = mult_frac(st->vref_mv, MICRO, st->gain_milli); 266 + 267 + /* Would multiply by NANO here but we multiplied by extra MILLI */ 268 + tmp2 = shift_right((u64)val * MICRO, scale_bits); 269 + tmp0 = div_s64_rem(tmp2, NANO, &tmp1); 270 + 271 + /* Store scale for when span compression is disabled */ 272 + st->scale_tbl[0][0] = tmp0; /* Integer part */ 273 + st->scale_tbl[0][1] = abs(tmp1); /* Fractional part */ 274 + 275 + /* Store scale for when span compression is enabled */ 276 + st->scale_tbl[1][0] = tmp0; 277 + 278 + /* The integer part is always zero so don't bother to divide it. */ 279 + if (chan->differential) 280 + st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 4, 5); 281 + else 282 + st->scale_tbl[1][1] = DIV_ROUND_CLOSEST(abs(tmp1) * 9, 10); 283 + } 284 + 285 + static int ad4000_write_reg(struct ad4000_state *st, uint8_t val) 286 + { 287 + st->tx_buf[0] = AD4000_WRITE_COMMAND; 288 + st->tx_buf[1] = val; 289 + return spi_write(st->spi, st->tx_buf, ARRAY_SIZE(st->tx_buf)); 290 + } 291 + 292 + static int ad4000_read_reg(struct ad4000_state *st, unsigned int *val) 293 + { 294 + struct spi_transfer t = { 295 + .tx_buf = st->tx_buf, 296 + .rx_buf = st->rx_buf, 297 + .len = 2, 298 + }; 299 + int ret; 300 + 301 + st->tx_buf[0] = AD4000_READ_COMMAND; 302 + ret = spi_sync_transfer(st->spi, &t, 1); 303 + if (ret < 0) 304 + return ret; 305 + 306 + *val = st->rx_buf[1]; 307 + return ret; 308 + } 309 + 310 + static int ad4000_convert_and_acquire(struct ad4000_state *st) 311 + { 312 + int ret; 313 + 314 + /* 315 + * In 4-wire mode, the CNV line is held high for the entire conversion 316 + * and acquisition process. In other modes, the CNV GPIO is optional 317 + * and, if provided, replaces controller CS. If CNV GPIO is not defined 318 + * gpiod_set_value_cansleep() has no effect. 319 + */ 320 + gpiod_set_value_cansleep(st->cnv_gpio, 1); 321 + ret = spi_sync(st->spi, &st->msg); 322 + gpiod_set_value_cansleep(st->cnv_gpio, 0); 323 + 324 + return ret; 325 + } 326 + 327 + static int ad4000_single_conversion(struct iio_dev *indio_dev, 328 + const struct iio_chan_spec *chan, int *val) 329 + { 330 + struct ad4000_state *st = iio_priv(indio_dev); 331 + u32 sample; 332 + int ret; 333 + 334 + ret = ad4000_convert_and_acquire(st); 335 + if (ret < 0) 336 + return ret; 337 + 338 + if (chan->scan_type.storagebits > 16) 339 + sample = be32_to_cpu(st->scan.data.sample_buf32); 340 + else 341 + sample = be16_to_cpu(st->scan.data.sample_buf16); 342 + 343 + sample >>= chan->scan_type.shift; 344 + 345 + if (chan->scan_type.sign == 's') 346 + *val = sign_extend32(sample, chan->scan_type.realbits - 1); 347 + 348 + return IIO_VAL_INT; 349 + } 350 + 351 + static int ad4000_read_raw(struct iio_dev *indio_dev, 352 + struct iio_chan_spec const *chan, int *val, 353 + int *val2, long info) 354 + { 355 + struct ad4000_state *st = iio_priv(indio_dev); 356 + 357 + switch (info) { 358 + case IIO_CHAN_INFO_RAW: 359 + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 360 + return ad4000_single_conversion(indio_dev, chan, val); 361 + unreachable(); 362 + case IIO_CHAN_INFO_SCALE: 363 + *val = st->scale_tbl[st->span_comp][0]; 364 + *val2 = st->scale_tbl[st->span_comp][1]; 365 + return IIO_VAL_INT_PLUS_NANO; 366 + case IIO_CHAN_INFO_OFFSET: 367 + *val = 0; 368 + if (st->span_comp) 369 + *val = mult_frac(st->vref_mv, 1, 10); 370 + 371 + return IIO_VAL_INT; 372 + default: 373 + return -EINVAL; 374 + } 375 + } 376 + 377 + static int ad4000_read_avail(struct iio_dev *indio_dev, 378 + struct iio_chan_spec const *chan, 379 + const int **vals, int *type, int *length, 380 + long info) 381 + { 382 + struct ad4000_state *st = iio_priv(indio_dev); 383 + 384 + switch (info) { 385 + case IIO_CHAN_INFO_SCALE: 386 + *vals = (int *)st->scale_tbl; 387 + *length = AD4000_SCALE_OPTIONS * 2; 388 + *type = IIO_VAL_INT_PLUS_NANO; 389 + return IIO_AVAIL_LIST; 390 + default: 391 + return -EINVAL; 392 + } 393 + } 394 + 395 + static int ad4000_write_raw_get_fmt(struct iio_dev *indio_dev, 396 + struct iio_chan_spec const *chan, long mask) 397 + { 398 + switch (mask) { 399 + case IIO_CHAN_INFO_SCALE: 400 + return IIO_VAL_INT_PLUS_NANO; 401 + default: 402 + return IIO_VAL_INT_PLUS_MICRO; 403 + } 404 + } 405 + 406 + static int ad4000_write_raw(struct iio_dev *indio_dev, 407 + struct iio_chan_spec const *chan, int val, int val2, 408 + long mask) 409 + { 410 + struct ad4000_state *st = iio_priv(indio_dev); 411 + unsigned int reg_val; 412 + bool span_comp_en; 413 + int ret; 414 + 415 + switch (mask) { 416 + case IIO_CHAN_INFO_SCALE: 417 + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 418 + guard(mutex)(&st->lock); 419 + 420 + ret = ad4000_read_reg(st, &reg_val); 421 + if (ret < 0) 422 + return ret; 423 + 424 + span_comp_en = val2 == st->scale_tbl[1][1]; 425 + reg_val &= ~AD4000_CFG_SPAN_COMP; 426 + reg_val |= FIELD_PREP(AD4000_CFG_SPAN_COMP, span_comp_en); 427 + 428 + ret = ad4000_write_reg(st, reg_val); 429 + if (ret < 0) 430 + return ret; 431 + 432 + st->span_comp = span_comp_en; 433 + return 0; 434 + } 435 + unreachable(); 436 + default: 437 + return -EINVAL; 438 + } 439 + } 440 + 441 + static irqreturn_t ad4000_trigger_handler(int irq, void *p) 442 + { 443 + struct iio_poll_func *pf = p; 444 + struct iio_dev *indio_dev = pf->indio_dev; 445 + struct ad4000_state *st = iio_priv(indio_dev); 446 + int ret; 447 + 448 + ret = ad4000_convert_and_acquire(st); 449 + if (ret < 0) 450 + goto err_out; 451 + 452 + iio_push_to_buffers_with_timestamp(indio_dev, &st->scan, pf->timestamp); 453 + 454 + err_out: 455 + iio_trigger_notify_done(indio_dev->trig); 456 + return IRQ_HANDLED; 457 + } 458 + 459 + static const struct iio_info ad4000_reg_access_info = { 460 + .read_raw = &ad4000_read_raw, 461 + .read_avail = &ad4000_read_avail, 462 + .write_raw = &ad4000_write_raw, 463 + .write_raw_get_fmt = &ad4000_write_raw_get_fmt, 464 + }; 465 + 466 + static const struct iio_info ad4000_info = { 467 + .read_raw = &ad4000_read_raw, 468 + }; 469 + 470 + /* 471 + * This executes a data sample transfer for when the device connections are 472 + * in "3-wire" mode, selected when the adi,sdi-pin device tree property is 473 + * absent or set to "high". In this connection mode, the ADC SDI pin is 474 + * connected to MOSI or to VIO and ADC CNV pin is connected either to a SPI 475 + * controller CS or to a GPIO. 476 + * AD4000 series of devices initiate conversions on the rising edge of CNV pin. 477 + * 478 + * If the CNV pin is connected to an SPI controller CS line (which is by default 479 + * active low), the ADC readings would have a latency (delay) of one read. 480 + * Moreover, since we also do ADC sampling for filling the buffer on triggered 481 + * buffer mode, the timestamps of buffer readings would be disarranged. 482 + * To prevent the read latency and reduce the time discrepancy between the 483 + * sample read request and the time of actual sampling by the ADC, do a 484 + * preparatory transfer to pulse the CS/CNV line. 485 + */ 486 + static int ad4000_prepare_3wire_mode_message(struct ad4000_state *st, 487 + const struct iio_chan_spec *chan) 488 + { 489 + unsigned int cnv_pulse_time = AD4000_TCONV_NS; 490 + struct spi_transfer *xfers = st->xfers; 491 + 492 + xfers[0].cs_change = 1; 493 + xfers[0].cs_change_delay.value = cnv_pulse_time; 494 + xfers[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 495 + 496 + xfers[1].rx_buf = &st->scan.data; 497 + xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); 498 + xfers[1].delay.value = AD4000_TQUIET2_NS; 499 + xfers[1].delay.unit = SPI_DELAY_UNIT_NSECS; 500 + 501 + spi_message_init_with_transfers(&st->msg, st->xfers, 2); 502 + 503 + return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); 504 + } 505 + 506 + /* 507 + * This executes a data sample transfer for when the device connections are 508 + * in "4-wire" mode, selected when the adi,sdi-pin device tree property is 509 + * set to "cs". In this connection mode, the controller CS pin is connected to 510 + * ADC SDI pin and a GPIO is connected to ADC CNV pin. 511 + * The GPIO connected to ADC CNV pin is set outside of the SPI transfer. 512 + */ 513 + static int ad4000_prepare_4wire_mode_message(struct ad4000_state *st, 514 + const struct iio_chan_spec *chan) 515 + { 516 + unsigned int cnv_to_sdi_time = AD4000_TCONV_NS; 517 + struct spi_transfer *xfers = st->xfers; 518 + 519 + /* 520 + * Dummy transfer to cause enough delay between CNV going high and SDI 521 + * going low. 522 + */ 523 + xfers[0].cs_off = 1; 524 + xfers[0].delay.value = cnv_to_sdi_time; 525 + xfers[0].delay.unit = SPI_DELAY_UNIT_NSECS; 526 + 527 + xfers[1].rx_buf = &st->scan.data; 528 + xfers[1].len = BITS_TO_BYTES(chan->scan_type.storagebits); 529 + 530 + spi_message_init_with_transfers(&st->msg, st->xfers, 2); 531 + 532 + return devm_spi_optimize_message(&st->spi->dev, st->spi, &st->msg); 533 + } 534 + 535 + static int ad4000_config(struct ad4000_state *st) 536 + { 537 + unsigned int reg_val = AD4000_CONFIG_REG_DEFAULT; 538 + 539 + if (device_property_present(&st->spi->dev, "adi,high-z-input")) 540 + reg_val |= FIELD_PREP(AD4000_CFG_HIGHZ, 1); 541 + 542 + return ad4000_write_reg(st, reg_val); 543 + } 544 + 545 + static int ad4000_probe(struct spi_device *spi) 546 + { 547 + const struct ad4000_chip_info *chip; 548 + struct device *dev = &spi->dev; 549 + struct iio_dev *indio_dev; 550 + struct ad4000_state *st; 551 + int gain_idx, ret; 552 + 553 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 554 + if (!indio_dev) 555 + return -ENOMEM; 556 + 557 + chip = spi_get_device_match_data(spi); 558 + if (!chip) 559 + return -EINVAL; 560 + 561 + st = iio_priv(indio_dev); 562 + st->spi = spi; 563 + 564 + ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(ad4000_power_supplies), 565 + ad4000_power_supplies); 566 + if (ret) 567 + return dev_err_probe(dev, ret, "Failed to enable power supplies\n"); 568 + 569 + ret = devm_regulator_get_enable_read_voltage(dev, "ref"); 570 + if (ret < 0) 571 + return dev_err_probe(dev, ret, 572 + "Failed to get ref regulator reference\n"); 573 + st->vref_mv = ret / 1000; 574 + 575 + st->cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_HIGH); 576 + if (IS_ERR(st->cnv_gpio)) 577 + return dev_err_probe(dev, PTR_ERR(st->cnv_gpio), 578 + "Failed to get CNV GPIO"); 579 + 580 + ret = device_property_match_property_string(dev, "adi,sdi-pin", 581 + ad4000_sdi_pin, 582 + ARRAY_SIZE(ad4000_sdi_pin)); 583 + if (ret < 0 && ret != -EINVAL) 584 + return dev_err_probe(dev, ret, 585 + "getting adi,sdi-pin property failed\n"); 586 + 587 + /* Default to usual SPI connections if pin properties are not present */ 588 + st->sdi_pin = ret == -EINVAL ? AD4000_SDI_MOSI : ret; 589 + switch (st->sdi_pin) { 590 + case AD4000_SDI_MOSI: 591 + indio_dev->info = &ad4000_reg_access_info; 592 + indio_dev->channels = &chip->reg_access_chan_spec; 593 + 594 + /* 595 + * In "3-wire mode", the ADC SDI line must be kept high when 596 + * data is not being clocked out of the controller. 597 + * Request the SPI controller to make MOSI idle high. 598 + */ 599 + spi->mode |= SPI_MOSI_IDLE_HIGH; 600 + ret = spi_setup(spi); 601 + if (ret < 0) 602 + return ret; 603 + 604 + ret = ad4000_prepare_3wire_mode_message(st, indio_dev->channels); 605 + if (ret) 606 + return ret; 607 + 608 + ret = ad4000_config(st); 609 + if (ret < 0) 610 + return dev_err_probe(dev, ret, "Failed to config device\n"); 611 + 612 + break; 613 + case AD4000_SDI_VIO: 614 + indio_dev->info = &ad4000_info; 615 + indio_dev->channels = &chip->chan_spec; 616 + ret = ad4000_prepare_3wire_mode_message(st, indio_dev->channels); 617 + if (ret) 618 + return ret; 619 + 620 + break; 621 + case AD4000_SDI_CS: 622 + indio_dev->info = &ad4000_info; 623 + indio_dev->channels = &chip->chan_spec; 624 + ret = ad4000_prepare_4wire_mode_message(st, indio_dev->channels); 625 + if (ret) 626 + return ret; 627 + 628 + break; 629 + case AD4000_SDI_GND: 630 + return dev_err_probe(dev, -EPROTONOSUPPORT, 631 + "Unsupported connection mode\n"); 632 + 633 + default: 634 + return dev_err_probe(dev, -EINVAL, "Unrecognized connection mode\n"); 635 + } 636 + 637 + indio_dev->name = chip->dev_name; 638 + indio_dev->num_channels = 1; 639 + 640 + devm_mutex_init(dev, &st->lock); 641 + 642 + st->gain_milli = 1000; 643 + if (chip->has_hardware_gain) { 644 + ret = device_property_read_u16(dev, "adi,gain-milli", 645 + &st->gain_milli); 646 + if (!ret) { 647 + /* Match gain value from dt to one of supported gains */ 648 + gain_idx = find_closest(st->gain_milli, ad4000_gains, 649 + ARRAY_SIZE(ad4000_gains)); 650 + st->gain_milli = ad4000_gains[gain_idx]; 651 + } else { 652 + return dev_err_probe(dev, ret, 653 + "Failed to read gain property\n"); 654 + } 655 + } 656 + 657 + ad4000_fill_scale_tbl(st, indio_dev->channels); 658 + 659 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 660 + &iio_pollfunc_store_time, 661 + &ad4000_trigger_handler, NULL); 662 + if (ret) 663 + return ret; 664 + 665 + return devm_iio_device_register(dev, indio_dev); 666 + } 667 + 668 + static const struct spi_device_id ad4000_id[] = { 669 + { "ad4000", (kernel_ulong_t)&ad4000_chip_info }, 670 + { "ad4001", (kernel_ulong_t)&ad4001_chip_info }, 671 + { "ad4002", (kernel_ulong_t)&ad4002_chip_info }, 672 + { "ad4003", (kernel_ulong_t)&ad4003_chip_info }, 673 + { "ad4004", (kernel_ulong_t)&ad4004_chip_info }, 674 + { "ad4005", (kernel_ulong_t)&ad4005_chip_info }, 675 + { "ad4006", (kernel_ulong_t)&ad4006_chip_info }, 676 + { "ad4007", (kernel_ulong_t)&ad4007_chip_info }, 677 + { "ad4008", (kernel_ulong_t)&ad4008_chip_info }, 678 + { "ad4010", (kernel_ulong_t)&ad4010_chip_info }, 679 + { "ad4011", (kernel_ulong_t)&ad4011_chip_info }, 680 + { "ad4020", (kernel_ulong_t)&ad4020_chip_info }, 681 + { "ad4021", (kernel_ulong_t)&ad4021_chip_info }, 682 + { "ad4022", (kernel_ulong_t)&ad4022_chip_info }, 683 + { "adaq4001", (kernel_ulong_t)&adaq4001_chip_info }, 684 + { "adaq4003", (kernel_ulong_t)&adaq4003_chip_info }, 685 + { } 686 + }; 687 + MODULE_DEVICE_TABLE(spi, ad4000_id); 688 + 689 + static const struct of_device_id ad4000_of_match[] = { 690 + { .compatible = "adi,ad4000", .data = &ad4000_chip_info }, 691 + { .compatible = "adi,ad4001", .data = &ad4001_chip_info }, 692 + { .compatible = "adi,ad4002", .data = &ad4002_chip_info }, 693 + { .compatible = "adi,ad4003", .data = &ad4003_chip_info }, 694 + { .compatible = "adi,ad4004", .data = &ad4004_chip_info }, 695 + { .compatible = "adi,ad4005", .data = &ad4005_chip_info }, 696 + { .compatible = "adi,ad4006", .data = &ad4006_chip_info }, 697 + { .compatible = "adi,ad4007", .data = &ad4007_chip_info }, 698 + { .compatible = "adi,ad4008", .data = &ad4008_chip_info }, 699 + { .compatible = "adi,ad4010", .data = &ad4010_chip_info }, 700 + { .compatible = "adi,ad4011", .data = &ad4011_chip_info }, 701 + { .compatible = "adi,ad4020", .data = &ad4020_chip_info }, 702 + { .compatible = "adi,ad4021", .data = &ad4021_chip_info }, 703 + { .compatible = "adi,ad4022", .data = &ad4022_chip_info }, 704 + { .compatible = "adi,adaq4001", .data = &adaq4001_chip_info }, 705 + { .compatible = "adi,adaq4003", .data = &adaq4003_chip_info }, 706 + { } 707 + }; 708 + MODULE_DEVICE_TABLE(of, ad4000_of_match); 709 + 710 + static struct spi_driver ad4000_driver = { 711 + .driver = { 712 + .name = "ad4000", 713 + .of_match_table = ad4000_of_match, 714 + }, 715 + .probe = ad4000_probe, 716 + .id_table = ad4000_id, 717 + }; 718 + module_spi_driver(ad4000_driver); 719 + 720 + MODULE_AUTHOR("Marcelo Schmitt <marcelo.schmitt@analog.com>"); 721 + MODULE_DESCRIPTION("Analog Devices AD4000 ADC driver"); 722 + MODULE_LICENSE("GPL");
+978
drivers/iio/adc/ad4695.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * SPI ADC driver for Analog Devices Inc. AD4695 and similar chips 4 + * 5 + * https://www.analog.com/en/products/ad4695.html 6 + * https://www.analog.com/en/products/ad4696.html 7 + * https://www.analog.com/en/products/ad4697.html 8 + * https://www.analog.com/en/products/ad4698.html 9 + * 10 + * Copyright 2024 Analog Devices Inc. 11 + * Copyright 2024 BayLibre, SAS 12 + */ 13 + 14 + #include <linux/align.h> 15 + #include <linux/bitfield.h> 16 + #include <linux/bits.h> 17 + #include <linux/compiler.h> 18 + #include <linux/delay.h> 19 + #include <linux/device.h> 20 + #include <linux/err.h> 21 + #include <linux/gpio/consumer.h> 22 + #include <linux/iio/buffer.h> 23 + #include <linux/iio/iio.h> 24 + #include <linux/iio/triggered_buffer.h> 25 + #include <linux/iio/trigger_consumer.h> 26 + #include <linux/property.h> 27 + #include <linux/regmap.h> 28 + #include <linux/regulator/consumer.h> 29 + #include <linux/spi/spi.h> 30 + #include <linux/units.h> 31 + 32 + #include <dt-bindings/iio/adi,ad4695.h> 33 + 34 + /* AD4695 registers */ 35 + #define AD4695_REG_SPI_CONFIG_A 0x0000 36 + #define AD4695_REG_SPI_CONFIG_A_SW_RST (BIT(7) | BIT(0)) 37 + #define AD4695_REG_SPI_CONFIG_B 0x0001 38 + #define AD4695_REG_SPI_CONFIG_B_INST_MODE BIT(7) 39 + #define AD4695_REG_DEVICE_TYPE 0x0003 40 + #define AD4695_REG_SCRATCH_PAD 0x000A 41 + #define AD4695_REG_VENDOR_L 0x000C 42 + #define AD4695_REG_VENDOR_H 0x000D 43 + #define AD4695_REG_LOOP_MODE 0x000E 44 + #define AD4695_REG_SPI_CONFIG_C 0x0010 45 + #define AD4695_REG_SPI_CONFIG_C_MB_STRICT BIT(7) 46 + #define AD4695_REG_SPI_STATUS 0x0011 47 + #define AD4695_REG_STATUS 0x0014 48 + #define AD4695_REG_ALERT_STATUS1 0x0015 49 + #define AD4695_REG_ALERT_STATUS2 0x0016 50 + #define AD4695_REG_CLAMP_STATUS 0x001A 51 + #define AD4695_REG_SETUP 0x0020 52 + #define AD4695_REG_SETUP_LDO_EN BIT(4) 53 + #define AD4695_REG_SETUP_SPI_MODE BIT(2) 54 + #define AD4695_REG_SETUP_SPI_CYC_CTRL BIT(1) 55 + #define AD4695_REG_REF_CTRL 0x0021 56 + #define AD4695_REG_REF_CTRL_OV_MODE BIT(7) 57 + #define AD4695_REG_REF_CTRL_VREF_SET GENMASK(4, 2) 58 + #define AD4695_REG_REF_CTRL_REFHIZ_EN BIT(1) 59 + #define AD4695_REG_REF_CTRL_REFBUF_EN BIT(0) 60 + #define AD4695_REG_SEQ_CTRL 0x0022 61 + #define AD4695_REG_SEQ_CTRL_STD_SEQ_EN BIT(7) 62 + #define AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS GENMASK(6, 0) 63 + #define AD4695_REG_AC_CTRL 0x0023 64 + #define AD4695_REG_STD_SEQ_CONFIG 0x0024 65 + #define AD4695_REG_GPIO_CTRL 0x0026 66 + #define AD4695_REG_GP_MODE 0x0027 67 + #define AD4695_REG_TEMP_CTRL 0x0029 68 + #define AD4695_REG_TEMP_CTRL_TEMP_EN BIT(0) 69 + #define AD4695_REG_CONFIG_IN(n) (0x0030 | (n)) 70 + #define AD4695_REG_CONFIG_IN_MODE BIT(6) 71 + #define AD4695_REG_CONFIG_IN_PAIR GENMASK(5, 4) 72 + #define AD4695_REG_CONFIG_IN_AINHIGHZ_EN BIT(3) 73 + #define AD4695_REG_UPPER_IN(n) (0x0040 | (2 * (n))) 74 + #define AD4695_REG_LOWER_IN(n) (0x0060 | (2 * (n))) 75 + #define AD4695_REG_HYST_IN(n) (0x0080 | (2 * (n))) 76 + #define AD4695_REG_OFFSET_IN(n) (0x00A0 | (2 * (n))) 77 + #define AD4695_REG_GAIN_IN(n) (0x00C0 | (2 * (n))) 78 + #define AD4695_REG_AS_SLOT(n) (0x0100 | (n)) 79 + #define AD4695_REG_AS_SLOT_INX GENMASK(3, 0) 80 + #define AD4695_MAX_REG 0x017F 81 + 82 + /* Conversion mode commands */ 83 + #define AD4695_CMD_EXIT_CNV_MODE 0x0A 84 + #define AD4695_CMD_TEMP_CHAN 0x0F 85 + #define AD4695_CMD_VOLTAGE_CHAN(n) (0x10 | (n)) 86 + 87 + /* timing specs */ 88 + #define AD4695_T_CONVERT_NS 415 89 + #define AD4695_T_WAKEUP_HW_MS 3 90 + #define AD4695_T_WAKEUP_SW_MS 3 91 + #define AD4695_T_REFBUF_MS 100 92 + #define AD4695_T_REGCONFIG_NS 20 93 + #define AD4695_REG_ACCESS_SCLK_HZ (10 * MEGA) 94 + 95 + /* Max number of voltage input channels. */ 96 + #define AD4695_MAX_CHANNELS 16 97 + /* Max size of 1 raw sample in bytes. */ 98 + #define AD4695_MAX_CHANNEL_SIZE 2 99 + 100 + enum ad4695_in_pair { 101 + AD4695_IN_PAIR_REFGND, 102 + AD4695_IN_PAIR_COM, 103 + AD4695_IN_PAIR_EVEN_ODD, 104 + }; 105 + 106 + struct ad4695_chip_info { 107 + const char *name; 108 + int max_sample_rate; 109 + u32 t_acq_ns; 110 + u8 num_voltage_inputs; 111 + }; 112 + 113 + struct ad4695_channel_config { 114 + unsigned int channel; 115 + bool highz_en; 116 + bool bipolar; 117 + enum ad4695_in_pair pin_pairing; 118 + unsigned int common_mode_mv; 119 + }; 120 + 121 + struct ad4695_state { 122 + struct spi_device *spi; 123 + struct regmap *regmap; 124 + struct gpio_desc *reset_gpio; 125 + /* voltages channels plus temperature and timestamp */ 126 + struct iio_chan_spec iio_chan[AD4695_MAX_CHANNELS + 2]; 127 + struct ad4695_channel_config channels_cfg[AD4695_MAX_CHANNELS]; 128 + const struct ad4695_chip_info *chip_info; 129 + /* Reference voltage. */ 130 + unsigned int vref_mv; 131 + /* Common mode input pin voltage. */ 132 + unsigned int com_mv; 133 + /* 1 per voltage and temperature chan plus 1 xfer to trigger 1st CNV */ 134 + struct spi_transfer buf_read_xfer[AD4695_MAX_CHANNELS + 2]; 135 + struct spi_message buf_read_msg; 136 + /* Raw conversion data received. */ 137 + u8 buf[ALIGN((AD4695_MAX_CHANNELS + 2) * AD4695_MAX_CHANNEL_SIZE, 138 + sizeof(s64)) + sizeof(s64)] __aligned(IIO_DMA_MINALIGN); 139 + u16 raw_data; 140 + /* Commands to send for single conversion. */ 141 + u16 cnv_cmd; 142 + u8 cnv_cmd2; 143 + }; 144 + 145 + static const struct regmap_range ad4695_regmap_rd_ranges[] = { 146 + regmap_reg_range(AD4695_REG_SPI_CONFIG_A, AD4695_REG_SPI_CONFIG_B), 147 + regmap_reg_range(AD4695_REG_DEVICE_TYPE, AD4695_REG_DEVICE_TYPE), 148 + regmap_reg_range(AD4695_REG_SCRATCH_PAD, AD4695_REG_SCRATCH_PAD), 149 + regmap_reg_range(AD4695_REG_VENDOR_L, AD4695_REG_LOOP_MODE), 150 + regmap_reg_range(AD4695_REG_SPI_CONFIG_C, AD4695_REG_SPI_STATUS), 151 + regmap_reg_range(AD4695_REG_STATUS, AD4695_REG_ALERT_STATUS2), 152 + regmap_reg_range(AD4695_REG_CLAMP_STATUS, AD4695_REG_CLAMP_STATUS), 153 + regmap_reg_range(AD4695_REG_SETUP, AD4695_REG_TEMP_CTRL), 154 + regmap_reg_range(AD4695_REG_CONFIG_IN(0), AD4695_MAX_REG), 155 + }; 156 + 157 + static const struct regmap_access_table ad4695_regmap_rd_table = { 158 + .yes_ranges = ad4695_regmap_rd_ranges, 159 + .n_yes_ranges = ARRAY_SIZE(ad4695_regmap_rd_ranges), 160 + }; 161 + 162 + static const struct regmap_range ad4695_regmap_wr_ranges[] = { 163 + regmap_reg_range(AD4695_REG_SPI_CONFIG_A, AD4695_REG_SPI_CONFIG_B), 164 + regmap_reg_range(AD4695_REG_SCRATCH_PAD, AD4695_REG_SCRATCH_PAD), 165 + regmap_reg_range(AD4695_REG_LOOP_MODE, AD4695_REG_LOOP_MODE), 166 + regmap_reg_range(AD4695_REG_SPI_CONFIG_C, AD4695_REG_SPI_STATUS), 167 + regmap_reg_range(AD4695_REG_SETUP, AD4695_REG_TEMP_CTRL), 168 + regmap_reg_range(AD4695_REG_CONFIG_IN(0), AD4695_MAX_REG), 169 + }; 170 + 171 + static const struct regmap_access_table ad4695_regmap_wr_table = { 172 + .yes_ranges = ad4695_regmap_wr_ranges, 173 + .n_yes_ranges = ARRAY_SIZE(ad4695_regmap_wr_ranges), 174 + }; 175 + 176 + static const struct regmap_config ad4695_regmap_config = { 177 + .name = "ad4695", 178 + .reg_bits = 16, 179 + .val_bits = 8, 180 + .max_register = AD4695_MAX_REG, 181 + .rd_table = &ad4695_regmap_rd_table, 182 + .wr_table = &ad4695_regmap_wr_table, 183 + .can_multi_write = true, 184 + }; 185 + 186 + static const struct iio_chan_spec ad4695_channel_template = { 187 + .type = IIO_VOLTAGE, 188 + .indexed = 1, 189 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 190 + BIT(IIO_CHAN_INFO_SCALE) | 191 + BIT(IIO_CHAN_INFO_OFFSET), 192 + .scan_type = { 193 + .sign = 'u', 194 + .realbits = 16, 195 + .storagebits = 16, 196 + }, 197 + }; 198 + 199 + static const struct iio_chan_spec ad4695_temp_channel_template = { 200 + .address = AD4695_CMD_TEMP_CHAN, 201 + .type = IIO_TEMP, 202 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 203 + BIT(IIO_CHAN_INFO_SCALE) | 204 + BIT(IIO_CHAN_INFO_OFFSET), 205 + .scan_type = { 206 + .sign = 's', 207 + .realbits = 16, 208 + .storagebits = 16, 209 + }, 210 + }; 211 + 212 + static const struct iio_chan_spec ad4695_soft_timestamp_channel_template = 213 + IIO_CHAN_SOFT_TIMESTAMP(0); 214 + 215 + static const char * const ad4695_power_supplies[] = { 216 + "avdd", "vio" 217 + }; 218 + 219 + static const struct ad4695_chip_info ad4695_chip_info = { 220 + .name = "ad4695", 221 + .max_sample_rate = 500 * KILO, 222 + .t_acq_ns = 1715, 223 + .num_voltage_inputs = 16, 224 + }; 225 + 226 + static const struct ad4695_chip_info ad4696_chip_info = { 227 + .name = "ad4696", 228 + .max_sample_rate = 1 * MEGA, 229 + .t_acq_ns = 715, 230 + .num_voltage_inputs = 16, 231 + }; 232 + 233 + static const struct ad4695_chip_info ad4697_chip_info = { 234 + .name = "ad4697", 235 + .max_sample_rate = 500 * KILO, 236 + .t_acq_ns = 1715, 237 + .num_voltage_inputs = 8, 238 + }; 239 + 240 + static const struct ad4695_chip_info ad4698_chip_info = { 241 + .name = "ad4698", 242 + .max_sample_rate = 1 * MEGA, 243 + .t_acq_ns = 715, 244 + .num_voltage_inputs = 8, 245 + }; 246 + 247 + /** 248 + * ad4695_set_single_cycle_mode - Set the device in single cycle mode 249 + * @st: The AD4695 state 250 + * @channel: The first channel to read 251 + * 252 + * As per the datasheet, to enable single cycle mode, we need to set 253 + * STD_SEQ_EN=0, NUM_SLOTS_AS=0 and CYC_CTRL=1 (Table 15). Setting SPI_MODE=1 254 + * triggers the first conversion using the channel in AS_SLOT0. 255 + * 256 + * Context: can sleep, must be called with iio_device_claim_direct held 257 + * Return: 0 on success, a negative error code on failure 258 + */ 259 + static int ad4695_set_single_cycle_mode(struct ad4695_state *st, 260 + unsigned int channel) 261 + { 262 + int ret; 263 + 264 + ret = regmap_clear_bits(st->regmap, AD4695_REG_SEQ_CTRL, 265 + AD4695_REG_SEQ_CTRL_STD_SEQ_EN | 266 + AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS); 267 + if (ret) 268 + return ret; 269 + 270 + ret = regmap_write(st->regmap, AD4695_REG_AS_SLOT(0), 271 + FIELD_PREP(AD4695_REG_AS_SLOT_INX, channel)); 272 + if (ret) 273 + return ret; 274 + 275 + return regmap_set_bits(st->regmap, AD4695_REG_SETUP, 276 + AD4695_REG_SETUP_SPI_MODE | 277 + AD4695_REG_SETUP_SPI_CYC_CTRL); 278 + } 279 + 280 + /** 281 + * ad4695_enter_advanced_sequencer_mode - Put the ADC in advanced sequencer mode 282 + * @st: The driver state 283 + * @n: The number of slots to use - must be >= 2, <= 128 284 + * 285 + * As per the datasheet, to enable advanced sequencer, we need to set 286 + * STD_SEQ_EN=0, NUM_SLOTS_AS=n-1 and CYC_CTRL=0 (Table 15). Setting SPI_MODE=1 287 + * triggers the first conversion using the channel in AS_SLOT0. 288 + * 289 + * Return: 0 on success, a negative error code on failure 290 + */ 291 + static int ad4695_enter_advanced_sequencer_mode(struct ad4695_state *st, u32 n) 292 + { 293 + int ret; 294 + 295 + ret = regmap_update_bits(st->regmap, AD4695_REG_SEQ_CTRL, 296 + AD4695_REG_SEQ_CTRL_STD_SEQ_EN | 297 + AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS, 298 + FIELD_PREP(AD4695_REG_SEQ_CTRL_STD_SEQ_EN, 0) | 299 + FIELD_PREP(AD4695_REG_SEQ_CTRL_NUM_SLOTS_AS, n - 1)); 300 + if (ret) 301 + return ret; 302 + 303 + return regmap_update_bits(st->regmap, AD4695_REG_SETUP, 304 + AD4695_REG_SETUP_SPI_MODE | AD4695_REG_SETUP_SPI_CYC_CTRL, 305 + FIELD_PREP(AD4695_REG_SETUP_SPI_MODE, 1) | 306 + FIELD_PREP(AD4695_REG_SETUP_SPI_CYC_CTRL, 0)); 307 + } 308 + 309 + /** 310 + * ad4695_exit_conversion_mode - Exit conversion mode 311 + * @st: The AD4695 state 312 + * 313 + * Sends SPI command to exit conversion mode. 314 + * 315 + * Return: 0 on success, a negative error code on failure 316 + */ 317 + static int ad4695_exit_conversion_mode(struct ad4695_state *st) 318 + { 319 + struct spi_transfer xfer = { 320 + .tx_buf = &st->cnv_cmd2, 321 + .len = 1, 322 + .delay.value = AD4695_T_REGCONFIG_NS, 323 + .delay.unit = SPI_DELAY_UNIT_NSECS, 324 + }; 325 + 326 + /* 327 + * Technically, could do a 5-bit transfer, but shifting to start of 328 + * 8 bits instead for better SPI controller support. 329 + */ 330 + st->cnv_cmd2 = AD4695_CMD_EXIT_CNV_MODE << 3; 331 + 332 + return spi_sync_transfer(st->spi, &xfer, 1); 333 + } 334 + 335 + static int ad4695_set_ref_voltage(struct ad4695_state *st, int vref_mv) 336 + { 337 + u8 val; 338 + 339 + if (vref_mv >= 2400 && vref_mv <= 2750) 340 + val = 0; 341 + else if (vref_mv > 2750 && vref_mv <= 3250) 342 + val = 1; 343 + else if (vref_mv > 3250 && vref_mv <= 3750) 344 + val = 2; 345 + else if (vref_mv > 3750 && vref_mv <= 4500) 346 + val = 3; 347 + else if (vref_mv > 4500 && vref_mv <= 5100) 348 + val = 4; 349 + else 350 + return -EINVAL; 351 + 352 + return regmap_update_bits(st->regmap, AD4695_REG_REF_CTRL, 353 + AD4695_REG_REF_CTRL_VREF_SET, 354 + FIELD_PREP(AD4695_REG_REF_CTRL_VREF_SET, val)); 355 + } 356 + 357 + static int ad4695_write_chn_cfg(struct ad4695_state *st, 358 + struct ad4695_channel_config *cfg) 359 + { 360 + u32 mask, val; 361 + 362 + mask = AD4695_REG_CONFIG_IN_MODE; 363 + val = FIELD_PREP(AD4695_REG_CONFIG_IN_MODE, cfg->bipolar ? 1 : 0); 364 + 365 + mask |= AD4695_REG_CONFIG_IN_PAIR; 366 + val |= FIELD_PREP(AD4695_REG_CONFIG_IN_PAIR, cfg->pin_pairing); 367 + 368 + mask |= AD4695_REG_CONFIG_IN_AINHIGHZ_EN; 369 + val |= FIELD_PREP(AD4695_REG_CONFIG_IN_AINHIGHZ_EN, 370 + cfg->highz_en ? 1 : 0); 371 + 372 + return regmap_update_bits(st->regmap, 373 + AD4695_REG_CONFIG_IN(cfg->channel), 374 + mask, val); 375 + } 376 + 377 + static int ad4695_buffer_preenable(struct iio_dev *indio_dev) 378 + { 379 + struct ad4695_state *st = iio_priv(indio_dev); 380 + struct spi_transfer *xfer; 381 + u8 temp_chan_bit = st->chip_info->num_voltage_inputs; 382 + u32 bit, num_xfer, num_slots; 383 + u32 temp_en = 0; 384 + int ret; 385 + 386 + /* 387 + * We are using the advanced sequencer since it is the only way to read 388 + * multiple channels that allows individual configuration of each 389 + * voltage input channel. Slot 0 in the advanced sequencer is used to 390 + * account for the gap between trigger polls - we don't read data from 391 + * this slot. Each enabled voltage channel is assigned a slot starting 392 + * with slot 1. 393 + */ 394 + num_slots = 1; 395 + 396 + memset(st->buf_read_xfer, 0, sizeof(st->buf_read_xfer)); 397 + 398 + /* First xfer is only to trigger conversion of slot 1, so no rx. */ 399 + xfer = &st->buf_read_xfer[0]; 400 + xfer->cs_change = 1; 401 + xfer->delay.value = st->chip_info->t_acq_ns; 402 + xfer->delay.unit = SPI_DELAY_UNIT_NSECS; 403 + xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; 404 + xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 405 + num_xfer = 1; 406 + 407 + iio_for_each_active_channel(indio_dev, bit) { 408 + xfer = &st->buf_read_xfer[num_xfer]; 409 + xfer->bits_per_word = 16; 410 + xfer->rx_buf = &st->buf[(num_xfer - 1) * 2]; 411 + xfer->len = 2; 412 + xfer->cs_change = 1; 413 + xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; 414 + xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 415 + 416 + if (bit == temp_chan_bit) { 417 + temp_en = 1; 418 + } else { 419 + ret = regmap_write(st->regmap, 420 + AD4695_REG_AS_SLOT(num_slots), 421 + FIELD_PREP(AD4695_REG_AS_SLOT_INX, bit)); 422 + if (ret) 423 + return ret; 424 + 425 + num_slots++; 426 + } 427 + 428 + num_xfer++; 429 + } 430 + 431 + /* 432 + * The advanced sequencer requires that at least 2 slots are enabled. 433 + * Since slot 0 is always used for other purposes, we need only 1 434 + * enabled voltage channel to meet this requirement. If the temperature 435 + * channel is the only enabled channel, we need to add one more slot 436 + * in the sequence but not read from it. 437 + */ 438 + if (num_slots < 2) { 439 + /* move last xfer so we can insert one more xfer before it */ 440 + st->buf_read_xfer[num_xfer] = *xfer; 441 + num_xfer++; 442 + 443 + /* modify 2nd to last xfer for extra slot */ 444 + memset(xfer, 0, sizeof(*xfer)); 445 + xfer->cs_change = 1; 446 + xfer->delay.value = st->chip_info->t_acq_ns; 447 + xfer->delay.unit = SPI_DELAY_UNIT_NSECS; 448 + xfer->cs_change_delay.value = AD4695_T_CONVERT_NS; 449 + xfer->cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 450 + xfer++; 451 + 452 + /* and add the extra slot in the sequencer */ 453 + ret = regmap_write(st->regmap, 454 + AD4695_REG_AS_SLOT(num_slots), 455 + FIELD_PREP(AD4695_REG_AS_SLOT_INX, 0)); 456 + if (ret) 457 + return ret; 458 + 459 + num_slots++; 460 + } 461 + 462 + /* 463 + * Don't keep CS asserted after last xfer. Also triggers conversion of 464 + * slot 0. 465 + */ 466 + xfer->cs_change = 0; 467 + 468 + /* 469 + * Temperature channel isn't included in the sequence, but rather 470 + * controlled by setting a bit in the TEMP_CTRL register. 471 + */ 472 + 473 + ret = regmap_update_bits(st->regmap, AD4695_REG_TEMP_CTRL, 474 + AD4695_REG_TEMP_CTRL_TEMP_EN, 475 + FIELD_PREP(AD4695_REG_TEMP_CTRL_TEMP_EN, temp_en)); 476 + if (ret) 477 + return ret; 478 + 479 + spi_message_init_with_transfers(&st->buf_read_msg, st->buf_read_xfer, 480 + num_xfer); 481 + 482 + ret = spi_optimize_message(st->spi, &st->buf_read_msg); 483 + if (ret) 484 + return ret; 485 + 486 + /* This triggers conversion of slot 0. */ 487 + ret = ad4695_enter_advanced_sequencer_mode(st, num_slots); 488 + if (ret) 489 + spi_unoptimize_message(&st->buf_read_msg); 490 + 491 + return ret; 492 + } 493 + 494 + static int ad4695_buffer_postdisable(struct iio_dev *indio_dev) 495 + { 496 + struct ad4695_state *st = iio_priv(indio_dev); 497 + int ret; 498 + 499 + ret = ad4695_exit_conversion_mode(st); 500 + if (ret) 501 + return ret; 502 + 503 + spi_unoptimize_message(&st->buf_read_msg); 504 + 505 + return 0; 506 + } 507 + 508 + static const struct iio_buffer_setup_ops ad4695_buffer_setup_ops = { 509 + .preenable = ad4695_buffer_preenable, 510 + .postdisable = ad4695_buffer_postdisable, 511 + }; 512 + 513 + static irqreturn_t ad4695_trigger_handler(int irq, void *p) 514 + { 515 + struct iio_poll_func *pf = p; 516 + struct iio_dev *indio_dev = pf->indio_dev; 517 + struct ad4695_state *st = iio_priv(indio_dev); 518 + int ret; 519 + 520 + ret = spi_sync(st->spi, &st->buf_read_msg); 521 + if (ret) 522 + goto out; 523 + 524 + iio_push_to_buffers_with_timestamp(indio_dev, st->buf, pf->timestamp); 525 + 526 + out: 527 + iio_trigger_notify_done(indio_dev->trig); 528 + 529 + return IRQ_HANDLED; 530 + } 531 + 532 + /** 533 + * ad4695_read_one_sample - Read a single sample using single-cycle mode 534 + * @st: The AD4695 state 535 + * @address: The address of the channel to read 536 + * 537 + * Upon successful return, the sample will be stored in `st->raw_data`. 538 + * 539 + * Context: can sleep, must be called with iio_device_claim_direct held 540 + * Return: 0 on success, a negative error code on failure 541 + */ 542 + static int ad4695_read_one_sample(struct ad4695_state *st, unsigned int address) 543 + { 544 + struct spi_transfer xfer[2] = { }; 545 + int ret, i = 0; 546 + 547 + ret = ad4695_set_single_cycle_mode(st, address); 548 + if (ret) 549 + return ret; 550 + 551 + /* 552 + * Setting the first channel to the temperature channel isn't supported 553 + * in single-cycle mode, so we have to do an extra xfer to read the 554 + * temperature. 555 + */ 556 + if (address == AD4695_CMD_TEMP_CHAN) { 557 + /* We aren't reading, so we can make this a short xfer. */ 558 + st->cnv_cmd2 = AD4695_CMD_TEMP_CHAN << 3; 559 + xfer[0].tx_buf = &st->cnv_cmd2; 560 + xfer[0].len = 1; 561 + xfer[0].cs_change = 1; 562 + xfer[0].cs_change_delay.value = AD4695_T_CONVERT_NS; 563 + xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 564 + 565 + i = 1; 566 + } 567 + 568 + /* Then read the result and exit conversion mode. */ 569 + st->cnv_cmd = AD4695_CMD_EXIT_CNV_MODE << 11; 570 + xfer[i].bits_per_word = 16; 571 + xfer[i].tx_buf = &st->cnv_cmd; 572 + xfer[i].rx_buf = &st->raw_data; 573 + xfer[i].len = 2; 574 + 575 + return spi_sync_transfer(st->spi, xfer, i + 1); 576 + } 577 + 578 + static int ad4695_read_raw(struct iio_dev *indio_dev, 579 + struct iio_chan_spec const *chan, 580 + int *val, int *val2, long mask) 581 + { 582 + struct ad4695_state *st = iio_priv(indio_dev); 583 + struct ad4695_channel_config *cfg = &st->channels_cfg[chan->scan_index]; 584 + u8 realbits = chan->scan_type.realbits; 585 + int ret; 586 + 587 + switch (mask) { 588 + case IIO_CHAN_INFO_RAW: 589 + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 590 + ret = ad4695_read_one_sample(st, chan->address); 591 + if (ret) 592 + return ret; 593 + 594 + if (chan->scan_type.sign == 's') 595 + *val = sign_extend32(st->raw_data, realbits - 1); 596 + else 597 + *val = st->raw_data; 598 + 599 + return IIO_VAL_INT; 600 + } 601 + unreachable(); 602 + case IIO_CHAN_INFO_SCALE: 603 + switch (chan->type) { 604 + case IIO_VOLTAGE: 605 + *val = st->vref_mv; 606 + *val2 = chan->scan_type.realbits; 607 + return IIO_VAL_FRACTIONAL_LOG2; 608 + case IIO_TEMP: 609 + /* T_scale (°C) = raw * V_REF (mV) / (-1.8 mV/°C * 2^16) */ 610 + *val = st->vref_mv * -556; 611 + *val2 = 16; 612 + return IIO_VAL_FRACTIONAL_LOG2; 613 + default: 614 + return -EINVAL; 615 + } 616 + case IIO_CHAN_INFO_OFFSET: 617 + switch (chan->type) { 618 + case IIO_VOLTAGE: 619 + if (cfg->pin_pairing == AD4695_IN_PAIR_COM) 620 + *val = st->com_mv * (1 << realbits) / st->vref_mv; 621 + else if (cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD) 622 + *val = cfg->common_mode_mv * (1 << realbits) / st->vref_mv; 623 + else 624 + *val = 0; 625 + 626 + return IIO_VAL_INT; 627 + case IIO_TEMP: 628 + /* T_offset (°C) = -725 mV / (-1.8 mV/°C) */ 629 + /* T_offset (raw) = T_offset (°C) * (-1.8 mV/°C) * 2^16 / V_REF (mV) */ 630 + *val = -47513600; 631 + *val2 = st->vref_mv; 632 + return IIO_VAL_FRACTIONAL; 633 + default: 634 + return -EINVAL; 635 + } 636 + default: 637 + return -EINVAL; 638 + } 639 + } 640 + 641 + static int ad4695_debugfs_reg_access(struct iio_dev *indio_dev, 642 + unsigned int reg, 643 + unsigned int writeval, 644 + unsigned int *readval) 645 + { 646 + struct ad4695_state *st = iio_priv(indio_dev); 647 + 648 + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 649 + if (readval) 650 + return regmap_read(st->regmap, reg, readval); 651 + 652 + return regmap_write(st->regmap, reg, writeval); 653 + } 654 + 655 + unreachable(); 656 + } 657 + 658 + static const struct iio_info ad4695_info = { 659 + .read_raw = &ad4695_read_raw, 660 + .debugfs_reg_access = &ad4695_debugfs_reg_access, 661 + }; 662 + 663 + static int ad4695_parse_channel_cfg(struct ad4695_state *st) 664 + { 665 + struct device *dev = &st->spi->dev; 666 + struct ad4695_channel_config *chan_cfg; 667 + struct iio_chan_spec *iio_chan; 668 + int ret, i; 669 + 670 + /* populate defaults */ 671 + for (i = 0; i < st->chip_info->num_voltage_inputs; i++) { 672 + chan_cfg = &st->channels_cfg[i]; 673 + iio_chan = &st->iio_chan[i]; 674 + 675 + chan_cfg->highz_en = true; 676 + chan_cfg->channel = i; 677 + 678 + *iio_chan = ad4695_channel_template; 679 + iio_chan->channel = i; 680 + iio_chan->scan_index = i; 681 + iio_chan->address = AD4695_CMD_VOLTAGE_CHAN(i); 682 + } 683 + 684 + /* modify based on firmware description */ 685 + device_for_each_child_node_scoped(dev, child) { 686 + u32 reg, val; 687 + 688 + ret = fwnode_property_read_u32(child, "reg", &reg); 689 + if (ret) 690 + return dev_err_probe(dev, ret, 691 + "failed to read reg property (%s)\n", 692 + fwnode_get_name(child)); 693 + 694 + if (reg >= st->chip_info->num_voltage_inputs) 695 + return dev_err_probe(dev, -EINVAL, 696 + "reg out of range (%s)\n", 697 + fwnode_get_name(child)); 698 + 699 + iio_chan = &st->iio_chan[reg]; 700 + chan_cfg = &st->channels_cfg[reg]; 701 + 702 + chan_cfg->highz_en = 703 + !fwnode_property_read_bool(child, "adi,no-high-z"); 704 + chan_cfg->bipolar = fwnode_property_read_bool(child, "bipolar"); 705 + 706 + ret = fwnode_property_read_u32(child, "common-mode-channel", 707 + &val); 708 + if (ret && ret != -EINVAL) 709 + return dev_err_probe(dev, ret, 710 + "failed to read common-mode-channel (%s)\n", 711 + fwnode_get_name(child)); 712 + 713 + if (ret == -EINVAL || val == AD4695_COMMON_MODE_REFGND) 714 + chan_cfg->pin_pairing = AD4695_IN_PAIR_REFGND; 715 + else if (val == AD4695_COMMON_MODE_COM) 716 + chan_cfg->pin_pairing = AD4695_IN_PAIR_COM; 717 + else 718 + chan_cfg->pin_pairing = AD4695_IN_PAIR_EVEN_ODD; 719 + 720 + if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD && 721 + val % 2 == 0) 722 + return dev_err_probe(dev, -EINVAL, 723 + "common-mode-channel must be odd number (%s)\n", 724 + fwnode_get_name(child)); 725 + 726 + if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD && 727 + val != reg + 1) 728 + return dev_err_probe(dev, -EINVAL, 729 + "common-mode-channel must be next consecutive channel (%s)\n", 730 + fwnode_get_name(child)); 731 + 732 + if (chan_cfg->pin_pairing == AD4695_IN_PAIR_EVEN_ODD) { 733 + char name[5]; 734 + 735 + snprintf(name, sizeof(name), "in%d", reg + 1); 736 + 737 + ret = devm_regulator_get_enable_read_voltage(dev, name); 738 + if (ret < 0) 739 + return dev_err_probe(dev, ret, 740 + "failed to get %s voltage (%s)\n", 741 + name, fwnode_get_name(child)); 742 + 743 + chan_cfg->common_mode_mv = ret / 1000; 744 + } 745 + 746 + if (chan_cfg->bipolar && 747 + chan_cfg->pin_pairing == AD4695_IN_PAIR_REFGND) 748 + return dev_err_probe(dev, -EINVAL, 749 + "bipolar mode is not available for inputs paired with REFGND (%s).\n", 750 + fwnode_get_name(child)); 751 + 752 + if (chan_cfg->bipolar) 753 + iio_chan->scan_type.sign = 's'; 754 + 755 + ret = ad4695_write_chn_cfg(st, chan_cfg); 756 + if (ret) 757 + return ret; 758 + } 759 + 760 + /* Temperature channel must be next scan index after voltage channels. */ 761 + st->iio_chan[i] = ad4695_temp_channel_template; 762 + st->iio_chan[i].scan_index = i; 763 + i++; 764 + 765 + st->iio_chan[i] = ad4695_soft_timestamp_channel_template; 766 + st->iio_chan[i].scan_index = i; 767 + 768 + return 0; 769 + } 770 + 771 + static int ad4695_probe(struct spi_device *spi) 772 + { 773 + struct device *dev = &spi->dev; 774 + struct ad4695_state *st; 775 + struct iio_dev *indio_dev; 776 + struct gpio_desc *cnv_gpio; 777 + bool use_internal_ldo_supply; 778 + bool use_internal_ref_buffer; 779 + int ret; 780 + 781 + cnv_gpio = devm_gpiod_get_optional(dev, "cnv", GPIOD_OUT_LOW); 782 + if (IS_ERR(cnv_gpio)) 783 + return dev_err_probe(dev, PTR_ERR(cnv_gpio), 784 + "Failed to get CNV GPIO\n"); 785 + 786 + /* Driver currently requires CNV pin to be connected to SPI CS */ 787 + if (cnv_gpio) 788 + return dev_err_probe(dev, -ENODEV, 789 + "CNV GPIO is not supported\n"); 790 + 791 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 792 + if (!indio_dev) 793 + return -ENOMEM; 794 + 795 + st = iio_priv(indio_dev); 796 + st->spi = spi; 797 + 798 + st->chip_info = spi_get_device_match_data(spi); 799 + if (!st->chip_info) 800 + return -EINVAL; 801 + 802 + /* Registers cannot be read at the max allowable speed */ 803 + spi->max_speed_hz = AD4695_REG_ACCESS_SCLK_HZ; 804 + 805 + st->regmap = devm_regmap_init_spi(spi, &ad4695_regmap_config); 806 + if (IS_ERR(st->regmap)) 807 + return dev_err_probe(dev, PTR_ERR(st->regmap), 808 + "Failed to initialize regmap\n"); 809 + 810 + ret = devm_regulator_bulk_get_enable(dev, 811 + ARRAY_SIZE(ad4695_power_supplies), 812 + ad4695_power_supplies); 813 + if (ret) 814 + return dev_err_probe(dev, ret, 815 + "Failed to enable power supplies\n"); 816 + 817 + /* If LDO_IN supply is present, then we are using internal LDO. */ 818 + ret = devm_regulator_get_enable_optional(dev, "ldo-in"); 819 + if (ret < 0 && ret != -ENODEV) 820 + return dev_err_probe(dev, ret, 821 + "Failed to enable LDO_IN supply\n"); 822 + 823 + use_internal_ldo_supply = ret == 0; 824 + 825 + if (!use_internal_ldo_supply) { 826 + /* Otherwise we need an external VDD supply. */ 827 + ret = devm_regulator_get_enable(dev, "vdd"); 828 + if (ret < 0) 829 + return dev_err_probe(dev, ret, 830 + "Failed to enable VDD supply\n"); 831 + } 832 + 833 + /* If REFIN supply is given, then we are using internal buffer */ 834 + ret = devm_regulator_get_enable_read_voltage(dev, "refin"); 835 + if (ret < 0 && ret != -ENODEV) 836 + return dev_err_probe(dev, ret, "Failed to get REFIN voltage\n"); 837 + 838 + if (ret != -ENODEV) { 839 + st->vref_mv = ret / 1000; 840 + use_internal_ref_buffer = true; 841 + } else { 842 + /* Otherwise, we need an external reference. */ 843 + ret = devm_regulator_get_enable_read_voltage(dev, "ref"); 844 + if (ret < 0) 845 + return dev_err_probe(dev, ret, 846 + "Failed to get REF voltage\n"); 847 + 848 + st->vref_mv = ret / 1000; 849 + use_internal_ref_buffer = false; 850 + } 851 + 852 + ret = devm_regulator_get_enable_read_voltage(dev, "com"); 853 + if (ret < 0 && ret != -ENODEV) 854 + return dev_err_probe(dev, ret, "Failed to get COM voltage\n"); 855 + 856 + st->com_mv = ret == -ENODEV ? 0 : ret / 1000; 857 + 858 + /* 859 + * Reset the device using hardware reset if available or fall back to 860 + * software reset. 861 + */ 862 + 863 + st->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH); 864 + if (IS_ERR(st->reset_gpio)) 865 + return PTR_ERR(st->reset_gpio); 866 + 867 + if (st->reset_gpio) { 868 + gpiod_set_value(st->reset_gpio, 0); 869 + msleep(AD4695_T_WAKEUP_HW_MS); 870 + } else { 871 + ret = regmap_write(st->regmap, AD4695_REG_SPI_CONFIG_A, 872 + AD4695_REG_SPI_CONFIG_A_SW_RST); 873 + if (ret) 874 + return ret; 875 + 876 + msleep(AD4695_T_WAKEUP_SW_MS); 877 + } 878 + 879 + /* Needed for debugfs since it only access registers 1 byte at a time. */ 880 + ret = regmap_set_bits(st->regmap, AD4695_REG_SPI_CONFIG_C, 881 + AD4695_REG_SPI_CONFIG_C_MB_STRICT); 882 + if (ret) 883 + return ret; 884 + 885 + /* Disable internal LDO if it isn't needed. */ 886 + ret = regmap_update_bits(st->regmap, AD4695_REG_SETUP, 887 + AD4695_REG_SETUP_LDO_EN, 888 + FIELD_PREP(AD4695_REG_SETUP_LDO_EN, 889 + use_internal_ldo_supply ? 1 : 0)); 890 + if (ret) 891 + return ret; 892 + 893 + /* configure reference supply */ 894 + 895 + if (device_property_present(dev, "adi,no-ref-current-limit")) { 896 + ret = regmap_set_bits(st->regmap, AD4695_REG_REF_CTRL, 897 + AD4695_REG_REF_CTRL_OV_MODE); 898 + if (ret) 899 + return ret; 900 + } 901 + 902 + if (device_property_present(dev, "adi,no-ref-high-z")) { 903 + if (use_internal_ref_buffer) 904 + return dev_err_probe(dev, -EINVAL, 905 + "Cannot disable high-Z mode for internal reference buffer\n"); 906 + 907 + ret = regmap_clear_bits(st->regmap, AD4695_REG_REF_CTRL, 908 + AD4695_REG_REF_CTRL_REFHIZ_EN); 909 + if (ret) 910 + return ret; 911 + } 912 + 913 + ret = ad4695_set_ref_voltage(st, st->vref_mv); 914 + if (ret) 915 + return ret; 916 + 917 + if (use_internal_ref_buffer) { 918 + ret = regmap_set_bits(st->regmap, AD4695_REG_REF_CTRL, 919 + AD4695_REG_REF_CTRL_REFBUF_EN); 920 + if (ret) 921 + return ret; 922 + 923 + /* Give the capacitor some time to charge up. */ 924 + msleep(AD4695_T_REFBUF_MS); 925 + } 926 + 927 + ret = ad4695_parse_channel_cfg(st); 928 + if (ret) 929 + return ret; 930 + 931 + indio_dev->name = st->chip_info->name; 932 + indio_dev->info = &ad4695_info; 933 + indio_dev->modes = INDIO_DIRECT_MODE; 934 + indio_dev->channels = st->iio_chan; 935 + indio_dev->num_channels = st->chip_info->num_voltage_inputs + 2; 936 + 937 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 938 + iio_pollfunc_store_time, 939 + ad4695_trigger_handler, 940 + &ad4695_buffer_setup_ops); 941 + if (ret) 942 + return ret; 943 + 944 + return devm_iio_device_register(dev, indio_dev); 945 + } 946 + 947 + static const struct spi_device_id ad4695_spi_id_table[] = { 948 + { .name = "ad4695", .driver_data = (kernel_ulong_t)&ad4695_chip_info }, 949 + { .name = "ad4696", .driver_data = (kernel_ulong_t)&ad4696_chip_info }, 950 + { .name = "ad4697", .driver_data = (kernel_ulong_t)&ad4697_chip_info }, 951 + { .name = "ad4698", .driver_data = (kernel_ulong_t)&ad4698_chip_info }, 952 + { } 953 + }; 954 + MODULE_DEVICE_TABLE(spi, ad4695_spi_id_table); 955 + 956 + static const struct of_device_id ad4695_of_match_table[] = { 957 + { .compatible = "adi,ad4695", .data = &ad4695_chip_info, }, 958 + { .compatible = "adi,ad4696", .data = &ad4696_chip_info, }, 959 + { .compatible = "adi,ad4697", .data = &ad4697_chip_info, }, 960 + { .compatible = "adi,ad4698", .data = &ad4698_chip_info, }, 961 + { } 962 + }; 963 + MODULE_DEVICE_TABLE(of, ad4695_of_match_table); 964 + 965 + static struct spi_driver ad4695_driver = { 966 + .driver = { 967 + .name = "ad4695", 968 + .of_match_table = ad4695_of_match_table, 969 + }, 970 + .probe = ad4695_probe, 971 + .id_table = ad4695_spi_id_table, 972 + }; 973 + module_spi_driver(ad4695_driver); 974 + 975 + MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>"); 976 + MODULE_AUTHOR("David Lechner <dlechner@baylibre.com>"); 977 + MODULE_DESCRIPTION("Analog Devices AD4695 ADC driver"); 978 + MODULE_LICENSE("GPL");
+1 -1
drivers/iio/adc/ad7091r8.c
··· 159 159 return spi_write(spi, &st->tx_buf, 2); 160 160 } 161 161 162 - static struct regmap_bus ad7091r8_regmap_bus = { 162 + static const struct regmap_bus ad7091r8_regmap_bus = { 163 163 .reg_read = ad7091r_regmap_bus_reg_read, 164 164 .reg_write = ad7091r_regmap_bus_reg_write, 165 165 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
+15 -19
drivers/iio/adc/ad7124.c
··· 378 378 cfg->vref_mv = 2500; 379 379 st->adc_control &= ~AD7124_ADC_CTRL_REF_EN_MSK; 380 380 st->adc_control |= AD7124_ADC_CTRL_REF_EN(1); 381 - return ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 382 - 2, st->adc_control); 381 + return 0; 383 382 default: 384 383 dev_err(&st->sd.spi->dev, "Invalid reference %d\n", refsel); 385 384 return -EINVAL; ··· 396 397 397 398 tmp = (cfg->buf_positive << 1) + cfg->buf_negative; 398 399 val = AD7124_CONFIG_BIPOLAR(cfg->bipolar) | AD7124_CONFIG_REF_SEL(cfg->refsel) | 399 - AD7124_CONFIG_IN_BUFF(tmp); 400 + AD7124_CONFIG_IN_BUFF(tmp) | AD7124_CONFIG_PGA(cfg->pga_bits); 401 + 400 402 ret = ad_sd_write_reg(&st->sd, AD7124_CONFIG(cfg->cfg_slot), 2, val); 401 403 if (ret < 0) 402 404 return ret; 403 405 404 - tmp = AD7124_FILTER_TYPE_SEL(cfg->filter_type); 405 - ret = ad7124_spi_write_mask(st, AD7124_FILTER(cfg->cfg_slot), AD7124_FILTER_TYPE_MSK, 406 - tmp, 3); 407 - if (ret < 0) 408 - return ret; 409 - 410 - ret = ad7124_spi_write_mask(st, AD7124_FILTER(cfg->cfg_slot), AD7124_FILTER_FS_MSK, 411 - AD7124_FILTER_FS(cfg->odr_sel_bits), 3); 412 - if (ret < 0) 413 - return ret; 414 - 415 - return ad7124_spi_write_mask(st, AD7124_CONFIG(cfg->cfg_slot), AD7124_CONFIG_PGA_MSK, 416 - AD7124_CONFIG_PGA(cfg->pga_bits), 2); 406 + tmp = AD7124_FILTER_TYPE_SEL(cfg->filter_type) | 407 + AD7124_FILTER_FS(cfg->odr_sel_bits); 408 + return ad7124_spi_write_mask(st, AD7124_FILTER(cfg->cfg_slot), 409 + AD7124_FILTER_TYPE_MSK | AD7124_FILTER_FS_MSK, 410 + tmp, 3); 417 411 } 418 412 419 413 static struct ad7124_channel_config *ad7124_pop_config(struct ad7124_state *st) ··· 895 903 /* Set the power mode */ 896 904 st->adc_control &= ~AD7124_ADC_CTRL_PWR_MSK; 897 905 st->adc_control |= AD7124_ADC_CTRL_PWR(power_mode); 898 - ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control); 899 - if (ret < 0) 900 - return ret; 906 + 907 + st->adc_control &= ~AD7124_ADC_CTRL_MODE_MSK; 908 + st->adc_control |= AD7124_ADC_CTRL_MODE(AD_SD_MODE_IDLE); 901 909 902 910 mutex_init(&st->cfgs_lock); 903 911 INIT_KFIFO(st->live_cfgs_fifo); ··· 914 922 */ 915 923 ad7124_set_channel_odr(st, i, 10); 916 924 } 925 + 926 + ret = ad_sd_write_reg(&st->sd, AD7124_ADC_CONTROL, 2, st->adc_control); 927 + if (ret < 0) 928 + return ret; 917 929 918 930 return ret; 919 931 }
+155 -28
drivers/iio/adc/ad7192.c
··· 8 8 #include <linux/interrupt.h> 9 9 #include <linux/bitfield.h> 10 10 #include <linux/clk.h> 11 + #include <linux/clk-provider.h> 11 12 #include <linux/device.h> 12 13 #include <linux/kernel.h> 13 14 #include <linux/slab.h> ··· 202 201 struct ad7192_state { 203 202 const struct ad7192_chip_info *chip_info; 204 203 struct clk *mclk; 204 + struct clk_hw int_clk_hw; 205 205 u16 int_vref_mv; 206 206 u32 aincom_mv; 207 207 u32 fclk; ··· 398 396 freq <= AD7192_EXT_FREQ_MHZ_MAX); 399 397 } 400 398 401 - static int ad7192_clock_select(struct ad7192_state *st) 399 + /* 400 + * Position 0 of ad7192_clock_names, xtal, corresponds to clock source 401 + * configuration AD7192_CLK_EXT_MCLK1_2 and position 1, mclk, corresponds to 402 + * AD7192_CLK_EXT_MCLK2 403 + */ 404 + static const char *const ad7192_clock_names[] = { 405 + "xtal", 406 + "mclk" 407 + }; 408 + 409 + static struct ad7192_state *clk_hw_to_ad7192(struct clk_hw *hw) 410 + { 411 + return container_of(hw, struct ad7192_state, int_clk_hw); 412 + } 413 + 414 + static unsigned long ad7192_clk_recalc_rate(struct clk_hw *hw, 415 + unsigned long parent_rate) 416 + { 417 + return AD7192_INT_FREQ_MHZ; 418 + } 419 + 420 + static int ad7192_clk_output_is_enabled(struct clk_hw *hw) 421 + { 422 + struct ad7192_state *st = clk_hw_to_ad7192(hw); 423 + 424 + return st->clock_sel == AD7192_CLK_INT_CO; 425 + } 426 + 427 + static int ad7192_clk_prepare(struct clk_hw *hw) 428 + { 429 + struct ad7192_state *st = clk_hw_to_ad7192(hw); 430 + int ret; 431 + 432 + st->mode &= ~AD7192_MODE_CLKSRC_MASK; 433 + st->mode |= AD7192_CLK_INT_CO; 434 + 435 + ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode); 436 + if (ret) 437 + return ret; 438 + 439 + st->clock_sel = AD7192_CLK_INT_CO; 440 + 441 + return 0; 442 + } 443 + 444 + static void ad7192_clk_unprepare(struct clk_hw *hw) 445 + { 446 + struct ad7192_state *st = clk_hw_to_ad7192(hw); 447 + int ret; 448 + 449 + st->mode &= ~AD7192_MODE_CLKSRC_MASK; 450 + st->mode |= AD7192_CLK_INT; 451 + 452 + ret = ad_sd_write_reg(&st->sd, AD7192_REG_MODE, 3, st->mode); 453 + if (ret) 454 + return; 455 + 456 + st->clock_sel = AD7192_CLK_INT; 457 + } 458 + 459 + static const struct clk_ops ad7192_int_clk_ops = { 460 + .recalc_rate = ad7192_clk_recalc_rate, 461 + .is_enabled = ad7192_clk_output_is_enabled, 462 + .prepare = ad7192_clk_prepare, 463 + .unprepare = ad7192_clk_unprepare, 464 + }; 465 + 466 + static int ad7192_register_clk_provider(struct ad7192_state *st) 402 467 { 403 468 struct device *dev = &st->sd.spi->dev; 404 - unsigned int clock_sel; 469 + struct clk_init_data init = {}; 470 + int ret; 405 471 406 - clock_sel = AD7192_CLK_INT; 472 + if (!IS_ENABLED(CONFIG_COMMON_CLK)) 473 + return 0; 407 474 408 - /* use internal clock */ 409 - if (!st->mclk) { 410 - if (device_property_read_bool(dev, "adi,int-clock-output-enable")) 411 - clock_sel = AD7192_CLK_INT_CO; 412 - } else { 413 - if (device_property_read_bool(dev, "adi,clock-xtal")) 414 - clock_sel = AD7192_CLK_EXT_MCLK1_2; 415 - else 416 - clock_sel = AD7192_CLK_EXT_MCLK2; 475 + if (!device_property_present(dev, "#clock-cells")) 476 + return 0; 477 + 478 + init.name = devm_kasprintf(dev, GFP_KERNEL, "%s-clk", 479 + fwnode_get_name(dev_fwnode(dev))); 480 + if (!init.name) 481 + return -ENOMEM; 482 + 483 + init.ops = &ad7192_int_clk_ops; 484 + 485 + st->int_clk_hw.init = &init; 486 + ret = devm_clk_hw_register(dev, &st->int_clk_hw); 487 + if (ret) 488 + return ret; 489 + 490 + return devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get, 491 + &st->int_clk_hw); 492 + } 493 + 494 + static int ad7192_clock_setup(struct ad7192_state *st) 495 + { 496 + struct device *dev = &st->sd.spi->dev; 497 + int ret; 498 + 499 + /* 500 + * The following two if branches are kept for backward compatibility but 501 + * the use of the two devicetree properties is highly discouraged. Clock 502 + * configuration should be done according to the bindings. 503 + */ 504 + 505 + if (device_property_read_bool(dev, "adi,int-clock-output-enable")) { 506 + st->clock_sel = AD7192_CLK_INT_CO; 507 + st->fclk = AD7192_INT_FREQ_MHZ; 508 + dev_warn(dev, "Property adi,int-clock-output-enable is deprecated! Check bindings!\n"); 509 + return 0; 417 510 } 418 511 419 - return clock_sel; 512 + if (device_property_read_bool(dev, "adi,clock-xtal")) { 513 + st->clock_sel = AD7192_CLK_EXT_MCLK1_2; 514 + st->mclk = devm_clk_get_enabled(dev, "mclk"); 515 + if (IS_ERR(st->mclk)) 516 + return dev_err_probe(dev, PTR_ERR(st->mclk), 517 + "Failed to get mclk\n"); 518 + 519 + st->fclk = clk_get_rate(st->mclk); 520 + if (!ad7192_valid_external_frequency(st->fclk)) 521 + return dev_err_probe(dev, -EINVAL, 522 + "External clock frequency out of bounds\n"); 523 + 524 + dev_warn(dev, "Property adi,clock-xtal is deprecated! Check bindings!\n"); 525 + return 0; 526 + } 527 + 528 + ret = device_property_match_property_string(dev, "clock-names", 529 + ad7192_clock_names, 530 + ARRAY_SIZE(ad7192_clock_names)); 531 + if (ret < 0) { 532 + st->clock_sel = AD7192_CLK_INT; 533 + st->fclk = AD7192_INT_FREQ_MHZ; 534 + 535 + ret = ad7192_register_clk_provider(st); 536 + if (ret) 537 + return dev_err_probe(dev, ret, 538 + "Failed to register clock provider\n"); 539 + return 0; 540 + } 541 + 542 + st->clock_sel = AD7192_CLK_EXT_MCLK1_2 + ret; 543 + 544 + st->mclk = devm_clk_get_enabled(dev, ad7192_clock_names[ret]); 545 + if (IS_ERR(st->mclk)) 546 + return dev_err_probe(dev, PTR_ERR(st->mclk), 547 + "Failed to get clock source\n"); 548 + 549 + st->fclk = clk_get_rate(st->mclk); 550 + if (!ad7192_valid_external_frequency(st->fclk)) 551 + return dev_err_probe(dev, -EINVAL, 552 + "External clock frequency out of bounds\n"); 553 + 554 + return 0; 420 555 } 421 556 422 557 static int ad7192_setup(struct iio_dev *indio_dev, struct device *dev) ··· 1414 1275 if (ret) 1415 1276 return ret; 1416 1277 1417 - st->fclk = AD7192_INT_FREQ_MHZ; 1418 - 1419 - st->mclk = devm_clk_get_optional_enabled(dev, "mclk"); 1420 - if (IS_ERR(st->mclk)) 1421 - return PTR_ERR(st->mclk); 1422 - 1423 - st->clock_sel = ad7192_clock_select(st); 1424 - 1425 - if (st->clock_sel == AD7192_CLK_EXT_MCLK1_2 || 1426 - st->clock_sel == AD7192_CLK_EXT_MCLK2) { 1427 - st->fclk = clk_get_rate(st->mclk); 1428 - if (!ad7192_valid_external_frequency(st->fclk)) 1429 - return dev_err_probe(dev, -EINVAL, 1430 - "External clock frequency out of bounds\n"); 1431 - } 1278 + ret = ad7192_clock_setup(st); 1279 + if (ret) 1280 + return ret; 1432 1281 1433 1282 ret = ad7192_setup(indio_dev, dev); 1434 1283 if (ret)
+2 -1
drivers/iio/adc/ad7266.c
··· 123 123 const unsigned long *scan_mask) 124 124 { 125 125 struct ad7266_state *st = iio_priv(indio_dev); 126 - unsigned int nr = find_first_bit(scan_mask, indio_dev->masklength); 126 + unsigned int nr = find_first_bit(scan_mask, 127 + iio_get_masklength(indio_dev)); 127 128 128 129 ad7266_select_input(st, nr); 129 130
+4 -6
drivers/iio/adc/ad7280a.c
··· 7 7 8 8 #include <linux/bitfield.h> 9 9 #include <linux/bits.h> 10 + #include <linux/cleanup.h> 10 11 #include <linux/crc8.h> 11 12 #include <linux/delay.h> 12 13 #include <linux/device.h> ··· 804 803 { 805 804 struct iio_dev *indio_dev = private; 806 805 struct ad7280_state *st = iio_priv(indio_dev); 807 - unsigned int *channels; 808 806 int i, ret; 809 807 810 - channels = kcalloc(st->scan_cnt, sizeof(*channels), GFP_KERNEL); 808 + unsigned int *channels __free(kfree) = kcalloc(st->scan_cnt, sizeof(*channels), 809 + GFP_KERNEL); 811 810 if (!channels) 812 811 return IRQ_HANDLED; 813 812 814 813 ret = ad7280_read_all_channels(st, st->scan_cnt, channels); 815 814 if (ret < 0) 816 - goto out; 815 + return IRQ_HANDLED; 817 816 818 817 for (i = 0; i < st->scan_cnt; i++) { 819 818 unsigned int val; ··· 852 851 } 853 852 } 854 853 } 855 - 856 - out: 857 - kfree(channels); 858 854 859 855 return IRQ_HANDLED; 860 856 }
+2 -1
drivers/iio/adc/ad7298.c
··· 109 109 int scan_count; 110 110 111 111 /* Now compute overall size */ 112 - scan_count = bitmap_weight(active_scan_mask, indio_dev->masklength); 112 + scan_count = bitmap_weight(active_scan_mask, 113 + iio_get_masklength(indio_dev)); 113 114 114 115 command = AD7298_WRITE | st->ext_ref; 115 116
+442 -81
drivers/iio/adc/ad7380.c
··· 8 8 * Datasheets of supported parts: 9 9 * ad7380/1 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7380-7381.pdf 10 10 * ad7383/4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-7384.pdf 11 + * ad7386/7/8 : https://www.analog.com/media/en/technical-documentation/data-sheets/AD7386-7387-7388.pdf 11 12 * ad7380-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7380-4.pdf 12 13 * ad7381-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7381-4.pdf 13 14 * ad7383/4-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7383-4-ad7384-4.pdf 15 + * ad7386/7/8-4 : https://www.analog.com/media/en/technical-documentation/data-sheets/ad7386-4-7387-4-7388-4.pdf 14 16 */ 15 17 16 18 #include <linux/align.h> ··· 33 31 #include <linux/iio/trigger_consumer.h> 34 32 #include <linux/iio/triggered_buffer.h> 35 33 36 - #define MAX_NUM_CHANNELS 4 34 + #define MAX_NUM_CHANNELS 8 37 35 /* 2.5V internal reference voltage */ 38 36 #define AD7380_INTERNAL_REF_MV 2500 39 37 ··· 51 49 #define AD7380_REG_ADDR_ALERT_LOW_TH 0x4 52 50 #define AD7380_REG_ADDR_ALERT_HIGH_TH 0x5 53 51 52 + #define AD7380_CONFIG1_CH BIT(11) 53 + #define AD7380_CONFIG1_SEQ BIT(10) 54 54 #define AD7380_CONFIG1_OS_MODE BIT(9) 55 55 #define AD7380_CONFIG1_OSR GENMASK(8, 6) 56 56 #define AD7380_CONFIG1_CRC_W BIT(5) ··· 84 80 const char *name; 85 81 const struct iio_chan_spec *channels; 86 82 unsigned int num_channels; 83 + unsigned int num_simult_channels; 84 + bool has_mux; 87 85 const char * const *vcm_supplies; 88 86 unsigned int num_vcm_supplies; 89 87 const unsigned long *available_scan_masks; ··· 97 91 AD7380_SCAN_TYPE_RESOLUTION_BOOST, 98 92 }; 99 93 100 - /* Extended scan types for 14-bit chips. */ 101 - static const struct iio_scan_type ad7380_scan_type_14[] = { 94 + /* Extended scan types for 12-bit unsigned chips. */ 95 + static const struct iio_scan_type ad7380_scan_type_12_u[] = { 96 + [AD7380_SCAN_TYPE_NORMAL] = { 97 + .sign = 'u', 98 + .realbits = 12, 99 + .storagebits = 16, 100 + .endianness = IIO_CPU, 101 + }, 102 + [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = { 103 + .sign = 'u', 104 + .realbits = 14, 105 + .storagebits = 16, 106 + .endianness = IIO_CPU, 107 + }, 108 + }; 109 + 110 + /* Extended scan types for 14-bit signed chips. */ 111 + static const struct iio_scan_type ad7380_scan_type_14_s[] = { 102 112 [AD7380_SCAN_TYPE_NORMAL] = { 103 113 .sign = 's', 104 114 .realbits = 14, 105 115 .storagebits = 16, 106 - .endianness = IIO_CPU 116 + .endianness = IIO_CPU, 107 117 }, 108 118 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = { 109 119 .sign = 's', 110 120 .realbits = 16, 111 121 .storagebits = 16, 112 - .endianness = IIO_CPU 122 + .endianness = IIO_CPU, 113 123 }, 114 124 }; 115 125 116 - /* Extended scan types for 16-bit chips. */ 117 - static const struct iio_scan_type ad7380_scan_type_16[] = { 126 + /* Extended scan types for 14-bit unsigned chips. */ 127 + static const struct iio_scan_type ad7380_scan_type_14_u[] = { 128 + [AD7380_SCAN_TYPE_NORMAL] = { 129 + .sign = 'u', 130 + .realbits = 14, 131 + .storagebits = 16, 132 + .endianness = IIO_CPU, 133 + }, 134 + [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = { 135 + .sign = 'u', 136 + .realbits = 16, 137 + .storagebits = 16, 138 + .endianness = IIO_CPU, 139 + }, 140 + }; 141 + 142 + /* Extended scan types for 16-bit signed_chips. */ 143 + static const struct iio_scan_type ad7380_scan_type_16_s[] = { 118 144 [AD7380_SCAN_TYPE_NORMAL] = { 119 145 .sign = 's', 120 146 .realbits = 16, 121 147 .storagebits = 16, 122 - .endianness = IIO_CPU 148 + .endianness = IIO_CPU, 123 149 }, 124 150 [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = { 125 151 .sign = 's', 126 152 .realbits = 18, 127 153 .storagebits = 32, 128 - .endianness = IIO_CPU 154 + .endianness = IIO_CPU, 129 155 }, 130 156 }; 131 157 132 - #define AD7380_CHANNEL(index, bits, diff) { \ 133 - .type = IIO_VOLTAGE, \ 134 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 135 - ((diff) ? 0 : BIT(IIO_CHAN_INFO_OFFSET)), \ 136 - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 137 - BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 138 - .info_mask_shared_by_type_available = \ 139 - BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 140 - .indexed = 1, \ 141 - .differential = (diff), \ 142 - .channel = (diff) ? (2 * (index)) : (index), \ 143 - .channel2 = (diff) ? (2 * (index) + 1) : 0, \ 144 - .scan_index = (index), \ 145 - .has_ext_scan_type = 1, \ 146 - .ext_scan_type = ad7380_scan_type_##bits, \ 147 - .num_ext_scan_type = ARRAY_SIZE(ad7380_scan_type_##bits),\ 158 + /* Extended scan types for 16-bit unsigned chips. */ 159 + static const struct iio_scan_type ad7380_scan_type_16_u[] = { 160 + [AD7380_SCAN_TYPE_NORMAL] = { 161 + .sign = 'u', 162 + .realbits = 16, 163 + .storagebits = 16, 164 + .endianness = IIO_CPU, 165 + }, 166 + [AD7380_SCAN_TYPE_RESOLUTION_BOOST] = { 167 + .sign = 'u', 168 + .realbits = 18, 169 + .storagebits = 32, 170 + .endianness = IIO_CPU, 171 + }, 172 + }; 173 + 174 + #define AD7380_CHANNEL(index, bits, diff, sign) { \ 175 + .type = IIO_VOLTAGE, \ 176 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 177 + ((diff) ? 0 : BIT(IIO_CHAN_INFO_OFFSET)), \ 178 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 179 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 180 + .info_mask_shared_by_type_available = \ 181 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 182 + .indexed = 1, \ 183 + .differential = (diff), \ 184 + .channel = (diff) ? (2 * (index)) : (index), \ 185 + .channel2 = (diff) ? (2 * (index) + 1) : 0, \ 186 + .scan_index = (index), \ 187 + .has_ext_scan_type = 1, \ 188 + .ext_scan_type = ad7380_scan_type_##bits##_##sign, \ 189 + .num_ext_scan_type = ARRAY_SIZE(ad7380_scan_type_##bits##_##sign), \ 148 190 } 149 191 150 - #define DEFINE_AD7380_2_CHANNEL(name, bits, diff) \ 192 + #define DEFINE_AD7380_2_CHANNEL(name, bits, diff, sign) \ 151 193 static const struct iio_chan_spec name[] = { \ 152 - AD7380_CHANNEL(0, bits, diff), \ 153 - AD7380_CHANNEL(1, bits, diff), \ 194 + AD7380_CHANNEL(0, bits, diff, sign), \ 195 + AD7380_CHANNEL(1, bits, diff, sign), \ 154 196 IIO_CHAN_SOFT_TIMESTAMP(2), \ 155 197 } 156 198 157 - #define DEFINE_AD7380_4_CHANNEL(name, bits, diff) \ 199 + #define DEFINE_AD7380_4_CHANNEL(name, bits, diff, sign) \ 158 200 static const struct iio_chan_spec name[] = { \ 159 - AD7380_CHANNEL(0, bits, diff), \ 160 - AD7380_CHANNEL(1, bits, diff), \ 161 - AD7380_CHANNEL(2, bits, diff), \ 162 - AD7380_CHANNEL(3, bits, diff), \ 201 + AD7380_CHANNEL(0, bits, diff, sign), \ 202 + AD7380_CHANNEL(1, bits, diff, sign), \ 203 + AD7380_CHANNEL(2, bits, diff, sign), \ 204 + AD7380_CHANNEL(3, bits, diff, sign), \ 163 205 IIO_CHAN_SOFT_TIMESTAMP(4), \ 164 206 } 165 207 208 + #define DEFINE_AD7380_8_CHANNEL(name, bits, diff, sign) \ 209 + static const struct iio_chan_spec name[] = { \ 210 + AD7380_CHANNEL(0, bits, diff, sign), \ 211 + AD7380_CHANNEL(1, bits, diff, sign), \ 212 + AD7380_CHANNEL(2, bits, diff, sign), \ 213 + AD7380_CHANNEL(3, bits, diff, sign), \ 214 + AD7380_CHANNEL(4, bits, diff, sign), \ 215 + AD7380_CHANNEL(5, bits, diff, sign), \ 216 + AD7380_CHANNEL(6, bits, diff, sign), \ 217 + AD7380_CHANNEL(7, bits, diff, sign), \ 218 + IIO_CHAN_SOFT_TIMESTAMP(8), \ 219 + } 220 + 166 221 /* fully differential */ 167 - DEFINE_AD7380_2_CHANNEL(ad7380_channels, 16, 1); 168 - DEFINE_AD7380_2_CHANNEL(ad7381_channels, 14, 1); 169 - DEFINE_AD7380_4_CHANNEL(ad7380_4_channels, 16, 1); 170 - DEFINE_AD7380_4_CHANNEL(ad7381_4_channels, 14, 1); 222 + DEFINE_AD7380_2_CHANNEL(ad7380_channels, 16, 1, s); 223 + DEFINE_AD7380_2_CHANNEL(ad7381_channels, 14, 1, s); 224 + DEFINE_AD7380_4_CHANNEL(ad7380_4_channels, 16, 1, s); 225 + DEFINE_AD7380_4_CHANNEL(ad7381_4_channels, 14, 1, s); 171 226 /* pseudo differential */ 172 - DEFINE_AD7380_2_CHANNEL(ad7383_channels, 16, 0); 173 - DEFINE_AD7380_2_CHANNEL(ad7384_channels, 14, 0); 174 - DEFINE_AD7380_4_CHANNEL(ad7383_4_channels, 16, 0); 175 - DEFINE_AD7380_4_CHANNEL(ad7384_4_channels, 14, 0); 227 + DEFINE_AD7380_2_CHANNEL(ad7383_channels, 16, 0, s); 228 + DEFINE_AD7380_2_CHANNEL(ad7384_channels, 14, 0, s); 229 + DEFINE_AD7380_4_CHANNEL(ad7383_4_channels, 16, 0, s); 230 + DEFINE_AD7380_4_CHANNEL(ad7384_4_channels, 14, 0, s); 231 + 232 + /* Single ended */ 233 + DEFINE_AD7380_4_CHANNEL(ad7386_channels, 16, 0, u); 234 + DEFINE_AD7380_4_CHANNEL(ad7387_channels, 14, 0, u); 235 + DEFINE_AD7380_4_CHANNEL(ad7388_channels, 12, 0, u); 236 + DEFINE_AD7380_8_CHANNEL(ad7386_4_channels, 16, 0, u); 237 + DEFINE_AD7380_8_CHANNEL(ad7387_4_channels, 14, 0, u); 238 + DEFINE_AD7380_8_CHANNEL(ad7388_4_channels, 12, 0, u); 176 239 177 240 static const char * const ad7380_2_channel_vcm_supplies[] = { 178 241 "aina", "ainb", ··· 259 184 260 185 static const unsigned long ad7380_4_channel_scan_masks[] = { 261 186 GENMASK(3, 0), 187 + 0 188 + }; 189 + 190 + /* 191 + * Single ended parts have a 2:1 multiplexer in front of each ADC. 192 + * 193 + * From an IIO point of view, all inputs are exported, i.e ad7386/7/8 194 + * export 4 channels and ad7386-4/7-4/8-4 export 8 channels. 195 + * 196 + * Inputs AinX0 of multiplexers correspond to the first half of IIO channels 197 + * (i.e 0-1 or 0-3) and inputs AinX1 correspond to second half (i.e 2-3 or 198 + * 4-7). Example for AD7386/7/8 (2 channels parts): 199 + * 200 + * IIO | AD7386/7/8 201 + * | +---------------------------- 202 + * | | _____ ______ 203 + * | | | | | | 204 + * voltage0 | AinA0 --|--->| | | | 205 + * | | | mux |----->| ADCA |--- 206 + * voltage2 | AinA1 --|--->| | | | 207 + * | | |_____| |_____ | 208 + * | | _____ ______ 209 + * | | | | | | 210 + * voltage1 | AinB0 --|--->| | | | 211 + * | | | mux |----->| ADCB |--- 212 + * voltage3 | AinB1 --|--->| | | | 213 + * | | |_____| |______| 214 + * | | 215 + * | +---------------------------- 216 + * 217 + * Since this is simultaneous sampling for AinX0 OR AinX1 we have two separate 218 + * scan masks. 219 + * When sequencer mode is enabled, chip automatically cycles through 220 + * AinX0 and AinX1 channels. From an IIO point of view, we ca enable all 221 + * channels, at the cost of an extra read, thus dividing the maximum rate by 222 + * two. 223 + */ 224 + enum { 225 + AD7380_SCAN_MASK_CH_0, 226 + AD7380_SCAN_MASK_CH_1, 227 + AD7380_SCAN_MASK_SEQ, 228 + }; 229 + 230 + static const unsigned long ad7380_2x2_channel_scan_masks[] = { 231 + [AD7380_SCAN_MASK_CH_0] = GENMASK(1, 0), 232 + [AD7380_SCAN_MASK_CH_1] = GENMASK(3, 2), 233 + [AD7380_SCAN_MASK_SEQ] = GENMASK(3, 0), 234 + 0 235 + }; 236 + 237 + static const unsigned long ad7380_2x4_channel_scan_masks[] = { 238 + [AD7380_SCAN_MASK_CH_0] = GENMASK(3, 0), 239 + [AD7380_SCAN_MASK_CH_1] = GENMASK(7, 4), 240 + [AD7380_SCAN_MASK_SEQ] = GENMASK(7, 0), 262 241 0 263 242 }; 264 243 ··· 337 208 .name = "ad7380", 338 209 .channels = ad7380_channels, 339 210 .num_channels = ARRAY_SIZE(ad7380_channels), 211 + .num_simult_channels = 2, 340 212 .available_scan_masks = ad7380_2_channel_scan_masks, 341 213 .timing_specs = &ad7380_timing, 342 214 }; ··· 346 216 .name = "ad7381", 347 217 .channels = ad7381_channels, 348 218 .num_channels = ARRAY_SIZE(ad7381_channels), 219 + .num_simult_channels = 2, 349 220 .available_scan_masks = ad7380_2_channel_scan_masks, 350 221 .timing_specs = &ad7380_timing, 351 222 }; ··· 355 224 .name = "ad7383", 356 225 .channels = ad7383_channels, 357 226 .num_channels = ARRAY_SIZE(ad7383_channels), 227 + .num_simult_channels = 2, 358 228 .vcm_supplies = ad7380_2_channel_vcm_supplies, 359 229 .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies), 360 230 .available_scan_masks = ad7380_2_channel_scan_masks, ··· 366 234 .name = "ad7384", 367 235 .channels = ad7384_channels, 368 236 .num_channels = ARRAY_SIZE(ad7384_channels), 237 + .num_simult_channels = 2, 369 238 .vcm_supplies = ad7380_2_channel_vcm_supplies, 370 239 .num_vcm_supplies = ARRAY_SIZE(ad7380_2_channel_vcm_supplies), 371 240 .available_scan_masks = ad7380_2_channel_scan_masks, 241 + .timing_specs = &ad7380_timing, 242 + }; 243 + 244 + static const struct ad7380_chip_info ad7386_chip_info = { 245 + .name = "ad7386", 246 + .channels = ad7386_channels, 247 + .num_channels = ARRAY_SIZE(ad7386_channels), 248 + .num_simult_channels = 2, 249 + .has_mux = true, 250 + .available_scan_masks = ad7380_2x2_channel_scan_masks, 251 + .timing_specs = &ad7380_timing, 252 + }; 253 + 254 + static const struct ad7380_chip_info ad7387_chip_info = { 255 + .name = "ad7387", 256 + .channels = ad7387_channels, 257 + .num_channels = ARRAY_SIZE(ad7387_channels), 258 + .num_simult_channels = 2, 259 + .has_mux = true, 260 + .available_scan_masks = ad7380_2x2_channel_scan_masks, 261 + .timing_specs = &ad7380_timing, 262 + }; 263 + 264 + static const struct ad7380_chip_info ad7388_chip_info = { 265 + .name = "ad7388", 266 + .channels = ad7388_channels, 267 + .num_channels = ARRAY_SIZE(ad7388_channels), 268 + .num_simult_channels = 2, 269 + .has_mux = true, 270 + .available_scan_masks = ad7380_2x2_channel_scan_masks, 372 271 .timing_specs = &ad7380_timing, 373 272 }; 374 273 ··· 407 244 .name = "ad7380-4", 408 245 .channels = ad7380_4_channels, 409 246 .num_channels = ARRAY_SIZE(ad7380_4_channels), 247 + .num_simult_channels = 4, 410 248 .available_scan_masks = ad7380_4_channel_scan_masks, 411 249 .timing_specs = &ad7380_4_timing, 412 250 }; ··· 416 252 .name = "ad7381-4", 417 253 .channels = ad7381_4_channels, 418 254 .num_channels = ARRAY_SIZE(ad7381_4_channels), 255 + .num_simult_channels = 4, 419 256 .available_scan_masks = ad7380_4_channel_scan_masks, 420 257 .timing_specs = &ad7380_4_timing, 421 258 }; ··· 425 260 .name = "ad7383-4", 426 261 .channels = ad7383_4_channels, 427 262 .num_channels = ARRAY_SIZE(ad7383_4_channels), 263 + .num_simult_channels = 4, 428 264 .vcm_supplies = ad7380_4_channel_vcm_supplies, 429 265 .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies), 430 266 .available_scan_masks = ad7380_4_channel_scan_masks, ··· 436 270 .name = "ad7384-4", 437 271 .channels = ad7384_4_channels, 438 272 .num_channels = ARRAY_SIZE(ad7384_4_channels), 273 + .num_simult_channels = 4, 439 274 .vcm_supplies = ad7380_4_channel_vcm_supplies, 440 275 .num_vcm_supplies = ARRAY_SIZE(ad7380_4_channel_vcm_supplies), 441 276 .available_scan_masks = ad7380_4_channel_scan_masks, 277 + .timing_specs = &ad7380_4_timing, 278 + }; 279 + 280 + static const struct ad7380_chip_info ad7386_4_chip_info = { 281 + .name = "ad7386-4", 282 + .channels = ad7386_4_channels, 283 + .num_channels = ARRAY_SIZE(ad7386_4_channels), 284 + .num_simult_channels = 4, 285 + .has_mux = true, 286 + .available_scan_masks = ad7380_2x4_channel_scan_masks, 287 + .timing_specs = &ad7380_4_timing, 288 + }; 289 + 290 + static const struct ad7380_chip_info ad7387_4_chip_info = { 291 + .name = "ad7387-4", 292 + .channels = ad7387_4_channels, 293 + .num_channels = ARRAY_SIZE(ad7387_4_channels), 294 + .num_simult_channels = 4, 295 + .has_mux = true, 296 + .available_scan_masks = ad7380_2x4_channel_scan_masks, 297 + .timing_specs = &ad7380_4_timing, 298 + }; 299 + 300 + static const struct ad7380_chip_info ad7388_4_chip_info = { 301 + .name = "ad7388-4", 302 + .channels = ad7388_4_channels, 303 + .num_channels = ARRAY_SIZE(ad7388_4_channels), 304 + .num_simult_channels = 4, 305 + .has_mux = true, 306 + .available_scan_masks = ad7380_2x4_channel_scan_masks, 442 307 .timing_specs = &ad7380_4_timing, 443 308 }; 444 309 ··· 479 282 struct regmap *regmap; 480 283 unsigned int oversampling_ratio; 481 284 bool resolution_boost_enabled; 285 + unsigned int ch; 286 + bool seq; 482 287 unsigned int vref_mv; 483 288 unsigned int vcm_mv[MAX_NUM_CHANNELS]; 484 289 /* xfers, message an buffer for reading sample data */ 485 - struct spi_transfer xfer[2]; 486 - struct spi_message msg; 290 + struct spi_transfer normal_xfer[2]; 291 + struct spi_message normal_msg; 292 + struct spi_transfer seq_xfer[4]; 293 + struct spi_message seq_msg; 487 294 /* 488 295 * DMA (thus cache coherency maintenance) requires the transfer buffers 489 296 * to live in their own cache lines. ··· 580 379 unreachable(); 581 380 } 582 381 382 + /* 383 + * When switching channel, the ADC require an additional settling time. 384 + * According to the datasheet, data is value on the third CS low. We already 385 + * have an extra toggle before each read (either direct reads or buffered reads) 386 + * to sample correct data, so we just add a single CS toggle at the end of the 387 + * register write. 388 + */ 389 + static int ad7380_set_ch(struct ad7380_state *st, unsigned int ch) 390 + { 391 + struct spi_transfer xfer = { 392 + .delay = { 393 + .value = T_CONVERT_NS, 394 + .unit = SPI_DELAY_UNIT_NSECS, 395 + } 396 + }; 397 + int ret; 398 + 399 + if (st->ch == ch) 400 + return 0; 401 + 402 + ret = regmap_update_bits(st->regmap, 403 + AD7380_REG_ADDR_CONFIG1, 404 + AD7380_CONFIG1_CH, 405 + FIELD_PREP(AD7380_CONFIG1_CH, ch)); 406 + 407 + if (ret) 408 + return ret; 409 + 410 + st->ch = ch; 411 + 412 + if (st->oversampling_ratio > 1) 413 + xfer.delay.value = T_CONVERT_0_NS + 414 + T_CONVERT_X_NS * (st->oversampling_ratio - 1); 415 + 416 + return spi_sync_transfer(st->spi, &xfer, 1); 417 + } 418 + 583 419 /** 584 420 * ad7380_update_xfers - update the SPI transfers base on the current scan type 585 421 * @st: device instance specific state ··· 625 387 static void ad7380_update_xfers(struct ad7380_state *st, 626 388 const struct iio_scan_type *scan_type) 627 389 { 390 + struct spi_transfer *xfer = st->seq ? st->seq_xfer : st->normal_xfer; 391 + unsigned int t_convert = T_CONVERT_NS; 392 + 628 393 /* 629 - * First xfer only triggers conversion and has to be long enough for 630 - * all conversions to complete, which can be multiple conversion in the 631 - * case of oversampling. Technically T_CONVERT_X_NS is lower for some 632 - * chips, but we use the maximum value for simplicity for now. 394 + * In the case of oversampling, conversion time is higher than in normal 395 + * mode. Technically T_CONVERT_X_NS is lower for some chips, but we use 396 + * the maximum value for simplicity for now. 633 397 */ 634 398 if (st->oversampling_ratio > 1) 635 - st->xfer[0].delay.value = T_CONVERT_0_NS + T_CONVERT_X_NS * 636 - (st->oversampling_ratio - 1); 637 - else 638 - st->xfer[0].delay.value = T_CONVERT_NS; 399 + t_convert = T_CONVERT_0_NS + T_CONVERT_X_NS * 400 + (st->oversampling_ratio - 1); 639 401 640 - st->xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS; 641 - 642 - /* 643 - * Second xfer reads all channels. Data size depends on if resolution 644 - * boost is enabled or not. 645 - */ 646 - st->xfer[1].bits_per_word = scan_type->realbits; 647 - st->xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) * 648 - (st->chip_info->num_channels - 1); 402 + if (st->seq) { 403 + xfer[0].delay.value = xfer[1].delay.value = t_convert; 404 + xfer[0].delay.unit = xfer[1].delay.unit = SPI_DELAY_UNIT_NSECS; 405 + xfer[2].bits_per_word = xfer[3].bits_per_word = 406 + scan_type->realbits; 407 + xfer[2].len = xfer[3].len = 408 + BITS_TO_BYTES(scan_type->storagebits) * 409 + st->chip_info->num_simult_channels; 410 + xfer[3].rx_buf = xfer[2].rx_buf + xfer[2].len; 411 + /* Additional delay required here when oversampling is enabled */ 412 + if (st->oversampling_ratio > 1) 413 + xfer[2].delay.value = t_convert; 414 + else 415 + xfer[2].delay.value = 0; 416 + xfer[2].delay.unit = SPI_DELAY_UNIT_NSECS; 417 + } else { 418 + xfer[0].delay.value = t_convert; 419 + xfer[0].delay.unit = SPI_DELAY_UNIT_NSECS; 420 + xfer[1].bits_per_word = scan_type->realbits; 421 + xfer[1].len = BITS_TO_BYTES(scan_type->storagebits) * 422 + st->chip_info->num_simult_channels; 423 + } 649 424 } 650 425 651 426 static int ad7380_triggered_buffer_preenable(struct iio_dev *indio_dev) 652 427 { 653 428 struct ad7380_state *st = iio_priv(indio_dev); 654 429 const struct iio_scan_type *scan_type; 430 + struct spi_message *msg = &st->normal_msg; 655 431 656 432 /* 657 433 * Currently, we always read all channels at the same time. The scan_type ··· 675 423 if (IS_ERR(scan_type)) 676 424 return PTR_ERR(scan_type); 677 425 426 + if (st->chip_info->has_mux) { 427 + unsigned int index; 428 + int ret; 429 + 430 + /* 431 + * Depending on the requested scan_mask and current state, 432 + * we need to either change CH bit, or enable sequencer mode 433 + * to sample correct data. 434 + * Sequencer mode is enabled if active mask corresponds to all 435 + * IIO channels enabled. Otherwise, CH bit is set. 436 + */ 437 + ret = iio_active_scan_mask_index(indio_dev); 438 + if (ret < 0) 439 + return ret; 440 + 441 + index = ret; 442 + if (index == AD7380_SCAN_MASK_SEQ) { 443 + ret = regmap_update_bits(st->regmap, 444 + AD7380_REG_ADDR_CONFIG1, 445 + AD7380_CONFIG1_SEQ, 446 + FIELD_PREP(AD7380_CONFIG1_SEQ, 1)); 447 + if (ret) 448 + return ret; 449 + msg = &st->seq_msg; 450 + st->seq = true; 451 + } else { 452 + ret = ad7380_set_ch(st, index); 453 + if (ret) 454 + return ret; 455 + } 456 + 457 + } 458 + 678 459 ad7380_update_xfers(st, scan_type); 679 460 680 - return spi_optimize_message(st->spi, &st->msg); 461 + return spi_optimize_message(st->spi, msg); 681 462 } 682 463 683 464 static int ad7380_triggered_buffer_postdisable(struct iio_dev *indio_dev) 684 465 { 685 466 struct ad7380_state *st = iio_priv(indio_dev); 467 + struct spi_message *msg = &st->normal_msg; 468 + int ret; 686 469 687 - spi_unoptimize_message(&st->msg); 470 + if (st->seq) { 471 + ret = regmap_update_bits(st->regmap, 472 + AD7380_REG_ADDR_CONFIG1, 473 + AD7380_CONFIG1_SEQ, 474 + FIELD_PREP(AD7380_CONFIG1_SEQ, 0)); 475 + if (ret) 476 + return ret; 477 + 478 + msg = &st->seq_msg; 479 + st->seq = false; 480 + } 481 + 482 + spi_unoptimize_message(msg); 688 483 689 484 return 0; 690 485 } ··· 746 447 struct iio_poll_func *pf = p; 747 448 struct iio_dev *indio_dev = pf->indio_dev; 748 449 struct ad7380_state *st = iio_priv(indio_dev); 450 + struct spi_message *msg = st->seq ? &st->seq_msg : &st->normal_msg; 749 451 int ret; 750 452 751 - ret = spi_sync(st->spi, &st->msg); 453 + ret = spi_sync(st->spi, msg); 752 454 if (ret) 753 455 goto out; 754 456 ··· 765 465 static int ad7380_read_direct(struct ad7380_state *st, unsigned int scan_index, 766 466 const struct iio_scan_type *scan_type, int *val) 767 467 { 468 + unsigned int index = scan_index; 768 469 int ret; 470 + 471 + if (st->chip_info->has_mux) { 472 + unsigned int ch = 0; 473 + 474 + if (index >= st->chip_info->num_simult_channels) { 475 + index -= st->chip_info->num_simult_channels; 476 + ch = 1; 477 + } 478 + 479 + ret = ad7380_set_ch(st, ch); 480 + if (ret) 481 + return ret; 482 + } 769 483 770 484 ad7380_update_xfers(st, scan_type); 771 485 772 - ret = spi_sync(st->spi, &st->msg); 486 + ret = spi_sync(st->spi, &st->normal_msg); 773 487 if (ret < 0) 774 488 return ret; 775 489 776 - if (scan_type->storagebits > 16) 777 - *val = sign_extend32(*(u32 *)(st->scan_data + 4 * scan_index), 778 - scan_type->realbits - 1); 779 - else 780 - *val = sign_extend32(*(u16 *)(st->scan_data + 2 * scan_index), 781 - scan_type->realbits - 1); 490 + if (scan_type->storagebits > 16) { 491 + if (scan_type->sign == 's') 492 + *val = sign_extend32(*(u32 *)(st->scan_data + 4 * index), 493 + scan_type->realbits - 1); 494 + else 495 + *val = *(u32 *)(st->scan_data + 4 * index) & 496 + GENMASK(scan_type->realbits - 1, 0); 497 + } else { 498 + if (scan_type->sign == 's') 499 + *val = sign_extend32(*(u16 *)(st->scan_data + 2 * index), 500 + scan_type->realbits - 1); 501 + else 502 + *val = *(u16 *)(st->scan_data + 2 * index) & 503 + GENMASK(scan_type->realbits - 1, 0); 504 + } 782 505 783 506 return IIO_VAL_INT; 784 507 } ··· 978 655 979 656 /* This is the default value after reset. */ 980 657 st->oversampling_ratio = 1; 658 + st->ch = 0; 659 + st->seq = false; 981 660 982 661 /* SPI 1-wire mode */ 983 662 return regmap_update_bits(st->regmap, AD7380_REG_ADDR_CONFIG2, ··· 1081 756 "failed to allocate register map\n"); 1082 757 1083 758 /* 1084 - * Setting up a low latency read for getting sample data. Used for both 1085 - * direct read an triggered buffer. Additional fields will be set up in 1086 - * ad7380_update_xfers() based on the current state of the driver at the 1087 - * time of the read. 759 + * Setting up xfer structures for both normal and sequence mode. These 760 + * struct are used for both direct read and triggered buffer. Additional 761 + * fields will be set up in ad7380_update_xfers() based on the current 762 + * state of the driver at the time of the read. 1088 763 */ 1089 764 1090 - /* toggle CS (no data xfer) to trigger a conversion */ 1091 - st->xfer[0].cs_change = 1; 1092 - st->xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns; 1093 - st->xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 765 + /* 766 + * In normal mode a read is composed of two steps: 767 + * - first, toggle CS (no data xfer) to trigger a conversion 768 + * - then, read data 769 + */ 770 + st->normal_xfer[0].cs_change = 1; 771 + st->normal_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns; 772 + st->normal_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 773 + st->normal_xfer[1].rx_buf = st->scan_data; 1094 774 1095 - /* then do a second xfer to read the data */ 1096 - st->xfer[1].rx_buf = st->scan_data; 775 + spi_message_init_with_transfers(&st->normal_msg, st->normal_xfer, 776 + ARRAY_SIZE(st->normal_xfer)); 777 + /* 778 + * In sequencer mode a read is composed of four steps: 779 + * - CS toggle (no data xfer) to get the right point in the sequence 780 + * - CS toggle (no data xfer) to trigger a conversion of AinX0 and 781 + * acquisition of AinX1 782 + * - 2 data reads, to read AinX0 and AinX1 783 + */ 784 + st->seq_xfer[0].cs_change = 1; 785 + st->seq_xfer[0].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns; 786 + st->seq_xfer[0].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 787 + st->seq_xfer[1].cs_change = 1; 788 + st->seq_xfer[1].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns; 789 + st->seq_xfer[1].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 1097 790 1098 - spi_message_init_with_transfers(&st->msg, st->xfer, ARRAY_SIZE(st->xfer)); 791 + st->seq_xfer[2].rx_buf = st->scan_data; 792 + st->seq_xfer[2].cs_change = 1; 793 + st->seq_xfer[2].cs_change_delay.value = st->chip_info->timing_specs->t_csh_ns; 794 + st->seq_xfer[2].cs_change_delay.unit = SPI_DELAY_UNIT_NSECS; 795 + 796 + spi_message_init_with_transfers(&st->seq_msg, st->seq_xfer, 797 + ARRAY_SIZE(st->seq_xfer)); 1099 798 1100 799 indio_dev->channels = st->chip_info->channels; 1101 800 indio_dev->num_channels = st->chip_info->num_channels; ··· 1147 798 { .compatible = "adi,ad7381", .data = &ad7381_chip_info }, 1148 799 { .compatible = "adi,ad7383", .data = &ad7383_chip_info }, 1149 800 { .compatible = "adi,ad7384", .data = &ad7384_chip_info }, 801 + { .compatible = "adi,ad7386", .data = &ad7386_chip_info }, 802 + { .compatible = "adi,ad7387", .data = &ad7387_chip_info }, 803 + { .compatible = "adi,ad7388", .data = &ad7388_chip_info }, 1150 804 { .compatible = "adi,ad7380-4", .data = &ad7380_4_chip_info }, 1151 805 { .compatible = "adi,ad7381-4", .data = &ad7381_4_chip_info }, 1152 806 { .compatible = "adi,ad7383-4", .data = &ad7383_4_chip_info }, 1153 807 { .compatible = "adi,ad7384-4", .data = &ad7384_4_chip_info }, 808 + { .compatible = "adi,ad7386-4", .data = &ad7386_4_chip_info }, 809 + { .compatible = "adi,ad7387-4", .data = &ad7387_4_chip_info }, 810 + { .compatible = "adi,ad7388-4", .data = &ad7388_4_chip_info }, 1154 811 { } 1155 812 }; 1156 813 ··· 1165 810 { "ad7381", (kernel_ulong_t)&ad7381_chip_info }, 1166 811 { "ad7383", (kernel_ulong_t)&ad7383_chip_info }, 1167 812 { "ad7384", (kernel_ulong_t)&ad7384_chip_info }, 813 + { "ad7386", (kernel_ulong_t)&ad7386_chip_info }, 814 + { "ad7387", (kernel_ulong_t)&ad7387_chip_info }, 815 + { "ad7388", (kernel_ulong_t)&ad7388_chip_info }, 1168 816 { "ad7380-4", (kernel_ulong_t)&ad7380_4_chip_info }, 1169 817 { "ad7381-4", (kernel_ulong_t)&ad7381_4_chip_info }, 1170 818 { "ad7383-4", (kernel_ulong_t)&ad7383_4_chip_info }, 1171 819 { "ad7384-4", (kernel_ulong_t)&ad7384_4_chip_info }, 820 + { "ad7386-4", (kernel_ulong_t)&ad7386_4_chip_info }, 821 + { "ad7387-4", (kernel_ulong_t)&ad7387_4_chip_info }, 822 + { "ad7388-4", (kernel_ulong_t)&ad7388_4_chip_info }, 1172 823 { } 1173 824 }; 1174 825 MODULE_DEVICE_TABLE(spi, ad7380_id_table);
+19 -28
drivers/iio/adc/ad7606.c
··· 69 69 struct ad7606_state *st = iio_priv(indio_dev); 70 70 int ret; 71 71 72 - mutex_lock(&st->lock); 72 + guard(mutex)(&st->lock); 73 + 73 74 if (readval) { 74 75 ret = st->bops->reg_read(st, reg); 75 76 if (ret < 0) 76 - goto err_unlock; 77 + return ret; 77 78 *readval = ret; 78 - ret = 0; 79 + return 0; 79 80 } else { 80 - ret = st->bops->reg_write(st, reg, writeval); 81 + return st->bops->reg_write(st, reg, writeval); 81 82 } 82 - err_unlock: 83 - mutex_unlock(&st->lock); 84 - return ret; 85 83 } 86 84 87 85 static int ad7606_read_samples(struct ad7606_state *st) ··· 122 124 struct ad7606_state *st = iio_priv(indio_dev); 123 125 int ret; 124 126 125 - mutex_lock(&st->lock); 127 + guard(mutex)(&st->lock); 126 128 127 129 ret = ad7606_read_samples(st); 128 - if (ret == 0) 129 - iio_push_to_buffers_with_timestamp(indio_dev, st->data, 130 - iio_get_time_ns(indio_dev)); 130 + if (ret) 131 + goto error_ret; 131 132 133 + iio_push_to_buffers_with_timestamp(indio_dev, st->data, 134 + iio_get_time_ns(indio_dev)); 135 + error_ret: 132 136 iio_trigger_notify_done(indio_dev->trig); 133 137 /* The rising edge of the CONVST signal starts a new conversion. */ 134 138 gpiod_set_value(st->gpio_convst, 1); 135 - 136 - mutex_unlock(&st->lock); 137 139 138 140 return IRQ_HANDLED; 139 141 } ··· 234 236 struct ad7606_state *st = iio_priv(indio_dev); 235 237 DECLARE_BITMAP(values, 3); 236 238 237 - values[0] = val; 239 + values[0] = val & GENMASK(2, 0); 238 240 239 - gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc, 241 + gpiod_set_array_value(st->gpio_os->ndescs, st->gpio_os->desc, 240 242 st->gpio_os->info, values); 241 243 242 244 /* AD7616 requires a reset to update value */ ··· 255 257 struct ad7606_state *st = iio_priv(indio_dev); 256 258 int i, ret, ch = 0; 257 259 260 + guard(mutex)(&st->lock); 261 + 258 262 switch (mask) { 259 263 case IIO_CHAN_INFO_SCALE: 260 - mutex_lock(&st->lock); 261 264 i = find_closest(val2, st->scale_avail, st->num_scales); 262 265 if (st->sw_mode_en) 263 266 ch = chan->address; 264 267 ret = st->write_scale(indio_dev, ch, i); 265 - if (ret < 0) { 266 - mutex_unlock(&st->lock); 268 + if (ret < 0) 267 269 return ret; 268 - } 269 270 st->range[ch] = i; 270 - mutex_unlock(&st->lock); 271 271 272 272 return 0; 273 273 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: ··· 273 277 return -EINVAL; 274 278 i = find_closest(val, st->oversampling_avail, 275 279 st->num_os_ratios); 276 - mutex_lock(&st->lock); 277 280 ret = st->write_os(indio_dev, i); 278 - if (ret < 0) { 279 - mutex_unlock(&st->lock); 281 + if (ret < 0) 280 282 return ret; 281 - } 282 - st->oversampling = st->oversampling_avail[i]; 283 - mutex_unlock(&st->lock); 284 283 285 284 return 0; 286 285 default: ··· 434 443 return PTR_ERR(st->gpio_range); 435 444 436 445 st->gpio_standby = devm_gpiod_get_optional(dev, "standby", 437 - GPIOD_OUT_HIGH); 446 + GPIOD_OUT_LOW); 438 447 if (IS_ERR(st->gpio_standby)) 439 448 return PTR_ERR(st->gpio_standby); 440 449 ··· 677 686 678 687 if (st->gpio_standby) { 679 688 gpiod_set_value(st->gpio_range, 1); 680 - gpiod_set_value(st->gpio_standby, 0); 689 + gpiod_set_value(st->gpio_standby, 1); 681 690 } 682 691 683 692 return 0;
+3 -2
drivers/iio/adc/ad7606_spi.c
··· 249 249 static int ad7606B_sw_mode_config(struct iio_dev *indio_dev) 250 250 { 251 251 struct ad7606_state *st = iio_priv(indio_dev); 252 - unsigned long os[3] = {1}; 252 + DECLARE_BITMAP(os, 3); 253 253 254 + bitmap_fill(os, 3); 254 255 /* 255 256 * Software mode is enabled when all three oversampling 256 257 * pins are set to high. If oversampling gpios are defined ··· 259 258 * otherwise, they must be hardwired to VDD 260 259 */ 261 260 if (st->gpio_os) { 262 - gpiod_set_array_value(ARRAY_SIZE(os), 261 + gpiod_set_array_value(st->gpio_os->ndescs, 263 262 st->gpio_os->desc, st->gpio_os->info, os); 264 263 } 265 264 /* OS of 128 and 256 are available only in software mode */
+1 -4
drivers/iio/adc/ad7768-1.c
··· 544 544 { 545 545 struct ad7768_state *st = iio_priv(indio_dev); 546 546 struct device *device = indio_dev->dev.parent; 547 - struct fwnode_handle *fwnode; 548 - struct fwnode_handle *child; 549 547 const char *label; 550 548 int crt_ch = 0; 551 549 552 - fwnode = dev_fwnode(device); 553 - fwnode_for_each_child_node(fwnode, child) { 550 + device_for_each_child_node_scoped(device, child) { 554 551 if (fwnode_property_read_u32(child, "reg", &crt_ch)) 555 552 continue; 556 553
+2 -1
drivers/iio/adc/ad799x.c
··· 237 237 if (!st->rx_buf) 238 238 return -ENOMEM; 239 239 240 - st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2; 240 + st->transfer_size = bitmap_weight(scan_mask, 241 + iio_get_masklength(indio_dev)) * 2; 241 242 242 243 switch (st->id) { 243 244 case ad7992:
+429 -58
drivers/iio/adc/ad9467.c
··· 15 15 #include <linux/kernel.h> 16 16 #include <linux/slab.h> 17 17 #include <linux/spi/spi.h> 18 + #include <linux/seq_file.h> 18 19 #include <linux/err.h> 19 20 #include <linux/delay.h> 20 21 #include <linux/gpio/consumer.h> ··· 105 104 #define AD9467_DEF_OUTPUT_MODE 0x08 106 105 #define AD9467_REG_VREF_MASK 0x0F 107 106 107 + /* 108 + * Analog Devices AD9643 14-Bit, 170/210/250 MSPS ADC 109 + */ 110 + 111 + #define CHIPID_AD9643 0x82 112 + #define AD9643_REG_VREF_MASK 0x1F 113 + 114 + /* 115 + * Analog Devices AD9652 16-bit 310 MSPS ADC 116 + */ 117 + 118 + #define CHIPID_AD9652 0xC1 119 + #define AD9652_REG_VREF_MASK 0xC0 120 + 121 + /* 122 + * Analog Devices AD9649 14-bit 20/40/65/80 MSPS ADC 123 + */ 124 + 125 + #define CHIPID_AD9649 0x6F 126 + #define AD9649_TEST_POINTS 8 127 + 108 128 #define AD9647_MAX_TEST_POINTS 32 129 + #define AD9467_CAN_INVERT(st) \ 130 + (!(st)->info->has_dco || (st)->info->has_dco_invert) 109 131 110 132 struct ad9467_chip_info { 111 133 const char *name; ··· 137 113 unsigned int num_channels; 138 114 const unsigned int (*scale_table)[2]; 139 115 int num_scales; 116 + unsigned long test_mask; 117 + unsigned int test_mask_len; 140 118 unsigned long max_rate; 141 119 unsigned int default_output_mode; 142 120 unsigned int vref_mask; 143 121 unsigned int num_lanes; 122 + unsigned int dco_en; 123 + unsigned int test_points; 144 124 /* data clock output */ 145 125 bool has_dco; 126 + bool has_dco_invert; 127 + }; 128 + 129 + struct ad9467_chan_test_mode { 130 + struct ad9467_state *st; 131 + unsigned int idx; 132 + u8 mode; 146 133 }; 147 134 148 135 struct ad9467_state { ··· 161 126 struct iio_backend *back; 162 127 struct spi_device *spi; 163 128 struct clk *clk; 129 + /* used for debugfs */ 130 + struct ad9467_chan_test_mode *chan_test; 164 131 unsigned int output_mode; 165 132 unsigned int (*scales)[2]; 166 133 /* ··· 175 138 * at the io delay control section. 176 139 */ 177 140 DECLARE_BITMAP(calib_map, AD9647_MAX_TEST_POINTS * 2); 141 + /* number of bits of the map */ 142 + unsigned int calib_map_size; 178 143 struct gpio_desc *pwrdown_gpio; 179 144 /* ensure consistent state obtained on multiple related accesses */ 180 145 struct mutex lock; ··· 250 211 {2300, 8}, {2400, 9}, {2500, 10}, 251 212 }; 252 213 214 + static const unsigned int ad9643_scale_table[][2] = { 215 + {2087, 0x0F}, {2065, 0x0E}, {2042, 0x0D}, {2020, 0x0C}, {1997, 0x0B}, 216 + {1975, 0x0A}, {1952, 0x09}, {1930, 0x08}, {1907, 0x07}, {1885, 0x06}, 217 + {1862, 0x05}, {1840, 0x04}, {1817, 0x03}, {1795, 0x02}, {1772, 0x01}, 218 + {1750, 0x00}, {1727, 0x1F}, {1704, 0x1E}, {1681, 0x1D}, {1658, 0x1C}, 219 + {1635, 0x1B}, {1612, 0x1A}, {1589, 0x19}, {1567, 0x18}, {1544, 0x17}, 220 + {1521, 0x16}, {1498, 0x15}, {1475, 0x14}, {1452, 0x13}, {1429, 0x12}, 221 + {1406, 0x11}, {1383, 0x10}, 222 + }; 223 + 224 + static const unsigned int ad9649_scale_table[][2] = { 225 + {2000, 0}, 226 + }; 227 + 228 + static const unsigned int ad9652_scale_table[][2] = { 229 + {1250, 0}, {1125, 1}, {1200, 2}, {1250, 3}, {1000, 5}, 230 + }; 231 + 253 232 static void __ad9467_get_scale(struct ad9467_state *st, int index, 254 233 unsigned int *val, unsigned int *val2) 255 234 { ··· 281 224 *val2 = tmp % 1000000; 282 225 } 283 226 284 - #define AD9467_CHAN(_chan, _si, _bits, _sign) \ 227 + #define AD9467_CHAN(_chan, avai_mask, _si, _bits, _sign) \ 285 228 { \ 286 229 .type = IIO_VOLTAGE, \ 287 230 .indexed = 1, \ 288 231 .channel = _chan, \ 289 232 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 290 233 BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 291 - .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \ 234 + .info_mask_shared_by_type_available = avai_mask, \ 292 235 .scan_index = _si, \ 293 236 .scan_type = { \ 294 237 .sign = _sign, \ ··· 298 241 } 299 242 300 243 static const struct iio_chan_spec ad9434_channels[] = { 301 - AD9467_CHAN(0, 0, 12, 's'), 244 + AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 12, 's'), 302 245 }; 303 246 304 247 static const struct iio_chan_spec ad9467_channels[] = { 305 - AD9467_CHAN(0, 0, 16, 's'), 248 + AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 16, 's'), 249 + }; 250 + 251 + static const struct iio_chan_spec ad9643_channels[] = { 252 + AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 14, 's'), 253 + AD9467_CHAN(1, BIT(IIO_CHAN_INFO_SCALE), 1, 14, 's'), 254 + }; 255 + 256 + static const struct iio_chan_spec ad9649_channels[] = { 257 + AD9467_CHAN(0, 0, 0, 14, 's'), 258 + }; 259 + 260 + static const struct iio_chan_spec ad9652_channels[] = { 261 + AD9467_CHAN(0, BIT(IIO_CHAN_INFO_SCALE), 0, 16, 's'), 262 + AD9467_CHAN(1, BIT(IIO_CHAN_INFO_SCALE), 1, 16, 's'), 263 + }; 264 + 265 + static const char * const ad9467_test_modes[] = { 266 + [AN877_ADC_TESTMODE_OFF] = "off", 267 + [AN877_ADC_TESTMODE_MIDSCALE_SHORT] = "midscale_short", 268 + [AN877_ADC_TESTMODE_POS_FULLSCALE] = "pos_fullscale", 269 + [AN877_ADC_TESTMODE_NEG_FULLSCALE] = "neg_fullscale", 270 + [AN877_ADC_TESTMODE_ALT_CHECKERBOARD] = "checkerboard", 271 + [AN877_ADC_TESTMODE_PN23_SEQ] = "prbs23", 272 + [AN877_ADC_TESTMODE_PN9_SEQ] = "prbs9", 273 + [AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE] = "one_zero_toggle", 274 + [AN877_ADC_TESTMODE_USER] = "user", 275 + [AN877_ADC_TESTMODE_BIT_TOGGLE] = "bit_toggle", 276 + [AN877_ADC_TESTMODE_SYNC] = "sync", 277 + [AN877_ADC_TESTMODE_ONE_BIT_HIGH] = "one_bit_high", 278 + [AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY] = "mixed_bit_frequency", 279 + [AN877_ADC_TESTMODE_RAMP] = "ramp", 306 280 }; 307 281 308 282 static const struct ad9467_chip_info ad9467_chip_tbl = { ··· 344 256 .num_scales = ARRAY_SIZE(ad9467_scale_table), 345 257 .channels = ad9467_channels, 346 258 .num_channels = ARRAY_SIZE(ad9467_channels), 259 + .test_points = AD9647_MAX_TEST_POINTS, 260 + .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE, 261 + AN877_ADC_TESTMODE_OFF), 262 + .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1, 347 263 .default_output_mode = AD9467_DEF_OUTPUT_MODE, 348 264 .vref_mask = AD9467_REG_VREF_MASK, 349 265 .num_lanes = 8, ··· 361 269 .num_scales = ARRAY_SIZE(ad9434_scale_table), 362 270 .channels = ad9434_channels, 363 271 .num_channels = ARRAY_SIZE(ad9434_channels), 272 + .test_points = AD9647_MAX_TEST_POINTS, 273 + .test_mask = GENMASK(AN877_ADC_TESTMODE_USER, AN877_ADC_TESTMODE_OFF), 274 + .test_mask_len = AN877_ADC_TESTMODE_USER + 1, 364 275 .default_output_mode = AD9434_DEF_OUTPUT_MODE, 365 276 .vref_mask = AD9434_REG_VREF_MASK, 366 277 .num_lanes = 6, ··· 377 282 .num_scales = ARRAY_SIZE(ad9265_scale_table), 378 283 .channels = ad9467_channels, 379 284 .num_channels = ARRAY_SIZE(ad9467_channels), 285 + .test_points = AD9647_MAX_TEST_POINTS, 286 + .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE, 287 + AN877_ADC_TESTMODE_OFF), 288 + .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1, 380 289 .default_output_mode = AD9265_DEF_OUTPUT_MODE, 381 290 .vref_mask = AD9265_REG_VREF_MASK, 291 + .has_dco = true, 292 + .has_dco_invert = true, 293 + }; 294 + 295 + static const struct ad9467_chip_info ad9643_chip_tbl = { 296 + .name = "ad9643", 297 + .id = CHIPID_AD9643, 298 + .max_rate = 250000000UL, 299 + .scale_table = ad9643_scale_table, 300 + .num_scales = ARRAY_SIZE(ad9643_scale_table), 301 + .channels = ad9643_channels, 302 + .num_channels = ARRAY_SIZE(ad9643_channels), 303 + .test_points = AD9647_MAX_TEST_POINTS, 304 + .test_mask = BIT(AN877_ADC_TESTMODE_RAMP) | 305 + GENMASK(AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY, AN877_ADC_TESTMODE_OFF), 306 + .test_mask_len = AN877_ADC_TESTMODE_RAMP + 1, 307 + .vref_mask = AD9643_REG_VREF_MASK, 308 + .has_dco = true, 309 + .has_dco_invert = true, 310 + .dco_en = AN877_ADC_DCO_DELAY_ENABLE, 311 + }; 312 + 313 + static const struct ad9467_chip_info ad9649_chip_tbl = { 314 + .name = "ad9649", 315 + .id = CHIPID_AD9649, 316 + .max_rate = 80000000UL, 317 + .scale_table = ad9649_scale_table, 318 + .num_scales = ARRAY_SIZE(ad9649_scale_table), 319 + .channels = ad9649_channels, 320 + .num_channels = ARRAY_SIZE(ad9649_channels), 321 + .test_points = AD9649_TEST_POINTS, 322 + .test_mask = GENMASK(AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY, 323 + AN877_ADC_TESTMODE_OFF), 324 + .test_mask_len = AN877_ADC_TESTMODE_MIXED_BIT_FREQUENCY + 1, 325 + .has_dco = true, 326 + .has_dco_invert = true, 327 + .dco_en = AN877_ADC_DCO_DELAY_ENABLE, 328 + }; 329 + 330 + static const struct ad9467_chip_info ad9652_chip_tbl = { 331 + .name = "ad9652", 332 + .id = CHIPID_AD9652, 333 + .max_rate = 310000000UL, 334 + .scale_table = ad9652_scale_table, 335 + .num_scales = ARRAY_SIZE(ad9652_scale_table), 336 + .channels = ad9652_channels, 337 + .num_channels = ARRAY_SIZE(ad9652_channels), 338 + .test_points = AD9647_MAX_TEST_POINTS, 339 + .test_mask = GENMASK(AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE, 340 + AN877_ADC_TESTMODE_OFF), 341 + .test_mask_len = AN877_ADC_TESTMODE_ONE_ZERO_TOGGLE + 1, 342 + .vref_mask = AD9652_REG_VREF_MASK, 382 343 .has_dco = true, 383 344 }; 384 345 385 346 static int ad9467_get_scale(struct ad9467_state *st, int *val, int *val2) 386 347 { 387 348 const struct ad9467_chip_info *info = st->info; 388 - unsigned int i, vref_val; 349 + unsigned int vref_val; 350 + unsigned int i = 0; 389 351 int ret; 352 + 353 + /* nothing to read if we only have one possible scale */ 354 + if (info->num_scales == 1) 355 + goto out_get_scale; 390 356 391 357 ret = ad9467_spi_read(st, AN877_ADC_REG_VREF); 392 358 if (ret < 0) ··· 463 307 if (i == info->num_scales) 464 308 return -ERANGE; 465 309 310 + out_get_scale: 466 311 __ad9467_get_scale(st, i, val, val2); 467 312 468 313 return IIO_VAL_INT_PLUS_MICRO; ··· 478 321 479 322 if (val != 0) 480 323 return -EINVAL; 324 + if (info->num_scales == 1) 325 + return -EOPNOTSUPP; 481 326 482 327 for (i = 0; i < info->num_scales; i++) { 483 328 __ad9467_get_scale(st, i, &scale_val[0], &scale_val[1]); ··· 511 352 AN877_ADC_TRANSFER_SYNC); 512 353 } 513 354 514 - static int ad9647_calibrate_prepare(struct ad9467_state *st) 355 + static int ad9467_testmode_set(struct ad9467_state *st, unsigned int chan, 356 + unsigned int test_mode) 357 + { 358 + int ret; 359 + 360 + if (st->info->num_channels > 1) { 361 + /* so that the test mode is only applied to one channel */ 362 + ret = ad9467_spi_write(st, AN877_ADC_REG_CHAN_INDEX, BIT(chan)); 363 + if (ret) 364 + return ret; 365 + } 366 + 367 + ret = ad9467_spi_write(st, AN877_ADC_REG_TEST_IO, test_mode); 368 + if (ret) 369 + return ret; 370 + 371 + if (st->info->num_channels > 1) { 372 + /* go to default state where all channels get write commands */ 373 + ret = ad9467_spi_write(st, AN877_ADC_REG_CHAN_INDEX, 374 + GENMASK(st->info->num_channels - 1, 0)); 375 + if (ret) 376 + return ret; 377 + } 378 + 379 + return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER, 380 + AN877_ADC_TRANSFER_SYNC); 381 + } 382 + 383 + static int ad9467_backend_testmode_on(struct ad9467_state *st, 384 + unsigned int chan, 385 + enum iio_backend_test_pattern pattern) 515 386 { 516 387 struct iio_backend_data_fmt data = { 517 388 .enable = false, 518 389 }; 519 - unsigned int c; 520 390 int ret; 521 391 522 - ret = ad9467_spi_write(st, AN877_ADC_REG_TEST_IO, 523 - AN877_ADC_TESTMODE_PN9_SEQ); 392 + ret = iio_backend_data_format_set(st->back, chan, &data); 524 393 if (ret) 525 394 return ret; 526 395 527 - ret = ad9467_spi_write(st, AN877_ADC_REG_TRANSFER, 528 - AN877_ADC_TRANSFER_SYNC); 396 + ret = iio_backend_test_pattern_set(st->back, chan, pattern); 529 397 if (ret) 530 398 return ret; 399 + 400 + return iio_backend_chan_enable(st->back, chan); 401 + } 402 + 403 + static int ad9467_backend_testmode_off(struct ad9467_state *st, 404 + unsigned int chan) 405 + { 406 + struct iio_backend_data_fmt data = { 407 + .enable = true, 408 + .sign_extend = true, 409 + }; 410 + int ret; 411 + 412 + ret = iio_backend_chan_disable(st->back, chan); 413 + if (ret) 414 + return ret; 415 + 416 + ret = iio_backend_test_pattern_set(st->back, chan, 417 + IIO_BACKEND_NO_TEST_PATTERN); 418 + if (ret) 419 + return ret; 420 + 421 + return iio_backend_data_format_set(st->back, chan, &data); 422 + } 423 + 424 + static int ad9647_calibrate_prepare(struct ad9467_state *st) 425 + { 426 + unsigned int c; 427 + int ret; 531 428 532 429 ret = ad9467_outputmode_set(st, st->info->default_output_mode); 533 430 if (ret) 534 431 return ret; 535 432 536 433 for (c = 0; c < st->info->num_channels; c++) { 537 - ret = iio_backend_data_format_set(st->back, c, &data); 434 + ret = ad9467_testmode_set(st, c, AN877_ADC_TESTMODE_PN9_SEQ); 435 + if (ret) 436 + return ret; 437 + 438 + ret = ad9467_backend_testmode_on(st, c, 439 + IIO_BACKEND_ADI_PRBS_9A); 538 440 if (ret) 539 441 return ret; 540 442 } 541 443 542 - ret = iio_backend_test_pattern_set(st->back, 0, 543 - IIO_BACKEND_ADI_PRBS_9A); 544 - if (ret) 545 - return ret; 546 - 547 - return iio_backend_chan_enable(st->back, 0); 444 + return 0; 548 445 } 549 446 550 447 static int ad9647_calibrate_polarity_set(struct ad9467_state *st, ··· 657 442 658 443 if (st->info->has_dco) { 659 444 ret = ad9467_spi_write(st, AN877_ADC_REG_OUTPUT_DELAY, 660 - val); 445 + val | st->info->dco_en); 661 446 if (ret) 662 447 return ret; 663 448 ··· 676 461 677 462 static int ad9647_calibrate_stop(struct ad9467_state *st) 678 463 { 679 - struct iio_backend_data_fmt data = { 680 - .sign_extend = true, 681 - .enable = true, 682 - }; 683 464 unsigned int c, mode; 684 465 int ret; 685 466 686 - ret = iio_backend_chan_disable(st->back, 0); 687 - if (ret) 688 - return ret; 689 - 690 - ret = iio_backend_test_pattern_set(st->back, 0, 691 - IIO_BACKEND_NO_TEST_PATTERN); 692 - if (ret) 693 - return ret; 694 - 695 467 for (c = 0; c < st->info->num_channels; c++) { 696 - ret = iio_backend_data_format_set(st->back, c, &data); 468 + ret = ad9467_backend_testmode_off(st, c); 469 + if (ret) 470 + return ret; 471 + 472 + ret = ad9467_testmode_set(st, c, AN877_ADC_TESTMODE_OFF); 697 473 if (ret) 698 474 return ret; 699 475 } 700 476 701 477 mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT; 702 - ret = ad9467_outputmode_set(st, mode); 703 - if (ret) 704 - return ret; 705 - 706 - ret = ad9467_spi_write(st, AN877_ADC_REG_TEST_IO, 707 - AN877_ADC_TESTMODE_OFF); 708 - if (ret) 709 - return ret; 710 - 711 - return ad9467_spi_write(st, AN877_ADC_REG_TRANSFER, 712 - AN877_ADC_TRANSFER_SYNC); 478 + return ad9467_outputmode_set(st, mode); 713 479 } 714 480 715 481 static int ad9467_calibrate(struct ad9467_state *st) 716 482 { 717 - unsigned int point, val, inv_val, cnt, inv_cnt = 0; 483 + unsigned int point, val, inv_val, cnt, inv_cnt = 0, c; 718 484 /* 719 485 * Half of the bitmap is for the inverted signal. The number of test 720 486 * points is the same though... 721 487 */ 722 - unsigned int test_points = AD9647_MAX_TEST_POINTS; 488 + unsigned int test_points = st->info->test_points; 723 489 unsigned long sample_rate = clk_get_rate(st->clk); 724 490 struct device *dev = &st->spi->dev; 725 491 bool invert = false, stat; 726 492 int ret; 727 493 728 494 /* all points invalid */ 729 - bitmap_fill(st->calib_map, BITS_PER_TYPE(st->calib_map)); 495 + bitmap_fill(st->calib_map, st->calib_map_size); 730 496 731 497 ret = ad9647_calibrate_prepare(st); 732 498 if (ret) ··· 717 521 if (ret) 718 522 return ret; 719 523 720 - for (point = 0; point < test_points; point++) { 524 + for (point = 0; point < st->info->test_points; point++) { 721 525 ret = ad9467_calibrate_apply(st, point); 722 526 if (ret) 723 527 return ret; 724 528 725 - ret = iio_backend_chan_status(st->back, 0, &stat); 726 - if (ret) 727 - return ret; 529 + for (c = 0; c < st->info->num_channels; c++) { 530 + ret = iio_backend_chan_status(st->back, c, &stat); 531 + if (ret) 532 + return ret; 728 533 729 - __assign_bit(point + invert * test_points, st->calib_map, stat); 534 + /* 535 + * A point is considered valid if all channels report no 536 + * error. If one reports an error, then we consider the 537 + * point as invalid and we can break the loop right away. 538 + */ 539 + if (stat) { 540 + dev_dbg(dev, "Invalid point(%u, inv:%u) for CH:%u\n", 541 + point, invert, c); 542 + break; 543 + } 544 + 545 + if (c == st->info->num_channels - 1) 546 + __clear_bit(point + invert * test_points, 547 + st->calib_map); 548 + } 730 549 } 731 550 732 551 if (!invert) { ··· 752 541 * a row. 753 542 */ 754 543 if (cnt < 3) { 755 - invert = true; 756 - goto retune; 544 + if (AD9467_CAN_INVERT(st)) { 545 + invert = true; 546 + goto retune; 547 + } 548 + 549 + if (!cnt) 550 + return -EIO; 757 551 } 758 552 } else { 759 553 inv_cnt = ad9467_find_optimal_point(st->calib_map, test_points, ··· 895 679 return 0; 896 680 } 897 681 898 - static const struct iio_info ad9467_info = { 682 + static struct iio_info ad9467_info = { 899 683 .read_raw = ad9467_read_raw, 900 684 .write_raw = ad9467_write_raw, 901 685 .update_scan_mode = ad9467_update_scan_mode, ··· 978 762 return -ENODEV; 979 763 } 980 764 765 + static int ad9467_test_mode_available_show(struct seq_file *s, void *ignored) 766 + { 767 + struct ad9467_state *st = s->private; 768 + unsigned int bit; 769 + 770 + for_each_set_bit(bit, &st->info->test_mask, st->info->test_mask_len) 771 + seq_printf(s, "%s\n", ad9467_test_modes[bit]); 772 + 773 + return 0; 774 + } 775 + DEFINE_SHOW_ATTRIBUTE(ad9467_test_mode_available); 776 + 777 + static ssize_t ad9467_chan_test_mode_read(struct file *file, 778 + char __user *userbuf, size_t count, 779 + loff_t *ppos) 780 + { 781 + struct ad9467_chan_test_mode *chan = file->private_data; 782 + struct ad9467_state *st = chan->st; 783 + char buf[128] = {0}; 784 + size_t len; 785 + int ret; 786 + 787 + if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ || 788 + chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) { 789 + len = scnprintf(buf, sizeof(buf), "Running \"%s\" Test:\n\t", 790 + ad9467_test_modes[chan->mode]); 791 + 792 + ret = iio_backend_debugfs_print_chan_status(st->back, chan->idx, 793 + buf + len, 794 + sizeof(buf) - len); 795 + if (ret < 0) 796 + return ret; 797 + len += ret; 798 + } else if (chan->mode == AN877_ADC_TESTMODE_OFF) { 799 + len = scnprintf(buf, sizeof(buf), "No test Running...\n"); 800 + } else { 801 + len = scnprintf(buf, sizeof(buf), "Running \"%s\" Test on CH:%u\n", 802 + ad9467_test_modes[chan->mode], chan->idx); 803 + } 804 + 805 + return simple_read_from_buffer(userbuf, count, ppos, buf, len); 806 + } 807 + 808 + static ssize_t ad9467_chan_test_mode_write(struct file *file, 809 + const char __user *userbuf, 810 + size_t count, loff_t *ppos) 811 + { 812 + struct ad9467_chan_test_mode *chan = file->private_data; 813 + struct ad9467_state *st = chan->st; 814 + char test_mode[32] = {0}; 815 + unsigned int mode; 816 + int ret; 817 + 818 + ret = simple_write_to_buffer(test_mode, sizeof(test_mode) - 1, ppos, 819 + userbuf, count); 820 + if (ret < 0) 821 + return ret; 822 + 823 + for_each_set_bit(mode, &st->info->test_mask, st->info->test_mask_len) { 824 + if (sysfs_streq(test_mode, ad9467_test_modes[mode])) 825 + break; 826 + } 827 + 828 + if (mode == st->info->test_mask_len) 829 + return -EINVAL; 830 + 831 + guard(mutex)(&st->lock); 832 + 833 + if (mode == AN877_ADC_TESTMODE_OFF) { 834 + unsigned int out_mode; 835 + 836 + if (chan->mode == AN877_ADC_TESTMODE_PN9_SEQ || 837 + chan->mode == AN877_ADC_TESTMODE_PN23_SEQ) { 838 + ret = ad9467_backend_testmode_off(st, chan->idx); 839 + if (ret) 840 + return ret; 841 + } 842 + 843 + ret = ad9467_testmode_set(st, chan->idx, mode); 844 + if (ret) 845 + return ret; 846 + 847 + out_mode = st->info->default_output_mode | AN877_ADC_OUTPUT_MODE_TWOS_COMPLEMENT; 848 + ret = ad9467_outputmode_set(st, out_mode); 849 + if (ret) 850 + return ret; 851 + } else { 852 + ret = ad9467_outputmode_set(st, st->info->default_output_mode); 853 + if (ret) 854 + return ret; 855 + 856 + ret = ad9467_testmode_set(st, chan->idx, mode); 857 + if (ret) 858 + return ret; 859 + 860 + /* some patterns have a backend matching monitoring block */ 861 + if (mode == AN877_ADC_TESTMODE_PN9_SEQ) { 862 + ret = ad9467_backend_testmode_on(st, chan->idx, 863 + IIO_BACKEND_ADI_PRBS_9A); 864 + if (ret) 865 + return ret; 866 + } else if (mode == AN877_ADC_TESTMODE_PN23_SEQ) { 867 + ret = ad9467_backend_testmode_on(st, chan->idx, 868 + IIO_BACKEND_ADI_PRBS_23A); 869 + if (ret) 870 + return ret; 871 + } 872 + } 873 + 874 + chan->mode = mode; 875 + 876 + return count; 877 + } 878 + 879 + static const struct file_operations ad9467_chan_test_mode_fops = { 880 + .open = simple_open, 881 + .read = ad9467_chan_test_mode_read, 882 + .write = ad9467_chan_test_mode_write, 883 + .llseek = default_llseek, 884 + .owner = THIS_MODULE, 885 + }; 886 + 981 887 static ssize_t ad9467_dump_calib_table(struct file *file, 982 888 char __user *userbuf, 983 889 size_t count, loff_t *ppos) 984 890 { 985 891 struct ad9467_state *st = file->private_data; 986 - unsigned int bit, size = BITS_PER_TYPE(st->calib_map); 892 + unsigned int bit; 987 893 /* +2 for the newline and +1 for the string termination */ 988 894 unsigned char map[AD9647_MAX_TEST_POINTS * 2 + 3]; 989 895 ssize_t len = 0; ··· 1114 776 if (*ppos) 1115 777 goto out_read; 1116 778 1117 - for (bit = 0; bit < size; bit++) { 1118 - if (bit == size / 2) 779 + for (bit = 0; bit < st->calib_map_size; bit++) { 780 + if (AD9467_CAN_INVERT(st) && bit == st->calib_map_size / 2) 1119 781 len += scnprintf(map + len, sizeof(map) - len, "\n"); 1120 782 1121 783 len += scnprintf(map + len, sizeof(map) - len, "%c", ··· 1138 800 { 1139 801 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 1140 802 struct ad9467_state *st = iio_priv(indio_dev); 803 + char attr_name[32]; 804 + unsigned int chan; 1141 805 1142 806 if (!IS_ENABLED(CONFIG_DEBUG_FS)) 1143 807 return; 1144 808 809 + st->chan_test = devm_kcalloc(&st->spi->dev, st->info->num_channels, 810 + sizeof(*st->chan_test), GFP_KERNEL); 811 + if (!st->chan_test) 812 + return; 813 + 1145 814 debugfs_create_file("calibration_table_dump", 0400, d, st, 1146 815 &ad9467_calib_table_fops); 816 + 817 + for (chan = 0; chan < st->info->num_channels; chan++) { 818 + snprintf(attr_name, sizeof(attr_name), "in_voltage%u_test_mode", 819 + chan); 820 + st->chan_test[chan].idx = chan; 821 + st->chan_test[chan].st = st; 822 + debugfs_create_file(attr_name, 0600, d, &st->chan_test[chan], 823 + &ad9467_chan_test_mode_fops); 824 + } 825 + 826 + debugfs_create_file("in_voltage_test_mode_available", 0400, d, st, 827 + &ad9467_test_mode_available_fops); 828 + 829 + iio_backend_debugfs_add(st->back, indio_dev); 1147 830 } 1148 831 1149 832 static int ad9467_probe(struct spi_device *spi) ··· 1184 825 st->info = spi_get_device_match_data(spi); 1185 826 if (!st->info) 1186 827 return -ENODEV; 828 + 829 + st->calib_map_size = st->info->test_points; 830 + if (AD9467_CAN_INVERT(st)) 831 + st->calib_map_size *= 2; 1187 832 1188 833 st->clk = devm_clk_get_enabled(&spi->dev, "adc-clk"); 1189 834 if (IS_ERR(st->clk)) ··· 1213 850 return -ENODEV; 1214 851 } 1215 852 853 + if (st->info->num_scales > 1) 854 + ad9467_info.read_avail = ad9467_read_avail; 1216 855 indio_dev->name = st->info->name; 1217 856 indio_dev->channels = st->info->channels; 1218 857 indio_dev->num_channels = st->info->num_channels; ··· 1249 884 { .compatible = "adi,ad9265", .data = &ad9265_chip_tbl, }, 1250 885 { .compatible = "adi,ad9434", .data = &ad9434_chip_tbl, }, 1251 886 { .compatible = "adi,ad9467", .data = &ad9467_chip_tbl, }, 887 + { .compatible = "adi,ad9643", .data = &ad9643_chip_tbl, }, 888 + { .compatible = "adi,ad9649", .data = &ad9649_chip_tbl, }, 889 + { .compatible = "adi,ad9652", .data = &ad9652_chip_tbl, }, 1252 890 {} 1253 891 }; 1254 892 MODULE_DEVICE_TABLE(of, ad9467_of_match); ··· 1260 892 { "ad9265", (kernel_ulong_t)&ad9265_chip_tbl }, 1261 893 { "ad9434", (kernel_ulong_t)&ad9434_chip_tbl }, 1262 894 { "ad9467", (kernel_ulong_t)&ad9467_chip_tbl }, 895 + { "ad9643", (kernel_ulong_t)&ad9643_chip_tbl }, 896 + { "ad9649", (kernel_ulong_t)&ad9649_chip_tbl, }, 897 + { "ad9652", (kernel_ulong_t)&ad9652_chip_tbl, }, 1263 898 {} 1264 899 }; 1265 900 MODULE_DEVICE_TABLE(spi, ad9467_ids);
+3 -3
drivers/iio/adc/ad_sigma_delta.c
··· 351 351 352 352 if (sigma_delta->num_slots == 1) { 353 353 channel = find_first_bit(indio_dev->active_scan_mask, 354 - indio_dev->masklength); 354 + iio_get_masklength(indio_dev)); 355 355 ret = ad_sigma_delta_set_channel(sigma_delta, 356 356 indio_dev->channels[channel].address); 357 357 if (ret) ··· 364 364 * implementation is mandatory. 365 365 */ 366 366 slot = 0; 367 - for_each_set_bit(i, indio_dev->active_scan_mask, indio_dev->masklength) { 367 + iio_for_each_active_channel(indio_dev, i) { 368 368 sigma_delta->slots[slot] = indio_dev->channels[i].address; 369 369 slot++; 370 370 } ··· 526 526 { 527 527 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); 528 528 529 - return bitmap_weight(mask, indio_dev->masklength) <= sigma_delta->num_slots; 529 + return bitmap_weight(mask, iio_get_masklength(indio_dev)) <= sigma_delta->num_slots; 530 530 } 531 531 532 532 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
+65 -6
drivers/iio/adc/adi-axi-adc.c
··· 61 61 62 62 #define ADI_AXI_ADC_REG_CHAN_STATUS(c) (0x0404 + (c) * 0x40) 63 63 #define ADI_AXI_ADC_CHAN_STAT_PN_MASK GENMASK(2, 1) 64 + /* out of sync */ 65 + #define ADI_AXI_ADC_CHAN_STAT_PN_OOS BIT(1) 66 + /* spurious out of sync */ 67 + #define ADI_AXI_ADC_CHAN_STAT_PN_ERR BIT(2) 64 68 65 69 #define ADI_AXI_ADC_REG_CHAN_CTRL_3(c) (0x0418 + (c) * 0x40) 66 70 #define ADI_AXI_ADC_CHAN_PN_SEL_MASK GENMASK(19, 16) ··· 203 199 return regmap_update_bits(st->regmap, ADI_AXI_ADC_REG_CHAN_CTRL_3(chan), 204 200 ADI_AXI_ADC_CHAN_PN_SEL_MASK, 205 201 FIELD_PREP(ADI_AXI_ADC_CHAN_PN_SEL_MASK, 0)); 202 + case IIO_BACKEND_ADI_PRBS_23A: 203 + return regmap_update_bits(st->regmap, ADI_AXI_ADC_REG_CHAN_CTRL_3(chan), 204 + ADI_AXI_ADC_CHAN_PN_SEL_MASK, 205 + FIELD_PREP(ADI_AXI_ADC_CHAN_PN_SEL_MASK, 1)); 206 206 default: 207 207 return -EINVAL; 208 208 } 209 209 } 210 210 211 - static int axi_adc_chan_status(struct iio_backend *back, unsigned int chan, 212 - bool *error) 211 + static int axi_adc_read_chan_status(struct adi_axi_adc_state *st, unsigned int chan, 212 + unsigned int *status) 213 213 { 214 - struct adi_axi_adc_state *st = iio_backend_get_priv(back); 215 214 int ret; 216 - u32 val; 217 215 218 216 guard(mutex)(&st->lock); 219 217 /* reset test bits by setting them */ ··· 227 221 /* let's give enough time to validate or erroring the incoming pattern */ 228 222 fsleep(1000); 229 223 230 - ret = regmap_read(st->regmap, ADI_AXI_ADC_REG_CHAN_STATUS(chan), &val); 224 + return regmap_read(st->regmap, ADI_AXI_ADC_REG_CHAN_STATUS(chan), 225 + status); 226 + } 227 + 228 + static int axi_adc_chan_status(struct iio_backend *back, unsigned int chan, 229 + bool *error) 230 + { 231 + struct adi_axi_adc_state *st = iio_backend_get_priv(back); 232 + u32 val; 233 + int ret; 234 + 235 + ret = axi_adc_read_chan_status(st, chan, &val); 231 236 if (ret) 232 237 return ret; 233 238 ··· 248 231 *error = false; 249 232 250 233 return 0; 234 + } 235 + 236 + static int axi_adc_debugfs_print_chan_status(struct iio_backend *back, 237 + unsigned int chan, char *buf, 238 + size_t len) 239 + { 240 + struct adi_axi_adc_state *st = iio_backend_get_priv(back); 241 + u32 val; 242 + int ret; 243 + 244 + ret = axi_adc_read_chan_status(st, chan, &val); 245 + if (ret) 246 + return ret; 247 + 248 + /* 249 + * PN_ERR is cleared in case out of sync is set. Hence, no point in 250 + * checking both bits. 251 + */ 252 + if (val & ADI_AXI_ADC_CHAN_STAT_PN_OOS) 253 + return scnprintf(buf, len, "CH%u: Out of Sync.\n", chan); 254 + if (val & ADI_AXI_ADC_CHAN_STAT_PN_ERR) 255 + return scnprintf(buf, len, "CH%u: Spurious Out of Sync.\n", chan); 256 + 257 + return scnprintf(buf, len, "CH%u: OK.\n", chan); 251 258 } 252 259 253 260 static int axi_adc_chan_enable(struct iio_backend *back, unsigned int chan) ··· 308 267 iio_dmaengine_buffer_free(buffer); 309 268 } 310 269 270 + static int axi_adc_reg_access(struct iio_backend *back, unsigned int reg, 271 + unsigned int writeval, unsigned int *readval) 272 + { 273 + struct adi_axi_adc_state *st = iio_backend_get_priv(back); 274 + 275 + if (readval) 276 + return regmap_read(st->regmap, reg, readval); 277 + 278 + return regmap_write(st->regmap, reg, writeval); 279 + } 280 + 311 281 static const struct regmap_config axi_adc_regmap_config = { 312 282 .val_bits = 32, 313 283 .reg_bits = 32, 314 284 .reg_stride = 4, 315 285 }; 316 286 317 - static const struct iio_backend_ops adi_axi_adc_generic = { 287 + static const struct iio_backend_ops adi_axi_adc_ops = { 318 288 .enable = axi_adc_enable, 319 289 .disable = axi_adc_disable, 320 290 .data_format_set = axi_adc_data_format_set, ··· 337 285 .iodelay_set = axi_adc_iodelays_set, 338 286 .test_pattern_set = axi_adc_test_pattern_set, 339 287 .chan_status = axi_adc_chan_status, 288 + .debugfs_reg_access = iio_backend_debugfs_ptr(axi_adc_reg_access), 289 + .debugfs_print_chan_status = iio_backend_debugfs_ptr(axi_adc_debugfs_print_chan_status), 290 + }; 291 + 292 + static const struct iio_backend_info adi_axi_adc_generic = { 293 + .name = "axi-adc", 294 + .ops = &adi_axi_adc_ops, 340 295 }; 341 296 342 297 static int adi_axi_adc_probe(struct platform_device *pdev)
+1 -2
drivers/iio/adc/aspeed_adc.c
··· 555 555 if (ret) 556 556 return ret; 557 557 558 - if (of_find_property(data->dev->of_node, "aspeed,battery-sensing", 559 - NULL)) { 558 + if (of_property_present(data->dev->of_node, "aspeed,battery-sensing")) { 560 559 if (data->model_data->bat_sense_sup) { 561 560 data->battery_sensing = 1; 562 561 if (readl(data->base + ASPEED_REG_ENGINE_CONTROL) &
+6 -11
drivers/iio/adc/at91_adc.c
··· 7 7 8 8 #include <linux/bitmap.h> 9 9 #include <linux/bitops.h> 10 + #include <linux/cleanup.h> 10 11 #include <linux/clk.h> 11 12 #include <linux/err.h> 12 13 #include <linux/io.h> ··· 269 268 struct iio_chan_spec const *chan; 270 269 int i, j = 0; 271 270 272 - for (i = 0; i < idev->masklength; i++) { 273 - if (!test_bit(i, idev->active_scan_mask)) 274 - continue; 271 + iio_for_each_active_channel(idev, i) { 275 272 chan = idev->channels + i; 276 273 st->buffer[j] = at91_adc_readl(st, AT91_ADC_CHAN(st, chan->channel)); 277 274 j++; ··· 542 543 int i; 543 544 544 545 for (i = 0; i < st->caps->trigger_number; i++) { 545 - char *name = kasprintf(GFP_KERNEL, 546 - "%s-dev%d-%s", 547 - idev->name, 548 - iio_device_id(idev), 549 - triggers[i].name); 546 + char *name __free(kfree) = kasprintf(GFP_KERNEL, "%s-dev%d-%s", 547 + idev->name, 548 + iio_device_id(idev), 549 + triggers[i].name); 550 550 if (!name) 551 551 return -ENOMEM; 552 552 553 553 if (strcmp(trigger_name, name) == 0) { 554 - kfree(name); 555 554 if (triggers[i].value == 0) 556 555 return -EINVAL; 557 556 return triggers[i].value; 558 557 } 559 - 560 - kfree(name); 561 558 } 562 559 563 560 return -EINVAL;
+1 -3
drivers/iio/adc/cc10001_adc.c
··· 157 157 158 158 i = 0; 159 159 sample_invalid = false; 160 - for_each_set_bit(scan_idx, indio_dev->active_scan_mask, 161 - indio_dev->masklength) { 162 - 160 + iio_for_each_active_channel(indio_dev, scan_idx) { 163 161 channel = indio_dev->channels[scan_idx].channel; 164 162 cc10001_adc_start(adc_dev, channel); 165 163
+3 -5
drivers/iio/adc/dln2-adc.c
··· 108 108 dln2->demux_count = 0; 109 109 110 110 /* Optimize all 8-channels case */ 111 - if (indio_dev->masklength && 111 + if (iio_get_masklength(indio_dev) && 112 112 (*indio_dev->active_scan_mask & 0xff) == 0xff) { 113 113 dln2_adc_add_demux(dln2, 0, 0, 16); 114 114 dln2->ts_pad_offset = 0; ··· 117 117 } 118 118 119 119 /* Build demux table from fixed 8-channels to active_scan_mask */ 120 - for_each_set_bit(out_ind, 121 - indio_dev->active_scan_mask, 122 - indio_dev->masklength) { 120 + iio_for_each_active_channel(indio_dev, out_ind) { 123 121 /* Handle timestamp separately */ 124 122 if (out_ind == DLN2_ADC_MAX_CHANNELS) 125 123 break; ··· 539 541 540 542 /* Assign trigger channel based on first enabled channel */ 541 543 trigger_chan = find_first_bit(indio_dev->active_scan_mask, 542 - indio_dev->masklength); 544 + iio_get_masklength(indio_dev)); 543 545 if (trigger_chan < DLN2_ADC_MAX_CHANNELS) { 544 546 dln2->trigger_chan = trigger_chan; 545 547 ret = dln2_adc_set_chan_period(dln2, dln2->trigger_chan,
+1 -4
drivers/iio/adc/hx711.c
··· 363 363 364 364 memset(hx711_data->buffer, 0, sizeof(hx711_data->buffer)); 365 365 366 - for (i = 0; i < indio_dev->masklength; i++) { 367 - if (!test_bit(i, indio_dev->active_scan_mask)) 368 - continue; 369 - 366 + iio_for_each_active_channel(indio_dev, i) { 370 367 hx711_data->buffer[j] = hx711_reset_read(hx711_data, 371 368 indio_dev->channels[i].channel); 372 369 j++;
+1 -2
drivers/iio/adc/ina2xx-adc.c
··· 755 755 * Single register reads: bulk_read will not work with ina226/219 756 756 * as there is no auto-increment of the register pointer. 757 757 */ 758 - for_each_set_bit(bit, indio_dev->active_scan_mask, 759 - indio_dev->masklength) { 758 + iio_for_each_active_channel(indio_dev, bit) { 760 759 unsigned int val; 761 760 762 761 ret = regmap_read(chip->regmap,
+1 -2
drivers/iio/adc/max1118.c
··· 174 174 175 175 mutex_lock(&adc->lock); 176 176 177 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 178 - indio_dev->masklength) { 177 + iio_for_each_active_channel(indio_dev, scan_index) { 179 178 const struct iio_chan_spec *scan_chan = 180 179 &indio_dev->channels[scan_index]; 181 180 int ret = max1118_read(indio_dev, scan_chan->channel);
+13 -21
drivers/iio/adc/max1363.c
··· 13 13 */ 14 14 15 15 #include <linux/interrupt.h> 16 + #include <linux/cleanup.h> 16 17 #include <linux/device.h> 17 18 #include <linux/kernel.h> 18 19 #include <linux/sysfs.h> ··· 819 818 820 819 static int max1363_monitor_mode_update(struct max1363_state *st, int enabled) 821 820 { 822 - u8 *tx_buf; 823 821 int ret, i = 3, j; 824 822 unsigned long numelements; 825 823 int len; ··· 850 850 } 851 851 numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS); 852 852 len = 3 * numelements + 3; 853 - tx_buf = kmalloc(len, GFP_KERNEL); 854 - if (!tx_buf) { 855 - ret = -ENOMEM; 856 - goto error_ret; 857 - } 853 + u8 *tx_buf __free(kfree) = kmalloc(len, GFP_KERNEL); 854 + if (!tx_buf) 855 + return -ENOMEM; 856 + 858 857 tx_buf[0] = st->configbyte; 859 858 tx_buf[1] = st->setupbyte; 860 859 tx_buf[2] = (st->monitor_speed << 1); ··· 892 893 893 894 ret = st->send(st->client, tx_buf, len); 894 895 if (ret < 0) 895 - goto error_ret; 896 - if (ret != len) { 897 - ret = -EIO; 898 - goto error_ret; 899 - } 896 + return ret; 897 + if (ret != len) 898 + return -EIO; 900 899 901 900 /* 902 901 * Now that we hopefully have sensible thresholds in place it is ··· 907 910 tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0; 908 911 ret = st->send(st->client, tx_buf, 2); 909 912 if (ret < 0) 910 - goto error_ret; 911 - if (ret != 2) { 912 - ret = -EIO; 913 - goto error_ret; 914 - } 915 - ret = 0; 913 + return ret; 914 + if (ret != 2) 915 + return -EIO; 916 + 916 917 st->monitor_on = true; 917 - error_ret: 918 918 919 - kfree(tx_buf); 920 - 921 - return ret; 919 + return 0; 922 920 } 923 921 924 922 /*
+16 -38
drivers/iio/adc/mcp3564.c
··· 349 349 * struct mcp3564_state - working data for a ADC device 350 350 * @chip_info: chip specific data 351 351 * @spi: SPI device structure 352 - * @vref: the regulator device used as a voltage reference in case 353 - * external voltage reference is used 354 352 * @vref_mv: voltage reference value in miliVolts 355 353 * @lock: synchronize access to driver's state members 356 354 * @dev_addr: hardware device address ··· 367 369 struct mcp3564_state { 368 370 const struct mcp3564_chip_info *chip_info; 369 371 struct spi_device *spi; 370 - struct regulator *vref; 371 372 unsigned short vref_mv; 372 373 struct mutex lock; /* Synchronize access to driver's state members */ 373 374 u8 dev_addr; ··· 1082 1085 return 0; 1083 1086 } 1084 1087 1085 - static void mcp3564_disable_reg(void *reg) 1086 - { 1087 - regulator_disable(reg); 1088 - } 1089 - 1090 1088 static void mcp3564_fill_scale_tbls(struct mcp3564_state *adc) 1091 1089 { 1092 1090 unsigned int pow = adc->chip_info->resolution - 1; ··· 1102 1110 } 1103 1111 } 1104 1112 1105 - static int mcp3564_config(struct iio_dev *indio_dev) 1113 + static int mcp3564_config(struct iio_dev *indio_dev, bool *use_internal_vref_attr) 1106 1114 { 1107 1115 struct mcp3564_state *adc = iio_priv(indio_dev); 1108 1116 struct device *dev = &adc->spi->dev; ··· 1111 1119 enum mcp3564_ids ids; 1112 1120 int ret = 0; 1113 1121 unsigned int tmp = 0x01; 1122 + bool internal_vref; 1114 1123 bool err = false; 1115 1124 1116 1125 /* ··· 1211 1218 1212 1219 dev_dbg(dev, "Found %s chip\n", adc->chip_info->name); 1213 1220 1214 - adc->vref = devm_regulator_get_optional(dev, "vref"); 1215 - if (IS_ERR(adc->vref)) { 1216 - if (PTR_ERR(adc->vref) != -ENODEV) 1217 - return dev_err_probe(dev, PTR_ERR(adc->vref), 1218 - "failed to get regulator\n"); 1221 + ret = devm_regulator_get_enable_read_voltage(dev, "vref"); 1222 + if (ret < 0 && ret != -ENODEV) 1223 + return dev_err_probe(dev, ret, "Failed to get vref voltage\n"); 1219 1224 1225 + internal_vref = ret == -ENODEV; 1226 + adc->vref_mv = internal_vref ? MCP3564R_INT_VREF_MV : ret / MILLI; 1227 + *use_internal_vref_attr = internal_vref; 1228 + 1229 + if (internal_vref) { 1220 1230 /* Check if chip has internal vref */ 1221 1231 if (!adc->have_vref) 1222 - return dev_err_probe(dev, PTR_ERR(adc->vref), 1223 - "Unknown Vref\n"); 1224 - adc->vref = NULL; 1232 + return dev_err_probe(dev, -ENODEV, "Unknown Vref\n"); 1233 + 1225 1234 dev_dbg(dev, "%s: Using internal Vref\n", __func__); 1226 1235 } else { 1227 - ret = regulator_enable(adc->vref); 1228 - if (ret) 1229 - return ret; 1230 - 1231 - ret = devm_add_action_or_reset(dev, mcp3564_disable_reg, 1232 - adc->vref); 1233 - if (ret) 1234 - return ret; 1235 - 1236 1236 dev_dbg(dev, "%s: Using External Vref\n", __func__); 1237 - 1238 - ret = regulator_get_voltage(adc->vref); 1239 - if (ret < 0) 1240 - return dev_err_probe(dev, ret, 1241 - "Failed to read vref regulator\n"); 1242 - 1243 - adc->vref_mv = ret / MILLI; 1244 1237 } 1245 1238 1246 1239 ret = mcp3564_parse_fw_children(indio_dev); ··· 1329 1350 tmp_reg |= FIELD_PREP(MCP3564_CONFIG0_CLK_SEL_MASK, MCP3564_CONFIG0_USE_INT_CLK); 1330 1351 tmp_reg |= MCP3456_CONFIG0_BIT6_DEFAULT; 1331 1352 1332 - if (!adc->vref) { 1353 + if (internal_vref) 1333 1354 tmp_reg |= FIELD_PREP(MCP3456_CONFIG0_VREF_MASK, 1); 1334 - adc->vref_mv = MCP3564R_INT_VREF_MV; 1335 - } 1336 1355 1337 1356 ret = mcp3564_write_8bits(adc, MCP3564_CONFIG0_REG, tmp_reg); 1338 1357 ··· 1389 1412 int ret; 1390 1413 struct iio_dev *indio_dev; 1391 1414 struct mcp3564_state *adc; 1415 + bool use_internal_vref_attr; 1392 1416 1393 1417 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc)); 1394 1418 if (!indio_dev) ··· 1406 1428 * enable/disable certain channels 1407 1429 * change the sampling rate to the requested value 1408 1430 */ 1409 - ret = mcp3564_config(indio_dev); 1431 + ret = mcp3564_config(indio_dev, &use_internal_vref_attr); 1410 1432 if (ret) 1411 1433 return dev_err_probe(&spi->dev, ret, 1412 1434 "Can't configure MCP356X device\n"); ··· 1418 1440 indio_dev->name = adc->chip_info->name; 1419 1441 indio_dev->modes = INDIO_DIRECT_MODE; 1420 1442 1421 - if (!adc->vref) 1443 + if (use_internal_vref_attr) 1422 1444 indio_dev->info = &mcp3564r_info; 1423 1445 else 1424 1446 indio_dev->info = &mcp3564_info;
+17 -44
drivers/iio/adc/mcp3911.c
··· 103 103 const struct iio_chan_spec *channels; 104 104 unsigned int num_channels; 105 105 106 - int (*config)(struct mcp3911 *adc); 106 + int (*config)(struct mcp3911 *adc, bool external_vref); 107 107 int (*get_osr)(struct mcp3911 *adc, u32 *val); 108 108 int (*set_osr)(struct mcp3911 *adc, u32 val); 109 109 int (*enable_offset)(struct mcp3911 *adc, bool enable); ··· 115 115 struct mcp3911 { 116 116 struct spi_device *spi; 117 117 struct mutex lock; 118 - struct regulator *vref; 119 118 struct clk *clki; 120 119 u32 dev_addr; 121 120 struct iio_trigger *trig; ··· 384 385 } 385 386 } 386 387 387 - static int mcp3911_calc_scale_table(struct mcp3911 *adc) 388 + static int mcp3911_calc_scale_table(u32 vref_mv) 388 389 { 389 - struct device *dev = &adc->spi->dev; 390 - u32 ref = MCP3911_INT_VREF_MV; 391 390 u32 div; 392 - int ret; 393 391 u64 tmp; 394 - 395 - if (adc->vref) { 396 - ret = regulator_get_voltage(adc->vref); 397 - if (ret < 0) { 398 - return dev_err_probe(dev, ret, "failed to get vref voltage\n"); 399 - } 400 - 401 - ref = ret / 1000; 402 - } 403 392 404 393 /* 405 394 * For 24-bit Conversion ··· 399 412 */ 400 413 for (int i = 0; i < MCP3911_NUM_SCALES; i++) { 401 414 div = 12582912 * BIT(i); 402 - tmp = div_s64((s64)ref * 1000000000LL, div); 415 + tmp = div_s64((s64)vref_mv * 1000000000LL, div); 403 416 404 417 mcp3911_scale_table[i][0] = 0; 405 418 mcp3911_scale_table[i][1] = tmp; ··· 510 523 goto out; 511 524 } 512 525 513 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, indio_dev->masklength) { 526 + iio_for_each_active_channel(indio_dev, scan_index) { 514 527 const struct iio_chan_spec *scan_chan = &indio_dev->channels[scan_index]; 515 528 516 529 adc->scan.channels[i] = get_unaligned_be24(&adc->rx_buf[scan_chan->channel * 3]); ··· 531 544 .write_raw_get_fmt = mcp3911_write_raw_get_fmt, 532 545 }; 533 546 534 - static int mcp3911_config(struct mcp3911 *adc) 547 + static int mcp3911_config(struct mcp3911 *adc, bool external_vref) 535 548 { 536 549 struct device *dev = &adc->spi->dev; 537 550 u32 regval; ··· 542 555 return ret; 543 556 544 557 regval &= ~MCP3911_CONFIG_VREFEXT; 545 - if (adc->vref) { 558 + if (external_vref) { 546 559 dev_dbg(dev, "use external voltage reference\n"); 547 560 regval |= FIELD_PREP(MCP3911_CONFIG_VREFEXT, 1); 548 561 } else { ··· 597 610 return mcp3911_write(adc, MCP3911_REG_GAIN, regval, 1); 598 611 } 599 612 600 - static int mcp3910_config(struct mcp3911 *adc) 613 + static int mcp3910_config(struct mcp3911 *adc, bool external_vref) 601 614 { 602 615 struct device *dev = &adc->spi->dev; 603 616 u32 regval; ··· 608 621 return ret; 609 622 610 623 regval &= ~MCP3910_CONFIG1_VREFEXT; 611 - if (adc->vref) { 624 + if (external_vref) { 612 625 dev_dbg(dev, "use external voltage reference\n"); 613 626 regval |= FIELD_PREP(MCP3910_CONFIG1_VREFEXT, 1); 614 627 } else { ··· 664 677 return adc->chip->enable_offset(adc, 0); 665 678 } 666 679 667 - static void mcp3911_cleanup_regulator(void *vref) 668 - { 669 - regulator_disable(vref); 670 - } 671 - 672 680 static int mcp3911_set_trigger_state(struct iio_trigger *trig, bool enable) 673 681 { 674 682 struct mcp3911 *adc = iio_trigger_get_drvdata(trig); ··· 686 704 struct device *dev = &spi->dev; 687 705 struct iio_dev *indio_dev; 688 706 struct mcp3911 *adc; 707 + bool external_vref; 708 + u32 vref_mv; 689 709 int ret; 690 710 691 711 indio_dev = devm_iio_device_alloc(dev, sizeof(*adc)); ··· 698 714 adc->spi = spi; 699 715 adc->chip = spi_get_device_match_data(spi); 700 716 701 - adc->vref = devm_regulator_get_optional(dev, "vref"); 702 - if (IS_ERR(adc->vref)) { 703 - if (PTR_ERR(adc->vref) == -ENODEV) { 704 - adc->vref = NULL; 705 - } else { 706 - return dev_err_probe(dev, PTR_ERR(adc->vref), "failed to get regulator\n"); 707 - } 717 + ret = devm_regulator_get_enable_read_voltage(dev, "vref"); 718 + if (ret < 0 && ret != -ENODEV) 719 + return dev_err_probe(dev, ret, "failed to get vref voltage\n"); 708 720 709 - } else { 710 - ret = regulator_enable(adc->vref); 711 - if (ret) 712 - return ret; 713 - 714 - ret = devm_add_action_or_reset(dev, mcp3911_cleanup_regulator, adc->vref); 715 - if (ret) 716 - return ret; 717 - } 721 + external_vref = ret != -ENODEV; 722 + vref_mv = external_vref ? ret / 1000 : MCP3911_INT_VREF_MV; 718 723 719 724 adc->clki = devm_clk_get_enabled(dev, NULL); 720 725 if (IS_ERR(adc->clki)) { ··· 728 755 } 729 756 dev_dbg(dev, "use device address %i\n", adc->dev_addr); 730 757 731 - ret = adc->chip->config(adc); 758 + ret = adc->chip->config(adc, external_vref); 732 759 if (ret) 733 760 return ret; 734 761 735 - ret = mcp3911_calc_scale_table(adc); 762 + ret = mcp3911_calc_scale_table(vref_mv); 736 763 if (ret) 737 764 return ret; 738 765
+1 -1
drivers/iio/adc/mt6360-adc.c
··· 268 268 int i = 0, bit, val, ret; 269 269 270 270 memset(&data, 0, sizeof(data)); 271 - for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) { 271 + iio_for_each_active_channel(indio_dev, bit) { 272 272 ret = mt6360_adc_read_channel(mad, bit, &val); 273 273 if (ret < 0) { 274 274 dev_warn(&indio_dev->dev, "Failed to get channel %d conversion val\n", bit);
+1261
drivers/iio/adc/pac1921.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * IIO driver for PAC1921 High-Side Power/Current Monitor 4 + * 5 + * Copyright (C) 2024 Matteo Martelli <matteomartelli3@gmail.com> 6 + */ 7 + 8 + #include <asm/unaligned.h> 9 + #include <linux/bitfield.h> 10 + #include <linux/i2c.h> 11 + #include <linux/iio/events.h> 12 + #include <linux/iio/iio.h> 13 + #include <linux/iio/trigger_consumer.h> 14 + #include <linux/iio/triggered_buffer.h> 15 + #include <linux/regmap.h> 16 + #include <linux/units.h> 17 + 18 + /* pac1921 registers */ 19 + #define PAC1921_REG_GAIN_CFG 0x00 20 + #define PAC1921_REG_INT_CFG 0x01 21 + #define PAC1921_REG_CONTROL 0x02 22 + #define PAC1921_REG_VBUS 0x10 23 + #define PAC1921_REG_VSENSE 0x12 24 + #define PAC1921_REG_OVERFLOW_STS 0x1C 25 + #define PAC1921_REG_VPOWER 0x1D 26 + 27 + /* pac1921 gain configuration bits */ 28 + #define PAC1921_GAIN_DI_GAIN_MASK GENMASK(5, 3) 29 + #define PAC1921_GAIN_DV_GAIN_MASK GENMASK(2, 0) 30 + 31 + /* pac1921 integration configuration bits */ 32 + #define PAC1921_INT_CFG_SMPL_MASK GENMASK(7, 4) 33 + #define PAC1921_INT_CFG_VSFEN BIT(3) 34 + #define PAC1921_INT_CFG_VBFEN BIT(2) 35 + #define PAC1921_INT_CFG_RIOV BIT(1) 36 + #define PAC1921_INT_CFG_INTEN BIT(0) 37 + 38 + /* pac1921 control bits */ 39 + #define PAC1921_CONTROL_MXSL_MASK GENMASK(7, 6) 40 + enum pac1921_mxsl { 41 + PAC1921_MXSL_VPOWER_PIN = 0, 42 + PAC1921_MXSL_VSENSE_FREE_RUN = 1, 43 + PAC1921_MXSL_VBUS_FREE_RUN = 2, 44 + PAC1921_MXSL_VPOWER_FREE_RUN = 3, 45 + }; 46 + #define PAC1921_CONTROL_SLEEP BIT(2) 47 + 48 + /* pac1921 result registers mask and resolution */ 49 + #define PAC1921_RES_MASK GENMASK(15, 6) 50 + #define PAC1921_RES_RESOLUTION 1023 51 + 52 + /* pac1921 overflow status bits */ 53 + #define PAC1921_OVERFLOW_VSOV BIT(2) 54 + #define PAC1921_OVERFLOW_VBOV BIT(1) 55 + #define PAC1921_OVERFLOW_VPOV BIT(0) 56 + 57 + /* pac1921 constants */ 58 + #define PAC1921_MAX_VSENSE_MV 100 59 + #define PAC1921_MAX_VBUS_V 32 60 + /* Time to first communication after power up (tINT_T) */ 61 + #define PAC1921_POWERUP_TIME_MS 20 62 + /* Time from Sleep State to Start of Integration Period (tSLEEP_TO_INT) */ 63 + #define PAC1921_SLEEP_TO_INT_TIME_US 86 64 + 65 + /* pac1921 defaults */ 66 + #define PAC1921_DEFAULT_DV_GAIN 0 /* 2^(value): 1x gain (HW default) */ 67 + #define PAC1921_DEFAULT_DI_GAIN 0 /* 2^(value): 1x gain (HW default) */ 68 + #define PAC1921_DEFAULT_NUM_SAMPLES 0 /* 2^(value): 1 sample (HW default) */ 69 + 70 + /* 71 + * Pre-computed scale factors for BUS voltage 72 + * format: IIO_VAL_INT_PLUS_NANO 73 + * unit: mV 74 + * 75 + * Vbus scale (mV) = max_vbus (mV) / dv_gain / resolution 76 + */ 77 + static const int pac1921_vbus_scales[][2] = { 78 + { 31, 280547409 }, /* dv_gain x1 */ 79 + { 15, 640273704 }, /* dv_gain x2 */ 80 + { 7, 820136852 }, /* dv_gain x4 */ 81 + { 3, 910068426 }, /* dv_gain x8 */ 82 + { 1, 955034213 }, /* dv_gain x16 */ 83 + { 0, 977517106 }, /* dv_gain x32 */ 84 + }; 85 + 86 + /* 87 + * Pre-computed scales for SENSE voltage 88 + * format: IIO_VAL_INT_PLUS_NANO 89 + * unit: mV 90 + * 91 + * Vsense scale (mV) = max_vsense (mV) / di_gain / resolution 92 + */ 93 + static const int pac1921_vsense_scales[][2] = { 94 + { 0, 97751710 }, /* di_gain x1 */ 95 + { 0, 48875855 }, /* di_gain x2 */ 96 + { 0, 24437927 }, /* di_gain x4 */ 97 + { 0, 12218963 }, /* di_gain x8 */ 98 + { 0, 6109481 }, /* di_gain x16 */ 99 + { 0, 3054740 }, /* di_gain x32 */ 100 + { 0, 1527370 }, /* di_gain x64 */ 101 + { 0, 763685 }, /* di_gain x128 */ 102 + }; 103 + 104 + /* 105 + * Numbers of samples used to integrate measurements at the end of an 106 + * integration period. 107 + * 108 + * Changing the number of samples affects the integration period: higher the 109 + * number of samples, longer the integration period. 110 + * 111 + * These correspond to the oversampling ratios available exposed to userspace. 112 + */ 113 + static const int pac1921_int_num_samples[] = { 114 + 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 115 + }; 116 + 117 + /* 118 + * The integration period depends on the configuration of number of integration 119 + * samples, measurement resolution and post filters. The following array 120 + * contains integration periods, in microsecs unit, based on table 4-5 from 121 + * datasheet considering power integration mode, 14-Bit resolution and post 122 + * filters on. Each index corresponds to a specific number of samples from 1 123 + * to 2048. 124 + */ 125 + static const unsigned int pac1921_int_periods_usecs[] = { 126 + 2720, /* 1 sample */ 127 + 4050, /* 2 samples */ 128 + 6790, /* 4 samples */ 129 + 12200, /* 8 samples */ 130 + 23000, /* 16 samples */ 131 + 46000, /* 32 samples */ 132 + 92000, /* 64 samples */ 133 + 184000, /* 128 samples */ 134 + 368000, /* 256 samples */ 135 + 736000, /* 512 samples */ 136 + 1471000, /* 1024 samples */ 137 + 2941000 /* 2048 samples */ 138 + }; 139 + 140 + /* pac1921 regmap configuration */ 141 + static const struct regmap_range pac1921_regmap_wr_ranges[] = { 142 + regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL), 143 + }; 144 + 145 + static const struct regmap_access_table pac1921_regmap_wr_table = { 146 + .yes_ranges = pac1921_regmap_wr_ranges, 147 + .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_wr_ranges), 148 + }; 149 + 150 + static const struct regmap_range pac1921_regmap_rd_ranges[] = { 151 + regmap_reg_range(PAC1921_REG_GAIN_CFG, PAC1921_REG_CONTROL), 152 + regmap_reg_range(PAC1921_REG_VBUS, PAC1921_REG_VPOWER + 1), 153 + }; 154 + 155 + static const struct regmap_access_table pac1921_regmap_rd_table = { 156 + .yes_ranges = pac1921_regmap_rd_ranges, 157 + .n_yes_ranges = ARRAY_SIZE(pac1921_regmap_rd_ranges), 158 + }; 159 + 160 + static const struct regmap_config pac1921_regmap_config = { 161 + .reg_bits = 8, 162 + .val_bits = 8, 163 + .rd_table = &pac1921_regmap_rd_table, 164 + .wr_table = &pac1921_regmap_wr_table, 165 + }; 166 + 167 + enum pac1921_channels { 168 + PAC1921_CHAN_VBUS = 0, 169 + PAC1921_CHAN_VSENSE = 1, 170 + PAC1921_CHAN_CURRENT = 2, 171 + PAC1921_CHAN_POWER = 3, 172 + }; 173 + #define PAC1921_NUM_MEAS_CHANS 4 174 + 175 + struct pac1921_priv { 176 + struct i2c_client *client; 177 + struct regmap *regmap; 178 + struct regulator *vdd; 179 + struct iio_info iio_info; 180 + 181 + /* 182 + * Synchronize access to private members, and ensure atomicity of 183 + * consecutive regmap operations. 184 + */ 185 + struct mutex lock; 186 + 187 + u32 rshunt_uohm; /* uOhm */ 188 + u8 dv_gain; 189 + u8 di_gain; 190 + u8 n_samples; 191 + u8 prev_ovf_flags; 192 + u8 ovf_enabled_events; 193 + 194 + bool first_integr_started; 195 + bool first_integr_done; 196 + unsigned long integr_started_time_jiffies; 197 + unsigned int integr_period_usecs; 198 + 199 + int current_scales[ARRAY_SIZE(pac1921_vsense_scales)][2]; 200 + 201 + struct { 202 + u16 chan[PAC1921_NUM_MEAS_CHANS]; 203 + s64 timestamp __aligned(8); 204 + } scan; 205 + }; 206 + 207 + /* 208 + * Check if first integration after configuration update has completed. 209 + * 210 + * Must be called with lock held. 211 + */ 212 + static bool pac1921_data_ready(struct pac1921_priv *priv) 213 + { 214 + if (!priv->first_integr_started) 215 + return false; 216 + 217 + if (!priv->first_integr_done) { 218 + unsigned long t_ready; 219 + 220 + /* 221 + * Data valid after the device entered into integration state, 222 + * considering worst case where the device was in sleep state, 223 + * and completed the first integration period. 224 + */ 225 + t_ready = priv->integr_started_time_jiffies + 226 + usecs_to_jiffies(PAC1921_SLEEP_TO_INT_TIME_US) + 227 + usecs_to_jiffies(priv->integr_period_usecs); 228 + 229 + if (time_before(jiffies, t_ready)) 230 + return false; 231 + 232 + priv->first_integr_done = true; 233 + } 234 + 235 + return true; 236 + } 237 + 238 + static inline void pac1921_calc_scale(int dividend, int divisor, int *val, 239 + int *val2) 240 + { 241 + s64 tmp; 242 + 243 + tmp = div_s64(dividend * (s64)NANO, divisor); 244 + *val = (int)div_s64_rem(tmp, NANO, val2); 245 + } 246 + 247 + /* 248 + * Fill the table of scale factors for current 249 + * format: IIO_VAL_INT_PLUS_NANO 250 + * unit: mA 251 + * 252 + * Vsense LSB (nV) = max_vsense (nV) * di_gain / resolution 253 + * Current scale (mA) = Vsense LSB (nV) / shunt (uOhm) 254 + * 255 + * Must be called with held lock when updating after first initialization. 256 + */ 257 + static void pac1921_calc_current_scales(struct pac1921_priv *priv) 258 + { 259 + for (unsigned int i = 0; i < ARRAY_SIZE(priv->current_scales); i++) { 260 + int max = (PAC1921_MAX_VSENSE_MV * MICRO) >> i; 261 + int vsense_lsb = DIV_ROUND_CLOSEST(max, PAC1921_RES_RESOLUTION); 262 + 263 + pac1921_calc_scale(vsense_lsb, (int)priv->rshunt_uohm, 264 + &priv->current_scales[i][0], 265 + &priv->current_scales[i][1]); 266 + } 267 + } 268 + 269 + /* 270 + * Check if overflow occurred and if so, push the corresponding events. 271 + * 272 + * Must be called with lock held. 273 + */ 274 + static int pac1921_check_push_overflow(struct iio_dev *indio_dev, s64 timestamp) 275 + { 276 + struct pac1921_priv *priv = iio_priv(indio_dev); 277 + unsigned int flags; 278 + int ret; 279 + 280 + ret = regmap_read(priv->regmap, PAC1921_REG_OVERFLOW_STS, &flags); 281 + if (ret) 282 + return ret; 283 + 284 + if (flags & PAC1921_OVERFLOW_VBOV && 285 + !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VBOV) && 286 + priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV) { 287 + iio_push_event(indio_dev, 288 + IIO_UNMOD_EVENT_CODE( 289 + IIO_VOLTAGE, PAC1921_CHAN_VBUS, 290 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 291 + timestamp); 292 + } 293 + if (flags & PAC1921_OVERFLOW_VSOV && 294 + !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VSOV) && 295 + priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV) { 296 + iio_push_event(indio_dev, 297 + IIO_UNMOD_EVENT_CODE( 298 + IIO_VOLTAGE, PAC1921_CHAN_VSENSE, 299 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 300 + timestamp); 301 + iio_push_event(indio_dev, 302 + IIO_UNMOD_EVENT_CODE( 303 + IIO_CURRENT, PAC1921_CHAN_CURRENT, 304 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 305 + timestamp); 306 + } 307 + if (flags & PAC1921_OVERFLOW_VPOV && 308 + !(priv->prev_ovf_flags & PAC1921_OVERFLOW_VPOV) && 309 + priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV) { 310 + iio_push_event(indio_dev, 311 + IIO_UNMOD_EVENT_CODE( 312 + IIO_POWER, PAC1921_CHAN_POWER, 313 + IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING), 314 + timestamp); 315 + } 316 + 317 + priv->prev_ovf_flags = (u8)flags; 318 + 319 + return 0; 320 + } 321 + 322 + /* 323 + * Read the value from a result register 324 + * 325 + * Result registers contain the most recent averaged values of Vbus, Vsense and 326 + * Vpower. Each value is 10 bits wide and spread across two consecutive 8 bit 327 + * registers, with 6 bit LSB zero padding. 328 + */ 329 + static int pac1921_read_res(struct pac1921_priv *priv, unsigned long reg, 330 + u16 *val) 331 + { 332 + int ret = regmap_bulk_read(priv->regmap, (unsigned int)reg, val, 333 + sizeof(*val)); 334 + if (ret) 335 + return ret; 336 + 337 + *val = FIELD_GET(PAC1921_RES_MASK, get_unaligned_be16(val)); 338 + 339 + return 0; 340 + } 341 + 342 + static int pac1921_read_raw(struct iio_dev *indio_dev, 343 + struct iio_chan_spec const *chan, int *val, 344 + int *val2, long mask) 345 + { 346 + struct pac1921_priv *priv = iio_priv(indio_dev); 347 + 348 + guard(mutex)(&priv->lock); 349 + 350 + switch (mask) { 351 + case IIO_CHAN_INFO_RAW: { 352 + s64 ts; 353 + u16 res_val; 354 + int ret; 355 + 356 + if (!pac1921_data_ready(priv)) 357 + return -EBUSY; 358 + 359 + ts = iio_get_time_ns(indio_dev); 360 + 361 + ret = pac1921_check_push_overflow(indio_dev, ts); 362 + if (ret) 363 + return ret; 364 + 365 + ret = pac1921_read_res(priv, chan->address, &res_val); 366 + if (ret) 367 + return ret; 368 + 369 + *val = (int)res_val; 370 + 371 + return IIO_VAL_INT; 372 + } 373 + case IIO_CHAN_INFO_SCALE: 374 + switch (chan->channel) { 375 + case PAC1921_CHAN_VBUS: 376 + *val = pac1921_vbus_scales[priv->dv_gain][0]; 377 + *val2 = pac1921_vbus_scales[priv->dv_gain][1]; 378 + return IIO_VAL_INT_PLUS_NANO; 379 + 380 + case PAC1921_CHAN_VSENSE: 381 + *val = pac1921_vsense_scales[priv->di_gain][0]; 382 + *val2 = pac1921_vsense_scales[priv->di_gain][1]; 383 + return IIO_VAL_INT_PLUS_NANO; 384 + 385 + case PAC1921_CHAN_CURRENT: 386 + *val = priv->current_scales[priv->di_gain][0]; 387 + *val2 = priv->current_scales[priv->di_gain][1]; 388 + return IIO_VAL_INT_PLUS_NANO; 389 + 390 + case PAC1921_CHAN_POWER: { 391 + /* 392 + * Power scale factor in mW: 393 + * Current scale (mA) * max_vbus (V) / dv_gain 394 + */ 395 + 396 + /* Get current scale based on di_gain */ 397 + int *curr_scale = priv->current_scales[priv->di_gain]; 398 + 399 + /* Convert current_scale from INT_PLUS_NANO to INT */ 400 + s64 tmp = curr_scale[0] * (s64)NANO + curr_scale[1]; 401 + 402 + /* Multiply by max_vbus (V) / dv_gain */ 403 + tmp *= PAC1921_MAX_VBUS_V >> (int)priv->dv_gain; 404 + 405 + /* Convert back to INT_PLUS_NANO */ 406 + *val = (int)div_s64_rem(tmp, NANO, val2); 407 + 408 + return IIO_VAL_INT_PLUS_NANO; 409 + } 410 + default: 411 + return -EINVAL; 412 + } 413 + 414 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 415 + *val = pac1921_int_num_samples[priv->n_samples]; 416 + return IIO_VAL_INT; 417 + 418 + case IIO_CHAN_INFO_SAMP_FREQ: 419 + /* 420 + * The sampling frequency (Hz) is read-only and corresponds to 421 + * how often the device provides integrated measurements into 422 + * the result registers, thus it's 1/integration_period. 423 + * The integration period depends on the number of integration 424 + * samples, measurement resolution and post filters. 425 + * 426 + * 1/(integr_period_usecs/MICRO) = MICRO/integr_period_usecs 427 + */ 428 + *val = MICRO; 429 + *val2 = (int)priv->integr_period_usecs; 430 + return IIO_VAL_FRACTIONAL; 431 + 432 + default: 433 + return -EINVAL; 434 + } 435 + } 436 + 437 + static int pac1921_read_avail(struct iio_dev *indio_dev, 438 + struct iio_chan_spec const *chan, 439 + const int **vals, int *type, int *length, 440 + long mask) 441 + { 442 + switch (mask) { 443 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 444 + *type = IIO_VAL_INT; 445 + *vals = pac1921_int_num_samples; 446 + *length = ARRAY_SIZE(pac1921_int_num_samples); 447 + return IIO_AVAIL_LIST; 448 + default: 449 + return -EINVAL; 450 + } 451 + } 452 + 453 + /* 454 + * Perform configuration update sequence: set the device into read state, then 455 + * write the config register and set the device back into integration state. 456 + * Also reset integration start time and mark first integration to be yet 457 + * completed. 458 + * 459 + * Must be called with lock held. 460 + */ 461 + static int pac1921_update_cfg_reg(struct pac1921_priv *priv, unsigned int reg, 462 + unsigned int mask, unsigned int val) 463 + { 464 + /* Enter READ state before configuration */ 465 + int ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 466 + PAC1921_INT_CFG_INTEN, 0); 467 + if (ret) 468 + return ret; 469 + 470 + /* Update configuration value */ 471 + ret = regmap_update_bits(priv->regmap, reg, mask, val); 472 + if (ret) 473 + return ret; 474 + 475 + /* Re-enable integration */ 476 + ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 477 + PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN); 478 + if (ret) 479 + return ret; 480 + 481 + /* 482 + * Reset integration started time and mark this integration period as 483 + * the first one so that new measurements will be considered as valid 484 + * only at the end of this integration period. 485 + */ 486 + priv->integr_started_time_jiffies = jiffies; 487 + priv->first_integr_done = false; 488 + 489 + return 0; 490 + } 491 + 492 + /* 493 + * Retrieve the index of the given scale (represented by scale_val and 494 + * scale_val2) from scales_tbl. The returned index (if found) is the log2 of 495 + * the gain corresponding to the given scale. 496 + * 497 + * Must be called with lock held if the scales_tbl can change runtime (e.g. for 498 + * the current scales table) 499 + */ 500 + static int pac1921_lookup_scale(const int (*const scales_tbl)[2], size_t size, 501 + int scale_val, int scale_val2) 502 + { 503 + for (unsigned int i = 0; i < size; i++) 504 + if (scales_tbl[i][0] == scale_val && 505 + scales_tbl[i][1] == scale_val2) 506 + return (int)i; 507 + 508 + return -EINVAL; 509 + } 510 + 511 + /* 512 + * Configure device with the given gain (only if changed) 513 + * 514 + * Must be called with lock held. 515 + */ 516 + static int pac1921_update_gain(struct pac1921_priv *priv, u8 *priv_val, u8 gain, 517 + unsigned int mask) 518 + { 519 + unsigned int reg_val; 520 + int ret; 521 + 522 + if (*priv_val == gain) 523 + return 0; 524 + 525 + reg_val = (gain << __ffs(mask)) & mask; 526 + ret = pac1921_update_cfg_reg(priv, PAC1921_REG_GAIN_CFG, mask, reg_val); 527 + if (ret) 528 + return ret; 529 + 530 + *priv_val = gain; 531 + 532 + return 0; 533 + } 534 + 535 + /* 536 + * Given a scale factor represented by scale_val and scale_val2 with format 537 + * IIO_VAL_INT_PLUS_NANO, find the corresponding gain value and write it to the 538 + * device. 539 + * 540 + * Must be called with lock held. 541 + */ 542 + static int pac1921_update_gain_from_scale(struct pac1921_priv *priv, 543 + struct iio_chan_spec const *chan, 544 + int scale_val, int scale_val2) 545 + { 546 + int ret; 547 + 548 + switch (chan->channel) { 549 + case PAC1921_CHAN_VBUS: 550 + ret = pac1921_lookup_scale(pac1921_vbus_scales, 551 + ARRAY_SIZE(pac1921_vbus_scales), 552 + scale_val, scale_val2); 553 + if (ret < 0) 554 + return ret; 555 + 556 + return pac1921_update_gain(priv, &priv->dv_gain, (u8)ret, 557 + PAC1921_GAIN_DV_GAIN_MASK); 558 + case PAC1921_CHAN_VSENSE: 559 + ret = pac1921_lookup_scale(pac1921_vsense_scales, 560 + ARRAY_SIZE(pac1921_vsense_scales), 561 + scale_val, scale_val2); 562 + if (ret < 0) 563 + return ret; 564 + 565 + return pac1921_update_gain(priv, &priv->di_gain, (u8)ret, 566 + PAC1921_GAIN_DI_GAIN_MASK); 567 + case PAC1921_CHAN_CURRENT: 568 + ret = pac1921_lookup_scale(priv->current_scales, 569 + ARRAY_SIZE(priv->current_scales), 570 + scale_val, scale_val2); 571 + if (ret < 0) 572 + return ret; 573 + 574 + return pac1921_update_gain(priv, &priv->di_gain, (u8)ret, 575 + PAC1921_GAIN_DI_GAIN_MASK); 576 + default: 577 + return -EINVAL; 578 + } 579 + } 580 + 581 + /* 582 + * Retrieve the index of the given number of samples from the constant table. 583 + * The returned index (if found) is the log2 of the given num_samples. 584 + */ 585 + static int pac1921_lookup_int_num_samples(int num_samples) 586 + { 587 + for (unsigned int i = 0; i < ARRAY_SIZE(pac1921_int_num_samples); i++) 588 + if (pac1921_int_num_samples[i] == num_samples) 589 + return (int)i; 590 + 591 + return -EINVAL; 592 + } 593 + 594 + /* 595 + * Update the device with the given number of integration samples. 596 + * 597 + * Must be called with lock held. 598 + */ 599 + static int pac1921_update_int_num_samples(struct pac1921_priv *priv, 600 + int num_samples) 601 + { 602 + unsigned int reg_val; 603 + u8 n_samples; 604 + int ret; 605 + 606 + ret = pac1921_lookup_int_num_samples(num_samples); 607 + if (ret < 0) 608 + return ret; 609 + 610 + n_samples = (u8)ret; 611 + 612 + if (priv->n_samples == n_samples) 613 + return 0; 614 + 615 + reg_val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, n_samples); 616 + 617 + ret = pac1921_update_cfg_reg(priv, PAC1921_REG_INT_CFG, 618 + PAC1921_INT_CFG_SMPL_MASK, reg_val); 619 + if (ret) 620 + return ret; 621 + 622 + priv->n_samples = n_samples; 623 + 624 + priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples]; 625 + 626 + return 0; 627 + } 628 + 629 + static int pac1921_write_raw_get_fmt(struct iio_dev *indio_dev, 630 + struct iio_chan_spec const *chan, 631 + long info) 632 + { 633 + switch (info) { 634 + case IIO_CHAN_INFO_SCALE: 635 + return IIO_VAL_INT_PLUS_NANO; 636 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 637 + return IIO_VAL_INT; 638 + default: 639 + return -EINVAL; 640 + } 641 + } 642 + 643 + static int pac1921_write_raw(struct iio_dev *indio_dev, 644 + struct iio_chan_spec const *chan, int val, 645 + int val2, long mask) 646 + { 647 + struct pac1921_priv *priv = iio_priv(indio_dev); 648 + 649 + guard(mutex)(&priv->lock); 650 + 651 + switch (mask) { 652 + case IIO_CHAN_INFO_SCALE: 653 + return pac1921_update_gain_from_scale(priv, chan, val, val2); 654 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 655 + return pac1921_update_int_num_samples(priv, val); 656 + default: 657 + return -EINVAL; 658 + } 659 + } 660 + 661 + static int pac1921_read_label(struct iio_dev *indio_dev, 662 + struct iio_chan_spec const *chan, char *label) 663 + { 664 + switch (chan->channel) { 665 + case PAC1921_CHAN_VBUS: 666 + return sprintf(label, "vbus\n"); 667 + case PAC1921_CHAN_VSENSE: 668 + return sprintf(label, "vsense\n"); 669 + case PAC1921_CHAN_CURRENT: 670 + return sprintf(label, "current\n"); 671 + case PAC1921_CHAN_POWER: 672 + return sprintf(label, "power\n"); 673 + default: 674 + return -EINVAL; 675 + } 676 + } 677 + 678 + static int pac1921_read_event_config(struct iio_dev *indio_dev, 679 + const struct iio_chan_spec *chan, 680 + enum iio_event_type type, 681 + enum iio_event_direction dir) 682 + { 683 + struct pac1921_priv *priv = iio_priv(indio_dev); 684 + 685 + guard(mutex)(&priv->lock); 686 + 687 + switch (chan->channel) { 688 + case PAC1921_CHAN_VBUS: 689 + return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VBOV); 690 + case PAC1921_CHAN_VSENSE: 691 + case PAC1921_CHAN_CURRENT: 692 + return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VSOV); 693 + case PAC1921_CHAN_POWER: 694 + return !!(priv->ovf_enabled_events & PAC1921_OVERFLOW_VPOV); 695 + default: 696 + return -EINVAL; 697 + } 698 + } 699 + 700 + static int pac1921_write_event_config(struct iio_dev *indio_dev, 701 + const struct iio_chan_spec *chan, 702 + enum iio_event_type type, 703 + enum iio_event_direction dir, int state) 704 + { 705 + struct pac1921_priv *priv = iio_priv(indio_dev); 706 + u8 ovf_bit; 707 + 708 + guard(mutex)(&priv->lock); 709 + 710 + switch (chan->channel) { 711 + case PAC1921_CHAN_VBUS: 712 + ovf_bit = PAC1921_OVERFLOW_VBOV; 713 + break; 714 + case PAC1921_CHAN_VSENSE: 715 + case PAC1921_CHAN_CURRENT: 716 + ovf_bit = PAC1921_OVERFLOW_VSOV; 717 + break; 718 + case PAC1921_CHAN_POWER: 719 + ovf_bit = PAC1921_OVERFLOW_VPOV; 720 + break; 721 + default: 722 + return -EINVAL; 723 + } 724 + 725 + if (state) 726 + priv->ovf_enabled_events |= ovf_bit; 727 + else 728 + priv->ovf_enabled_events &= ~ovf_bit; 729 + 730 + return 0; 731 + } 732 + 733 + static int pac1921_read_event_value(struct iio_dev *indio_dev, 734 + const struct iio_chan_spec *chan, 735 + enum iio_event_type type, 736 + enum iio_event_direction dir, 737 + enum iio_event_info info, int *val, 738 + int *val2) 739 + { 740 + switch (info) { 741 + case IIO_EV_INFO_VALUE: 742 + *val = PAC1921_RES_RESOLUTION; 743 + return IIO_VAL_INT; 744 + default: 745 + return -EINVAL; 746 + } 747 + } 748 + 749 + static const struct iio_info pac1921_iio = { 750 + .read_raw = pac1921_read_raw, 751 + .read_avail = pac1921_read_avail, 752 + .write_raw = pac1921_write_raw, 753 + .write_raw_get_fmt = pac1921_write_raw_get_fmt, 754 + .read_label = pac1921_read_label, 755 + .read_event_config = pac1921_read_event_config, 756 + .write_event_config = pac1921_write_event_config, 757 + .read_event_value = pac1921_read_event_value, 758 + }; 759 + 760 + static ssize_t pac1921_read_shunt_resistor(struct iio_dev *indio_dev, 761 + uintptr_t private, 762 + const struct iio_chan_spec *chan, 763 + char *buf) 764 + { 765 + struct pac1921_priv *priv = iio_priv(indio_dev); 766 + int vals[2]; 767 + 768 + if (chan->channel != PAC1921_CHAN_CURRENT) 769 + return -EINVAL; 770 + 771 + guard(mutex)(&priv->lock); 772 + 773 + vals[0] = (int)priv->rshunt_uohm; 774 + vals[1] = MICRO; 775 + 776 + return iio_format_value(buf, IIO_VAL_FRACTIONAL, 1, vals); 777 + } 778 + 779 + static ssize_t pac1921_write_shunt_resistor(struct iio_dev *indio_dev, 780 + uintptr_t private, 781 + const struct iio_chan_spec *chan, 782 + const char *buf, size_t len) 783 + { 784 + struct pac1921_priv *priv = iio_priv(indio_dev); 785 + u64 rshunt_uohm; 786 + int val, val_fract; 787 + int ret; 788 + 789 + if (chan->channel != PAC1921_CHAN_CURRENT) 790 + return -EINVAL; 791 + 792 + ret = iio_str_to_fixpoint(buf, 100000, &val, &val_fract); 793 + if (ret) 794 + return ret; 795 + 796 + rshunt_uohm = (u32)val * MICRO + (u32)val_fract; 797 + if (rshunt_uohm == 0 || rshunt_uohm > INT_MAX) 798 + return -EINVAL; 799 + 800 + guard(mutex)(&priv->lock); 801 + 802 + priv->rshunt_uohm = (u32)rshunt_uohm; 803 + 804 + pac1921_calc_current_scales(priv); 805 + 806 + return len; 807 + } 808 + 809 + /* 810 + * Emit on sysfs the list of available scales contained in scales_tbl 811 + * 812 + * TODO:: this function can be replaced with iio_format_avail_list() if the 813 + * latter will ever be exported. 814 + * 815 + * Must be called with lock held if the scales_tbl can change runtime (e.g. for 816 + * the current scales table) 817 + */ 818 + static ssize_t pac1921_format_scale_avail(const int (*const scales_tbl)[2], 819 + size_t size, char *buf) 820 + { 821 + ssize_t len = 0; 822 + 823 + for (unsigned int i = 0; i < size; i++) { 824 + if (i != 0) { 825 + len += sysfs_emit_at(buf, len, " "); 826 + if (len >= PAGE_SIZE) 827 + return -EFBIG; 828 + } 829 + len += sysfs_emit_at(buf, len, "%d.%09d", scales_tbl[i][0], 830 + scales_tbl[i][1]); 831 + if (len >= PAGE_SIZE) 832 + return -EFBIG; 833 + } 834 + 835 + len += sysfs_emit_at(buf, len, "\n"); 836 + return len; 837 + } 838 + 839 + /* 840 + * Read available scales for a specific channel 841 + * 842 + * NOTE: using extended info insted of iio.read_avail() because access to 843 + * current scales must be locked as they depend on shunt resistor which may 844 + * change runtime. Caller of iio.read_avail() would access the table unlocked 845 + * instead. 846 + */ 847 + static ssize_t pac1921_read_scale_avail(struct iio_dev *indio_dev, 848 + uintptr_t private, 849 + const struct iio_chan_spec *chan, 850 + char *buf) 851 + { 852 + struct pac1921_priv *priv = iio_priv(indio_dev); 853 + const int (*scales_tbl)[2]; 854 + size_t size; 855 + 856 + switch (chan->channel) { 857 + case PAC1921_CHAN_VBUS: 858 + scales_tbl = pac1921_vbus_scales; 859 + size = ARRAY_SIZE(pac1921_vbus_scales); 860 + return pac1921_format_scale_avail(scales_tbl, size, buf); 861 + 862 + case PAC1921_CHAN_VSENSE: 863 + scales_tbl = pac1921_vsense_scales; 864 + size = ARRAY_SIZE(pac1921_vsense_scales); 865 + return pac1921_format_scale_avail(scales_tbl, size, buf); 866 + 867 + case PAC1921_CHAN_CURRENT: { 868 + guard(mutex)(&priv->lock); 869 + scales_tbl = priv->current_scales; 870 + size = ARRAY_SIZE(priv->current_scales); 871 + return pac1921_format_scale_avail(scales_tbl, size, buf); 872 + } 873 + default: 874 + return -EINVAL; 875 + } 876 + } 877 + 878 + #define PAC1921_EXT_INFO_SCALE_AVAIL { \ 879 + .name = "scale_available", \ 880 + .read = pac1921_read_scale_avail, \ 881 + .shared = IIO_SEPARATE, \ 882 + } 883 + 884 + static const struct iio_chan_spec_ext_info pac1921_ext_info_voltage[] = { 885 + PAC1921_EXT_INFO_SCALE_AVAIL, 886 + {} 887 + }; 888 + 889 + static const struct iio_chan_spec_ext_info pac1921_ext_info_current[] = { 890 + PAC1921_EXT_INFO_SCALE_AVAIL, 891 + { 892 + .name = "shunt_resistor", 893 + .read = pac1921_read_shunt_resistor, 894 + .write = pac1921_write_shunt_resistor, 895 + .shared = IIO_SEPARATE, 896 + }, 897 + {} 898 + }; 899 + 900 + static const struct iio_event_spec pac1921_overflow_event[] = { 901 + { 902 + .type = IIO_EV_TYPE_THRESH, 903 + .dir = IIO_EV_DIR_RISING, 904 + .mask_shared_by_all = BIT(IIO_EV_INFO_VALUE), 905 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 906 + }, 907 + }; 908 + 909 + static const struct iio_chan_spec pac1921_channels[] = { 910 + { 911 + .type = IIO_VOLTAGE, 912 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 913 + BIT(IIO_CHAN_INFO_SCALE), 914 + .info_mask_shared_by_all = 915 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 916 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 917 + .info_mask_shared_by_all_available = 918 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 919 + .channel = PAC1921_CHAN_VBUS, 920 + .address = PAC1921_REG_VBUS, 921 + .scan_index = PAC1921_CHAN_VBUS, 922 + .scan_type = { 923 + .sign = 'u', 924 + .realbits = 10, 925 + .storagebits = 16, 926 + .endianness = IIO_CPU 927 + }, 928 + .indexed = 1, 929 + .event_spec = pac1921_overflow_event, 930 + .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 931 + .ext_info = pac1921_ext_info_voltage, 932 + }, 933 + { 934 + .type = IIO_VOLTAGE, 935 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 936 + BIT(IIO_CHAN_INFO_SCALE), 937 + .info_mask_shared_by_all = 938 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 939 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 940 + .info_mask_shared_by_all_available = 941 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 942 + .channel = PAC1921_CHAN_VSENSE, 943 + .address = PAC1921_REG_VSENSE, 944 + .scan_index = PAC1921_CHAN_VSENSE, 945 + .scan_type = { 946 + .sign = 'u', 947 + .realbits = 10, 948 + .storagebits = 16, 949 + .endianness = IIO_CPU 950 + }, 951 + .indexed = 1, 952 + .event_spec = pac1921_overflow_event, 953 + .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 954 + .ext_info = pac1921_ext_info_voltage, 955 + }, 956 + { 957 + .type = IIO_CURRENT, 958 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 959 + BIT(IIO_CHAN_INFO_SCALE), 960 + .info_mask_shared_by_all = 961 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 962 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 963 + .info_mask_shared_by_all_available = 964 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 965 + .channel = PAC1921_CHAN_CURRENT, 966 + .address = PAC1921_REG_VSENSE, 967 + .scan_index = PAC1921_CHAN_CURRENT, 968 + .scan_type = { 969 + .sign = 'u', 970 + .realbits = 10, 971 + .storagebits = 16, 972 + .endianness = IIO_CPU 973 + }, 974 + .event_spec = pac1921_overflow_event, 975 + .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 976 + .ext_info = pac1921_ext_info_current, 977 + }, 978 + { 979 + .type = IIO_POWER, 980 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 981 + BIT(IIO_CHAN_INFO_SCALE), 982 + .info_mask_shared_by_all = 983 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 984 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 985 + .info_mask_shared_by_all_available = 986 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 987 + .channel = PAC1921_CHAN_POWER, 988 + .address = PAC1921_REG_VPOWER, 989 + .scan_index = PAC1921_CHAN_POWER, 990 + .scan_type = { 991 + .sign = 'u', 992 + .realbits = 10, 993 + .storagebits = 16, 994 + .endianness = IIO_CPU 995 + }, 996 + .event_spec = pac1921_overflow_event, 997 + .num_event_specs = ARRAY_SIZE(pac1921_overflow_event), 998 + }, 999 + IIO_CHAN_SOFT_TIMESTAMP(PAC1921_NUM_MEAS_CHANS), 1000 + }; 1001 + 1002 + static irqreturn_t pac1921_trigger_handler(int irq, void *p) 1003 + { 1004 + struct iio_poll_func *pf = p; 1005 + struct iio_dev *idev = pf->indio_dev; 1006 + struct pac1921_priv *priv = iio_priv(idev); 1007 + int ret; 1008 + int bit; 1009 + int ch = 0; 1010 + 1011 + guard(mutex)(&priv->lock); 1012 + 1013 + if (!pac1921_data_ready(priv)) 1014 + goto done; 1015 + 1016 + ret = pac1921_check_push_overflow(idev, pf->timestamp); 1017 + if (ret) 1018 + goto done; 1019 + 1020 + iio_for_each_active_channel(idev, bit) { 1021 + u16 val; 1022 + 1023 + ret = pac1921_read_res(priv, idev->channels[ch].address, &val); 1024 + if (ret) 1025 + goto done; 1026 + 1027 + priv->scan.chan[ch++] = val; 1028 + } 1029 + 1030 + iio_push_to_buffers_with_timestamp(idev, &priv->scan, pf->timestamp); 1031 + 1032 + done: 1033 + iio_trigger_notify_done(idev->trig); 1034 + 1035 + return IRQ_HANDLED; 1036 + } 1037 + 1038 + /* 1039 + * Initialize device by writing initial configuration and putting it into 1040 + * integration state. 1041 + * 1042 + * Must be called with lock held when called after first initialization 1043 + * (e.g. from pm resume) 1044 + */ 1045 + static int pac1921_init(struct pac1921_priv *priv) 1046 + { 1047 + unsigned int val; 1048 + int ret; 1049 + 1050 + /* Enter READ state before configuration */ 1051 + ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1052 + PAC1921_INT_CFG_INTEN, 0); 1053 + if (ret) 1054 + return ret; 1055 + 1056 + /* Configure gains, use 14-bits measurement resolution (HW default) */ 1057 + val = FIELD_PREP(PAC1921_GAIN_DI_GAIN_MASK, priv->di_gain) | 1058 + FIELD_PREP(PAC1921_GAIN_DV_GAIN_MASK, priv->dv_gain); 1059 + ret = regmap_write(priv->regmap, PAC1921_REG_GAIN_CFG, val); 1060 + if (ret) 1061 + return ret; 1062 + 1063 + /* 1064 + * Configure integration: 1065 + * - num of integration samples 1066 + * - filters enabled (HW default) 1067 + * - set READ/INT pin override (RIOV) to control operation mode via 1068 + * register instead of pin 1069 + */ 1070 + val = FIELD_PREP(PAC1921_INT_CFG_SMPL_MASK, priv->n_samples) | 1071 + PAC1921_INT_CFG_VSFEN | PAC1921_INT_CFG_VBFEN | 1072 + PAC1921_INT_CFG_RIOV; 1073 + ret = regmap_write(priv->regmap, PAC1921_REG_INT_CFG, val); 1074 + if (ret) 1075 + return ret; 1076 + 1077 + /* 1078 + * Init control register: 1079 + * - VPower free run integration mode 1080 + * - OUT pin full scale range: 3V (HW detault) 1081 + * - no timeout, no sleep, no sleep override, no recalc (HW defaults) 1082 + */ 1083 + val = FIELD_PREP(PAC1921_CONTROL_MXSL_MASK, 1084 + PAC1921_MXSL_VPOWER_FREE_RUN); 1085 + ret = regmap_write(priv->regmap, PAC1921_REG_CONTROL, val); 1086 + if (ret) 1087 + return ret; 1088 + 1089 + /* Enable integration */ 1090 + ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1091 + PAC1921_INT_CFG_INTEN, PAC1921_INT_CFG_INTEN); 1092 + if (ret) 1093 + return ret; 1094 + 1095 + priv->first_integr_started = true; 1096 + priv->integr_started_time_jiffies = jiffies; 1097 + priv->integr_period_usecs = pac1921_int_periods_usecs[priv->n_samples]; 1098 + 1099 + return 0; 1100 + } 1101 + 1102 + static int pac1921_suspend(struct device *dev) 1103 + { 1104 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 1105 + struct pac1921_priv *priv = iio_priv(indio_dev); 1106 + int ret; 1107 + 1108 + guard(mutex)(&priv->lock); 1109 + 1110 + priv->first_integr_started = false; 1111 + priv->first_integr_done = false; 1112 + 1113 + ret = regmap_update_bits(priv->regmap, PAC1921_REG_INT_CFG, 1114 + PAC1921_INT_CFG_INTEN, 0); 1115 + if (ret) 1116 + return ret; 1117 + 1118 + ret = regmap_update_bits(priv->regmap, PAC1921_REG_CONTROL, 1119 + PAC1921_CONTROL_SLEEP, PAC1921_CONTROL_SLEEP); 1120 + if (ret) 1121 + return ret; 1122 + 1123 + return regulator_disable(priv->vdd); 1124 + 1125 + } 1126 + 1127 + static int pac1921_resume(struct device *dev) 1128 + { 1129 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 1130 + struct pac1921_priv *priv = iio_priv(indio_dev); 1131 + int ret; 1132 + 1133 + guard(mutex)(&priv->lock); 1134 + 1135 + ret = regulator_enable(priv->vdd); 1136 + if (ret) 1137 + return ret; 1138 + 1139 + msleep(PAC1921_POWERUP_TIME_MS); 1140 + 1141 + return pac1921_init(priv); 1142 + } 1143 + 1144 + static DEFINE_SIMPLE_DEV_PM_OPS(pac1921_pm_ops, pac1921_suspend, 1145 + pac1921_resume); 1146 + 1147 + static void pac1921_regulator_disable(void *data) 1148 + { 1149 + struct regulator *regulator = data; 1150 + 1151 + regulator_disable(regulator); 1152 + } 1153 + 1154 + static int pac1921_probe(struct i2c_client *client) 1155 + { 1156 + struct device *dev = &client->dev; 1157 + struct pac1921_priv *priv; 1158 + struct iio_dev *indio_dev; 1159 + int ret; 1160 + 1161 + indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); 1162 + if (!indio_dev) 1163 + return -ENOMEM; 1164 + 1165 + priv = iio_priv(indio_dev); 1166 + priv->client = client; 1167 + i2c_set_clientdata(client, indio_dev); 1168 + 1169 + priv->regmap = devm_regmap_init_i2c(client, &pac1921_regmap_config); 1170 + if (IS_ERR(priv->regmap)) 1171 + return dev_err_probe(dev, (int)PTR_ERR(priv->regmap), 1172 + "Cannot initialize register map\n"); 1173 + 1174 + devm_mutex_init(dev, &priv->lock); 1175 + 1176 + priv->dv_gain = PAC1921_DEFAULT_DV_GAIN; 1177 + priv->di_gain = PAC1921_DEFAULT_DI_GAIN; 1178 + priv->n_samples = PAC1921_DEFAULT_NUM_SAMPLES; 1179 + 1180 + ret = device_property_read_u32(dev, "shunt-resistor-micro-ohms", 1181 + &priv->rshunt_uohm); 1182 + if (ret) 1183 + return dev_err_probe(dev, ret, 1184 + "Cannot read shunt resistor property\n"); 1185 + if (priv->rshunt_uohm == 0 || priv->rshunt_uohm > INT_MAX) 1186 + return dev_err_probe(dev, -EINVAL, 1187 + "Invalid shunt resistor: %u\n", 1188 + priv->rshunt_uohm); 1189 + 1190 + pac1921_calc_current_scales(priv); 1191 + 1192 + priv->vdd = devm_regulator_get(dev, "vdd"); 1193 + if (IS_ERR(priv->vdd)) 1194 + return dev_err_probe(dev, (int)PTR_ERR(priv->vdd), 1195 + "Cannot get vdd regulator\n"); 1196 + 1197 + ret = regulator_enable(priv->vdd); 1198 + if (ret) 1199 + return dev_err_probe(dev, ret, "Cannot enable vdd regulator\n"); 1200 + 1201 + ret = devm_add_action_or_reset(dev, pac1921_regulator_disable, 1202 + priv->vdd); 1203 + if (ret) 1204 + return dev_err_probe(dev, ret, 1205 + "Cannot add action for vdd regulator disposal\n"); 1206 + 1207 + msleep(PAC1921_POWERUP_TIME_MS); 1208 + 1209 + ret = pac1921_init(priv); 1210 + if (ret) 1211 + return dev_err_probe(dev, ret, "Cannot initialize device\n"); 1212 + 1213 + priv->iio_info = pac1921_iio; 1214 + 1215 + indio_dev->name = "pac1921"; 1216 + indio_dev->info = &priv->iio_info; 1217 + indio_dev->modes = INDIO_DIRECT_MODE; 1218 + indio_dev->channels = pac1921_channels; 1219 + indio_dev->num_channels = ARRAY_SIZE(pac1921_channels); 1220 + 1221 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 1222 + &iio_pollfunc_store_time, 1223 + &pac1921_trigger_handler, NULL); 1224 + if (ret) 1225 + return dev_err_probe(dev, ret, 1226 + "Cannot setup IIO triggered buffer\n"); 1227 + 1228 + ret = devm_iio_device_register(dev, indio_dev); 1229 + if (ret) 1230 + return dev_err_probe(dev, ret, "Cannot register IIO device\n"); 1231 + 1232 + return 0; 1233 + } 1234 + 1235 + static const struct i2c_device_id pac1921_id[] = { 1236 + { .name = "pac1921", 0 }, 1237 + {} 1238 + }; 1239 + MODULE_DEVICE_TABLE(i2c, pac1921_id); 1240 + 1241 + static const struct of_device_id pac1921_of_match[] = { 1242 + { .compatible = "microchip,pac1921" }, 1243 + {} 1244 + }; 1245 + MODULE_DEVICE_TABLE(of, pac1921_of_match); 1246 + 1247 + static struct i2c_driver pac1921_driver = { 1248 + .driver = { 1249 + .name = "pac1921", 1250 + .pm = pm_sleep_ptr(&pac1921_pm_ops), 1251 + .of_match_table = pac1921_of_match, 1252 + }, 1253 + .probe = pac1921_probe, 1254 + .id_table = pac1921_id, 1255 + }; 1256 + 1257 + module_i2c_driver(pac1921_driver); 1258 + 1259 + MODULE_AUTHOR("Matteo Martelli <matteomartelli3@gmail.com>"); 1260 + MODULE_DESCRIPTION("IIO driver for PAC1921 High-Side Power/Current Monitor"); 1261 + MODULE_LICENSE("GPL");
+1 -1
drivers/iio/adc/rockchip_saradc.c
··· 370 370 371 371 mutex_lock(&info->lock); 372 372 373 - for_each_set_bit(i, i_dev->active_scan_mask, i_dev->masklength) { 373 + iio_for_each_active_channel(i_dev, i) { 374 374 const struct iio_chan_spec *chan = &i_dev->channels[i]; 375 375 376 376 ret = rockchip_saradc_conversion(info, chan);
+1 -1
drivers/iio/adc/rtq6056.c
··· 643 643 644 644 pm_runtime_get_sync(dev); 645 645 646 - for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) { 646 + iio_for_each_active_channel(indio_dev, bit) { 647 647 unsigned int addr = rtq6056_channels[bit].address; 648 648 649 649 ret = regmap_read(priv->regmap, addr, &raw);
+96 -1
drivers/iio/adc/sd_adc_modulator.c
··· 6 6 * Author: Arnaud Pouliquen <arnaud.pouliquen@st.com>. 7 7 */ 8 8 9 + #include <linux/iio/backend.h> 9 10 #include <linux/iio/iio.h> 10 11 #include <linux/iio/triggered_buffer.h> 11 12 #include <linux/module.h> 12 13 #include <linux/mod_devicetable.h> 13 14 #include <linux/platform_device.h> 15 + #include <linux/property.h> 16 + #include <linux/regulator/consumer.h> 14 17 15 18 static const struct iio_info iio_sd_mod_iio_info; 16 19 ··· 27 24 }, 28 25 }; 29 26 30 - static int iio_sd_mod_probe(struct platform_device *pdev) 27 + struct iio_sd_backend_priv { 28 + struct regulator *vref; 29 + int vref_mv; 30 + }; 31 + 32 + static int iio_sd_mod_enable(struct iio_backend *backend) 33 + { 34 + struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend); 35 + 36 + if (priv->vref) 37 + return regulator_enable(priv->vref); 38 + 39 + return 0; 40 + }; 41 + 42 + static void iio_sd_mod_disable(struct iio_backend *backend) 43 + { 44 + struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend); 45 + 46 + if (priv->vref) 47 + regulator_disable(priv->vref); 48 + }; 49 + 50 + static int iio_sd_mod_read(struct iio_backend *backend, struct iio_chan_spec const *chan, int *val, 51 + int *val2, long mask) 52 + { 53 + struct iio_sd_backend_priv *priv = iio_backend_get_priv(backend); 54 + 55 + switch (mask) { 56 + case IIO_CHAN_INFO_SCALE: 57 + *val = priv->vref_mv; 58 + return IIO_VAL_INT; 59 + 60 + case IIO_CHAN_INFO_OFFSET: 61 + *val = 0; 62 + return IIO_VAL_INT; 63 + } 64 + 65 + return -EOPNOTSUPP; 66 + }; 67 + 68 + static const struct iio_backend_ops sd_backend_ops = { 69 + .enable = iio_sd_mod_enable, 70 + .disable = iio_sd_mod_disable, 71 + .read_raw = iio_sd_mod_read, 72 + }; 73 + 74 + static const struct iio_backend_info sd_backend_info = { 75 + .name = "sd-modulator", 76 + .ops = &sd_backend_ops, 77 + }; 78 + 79 + static int iio_sd_mod_register(struct platform_device *pdev) 31 80 { 32 81 struct device *dev = &pdev->dev; 33 82 struct iio_dev *iio; ··· 99 44 100 45 return devm_iio_device_register(&pdev->dev, iio); 101 46 } 47 + 48 + static int iio_sd_mod_probe(struct platform_device *pdev) 49 + { 50 + struct device *dev = &pdev->dev; 51 + struct regulator *vref; 52 + struct iio_sd_backend_priv *priv; 53 + int ret; 54 + 55 + /* If sd modulator is not defined as an IIO backend device, fallback to legacy */ 56 + if (!device_property_present(dev, "#io-backend-cells")) 57 + return iio_sd_mod_register(pdev); 58 + 59 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 60 + if (!priv) 61 + return -ENOMEM; 62 + 63 + /* 64 + * Get regulator reference if any, but don't enable regulator right now. 65 + * Rely on enable and disable callbacks to manage regulator power. 66 + */ 67 + vref = devm_regulator_get_optional(dev, "vref"); 68 + if (IS_ERR(vref)) { 69 + if (PTR_ERR(vref) != -ENODEV) 70 + return dev_err_probe(dev, PTR_ERR(vref), "Failed to get vref\n"); 71 + } else { 72 + /* 73 + * Retrieve voltage right now, as regulator_get_voltage() provides it whatever 74 + * the state of the regulator. 75 + */ 76 + ret = regulator_get_voltage(vref); 77 + if (ret < 0) 78 + return ret; 79 + 80 + priv->vref = vref; 81 + priv->vref_mv = ret / 1000; 82 + } 83 + 84 + return devm_iio_backend_register(&pdev->dev, &sd_backend_info, priv); 85 + }; 102 86 103 87 static const struct of_device_id sd_adc_of_match[] = { 104 88 { .compatible = "sd-modulator" }, ··· 159 65 MODULE_DESCRIPTION("Basic sigma delta modulator"); 160 66 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>"); 161 67 MODULE_LICENSE("GPL v2"); 68 + MODULE_IMPORT_NS(IIO_BACKEND);
+2 -2
drivers/iio/adc/stm32-adc.c
··· 1261 1261 stm32_adc_writel(adc, adc->cfg->regs->smpr[0], adc->smpr_val[0]); 1262 1262 stm32_adc_writel(adc, adc->cfg->regs->smpr[1], adc->smpr_val[1]); 1263 1263 1264 - for_each_set_bit(bit, scan_mask, indio_dev->masklength) { 1264 + for_each_set_bit(bit, scan_mask, iio_get_masklength(indio_dev)) { 1265 1265 chan = indio_dev->channels + bit; 1266 1266 /* 1267 1267 * Assign one channel per SQ entry in regular ··· 1619 1619 if (ret < 0) 1620 1620 return ret; 1621 1621 1622 - adc->num_conv = bitmap_weight(scan_mask, indio_dev->masklength); 1622 + adc->num_conv = bitmap_weight(scan_mask, iio_get_masklength(indio_dev)); 1623 1623 1624 1624 ret = stm32_adc_conf_scan_seq(indio_dev, scan_mask); 1625 1625 pm_runtime_mark_last_busy(dev);
+259 -38
drivers/iio/adc/stm32-dfsdm-adc.c
··· 9 9 #include <linux/dmaengine.h> 10 10 #include <linux/dma-mapping.h> 11 11 #include <linux/iio/adc/stm32-dfsdm-adc.h> 12 + #include <linux/iio/backend.h> 12 13 #include <linux/iio/buffer.h> 13 14 #include <linux/iio/hw-consumer.h> 14 15 #include <linux/iio/sysfs.h> ··· 79 78 /* ADC specific */ 80 79 unsigned int oversamp; 81 80 struct iio_hw_consumer *hwc; 81 + struct iio_backend **backend; 82 82 struct completion completion; 83 83 u32 *buffer; 84 84 ··· 668 666 return 0; 669 667 } 670 668 669 + static int stm32_dfsdm_generic_channel_parse_of(struct stm32_dfsdm *dfsdm, 670 + struct iio_dev *indio_dev, 671 + struct iio_chan_spec *ch, 672 + struct fwnode_handle *node) 673 + { 674 + struct stm32_dfsdm_channel *df_ch; 675 + struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 676 + struct iio_backend *backend; 677 + const char *of_str; 678 + int ret, val; 679 + 680 + ret = fwnode_property_read_u32(node, "reg", &ch->channel); 681 + if (ret < 0) { 682 + dev_err(&indio_dev->dev, "Missing channel index %d\n", ret); 683 + return ret; 684 + } 685 + 686 + if (ch->channel >= dfsdm->num_chs) { 687 + dev_err(&indio_dev->dev, " Error bad channel number %d (max = %d)\n", 688 + ch->channel, dfsdm->num_chs); 689 + return -EINVAL; 690 + } 691 + 692 + ret = fwnode_property_read_string(node, "label", &ch->datasheet_name); 693 + if (ret < 0) { 694 + dev_err(&indio_dev->dev, 695 + " Error parsing 'label' for idx %d\n", ch->channel); 696 + return ret; 697 + } 698 + 699 + df_ch = &dfsdm->ch_list[ch->channel]; 700 + df_ch->id = ch->channel; 701 + 702 + ret = fwnode_property_read_string(node, "st,adc-channel-type", &of_str); 703 + if (!ret) { 704 + val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_type); 705 + if (val < 0) 706 + return val; 707 + } else { 708 + val = 0; 709 + } 710 + df_ch->type = val; 711 + 712 + ret = fwnode_property_read_string(node, "st,adc-channel-clk-src", &of_str); 713 + if (!ret) { 714 + val = stm32_dfsdm_str2val(of_str, stm32_dfsdm_chan_src); 715 + if (val < 0) 716 + return val; 717 + } else { 718 + val = 0; 719 + } 720 + df_ch->src = val; 721 + 722 + ret = fwnode_property_read_u32(node, "st,adc-alt-channel", &df_ch->alt_si); 723 + if (ret != -EINVAL) 724 + df_ch->alt_si = 0; 725 + 726 + if (adc->dev_data->type == DFSDM_IIO) { 727 + backend = devm_iio_backend_fwnode_get(&indio_dev->dev, NULL, node); 728 + if (IS_ERR(backend)) 729 + return dev_err_probe(&indio_dev->dev, PTR_ERR(backend), 730 + "Failed to get backend\n"); 731 + adc->backend[ch->scan_index] = backend; 732 + } 733 + 734 + return 0; 735 + } 736 + 671 737 static ssize_t dfsdm_adc_audio_get_spiclk(struct iio_dev *indio_dev, 672 738 uintptr_t priv, 673 739 const struct iio_chan_spec *chan, ··· 1057 987 { 1058 988 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1059 989 1060 - adc->nconv = bitmap_weight(scan_mask, indio_dev->masklength); 990 + adc->nconv = bitmap_weight(scan_mask, iio_get_masklength(indio_dev)); 1061 991 adc->smask = *scan_mask; 1062 992 1063 993 dev_dbg(&indio_dev->dev, "nconv=%d mask=%lx\n", adc->nconv, *scan_mask); ··· 1068 998 static int stm32_dfsdm_postenable(struct iio_dev *indio_dev) 1069 999 { 1070 1000 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1001 + int i = 0; 1071 1002 int ret; 1072 1003 1073 1004 /* Reset adc buffer index */ ··· 1078 1007 ret = iio_hw_consumer_enable(adc->hwc); 1079 1008 if (ret < 0) 1080 1009 return ret; 1010 + } 1011 + 1012 + if (adc->backend) { 1013 + while (adc->backend[i]) { 1014 + ret = iio_backend_enable(adc->backend[i]); 1015 + if (ret < 0) 1016 + return ret; 1017 + i++; 1018 + } 1081 1019 } 1082 1020 1083 1021 ret = stm32_dfsdm_start_dfsdm(adc->dfsdm); ··· 1121 1041 static int stm32_dfsdm_predisable(struct iio_dev *indio_dev) 1122 1042 { 1123 1043 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1044 + int i = 0; 1124 1045 1125 1046 stm32_dfsdm_stop_conv(indio_dev); 1126 1047 1127 1048 stm32_dfsdm_adc_dma_stop(indio_dev); 1128 1049 1129 1050 stm32_dfsdm_stop_dfsdm(adc->dfsdm); 1051 + 1052 + if (adc->backend) { 1053 + while (adc->backend[i]) { 1054 + iio_backend_disable(adc->backend[i]); 1055 + i++; 1056 + } 1057 + } 1130 1058 1131 1059 if (adc->hwc) 1132 1060 iio_hw_consumer_disable(adc->hwc); ··· 1308 1220 int *val2, long mask) 1309 1221 { 1310 1222 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1223 + 1224 + struct stm32_dfsdm_filter *fl = &adc->dfsdm->fl_list[adc->fl_id]; 1225 + struct stm32_dfsdm_filter_osr *flo = &fl->flo[fl->fast]; 1226 + u32 max = flo->max << (flo->lshift - chan->scan_type.shift); 1227 + int idx = chan->scan_index; 1311 1228 int ret; 1229 + 1230 + if (flo->lshift < chan->scan_type.shift) 1231 + max = flo->max >> (chan->scan_type.shift - flo->lshift); 1312 1232 1313 1233 switch (mask) { 1314 1234 case IIO_CHAN_INFO_RAW: 1315 1235 ret = iio_device_claim_direct_mode(indio_dev); 1316 1236 if (ret) 1317 1237 return ret; 1318 - ret = iio_hw_consumer_enable(adc->hwc); 1238 + if (adc->hwc) 1239 + ret = iio_hw_consumer_enable(adc->hwc); 1240 + if (adc->backend) 1241 + ret = iio_backend_enable(adc->backend[idx]); 1319 1242 if (ret < 0) { 1320 1243 dev_err(&indio_dev->dev, 1321 1244 "%s: IIO enable failed (channel %d)\n", ··· 1335 1236 return ret; 1336 1237 } 1337 1238 ret = stm32_dfsdm_single_conv(indio_dev, chan, val); 1338 - iio_hw_consumer_disable(adc->hwc); 1239 + if (adc->hwc) 1240 + iio_hw_consumer_disable(adc->hwc); 1241 + if (adc->backend) 1242 + iio_backend_disable(adc->backend[idx]); 1339 1243 if (ret < 0) { 1340 1244 dev_err(&indio_dev->dev, 1341 1245 "%s: Conversion failed (channel %d)\n", ··· 1357 1255 case IIO_CHAN_INFO_SAMP_FREQ: 1358 1256 *val = adc->sample_freq; 1359 1257 1258 + return IIO_VAL_INT; 1259 + 1260 + case IIO_CHAN_INFO_SCALE: 1261 + /* 1262 + * Scale is expressed in mV. 1263 + * When fast mode is disabled, actual resolution may be lower 1264 + * than 2^n, where n = realbits - 1. 1265 + * This leads to underestimating the input voltage. 1266 + * To compensate this deviation, the voltage reference can be 1267 + * corrected with a factor = realbits resolution / actual max 1268 + */ 1269 + if (adc->backend) { 1270 + ret = iio_backend_read_scale(adc->backend[idx], chan, val, NULL); 1271 + if (ret < 0) 1272 + return ret; 1273 + 1274 + *val = div_u64((u64)*val * (u64)BIT(DFSDM_DATA_RES - 1), max); 1275 + *val2 = chan->scan_type.realbits; 1276 + if (chan->differential) 1277 + *val *= 2; 1278 + } 1279 + return IIO_VAL_FRACTIONAL_LOG2; 1280 + 1281 + case IIO_CHAN_INFO_OFFSET: 1282 + /* 1283 + * DFSDM output data are in the range [-2^n, 2^n], 1284 + * with n = realbits - 1. 1285 + * - Differential modulator: 1286 + * Offset correspond to SD modulator offset. 1287 + * - Single ended modulator: 1288 + * Input is in [0V, Vref] range, 1289 + * where 0V corresponds to -2^n, and Vref to 2^n. 1290 + * Add 2^n to offset. (i.e. middle of input range) 1291 + * offset = offset(sd) * vref / res(sd) * max / vref. 1292 + */ 1293 + if (adc->backend) { 1294 + ret = iio_backend_read_offset(adc->backend[idx], chan, val, NULL); 1295 + if (ret < 0) 1296 + return ret; 1297 + 1298 + *val = div_u64((u64)max * *val, BIT(*val2 - 1)); 1299 + if (!chan->differential) 1300 + *val += max; 1301 + } 1360 1302 return IIO_VAL_INT; 1361 1303 } 1362 1304 ··· 1508 1362 return 0; 1509 1363 } 1510 1364 1511 - static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, 1512 - struct iio_chan_spec *ch) 1365 + static int stm32_dfsdm_adc_chan_init_one(struct iio_dev *indio_dev, struct iio_chan_spec *ch, 1366 + struct fwnode_handle *child) 1513 1367 { 1514 1368 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1515 1369 int ret; 1516 1370 1517 - ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); 1371 + if (child) 1372 + ret = stm32_dfsdm_generic_channel_parse_of(adc->dfsdm, indio_dev, ch, child); 1373 + else /* Legacy binding */ 1374 + ret = stm32_dfsdm_channel_parse_of(adc->dfsdm, indio_dev, ch); 1518 1375 if (ret < 0) 1519 - return ret; 1376 + return dev_err_probe(&indio_dev->dev, ret, "Failed to parse channel\n"); 1520 1377 1521 1378 ch->type = IIO_VOLTAGE; 1522 1379 ch->indexed = 1; ··· 1528 1379 * IIO_CHAN_INFO_RAW: used to compute regular conversion 1529 1380 * IIO_CHAN_INFO_OVERSAMPLING_RATIO: used to set oversampling 1530 1381 */ 1531 - ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 1382 + if (child) { 1383 + ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 1384 + BIT(IIO_CHAN_INFO_SCALE) | 1385 + BIT(IIO_CHAN_INFO_OFFSET); 1386 + } else { 1387 + /* Legacy. Scaling not supported */ 1388 + ch->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); 1389 + } 1390 + 1532 1391 ch->info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) | 1533 1392 BIT(IIO_CHAN_INFO_SAMP_FREQ); 1534 1393 1535 1394 if (adc->dev_data->type == DFSDM_AUDIO) { 1536 1395 ch->ext_info = dfsdm_adc_audio_ext_info; 1396 + ch->scan_index = 0; 1537 1397 } else { 1538 1398 ch->scan_type.shift = 8; 1539 1399 } ··· 1554 1396 &adc->dfsdm->ch_list[ch->channel]); 1555 1397 } 1556 1398 1399 + static int stm32_dfsdm_chan_init(struct iio_dev *indio_dev, struct iio_chan_spec *channels) 1400 + { 1401 + int num_ch = indio_dev->num_channels; 1402 + int chan_idx = 0; 1403 + int ret; 1404 + 1405 + for (chan_idx = 0; chan_idx < num_ch; chan_idx++) { 1406 + channels[chan_idx].scan_index = chan_idx; 1407 + ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &channels[chan_idx], NULL); 1408 + if (ret < 0) 1409 + return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); 1410 + } 1411 + 1412 + return 0; 1413 + } 1414 + 1415 + static int stm32_dfsdm_generic_chan_init(struct iio_dev *indio_dev, struct iio_chan_spec *channels) 1416 + { 1417 + int chan_idx = 0, ret; 1418 + 1419 + device_for_each_child_node_scoped(&indio_dev->dev, child) { 1420 + /* Skip DAI node in DFSDM audio nodes */ 1421 + if (fwnode_property_present(child, "compatible")) 1422 + continue; 1423 + 1424 + channels[chan_idx].scan_index = chan_idx; 1425 + ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &channels[chan_idx], child); 1426 + if (ret < 0) 1427 + return dev_err_probe(&indio_dev->dev, ret, "Channels init failed\n"); 1428 + 1429 + chan_idx++; 1430 + } 1431 + 1432 + return chan_idx; 1433 + } 1434 + 1557 1435 static int stm32_dfsdm_audio_init(struct device *dev, struct iio_dev *indio_dev) 1558 1436 { 1559 1437 struct iio_chan_spec *ch; 1560 1438 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1561 1439 struct stm32_dfsdm_channel *d_ch; 1562 - int ret; 1440 + bool legacy = false; 1441 + int num_ch, ret; 1442 + 1443 + /* If st,adc-channels is defined legacy binding is used. Else assume generic binding. */ 1444 + num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); 1445 + if (num_ch == 1) 1446 + legacy = true; 1563 1447 1564 1448 ch = devm_kzalloc(&indio_dev->dev, sizeof(*ch), GFP_KERNEL); 1565 1449 if (!ch) 1566 1450 return -ENOMEM; 1567 1451 1568 - ch->scan_index = 0; 1452 + indio_dev->num_channels = 1; 1453 + indio_dev->channels = ch; 1569 1454 1570 - ret = stm32_dfsdm_adc_chan_init_one(indio_dev, ch); 1455 + if (legacy) 1456 + ret = stm32_dfsdm_chan_init(indio_dev, ch); 1457 + else 1458 + ret = stm32_dfsdm_generic_chan_init(indio_dev, ch); 1459 + 1571 1460 if (ret < 0) { 1572 1461 dev_err(&indio_dev->dev, "Channels init failed\n"); 1573 1462 return ret; ··· 1625 1420 if (d_ch->src != DFSDM_CHANNEL_SPI_CLOCK_EXTERNAL) 1626 1421 adc->spi_freq = adc->dfsdm->spi_master_freq; 1627 1422 1628 - indio_dev->num_channels = 1; 1629 - indio_dev->channels = ch; 1630 - 1631 1423 return stm32_dfsdm_dma_request(dev, indio_dev); 1632 1424 } 1633 1425 ··· 1632 1430 { 1633 1431 struct iio_chan_spec *ch; 1634 1432 struct stm32_dfsdm_adc *adc = iio_priv(indio_dev); 1635 - int num_ch; 1636 - int ret, chan_idx; 1433 + int num_ch, ret; 1434 + bool legacy = false; 1637 1435 1638 1436 adc->oversamp = DFSDM_DEFAULT_OVERSAMPLING; 1639 1437 ret = stm32_dfsdm_compute_all_osrs(indio_dev, adc->oversamp); 1640 1438 if (ret < 0) 1641 1439 return ret; 1642 1440 1643 - num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, 1644 - "st,adc-channels"); 1645 - if (num_ch < 0 || num_ch > adc->dfsdm->num_chs) { 1646 - dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); 1647 - return num_ch < 0 ? num_ch : -EINVAL; 1441 + num_ch = device_get_child_node_count(&indio_dev->dev); 1442 + if (!num_ch) { 1443 + /* No channels nodes found. Assume legacy binding */ 1444 + num_ch = of_property_count_u32_elems(indio_dev->dev.of_node, "st,adc-channels"); 1445 + if (num_ch < 0) { 1446 + dev_err(&indio_dev->dev, "Bad st,adc-channels\n"); 1447 + return num_ch; 1448 + } 1449 + 1450 + legacy = true; 1648 1451 } 1649 1452 1650 - /* Bind to SD modulator IIO device */ 1651 - adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); 1652 - if (IS_ERR(adc->hwc)) 1653 - return -EPROBE_DEFER; 1453 + if (num_ch > adc->dfsdm->num_chs) { 1454 + dev_err(&indio_dev->dev, "Number of channel [%d] exceeds [%d]\n", 1455 + num_ch, adc->dfsdm->num_chs); 1456 + return -EINVAL; 1457 + } 1458 + indio_dev->num_channels = num_ch; 1654 1459 1655 - ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), 1656 - GFP_KERNEL); 1460 + if (legacy) { 1461 + /* Bind to SD modulator IIO device. */ 1462 + adc->hwc = devm_iio_hw_consumer_alloc(&indio_dev->dev); 1463 + if (IS_ERR(adc->hwc)) 1464 + return dev_err_probe(&indio_dev->dev, -EPROBE_DEFER, 1465 + "waiting for SD modulator\n"); 1466 + } else { 1467 + /* Generic binding. SD modulator IIO device not used. Use SD modulator backend. */ 1468 + adc->hwc = NULL; 1469 + 1470 + adc->backend = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*adc->backend), 1471 + GFP_KERNEL); 1472 + if (!adc->backend) 1473 + return -ENOMEM; 1474 + } 1475 + 1476 + ch = devm_kcalloc(&indio_dev->dev, num_ch, sizeof(*ch), GFP_KERNEL); 1657 1477 if (!ch) 1658 1478 return -ENOMEM; 1659 - 1660 - for (chan_idx = 0; chan_idx < num_ch; chan_idx++) { 1661 - ch[chan_idx].scan_index = chan_idx; 1662 - ret = stm32_dfsdm_adc_chan_init_one(indio_dev, &ch[chan_idx]); 1663 - if (ret < 0) { 1664 - dev_err(&indio_dev->dev, "Channels init failed\n"); 1665 - return ret; 1666 - } 1667 - } 1668 - 1669 - indio_dev->num_channels = num_ch; 1670 1479 indio_dev->channels = ch; 1480 + 1481 + if (legacy) 1482 + ret = stm32_dfsdm_chan_init(indio_dev, ch); 1483 + else 1484 + ret = stm32_dfsdm_generic_chan_init(indio_dev, ch); 1485 + if (ret < 0) 1486 + return ret; 1671 1487 1672 1488 init_completion(&adc->completion); 1673 1489 ··· 1897 1677 MODULE_DESCRIPTION("STM32 sigma delta ADC"); 1898 1678 MODULE_AUTHOR("Arnaud Pouliquen <arnaud.pouliquen@st.com>"); 1899 1679 MODULE_LICENSE("GPL v2"); 1680 + MODULE_IMPORT_NS(IIO_BACKEND);
+1 -2
drivers/iio/adc/ti-adc0832.c
··· 211 211 212 212 mutex_lock(&adc->lock); 213 213 214 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 215 - indio_dev->masklength) { 214 + iio_for_each_active_channel(indio_dev, scan_index) { 216 215 const struct iio_chan_spec *scan_chan = 217 216 &indio_dev->channels[scan_index]; 218 217 int ret = adc0832_adc_conversion(adc, scan_chan->channel,
+1 -2
drivers/iio/adc/ti-adc084s021.c
··· 166 166 int scan_index; 167 167 int i = 0; 168 168 169 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 170 - indio_dev->masklength) { 169 + iio_for_each_active_channel(indio_dev, scan_index) { 171 170 const struct iio_chan_spec *channel = 172 171 &indio_dev->channels[scan_index]; 173 172 adc->tx_buf[i++] = channel->channel << 3;
+1 -2
drivers/iio/adc/ti-adc12138.c
··· 344 344 345 345 mutex_lock(&adc->lock); 346 346 347 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 348 - indio_dev->masklength) { 347 + iio_for_each_active_channel(indio_dev, scan_index) { 349 348 const struct iio_chan_spec *scan_chan = 350 349 &indio_dev->channels[scan_index]; 351 350
+1 -1
drivers/iio/adc/ti-ads1015.c
··· 456 456 457 457 mutex_lock(&data->lock); 458 458 chan = find_first_bit(indio_dev->active_scan_mask, 459 - indio_dev->masklength); 459 + iio_get_masklength(indio_dev)); 460 460 ret = ads1015_get_adc_result(data, chan, &res); 461 461 if (ret < 0) { 462 462 mutex_unlock(&data->lock);
+2 -2
drivers/iio/adc/ti-ads1119.c
··· 435 435 int ret; 436 436 437 437 index = find_first_bit(indio_dev->active_scan_mask, 438 - indio_dev->masklength); 438 + iio_get_masklength(indio_dev)); 439 439 440 440 ret = ads1119_set_conv_mode(st, true); 441 441 if (ret) ··· 508 508 509 509 if (!iio_trigger_using_own(indio_dev)) { 510 510 index = find_first_bit(indio_dev->active_scan_mask, 511 - indio_dev->masklength); 511 + iio_get_masklength(indio_dev)); 512 512 513 513 ret = ads1119_poll_data_ready(st, &indio_dev->channels[index]); 514 514 if (ret) {
+1 -2
drivers/iio/adc/ti-ads124s08.c
··· 279 279 int scan_index, j = 0; 280 280 int ret; 281 281 282 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 283 - indio_dev->masklength) { 282 + iio_for_each_active_channel(indio_dev, scan_index) { 284 283 ret = ads124s_write_reg(indio_dev, ADS124S08_INPUT_MUX, 285 284 scan_index); 286 285 if (ret)
+1 -2
drivers/iio/adc/ti-ads1298.c
··· 502 502 } 503 503 504 504 /* Demux the channel data into our bounce buffer */ 505 - for_each_set_bit(scan_index, indio_dev->active_scan_mask, 506 - indio_dev->masklength) { 505 + iio_for_each_active_channel(indio_dev, scan_index) { 507 506 const struct iio_chan_spec *scan_chan = 508 507 &indio_dev->channels[scan_index]; 509 508 const u8 *data = priv->rx_buffer + scan_chan->address;
+1 -1
drivers/iio/adc/ti-ads131e08.c
··· 637 637 if (ret) 638 638 goto out; 639 639 640 - for_each_set_bit(chn, indio_dev->active_scan_mask, indio_dev->masklength) { 640 + iio_for_each_active_channel(indio_dev, chn) { 641 641 src = st->rx_buf + ADS131E08_NUM_STATUS_BYTES + chn * num_bytes; 642 642 dest = st->tmp_buf.data + i * ADS131E08_NUM_STORAGE_BYTES; 643 643
+1 -3
drivers/iio/adc/ti-ads8688.c
··· 384 384 u16 buffer[ADS8688_MAX_CHANNELS + sizeof(s64)/sizeof(u16)] __aligned(8); 385 385 int i, j = 0; 386 386 387 - for (i = 0; i < indio_dev->masklength; i++) { 388 - if (!test_bit(i, indio_dev->active_scan_mask)) 389 - continue; 387 + iio_for_each_active_channel(indio_dev, i) { 390 388 buffer[j] = ads8688_read(indio_dev, i); 391 389 j++; 392 390 }
+19 -64
drivers/iio/adc/ti-tsc2046.c
··· 6 6 */ 7 7 8 8 #include <linux/bitfield.h> 9 + #include <linux/cleanup.h> 9 10 #include <linux/delay.h> 10 11 #include <linux/module.h> 11 12 #include <linux/regulator/consumer.h> ··· 142 141 struct tsc2046_adc_priv { 143 142 struct spi_device *spi; 144 143 const struct tsc2046_adc_dcfg *dcfg; 145 - struct regulator *vref_reg; 144 + bool internal_vref; 146 145 147 146 struct iio_trigger *trig; 148 147 struct hrtimer trig_timer; ··· 258 257 case TI_TSC2046_ADDR_VBAT: 259 258 case TI_TSC2046_ADDR_TEMP0: 260 259 pd |= TI_TSC2046_SER; 261 - if (!priv->vref_reg) 260 + if (priv->internal_vref) 262 261 pd |= TI_TSC2046_PD1_VREF_ON; 263 262 } 264 263 ··· 274 273 u32 *effective_speed_hz) 275 274 { 276 275 struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx]; 277 - struct tsc2046_adc_atom *rx_buf, *tx_buf; 278 276 unsigned int val, val_normalized = 0; 279 277 int ret, i, count_skip = 0, max_count; 280 278 struct spi_transfer xfer; ··· 287 287 max_count = 1; 288 288 } 289 289 290 - if (sizeof(*tx_buf) * max_count > PAGE_SIZE) 290 + if (sizeof(struct tsc2046_adc_atom) * max_count > PAGE_SIZE) 291 291 return -ENOSPC; 292 292 293 - tx_buf = kcalloc(max_count, sizeof(*tx_buf), GFP_KERNEL); 293 + struct tsc2046_adc_atom *tx_buf __free(kfree) = kcalloc(max_count, 294 + sizeof(*tx_buf), 295 + GFP_KERNEL); 294 296 if (!tx_buf) 295 297 return -ENOMEM; 296 298 297 - rx_buf = kcalloc(max_count, sizeof(*rx_buf), GFP_KERNEL); 298 - if (!rx_buf) { 299 - ret = -ENOMEM; 300 - goto free_tx; 301 - } 299 + struct tsc2046_adc_atom *rx_buf __free(kfree) = kcalloc(max_count, 300 + sizeof(*rx_buf), 301 + GFP_KERNEL); 302 + if (!rx_buf) 303 + return -ENOMEM; 302 304 303 305 /* 304 306 * Do not enable automatic power down on working samples. Otherwise the ··· 328 326 if (ret) { 329 327 dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n", 330 328 ERR_PTR(ret)); 331 - goto free_bufs; 329 + return ret; 332 330 } 333 331 334 332 if (effective_speed_hz) ··· 339 337 val_normalized += val; 340 338 } 341 339 342 - ret = DIV_ROUND_UP(val_normalized, max_count - count_skip); 343 - 344 - free_bufs: 345 - kfree(rx_buf); 346 - free_tx: 347 - kfree(tx_buf); 348 - 349 - return ret; 340 + return DIV_ROUND_UP(val_normalized, max_count - count_skip); 350 341 } 351 342 352 343 static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv, ··· 741 746 } 742 747 } 743 748 744 - static void tsc2046_adc_regulator_disable(void *data) 745 - { 746 - struct tsc2046_adc_priv *priv = data; 747 - 748 - regulator_disable(priv->vref_reg); 749 - } 750 - 751 - static int tsc2046_adc_configure_regulator(struct tsc2046_adc_priv *priv) 752 - { 753 - struct device *dev = &priv->spi->dev; 754 - int ret; 755 - 756 - priv->vref_reg = devm_regulator_get_optional(dev, "vref"); 757 - if (IS_ERR(priv->vref_reg)) { 758 - /* If regulator exists but can't be get, return an error */ 759 - if (PTR_ERR(priv->vref_reg) != -ENODEV) 760 - return PTR_ERR(priv->vref_reg); 761 - priv->vref_reg = NULL; 762 - } 763 - if (!priv->vref_reg) { 764 - /* Use internal reference */ 765 - priv->vref_mv = TI_TSC2046_INT_VREF; 766 - return 0; 767 - } 768 - 769 - ret = regulator_enable(priv->vref_reg); 770 - if (ret) 771 - return ret; 772 - 773 - ret = devm_add_action_or_reset(dev, tsc2046_adc_regulator_disable, 774 - priv); 775 - if (ret) 776 - return ret; 777 - 778 - ret = regulator_get_voltage(priv->vref_reg); 779 - if (ret < 0) 780 - return ret; 781 - 782 - priv->vref_mv = ret / MILLI; 783 - 784 - return 0; 785 - } 786 - 787 749 static int tsc2046_adc_probe(struct spi_device *spi) 788 750 { 789 751 const struct tsc2046_adc_dcfg *dcfg; ··· 782 830 indio_dev->num_channels = dcfg->num_channels; 783 831 indio_dev->info = &tsc2046_adc_info; 784 832 785 - ret = tsc2046_adc_configure_regulator(priv); 786 - if (ret) 833 + ret = devm_regulator_get_enable_read_voltage(dev, "vref"); 834 + if (ret < 0 && ret != -ENODEV) 787 835 return ret; 836 + 837 + priv->internal_vref = ret == -ENODEV; 838 + priv->vref_mv = priv->internal_vref ? TI_TSC2046_INT_VREF : ret / MILLI; 788 839 789 840 tsc2046_adc_parse_fwnode(priv); 790 841
+1 -1
drivers/iio/adc/vf610_adc.c
··· 752 752 writel(val, info->regs + VF610_REG_ADC_GC); 753 753 754 754 channel = find_first_bit(indio_dev->active_scan_mask, 755 - indio_dev->masklength); 755 + iio_get_masklength(indio_dev)); 756 756 757 757 val = VF610_ADC_ADCHC(channel); 758 758 val |= VF610_ADC_AIEN;
+2 -3
drivers/iio/adc/xilinx-xadc-core.c
··· 628 628 size_t n; 629 629 void *data; 630 630 631 - n = bitmap_weight(mask, indio_dev->masklength); 631 + n = bitmap_weight(mask, iio_get_masklength(indio_dev)); 632 632 633 633 data = devm_krealloc_array(indio_dev->dev.parent, xadc->data, 634 634 n, sizeof(*xadc->data), GFP_KERNEL); ··· 681 681 goto out; 682 682 683 683 j = 0; 684 - for_each_set_bit(i, indio_dev->active_scan_mask, 685 - indio_dev->masklength) { 684 + iio_for_each_active_channel(indio_dev, i) { 686 685 chan = xadc_scan_index_to_channel(i); 687 686 xadc_read_adc_reg(xadc, chan, &xadc->data[j]); 688 687 j++;
+1 -1
drivers/iio/buffer/industrialio-buffer-cb.c
··· 77 77 } 78 78 79 79 cb_buff->indio_dev = cb_buff->channels[0].indio_dev; 80 - cb_buff->buffer.scan_mask = bitmap_zalloc(cb_buff->indio_dev->masklength, 80 + cb_buff->buffer.scan_mask = bitmap_zalloc(iio_get_masklength(cb_buff->indio_dev), 81 81 GFP_KERNEL); 82 82 if (cb_buff->buffer.scan_mask == NULL) { 83 83 ret = -ENOMEM;
+18 -18
drivers/iio/buffer/industrialio-buffer-dma.c
··· 248 248 iio_dma_buffer_queue_wake(queue); 249 249 dma_fence_end_signalling(cookie); 250 250 } 251 - EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done); 251 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_block_done, IIO_DMA_BUFFER); 252 252 253 253 /** 254 254 * iio_dma_buffer_block_list_abort() - Indicate that a list block has been ··· 287 287 iio_dma_buffer_queue_wake(queue); 288 288 dma_fence_end_signalling(cookie); 289 289 } 290 - EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort); 290 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_block_list_abort, IIO_DMA_BUFFER); 291 291 292 292 static bool iio_dma_block_reusable(struct iio_dma_buffer_block *block) 293 293 { ··· 420 420 421 421 return ret; 422 422 } 423 - EXPORT_SYMBOL_GPL(iio_dma_buffer_request_update); 423 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_request_update, IIO_DMA_BUFFER); 424 424 425 425 static void iio_dma_buffer_fileio_free(struct iio_dma_buffer_queue *queue) 426 426 { ··· 506 506 507 507 return 0; 508 508 } 509 - EXPORT_SYMBOL_GPL(iio_dma_buffer_enable); 509 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_enable, IIO_DMA_BUFFER); 510 510 511 511 /** 512 512 * iio_dma_buffer_disable() - Disable DMA buffer ··· 530 530 531 531 return 0; 532 532 } 533 - EXPORT_SYMBOL_GPL(iio_dma_buffer_disable); 533 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_disable, IIO_DMA_BUFFER); 534 534 535 535 static void iio_dma_buffer_enqueue(struct iio_dma_buffer_queue *queue, 536 536 struct iio_dma_buffer_block *block) ··· 636 636 { 637 637 return iio_dma_buffer_io(buffer, n, user_buffer, false); 638 638 } 639 - EXPORT_SYMBOL_GPL(iio_dma_buffer_read); 639 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_read, IIO_DMA_BUFFER); 640 640 641 641 /** 642 642 * iio_dma_buffer_write() - DMA buffer write callback ··· 653 653 return iio_dma_buffer_io(buffer, n, 654 654 (__force __user char *)user_buffer, true); 655 655 } 656 - EXPORT_SYMBOL_GPL(iio_dma_buffer_write); 656 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_write, IIO_DMA_BUFFER); 657 657 658 658 /** 659 659 * iio_dma_buffer_usage() - DMA buffer data_available and ··· 696 696 697 697 return data_available; 698 698 } 699 - EXPORT_SYMBOL_GPL(iio_dma_buffer_usage); 699 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_usage, IIO_DMA_BUFFER); 700 700 701 701 struct iio_dma_buffer_block * 702 702 iio_dma_buffer_attach_dmabuf(struct iio_buffer *buffer, ··· 723 723 724 724 return block; 725 725 } 726 - EXPORT_SYMBOL_GPL(iio_dma_buffer_attach_dmabuf); 726 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_attach_dmabuf, IIO_DMA_BUFFER); 727 727 728 728 void iio_dma_buffer_detach_dmabuf(struct iio_buffer *buffer, 729 729 struct iio_dma_buffer_block *block) ··· 731 731 block->state = IIO_BLOCK_STATE_DEAD; 732 732 iio_buffer_block_put_atomic(block); 733 733 } 734 - EXPORT_SYMBOL_GPL(iio_dma_buffer_detach_dmabuf); 734 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_detach_dmabuf, IIO_DMA_BUFFER); 735 735 736 736 static int iio_dma_can_enqueue_block(struct iio_dma_buffer_block *block) 737 737 { ··· 784 784 785 785 return ret; 786 786 } 787 - EXPORT_SYMBOL_GPL(iio_dma_buffer_enqueue_dmabuf); 787 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_enqueue_dmabuf, IIO_DMA_BUFFER); 788 788 789 789 void iio_dma_buffer_lock_queue(struct iio_buffer *buffer) 790 790 { ··· 792 792 793 793 mutex_lock(&queue->lock); 794 794 } 795 - EXPORT_SYMBOL_GPL(iio_dma_buffer_lock_queue); 795 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_lock_queue, IIO_DMA_BUFFER); 796 796 797 797 void iio_dma_buffer_unlock_queue(struct iio_buffer *buffer) 798 798 { ··· 800 800 801 801 mutex_unlock(&queue->lock); 802 802 } 803 - EXPORT_SYMBOL_GPL(iio_dma_buffer_unlock_queue); 803 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_unlock_queue, IIO_DMA_BUFFER); 804 804 805 805 /** 806 806 * iio_dma_buffer_set_bytes_per_datum() - DMA buffer set_bytes_per_datum callback ··· 816 816 817 817 return 0; 818 818 } 819 - EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum); 819 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_set_bytes_per_datum, IIO_DMA_BUFFER); 820 820 821 821 /** 822 822 * iio_dma_buffer_set_length - DMA buffer set_length callback ··· 836 836 837 837 return 0; 838 838 } 839 - EXPORT_SYMBOL_GPL(iio_dma_buffer_set_length); 839 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_set_length, IIO_DMA_BUFFER); 840 840 841 841 /** 842 842 * iio_dma_buffer_init() - Initialize DMA buffer queue ··· 864 864 865 865 return 0; 866 866 } 867 - EXPORT_SYMBOL_GPL(iio_dma_buffer_init); 867 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_init, IIO_DMA_BUFFER); 868 868 869 869 /** 870 870 * iio_dma_buffer_exit() - Cleanup DMA buffer queue ··· 882 882 883 883 mutex_unlock(&queue->lock); 884 884 } 885 - EXPORT_SYMBOL_GPL(iio_dma_buffer_exit); 885 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_exit, IIO_DMA_BUFFER); 886 886 887 887 /** 888 888 * iio_dma_buffer_release() - Release final buffer resources ··· 896 896 { 897 897 mutex_destroy(&queue->lock); 898 898 } 899 - EXPORT_SYMBOL_GPL(iio_dma_buffer_release); 899 + EXPORT_SYMBOL_NS_GPL(iio_dma_buffer_release, IIO_DMA_BUFFER); 900 900 901 901 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 902 902 MODULE_DESCRIPTION("DMA buffer for the IIO framework");
+1
drivers/iio/buffer/industrialio-buffer-dmaengine.c
··· 348 348 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 349 349 MODULE_DESCRIPTION("DMA buffer for the IIO framework"); 350 350 MODULE_LICENSE("GPL"); 351 + MODULE_IMPORT_NS(IIO_DMA_BUFFER);
+2 -2
drivers/iio/buffer/industrialio-hw-consumer.c
··· 52 52 static struct hw_consumer_buffer *iio_hw_consumer_get_buffer( 53 53 struct iio_hw_consumer *hwc, struct iio_dev *indio_dev) 54 54 { 55 + unsigned int mask_longs = BITS_TO_LONGS(iio_get_masklength(indio_dev)); 55 56 struct hw_consumer_buffer *buf; 56 57 57 58 list_for_each_entry(buf, &hwc->buffers, head) { ··· 60 59 return buf; 61 60 } 62 61 63 - buf = kzalloc(struct_size(buf, scan_mask, BITS_TO_LONGS(indio_dev->masklength)), 64 - GFP_KERNEL); 62 + buf = kzalloc(struct_size(buf, scan_mask, mask_longs), GFP_KERNEL); 65 63 if (!buf) 66 64 return NULL; 67 65
+15 -26
drivers/iio/chemical/bme680.h
··· 12 12 13 13 #define BME680_REG_TEMP_MSB 0x22 14 14 #define BME680_REG_PRESS_MSB 0x1F 15 - #define BM6880_REG_HUMIDITY_MSB 0x25 15 + #define BME680_REG_HUMIDITY_MSB 0x25 16 16 #define BME680_REG_GAS_MSB 0x2A 17 17 #define BME680_REG_GAS_R_LSB 0x2B 18 18 #define BME680_GAS_STAB_BIT BIT(4) ··· 39 39 #define BME680_HUM_REG_SHIFT_VAL 4 40 40 #define BME680_BIT_H1_DATA_MASK GENMASK(3, 0) 41 41 42 - #define BME680_REG_RES_HEAT_RANGE 0x02 43 42 #define BME680_RHRANGE_MASK GENMASK(5, 4) 44 43 #define BME680_REG_RES_HEAT_VAL 0x00 45 - #define BME680_REG_RANGE_SW_ERR 0x04 46 44 #define BME680_RSERROR_MASK GENMASK(7, 4) 47 45 #define BME680_REG_RES_HEAT_0 0x5A 48 46 #define BME680_REG_GAS_WAIT_0 0x64 49 - #define BME680_ADC_GAS_RES_SHIFT 6 47 + #define BME680_ADC_GAS_RES GENMASK(15, 6) 50 48 #define BME680_AMB_TEMP 25 51 49 52 50 #define BME680_REG_CTRL_GAS_1 0x71 ··· 56 58 #define BME680_GAS_MEAS_BIT BIT(6) 57 59 #define BME680_MEAS_BIT BIT(5) 58 60 61 + #define BME680_TEMP_NUM_BYTES 3 62 + #define BME680_PRESS_NUM_BYTES 3 63 + #define BME680_HUMID_NUM_BYTES 2 64 + #define BME680_GAS_NUM_BYTES 2 65 + 66 + #define BME680_MEAS_TRIM_MASK GENMASK(24, 4) 67 + 68 + #define BME680_STARTUP_TIME_US 5000 69 + 59 70 /* Calibration Parameters */ 60 71 #define BME680_T2_LSB_REG 0x8A 61 - #define BME680_T3_REG 0x8C 62 - #define BME680_P1_LSB_REG 0x8E 63 - #define BME680_P2_LSB_REG 0x90 64 - #define BME680_P3_REG 0x92 65 - #define BME680_P4_LSB_REG 0x94 66 - #define BME680_P5_LSB_REG 0x96 67 - #define BME680_P7_REG 0x98 68 - #define BME680_P6_REG 0x99 69 - #define BME680_P8_LSB_REG 0x9C 70 - #define BME680_P9_LSB_REG 0x9E 71 - #define BME680_P10_REG 0xA0 72 - #define BME680_H2_LSB_REG 0xE2 73 72 #define BME680_H2_MSB_REG 0xE1 74 - #define BME680_H1_MSB_REG 0xE3 75 - #define BME680_H1_LSB_REG 0xE2 76 - #define BME680_H3_REG 0xE4 77 - #define BME680_H4_REG 0xE5 78 - #define BME680_H5_REG 0xE6 79 - #define BME680_H6_REG 0xE7 80 - #define BME680_H7_REG 0xE8 81 - #define BME680_T1_LSB_REG 0xE9 82 - #define BME680_GH2_LSB_REG 0xEB 83 - #define BME680_GH1_REG 0xED 84 73 #define BME680_GH3_REG 0xEE 74 + 75 + #define BME680_CALIB_RANGE_1_LEN 23 76 + #define BME680_CALIB_RANGE_2_LEN 14 77 + #define BME680_CALIB_RANGE_3_LEN 5 85 78 86 79 extern const struct regmap_config bme680_regmap_config; 87 80
+277 -356
drivers/iio/chemical/bme680_core.c
··· 8 8 * Datasheet: 9 9 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf 10 10 */ 11 - #include <linux/acpi.h> 12 11 #include <linux/bitfield.h> 12 + #include <linux/cleanup.h> 13 13 #include <linux/delay.h> 14 14 #include <linux/device.h> 15 - #include <linux/module.h> 16 15 #include <linux/log2.h> 16 + #include <linux/module.h> 17 17 #include <linux/regmap.h> 18 + 18 19 #include <linux/iio/iio.h> 19 20 #include <linux/iio/sysfs.h> 20 21 22 + #include <asm/unaligned.h> 23 + 21 24 #include "bme680.h" 25 + 26 + /* 1st set of calibration data */ 27 + enum { 28 + /* Temperature calib indexes */ 29 + T2_LSB = 0, 30 + T3 = 2, 31 + /* Pressure calib indexes */ 32 + P1_LSB = 4, 33 + P2_LSB = 6, 34 + P3 = 8, 35 + P4_LSB = 10, 36 + P5_LSB = 12, 37 + P7 = 14, 38 + P6 = 15, 39 + P8_LSB = 18, 40 + P9_LSB = 20, 41 + P10 = 22, 42 + }; 43 + 44 + /* 2nd set of calibration data */ 45 + enum { 46 + /* Humidity calib indexes */ 47 + H2_MSB = 0, 48 + H1_LSB = 1, 49 + H3 = 3, 50 + H4 = 4, 51 + H5 = 5, 52 + H6 = 6, 53 + H7 = 7, 54 + /* Stray T1 calib index */ 55 + T1_LSB = 8, 56 + /* Gas heater calib indexes */ 57 + GH2_LSB = 10, 58 + GH1 = 12, 59 + GH3 = 13, 60 + }; 61 + 62 + /* 3rd set of calibration data */ 63 + enum { 64 + RES_HEAT_VAL = 0, 65 + RES_HEAT_RANGE = 2, 66 + RANGE_SW_ERR = 4, 67 + }; 22 68 23 69 struct bme680_calib { 24 70 u16 par_t1; ··· 98 52 struct bme680_data { 99 53 struct regmap *regmap; 100 54 struct bme680_calib bme680; 55 + struct mutex lock; /* Protect multiple serial R/W ops to device. */ 101 56 u8 oversampling_temp; 102 57 u8 oversampling_press; 103 58 u8 oversampling_humid; 104 59 u16 heater_dur; 105 60 u16 heater_temp; 106 - /* 107 - * Carryover value from temperature conversion, used in pressure 108 - * and humidity compensation calculations. 109 - */ 110 - s32 t_fine; 61 + 62 + union { 63 + u8 buf[3]; 64 + unsigned int check; 65 + __be16 be16; 66 + u8 bme680_cal_buf_1[BME680_CALIB_RANGE_1_LEN]; 67 + u8 bme680_cal_buf_2[BME680_CALIB_RANGE_2_LEN]; 68 + u8 bme680_cal_buf_3[BME680_CALIB_RANGE_3_LEN]; 69 + }; 111 70 }; 112 71 113 72 static const struct regmap_range bme680_volatile_ranges[] = { ··· 161 110 struct bme680_calib *calib) 162 111 { 163 112 struct device *dev = regmap_get_device(data->regmap); 164 - unsigned int tmp, tmp_msb, tmp_lsb; 113 + unsigned int tmp_msb, tmp_lsb; 165 114 int ret; 166 - __le16 buf; 167 - 168 - /* Temperature related coefficients */ 169 - ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG, 170 - &buf, sizeof(buf)); 171 - if (ret < 0) { 172 - dev_err(dev, "failed to read BME680_T1_LSB_REG\n"); 173 - return ret; 174 - } 175 - calib->par_t1 = le16_to_cpu(buf); 176 115 177 116 ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG, 178 - &buf, sizeof(buf)); 117 + data->bme680_cal_buf_1, 118 + sizeof(data->bme680_cal_buf_1)); 179 119 if (ret < 0) { 180 - dev_err(dev, "failed to read BME680_T2_LSB_REG\n"); 120 + dev_err(dev, "failed to read 1st set of calib data;\n"); 181 121 return ret; 182 122 } 183 - calib->par_t2 = le16_to_cpu(buf); 184 123 185 - ret = regmap_read(data->regmap, BME680_T3_REG, &tmp); 186 - if (ret < 0) { 187 - dev_err(dev, "failed to read BME680_T3_REG\n"); 188 - return ret; 189 - } 190 - calib->par_t3 = tmp; 124 + calib->par_t2 = get_unaligned_le16(&data->bme680_cal_buf_1[T2_LSB]); 125 + calib->par_t3 = data->bme680_cal_buf_1[T3]; 126 + calib->par_p1 = get_unaligned_le16(&data->bme680_cal_buf_1[P1_LSB]); 127 + calib->par_p2 = get_unaligned_le16(&data->bme680_cal_buf_1[P2_LSB]); 128 + calib->par_p3 = data->bme680_cal_buf_1[P3]; 129 + calib->par_p4 = get_unaligned_le16(&data->bme680_cal_buf_1[P4_LSB]); 130 + calib->par_p5 = get_unaligned_le16(&data->bme680_cal_buf_1[P5_LSB]); 131 + calib->par_p7 = data->bme680_cal_buf_1[P7]; 132 + calib->par_p6 = data->bme680_cal_buf_1[P6]; 133 + calib->par_p8 = get_unaligned_le16(&data->bme680_cal_buf_1[P8_LSB]); 134 + calib->par_p9 = get_unaligned_le16(&data->bme680_cal_buf_1[P9_LSB]); 135 + calib->par_p10 = data->bme680_cal_buf_1[P10]; 191 136 192 - /* Pressure related coefficients */ 193 - ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG, 194 - &buf, sizeof(buf)); 137 + ret = regmap_bulk_read(data->regmap, BME680_H2_MSB_REG, 138 + data->bme680_cal_buf_2, 139 + sizeof(data->bme680_cal_buf_2)); 195 140 if (ret < 0) { 196 - dev_err(dev, "failed to read BME680_P1_LSB_REG\n"); 141 + dev_err(dev, "failed to read 2nd set of calib data;\n"); 197 142 return ret; 198 143 } 199 - calib->par_p1 = le16_to_cpu(buf); 200 144 201 - ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG, 202 - &buf, sizeof(buf)); 203 - if (ret < 0) { 204 - dev_err(dev, "failed to read BME680_P2_LSB_REG\n"); 205 - return ret; 206 - } 207 - calib->par_p2 = le16_to_cpu(buf); 208 - 209 - ret = regmap_read(data->regmap, BME680_P3_REG, &tmp); 210 - if (ret < 0) { 211 - dev_err(dev, "failed to read BME680_P3_REG\n"); 212 - return ret; 213 - } 214 - calib->par_p3 = tmp; 215 - 216 - ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG, 217 - &buf, sizeof(buf)); 218 - if (ret < 0) { 219 - dev_err(dev, "failed to read BME680_P4_LSB_REG\n"); 220 - return ret; 221 - } 222 - calib->par_p4 = le16_to_cpu(buf); 223 - 224 - ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG, 225 - &buf, sizeof(buf)); 226 - if (ret < 0) { 227 - dev_err(dev, "failed to read BME680_P5_LSB_REG\n"); 228 - return ret; 229 - } 230 - calib->par_p5 = le16_to_cpu(buf); 231 - 232 - ret = regmap_read(data->regmap, BME680_P6_REG, &tmp); 233 - if (ret < 0) { 234 - dev_err(dev, "failed to read BME680_P6_REG\n"); 235 - return ret; 236 - } 237 - calib->par_p6 = tmp; 238 - 239 - ret = regmap_read(data->regmap, BME680_P7_REG, &tmp); 240 - if (ret < 0) { 241 - dev_err(dev, "failed to read BME680_P7_REG\n"); 242 - return ret; 243 - } 244 - calib->par_p7 = tmp; 245 - 246 - ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG, 247 - &buf, sizeof(buf)); 248 - if (ret < 0) { 249 - dev_err(dev, "failed to read BME680_P8_LSB_REG\n"); 250 - return ret; 251 - } 252 - calib->par_p8 = le16_to_cpu(buf); 253 - 254 - ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG, 255 - &buf, sizeof(buf)); 256 - if (ret < 0) { 257 - dev_err(dev, "failed to read BME680_P9_LSB_REG\n"); 258 - return ret; 259 - } 260 - calib->par_p9 = le16_to_cpu(buf); 261 - 262 - ret = regmap_read(data->regmap, BME680_P10_REG, &tmp); 263 - if (ret < 0) { 264 - dev_err(dev, "failed to read BME680_P10_REG\n"); 265 - return ret; 266 - } 267 - calib->par_p10 = tmp; 268 - 269 - /* Humidity related coefficients */ 270 - ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb); 271 - if (ret < 0) { 272 - dev_err(dev, "failed to read BME680_H1_MSB_REG\n"); 273 - return ret; 274 - } 275 - ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb); 276 - if (ret < 0) { 277 - dev_err(dev, "failed to read BME680_H1_LSB_REG\n"); 278 - return ret; 279 - } 145 + tmp_lsb = data->bme680_cal_buf_2[H1_LSB]; 146 + tmp_msb = data->bme680_cal_buf_2[H1_LSB + 1]; 280 147 calib->par_h1 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | 281 148 (tmp_lsb & BME680_BIT_H1_DATA_MASK); 282 149 283 - ret = regmap_read(data->regmap, BME680_H2_MSB_REG, &tmp_msb); 284 - if (ret < 0) { 285 - dev_err(dev, "failed to read BME680_H2_MSB_REG\n"); 286 - return ret; 287 - } 288 - ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb); 289 - if (ret < 0) { 290 - dev_err(dev, "failed to read BME680_H2_LSB_REG\n"); 291 - return ret; 292 - } 150 + tmp_msb = data->bme680_cal_buf_2[H2_MSB]; 151 + tmp_lsb = data->bme680_cal_buf_2[H2_MSB + 1]; 293 152 calib->par_h2 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) | 294 153 (tmp_lsb >> BME680_HUM_REG_SHIFT_VAL); 295 154 296 - ret = regmap_read(data->regmap, BME680_H3_REG, &tmp); 297 - if (ret < 0) { 298 - dev_err(dev, "failed to read BME680_H3_REG\n"); 299 - return ret; 300 - } 301 - calib->par_h3 = tmp; 155 + calib->par_h3 = data->bme680_cal_buf_2[H3]; 156 + calib->par_h4 = data->bme680_cal_buf_2[H4]; 157 + calib->par_h5 = data->bme680_cal_buf_2[H5]; 158 + calib->par_h6 = data->bme680_cal_buf_2[H6]; 159 + calib->par_h7 = data->bme680_cal_buf_2[H7]; 160 + calib->par_t1 = get_unaligned_le16(&data->bme680_cal_buf_2[T1_LSB]); 161 + calib->par_gh2 = get_unaligned_le16(&data->bme680_cal_buf_2[GH2_LSB]); 162 + calib->par_gh1 = data->bme680_cal_buf_2[GH1]; 163 + calib->par_gh3 = data->bme680_cal_buf_2[GH3]; 302 164 303 - ret = regmap_read(data->regmap, BME680_H4_REG, &tmp); 165 + ret = regmap_bulk_read(data->regmap, BME680_REG_RES_HEAT_VAL, 166 + data->bme680_cal_buf_3, 167 + sizeof(data->bme680_cal_buf_3)); 304 168 if (ret < 0) { 305 - dev_err(dev, "failed to read BME680_H4_REG\n"); 169 + dev_err(dev, "failed to read 3rd set of calib data;\n"); 306 170 return ret; 307 171 } 308 - calib->par_h4 = tmp; 309 172 310 - ret = regmap_read(data->regmap, BME680_H5_REG, &tmp); 311 - if (ret < 0) { 312 - dev_err(dev, "failed to read BME680_H5_REG\n"); 313 - return ret; 314 - } 315 - calib->par_h5 = tmp; 173 + calib->res_heat_val = data->bme680_cal_buf_3[RES_HEAT_VAL]; 316 174 317 - ret = regmap_read(data->regmap, BME680_H6_REG, &tmp); 318 - if (ret < 0) { 319 - dev_err(dev, "failed to read BME680_H6_REG\n"); 320 - return ret; 321 - } 322 - calib->par_h6 = tmp; 175 + calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, 176 + data->bme680_cal_buf_3[RES_HEAT_RANGE]); 323 177 324 - ret = regmap_read(data->regmap, BME680_H7_REG, &tmp); 325 - if (ret < 0) { 326 - dev_err(dev, "failed to read BME680_H7_REG\n"); 327 - return ret; 328 - } 329 - calib->par_h7 = tmp; 178 + calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, 179 + data->bme680_cal_buf_3[RANGE_SW_ERR]); 330 180 331 - /* Gas heater related coefficients */ 332 - ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp); 333 - if (ret < 0) { 334 - dev_err(dev, "failed to read BME680_GH1_REG\n"); 335 - return ret; 336 - } 337 - calib->par_gh1 = tmp; 181 + return 0; 182 + } 338 183 339 - ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG, 340 - &buf, sizeof(buf)); 341 - if (ret < 0) { 342 - dev_err(dev, "failed to read BME680_GH2_LSB_REG\n"); 343 - return ret; 344 - } 345 - calib->par_gh2 = le16_to_cpu(buf); 184 + static int bme680_read_temp_adc(struct bme680_data *data, u32 *adc_temp) 185 + { 186 + struct device *dev = regmap_get_device(data->regmap); 187 + u32 value_temp; 188 + int ret; 346 189 347 - ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp); 190 + ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB, 191 + data->buf, BME680_TEMP_NUM_BYTES); 348 192 if (ret < 0) { 349 - dev_err(dev, "failed to read BME680_GH3_REG\n"); 193 + dev_err(dev, "failed to read temperature\n"); 350 194 return ret; 351 195 } 352 - calib->par_gh3 = tmp; 353 196 354 - /* Other coefficients */ 355 - ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp); 356 - if (ret < 0) { 357 - dev_err(dev, "failed to read resistance heat range\n"); 358 - return ret; 197 + value_temp = FIELD_GET(BME680_MEAS_TRIM_MASK, 198 + get_unaligned_be24(data->buf)); 199 + if (value_temp == BME680_MEAS_SKIPPED) { 200 + /* reading was skipped */ 201 + dev_err(dev, "reading temperature skipped\n"); 202 + return -EINVAL; 359 203 } 360 - calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, tmp); 361 - 362 - ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp); 363 - if (ret < 0) { 364 - dev_err(dev, "failed to read resistance heat value\n"); 365 - return ret; 366 - } 367 - calib->res_heat_val = tmp; 368 - 369 - ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp); 370 - if (ret < 0) { 371 - dev_err(dev, "failed to read range software error\n"); 372 - return ret; 373 - } 374 - calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, tmp); 204 + *adc_temp = value_temp; 375 205 376 206 return 0; 377 207 } ··· 264 332 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore, 265 333 * output value of "3233" represents 32.33 DegC. 266 334 */ 267 - static s16 bme680_compensate_temp(struct bme680_data *data, 268 - s32 adc_temp) 335 + static s32 bme680_calc_t_fine(struct bme680_data *data, u32 adc_temp) 269 336 { 270 337 struct bme680_calib *calib = &data->bme680; 271 338 s64 var1, var2, var3; 272 - s16 calc_temp; 273 339 274 340 /* If the calibration is invalid, attempt to reload it */ 275 341 if (!calib->par_t2) 276 342 bme680_read_calib(data, calib); 277 343 278 - var1 = (adc_temp >> 3) - ((s32)calib->par_t1 << 1); 344 + var1 = ((s32)adc_temp >> 3) - ((s32)calib->par_t1 << 1); 279 345 var2 = (var1 * calib->par_t2) >> 11; 280 346 var3 = ((var1 >> 1) * (var1 >> 1)) >> 12; 281 347 var3 = (var3 * ((s32)calib->par_t3 << 4)) >> 14; 282 - data->t_fine = var2 + var3; 283 - calc_temp = (data->t_fine * 5 + 128) >> 8; 348 + return var2 + var3; /* t_fine = var2 + var3 */ 349 + } 284 350 285 - return calc_temp; 351 + static int bme680_get_t_fine(struct bme680_data *data, s32 *t_fine) 352 + { 353 + u32 adc_temp; 354 + int ret; 355 + 356 + ret = bme680_read_temp_adc(data, &adc_temp); 357 + if (ret) 358 + return ret; 359 + 360 + *t_fine = bme680_calc_t_fine(data, adc_temp); 361 + 362 + return 0; 363 + } 364 + 365 + static s16 bme680_compensate_temp(struct bme680_data *data, 366 + u32 adc_temp) 367 + { 368 + return (bme680_calc_t_fine(data, adc_temp) * 5 + 128) / 256; 369 + } 370 + 371 + static int bme680_read_press_adc(struct bme680_data *data, u32 *adc_press) 372 + { 373 + struct device *dev = regmap_get_device(data->regmap); 374 + u32 value_press; 375 + int ret; 376 + 377 + ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB, 378 + data->buf, BME680_PRESS_NUM_BYTES); 379 + if (ret < 0) { 380 + dev_err(dev, "failed to read pressure\n"); 381 + return ret; 382 + } 383 + 384 + value_press = FIELD_GET(BME680_MEAS_TRIM_MASK, 385 + get_unaligned_be24(data->buf)); 386 + if (value_press == BME680_MEAS_SKIPPED) { 387 + /* reading was skipped */ 388 + dev_err(dev, "reading pressure skipped\n"); 389 + return -EINVAL; 390 + } 391 + *adc_press = value_press; 392 + 393 + return 0; 286 394 } 287 395 288 396 /* ··· 333 361 * 97356 Pa = 973.56 hPa. 334 362 */ 335 363 static u32 bme680_compensate_press(struct bme680_data *data, 336 - u32 adc_press) 364 + u32 adc_press, s32 t_fine) 337 365 { 338 366 struct bme680_calib *calib = &data->bme680; 339 367 s32 var1, var2, var3, press_comp; 340 368 341 - var1 = (data->t_fine >> 1) - 64000; 369 + var1 = (t_fine >> 1) - 64000; 342 370 var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib->par_p6) >> 2; 343 371 var2 = var2 + (var1 * calib->par_p5 << 1); 344 372 var2 = (var2 >> 2) + ((s32)calib->par_p4 << 16); ··· 366 394 return press_comp; 367 395 } 368 396 397 + static int bme680_read_humid_adc(struct bme680_data *data, u32 *adc_humidity) 398 + { 399 + struct device *dev = regmap_get_device(data->regmap); 400 + u32 value_humidity; 401 + int ret; 402 + 403 + ret = regmap_bulk_read(data->regmap, BME680_REG_HUMIDITY_MSB, 404 + &data->be16, BME680_HUMID_NUM_BYTES); 405 + if (ret < 0) { 406 + dev_err(dev, "failed to read humidity\n"); 407 + return ret; 408 + } 409 + 410 + value_humidity = be16_to_cpu(data->be16); 411 + if (value_humidity == BME680_MEAS_SKIPPED) { 412 + /* reading was skipped */ 413 + dev_err(dev, "reading humidity skipped\n"); 414 + return -EINVAL; 415 + } 416 + *adc_humidity = value_humidity; 417 + 418 + return 0; 419 + } 420 + 369 421 /* 370 422 * Taken from Bosch BME680 API: 371 423 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937 ··· 398 402 * value of "43215" represents 43.215 %rH. 399 403 */ 400 404 static u32 bme680_compensate_humid(struct bme680_data *data, 401 - u16 adc_humid) 405 + u16 adc_humid, s32 t_fine) 402 406 { 403 407 struct bme680_calib *calib = &data->bme680; 404 408 s32 var1, var2, var3, var4, var5, var6, temp_scaled, calc_hum; 405 409 406 - temp_scaled = (data->t_fine * 5 + 128) >> 8; 407 - var1 = (adc_humid - ((s32) ((s32) calib->par_h1 * 16))) - 408 - (((temp_scaled * (s32) calib->par_h3) / 100) >> 1); 409 - var2 = ((s32) calib->par_h2 * 410 + temp_scaled = (t_fine * 5 + 128) >> 8; 411 + var1 = (adc_humid - (((s32)calib->par_h1 * 16))) - 412 + (((temp_scaled * calib->par_h3) / 100) >> 1); 413 + var2 = (calib->par_h2 * 410 414 (((temp_scaled * calib->par_h4) / 100) + 411 415 (((temp_scaled * ((temp_scaled * calib->par_h5) / 100)) 412 416 >> 6) / 100) + (1 << 14))) >> 10; ··· 438 442 u32 calc_gas_res; 439 443 440 444 /* Look up table for the possible gas range values */ 441 - const u32 lookupTable[16] = {2147483647u, 2147483647u, 445 + static const u32 lookupTable[16] = {2147483647u, 2147483647u, 442 446 2147483647u, 2147483647u, 2147483647u, 443 447 2126008810u, 2147483647u, 2130303777u, 444 448 2147483647u, 2147483647u, 2143188679u, ··· 536 540 static int bme680_wait_for_eoc(struct bme680_data *data) 537 541 { 538 542 struct device *dev = regmap_get_device(data->regmap); 539 - unsigned int check; 540 543 int ret; 541 544 /* 542 545 * (Sum of oversampling ratios * time per oversampling) + ··· 548 553 549 554 usleep_range(wait_eoc_us, wait_eoc_us + 100); 550 555 551 - ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check); 556 + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check); 552 557 if (ret) { 553 558 dev_err(dev, "failed to read measurement status register.\n"); 554 559 return ret; 555 560 } 556 - if (check & BME680_MEAS_BIT) { 561 + if (data->check & BME680_MEAS_BIT) { 557 562 dev_err(dev, "Device measurement cycle incomplete.\n"); 558 563 return -EBUSY; 559 564 } 560 - if (!(check & BME680_NEW_DATA_BIT)) { 565 + if (!(data->check & BME680_NEW_DATA_BIT)) { 561 566 dev_err(dev, "No new data available from the device.\n"); 562 567 return -ENODATA; 563 568 } ··· 601 606 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS, 602 607 BME680_OSRS_TEMP_MASK | BME680_OSRS_PRESS_MASK, 603 608 osrs); 604 - if (ret < 0) 609 + if (ret < 0) { 605 610 dev_err(dev, "failed to write ctrl_meas register\n"); 611 + return ret; 612 + } 606 613 607 - return ret; 614 + return 0; 608 615 } 609 616 610 617 static int bme680_gas_config(struct bme680_data *data) ··· 614 617 struct device *dev = regmap_get_device(data->regmap); 615 618 int ret; 616 619 u8 heatr_res, heatr_dur; 620 + 621 + /* Go to sleep */ 622 + ret = bme680_set_mode(data, false); 623 + if (ret < 0) 624 + return ret; 617 625 618 626 heatr_res = bme680_calc_heater_res(data, data->heater_temp); 619 627 ··· 651 649 652 650 static int bme680_read_temp(struct bme680_data *data, int *val) 653 651 { 654 - struct device *dev = regmap_get_device(data->regmap); 655 652 int ret; 656 - __be32 tmp = 0; 657 - s32 adc_temp; 653 + u32 adc_temp; 658 654 s16 comp_temp; 659 655 660 - /* set forced mode to trigger measurement */ 661 - ret = bme680_set_mode(data, true); 662 - if (ret < 0) 663 - return ret; 664 - 665 - ret = bme680_wait_for_eoc(data); 656 + ret = bme680_read_temp_adc(data, &adc_temp); 666 657 if (ret) 667 658 return ret; 668 659 669 - ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB, 670 - &tmp, 3); 671 - if (ret < 0) { 672 - dev_err(dev, "failed to read temperature\n"); 673 - return ret; 674 - } 675 - 676 - adc_temp = be32_to_cpu(tmp) >> 12; 677 - if (adc_temp == BME680_MEAS_SKIPPED) { 678 - /* reading was skipped */ 679 - dev_err(dev, "reading temperature skipped\n"); 680 - return -EINVAL; 681 - } 682 660 comp_temp = bme680_compensate_temp(data, adc_temp); 683 - /* 684 - * val might be NULL if we're called by the read_press/read_humid 685 - * routine which is called to get t_fine value used in 686 - * compensate_press/compensate_humid to get compensated 687 - * pressure/humidity readings. 688 - */ 689 - if (val) { 690 - *val = comp_temp * 10; /* Centidegrees to millidegrees */ 691 - return IIO_VAL_INT; 692 - } 693 - 694 - return ret; 661 + *val = comp_temp * 10; /* Centidegrees to millidegrees */ 662 + return IIO_VAL_INT; 695 663 } 696 664 697 665 static int bme680_read_press(struct bme680_data *data, 698 666 int *val, int *val2) 699 667 { 700 - struct device *dev = regmap_get_device(data->regmap); 701 668 int ret; 702 - __be32 tmp = 0; 703 - s32 adc_press; 669 + u32 adc_press; 670 + s32 t_fine; 704 671 705 - /* Read and compensate temperature to get a reading of t_fine */ 706 - ret = bme680_read_temp(data, NULL); 707 - if (ret < 0) 672 + ret = bme680_get_t_fine(data, &t_fine); 673 + if (ret) 708 674 return ret; 709 675 710 - ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB, 711 - &tmp, 3); 712 - if (ret < 0) { 713 - dev_err(dev, "failed to read pressure\n"); 676 + ret = bme680_read_press_adc(data, &adc_press); 677 + if (ret) 714 678 return ret; 715 - } 716 679 717 - adc_press = be32_to_cpu(tmp) >> 12; 718 - if (adc_press == BME680_MEAS_SKIPPED) { 719 - /* reading was skipped */ 720 - dev_err(dev, "reading pressure skipped\n"); 721 - return -EINVAL; 722 - } 723 - 724 - *val = bme680_compensate_press(data, adc_press); 680 + *val = bme680_compensate_press(data, adc_press, t_fine); 725 681 *val2 = 1000; 726 682 return IIO_VAL_FRACTIONAL; 727 683 } ··· 687 727 static int bme680_read_humid(struct bme680_data *data, 688 728 int *val, int *val2) 689 729 { 690 - struct device *dev = regmap_get_device(data->regmap); 691 730 int ret; 692 - __be16 tmp = 0; 693 - s32 adc_humidity; 694 - u32 comp_humidity; 731 + u32 adc_humidity, comp_humidity; 732 + s32 t_fine; 695 733 696 - /* Read and compensate temperature to get a reading of t_fine */ 697 - ret = bme680_read_temp(data, NULL); 698 - if (ret < 0) 734 + ret = bme680_get_t_fine(data, &t_fine); 735 + if (ret) 699 736 return ret; 700 737 701 - ret = regmap_bulk_read(data->regmap, BM6880_REG_HUMIDITY_MSB, 702 - &tmp, sizeof(tmp)); 703 - if (ret < 0) { 704 - dev_err(dev, "failed to read humidity\n"); 738 + ret = bme680_read_humid_adc(data, &adc_humidity); 739 + if (ret) 705 740 return ret; 706 - } 707 741 708 - adc_humidity = be16_to_cpu(tmp); 709 - if (adc_humidity == BME680_MEAS_SKIPPED) { 710 - /* reading was skipped */ 711 - dev_err(dev, "reading humidity skipped\n"); 712 - return -EINVAL; 713 - } 714 - comp_humidity = bme680_compensate_humid(data, adc_humidity); 742 + comp_humidity = bme680_compensate_humid(data, adc_humidity, t_fine); 715 743 716 744 *val = comp_humidity; 717 745 *val2 = 1000; ··· 711 763 { 712 764 struct device *dev = regmap_get_device(data->regmap); 713 765 int ret; 714 - __be16 tmp = 0; 715 - unsigned int check; 716 - u16 adc_gas_res; 766 + u16 adc_gas_res, gas_regs_val; 717 767 u8 gas_range; 718 768 719 - /* Set heater settings */ 720 - ret = bme680_gas_config(data); 721 - if (ret < 0) { 722 - dev_err(dev, "failed to set gas config\n"); 723 - return ret; 724 - } 725 - 726 - /* set forced mode to trigger measurement */ 727 - ret = bme680_set_mode(data, true); 728 - if (ret < 0) 729 - return ret; 730 - 731 - ret = bme680_wait_for_eoc(data); 732 - if (ret) 733 - return ret; 734 - 735 - ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check); 736 - if (check & BME680_GAS_MEAS_BIT) { 769 + ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &data->check); 770 + if (data->check & BME680_GAS_MEAS_BIT) { 737 771 dev_err(dev, "gas measurement incomplete\n"); 738 772 return -EBUSY; 739 773 } 740 774 741 - ret = regmap_read(data->regmap, BME680_REG_GAS_R_LSB, &check); 775 + ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB, 776 + &data->be16, BME680_GAS_NUM_BYTES); 742 777 if (ret < 0) { 743 - dev_err(dev, "failed to read gas_r_lsb register\n"); 778 + dev_err(dev, "failed to read gas resistance\n"); 744 779 return ret; 745 780 } 781 + 782 + gas_regs_val = be16_to_cpu(data->be16); 783 + adc_gas_res = FIELD_GET(BME680_ADC_GAS_RES, gas_regs_val); 746 784 747 785 /* 748 786 * occurs if either the gas heating duration was insuffient ··· 736 802 * heater temperature was too high for the heater sink to 737 803 * reach. 738 804 */ 739 - if ((check & BME680_GAS_STAB_BIT) == 0) { 805 + if ((gas_regs_val & BME680_GAS_STAB_BIT) == 0) { 740 806 dev_err(dev, "heater failed to reach the target temperature\n"); 741 807 return -EINVAL; 742 808 } 743 809 744 - ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB, 745 - &tmp, sizeof(tmp)); 746 - if (ret < 0) { 747 - dev_err(dev, "failed to read gas resistance\n"); 748 - return ret; 749 - } 750 - 751 - gas_range = check & BME680_GAS_RANGE_MASK; 752 - adc_gas_res = be16_to_cpu(tmp) >> BME680_ADC_GAS_RES_SHIFT; 810 + gas_range = FIELD_GET(BME680_GAS_RANGE_MASK, gas_regs_val); 753 811 754 812 *val = bme680_compensate_gas(data, adc_gas_res, gas_range); 755 813 return IIO_VAL_INT; ··· 752 826 int *val, int *val2, long mask) 753 827 { 754 828 struct bme680_data *data = iio_priv(indio_dev); 829 + int ret; 830 + 831 + guard(mutex)(&data->lock); 832 + 833 + /* set forced mode to trigger measurement */ 834 + ret = bme680_set_mode(data, true); 835 + if (ret < 0) 836 + return ret; 837 + 838 + ret = bme680_wait_for_eoc(data); 839 + if (ret) 840 + return ret; 755 841 756 842 switch (mask) { 757 843 case IIO_CHAN_INFO_PROCESSED: ··· 808 870 int val, int val2, long mask) 809 871 { 810 872 struct bme680_data *data = iio_priv(indio_dev); 873 + 874 + guard(mutex)(&data->lock); 811 875 812 876 if (val2 != 0) 813 877 return -EINVAL; ··· 861 921 .attrs = &bme680_attribute_group, 862 922 }; 863 923 864 - static const char *bme680_match_acpi_device(struct device *dev) 865 - { 866 - const struct acpi_device_id *id; 867 - 868 - id = acpi_match_device(dev->driver->acpi_match_table, dev); 869 - if (!id) 870 - return NULL; 871 - 872 - return dev_name(dev); 873 - } 874 - 875 924 int bme680_core_probe(struct device *dev, struct regmap *regmap, 876 925 const char *name) 877 926 { 878 927 struct iio_dev *indio_dev; 879 928 struct bme680_data *data; 880 - unsigned int val; 881 929 int ret; 882 - 883 - ret = regmap_write(regmap, BME680_REG_SOFT_RESET, 884 - BME680_CMD_SOFTRESET); 885 - if (ret < 0) { 886 - dev_err(dev, "Failed to reset chip\n"); 887 - return ret; 888 - } 889 - 890 - ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val); 891 - if (ret < 0) { 892 - dev_err(dev, "Error reading chip ID\n"); 893 - return ret; 894 - } 895 - 896 - if (val != BME680_CHIP_ID_VAL) { 897 - dev_err(dev, "Wrong chip ID, got %x expected %x\n", 898 - val, BME680_CHIP_ID_VAL); 899 - return -ENODEV; 900 - } 901 930 902 931 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 903 932 if (!indio_dev) 904 933 return -ENOMEM; 905 934 906 - if (!name && ACPI_HANDLE(dev)) 907 - name = bme680_match_acpi_device(dev); 908 - 909 935 data = iio_priv(indio_dev); 936 + mutex_init(&data->lock); 910 937 dev_set_drvdata(dev, indio_dev); 911 938 data->regmap = regmap; 912 939 indio_dev->name = name; ··· 889 982 data->heater_temp = 320; /* degree Celsius */ 890 983 data->heater_dur = 150; /* milliseconds */ 891 984 892 - ret = bme680_chip_config(data); 893 - if (ret < 0) { 894 - dev_err(dev, "failed to set chip_config data\n"); 895 - return ret; 896 - } 985 + ret = regmap_write(regmap, BME680_REG_SOFT_RESET, 986 + BME680_CMD_SOFTRESET); 987 + if (ret < 0) 988 + return dev_err_probe(dev, ret, "Failed to reset chip\n"); 897 989 898 - ret = bme680_gas_config(data); 899 - if (ret < 0) { 900 - dev_err(dev, "failed to set gas config data\n"); 901 - return ret; 990 + usleep_range(BME680_STARTUP_TIME_US, BME680_STARTUP_TIME_US + 1000); 991 + 992 + ret = regmap_read(regmap, BME680_REG_CHIP_ID, &data->check); 993 + if (ret < 0) 994 + return dev_err_probe(dev, ret, "Error reading chip ID\n"); 995 + 996 + if (data->check != BME680_CHIP_ID_VAL) { 997 + dev_err(dev, "Wrong chip ID, got %x expected %x\n", 998 + data->check, BME680_CHIP_ID_VAL); 999 + return -ENODEV; 902 1000 } 903 1001 904 1002 ret = bme680_read_calib(data, &data->bme680); 905 1003 if (ret < 0) { 906 - dev_err(dev, 1004 + return dev_err_probe(dev, ret, 907 1005 "failed to read calibration coefficients at probe\n"); 908 - return ret; 909 1006 } 1007 + 1008 + ret = bme680_chip_config(data); 1009 + if (ret < 0) 1010 + return dev_err_probe(dev, ret, 1011 + "failed to set chip_config data\n"); 1012 + 1013 + ret = bme680_gas_config(data); 1014 + if (ret < 0) 1015 + return dev_err_probe(dev, ret, 1016 + "failed to set gas config data\n"); 910 1017 911 1018 return devm_iio_device_register(dev, indio_dev); 912 1019 }
+1 -1
drivers/iio/chemical/bme680_spi.c
··· 100 100 return spi_write_then_read(spi, &addr, 1, val, val_size); 101 101 } 102 102 103 - static struct regmap_bus bme680_regmap_bus = { 103 + static const struct regmap_bus bme680_regmap_bus = { 104 104 .write = bme680_regmap_spi_write, 105 105 .read = bme680_regmap_spi_read, 106 106 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
+8 -3
drivers/iio/chemical/sgp40.c
··· 14 14 * 1) read raw logarithmic resistance value from sensor 15 15 * --> useful to pass it to the algorithm of the sensor vendor for 16 16 * measuring deteriorations and improvements of air quality. 17 + * It can be read from the attribute in_resistance_raw. 17 18 * 18 - * 2) calculate an estimated absolute voc index (0 - 500 index points) for 19 - * measuring the air quality. 19 + * 2) calculate an estimated absolute voc index (in_concentration_input) 20 + * with 0 - 500 index points) for measuring the air quality. 20 21 * For this purpose the value of the resistance for which the voc index 21 - * will be 250 can be set up using calibbias. 22 + * will be 250 can be set up using in_resistance_calibbias (default 30000). 23 + * 24 + * The voc index is calculated as: 25 + * x = (in_resistance_raw - in_resistance_calibbias) * 0.65 26 + * in_concentration_input = 500 / (1 + e^x) 22 27 * 23 28 * Compensation values of relative humidity and temperature can be set up 24 29 * by writing to the out values of temp and humidityrelative.
+3 -5
drivers/iio/common/cros_ec_sensors/cros_ec_sensors_core.c
··· 198 198 return 0; 199 199 200 200 out = (s16 *)st->samples; 201 - for_each_set_bit(i, 202 - indio_dev->active_scan_mask, 203 - indio_dev->masklength) { 201 + iio_for_each_active_channel(indio_dev, i) { 204 202 *out = data[i]; 205 203 out++; 206 204 } ··· 585 587 int ret; 586 588 587 589 /* Read all sensors enabled in scan_mask. Each value is 2 bytes. */ 588 - for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 590 + for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { 589 591 ret = cros_ec_sensors_cmd_read_u16(ec, 590 592 cros_ec_sensors_idx_to_reg(st, i), 591 593 data); ··· 681 683 return ret; 682 684 } 683 685 684 - for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 686 + for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { 685 687 *data = st->resp->data.data[i]; 686 688 data++; 687 689 }
+1 -1
drivers/iio/common/scmi_sensors/scmi_iio.c
··· 158 158 * To calculate the multiplier,we convert the sf into char string and 159 159 * count the number of characters 160 160 */ 161 - sf = (u64)uHz * 0xFFFF; 161 + sf = uHz * 0xFFFF; 162 162 do_div(sf, MICROHZ_PER_HZ); 163 163 mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1; 164 164
+11
drivers/iio/dac/Kconfig
··· 371 371 To compile this driver as a module, choose M here: the 372 372 module will be called ltc2632. 373 373 374 + config LTC2664 375 + tristate "Analog Devices LTC2664 and LTC2672 DAC SPI driver" 376 + depends on SPI 377 + select REGMAP 378 + help 379 + Say yes here to build support for Analog Devices 380 + LTC2664 and LTC2672 converters (DAC). 381 + 382 + To compile this driver as a module, choose M here: the 383 + module will be called ltc2664. 384 + 374 385 config M62332 375 386 tristate "Mitsubishi M62332 DAC driver" 376 387 depends on I2C
+1
drivers/iio/dac/Makefile
··· 37 37 obj-$(CONFIG_LPC18XX_DAC) += lpc18xx_dac.o 38 38 obj-$(CONFIG_LTC1660) += ltc1660.o 39 39 obj-$(CONFIG_LTC2632) += ltc2632.o 40 + obj-$(CONFIG_LTC2664) += ltc2664.o 40 41 obj-$(CONFIG_LTC2688) += ltc2688.o 41 42 obj-$(CONFIG_M62332) += m62332.o 42 43 obj-$(CONFIG_MAX517) += max517.o
+9 -4
drivers/iio/dac/ad9739a.c
··· 145 145 struct ad9739a_state *st = iio_priv(indio_dev); 146 146 147 147 return iio_backend_data_source_set(st->back, 0, 148 - IIO_BACKEND_INTERNAL_CONTINUOS_WAVE); 148 + IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE); 149 149 } 150 150 151 151 static bool ad9739a_reg_accessible(struct device *dev, unsigned int reg) ··· 413 413 if (ret) 414 414 return ret; 415 415 416 - ret = iio_backend_extend_chan_spec(indio_dev, st->back, 417 - &ad9739a_channels[0]); 416 + ret = iio_backend_extend_chan_spec(st->back, &ad9739a_channels[0]); 418 417 if (ret) 419 418 return ret; 420 419 ··· 431 432 indio_dev->num_channels = ARRAY_SIZE(ad9739a_channels); 432 433 indio_dev->setup_ops = &ad9739a_buffer_setup_ops; 433 434 434 - return devm_iio_device_register(&spi->dev, indio_dev); 435 + ret = devm_iio_device_register(&spi->dev, indio_dev); 436 + if (ret) 437 + return ret; 438 + 439 + iio_backend_debugfs_add(st->back, indio_dev); 440 + 441 + return 0; 435 442 } 436 443 437 444 static const struct of_device_id ad9739a_of_match[] = {
+19 -2
drivers/iio/dac/adi-axi-dac.c
··· 452 452 struct axi_dac_state *st = iio_backend_get_priv(back); 453 453 454 454 switch (data) { 455 - case IIO_BACKEND_INTERNAL_CONTINUOS_WAVE: 455 + case IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE: 456 456 return regmap_update_bits(st->regmap, 457 457 AXI_DAC_REG_CHAN_CNTRL_7(chan), 458 458 AXI_DAC_DATA_SEL, ··· 507 507 return 0; 508 508 } 509 509 510 - static const struct iio_backend_ops axi_dac_generic = { 510 + static int axi_dac_reg_access(struct iio_backend *back, unsigned int reg, 511 + unsigned int writeval, unsigned int *readval) 512 + { 513 + struct axi_dac_state *st = iio_backend_get_priv(back); 514 + 515 + if (readval) 516 + return regmap_read(st->regmap, reg, readval); 517 + 518 + return regmap_write(st->regmap, reg, writeval); 519 + } 520 + 521 + static const struct iio_backend_ops axi_dac_generic_ops = { 511 522 .enable = axi_dac_enable, 512 523 .disable = axi_dac_disable, 513 524 .request_buffer = axi_dac_request_buffer, ··· 528 517 .ext_info_get = axi_dac_ext_info_get, 529 518 .data_source_set = axi_dac_data_source_set, 530 519 .set_sample_rate = axi_dac_set_sample_rate, 520 + .debugfs_reg_access = iio_backend_debugfs_ptr(axi_dac_reg_access), 521 + }; 522 + 523 + static const struct iio_backend_info axi_dac_generic = { 524 + .name = "axi-dac", 525 + .ops = &axi_dac_generic_ops, 531 526 }; 532 527 533 528 static const struct regmap_config axi_dac_regmap_config = {
+735
drivers/iio/dac/ltc2664.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * LTC2664 4 channel, 12-/16-Bit Voltage Output SoftSpan DAC driver 4 + * LTC2672 5 channel, 12-/16-Bit Current Output Softspan DAC driver 5 + * 6 + * Copyright 2024 Analog Devices Inc. 7 + */ 8 + 9 + #include <linux/bitfield.h> 10 + #include <linux/cleanup.h> 11 + #include <linux/device.h> 12 + #include <linux/gpio/consumer.h> 13 + #include <linux/iio/iio.h> 14 + #include <linux/kernel.h> 15 + #include <linux/math64.h> 16 + #include <linux/module.h> 17 + #include <linux/mod_devicetable.h> 18 + #include <linux/mutex.h> 19 + #include <linux/property.h> 20 + #include <linux/regmap.h> 21 + #include <linux/regulator/consumer.h> 22 + #include <linux/spi/spi.h> 23 + 24 + #define LTC2664_CMD_WRITE_N(n) (0x00 + (n)) 25 + #define LTC2664_CMD_UPDATE_N(n) (0x10 + (n)) 26 + #define LTC2664_CMD_WRITE_N_UPDATE_ALL 0x20 27 + #define LTC2664_CMD_WRITE_N_UPDATE_N(n) (0x30 + (n)) 28 + #define LTC2664_CMD_POWER_DOWN_N(n) (0x40 + (n)) 29 + #define LTC2664_CMD_POWER_DOWN_ALL 0x50 30 + #define LTC2664_CMD_SPAN_N(n) (0x60 + (n)) 31 + #define LTC2664_CMD_CONFIG 0x70 32 + #define LTC2664_CMD_MUX 0xB0 33 + #define LTC2664_CMD_TOGGLE_SEL 0xC0 34 + #define LTC2664_CMD_GLOBAL_TOGGLE 0xD0 35 + #define LTC2664_CMD_NO_OPERATION 0xF0 36 + #define LTC2664_REF_DISABLE 0x0001 37 + #define LTC2664_MSPAN_SOFTSPAN 7 38 + 39 + #define LTC2672_MAX_CHANNEL 5 40 + #define LTC2672_MAX_SPAN 7 41 + #define LTC2672_SCALE_MULTIPLIER(n) (50 * BIT(n)) 42 + 43 + enum { 44 + LTC2664_SPAN_RANGE_0V_5V, 45 + LTC2664_SPAN_RANGE_0V_10V, 46 + LTC2664_SPAN_RANGE_M5V_5V, 47 + LTC2664_SPAN_RANGE_M10V_10V, 48 + LTC2664_SPAN_RANGE_M2V5_2V5, 49 + }; 50 + 51 + enum { 52 + LTC2664_INPUT_A, 53 + LTC2664_INPUT_B, 54 + LTC2664_INPUT_B_AVAIL, 55 + LTC2664_POWERDOWN, 56 + LTC2664_POWERDOWN_MODE, 57 + LTC2664_TOGGLE_EN, 58 + LTC2664_GLOBAL_TOGGLE, 59 + }; 60 + 61 + static const u16 ltc2664_mspan_lut[8][2] = { 62 + { LTC2664_SPAN_RANGE_M10V_10V, 32768 }, /* MPS2=0, MPS1=0, MSP0=0 (0)*/ 63 + { LTC2664_SPAN_RANGE_M5V_5V, 32768 }, /* MPS2=0, MPS1=0, MSP0=1 (1)*/ 64 + { LTC2664_SPAN_RANGE_M2V5_2V5, 32768 }, /* MPS2=0, MPS1=1, MSP0=0 (2)*/ 65 + { LTC2664_SPAN_RANGE_0V_10V, 0 }, /* MPS2=0, MPS1=1, MSP0=1 (3)*/ 66 + { LTC2664_SPAN_RANGE_0V_10V, 32768 }, /* MPS2=1, MPS1=0, MSP0=0 (4)*/ 67 + { LTC2664_SPAN_RANGE_0V_5V, 0 }, /* MPS2=1, MPS1=0, MSP0=1 (5)*/ 68 + { LTC2664_SPAN_RANGE_0V_5V, 32768 }, /* MPS2=1, MPS1=1, MSP0=0 (6)*/ 69 + { LTC2664_SPAN_RANGE_0V_5V, 0 } /* MPS2=1, MPS1=1, MSP0=1 (7)*/ 70 + }; 71 + 72 + struct ltc2664_state; 73 + 74 + struct ltc2664_chip_info { 75 + const char *name; 76 + int (*scale_get)(const struct ltc2664_state *st, int c); 77 + int (*offset_get)(const struct ltc2664_state *st, int c); 78 + int measurement_type; 79 + unsigned int num_channels; 80 + const int (*span_helper)[2]; 81 + unsigned int num_span; 82 + unsigned int internal_vref_mv; 83 + bool manual_span_support; 84 + bool rfsadj_support; 85 + }; 86 + 87 + struct ltc2664_chan { 88 + /* indicates if the channel should be toggled */ 89 + bool toggle_chan; 90 + /* indicates if the channel is in powered down state */ 91 + bool powerdown; 92 + /* span code of the channel */ 93 + u8 span; 94 + /* raw data of the current state of the chip registers (A/B) */ 95 + u16 raw[2]; 96 + }; 97 + 98 + struct ltc2664_state { 99 + struct spi_device *spi; 100 + struct regmap *regmap; 101 + struct ltc2664_chan channels[LTC2672_MAX_CHANNEL]; 102 + /* lock to protect against multiple access to the device and shared data */ 103 + struct mutex lock; 104 + const struct ltc2664_chip_info *chip_info; 105 + struct iio_chan_spec *iio_channels; 106 + int vref_mv; 107 + u32 rfsadj_ohms; 108 + u32 toggle_sel; 109 + bool global_toggle; 110 + }; 111 + 112 + static const int ltc2664_span_helper[][2] = { 113 + { 0, 5000 }, 114 + { 0, 10000 }, 115 + { -5000, 5000 }, 116 + { -10000, 10000 }, 117 + { -2500, 2500 }, 118 + }; 119 + 120 + static const int ltc2672_span_helper[][2] = { 121 + { 0, 0 }, 122 + { 0, 3125 }, 123 + { 0, 6250 }, 124 + { 0, 12500 }, 125 + { 0, 25000 }, 126 + { 0, 50000 }, 127 + { 0, 100000 }, 128 + { 0, 200000 }, 129 + { 0, 300000 }, 130 + }; 131 + 132 + static int ltc2664_scale_get(const struct ltc2664_state *st, int c) 133 + { 134 + const struct ltc2664_chan *chan = &st->channels[c]; 135 + const int (*span_helper)[2] = st->chip_info->span_helper; 136 + int span, fs; 137 + 138 + span = chan->span; 139 + if (span < 0) 140 + return span; 141 + 142 + fs = span_helper[span][1] - span_helper[span][0]; 143 + 144 + return fs * st->vref_mv / 2500; 145 + } 146 + 147 + static int ltc2672_scale_get(const struct ltc2664_state *st, int c) 148 + { 149 + const struct ltc2664_chan *chan = &st->channels[c]; 150 + int span, fs; 151 + 152 + span = chan->span - 1; 153 + if (span < 0) 154 + return span; 155 + 156 + fs = 1000 * st->vref_mv; 157 + 158 + if (span == LTC2672_MAX_SPAN) 159 + return mul_u64_u32_div(4800, fs, st->rfsadj_ohms); 160 + 161 + return mul_u64_u32_div(LTC2672_SCALE_MULTIPLIER(span), fs, st->rfsadj_ohms); 162 + } 163 + 164 + static int ltc2664_offset_get(const struct ltc2664_state *st, int c) 165 + { 166 + const struct ltc2664_chan *chan = &st->channels[c]; 167 + int span; 168 + 169 + span = chan->span; 170 + if (span < 0) 171 + return span; 172 + 173 + if (st->chip_info->span_helper[span][0] < 0) 174 + return -32768; 175 + 176 + return 0; 177 + } 178 + 179 + static int ltc2664_dac_code_write(struct ltc2664_state *st, u32 chan, u32 input, 180 + u16 code) 181 + { 182 + struct ltc2664_chan *c = &st->channels[chan]; 183 + int ret, reg; 184 + 185 + guard(mutex)(&st->lock); 186 + /* select the correct input register to write to */ 187 + if (c->toggle_chan) { 188 + ret = regmap_write(st->regmap, LTC2664_CMD_TOGGLE_SEL, 189 + input << chan); 190 + if (ret) 191 + return ret; 192 + } 193 + /* 194 + * If in toggle mode the dac should be updated by an 195 + * external signal (or sw toggle) and not here. 196 + */ 197 + if (st->toggle_sel & BIT(chan)) 198 + reg = LTC2664_CMD_WRITE_N(chan); 199 + else 200 + reg = LTC2664_CMD_WRITE_N_UPDATE_N(chan); 201 + 202 + ret = regmap_write(st->regmap, reg, code); 203 + if (ret) 204 + return ret; 205 + 206 + c->raw[input] = code; 207 + 208 + if (c->toggle_chan) { 209 + ret = regmap_write(st->regmap, LTC2664_CMD_TOGGLE_SEL, 210 + st->toggle_sel); 211 + if (ret) 212 + return ret; 213 + } 214 + 215 + return 0; 216 + } 217 + 218 + static void ltc2664_dac_code_read(struct ltc2664_state *st, u32 chan, u32 input, 219 + u32 *code) 220 + { 221 + guard(mutex)(&st->lock); 222 + *code = st->channels[chan].raw[input]; 223 + } 224 + 225 + static const int ltc2664_raw_range[] = { 0, 1, U16_MAX }; 226 + 227 + static int ltc2664_read_avail(struct iio_dev *indio_dev, 228 + struct iio_chan_spec const *chan, 229 + const int **vals, int *type, int *length, 230 + long info) 231 + { 232 + switch (info) { 233 + case IIO_CHAN_INFO_RAW: 234 + *vals = ltc2664_raw_range; 235 + *type = IIO_VAL_INT; 236 + 237 + return IIO_AVAIL_RANGE; 238 + default: 239 + return -EINVAL; 240 + } 241 + } 242 + 243 + static int ltc2664_read_raw(struct iio_dev *indio_dev, 244 + struct iio_chan_spec const *chan, int *val, 245 + int *val2, long info) 246 + { 247 + struct ltc2664_state *st = iio_priv(indio_dev); 248 + 249 + switch (info) { 250 + case IIO_CHAN_INFO_RAW: 251 + ltc2664_dac_code_read(st, chan->channel, LTC2664_INPUT_A, val); 252 + 253 + return IIO_VAL_INT; 254 + case IIO_CHAN_INFO_OFFSET: 255 + *val = st->chip_info->offset_get(st, chan->channel); 256 + 257 + return IIO_VAL_INT; 258 + case IIO_CHAN_INFO_SCALE: 259 + *val = st->chip_info->scale_get(st, chan->channel); 260 + 261 + *val2 = 16; 262 + return IIO_VAL_FRACTIONAL_LOG2; 263 + default: 264 + return -EINVAL; 265 + } 266 + } 267 + 268 + static int ltc2664_write_raw(struct iio_dev *indio_dev, 269 + struct iio_chan_spec const *chan, int val, 270 + int val2, long info) 271 + { 272 + struct ltc2664_state *st = iio_priv(indio_dev); 273 + 274 + switch (info) { 275 + case IIO_CHAN_INFO_RAW: 276 + if (val > U16_MAX || val < 0) 277 + return -EINVAL; 278 + 279 + return ltc2664_dac_code_write(st, chan->channel, 280 + LTC2664_INPUT_A, val); 281 + default: 282 + return -EINVAL; 283 + } 284 + } 285 + 286 + static ssize_t ltc2664_reg_bool_get(struct iio_dev *indio_dev, 287 + uintptr_t private, 288 + const struct iio_chan_spec *chan, 289 + char *buf) 290 + { 291 + struct ltc2664_state *st = iio_priv(indio_dev); 292 + u32 val; 293 + 294 + guard(mutex)(&st->lock); 295 + switch (private) { 296 + case LTC2664_POWERDOWN: 297 + val = st->channels[chan->channel].powerdown; 298 + 299 + return sysfs_emit(buf, "%u\n", val); 300 + case LTC2664_POWERDOWN_MODE: 301 + return sysfs_emit(buf, "42kohm_to_gnd\n"); 302 + case LTC2664_TOGGLE_EN: 303 + val = !!(st->toggle_sel & BIT(chan->channel)); 304 + 305 + return sysfs_emit(buf, "%u\n", val); 306 + case LTC2664_GLOBAL_TOGGLE: 307 + val = st->global_toggle; 308 + 309 + return sysfs_emit(buf, "%u\n", val); 310 + default: 311 + return -EINVAL; 312 + } 313 + } 314 + 315 + static ssize_t ltc2664_reg_bool_set(struct iio_dev *indio_dev, 316 + uintptr_t private, 317 + const struct iio_chan_spec *chan, 318 + const char *buf, size_t len) 319 + { 320 + struct ltc2664_state *st = iio_priv(indio_dev); 321 + int ret; 322 + bool en; 323 + 324 + ret = kstrtobool(buf, &en); 325 + if (ret) 326 + return ret; 327 + 328 + guard(mutex)(&st->lock); 329 + switch (private) { 330 + case LTC2664_POWERDOWN: 331 + ret = regmap_write(st->regmap, 332 + en ? LTC2664_CMD_POWER_DOWN_N(chan->channel) : 333 + LTC2664_CMD_UPDATE_N(chan->channel), en); 334 + if (ret) 335 + return ret; 336 + 337 + st->channels[chan->channel].powerdown = en; 338 + 339 + return len; 340 + case LTC2664_TOGGLE_EN: 341 + if (en) 342 + st->toggle_sel |= BIT(chan->channel); 343 + else 344 + st->toggle_sel &= ~BIT(chan->channel); 345 + 346 + ret = regmap_write(st->regmap, LTC2664_CMD_TOGGLE_SEL, 347 + st->toggle_sel); 348 + if (ret) 349 + return ret; 350 + 351 + return len; 352 + case LTC2664_GLOBAL_TOGGLE: 353 + ret = regmap_write(st->regmap, LTC2664_CMD_GLOBAL_TOGGLE, en); 354 + if (ret) 355 + return ret; 356 + 357 + st->global_toggle = en; 358 + 359 + return len; 360 + default: 361 + return -EINVAL; 362 + } 363 + } 364 + 365 + static ssize_t ltc2664_dac_input_read(struct iio_dev *indio_dev, 366 + uintptr_t private, 367 + const struct iio_chan_spec *chan, 368 + char *buf) 369 + { 370 + struct ltc2664_state *st = iio_priv(indio_dev); 371 + u32 val; 372 + 373 + if (private == LTC2664_INPUT_B_AVAIL) 374 + return sysfs_emit(buf, "[%u %u %u]\n", ltc2664_raw_range[0], 375 + ltc2664_raw_range[1], 376 + ltc2664_raw_range[2] / 4); 377 + 378 + ltc2664_dac_code_read(st, chan->channel, private, &val); 379 + 380 + return sysfs_emit(buf, "%u\n", val); 381 + } 382 + 383 + static ssize_t ltc2664_dac_input_write(struct iio_dev *indio_dev, 384 + uintptr_t private, 385 + const struct iio_chan_spec *chan, 386 + const char *buf, size_t len) 387 + { 388 + struct ltc2664_state *st = iio_priv(indio_dev); 389 + int ret; 390 + u16 val; 391 + 392 + if (private == LTC2664_INPUT_B_AVAIL) 393 + return -EINVAL; 394 + 395 + ret = kstrtou16(buf, 10, &val); 396 + if (ret) 397 + return ret; 398 + 399 + ret = ltc2664_dac_code_write(st, chan->channel, private, val); 400 + if (ret) 401 + return ret; 402 + 403 + return len; 404 + } 405 + 406 + static int ltc2664_reg_access(struct iio_dev *indio_dev, 407 + unsigned int reg, 408 + unsigned int writeval, 409 + unsigned int *readval) 410 + { 411 + struct ltc2664_state *st = iio_priv(indio_dev); 412 + 413 + if (readval) 414 + return -EOPNOTSUPP; 415 + 416 + return regmap_write(st->regmap, reg, writeval); 417 + } 418 + 419 + #define LTC2664_CHAN_EXT_INFO(_name, _what, _shared, _read, _write) { \ 420 + .name = _name, \ 421 + .read = (_read), \ 422 + .write = (_write), \ 423 + .private = (_what), \ 424 + .shared = (_shared), \ 425 + } 426 + 427 + /* 428 + * For toggle mode we only expose the symbol attr (sw_toggle) in case a TGPx is 429 + * not provided in dts. 430 + */ 431 + static const struct iio_chan_spec_ext_info ltc2664_toggle_sym_ext_info[] = { 432 + LTC2664_CHAN_EXT_INFO("raw0", LTC2664_INPUT_A, IIO_SEPARATE, 433 + ltc2664_dac_input_read, ltc2664_dac_input_write), 434 + LTC2664_CHAN_EXT_INFO("raw1", LTC2664_INPUT_B, IIO_SEPARATE, 435 + ltc2664_dac_input_read, ltc2664_dac_input_write), 436 + LTC2664_CHAN_EXT_INFO("powerdown", LTC2664_POWERDOWN, IIO_SEPARATE, 437 + ltc2664_reg_bool_get, ltc2664_reg_bool_set), 438 + LTC2664_CHAN_EXT_INFO("powerdown_mode", LTC2664_POWERDOWN_MODE, 439 + IIO_SEPARATE, ltc2664_reg_bool_get, NULL), 440 + LTC2664_CHAN_EXT_INFO("symbol", LTC2664_GLOBAL_TOGGLE, IIO_SEPARATE, 441 + ltc2664_reg_bool_get, ltc2664_reg_bool_set), 442 + LTC2664_CHAN_EXT_INFO("toggle_en", LTC2664_TOGGLE_EN, 443 + IIO_SEPARATE, ltc2664_reg_bool_get, 444 + ltc2664_reg_bool_set), 445 + { } 446 + }; 447 + 448 + static const struct iio_chan_spec_ext_info ltc2664_ext_info[] = { 449 + LTC2664_CHAN_EXT_INFO("powerdown", LTC2664_POWERDOWN, IIO_SEPARATE, 450 + ltc2664_reg_bool_get, ltc2664_reg_bool_set), 451 + LTC2664_CHAN_EXT_INFO("powerdown_mode", LTC2664_POWERDOWN_MODE, 452 + IIO_SEPARATE, ltc2664_reg_bool_get, NULL), 453 + { } 454 + }; 455 + 456 + static const struct iio_chan_spec ltc2664_channel_template = { 457 + .indexed = 1, 458 + .output = 1, 459 + .info_mask_separate = BIT(IIO_CHAN_INFO_SCALE) | 460 + BIT(IIO_CHAN_INFO_OFFSET) | 461 + BIT(IIO_CHAN_INFO_RAW), 462 + .info_mask_separate_available = BIT(IIO_CHAN_INFO_RAW), 463 + .ext_info = ltc2664_ext_info, 464 + }; 465 + 466 + static const struct ltc2664_chip_info ltc2664_chip = { 467 + .name = "ltc2664", 468 + .scale_get = ltc2664_scale_get, 469 + .offset_get = ltc2664_offset_get, 470 + .measurement_type = IIO_VOLTAGE, 471 + .num_channels = 4, 472 + .span_helper = ltc2664_span_helper, 473 + .num_span = ARRAY_SIZE(ltc2664_span_helper), 474 + .internal_vref_mv = 2500, 475 + .manual_span_support = true, 476 + .rfsadj_support = false, 477 + }; 478 + 479 + static const struct ltc2664_chip_info ltc2672_chip = { 480 + .name = "ltc2672", 481 + .scale_get = ltc2672_scale_get, 482 + .offset_get = ltc2664_offset_get, 483 + .measurement_type = IIO_CURRENT, 484 + .num_channels = 5, 485 + .span_helper = ltc2672_span_helper, 486 + .num_span = ARRAY_SIZE(ltc2672_span_helper), 487 + .internal_vref_mv = 1250, 488 + .manual_span_support = false, 489 + .rfsadj_support = true, 490 + }; 491 + 492 + static int ltc2664_set_span(const struct ltc2664_state *st, int min, int max, 493 + int chan) 494 + { 495 + const struct ltc2664_chip_info *chip_info = st->chip_info; 496 + const int (*span_helper)[2] = chip_info->span_helper; 497 + int span, ret; 498 + 499 + for (span = 0; span < chip_info->num_span; span++) { 500 + if (min == span_helper[span][0] && max == span_helper[span][1]) 501 + break; 502 + } 503 + 504 + if (span == chip_info->num_span) 505 + return -EINVAL; 506 + 507 + ret = regmap_write(st->regmap, LTC2664_CMD_SPAN_N(chan), span); 508 + if (ret) 509 + return ret; 510 + 511 + return span; 512 + } 513 + 514 + static int ltc2664_channel_config(struct ltc2664_state *st) 515 + { 516 + const struct ltc2664_chip_info *chip_info = st->chip_info; 517 + struct device *dev = &st->spi->dev; 518 + u32 reg, tmp[2], mspan; 519 + int ret, span = 0; 520 + 521 + mspan = LTC2664_MSPAN_SOFTSPAN; 522 + ret = device_property_read_u32(dev, "adi,manual-span-operation-config", 523 + &mspan); 524 + if (!ret) { 525 + if (!chip_info->manual_span_support) 526 + return dev_err_probe(dev, -EINVAL, 527 + "adi,manual-span-operation-config not supported\n"); 528 + 529 + if (mspan >= ARRAY_SIZE(ltc2664_mspan_lut)) 530 + return dev_err_probe(dev, -EINVAL, 531 + "adi,manual-span-operation-config not in range\n"); 532 + } 533 + 534 + st->rfsadj_ohms = 20000; 535 + ret = device_property_read_u32(dev, "adi,rfsadj-ohms", &st->rfsadj_ohms); 536 + if (!ret) { 537 + if (!chip_info->rfsadj_support) 538 + return dev_err_probe(dev, -EINVAL, 539 + "adi,rfsadj-ohms not supported\n"); 540 + 541 + if (st->rfsadj_ohms < 19000 || st->rfsadj_ohms > 41000) 542 + return dev_err_probe(dev, -EINVAL, 543 + "adi,rfsadj-ohms not in range\n"); 544 + } 545 + 546 + device_for_each_child_node_scoped(dev, child) { 547 + struct ltc2664_chan *chan; 548 + 549 + ret = fwnode_property_read_u32(child, "reg", &reg); 550 + if (ret) 551 + return dev_err_probe(dev, ret, 552 + "Failed to get reg property\n"); 553 + 554 + if (reg >= chip_info->num_channels) 555 + return dev_err_probe(dev, -EINVAL, 556 + "reg bigger than: %d\n", 557 + chip_info->num_channels); 558 + 559 + chan = &st->channels[reg]; 560 + 561 + if (fwnode_property_read_bool(child, "adi,toggle-mode")) { 562 + chan->toggle_chan = true; 563 + /* assume sw toggle ABI */ 564 + st->iio_channels[reg].ext_info = ltc2664_toggle_sym_ext_info; 565 + 566 + /* 567 + * Clear IIO_CHAN_INFO_RAW bit as toggle channels expose 568 + * out_voltage/current_raw{0|1} files. 569 + */ 570 + __clear_bit(IIO_CHAN_INFO_RAW, 571 + &st->iio_channels[reg].info_mask_separate); 572 + } 573 + 574 + chan->raw[0] = ltc2664_mspan_lut[mspan][1]; 575 + chan->raw[1] = ltc2664_mspan_lut[mspan][1]; 576 + 577 + chan->span = ltc2664_mspan_lut[mspan][0]; 578 + 579 + ret = fwnode_property_read_u32_array(child, "output-range-microvolt", 580 + tmp, ARRAY_SIZE(tmp)); 581 + if (!ret && mspan == LTC2664_MSPAN_SOFTSPAN) { 582 + chan->span = ltc2664_set_span(st, tmp[0] / 1000, 583 + tmp[1] / 1000, reg); 584 + if (span < 0) 585 + return dev_err_probe(dev, span, 586 + "Failed to set span\n"); 587 + } 588 + 589 + ret = fwnode_property_read_u32_array(child, "output-range-microamp", 590 + tmp, ARRAY_SIZE(tmp)); 591 + if (!ret) { 592 + chan->span = ltc2664_set_span(st, 0, tmp[1] / 1000, reg); 593 + if (span < 0) 594 + return dev_err_probe(dev, span, 595 + "Failed to set span\n"); 596 + } 597 + } 598 + 599 + return 0; 600 + } 601 + 602 + static int ltc2664_setup(struct ltc2664_state *st) 603 + { 604 + const struct ltc2664_chip_info *chip_info = st->chip_info; 605 + struct gpio_desc *gpio; 606 + int ret, i; 607 + 608 + /* If we have a clr/reset pin, use that to reset the chip. */ 609 + gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH); 610 + if (IS_ERR(gpio)) 611 + return dev_err_probe(&st->spi->dev, PTR_ERR(gpio), 612 + "Failed to get reset gpio"); 613 + if (gpio) { 614 + fsleep(1000); 615 + gpiod_set_value_cansleep(gpio, 0); 616 + } 617 + 618 + /* 619 + * Duplicate the default channel configuration as it can change during 620 + * @ltc2664_channel_config() 621 + */ 622 + st->iio_channels = devm_kcalloc(&st->spi->dev, 623 + chip_info->num_channels, 624 + sizeof(struct iio_chan_spec), 625 + GFP_KERNEL); 626 + if (!st->iio_channels) 627 + return -ENOMEM; 628 + 629 + for (i = 0; i < chip_info->num_channels; i++) { 630 + st->iio_channels[i] = ltc2664_channel_template; 631 + st->iio_channels[i].type = chip_info->measurement_type; 632 + st->iio_channels[i].channel = i; 633 + } 634 + 635 + ret = ltc2664_channel_config(st); 636 + if (ret) 637 + return ret; 638 + 639 + return regmap_set_bits(st->regmap, LTC2664_CMD_CONFIG, LTC2664_REF_DISABLE); 640 + } 641 + 642 + static const struct regmap_config ltc2664_regmap_config = { 643 + .reg_bits = 8, 644 + .val_bits = 16, 645 + .max_register = LTC2664_CMD_NO_OPERATION, 646 + }; 647 + 648 + static const struct iio_info ltc2664_info = { 649 + .write_raw = ltc2664_write_raw, 650 + .read_raw = ltc2664_read_raw, 651 + .read_avail = ltc2664_read_avail, 652 + .debugfs_reg_access = ltc2664_reg_access, 653 + }; 654 + 655 + static int ltc2664_probe(struct spi_device *spi) 656 + { 657 + static const char * const regulators[] = { "vcc", "iovcc", "v-neg" }; 658 + const struct ltc2664_chip_info *chip_info; 659 + struct device *dev = &spi->dev; 660 + struct iio_dev *indio_dev; 661 + struct ltc2664_state *st; 662 + int ret; 663 + 664 + indio_dev = devm_iio_device_alloc(dev, sizeof(*st)); 665 + if (!indio_dev) 666 + return -ENOMEM; 667 + 668 + st = iio_priv(indio_dev); 669 + st->spi = spi; 670 + 671 + chip_info = spi_get_device_match_data(spi); 672 + if (!chip_info) 673 + return -ENODEV; 674 + 675 + st->chip_info = chip_info; 676 + 677 + mutex_init(&st->lock); 678 + 679 + st->regmap = devm_regmap_init_spi(spi, &ltc2664_regmap_config); 680 + if (IS_ERR(st->regmap)) 681 + return dev_err_probe(dev, PTR_ERR(st->regmap), 682 + "Failed to init regmap"); 683 + 684 + ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators), 685 + regulators); 686 + if (ret) 687 + return dev_err_probe(dev, ret, "Failed to enable regulators\n"); 688 + 689 + ret = devm_regulator_get_enable_read_voltage(dev, "ref"); 690 + if (ret < 0 && ret != -ENODEV) 691 + return ret; 692 + 693 + st->vref_mv = ret > 0 ? ret / 1000 : chip_info->internal_vref_mv; 694 + 695 + ret = ltc2664_setup(st); 696 + if (ret) 697 + return ret; 698 + 699 + indio_dev->name = chip_info->name; 700 + indio_dev->info = &ltc2664_info; 701 + indio_dev->modes = INDIO_DIRECT_MODE; 702 + indio_dev->channels = st->iio_channels; 703 + indio_dev->num_channels = chip_info->num_channels; 704 + 705 + return devm_iio_device_register(dev, indio_dev); 706 + } 707 + 708 + static const struct spi_device_id ltc2664_id[] = { 709 + { "ltc2664", (kernel_ulong_t)&ltc2664_chip }, 710 + { "ltc2672", (kernel_ulong_t)&ltc2672_chip }, 711 + { } 712 + }; 713 + MODULE_DEVICE_TABLE(spi, ltc2664_id); 714 + 715 + static const struct of_device_id ltc2664_of_id[] = { 716 + { .compatible = "adi,ltc2664", .data = &ltc2664_chip }, 717 + { .compatible = "adi,ltc2672", .data = &ltc2672_chip }, 718 + { } 719 + }; 720 + MODULE_DEVICE_TABLE(of, ltc2664_of_id); 721 + 722 + static struct spi_driver ltc2664_driver = { 723 + .driver = { 724 + .name = "ltc2664", 725 + .of_match_table = ltc2664_of_id, 726 + }, 727 + .probe = ltc2664_probe, 728 + .id_table = ltc2664_id, 729 + }; 730 + module_spi_driver(ltc2664_driver); 731 + 732 + MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 733 + MODULE_AUTHOR("Kim Seer Paller <kimseer.paller@analog.com>"); 734 + MODULE_DESCRIPTION("Analog Devices LTC2664 and LTC2672 DAC"); 735 + MODULE_LICENSE("GPL");
+1 -1
drivers/iio/dac/ltc2688.c
··· 918 918 return false; 919 919 } 920 920 921 - static struct regmap_bus ltc2688_regmap_bus = { 921 + static const struct regmap_bus ltc2688_regmap_bus = { 922 922 .read = ltc2688_spi_read, 923 923 .write = ltc2688_spi_write, 924 924 .read_flag_mask = LTC2688_READ_OPERATION,
+13 -32
drivers/iio/dac/mcp4728.c
··· 84 84 85 85 struct mcp4728_data { 86 86 struct i2c_client *client; 87 - struct regulator *vdd_reg; 88 87 bool powerdown; 89 88 int scales_avail[MCP4728_N_SCALES * 2]; 90 89 struct mcp4728_channel_data chdata[MCP4728_N_CHANNELS]; ··· 414 415 data->scales_avail[scale * 2 + 1] = value_micro; 415 416 } 416 417 417 - static int mcp4728_init_scales_avail(struct mcp4728_data *data) 418 + static int mcp4728_init_scales_avail(struct mcp4728_data *data, int vdd_mv) 418 419 { 419 - int ret; 420 - 421 - ret = regulator_get_voltage(data->vdd_reg); 422 - if (ret < 0) 423 - return ret; 424 - 425 - mcp4728_init_scale_avail(MCP4728_SCALE_VDD, ret / 1000, data); 420 + mcp4728_init_scale_avail(MCP4728_SCALE_VDD, vdd_mv, data); 426 421 mcp4728_init_scale_avail(MCP4728_SCALE_VINT_NO_GAIN, 2048, data); 427 422 mcp4728_init_scale_avail(MCP4728_SCALE_VINT_GAIN_X2, 4096, data); 428 423 ··· 523 530 return 0; 524 531 } 525 532 526 - static void mcp4728_reg_disable(void *reg) 527 - { 528 - regulator_disable(reg); 529 - } 530 - 531 533 static int mcp4728_probe(struct i2c_client *client) 532 534 { 533 535 const struct i2c_device_id *id = i2c_client_get_device_id(client); 534 536 struct mcp4728_data *data; 535 537 struct iio_dev *indio_dev; 536 - int err; 538 + int ret, vdd_mv; 537 539 538 540 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 539 541 if (!indio_dev) ··· 538 550 i2c_set_clientdata(client, indio_dev); 539 551 data->client = client; 540 552 541 - data->vdd_reg = devm_regulator_get(&client->dev, "vdd"); 542 - if (IS_ERR(data->vdd_reg)) 543 - return PTR_ERR(data->vdd_reg); 553 + ret = devm_regulator_get_enable_read_voltage(&client->dev, "vdd"); 554 + if (ret < 0) 555 + return ret; 544 556 545 - err = regulator_enable(data->vdd_reg); 546 - if (err) 547 - return err; 548 - 549 - err = devm_add_action_or_reset(&client->dev, mcp4728_reg_disable, 550 - data->vdd_reg); 551 - if (err) 552 - return err; 557 + vdd_mv = ret / 1000; 553 558 554 559 /* 555 560 * MCP4728 has internal EEPROM that save each channel boot ··· 550 569 * driver at kernel boot. mcp4728_init_channels_data() reads back DAC 551 570 * settings and stores them in data structure. 552 571 */ 553 - err = mcp4728_init_channels_data(data); 554 - if (err) { 555 - return dev_err_probe(&client->dev, err, 572 + ret = mcp4728_init_channels_data(data); 573 + if (ret) { 574 + return dev_err_probe(&client->dev, ret, 556 575 "failed to read mcp4728 current configuration\n"); 557 576 } 558 577 559 - err = mcp4728_init_scales_avail(data); 560 - if (err) { 561 - return dev_err_probe(&client->dev, err, 578 + ret = mcp4728_init_scales_avail(data, vdd_mv); 579 + if (ret) { 580 + return dev_err_probe(&client->dev, ret, 562 581 "failed to init scales\n"); 563 582 } 564 583
+6 -39
drivers/iio/dac/mcp4922.c
··· 30 30 struct spi_device *spi; 31 31 unsigned int value[MCP4922_NUM_CHANNELS]; 32 32 unsigned int vref_mv; 33 - struct regulator *vref_reg; 34 33 u8 mosi[2] __aligned(IIO_DMA_MINALIGN); 35 34 }; 36 35 ··· 131 132 132 133 state = iio_priv(indio_dev); 133 134 state->spi = spi; 134 - state->vref_reg = devm_regulator_get(&spi->dev, "vref"); 135 - if (IS_ERR(state->vref_reg)) 136 - return dev_err_probe(&spi->dev, PTR_ERR(state->vref_reg), 137 - "Vref regulator not specified\n"); 138 135 139 - ret = regulator_enable(state->vref_reg); 140 - if (ret) { 141 - dev_err(&spi->dev, "Failed to enable vref regulator: %d\n", 142 - ret); 143 - return ret; 144 - } 136 + ret = devm_regulator_get_enable_read_voltage(&spi->dev, "vref"); 137 + if (ret < 0) 138 + return dev_err_probe(&spi->dev, ret, "Failed to get vref voltage\n"); 145 139 146 - ret = regulator_get_voltage(state->vref_reg); 147 - if (ret < 0) { 148 - dev_err(&spi->dev, "Failed to read vref regulator: %d\n", 149 - ret); 150 - goto error_disable_reg; 151 - } 152 140 state->vref_mv = ret / 1000; 153 141 154 - spi_set_drvdata(spi, indio_dev); 155 142 id = spi_get_device_id(spi); 156 143 indio_dev->info = &mcp4922_info; 157 144 indio_dev->modes = INDIO_DIRECT_MODE; ··· 148 163 indio_dev->num_channels = MCP4922_NUM_CHANNELS; 149 164 indio_dev->name = id->name; 150 165 151 - ret = iio_device_register(indio_dev); 152 - if (ret) { 153 - dev_err(&spi->dev, "Failed to register iio device: %d\n", 154 - ret); 155 - goto error_disable_reg; 156 - } 166 + ret = devm_iio_device_register(&spi->dev, indio_dev); 167 + if (ret) 168 + return dev_err_probe(&spi->dev, ret, "Failed to register iio device\n"); 157 169 158 170 return 0; 159 - 160 - error_disable_reg: 161 - regulator_disable(state->vref_reg); 162 - 163 - return ret; 164 171 } 165 172 166 - static void mcp4922_remove(struct spi_device *spi) 167 - { 168 - struct iio_dev *indio_dev = spi_get_drvdata(spi); 169 - struct mcp4922_state *state; 170 - 171 - iio_device_unregister(indio_dev); 172 - state = iio_priv(indio_dev); 173 - regulator_disable(state->vref_reg); 174 - } 175 173 176 174 static const struct spi_device_id mcp4922_id[] = { 177 175 {"mcp4902", ID_MCP4902}, ··· 170 202 .name = "mcp4922", 171 203 }, 172 204 .probe = mcp4922_probe, 173 - .remove = mcp4922_remove, 174 205 .id_table = mcp4922_id, 175 206 }; 176 207 module_spi_driver(mcp4922_driver);
+3 -1
drivers/iio/dac/ti-dac7311.c
··· 249 249 250 250 spi->mode = SPI_MODE_1; 251 251 spi->bits_per_word = 16; 252 - spi_setup(spi); 252 + ret = spi_setup(spi); 253 + if (ret < 0) 254 + return dev_err_probe(dev, ret, "spi_setup failed\n"); 253 255 254 256 indio_dev->info = &ti_dac_info; 255 257 indio_dev->name = spi_get_device_id(spi)->name;
+1 -1
drivers/iio/dummy/iio_simple_dummy_buffer.c
··· 68 68 * Here let's pretend we have random access. And the values are in the 69 69 * constant table fakedata. 70 70 */ 71 - for_each_set_bit(j, indio_dev->active_scan_mask, indio_dev->masklength) 71 + iio_for_each_active_channel(indio_dev, j) 72 72 data[i++] = fakedata[j]; 73 73 74 74 iio_push_to_buffers_with_timestamp(indio_dev, data,
+28 -7
drivers/iio/frequency/adf4377.c
··· 400 400 ADF4377_MUXOUT_HIGH = 0x8, 401 401 }; 402 402 403 + struct adf4377_chip_info { 404 + const char *name; 405 + bool has_gpio_enclk2; 406 + }; 407 + 403 408 struct adf4377_state { 409 + const struct adf4377_chip_info *chip_info; 404 410 struct spi_device *spi; 405 411 struct regmap *regmap; 406 412 struct clk *clkin; ··· 895 889 return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_enclk1), 896 890 "failed to get the CE GPIO\n"); 897 891 898 - st->gpio_enclk2 = devm_gpiod_get_optional(&st->spi->dev, "clk2-enable", 899 - GPIOD_OUT_LOW); 900 - if (IS_ERR(st->gpio_enclk2)) 901 - return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_enclk2), 902 - "failed to get the CE GPIO\n"); 892 + if (st->chip_info->has_gpio_enclk2) { 893 + st->gpio_enclk2 = devm_gpiod_get_optional(&st->spi->dev, "clk2-enable", 894 + GPIOD_OUT_LOW); 895 + if (IS_ERR(st->gpio_enclk2)) 896 + return dev_err_probe(&spi->dev, PTR_ERR(st->gpio_enclk2), 897 + "failed to get the CE GPIO\n"); 898 + } 903 899 904 900 ret = device_property_match_property_string(&spi->dev, "adi,muxout-select", 905 901 adf4377_muxout_modes, ··· 929 921 return NOTIFY_OK; 930 922 } 931 923 924 + static const struct adf4377_chip_info adf4377_chip_info = { 925 + .name = "adf4377", 926 + .has_gpio_enclk2 = true, 927 + }; 928 + 929 + static const struct adf4377_chip_info adf4378_chip_info = { 930 + .name = "adf4378", 931 + .has_gpio_enclk2 = false, 932 + }; 933 + 932 934 static int adf4377_probe(struct spi_device *spi) 933 935 { 934 936 struct iio_dev *indio_dev; ··· 963 945 964 946 st->regmap = regmap; 965 947 st->spi = spi; 948 + st->chip_info = spi_get_device_match_data(spi); 966 949 mutex_init(&st->lock); 967 950 968 951 ret = adf4377_properties_parse(st); ··· 983 964 } 984 965 985 966 static const struct spi_device_id adf4377_id[] = { 986 - { "adf4377", 0 }, 967 + { "adf4377", (kernel_ulong_t)&adf4377_chip_info }, 968 + { "adf4378", (kernel_ulong_t)&adf4378_chip_info }, 987 969 {} 988 970 }; 989 971 MODULE_DEVICE_TABLE(spi, adf4377_id); 990 972 991 973 static const struct of_device_id adf4377_of_match[] = { 992 - { .compatible = "adi,adf4377" }, 974 + { .compatible = "adi,adf4377", .data = &adf4377_chip_info }, 975 + { .compatible = "adi,adf4378", .data = &adf4378_chip_info }, 993 976 {} 994 977 }; 995 978 MODULE_DEVICE_TABLE(of, adf4377_of_match);
+1 -2
drivers/iio/health/afe4403.c
··· 321 321 if (ret) 322 322 goto err; 323 323 324 - for_each_set_bit(bit, indio_dev->active_scan_mask, 325 - indio_dev->masklength) { 324 + iio_for_each_active_channel(indio_dev, bit) { 326 325 ret = spi_write_then_read(afe->spi, 327 326 &afe4403_channel_values[bit], 1, 328 327 rx, sizeof(rx));
+1 -2
drivers/iio/health/afe4404.c
··· 333 333 struct afe4404_data *afe = iio_priv(indio_dev); 334 334 int ret, bit, i = 0; 335 335 336 - for_each_set_bit(bit, indio_dev->active_scan_mask, 337 - indio_dev->masklength) { 336 + iio_for_each_active_channel(indio_dev, bit) { 338 337 ret = regmap_read(afe->regmap, afe4404_channel_values[bit], 339 338 &afe->buffer[i++]); 340 339 if (ret)
+1 -1
drivers/iio/health/max30102.c
··· 293 293 struct iio_dev *indio_dev = private; 294 294 struct max30102_data *data = iio_priv(indio_dev); 295 295 unsigned int measurements = bitmap_weight(indio_dev->active_scan_mask, 296 - indio_dev->masklength); 296 + iio_get_masklength(indio_dev)); 297 297 int ret, cnt = 0; 298 298 299 299 mutex_lock(&data->lock);
+11
drivers/iio/humidity/Kconfig
··· 25 25 Other sensors should work as well as long as they speak the 26 26 same protocol. 27 27 28 + config ENS210 29 + tristate "ENS210 temperature and humidity sensor" 30 + depends on I2C 31 + select CRC7 32 + help 33 + Say yes here to get support for the ScioSense ENS210 family of 34 + humidity and temperature sensors. 35 + 36 + This driver can also be built as a module. If so, the module will be 37 + called ens210. 38 + 28 39 config HDC100X 29 40 tristate "TI HDC100x relative humidity and temperature sensor" 30 41 depends on I2C
+1
drivers/iio/humidity/Makefile
··· 5 5 6 6 obj-$(CONFIG_AM2315) += am2315.o 7 7 obj-$(CONFIG_DHT11) += dht11.o 8 + obj-$(CONFIG_ENS210) += ens210.o 8 9 obj-$(CONFIG_HDC100X) += hdc100x.o 9 10 obj-$(CONFIG_HDC2010) += hdc2010.o 10 11 obj-$(CONFIG_HDC3020) += hdc3020.o
+1 -2
drivers/iio/humidity/am2315.c
··· 174 174 data->scan.chans[1] = sensor_data.temp_data; 175 175 } else { 176 176 i = 0; 177 - for_each_set_bit(bit, indio_dev->active_scan_mask, 178 - indio_dev->masklength) { 177 + iio_for_each_active_channel(indio_dev, bit) { 179 178 data->scan.chans[i] = (bit ? sensor_data.temp_data : 180 179 sensor_data.hum_data); 181 180 i++;
+339
drivers/iio/humidity/ens210.c
··· 1 + // SPDX-License-Identifier: GPL-2.0+ 2 + /* 3 + * ens210.c - Support for ScioSense ens210 temperature & humidity sensor family 4 + * 5 + * (7-bit I2C slave address 0x43 ENS210) 6 + * (7-bit I2C slave address 0x43 ENS210A) 7 + * (7-bit I2C slave address 0x44 ENS211) 8 + * (7-bit I2C slave address 0x45 ENS212) 9 + * (7-bit I2C slave address 0x46 ENS213A) 10 + * (7-bit I2C slave address 0x47 ENS215) 11 + * 12 + * Datasheet: 13 + * https://www.sciosense.com/wp-content/uploads/2024/04/ENS21x-Datasheet.pdf 14 + * https://www.sciosense.com/wp-content/uploads/2023/12/ENS210-Datasheet.pdf 15 + */ 16 + 17 + #include <linux/crc7.h> 18 + #include <linux/delay.h> 19 + #include <linux/i2c.h> 20 + #include <linux/iio/iio.h> 21 + #include <linux/mod_devicetable.h> 22 + #include <linux/module.h> 23 + #include <linux/types.h> 24 + 25 + #include <asm/unaligned.h> 26 + 27 + /* register definitions */ 28 + #define ENS210_REG_PART_ID 0x00 29 + #define ENS210_REG_DIE_REV 0x02 30 + #define ENS210_REG_UID 0x04 31 + #define ENS210_REG_SYS_CTRL 0x10 32 + #define ENS210_REG_SYS_STAT 0x11 33 + #define ENS210_REG_SENS_RUN 0x21 34 + #define ENS210_REG_SENS_START 0x22 35 + #define ENS210_REG_SENS_STOP 0x23 36 + #define ENS210_REG_SENS_STAT 0x24 37 + #define ENS210_REG_T_VAL 0x30 38 + #define ENS210_REG_H_VAL 0x33 39 + 40 + /* value definitions */ 41 + #define ENS210_SENS_START_T_START BIT(0) 42 + #define ENS210_SENS_START_H_START BIT(1) 43 + 44 + #define ENS210_SENS_STAT_T_ACTIVE BIT(0) 45 + #define ENS210_SENS_STAT_H_ACTIVE BIT(1) 46 + 47 + #define ENS210_SYS_CTRL_LOW_POWER_ENABLE BIT(0) 48 + #define ENS210_SYS_CTRL_SYS_RESET BIT(7) 49 + 50 + #define ENS210_SYS_STAT_SYS_ACTIVE BIT(0) 51 + 52 + enum ens210_partnumber { 53 + ENS210 = 0x0210, 54 + ENS210A = 0xa210, 55 + ENS211 = 0x0211, 56 + ENS212 = 0x0212, 57 + ENS213A = 0xa213, 58 + ENS215 = 0x0215, 59 + }; 60 + 61 + /** 62 + * struct ens210_chip_info - Humidity/Temperature chip specific information 63 + * @name: name of device 64 + * @part_id: chip identifier 65 + * @conv_time_msec: time for conversion calculation in m/s 66 + */ 67 + struct ens210_chip_info { 68 + const char *name; 69 + enum ens210_partnumber part_id; 70 + unsigned int conv_time_msec; 71 + }; 72 + 73 + /** 74 + * struct ens210_data - Humidity/Temperature sensor device structure 75 + * @client: i2c client 76 + * @chip_info: chip specific information 77 + * @lock: lock protecting against simultaneous callers of get_measurement 78 + * since multiple uninterrupted transactions are required 79 + */ 80 + struct ens210_data { 81 + struct i2c_client *client; 82 + const struct ens210_chip_info *chip_info; 83 + struct mutex lock; 84 + }; 85 + 86 + /* calculate 17-bit crc7 */ 87 + static u8 ens210_crc7(u32 val) 88 + { 89 + unsigned int val_be = (val & 0x1ffff) >> 0x8; 90 + 91 + return crc7_be(0xde, (u8 *)&val_be, 3) >> 1; 92 + } 93 + 94 + static int ens210_get_measurement(struct iio_dev *indio_dev, bool temp, int *val) 95 + { 96 + struct ens210_data *data = iio_priv(indio_dev); 97 + struct device *dev = &data->client->dev; 98 + u32 regval; 99 + u8 regval_le[3]; 100 + int ret; 101 + 102 + /* assert read */ 103 + ret = i2c_smbus_write_byte_data(data->client, ENS210_REG_SENS_START, 104 + temp ? ENS210_SENS_START_T_START : 105 + ENS210_SENS_START_H_START); 106 + if (ret) 107 + return ret; 108 + 109 + /* wait for conversion to be ready */ 110 + msleep(data->chip_info->conv_time_msec); 111 + 112 + ret = i2c_smbus_read_byte_data(data->client, ENS210_REG_SENS_STAT); 113 + if (ret < 0) 114 + return ret; 115 + 116 + /* perform read */ 117 + ret = i2c_smbus_read_i2c_block_data( 118 + data->client, temp ? ENS210_REG_T_VAL : ENS210_REG_H_VAL, 3, 119 + regval_le); 120 + if (ret < 0) { 121 + dev_err(dev, "failed to read register"); 122 + return -EIO; 123 + } 124 + if (ret != 3) { 125 + dev_err(dev, "expected 3 bytes, received %d\n", ret); 126 + return -EIO; 127 + } 128 + 129 + regval = get_unaligned_le24(regval_le); 130 + if (ens210_crc7(regval) != ((regval >> 17) & 0x7f)) { 131 + dev_err(dev, "invalid crc\n"); 132 + return -EIO; 133 + } 134 + 135 + if (!((regval >> 16) & 0x1)) { 136 + dev_err(dev, "data is not valid"); 137 + return -EIO; 138 + } 139 + 140 + *val = regval & GENMASK(15, 0); 141 + return IIO_VAL_INT; 142 + } 143 + 144 + static int ens210_read_raw(struct iio_dev *indio_dev, 145 + struct iio_chan_spec const *channel, int *val, 146 + int *val2, long mask) 147 + { 148 + struct ens210_data *data = iio_priv(indio_dev); 149 + int ret; 150 + 151 + switch (mask) { 152 + case IIO_CHAN_INFO_RAW: 153 + scoped_guard(mutex, &data->lock) { 154 + ret = ens210_get_measurement( 155 + indio_dev, channel->type == IIO_TEMP, val); 156 + if (ret) 157 + return ret; 158 + return IIO_VAL_INT; 159 + } 160 + return -EINVAL; /* compiler warning workaround */ 161 + case IIO_CHAN_INFO_SCALE: 162 + if (channel->type == IIO_TEMP) { 163 + *val = 15; 164 + *val2 = 625000; 165 + } else { 166 + *val = 1; 167 + *val2 = 953125; 168 + } 169 + return IIO_VAL_INT_PLUS_MICRO; 170 + case IIO_CHAN_INFO_OFFSET: 171 + *val = -17481; 172 + *val2 = 600000; 173 + return IIO_VAL_INT_PLUS_MICRO; 174 + default: 175 + return -EINVAL; 176 + } 177 + } 178 + 179 + static const struct iio_chan_spec ens210_channels[] = { 180 + { 181 + .type = IIO_TEMP, 182 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 183 + BIT(IIO_CHAN_INFO_SCALE) | 184 + BIT(IIO_CHAN_INFO_OFFSET), 185 + }, 186 + { 187 + .type = IIO_HUMIDITYRELATIVE, 188 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 189 + BIT(IIO_CHAN_INFO_SCALE), 190 + } 191 + }; 192 + 193 + static const struct iio_info ens210_info = { 194 + .read_raw = ens210_read_raw, 195 + }; 196 + 197 + static int ens210_probe(struct i2c_client *client) 198 + { 199 + struct ens210_data *data; 200 + struct iio_dev *indio_dev; 201 + uint16_t part_id; 202 + int ret; 203 + 204 + if (!i2c_check_functionality(client->adapter, 205 + I2C_FUNC_SMBUS_WRITE_BYTE_DATA | 206 + I2C_FUNC_SMBUS_WRITE_BYTE | 207 + I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 208 + return dev_err_probe(&client->dev, -EOPNOTSUPP, 209 + "adapter does not support some i2c transactions\n"); 210 + } 211 + 212 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 213 + if (!indio_dev) 214 + return -ENOMEM; 215 + 216 + data = iio_priv(indio_dev); 217 + data->client = client; 218 + mutex_init(&data->lock); 219 + data->chip_info = i2c_get_match_data(client); 220 + 221 + ret = devm_regulator_get_enable(&client->dev, "vdd"); 222 + if (ret) 223 + return ret; 224 + 225 + /* reset device */ 226 + ret = i2c_smbus_write_byte_data(client, ENS210_REG_SYS_CTRL, 227 + ENS210_SYS_CTRL_SYS_RESET); 228 + if (ret) 229 + return ret; 230 + 231 + /* wait for device to become active */ 232 + usleep_range(4000, 5000); 233 + 234 + /* disable low power mode */ 235 + ret = i2c_smbus_write_byte_data(client, ENS210_REG_SYS_CTRL, 0x00); 236 + if (ret) 237 + return ret; 238 + 239 + /* wait for device to finish */ 240 + usleep_range(4000, 5000); 241 + 242 + /* get part_id */ 243 + ret = i2c_smbus_read_word_data(client, ENS210_REG_PART_ID); 244 + if (ret < 0) 245 + return ret; 246 + part_id = ret; 247 + 248 + if (part_id != data->chip_info->part_id) { 249 + dev_info(&client->dev, 250 + "Part ID does not match (0x%04x != 0x%04x)\n", part_id, 251 + data->chip_info->part_id); 252 + } 253 + 254 + /* reenable low power */ 255 + ret = i2c_smbus_write_byte_data(client, ENS210_REG_SYS_CTRL, 256 + ENS210_SYS_CTRL_LOW_POWER_ENABLE); 257 + if (ret) 258 + return ret; 259 + 260 + indio_dev->name = data->chip_info->name; 261 + indio_dev->modes = INDIO_DIRECT_MODE; 262 + indio_dev->channels = ens210_channels; 263 + indio_dev->num_channels = ARRAY_SIZE(ens210_channels); 264 + indio_dev->info = &ens210_info; 265 + 266 + return devm_iio_device_register(&client->dev, indio_dev); 267 + } 268 + 269 + static const struct ens210_chip_info ens210_chip_info_data = { 270 + .name = "ens210", 271 + .part_id = ENS210, 272 + .conv_time_msec = 130, 273 + }; 274 + 275 + static const struct ens210_chip_info ens210a_chip_info_data = { 276 + .name = "ens210a", 277 + .part_id = ENS210A, 278 + .conv_time_msec = 130, 279 + }; 280 + 281 + static const struct ens210_chip_info ens211_chip_info_data = { 282 + .name = "ens211", 283 + .part_id = ENS211, 284 + .conv_time_msec = 32, 285 + }; 286 + 287 + static const struct ens210_chip_info ens212_chip_info_data = { 288 + .name = "ens212", 289 + .part_id = ENS212, 290 + .conv_time_msec = 32, 291 + }; 292 + 293 + static const struct ens210_chip_info ens213a_chip_info_data = { 294 + .name = "ens213a", 295 + .part_id = ENS213A, 296 + .conv_time_msec = 130, 297 + }; 298 + 299 + static const struct ens210_chip_info ens215_chip_info_data = { 300 + .name = "ens215", 301 + .part_id = ENS215, 302 + .conv_time_msec = 130, 303 + }; 304 + 305 + static const struct of_device_id ens210_of_match[] = { 306 + { .compatible = "sciosense,ens210", .data = &ens210_chip_info_data }, 307 + { .compatible = "sciosense,ens210a", .data = &ens210a_chip_info_data }, 308 + { .compatible = "sciosense,ens211", .data = &ens211_chip_info_data }, 309 + { .compatible = "sciosense,ens212", .data = &ens212_chip_info_data }, 310 + { .compatible = "sciosense,ens213a", .data = &ens213a_chip_info_data }, 311 + { .compatible = "sciosense,ens215", .data = &ens215_chip_info_data }, 312 + { } 313 + }; 314 + MODULE_DEVICE_TABLE(of, ens210_of_match); 315 + 316 + static const struct i2c_device_id ens210_id_table[] = { 317 + { "ens210", (kernel_ulong_t)&ens210_chip_info_data }, 318 + { "ens210a", (kernel_ulong_t)&ens210a_chip_info_data }, 319 + { "ens211", (kernel_ulong_t)&ens211_chip_info_data }, 320 + { "ens212", (kernel_ulong_t)&ens212_chip_info_data }, 321 + { "ens213a", (kernel_ulong_t)&ens213a_chip_info_data }, 322 + { "ens215", (kernel_ulong_t)&ens215_chip_info_data }, 323 + { } 324 + }; 325 + MODULE_DEVICE_TABLE(i2c, ens210_id_table); 326 + 327 + static struct i2c_driver ens210_driver = { 328 + .probe = ens210_probe, 329 + .id_table = ens210_id_table, 330 + .driver = { 331 + .name = "ens210", 332 + .of_match_table = ens210_of_match, 333 + }, 334 + }; 335 + module_i2c_driver(ens210_driver); 336 + 337 + MODULE_DESCRIPTION("ScioSense ENS210 temperature and humidity sensor driver"); 338 + MODULE_AUTHOR("Joshua Felmeden <jfelmeden@thegoodpenguin.co.uk>"); 339 + MODULE_LICENSE("GPL");
+4 -14
drivers/iio/imu/adis16400.c
··· 202 202 ADIS16400_SCAN_TIMESTAMP, 203 203 }; 204 204 205 - #ifdef CONFIG_DEBUG_FS 206 - 207 205 static ssize_t adis16400_show_serial_number(struct file *file, 208 206 char __user *userbuf, size_t count, loff_t *ppos) 209 207 { ··· 271 273 DEFINE_DEBUGFS_ATTRIBUTE(adis16400_flash_count_fops, 272 274 adis16400_show_flash_count, NULL, "%lld\n"); 273 275 274 - static int adis16400_debugfs_init(struct iio_dev *indio_dev) 276 + static void adis16400_debugfs_init(struct iio_dev *indio_dev) 275 277 { 276 278 struct adis16400_state *st = iio_priv(indio_dev); 277 279 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 280 + 281 + if (!IS_ENABLED(CONFIG_DEBUG_FS)) 282 + return; 278 283 279 284 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER) 280 285 debugfs_create_file_unsafe("serial_number", 0400, ··· 287 286 d, st, &adis16400_product_id_fops); 288 287 debugfs_create_file_unsafe("flash_count", 0400, 289 288 d, st, &adis16400_flash_count_fops); 290 - 291 - return 0; 292 289 } 293 - 294 - #else 295 - 296 - static int adis16400_debugfs_init(struct iio_dev *indio_dev) 297 - { 298 - return 0; 299 - } 300 - 301 - #endif 302 290 303 291 enum adis16400_chip_variant { 304 292 ADIS16300,
+4 -14
drivers/iio/imu/adis16460.c
··· 69 69 struct adis adis; 70 70 }; 71 71 72 - #ifdef CONFIG_DEBUG_FS 73 - 74 72 static int adis16460_show_serial_number(void *arg, u64 *val) 75 73 { 76 74 struct adis16460 *adis16460 = arg; ··· 123 125 DEFINE_DEBUGFS_ATTRIBUTE(adis16460_flash_count_fops, 124 126 adis16460_show_flash_count, NULL, "%lld\n"); 125 127 126 - static int adis16460_debugfs_init(struct iio_dev *indio_dev) 128 + static void adis16460_debugfs_init(struct iio_dev *indio_dev) 127 129 { 128 130 struct adis16460 *adis16460 = iio_priv(indio_dev); 129 131 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 132 + 133 + if (!IS_ENABLED(CONFIG_DEBUG_FS)) 134 + return; 130 135 131 136 debugfs_create_file_unsafe("serial_number", 0400, 132 137 d, adis16460, &adis16460_serial_number_fops); ··· 137 136 d, adis16460, &adis16460_product_id_fops); 138 137 debugfs_create_file_unsafe("flash_count", 0400, 139 138 d, adis16460, &adis16460_flash_count_fops); 140 - 141 - return 0; 142 139 } 143 - 144 - #else 145 - 146 - static int adis16460_debugfs_init(struct iio_dev *indio_dev) 147 - { 148 - return 0; 149 - } 150 - 151 - #endif 152 140 153 141 static int adis16460_set_freq(struct iio_dev *indio_dev, int val, int val2) 154 142 {
+4 -8
drivers/iio/imu/adis16475.c
··· 164 164 MODULE_PARM_DESC(low_rate_allow, 165 165 "Allow IMU rates below the minimum advisable when external clk is used in SCALED mode (default: N)"); 166 166 167 - #ifdef CONFIG_DEBUG_FS 168 167 static ssize_t adis16475_show_firmware_revision(struct file *file, 169 168 char __user *userbuf, 170 169 size_t count, loff_t *ppos) ··· 278 279 struct adis16475 *st = iio_priv(indio_dev); 279 280 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 280 281 282 + if (!IS_ENABLED(CONFIG_DEBUG_FS)) 283 + return; 284 + 281 285 debugfs_create_file_unsafe("serial_number", 0400, 282 286 d, st, &adis16475_serial_number_fops); 283 287 debugfs_create_file_unsafe("product_id", 0400, ··· 292 290 debugfs_create_file("firmware_date", 0400, d, 293 291 st, &adis16475_firmware_date_fops); 294 292 } 295 - #else 296 - static void adis16475_debugfs_init(struct iio_dev *indio_dev) 297 - { 298 - } 299 - #endif 300 293 301 294 static int adis16475_get_freq(struct adis16475 *st, u32 *freq) 302 295 { ··· 1590 1593 return -EINVAL; 1591 1594 } 1592 1595 1593 - for_each_set_bit(bit, indio_dev->active_scan_mask, 1594 - indio_dev->masklength) { 1596 + iio_for_each_active_channel(indio_dev, bit) { 1595 1597 /* 1596 1598 * When burst mode is used, system flags is the first data 1597 1599 * channel in the sequence, but the scan index is 7.
+5 -15
drivers/iio/imu/adis16480.c
··· 193 193 MODULE_PARM_DESC(low_rate_allow, 194 194 "Allow IMU rates below the minimum advisable when external clk is used in PPS mode (default: N)"); 195 195 196 - #ifdef CONFIG_DEBUG_FS 197 - 198 196 static ssize_t adis16480_show_firmware_revision(struct file *file, 199 197 char __user *userbuf, size_t count, loff_t *ppos) 200 198 { ··· 302 304 DEFINE_DEBUGFS_ATTRIBUTE(adis16480_flash_count_fops, 303 305 adis16480_show_flash_count, NULL, "%lld\n"); 304 306 305 - static int adis16480_debugfs_init(struct iio_dev *indio_dev) 307 + static void adis16480_debugfs_init(struct iio_dev *indio_dev) 306 308 { 307 309 struct adis16480 *adis16480 = iio_priv(indio_dev); 308 310 struct dentry *d = iio_get_debugfs_dentry(indio_dev); 311 + 312 + if (!IS_ENABLED(CONFIG_DEBUG_FS)) 313 + return; 309 314 310 315 debugfs_create_file_unsafe("firmware_revision", 0400, 311 316 d, adis16480, &adis16480_firmware_revision_fops); ··· 320 319 d, adis16480, &adis16480_product_id_fops); 321 320 debugfs_create_file_unsafe("flash_count", 0400, 322 321 d, adis16480, &adis16480_flash_count_fops); 323 - 324 - return 0; 325 322 } 326 - 327 - #else 328 - 329 - static int adis16480_debugfs_init(struct iio_dev *indio_dev) 330 - { 331 - return 0; 332 - } 333 - 334 - #endif 335 323 336 324 static int adis16480_set_freq(struct iio_dev *indio_dev, int val, int val2) 337 325 { ··· 1385 1395 goto irq_done; 1386 1396 } 1387 1397 1388 - for_each_set_bit(bit, indio_dev->active_scan_mask, indio_dev->masklength) { 1398 + iio_for_each_active_channel(indio_dev, bit) { 1389 1399 /* 1390 1400 * When burst mode is used, temperature is the first data 1391 1401 * channel in the sequence, but the temperature scan index
+1 -2
drivers/iio/imu/bmi160/bmi160_core.c
··· 435 435 int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L; 436 436 __le16 sample; 437 437 438 - for_each_set_bit(i, indio_dev->active_scan_mask, 439 - indio_dev->masklength) { 438 + iio_for_each_active_channel(indio_dev, i) { 440 439 ret = regmap_bulk_read(data->regmap, base + i * sizeof(sample), 441 440 &sample, sizeof(sample)); 442 441 if (ret)
+1
drivers/iio/imu/bmi323/bmi323.h
··· 205 205 struct device; 206 206 int bmi323_core_probe(struct device *dev); 207 207 extern const struct regmap_config bmi323_regmap_config; 208 + extern const struct dev_pm_ops bmi323_core_pm_ops; 208 209 209 210 #endif
+23
drivers/iio/imu/bmi323/bmi323_core.c
··· 2121 2121 } 2122 2122 EXPORT_SYMBOL_NS_GPL(bmi323_core_probe, IIO_BMI323); 2123 2123 2124 + #if defined(CONFIG_PM) 2125 + static int bmi323_core_runtime_suspend(struct device *dev) 2126 + { 2127 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 2128 + 2129 + return iio_device_suspend_triggering(indio_dev); 2130 + } 2131 + 2132 + static int bmi323_core_runtime_resume(struct device *dev) 2133 + { 2134 + struct iio_dev *indio_dev = dev_get_drvdata(dev); 2135 + 2136 + return iio_device_resume_triggering(indio_dev); 2137 + } 2138 + 2139 + #endif 2140 + 2141 + const struct dev_pm_ops bmi323_core_pm_ops = { 2142 + SET_RUNTIME_PM_OPS(bmi323_core_runtime_suspend, 2143 + bmi323_core_runtime_resume, NULL) 2144 + }; 2145 + EXPORT_SYMBOL_NS_GPL(bmi323_core_pm_ops, IIO_BMI323); 2146 + 2124 2147 MODULE_DESCRIPTION("Bosch BMI323 IMU driver"); 2125 2148 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>"); 2126 2149 MODULE_LICENSE("GPL");
+2 -1
drivers/iio/imu/bmi323/bmi323_i2c.c
··· 61 61 data + sizeof(u8)); 62 62 } 63 63 64 - static struct regmap_bus bmi323_regmap_bus = { 64 + static const struct regmap_bus bmi323_regmap_bus = { 65 65 .read = bmi323_regmap_i2c_read, 66 66 .write = bmi323_regmap_i2c_write, 67 67 }; ··· 128 128 static struct i2c_driver bmi323_i2c_driver = { 129 129 .driver = { 130 130 .name = "bmi323", 131 + .pm = pm_ptr(&bmi323_core_pm_ops), 131 132 .of_match_table = bmi323_of_i2c_match, 132 133 .acpi_match_table = bmi323_acpi_match, 133 134 },
+2 -1
drivers/iio/imu/bmi323/bmi323_spi.c
··· 36 36 return spi_write(spi, data_buff + 1, count - 1); 37 37 } 38 38 39 - static struct regmap_bus bmi323_regmap_bus = { 39 + static const struct regmap_bus bmi323_regmap_bus = { 40 40 .read = bmi323_regmap_spi_read, 41 41 .write = bmi323_regmap_spi_write, 42 42 }; ··· 79 79 static struct spi_driver bmi323_spi_driver = { 80 80 .driver = { 81 81 .name = "bmi323", 82 + .pm = pm_ptr(&bmi323_core_pm_ops), 82 83 .of_match_table = bmi323_of_spi_match, 83 84 }, 84 85 .probe = bmi323_spi_probe,
+1 -1
drivers/iio/imu/bno055/bno055.c
··· 1458 1458 * then we split the transfer, skipping the gap. 1459 1459 */ 1460 1460 for_each_set_bitrange(start, end, iio_dev->active_scan_mask, 1461 - iio_dev->masklength) { 1461 + iio_get_masklength(iio_dev)) { 1462 1462 /* 1463 1463 * First transfer will start from the beginning of the first 1464 1464 * ones-field in the bitmap
+1 -1
drivers/iio/imu/bno055/bno055_ser_core.c
··· 492 492 .write_wakeup = serdev_device_write_wakeup, 493 493 }; 494 494 495 - static struct regmap_bus bno055_ser_regmap_bus = { 495 + static const struct regmap_bus bno055_ser_regmap_bus = { 496 496 .write = bno055_ser_write_reg, 497 497 .read = bno055_ser_read_reg, 498 498 };
+1 -2
drivers/iio/imu/kmx61.c
··· 1200 1200 base = KMX61_MAG_XOUT_L; 1201 1201 1202 1202 mutex_lock(&data->lock); 1203 - for_each_set_bit(bit, indio_dev->active_scan_mask, 1204 - indio_dev->masklength) { 1203 + iio_for_each_active_channel(indio_dev, bit) { 1205 1204 ret = kmx61_read_measurement(data, base, bit); 1206 1205 if (ret < 0) { 1207 1206 mutex_unlock(&data->lock);
+239 -25
drivers/iio/industrialio-backend.c
··· 32 32 #define dev_fmt(fmt) "iio-backend: " fmt 33 33 34 34 #include <linux/cleanup.h> 35 + #include <linux/debugfs.h> 35 36 #include <linux/device.h> 36 37 #include <linux/err.h> 37 38 #include <linux/errno.h> ··· 41 40 #include <linux/mutex.h> 42 41 #include <linux/property.h> 43 42 #include <linux/slab.h> 43 + #include <linux/stringify.h> 44 44 #include <linux/types.h> 45 45 46 46 #include <linux/iio/backend.h> ··· 54 52 struct device *dev; 55 53 struct module *owner; 56 54 void *priv; 55 + const char *name; 56 + unsigned int cached_reg_addr; 57 + /* 58 + * This index is relative to the frontend. Meaning that for 59 + * frontends with multiple backends, this will be the index of this 60 + * backend. Used for the debugfs directory name. 61 + */ 62 + u8 idx; 57 63 }; 58 64 59 65 /* ··· 121 111 __ret = iio_backend_check_op(__back, op); \ 122 112 if (!__ret) \ 123 113 __back->ops->op(__back, ##args); \ 114 + else \ 115 + dev_dbg(__back->dev, "Op(%s) not implemented\n",\ 116 + __stringify(op)); \ 124 117 } 118 + 119 + static ssize_t iio_backend_debugfs_read_reg(struct file *file, 120 + char __user *userbuf, 121 + size_t count, loff_t *ppos) 122 + { 123 + struct iio_backend *back = file->private_data; 124 + char read_buf[20]; 125 + unsigned int val; 126 + int ret, len; 127 + 128 + ret = iio_backend_op_call(back, debugfs_reg_access, 129 + back->cached_reg_addr, 0, &val); 130 + if (ret) 131 + return ret; 132 + 133 + len = scnprintf(read_buf, sizeof(read_buf), "0x%X\n", val); 134 + 135 + return simple_read_from_buffer(userbuf, count, ppos, read_buf, len); 136 + } 137 + 138 + static ssize_t iio_backend_debugfs_write_reg(struct file *file, 139 + const char __user *userbuf, 140 + size_t count, loff_t *ppos) 141 + { 142 + struct iio_backend *back = file->private_data; 143 + unsigned int val; 144 + char buf[80]; 145 + ssize_t rc; 146 + int ret; 147 + 148 + rc = simple_write_to_buffer(buf, sizeof(buf), ppos, userbuf, count); 149 + if (rc < 0) 150 + return rc; 151 + 152 + ret = sscanf(buf, "%i %i", &back->cached_reg_addr, &val); 153 + 154 + switch (ret) { 155 + case 1: 156 + return count; 157 + case 2: 158 + ret = iio_backend_op_call(back, debugfs_reg_access, 159 + back->cached_reg_addr, val, NULL); 160 + if (ret) 161 + return ret; 162 + return count; 163 + default: 164 + return -EINVAL; 165 + } 166 + } 167 + 168 + static const struct file_operations iio_backend_debugfs_reg_fops = { 169 + .open = simple_open, 170 + .read = iio_backend_debugfs_read_reg, 171 + .write = iio_backend_debugfs_write_reg, 172 + }; 173 + 174 + static ssize_t iio_backend_debugfs_read_name(struct file *file, 175 + char __user *userbuf, 176 + size_t count, loff_t *ppos) 177 + { 178 + struct iio_backend *back = file->private_data; 179 + char name[128]; 180 + int len; 181 + 182 + len = scnprintf(name, sizeof(name), "%s\n", back->name); 183 + 184 + return simple_read_from_buffer(userbuf, count, ppos, name, len); 185 + } 186 + 187 + static const struct file_operations iio_backend_debugfs_name_fops = { 188 + .open = simple_open, 189 + .read = iio_backend_debugfs_read_name, 190 + }; 191 + 192 + /** 193 + * iio_backend_debugfs_add - Add debugfs interfaces for Backends 194 + * @back: Backend device 195 + * @indio_dev: IIO device 196 + */ 197 + void iio_backend_debugfs_add(struct iio_backend *back, 198 + struct iio_dev *indio_dev) 199 + { 200 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 201 + struct dentry *back_d; 202 + char name[128]; 203 + 204 + if (!IS_ENABLED(CONFIG_DEBUG_FS) || !d) 205 + return; 206 + if (!back->ops->debugfs_reg_access && !back->name) 207 + return; 208 + 209 + snprintf(name, sizeof(name), "backend%d", back->idx); 210 + 211 + back_d = debugfs_create_dir(name, d); 212 + if (IS_ERR(back_d)) 213 + return; 214 + 215 + if (back->ops->debugfs_reg_access) 216 + debugfs_create_file("direct_reg_access", 0600, back_d, back, 217 + &iio_backend_debugfs_reg_fops); 218 + 219 + if (back->name) 220 + debugfs_create_file("name", 0400, back_d, back, 221 + &iio_backend_debugfs_name_fops); 222 + } 223 + EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_add, IIO_BACKEND); 224 + 225 + /** 226 + * iio_backend_debugfs_print_chan_status - Print channel status 227 + * @back: Backend device 228 + * @chan: Channel number 229 + * @buf: Buffer where to print the status 230 + * @len: Available space 231 + * 232 + * One usecase where this is useful is for testing test tones in a digital 233 + * interface and "ask" the backend to dump more details on why a test tone might 234 + * have errors. 235 + * 236 + * RETURNS: 237 + * Number of copied bytes on success, negative error code on failure. 238 + */ 239 + ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back, 240 + unsigned int chan, char *buf, 241 + size_t len) 242 + { 243 + if (!IS_ENABLED(CONFIG_DEBUG_FS)) 244 + return -ENODEV; 245 + 246 + return iio_backend_op_call(back, debugfs_print_chan_status, chan, buf, 247 + len); 248 + } 249 + EXPORT_SYMBOL_NS_GPL(iio_backend_debugfs_print_chan_status, IIO_BACKEND); 125 250 126 251 /** 127 252 * iio_backend_chan_enable - Enable a backend channel ··· 292 147 } 293 148 294 149 /** 150 + * iio_backend_disable - Backend disable 151 + * @back: Backend device 152 + */ 153 + void iio_backend_disable(struct iio_backend *back) 154 + { 155 + __iio_backend_disable(back); 156 + } 157 + EXPORT_SYMBOL_NS_GPL(iio_backend_disable, IIO_BACKEND); 158 + 159 + /** 160 + * iio_backend_enable - Backend enable 161 + * @back: Backend device 162 + * 163 + * RETURNS: 164 + * 0 on success, negative error number on failure. 165 + */ 166 + int iio_backend_enable(struct iio_backend *back) 167 + { 168 + return iio_backend_op_call(back, enable); 169 + } 170 + EXPORT_SYMBOL_NS_GPL(iio_backend_enable, IIO_BACKEND); 171 + 172 + /** 295 173 * devm_iio_backend_enable - Device managed backend enable 296 174 * @dev: Consumer device for the backend 297 175 * @back: Backend device ··· 326 158 { 327 159 int ret; 328 160 329 - ret = iio_backend_op_call(back, enable); 161 + ret = iio_backend_enable(back); 330 162 if (ret) 331 163 return ret; 332 164 ··· 525 357 } 526 358 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_request_buffer, IIO_BACKEND); 527 359 360 + /** 361 + * iio_backend_read_raw - Read a channel attribute from a backend device. 362 + * @back: Backend device 363 + * @chan: IIO channel reference 364 + * @val: First returned value 365 + * @val2: Second returned value 366 + * @mask: Specify the attribute to return 367 + * 368 + * RETURNS: 369 + * 0 on success, negative error number on failure. 370 + */ 371 + int iio_backend_read_raw(struct iio_backend *back, 372 + struct iio_chan_spec const *chan, int *val, int *val2, 373 + long mask) 374 + { 375 + return iio_backend_op_call(back, read_raw, chan, val, val2, mask); 376 + } 377 + EXPORT_SYMBOL_NS_GPL(iio_backend_read_raw, IIO_BACKEND); 378 + 528 379 static struct iio_backend *iio_backend_from_indio_dev_parent(const struct device *dev) 529 380 { 530 381 struct iio_backend *back = ERR_PTR(-ENODEV), *iter; ··· 638 451 639 452 /** 640 453 * iio_backend_extend_chan_spec - Extend an IIO channel 641 - * @indio_dev: IIO device 642 454 * @back: Backend device 643 455 * @chan: IIO channel 644 456 * ··· 647 461 * RETURNS: 648 462 * 0 on success, negative error number on failure. 649 463 */ 650 - int iio_backend_extend_chan_spec(struct iio_dev *indio_dev, 651 - struct iio_backend *back, 464 + int iio_backend_extend_chan_spec(struct iio_backend *back, 652 465 struct iio_chan_spec *chan) 653 466 { 654 467 const struct iio_chan_spec_ext_info *frontend_ext_info = chan->ext_info; ··· 718 533 return 0; 719 534 } 720 535 721 - /** 722 - * devm_iio_backend_get - Device managed backend device get 723 - * @dev: Consumer device for the backend 724 - * @name: Backend name 725 - * 726 - * Get's the backend associated with @dev. 727 - * 728 - * RETURNS: 729 - * A backend pointer, negative error pointer otherwise. 730 - */ 731 - struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name) 536 + static struct iio_backend *__devm_iio_backend_fwnode_get(struct device *dev, const char *name, 537 + struct fwnode_handle *fwnode) 732 538 { 733 - struct fwnode_handle *fwnode; 539 + struct fwnode_handle *fwnode_back; 734 540 struct iio_backend *back; 735 541 unsigned int index; 736 542 int ret; ··· 736 560 index = 0; 737 561 } 738 562 739 - fwnode = fwnode_find_reference(dev_fwnode(dev), "io-backends", index); 563 + fwnode_back = fwnode_find_reference(fwnode, "io-backends", index); 740 564 if (IS_ERR(fwnode)) 741 565 return dev_err_cast_probe(dev, fwnode, 742 566 "Cannot get Firmware reference\n"); 743 567 744 568 guard(mutex)(&iio_back_lock); 745 569 list_for_each_entry(back, &iio_back_list, entry) { 746 - if (!device_match_fwnode(back->dev, fwnode)) 570 + if (!device_match_fwnode(back->dev, fwnode_back)) 747 571 continue; 748 572 749 - fwnode_handle_put(fwnode); 573 + fwnode_handle_put(fwnode_back); 750 574 ret = __devm_iio_backend_get(dev, back); 751 575 if (ret) 752 576 return ERR_PTR(ret); 753 577 578 + if (name) 579 + back->idx = index; 580 + 754 581 return back; 755 582 } 756 583 757 - fwnode_handle_put(fwnode); 584 + fwnode_handle_put(fwnode_back); 758 585 return ERR_PTR(-EPROBE_DEFER); 759 586 } 587 + 588 + /** 589 + * devm_iio_backend_get - Device managed backend device get 590 + * @dev: Consumer device for the backend 591 + * @name: Backend name 592 + * 593 + * Get's the backend associated with @dev. 594 + * 595 + * RETURNS: 596 + * A backend pointer, negative error pointer otherwise. 597 + */ 598 + struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name) 599 + { 600 + return __devm_iio_backend_fwnode_get(dev, name, dev_fwnode(dev)); 601 + } 760 602 EXPORT_SYMBOL_NS_GPL(devm_iio_backend_get, IIO_BACKEND); 603 + 604 + /** 605 + * devm_iio_backend_fwnode_get - Device managed backend firmware node get 606 + * @dev: Consumer device for the backend 607 + * @name: Backend name 608 + * @fwnode: Firmware node of the backend consumer 609 + * 610 + * Get's the backend associated with a firmware node. 611 + * 612 + * RETURNS: 613 + * A backend pointer, negative error pointer otherwise. 614 + */ 615 + struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev, 616 + const char *name, 617 + struct fwnode_handle *fwnode) 618 + { 619 + return __devm_iio_backend_fwnode_get(dev, name, fwnode); 620 + } 621 + EXPORT_SYMBOL_NS_GPL(devm_iio_backend_fwnode_get, IIO_BACKEND); 761 622 762 623 /** 763 624 * __devm_iio_backend_get_from_fwnode_lookup - Device managed fwnode backend device get ··· 852 639 /** 853 640 * devm_iio_backend_register - Device managed backend device register 854 641 * @dev: Backend device being registered 855 - * @ops: Backend ops 642 + * @info: Backend info 856 643 * @priv: Device private data 857 644 * 858 - * @ops is mandatory. Not providing it results in -EINVAL. 645 + * @info is mandatory. Not providing it results in -EINVAL. 859 646 * 860 647 * RETURNS: 861 648 * 0 on success, negative error number on failure. 862 649 */ 863 650 int devm_iio_backend_register(struct device *dev, 864 - const struct iio_backend_ops *ops, void *priv) 651 + const struct iio_backend_info *info, void *priv) 865 652 { 866 653 struct iio_backend *back; 867 654 868 - if (!ops) 655 + if (!info || !info->ops) 869 656 return dev_err_probe(dev, -EINVAL, "No backend ops given\n"); 870 657 871 658 /* ··· 878 665 if (!back) 879 666 return -ENOMEM; 880 667 881 - back->ops = ops; 668 + back->ops = info->ops; 669 + back->name = info->name; 882 670 back->owner = dev->driver->owner; 883 671 back->dev = dev; 884 672 back->priv = priv;
+24 -28
drivers/iio/industrialio-buffer.c
··· 508 508 static int iio_scan_mask_set(struct iio_dev *indio_dev, 509 509 struct iio_buffer *buffer, int bit) 510 510 { 511 + unsigned int masklength = iio_get_masklength(indio_dev); 511 512 const unsigned long *mask; 512 513 unsigned long *trialmask; 513 514 514 - if (!indio_dev->masklength) { 515 + if (!masklength) { 515 516 WARN(1, "Trying to set scanmask prior to registering buffer\n"); 516 517 return -EINVAL; 517 518 } 518 519 519 - trialmask = bitmap_alloc(indio_dev->masklength, GFP_KERNEL); 520 + trialmask = bitmap_alloc(masklength, GFP_KERNEL); 520 521 if (!trialmask) 521 522 return -ENOMEM; 522 - bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 523 + bitmap_copy(trialmask, buffer->scan_mask, masklength); 523 524 set_bit(bit, trialmask); 524 525 525 526 if (!iio_validate_scan_mask(indio_dev, trialmask)) ··· 528 527 529 528 if (indio_dev->available_scan_masks) { 530 529 mask = iio_scan_mask_match(indio_dev->available_scan_masks, 531 - indio_dev->masklength, 532 - trialmask, false); 530 + masklength, trialmask, false); 533 531 if (!mask) 534 532 goto err_invalid_mask; 535 533 } 536 - bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength); 534 + bitmap_copy(buffer->scan_mask, trialmask, masklength); 537 535 538 536 bitmap_free(trialmask); 539 537 ··· 552 552 static int iio_scan_mask_query(struct iio_dev *indio_dev, 553 553 struct iio_buffer *buffer, int bit) 554 554 { 555 - if (bit > indio_dev->masklength) 555 + if (bit > iio_get_masklength(indio_dev)) 556 556 return -EINVAL; 557 557 558 558 if (!buffer->scan_mask) ··· 768 768 int length, i, largest = 0; 769 769 770 770 /* How much space will the demuxed element take? */ 771 - for_each_set_bit(i, mask, 772 - indio_dev->masklength) { 771 + for_each_set_bit(i, mask, iio_get_masklength(indio_dev)) { 773 772 length = iio_storage_bytes_for_si(indio_dev, i); 774 773 if (length < 0) 775 774 return length; ··· 889 890 struct iio_device_config *config) 890 891 { 891 892 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 893 + unsigned int masklength = iio_get_masklength(indio_dev); 892 894 unsigned long *compound_mask; 893 895 const unsigned long *scan_mask; 894 896 bool strict_scanmask = false; ··· 898 898 unsigned int modes; 899 899 900 900 if (insert_buffer && 901 - bitmap_empty(insert_buffer->scan_mask, indio_dev->masklength)) { 901 + bitmap_empty(insert_buffer->scan_mask, masklength)) { 902 902 dev_dbg(&indio_dev->dev, 903 903 "At least one scan element must be enabled first\n"); 904 904 return -EINVAL; ··· 952 952 } 953 953 954 954 /* What scan mask do we actually have? */ 955 - compound_mask = bitmap_zalloc(indio_dev->masklength, GFP_KERNEL); 955 + compound_mask = bitmap_zalloc(masklength, GFP_KERNEL); 956 956 if (!compound_mask) 957 957 return -ENOMEM; 958 958 ··· 962 962 if (buffer == remove_buffer) 963 963 continue; 964 964 bitmap_or(compound_mask, compound_mask, buffer->scan_mask, 965 - indio_dev->masklength); 965 + masklength); 966 966 scan_timestamp |= buffer->scan_timestamp; 967 967 } 968 968 969 969 if (insert_buffer) { 970 970 bitmap_or(compound_mask, compound_mask, 971 - insert_buffer->scan_mask, indio_dev->masklength); 971 + insert_buffer->scan_mask, masklength); 972 972 scan_timestamp |= insert_buffer->scan_timestamp; 973 973 } 974 974 975 975 if (indio_dev->available_scan_masks) { 976 976 scan_mask = iio_scan_mask_match(indio_dev->available_scan_masks, 977 - indio_dev->masklength, 978 - compound_mask, 977 + masklength, compound_mask, 979 978 strict_scanmask); 980 979 bitmap_free(compound_mask); 981 980 if (!scan_mask) ··· 1039 1040 static int iio_buffer_update_demux(struct iio_dev *indio_dev, 1040 1041 struct iio_buffer *buffer) 1041 1042 { 1043 + unsigned int masklength = iio_get_masklength(indio_dev); 1042 1044 int ret, in_ind = -1, out_ind, length; 1043 1045 unsigned int in_loc = 0, out_loc = 0; 1044 1046 struct iio_demux_table *p = NULL; ··· 1051 1051 1052 1052 /* First work out which scan mode we will actually have */ 1053 1053 if (bitmap_equal(indio_dev->active_scan_mask, 1054 - buffer->scan_mask, 1055 - indio_dev->masklength)) 1054 + buffer->scan_mask, masklength)) 1056 1055 return 0; 1057 1056 1058 1057 /* Now we have the two masks, work from least sig and build up sizes */ 1059 - for_each_set_bit(out_ind, 1060 - buffer->scan_mask, 1061 - indio_dev->masklength) { 1058 + for_each_set_bit(out_ind, buffer->scan_mask, masklength) { 1062 1059 in_ind = find_next_bit(indio_dev->active_scan_mask, 1063 - indio_dev->masklength, 1064 - in_ind + 1); 1060 + masklength, in_ind + 1); 1065 1061 while (in_ind != out_ind) { 1066 1062 ret = iio_storage_bytes_for_si(indio_dev, in_ind); 1067 1063 if (ret < 0) ··· 1067 1071 /* Make sure we are aligned */ 1068 1072 in_loc = roundup(in_loc, length) + length; 1069 1073 in_ind = find_next_bit(indio_dev->active_scan_mask, 1070 - indio_dev->masklength, 1071 - in_ind + 1); 1074 + masklength, in_ind + 1); 1072 1075 } 1073 1076 ret = iio_storage_bytes_for_si(indio_dev, in_ind); 1074 1077 if (ret < 0) ··· 2099 2104 int index) 2100 2105 { 2101 2106 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 2107 + unsigned int masklength = iio_get_masklength(indio_dev); 2102 2108 struct iio_dev_attr *p; 2103 2109 const struct iio_dev_attr *id_attr; 2104 2110 struct attribute **attr; ··· 2162 2166 iio_dev_opaque->scan_index_timestamp = 2163 2167 channels[i].scan_index; 2164 2168 } 2165 - if (indio_dev->masklength && !buffer->scan_mask) { 2166 - buffer->scan_mask = bitmap_zalloc(indio_dev->masklength, 2169 + if (masklength && !buffer->scan_mask) { 2170 + buffer->scan_mask = bitmap_zalloc(masklength, 2167 2171 GFP_KERNEL); 2168 2172 if (!buffer->scan_mask) { 2169 2173 ret = -ENOMEM; ··· 2269 2273 2270 2274 for (i = 0; i < indio_dev->num_channels; i++) 2271 2275 ml = max(ml, channels[i].scan_index + 1); 2272 - indio_dev->masklength = ml; 2276 + ACCESS_PRIVATE(indio_dev, masklength) = ml; 2273 2277 } 2274 2278 2275 2279 if (!iio_dev_opaque->attached_buffers_cnt) ··· 2333 2337 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, 2334 2338 const unsigned long *mask) 2335 2339 { 2336 - return bitmap_weight(mask, indio_dev->masklength) == 1; 2340 + return bitmap_weight(mask, iio_get_masklength(indio_dev)) == 1; 2337 2341 } 2338 2342 EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot); 2339 2343
+44 -2
drivers/iio/industrialio-core.c
··· 667 667 vals[1]); 668 668 case IIO_VAL_FRACTIONAL: 669 669 tmp2 = div_s64((s64)vals[0] * 1000000000LL, vals[1]); 670 - tmp1 = vals[1]; 671 670 tmp0 = (int)div_s64_rem(tmp2, 1000000000, &tmp1); 672 671 if ((tmp2 < 0) && (tmp0 == 0)) 673 672 return sysfs_emit_at(buf, offset, "-0.%09u", abs(tmp1)); ··· 1911 1912 int i; 1912 1913 1913 1914 av_masks = indio_dev->available_scan_masks; 1914 - masklength = indio_dev->masklength; 1915 + masklength = iio_get_masklength(indio_dev); 1915 1916 longs_per_mask = BITS_TO_LONGS(masklength); 1916 1917 1917 1918 /* ··· 1963 1964 } 1964 1965 } 1965 1966 } 1967 + 1968 + /** 1969 + * iio_active_scan_mask_index - Get index of the active scan mask inside the 1970 + * available scan masks array 1971 + * @indio_dev: the IIO device containing the active and available scan masks 1972 + * 1973 + * Returns: the index or -EINVAL if active_scan_mask is not set 1974 + */ 1975 + int iio_active_scan_mask_index(struct iio_dev *indio_dev) 1976 + 1977 + { 1978 + const unsigned long *av_masks; 1979 + unsigned int masklength = iio_get_masklength(indio_dev); 1980 + int i = 0; 1981 + 1982 + if (!indio_dev->active_scan_mask) 1983 + return -EINVAL; 1984 + 1985 + /* 1986 + * As in iio_scan_mask_match and iio_sanity_check_avail_scan_masks, 1987 + * the condition here do not handle multi-long masks correctly. 1988 + * It only checks the first long to be zero, and will use such mask 1989 + * as a terminator even if there was bits set after the first long. 1990 + * 1991 + * This should be fine since the available_scan_mask has already been 1992 + * sanity tested using iio_sanity_check_avail_scan_masks. 1993 + * 1994 + * See iio_scan_mask_match and iio_sanity_check_avail_scan_masks for 1995 + * more details 1996 + */ 1997 + av_masks = indio_dev->available_scan_masks; 1998 + while (*av_masks) { 1999 + if (indio_dev->active_scan_mask == av_masks) 2000 + return i; 2001 + av_masks += BITS_TO_LONGS(masklength); 2002 + i++; 2003 + } 2004 + 2005 + dev_warn(indio_dev->dev.parent, 2006 + "active scan mask is not part of the available scan masks\n"); 2007 + return -EINVAL; 2008 + } 2009 + EXPORT_SYMBOL_GPL(iio_active_scan_mask_index); 1966 2010 1967 2011 int __iio_device_register(struct iio_dev *indio_dev, struct module *this_mod) 1968 2012 {
+27
drivers/iio/industrialio-trigger.c
··· 347 347 iio_trigger_put_irq(trig, pf->irq); 348 348 free_irq(pf->irq, pf); 349 349 module_put(iio_dev_opaque->driver_module); 350 + pf->irq = 0; 350 351 351 352 return ret; 352 353 } ··· 771 770 if (indio_dev->trig) 772 771 iio_trigger_put(indio_dev->trig); 773 772 } 773 + 774 + int iio_device_suspend_triggering(struct iio_dev *indio_dev) 775 + { 776 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 777 + 778 + guard(mutex)(&iio_dev_opaque->mlock); 779 + 780 + if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0)) 781 + disable_irq(indio_dev->pollfunc->irq); 782 + 783 + return 0; 784 + } 785 + EXPORT_SYMBOL(iio_device_suspend_triggering); 786 + 787 + int iio_device_resume_triggering(struct iio_dev *indio_dev) 788 + { 789 + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 790 + 791 + guard(mutex)(&iio_dev_opaque->mlock); 792 + 793 + if ((indio_dev->pollfunc) && (indio_dev->pollfunc->irq > 0)) 794 + enable_irq(indio_dev->pollfunc->irq); 795 + 796 + return 0; 797 + } 798 + EXPORT_SYMBOL(iio_device_resume_triggering);
+13
drivers/iio/light/Kconfig
··· 114 114 This driver can also be built as a module. If so, the module 115 115 will be called as73211. 116 116 117 + config BH1745 118 + tristate "ROHM BH1745 colour sensor" 119 + depends on I2C 120 + select REGMAP_I2C 121 + select IIO_BUFFER 122 + select IIO_TRIGGERED_BUFFER 123 + select IIO_GTS_HELPER 124 + help 125 + Say Y here to build support for the ROHM bh1745 colour sensor. 126 + 127 + To compile this driver as a module, choose M here: the module will 128 + be called bh1745. 129 + 117 130 config BH1750 118 131 tristate "ROHM BH1750 ambient light sensor" 119 132 depends on I2C
+1
drivers/iio/light/Makefile
··· 13 13 obj-$(CONFIG_APDS9306) += apds9306.o 14 14 obj-$(CONFIG_APDS9960) += apds9960.o 15 15 obj-$(CONFIG_AS73211) += as73211.o 16 + obj-$(CONFIG_BH1745) += bh1745.o 16 17 obj-$(CONFIG_BH1750) += bh1750.o 17 18 obj-$(CONFIG_BH1780) += bh1780.o 18 19 obj-$(CONFIG_CM32181) += cm32181.o
+1 -2
drivers/iio/light/adjd_s311.c
··· 125 125 if (ret < 0) 126 126 goto done; 127 127 128 - for_each_set_bit(i, indio_dev->active_scan_mask, 129 - indio_dev->masklength) { 128 + iio_for_each_active_channel(indio_dev, i) { 130 129 ret = i2c_smbus_read_word_data(data->client, 131 130 ADJD_S311_DATA_REG(i)); 132 131 if (ret < 0)
+54 -1
drivers/iio/light/apds9960.c
··· 146 146 147 147 /* gesture buffer */ 148 148 u8 buffer[4]; /* 4 8-bit channels */ 149 + 150 + /* calibration value buffer */ 151 + int calibbias[5]; 152 + }; 153 + 154 + enum { 155 + APDS9960_CHAN_PROXIMITY, 156 + APDS9960_CHAN_GESTURE_UP, 157 + APDS9960_CHAN_GESTURE_DOWN, 158 + APDS9960_CHAN_GESTURE_LEFT, 159 + APDS9960_CHAN_GESTURE_RIGHT, 160 + }; 161 + 162 + static const unsigned int apds9960_offset_regs[][2] = { 163 + [APDS9960_CHAN_PROXIMITY] = {APDS9960_REG_POFFSET_UR, APDS9960_REG_POFFSET_DL}, 164 + [APDS9960_CHAN_GESTURE_UP] = {APDS9960_REG_GOFFSET_U, 0}, 165 + [APDS9960_CHAN_GESTURE_DOWN] = {APDS9960_REG_GOFFSET_D, 0}, 166 + [APDS9960_CHAN_GESTURE_LEFT] = {APDS9960_REG_GOFFSET_L, 0}, 167 + [APDS9960_CHAN_GESTURE_RIGHT] = {APDS9960_REG_GOFFSET_R, 0}, 149 168 }; 150 169 151 170 static const struct reg_default apds9960_reg_defaults[] = { ··· 274 255 275 256 #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \ 276 257 .type = IIO_PROXIMITY, \ 258 + .info_mask_separate = BIT(IIO_CHAN_INFO_CALIBBIAS), \ 277 259 .channel = _si + 1, \ 278 260 .scan_index = _si, \ 279 261 .indexed = 1, \ ··· 302 282 { 303 283 .type = IIO_PROXIMITY, 304 284 .address = APDS9960_REG_PDATA, 305 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 285 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 286 + BIT(IIO_CHAN_INFO_CALIBBIAS), 306 287 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 307 288 .channel = 0, 308 289 .indexed = 0, ··· 336 315 APDS9960_INTENSITY_CHANNEL(GREEN), 337 316 APDS9960_INTENSITY_CHANNEL(BLUE), 338 317 }; 318 + 319 + static int apds9960_set_calibbias(struct apds9960_data *data, 320 + struct iio_chan_spec const *chan, int calibbias) 321 + { 322 + int ret, i; 323 + 324 + if (calibbias < S8_MIN || calibbias > S8_MAX) 325 + return -EINVAL; 326 + 327 + guard(mutex)(&data->lock); 328 + for (i = 0; i < 2; i++) { 329 + if (apds9960_offset_regs[chan->channel][i] == 0) 330 + break; 331 + 332 + ret = regmap_write(data->regmap, apds9960_offset_regs[chan->channel][i], calibbias); 333 + if (ret < 0) 334 + return ret; 335 + } 336 + data->calibbias[chan->channel] = calibbias; 337 + 338 + return 0; 339 + } 339 340 340 341 /* integration time in us */ 341 342 static const int apds9960_int_time[][2] = { ··· 574 531 } 575 532 mutex_unlock(&data->lock); 576 533 break; 534 + case IIO_CHAN_INFO_CALIBBIAS: 535 + mutex_lock(&data->lock); 536 + *val = data->calibbias[chan->channel]; 537 + ret = IIO_VAL_INT; 538 + mutex_unlock(&data->lock); 539 + break; 577 540 } 578 541 579 542 return ret; ··· 613 564 default: 614 565 return -EINVAL; 615 566 } 567 + case IIO_CHAN_INFO_CALIBBIAS: 568 + if (val2 != 0) 569 + return -EINVAL; 570 + return apds9960_set_calibbias(data, chan, val); 616 571 default: 617 572 return -EINVAL; 618 573 }
+906
drivers/iio/light/bh1745.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * ROHM BH1745 digital colour sensor driver 4 + * 5 + * Copyright (C) Mudit Sharma <muditsharma.info@gmail.com> 6 + * 7 + * 7-bit I2C slave addresses: 8 + * 0x38 (ADDR pin low) 9 + * 0x39 (ADDR pin high) 10 + */ 11 + 12 + #include <linux/i2c.h> 13 + #include <linux/mutex.h> 14 + #include <linux/util_macros.h> 15 + #include <linux/iio/events.h> 16 + #include <linux/regmap.h> 17 + #include <linux/bits.h> 18 + #include <linux/bitfield.h> 19 + 20 + #include <linux/iio/iio.h> 21 + #include <linux/iio/sysfs.h> 22 + #include <linux/iio/trigger.h> 23 + #include <linux/iio/trigger_consumer.h> 24 + #include <linux/iio/triggered_buffer.h> 25 + #include <linux/iio/iio-gts-helper.h> 26 + 27 + /* BH1745 configuration registers */ 28 + 29 + /* System control */ 30 + #define BH1745_SYS_CTRL 0x40 31 + #define BH1745_SYS_CTRL_SW_RESET BIT(7) 32 + #define BH1745_SYS_CTRL_INTR_RESET BIT(6) 33 + #define BH1745_SYS_CTRL_PART_ID_MASK GENMASK(5, 0) 34 + #define BH1745_PART_ID 0x0B 35 + 36 + /* Mode control 1 */ 37 + #define BH1745_MODE_CTRL1 0x41 38 + #define BH1745_CTRL1_MEASUREMENT_TIME_MASK GENMASK(2, 0) 39 + 40 + /* Mode control 2 */ 41 + #define BH1745_MODE_CTRL2 0x42 42 + #define BH1745_CTRL2_RGBC_EN BIT(4) 43 + #define BH1745_CTRL2_ADC_GAIN_MASK GENMASK(1, 0) 44 + 45 + /* Interrupt */ 46 + #define BH1745_INTR 0x60 47 + #define BH1745_INTR_STATUS BIT(7) 48 + #define BH1745_INTR_SOURCE_MASK GENMASK(3, 2) 49 + #define BH1745_INTR_ENABLE BIT(0) 50 + 51 + #define BH1745_PERSISTENCE 0x61 52 + 53 + /* Threshold high */ 54 + #define BH1745_TH_LSB 0x62 55 + #define BH1745_TH_MSB 0x63 56 + 57 + /* Threshold low */ 58 + #define BH1745_TL_LSB 0x64 59 + #define BH1745_TL_MSB 0x65 60 + 61 + /* BH1745 data output regs */ 62 + #define BH1745_RED_LSB 0x50 63 + #define BH1745_RED_MSB 0x51 64 + #define BH1745_GREEN_LSB 0x52 65 + #define BH1745_GREEN_MSB 0x53 66 + #define BH1745_BLUE_LSB 0x54 67 + #define BH1745_BLUE_MSB 0x55 68 + #define BH1745_CLEAR_LSB 0x56 69 + #define BH1745_CLEAR_MSB 0x57 70 + 71 + #define BH1745_MANU_ID_REG 0x92 72 + 73 + /* From 16x max HW gain and 32x max integration time */ 74 + #define BH1745_MAX_GAIN 512 75 + 76 + enum bh1745_int_source { 77 + BH1745_INTR_SOURCE_RED, 78 + BH1745_INTR_SOURCE_GREEN, 79 + BH1745_INTR_SOURCE_BLUE, 80 + BH1745_INTR_SOURCE_CLEAR, 81 + }; 82 + 83 + enum bh1745_gain { 84 + BH1745_ADC_GAIN_1X, 85 + BH1745_ADC_GAIN_2X, 86 + BH1745_ADC_GAIN_16X, 87 + }; 88 + 89 + enum bh1745_measurement_time { 90 + BH1745_MEASUREMENT_TIME_160MS, 91 + BH1745_MEASUREMENT_TIME_320MS, 92 + BH1745_MEASUREMENT_TIME_640MS, 93 + BH1745_MEASUREMENT_TIME_1280MS, 94 + BH1745_MEASUREMENT_TIME_2560MS, 95 + BH1745_MEASUREMENT_TIME_5120MS, 96 + }; 97 + 98 + enum bh1745_presistence_value { 99 + BH1745_PRESISTENCE_UPDATE_TOGGLE, 100 + BH1745_PRESISTENCE_UPDATE_EACH_MEASUREMENT, 101 + BH1745_PRESISTENCE_UPDATE_FOUR_MEASUREMENT, 102 + BH1745_PRESISTENCE_UPDATE_EIGHT_MEASUREMENT, 103 + }; 104 + 105 + static const struct iio_gain_sel_pair bh1745_gain[] = { 106 + GAIN_SCALE_GAIN(1, BH1745_ADC_GAIN_1X), 107 + GAIN_SCALE_GAIN(2, BH1745_ADC_GAIN_2X), 108 + GAIN_SCALE_GAIN(16, BH1745_ADC_GAIN_16X), 109 + }; 110 + 111 + static const struct iio_itime_sel_mul bh1745_itimes[] = { 112 + GAIN_SCALE_ITIME_US(5120000, BH1745_MEASUREMENT_TIME_5120MS, 32), 113 + GAIN_SCALE_ITIME_US(2560000, BH1745_MEASUREMENT_TIME_2560MS, 16), 114 + GAIN_SCALE_ITIME_US(1280000, BH1745_MEASUREMENT_TIME_1280MS, 8), 115 + GAIN_SCALE_ITIME_US(640000, BH1745_MEASUREMENT_TIME_640MS, 4), 116 + GAIN_SCALE_ITIME_US(320000, BH1745_MEASUREMENT_TIME_320MS, 2), 117 + GAIN_SCALE_ITIME_US(160000, BH1745_MEASUREMENT_TIME_160MS, 1), 118 + }; 119 + 120 + struct bh1745_data { 121 + /* 122 + * Lock to prevent device setting update or read before 123 + * related calculations are completed 124 + */ 125 + struct mutex lock; 126 + struct regmap *regmap; 127 + struct device *dev; 128 + struct iio_trigger *trig; 129 + struct iio_gts gts; 130 + }; 131 + 132 + static const struct regmap_range bh1745_volatile_ranges[] = { 133 + regmap_reg_range(BH1745_MODE_CTRL2, BH1745_MODE_CTRL2), /* VALID */ 134 + regmap_reg_range(BH1745_RED_LSB, BH1745_CLEAR_MSB), /* Data */ 135 + regmap_reg_range(BH1745_INTR, BH1745_INTR), /* Interrupt */ 136 + }; 137 + 138 + static const struct regmap_access_table bh1745_volatile_regs = { 139 + .yes_ranges = bh1745_volatile_ranges, 140 + .n_yes_ranges = ARRAY_SIZE(bh1745_volatile_ranges), 141 + }; 142 + 143 + static const struct regmap_range bh1745_readable_ranges[] = { 144 + regmap_reg_range(BH1745_SYS_CTRL, BH1745_MODE_CTRL2), 145 + regmap_reg_range(BH1745_RED_LSB, BH1745_CLEAR_MSB), 146 + regmap_reg_range(BH1745_INTR, BH1745_INTR), 147 + regmap_reg_range(BH1745_PERSISTENCE, BH1745_TL_MSB), 148 + regmap_reg_range(BH1745_MANU_ID_REG, BH1745_MANU_ID_REG), 149 + }; 150 + 151 + static const struct regmap_access_table bh1745_readable_regs = { 152 + .yes_ranges = bh1745_readable_ranges, 153 + .n_yes_ranges = ARRAY_SIZE(bh1745_readable_ranges), 154 + }; 155 + 156 + static const struct regmap_range bh1745_writable_ranges[] = { 157 + regmap_reg_range(BH1745_SYS_CTRL, BH1745_MODE_CTRL2), 158 + regmap_reg_range(BH1745_INTR, BH1745_INTR), 159 + regmap_reg_range(BH1745_PERSISTENCE, BH1745_TL_MSB), 160 + }; 161 + 162 + static const struct regmap_access_table bh1745_writable_regs = { 163 + .yes_ranges = bh1745_writable_ranges, 164 + .n_yes_ranges = ARRAY_SIZE(bh1745_writable_ranges), 165 + }; 166 + 167 + static const struct regmap_config bh1745_regmap = { 168 + .reg_bits = 8, 169 + .val_bits = 8, 170 + .max_register = BH1745_MANU_ID_REG, 171 + .cache_type = REGCACHE_RBTREE, 172 + .volatile_table = &bh1745_volatile_regs, 173 + .wr_table = &bh1745_writable_regs, 174 + .rd_table = &bh1745_readable_regs, 175 + }; 176 + 177 + static const struct iio_event_spec bh1745_event_spec[] = { 178 + { 179 + .type = IIO_EV_TYPE_THRESH, 180 + .dir = IIO_EV_DIR_RISING, 181 + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE), 182 + }, 183 + { 184 + .type = IIO_EV_TYPE_THRESH, 185 + .dir = IIO_EV_DIR_FALLING, 186 + .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE), 187 + }, 188 + { 189 + .type = IIO_EV_TYPE_THRESH, 190 + .dir = IIO_EV_DIR_EITHER, 191 + .mask_shared_by_type = BIT(IIO_EV_INFO_PERIOD), 192 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 193 + }, 194 + }; 195 + 196 + #define BH1745_CHANNEL(_colour, _si, _addr) \ 197 + { \ 198 + .type = IIO_INTENSITY, .modified = 1, \ 199 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 200 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE) | \ 201 + BIT(IIO_CHAN_INFO_INT_TIME), \ 202 + .info_mask_shared_by_all_available = \ 203 + BIT(IIO_CHAN_INFO_SCALE) | \ 204 + BIT(IIO_CHAN_INFO_INT_TIME), \ 205 + .event_spec = bh1745_event_spec, \ 206 + .num_event_specs = ARRAY_SIZE(bh1745_event_spec), \ 207 + .channel2 = IIO_MOD_LIGHT_##_colour, .address = _addr, \ 208 + .scan_index = _si, \ 209 + .scan_type = { \ 210 + .sign = 'u', \ 211 + .realbits = 16, \ 212 + .storagebits = 16, \ 213 + .endianness = IIO_CPU, \ 214 + }, \ 215 + } 216 + 217 + static const struct iio_chan_spec bh1745_channels[] = { 218 + BH1745_CHANNEL(RED, 0, BH1745_RED_LSB), 219 + BH1745_CHANNEL(GREEN, 1, BH1745_GREEN_LSB), 220 + BH1745_CHANNEL(BLUE, 2, BH1745_BLUE_LSB), 221 + BH1745_CHANNEL(CLEAR, 3, BH1745_CLEAR_LSB), 222 + IIO_CHAN_SOFT_TIMESTAMP(4), 223 + }; 224 + 225 + static int bh1745_reset(struct bh1745_data *data) 226 + { 227 + return regmap_set_bits(data->regmap, BH1745_SYS_CTRL, 228 + BH1745_SYS_CTRL_SW_RESET | 229 + BH1745_SYS_CTRL_INTR_RESET); 230 + } 231 + 232 + static int bh1745_power_on(struct bh1745_data *data) 233 + { 234 + return regmap_set_bits(data->regmap, BH1745_MODE_CTRL2, 235 + BH1745_CTRL2_RGBC_EN); 236 + } 237 + 238 + static void bh1745_power_off(void *data_ptr) 239 + { 240 + struct bh1745_data *data = data_ptr; 241 + struct device *dev = data->dev; 242 + int ret; 243 + 244 + ret = regmap_clear_bits(data->regmap, BH1745_MODE_CTRL2, 245 + BH1745_CTRL2_RGBC_EN); 246 + if (ret) 247 + dev_err(dev, "Failed to turn off device\n"); 248 + } 249 + 250 + static int bh1745_get_scale(struct bh1745_data *data, int *val, int *val2) 251 + { 252 + int ret; 253 + int value; 254 + int gain_sel, int_time_sel; 255 + int gain; 256 + const struct iio_itime_sel_mul *int_time; 257 + 258 + ret = regmap_read(data->regmap, BH1745_MODE_CTRL2, &value); 259 + if (ret) 260 + return ret; 261 + 262 + gain_sel = FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK, value); 263 + gain = iio_gts_find_gain_by_sel(&data->gts, gain_sel); 264 + 265 + ret = regmap_read(data->regmap, BH1745_MODE_CTRL1, &value); 266 + if (ret) 267 + return ret; 268 + 269 + int_time_sel = FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK, value); 270 + int_time = iio_gts_find_itime_by_sel(&data->gts, int_time_sel); 271 + 272 + return iio_gts_get_scale(&data->gts, gain, int_time->time_us, val, 273 + val2); 274 + } 275 + 276 + static int bh1745_set_scale(struct bh1745_data *data, int val) 277 + { 278 + struct device *dev = data->dev; 279 + int ret; 280 + int value; 281 + int hw_gain_sel, current_int_time_sel, new_int_time_sel; 282 + 283 + ret = regmap_read(data->regmap, BH1745_MODE_CTRL1, &value); 284 + if (ret) 285 + return ret; 286 + 287 + current_int_time_sel = FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK, 288 + value); 289 + ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, 290 + current_int_time_sel, 291 + val, 0, &hw_gain_sel); 292 + if (ret) { 293 + for (int i = 0; i < data->gts.num_itime; i++) { 294 + new_int_time_sel = data->gts.itime_table[i].sel; 295 + 296 + if (new_int_time_sel == current_int_time_sel) 297 + continue; 298 + 299 + ret = iio_gts_find_gain_sel_for_scale_using_time(&data->gts, 300 + new_int_time_sel, 301 + val, 0, 302 + &hw_gain_sel); 303 + if (!ret) 304 + break; 305 + } 306 + 307 + if (ret) { 308 + dev_dbg(dev, "Unsupported scale value requested: %d\n", 309 + val); 310 + return -EINVAL; 311 + } 312 + 313 + ret = regmap_write_bits(data->regmap, BH1745_MODE_CTRL1, 314 + BH1745_CTRL1_MEASUREMENT_TIME_MASK, 315 + new_int_time_sel); 316 + if (ret) 317 + return ret; 318 + } 319 + 320 + return regmap_write_bits(data->regmap, BH1745_MODE_CTRL2, 321 + BH1745_CTRL2_ADC_GAIN_MASK, hw_gain_sel); 322 + } 323 + 324 + static int bh1745_get_int_time(struct bh1745_data *data, int *val) 325 + { 326 + int ret; 327 + int value; 328 + int int_time, int_time_sel; 329 + 330 + ret = regmap_read(data->regmap, BH1745_MODE_CTRL1, &value); 331 + if (ret) 332 + return ret; 333 + 334 + int_time_sel = FIELD_GET(BH1745_CTRL1_MEASUREMENT_TIME_MASK, value); 335 + int_time = iio_gts_find_int_time_by_sel(&data->gts, int_time_sel); 336 + if (int_time < 0) 337 + return int_time; 338 + 339 + *val = int_time; 340 + 341 + return 0; 342 + } 343 + 344 + static int bh1745_set_int_time(struct bh1745_data *data, int val, int val2) 345 + { 346 + struct device *dev = data->dev; 347 + int ret; 348 + int value; 349 + int current_int_time, current_hwgain_sel, current_hwgain; 350 + int new_hwgain, new_hwgain_sel, new_int_time_sel; 351 + int req_int_time = (1000000 * val) + val2; 352 + 353 + if (!iio_gts_valid_time(&data->gts, req_int_time)) { 354 + dev_dbg(dev, "Unsupported integration time requested: %d\n", 355 + req_int_time); 356 + return -EINVAL; 357 + } 358 + 359 + ret = bh1745_get_int_time(data, &current_int_time); 360 + if (ret) 361 + return ret; 362 + 363 + if (current_int_time == req_int_time) 364 + return 0; 365 + 366 + ret = regmap_read(data->regmap, BH1745_MODE_CTRL2, &value); 367 + if (ret) 368 + return ret; 369 + 370 + current_hwgain_sel = FIELD_GET(BH1745_CTRL2_ADC_GAIN_MASK, value); 371 + current_hwgain = iio_gts_find_gain_by_sel(&data->gts, 372 + current_hwgain_sel); 373 + ret = iio_gts_find_new_gain_by_old_gain_time(&data->gts, current_hwgain, 374 + current_int_time, 375 + req_int_time, 376 + &new_hwgain); 377 + if (new_hwgain < 0) { 378 + dev_dbg(dev, "No corresponding gain for requested integration time\n"); 379 + return ret; 380 + } 381 + 382 + if (ret) { 383 + bool in_range; 384 + 385 + new_hwgain = iio_find_closest_gain_low(&data->gts, new_hwgain, 386 + &in_range); 387 + if (new_hwgain < 0) { 388 + new_hwgain = iio_gts_get_min_gain(&data->gts); 389 + if (new_hwgain < 0) 390 + return ret; 391 + } 392 + 393 + if (!in_range) 394 + dev_dbg(dev, "Optimal gain out of range\n"); 395 + 396 + dev_dbg(dev, "Scale changed, new hw_gain %d\n", new_hwgain); 397 + } 398 + 399 + new_hwgain_sel = iio_gts_find_sel_by_gain(&data->gts, new_hwgain); 400 + if (new_hwgain_sel < 0) 401 + return new_hwgain_sel; 402 + 403 + ret = regmap_write_bits(data->regmap, BH1745_MODE_CTRL2, 404 + BH1745_CTRL2_ADC_GAIN_MASK, 405 + new_hwgain_sel); 406 + if (ret) 407 + return ret; 408 + 409 + new_int_time_sel = iio_gts_find_sel_by_int_time(&data->gts, 410 + req_int_time); 411 + if (new_int_time_sel < 0) 412 + return new_int_time_sel; 413 + 414 + return regmap_write_bits(data->regmap, BH1745_MODE_CTRL1, 415 + BH1745_CTRL1_MEASUREMENT_TIME_MASK, 416 + new_int_time_sel); 417 + } 418 + 419 + static int bh1745_read_raw(struct iio_dev *indio_dev, 420 + struct iio_chan_spec const *chan, 421 + int *val, int *val2, long mask) 422 + { 423 + struct bh1745_data *data = iio_priv(indio_dev); 424 + int ret; 425 + int value; 426 + 427 + switch (mask) { 428 + case IIO_CHAN_INFO_RAW: 429 + iio_device_claim_direct_scoped(return -EBUSY, indio_dev) { 430 + ret = regmap_bulk_read(data->regmap, chan->address, 431 + &value, 2); 432 + if (ret) 433 + return ret; 434 + *val = value; 435 + 436 + return IIO_VAL_INT; 437 + } 438 + unreachable(); 439 + 440 + case IIO_CHAN_INFO_SCALE: { 441 + guard(mutex)(&data->lock); 442 + ret = bh1745_get_scale(data, val, val2); 443 + if (ret) 444 + return ret; 445 + 446 + return IIO_VAL_INT; 447 + } 448 + 449 + case IIO_CHAN_INFO_INT_TIME: { 450 + guard(mutex)(&data->lock); 451 + *val = 0; 452 + ret = bh1745_get_int_time(data, val2); 453 + if (ret) 454 + return 0; 455 + 456 + return IIO_VAL_INT_PLUS_MICRO; 457 + } 458 + 459 + default: 460 + return -EINVAL; 461 + } 462 + } 463 + 464 + static int bh1745_write_raw(struct iio_dev *indio_dev, 465 + struct iio_chan_spec const *chan, 466 + int val, int val2, long mask) 467 + { 468 + struct bh1745_data *data = iio_priv(indio_dev); 469 + 470 + guard(mutex)(&data->lock); 471 + switch (mask) { 472 + case IIO_CHAN_INFO_SCALE: 473 + return bh1745_set_scale(data, val); 474 + 475 + case IIO_CHAN_INFO_INT_TIME: 476 + return bh1745_set_int_time(data, val, val2); 477 + 478 + default: 479 + return -EINVAL; 480 + } 481 + } 482 + 483 + static int bh1745_write_raw_get_fmt(struct iio_dev *indio_dev, 484 + struct iio_chan_spec const *chan, 485 + long mask) 486 + { 487 + switch (mask) { 488 + case IIO_CHAN_INFO_SCALE: 489 + return IIO_VAL_INT; 490 + 491 + case IIO_CHAN_INFO_INT_TIME: 492 + return IIO_VAL_INT_PLUS_MICRO; 493 + 494 + default: 495 + return -EINVAL; 496 + } 497 + } 498 + 499 + static int bh1745_read_thresh(struct iio_dev *indio_dev, 500 + const struct iio_chan_spec *chan, 501 + enum iio_event_type type, 502 + enum iio_event_direction dir, 503 + enum iio_event_info info, int *val, int *val2) 504 + { 505 + struct bh1745_data *data = iio_priv(indio_dev); 506 + int ret; 507 + 508 + switch (info) { 509 + case IIO_EV_INFO_VALUE: 510 + switch (dir) { 511 + case IIO_EV_DIR_RISING: 512 + ret = regmap_bulk_read(data->regmap, BH1745_TH_LSB, 513 + val, 2); 514 + if (ret) 515 + return ret; 516 + 517 + return IIO_VAL_INT; 518 + 519 + case IIO_EV_DIR_FALLING: 520 + ret = regmap_bulk_read(data->regmap, BH1745_TL_LSB, 521 + val, 2); 522 + if (ret) 523 + return ret; 524 + 525 + return IIO_VAL_INT; 526 + 527 + default: 528 + return -EINVAL; 529 + } 530 + 531 + case IIO_EV_INFO_PERIOD: 532 + ret = regmap_read(data->regmap, BH1745_PERSISTENCE, val); 533 + if (ret) 534 + return ret; 535 + 536 + return IIO_VAL_INT; 537 + 538 + default: 539 + return -EINVAL; 540 + } 541 + } 542 + 543 + static int bh1745_write_thresh(struct iio_dev *indio_dev, 544 + const struct iio_chan_spec *chan, 545 + enum iio_event_type type, 546 + enum iio_event_direction dir, 547 + enum iio_event_info info, int val, int val2) 548 + { 549 + struct bh1745_data *data = iio_priv(indio_dev); 550 + int ret; 551 + 552 + switch (info) { 553 + case IIO_EV_INFO_VALUE: 554 + if (val < 0x0 || val > 0xFFFF) 555 + return -EINVAL; 556 + 557 + switch (dir) { 558 + case IIO_EV_DIR_RISING: 559 + ret = regmap_bulk_write(data->regmap, BH1745_TH_LSB, 560 + &val, 2); 561 + if (ret) 562 + return ret; 563 + 564 + return IIO_VAL_INT; 565 + 566 + case IIO_EV_DIR_FALLING: 567 + ret = regmap_bulk_write(data->regmap, BH1745_TL_LSB, 568 + &val, 2); 569 + if (ret) 570 + return ret; 571 + 572 + return IIO_VAL_INT; 573 + 574 + default: 575 + return -EINVAL; 576 + } 577 + 578 + case IIO_EV_INFO_PERIOD: 579 + if (val < BH1745_PRESISTENCE_UPDATE_TOGGLE || 580 + val > BH1745_PRESISTENCE_UPDATE_EIGHT_MEASUREMENT) 581 + return -EINVAL; 582 + ret = regmap_write(data->regmap, BH1745_PERSISTENCE, val); 583 + if (ret) 584 + return ret; 585 + 586 + return IIO_VAL_INT; 587 + 588 + default: 589 + return -EINVAL; 590 + } 591 + } 592 + 593 + static int bh1745_read_event_config(struct iio_dev *indio_dev, 594 + const struct iio_chan_spec *chan, 595 + enum iio_event_type type, 596 + enum iio_event_direction dir) 597 + { 598 + struct bh1745_data *data = iio_priv(indio_dev); 599 + int ret; 600 + int value; 601 + int int_src; 602 + 603 + ret = regmap_read(data->regmap, BH1745_INTR, &value); 604 + if (ret) 605 + return ret; 606 + 607 + if (!FIELD_GET(BH1745_INTR_ENABLE, value)) 608 + return 0; 609 + 610 + int_src = FIELD_GET(BH1745_INTR_SOURCE_MASK, value); 611 + 612 + switch (chan->channel2) { 613 + case IIO_MOD_LIGHT_RED: 614 + if (int_src == BH1745_INTR_SOURCE_RED) 615 + return 1; 616 + return 0; 617 + 618 + case IIO_MOD_LIGHT_GREEN: 619 + if (int_src == BH1745_INTR_SOURCE_GREEN) 620 + return 1; 621 + return 0; 622 + 623 + case IIO_MOD_LIGHT_BLUE: 624 + if (int_src == BH1745_INTR_SOURCE_BLUE) 625 + return 1; 626 + return 0; 627 + 628 + case IIO_MOD_LIGHT_CLEAR: 629 + if (int_src == BH1745_INTR_SOURCE_CLEAR) 630 + return 1; 631 + return 0; 632 + 633 + default: 634 + return -EINVAL; 635 + } 636 + } 637 + 638 + static int bh1745_write_event_config(struct iio_dev *indio_dev, 639 + const struct iio_chan_spec *chan, 640 + enum iio_event_type type, 641 + enum iio_event_direction dir, int state) 642 + { 643 + struct bh1745_data *data = iio_priv(indio_dev); 644 + int value; 645 + 646 + if (state == 0) 647 + return regmap_clear_bits(data->regmap, 648 + BH1745_INTR, BH1745_INTR_ENABLE); 649 + 650 + if (state == 1) { 651 + /* Latch is always enabled when enabling interrupt */ 652 + value = BH1745_INTR_ENABLE; 653 + 654 + switch (chan->channel2) { 655 + case IIO_MOD_LIGHT_RED: 656 + return regmap_write(data->regmap, BH1745_INTR, 657 + value | FIELD_PREP(BH1745_INTR_SOURCE_MASK, 658 + BH1745_INTR_SOURCE_RED)); 659 + 660 + case IIO_MOD_LIGHT_GREEN: 661 + return regmap_write(data->regmap, BH1745_INTR, 662 + value | FIELD_PREP(BH1745_INTR_SOURCE_MASK, 663 + BH1745_INTR_SOURCE_GREEN)); 664 + 665 + case IIO_MOD_LIGHT_BLUE: 666 + return regmap_write(data->regmap, BH1745_INTR, 667 + value | FIELD_PREP(BH1745_INTR_SOURCE_MASK, 668 + BH1745_INTR_SOURCE_BLUE)); 669 + 670 + case IIO_MOD_LIGHT_CLEAR: 671 + return regmap_write(data->regmap, BH1745_INTR, 672 + value | FIELD_PREP(BH1745_INTR_SOURCE_MASK, 673 + BH1745_INTR_SOURCE_CLEAR)); 674 + 675 + default: 676 + return -EINVAL; 677 + } 678 + } 679 + 680 + return -EINVAL; 681 + } 682 + 683 + static int bh1745_read_avail(struct iio_dev *indio_dev, 684 + struct iio_chan_spec const *chan, const int **vals, 685 + int *type, int *length, long mask) 686 + { 687 + struct bh1745_data *data = iio_priv(indio_dev); 688 + 689 + switch (mask) { 690 + case IIO_CHAN_INFO_INT_TIME: 691 + return iio_gts_avail_times(&data->gts, vals, type, length); 692 + 693 + case IIO_CHAN_INFO_SCALE: 694 + return iio_gts_all_avail_scales(&data->gts, vals, type, length); 695 + 696 + default: 697 + return -EINVAL; 698 + } 699 + } 700 + 701 + static const struct iio_info bh1745_info = { 702 + .read_raw = bh1745_read_raw, 703 + .write_raw = bh1745_write_raw, 704 + .write_raw_get_fmt = bh1745_write_raw_get_fmt, 705 + .read_event_value = bh1745_read_thresh, 706 + .write_event_value = bh1745_write_thresh, 707 + .read_event_config = bh1745_read_event_config, 708 + .write_event_config = bh1745_write_event_config, 709 + .read_avail = bh1745_read_avail, 710 + }; 711 + 712 + static irqreturn_t bh1745_interrupt_handler(int interrupt, void *p) 713 + { 714 + struct iio_dev *indio_dev = p; 715 + struct bh1745_data *data = iio_priv(indio_dev); 716 + int ret; 717 + int value; 718 + int int_src; 719 + 720 + ret = regmap_read(data->regmap, BH1745_INTR, &value); 721 + if (ret) 722 + return IRQ_NONE; 723 + 724 + int_src = FIELD_GET(BH1745_INTR_SOURCE_MASK, value); 725 + 726 + if (value & BH1745_INTR_STATUS) { 727 + iio_push_event(indio_dev, 728 + IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, int_src, 729 + IIO_EV_TYPE_THRESH, 730 + IIO_EV_DIR_EITHER), 731 + iio_get_time_ns(indio_dev)); 732 + 733 + return IRQ_HANDLED; 734 + } 735 + 736 + return IRQ_NONE; 737 + } 738 + 739 + static irqreturn_t bh1745_trigger_handler(int interrupt, void *p) 740 + { 741 + struct iio_poll_func *pf = p; 742 + struct iio_dev *indio_dev = pf->indio_dev; 743 + struct bh1745_data *data = iio_priv(indio_dev); 744 + struct { 745 + u16 chans[4]; 746 + s64 timestamp __aligned(8); 747 + } scan; 748 + u16 value; 749 + int ret; 750 + int i; 751 + int j = 0; 752 + 753 + iio_for_each_active_channel(indio_dev, i) { 754 + ret = regmap_bulk_read(data->regmap, BH1745_RED_LSB + 2 * i, 755 + &value, 2); 756 + if (ret) 757 + goto err; 758 + 759 + scan.chans[j++] = value; 760 + } 761 + 762 + iio_push_to_buffers_with_timestamp(indio_dev, &scan, 763 + iio_get_time_ns(indio_dev)); 764 + 765 + err: 766 + iio_trigger_notify_done(indio_dev->trig); 767 + 768 + return IRQ_HANDLED; 769 + } 770 + 771 + static int bh1745_setup_triggered_buffer(struct iio_dev *indio_dev, 772 + struct device *parent, 773 + int irq) 774 + { 775 + struct bh1745_data *data = iio_priv(indio_dev); 776 + struct device *dev = data->dev; 777 + int ret; 778 + 779 + ret = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL, 780 + bh1745_trigger_handler, NULL); 781 + if (ret) 782 + return dev_err_probe(dev, ret, 783 + "Triggered buffer setup failed\n"); 784 + 785 + if (irq) { 786 + ret = devm_request_threaded_irq(dev, irq, NULL, 787 + bh1745_interrupt_handler, 788 + IRQF_ONESHOT, 789 + "bh1745_interrupt", indio_dev); 790 + if (ret) 791 + return dev_err_probe(dev, ret, 792 + "Request for IRQ failed\n"); 793 + } 794 + 795 + return 0; 796 + } 797 + 798 + static int bh1745_init(struct bh1745_data *data) 799 + { 800 + int ret; 801 + struct device *dev = data->dev; 802 + 803 + mutex_init(&data->lock); 804 + 805 + ret = devm_iio_init_iio_gts(dev, BH1745_MAX_GAIN, 0, bh1745_gain, 806 + ARRAY_SIZE(bh1745_gain), bh1745_itimes, 807 + ARRAY_SIZE(bh1745_itimes), &data->gts); 808 + if (ret) 809 + return ret; 810 + 811 + ret = bh1745_reset(data); 812 + if (ret) 813 + return dev_err_probe(dev, ret, "Failed to reset sensor\n"); 814 + 815 + ret = bh1745_power_on(data); 816 + if (ret) 817 + return dev_err_probe(dev, ret, "Failed to turn on sensor\n"); 818 + 819 + ret = devm_add_action_or_reset(dev, bh1745_power_off, data); 820 + if (ret) 821 + return dev_err_probe(dev, ret, 822 + "Failed to add action or reset\n"); 823 + 824 + return 0; 825 + } 826 + 827 + static int bh1745_probe(struct i2c_client *client) 828 + { 829 + int ret; 830 + int value; 831 + int part_id; 832 + struct bh1745_data *data; 833 + struct iio_dev *indio_dev; 834 + struct device *dev = &client->dev; 835 + 836 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 837 + if (!indio_dev) 838 + return -ENOMEM; 839 + 840 + indio_dev->info = &bh1745_info; 841 + indio_dev->name = "bh1745"; 842 + indio_dev->channels = bh1745_channels; 843 + indio_dev->modes = INDIO_DIRECT_MODE; 844 + indio_dev->num_channels = ARRAY_SIZE(bh1745_channels); 845 + data = iio_priv(indio_dev); 846 + data->dev = &client->dev; 847 + data->regmap = devm_regmap_init_i2c(client, &bh1745_regmap); 848 + if (IS_ERR(data->regmap)) 849 + return dev_err_probe(dev, PTR_ERR(data->regmap), 850 + "Failed to initialize Regmap\n"); 851 + 852 + ret = regmap_read(data->regmap, BH1745_SYS_CTRL, &value); 853 + if (ret) 854 + return ret; 855 + 856 + part_id = FIELD_GET(BH1745_SYS_CTRL_PART_ID_MASK, value); 857 + if (part_id != BH1745_PART_ID) 858 + dev_warn(dev, "Unknown part ID 0x%x\n", part_id); 859 + 860 + ret = devm_regulator_get_enable(dev, "vdd"); 861 + if (ret) 862 + return dev_err_probe(dev, ret, 863 + "Failed to get and enable regulator\n"); 864 + 865 + ret = bh1745_init(data); 866 + if (ret) 867 + return ret; 868 + 869 + ret = bh1745_setup_triggered_buffer(indio_dev, indio_dev->dev.parent, 870 + client->irq); 871 + if (ret) 872 + return ret; 873 + 874 + ret = devm_iio_device_register(dev, indio_dev); 875 + if (ret) 876 + return dev_err_probe(dev, ret, "Failed to register device\n"); 877 + 878 + return 0; 879 + } 880 + 881 + static const struct i2c_device_id bh1745_idtable[] = { 882 + { "bh1745" }, 883 + { } 884 + }; 885 + MODULE_DEVICE_TABLE(i2c, bh1745_idtable); 886 + 887 + static const struct of_device_id bh1745_of_match[] = { 888 + { .compatible = "rohm,bh1745" }, 889 + { } 890 + }; 891 + MODULE_DEVICE_TABLE(of, bh1745_of_match); 892 + 893 + static struct i2c_driver bh1745_driver = { 894 + .driver = { 895 + .name = "bh1745", 896 + .of_match_table = bh1745_of_match, 897 + }, 898 + .probe = bh1745_probe, 899 + .id_table = bh1745_idtable, 900 + }; 901 + module_i2c_driver(bh1745_driver); 902 + 903 + MODULE_LICENSE("GPL"); 904 + MODULE_AUTHOR("Mudit Sharma <muditsharma.info@gmail.com>"); 905 + MODULE_DESCRIPTION("BH1745 colour sensor driver"); 906 + MODULE_IMPORT_NS(IIO_GTS_HELPER);
+1 -1
drivers/iio/light/gp2ap002.c
··· 420 420 return i2c_smbus_write_byte_data(i2c, reg, val); 421 421 } 422 422 423 - static struct regmap_bus gp2ap002_regmap_bus = { 423 + static const struct regmap_bus gp2ap002_regmap_bus = { 424 424 .reg_read = gp2ap002_regmap_i2c_read, 425 425 .reg_write = gp2ap002_regmap_i2c_write, 426 426 };
+3 -6
drivers/iio/light/gp2ap020a00f.c
··· 965 965 size_t d_size = 0; 966 966 int i, out_val, ret; 967 967 968 - for_each_set_bit(i, indio_dev->active_scan_mask, 969 - indio_dev->masklength) { 968 + iio_for_each_active_channel(indio_dev, i) { 970 969 ret = regmap_bulk_read(priv->regmap, 971 970 GP2AP020A00F_DATA_REG(i), 972 971 &priv->buffer[d_size], 2); ··· 1396 1397 * two separate IIO channels they are treated in the driver logic 1397 1398 * as if they were controlled independently. 1398 1399 */ 1399 - for_each_set_bit(i, indio_dev->active_scan_mask, 1400 - indio_dev->masklength) { 1400 + iio_for_each_active_channel(indio_dev, i) { 1401 1401 switch (i) { 1402 1402 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR: 1403 1403 err = gp2ap020a00f_exec_cmd(data, ··· 1433 1435 1434 1436 mutex_lock(&data->lock); 1435 1437 1436 - for_each_set_bit(i, indio_dev->active_scan_mask, 1437 - indio_dev->masklength) { 1438 + iio_for_each_active_channel(indio_dev, i) { 1438 1439 switch (i) { 1439 1440 case GP2AP020A00F_SCAN_MODE_LIGHT_CLEAR: 1440 1441 err = gp2ap020a00f_exec_cmd(data,
+1 -2
drivers/iio/light/isl29125.c
··· 181 181 struct isl29125_data *data = iio_priv(indio_dev); 182 182 int i, j = 0; 183 183 184 - for_each_set_bit(i, indio_dev->active_scan_mask, 185 - indio_dev->masklength) { 184 + iio_for_each_active_channel(indio_dev, i) { 186 185 int ret = i2c_smbus_read_word_data(data->client, 187 186 isl29125_regs[i].data); 188 187 if (ret < 0)
+220 -21
drivers/iio/light/ltr390.c
··· 23 23 #include <linux/module.h> 24 24 #include <linux/mutex.h> 25 25 #include <linux/regmap.h> 26 + #include <linux/bitfield.h> 26 27 27 28 #include <linux/iio/iio.h> 28 29 29 30 #include <asm/unaligned.h> 30 31 31 - #define LTR390_MAIN_CTRL 0x00 32 - #define LTR390_PART_ID 0x06 33 - #define LTR390_UVS_DATA 0x10 32 + #define LTR390_MAIN_CTRL 0x00 33 + #define LTR390_ALS_UVS_MEAS_RATE 0x04 34 + #define LTR390_ALS_UVS_GAIN 0x05 35 + #define LTR390_PART_ID 0x06 36 + #define LTR390_ALS_DATA 0x0D 37 + #define LTR390_UVS_DATA 0x10 38 + #define LTR390_INT_CFG 0x19 39 + 40 + #define LTR390_PART_NUMBER_ID 0xb 41 + #define LTR390_ALS_UVS_GAIN_MASK 0x07 42 + #define LTR390_ALS_UVS_INT_TIME_MASK 0x70 43 + #define LTR390_ALS_UVS_INT_TIME(x) FIELD_PREP(LTR390_ALS_UVS_INT_TIME_MASK, (x)) 34 44 35 45 #define LTR390_SW_RESET BIT(4) 36 46 #define LTR390_UVS_MODE BIT(3) 37 47 #define LTR390_SENSOR_ENABLE BIT(1) 38 48 39 - #define LTR390_PART_NUMBER_ID 0xb 49 + #define LTR390_FRACTIONAL_PRECISION 100 40 50 41 51 /* 42 52 * At 20-bit resolution (integration time: 400ms) and 18x gain, 2300 counts of ··· 65 55 */ 66 56 #define LTR390_WINDOW_FACTOR 1 67 57 58 + enum ltr390_mode { 59 + LTR390_SET_ALS_MODE, 60 + LTR390_SET_UVS_MODE, 61 + }; 62 + 68 63 struct ltr390_data { 69 64 struct regmap *regmap; 70 65 struct i2c_client *client; 71 66 /* Protects device from simulataneous reads */ 72 67 struct mutex lock; 68 + enum ltr390_mode mode; 69 + int gain; 70 + int int_time_us; 73 71 }; 74 72 75 73 static const struct regmap_config ltr390_regmap_config = { ··· 93 75 int ret; 94 76 u8 recieve_buffer[3]; 95 77 96 - guard(mutex)(&data->lock); 97 - 98 78 ret = regmap_bulk_read(data->regmap, register_address, recieve_buffer, 99 79 sizeof(recieve_buffer)); 100 80 if (ret) { ··· 103 87 return get_unaligned_le24(recieve_buffer); 104 88 } 105 89 90 + static int ltr390_set_mode(struct ltr390_data *data, enum ltr390_mode mode) 91 + { 92 + int ret; 93 + 94 + if (data->mode == mode) 95 + return 0; 96 + 97 + switch (mode) { 98 + case LTR390_SET_ALS_MODE: 99 + ret = regmap_clear_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_UVS_MODE); 100 + break; 101 + 102 + case LTR390_SET_UVS_MODE: 103 + ret = regmap_set_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_UVS_MODE); 104 + break; 105 + } 106 + 107 + if (ret) 108 + return ret; 109 + 110 + data->mode = mode; 111 + return 0; 112 + } 113 + 114 + static int ltr390_counts_per_uvi(struct ltr390_data *data) 115 + { 116 + const int orig_gain = 18; 117 + const int orig_int_time = 400; 118 + 119 + return DIV_ROUND_CLOSEST(23 * data->gain * data->int_time_us, 10 * orig_gain * orig_int_time); 120 + } 121 + 106 122 static int ltr390_read_raw(struct iio_dev *iio_device, 107 123 struct iio_chan_spec const *chan, int *val, 108 124 int *val2, long mask) ··· 142 94 int ret; 143 95 struct ltr390_data *data = iio_priv(iio_device); 144 96 97 + guard(mutex)(&data->lock); 145 98 switch (mask) { 146 99 case IIO_CHAN_INFO_RAW: 147 - ret = ltr390_register_read(data, LTR390_UVS_DATA); 148 - if (ret < 0) 149 - return ret; 100 + switch (chan->type) { 101 + case IIO_UVINDEX: 102 + ret = ltr390_set_mode(data, LTR390_SET_UVS_MODE); 103 + if (ret < 0) 104 + return ret; 105 + 106 + ret = ltr390_register_read(data, LTR390_UVS_DATA); 107 + if (ret < 0) 108 + return ret; 109 + break; 110 + 111 + case IIO_LIGHT: 112 + ret = ltr390_set_mode(data, LTR390_SET_ALS_MODE); 113 + if (ret < 0) 114 + return ret; 115 + 116 + ret = ltr390_register_read(data, LTR390_ALS_DATA); 117 + if (ret < 0) 118 + return ret; 119 + break; 120 + 121 + default: 122 + return -EINVAL; 123 + } 150 124 *val = ret; 151 125 return IIO_VAL_INT; 152 126 case IIO_CHAN_INFO_SCALE: 153 - *val = LTR390_WINDOW_FACTOR; 154 - *val2 = LTR390_COUNTS_PER_UVI; 155 - return IIO_VAL_FRACTIONAL; 127 + switch (chan->type) { 128 + case IIO_UVINDEX: 129 + *val = LTR390_WINDOW_FACTOR * LTR390_FRACTIONAL_PRECISION; 130 + *val2 = ltr390_counts_per_uvi(data); 131 + return IIO_VAL_FRACTIONAL; 132 + 133 + case IIO_LIGHT: 134 + *val = LTR390_WINDOW_FACTOR * 6 * 100; 135 + *val2 = data->gain * data->int_time_us; 136 + return IIO_VAL_FRACTIONAL; 137 + 138 + default: 139 + return -EINVAL; 140 + } 141 + 142 + case IIO_CHAN_INFO_INT_TIME: 143 + *val = data->int_time_us; 144 + return IIO_VAL_INT; 145 + 146 + default: 147 + return -EINVAL; 148 + } 149 + } 150 + 151 + /* integration time in us */ 152 + static const int ltr390_int_time_map_us[] = { 400000, 200000, 100000, 50000, 25000, 12500 }; 153 + static const int ltr390_gain_map[] = { 1, 3, 6, 9, 18 }; 154 + 155 + static const struct iio_chan_spec ltr390_channels[] = { 156 + /* UV sensor */ 157 + { 158 + .type = IIO_UVINDEX, 159 + .scan_index = 0, 160 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 161 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), 162 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) 163 + }, 164 + /* ALS sensor */ 165 + { 166 + .type = IIO_LIGHT, 167 + .scan_index = 1, 168 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 169 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), 170 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME) | BIT(IIO_CHAN_INFO_SCALE) 171 + }, 172 + }; 173 + 174 + static int ltr390_set_gain(struct ltr390_data *data, int val) 175 + { 176 + int ret, idx; 177 + 178 + for (idx = 0; idx < ARRAY_SIZE(ltr390_gain_map); idx++) { 179 + if (ltr390_gain_map[idx] != val) 180 + continue; 181 + 182 + guard(mutex)(&data->lock); 183 + ret = regmap_update_bits(data->regmap, 184 + LTR390_ALS_UVS_GAIN, 185 + LTR390_ALS_UVS_GAIN_MASK, idx); 186 + if (ret) 187 + return ret; 188 + 189 + data->gain = ltr390_gain_map[idx]; 190 + return 0; 191 + } 192 + 193 + return -EINVAL; 194 + } 195 + 196 + static int ltr390_set_int_time(struct ltr390_data *data, int val) 197 + { 198 + int ret, idx; 199 + 200 + for (idx = 0; idx < ARRAY_SIZE(ltr390_int_time_map_us); idx++) { 201 + if (ltr390_int_time_map_us[idx] != val) 202 + continue; 203 + 204 + guard(mutex)(&data->lock); 205 + ret = regmap_update_bits(data->regmap, 206 + LTR390_ALS_UVS_MEAS_RATE, 207 + LTR390_ALS_UVS_INT_TIME_MASK, 208 + LTR390_ALS_UVS_INT_TIME(idx)); 209 + if (ret) 210 + return ret; 211 + 212 + data->int_time_us = ltr390_int_time_map_us[idx]; 213 + return 0; 214 + } 215 + 216 + return -EINVAL; 217 + } 218 + 219 + static int ltr390_read_avail(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 220 + const int **vals, int *type, int *length, long mask) 221 + { 222 + switch (mask) { 223 + case IIO_CHAN_INFO_SCALE: 224 + *length = ARRAY_SIZE(ltr390_gain_map); 225 + *type = IIO_VAL_INT; 226 + *vals = ltr390_gain_map; 227 + return IIO_AVAIL_LIST; 228 + case IIO_CHAN_INFO_INT_TIME: 229 + *length = ARRAY_SIZE(ltr390_int_time_map_us); 230 + *type = IIO_VAL_INT; 231 + *vals = ltr390_int_time_map_us; 232 + return IIO_AVAIL_LIST; 233 + default: 234 + return -EINVAL; 235 + } 236 + } 237 + 238 + static int ltr390_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, 239 + int val, int val2, long mask) 240 + { 241 + struct ltr390_data *data = iio_priv(indio_dev); 242 + 243 + switch (mask) { 244 + case IIO_CHAN_INFO_SCALE: 245 + if (val2 != 0) 246 + return -EINVAL; 247 + 248 + return ltr390_set_gain(data, val); 249 + 250 + case IIO_CHAN_INFO_INT_TIME: 251 + if (val2 != 0) 252 + return -EINVAL; 253 + 254 + return ltr390_set_int_time(data, val); 255 + 156 256 default: 157 257 return -EINVAL; 158 258 } ··· 308 112 309 113 static const struct iio_info ltr390_info = { 310 114 .read_raw = ltr390_read_raw, 311 - }; 312 - 313 - static const struct iio_chan_spec ltr390_channel = { 314 - .type = IIO_UVINDEX, 315 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) 115 + .write_raw = ltr390_write_raw, 116 + .read_avail = ltr390_read_avail, 316 117 }; 317 118 318 119 static int ltr390_probe(struct i2c_client *client) ··· 332 139 "regmap initialization failed\n"); 333 140 334 141 data->client = client; 142 + /* default value of integration time from pg: 15 of the datasheet */ 143 + data->int_time_us = 100000; 144 + /* default value of gain from pg: 16 of the datasheet */ 145 + data->gain = 3; 146 + /* default mode for ltr390 is ALS mode */ 147 + data->mode = LTR390_SET_ALS_MODE; 148 + 335 149 mutex_init(&data->lock); 336 150 337 151 indio_dev->info = &ltr390_info; 338 - indio_dev->channels = &ltr390_channel; 339 - indio_dev->num_channels = 1; 152 + indio_dev->channels = ltr390_channels; 153 + indio_dev->num_channels = ARRAY_SIZE(ltr390_channels); 340 154 indio_dev->name = "ltr390"; 341 155 342 156 ret = regmap_read(data->regmap, LTR390_PART_ID, &part_number); ··· 361 161 /* Wait for the registers to reset before proceeding */ 362 162 usleep_range(1000, 2000); 363 163 364 - ret = regmap_set_bits(data->regmap, LTR390_MAIN_CTRL, 365 - LTR390_SENSOR_ENABLE | LTR390_UVS_MODE); 164 + ret = regmap_set_bits(data->regmap, LTR390_MAIN_CTRL, LTR390_SENSOR_ENABLE); 366 165 if (ret) 367 166 return dev_err_probe(dev, ret, "failed to enable the sensor\n"); 368 167
+43 -10
drivers/iio/light/ltrf216a.c
··· 68 68 { 25, 0x40 }, 69 69 }; 70 70 71 + struct ltr_chip_info { 72 + /* Chip contains CLEAR_DATA_0/1/2 registers at offset 0xa..0xc */ 73 + bool has_clear_data; 74 + /* Lux calculation multiplier for ALS data */ 75 + int lux_multiplier; 76 + }; 77 + 71 78 /* 72 79 * Window Factor is needed when the device is under Window glass 73 80 * with coated tinted ink. This is to compensate for the light loss ··· 86 79 struct ltrf216a_data { 87 80 struct regmap *regmap; 88 81 struct i2c_client *client; 82 + const struct ltr_chip_info *info; 89 83 u32 int_time; 90 84 u16 int_time_fac; 91 85 u8 als_gain_fac; ··· 254 246 255 247 ltrf216a_set_power_state(data, false); 256 248 257 - lux = greendata * 45 * LTRF216A_WIN_FAC; 249 + lux = greendata * data->info->lux_multiplier * LTRF216A_WIN_FAC; 258 250 259 251 return lux; 260 252 } ··· 342 334 343 335 static bool ltrf216a_readable_reg(struct device *dev, unsigned int reg) 344 336 { 337 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 338 + struct ltrf216a_data *data = iio_priv(indio_dev); 339 + 345 340 switch (reg) { 346 341 case LTRF216A_MAIN_CTRL: 347 342 case LTRF216A_ALS_MEAS_RES: 348 343 case LTRF216A_ALS_GAIN: 349 344 case LTRF216A_PART_ID: 350 345 case LTRF216A_MAIN_STATUS: 351 - case LTRF216A_ALS_CLEAR_DATA_0: 352 - case LTRF216A_ALS_CLEAR_DATA_1: 353 - case LTRF216A_ALS_CLEAR_DATA_2: 354 346 case LTRF216A_ALS_DATA_0: 355 347 case LTRF216A_ALS_DATA_1: 356 348 case LTRF216A_ALS_DATA_2: ··· 363 355 case LTRF216A_ALS_THRES_LOW_1: 364 356 case LTRF216A_ALS_THRES_LOW_2: 365 357 return true; 358 + case LTRF216A_ALS_CLEAR_DATA_0: 359 + case LTRF216A_ALS_CLEAR_DATA_1: 360 + case LTRF216A_ALS_CLEAR_DATA_2: 361 + return data->info->has_clear_data; 366 362 default: 367 363 return false; 368 364 } ··· 394 382 395 383 static bool ltrf216a_volatile_reg(struct device *dev, unsigned int reg) 396 384 { 385 + struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 386 + struct ltrf216a_data *data = iio_priv(indio_dev); 387 + 397 388 switch (reg) { 398 389 case LTRF216A_MAIN_STATUS: 399 - case LTRF216A_ALS_CLEAR_DATA_0: 400 - case LTRF216A_ALS_CLEAR_DATA_1: 401 - case LTRF216A_ALS_CLEAR_DATA_2: 402 390 case LTRF216A_ALS_DATA_0: 403 391 case LTRF216A_ALS_DATA_1: 404 392 case LTRF216A_ALS_DATA_2: 405 393 return true; 394 + /* 395 + * If these registers are not present on a chip (like LTR-308), 396 + * the missing registers are not considered volatile. 397 + */ 398 + case LTRF216A_ALS_CLEAR_DATA_0: 399 + case LTRF216A_ALS_CLEAR_DATA_1: 400 + case LTRF216A_ALS_CLEAR_DATA_2: 401 + return data->info->has_clear_data; 406 402 default: 407 403 return false; 408 404 } ··· 453 433 454 434 i2c_set_clientdata(client, indio_dev); 455 435 data->client = client; 436 + data->info = i2c_get_match_data(client); 456 437 457 438 mutex_init(&data->lock); 458 439 ··· 541 520 static DEFINE_RUNTIME_DEV_PM_OPS(ltrf216a_pm_ops, ltrf216a_runtime_suspend, 542 521 ltrf216a_runtime_resume, NULL); 543 522 523 + static const struct ltr_chip_info ltr308_chip_info = { 524 + .has_clear_data = false, 525 + .lux_multiplier = 60, 526 + }; 527 + 528 + static const struct ltr_chip_info ltrf216a_chip_info = { 529 + .has_clear_data = true, 530 + .lux_multiplier = 45, 531 + }; 532 + 544 533 static const struct i2c_device_id ltrf216a_id[] = { 545 - { "ltrf216a" }, 534 + { "ltr308", .driver_data = (kernel_ulong_t)&ltr308_chip_info }, 535 + { "ltrf216a", .driver_data = (kernel_ulong_t)&ltrf216a_chip_info }, 546 536 {} 547 537 }; 548 538 MODULE_DEVICE_TABLE(i2c, ltrf216a_id); 549 539 550 540 static const struct of_device_id ltrf216a_of_match[] = { 551 - { .compatible = "liteon,ltrf216a" }, 552 - { .compatible = "ltr,ltrf216a" }, 541 + { .compatible = "liteon,ltr308", .data = &ltr308_chip_info }, 542 + { .compatible = "liteon,ltrf216a", .data = &ltrf216a_chip_info }, 543 + { .compatible = "ltr,ltrf216a", .data = &ltrf216a_chip_info }, 553 544 {} 554 545 }; 555 546 MODULE_DEVICE_TABLE(of, ltrf216a_of_match);
+109 -62
drivers/iio/light/noa1305.c
··· 29 29 #define NOA1305_INTEGR_TIME_25MS 0x05 30 30 #define NOA1305_INTEGR_TIME_12_5MS 0x06 31 31 #define NOA1305_INTEGR_TIME_6_25MS 0x07 32 + #define NOA1305_INTEGR_TIME_MASK 0x07 32 33 #define NOA1305_REG_INT_SELECT 0x3 33 34 #define NOA1305_INT_SEL_ACTIVE_HIGH 0x01 34 35 #define NOA1305_INT_SEL_ACTIVE_LOW 0x02 ··· 44 43 #define NOA1305_DEVICE_ID 0x0519 45 44 #define NOA1305_DRIVER_NAME "noa1305" 46 45 46 + static int noa1305_scale_available[] = { 47 + 100, 8 * 77, /* 800 ms */ 48 + 100, 4 * 77, /* 400 ms */ 49 + 100, 2 * 77, /* 200 ms */ 50 + 100, 1 * 77, /* 100 ms */ 51 + 1000, 5 * 77, /* 50 ms */ 52 + 10000, 25 * 77, /* 25 ms */ 53 + 100000, 125 * 77, /* 12.5 ms */ 54 + 1000000, 625 * 77, /* 6.25 ms */ 55 + }; 56 + 57 + static int noa1305_int_time_available[] = { 58 + 0, 800000, /* 800 ms */ 59 + 0, 400000, /* 400 ms */ 60 + 0, 200000, /* 200 ms */ 61 + 0, 100000, /* 100 ms */ 62 + 0, 50000, /* 50 ms */ 63 + 0, 25000, /* 25 ms */ 64 + 0, 12500, /* 12.5 ms */ 65 + 0, 6250, /* 6.25 ms */ 66 + }; 67 + 47 68 struct noa1305_priv { 48 69 struct i2c_client *client; 49 70 struct regmap *regmap; 50 71 }; 51 72 52 - static int noa1305_measure(struct noa1305_priv *priv) 73 + static int noa1305_measure(struct noa1305_priv *priv, int *val) 53 74 { 54 75 __le16 data; 55 76 int ret; ··· 81 58 if (ret < 0) 82 59 return ret; 83 60 84 - return le16_to_cpu(data); 61 + *val = le16_to_cpu(data); 62 + 63 + return IIO_VAL_INT; 85 64 } 86 65 87 66 static int noa1305_scale(struct noa1305_priv *priv, int *val, int *val2) ··· 101 76 * Integration Constant = 7.7 102 77 * Integration Time in Seconds 103 78 */ 104 - switch (data) { 105 - case NOA1305_INTEGR_TIME_800MS: 106 - *val = 100; 107 - *val2 = 77 * 8; 108 - break; 109 - case NOA1305_INTEGR_TIME_400MS: 110 - *val = 100; 111 - *val2 = 77 * 4; 112 - break; 113 - case NOA1305_INTEGR_TIME_200MS: 114 - *val = 100; 115 - *val2 = 77 * 2; 116 - break; 117 - case NOA1305_INTEGR_TIME_100MS: 118 - *val = 100; 119 - *val2 = 77; 120 - break; 121 - case NOA1305_INTEGR_TIME_50MS: 122 - *val = 1000; 123 - *val2 = 77 * 5; 124 - break; 125 - case NOA1305_INTEGR_TIME_25MS: 126 - *val = 10000; 127 - *val2 = 77 * 25; 128 - break; 129 - case NOA1305_INTEGR_TIME_12_5MS: 130 - *val = 100000; 131 - *val2 = 77 * 125; 132 - break; 133 - case NOA1305_INTEGR_TIME_6_25MS: 134 - *val = 1000000; 135 - *val2 = 77 * 625; 136 - break; 137 - default: 138 - return -EINVAL; 139 - } 79 + data &= NOA1305_INTEGR_TIME_MASK; 80 + *val = noa1305_scale_available[2 * data + 0]; 81 + *val2 = noa1305_scale_available[2 * data + 1]; 140 82 141 83 return IIO_VAL_FRACTIONAL; 84 + } 85 + 86 + static int noa1305_int_time(struct noa1305_priv *priv, int *val, int *val2) 87 + { 88 + int data; 89 + int ret; 90 + 91 + ret = regmap_read(priv->regmap, NOA1305_REG_INTEGRATION_TIME, &data); 92 + if (ret < 0) 93 + return ret; 94 + 95 + data &= NOA1305_INTEGR_TIME_MASK; 96 + *val = noa1305_int_time_available[2 * data + 0]; 97 + *val2 = noa1305_int_time_available[2 * data + 1]; 98 + 99 + return IIO_VAL_INT_PLUS_MICRO; 142 100 } 143 101 144 102 static const struct iio_chan_spec noa1305_channels[] = { ··· 129 121 .type = IIO_LIGHT, 130 122 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 131 123 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 124 + .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), 125 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), 126 + .info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_INT_TIME), 132 127 } 133 128 }; 134 129 135 - static int noa1305_read_raw(struct iio_dev *indio_dev, 136 - struct iio_chan_spec const *chan, 137 - int *val, int *val2, long mask) 130 + static int noa1305_read_avail(struct iio_dev *indio_dev, 131 + struct iio_chan_spec const *chan, 132 + const int **vals, int *type, 133 + int *length, long mask) 138 134 { 139 - int ret = -EINVAL; 135 + if (chan->type != IIO_LIGHT) 136 + return -EINVAL; 137 + 138 + switch (mask) { 139 + case IIO_CHAN_INFO_SCALE: 140 + *vals = noa1305_scale_available; 141 + *length = ARRAY_SIZE(noa1305_scale_available); 142 + *type = IIO_VAL_FRACTIONAL; 143 + return IIO_AVAIL_LIST; 144 + case IIO_CHAN_INFO_INT_TIME: 145 + *vals = noa1305_int_time_available; 146 + *length = ARRAY_SIZE(noa1305_int_time_available); 147 + *type = IIO_VAL_INT_PLUS_MICRO; 148 + return IIO_AVAIL_LIST; 149 + default: 150 + return -EINVAL; 151 + } 152 + } 153 + 154 + static int noa1305_read_raw(struct iio_dev *indio_dev, 155 + struct iio_chan_spec const *chan, 156 + int *val, int *val2, long mask) 157 + { 140 158 struct noa1305_priv *priv = iio_priv(indio_dev); 159 + 160 + if (chan->type != IIO_LIGHT) 161 + return -EINVAL; 141 162 142 163 switch (mask) { 143 164 case IIO_CHAN_INFO_RAW: 144 - switch (chan->type) { 145 - case IIO_LIGHT: 146 - ret = noa1305_measure(priv); 147 - if (ret < 0) 148 - return ret; 149 - *val = ret; 150 - return IIO_VAL_INT; 151 - default: 152 - break; 153 - } 154 - break; 165 + return noa1305_measure(priv, val); 155 166 case IIO_CHAN_INFO_SCALE: 156 - switch (chan->type) { 157 - case IIO_LIGHT: 158 - return noa1305_scale(priv, val, val2); 159 - default: 160 - break; 161 - } 162 - break; 167 + return noa1305_scale(priv, val, val2); 168 + case IIO_CHAN_INFO_INT_TIME: 169 + return noa1305_int_time(priv, val, val2); 163 170 default: 164 - break; 171 + return -EINVAL; 165 172 } 173 + } 166 174 167 - return ret; 175 + static int noa1305_write_raw(struct iio_dev *indio_dev, 176 + struct iio_chan_spec const *chan, 177 + int val, int val2, long mask) 178 + { 179 + struct noa1305_priv *priv = iio_priv(indio_dev); 180 + int i; 181 + 182 + if (chan->type != IIO_LIGHT) 183 + return -EINVAL; 184 + 185 + if (mask != IIO_CHAN_INFO_INT_TIME) 186 + return -EINVAL; 187 + 188 + if (val) /* >= 1s integration time not supported */ 189 + return -EINVAL; 190 + 191 + /* Look up integration time register settings and write it if found. */ 192 + for (i = 0; i < ARRAY_SIZE(noa1305_int_time_available) / 2; i++) 193 + if (noa1305_int_time_available[2 * i + 1] == val2) 194 + return regmap_write(priv->regmap, NOA1305_REG_INTEGRATION_TIME, i); 195 + 196 + return -EINVAL; 168 197 } 169 198 170 199 static const struct iio_info noa1305_info = { 200 + .read_avail = noa1305_read_avail, 171 201 .read_raw = noa1305_read_raw, 202 + .write_raw = noa1305_write_raw, 172 203 }; 173 204 174 205 static bool noa1305_writable_reg(struct device *dev, unsigned int reg)
+84 -253
drivers/iio/light/rohm-bu27034.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 /* 3 - * BU27034 ROHM Ambient Light Sensor 3 + * BU27034ANUC ROHM Ambient Light Sensor 4 4 * 5 5 * Copyright (c) 2023, ROHM Semiconductor. 6 - * https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/light/bu27034nuc-e.pdf 7 6 */ 8 7 9 8 #include <linux/bitfield.h> ··· 29 30 30 31 #define BU27034_REG_MODE_CONTROL2 0x42 31 32 #define BU27034_MASK_D01_GAIN GENMASK(7, 3) 32 - #define BU27034_MASK_D2_GAIN_HI GENMASK(7, 6) 33 - #define BU27034_MASK_D2_GAIN_LO GENMASK(2, 0) 34 33 35 34 #define BU27034_REG_MODE_CONTROL3 0x43 36 35 #define BU27034_REG_MODE_CONTROL4 0x44 37 36 #define BU27034_MASK_MEAS_EN BIT(0) 38 37 #define BU27034_MASK_VALID BIT(7) 38 + #define BU27034_NUM_HW_DATA_CHANS 2 39 39 #define BU27034_REG_DATA0_LO 0x50 40 40 #define BU27034_REG_DATA1_LO 0x52 41 - #define BU27034_REG_DATA2_LO 0x54 42 - #define BU27034_REG_DATA2_HI 0x55 41 + #define BU27034_REG_DATA1_HI 0x53 43 42 #define BU27034_REG_MANUFACTURER_ID 0x92 44 43 #define BU27034_REG_MAX BU27034_REG_MANUFACTURER_ID 45 44 ··· 85 88 BU27034_CHAN_ALS, 86 89 BU27034_CHAN_DATA0, 87 90 BU27034_CHAN_DATA1, 88 - BU27034_CHAN_DATA2, 89 91 BU27034_NUM_CHANS 90 92 }; 91 93 92 94 static const unsigned long bu27034_scan_masks[] = { 93 - GENMASK(BU27034_CHAN_DATA2, BU27034_CHAN_ALS), 0 95 + GENMASK(BU27034_CHAN_DATA1, BU27034_CHAN_DATA0), 96 + GENMASK(BU27034_CHAN_DATA1, BU27034_CHAN_ALS), 0 94 97 }; 95 98 96 99 /* 97 - * Available scales with gain 1x - 4096x, timings 55, 100, 200, 400 mS 100 + * Available scales with gain 1x - 1024x, timings 55, 100, 200, 400 mS 98 101 * Time impacts to gain: 1x, 2x, 4x, 8x. 99 102 * 100 - * => Max total gain is HWGAIN * gain by integration time (8 * 4096) = 32768 103 + * => Max total gain is HWGAIN * gain by integration time (8 * 1024) = 8192 104 + * if 1x gain is scale 1, scale for 2x gain is 0.5, 4x => 0.25, 105 + * ... 8192x => 0.0001220703125 => 122070.3125 nanos 101 106 * 102 - * Using NANO precision for scale we must use scale 64x corresponding gain 1x 103 - * to avoid precision loss. (32x would result scale 976 562.5(nanos). 107 + * Using NANO precision for scale, we must use scale 16x corresponding gain 1x 108 + * to avoid precision loss. (8x would result scale 976 562.5(nanos). 104 109 */ 105 - #define BU27034_SCALE_1X 64 110 + #define BU27034_SCALE_1X 16 106 111 107 112 /* See the data sheet for the "Gain Setting" table */ 108 113 #define BU27034_GSEL_1X 0x00 /* 00000 */ 109 114 #define BU27034_GSEL_4X 0x08 /* 01000 */ 110 - #define BU27034_GSEL_16X 0x0a /* 01010 */ 111 115 #define BU27034_GSEL_32X 0x0b /* 01011 */ 112 - #define BU27034_GSEL_64X 0x0c /* 01100 */ 113 116 #define BU27034_GSEL_256X 0x18 /* 11000 */ 114 117 #define BU27034_GSEL_512X 0x19 /* 11001 */ 115 118 #define BU27034_GSEL_1024X 0x1a /* 11010 */ 116 - #define BU27034_GSEL_2048X 0x1b /* 11011 */ 117 - #define BU27034_GSEL_4096X 0x1c /* 11100 */ 118 119 119 120 /* Available gain settings */ 120 121 static const struct iio_gain_sel_pair bu27034_gains[] = { 121 122 GAIN_SCALE_GAIN(1, BU27034_GSEL_1X), 122 123 GAIN_SCALE_GAIN(4, BU27034_GSEL_4X), 123 - GAIN_SCALE_GAIN(16, BU27034_GSEL_16X), 124 124 GAIN_SCALE_GAIN(32, BU27034_GSEL_32X), 125 - GAIN_SCALE_GAIN(64, BU27034_GSEL_64X), 126 125 GAIN_SCALE_GAIN(256, BU27034_GSEL_256X), 127 126 GAIN_SCALE_GAIN(512, BU27034_GSEL_512X), 128 127 GAIN_SCALE_GAIN(1024, BU27034_GSEL_1024X), 129 - GAIN_SCALE_GAIN(2048, BU27034_GSEL_2048X), 130 - GAIN_SCALE_GAIN(4096, BU27034_GSEL_4096X), 131 128 }; 132 129 133 130 /* 134 - * The IC has 5 modes for sampling time. 5 mS mode is exceptional as it limits 135 - * the data collection to data0-channel only and cuts the supported range to 136 - * 10 bit. It is not supported by the driver. 137 - * 138 - * "normal" modes are 55, 100, 200 and 400 mS modes - which do have direct 139 - * multiplying impact to the register values (similar to gain). 131 + * Measurement modes are 55, 100, 200 and 400 mS modes - which do have direct 132 + * multiplying impact to the data register values (similar to gain). 140 133 * 141 134 * This means that if meas-mode is changed for example from 400 => 200, 142 135 * the scale is doubled. Eg, time impact to total gain is x1, x2, x4, x8. ··· 143 156 GAIN_SCALE_ITIME_US(55000, BU27034_MEAS_MODE_55MS, 1), 144 157 }; 145 158 146 - #define BU27034_CHAN_DATA(_name, _ch2) \ 159 + #define BU27034_CHAN_DATA(_name) \ 147 160 { \ 148 161 .type = IIO_INTENSITY, \ 149 162 .channel = BU27034_CHAN_##_name, \ 150 - .channel2 = (_ch2), \ 151 163 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 152 - BIT(IIO_CHAN_INFO_SCALE), \ 164 + BIT(IIO_CHAN_INFO_SCALE) | \ 165 + BIT(IIO_CHAN_INFO_HARDWAREGAIN), \ 153 166 .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE), \ 154 167 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \ 155 168 .info_mask_shared_by_all_available = \ ··· 182 195 /* 183 196 * The BU27034 DATA0 and DATA1 channels are both on the visible light 184 197 * area (mostly). The data0 sensitivity peaks at 500nm, DATA1 at 600nm. 185 - * These wave lengths are pretty much on the border of colours making 186 - * these a poor candidates for R/G/B standardization. Hence they're both 187 - * marked as clear channels 198 + * These wave lengths are cyan(ish) and orange(ish), making these 199 + * sub-optiomal candidates for R/G/B standardization. Hence the 200 + * colour modifier is omitted. 188 201 */ 189 - BU27034_CHAN_DATA(DATA0, IIO_MOD_LIGHT_CLEAR), 190 - BU27034_CHAN_DATA(DATA1, IIO_MOD_LIGHT_CLEAR), 191 - BU27034_CHAN_DATA(DATA2, IIO_MOD_LIGHT_IR), 202 + BU27034_CHAN_DATA(DATA0), 203 + BU27034_CHAN_DATA(DATA1), 192 204 IIO_CHAN_SOFT_TIMESTAMP(4), 193 205 }; 194 206 ··· 201 215 struct mutex mutex; 202 216 struct iio_gts gts; 203 217 struct task_struct *task; 204 - __le16 raw[3]; 218 + __le16 raw[BU27034_NUM_HW_DATA_CHANS]; 205 219 struct { 206 220 u32 mlux; 207 - __le16 channels[3]; 221 + __le16 channels[BU27034_NUM_HW_DATA_CHANS]; 208 222 s64 ts __aligned(8); 209 223 } scan; 210 224 }; ··· 218 232 .range_max = BU27034_REG_MODE_CONTROL4, 219 233 }, { 220 234 .range_min = BU27034_REG_DATA0_LO, 221 - .range_max = BU27034_REG_DATA2_HI, 235 + .range_max = BU27034_REG_DATA1_HI, 222 236 }, 223 237 }; 224 238 ··· 230 244 static const struct regmap_range bu27034_read_only_ranges[] = { 231 245 { 232 246 .range_min = BU27034_REG_DATA0_LO, 233 - .range_max = BU27034_REG_DATA2_HI, 247 + .range_max = BU27034_REG_DATA1_HI, 234 248 }, { 235 249 .range_min = BU27034_REG_MANUFACTURER_ID, 236 250 .range_max = BU27034_REG_MANUFACTURER_ID, ··· 259 273 260 274 static int bu27034_get_gain_sel(struct bu27034_data *data, int chan) 261 275 { 276 + int reg[] = { 277 + [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2, 278 + [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3, 279 + }; 262 280 int ret, val; 263 281 264 - switch (chan) { 265 - case BU27034_CHAN_DATA0: 266 - case BU27034_CHAN_DATA1: 267 - { 268 - int reg[] = { 269 - [BU27034_CHAN_DATA0] = BU27034_REG_MODE_CONTROL2, 270 - [BU27034_CHAN_DATA1] = BU27034_REG_MODE_CONTROL3, 271 - }; 272 - ret = regmap_read(data->regmap, reg[chan], &val); 273 - if (ret) 274 - return ret; 282 + ret = regmap_read(data->regmap, reg[chan], &val); 283 + if (ret) 284 + return ret; 275 285 276 - return FIELD_GET(BU27034_MASK_D01_GAIN, val); 277 - } 278 - case BU27034_CHAN_DATA2: 279 - { 280 - int d2_lo_bits = fls(BU27034_MASK_D2_GAIN_LO); 281 - 282 - ret = regmap_read(data->regmap, BU27034_REG_MODE_CONTROL2, &val); 283 - if (ret) 284 - return ret; 285 - 286 - /* 287 - * The data2 channel gain is composed by 5 non continuous bits 288 - * [7:6], [2:0]. Thus when we combine the 5-bit 'selector' 289 - * from register value we must right shift the high bits by 3. 290 - */ 291 - return FIELD_GET(BU27034_MASK_D2_GAIN_HI, val) << d2_lo_bits | 292 - FIELD_GET(BU27034_MASK_D2_GAIN_LO, val); 293 - } 294 - default: 295 - return -EINVAL; 296 - } 286 + return FIELD_GET(BU27034_MASK_D01_GAIN, val); 297 287 } 298 288 299 289 static int bu27034_get_gain(struct bu27034_data *data, int chan, int *gain) ··· 352 390 }; 353 391 int mask, val; 354 392 355 - if (chan != BU27034_CHAN_DATA0 && chan != BU27034_CHAN_DATA1) 356 - return -EINVAL; 357 - 358 393 val = FIELD_PREP(BU27034_MASK_D01_GAIN, sel); 359 - 360 394 mask = BU27034_MASK_D01_GAIN; 361 - 362 - if (chan == BU27034_CHAN_DATA0) { 363 - /* 364 - * We keep the same gain for channel 2 as we set for channel 0 365 - * We can't allow them to be individually controlled because 366 - * setting one will impact also the other. Also, if we don't 367 - * always update both gains we may result unsupported bit 368 - * combinations. 369 - * 370 - * This is not nice but this is yet another place where the 371 - * user space must be prepared to surprizes. Namely, see chan 2 372 - * gain changed when chan 0 gain is changed. 373 - * 374 - * This is not fatal for most users though. I don't expect the 375 - * channel 2 to be used in any generic cases - the intensity 376 - * values provided by the sensor for IR area are not openly 377 - * documented. Also, channel 2 is not used for visible light. 378 - * 379 - * So, if there is application which is written to utilize the 380 - * channel 2 - then it is probably specifically targeted to this 381 - * sensor and knows how to utilize those values. It is safe to 382 - * hope such user can also cope with the gain changes. 383 - */ 384 - mask |= BU27034_MASK_D2_GAIN_LO; 385 - 386 - /* 387 - * The D2 gain bits are directly the lowest bits of selector. 388 - * Just do add those bits to the value 389 - */ 390 - val |= sel & BU27034_MASK_D2_GAIN_LO; 391 - } 392 395 393 396 return regmap_update_bits(data->regmap, reg[chan], mask, val); 394 397 } ··· 361 434 static int bu27034_set_gain(struct bu27034_data *data, int chan, int gain) 362 435 { 363 436 int ret; 364 - 365 - /* 366 - * We don't allow setting channel 2 gain as it messes up the 367 - * gain for channel 0 - which shares the high bits 368 - */ 369 - if (chan != BU27034_CHAN_DATA0 && chan != BU27034_CHAN_DATA1) 370 - return -EINVAL; 371 437 372 438 ret = iio_gts_find_sel_by_gain(&data->gts, gain); 373 439 if (ret < 0) ··· 485 565 int ret, time_sel, gain_sel, i; 486 566 bool found = false; 487 567 488 - if (chan == BU27034_CHAN_DATA2) 489 - return -EINVAL; 490 - 491 568 if (chan == BU27034_CHAN_ALS) { 492 569 if (val == 0 && val2 == 1000000) 493 570 return 0; ··· 509 592 510 593 /* 511 594 * Populate information for the other channel which should also 512 - * maintain the scale. (Due to the HW limitations the chan2 513 - * gets the same gain as chan0, so we only need to explicitly 514 - * set the chan 0 and 1). 595 + * maintain the scale. 515 596 */ 516 597 if (chan == BU27034_CHAN_DATA0) 517 598 gain.chan = BU27034_CHAN_DATA1; ··· 523 608 /* 524 609 * Iterate through all the times to see if we find one which 525 610 * can support requested scale for requested channel, while 526 - * maintaining the scale for other channels 611 + * maintaining the scale for the other channel 527 612 */ 528 613 for (i = 0; i < data->gts.num_itime; i++) { 529 614 new_time_sel = data->gts.itime_table[i].sel; ··· 538 623 if (ret) 539 624 continue; 540 625 541 - /* Can the other channel(s) maintain scale? */ 626 + /* Can the other channel maintain scale? */ 542 627 ret = iio_gts_find_new_gain_sel_by_old_gain_time( 543 628 &data->gts, gain.old_gain, time_sel, 544 629 new_time_sel, &gain.new_gain); ··· 550 635 } 551 636 if (!found) { 552 637 dev_dbg(data->dev, 553 - "Can't set scale maintaining other channels\n"); 638 + "Can't set scale maintaining other channel\n"); 554 639 ret = -EINVAL; 555 640 556 641 goto unlock_out; ··· 574 659 } 575 660 576 661 /* 577 - * for (D1/D0 < 0.87): 578 - * lx = 0.004521097 * D1 - 0.002663996 * D0 + 579 - * 0.00012213 * D1 * D1 / D0 662 + * for (D1/D0 < 1.5): 663 + * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1/D0 – 1.5) * (0.25) + 1) 580 664 * 581 - * => 115.7400832 * ch1 / gain1 / mt - 582 - * 68.1982976 * ch0 / gain0 / mt + 583 - * 0.00012213 * 25600 * (ch1 / gain1 / mt) * 25600 * 584 - * (ch1 /gain1 / mt) / (25600 * ch0 / gain0 / mt) 665 + * => -0.000745625 * D0 + 0.0002515625 * D1 + -0.000018675 * D1 * D1 / D0 585 666 * 586 - * A = 0.00012213 * 25600 * (ch1 /gain1 / mt) * 25600 * 587 - * (ch1 /gain1 / mt) / (25600 * ch0 / gain0 / mt) 588 - * => 0.00012213 * 25600 * (ch1 /gain1 / mt) * 589 - * (ch1 /gain1 / mt) / (ch0 / gain0 / mt) 590 - * => 0.00012213 * 25600 * (ch1 / gain1) * (ch1 /gain1 / mt) / 591 - * (ch0 / gain0) 592 - * => 0.00012213 * 25600 * (ch1 / gain1) * (ch1 /gain1 / mt) * 593 - * gain0 / ch0 594 - * => 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / mt /ch0 667 + * => (6.44 * ch1 / gain1 + 19.088 * ch0 / gain0 - 668 + * 0.47808 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0) / 669 + * mt 595 670 * 596 - * lx = (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) / 597 - * mt + A 598 - * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0) / 599 - * mt + 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / mt / 600 - * ch0 671 + * Else 672 + * lx = 0.001193 * D0 - 0.0000747 * D1 601 673 * 602 - * => (115.7400832 * ch1 / gain1 - 68.1982976 * ch0 / gain0 + 603 - * 3.126528 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0) / 604 - * mt 605 - * 606 - * For (0.87 <= D1/D0 < 1.00) 607 - * lx = (0.001331* D0 + 0.0000354 * D1) * ((D1/D0 – 0.87) * (0.385) + 1) 608 - * => (0.001331 * 256 * 100 * ch0 / gain0 / mt + 0.0000354 * 256 * 609 - * 100 * ch1 / gain1 / mt) * ((D1/D0 - 0.87) * (0.385) + 1) 610 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 611 - * ((D1/D0 - 0.87) * (0.385) + 1) 612 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 613 - * (0.385 * D1/D0 - 0.66505) 614 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 615 - * (0.385 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) - 0.66505) 616 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 617 - * (9856 * ch1 / gain1 / mt / (25600 * ch0 / gain0 / mt) + 0.66505) 618 - * => 13.118336 * ch1 / (gain1 * mt) 619 - * + 22.66064768 * ch0 / (gain0 * mt) 620 - * + 8931.90144 * ch1 * ch1 * gain0 / 621 - * (25600 * ch0 * gain1 * gain1 * mt) 622 - * + 0.602694912 * ch1 / (gain1 * mt) 623 - * 624 - * => [0.3489024 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) 625 - * + 22.66064768 * ch0 / gain0 626 - * + 13.721030912 * ch1 / gain1 627 - * ] / mt 628 - * 629 - * For (D1/D0 >= 1.00) 630 - * 631 - * lx = (0.001331* D0 + 0.0000354 * D1) * ((D1/D0 – 2.0) * (-0.05) + 1) 632 - * => (0.001331* D0 + 0.0000354 * D1) * (-0.05D1/D0 + 1.1) 633 - * => (0.001331 * 256 * 100 * ch0 / gain0 / mt + 0.0000354 * 256 * 634 - * 100 * ch1 / gain1 / mt) * (-0.05D1/D0 + 1.1) 635 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 636 - * (-0.05 * 256 * 100 * ch1 / gain1 / mt / (256 * 100 * ch0 / gain0 / mt) + 1.1) 637 - * => (34.0736 * ch0 / gain0 / mt + 0.90624 * ch1 / gain1 / mt) * 638 - * (-1280 * ch1 / (gain1 * mt * 25600 * ch0 / gain0 / mt) + 1.1) 639 - * => (34.0736 * ch0 * -1280 * ch1 * gain0 * mt /( gain0 * mt * gain1 * mt * 25600 * ch0) 640 - * + 34.0736 * 1.1 * ch0 / (gain0 * mt) 641 - * + 0.90624 * ch1 * -1280 * ch1 *gain0 * mt / (gain1 * mt *gain1 * mt * 25600 * ch0) 642 - * + 1.1 * 0.90624 * ch1 / (gain1 * mt) 643 - * => -43614.208 * ch1 / (gain1 * mt * 25600) 644 - * + 37.48096 ch0 / (gain0 * mt) 645 - * - 1159.9872 * ch1 * ch1 * gain0 / (gain1 * gain1 * mt * 25600 * ch0) 646 - * + 0.996864 ch1 / (gain1 * mt) 647 - * => [ 648 - * - 0.045312 * ch1 * ch1 * gain0 / (gain1 * gain1 * ch0) 649 - * - 0.706816 * ch1 / gain1 650 - * + 37.48096 ch0 /gain0 651 - * ] * mt 652 - * 653 - * 654 - * So, the first case (D1/D0 < 0.87) can be computed to a form: 655 - * 656 - * lx = (3.126528 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) + 657 - * 115.7400832 * ch1 / gain1 + 658 - * -68.1982976 * ch0 / gain0 659 - * / mt 660 - * 661 - * Second case (0.87 <= D1/D0 < 1.00) goes to form: 662 - * 663 - * => [0.3489024 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) + 664 - * 13.721030912 * ch1 / gain1 + 665 - * 22.66064768 * ch0 / gain0 666 - * ] / mt 667 - * 668 - * Third case (D1/D0 >= 1.00) goes to form: 669 - * => [-0.045312 * ch1 * ch1 * gain0 / (ch0 * gain1 * gain1) + 670 - * -0.706816 * ch1 / gain1 + 671 - * 37.48096 ch0 /(gain0 672 - * ] / mt 674 + * => (1.91232 * ch1 / gain1 + 30.5408 * ch0 / gain0 + 675 + * [0 * ch1 * ch1 * gain0 / gain1 / gain1 / ch0] ) / 676 + * mt 673 677 * 674 678 * This can be unified to format: 675 679 * lx = [ ··· 598 764 * ] / mt 599 765 * 600 766 * For case 1: 601 - * A = 3.126528, 602 - * B = 115.7400832 603 - * C = -68.1982976 767 + * A = -0.47808, 768 + * B = 6.44, 769 + * C = 19.088 604 770 * 605 771 * For case 2: 606 - * A = 0.3489024 607 - * B = 13.721030912 608 - * C = 22.66064768 609 - * 610 - * For case 3: 611 - * A = -0.045312 612 - * B = -0.706816 613 - * C = 37.48096 772 + * A = 0 773 + * B = 1.91232 774 + * C = 30.5408 614 775 */ 615 776 616 777 struct bu27034_lx_coeff { ··· 710 881 { 711 882 static const struct bu27034_lx_coeff coeff[] = { 712 883 { 713 - .A = 31265280, /* 3.126528 */ 714 - .B = 1157400832, /*115.7400832 */ 715 - .C = 681982976, /* -68.1982976 */ 716 - .is_neg = {false, false, true}, 884 + .A = 4780800, /* -0.47808 */ 885 + .B = 64400000, /* 6.44 */ 886 + .C = 190880000, /* 19.088 */ 887 + .is_neg = { true, false, false }, 717 888 }, { 718 - .A = 3489024, /* 0.3489024 */ 719 - .B = 137210309, /* 13.721030912 */ 720 - .C = 226606476, /* 22.66064768 */ 889 + .A = 0, /* 0 */ 890 + .B = 19123200, /* 1.91232 */ 891 + .C = 305408000, /* 30.5408 */ 721 892 /* All terms positive */ 722 - }, { 723 - .A = 453120, /* -0.045312 */ 724 - .B = 7068160, /* -0.706816 */ 725 - .C = 374809600, /* 37.48096 */ 726 - .is_neg = {true, true, false}, 727 - } 893 + }, 728 894 }; 729 895 const struct bu27034_lx_coeff *c = &coeff[coeff_idx]; 730 896 u64 res = 0, terms[3]; ··· 791 967 int reg[] = { 792 968 [BU27034_CHAN_DATA0] = BU27034_REG_DATA0_LO, 793 969 [BU27034_CHAN_DATA1] = BU27034_REG_DATA1_LO, 794 - [BU27034_CHAN_DATA2] = BU27034_REG_DATA2_LO, 795 970 }; 796 971 int valid, ret; 797 972 __le16 val; ··· 857 1034 { 858 1035 int ret; 859 1036 860 - if (chan < BU27034_CHAN_DATA0 || chan > BU27034_CHAN_DATA2) 1037 + if (chan < BU27034_CHAN_DATA0 || chan > BU27034_CHAN_DATA1) 861 1038 return -EINVAL; 862 1039 863 1040 ret = bu27034_meas_set(data, true); ··· 882 1059 * D1 = data1/ch1_gain/meas_time_ms * 25600 883 1060 * 884 1061 * Then: 885 - * if (D1/D0 < 0.87) 886 - * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 0.87) * 3.45 + 1) 887 - * else if (D1/D0 < 1) 888 - * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 0.87) * 0.385 + 1) 889 - * else 890 - * lx = (0.001331 * D0 + 0.0000354 * D1) * ((D1 / D0 - 2) * -0.05 + 1) 1062 + * If (D1/D0 < 1.5) 1063 + * lx = (0.001193 * D0 + (-0.0000747) * D1) * ((D1 / D0 – 1.5) * 0.25 + 1) 1064 + * Else 1065 + * lx = (0.001193 * D0 + (-0.0000747) * D1) 891 1066 * 892 1067 * We use it here. Users who have for example some colored lens 893 1068 * need to modify the calculation but I hope this gives a starting point for ··· 936 1115 d1_d0_ratio_scaled /= ch0 * gain1; 937 1116 } 938 1117 939 - if (d1_d0_ratio_scaled < 87) 1118 + if (d1_d0_ratio_scaled < 150) 940 1119 ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 0); 941 - else if (d1_d0_ratio_scaled < 100) 942 - ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 1); 943 1120 else 944 - ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 2); 1121 + ret = bu27034_fixp_calc_lx(ch0, ch1, gain0, gain1, meastime, 1); 945 1122 946 1123 if (ret < 0) 947 1124 return ret; ··· 952 1133 953 1134 static int bu27034_get_mlux(struct bu27034_data *data, int chan, int *val) 954 1135 { 955 - __le16 res[3]; 1136 + __le16 res[BU27034_NUM_HW_DATA_CHANS]; 956 1137 int ret; 957 1138 958 1139 ret = bu27034_meas_set(data, true); ··· 989 1170 return *val2; 990 1171 991 1172 return IIO_VAL_INT_PLUS_MICRO; 1173 + 1174 + case IIO_CHAN_INFO_HARDWAREGAIN: 1175 + ret = bu27034_get_gain(data, chan->channel, val); 1176 + if (ret) 1177 + return ret; 1178 + 1179 + return IIO_VAL_INT; 992 1180 993 1181 case IIO_CHAN_INFO_SCALE: 994 1182 return bu27034_get_scale(data, chan->channel, val, val2); ··· 1041 1215 struct iio_chan_spec const *chan, 1042 1216 long mask) 1043 1217 { 1218 + struct bu27034_data *data = iio_priv(indio_dev); 1044 1219 1045 1220 switch (mask) { 1046 1221 case IIO_CHAN_INFO_SCALE: 1047 1222 return IIO_VAL_INT_PLUS_NANO; 1048 1223 case IIO_CHAN_INFO_INT_TIME: 1049 1224 return IIO_VAL_INT_PLUS_MICRO; 1225 + case IIO_CHAN_INFO_HARDWAREGAIN: 1226 + dev_dbg(data->dev, 1227 + "HARDWAREGAIN is read-only, use scale to set\n"); 1228 + return -EINVAL; 1050 1229 default: 1051 1230 return -EINVAL; 1052 1231 } ··· 1332 1501 } 1333 1502 1334 1503 static const struct of_device_id bu27034_of_match[] = { 1335 - { .compatible = "rohm,bu27034" }, 1504 + { .compatible = "rohm,bu27034anuc" }, 1336 1505 { } 1337 1506 }; 1338 1507 MODULE_DEVICE_TABLE(of, bu27034_of_match);
+3 -4
drivers/iio/light/si1145.c
··· 465 465 goto done; 466 466 } 467 467 468 - for_each_set_bit(i, indio_dev->active_scan_mask, 469 - indio_dev->masklength) { 468 + iio_for_each_active_channel(indio_dev, i) { 470 469 int run = 1; 471 470 472 - while (i + run < indio_dev->masklength) { 471 + while (i + run < iio_get_masklength(indio_dev)) { 473 472 if (!test_bit(i + run, indio_dev->active_scan_mask)) 474 473 break; 475 474 if (indio_dev->channels[i + run].address != ··· 513 514 if (data->scan_mask == scan_mask) 514 515 return 0; 515 516 516 - for_each_set_bit(i, &scan_mask, indio_dev->masklength) { 517 + for_each_set_bit(i, &scan_mask, iio_get_masklength(indio_dev)) { 517 518 switch (indio_dev->channels[i].address) { 518 519 case SI1145_REG_ALSVIS_DATA: 519 520 reg |= SI1145_CHLIST_EN_ALSVIS;
+6 -1
drivers/iio/light/stk3310.c
··· 35 35 #define STK3310_STATE_EN_ALS BIT(1) 36 36 #define STK3310_STATE_STANDBY 0x00 37 37 38 + #define STK3013_CHIP_ID_VAL 0x31 38 39 #define STK3310_CHIP_ID_VAL 0x13 39 40 #define STK3311_CHIP_ID_VAL 0x1D 40 41 #define STK3311A_CHIP_ID_VAL 0x15 ··· 85 84 REG_FIELD(STK3310_REG_FLAG, 0, 0); 86 85 87 86 static const u8 stk3310_chip_ids[] = { 87 + STK3013_CHIP_ID_VAL, 88 88 STK3310_CHIP_ID_VAL, 89 89 STK3311A_CHIP_ID_VAL, 90 90 STK3311S34_CHIP_ID_VAL, ··· 498 496 499 497 ret = stk3310_check_chip_id(chipid); 500 498 if (ret < 0) 501 - dev_warn(&client->dev, "unknown chip id: 0x%x\n", chipid); 499 + dev_info(&client->dev, "new unknown chip id: 0x%x\n", chipid); 502 500 503 501 state = STK3310_STATE_EN_ALS | STK3310_STATE_EN_PS; 504 502 ret = stk3310_set_state(data, state); ··· 702 700 stk3310_resume); 703 701 704 702 static const struct i2c_device_id stk3310_i2c_id[] = { 703 + { "STK3013" }, 705 704 { "STK3310" }, 706 705 { "STK3311" }, 707 706 { "STK3335" }, ··· 711 708 MODULE_DEVICE_TABLE(i2c, stk3310_i2c_id); 712 709 713 710 static const struct acpi_device_id stk3310_acpi_id[] = { 711 + {"STK3013", 0}, 714 712 {"STK3310", 0}, 715 713 {"STK3311", 0}, 716 714 {} ··· 720 716 MODULE_DEVICE_TABLE(acpi, stk3310_acpi_id); 721 717 722 718 static const struct of_device_id stk3310_of_match[] = { 719 + { .compatible = "sensortek,stk3013", }, 723 720 { .compatible = "sensortek,stk3310", }, 724 721 { .compatible = "sensortek,stk3311", }, 725 722 { .compatible = "sensortek,stk3335", },
+1 -2
drivers/iio/light/tcs3414.c
··· 206 206 struct tcs3414_data *data = iio_priv(indio_dev); 207 207 int i, j = 0; 208 208 209 - for_each_set_bit(i, indio_dev->active_scan_mask, 210 - indio_dev->masklength) { 209 + iio_for_each_active_channel(indio_dev, i) { 211 210 int ret = i2c_smbus_read_word_data(data->client, 212 211 TCS3414_DATA_GREEN + 2*i); 213 212 if (ret < 0)
+1 -2
drivers/iio/light/tcs3472.c
··· 383 383 if (ret < 0) 384 384 goto done; 385 385 386 - for_each_set_bit(i, indio_dev->active_scan_mask, 387 - indio_dev->masklength) { 386 + iio_for_each_active_channel(indio_dev, i) { 388 387 ret = i2c_smbus_read_word_data(data->client, 389 388 TCS3472_CDATA + 2*i); 390 389 if (ret < 0)
-1
drivers/iio/magnetometer/ak8975.c
··· 1081 1081 { .compatible = "asahi-kasei,ak09912", .data = &ak_def_array[AK09912] }, 1082 1082 { .compatible = "ak09912", .data = &ak_def_array[AK09912] }, 1083 1083 { .compatible = "asahi-kasei,ak09916", .data = &ak_def_array[AK09916] }, 1084 - { .compatible = "ak09916", .data = &ak_def_array[AK09916] }, 1085 1084 {} 1086 1085 }; 1087 1086 MODULE_DEVICE_TABLE(of, ak8975_of_match);
+1 -1
drivers/iio/magnetometer/rm3100-core.c
··· 464 464 struct iio_poll_func *pf = p; 465 465 struct iio_dev *indio_dev = pf->indio_dev; 466 466 unsigned long scan_mask = *indio_dev->active_scan_mask; 467 - unsigned int mask_len = indio_dev->masklength; 467 + unsigned int mask_len = iio_get_masklength(indio_dev); 468 468 struct rm3100_data *data = iio_priv(indio_dev); 469 469 struct regmap *regmap = data->regmap; 470 470 int ret, i, bit;
+11
drivers/iio/pressure/Kconfig
··· 31 31 select REGMAP 32 32 select BMP280_I2C if (I2C) 33 33 select BMP280_SPI if (SPI_MASTER) 34 + select IIO_BUFFER 35 + select IIO_TRIGGERED_BUFFER 34 36 help 35 37 Say yes here to build support for Bosch Sensortec BMP180, BMP280, BMP380 36 38 and BMP580 pressure and temperature sensors. Also supports the BME280 with ··· 249 247 250 248 This driver can also be built as a module. If so, the module will 251 249 be called ms5637. 250 + 251 + config SDP500 252 + tristate "Sensirion SDP500 differential pressure sensor I2C driver" 253 + depends on I2C 254 + help 255 + Say Y here to build support for Sensirion SDP500 differential pressure 256 + sensor I2C driver. 257 + To compile this driver as a module, choose M here: the core module 258 + will be called sdp500. 252 259 253 260 config IIO_ST_PRESS 254 261 tristate "STMicroelectronics pressure sensor Driver"
+1
drivers/iio/pressure/Makefile
··· 30 30 obj-$(CONFIG_MS5611_I2C) += ms5611_i2c.o 31 31 obj-$(CONFIG_MS5611_SPI) += ms5611_spi.o 32 32 obj-$(CONFIG_MS5637) += ms5637.o 33 + obj-$(CONFIG_SDP500) += sdp500.o 33 34 obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o 34 35 st_pressure-y := st_pressure_core.o 35 36 st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
+561 -93
drivers/iio/pressure/bmp280-core.c
··· 41 41 #include <linux/regmap.h> 42 42 #include <linux/regulator/consumer.h> 43 43 44 + #include <linux/iio/buffer.h> 44 45 #include <linux/iio/iio.h> 46 + #include <linux/iio/trigger_consumer.h> 47 + #include <linux/iio/triggered_buffer.h> 45 48 46 49 #include <asm/unaligned.h> 47 50 ··· 137 134 BMP380_P11 = 20, 138 135 }; 139 136 137 + enum bmp280_scan { 138 + BMP280_PRESS, 139 + BMP280_TEMP, 140 + BME280_HUMID, 141 + }; 142 + 140 143 static const struct iio_chan_spec bmp280_channels[] = { 141 144 { 142 145 .type = IIO_PRESSURE, 146 + /* PROCESSED maintained for ABI backwards compatibility */ 143 147 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 148 + BIT(IIO_CHAN_INFO_RAW) | 149 + BIT(IIO_CHAN_INFO_SCALE) | 144 150 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 151 + .scan_index = 0, 152 + .scan_type = { 153 + .sign = 'u', 154 + .realbits = 32, 155 + .storagebits = 32, 156 + .endianness = IIO_CPU, 157 + }, 145 158 }, 146 159 { 147 160 .type = IIO_TEMP, 161 + /* PROCESSED maintained for ABI backwards compatibility */ 148 162 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 163 + BIT(IIO_CHAN_INFO_RAW) | 164 + BIT(IIO_CHAN_INFO_SCALE) | 149 165 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 166 + .scan_index = 1, 167 + .scan_type = { 168 + .sign = 's', 169 + .realbits = 32, 170 + .storagebits = 32, 171 + .endianness = IIO_CPU, 172 + }, 173 + }, 174 + IIO_CHAN_SOFT_TIMESTAMP(2), 175 + }; 176 + 177 + static const struct iio_chan_spec bme280_channels[] = { 178 + { 179 + .type = IIO_PRESSURE, 180 + /* PROCESSED maintained for ABI backwards compatibility */ 181 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 182 + BIT(IIO_CHAN_INFO_RAW) | 183 + BIT(IIO_CHAN_INFO_SCALE) | 184 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 185 + .scan_index = 0, 186 + .scan_type = { 187 + .sign = 'u', 188 + .realbits = 32, 189 + .storagebits = 32, 190 + .endianness = IIO_CPU, 191 + }, 192 + }, 193 + { 194 + .type = IIO_TEMP, 195 + /* PROCESSED maintained for ABI backwards compatibility */ 196 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 197 + BIT(IIO_CHAN_INFO_RAW) | 198 + BIT(IIO_CHAN_INFO_SCALE) | 199 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 200 + .scan_index = 1, 201 + .scan_type = { 202 + .sign = 's', 203 + .realbits = 32, 204 + .storagebits = 32, 205 + .endianness = IIO_CPU, 206 + }, 150 207 }, 151 208 { 152 209 .type = IIO_HUMIDITYRELATIVE, 210 + /* PROCESSED maintained for ABI backwards compatibility */ 153 211 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 212 + BIT(IIO_CHAN_INFO_RAW) | 213 + BIT(IIO_CHAN_INFO_SCALE) | 154 214 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 215 + .scan_index = 2, 216 + .scan_type = { 217 + .sign = 'u', 218 + .realbits = 32, 219 + .storagebits = 32, 220 + .endianness = IIO_CPU, 221 + }, 155 222 }, 223 + IIO_CHAN_SOFT_TIMESTAMP(3), 156 224 }; 157 225 158 226 static const struct iio_chan_spec bmp380_channels[] = { 159 227 { 160 228 .type = IIO_PRESSURE, 229 + /* PROCESSED maintained for ABI backwards compatibility */ 161 230 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 231 + BIT(IIO_CHAN_INFO_RAW) | 232 + BIT(IIO_CHAN_INFO_SCALE) | 162 233 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 163 234 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 164 235 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 236 + .scan_index = 0, 237 + .scan_type = { 238 + .sign = 'u', 239 + .realbits = 32, 240 + .storagebits = 32, 241 + .endianness = IIO_CPU, 242 + }, 165 243 }, 166 244 { 167 245 .type = IIO_TEMP, 246 + /* PROCESSED maintained for ABI backwards compatibility */ 168 247 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 248 + BIT(IIO_CHAN_INFO_RAW) | 249 + BIT(IIO_CHAN_INFO_SCALE) | 169 250 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 170 251 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 171 252 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 253 + .scan_index = 1, 254 + .scan_type = { 255 + .sign = 's', 256 + .realbits = 32, 257 + .storagebits = 32, 258 + .endianness = IIO_CPU, 259 + }, 260 + }, 261 + IIO_CHAN_SOFT_TIMESTAMP(2), 262 + }; 263 + 264 + static const struct iio_chan_spec bmp580_channels[] = { 265 + { 266 + .type = IIO_PRESSURE, 267 + /* PROCESSED maintained for ABI backwards compatibility */ 268 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 269 + BIT(IIO_CHAN_INFO_RAW) | 270 + BIT(IIO_CHAN_INFO_SCALE) | 271 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 272 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 273 + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 274 + .scan_index = 0, 275 + .scan_type = { 276 + .sign = 'u', 277 + .realbits = 24, 278 + .storagebits = 32, 279 + .endianness = IIO_LE, 280 + }, 172 281 }, 173 282 { 174 - .type = IIO_HUMIDITYRELATIVE, 283 + .type = IIO_TEMP, 284 + /* PROCESSED maintained for ABI backwards compatibility */ 175 285 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | 286 + BIT(IIO_CHAN_INFO_RAW) | 287 + BIT(IIO_CHAN_INFO_SCALE) | 176 288 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), 177 289 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | 178 290 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), 291 + .scan_index = 1, 292 + .scan_type = { 293 + .sign = 's', 294 + .realbits = 24, 295 + .storagebits = 32, 296 + .endianness = IIO_LE, 297 + }, 179 298 }, 299 + IIO_CHAN_SOFT_TIMESTAMP(2), 180 300 }; 181 301 182 302 static int bmp280_read_calib(struct bmp280_data *data) ··· 415 289 int ret; 416 290 417 291 ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB, 418 - &data->be16, sizeof(data->be16)); 292 + &data->be16, BME280_NUM_HUMIDITY_BYTES); 419 293 if (ret) { 420 294 dev_err(data->dev, "failed to read humidity\n"); 421 295 return ret; ··· 461 335 int ret; 462 336 463 337 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, 464 - data->buf, sizeof(data->buf)); 338 + data->buf, BMP280_NUM_TEMP_BYTES); 465 339 if (ret) { 466 340 dev_err(data->dev, "failed to read temperature\n"); 467 341 return ret; ··· 522 396 int ret; 523 397 524 398 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 525 - data->buf, sizeof(data->buf)); 399 + data->buf, BMP280_NUM_PRESS_BYTES); 526 400 if (ret) { 527 401 dev_err(data->dev, "failed to read pressure\n"); 528 402 return ret; ··· 571 445 return (u32)p; 572 446 } 573 447 574 - static int bmp280_read_temp(struct bmp280_data *data, 575 - int *val, int *val2) 448 + static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp) 576 449 { 577 - s32 comp_temp; 578 450 u32 adc_temp; 579 451 int ret; 580 452 ··· 580 456 if (ret) 581 457 return ret; 582 458 583 - comp_temp = bmp280_compensate_temp(data, adc_temp); 459 + *comp_temp = bmp280_compensate_temp(data, adc_temp); 584 460 585 - *val = comp_temp * 10; 586 - return IIO_VAL_INT; 461 + return 0; 587 462 } 588 463 589 - static int bmp280_read_press(struct bmp280_data *data, 590 - int *val, int *val2) 464 + static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press) 591 465 { 592 - u32 comp_press, adc_press, t_fine; 466 + u32 adc_press; 467 + s32 t_fine; 593 468 int ret; 594 469 595 470 ret = bmp280_get_t_fine(data, &t_fine); ··· 599 476 if (ret) 600 477 return ret; 601 478 602 - comp_press = bmp280_compensate_press(data, adc_press, t_fine); 479 + *comp_press = bmp280_compensate_press(data, adc_press, t_fine); 603 480 604 - *val = comp_press; 605 - *val2 = 256000; 606 - 607 - return IIO_VAL_FRACTIONAL; 481 + return 0; 608 482 } 609 483 610 - static int bme280_read_humid(struct bmp280_data *data, int *val, int *val2) 484 + static int bme280_read_humid(struct bmp280_data *data, u32 *comp_humidity) 611 485 { 612 - u32 comp_humidity; 613 486 u16 adc_humidity; 614 487 s32 t_fine; 615 488 int ret; ··· 618 499 if (ret) 619 500 return ret; 620 501 621 - comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 502 + *comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine); 622 503 623 - *val = comp_humidity * 1000 / 1024; 624 - 625 - return IIO_VAL_INT; 504 + return 0; 626 505 } 627 506 628 507 static int bmp280_read_raw_impl(struct iio_dev *indio_dev, ··· 628 511 int *val, int *val2, long mask) 629 512 { 630 513 struct bmp280_data *data = iio_priv(indio_dev); 514 + int chan_value; 515 + int ret; 631 516 632 517 guard(mutex)(&data->lock); 633 518 ··· 637 518 case IIO_CHAN_INFO_PROCESSED: 638 519 switch (chan->type) { 639 520 case IIO_HUMIDITYRELATIVE: 640 - return data->chip_info->read_humid(data, val, val2); 521 + ret = data->chip_info->read_humid(data, &chan_value); 522 + if (ret) 523 + return ret; 524 + 525 + *val = data->chip_info->humid_coeffs[0] * chan_value; 526 + *val2 = data->chip_info->humid_coeffs[1]; 527 + return data->chip_info->humid_coeffs_type; 641 528 case IIO_PRESSURE: 642 - return data->chip_info->read_press(data, val, val2); 529 + ret = data->chip_info->read_press(data, &chan_value); 530 + if (ret) 531 + return ret; 532 + 533 + *val = data->chip_info->press_coeffs[0] * chan_value; 534 + *val2 = data->chip_info->press_coeffs[1]; 535 + return data->chip_info->press_coeffs_type; 643 536 case IIO_TEMP: 644 - return data->chip_info->read_temp(data, val, val2); 537 + ret = data->chip_info->read_temp(data, &chan_value); 538 + if (ret) 539 + return ret; 540 + 541 + *val = data->chip_info->temp_coeffs[0] * chan_value; 542 + *val2 = data->chip_info->temp_coeffs[1]; 543 + return data->chip_info->temp_coeffs_type; 544 + default: 545 + return -EINVAL; 546 + } 547 + case IIO_CHAN_INFO_RAW: 548 + switch (chan->type) { 549 + case IIO_HUMIDITYRELATIVE: 550 + ret = data->chip_info->read_humid(data, &chan_value); 551 + if (ret) 552 + return ret; 553 + 554 + *val = chan_value; 555 + return IIO_VAL_INT; 556 + case IIO_PRESSURE: 557 + ret = data->chip_info->read_press(data, &chan_value); 558 + if (ret) 559 + return ret; 560 + 561 + *val = chan_value; 562 + return IIO_VAL_INT; 563 + case IIO_TEMP: 564 + ret = data->chip_info->read_temp(data, &chan_value); 565 + if (ret) 566 + return ret; 567 + 568 + *val = chan_value; 569 + return IIO_VAL_INT; 570 + default: 571 + return -EINVAL; 572 + } 573 + case IIO_CHAN_INFO_SCALE: 574 + switch (chan->type) { 575 + case IIO_HUMIDITYRELATIVE: 576 + *val = data->chip_info->humid_coeffs[0]; 577 + *val2 = data->chip_info->humid_coeffs[1]; 578 + return data->chip_info->humid_coeffs_type; 579 + case IIO_PRESSURE: 580 + *val = data->chip_info->press_coeffs[0]; 581 + *val2 = data->chip_info->press_coeffs[1]; 582 + return data->chip_info->press_coeffs_type; 583 + case IIO_TEMP: 584 + *val = data->chip_info->temp_coeffs[0]; 585 + *val2 = data->chip_info->temp_coeffs[1]; 586 + return data->chip_info->temp_coeffs_type; 645 587 default: 646 588 return -EINVAL; 647 589 } ··· 973 793 .write_raw = &bmp280_write_raw, 974 794 }; 975 795 796 + static const unsigned long bmp280_avail_scan_masks[] = { 797 + BIT(BMP280_TEMP) | BIT(BMP280_PRESS), 798 + 0 799 + }; 800 + 801 + static const unsigned long bme280_avail_scan_masks[] = { 802 + BIT(BME280_HUMID) | BIT(BMP280_TEMP) | BIT(BMP280_PRESS), 803 + 0 804 + }; 805 + 976 806 static int bmp280_chip_config(struct bmp280_data *data) 977 807 { 978 808 u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) | ··· 1010 820 return ret; 1011 821 } 1012 822 823 + static irqreturn_t bmp280_trigger_handler(int irq, void *p) 824 + { 825 + struct iio_poll_func *pf = p; 826 + struct iio_dev *indio_dev = pf->indio_dev; 827 + struct bmp280_data *data = iio_priv(indio_dev); 828 + s32 adc_temp, adc_press, t_fine; 829 + int ret; 830 + 831 + guard(mutex)(&data->lock); 832 + 833 + /* Burst read data registers */ 834 + ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 835 + data->buf, BMP280_BURST_READ_BYTES); 836 + if (ret) { 837 + dev_err(data->dev, "failed to burst read sensor data\n"); 838 + goto out; 839 + } 840 + 841 + /* Temperature calculations */ 842 + adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3])); 843 + if (adc_temp == BMP280_TEMP_SKIPPED) { 844 + dev_err(data->dev, "reading temperature skipped\n"); 845 + goto out; 846 + } 847 + 848 + data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp); 849 + 850 + /* Pressure calculations */ 851 + adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); 852 + if (adc_press == BMP280_PRESS_SKIPPED) { 853 + dev_err(data->dev, "reading pressure skipped\n"); 854 + goto out; 855 + } 856 + 857 + t_fine = bmp280_calc_t_fine(data, adc_temp); 858 + 859 + data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine); 860 + 861 + iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, 862 + iio_get_time_ns(indio_dev)); 863 + 864 + out: 865 + iio_trigger_notify_done(indio_dev->trig); 866 + 867 + return IRQ_HANDLED; 868 + } 869 + 1013 870 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; 1014 871 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID }; 872 + static const int bmp280_temp_coeffs[] = { 10, 1 }; 873 + static const int bmp280_press_coeffs[] = { 1, 256000 }; 1015 874 1016 875 const struct bmp280_chip_info bmp280_chip_info = { 1017 876 .id_reg = BMP280_REG_ID, ··· 1069 830 .regmap_config = &bmp280_regmap_config, 1070 831 .start_up_time = 2000, 1071 832 .channels = bmp280_channels, 1072 - .num_channels = 2, 833 + .num_channels = ARRAY_SIZE(bmp280_channels), 834 + .avail_scan_masks = bmp280_avail_scan_masks, 1073 835 1074 836 .oversampling_temp_avail = bmp280_oversampling_avail, 1075 837 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), ··· 1090 850 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1091 851 .oversampling_press_default = BMP280_OSRS_PRESS_16X - 1, 1092 852 853 + .temp_coeffs = bmp280_temp_coeffs, 854 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 855 + .press_coeffs = bmp280_press_coeffs, 856 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 857 + 1093 858 .chip_config = bmp280_chip_config, 1094 859 .read_temp = bmp280_read_temp, 1095 860 .read_press = bmp280_read_press, 1096 861 .read_calib = bmp280_read_calib, 862 + 863 + .trigger_handler = bmp280_trigger_handler, 1097 864 }; 1098 865 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280); 1099 866 ··· 1123 876 return bmp280_chip_config(data); 1124 877 } 1125 878 879 + static irqreturn_t bme280_trigger_handler(int irq, void *p) 880 + { 881 + struct iio_poll_func *pf = p; 882 + struct iio_dev *indio_dev = pf->indio_dev; 883 + struct bmp280_data *data = iio_priv(indio_dev); 884 + s32 adc_temp, adc_press, adc_humidity, t_fine; 885 + int ret; 886 + 887 + guard(mutex)(&data->lock); 888 + 889 + /* Burst read data registers */ 890 + ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, 891 + data->buf, BME280_BURST_READ_BYTES); 892 + if (ret) { 893 + dev_err(data->dev, "failed to burst read sensor data\n"); 894 + goto out; 895 + } 896 + 897 + /* Temperature calculations */ 898 + adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3])); 899 + if (adc_temp == BMP280_TEMP_SKIPPED) { 900 + dev_err(data->dev, "reading temperature skipped\n"); 901 + goto out; 902 + } 903 + 904 + data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp); 905 + 906 + /* Pressure calculations */ 907 + adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0])); 908 + if (adc_press == BMP280_PRESS_SKIPPED) { 909 + dev_err(data->dev, "reading pressure skipped\n"); 910 + goto out; 911 + } 912 + 913 + t_fine = bmp280_calc_t_fine(data, adc_temp); 914 + 915 + data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine); 916 + 917 + /* Humidity calculations */ 918 + adc_humidity = get_unaligned_be16(&data->buf[6]); 919 + 920 + if (adc_humidity == BMP280_HUMIDITY_SKIPPED) { 921 + dev_err(data->dev, "reading humidity skipped\n"); 922 + goto out; 923 + } 924 + data->sensor_data[2] = bme280_compensate_humidity(data, adc_humidity, t_fine); 925 + 926 + iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, 927 + iio_get_time_ns(indio_dev)); 928 + 929 + out: 930 + iio_trigger_notify_done(indio_dev->trig); 931 + 932 + return IRQ_HANDLED; 933 + } 934 + 1126 935 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID }; 936 + static const int bme280_humid_coeffs[] = { 1000, 1024 }; 1127 937 1128 938 const struct bmp280_chip_info bme280_chip_info = { 1129 939 .id_reg = BMP280_REG_ID, 1130 940 .chip_id = bme280_chip_ids, 1131 941 .num_chip_id = ARRAY_SIZE(bme280_chip_ids), 1132 - .regmap_config = &bmp280_regmap_config, 942 + .regmap_config = &bme280_regmap_config, 1133 943 .start_up_time = 2000, 1134 - .channels = bmp280_channels, 1135 - .num_channels = 3, 944 + .channels = bme280_channels, 945 + .num_channels = ARRAY_SIZE(bme280_channels), 946 + .avail_scan_masks = bme280_avail_scan_masks, 1136 947 1137 948 .oversampling_temp_avail = bmp280_oversampling_avail, 1138 949 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), ··· 1204 899 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail), 1205 900 .oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1, 1206 901 902 + .temp_coeffs = bmp280_temp_coeffs, 903 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 904 + .press_coeffs = bmp280_press_coeffs, 905 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 906 + .humid_coeffs = bme280_humid_coeffs, 907 + .humid_coeffs_type = IIO_VAL_FRACTIONAL, 908 + 1207 909 .chip_config = bme280_chip_config, 1208 910 .read_temp = bmp280_read_temp, 1209 911 .read_press = bmp280_read_press, 1210 912 .read_humid = bme280_read_humid, 1211 913 .read_calib = bme280_read_calib, 914 + 915 + .trigger_handler = bme280_trigger_handler, 1212 916 }; 1213 917 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280); 1214 918 ··· 1272 958 int ret; 1273 959 1274 960 ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB, 1275 - data->buf, sizeof(data->buf)); 961 + data->buf, BMP280_NUM_TEMP_BYTES); 1276 962 if (ret) { 1277 963 dev_err(data->dev, "failed to read temperature\n"); 1278 964 return ret; ··· 1341 1027 int ret; 1342 1028 1343 1029 ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, 1344 - data->buf, sizeof(data->buf)); 1030 + data->buf, BMP280_NUM_PRESS_BYTES); 1345 1031 if (ret) { 1346 1032 dev_err(data->dev, "failed to read pressure\n"); 1347 1033 return ret; ··· 1405 1091 return comp_press; 1406 1092 } 1407 1093 1408 - static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2) 1094 + static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp) 1409 1095 { 1410 - s32 comp_temp; 1411 1096 u32 adc_temp; 1412 1097 int ret; 1413 1098 ··· 1414 1101 if (ret) 1415 1102 return ret; 1416 1103 1417 - comp_temp = bmp380_compensate_temp(data, adc_temp); 1104 + *comp_temp = bmp380_compensate_temp(data, adc_temp); 1418 1105 1419 - *val = comp_temp * 10; 1420 - return IIO_VAL_INT; 1106 + return 0; 1421 1107 } 1422 1108 1423 - static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2) 1109 + static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press) 1424 1110 { 1425 - u32 adc_press, comp_press, t_fine; 1111 + u32 adc_press, t_fine; 1426 1112 int ret; 1427 1113 1428 1114 ret = bmp380_get_t_fine(data, &t_fine); ··· 1432 1120 if (ret) 1433 1121 return ret; 1434 1122 1435 - comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1123 + *comp_press = bmp380_compensate_press(data, adc_press, t_fine); 1436 1124 1437 - *val = comp_press; 1438 - *val2 = 100000; 1439 - 1440 - return IIO_VAL_FRACTIONAL; 1125 + return 0; 1441 1126 } 1442 1127 1443 1128 static int bmp380_read_calib(struct bmp280_data *data) ··· 1581 1272 } 1582 1273 /* 1583 1274 * Waits for measurement before checking configuration error 1584 - * flag. Selected longest measure time indicated in 1585 - * section 3.9.1 in the datasheet. 1275 + * flag. Selected longest measurement time, calculated from 1276 + * formula in datasheet section 3.9.2 with an offset of ~+15% 1277 + * as it seen as well in table 3.9.1. 1586 1278 */ 1587 - msleep(80); 1279 + msleep(150); 1588 1280 1589 1281 /* Check config error flag */ 1590 1282 ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp); ··· 1603 1293 return 0; 1604 1294 } 1605 1295 1296 + static irqreturn_t bmp380_trigger_handler(int irq, void *p) 1297 + { 1298 + struct iio_poll_func *pf = p; 1299 + struct iio_dev *indio_dev = pf->indio_dev; 1300 + struct bmp280_data *data = iio_priv(indio_dev); 1301 + s32 adc_temp, adc_press, t_fine; 1302 + int ret; 1303 + 1304 + guard(mutex)(&data->lock); 1305 + 1306 + /* Burst read data registers */ 1307 + ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB, 1308 + data->buf, BMP280_BURST_READ_BYTES); 1309 + if (ret) { 1310 + dev_err(data->dev, "failed to burst read sensor data\n"); 1311 + goto out; 1312 + } 1313 + 1314 + /* Temperature calculations */ 1315 + adc_temp = get_unaligned_le24(&data->buf[3]); 1316 + if (adc_temp == BMP380_TEMP_SKIPPED) { 1317 + dev_err(data->dev, "reading temperature skipped\n"); 1318 + goto out; 1319 + } 1320 + 1321 + data->sensor_data[1] = bmp380_compensate_temp(data, adc_temp); 1322 + 1323 + /* Pressure calculations */ 1324 + adc_press = get_unaligned_le24(&data->buf[0]); 1325 + if (adc_press == BMP380_PRESS_SKIPPED) { 1326 + dev_err(data->dev, "reading pressure skipped\n"); 1327 + goto out; 1328 + } 1329 + 1330 + t_fine = bmp380_calc_t_fine(data, adc_temp); 1331 + 1332 + data->sensor_data[0] = bmp380_compensate_press(data, adc_press, t_fine); 1333 + 1334 + iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, 1335 + iio_get_time_ns(indio_dev)); 1336 + 1337 + out: 1338 + iio_trigger_notify_done(indio_dev->trig); 1339 + 1340 + return IRQ_HANDLED; 1341 + } 1342 + 1606 1343 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 }; 1607 1344 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128}; 1608 1345 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID }; 1346 + static const int bmp380_temp_coeffs[] = { 10, 1 }; 1347 + static const int bmp380_press_coeffs[] = { 1, 100000 }; 1609 1348 1610 1349 const struct bmp280_chip_info bmp380_chip_info = { 1611 1350 .id_reg = BMP380_REG_ID, ··· 1664 1305 .spi_read_extra_byte = true, 1665 1306 .start_up_time = 2000, 1666 1307 .channels = bmp380_channels, 1667 - .num_channels = 2, 1308 + .num_channels = ARRAY_SIZE(bmp380_channels), 1309 + .avail_scan_masks = bmp280_avail_scan_masks, 1668 1310 1669 1311 .oversampling_temp_avail = bmp380_oversampling_avail, 1670 1312 .num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail), ··· 1683 1323 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 1684 1324 .iir_filter_coeff_default = 2, 1685 1325 1326 + .temp_coeffs = bmp380_temp_coeffs, 1327 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 1328 + .press_coeffs = bmp380_press_coeffs, 1329 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 1330 + 1686 1331 .chip_config = bmp380_chip_config, 1687 1332 .read_temp = bmp380_read_temp, 1688 1333 .read_press = bmp380_read_press, 1689 1334 .read_calib = bmp380_read_calib, 1690 1335 .preinit = bmp380_preinit, 1336 + 1337 + .trigger_handler = bmp380_trigger_handler, 1691 1338 }; 1692 1339 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280); 1693 1340 ··· 1810 1443 * for what is expected on IIO ABI. 1811 1444 */ 1812 1445 1813 - static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2) 1446 + static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp) 1814 1447 { 1815 - s32 raw_temp; 1448 + s32 value_temp; 1816 1449 int ret; 1817 1450 1818 - ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf, 1819 - sizeof(data->buf)); 1451 + ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, 1452 + data->buf, BMP280_NUM_TEMP_BYTES); 1820 1453 if (ret) { 1821 1454 dev_err(data->dev, "failed to read temperature\n"); 1822 1455 return ret; 1823 1456 } 1824 1457 1825 - raw_temp = get_unaligned_le24(data->buf); 1826 - if (raw_temp == BMP580_TEMP_SKIPPED) { 1458 + value_temp = get_unaligned_le24(data->buf); 1459 + if (value_temp == BMP580_TEMP_SKIPPED) { 1827 1460 dev_err(data->dev, "reading temperature skipped\n"); 1828 1461 return -EIO; 1829 1462 } 1463 + *raw_temp = sign_extend32(value_temp, 23); 1830 1464 1831 - /* 1832 - * Temperature is returned in Celsius degrees in fractional 1833 - * form down 2^16. We rescale by x1000 to return millidegrees 1834 - * Celsius to respect IIO ABI. 1835 - */ 1836 - raw_temp = sign_extend32(raw_temp, 23); 1837 - *val = ((s64)raw_temp * 1000) / (1 << 16); 1838 - return IIO_VAL_INT; 1465 + return 0; 1839 1466 } 1840 1467 1841 - static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2) 1468 + static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press) 1842 1469 { 1843 - u32 raw_press; 1470 + u32 value_press; 1844 1471 int ret; 1845 1472 1846 - ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf, 1847 - sizeof(data->buf)); 1473 + ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, 1474 + data->buf, BMP280_NUM_PRESS_BYTES); 1848 1475 if (ret) { 1849 1476 dev_err(data->dev, "failed to read pressure\n"); 1850 1477 return ret; 1851 1478 } 1852 1479 1853 - raw_press = get_unaligned_le24(data->buf); 1854 - if (raw_press == BMP580_PRESS_SKIPPED) { 1480 + value_press = get_unaligned_le24(data->buf); 1481 + if (value_press == BMP580_PRESS_SKIPPED) { 1855 1482 dev_err(data->dev, "reading pressure skipped\n"); 1856 1483 return -EIO; 1857 1484 } 1858 - /* 1859 - * Pressure is returned in Pascals in fractional form down 2^16. 1860 - * We rescale /1000 to convert to kilopascal to respect IIO ABI. 1861 - */ 1862 - *val = raw_press; 1863 - *val2 = 64000; /* 2^6 * 1000 */ 1864 - return IIO_VAL_FRACTIONAL; 1485 + *raw_press = value_press; 1486 + 1487 + return 0; 1865 1488 } 1866 1489 1867 1490 static const int bmp580_odr_table[][2] = { ··· 2185 1828 return 0; 2186 1829 } 2187 1830 1831 + static irqreturn_t bmp580_trigger_handler(int irq, void *p) 1832 + { 1833 + struct iio_poll_func *pf = p; 1834 + struct iio_dev *indio_dev = pf->indio_dev; 1835 + struct bmp280_data *data = iio_priv(indio_dev); 1836 + int ret; 1837 + 1838 + guard(mutex)(&data->lock); 1839 + 1840 + /* Burst read data registers */ 1841 + ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, 1842 + data->buf, BMP280_BURST_READ_BYTES); 1843 + if (ret) { 1844 + dev_err(data->dev, "failed to burst read sensor data\n"); 1845 + goto out; 1846 + } 1847 + 1848 + /* Temperature calculations */ 1849 + memcpy(&data->sensor_data[1], &data->buf[0], 3); 1850 + 1851 + /* Pressure calculations */ 1852 + memcpy(&data->sensor_data[0], &data->buf[3], 3); 1853 + 1854 + iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, 1855 + iio_get_time_ns(indio_dev)); 1856 + 1857 + out: 1858 + iio_trigger_notify_done(indio_dev->trig); 1859 + 1860 + return IRQ_HANDLED; 1861 + } 1862 + 2188 1863 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 2189 1864 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT }; 1865 + /* Instead of { 1000, 16 } we do this, to avoid overflow issues */ 1866 + static const int bmp580_temp_coeffs[] = { 125, 13 }; 1867 + static const int bmp580_press_coeffs[] = { 1, 64000}; 2190 1868 2191 1869 const struct bmp280_chip_info bmp580_chip_info = { 2192 1870 .id_reg = BMP580_REG_CHIP_ID, ··· 2229 1837 .num_chip_id = ARRAY_SIZE(bmp580_chip_ids), 2230 1838 .regmap_config = &bmp580_regmap_config, 2231 1839 .start_up_time = 2000, 2232 - .channels = bmp380_channels, 2233 - .num_channels = 2, 1840 + .channels = bmp580_channels, 1841 + .num_channels = ARRAY_SIZE(bmp580_channels), 1842 + .avail_scan_masks = bmp280_avail_scan_masks, 2234 1843 2235 1844 .oversampling_temp_avail = bmp580_oversampling_avail, 2236 1845 .num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail), ··· 2249 1856 .num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail), 2250 1857 .iir_filter_coeff_default = 2, 2251 1858 1859 + .temp_coeffs = bmp580_temp_coeffs, 1860 + .temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2, 1861 + .press_coeffs = bmp580_press_coeffs, 1862 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 1863 + 2252 1864 .chip_config = bmp580_chip_config, 2253 1865 .read_temp = bmp580_read_temp, 2254 1866 .read_press = bmp580_read_press, 2255 1867 .preinit = bmp580_preinit, 1868 + 1869 + .trigger_handler = bmp580_trigger_handler, 2256 1870 }; 2257 1871 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280); 2258 1872 2259 1873 static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas) 2260 1874 { 2261 - const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; 1875 + static const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; 2262 1876 unsigned int delay_us; 2263 1877 unsigned int ctrl; 2264 1878 int ret; ··· 2411 2011 return (bmp180_calc_t_fine(data, adc_temp) + 8) / 16; 2412 2012 } 2413 2013 2414 - static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2) 2014 + static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp) 2415 2015 { 2416 - s32 comp_temp; 2417 2016 u32 adc_temp; 2418 2017 int ret; 2419 2018 ··· 2420 2021 if (ret) 2421 2022 return ret; 2422 2023 2423 - comp_temp = bmp180_compensate_temp(data, adc_temp); 2024 + *comp_temp = bmp180_compensate_temp(data, adc_temp); 2424 2025 2425 - *val = comp_temp * 100; 2426 - return IIO_VAL_INT; 2026 + return 0; 2427 2027 } 2428 2028 2429 2029 static int bmp180_read_press_adc(struct bmp280_data *data, u32 *adc_press) ··· 2438 2040 return ret; 2439 2041 2440 2042 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, 2441 - data->buf, sizeof(data->buf)); 2043 + data->buf, BMP280_NUM_PRESS_BYTES); 2442 2044 if (ret) { 2443 2045 dev_err(data->dev, "failed to read pressure\n"); 2444 2046 return ret; ··· 2485 2087 return p + ((x1 + x2 + 3791) >> 4); 2486 2088 } 2487 2089 2488 - static int bmp180_read_press(struct bmp280_data *data, int *val, int *val2) 2090 + static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press) 2489 2091 { 2490 - u32 comp_press, adc_press; 2092 + u32 adc_press; 2491 2093 s32 t_fine; 2492 2094 int ret; 2493 2095 ··· 2499 2101 if (ret) 2500 2102 return ret; 2501 2103 2502 - comp_press = bmp180_compensate_press(data, adc_press, t_fine); 2104 + *comp_press = bmp180_compensate_press(data, adc_press, t_fine); 2503 2105 2504 - *val = comp_press; 2505 - *val2 = 1000; 2506 - 2507 - return IIO_VAL_FRACTIONAL; 2106 + return 0; 2508 2107 } 2509 2108 2510 2109 static int bmp180_chip_config(struct bmp280_data *data) ··· 2509 2114 return 0; 2510 2115 } 2511 2116 2117 + static irqreturn_t bmp180_trigger_handler(int irq, void *p) 2118 + { 2119 + struct iio_poll_func *pf = p; 2120 + struct iio_dev *indio_dev = pf->indio_dev; 2121 + struct bmp280_data *data = iio_priv(indio_dev); 2122 + int ret, chan_value; 2123 + 2124 + guard(mutex)(&data->lock); 2125 + 2126 + ret = bmp180_read_temp(data, &chan_value); 2127 + if (ret) 2128 + goto out; 2129 + 2130 + data->sensor_data[1] = chan_value; 2131 + 2132 + ret = bmp180_read_press(data, &chan_value); 2133 + if (ret) 2134 + goto out; 2135 + 2136 + data->sensor_data[0] = chan_value; 2137 + 2138 + iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, 2139 + iio_get_time_ns(indio_dev)); 2140 + 2141 + out: 2142 + iio_trigger_notify_done(indio_dev->trig); 2143 + 2144 + return IRQ_HANDLED; 2145 + } 2146 + 2512 2147 static const int bmp180_oversampling_temp_avail[] = { 1 }; 2513 2148 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; 2514 2149 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID }; 2150 + static const int bmp180_temp_coeffs[] = { 100, 1 }; 2151 + static const int bmp180_press_coeffs[] = { 1, 1000 }; 2515 2152 2516 2153 const struct bmp280_chip_info bmp180_chip_info = { 2517 2154 .id_reg = BMP280_REG_ID, ··· 2552 2125 .regmap_config = &bmp180_regmap_config, 2553 2126 .start_up_time = 2000, 2554 2127 .channels = bmp280_channels, 2555 - .num_channels = 2, 2128 + .num_channels = ARRAY_SIZE(bmp280_channels), 2129 + .avail_scan_masks = bmp280_avail_scan_masks, 2556 2130 2557 2131 .oversampling_temp_avail = bmp180_oversampling_temp_avail, 2558 2132 .num_oversampling_temp_avail = ··· 2565 2137 ARRAY_SIZE(bmp180_oversampling_press_avail), 2566 2138 .oversampling_press_default = BMP180_MEAS_PRESS_8X, 2567 2139 2140 + .temp_coeffs = bmp180_temp_coeffs, 2141 + .temp_coeffs_type = IIO_VAL_FRACTIONAL, 2142 + .press_coeffs = bmp180_press_coeffs, 2143 + .press_coeffs_type = IIO_VAL_FRACTIONAL, 2144 + 2568 2145 .chip_config = bmp180_chip_config, 2569 2146 .read_temp = bmp180_read_temp, 2570 2147 .read_press = bmp180_read_press, 2571 2148 .read_calib = bmp180_read_calib, 2149 + 2150 + .trigger_handler = bmp180_trigger_handler, 2572 2151 }; 2573 2152 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280); 2574 2153 ··· 2620 2185 data->use_eoc = true; 2621 2186 return 0; 2622 2187 } 2188 + 2189 + static int bmp280_buffer_preenable(struct iio_dev *indio_dev) 2190 + { 2191 + struct bmp280_data *data = iio_priv(indio_dev); 2192 + 2193 + pm_runtime_get_sync(data->dev); 2194 + 2195 + return 0; 2196 + } 2197 + 2198 + static int bmp280_buffer_postdisable(struct iio_dev *indio_dev) 2199 + { 2200 + struct bmp280_data *data = iio_priv(indio_dev); 2201 + 2202 + pm_runtime_mark_last_busy(data->dev); 2203 + pm_runtime_put_autosuspend(data->dev); 2204 + 2205 + return 0; 2206 + } 2207 + 2208 + static const struct iio_buffer_setup_ops bmp280_buffer_setup_ops = { 2209 + .preenable = bmp280_buffer_preenable, 2210 + .postdisable = bmp280_buffer_postdisable, 2211 + }; 2623 2212 2624 2213 static void bmp280_pm_disable(void *data) 2625 2214 { ··· 2691 2232 /* Apply initial values from chip info structure */ 2692 2233 indio_dev->channels = chip_info->channels; 2693 2234 indio_dev->num_channels = chip_info->num_channels; 2235 + indio_dev->available_scan_masks = chip_info->avail_scan_masks; 2694 2236 data->oversampling_press = chip_info->oversampling_press_default; 2695 2237 data->oversampling_humid = chip_info->oversampling_humid_default; 2696 2238 data->oversampling_temp = chip_info->oversampling_temp_default; ··· 2776 2316 return dev_err_probe(data->dev, ret, 2777 2317 "failed to read calibration coefficients\n"); 2778 2318 } 2319 + 2320 + ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev, 2321 + iio_pollfunc_store_time, 2322 + data->chip_info->trigger_handler, 2323 + &bmp280_buffer_setup_ops); 2324 + if (ret) 2325 + return dev_err_probe(data->dev, ret, 2326 + "iio triggered buffer setup failed\n"); 2779 2327 2780 2328 /* 2781 2329 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
+1 -1
drivers/iio/pressure/bmp280-i2c.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 - #include <linux/module.h> 3 2 #include <linux/i2c.h> 3 + #include <linux/module.h> 4 4 #include <linux/regmap.h> 5 5 6 6 #include "bmp280.h"
+42 -3
drivers/iio/pressure/bmp280-regmap.c
··· 41 41 }; 42 42 EXPORT_SYMBOL_NS(bmp180_regmap_config, IIO_BMP280); 43 43 44 - static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg) 44 + static bool bme280_is_writeable_reg(struct device *dev, unsigned int reg) 45 45 { 46 46 switch (reg) { 47 47 case BMP280_REG_CONFIG: ··· 54 54 } 55 55 } 56 56 57 + static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg) 58 + { 59 + switch (reg) { 60 + case BMP280_REG_CONFIG: 61 + case BMP280_REG_CTRL_MEAS: 62 + case BMP280_REG_RESET: 63 + return true; 64 + default: 65 + return false; 66 + } 67 + } 68 + 57 69 static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg) 70 + { 71 + switch (reg) { 72 + case BMP280_REG_TEMP_XLSB: 73 + case BMP280_REG_TEMP_LSB: 74 + case BMP280_REG_TEMP_MSB: 75 + case BMP280_REG_PRESS_XLSB: 76 + case BMP280_REG_PRESS_LSB: 77 + case BMP280_REG_PRESS_MSB: 78 + case BMP280_REG_STATUS: 79 + return true; 80 + default: 81 + return false; 82 + } 83 + } 84 + 85 + static bool bme280_is_volatile_reg(struct device *dev, unsigned int reg) 58 86 { 59 87 switch (reg) { 60 88 case BME280_REG_HUMIDITY_LSB: ··· 99 71 return false; 100 72 } 101 73 } 102 - 103 74 static bool bmp380_is_writeable_reg(struct device *dev, unsigned int reg) 104 75 { 105 76 switch (reg) { ··· 194 167 .reg_bits = 8, 195 168 .val_bits = 8, 196 169 197 - .max_register = BME280_REG_HUMIDITY_LSB, 170 + .max_register = BMP280_REG_TEMP_XLSB, 198 171 .cache_type = REGCACHE_RBTREE, 199 172 200 173 .writeable_reg = bmp280_is_writeable_reg, 201 174 .volatile_reg = bmp280_is_volatile_reg, 202 175 }; 203 176 EXPORT_SYMBOL_NS(bmp280_regmap_config, IIO_BMP280); 177 + 178 + const struct regmap_config bme280_regmap_config = { 179 + .reg_bits = 8, 180 + .val_bits = 8, 181 + 182 + .max_register = BME280_REG_HUMIDITY_LSB, 183 + .cache_type = REGCACHE_RBTREE, 184 + 185 + .writeable_reg = bme280_is_writeable_reg, 186 + .volatile_reg = bme280_is_volatile_reg, 187 + }; 188 + EXPORT_SYMBOL_NS(bme280_regmap_config, IIO_BMP280); 204 189 205 190 const struct regmap_config bmp380_regmap_config = { 206 191 .reg_bits = 8,
+7 -11
drivers/iio/pressure/bmp280-spi.c
··· 5 5 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c 6 6 */ 7 7 #include <linux/bits.h> 8 - #include <linux/module.h> 9 - #include <linux/spi/spi.h> 10 8 #include <linux/err.h> 9 + #include <linux/module.h> 11 10 #include <linux/regmap.h> 11 + #include <linux/spi/spi.h> 12 12 13 13 #include "bmp280.h" 14 14 ··· 40 40 size_t reg_size, void *val, size_t val_size) 41 41 { 42 42 struct spi_device *spi = to_spi_device(context); 43 - u8 rx_buf[4]; 43 + u8 rx_buf[BME280_BURST_READ_BYTES + 1]; 44 44 ssize_t status; 45 45 46 - /* 47 - * Maximum number of consecutive bytes read for a temperature or 48 - * pressure measurement is 3. 49 - */ 50 - if (val_size > 3) 46 + if (val_size > BME280_BURST_READ_BYTES) 51 47 return -EINVAL; 52 48 53 49 /* ··· 60 64 return 0; 61 65 } 62 66 63 - static struct regmap_bus bmp280_regmap_bus = { 67 + static const struct regmap_bus bmp280_regmap_bus = { 64 68 .write = bmp280_regmap_spi_write, 65 69 .read = bmp280_regmap_spi_read, 66 70 .reg_format_endian_default = REGMAP_ENDIAN_BIG, 67 71 .val_format_endian_default = REGMAP_ENDIAN_BIG, 68 72 }; 69 73 70 - static struct regmap_bus bmp380_regmap_bus = { 74 + static const struct regmap_bus bmp380_regmap_bus = { 71 75 .write = bmp280_regmap_spi_write, 72 76 .read = bmp380_regmap_spi_read, 73 77 .read_flag_mask = BIT(7), ··· 79 83 { 80 84 const struct spi_device_id *id = spi_get_device_id(spi); 81 85 const struct bmp280_chip_info *chip_info; 82 - struct regmap_bus *bmp_regmap_bus; 86 + struct regmap_bus const *bmp_regmap_bus; 83 87 struct regmap *regmap; 84 88 int ret; 85 89
+32 -5
drivers/iio/pressure/bmp280.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 #include <linux/bitops.h> 3 3 #include <linux/device.h> 4 - #include <linux/iio/iio.h> 5 4 #include <linux/regmap.h> 6 5 #include <linux/regulator/consumer.h> 7 6 7 + #include <linux/iio/iio.h> 8 8 9 9 /* BMP580 specific registers */ 10 10 #define BMP580_REG_CMD 0x7E ··· 304 304 #define BMP280_PRESS_SKIPPED 0x80000 305 305 #define BMP280_HUMIDITY_SKIPPED 0x8000 306 306 307 + /* Number of bytes for each value */ 308 + #define BMP280_NUM_PRESS_BYTES 3 309 + #define BMP280_NUM_TEMP_BYTES 3 310 + #define BME280_NUM_HUMIDITY_BYTES 2 311 + #define BMP280_BURST_READ_BYTES (BMP280_NUM_PRESS_BYTES + \ 312 + BMP280_NUM_TEMP_BYTES) 313 + #define BME280_BURST_READ_BYTES (BMP280_NUM_PRESS_BYTES + \ 314 + BMP280_NUM_TEMP_BYTES + \ 315 + BME280_NUM_HUMIDITY_BYTES) 316 + 307 317 /* Core exported structs */ 308 318 309 319 static const char *const bmp280_supply_names[] = { ··· 408 398 int sampling_freq; 409 399 410 400 /* 401 + * Data to push to userspace triggered buffer. Up to 3 channels and 402 + * s64 timestamp, aligned. 403 + */ 404 + s32 sensor_data[6] __aligned(8); 405 + 406 + /* 411 407 * DMA (thus cache coherency maintenance) may require the 412 408 * transfer buffers to live in their own cache lines. 413 409 */ 414 410 union { 415 411 /* Sensor data buffer */ 416 - u8 buf[3]; 412 + u8 buf[BME280_BURST_READ_BYTES]; 417 413 /* Calibration data buffers */ 418 414 __le16 bmp280_cal_buf[BMP280_CONTIGUOUS_CALIB_REGS / 2]; 419 415 __be16 bmp180_cal_buf[BMP180_REG_CALIB_COUNT / 2]; ··· 441 425 const struct iio_chan_spec *channels; 442 426 int num_channels; 443 427 unsigned int start_up_time; 428 + const unsigned long *avail_scan_masks; 444 429 445 430 const int *oversampling_temp_avail; 446 431 int num_oversampling_temp_avail; ··· 463 446 int num_sampling_freq_avail; 464 447 int sampling_freq_default; 465 448 449 + const int *temp_coeffs; 450 + const int temp_coeffs_type; 451 + const int *press_coeffs; 452 + const int press_coeffs_type; 453 + const int *humid_coeffs; 454 + const int humid_coeffs_type; 455 + 466 456 int (*chip_config)(struct bmp280_data *data); 467 - int (*read_temp)(struct bmp280_data *data, int *val, int *val2); 468 - int (*read_press)(struct bmp280_data *data, int *val, int *val2); 469 - int (*read_humid)(struct bmp280_data *data, int *val, int *val2); 457 + int (*read_temp)(struct bmp280_data *data, s32 *adc_temp); 458 + int (*read_press)(struct bmp280_data *data, u32 *adc_press); 459 + int (*read_humid)(struct bmp280_data *data, u32 *adc_humidity); 470 460 int (*read_calib)(struct bmp280_data *data); 471 461 int (*preinit)(struct bmp280_data *data); 462 + 463 + irqreturn_t (*trigger_handler)(int irq, void *p); 472 464 }; 473 465 474 466 /* Chip infos for each variant */ ··· 490 464 /* Regmap configurations */ 491 465 extern const struct regmap_config bmp180_regmap_config; 492 466 extern const struct regmap_config bmp280_regmap_config; 467 + extern const struct regmap_config bme280_regmap_config; 493 468 extern const struct regmap_config bmp380_regmap_config; 494 469 extern const struct regmap_config bmp580_regmap_config; 495 470
+1 -2
drivers/iio/pressure/dlhl60d.c
··· 256 256 if (ret) 257 257 goto out; 258 258 259 - for_each_set_bit(chn, indio_dev->active_scan_mask, 260 - indio_dev->masklength) { 259 + iio_for_each_active_channel(indio_dev, chn) { 261 260 memcpy(&tmp_buf[i++], 262 261 &st->rx_buf[1] + chn * DLH_NUM_DATA_BYTES, 263 262 DLH_NUM_DATA_BYTES);
+156
drivers/iio/pressure/sdp500.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Driver for Sensirion sdp500 and sdp510 pressure sensors 4 + * 5 + * Datasheet: https://sensirion.com/resource/datasheet/sdp600 6 + */ 7 + 8 + #include <linux/i2c.h> 9 + #include <linux/crc8.h> 10 + #include <linux/iio/iio.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/regulator/consumer.h> 13 + #include <asm/unaligned.h> 14 + 15 + #define SDP500_CRC8_POLYNOMIAL 0x31 /* x8+x5+x4+1 (normalized to 0x31) */ 16 + #define SDP500_READ_SIZE 3 17 + 18 + #define SDP500_I2C_START_MEAS 0xF1 19 + 20 + struct sdp500_data { 21 + struct device *dev; 22 + }; 23 + 24 + DECLARE_CRC8_TABLE(sdp500_crc8_table); 25 + 26 + static int sdp500_start_measurement(struct sdp500_data *data) 27 + { 28 + struct i2c_client *client = to_i2c_client(data->dev); 29 + 30 + return i2c_smbus_write_byte(client, SDP500_I2C_START_MEAS); 31 + } 32 + 33 + static const struct iio_chan_spec sdp500_channels[] = { 34 + { 35 + .type = IIO_PRESSURE, 36 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 37 + BIT(IIO_CHAN_INFO_SCALE), 38 + }, 39 + }; 40 + 41 + static int sdp500_read_raw(struct iio_dev *indio_dev, 42 + struct iio_chan_spec const *chan, 43 + int *val, int *val2, long mask) 44 + { 45 + int ret; 46 + u8 rxbuf[SDP500_READ_SIZE]; 47 + u8 received_crc, calculated_crc; 48 + struct sdp500_data *data = iio_priv(indio_dev); 49 + struct i2c_client *client = to_i2c_client(data->dev); 50 + 51 + switch (mask) { 52 + case IIO_CHAN_INFO_RAW: 53 + ret = i2c_master_recv(client, rxbuf, SDP500_READ_SIZE); 54 + if (ret < 0) { 55 + dev_err(data->dev, "Failed to receive data"); 56 + return ret; 57 + } 58 + if (ret != SDP500_READ_SIZE) { 59 + dev_err(data->dev, "Data is received wrongly"); 60 + return -EIO; 61 + } 62 + 63 + received_crc = rxbuf[2]; 64 + calculated_crc = crc8(sdp500_crc8_table, rxbuf, 65 + sizeof(rxbuf) - 1, 0x00); 66 + if (received_crc != calculated_crc) { 67 + dev_err(data->dev, 68 + "calculated crc = 0x%.2X, received 0x%.2X", 69 + calculated_crc, received_crc); 70 + return -EIO; 71 + } 72 + 73 + *val = get_unaligned_be16(rxbuf); 74 + return IIO_VAL_INT; 75 + case IIO_CHAN_INFO_SCALE: 76 + *val = 1; 77 + *val2 = 60; 78 + 79 + return IIO_VAL_FRACTIONAL; 80 + default: 81 + return -EINVAL; 82 + } 83 + } 84 + 85 + static const struct iio_info sdp500_info = { 86 + .read_raw = &sdp500_read_raw, 87 + }; 88 + 89 + static int sdp500_probe(struct i2c_client *client) 90 + { 91 + struct iio_dev *indio_dev; 92 + struct sdp500_data *data; 93 + struct device *dev = &client->dev; 94 + int ret; 95 + u8 rxbuf[SDP500_READ_SIZE]; 96 + 97 + ret = devm_regulator_get_enable(dev, "vdd"); 98 + if (ret) 99 + return dev_err_probe(dev, ret, 100 + "Failed to get and enable regulator\n"); 101 + 102 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 103 + if (!indio_dev) 104 + return -ENOMEM; 105 + 106 + /* has to be done before the first i2c communication */ 107 + crc8_populate_msb(sdp500_crc8_table, SDP500_CRC8_POLYNOMIAL); 108 + 109 + data = iio_priv(indio_dev); 110 + data->dev = dev; 111 + 112 + indio_dev->name = "sdp500"; 113 + indio_dev->channels = sdp500_channels; 114 + indio_dev->info = &sdp500_info; 115 + indio_dev->modes = INDIO_DIRECT_MODE; 116 + indio_dev->num_channels = ARRAY_SIZE(sdp500_channels); 117 + 118 + ret = sdp500_start_measurement(data); 119 + if (ret) 120 + return dev_err_probe(dev, ret, "Failed to start measurement"); 121 + 122 + /* First measurement is not correct, read it out to get rid of it */ 123 + i2c_master_recv(client, rxbuf, SDP500_READ_SIZE); 124 + 125 + ret = devm_iio_device_register(dev, indio_dev); 126 + if (ret) 127 + return dev_err_probe(dev, ret, "Failed to register indio_dev"); 128 + 129 + return 0; 130 + } 131 + 132 + static const struct i2c_device_id sdp500_id[] = { 133 + { "sdp500" }, 134 + { } 135 + }; 136 + MODULE_DEVICE_TABLE(i2c, sdp500_id); 137 + 138 + static const struct of_device_id sdp500_of_match[] = { 139 + { .compatible = "sensirion,sdp500" }, 140 + { } 141 + }; 142 + MODULE_DEVICE_TABLE(of, sdp500_of_match); 143 + 144 + static struct i2c_driver sdp500_driver = { 145 + .driver = { 146 + .name = "sensirion,sdp500", 147 + .of_match_table = sdp500_of_match, 148 + }, 149 + .probe = sdp500_probe, 150 + .id_table = sdp500_id, 151 + }; 152 + module_i2c_driver(sdp500_driver); 153 + 154 + MODULE_AUTHOR("Thomas Sioutas <thomas.sioutas@prodrive-technologies.com>"); 155 + MODULE_DESCRIPTION("Driver for Sensirion SDP500 differential pressure sensor"); 156 + MODULE_LICENSE("GPL");
+14
drivers/iio/proximity/Kconfig
··· 32 32 To compile this driver as a module, choose M here: the 33 33 module will be called cros_ec_mkbp_proximity. 34 34 35 + config HX9023S 36 + tristate "TYHX HX9023S SAR sensor" 37 + select IIO_BUFFER 38 + select IIO_TRIGGERED_BUFFER 39 + select REGMAP_I2C 40 + depends on I2C 41 + help 42 + Say Y here to build a driver for TYHX HX9023S capacitive SAR sensor. 43 + This driver supports the TYHX HX9023S capacitive 44 + SAR sensors. This sensors is used for proximity detection applications. 45 + 46 + To compile this driver as a module, choose M here: the 47 + module will be called hx9023s. 48 + 35 49 config IRSD200 36 50 tristate "Murata IRS-D200 PIR sensor" 37 51 select IIO_BUFFER
+1
drivers/iio/proximity/Makefile
··· 6 6 # When adding new entries keep the list in alphabetical order 7 7 obj-$(CONFIG_AS3935) += as3935.o 8 8 obj-$(CONFIG_CROS_EC_MKBP_PROXIMITY) += cros_ec_mkbp_proximity.o 9 + obj-$(CONFIG_HX9023S) += hx9023s.o 9 10 obj-$(CONFIG_IRSD200) += irsd200.o 10 11 obj-$(CONFIG_ISL29501) += isl29501.o 11 12 obj-$(CONFIG_LIDAR_LITE_V2) += pulsedlight-lidar-lite-v2.o
+1144
drivers/iio/proximity/hx9023s.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Copyright (C) 2024 NanjingTianyihexin Electronics Ltd. 4 + * http://www.tianyihexin.com 5 + * 6 + * Driver for NanjingTianyihexin HX9023S Cap Sensor. 7 + * Datasheet available at: 8 + * http://www.tianyihexin.com/ueditor/php/upload/file/20240614/1718336303992081.pdf 9 + */ 10 + 11 + #include <linux/array_size.h> 12 + #include <linux/bitfield.h> 13 + #include <linux/bitops.h> 14 + #include <linux/cleanup.h> 15 + #include <linux/device.h> 16 + #include <linux/errno.h> 17 + #include <linux/i2c.h> 18 + #include <linux/interrupt.h> 19 + #include <linux/irqreturn.h> 20 + #include <linux/math64.h> 21 + #include <linux/mod_devicetable.h> 22 + #include <linux/module.h> 23 + #include <linux/mutex.h> 24 + #include <linux/pm.h> 25 + #include <linux/property.h> 26 + #include <linux/regmap.h> 27 + #include <linux/regulator/consumer.h> 28 + #include <linux/types.h> 29 + #include <linux/units.h> 30 + 31 + #include <asm/byteorder.h> 32 + #include <asm/unaligned.h> 33 + 34 + #include <linux/iio/buffer.h> 35 + #include <linux/iio/events.h> 36 + #include <linux/iio/iio.h> 37 + #include <linux/iio/trigger.h> 38 + #include <linux/iio/triggered_buffer.h> 39 + #include <linux/iio/trigger_consumer.h> 40 + #include <linux/iio/types.h> 41 + 42 + #define HX9023S_CHIP_ID 0x1D 43 + #define HX9023S_CH_NUM 5 44 + #define HX9023S_POS 0x03 45 + #define HX9023S_NEG 0x02 46 + #define HX9023S_NOT_CONNECTED 16 47 + 48 + #define HX9023S_GLOBAL_CTRL0 0x00 49 + #define HX9023S_PRF_CFG 0x02 50 + #define HX9023S_CH0_CFG_7_0 0x03 51 + #define HX9023S_CH4_CFG_9_8 0x0C 52 + #define HX9023S_RANGE_7_0 0x0D 53 + #define HX9023S_RANGE_9_8 0x0E 54 + #define HX9023S_RANGE_18_16 0x0F 55 + #define HX9023S_AVG0_NOSR0_CFG 0x10 56 + #define HX9023S_NOSR12_CFG 0x11 57 + #define HX9023S_NOSR34_CFG 0x12 58 + #define HX9023S_AVG12_CFG 0x13 59 + #define HX9023S_AVG34_CFG 0x14 60 + #define HX9023S_OFFSET_DAC0_7_0 0x15 61 + #define HX9023S_OFFSET_DAC4_9_8 0x1E 62 + #define HX9023S_SAMPLE_NUM_7_0 0x1F 63 + #define HX9023S_INTEGRATION_NUM_7_0 0x21 64 + #define HX9023S_CH_NUM_CFG 0x24 65 + #define HX9023S_LP_ALP_4_CFG 0x29 66 + #define HX9023S_LP_ALP_1_0_CFG 0x2A 67 + #define HX9023S_LP_ALP_3_2_CFG 0x2B 68 + #define HX9023S_UP_ALP_1_0_CFG 0x2C 69 + #define HX9023S_UP_ALP_3_2_CFG 0x2D 70 + #define HX9023S_DN_UP_ALP_0_4_CFG 0x2E 71 + #define HX9023S_DN_ALP_2_1_CFG 0x2F 72 + #define HX9023S_DN_ALP_4_3_CFG 0x30 73 + #define HX9023S_RAW_BL_RD_CFG 0x38 74 + #define HX9023S_INTERRUPT_CFG 0x39 75 + #define HX9023S_INTERRUPT_CFG1 0x3A 76 + #define HX9023S_CALI_DIFF_CFG 0x3B 77 + #define HX9023S_DITHER_CFG 0x3C 78 + #define HX9023S_DEVICE_ID 0x60 79 + #define HX9023S_PROX_STATUS 0x6B 80 + #define HX9023S_PROX_INT_HIGH_CFG 0x6C 81 + #define HX9023S_PROX_INT_LOW_CFG 0x6D 82 + #define HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 0x80 83 + #define HX9023S_PROX_LOW_DIFF_CFG_CH0_0 0x88 84 + #define HX9023S_PROX_LOW_DIFF_CFG_CH3_1 0x8F 85 + #define HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 0x9E 86 + #define HX9023S_PROX_HIGH_DIFF_CFG_CH4_1 0x9F 87 + #define HX9023S_PROX_LOW_DIFF_CFG_CH4_0 0xA2 88 + #define HX9023S_PROX_LOW_DIFF_CFG_CH4_1 0xA3 89 + #define HX9023S_CAP_INI_CH4_0 0xB3 90 + #define HX9023S_LP_DIFF_CH4_2 0xBA 91 + #define HX9023S_RAW_BL_CH4_0 0xB5 92 + #define HX9023S_LP_DIFF_CH4_0 0xB8 93 + #define HX9023S_DSP_CONFIG_CTRL1 0xC8 94 + #define HX9023S_CAP_INI_CH0_0 0xE0 95 + #define HX9023S_RAW_BL_CH0_0 0xE8 96 + #define HX9023S_LP_DIFF_CH0_0 0xF4 97 + #define HX9023S_LP_DIFF_CH3_2 0xFF 98 + 99 + #define HX9023S_DATA_LOCK_MASK BIT(4) 100 + #define HX9023S_INTERRUPT_MASK GENMASK(9, 0) 101 + #define HX9023S_PROX_DEBOUNCE_MASK GENMASK(3, 0) 102 + 103 + struct hx9023s_ch_data { 104 + s16 raw; /* Raw Data*/ 105 + s16 lp; /* Low Pass Filter Data*/ 106 + s16 bl; /* Base Line Data */ 107 + s16 diff; /* Difference of Low Pass Data and Base Line Data */ 108 + 109 + struct { 110 + unsigned int near; 111 + unsigned int far; 112 + } thres; 113 + 114 + u16 dac; 115 + u8 channel_positive; 116 + u8 channel_negative; 117 + bool sel_bl; 118 + bool sel_raw; 119 + bool sel_diff; 120 + bool sel_lp; 121 + bool enable; 122 + }; 123 + 124 + struct hx9023s_data { 125 + struct iio_trigger *trig; 126 + struct regmap *regmap; 127 + unsigned long chan_prox_stat; 128 + unsigned long chan_read; 129 + unsigned long chan_event; 130 + unsigned long ch_en_stat; 131 + unsigned long chan_in_use; 132 + unsigned int prox_state_reg; 133 + bool trigger_enabled; 134 + 135 + struct { 136 + __le16 channels[HX9023S_CH_NUM]; 137 + s64 ts __aligned(8); 138 + } buffer; 139 + 140 + /* 141 + * Serialize access to registers below: 142 + * HX9023S_PROX_INT_LOW_CFG, 143 + * HX9023S_PROX_INT_HIGH_CFG, 144 + * HX9023S_INTERRUPT_CFG, 145 + * HX9023S_CH_NUM_CFG 146 + * Serialize access to channel configuration in 147 + * hx9023s_push_events and hx9023s_trigger_handler. 148 + */ 149 + struct mutex mutex; 150 + struct hx9023s_ch_data ch_data[HX9023S_CH_NUM]; 151 + }; 152 + 153 + static const struct reg_sequence hx9023s_reg_init_list[] = { 154 + /* scan period */ 155 + REG_SEQ0(HX9023S_PRF_CFG, 0x17), 156 + 157 + /* full scale of conversion phase of each channel */ 158 + REG_SEQ0(HX9023S_RANGE_7_0, 0x11), 159 + REG_SEQ0(HX9023S_RANGE_9_8, 0x02), 160 + REG_SEQ0(HX9023S_RANGE_18_16, 0x00), 161 + 162 + /* ADC average number and OSR number of each channel */ 163 + REG_SEQ0(HX9023S_AVG0_NOSR0_CFG, 0x71), 164 + REG_SEQ0(HX9023S_NOSR12_CFG, 0x44), 165 + REG_SEQ0(HX9023S_NOSR34_CFG, 0x00), 166 + REG_SEQ0(HX9023S_AVG12_CFG, 0x33), 167 + REG_SEQ0(HX9023S_AVG34_CFG, 0x00), 168 + 169 + /* sample & integration frequency of the ADC */ 170 + REG_SEQ0(HX9023S_SAMPLE_NUM_7_0, 0x65), 171 + REG_SEQ0(HX9023S_INTEGRATION_NUM_7_0, 0x65), 172 + 173 + /* coefficient of the first order low pass filter during each channel */ 174 + REG_SEQ0(HX9023S_LP_ALP_1_0_CFG, 0x22), 175 + REG_SEQ0(HX9023S_LP_ALP_3_2_CFG, 0x22), 176 + REG_SEQ0(HX9023S_LP_ALP_4_CFG, 0x02), 177 + 178 + /* up coefficient of the first order low pass filter during each channel */ 179 + REG_SEQ0(HX9023S_UP_ALP_1_0_CFG, 0x88), 180 + REG_SEQ0(HX9023S_UP_ALP_3_2_CFG, 0x88), 181 + REG_SEQ0(HX9023S_DN_UP_ALP_0_4_CFG, 0x18), 182 + 183 + /* down coefficient of the first order low pass filter during each channel */ 184 + REG_SEQ0(HX9023S_DN_ALP_2_1_CFG, 0x11), 185 + REG_SEQ0(HX9023S_DN_ALP_4_3_CFG, 0x11), 186 + 187 + /* selection of data for the Data Mux Register to output data */ 188 + REG_SEQ0(HX9023S_RAW_BL_RD_CFG, 0xF0), 189 + 190 + /* enable the interrupt function */ 191 + REG_SEQ0(HX9023S_INTERRUPT_CFG, 0xFF), 192 + REG_SEQ0(HX9023S_INTERRUPT_CFG1, 0x3B), 193 + REG_SEQ0(HX9023S_DITHER_CFG, 0x21), 194 + 195 + /* threshold of the offset compensation */ 196 + REG_SEQ0(HX9023S_CALI_DIFF_CFG, 0x07), 197 + 198 + /* proximity persistency number(near & far) */ 199 + REG_SEQ0(HX9023S_PROX_INT_HIGH_CFG, 0x01), 200 + REG_SEQ0(HX9023S_PROX_INT_LOW_CFG, 0x01), 201 + 202 + /* disable the data lock */ 203 + REG_SEQ0(HX9023S_DSP_CONFIG_CTRL1, 0x00), 204 + }; 205 + 206 + static const struct iio_event_spec hx9023s_events[] = { 207 + { 208 + .type = IIO_EV_TYPE_THRESH, 209 + .dir = IIO_EV_DIR_RISING, 210 + .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD), 211 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 212 + }, 213 + { 214 + .type = IIO_EV_TYPE_THRESH, 215 + .dir = IIO_EV_DIR_FALLING, 216 + .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD), 217 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 218 + 219 + }, 220 + { 221 + .type = IIO_EV_TYPE_THRESH, 222 + .dir = IIO_EV_DIR_EITHER, 223 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 224 + }, 225 + }; 226 + 227 + #define HX9023S_CHANNEL(idx) \ 228 + { \ 229 + .type = IIO_PROXIMITY, \ 230 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 231 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\ 232 + .indexed = 1, \ 233 + .channel = idx, \ 234 + .address = 0, \ 235 + .event_spec = hx9023s_events, \ 236 + .num_event_specs = ARRAY_SIZE(hx9023s_events), \ 237 + .scan_index = idx, \ 238 + .scan_type = { \ 239 + .sign = 's', \ 240 + .realbits = 16, \ 241 + .storagebits = 16, \ 242 + .endianness = IIO_BE, \ 243 + }, \ 244 + } 245 + 246 + static const struct iio_chan_spec hx9023s_channels[] = { 247 + HX9023S_CHANNEL(0), 248 + HX9023S_CHANNEL(1), 249 + HX9023S_CHANNEL(2), 250 + HX9023S_CHANNEL(3), 251 + HX9023S_CHANNEL(4), 252 + IIO_CHAN_SOFT_TIMESTAMP(5), 253 + }; 254 + 255 + static const unsigned int hx9023s_samp_freq_table[] = { 256 + 2, 2, 4, 6, 8, 10, 14, 18, 22, 26, 257 + 30, 34, 38, 42, 46, 50, 56, 62, 68, 74, 258 + 80, 90, 100, 200, 300, 400, 600, 800, 1000, 2000, 259 + 3000, 4000, 260 + }; 261 + 262 + static const struct regmap_range hx9023s_rd_reg_ranges[] = { 263 + regmap_reg_range(HX9023S_GLOBAL_CTRL0, HX9023S_LP_DIFF_CH3_2), 264 + }; 265 + 266 + static const struct regmap_range hx9023s_wr_reg_ranges[] = { 267 + regmap_reg_range(HX9023S_GLOBAL_CTRL0, HX9023S_LP_DIFF_CH3_2), 268 + }; 269 + 270 + static const struct regmap_range hx9023s_volatile_reg_ranges[] = { 271 + regmap_reg_range(HX9023S_CAP_INI_CH4_0, HX9023S_LP_DIFF_CH4_2), 272 + regmap_reg_range(HX9023S_CAP_INI_CH0_0, HX9023S_LP_DIFF_CH3_2), 273 + regmap_reg_range(HX9023S_PROX_STATUS, HX9023S_PROX_STATUS), 274 + }; 275 + 276 + static const struct regmap_access_table hx9023s_rd_regs = { 277 + .yes_ranges = hx9023s_rd_reg_ranges, 278 + .n_yes_ranges = ARRAY_SIZE(hx9023s_rd_reg_ranges), 279 + }; 280 + 281 + static const struct regmap_access_table hx9023s_wr_regs = { 282 + .yes_ranges = hx9023s_wr_reg_ranges, 283 + .n_yes_ranges = ARRAY_SIZE(hx9023s_wr_reg_ranges), 284 + }; 285 + 286 + static const struct regmap_access_table hx9023s_volatile_regs = { 287 + .yes_ranges = hx9023s_volatile_reg_ranges, 288 + .n_yes_ranges = ARRAY_SIZE(hx9023s_volatile_reg_ranges), 289 + }; 290 + 291 + static const struct regmap_config hx9023s_regmap_config = { 292 + .reg_bits = 8, 293 + .val_bits = 8, 294 + .cache_type = REGCACHE_MAPLE, 295 + .rd_table = &hx9023s_rd_regs, 296 + .wr_table = &hx9023s_wr_regs, 297 + .volatile_table = &hx9023s_volatile_regs, 298 + }; 299 + 300 + static int hx9023s_interrupt_enable(struct hx9023s_data *data) 301 + { 302 + return regmap_update_bits(data->regmap, HX9023S_INTERRUPT_CFG, 303 + HX9023S_INTERRUPT_MASK, HX9023S_INTERRUPT_MASK); 304 + } 305 + 306 + static int hx9023s_interrupt_disable(struct hx9023s_data *data) 307 + { 308 + return regmap_update_bits(data->regmap, HX9023S_INTERRUPT_CFG, 309 + HX9023S_INTERRUPT_MASK, 0x00); 310 + } 311 + 312 + static int hx9023s_data_lock(struct hx9023s_data *data, bool locked) 313 + { 314 + if (locked) 315 + return regmap_update_bits(data->regmap, 316 + HX9023S_DSP_CONFIG_CTRL1, 317 + HX9023S_DATA_LOCK_MASK, 318 + HX9023S_DATA_LOCK_MASK); 319 + else 320 + return regmap_update_bits(data->regmap, 321 + HX9023S_DSP_CONFIG_CTRL1, 322 + HX9023S_DATA_LOCK_MASK, 0); 323 + } 324 + 325 + static int hx9023s_ch_cfg(struct hx9023s_data *data) 326 + { 327 + __le16 reg_list[HX9023S_CH_NUM]; 328 + u8 ch_pos[HX9023S_CH_NUM]; 329 + u8 ch_neg[HX9023S_CH_NUM]; 330 + /* Bit positions corresponding to input pin connections */ 331 + u8 conn_cs[HX9023S_CH_NUM] = { 0, 2, 4, 6, 8 }; 332 + unsigned int i; 333 + u16 reg; 334 + 335 + for (i = 0; i < HX9023S_CH_NUM; i++) { 336 + ch_pos[i] = data->ch_data[i].channel_positive == HX9023S_NOT_CONNECTED ? 337 + HX9023S_NOT_CONNECTED : conn_cs[data->ch_data[i].channel_positive]; 338 + ch_neg[i] = data->ch_data[i].channel_negative == HX9023S_NOT_CONNECTED ? 339 + HX9023S_NOT_CONNECTED : conn_cs[data->ch_data[i].channel_negative]; 340 + 341 + reg = (HX9023S_POS << ch_pos[i]) | (HX9023S_NEG << ch_neg[i]); 342 + reg_list[i] = cpu_to_le16(reg); 343 + } 344 + 345 + return regmap_bulk_write(data->regmap, HX9023S_CH0_CFG_7_0, reg_list, 346 + sizeof(reg_list)); 347 + } 348 + 349 + static int hx9023s_write_far_debounce(struct hx9023s_data *data, int val) 350 + { 351 + guard(mutex)(&data->mutex); 352 + return regmap_update_bits(data->regmap, HX9023S_PROX_INT_LOW_CFG, 353 + HX9023S_PROX_DEBOUNCE_MASK, 354 + FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val)); 355 + } 356 + 357 + static int hx9023s_write_near_debounce(struct hx9023s_data *data, int val) 358 + { 359 + guard(mutex)(&data->mutex); 360 + return regmap_update_bits(data->regmap, HX9023S_PROX_INT_HIGH_CFG, 361 + HX9023S_PROX_DEBOUNCE_MASK, 362 + FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, val)); 363 + } 364 + 365 + static int hx9023s_read_far_debounce(struct hx9023s_data *data, int *val) 366 + { 367 + int ret; 368 + 369 + ret = regmap_read(data->regmap, HX9023S_PROX_INT_LOW_CFG, val); 370 + if (ret) 371 + return ret; 372 + 373 + *val = FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, *val); 374 + 375 + return IIO_VAL_INT; 376 + } 377 + 378 + static int hx9023s_read_near_debounce(struct hx9023s_data *data, int *val) 379 + { 380 + int ret; 381 + 382 + ret = regmap_read(data->regmap, HX9023S_PROX_INT_HIGH_CFG, val); 383 + if (ret) 384 + return ret; 385 + 386 + *val = FIELD_GET(HX9023S_PROX_DEBOUNCE_MASK, *val); 387 + 388 + return IIO_VAL_INT; 389 + } 390 + 391 + static int hx9023s_get_thres_near(struct hx9023s_data *data, u8 ch, int *val) 392 + { 393 + int ret; 394 + __le16 buf; 395 + unsigned int reg, tmp; 396 + 397 + reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 : 398 + HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2); 399 + 400 + ret = regmap_bulk_read(data->regmap, reg, &buf, sizeof(buf)); 401 + if (ret) 402 + return ret; 403 + 404 + tmp = (le16_to_cpu(buf) & GENMASK(9, 0)) * 32; 405 + data->ch_data[ch].thres.near = tmp; 406 + *val = tmp; 407 + 408 + return IIO_VAL_INT; 409 + } 410 + 411 + static int hx9023s_get_thres_far(struct hx9023s_data *data, u8 ch, int *val) 412 + { 413 + int ret; 414 + __le16 buf; 415 + unsigned int reg, tmp; 416 + 417 + reg = (ch == 4) ? HX9023S_PROX_LOW_DIFF_CFG_CH4_0 : 418 + HX9023S_PROX_LOW_DIFF_CFG_CH0_0 + (ch * 2); 419 + 420 + ret = regmap_bulk_read(data->regmap, reg, &buf, sizeof(buf)); 421 + if (ret) 422 + return ret; 423 + 424 + tmp = (le16_to_cpu(buf) & GENMASK(9, 0)) * 32; 425 + data->ch_data[ch].thres.far = tmp; 426 + *val = tmp; 427 + 428 + return IIO_VAL_INT; 429 + } 430 + 431 + static int hx9023s_set_thres_near(struct hx9023s_data *data, u8 ch, int val) 432 + { 433 + __le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0)); 434 + unsigned int reg; 435 + 436 + data->ch_data[ch].thres.near = ((val / 32) & GENMASK(9, 0)) * 32; 437 + reg = (ch == 4) ? HX9023S_PROX_HIGH_DIFF_CFG_CH4_0 : 438 + HX9023S_PROX_HIGH_DIFF_CFG_CH0_0 + (ch * 2); 439 + 440 + return regmap_bulk_write(data->regmap, reg, &val_le16, sizeof(val_le16)); 441 + } 442 + 443 + static int hx9023s_set_thres_far(struct hx9023s_data *data, u8 ch, int val) 444 + { 445 + __le16 val_le16 = cpu_to_le16((val / 32) & GENMASK(9, 0)); 446 + unsigned int reg; 447 + 448 + data->ch_data[ch].thres.far = ((val / 32) & GENMASK(9, 0)) * 32; 449 + reg = (ch == 4) ? HX9023S_PROX_LOW_DIFF_CFG_CH4_0 : 450 + HX9023S_PROX_LOW_DIFF_CFG_CH0_0 + (ch * 2); 451 + 452 + return regmap_bulk_write(data->regmap, reg, &val_le16, sizeof(val_le16)); 453 + } 454 + 455 + static int hx9023s_get_prox_state(struct hx9023s_data *data) 456 + { 457 + return regmap_read(data->regmap, HX9023S_PROX_STATUS, &data->prox_state_reg); 458 + } 459 + 460 + static int hx9023s_data_select(struct hx9023s_data *data) 461 + { 462 + int ret; 463 + unsigned int i, buf; 464 + unsigned long tmp; 465 + 466 + ret = regmap_read(data->regmap, HX9023S_RAW_BL_RD_CFG, &buf); 467 + if (ret) 468 + return ret; 469 + 470 + tmp = buf; 471 + for (i = 0; i < 4; i++) { 472 + data->ch_data[i].sel_diff = test_bit(i, &tmp); 473 + data->ch_data[i].sel_lp = !data->ch_data[i].sel_diff; 474 + data->ch_data[i].sel_bl = test_bit(i + 4, &tmp); 475 + data->ch_data[i].sel_raw = !data->ch_data[i].sel_bl; 476 + } 477 + 478 + ret = regmap_read(data->regmap, HX9023S_INTERRUPT_CFG1, &buf); 479 + if (ret) 480 + return ret; 481 + 482 + tmp = buf; 483 + data->ch_data[4].sel_diff = test_bit(2, &tmp); 484 + data->ch_data[4].sel_lp = !data->ch_data[4].sel_diff; 485 + data->ch_data[4].sel_bl = test_bit(3, &tmp); 486 + data->ch_data[4].sel_raw = !data->ch_data[4].sel_bl; 487 + 488 + return 0; 489 + } 490 + 491 + static int hx9023s_sample(struct hx9023s_data *data) 492 + { 493 + int ret; 494 + unsigned int i; 495 + u8 buf[HX9023S_CH_NUM * 3]; 496 + u16 value; 497 + 498 + ret = hx9023s_data_lock(data, true); 499 + if (ret) 500 + return ret; 501 + 502 + ret = hx9023s_data_select(data); 503 + if (ret) 504 + goto err; 505 + 506 + /* 3 bytes for each of channels 0 to 3 which have contiguous registers */ 507 + ret = regmap_bulk_read(data->regmap, HX9023S_RAW_BL_CH0_0, buf, 12); 508 + if (ret) 509 + goto err; 510 + 511 + /* 3 bytes for channel 4 */ 512 + ret = regmap_bulk_read(data->regmap, HX9023S_RAW_BL_CH4_0, buf + 12, 3); 513 + if (ret) 514 + goto err; 515 + 516 + for (i = 0; i < HX9023S_CH_NUM; i++) { 517 + value = get_unaligned_le16(&buf[i * 3 + 1]); 518 + data->ch_data[i].raw = 0; 519 + data->ch_data[i].bl = 0; 520 + if (data->ch_data[i].sel_raw) 521 + data->ch_data[i].raw = value; 522 + if (data->ch_data[i].sel_bl) 523 + data->ch_data[i].bl = value; 524 + } 525 + 526 + /* 3 bytes for each of channels 0 to 3 which have contiguous registers */ 527 + ret = regmap_bulk_read(data->regmap, HX9023S_LP_DIFF_CH0_0, buf, 12); 528 + if (ret) 529 + goto err; 530 + 531 + /* 3 bytes for channel 4 */ 532 + ret = regmap_bulk_read(data->regmap, HX9023S_LP_DIFF_CH4_0, buf + 12, 3); 533 + if (ret) 534 + goto err; 535 + 536 + for (i = 0; i < HX9023S_CH_NUM; i++) { 537 + value = get_unaligned_le16(&buf[i * 3 + 1]); 538 + data->ch_data[i].lp = 0; 539 + data->ch_data[i].diff = 0; 540 + if (data->ch_data[i].sel_lp) 541 + data->ch_data[i].lp = value; 542 + if (data->ch_data[i].sel_diff) 543 + data->ch_data[i].diff = value; 544 + } 545 + 546 + for (i = 0; i < HX9023S_CH_NUM; i++) { 547 + if (data->ch_data[i].sel_lp && data->ch_data[i].sel_bl) 548 + data->ch_data[i].diff = data->ch_data[i].lp - data->ch_data[i].bl; 549 + } 550 + 551 + /* 2 bytes for each of channels 0 to 4 which have contiguous registers */ 552 + ret = regmap_bulk_read(data->regmap, HX9023S_OFFSET_DAC0_7_0, buf, 10); 553 + if (ret) 554 + goto err; 555 + 556 + for (i = 0; i < HX9023S_CH_NUM; i++) { 557 + value = get_unaligned_le16(&buf[i * 2]); 558 + value = FIELD_GET(GENMASK(11, 0), value); 559 + data->ch_data[i].dac = value; 560 + } 561 + 562 + err: 563 + return hx9023s_data_lock(data, false); 564 + } 565 + 566 + static int hx9023s_ch_en(struct hx9023s_data *data, u8 ch_id, bool en) 567 + { 568 + int ret; 569 + unsigned int buf; 570 + 571 + ret = regmap_read(data->regmap, HX9023S_CH_NUM_CFG, &buf); 572 + if (ret) 573 + return ret; 574 + 575 + data->ch_en_stat = buf; 576 + if (en && data->ch_en_stat == 0) 577 + data->prox_state_reg = 0; 578 + 579 + data->ch_data[ch_id].enable = en; 580 + __assign_bit(ch_id, &data->ch_en_stat, en); 581 + 582 + return regmap_write(data->regmap, HX9023S_CH_NUM_CFG, data->ch_en_stat); 583 + } 584 + 585 + static int hx9023s_property_get(struct hx9023s_data *data) 586 + { 587 + struct device *dev = regmap_get_device(data->regmap); 588 + u32 array[2]; 589 + u32 i, reg, temp; 590 + int ret; 591 + 592 + data->chan_in_use = 0; 593 + for (i = 0; i < HX9023S_CH_NUM; i++) { 594 + data->ch_data[i].channel_positive = HX9023S_NOT_CONNECTED; 595 + data->ch_data[i].channel_negative = HX9023S_NOT_CONNECTED; 596 + } 597 + 598 + device_for_each_child_node_scoped(dev, child) { 599 + ret = fwnode_property_read_u32(child, "reg", &reg); 600 + if (ret || reg >= HX9023S_CH_NUM) 601 + return dev_err_probe(dev, ret < 0 ? ret : -EINVAL, 602 + "Failed to read reg\n"); 603 + __set_bit(reg, &data->chan_in_use); 604 + 605 + ret = fwnode_property_read_u32(child, "single-channel", &temp); 606 + if (ret == 0) { 607 + data->ch_data[reg].channel_positive = temp; 608 + data->ch_data[reg].channel_negative = HX9023S_NOT_CONNECTED; 609 + } else { 610 + ret = fwnode_property_read_u32_array(child, "diff-channels", 611 + array, ARRAY_SIZE(array)); 612 + if (ret == 0) { 613 + data->ch_data[reg].channel_positive = array[0]; 614 + data->ch_data[reg].channel_negative = array[1]; 615 + } else { 616 + return dev_err_probe(dev, ret, 617 + "Property read failed: %d\n", 618 + reg); 619 + } 620 + } 621 + } 622 + 623 + return 0; 624 + } 625 + 626 + static int hx9023s_update_chan_en(struct hx9023s_data *data, 627 + unsigned long chan_read, 628 + unsigned long chan_event) 629 + { 630 + unsigned int i; 631 + unsigned long channels = chan_read | chan_event; 632 + 633 + if ((data->chan_read | data->chan_event) != channels) { 634 + for_each_set_bit(i, &channels, HX9023S_CH_NUM) 635 + hx9023s_ch_en(data, i, test_bit(i, &data->chan_in_use)); 636 + for_each_clear_bit(i, &channels, HX9023S_CH_NUM) 637 + hx9023s_ch_en(data, i, false); 638 + } 639 + 640 + data->chan_read = chan_read; 641 + data->chan_event = chan_event; 642 + 643 + return 0; 644 + } 645 + 646 + static int hx9023s_get_proximity(struct hx9023s_data *data, 647 + const struct iio_chan_spec *chan, 648 + int *val) 649 + { 650 + int ret; 651 + 652 + ret = hx9023s_sample(data); 653 + if (ret) 654 + return ret; 655 + 656 + ret = hx9023s_get_prox_state(data); 657 + if (ret) 658 + return ret; 659 + 660 + *val = data->ch_data[chan->channel].diff; 661 + return IIO_VAL_INT; 662 + } 663 + 664 + static int hx9023s_get_samp_freq(struct hx9023s_data *data, int *val, int *val2) 665 + { 666 + int ret; 667 + unsigned int odr, index; 668 + 669 + ret = regmap_read(data->regmap, HX9023S_PRF_CFG, &index); 670 + if (ret) 671 + return ret; 672 + 673 + odr = hx9023s_samp_freq_table[index]; 674 + *val = KILO / odr; 675 + *val2 = div_u64((KILO % odr) * MICRO, odr); 676 + 677 + return IIO_VAL_INT_PLUS_MICRO; 678 + } 679 + 680 + static int hx9023s_read_raw(struct iio_dev *indio_dev, 681 + const struct iio_chan_spec *chan, 682 + int *val, int *val2, long mask) 683 + { 684 + struct hx9023s_data *data = iio_priv(indio_dev); 685 + int ret; 686 + 687 + if (chan->type != IIO_PROXIMITY) 688 + return -EINVAL; 689 + 690 + switch (mask) { 691 + case IIO_CHAN_INFO_RAW: 692 + ret = iio_device_claim_direct_mode(indio_dev); 693 + if (ret) 694 + return ret; 695 + 696 + ret = hx9023s_get_proximity(data, chan, val); 697 + iio_device_release_direct_mode(indio_dev); 698 + return ret; 699 + case IIO_CHAN_INFO_SAMP_FREQ: 700 + return hx9023s_get_samp_freq(data, val, val2); 701 + default: 702 + return -EINVAL; 703 + } 704 + } 705 + 706 + static int hx9023s_set_samp_freq(struct hx9023s_data *data, int val, int val2) 707 + { 708 + struct device *dev = regmap_get_device(data->regmap); 709 + unsigned int i, period_ms; 710 + 711 + period_ms = div_u64(NANO, (val * MEGA + val2)); 712 + 713 + for (i = 0; i < ARRAY_SIZE(hx9023s_samp_freq_table); i++) { 714 + if (period_ms == hx9023s_samp_freq_table[i]) 715 + break; 716 + } 717 + if (i == ARRAY_SIZE(hx9023s_samp_freq_table)) { 718 + dev_err(dev, "Period:%dms NOT found!\n", period_ms); 719 + return -EINVAL; 720 + } 721 + 722 + return regmap_write(data->regmap, HX9023S_PRF_CFG, i); 723 + } 724 + 725 + static int hx9023s_write_raw(struct iio_dev *indio_dev, 726 + const struct iio_chan_spec *chan, 727 + int val, int val2, long mask) 728 + { 729 + struct hx9023s_data *data = iio_priv(indio_dev); 730 + 731 + if (chan->type != IIO_PROXIMITY) 732 + return -EINVAL; 733 + 734 + if (mask != IIO_CHAN_INFO_SAMP_FREQ) 735 + return -EINVAL; 736 + 737 + return hx9023s_set_samp_freq(data, val, val2); 738 + } 739 + 740 + static irqreturn_t hx9023s_irq_handler(int irq, void *private) 741 + { 742 + struct iio_dev *indio_dev = private; 743 + struct hx9023s_data *data = iio_priv(indio_dev); 744 + 745 + if (data->trigger_enabled) 746 + iio_trigger_poll(data->trig); 747 + 748 + return IRQ_WAKE_THREAD; 749 + } 750 + 751 + static void hx9023s_push_events(struct iio_dev *indio_dev) 752 + { 753 + struct hx9023s_data *data = iio_priv(indio_dev); 754 + s64 timestamp = iio_get_time_ns(indio_dev); 755 + unsigned long prox_changed; 756 + unsigned int chan; 757 + int ret; 758 + 759 + ret = hx9023s_sample(data); 760 + if (ret) 761 + return; 762 + 763 + ret = hx9023s_get_prox_state(data); 764 + if (ret) 765 + return; 766 + 767 + prox_changed = (data->chan_prox_stat ^ data->prox_state_reg) & data->chan_event; 768 + for_each_set_bit(chan, &prox_changed, HX9023S_CH_NUM) { 769 + unsigned int dir; 770 + 771 + dir = (data->prox_state_reg & BIT(chan)) ? 772 + IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING; 773 + 774 + iio_push_event(indio_dev, 775 + IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan, 776 + IIO_EV_TYPE_THRESH, dir), 777 + timestamp); 778 + } 779 + data->chan_prox_stat = data->prox_state_reg; 780 + } 781 + 782 + static irqreturn_t hx9023s_irq_thread_handler(int irq, void *private) 783 + { 784 + struct iio_dev *indio_dev = private; 785 + struct hx9023s_data *data = iio_priv(indio_dev); 786 + 787 + guard(mutex)(&data->mutex); 788 + hx9023s_push_events(indio_dev); 789 + 790 + return IRQ_HANDLED; 791 + } 792 + 793 + static int hx9023s_read_event_val(struct iio_dev *indio_dev, 794 + const struct iio_chan_spec *chan, 795 + enum iio_event_type type, 796 + enum iio_event_direction dir, 797 + enum iio_event_info info, int *val, int *val2) 798 + { 799 + struct hx9023s_data *data = iio_priv(indio_dev); 800 + 801 + if (chan->type != IIO_PROXIMITY) 802 + return -EINVAL; 803 + 804 + switch (info) { 805 + case IIO_EV_INFO_VALUE: 806 + switch (dir) { 807 + case IIO_EV_DIR_RISING: 808 + return hx9023s_get_thres_far(data, chan->channel, val); 809 + case IIO_EV_DIR_FALLING: 810 + return hx9023s_get_thres_near(data, chan->channel, val); 811 + default: 812 + return -EINVAL; 813 + } 814 + case IIO_EV_INFO_PERIOD: 815 + switch (dir) { 816 + case IIO_EV_DIR_RISING: 817 + return hx9023s_read_far_debounce(data, val); 818 + case IIO_EV_DIR_FALLING: 819 + return hx9023s_read_near_debounce(data, val); 820 + default: 821 + return -EINVAL; 822 + } 823 + default: 824 + return -EINVAL; 825 + } 826 + } 827 + 828 + static int hx9023s_write_event_val(struct iio_dev *indio_dev, 829 + const struct iio_chan_spec *chan, 830 + enum iio_event_type type, 831 + enum iio_event_direction dir, 832 + enum iio_event_info info, int val, int val2) 833 + { 834 + struct hx9023s_data *data = iio_priv(indio_dev); 835 + 836 + if (chan->type != IIO_PROXIMITY) 837 + return -EINVAL; 838 + 839 + switch (info) { 840 + case IIO_EV_INFO_VALUE: 841 + switch (dir) { 842 + case IIO_EV_DIR_RISING: 843 + return hx9023s_set_thres_far(data, chan->channel, val); 844 + case IIO_EV_DIR_FALLING: 845 + return hx9023s_set_thres_near(data, chan->channel, val); 846 + default: 847 + return -EINVAL; 848 + } 849 + case IIO_EV_INFO_PERIOD: 850 + switch (dir) { 851 + case IIO_EV_DIR_RISING: 852 + return hx9023s_write_far_debounce(data, val); 853 + case IIO_EV_DIR_FALLING: 854 + return hx9023s_write_near_debounce(data, val); 855 + default: 856 + return -EINVAL; 857 + } 858 + default: 859 + return -EINVAL; 860 + } 861 + } 862 + 863 + static int hx9023s_read_event_config(struct iio_dev *indio_dev, 864 + const struct iio_chan_spec *chan, 865 + enum iio_event_type type, 866 + enum iio_event_direction dir) 867 + { 868 + struct hx9023s_data *data = iio_priv(indio_dev); 869 + 870 + return test_bit(chan->channel, &data->chan_event); 871 + } 872 + 873 + static int hx9023s_write_event_config(struct iio_dev *indio_dev, 874 + const struct iio_chan_spec *chan, 875 + enum iio_event_type type, 876 + enum iio_event_direction dir, 877 + int state) 878 + { 879 + struct hx9023s_data *data = iio_priv(indio_dev); 880 + 881 + if (test_bit(chan->channel, &data->chan_in_use)) { 882 + hx9023s_ch_en(data, chan->channel, !!state); 883 + __assign_bit(chan->channel, &data->chan_event, 884 + data->ch_data[chan->channel].enable); 885 + } 886 + 887 + return 0; 888 + } 889 + 890 + static const struct iio_info hx9023s_info = { 891 + .read_raw = hx9023s_read_raw, 892 + .write_raw = hx9023s_write_raw, 893 + .read_event_value = hx9023s_read_event_val, 894 + .write_event_value = hx9023s_write_event_val, 895 + .read_event_config = hx9023s_read_event_config, 896 + .write_event_config = hx9023s_write_event_config, 897 + }; 898 + 899 + static int hx9023s_set_trigger_state(struct iio_trigger *trig, bool state) 900 + { 901 + struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 902 + struct hx9023s_data *data = iio_priv(indio_dev); 903 + 904 + guard(mutex)(&data->mutex); 905 + if (state) 906 + hx9023s_interrupt_enable(data); 907 + else if (!data->chan_read) 908 + hx9023s_interrupt_disable(data); 909 + data->trigger_enabled = state; 910 + 911 + return 0; 912 + } 913 + 914 + static const struct iio_trigger_ops hx9023s_trigger_ops = { 915 + .set_trigger_state = hx9023s_set_trigger_state, 916 + }; 917 + 918 + static irqreturn_t hx9023s_trigger_handler(int irq, void *private) 919 + { 920 + struct iio_poll_func *pf = private; 921 + struct iio_dev *indio_dev = pf->indio_dev; 922 + struct hx9023s_data *data = iio_priv(indio_dev); 923 + struct device *dev = regmap_get_device(data->regmap); 924 + unsigned int bit, index, i = 0; 925 + int ret; 926 + 927 + guard(mutex)(&data->mutex); 928 + ret = hx9023s_sample(data); 929 + if (ret) { 930 + dev_warn(dev, "sampling failed\n"); 931 + goto out; 932 + } 933 + 934 + ret = hx9023s_get_prox_state(data); 935 + if (ret) { 936 + dev_warn(dev, "get prox failed\n"); 937 + goto out; 938 + } 939 + 940 + iio_for_each_active_channel(indio_dev, bit) { 941 + index = indio_dev->channels[bit].channel; 942 + data->buffer.channels[i++] = cpu_to_le16(data->ch_data[index].diff); 943 + } 944 + 945 + iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, 946 + pf->timestamp); 947 + 948 + out: 949 + iio_trigger_notify_done(indio_dev->trig); 950 + 951 + return IRQ_HANDLED; 952 + } 953 + 954 + static int hx9023s_buffer_preenable(struct iio_dev *indio_dev) 955 + { 956 + struct hx9023s_data *data = iio_priv(indio_dev); 957 + unsigned long channels = 0; 958 + unsigned int bit; 959 + 960 + guard(mutex)(&data->mutex); 961 + iio_for_each_active_channel(indio_dev, bit) 962 + __set_bit(indio_dev->channels[bit].channel, &channels); 963 + 964 + hx9023s_update_chan_en(data, channels, data->chan_event); 965 + 966 + return 0; 967 + } 968 + 969 + static int hx9023s_buffer_postdisable(struct iio_dev *indio_dev) 970 + { 971 + struct hx9023s_data *data = iio_priv(indio_dev); 972 + 973 + guard(mutex)(&data->mutex); 974 + hx9023s_update_chan_en(data, 0, data->chan_event); 975 + 976 + return 0; 977 + } 978 + 979 + static const struct iio_buffer_setup_ops hx9023s_buffer_setup_ops = { 980 + .preenable = hx9023s_buffer_preenable, 981 + .postdisable = hx9023s_buffer_postdisable, 982 + }; 983 + 984 + static int hx9023s_id_check(struct iio_dev *indio_dev) 985 + { 986 + struct hx9023s_data *data = iio_priv(indio_dev); 987 + struct device *dev = regmap_get_device(data->regmap); 988 + unsigned int id; 989 + int ret; 990 + 991 + ret = regmap_read(data->regmap, HX9023S_DEVICE_ID, &id); 992 + if (ret) 993 + return ret; 994 + 995 + if (id != HX9023S_CHIP_ID) 996 + dev_warn(dev, "Unexpected chip ID, assuming compatible\n"); 997 + 998 + return 0; 999 + } 1000 + 1001 + static int hx9023s_probe(struct i2c_client *client) 1002 + { 1003 + struct device *dev = &client->dev; 1004 + struct iio_dev *indio_dev; 1005 + struct hx9023s_data *data; 1006 + int ret; 1007 + 1008 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 1009 + if (!indio_dev) 1010 + return -ENOMEM; 1011 + 1012 + data = iio_priv(indio_dev); 1013 + mutex_init(&data->mutex); 1014 + 1015 + data->regmap = devm_regmap_init_i2c(client, &hx9023s_regmap_config); 1016 + if (IS_ERR(data->regmap)) 1017 + return dev_err_probe(dev, PTR_ERR(data->regmap), 1018 + "regmap init failed\n"); 1019 + 1020 + ret = hx9023s_property_get(data); 1021 + if (ret) 1022 + return dev_err_probe(dev, ret, "dts phase failed\n"); 1023 + 1024 + ret = devm_regulator_get_enable(dev, "vdd"); 1025 + if (ret) 1026 + return dev_err_probe(dev, ret, "regulator get failed\n"); 1027 + 1028 + ret = hx9023s_id_check(indio_dev); 1029 + if (ret) 1030 + return dev_err_probe(dev, ret, "id check failed\n"); 1031 + 1032 + indio_dev->name = "hx9023s"; 1033 + indio_dev->channels = hx9023s_channels; 1034 + indio_dev->num_channels = ARRAY_SIZE(hx9023s_channels); 1035 + indio_dev->info = &hx9023s_info; 1036 + indio_dev->modes = INDIO_DIRECT_MODE; 1037 + i2c_set_clientdata(client, indio_dev); 1038 + 1039 + ret = regmap_multi_reg_write(data->regmap, hx9023s_reg_init_list, 1040 + ARRAY_SIZE(hx9023s_reg_init_list)); 1041 + if (ret) 1042 + return dev_err_probe(dev, ret, "device init failed\n"); 1043 + 1044 + ret = hx9023s_ch_cfg(data); 1045 + if (ret) 1046 + return dev_err_probe(dev, ret, "channel config failed\n"); 1047 + 1048 + ret = regcache_sync(data->regmap); 1049 + if (ret) 1050 + return dev_err_probe(dev, ret, "regcache sync failed\n"); 1051 + 1052 + if (client->irq) { 1053 + ret = devm_request_threaded_irq(dev, client->irq, 1054 + hx9023s_irq_handler, 1055 + hx9023s_irq_thread_handler, 1056 + IRQF_ONESHOT, 1057 + "hx9023s_event", indio_dev); 1058 + if (ret) 1059 + return dev_err_probe(dev, ret, "irq request failed\n"); 1060 + 1061 + data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d", 1062 + indio_dev->name, 1063 + iio_device_id(indio_dev)); 1064 + if (!data->trig) 1065 + return dev_err_probe(dev, -ENOMEM, 1066 + "iio trigger alloc failed\n"); 1067 + 1068 + data->trig->ops = &hx9023s_trigger_ops; 1069 + iio_trigger_set_drvdata(data->trig, indio_dev); 1070 + 1071 + ret = devm_iio_trigger_register(dev, data->trig); 1072 + if (ret) 1073 + return dev_err_probe(dev, ret, 1074 + "iio trigger register failed\n"); 1075 + } 1076 + 1077 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, 1078 + iio_pollfunc_store_time, 1079 + hx9023s_trigger_handler, 1080 + &hx9023s_buffer_setup_ops); 1081 + if (ret) 1082 + return dev_err_probe(dev, ret, 1083 + "iio triggered buffer setup failed\n"); 1084 + 1085 + return devm_iio_device_register(dev, indio_dev); 1086 + } 1087 + 1088 + static int hx9023s_suspend(struct device *dev) 1089 + { 1090 + struct hx9023s_data *data = iio_priv(dev_get_drvdata(dev)); 1091 + 1092 + guard(mutex)(&data->mutex); 1093 + hx9023s_interrupt_disable(data); 1094 + 1095 + return 0; 1096 + } 1097 + 1098 + static int hx9023s_resume(struct device *dev) 1099 + { 1100 + struct hx9023s_data *data = iio_priv(dev_get_drvdata(dev)); 1101 + 1102 + guard(mutex)(&data->mutex); 1103 + if (data->trigger_enabled) 1104 + hx9023s_interrupt_enable(data); 1105 + 1106 + return 0; 1107 + } 1108 + 1109 + static DEFINE_SIMPLE_DEV_PM_OPS(hx9023s_pm_ops, hx9023s_suspend, 1110 + hx9023s_resume); 1111 + 1112 + static const struct of_device_id hx9023s_of_match[] = { 1113 + { .compatible = "tyhx,hx9023s" }, 1114 + {} 1115 + }; 1116 + MODULE_DEVICE_TABLE(of, hx9023s_of_match); 1117 + 1118 + static const struct i2c_device_id hx9023s_id[] = { 1119 + { "hx9023s" }, 1120 + {} 1121 + }; 1122 + MODULE_DEVICE_TABLE(i2c, hx9023s_id); 1123 + 1124 + static struct i2c_driver hx9023s_driver = { 1125 + .driver = { 1126 + .name = "hx9023s", 1127 + .of_match_table = hx9023s_of_match, 1128 + .pm = &hx9023s_pm_ops, 1129 + 1130 + /* 1131 + * The I2C operations in hx9023s_reg_init() and hx9023s_ch_cfg() 1132 + * are time-consuming. Prefer async so we don't delay boot 1133 + * if we're builtin to the kernel. 1134 + */ 1135 + .probe_type = PROBE_PREFER_ASYNCHRONOUS, 1136 + }, 1137 + .probe = hx9023s_probe, 1138 + .id_table = hx9023s_id, 1139 + }; 1140 + module_i2c_driver(hx9023s_driver); 1141 + 1142 + MODULE_AUTHOR("Yasin Lee <yasin.lee.x@gmail.com>"); 1143 + MODULE_DESCRIPTION("Driver for TYHX HX9023S SAR sensor"); 1144 + MODULE_LICENSE("GPL");
+1 -2
drivers/iio/proximity/sx9500.c
··· 654 654 655 655 mutex_lock(&data->mutex); 656 656 657 - for_each_set_bit(bit, indio_dev->active_scan_mask, 658 - indio_dev->masklength) { 657 + iio_for_each_active_channel(indio_dev, bit) { 659 658 ret = sx9500_read_prox_data(data, &indio_dev->channels[bit], 660 659 &val); 661 660 if (ret < 0)
+2 -4
drivers/iio/proximity/sx_common.c
··· 369 369 370 370 mutex_lock(&data->mutex); 371 371 372 - for_each_set_bit(bit, indio_dev->active_scan_mask, 373 - indio_dev->masklength) { 372 + iio_for_each_active_channel(indio_dev, bit) { 374 373 ret = data->chip_info->ops.read_prox_data(data, 375 374 &indio_dev->channels[bit], 376 375 &val); ··· 397 398 int bit, ret; 398 399 399 400 mutex_lock(&data->mutex); 400 - for_each_set_bit(bit, indio_dev->active_scan_mask, 401 - indio_dev->masklength) 401 + iio_for_each_active_channel(indio_dev, bit) 402 402 __set_bit(indio_dev->channels[bit].channel, &channels); 403 403 404 404 ret = sx_common_update_chan_en(data, channels, data->chan_event);
+12 -3
drivers/spi/spi-axi-spi-engine.c
··· 41 41 #define SPI_ENGINE_CONFIG_CPHA BIT(0) 42 42 #define SPI_ENGINE_CONFIG_CPOL BIT(1) 43 43 #define SPI_ENGINE_CONFIG_3WIRE BIT(2) 44 + #define SPI_ENGINE_CONFIG_SDO_IDLE_HIGH BIT(3) 44 45 45 46 #define SPI_ENGINE_INST_TRANSFER 0x0 46 47 #define SPI_ENGINE_INST_ASSERT 0x1 ··· 138 137 config |= SPI_ENGINE_CONFIG_CPHA; 139 138 if (spi->mode & SPI_3WIRE) 140 139 config |= SPI_ENGINE_CONFIG_3WIRE; 140 + if (spi->mode & SPI_MOSI_IDLE_HIGH) 141 + config |= SPI_ENGINE_CONFIG_SDO_IDLE_HIGH; 142 + if (spi->mode & SPI_MOSI_IDLE_LOW) 143 + config &= ~SPI_ENGINE_CONFIG_SDO_IDLE_HIGH; 141 144 142 145 return config; 143 146 } ··· 697 692 host->num_chipselect = 8; 698 693 699 694 /* Some features depend of the IP core version. */ 700 - if (ADI_AXI_PCORE_VER_MINOR(version) >= 2) { 701 - host->mode_bits |= SPI_CS_HIGH; 702 - host->setup = spi_engine_setup; 695 + if (ADI_AXI_PCORE_VER_MAJOR(version) >= 1) { 696 + if (ADI_AXI_PCORE_VER_MINOR(version) >= 2) { 697 + host->mode_bits |= SPI_CS_HIGH; 698 + host->setup = spi_engine_setup; 699 + } 700 + if (ADI_AXI_PCORE_VER_MINOR(version) >= 3) 701 + host->mode_bits |= SPI_MOSI_IDLE_LOW | SPI_MOSI_IDLE_HIGH; 703 702 } 704 703 705 704 if (host->max_speed_hz == 0)
+24
drivers/spi/spi-bitbang.c
··· 54 54 struct spi_transfer *t, 55 55 unsigned int flags) 56 56 { 57 + struct spi_bitbang *bitbang; 57 58 unsigned int bits = t->bits_per_word; 58 59 unsigned int count = t->len; 59 60 const u8 *tx = t->tx_buf; 60 61 u8 *rx = t->rx_buf; 61 62 63 + bitbang = spi_controller_get_devdata(spi->controller); 62 64 while (likely(count > 0)) { 63 65 u8 word = 0; 64 66 65 67 if (tx) 66 68 word = *tx++; 69 + else 70 + word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFF : 0; 67 71 word = txrx_word(spi, ns, word, bits, flags); 68 72 if (rx) 69 73 *rx++ = word; 70 74 count -= 1; 71 75 } 76 + if (bitbang->set_mosi_idle) 77 + bitbang->set_mosi_idle(spi); 78 + 72 79 return t->len - count; 73 80 } 74 81 ··· 85 78 struct spi_transfer *t, 86 79 unsigned int flags) 87 80 { 81 + struct spi_bitbang *bitbang; 88 82 unsigned int bits = t->bits_per_word; 89 83 unsigned int count = t->len; 90 84 const u16 *tx = t->tx_buf; 91 85 u16 *rx = t->rx_buf; 92 86 87 + bitbang = spi_controller_get_devdata(spi->controller); 93 88 while (likely(count > 1)) { 94 89 u16 word = 0; 95 90 96 91 if (tx) 97 92 word = *tx++; 93 + else 94 + word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFF : 0; 98 95 word = txrx_word(spi, ns, word, bits, flags); 99 96 if (rx) 100 97 *rx++ = word; 101 98 count -= 2; 102 99 } 100 + if (bitbang->set_mosi_idle) 101 + bitbang->set_mosi_idle(spi); 102 + 103 103 return t->len - count; 104 104 } 105 105 ··· 116 102 struct spi_transfer *t, 117 103 unsigned int flags) 118 104 { 105 + struct spi_bitbang *bitbang; 119 106 unsigned int bits = t->bits_per_word; 120 107 unsigned int count = t->len; 121 108 const u32 *tx = t->tx_buf; 122 109 u32 *rx = t->rx_buf; 123 110 111 + bitbang = spi_controller_get_devdata(spi->controller); 124 112 while (likely(count > 3)) { 125 113 u32 word = 0; 126 114 127 115 if (tx) 128 116 word = *tx++; 117 + else 118 + word = spi->mode & SPI_MOSI_IDLE_HIGH ? 0xFFFFFFFF : 0; 129 119 word = txrx_word(spi, ns, word, bits, flags); 130 120 if (rx) 131 121 *rx++ = word; 132 122 count -= 4; 133 123 } 124 + if (bitbang->set_mosi_idle) 125 + bitbang->set_mosi_idle(spi); 126 + 134 127 return t->len - count; 135 128 } 136 129 ··· 212 191 if (retval < 0) 213 192 goto err_free; 214 193 } 194 + 195 + if (bitbang->set_mosi_idle) 196 + bitbang->set_mosi_idle(spi); 215 197 216 198 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs); 217 199
+11 -1
drivers/spi/spi-gpio.c
··· 236 236 } 237 237 } 238 238 239 + static void spi_gpio_set_mosi_idle(struct spi_device *spi) 240 + { 241 + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); 242 + 243 + gpiod_set_value_cansleep(spi_gpio->mosi, 244 + !!(spi->mode & SPI_MOSI_IDLE_HIGH)); 245 + } 246 + 239 247 static int spi_gpio_setup(struct spi_device *spi) 240 248 { 241 249 struct gpio_desc *cs; ··· 397 389 398 390 host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 399 391 host->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL | 400 - SPI_CS_HIGH | SPI_LSB_FIRST; 392 + SPI_CS_HIGH | SPI_LSB_FIRST | SPI_MOSI_IDLE_LOW | 393 + SPI_MOSI_IDLE_HIGH; 401 394 if (!spi_gpio->mosi) { 402 395 /* HW configuration without MOSI pin 403 396 * ··· 423 414 host->flags |= SPI_CONTROLLER_GPIO_SS; 424 415 bb->chipselect = spi_gpio_chipselect; 425 416 bb->set_line_direction = spi_gpio_set_direction; 417 + bb->set_mosi_idle = spi_gpio_set_mosi_idle; 426 418 427 419 if (host->flags & SPI_CONTROLLER_NO_TX) { 428 420 bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
+6
drivers/spi/spi.c
··· 3921 3921 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL | 3922 3922 SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))) 3923 3923 return -EINVAL; 3924 + /* Check against conflicting MOSI idle configuration */ 3925 + if ((spi->mode & SPI_MOSI_IDLE_LOW) && (spi->mode & SPI_MOSI_IDLE_HIGH)) { 3926 + dev_err(&spi->dev, 3927 + "setup: MOSI configured to idle low and high at the same time.\n"); 3928 + return -EINVAL; 3929 + } 3924 3930 /* 3925 3931 * Help drivers fail *cleanly* when they need options 3926 3932 * that aren't supported with their current controller.
+3 -2
drivers/staging/iio/impedance-analyzer/ad5933.c
··· 547 547 struct ad5933_state *st = iio_priv(indio_dev); 548 548 int ret; 549 549 550 - if (bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) 550 + if (bitmap_empty(indio_dev->active_scan_mask, 551 + iio_get_masklength(indio_dev))) 551 552 return -EINVAL; 552 553 553 554 ret = ad5933_reset(st); ··· 626 625 627 626 if (status & AD5933_STAT_DATA_VALID) { 628 627 int scan_count = bitmap_weight(indio_dev->active_scan_mask, 629 - indio_dev->masklength); 628 + iio_get_masklength(indio_dev)); 630 629 ret = ad5933_i2c_read(st->client, 631 630 test_bit(1, indio_dev->active_scan_mask) ? 632 631 AD5933_REG_REAL_DATA : AD5933_REG_IMAG_DATA,
+9
include/dt-bindings/iio/adi,ad4695.h
··· 1 + /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */ 2 + 3 + #ifndef _DT_BINDINGS_ADI_AD4695_H 4 + #define _DT_BINDINGS_ADI_AD4695_H 5 + 6 + #define AD4695_COMMON_MODE_REFGND 0xFF 7 + #define AD4695_COMMON_MODE_COM 0xFE 8 + 9 + #endif /* _DT_BINDINGS_ADI_AD4695_H */
+57 -5
include/linux/iio/backend.h
··· 3 3 #define _IIO_BACKEND_H_ 4 4 5 5 #include <linux/types.h> 6 + #include <linux/iio/iio.h> 6 7 7 8 struct iio_chan_spec; 8 9 struct fwnode_handle; ··· 18 17 }; 19 18 20 19 enum iio_backend_data_source { 21 - IIO_BACKEND_INTERNAL_CONTINUOS_WAVE, 20 + IIO_BACKEND_INTERNAL_CONTINUOUS_WAVE, 22 21 IIO_BACKEND_EXTERNAL, 23 22 IIO_BACKEND_DATA_SOURCE_MAX 24 23 }; 24 + 25 + #define iio_backend_debugfs_ptr(ptr) PTR_IF(IS_ENABLED(CONFIG_DEBUG_FS), ptr) 25 26 26 27 /** 27 28 * IIO_BACKEND_EX_INFO - Helper for an IIO extended channel attribute ··· 57 54 IIO_BACKEND_NO_TEST_PATTERN, 58 55 /* modified prbs9 */ 59 56 IIO_BACKEND_ADI_PRBS_9A = 32, 57 + /* modified prbs23 */ 58 + IIO_BACKEND_ADI_PRBS_23A, 60 59 IIO_BACKEND_TEST_PATTERN_MAX 61 60 }; 62 61 ··· 86 81 * @extend_chan_spec: Extend an IIO channel. 87 82 * @ext_info_set: Extended info setter. 88 83 * @ext_info_get: Extended info getter. 84 + * @read_raw: Read a channel attribute from a backend device 85 + * @debugfs_print_chan_status: Print channel status into a buffer. 86 + * @debugfs_reg_access: Read or write register value of backend. 89 87 **/ 90 88 struct iio_backend_ops { 91 89 int (*enable)(struct iio_backend *back); ··· 121 113 const char *buf, size_t len); 122 114 int (*ext_info_get)(struct iio_backend *back, uintptr_t private, 123 115 const struct iio_chan_spec *chan, char *buf); 116 + int (*read_raw)(struct iio_backend *back, 117 + struct iio_chan_spec const *chan, int *val, int *val2, 118 + long mask); 119 + int (*debugfs_print_chan_status)(struct iio_backend *back, 120 + unsigned int chan, char *buf, 121 + size_t len); 122 + int (*debugfs_reg_access)(struct iio_backend *back, unsigned int reg, 123 + unsigned int writeval, unsigned int *readval); 124 + }; 125 + 126 + /** 127 + * struct iio_backend_info - info structure for an iio_backend 128 + * @name: Backend name. 129 + * @ops: Backend operations. 130 + */ 131 + struct iio_backend_info { 132 + const char *name; 133 + const struct iio_backend_ops *ops; 124 134 }; 125 135 126 136 int iio_backend_chan_enable(struct iio_backend *back, unsigned int chan); 127 137 int iio_backend_chan_disable(struct iio_backend *back, unsigned int chan); 128 138 int devm_iio_backend_enable(struct device *dev, struct iio_backend *back); 139 + int iio_backend_enable(struct iio_backend *back); 140 + void iio_backend_disable(struct iio_backend *back); 129 141 int iio_backend_data_format_set(struct iio_backend *back, unsigned int chan, 130 142 const struct iio_backend_data_fmt *data); 131 143 int iio_backend_data_source_set(struct iio_backend *back, unsigned int chan, ··· 169 141 const char *buf, size_t len); 170 142 ssize_t iio_backend_ext_info_get(struct iio_dev *indio_dev, uintptr_t private, 171 143 const struct iio_chan_spec *chan, char *buf); 172 - 173 - int iio_backend_extend_chan_spec(struct iio_dev *indio_dev, 174 - struct iio_backend *back, 144 + int iio_backend_read_raw(struct iio_backend *back, 145 + struct iio_chan_spec const *chan, int *val, int *val2, 146 + long mask); 147 + int iio_backend_extend_chan_spec(struct iio_backend *back, 175 148 struct iio_chan_spec *chan); 176 149 void *iio_backend_get_priv(const struct iio_backend *conv); 177 150 struct iio_backend *devm_iio_backend_get(struct device *dev, const char *name); 151 + struct iio_backend *devm_iio_backend_fwnode_get(struct device *dev, 152 + const char *name, 153 + struct fwnode_handle *fwnode); 178 154 struct iio_backend * 179 155 __devm_iio_backend_get_from_fwnode_lookup(struct device *dev, 180 156 struct fwnode_handle *fwnode); 181 157 182 158 int devm_iio_backend_register(struct device *dev, 183 - const struct iio_backend_ops *ops, void *priv); 159 + const struct iio_backend_info *info, void *priv); 184 160 161 + static inline int iio_backend_read_scale(struct iio_backend *back, 162 + struct iio_chan_spec const *chan, 163 + int *val, int *val2) 164 + { 165 + return iio_backend_read_raw(back, chan, val, val2, IIO_CHAN_INFO_SCALE); 166 + } 167 + 168 + static inline int iio_backend_read_offset(struct iio_backend *back, 169 + struct iio_chan_spec const *chan, 170 + int *val, int *val2) 171 + { 172 + return iio_backend_read_raw(back, chan, val, val2, 173 + IIO_CHAN_INFO_OFFSET); 174 + } 175 + 176 + ssize_t iio_backend_debugfs_print_chan_status(struct iio_backend *back, 177 + unsigned int chan, char *buf, 178 + size_t len); 179 + void iio_backend_debugfs_add(struct iio_backend *back, 180 + struct iio_dev *indio_dev); 185 181 #endif
+38 -1
include/linux/iio/iio.h
··· 609 609 int scan_bytes; 610 610 611 611 const unsigned long *available_scan_masks; 612 - unsigned masklength; 612 + unsigned __private masklength; 613 613 const unsigned long *active_scan_mask; 614 614 bool scan_timestamp; 615 615 struct iio_trigger *trig; ··· 810 810 } 811 811 #endif 812 812 813 + /** 814 + * iio_device_suspend_triggering() - suspend trigger attached to an iio_dev 815 + * @indio_dev: iio_dev associated with the device that will have triggers suspended 816 + * 817 + * Return 0 if successful, negative otherwise 818 + **/ 819 + int iio_device_suspend_triggering(struct iio_dev *indio_dev); 820 + 821 + /** 822 + * iio_device_resume_triggering() - resume trigger attached to an iio_dev 823 + * that was previously suspended with iio_device_suspend_triggering() 824 + * @indio_dev: iio_dev associated with the device that will have triggers resumed 825 + * 826 + * Return 0 if successful, negative otherwise 827 + **/ 828 + int iio_device_resume_triggering(struct iio_dev *indio_dev); 829 + 813 830 #ifdef CONFIG_ACPI 814 831 bool iio_read_acpi_mount_matrix(struct device *dev, 815 832 struct iio_mount_matrix *orientation, ··· 871 854 872 855 return &chan->scan_type; 873 856 } 857 + 858 + /** 859 + * iio_get_masklength - Get length of the channels mask 860 + * @indio_dev: the IIO device to get the masklength for 861 + */ 862 + static inline unsigned int iio_get_masklength(const struct iio_dev *indio_dev) 863 + { 864 + return ACCESS_PRIVATE(indio_dev, masklength); 865 + } 866 + 867 + int iio_active_scan_mask_index(struct iio_dev *indio_dev); 868 + 869 + /** 870 + * iio_for_each_active_channel - Iterated over active channels 871 + * @indio_dev: the IIO device 872 + * @chan: Holds the index of the enabled channel 873 + */ 874 + #define iio_for_each_active_channel(indio_dev, chan) \ 875 + for_each_set_bit((chan), (indio_dev)->active_scan_mask, \ 876 + iio_get_masklength(indio_dev)) 874 877 875 878 ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals); 876 879
+1
include/linux/spi/spi_bitbang.h
··· 24 24 #define BITBANG_CS_ACTIVE 1 /* normally nCS, active low */ 25 25 #define BITBANG_CS_INACTIVE 0 26 26 27 + void (*set_mosi_idle)(struct spi_device *spi); 27 28 /* txrx_bufs() may handle dma mapping for transfers that don't 28 29 * already have one (transfer.{tx,rx}_dma is zero), or use PIO 29 30 */
+3 -2
include/uapi/linux/spi/spi.h
··· 28 28 #define SPI_RX_OCTAL _BITUL(14) /* receive with 8 wires */ 29 29 #define SPI_3WIRE_HIZ _BITUL(15) /* high impedance turnaround */ 30 30 #define SPI_RX_CPHA_FLIP _BITUL(16) /* flip CPHA on Rx only xfer */ 31 - #define SPI_MOSI_IDLE_LOW _BITUL(17) /* leave mosi line low when idle */ 31 + #define SPI_MOSI_IDLE_LOW _BITUL(17) /* leave MOSI line low when idle */ 32 + #define SPI_MOSI_IDLE_HIGH _BITUL(18) /* leave MOSI line high when idle */ 32 33 33 34 /* 34 35 * All the bits defined above should be covered by SPI_MODE_USER_MASK. ··· 39 38 * These bits must not overlap. A static assert check should make sure of that. 40 39 * If adding extra bits, make sure to increase the bit index below as well. 41 40 */ 42 - #define SPI_MODE_USER_MASK (_BITUL(18) - 1) 41 + #define SPI_MODE_USER_MASK (_BITUL(19) - 1) 43 42 44 43 #endif /* _UAPI_SPI_H */