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

Merge tag 'iio-for-5.8b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-next

Jonathan writes:

Second set of new device support, cleanups and features for IIO in the 5.8 cycle

Usual mixed back but with a few subsystem wide or device type
wide cleanups.

New device support

* adis16475
- New driver supporting adis16470, adis16475, adis16477, adis16465,
adis16467, adis16500, adis16505 and adis16507.
Includes some rework of the adis library to simplify using it
for this new driver.
* ak8974
- Add support for Alps hscdt008a. ID only. Related patches add support
for scale.
* atlas-sensor
- Add support for RTD-SM OEM temperature sensor.
* cm32181
- Add support for CM3218 including support for SMBUS alert via
ACPI resources.
* ltc2632
- Add support for ltc2634-12/10/8 DACS including handling per
device type numbers of channels.

Major Features

* cm32181
- ACPI bindings including parsing CPM0 and CPM1 custom ACPI tables.
Includes minor tidy ups and fixes.
* vcnl4000
- Add event support
- Add buffered data capture support
- Add control of sampling frequency

Cleanups and minor fixes.

* core
- Trivial rework of iio_device_alloc to use an early return and
improve readability.
- Precursors to addition of multiple buffer support. So far
minor refactoring.
* subsystem wide
- Use get_unaligned_be24 slightly improve readability over open
coding it.
* adis drivers
- Use iio_get_debugfs_dentry access function.
* bh1780, cm32181, cm3232, gp2ap02a00f, opt3001, st_uvis25, vl6180,
dmard06, kxsd9
- Drop use of of_match_ptr to allow ACPI based probing via PRP0001.
Part of clear out of this to avoid cut and paste into new drivers.
* ad5592r, ad5593r
- Fix typos
* ad5933
- Use managed interfaces to automate error handling and remove.
* ak8974
- Fix wrong number of 'real bits' for buffered data.
- Refactor to pull measurement code out as separate function.
bmp280
- Fix lack of clamp on range during data capture.
* at91-sama5d2_adc
- Handle unfinished conversions correctly.
- Allow use of triggers other than it's own.
- Reorganize buffer setup and tear down as part of long running
subsystem wide rework.
* ccs811
- Add DT binding docs and match table.
- Support external reset and wakeup pins.
* hid-sensors
- Reorganize buffer setup and tear down as part of long running
subsystem wide rework.
* ltr501
- Constify some structs.
* vcnl4000
- Fix an endian issue by using explicit byte swapped i2c accessors.

* tag 'iio-for-5.8b' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio: (74 commits)
iio: light: ltr501: Constify structs
staging: iio: ad5933: attach life-cycle of kfifo buffer to parent device and use managed calls throughout
iio: bmp280: fix compensation of humidity
iio: light: cm32181: Fix integartion time typo
iio: light: cm32181: Add support for parsing CPM0 and CPM1 ACPI tables
iio: light: cm32181: Make lux_per_bit and lux_per_bit_base_it runtime settings
iio: light: cm32181: Use units of 1/100000th for calibscale and lux_per_bit
iio: light: cm32181: Change reg_init to use a bitmap of which registers to init
iio: light: cm32181: Handle CM3218 ACPI devices with 2 I2C resources
iio: light: cm32181: Clean up the probe function a bit
iio: light: cm32181: Add support for the CM3218
iio: light: cm32181: Add some extra register defines
iio: light: cm32181: Add support for ACPI enumeration
iio: light: cm32181: Switch to new style i2c-driver probe function
iio: hid-sensors: move triggered buffer setup into hid_sensor_setup_trigger
iio: vcnl4000: Add buffer support for VCNL4010/20.
iio: vcnl4000: Add sampling frequency support for VCNL4010/20.
iio: vcnl4000: Add event support for VCNL4010/20.
iio: vcnl4000: Factorize data reading and writing.
iio: vcnl4000: Fix i2c swapped word reading.
...

+3471 -662
+53
Documentation/devicetree/bindings/iio/chemical/ams,ccs811.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/iio/chemical/ams,ccs811.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: AMS CCS811 VOC Sensor 8 + 9 + maintainers: 10 + - Narcisa Vasile <narcisaanamaria12@gmail.com> 11 + 12 + description: | 13 + Ultra-Low Power Digital Gas Sensor for Monitoring Indoor Air Quality. 14 + 15 + properties: 16 + compatible: 17 + enum: 18 + - ams,ccs811 19 + reg: 20 + maxItems: 1 21 + 22 + reset-gpios: 23 + description: GPIO connected to the nRESET line. This is an active low 24 + input to CCS811. 25 + maxItems: 1 26 + 27 + wakeup-gpios: 28 + description: GPIO connected to the nWAKE line. This is an active low 29 + input to CCS811. 30 + maxItems: 1 31 + 32 + required: 33 + - compatible 34 + - reg 35 + 36 + additionalProperties: false 37 + 38 + examples: 39 + - | 40 + #include <dt-bindings/gpio/gpio.h> 41 + i2c { 42 + #address-cells = <1>; 43 + #size-cells = <0>; 44 + 45 + voc@5b { 46 + compatible = "ams,ccs811"; 47 + reg = <0x5b>; 48 + reset-gpios = <&gpioa 11 GPIO_ACTIVE_LOW>; 49 + wakeup-gpios = <&gpioa 12 GPIO_ACTIVE_LOW>; 50 + }; 51 + }; 52 + 53 + ...
+2
Documentation/devicetree/bindings/iio/chemical/atlas,sensor.yaml
··· 17 17 http://www.atlas-scientific.com/_files/_datasheets/_oem/EC_oem_datasheet.pdf 18 18 http://www.atlas-scientific.com/_files/_datasheets/_oem/ORP_oem_datasheet.pdf 19 19 http://www.atlas-scientific.com/_files/_datasheets/_oem/pH_oem_datasheet.pdf 20 + http://www.atlas-scientific.com/_files/_datasheets/_oem/RTD_oem_datasheet.pdf 20 21 21 22 properties: 22 23 compatible: ··· 26 25 - atlas,ec-sm 27 26 - atlas,orp-sm 28 27 - atlas,ph-sm 28 + - atlas,rtd-sm 29 29 30 30 reg: 31 31 maxItems: 1
+7 -1
Documentation/devicetree/bindings/iio/dac/ltc2632.txt
··· 1 - Linear Technology LTC2632/2636 DAC 1 + Linear Technology LTC2632/2634/2636 DAC 2 2 3 3 Required properties: 4 4 - compatible: Has to contain one of the following: ··· 8 8 lltc,ltc2632-h12 9 9 lltc,ltc2632-h10 10 10 lltc,ltc2632-h8 11 + lltc,ltc2634-l12 12 + lltc,ltc2634-l10 13 + lltc,ltc2634-l8 14 + lltc,ltc2634-h12 15 + lltc,ltc2634-h10 16 + lltc,ltc2634-h8 11 17 lltc,ltc2636-l12 12 18 lltc,ltc2636-l10 13 19 lltc,ltc2636-l8
+137
Documentation/devicetree/bindings/iio/imu/adi,adis16475.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/imu/adi,adis16475.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Analog Devices ADIS16475 and similar IMUs 8 + 9 + maintainers: 10 + - Nuno Sá <nuno.sa@analog.com> 11 + 12 + description: | 13 + Analog Devices ADIS16475 and similar IMUs 14 + https://www.analog.com/media/en/technical-documentation/data-sheets/ADIS16475.pdf 15 + 16 + properties: 17 + compatible: 18 + enum: 19 + - adi,adis16475-1 20 + - adi,adis16475-2 21 + - adi,adis16475-3 22 + - adi,adis16477-1 23 + - adi,adis16477-2 24 + - adi,adis16477-3 25 + - adi,adis16470 26 + - adi,adis16465-1 27 + - adi,adis16465-2 28 + - adi,adis16465-3 29 + - adi,adis16467-1 30 + - adi,adis16467-2 31 + - adi,adis16467-3 32 + - adi,adis16500 33 + - adi,adis16505-1 34 + - adi,adis16505-2 35 + - adi,adis16505-3 36 + - adi,adis16507-1 37 + - adi,adis16507-2 38 + - adi,adis16507-3 39 + 40 + reg: 41 + maxItems: 1 42 + 43 + spi-cpha: true 44 + 45 + spi-cpol: true 46 + 47 + spi-max-frequency: 48 + maximum: 2000000 49 + 50 + interrupts: 51 + maxItems: 1 52 + 53 + clocks: 54 + maxItems: 1 55 + 56 + reset-gpios: 57 + description: 58 + Must be the device tree identifier of the RESET pin. If specified, 59 + it will be asserted during driver probe. As the line is active low, 60 + it should be marked GPIO_ACTIVE_LOW. 61 + maxItems: 1 62 + 63 + adi,sync-mode: 64 + description: 65 + Configures the device SYNC pin. The following modes are supported 66 + 0 - output_sync 67 + 1 - direct_sync 68 + 2 - scaled_sync 69 + 3 - pulse_sync 70 + allOf: 71 + - $ref: /schemas/types.yaml#/definitions/uint32 72 + minimum: 0 73 + maximum: 3 74 + 75 + adi,scaled-output-hz: 76 + description: 77 + This property must be present if the clock mode is scaled-sync through 78 + clock-names property. In this mode, the input clock can have a range 79 + of 1Hz to 128HZ which must be scaled to originate an allowable sample 80 + rate. This property specifies that rate. 81 + minimum: 1900 82 + maximum: 2100 83 + 84 + required: 85 + - compatible 86 + - reg 87 + - interrupts 88 + - spi-cpha 89 + - spi-cpol 90 + 91 + allOf: 92 + - if: 93 + properties: 94 + compatible: 95 + contains: 96 + enum: 97 + - adi,adis16500 98 + - adi,adis16505-1 99 + - adi,adis16505-2 100 + - adi,adis16505-3 101 + - adi,adis16507-1 102 + - adi,adis16507-2 103 + - adi,adis16507-3 104 + 105 + then: 106 + properties: 107 + adi,sync-mode: 108 + minimum: 0 109 + maximum: 2 110 + 111 + - if: 112 + properties: 113 + adi,sync-mode: 114 + enum: [1, 2, 3] 115 + 116 + then: 117 + dependencies: 118 + adi,sync-mode: [ clocks ] 119 + 120 + examples: 121 + - | 122 + #include <dt-bindings/interrupt-controller/irq.h> 123 + spi { 124 + #address-cells = <1>; 125 + #size-cells = <0>; 126 + 127 + adis16475: adis16475-3@0 { 128 + compatible = "adi,adis16475-3"; 129 + reg = <0>; 130 + spi-cpha; 131 + spi-cpol; 132 + spi-max-frequency = <2000000>; 133 + interrupts = <4 IRQ_TYPE_EDGE_RISING>; 134 + interrupt-parent = <&gpio>; 135 + }; 136 + }; 137 + ...
+3 -1
Documentation/devicetree/bindings/iio/magnetometer/ak8974.txt
··· 2 2 3 3 Required properties: 4 4 5 - - compatible : should be "asahi-kasei,ak8974" 5 + - compatible: 6 + * "asahi-kasei,ak8974" 7 + * "alps,hscdtd008a" 6 8 - reg : the I2C address of the magnetometer 7 9 8 10 Optional properties:
+8
MAINTAINERS
··· 1031 1031 F: Documentation/devicetree/bindings/iio/imu/adi,adis16460.yaml 1032 1032 F: drivers/iio/imu/adis16460.c 1033 1033 1034 + ANALOG DEVICES INC ADIS16475 DRIVER 1035 + M: Nuno Sa <nuno.sa@analog.com> 1036 + L: linux-iio@vger.kernel.org 1037 + W: http://ez.analog.com/community/linux-device-drivers 1038 + S: Supported 1039 + F: drivers/iio/imu/adis16475.c 1040 + F: Documentation/devicetree/bindings/iio/imu/adi,adis16475.yaml 1041 + 1034 1042 ANALOG DEVICES INC ADM1177 DRIVER 1035 1043 M: Beniamin Bia <beniamin.bia@analog.com> 1036 1044 M: Michael Hennerich <Michael.Hennerich@analog.com>
+2 -1
drivers/iio/accel/dmard06.c
··· 6 6 */ 7 7 8 8 #include <linux/module.h> 9 + #include <linux/mod_devicetable.h> 9 10 #include <linux/i2c.h> 10 11 #include <linux/iio/iio.h> 11 12 ··· 227 226 .id_table = dmard06_id, 228 227 .driver = { 229 228 .name = DMARD06_DRV_NAME, 230 - .of_match_table = of_match_ptr(dmard06_of_match), 229 + .of_match_table = dmard06_of_match, 231 230 .pm = DMARD06_PM_OPS, 232 231 }, 233 232 };
+4 -14
drivers/iio/accel/hid-sensor-accel-3d.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 enum accel_3d_channel { ··· 389 391 indio_dev->name = name; 390 392 indio_dev->modes = INDIO_DIRECT_MODE; 391 393 392 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 393 - NULL, NULL); 394 - if (ret) { 395 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 396 - goto error_free_dev_mem; 397 - } 398 394 atomic_set(&accel_state->common_attributes.data_ready, 0); 395 + 399 396 ret = hid_sensor_setup_trigger(indio_dev, name, 400 397 &accel_state->common_attributes); 401 398 if (ret < 0) { 402 399 dev_err(&pdev->dev, "trigger setup failed\n"); 403 - goto error_unreg_buffer_funcs; 400 + goto error_free_dev_mem; 404 401 } 405 402 406 403 ret = iio_device_register(indio_dev); ··· 419 426 error_iio_unreg: 420 427 iio_device_unregister(indio_dev); 421 428 error_remove_trigger: 422 - hid_sensor_remove_trigger(&accel_state->common_attributes); 423 - error_unreg_buffer_funcs: 424 - iio_triggered_buffer_cleanup(indio_dev); 429 + hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 425 430 error_free_dev_mem: 426 431 kfree(indio_dev->channels); 427 432 return ret; ··· 434 443 435 444 sensor_hub_remove_callback(hsdev, hsdev->usage); 436 445 iio_device_unregister(indio_dev); 437 - hid_sensor_remove_trigger(&accel_state->common_attributes); 438 - iio_triggered_buffer_cleanup(indio_dev); 446 + hid_sensor_remove_trigger(indio_dev, &accel_state->common_attributes); 439 447 kfree(indio_dev->channels); 440 448 441 449 return 0;
+2 -5
drivers/iio/accel/kxsd9-i2c.c
··· 2 2 #include <linux/device.h> 3 3 #include <linux/kernel.h> 4 4 #include <linux/module.h> 5 + #include <linux/mod_devicetable.h> 5 6 #include <linux/slab.h> 6 7 #include <linux/i2c.h> 7 8 #include <linux/delay.h> ··· 37 36 return kxsd9_common_remove(&client->dev); 38 37 } 39 38 40 - #ifdef CONFIG_OF 41 39 static const struct of_device_id kxsd9_of_match[] = { 42 40 { .compatible = "kionix,kxsd9", }, 43 41 { }, 44 42 }; 45 43 MODULE_DEVICE_TABLE(of, kxsd9_of_match); 46 - #else 47 - #define kxsd9_of_match NULL 48 - #endif 49 44 50 45 static const struct i2c_device_id kxsd9_i2c_id[] = { 51 46 {"kxsd9", 0}, ··· 52 55 static struct i2c_driver kxsd9_i2c_driver = { 53 56 .driver = { 54 57 .name = "kxsd9", 55 - .of_match_table = of_match_ptr(kxsd9_of_match), 58 + .of_match_table = kxsd9_of_match, 56 59 .pm = &kxsd9_dev_pm_ops, 57 60 }, 58 61 .probe = kxsd9_i2c_probe,
+1 -1
drivers/iio/adc/ad7476.c
··· 309 309 indio_dev->num_channels = 2; 310 310 indio_dev->info = &ad7476_info; 311 311 312 - if (st->convst_gpio && st->chip_info->convst_channel) 312 + if (st->convst_gpio) 313 313 indio_dev->channels = st->chip_info->convst_channel; 314 314 /* Setup default message */ 315 315
+2 -6
drivers/iio/adc/ad_sigma_delta.c
··· 70 70 71 71 switch (size) { 72 72 case 3: 73 - data[1] = val >> 16; 74 - data[2] = val >> 8; 75 - data[3] = val; 73 + put_unaligned_be24(val, &data[1]); 76 74 break; 77 75 case 2: 78 76 put_unaligned_be16(val, &data[1]); ··· 155 157 *val = get_unaligned_be32(sigma_delta->data); 156 158 break; 157 159 case 3: 158 - *val = (sigma_delta->data[0] << 16) | 159 - (sigma_delta->data[1] << 8) | 160 - sigma_delta->data[2]; 160 + *val = get_unaligned_be24(&sigma_delta->data[0]); 161 161 break; 162 162 case 2: 163 163 *val = get_unaligned_be16(sigma_delta->data);
+140 -93
drivers/iio/adc/at91-sama5d2_adc.c
··· 8 8 9 9 #include <linux/bitops.h> 10 10 #include <linux/clk.h> 11 + #include <linux/delay.h> 11 12 #include <linux/dma-mapping.h> 12 13 #include <linux/dmaengine.h> 13 14 #include <linux/interrupt.h> ··· 101 100 #define AT91_SAMA5D2_IER_YRDY BIT(21) 102 101 /* Interrupt Enable Register - TS pressure measurement ready */ 103 102 #define AT91_SAMA5D2_IER_PRDY BIT(22) 103 + /* Interrupt Enable Register - Data ready */ 104 + #define AT91_SAMA5D2_IER_DRDY BIT(24) 104 105 /* Interrupt Enable Register - general overrun error */ 105 106 #define AT91_SAMA5D2_IER_GOVRE BIT(25) 106 107 /* Interrupt Enable Register - Pen detect */ ··· 489 486 return at91_adc_chan_xlate(indio_dev, iiospec->args[0]); 490 487 } 491 488 489 + static unsigned int at91_adc_active_scan_mask_to_reg(struct iio_dev *indio_dev) 490 + { 491 + u32 mask = 0; 492 + u8 bit; 493 + 494 + for_each_set_bit(bit, indio_dev->active_scan_mask, 495 + indio_dev->num_channels) { 496 + struct iio_chan_spec const *chan = 497 + at91_adc_chan_get(indio_dev, bit); 498 + mask |= BIT(chan->channel); 499 + } 500 + 501 + return mask & GENMASK(11, 0); 502 + } 503 + 492 504 static void at91_adc_config_emr(struct at91_adc_state *st) 493 505 { 494 506 /* configure the extended mode register */ ··· 728 710 struct iio_dev *indio = iio_trigger_get_drvdata(trig); 729 711 struct at91_adc_state *st = iio_priv(indio); 730 712 u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR); 731 - u8 bit; 732 713 733 714 /* clear TRGMOD */ 734 715 status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK; ··· 737 720 738 721 /* set/unset hw trigger */ 739 722 at91_adc_writel(st, AT91_SAMA5D2_TRGR, status); 740 - 741 - for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) { 742 - struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit); 743 - u32 cor; 744 - 745 - if (!chan) 746 - continue; 747 - /* these channel types cannot be handled by this trigger */ 748 - if (chan->type == IIO_POSITIONRELATIVE || 749 - chan->type == IIO_PRESSURE) 750 - continue; 751 - 752 - if (state) { 753 - cor = at91_adc_readl(st, AT91_SAMA5D2_COR); 754 - 755 - if (chan->differential) 756 - cor |= (BIT(chan->channel) | 757 - BIT(chan->channel2)) << 758 - AT91_SAMA5D2_COR_DIFF_OFFSET; 759 - else 760 - cor &= ~(BIT(chan->channel) << 761 - AT91_SAMA5D2_COR_DIFF_OFFSET); 762 - 763 - at91_adc_writel(st, AT91_SAMA5D2_COR, cor); 764 - } 765 - 766 - if (state) { 767 - at91_adc_writel(st, AT91_SAMA5D2_CHER, 768 - BIT(chan->channel)); 769 - /* enable irq only if not using DMA */ 770 - if (!st->dma_st.dma_chan) { 771 - at91_adc_writel(st, AT91_SAMA5D2_IER, 772 - BIT(chan->channel)); 773 - } 774 - } else { 775 - /* disable irq only if not using DMA */ 776 - if (!st->dma_st.dma_chan) { 777 - at91_adc_writel(st, AT91_SAMA5D2_IDR, 778 - BIT(chan->channel)); 779 - } 780 - at91_adc_writel(st, AT91_SAMA5D2_CHDR, 781 - BIT(chan->channel)); 782 - } 783 - } 784 723 785 724 return 0; 786 725 } ··· 754 781 755 782 /* Needed to ACK the DRDY interruption */ 756 783 at91_adc_readl(st, AT91_SAMA5D2_LCDR); 784 + 757 785 return 0; 758 786 } 759 787 ··· 862 888 return 0; 863 889 } 864 890 865 - static int at91_adc_buffer_postenable(struct iio_dev *indio_dev) 891 + static bool at91_adc_buffer_check_use_irq(struct iio_dev *indio, 892 + struct at91_adc_state *st) 893 + { 894 + /* if using DMA, we do not use our own IRQ (we use DMA-controller) */ 895 + if (st->dma_st.dma_chan) 896 + return false; 897 + /* if the trigger is not ours, then it has its own IRQ */ 898 + if (iio_trigger_validate_own_device(indio->trig, indio)) 899 + return false; 900 + return true; 901 + } 902 + 903 + static bool at91_adc_current_chan_is_touch(struct iio_dev *indio_dev) 904 + { 905 + struct at91_adc_state *st = iio_priv(indio_dev); 906 + 907 + return !!bitmap_subset(indio_dev->active_scan_mask, 908 + &st->touch_st.channels_bitmask, 909 + AT91_SAMA5D2_MAX_CHAN_IDX + 1); 910 + } 911 + 912 + static int at91_adc_buffer_preenable(struct iio_dev *indio_dev) 866 913 { 867 914 int ret; 915 + u8 bit; 868 916 struct at91_adc_state *st = iio_priv(indio_dev); 869 917 870 918 /* check if we are enabling triggered buffer or the touchscreen */ 871 - if (bitmap_subset(indio_dev->active_scan_mask, 872 - &st->touch_st.channels_bitmask, 873 - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { 874 - /* touchscreen enabling */ 919 + if (at91_adc_current_chan_is_touch(indio_dev)) 875 920 return at91_adc_configure_touch(st, true); 876 - } 921 + 877 922 /* if we are not in triggered mode, we cannot enable the buffer. */ 878 923 if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) 879 924 return -EINVAL; ··· 904 911 return ret; 905 912 } 906 913 914 + for_each_set_bit(bit, indio_dev->active_scan_mask, 915 + indio_dev->num_channels) { 916 + struct iio_chan_spec const *chan = 917 + at91_adc_chan_get(indio_dev, bit); 918 + u32 cor; 919 + 920 + if (!chan) 921 + continue; 922 + /* these channel types cannot be handled by this trigger */ 923 + if (chan->type == IIO_POSITIONRELATIVE || 924 + chan->type == IIO_PRESSURE) 925 + continue; 926 + 927 + cor = at91_adc_readl(st, AT91_SAMA5D2_COR); 928 + 929 + if (chan->differential) 930 + cor |= (BIT(chan->channel) | BIT(chan->channel2)) << 931 + AT91_SAMA5D2_COR_DIFF_OFFSET; 932 + else 933 + cor &= ~(BIT(chan->channel) << 934 + AT91_SAMA5D2_COR_DIFF_OFFSET); 935 + 936 + at91_adc_writel(st, AT91_SAMA5D2_COR, cor); 937 + 938 + at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel)); 939 + } 940 + 941 + if (at91_adc_buffer_check_use_irq(indio_dev, st)) 942 + at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_DRDY); 943 + 944 + return 0; 945 + } 946 + 947 + static int at91_adc_buffer_postenable(struct iio_dev *indio_dev) 948 + { 949 + if (at91_adc_current_chan_is_touch(indio_dev)) 950 + return 0; 951 + 907 952 return iio_triggered_buffer_postenable(indio_dev); 908 953 } 909 954 910 - static int at91_adc_buffer_predisable(struct iio_dev *indio_dev) 955 + static int at91_adc_buffer_postdisable(struct iio_dev *indio_dev) 911 956 { 912 957 struct at91_adc_state *st = iio_priv(indio_dev); 913 - int ret; 914 958 u8 bit; 915 959 916 960 /* check if we are disabling triggered buffer or the touchscreen */ 917 - if (bitmap_subset(indio_dev->active_scan_mask, 918 - &st->touch_st.channels_bitmask, 919 - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { 920 - /* touchscreen disable */ 961 + if (at91_adc_current_chan_is_touch(indio_dev)) 921 962 return at91_adc_configure_touch(st, false); 922 - } 963 + 923 964 /* if we are not in triggered mode, nothing to do here */ 924 965 if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES)) 925 966 return -EINVAL; 926 967 927 - /* continue with the triggered buffer */ 928 - ret = iio_triggered_buffer_predisable(indio_dev); 929 - if (ret < 0) 930 - dev_err(&indio_dev->dev, "buffer predisable failed\n"); 931 - 932 - if (!st->dma_st.dma_chan) 933 - return ret; 934 - 935 - /* if we are using DMA we must clear registers and end DMA */ 936 - dmaengine_terminate_sync(st->dma_st.dma_chan); 937 - 938 968 /* 939 - * For each enabled channel we must read the last converted value 969 + * For each enable channel we must disable it in hardware. 970 + * In the case of DMA, we must read the last converted value 940 971 * to clear EOC status and not get a possible interrupt later. 941 - * This value is being read by DMA from LCDR anyway 972 + * This value is being read by DMA from LCDR anyway, so it's not lost. 942 973 */ 943 974 for_each_set_bit(bit, indio_dev->active_scan_mask, 944 975 indio_dev->num_channels) { ··· 975 958 if (chan->type == IIO_POSITIONRELATIVE || 976 959 chan->type == IIO_PRESSURE) 977 960 continue; 961 + 962 + at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel)); 963 + 978 964 if (st->dma_st.dma_chan) 979 965 at91_adc_readl(st, chan->address); 980 966 } 981 967 968 + if (at91_adc_buffer_check_use_irq(indio_dev, st)) 969 + at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_DRDY); 970 + 982 971 /* read overflow register to clear possible overflow status */ 983 972 at91_adc_readl(st, AT91_SAMA5D2_OVER); 984 - return ret; 973 + 974 + /* if we are using DMA we must clear registers and end DMA */ 975 + if (st->dma_st.dma_chan) 976 + dmaengine_terminate_sync(st->dma_st.dma_chan); 977 + 978 + return 0; 979 + } 980 + 981 + static int at91_adc_buffer_predisable(struct iio_dev *indio_dev) 982 + { 983 + if (at91_adc_current_chan_is_touch(indio_dev)) 984 + return 0; 985 + 986 + return iio_triggered_buffer_predisable(indio_dev); 985 987 } 986 988 987 989 static const struct iio_buffer_setup_ops at91_buffer_setup_ops = { 990 + .preenable = &at91_adc_buffer_preenable, 991 + .postdisable = &at91_adc_buffer_postdisable, 988 992 .postenable = &at91_adc_buffer_postenable, 989 993 .predisable = &at91_adc_buffer_predisable, 990 994 }; ··· 1053 1015 int i = 0; 1054 1016 int val; 1055 1017 u8 bit; 1018 + u32 mask = at91_adc_active_scan_mask_to_reg(indio_dev); 1019 + unsigned int timeout = 50; 1020 + 1021 + /* 1022 + * Check if the conversion is ready. If not, wait a little bit, and 1023 + * in case of timeout exit with an error. 1024 + */ 1025 + while ((at91_adc_readl(st, AT91_SAMA5D2_ISR) & mask) != mask && 1026 + timeout) { 1027 + usleep_range(50, 100); 1028 + timeout--; 1029 + } 1030 + 1031 + /* Cannot read data, not ready. Continue without reporting data */ 1032 + if (!timeout) 1033 + return; 1056 1034 1057 1035 for_each_set_bit(bit, indio_dev->active_scan_mask, 1058 1036 indio_dev->num_channels) { ··· 1156 1102 struct iio_dev *indio_dev = pf->indio_dev; 1157 1103 struct at91_adc_state *st = iio_priv(indio_dev); 1158 1104 1105 + /* 1106 + * If it's not our trigger, start a conversion now, as we are 1107 + * actually polling the trigger now. 1108 + */ 1109 + if (iio_trigger_validate_own_device(indio_dev->trig, indio_dev)) 1110 + at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START); 1111 + 1159 1112 if (st->dma_st.dma_chan) 1160 1113 at91_adc_trigger_handler_dma(indio_dev); 1161 1114 else ··· 1175 1114 1176 1115 static int at91_adc_buffer_init(struct iio_dev *indio) 1177 1116 { 1178 - struct at91_adc_state *st = iio_priv(indio); 1179 - 1180 - if (st->selected_trig->hw_trig) { 1181 - return devm_iio_triggered_buffer_setup(&indio->dev, indio, 1182 - &iio_pollfunc_store_time, 1183 - &at91_adc_trigger_handler, &at91_buffer_setup_ops); 1184 - } 1185 - /* 1186 - * we need to prepare the buffer ops in case we will get 1187 - * another buffer attached (like a callback buffer for the touchscreen) 1188 - */ 1189 - indio->setup_ops = &at91_buffer_setup_ops; 1190 - 1191 - return 0; 1117 + return devm_iio_triggered_buffer_setup(&indio->dev, indio, 1118 + &iio_pollfunc_store_time, 1119 + &at91_adc_trigger_handler, &at91_buffer_setup_ops); 1192 1120 } 1193 1121 1194 1122 static unsigned at91_adc_startup_time(unsigned startup_time_min, ··· 1331 1281 status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR); 1332 1282 status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR); 1333 1283 status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR); 1334 - } else if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) { 1284 + } else if (iio_buffer_enabled(indio) && 1285 + (status & AT91_SAMA5D2_IER_DRDY)) { 1335 1286 /* triggered buffer without DMA */ 1336 1287 disable_irq_nosync(irq); 1337 1288 iio_trigger_poll(indio->trig); ··· 1952 1901 return 0; 1953 1902 1954 1903 /* check if we are enabling triggered buffer or the touchscreen */ 1955 - if (bitmap_subset(indio_dev->active_scan_mask, 1956 - &st->touch_st.channels_bitmask, 1957 - AT91_SAMA5D2_MAX_CHAN_IDX + 1)) { 1958 - /* touchscreen enabling */ 1904 + if (at91_adc_current_chan_is_touch(indio_dev)) 1959 1905 return at91_adc_configure_touch(st, true); 1960 - } else { 1906 + else 1961 1907 return at91_adc_configure_trigger(st->trig, true); 1962 - } 1963 1908 1964 1909 /* not needed but more explicit */ 1965 1910 return 0;
+3 -2
drivers/iio/adc/mcp3422.c
··· 19 19 #include <linux/delay.h> 20 20 #include <linux/sysfs.h> 21 21 #include <linux/of.h> 22 + #include <asm/unaligned.h> 22 23 23 24 #include <linux/iio/iio.h> 24 25 #include <linux/iio/sysfs.h> ··· 118 117 119 118 if (sample_rate == MCP3422_SRATE_3) { 120 119 ret = i2c_master_recv(adc->i2c, buf, 4); 121 - temp = buf[0] << 16 | buf[1] << 8 | buf[2]; 120 + temp = get_unaligned_be24(&buf[0]); 122 121 *config = buf[3]; 123 122 } else { 124 123 ret = i2c_master_recv(adc->i2c, buf, 3); 125 - temp = buf[0] << 8 | buf[1]; 124 + temp = get_unaligned_be16(&buf[0]); 126 125 *config = buf[2]; 127 126 } 128 127
+3 -4
drivers/iio/adc/ti-ads124s08.c
··· 22 22 #include <linux/iio/triggered_buffer.h> 23 23 #include <linux/iio/sysfs.h> 24 24 25 + #include <asm/unaligned.h> 26 + 25 27 /* Commands */ 26 28 #define ADS124S08_CMD_NOP 0x00 27 29 #define ADS124S08_CMD_WAKEUP 0x02 ··· 190 188 { 191 189 struct ads124s_private *priv = iio_priv(indio_dev); 192 190 int ret; 193 - u32 tmp; 194 191 struct spi_transfer t[] = { 195 192 { 196 193 .tx_buf = &priv->data[0], ··· 209 208 if (ret < 0) 210 209 return ret; 211 210 212 - tmp = priv->data[2] << 16 | priv->data[3] << 8 | priv->data[4]; 213 - 214 - return tmp; 211 + return get_unaligned_be24(&priv->data[2]); 215 212 } 216 213 217 214 static int ads124s_read_raw(struct iio_dev *indio_dev,
+29
drivers/iio/chemical/atlas-sensor.c
··· 53 53 #define ATLAS_REG_DO_CALIB_STATUS_PRESSURE BIT(0) 54 54 #define ATLAS_REG_DO_CALIB_STATUS_DO BIT(1) 55 55 56 + #define ATLAS_REG_RTD_DATA 0x0e 57 + 56 58 #define ATLAS_REG_PH_TEMP_DATA 0x0e 57 59 #define ATLAS_REG_PH_DATA 0x16 58 60 ··· 74 72 #define ATLAS_EC_INT_TIME_IN_MS 650 75 73 #define ATLAS_ORP_INT_TIME_IN_MS 450 76 74 #define ATLAS_DO_INT_TIME_IN_MS 450 75 + #define ATLAS_RTD_INT_TIME_IN_MS 450 77 76 78 77 enum { 79 78 ATLAS_PH_SM, 80 79 ATLAS_EC_SM, 81 80 ATLAS_ORP_SM, 82 81 ATLAS_DO_SM, 82 + ATLAS_RTD_SM, 83 83 }; 84 84 85 85 struct atlas_data { ··· 208 204 .output = 1, 209 205 .scan_index = -1 210 206 }, 207 + }; 208 + 209 + static const struct iio_chan_spec atlas_rtd_channels[] = { 210 + { 211 + .type = IIO_TEMP, 212 + .address = ATLAS_REG_RTD_DATA, 213 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 214 + .scan_index = 0, 215 + .scan_type = { 216 + .sign = 's', 217 + .realbits = 32, 218 + .storagebits = 32, 219 + .endianness = IIO_BE, 220 + }, 221 + }, 222 + IIO_CHAN_SOFT_TIMESTAMP(1), 211 223 }; 212 224 213 225 static int atlas_check_ph_calibration(struct atlas_data *data) ··· 370 350 .calibration = &atlas_check_do_calibration, 371 351 .delay = ATLAS_DO_INT_TIME_IN_MS, 372 352 }, 353 + [ATLAS_RTD_SM] = { 354 + .channels = atlas_rtd_channels, 355 + .num_channels = 2, 356 + .data_reg = ATLAS_REG_RTD_DATA, 357 + .delay = ATLAS_RTD_INT_TIME_IN_MS, 358 + }, 373 359 }; 374 360 375 361 static int atlas_set_powermode(struct atlas_data *data, int on) ··· 503 477 struct atlas_data *data = iio_priv(indio_dev); 504 478 505 479 switch (mask) { 480 + case IIO_CHAN_INFO_PROCESSED: 506 481 case IIO_CHAN_INFO_RAW: { 507 482 int ret; 508 483 __be32 reg; ··· 592 565 { "atlas-ec-sm", ATLAS_EC_SM}, 593 566 { "atlas-orp-sm", ATLAS_ORP_SM}, 594 567 { "atlas-do-sm", ATLAS_DO_SM}, 568 + { "atlas-rtd-sm", ATLAS_RTD_SM}, 595 569 {} 596 570 }; 597 571 MODULE_DEVICE_TABLE(i2c, atlas_id); ··· 602 574 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, }, 603 575 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, }, 604 576 { .compatible = "atlas,do-sm", .data = (void *)ATLAS_DO_SM, }, 577 + { .compatible = "atlas,rtd-sm", .data = (void *)ATLAS_RTD_SM, }, 605 578 { } 606 579 }; 607 580 MODULE_DEVICE_TABLE(of, atlas_dt_ids);
+114 -24
drivers/iio/chemical/ccs811.c
··· 16 16 */ 17 17 18 18 #include <linux/delay.h> 19 + #include <linux/gpio/consumer.h> 19 20 #include <linux/i2c.h> 20 21 #include <linux/iio/iio.h> 21 22 #include <linux/iio/buffer.h> ··· 37 36 #define CCS811_ERR 0xE0 38 37 /* Used to transition from boot to application mode */ 39 38 #define CCS811_APP_START 0xF4 39 + #define CCS811_SW_RESET 0xFF 40 40 41 41 /* Status register flags */ 42 42 #define CCS811_STATUS_ERROR BIT(0) ··· 76 74 struct mutex lock; /* Protect readings */ 77 75 struct ccs811_reading buffer; 78 76 struct iio_trigger *drdy_trig; 77 + struct gpio_desc *wakeup_gpio; 79 78 bool drdy_trig_on; 80 79 }; 81 80 ··· 169 166 CCS811_MODE_IAQ_1SEC); 170 167 } 171 168 169 + static void ccs811_set_wakeup(struct ccs811_data *data, bool enable) 170 + { 171 + if (!data->wakeup_gpio) 172 + return; 173 + 174 + gpiod_set_value(data->wakeup_gpio, enable); 175 + 176 + if (enable) 177 + usleep_range(50, 60); 178 + else 179 + usleep_range(20, 30); 180 + } 181 + 172 182 static int ccs811_get_measurement(struct ccs811_data *data) 173 183 { 174 184 int ret, tries = 11; 185 + 186 + ccs811_set_wakeup(data, true); 175 187 176 188 /* Maximum waiting time: 1s, as measurements are made every second */ 177 189 while (tries-- > 0) { ··· 201 183 if (!(ret & CCS811_STATUS_DATA_READY)) 202 184 return -EIO; 203 185 204 - return i2c_smbus_read_i2c_block_data(data->client, 186 + ret = i2c_smbus_read_i2c_block_data(data->client, 205 187 CCS811_ALG_RESULT_DATA, 8, 206 188 (char *)&data->buffer); 189 + ccs811_set_wakeup(data, false); 190 + 191 + return ret; 207 192 } 208 193 209 194 static int ccs811_read_raw(struct iio_dev *indio_dev, ··· 357 336 return IRQ_HANDLED; 358 337 } 359 338 339 + static int ccs811_reset(struct i2c_client *client) 340 + { 341 + struct gpio_desc *reset_gpio; 342 + int ret; 343 + 344 + reset_gpio = devm_gpiod_get_optional(&client->dev, "reset", 345 + GPIOD_OUT_LOW); 346 + if (IS_ERR(reset_gpio)) 347 + return PTR_ERR(reset_gpio); 348 + 349 + /* Try to reset using nRESET pin if available else do SW reset */ 350 + if (reset_gpio) { 351 + gpiod_set_value(reset_gpio, 1); 352 + usleep_range(20, 30); 353 + gpiod_set_value(reset_gpio, 0); 354 + } else { 355 + /* 356 + * As per the datasheet, this sequence of values needs to be 357 + * written to the SW_RESET register for triggering the soft 358 + * reset in the device and placing it in boot mode. 359 + */ 360 + static const u8 reset_seq[] = { 361 + 0x11, 0xE5, 0x72, 0x8A, 362 + }; 363 + 364 + ret = i2c_smbus_write_i2c_block_data(client, CCS811_SW_RESET, 365 + sizeof(reset_seq), reset_seq); 366 + if (ret < 0) { 367 + dev_err(&client->dev, "Failed to reset sensor\n"); 368 + return ret; 369 + } 370 + } 371 + 372 + /* tSTART delay required after reset */ 373 + usleep_range(1000, 2000); 374 + 375 + return 0; 376 + } 377 + 360 378 static int ccs811_probe(struct i2c_client *client, 361 379 const struct i2c_device_id *id) 362 380 { ··· 408 348 | I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 409 349 return -EOPNOTSUPP; 410 350 411 - /* Check hardware id (should be 0x81 for this family of devices) */ 412 - ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID); 413 - if (ret < 0) 414 - return ret; 415 - 416 - if (ret != CCS811_HW_ID_VALUE) { 417 - dev_err(&client->dev, "hardware id doesn't match CCS81x\n"); 418 - return -ENODEV; 419 - } 420 - 421 - ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION); 422 - if (ret < 0) 423 - return ret; 424 - 425 - if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) { 426 - dev_err(&client->dev, "no CCS811 sensor\n"); 427 - return -ENODEV; 428 - } 429 - 430 351 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 431 352 if (!indio_dev) 432 353 return -ENOMEM; 433 354 434 - ret = ccs811_setup(client); 435 - if (ret < 0) 436 - return ret; 437 - 438 355 data = iio_priv(indio_dev); 439 356 i2c_set_clientdata(client, indio_dev); 440 357 data->client = client; 358 + 359 + data->wakeup_gpio = devm_gpiod_get_optional(&client->dev, "wakeup", 360 + GPIOD_OUT_HIGH); 361 + if (IS_ERR(data->wakeup_gpio)) 362 + return PTR_ERR(data->wakeup_gpio); 363 + 364 + ccs811_set_wakeup(data, true); 365 + 366 + ret = ccs811_reset(client); 367 + if (ret) { 368 + ccs811_set_wakeup(data, false); 369 + return ret; 370 + } 371 + 372 + /* Check hardware id (should be 0x81 for this family of devices) */ 373 + ret = i2c_smbus_read_byte_data(client, CCS811_HW_ID); 374 + if (ret < 0) { 375 + ccs811_set_wakeup(data, false); 376 + return ret; 377 + } 378 + 379 + if (ret != CCS811_HW_ID_VALUE) { 380 + dev_err(&client->dev, "hardware id doesn't match CCS81x\n"); 381 + ccs811_set_wakeup(data, false); 382 + return -ENODEV; 383 + } 384 + 385 + ret = i2c_smbus_read_byte_data(client, CCS811_HW_VERSION); 386 + if (ret < 0) { 387 + ccs811_set_wakeup(data, false); 388 + return ret; 389 + } 390 + 391 + if ((ret & CCS811_HW_VERSION_MASK) != CCS811_HW_VERSION_VALUE) { 392 + dev_err(&client->dev, "no CCS811 sensor\n"); 393 + ccs811_set_wakeup(data, false); 394 + return -ENODEV; 395 + } 396 + 397 + ret = ccs811_setup(client); 398 + if (ret < 0) { 399 + ccs811_set_wakeup(data, false); 400 + return ret; 401 + } 402 + 403 + ccs811_set_wakeup(data, false); 441 404 442 405 mutex_init(&data->lock); 443 406 ··· 549 466 }; 550 467 MODULE_DEVICE_TABLE(i2c, ccs811_id); 551 468 469 + static const struct of_device_id ccs811_dt_ids[] = { 470 + { .compatible = "ams,ccs811" }, 471 + { } 472 + }; 473 + MODULE_DEVICE_TABLE(of, ccs811_dt_ids); 474 + 552 475 static struct i2c_driver ccs811_driver = { 553 476 .driver = { 554 477 .name = "ccs811", 478 + .of_match_table = ccs811_dt_ids, 555 479 }, 556 480 .probe = ccs811_probe, 557 481 .remove = ccs811_remove,
+15 -3
drivers/iio/common/hid-sensors/hid-sensor-trigger.c
··· 13 13 #include <linux/hid-sensor-hub.h> 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/trigger.h> 16 + #include <linux/iio/triggered_buffer.h> 17 + #include <linux/iio/trigger_consumer.h> 16 18 #include <linux/iio/buffer.h> 17 19 #include <linux/iio/sysfs.h> 18 20 #include "hid-sensor-trigger.h" ··· 224 222 return hid_sensor_power_state(iio_trigger_get_drvdata(trig), state); 225 223 } 226 224 227 - void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) 225 + void hid_sensor_remove_trigger(struct iio_dev *indio_dev, 226 + struct hid_sensor_common *attrb) 228 227 { 229 228 if (atomic_read(&attrb->runtime_pm_enable)) 230 229 pm_runtime_disable(&attrb->pdev->dev); ··· 236 233 cancel_work_sync(&attrb->work); 237 234 iio_trigger_unregister(attrb->trigger); 238 235 iio_trigger_free(attrb->trigger); 236 + iio_triggered_buffer_cleanup(indio_dev); 239 237 } 240 238 EXPORT_SYMBOL(hid_sensor_remove_trigger); 241 239 ··· 250 246 int ret; 251 247 struct iio_trigger *trig; 252 248 249 + ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 250 + NULL, NULL); 251 + if (ret) { 252 + dev_err(&indio_dev->dev, "Triggered Buffer Setup Failed\n"); 253 + return ret; 254 + } 255 + 253 256 trig = iio_trigger_alloc("%s-dev%d", name, indio_dev->id); 254 257 if (trig == NULL) { 255 258 dev_err(&indio_dev->dev, "Trigger Allocate Failed\n"); 256 259 ret = -ENOMEM; 257 - goto error_ret; 260 + goto error_triggered_buffer_cleanup; 258 261 } 259 262 260 263 trig->dev.parent = indio_dev->dev.parent; ··· 295 284 iio_trigger_unregister(trig); 296 285 error_free_trig: 297 286 iio_trigger_free(trig); 298 - error_ret: 287 + error_triggered_buffer_cleanup: 288 + iio_triggered_buffer_cleanup(indio_dev); 299 289 return ret; 300 290 } 301 291 EXPORT_SYMBOL(hid_sensor_setup_trigger);
+2 -1
drivers/iio/common/hid-sensors/hid-sensor-trigger.h
··· 13 13 14 14 int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name, 15 15 struct hid_sensor_common *attrb); 16 - void hid_sensor_remove_trigger(struct hid_sensor_common *attrb); 16 + void hid_sensor_remove_trigger(struct iio_dev *indio_dev, 17 + struct hid_sensor_common *attrb); 17 18 int hid_sensor_power_state(struct hid_sensor_common *st, bool state); 18 19 19 20 #endif
+1 -6
drivers/iio/common/st_sensors/st_sensors_core.c
··· 20 20 21 21 #include "st_sensors_core.h" 22 22 23 - static inline u32 st_sensors_get_unaligned_le24(const u8 *p) 24 - { 25 - return (s32)((p[0] | p[1] << 8 | p[2] << 16) << 8) >> 8; 26 - } 27 - 28 23 int st_sensors_write_data_with_mask(struct iio_dev *indio_dev, 29 24 u8 reg_addr, u8 mask, u8 data) 30 25 { ··· 538 543 else if (byte_for_channel == 2) 539 544 *data = (s16)get_unaligned_le16(outdata); 540 545 else if (byte_for_channel == 3) 541 - *data = (s32)st_sensors_get_unaligned_le24(outdata); 546 + *data = (s32)sign_extend32(get_unaligned_le24(outdata), 23); 542 547 543 548 st_sensors_free_memory: 544 549 kfree(outdata);
+3 -3
drivers/iio/dac/Kconfig
··· 279 279 module will be called ltc1660. 280 280 281 281 config LTC2632 282 - tristate "Linear Technology LTC2632-12/10/8 and LTC2636-12/10/8 DAC spi driver" 282 + tristate "Linear Technology LTC2632-12/10/8 and similar DAC spi driver" 283 283 depends on SPI 284 284 help 285 285 Say yes here to build support for Linear Technology 286 - LTC2632-12, LTC2632-10, LTC2632-8, LTC2636-12, LTC2636-10 and 287 - LTC2636-8 converters (DAC). 286 + LTC2632, LTC2634 and LTC2636 DAC resolution 12/10/8 bit 287 + low 0-2.5V and high 0-4.096V range converters. 288 288 289 289 To compile this driver as a module, choose M here: the 290 290 module will be called ltc2632.
+3 -3
drivers/iio/dac/ad5446.c
··· 21 21 #include <linux/iio/iio.h> 22 22 #include <linux/iio/sysfs.h> 23 23 24 + #include <asm/unaligned.h> 25 + 24 26 #define MODE_PWRDWN_1k 0x1 25 27 #define MODE_PWRDWN_100k 0x2 26 28 #define MODE_PWRDWN_TRISTATE 0x3 ··· 304 302 struct spi_device *spi = to_spi_device(st->dev); 305 303 uint8_t data[3]; 306 304 307 - data[0] = (val >> 16) & 0xFF; 308 - data[1] = (val >> 8) & 0xFF; 309 - data[2] = val & 0xFF; 305 + put_unaligned_be24(val, &data[0]); 310 306 311 307 return spi_write(spi, data, sizeof(data)); 312 308 }
+2 -2
drivers/iio/dac/ad5592r.c
··· 98 98 return 0; 99 99 } 100 100 101 - static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value) 101 + static int ad5592r_gpio_read(struct ad5592r_state *st, u8 *value) 102 102 { 103 103 int ret; 104 104 ··· 121 121 .read_adc = ad5592r_read_adc, 122 122 .reg_write = ad5592r_reg_write, 123 123 .reg_read = ad5592r_reg_read, 124 - .gpio_read = ad5593r_gpio_read, 124 + .gpio_read = ad5592r_gpio_read, 125 125 }; 126 126 127 127 static int ad5592r_spi_probe(struct spi_device *spi)
+1 -1
drivers/iio/dac/ad5593r.c
··· 134 134 module_i2c_driver(ad5593r_driver); 135 135 136 136 MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); 137 - MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); 137 + MODULE_DESCRIPTION("Analog Devices AD5593R multi-channel converters"); 138 138 MODULE_LICENSE("GPL v2");
+4 -4
drivers/iio/dac/ad5624r_spi.c
··· 18 18 #include <linux/iio/iio.h> 19 19 #include <linux/iio/sysfs.h> 20 20 21 + #include <asm/unaligned.h> 22 + 21 23 #include "ad5624r.h" 22 24 23 25 static int ad5624r_spi_write(struct spi_device *spi, ··· 37 35 * for the AD5664R, AD5644R, and AD5624R, respectively. 38 36 */ 39 37 data = (0 << 22) | (cmd << 19) | (addr << 16) | (val << shift); 40 - msg[0] = data >> 16; 41 - msg[1] = data >> 8; 42 - msg[2] = data; 38 + put_unaligned_be24(data, &msg[0]); 43 39 44 - return spi_write(spi, msg, 3); 40 + return spi_write(spi, msg, sizeof(msg)); 45 41 } 46 42 47 43 static int ad5624r_read_raw(struct iio_dev *indio_dev,
+64 -3
drivers/iio/dac/ltc2632.c
··· 12 12 #include <linux/iio/iio.h> 13 13 #include <linux/regulator/consumer.h> 14 14 15 + #include <asm/unaligned.h> 16 + 15 17 #define LTC2632_CMD_WRITE_INPUT_N 0x0 16 18 #define LTC2632_CMD_UPDATE_DAC_N 0x1 17 19 #define LTC2632_CMD_WRITE_INPUT_N_UPDATE_ALL 0x2 ··· 26 24 /** 27 25 * struct ltc2632_chip_info - chip specific information 28 26 * @channels: channel spec for the DAC 27 + * @num_channels: DAC channel count of the chip 29 28 * @vref_mv: internal reference voltage 30 29 */ 31 30 struct ltc2632_chip_info { ··· 56 53 ID_LTC2632H12, 57 54 ID_LTC2632H10, 58 55 ID_LTC2632H8, 56 + ID_LTC2634L12, 57 + ID_LTC2634L10, 58 + ID_LTC2634L8, 59 + ID_LTC2634H12, 60 + ID_LTC2634H10, 61 + ID_LTC2634H8, 59 62 ID_LTC2636L12, 60 63 ID_LTC2636L10, 61 64 ID_LTC2636L8, ··· 84 75 * 10-, 8-bit input code followed by 4, 6, or 8 don't care bits. 85 76 */ 86 77 data = (cmd << 20) | (addr << 16) | (val << shift); 87 - msg[0] = data >> 16; 88 - msg[1] = data >> 8; 89 - msg[2] = data; 78 + put_unaligned_be24(data, &msg[0]); 90 79 91 80 return spi_write(spi, msg, sizeof(msg)); 92 81 } ··· 242 235 .num_channels = 2, 243 236 .vref_mv = 4096, 244 237 }, 238 + [ID_LTC2634L12] = { 239 + .channels = ltc2632x12_channels, 240 + .num_channels = 4, 241 + .vref_mv = 2500, 242 + }, 243 + [ID_LTC2634L10] = { 244 + .channels = ltc2632x10_channels, 245 + .num_channels = 4, 246 + .vref_mv = 2500, 247 + }, 248 + [ID_LTC2634L8] = { 249 + .channels = ltc2632x8_channels, 250 + .num_channels = 4, 251 + .vref_mv = 2500, 252 + }, 253 + [ID_LTC2634H12] = { 254 + .channels = ltc2632x12_channels, 255 + .num_channels = 4, 256 + .vref_mv = 4096, 257 + }, 258 + [ID_LTC2634H10] = { 259 + .channels = ltc2632x10_channels, 260 + .num_channels = 4, 261 + .vref_mv = 4096, 262 + }, 263 + [ID_LTC2634H8] = { 264 + .channels = ltc2632x8_channels, 265 + .num_channels = 4, 266 + .vref_mv = 4096, 267 + }, 245 268 [ID_LTC2636L12] = { 246 269 .channels = ltc2632x12_channels, 247 270 .num_channels = 8, ··· 393 356 { "ltc2632-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H12] }, 394 357 { "ltc2632-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H10] }, 395 358 { "ltc2632-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2632H8] }, 359 + { "ltc2634-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L12] }, 360 + { "ltc2634-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L10] }, 361 + { "ltc2634-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634L8] }, 362 + { "ltc2634-h12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H12] }, 363 + { "ltc2634-h10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H10] }, 364 + { "ltc2634-h8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2634H8] }, 396 365 { "ltc2636-l12", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L12] }, 397 366 { "ltc2636-l10", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L10] }, 398 367 { "ltc2636-l8", (kernel_ulong_t)&ltc2632_chip_info_tbl[ID_LTC2636L8] }, ··· 428 385 }, { 429 386 .compatible = "lltc,ltc2632-h8", 430 387 .data = &ltc2632_chip_info_tbl[ID_LTC2632H8] 388 + }, { 389 + .compatible = "lltc,ltc2634-l12", 390 + .data = &ltc2632_chip_info_tbl[ID_LTC2634L12] 391 + }, { 392 + .compatible = "lltc,ltc2634-l10", 393 + .data = &ltc2632_chip_info_tbl[ID_LTC2634L10] 394 + }, { 395 + .compatible = "lltc,ltc2634-l8", 396 + .data = &ltc2632_chip_info_tbl[ID_LTC2634L8] 397 + }, { 398 + .compatible = "lltc,ltc2634-h12", 399 + .data = &ltc2632_chip_info_tbl[ID_LTC2634H12] 400 + }, { 401 + .compatible = "lltc,ltc2634-h10", 402 + .data = &ltc2632_chip_info_tbl[ID_LTC2634H10] 403 + }, { 404 + .compatible = "lltc,ltc2634-h8", 405 + .data = &ltc2632_chip_info_tbl[ID_LTC2634H8] 431 406 }, { 432 407 .compatible = "lltc,ltc2636-l12", 433 408 .data = &ltc2632_chip_info_tbl[ID_LTC2636L12]
+3 -1
drivers/iio/gyro/adis16130.c
··· 12 12 13 13 #include <linux/iio/iio.h> 14 14 15 + #include <asm/unaligned.h> 16 + 15 17 #define ADIS16130_CON 0x0 16 18 #define ADIS16130_CON_RD (1 << 6) 17 19 #define ADIS16130_IOP 0x1 ··· 61 59 62 60 ret = spi_sync_transfer(st->us, &xfer, 1); 63 61 if (ret == 0) 64 - *val = (st->buf[1] << 16) | (st->buf[2] << 8) | st->buf[3]; 62 + *val = get_unaligned_be24(&st->buf[1]); 65 63 mutex_unlock(&st->buf_lock); 66 64 67 65 return ret;
+4 -6
drivers/iio/gyro/adis16136.c
··· 148 148 static int adis16136_debugfs_init(struct iio_dev *indio_dev) 149 149 { 150 150 struct adis16136 *adis16136 = iio_priv(indio_dev); 151 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 151 152 152 153 debugfs_create_file_unsafe("serial_number", 0400, 153 - indio_dev->debugfs_dentry, adis16136, 154 - &adis16136_serial_fops); 154 + d, adis16136, &adis16136_serial_fops); 155 155 debugfs_create_file_unsafe("product_id", 0400, 156 - indio_dev->debugfs_dentry, 157 - adis16136, &adis16136_product_id_fops); 156 + d, adis16136, &adis16136_product_id_fops); 158 157 debugfs_create_file_unsafe("flash_count", 0400, 159 - indio_dev->debugfs_dentry, 160 - adis16136, &adis16136_flash_count_fops); 158 + d, adis16136, &adis16136_flash_count_fops); 161 159 162 160 return 0; 163 161 }
+4 -14
drivers/iio/gyro/hid-sensor-gyro-3d.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 enum gyro_3d_channel { ··· 324 326 indio_dev->name = name; 325 327 indio_dev->modes = INDIO_DIRECT_MODE; 326 328 327 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 328 - NULL, NULL); 329 - if (ret) { 330 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 331 - goto error_free_dev_mem; 332 - } 333 329 atomic_set(&gyro_state->common_attributes.data_ready, 0); 330 + 334 331 ret = hid_sensor_setup_trigger(indio_dev, name, 335 332 &gyro_state->common_attributes); 336 333 if (ret < 0) { 337 334 dev_err(&pdev->dev, "trigger setup failed\n"); 338 - goto error_unreg_buffer_funcs; 335 + goto error_free_dev_mem; 339 336 } 340 337 341 338 ret = iio_device_register(indio_dev); ··· 354 361 error_iio_unreg: 355 362 iio_device_unregister(indio_dev); 356 363 error_remove_trigger: 357 - hid_sensor_remove_trigger(&gyro_state->common_attributes); 358 - error_unreg_buffer_funcs: 359 - iio_triggered_buffer_cleanup(indio_dev); 364 + hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes); 360 365 error_free_dev_mem: 361 366 kfree(indio_dev->channels); 362 367 return ret; ··· 369 378 370 379 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_GYRO_3D); 371 380 iio_device_unregister(indio_dev); 372 - hid_sensor_remove_trigger(&gyro_state->common_attributes); 373 - iio_triggered_buffer_cleanup(indio_dev); 381 + hid_sensor_remove_trigger(indio_dev, &gyro_state->common_attributes); 374 382 kfree(indio_dev->channels); 375 383 376 384 return 0;
+6 -8
drivers/iio/health/afe4403.c
··· 23 23 #include <linux/iio/triggered_buffer.h> 24 24 #include <linux/iio/trigger_consumer.h> 25 25 26 + #include <asm/unaligned.h> 27 + 26 28 #include "afe440x.h" 27 29 28 30 #define AFE4403_DRIVER_NAME "afe4403" ··· 222 220 if (ret) 223 221 return ret; 224 222 225 - ret = spi_write_then_read(afe->spi, &reg, 1, rx, 3); 223 + ret = spi_write_then_read(afe->spi, &reg, 1, rx, sizeof(rx)); 226 224 if (ret) 227 225 return ret; 228 226 229 - *val = (rx[0] << 16) | 230 - (rx[1] << 8) | 231 - (rx[2]); 227 + *val = get_unaligned_be24(&rx[0]); 232 228 233 229 /* Disable reading from the device */ 234 230 tx[3] = AFE440X_CONTROL0_WRITE; ··· 322 322 indio_dev->masklength) { 323 323 ret = spi_write_then_read(afe->spi, 324 324 &afe4403_channel_values[bit], 1, 325 - rx, 3); 325 + rx, sizeof(rx)); 326 326 if (ret) 327 327 goto err; 328 328 329 - buffer[i++] = (rx[0] << 16) | 330 - (rx[1] << 8) | 331 - (rx[2]); 329 + buffer[i++] = get_unaligned_be24(&rx[0]); 332 330 } 333 331 334 332 /* Disable reading from the device */
+3 -9
drivers/iio/humidity/hid-sensor-humidity.c
··· 7 7 #include <linux/hid-sensor-hub.h> 8 8 #include <linux/iio/buffer.h> 9 9 #include <linux/iio/iio.h> 10 - #include <linux/iio/triggered_buffer.h> 11 - #include <linux/iio/trigger_consumer.h> 12 10 #include <linux/module.h> 13 11 #include <linux/platform_device.h> 14 12 ··· 231 233 indio_dev->name = name; 232 234 indio_dev->modes = INDIO_DIRECT_MODE; 233 235 234 - ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev, 235 - &iio_pollfunc_store_time, NULL, NULL); 236 - if (ret) 237 - return ret; 238 - 239 236 atomic_set(&humid_st->common_attributes.data_ready, 0); 237 + 240 238 ret = hid_sensor_setup_trigger(indio_dev, name, 241 239 &humid_st->common_attributes); 242 240 if (ret) ··· 255 261 error_remove_callback: 256 262 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 257 263 error_remove_trigger: 258 - hid_sensor_remove_trigger(&humid_st->common_attributes); 264 + hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes); 259 265 return ret; 260 266 } 261 267 ··· 268 274 269 275 iio_device_unregister(indio_dev); 270 276 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 271 - hid_sensor_remove_trigger(&humid_st->common_attributes); 277 + hid_sensor_remove_trigger(indio_dev, &humid_st->common_attributes); 272 278 273 279 return 0; 274 280 }
+13
drivers/iio/imu/Kconfig
··· 29 29 To compile this driver as a module, choose M here: the module will be 30 30 called adis16460. 31 31 32 + config ADIS16475 33 + tristate "Analog Devices ADIS16475 and similar IMU driver" 34 + depends on SPI 35 + select IIO_ADIS_LIB 36 + select IIO_ADIS_LIB_BUFFER if IIO_BUFFER 37 + help 38 + Say yes here to build support for Analog Devices ADIS16470, ADIS16475, 39 + ADIS16477, ADIS16465, ADIS16467, ADIS16500, ADIS16505, ADIS16507 inertial 40 + sensors. 41 + 42 + To compile this driver as a module, choose M here: the module will be 43 + called adis16475. 44 + 32 45 config ADIS16480 33 46 tristate "Analog Devices ADIS16480 and similar IMU driver" 34 47 depends on SPI
+1
drivers/iio/imu/Makefile
··· 6 6 # When adding new entries keep the list in alphabetical order 7 7 obj-$(CONFIG_ADIS16400) += adis16400.o 8 8 obj-$(CONFIG_ADIS16460) += adis16460.o 9 + obj-$(CONFIG_ADIS16475) += adis16475.o 9 10 obj-$(CONFIG_ADIS16480) += adis16480.o 10 11 11 12 adis_lib-y += adis.o
+25
drivers/iio/imu/adis.c
··· 223 223 return ret; 224 224 } 225 225 EXPORT_SYMBOL_GPL(__adis_read_reg); 226 + /** 227 + * __adis_update_bits_base() - ADIS Update bits function - Unlocked version 228 + * @adis: The adis device 229 + * @reg: The address of the lower of the two registers 230 + * @mask: Bitmask to change 231 + * @val: Value to be written 232 + * @size: Size of the register to update 233 + * 234 + * Updates the desired bits of @reg in accordance with @mask and @val. 235 + */ 236 + int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask, 237 + const u32 val, u8 size) 238 + { 239 + int ret; 240 + u32 __val; 241 + 242 + ret = __adis_read_reg(adis, reg, &__val, size); 243 + if (ret) 244 + return ret; 245 + 246 + __val = (__val & ~mask) | (val & mask); 247 + 248 + return __adis_write_reg(adis, reg, __val, size); 249 + } 250 + EXPORT_SYMBOL_GPL(__adis_update_bits_base); 226 251 227 252 #ifdef CONFIG_DEBUG_FS 228 253
+5 -7
drivers/iio/imu/adis16400.c
··· 281 281 static int adis16400_debugfs_init(struct iio_dev *indio_dev) 282 282 { 283 283 struct adis16400_state *st = iio_priv(indio_dev); 284 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 284 285 285 286 if (st->variant->flags & ADIS16400_HAS_SERIAL_NUMBER) 286 287 debugfs_create_file_unsafe("serial_number", 0400, 287 - indio_dev->debugfs_dentry, st, 288 - &adis16400_serial_number_fops); 288 + d, st, &adis16400_serial_number_fops); 289 289 if (st->variant->flags & ADIS16400_HAS_PROD_ID) 290 290 debugfs_create_file_unsafe("product_id", 0400, 291 - indio_dev->debugfs_dentry, st, 292 - &adis16400_product_id_fops); 291 + d, st, &adis16400_product_id_fops); 293 292 debugfs_create_file_unsafe("flash_count", 0400, 294 - indio_dev->debugfs_dentry, st, 295 - &adis16400_flash_count_fops); 293 + d, st, &adis16400_flash_count_fops); 296 294 297 295 return 0; 298 296 } ··· 1193 1195 indio_dev->available_scan_masks = st->avail_scan_mask; 1194 1196 st->adis.burst = &adis16400_burst; 1195 1197 if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) 1196 - st->adis.burst->extra_len = sizeof(u16); 1198 + st->adis.burst_extra_len = sizeof(u16); 1197 1199 } 1198 1200 1199 1201 adis16400_data = &st->variant->adis_data;
+4 -6
drivers/iio/imu/adis16460.c
··· 129 129 static int adis16460_debugfs_init(struct iio_dev *indio_dev) 130 130 { 131 131 struct adis16460 *adis16460 = iio_priv(indio_dev); 132 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 132 133 133 134 debugfs_create_file_unsafe("serial_number", 0400, 134 - indio_dev->debugfs_dentry, adis16460, 135 - &adis16460_serial_number_fops); 135 + d, adis16460, &adis16460_serial_number_fops); 136 136 debugfs_create_file_unsafe("product_id", 0400, 137 - indio_dev->debugfs_dentry, adis16460, 138 - &adis16460_product_id_fops); 137 + d, adis16460, &adis16460_product_id_fops); 139 138 debugfs_create_file_unsafe("flash_count", 0400, 140 - indio_dev->debugfs_dentry, adis16460, 141 - &adis16460_flash_count_fops); 139 + d, adis16460, &adis16460_flash_count_fops); 142 140 143 141 return 0; 144 142 }
+1338
drivers/iio/imu/adis16475.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * ADIS16475 IMU driver 4 + * 5 + * Copyright 2019 Analog Devices Inc. 6 + */ 7 + #include <linux/bitfield.h> 8 + #include <linux/bitops.h> 9 + #include <linux/clk.h> 10 + #include <linux/debugfs.h> 11 + #include <linux/delay.h> 12 + #include <linux/device.h> 13 + #include <linux/kernel.h> 14 + #include <linux/iio/buffer.h> 15 + #include <linux/iio/iio.h> 16 + #include <linux/iio/imu/adis.h> 17 + #include <linux/iio/sysfs.h> 18 + #include <linux/iio/trigger_consumer.h> 19 + #include <linux/irq.h> 20 + #include <linux/module.h> 21 + #include <linux/mod_devicetable.h> 22 + #include <linux/property.h> 23 + #include <linux/spi/spi.h> 24 + 25 + #define ADIS16475_REG_DIAG_STAT 0x02 26 + #define ADIS16475_REG_X_GYRO_L 0x04 27 + #define ADIS16475_REG_Y_GYRO_L 0x08 28 + #define ADIS16475_REG_Z_GYRO_L 0x0C 29 + #define ADIS16475_REG_X_ACCEL_L 0x10 30 + #define ADIS16475_REG_Y_ACCEL_L 0x14 31 + #define ADIS16475_REG_Z_ACCEL_L 0x18 32 + #define ADIS16475_REG_TEMP_OUT 0x1c 33 + #define ADIS16475_REG_X_GYRO_BIAS_L 0x40 34 + #define ADIS16475_REG_Y_GYRO_BIAS_L 0x44 35 + #define ADIS16475_REG_Z_GYRO_BIAS_L 0x48 36 + #define ADIS16475_REG_X_ACCEL_BIAS_L 0x4c 37 + #define ADIS16475_REG_Y_ACCEL_BIAS_L 0x50 38 + #define ADIS16475_REG_Z_ACCEL_BIAS_L 0x54 39 + #define ADIS16475_REG_FILT_CTRL 0x5c 40 + #define ADIS16475_FILT_CTRL_MASK GENMASK(2, 0) 41 + #define ADIS16475_FILT_CTRL(x) FIELD_PREP(ADIS16475_FILT_CTRL_MASK, x) 42 + #define ADIS16475_REG_MSG_CTRL 0x60 43 + #define ADIS16475_MSG_CTRL_DR_POL_MASK BIT(0) 44 + #define ADIS16475_MSG_CTRL_DR_POL(x) \ 45 + FIELD_PREP(ADIS16475_MSG_CTRL_DR_POL_MASK, x) 46 + #define ADIS16475_SYNC_MODE_MASK GENMASK(4, 2) 47 + #define ADIS16475_SYNC_MODE(x) FIELD_PREP(ADIS16475_SYNC_MODE_MASK, x) 48 + #define ADIS16475_REG_UP_SCALE 0x62 49 + #define ADIS16475_REG_DEC_RATE 0x64 50 + #define ADIS16475_REG_GLOB_CMD 0x68 51 + #define ADIS16475_REG_FIRM_REV 0x6c 52 + #define ADIS16475_REG_FIRM_DM 0x6e 53 + #define ADIS16475_REG_FIRM_Y 0x70 54 + #define ADIS16475_REG_PROD_ID 0x72 55 + #define ADIS16475_REG_SERIAL_NUM 0x74 56 + #define ADIS16475_REG_FLASH_CNT 0x7c 57 + #define ADIS16500_BURST32_MASK BIT(9) 58 + #define ADIS16500_BURST32(x) FIELD_PREP(ADIS16500_BURST32_MASK, x) 59 + /* number of data elements in burst mode */ 60 + #define ADIS16475_BURST32_MAX_DATA 32 61 + #define ADIS16475_BURST_MAX_DATA 20 62 + #define ADIS16475_MAX_SCAN_DATA 20 63 + /* spi max speed in brust mode */ 64 + #define ADIS16475_BURST_MAX_SPEED 1000000 65 + #define ADIS16475_LSB_DEC_MASK BIT(0) 66 + #define ADIS16475_LSB_FIR_MASK BIT(1) 67 + 68 + enum { 69 + ADIS16475_SYNC_DIRECT = 1, 70 + ADIS16475_SYNC_SCALED, 71 + ADIS16475_SYNC_OUTPUT, 72 + ADIS16475_SYNC_PULSE = 5, 73 + }; 74 + 75 + struct adis16475_sync { 76 + u16 sync_mode; 77 + u16 min_rate; 78 + u16 max_rate; 79 + }; 80 + 81 + struct adis16475_chip_info { 82 + const struct iio_chan_spec *channels; 83 + const struct adis16475_sync *sync; 84 + const struct adis_data adis_data; 85 + const char *name; 86 + u32 num_channels; 87 + u32 gyro_max_val; 88 + u32 gyro_max_scale; 89 + u32 accel_max_val; 90 + u32 accel_max_scale; 91 + u32 temp_scale; 92 + u32 int_clk; 93 + u16 max_dec; 94 + u8 num_sync; 95 + bool has_burst32; 96 + }; 97 + 98 + struct adis16475 { 99 + const struct adis16475_chip_info *info; 100 + struct adis adis; 101 + u32 clk_freq; 102 + bool burst32; 103 + unsigned long lsb_flag; 104 + /* Alignment needed for the timestamp */ 105 + __be16 data[ADIS16475_MAX_SCAN_DATA] __aligned(8); 106 + }; 107 + 108 + enum { 109 + ADIS16475_SCAN_GYRO_X, 110 + ADIS16475_SCAN_GYRO_Y, 111 + ADIS16475_SCAN_GYRO_Z, 112 + ADIS16475_SCAN_ACCEL_X, 113 + ADIS16475_SCAN_ACCEL_Y, 114 + ADIS16475_SCAN_ACCEL_Z, 115 + ADIS16475_SCAN_TEMP, 116 + ADIS16475_SCAN_DIAG_S_FLAGS, 117 + ADIS16475_SCAN_CRC_FAILURE, 118 + }; 119 + 120 + #ifdef CONFIG_DEBUG_FS 121 + static ssize_t adis16475_show_firmware_revision(struct file *file, 122 + char __user *userbuf, 123 + size_t count, loff_t *ppos) 124 + { 125 + struct adis16475 *st = file->private_data; 126 + char buf[7]; 127 + size_t len; 128 + u16 rev; 129 + int ret; 130 + 131 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_REV, &rev); 132 + if (ret) 133 + return ret; 134 + 135 + len = scnprintf(buf, sizeof(buf), "%x.%x\n", rev >> 8, rev & 0xff); 136 + 137 + return simple_read_from_buffer(userbuf, count, ppos, buf, len); 138 + } 139 + 140 + static const struct file_operations adis16475_firmware_revision_fops = { 141 + .open = simple_open, 142 + .read = adis16475_show_firmware_revision, 143 + .llseek = default_llseek, 144 + .owner = THIS_MODULE, 145 + }; 146 + 147 + static ssize_t adis16475_show_firmware_date(struct file *file, 148 + char __user *userbuf, 149 + size_t count, loff_t *ppos) 150 + { 151 + struct adis16475 *st = file->private_data; 152 + u16 md, year; 153 + char buf[12]; 154 + size_t len; 155 + int ret; 156 + 157 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_Y, &year); 158 + if (ret) 159 + return ret; 160 + 161 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FIRM_DM, &md); 162 + if (ret) 163 + return ret; 164 + 165 + len = snprintf(buf, sizeof(buf), "%.2x-%.2x-%.4x\n", md >> 8, md & 0xff, 166 + year); 167 + 168 + return simple_read_from_buffer(userbuf, count, ppos, buf, len); 169 + } 170 + 171 + static const struct file_operations adis16475_firmware_date_fops = { 172 + .open = simple_open, 173 + .read = adis16475_show_firmware_date, 174 + .llseek = default_llseek, 175 + .owner = THIS_MODULE, 176 + }; 177 + 178 + static int adis16475_show_serial_number(void *arg, u64 *val) 179 + { 180 + struct adis16475 *st = arg; 181 + u16 serial; 182 + int ret; 183 + 184 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_SERIAL_NUM, &serial); 185 + if (ret) 186 + return ret; 187 + 188 + *val = serial; 189 + 190 + return 0; 191 + } 192 + DEFINE_DEBUGFS_ATTRIBUTE(adis16475_serial_number_fops, 193 + adis16475_show_serial_number, NULL, "0x%.4llx\n"); 194 + 195 + static int adis16475_show_product_id(void *arg, u64 *val) 196 + { 197 + struct adis16475 *st = arg; 198 + u16 prod_id; 199 + int ret; 200 + 201 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_PROD_ID, &prod_id); 202 + if (ret) 203 + return ret; 204 + 205 + *val = prod_id; 206 + 207 + return 0; 208 + } 209 + DEFINE_DEBUGFS_ATTRIBUTE(adis16475_product_id_fops, 210 + adis16475_show_product_id, NULL, "%llu\n"); 211 + 212 + static int adis16475_show_flash_count(void *arg, u64 *val) 213 + { 214 + struct adis16475 *st = arg; 215 + u32 flash_count; 216 + int ret; 217 + 218 + ret = adis_read_reg_32(&st->adis, ADIS16475_REG_FLASH_CNT, 219 + &flash_count); 220 + if (ret) 221 + return ret; 222 + 223 + *val = flash_count; 224 + 225 + return 0; 226 + } 227 + DEFINE_DEBUGFS_ATTRIBUTE(adis16475_flash_count_fops, 228 + adis16475_show_flash_count, NULL, "%lld\n"); 229 + 230 + static void adis16475_debugfs_init(struct iio_dev *indio_dev) 231 + { 232 + struct adis16475 *st = iio_priv(indio_dev); 233 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 234 + 235 + debugfs_create_file_unsafe("serial_number", 0400, 236 + d, st, &adis16475_serial_number_fops); 237 + debugfs_create_file_unsafe("product_id", 0400, 238 + d, st, &adis16475_product_id_fops); 239 + debugfs_create_file_unsafe("flash_count", 0400, 240 + d, st, &adis16475_flash_count_fops); 241 + debugfs_create_file("firmware_revision", 0400, 242 + d, st, &adis16475_firmware_revision_fops); 243 + debugfs_create_file("firmware_date", 0400, d, 244 + st, &adis16475_firmware_date_fops); 245 + } 246 + #else 247 + static void adis16475_debugfs_init(struct iio_dev *indio_dev) 248 + { 249 + } 250 + #endif 251 + 252 + static int adis16475_get_freq(struct adis16475 *st, u32 *freq) 253 + { 254 + int ret; 255 + u16 dec; 256 + 257 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, &dec); 258 + if (ret) 259 + return -EINVAL; 260 + 261 + *freq = DIV_ROUND_CLOSEST(st->clk_freq, dec + 1); 262 + 263 + return 0; 264 + } 265 + 266 + static int adis16475_set_freq(struct adis16475 *st, const u32 freq) 267 + { 268 + u16 dec; 269 + int ret; 270 + 271 + if (!freq) 272 + return -EINVAL; 273 + 274 + dec = DIV_ROUND_CLOSEST(st->clk_freq, freq); 275 + 276 + if (dec) 277 + dec--; 278 + 279 + if (dec > st->info->max_dec) 280 + dec = st->info->max_dec; 281 + 282 + ret = adis_write_reg_16(&st->adis, ADIS16475_REG_DEC_RATE, dec); 283 + if (ret) 284 + return ret; 285 + 286 + /* 287 + * If decimation is used, then gyro and accel data will have meaningful 288 + * bits on the LSB registers. This info is used on the trigger handler. 289 + */ 290 + assign_bit(ADIS16475_LSB_DEC_MASK, &st->lsb_flag, dec); 291 + 292 + return 0; 293 + } 294 + 295 + /* The values are approximated. */ 296 + static const u32 adis16475_3db_freqs[] = { 297 + [0] = 720, /* Filter disabled, full BW (~720Hz) */ 298 + [1] = 360, 299 + [2] = 164, 300 + [3] = 80, 301 + [4] = 40, 302 + [5] = 20, 303 + [6] = 10, 304 + }; 305 + 306 + static int adis16475_get_filter(struct adis16475 *st, u32 *filter) 307 + { 308 + u16 filter_sz; 309 + int ret; 310 + const int mask = ADIS16475_FILT_CTRL_MASK; 311 + 312 + ret = adis_read_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL, &filter_sz); 313 + if (ret) 314 + return ret; 315 + 316 + *filter = adis16475_3db_freqs[filter_sz & mask]; 317 + 318 + return 0; 319 + } 320 + 321 + static int adis16475_set_filter(struct adis16475 *st, const u32 filter) 322 + { 323 + int i = ARRAY_SIZE(adis16475_3db_freqs); 324 + int ret; 325 + 326 + while (--i) { 327 + if (adis16475_3db_freqs[i] >= filter) 328 + break; 329 + } 330 + 331 + ret = adis_write_reg_16(&st->adis, ADIS16475_REG_FILT_CTRL, 332 + ADIS16475_FILT_CTRL(i)); 333 + if (ret) 334 + return ret; 335 + 336 + /* 337 + * If FIR is used, then gyro and accel data will have meaningful 338 + * bits on the LSB registers. This info is used on the trigger handler. 339 + */ 340 + assign_bit(ADIS16475_LSB_FIR_MASK, &st->lsb_flag, i); 341 + 342 + return 0; 343 + } 344 + 345 + static const u32 adis16475_calib_regs[] = { 346 + [ADIS16475_SCAN_GYRO_X] = ADIS16475_REG_X_GYRO_BIAS_L, 347 + [ADIS16475_SCAN_GYRO_Y] = ADIS16475_REG_Y_GYRO_BIAS_L, 348 + [ADIS16475_SCAN_GYRO_Z] = ADIS16475_REG_Z_GYRO_BIAS_L, 349 + [ADIS16475_SCAN_ACCEL_X] = ADIS16475_REG_X_ACCEL_BIAS_L, 350 + [ADIS16475_SCAN_ACCEL_Y] = ADIS16475_REG_Y_ACCEL_BIAS_L, 351 + [ADIS16475_SCAN_ACCEL_Z] = ADIS16475_REG_Z_ACCEL_BIAS_L, 352 + }; 353 + 354 + static int adis16475_read_raw(struct iio_dev *indio_dev, 355 + const struct iio_chan_spec *chan, 356 + int *val, int *val2, long info) 357 + { 358 + struct adis16475 *st = iio_priv(indio_dev); 359 + int ret; 360 + u32 tmp; 361 + 362 + switch (info) { 363 + case IIO_CHAN_INFO_RAW: 364 + return adis_single_conversion(indio_dev, chan, 0, val); 365 + case IIO_CHAN_INFO_SCALE: 366 + switch (chan->type) { 367 + case IIO_ANGL_VEL: 368 + *val = st->info->gyro_max_val; 369 + *val2 = st->info->gyro_max_scale; 370 + return IIO_VAL_FRACTIONAL; 371 + case IIO_ACCEL: 372 + *val = st->info->accel_max_val; 373 + *val2 = st->info->accel_max_scale; 374 + return IIO_VAL_FRACTIONAL; 375 + case IIO_TEMP: 376 + *val = st->info->temp_scale; 377 + return IIO_VAL_INT; 378 + default: 379 + return -EINVAL; 380 + } 381 + case IIO_CHAN_INFO_CALIBBIAS: 382 + ret = adis_read_reg_32(&st->adis, 383 + adis16475_calib_regs[chan->scan_index], 384 + val); 385 + if (ret) 386 + return ret; 387 + 388 + return IIO_VAL_INT; 389 + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 390 + ret = adis16475_get_filter(st, val); 391 + if (ret) 392 + return ret; 393 + 394 + return IIO_VAL_INT; 395 + case IIO_CHAN_INFO_SAMP_FREQ: 396 + ret = adis16475_get_freq(st, &tmp); 397 + if (ret) 398 + return ret; 399 + 400 + *val = tmp / 1000; 401 + *val2 = (tmp % 1000) * 1000; 402 + return IIO_VAL_INT_PLUS_MICRO; 403 + default: 404 + return -EINVAL; 405 + } 406 + } 407 + 408 + static int adis16475_write_raw(struct iio_dev *indio_dev, 409 + const struct iio_chan_spec *chan, 410 + int val, int val2, long info) 411 + { 412 + struct adis16475 *st = iio_priv(indio_dev); 413 + u32 tmp; 414 + 415 + switch (info) { 416 + case IIO_CHAN_INFO_SAMP_FREQ: 417 + tmp = val * 1000 + val2 / 1000; 418 + return adis16475_set_freq(st, tmp); 419 + case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY: 420 + return adis16475_set_filter(st, val); 421 + case IIO_CHAN_INFO_CALIBBIAS: 422 + return adis_write_reg_32(&st->adis, 423 + adis16475_calib_regs[chan->scan_index], 424 + val); 425 + default: 426 + return -EINVAL; 427 + } 428 + } 429 + 430 + #define ADIS16475_MOD_CHAN(_type, _mod, _address, _si, _r_bits, _s_bits) \ 431 + { \ 432 + .type = (_type), \ 433 + .modified = 1, \ 434 + .channel2 = (_mod), \ 435 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 436 + BIT(IIO_CHAN_INFO_CALIBBIAS), \ 437 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 438 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 439 + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 440 + .address = (_address), \ 441 + .scan_index = (_si), \ 442 + .scan_type = { \ 443 + .sign = 's', \ 444 + .realbits = (_r_bits), \ 445 + .storagebits = (_s_bits), \ 446 + .endianness = IIO_BE, \ 447 + }, \ 448 + } 449 + 450 + #define ADIS16475_GYRO_CHANNEL(_mod) \ 451 + ADIS16475_MOD_CHAN(IIO_ANGL_VEL, IIO_MOD_ ## _mod, \ 452 + ADIS16475_REG_ ## _mod ## _GYRO_L, \ 453 + ADIS16475_SCAN_GYRO_ ## _mod, 32, 32) 454 + 455 + #define ADIS16475_ACCEL_CHANNEL(_mod) \ 456 + ADIS16475_MOD_CHAN(IIO_ACCEL, IIO_MOD_ ## _mod, \ 457 + ADIS16475_REG_ ## _mod ## _ACCEL_L, \ 458 + ADIS16475_SCAN_ACCEL_ ## _mod, 32, 32) 459 + 460 + #define ADIS16475_TEMP_CHANNEL() { \ 461 + .type = IIO_TEMP, \ 462 + .indexed = 1, \ 463 + .channel = 0, \ 464 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 465 + BIT(IIO_CHAN_INFO_SCALE), \ 466 + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \ 467 + BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \ 468 + .address = ADIS16475_REG_TEMP_OUT, \ 469 + .scan_index = ADIS16475_SCAN_TEMP, \ 470 + .scan_type = { \ 471 + .sign = 's', \ 472 + .realbits = 16, \ 473 + .storagebits = 16, \ 474 + .endianness = IIO_BE, \ 475 + }, \ 476 + } 477 + 478 + static const struct iio_chan_spec adis16475_channels[] = { 479 + ADIS16475_GYRO_CHANNEL(X), 480 + ADIS16475_GYRO_CHANNEL(Y), 481 + ADIS16475_GYRO_CHANNEL(Z), 482 + ADIS16475_ACCEL_CHANNEL(X), 483 + ADIS16475_ACCEL_CHANNEL(Y), 484 + ADIS16475_ACCEL_CHANNEL(Z), 485 + ADIS16475_TEMP_CHANNEL(), 486 + IIO_CHAN_SOFT_TIMESTAMP(7) 487 + }; 488 + 489 + enum adis16475_variant { 490 + ADIS16470, 491 + ADIS16475_1, 492 + ADIS16475_2, 493 + ADIS16475_3, 494 + ADIS16477_1, 495 + ADIS16477_2, 496 + ADIS16477_3, 497 + ADIS16465_1, 498 + ADIS16465_2, 499 + ADIS16465_3, 500 + ADIS16467_1, 501 + ADIS16467_2, 502 + ADIS16467_3, 503 + ADIS16500, 504 + ADIS16505_1, 505 + ADIS16505_2, 506 + ADIS16505_3, 507 + ADIS16507_1, 508 + ADIS16507_2, 509 + ADIS16507_3, 510 + }; 511 + 512 + enum { 513 + ADIS16475_DIAG_STAT_DATA_PATH = 1, 514 + ADIS16475_DIAG_STAT_FLASH_MEM, 515 + ADIS16475_DIAG_STAT_SPI, 516 + ADIS16475_DIAG_STAT_STANDBY, 517 + ADIS16475_DIAG_STAT_SENSOR, 518 + ADIS16475_DIAG_STAT_MEMORY, 519 + ADIS16475_DIAG_STAT_CLK, 520 + }; 521 + 522 + static const char * const adis16475_status_error_msgs[] = { 523 + [ADIS16475_DIAG_STAT_DATA_PATH] = "Data Path Overrun", 524 + [ADIS16475_DIAG_STAT_FLASH_MEM] = "Flash memory update failure", 525 + [ADIS16475_DIAG_STAT_SPI] = "SPI communication error", 526 + [ADIS16475_DIAG_STAT_STANDBY] = "Standby mode", 527 + [ADIS16475_DIAG_STAT_SENSOR] = "Sensor failure", 528 + [ADIS16475_DIAG_STAT_MEMORY] = "Memory failure", 529 + [ADIS16475_DIAG_STAT_CLK] = "Clock error", 530 + }; 531 + 532 + static int adis16475_enable_irq(struct adis *adis, bool enable) 533 + { 534 + /* 535 + * There is no way to gate the data-ready signal internally inside the 536 + * ADIS16475. We can only control it's polarity... 537 + */ 538 + if (enable) 539 + enable_irq(adis->spi->irq); 540 + else 541 + disable_irq(adis->spi->irq); 542 + 543 + return 0; 544 + } 545 + 546 + #define ADIS16475_DATA(_prod_id, _timeouts) \ 547 + { \ 548 + .msc_ctrl_reg = ADIS16475_REG_MSG_CTRL, \ 549 + .glob_cmd_reg = ADIS16475_REG_GLOB_CMD, \ 550 + .diag_stat_reg = ADIS16475_REG_DIAG_STAT, \ 551 + .prod_id_reg = ADIS16475_REG_PROD_ID, \ 552 + .prod_id = (_prod_id), \ 553 + .self_test_mask = BIT(2), \ 554 + .self_test_reg = ADIS16475_REG_GLOB_CMD, \ 555 + .cs_change_delay = 16, \ 556 + .read_delay = 5, \ 557 + .write_delay = 5, \ 558 + .status_error_msgs = adis16475_status_error_msgs, \ 559 + .status_error_mask = BIT(ADIS16475_DIAG_STAT_DATA_PATH) | \ 560 + BIT(ADIS16475_DIAG_STAT_FLASH_MEM) | \ 561 + BIT(ADIS16475_DIAG_STAT_SPI) | \ 562 + BIT(ADIS16475_DIAG_STAT_STANDBY) | \ 563 + BIT(ADIS16475_DIAG_STAT_SENSOR) | \ 564 + BIT(ADIS16475_DIAG_STAT_MEMORY) | \ 565 + BIT(ADIS16475_DIAG_STAT_CLK), \ 566 + .enable_irq = adis16475_enable_irq, \ 567 + .timeouts = (_timeouts), \ 568 + } 569 + 570 + static const struct adis16475_sync adis16475_sync_mode[] = { 571 + { ADIS16475_SYNC_OUTPUT }, 572 + { ADIS16475_SYNC_DIRECT, 1900, 2100 }, 573 + { ADIS16475_SYNC_SCALED, 1, 128 }, 574 + { ADIS16475_SYNC_PULSE, 1000, 2100 }, 575 + }; 576 + 577 + static const struct adis_timeout adis16475_timeouts = { 578 + .reset_ms = 200, 579 + .sw_reset_ms = 200, 580 + .self_test_ms = 20, 581 + }; 582 + 583 + static const struct adis_timeout adis1650x_timeouts = { 584 + .reset_ms = 260, 585 + .sw_reset_ms = 260, 586 + .self_test_ms = 30, 587 + }; 588 + 589 + static const struct adis16475_chip_info adis16475_chip_info[] = { 590 + [ADIS16470] = { 591 + .name = "adis16470", 592 + .num_channels = ARRAY_SIZE(adis16475_channels), 593 + .channels = adis16475_channels, 594 + .gyro_max_val = 1, 595 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 596 + .accel_max_val = 1, 597 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 598 + .temp_scale = 100, 599 + .int_clk = 2000, 600 + .max_dec = 1999, 601 + .sync = adis16475_sync_mode, 602 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 603 + .adis_data = ADIS16475_DATA(16470, &adis16475_timeouts), 604 + }, 605 + [ADIS16475_1] = { 606 + .name = "adis16475-1", 607 + .num_channels = ARRAY_SIZE(adis16475_channels), 608 + .channels = adis16475_channels, 609 + .gyro_max_val = 1, 610 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 611 + .accel_max_val = 1, 612 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 613 + .temp_scale = 100, 614 + .int_clk = 2000, 615 + .max_dec = 1999, 616 + .sync = adis16475_sync_mode, 617 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 618 + .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts), 619 + }, 620 + [ADIS16475_2] = { 621 + .name = "adis16475-2", 622 + .num_channels = ARRAY_SIZE(adis16475_channels), 623 + .channels = adis16475_channels, 624 + .gyro_max_val = 1, 625 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 626 + .accel_max_val = 1, 627 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 628 + .temp_scale = 100, 629 + .int_clk = 2000, 630 + .max_dec = 1999, 631 + .sync = adis16475_sync_mode, 632 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 633 + .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts), 634 + }, 635 + [ADIS16475_3] = { 636 + .name = "adis16475-3", 637 + .num_channels = ARRAY_SIZE(adis16475_channels), 638 + .channels = adis16475_channels, 639 + .gyro_max_val = 1, 640 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 641 + .accel_max_val = 1, 642 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 643 + .temp_scale = 100, 644 + .int_clk = 2000, 645 + .max_dec = 1999, 646 + .sync = adis16475_sync_mode, 647 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 648 + .adis_data = ADIS16475_DATA(16475, &adis16475_timeouts), 649 + }, 650 + [ADIS16477_1] = { 651 + .name = "adis16477-1", 652 + .num_channels = ARRAY_SIZE(adis16475_channels), 653 + .channels = adis16475_channels, 654 + .gyro_max_val = 1, 655 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 656 + .accel_max_val = 1, 657 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 658 + .temp_scale = 100, 659 + .int_clk = 2000, 660 + .max_dec = 1999, 661 + .sync = adis16475_sync_mode, 662 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 663 + .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts), 664 + }, 665 + [ADIS16477_2] = { 666 + .name = "adis16477-2", 667 + .num_channels = ARRAY_SIZE(adis16475_channels), 668 + .channels = adis16475_channels, 669 + .gyro_max_val = 1, 670 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 671 + .accel_max_val = 1, 672 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 673 + .temp_scale = 100, 674 + .int_clk = 2000, 675 + .max_dec = 1999, 676 + .sync = adis16475_sync_mode, 677 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 678 + .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts), 679 + }, 680 + [ADIS16477_3] = { 681 + .name = "adis16477-3", 682 + .num_channels = ARRAY_SIZE(adis16475_channels), 683 + .channels = adis16475_channels, 684 + .gyro_max_val = 1, 685 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 686 + .accel_max_val = 1, 687 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 688 + .temp_scale = 100, 689 + .int_clk = 2000, 690 + .max_dec = 1999, 691 + .sync = adis16475_sync_mode, 692 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 693 + .adis_data = ADIS16475_DATA(16477, &adis16475_timeouts), 694 + }, 695 + [ADIS16465_1] = { 696 + .name = "adis16465-1", 697 + .num_channels = ARRAY_SIZE(adis16475_channels), 698 + .channels = adis16475_channels, 699 + .gyro_max_val = 1, 700 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 701 + .accel_max_val = 1, 702 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 703 + .temp_scale = 100, 704 + .int_clk = 2000, 705 + .max_dec = 1999, 706 + .sync = adis16475_sync_mode, 707 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 708 + .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts), 709 + }, 710 + [ADIS16465_2] = { 711 + .name = "adis16465-2", 712 + .num_channels = ARRAY_SIZE(adis16475_channels), 713 + .channels = adis16475_channels, 714 + .gyro_max_val = 1, 715 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 716 + .accel_max_val = 1, 717 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 718 + .temp_scale = 100, 719 + .int_clk = 2000, 720 + .max_dec = 1999, 721 + .sync = adis16475_sync_mode, 722 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 723 + .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts), 724 + }, 725 + [ADIS16465_3] = { 726 + .name = "adis16465-3", 727 + .num_channels = ARRAY_SIZE(adis16475_channels), 728 + .channels = adis16475_channels, 729 + .gyro_max_val = 1, 730 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 731 + .accel_max_val = 1, 732 + .accel_max_scale = IIO_M_S_2_TO_G(4000 << 16), 733 + .temp_scale = 100, 734 + .int_clk = 2000, 735 + .max_dec = 1999, 736 + .sync = adis16475_sync_mode, 737 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 738 + .adis_data = ADIS16475_DATA(16465, &adis16475_timeouts), 739 + }, 740 + [ADIS16467_1] = { 741 + .name = "adis16467-1", 742 + .num_channels = ARRAY_SIZE(adis16475_channels), 743 + .channels = adis16475_channels, 744 + .gyro_max_val = 1, 745 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 746 + .accel_max_val = 1, 747 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 748 + .temp_scale = 100, 749 + .int_clk = 2000, 750 + .max_dec = 1999, 751 + .sync = adis16475_sync_mode, 752 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 753 + .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts), 754 + }, 755 + [ADIS16467_2] = { 756 + .name = "adis16467-2", 757 + .num_channels = ARRAY_SIZE(adis16475_channels), 758 + .channels = adis16475_channels, 759 + .gyro_max_val = 1, 760 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 761 + .accel_max_val = 1, 762 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 763 + .temp_scale = 100, 764 + .int_clk = 2000, 765 + .max_dec = 1999, 766 + .sync = adis16475_sync_mode, 767 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 768 + .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts), 769 + }, 770 + [ADIS16467_3] = { 771 + .name = "adis16467-3", 772 + .num_channels = ARRAY_SIZE(adis16475_channels), 773 + .channels = adis16475_channels, 774 + .gyro_max_val = 1, 775 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 776 + .accel_max_val = 1, 777 + .accel_max_scale = IIO_M_S_2_TO_G(800 << 16), 778 + .temp_scale = 100, 779 + .int_clk = 2000, 780 + .max_dec = 1999, 781 + .sync = adis16475_sync_mode, 782 + .num_sync = ARRAY_SIZE(adis16475_sync_mode), 783 + .adis_data = ADIS16475_DATA(16467, &adis16475_timeouts), 784 + }, 785 + [ADIS16500] = { 786 + .name = "adis16500", 787 + .num_channels = ARRAY_SIZE(adis16475_channels), 788 + .channels = adis16475_channels, 789 + .gyro_max_val = 1, 790 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 791 + .accel_max_val = 392, 792 + .accel_max_scale = 32000 << 16, 793 + .temp_scale = 100, 794 + .int_clk = 2000, 795 + .max_dec = 1999, 796 + .sync = adis16475_sync_mode, 797 + /* pulse sync not supported */ 798 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 799 + .has_burst32 = true, 800 + .adis_data = ADIS16475_DATA(16500, &adis1650x_timeouts), 801 + }, 802 + [ADIS16505_1] = { 803 + .name = "adis16505-1", 804 + .num_channels = ARRAY_SIZE(adis16475_channels), 805 + .channels = adis16475_channels, 806 + .gyro_max_val = 1, 807 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 808 + .accel_max_val = 78, 809 + .accel_max_scale = 32000 << 16, 810 + .temp_scale = 100, 811 + .int_clk = 2000, 812 + .max_dec = 1999, 813 + .sync = adis16475_sync_mode, 814 + /* pulse sync not supported */ 815 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 816 + .has_burst32 = true, 817 + .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts), 818 + }, 819 + [ADIS16505_2] = { 820 + .name = "adis16505-2", 821 + .num_channels = ARRAY_SIZE(adis16475_channels), 822 + .channels = adis16475_channels, 823 + .gyro_max_val = 1, 824 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 825 + .accel_max_val = 78, 826 + .accel_max_scale = 32000 << 16, 827 + .temp_scale = 100, 828 + .int_clk = 2000, 829 + .max_dec = 1999, 830 + .sync = adis16475_sync_mode, 831 + /* pulse sync not supported */ 832 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 833 + .has_burst32 = true, 834 + .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts), 835 + }, 836 + [ADIS16505_3] = { 837 + .name = "adis16505-3", 838 + .num_channels = ARRAY_SIZE(adis16475_channels), 839 + .channels = adis16475_channels, 840 + .gyro_max_val = 1, 841 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 842 + .accel_max_val = 78, 843 + .accel_max_scale = 32000 << 16, 844 + .temp_scale = 100, 845 + .int_clk = 2000, 846 + .max_dec = 1999, 847 + .sync = adis16475_sync_mode, 848 + /* pulse sync not supported */ 849 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 850 + .has_burst32 = true, 851 + .adis_data = ADIS16475_DATA(16505, &adis1650x_timeouts), 852 + }, 853 + [ADIS16507_1] = { 854 + .name = "adis16507-1", 855 + .num_channels = ARRAY_SIZE(adis16475_channels), 856 + .channels = adis16475_channels, 857 + .gyro_max_val = 1, 858 + .gyro_max_scale = IIO_RAD_TO_DEGREE(160 << 16), 859 + .accel_max_val = 392, 860 + .accel_max_scale = 32000 << 16, 861 + .temp_scale = 100, 862 + .int_clk = 2000, 863 + .max_dec = 1999, 864 + .sync = adis16475_sync_mode, 865 + /* pulse sync not supported */ 866 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 867 + .has_burst32 = true, 868 + .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts), 869 + }, 870 + [ADIS16507_2] = { 871 + .name = "adis16507-2", 872 + .num_channels = ARRAY_SIZE(adis16475_channels), 873 + .channels = adis16475_channels, 874 + .gyro_max_val = 1, 875 + .gyro_max_scale = IIO_RAD_TO_DEGREE(40 << 16), 876 + .accel_max_val = 392, 877 + .accel_max_scale = 32000 << 16, 878 + .temp_scale = 100, 879 + .int_clk = 2000, 880 + .max_dec = 1999, 881 + .sync = adis16475_sync_mode, 882 + /* pulse sync not supported */ 883 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 884 + .has_burst32 = true, 885 + .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts), 886 + }, 887 + [ADIS16507_3] = { 888 + .name = "adis16507-3", 889 + .num_channels = ARRAY_SIZE(adis16475_channels), 890 + .channels = adis16475_channels, 891 + .gyro_max_val = 1, 892 + .gyro_max_scale = IIO_RAD_TO_DEGREE(10 << 16), 893 + .accel_max_val = 392, 894 + .accel_max_scale = 32000 << 16, 895 + .temp_scale = 100, 896 + .int_clk = 2000, 897 + .max_dec = 1999, 898 + .sync = adis16475_sync_mode, 899 + /* pulse sync not supported */ 900 + .num_sync = ARRAY_SIZE(adis16475_sync_mode) - 1, 901 + .has_burst32 = true, 902 + .adis_data = ADIS16475_DATA(16507, &adis1650x_timeouts), 903 + }, 904 + }; 905 + 906 + static const struct iio_info adis16475_info = { 907 + .read_raw = &adis16475_read_raw, 908 + .write_raw = &adis16475_write_raw, 909 + .update_scan_mode = adis_update_scan_mode, 910 + .debugfs_reg_access = adis_debugfs_reg_access, 911 + }; 912 + 913 + static struct adis_burst adis16475_burst = { 914 + .en = true, 915 + .reg_cmd = ADIS16475_REG_GLOB_CMD, 916 + /* 917 + * adis_update_scan_mode_burst() sets the burst length in respect with 918 + * the number of channels and allocates 16 bits for each. However, 919 + * adis1647x devices also need space for DIAG_STAT, DATA_CNTR or 920 + * TIME_STAMP (depending on the clock mode but for us these bytes are 921 + * don't care...) and CRC. 922 + */ 923 + .extra_len = 3 * sizeof(u16), 924 + .burst_max_len = ADIS16475_BURST32_MAX_DATA, 925 + }; 926 + 927 + static bool adis16475_validate_crc(const u8 *buffer, u16 crc, 928 + const bool burst32) 929 + { 930 + int i; 931 + /* extra 6 elements for low gyro and accel */ 932 + const u16 sz = burst32 ? ADIS16475_BURST32_MAX_DATA : 933 + ADIS16475_BURST_MAX_DATA; 934 + 935 + for (i = 0; i < sz - 2; i++) 936 + crc -= buffer[i]; 937 + 938 + return crc == 0; 939 + } 940 + 941 + static void adis16475_burst32_check(struct adis16475 *st) 942 + { 943 + int ret; 944 + struct adis *adis = &st->adis; 945 + 946 + if (!st->info->has_burst32) 947 + return; 948 + 949 + if (st->lsb_flag && !st->burst32) { 950 + const u16 en = ADIS16500_BURST32(1); 951 + 952 + ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL, 953 + ADIS16500_BURST32_MASK, en); 954 + if (ret) 955 + return; 956 + 957 + st->burst32 = true; 958 + 959 + /* 960 + * In 32-bit mode we need extra 2 bytes for all gyro 961 + * and accel channels. 962 + */ 963 + adis->burst_extra_len = 6 * sizeof(u16); 964 + adis->xfer[1].len += 6 * sizeof(u16); 965 + dev_dbg(&adis->spi->dev, "Enable burst32 mode, xfer:%d", 966 + adis->xfer[1].len); 967 + 968 + } else if (!st->lsb_flag && st->burst32) { 969 + const u16 en = ADIS16500_BURST32(0); 970 + 971 + ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL, 972 + ADIS16500_BURST32_MASK, en); 973 + if (ret) 974 + return; 975 + 976 + st->burst32 = false; 977 + 978 + /* Remove the extra bits */ 979 + adis->burst_extra_len = 0; 980 + adis->xfer[1].len -= 6 * sizeof(u16); 981 + dev_dbg(&adis->spi->dev, "Disable burst32 mode, xfer:%d\n", 982 + adis->xfer[1].len); 983 + } 984 + } 985 + 986 + static irqreturn_t adis16475_trigger_handler(int irq, void *p) 987 + { 988 + struct iio_poll_func *pf = p; 989 + struct iio_dev *indio_dev = pf->indio_dev; 990 + struct adis16475 *st = iio_priv(indio_dev); 991 + struct adis *adis = &st->adis; 992 + int ret, bit, i = 0; 993 + __be16 *buffer; 994 + u16 crc; 995 + bool valid; 996 + /* offset until the first element after gyro and accel */ 997 + const u8 offset = st->burst32 ? 13 : 7; 998 + const u32 cached_spi_speed_hz = adis->spi->max_speed_hz; 999 + 1000 + adis->spi->max_speed_hz = ADIS16475_BURST_MAX_SPEED; 1001 + 1002 + ret = spi_sync(adis->spi, &adis->msg); 1003 + if (ret) 1004 + return ret; 1005 + 1006 + adis->spi->max_speed_hz = cached_spi_speed_hz; 1007 + buffer = adis->buffer; 1008 + 1009 + crc = be16_to_cpu(buffer[offset + 2]); 1010 + valid = adis16475_validate_crc(adis->buffer, crc, st->burst32); 1011 + if (!valid) { 1012 + dev_err(&adis->spi->dev, "Invalid crc\n"); 1013 + goto check_burst32; 1014 + } 1015 + 1016 + for_each_set_bit(bit, indio_dev->active_scan_mask, 1017 + indio_dev->masklength) { 1018 + /* 1019 + * When burst mode is used, system flags is the first data 1020 + * channel in the sequence, but the scan index is 7. 1021 + */ 1022 + switch (bit) { 1023 + case ADIS16475_SCAN_TEMP: 1024 + st->data[i++] = buffer[offset]; 1025 + break; 1026 + case ADIS16475_SCAN_GYRO_X ... ADIS16475_SCAN_ACCEL_Z: 1027 + /* 1028 + * The first 2 bytes on the received data are the 1029 + * DIAG_STAT reg, hence the +1 offset here... 1030 + */ 1031 + if (st->burst32) { 1032 + /* upper 16 */ 1033 + st->data[i++] = buffer[bit * 2 + 2]; 1034 + /* lower 16 */ 1035 + st->data[i++] = buffer[bit * 2 + 1]; 1036 + } else { 1037 + st->data[i++] = buffer[bit + 1]; 1038 + /* 1039 + * Don't bother in doing the manual read if the 1040 + * device supports burst32. burst32 will be 1041 + * enabled in the next call to 1042 + * adis16475_burst32_check()... 1043 + */ 1044 + if (st->lsb_flag && !st->info->has_burst32) { 1045 + u16 val = 0; 1046 + const u32 reg = ADIS16475_REG_X_GYRO_L + 1047 + bit * 4; 1048 + 1049 + adis_read_reg_16(adis, reg, &val); 1050 + st->data[i++] = cpu_to_be16(val); 1051 + } else { 1052 + /* lower not used */ 1053 + st->data[i++] = 0; 1054 + } 1055 + } 1056 + break; 1057 + } 1058 + } 1059 + 1060 + iio_push_to_buffers_with_timestamp(indio_dev, st->data, pf->timestamp); 1061 + check_burst32: 1062 + /* 1063 + * We only check the burst mode at the end of the current capture since 1064 + * it takes a full data ready cycle for the device to update the burst 1065 + * array. 1066 + */ 1067 + adis16475_burst32_check(st); 1068 + iio_trigger_notify_done(indio_dev->trig); 1069 + 1070 + return IRQ_HANDLED; 1071 + } 1072 + 1073 + static void adis16475_disable_clk(void *data) 1074 + { 1075 + clk_disable_unprepare((struct clk *)data); 1076 + } 1077 + 1078 + static int adis16475_config_sync_mode(struct adis16475 *st) 1079 + { 1080 + int ret; 1081 + struct device *dev = &st->adis.spi->dev; 1082 + const struct adis16475_sync *sync; 1083 + u32 sync_mode; 1084 + 1085 + /* default to internal clk */ 1086 + st->clk_freq = st->info->int_clk * 1000; 1087 + 1088 + ret = device_property_read_u32(dev, "adi,sync-mode", &sync_mode); 1089 + if (ret) 1090 + return 0; 1091 + 1092 + if (sync_mode >= st->info->num_sync) { 1093 + dev_err(dev, "Invalid sync mode: %u for %s\n", sync_mode, 1094 + st->info->name); 1095 + return -EINVAL; 1096 + } 1097 + 1098 + sync = &st->info->sync[sync_mode]; 1099 + 1100 + /* All the other modes require external input signal */ 1101 + if (sync->sync_mode != ADIS16475_SYNC_OUTPUT) { 1102 + struct clk *clk = devm_clk_get(dev, NULL); 1103 + 1104 + if (IS_ERR(clk)) 1105 + return PTR_ERR(clk); 1106 + 1107 + ret = clk_prepare_enable(clk); 1108 + if (ret) 1109 + return ret; 1110 + 1111 + ret = devm_add_action_or_reset(dev, adis16475_disable_clk, clk); 1112 + if (ret) 1113 + return ret; 1114 + 1115 + st->clk_freq = clk_get_rate(clk); 1116 + if (st->clk_freq < sync->min_rate || 1117 + st->clk_freq > sync->max_rate) { 1118 + dev_err(dev, 1119 + "Clk rate:%u not in a valid range:[%u %u]\n", 1120 + st->clk_freq, sync->min_rate, sync->max_rate); 1121 + return -EINVAL; 1122 + } 1123 + 1124 + if (sync->sync_mode == ADIS16475_SYNC_SCALED) { 1125 + u16 up_scale; 1126 + u32 scaled_out_freq = 0; 1127 + /* 1128 + * If we are in scaled mode, we must have an up_scale. 1129 + * In scaled mode the allowable input clock range is 1130 + * 1 Hz to 128 Hz, and the allowable output range is 1131 + * 1900 to 2100 Hz. Hence, a scale must be given to 1132 + * get the allowable output. 1133 + */ 1134 + ret = device_property_read_u32(dev, 1135 + "adi,scaled-output-hz", 1136 + &scaled_out_freq); 1137 + if (ret) { 1138 + dev_err(dev, "adi,scaled-output-hz must be given when in scaled sync mode"); 1139 + return -EINVAL; 1140 + } else if (scaled_out_freq < 1900 || 1141 + scaled_out_freq > 2100) { 1142 + dev_err(dev, "Invalid value: %u for adi,scaled-output-hz", 1143 + scaled_out_freq); 1144 + return -EINVAL; 1145 + } 1146 + 1147 + up_scale = DIV_ROUND_CLOSEST(scaled_out_freq, 1148 + st->clk_freq); 1149 + 1150 + ret = __adis_write_reg_16(&st->adis, 1151 + ADIS16475_REG_UP_SCALE, 1152 + up_scale); 1153 + if (ret) 1154 + return ret; 1155 + 1156 + st->clk_freq = scaled_out_freq; 1157 + } 1158 + 1159 + st->clk_freq *= 1000; 1160 + } 1161 + /* 1162 + * Keep in mind that the mask for the clk modes in adis1650* 1163 + * chips is different (1100 instead of 11100). However, we 1164 + * are not configuring BIT(4) in these chips and the default 1165 + * value is 0, so we are fine in doing the below operations. 1166 + * I'm keeping this for simplicity and avoiding extra variables 1167 + * in chip_info. 1168 + */ 1169 + ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL, 1170 + ADIS16475_SYNC_MODE_MASK, sync->sync_mode); 1171 + if (ret) 1172 + return ret; 1173 + 1174 + usleep_range(250, 260); 1175 + 1176 + return 0; 1177 + } 1178 + 1179 + static int adis16475_config_irq_pin(struct adis16475 *st) 1180 + { 1181 + int ret; 1182 + struct irq_data *desc; 1183 + u32 irq_type; 1184 + u16 val = 0; 1185 + u8 polarity; 1186 + struct spi_device *spi = st->adis.spi; 1187 + 1188 + desc = irq_get_irq_data(spi->irq); 1189 + if (!desc) { 1190 + dev_err(&spi->dev, "Could not find IRQ %d\n", spi->irq); 1191 + return -EINVAL; 1192 + } 1193 + /* 1194 + * It is possible to configure the data ready polarity. Furthermore, we 1195 + * need to update the adis struct if we want data ready as active low. 1196 + */ 1197 + irq_type = irqd_get_trigger_type(desc); 1198 + if (irq_type == IRQ_TYPE_EDGE_RISING) { 1199 + polarity = 1; 1200 + st->adis.irq_flag = IRQF_TRIGGER_RISING; 1201 + } else if (irq_type == IRQ_TYPE_EDGE_FALLING) { 1202 + polarity = 0; 1203 + st->adis.irq_flag = IRQF_TRIGGER_FALLING; 1204 + } else { 1205 + dev_err(&spi->dev, "Invalid interrupt type 0x%x specified\n", 1206 + irq_type); 1207 + return -EINVAL; 1208 + } 1209 + 1210 + val = ADIS16475_MSG_CTRL_DR_POL(polarity); 1211 + ret = __adis_update_bits(&st->adis, ADIS16475_REG_MSG_CTRL, 1212 + ADIS16475_MSG_CTRL_DR_POL_MASK, val); 1213 + if (ret) 1214 + return ret; 1215 + /* 1216 + * There is a delay writing to any bits written to the MSC_CTRL 1217 + * register. It should not be bigger than 200us, so 250 should be more 1218 + * than enough! 1219 + */ 1220 + usleep_range(250, 260); 1221 + 1222 + return 0; 1223 + } 1224 + 1225 + static const struct of_device_id adis16475_of_match[] = { 1226 + { .compatible = "adi,adis16470", 1227 + .data = &adis16475_chip_info[ADIS16470] }, 1228 + { .compatible = "adi,adis16475-1", 1229 + .data = &adis16475_chip_info[ADIS16475_1] }, 1230 + { .compatible = "adi,adis16475-2", 1231 + .data = &adis16475_chip_info[ADIS16475_2] }, 1232 + { .compatible = "adi,adis16475-3", 1233 + .data = &adis16475_chip_info[ADIS16475_3] }, 1234 + { .compatible = "adi,adis16477-1", 1235 + .data = &adis16475_chip_info[ADIS16477_1] }, 1236 + { .compatible = "adi,adis16477-2", 1237 + .data = &adis16475_chip_info[ADIS16477_2] }, 1238 + { .compatible = "adi,adis16477-3", 1239 + .data = &adis16475_chip_info[ADIS16477_3] }, 1240 + { .compatible = "adi,adis16465-1", 1241 + .data = &adis16475_chip_info[ADIS16465_1] }, 1242 + { .compatible = "adi,adis16465-2", 1243 + .data = &adis16475_chip_info[ADIS16465_2] }, 1244 + { .compatible = "adi,adis16465-3", 1245 + .data = &adis16475_chip_info[ADIS16465_3] }, 1246 + { .compatible = "adi,adis16467-1", 1247 + .data = &adis16475_chip_info[ADIS16467_1] }, 1248 + { .compatible = "adi,adis16467-2", 1249 + .data = &adis16475_chip_info[ADIS16467_2] }, 1250 + { .compatible = "adi,adis16467-3", 1251 + .data = &adis16475_chip_info[ADIS16467_3] }, 1252 + { .compatible = "adi,adis16500", 1253 + .data = &adis16475_chip_info[ADIS16500] }, 1254 + { .compatible = "adi,adis16505-1", 1255 + .data = &adis16475_chip_info[ADIS16505_1] }, 1256 + { .compatible = "adi,adis16505-2", 1257 + .data = &adis16475_chip_info[ADIS16505_2] }, 1258 + { .compatible = "adi,adis16505-3", 1259 + .data = &adis16475_chip_info[ADIS16505_3] }, 1260 + { .compatible = "adi,adis16507-1", 1261 + .data = &adis16475_chip_info[ADIS16507_1] }, 1262 + { .compatible = "adi,adis16507-2", 1263 + .data = &adis16475_chip_info[ADIS16507_2] }, 1264 + { .compatible = "adi,adis16507-3", 1265 + .data = &adis16475_chip_info[ADIS16507_3] }, 1266 + { }, 1267 + }; 1268 + MODULE_DEVICE_TABLE(of, adis16475_of_match); 1269 + 1270 + static int adis16475_probe(struct spi_device *spi) 1271 + { 1272 + struct iio_dev *indio_dev; 1273 + struct adis16475 *st; 1274 + int ret; 1275 + 1276 + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 1277 + if (!indio_dev) 1278 + return -ENOMEM; 1279 + 1280 + st = iio_priv(indio_dev); 1281 + spi_set_drvdata(spi, indio_dev); 1282 + st->adis.burst = &adis16475_burst; 1283 + 1284 + st->info = device_get_match_data(&spi->dev); 1285 + if (!st->info) 1286 + return -EINVAL; 1287 + 1288 + ret = adis_init(&st->adis, indio_dev, spi, &st->info->adis_data); 1289 + if (ret) 1290 + return ret; 1291 + 1292 + indio_dev->dev.parent = &spi->dev; 1293 + indio_dev->name = st->info->name; 1294 + indio_dev->channels = st->info->channels; 1295 + indio_dev->num_channels = st->info->num_channels; 1296 + indio_dev->info = &adis16475_info; 1297 + indio_dev->modes = INDIO_DIRECT_MODE; 1298 + 1299 + ret = __adis_initial_startup(&st->adis); 1300 + if (ret) 1301 + return ret; 1302 + 1303 + ret = adis16475_config_irq_pin(st); 1304 + if (ret) 1305 + return ret; 1306 + 1307 + ret = adis16475_config_sync_mode(st); 1308 + if (ret) 1309 + return ret; 1310 + 1311 + ret = devm_adis_setup_buffer_and_trigger(&st->adis, indio_dev, 1312 + adis16475_trigger_handler); 1313 + if (ret) 1314 + return ret; 1315 + 1316 + adis16475_enable_irq(&st->adis, false); 1317 + 1318 + ret = devm_iio_device_register(&spi->dev, indio_dev); 1319 + if (ret) 1320 + return ret; 1321 + 1322 + adis16475_debugfs_init(indio_dev); 1323 + 1324 + return 0; 1325 + } 1326 + 1327 + static struct spi_driver adis16475_driver = { 1328 + .driver = { 1329 + .name = "adis16475", 1330 + .of_match_table = adis16475_of_match, 1331 + }, 1332 + .probe = adis16475_probe, 1333 + }; 1334 + module_spi_driver(adis16475_driver); 1335 + 1336 + MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); 1337 + MODULE_DESCRIPTION("Analog Devices ADIS16475 IMU driver"); 1338 + MODULE_LICENSE("GPL");
+6 -10
drivers/iio/imu/adis16480.c
··· 284 284 static int adis16480_debugfs_init(struct iio_dev *indio_dev) 285 285 { 286 286 struct adis16480 *adis16480 = iio_priv(indio_dev); 287 + struct dentry *d = iio_get_debugfs_dentry(indio_dev); 287 288 288 289 debugfs_create_file_unsafe("firmware_revision", 0400, 289 - indio_dev->debugfs_dentry, adis16480, 290 - &adis16480_firmware_revision_fops); 290 + d, adis16480, &adis16480_firmware_revision_fops); 291 291 debugfs_create_file_unsafe("firmware_date", 0400, 292 - indio_dev->debugfs_dentry, adis16480, 293 - &adis16480_firmware_date_fops); 292 + d, adis16480, &adis16480_firmware_date_fops); 294 293 debugfs_create_file_unsafe("serial_number", 0400, 295 - indio_dev->debugfs_dentry, adis16480, 296 - &adis16480_serial_number_fops); 294 + d, adis16480, &adis16480_serial_number_fops); 297 295 debugfs_create_file_unsafe("product_id", 0400, 298 - indio_dev->debugfs_dentry, adis16480, 299 - &adis16480_product_id_fops); 296 + d, adis16480, &adis16480_product_id_fops); 300 297 debugfs_create_file_unsafe("flash_count", 0400, 301 - indio_dev->debugfs_dentry, adis16480, 302 - &adis16480_flash_count_fops); 298 + d, adis16480, &adis16480_flash_count_fops); 303 299 304 300 return 0; 305 301 }
+54 -4
drivers/iio/imu/adis_buffer.c
··· 23 23 const unsigned long *scan_mask) 24 24 { 25 25 struct adis *adis = iio_device_get_drvdata(indio_dev); 26 - unsigned int burst_length; 26 + unsigned int burst_length, burst_max_length; 27 27 u8 *tx; 28 28 29 29 /* All but the timestamp channel */ 30 30 burst_length = (indio_dev->num_channels - 1) * sizeof(u16); 31 - burst_length += adis->burst->extra_len; 31 + burst_length += adis->burst->extra_len + adis->burst_extra_len; 32 + 33 + if (adis->burst->burst_max_len) 34 + burst_max_length = adis->burst->burst_max_len; 35 + else 36 + burst_max_length = burst_length; 32 37 33 38 adis->xfer = kcalloc(2, sizeof(*adis->xfer), GFP_KERNEL); 34 39 if (!adis->xfer) 35 40 return -ENOMEM; 36 41 37 - adis->buffer = kzalloc(burst_length + sizeof(u16), GFP_KERNEL); 42 + adis->buffer = kzalloc(burst_max_length + sizeof(u16), GFP_KERNEL); 38 43 if (!adis->buffer) { 39 44 kfree(adis->xfer); 40 45 adis->xfer = NULL; 41 46 return -ENOMEM; 42 47 } 43 48 44 - tx = adis->buffer + burst_length; 49 + tx = adis->buffer + burst_max_length; 45 50 tx[0] = ADIS_READ_REG(adis->burst->reg_cmd); 46 51 tx[1] = 0; 47 52 ··· 161 156 return IRQ_HANDLED; 162 157 } 163 158 159 + static void adis_buffer_cleanup(void *arg) 160 + { 161 + struct adis *adis = arg; 162 + 163 + kfree(adis->buffer); 164 + kfree(adis->xfer); 165 + } 166 + 164 167 /** 165 168 * adis_setup_buffer_and_trigger() - Sets up buffer and trigger for the adis device 166 169 * @adis: The adis device. ··· 210 197 return ret; 211 198 } 212 199 EXPORT_SYMBOL_GPL(adis_setup_buffer_and_trigger); 200 + 201 + /** 202 + * devm_adis_setup_buffer_and_trigger() - Sets up buffer and trigger for 203 + * the managed adis device 204 + * @adis: The adis device 205 + * @indio_dev: The IIO device 206 + * @trigger_handler: Optional trigger handler, may be NULL. 207 + * 208 + * Returns 0 on success, a negative error code otherwise. 209 + * 210 + * This function perfoms exactly the same as adis_setup_buffer_and_trigger() 211 + */ 212 + int 213 + devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev, 214 + irq_handler_t trigger_handler) 215 + { 216 + int ret; 217 + 218 + if (!trigger_handler) 219 + trigger_handler = adis_trigger_handler; 220 + 221 + ret = devm_iio_triggered_buffer_setup(&adis->spi->dev, indio_dev, 222 + &iio_pollfunc_store_time, 223 + trigger_handler, NULL); 224 + if (ret) 225 + return ret; 226 + 227 + if (adis->spi->irq) { 228 + ret = devm_adis_probe_trigger(adis, indio_dev); 229 + if (ret) 230 + return ret; 231 + } 232 + 233 + return devm_add_action_or_reset(&adis->spi->dev, adis_buffer_cleanup, 234 + adis); 235 + } 236 + EXPORT_SYMBOL_GPL(devm_adis_setup_buffer_and_trigger); 213 237 214 238 /** 215 239 * adis_cleanup_buffer_and_trigger() - Free buffer and trigger resources
+68 -4
drivers/iio/imu/adis_trigger.c
··· 27 27 .set_trigger_state = &adis_data_rdy_trigger_set_state, 28 28 }; 29 29 30 + static void adis_trigger_setup(struct adis *adis) 31 + { 32 + adis->trig->dev.parent = &adis->spi->dev; 33 + adis->trig->ops = &adis_trigger_ops; 34 + iio_trigger_set_drvdata(adis->trig, adis); 35 + } 36 + 37 + static int adis_validate_irq_flag(struct adis *adis) 38 + { 39 + /* 40 + * Typically this devices have data ready either on the rising edge or 41 + * on the falling edge of the data ready pin. This checks enforces that 42 + * one of those is set in the drivers... It defaults to 43 + * IRQF_TRIGGER_RISING for backward compatibility wiht devices that 44 + * don't support changing the pin polarity. 45 + */ 46 + if (!adis->irq_flag) { 47 + adis->irq_flag = IRQF_TRIGGER_RISING; 48 + return 0; 49 + } else if (adis->irq_flag != IRQF_TRIGGER_RISING && 50 + adis->irq_flag != IRQF_TRIGGER_FALLING) { 51 + dev_err(&adis->spi->dev, "Invalid IRQ mask: %08lx\n", 52 + adis->irq_flag); 53 + return -EINVAL; 54 + } 55 + 56 + return 0; 57 + } 30 58 /** 31 59 * adis_probe_trigger() - Sets up trigger for a adis device 32 60 * @adis: The adis device ··· 73 45 if (adis->trig == NULL) 74 46 return -ENOMEM; 75 47 76 - adis->trig->dev.parent = &adis->spi->dev; 77 - adis->trig->ops = &adis_trigger_ops; 78 - iio_trigger_set_drvdata(adis->trig, adis); 48 + adis_trigger_setup(adis); 49 + 50 + ret = adis_validate_irq_flag(adis); 51 + if (ret) 52 + return ret; 79 53 80 54 ret = request_irq(adis->spi->irq, 81 55 &iio_trigger_generic_data_rdy_poll, 82 - IRQF_TRIGGER_RISING, 56 + adis->irq_flag, 83 57 indio_dev->name, 84 58 adis->trig); 85 59 if (ret) ··· 102 72 return ret; 103 73 } 104 74 EXPORT_SYMBOL_GPL(adis_probe_trigger); 75 + 76 + /** 77 + * devm_adis_probe_trigger() - Sets up trigger for a managed adis device 78 + * @adis: The adis device 79 + * @indio_dev: The IIO device 80 + * 81 + * Returns 0 on success or a negative error code 82 + */ 83 + int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev) 84 + { 85 + int ret; 86 + 87 + adis->trig = devm_iio_trigger_alloc(&adis->spi->dev, "%s-dev%d", 88 + indio_dev->name, indio_dev->id); 89 + if (!adis->trig) 90 + return -ENOMEM; 91 + 92 + adis_trigger_setup(adis); 93 + 94 + ret = adis_validate_irq_flag(adis); 95 + if (ret) 96 + return ret; 97 + 98 + ret = devm_request_irq(&adis->spi->dev, adis->spi->irq, 99 + &iio_trigger_generic_data_rdy_poll, 100 + adis->irq_flag, 101 + indio_dev->name, 102 + adis->trig); 103 + if (ret) 104 + return ret; 105 + 106 + return devm_iio_trigger_register(&adis->spi->dev, adis->trig); 107 + } 108 + EXPORT_SYMBOL_GPL(devm_adis_probe_trigger); 105 109 106 110 /** 107 111 * adis_remove_trigger() - Remove trigger for a adis devices
+14 -7
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
··· 27 27 * - FIFO size: 4KB 28 28 * 29 29 * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR/ISM330DHCX: 30 - * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416 30 + * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416, 31 + * 833 31 32 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16 32 33 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000 33 34 * - FIFO size: 3KB ··· 792 791 .odr_avl[3] = { 104000, 0x04 }, 793 792 .odr_avl[4] = { 208000, 0x05 }, 794 793 .odr_avl[5] = { 416000, 0x06 }, 795 - .odr_len = 6, 794 + .odr_avl[6] = { 833000, 0x07 }, 795 + .odr_len = 7, 796 796 }, 797 797 [ST_LSM6DSX_ID_GYRO] = { 798 798 .reg = { ··· 806 804 .odr_avl[3] = { 104000, 0x04 }, 807 805 .odr_avl[4] = { 208000, 0x05 }, 808 806 .odr_avl[5] = { 416000, 0x06 }, 809 - .odr_len = 6, 807 + .odr_avl[6] = { 833000, 0x07 }, 808 + .odr_len = 7, 810 809 }, 811 810 }, 812 811 .fs_table = { ··· 997 994 .odr_avl[3] = { 104000, 0x04 }, 998 995 .odr_avl[4] = { 208000, 0x05 }, 999 996 .odr_avl[5] = { 416000, 0x06 }, 1000 - .odr_len = 6, 997 + .odr_avl[6] = { 833000, 0x07 }, 998 + .odr_len = 7, 1001 999 }, 1002 1000 [ST_LSM6DSX_ID_GYRO] = { 1003 1001 .reg = { ··· 1011 1007 .odr_avl[3] = { 104000, 0x04 }, 1012 1008 .odr_avl[4] = { 208000, 0x05 }, 1013 1009 .odr_avl[5] = { 416000, 0x06 }, 1014 - .odr_len = 6, 1010 + .odr_avl[6] = { 833000, 0x07 }, 1011 + .odr_len = 7, 1015 1012 }, 1016 1013 }, 1017 1014 .fs_table = { ··· 1176 1171 .odr_avl[3] = { 104000, 0x04 }, 1177 1172 .odr_avl[4] = { 208000, 0x05 }, 1178 1173 .odr_avl[5] = { 416000, 0x06 }, 1179 - .odr_len = 6, 1174 + .odr_avl[6] = { 833000, 0x07 }, 1175 + .odr_len = 7, 1180 1176 }, 1181 1177 [ST_LSM6DSX_ID_GYRO] = { 1182 1178 .reg = { ··· 1190 1184 .odr_avl[3] = { 104000, 0x04 }, 1191 1185 .odr_avl[4] = { 208000, 0x05 }, 1192 1186 .odr_avl[5] = { 416000, 0x06 }, 1193 - .odr_len = 6, 1187 + .odr_avl[6] = { 833000, 0x07 }, 1188 + .odr_len = 7, 1194 1189 }, 1195 1190 }, 1196 1191 .fs_table = {
+34 -27
drivers/iio/industrialio-buffer.c
··· 189 189 */ 190 190 void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) 191 191 { 192 - if (!indio_dev->buffer) 192 + struct iio_buffer *buffer = indio_dev->buffer; 193 + 194 + if (!buffer) 193 195 return; 194 196 195 - wake_up(&indio_dev->buffer->pollq); 197 + wake_up(&buffer->pollq); 196 198 } 197 199 198 200 void iio_buffer_init(struct iio_buffer *buffer) ··· 264 262 { 265 263 int ret; 266 264 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 265 + struct iio_buffer *buffer = indio_dev->buffer; 267 266 268 267 /* Ensure ret is 0 or 1. */ 269 268 ret = !!test_bit(to_iio_dev_attr(attr)->address, 270 - indio_dev->buffer->scan_mask); 269 + buffer->scan_mask); 271 270 272 271 return sprintf(buf, "%d\n", ret); 273 272 } ··· 384 381 if (ret < 0) 385 382 return ret; 386 383 mutex_lock(&indio_dev->mlock); 387 - if (iio_buffer_is_active(indio_dev->buffer)) { 384 + if (iio_buffer_is_active(buffer)) { 388 385 ret = -EBUSY; 389 386 goto error_ret; 390 387 } ··· 413 410 char *buf) 414 411 { 415 412 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 416 - return sprintf(buf, "%d\n", indio_dev->buffer->scan_timestamp); 413 + struct iio_buffer *buffer = indio_dev->buffer; 414 + 415 + return sprintf(buf, "%d\n", buffer->scan_timestamp); 417 416 } 418 417 419 418 static ssize_t iio_scan_el_ts_store(struct device *dev, ··· 425 420 { 426 421 int ret; 427 422 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 423 + struct iio_buffer *buffer = indio_dev->buffer; 428 424 bool state; 429 425 430 426 ret = strtobool(buf, &state); ··· 433 427 return ret; 434 428 435 429 mutex_lock(&indio_dev->mlock); 436 - if (iio_buffer_is_active(indio_dev->buffer)) { 430 + if (iio_buffer_is_active(buffer)) { 437 431 ret = -EBUSY; 438 432 goto error_ret; 439 433 } 440 - indio_dev->buffer->scan_timestamp = state; 434 + buffer->scan_timestamp = state; 441 435 error_ret: 442 436 mutex_unlock(&indio_dev->mlock); 443 437 ··· 445 439 } 446 440 447 441 static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev, 442 + struct iio_buffer *buffer, 448 443 const struct iio_chan_spec *chan) 449 444 { 450 445 int ret, attrcount = 0; 451 - struct iio_buffer *buffer = indio_dev->buffer; 452 446 453 447 ret = __iio_add_chan_devattr("index", 454 448 chan, ··· 524 518 return len; 525 519 526 520 mutex_lock(&indio_dev->mlock); 527 - if (iio_buffer_is_active(indio_dev->buffer)) { 521 + if (iio_buffer_is_active(buffer)) { 528 522 ret = -EBUSY; 529 523 } else { 530 524 buffer->access->set_length(buffer, val); ··· 545 539 char *buf) 546 540 { 547 541 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 548 - return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 542 + struct iio_buffer *buffer = indio_dev->buffer; 543 + 544 + return sprintf(buf, "%d\n", iio_buffer_is_active(buffer)); 549 545 } 550 546 551 547 static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, ··· 1137 1129 int ret; 1138 1130 bool requested_state; 1139 1131 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1132 + struct iio_buffer *buffer = indio_dev->buffer; 1140 1133 bool inlist; 1141 1134 1142 1135 ret = strtobool(buf, &requested_state); ··· 1147 1138 mutex_lock(&indio_dev->mlock); 1148 1139 1149 1140 /* Find out if it is in the list */ 1150 - inlist = iio_buffer_is_active(indio_dev->buffer); 1141 + inlist = iio_buffer_is_active(buffer); 1151 1142 /* Already in desired state */ 1152 1143 if (inlist == requested_state) 1153 1144 goto done; 1154 1145 1155 1146 if (requested_state) 1156 - ret = __iio_update_buffers(indio_dev, 1157 - indio_dev->buffer, NULL); 1147 + ret = __iio_update_buffers(indio_dev, buffer, NULL); 1158 1148 else 1159 - ret = __iio_update_buffers(indio_dev, 1160 - NULL, indio_dev->buffer); 1149 + ret = __iio_update_buffers(indio_dev, NULL, buffer); 1161 1150 1162 1151 done: 1163 1152 mutex_unlock(&indio_dev->mlock); ··· 1197 1190 goto out; 1198 1191 } 1199 1192 1200 - if (iio_buffer_is_active(indio_dev->buffer)) { 1193 + if (iio_buffer_is_active(buffer)) { 1201 1194 ret = -EBUSY; 1202 1195 goto out; 1203 1196 } ··· 1214 1207 char *buf) 1215 1208 { 1216 1209 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 1217 - size_t bytes; 1210 + struct iio_buffer *buffer = indio_dev->buffer; 1218 1211 1219 - bytes = iio_buffer_data_available(indio_dev->buffer); 1220 - 1221 - return sprintf(buf, "%zu\n", bytes); 1212 + return sprintf(buf, "%zu\n", iio_buffer_data_available(buffer)); 1222 1213 } 1223 1214 1224 1215 static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length, ··· 1297 1292 if (channels[i].scan_index < 0) 1298 1293 continue; 1299 1294 1300 - ret = iio_buffer_add_channel_sysfs(indio_dev, 1295 + ret = iio_buffer_add_channel_sysfs(indio_dev, buffer, 1301 1296 &channels[i]); 1302 1297 if (ret < 0) 1303 1298 goto error_cleanup_dynamic; ··· 1337 1332 bitmap_free(buffer->scan_mask); 1338 1333 error_cleanup_dynamic: 1339 1334 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 1340 - kfree(indio_dev->buffer->buffer_group.attrs); 1335 + kfree(buffer->buffer_group.attrs); 1341 1336 1342 1337 return ret; 1343 1338 } 1344 1339 1345 1340 void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) 1346 1341 { 1347 - if (!indio_dev->buffer) 1342 + struct iio_buffer *buffer = indio_dev->buffer; 1343 + 1344 + if (!buffer) 1348 1345 return; 1349 1346 1350 - bitmap_free(indio_dev->buffer->scan_mask); 1351 - kfree(indio_dev->buffer->buffer_group.attrs); 1352 - kfree(indio_dev->buffer->scan_el_group.attrs); 1353 - iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list); 1347 + bitmap_free(buffer->scan_mask); 1348 + kfree(buffer->buffer_group.attrs); 1349 + kfree(buffer->scan_el_group.attrs); 1350 + iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list); 1354 1351 } 1355 1352 1356 1353 /**
+18 -18
drivers/iio/industrialio-core.c
··· 1507 1507 alloc_size += IIO_ALIGN - 1; 1508 1508 1509 1509 dev = kzalloc(alloc_size, GFP_KERNEL); 1510 + if (!dev) 1511 + return NULL; 1510 1512 1511 - if (dev) { 1512 - dev->dev.groups = dev->groups; 1513 - dev->dev.type = &iio_device_type; 1514 - dev->dev.bus = &iio_bus_type; 1515 - device_initialize(&dev->dev); 1516 - dev_set_drvdata(&dev->dev, (void *)dev); 1517 - mutex_init(&dev->mlock); 1518 - mutex_init(&dev->info_exist_lock); 1519 - INIT_LIST_HEAD(&dev->channel_attr_list); 1513 + dev->dev.groups = dev->groups; 1514 + dev->dev.type = &iio_device_type; 1515 + dev->dev.bus = &iio_bus_type; 1516 + device_initialize(&dev->dev); 1517 + dev_set_drvdata(&dev->dev, (void *)dev); 1518 + mutex_init(&dev->mlock); 1519 + mutex_init(&dev->info_exist_lock); 1520 + INIT_LIST_HEAD(&dev->channel_attr_list); 1520 1521 1521 - dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1522 - if (dev->id < 0) { 1523 - /* cannot use a dev_err as the name isn't available */ 1524 - pr_err("failed to get device id\n"); 1525 - kfree(dev); 1526 - return NULL; 1527 - } 1528 - dev_set_name(&dev->dev, "iio:device%d", dev->id); 1529 - INIT_LIST_HEAD(&dev->buffer_list); 1522 + dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL); 1523 + if (dev->id < 0) { 1524 + /* cannot use a dev_err as the name isn't available */ 1525 + pr_err("failed to get device id\n"); 1526 + kfree(dev); 1527 + return NULL; 1530 1528 } 1529 + dev_set_name(&dev->dev, "iio:device%d", dev->id); 1530 + INIT_LIST_HEAD(&dev->buffer_list); 1531 1531 1532 1532 return dev; 1533 1533 }
+2
drivers/iio/light/Kconfig
··· 516 516 517 517 config VCNL4000 518 518 tristate "VCNL4000/4010/4020/4200 combined ALS and proximity sensor" 519 + select IIO_BUFFER 520 + select IIO_TRIGGERED_BUFFER 519 521 depends on I2C 520 522 help 521 523 Say Y here if you want to build a driver for the Vishay VCNL4000,
+2 -4
drivers/iio/light/bh1780.c
··· 13 13 #include <linux/platform_device.h> 14 14 #include <linux/delay.h> 15 15 #include <linux/module.h> 16 - #include <linux/of.h> 16 + #include <linux/mod_devicetable.h> 17 17 #include <linux/pm_runtime.h> 18 18 #include <linux/iio/iio.h> 19 19 #include <linux/iio/sysfs.h> ··· 273 273 274 274 MODULE_DEVICE_TABLE(i2c, bh1780_id); 275 275 276 - #ifdef CONFIG_OF 277 276 static const struct of_device_id of_bh1780_match[] = { 278 277 { .compatible = "rohm,bh1780gli", }, 279 278 {}, 280 279 }; 281 280 MODULE_DEVICE_TABLE(of, of_bh1780_match); 282 - #endif 283 281 284 282 static struct i2c_driver bh1780_driver = { 285 283 .probe = bh1780_probe, ··· 286 288 .driver = { 287 289 .name = "bh1780", 288 290 .pm = &bh1780_dev_pm_ops, 289 - .of_match_table = of_match_ptr(of_bh1780_match), 291 + .of_match_table = of_bh1780_match, 290 292 }, 291 293 }; 292 294
+211 -60
drivers/iio/light/cm32181.c
··· 4 4 * Author: Kevin Tsai <ktsai@capellamicro.com> 5 5 */ 6 6 7 + #include <linux/acpi.h> 7 8 #include <linux/delay.h> 8 9 #include <linux/err.h> 9 10 #include <linux/i2c.h> 10 11 #include <linux/mutex.h> 11 12 #include <linux/module.h> 13 + #include <linux/mod_devicetable.h> 12 14 #include <linux/interrupt.h> 13 15 #include <linux/regulator/consumer.h> 14 16 #include <linux/iio/iio.h> ··· 20 18 21 19 /* Registers Address */ 22 20 #define CM32181_REG_ADDR_CMD 0x00 21 + #define CM32181_REG_ADDR_WH 0x01 22 + #define CM32181_REG_ADDR_WL 0x02 23 + #define CM32181_REG_ADDR_TEST 0x03 23 24 #define CM32181_REG_ADDR_ALS 0x04 24 25 #define CM32181_REG_ADDR_STATUS 0x06 25 26 #define CM32181_REG_ADDR_ID 0x07 26 27 27 28 /* Number of Configurable Registers */ 28 - #define CM32181_CONF_REG_NUM 0x01 29 + #define CM32181_CONF_REG_NUM 4 29 30 30 31 /* CMD register */ 31 - #define CM32181_CMD_ALS_ENABLE 0x00 32 - #define CM32181_CMD_ALS_DISABLE 0x01 33 - #define CM32181_CMD_ALS_INT_EN 0x02 32 + #define CM32181_CMD_ALS_DISABLE BIT(0) 33 + #define CM32181_CMD_ALS_INT_EN BIT(1) 34 + #define CM32181_CMD_ALS_THRES_WINDOW BIT(2) 35 + 36 + #define CM32181_CMD_ALS_PERS_SHIFT 4 37 + #define CM32181_CMD_ALS_PERS_MASK (0x03 << CM32181_CMD_ALS_PERS_SHIFT) 38 + #define CM32181_CMD_ALS_PERS_DEFAULT (0x01 << CM32181_CMD_ALS_PERS_SHIFT) 34 39 35 40 #define CM32181_CMD_ALS_IT_SHIFT 6 36 41 #define CM32181_CMD_ALS_IT_MASK (0x0F << CM32181_CMD_ALS_IT_SHIFT) ··· 47 38 #define CM32181_CMD_ALS_SM_MASK (0x03 << CM32181_CMD_ALS_SM_SHIFT) 48 39 #define CM32181_CMD_ALS_SM_DEFAULT (0x01 << CM32181_CMD_ALS_SM_SHIFT) 49 40 50 - #define CM32181_MLUX_PER_BIT 5 /* ALS_SM=01 IT=800ms */ 51 - #define CM32181_MLUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ 52 - #define CM32181_CALIBSCALE_DEFAULT 1000 53 - #define CM32181_CALIBSCALE_RESOLUTION 1000 54 - #define MLUX_PER_LUX 1000 41 + #define CM32181_LUX_PER_BIT 500 /* ALS_SM=01 IT=800ms */ 42 + #define CM32181_LUX_PER_BIT_RESOLUTION 100000 43 + #define CM32181_LUX_PER_BIT_BASE_IT 800000 /* Based on IT=800ms */ 44 + #define CM32181_CALIBSCALE_DEFAULT 100000 45 + #define CM32181_CALIBSCALE_RESOLUTION 100000 55 46 56 - static const u8 cm32181_reg[CM32181_CONF_REG_NUM] = { 57 - CM32181_REG_ADDR_CMD, 47 + #define SMBUS_ALERT_RESPONSE_ADDRESS 0x0c 48 + 49 + /* CPM0 Index 0: device-id (3218 or 32181), 1: Unknown, 2: init_regs_bitmap */ 50 + #define CPM0_REGS_BITMAP 2 51 + #define CPM0_HEADER_SIZE 3 52 + 53 + /* CPM1 Index 0: lux_per_bit, 1: calibscale, 2: resolution (100000) */ 54 + #define CPM1_LUX_PER_BIT 0 55 + #define CPM1_CALIBSCALE 1 56 + #define CPM1_SIZE 3 57 + 58 + /* CM3218 Family */ 59 + static const int cm3218_als_it_bits[] = { 0, 1, 2, 3 }; 60 + static const int cm3218_als_it_values[] = { 100000, 200000, 400000, 800000 }; 61 + 62 + /* CM32181 Family */ 63 + static const int cm32181_als_it_bits[] = { 12, 8, 0, 1, 2, 3 }; 64 + static const int cm32181_als_it_values[] = { 65 + 25000, 50000, 100000, 200000, 400000, 800000 58 66 }; 59 - 60 - static const int als_it_bits[] = {12, 8, 0, 1, 2, 3}; 61 - static const int als_it_value[] = {25000, 50000, 100000, 200000, 400000, 62 - 800000}; 63 67 64 68 struct cm32181_chip { 65 69 struct i2c_client *client; 70 + struct device *dev; 66 71 struct mutex lock; 67 72 u16 conf_regs[CM32181_CONF_REG_NUM]; 73 + unsigned long init_regs_bitmap; 68 74 int calibscale; 75 + int lux_per_bit; 76 + int lux_per_bit_base_it; 77 + int num_als_it; 78 + const int *als_it_bits; 79 + const int *als_it_values; 69 80 }; 81 + 82 + static int cm32181_read_als_it(struct cm32181_chip *cm32181, int *val2); 83 + 84 + #ifdef CONFIG_ACPI 85 + /** 86 + * cm32181_acpi_get_cpm() - Get CPM object from ACPI 87 + * @client pointer of struct i2c_client. 88 + * @obj_name pointer of ACPI object name. 89 + * @count maximum size of return array. 90 + * @vals pointer of array for return elements. 91 + * 92 + * Convert ACPI CPM table to array. 93 + * 94 + * Return: -ENODEV for fail. Otherwise is number of elements. 95 + */ 96 + static int cm32181_acpi_get_cpm(struct device *dev, char *obj_name, 97 + u64 *values, int count) 98 + { 99 + struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 100 + union acpi_object *cpm, *elem; 101 + acpi_handle handle; 102 + acpi_status status; 103 + int i; 104 + 105 + handle = ACPI_HANDLE(dev); 106 + if (!handle) 107 + return -ENODEV; 108 + 109 + status = acpi_evaluate_object(handle, obj_name, NULL, &buffer); 110 + if (ACPI_FAILURE(status)) { 111 + dev_err(dev, "object %s not found\n", obj_name); 112 + return -ENODEV; 113 + } 114 + 115 + cpm = buffer.pointer; 116 + if (cpm->package.count > count) 117 + dev_warn(dev, "%s table contains %u values, only using first %d values\n", 118 + obj_name, cpm->package.count, count); 119 + 120 + count = min_t(int, cpm->package.count, count); 121 + for (i = 0; i < count; i++) { 122 + elem = &(cpm->package.elements[i]); 123 + values[i] = elem->integer.value; 124 + } 125 + 126 + kfree(buffer.pointer); 127 + 128 + return count; 129 + } 130 + 131 + static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181) 132 + { 133 + u64 vals[CPM0_HEADER_SIZE + CM32181_CONF_REG_NUM]; 134 + struct device *dev = cm32181->dev; 135 + int i, count; 136 + 137 + count = cm32181_acpi_get_cpm(dev, "CPM0", vals, ARRAY_SIZE(vals)); 138 + if (count <= CPM0_HEADER_SIZE) 139 + return; 140 + 141 + count -= CPM0_HEADER_SIZE; 142 + 143 + cm32181->init_regs_bitmap = vals[CPM0_REGS_BITMAP]; 144 + cm32181->init_regs_bitmap &= GENMASK(count - 1, 0); 145 + for_each_set_bit(i, &cm32181->init_regs_bitmap, count) 146 + cm32181->conf_regs[i] = vals[CPM0_HEADER_SIZE + i]; 147 + 148 + count = cm32181_acpi_get_cpm(dev, "CPM1", vals, ARRAY_SIZE(vals)); 149 + if (count != CPM1_SIZE) 150 + return; 151 + 152 + cm32181->lux_per_bit = vals[CPM1_LUX_PER_BIT]; 153 + 154 + /* Check for uncalibrated devices */ 155 + if (vals[CPM1_CALIBSCALE] == CM32181_CALIBSCALE_DEFAULT) 156 + return; 157 + 158 + cm32181->calibscale = vals[CPM1_CALIBSCALE]; 159 + /* CPM1 lux_per_bit is for the current it value */ 160 + cm32181_read_als_it(cm32181, &cm32181->lux_per_bit_base_it); 161 + } 162 + #else 163 + static void cm32181_acpi_parse_cpm_tables(struct cm32181_chip *cm32181) 164 + { 165 + } 166 + #endif /* CONFIG_ACPI */ 70 167 71 168 /** 72 169 * cm32181_reg_init() - Initialize CM32181 registers ··· 193 78 return ret; 194 79 195 80 /* check device ID */ 196 - if ((ret & 0xFF) != 0x81) 81 + switch (ret & 0xFF) { 82 + case 0x18: /* CM3218 */ 83 + cm32181->num_als_it = ARRAY_SIZE(cm3218_als_it_bits); 84 + cm32181->als_it_bits = cm3218_als_it_bits; 85 + cm32181->als_it_values = cm3218_als_it_values; 86 + break; 87 + case 0x81: /* CM32181 */ 88 + case 0x82: /* CM32182, fully compat. with CM32181 */ 89 + cm32181->num_als_it = ARRAY_SIZE(cm32181_als_it_bits); 90 + cm32181->als_it_bits = cm32181_als_it_bits; 91 + cm32181->als_it_values = cm32181_als_it_values; 92 + break; 93 + default: 197 94 return -ENODEV; 95 + } 198 96 199 97 /* Default Values */ 200 - cm32181->conf_regs[CM32181_REG_ADDR_CMD] = CM32181_CMD_ALS_ENABLE | 98 + cm32181->conf_regs[CM32181_REG_ADDR_CMD] = 201 99 CM32181_CMD_ALS_IT_DEFAULT | CM32181_CMD_ALS_SM_DEFAULT; 100 + cm32181->init_regs_bitmap = BIT(CM32181_REG_ADDR_CMD); 202 101 cm32181->calibscale = CM32181_CALIBSCALE_DEFAULT; 102 + cm32181->lux_per_bit = CM32181_LUX_PER_BIT; 103 + cm32181->lux_per_bit_base_it = CM32181_LUX_PER_BIT_BASE_IT; 104 + 105 + if (ACPI_HANDLE(cm32181->dev)) 106 + cm32181_acpi_parse_cpm_tables(cm32181); 203 107 204 108 /* Initialize registers*/ 205 - for (i = 0; i < CM32181_CONF_REG_NUM; i++) { 206 - ret = i2c_smbus_write_word_data(client, cm32181_reg[i], 207 - cm32181->conf_regs[i]); 109 + for_each_set_bit(i, &cm32181->init_regs_bitmap, CM32181_CONF_REG_NUM) { 110 + ret = i2c_smbus_write_word_data(client, i, 111 + cm32181->conf_regs[i]); 208 112 if (ret < 0) 209 113 return ret; 210 114 } ··· 236 102 * @cm32181: pointer of struct cm32181 237 103 * @val2: pointer of int to load the als_it value. 238 104 * 239 - * Report the current integartion time by millisecond. 105 + * Report the current integration time in milliseconds. 240 106 * 241 107 * Return: IIO_VAL_INT_PLUS_MICRO for success, otherwise -EINVAL. 242 108 */ ··· 248 114 als_it = cm32181->conf_regs[CM32181_REG_ADDR_CMD]; 249 115 als_it &= CM32181_CMD_ALS_IT_MASK; 250 116 als_it >>= CM32181_CMD_ALS_IT_SHIFT; 251 - for (i = 0; i < ARRAY_SIZE(als_it_bits); i++) { 252 - if (als_it == als_it_bits[i]) { 253 - *val2 = als_it_value[i]; 117 + for (i = 0; i < cm32181->num_als_it; i++) { 118 + if (als_it == cm32181->als_it_bits[i]) { 119 + *val2 = cm32181->als_it_values[i]; 254 120 return IIO_VAL_INT_PLUS_MICRO; 255 121 } 256 122 } ··· 273 139 u16 als_it; 274 140 int ret, i, n; 275 141 276 - n = ARRAY_SIZE(als_it_value); 142 + n = cm32181->num_als_it; 277 143 for (i = 0; i < n; i++) 278 - if (val <= als_it_value[i]) 144 + if (val <= cm32181->als_it_values[i]) 279 145 break; 280 146 if (i >= n) 281 147 i = n - 1; 282 148 283 - als_it = als_it_bits[i]; 149 + als_it = cm32181->als_it_bits[i]; 284 150 als_it <<= CM32181_CMD_ALS_IT_SHIFT; 285 151 286 152 mutex_lock(&cm32181->lock); ··· 309 175 struct i2c_client *client = cm32181->client; 310 176 int ret; 311 177 int als_it; 312 - unsigned long lux; 178 + u64 lux; 313 179 314 180 ret = cm32181_read_als_it(cm32181, &als_it); 315 181 if (ret < 0) 316 182 return -EINVAL; 317 183 318 - lux = CM32181_MLUX_PER_BIT; 319 - lux *= CM32181_MLUX_PER_BIT_BASE_IT; 320 - lux /= als_it; 184 + lux = cm32181->lux_per_bit; 185 + lux *= cm32181->lux_per_bit_base_it; 186 + lux = div_u64(lux, als_it); 321 187 322 188 ret = i2c_smbus_read_word_data(client, CM32181_REG_ADDR_ALS); 323 189 if (ret < 0) ··· 325 191 326 192 lux *= ret; 327 193 lux *= cm32181->calibscale; 328 - lux /= CM32181_CALIBSCALE_RESOLUTION; 329 - lux /= MLUX_PER_LUX; 194 + lux = div_u64(lux, CM32181_CALIBSCALE_RESOLUTION); 195 + lux = div_u64(lux, CM32181_LUX_PER_BIT_RESOLUTION); 330 196 331 197 if (lux > 0xFFFF) 332 198 lux = 0xFFFF; ··· 392 258 static ssize_t cm32181_get_it_available(struct device *dev, 393 259 struct device_attribute *attr, char *buf) 394 260 { 261 + struct cm32181_chip *cm32181 = iio_priv(dev_to_iio_dev(dev)); 395 262 int i, n, len; 396 263 397 - n = ARRAY_SIZE(als_it_value); 264 + n = cm32181->num_als_it; 398 265 for (i = 0, len = 0; i < n; i++) 399 - len += sprintf(buf + len, "0.%06u ", als_it_value[i]); 266 + len += sprintf(buf + len, "0.%06u ", cm32181->als_it_values[i]); 400 267 return len + sprintf(buf + len, "\n"); 401 268 } 402 269 ··· 429 294 .attrs = &cm32181_attribute_group, 430 295 }; 431 296 432 - static int cm32181_probe(struct i2c_client *client, 433 - const struct i2c_device_id *id) 297 + static int cm32181_probe(struct i2c_client *client) 434 298 { 299 + struct device *dev = &client->dev; 435 300 struct cm32181_chip *cm32181; 436 301 struct iio_dev *indio_dev; 437 302 int ret; 438 303 439 - indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*cm32181)); 440 - if (!indio_dev) { 441 - dev_err(&client->dev, "devm_iio_device_alloc failed\n"); 304 + indio_dev = devm_iio_device_alloc(dev, sizeof(*cm32181)); 305 + if (!indio_dev) 442 306 return -ENOMEM; 307 + 308 + /* 309 + * Some ACPI systems list 2 I2C resources for the CM3218 sensor, the 310 + * SMBus Alert Response Address (ARA, 0x0c) and the actual I2C address. 311 + * Detect this and take the following step to deal with it: 312 + * 1. When a SMBus Alert capable sensor has an Alert asserted, it will 313 + * not respond on its actual I2C address. Read a byte from the ARA 314 + * to clear any pending Alerts. 315 + * 2. Create a "dummy" client for the actual I2C address and 316 + * use that client to communicate with the sensor. 317 + */ 318 + if (ACPI_HANDLE(dev) && client->addr == SMBUS_ALERT_RESPONSE_ADDRESS) { 319 + struct i2c_board_info board_info = { .type = "dummy" }; 320 + 321 + i2c_smbus_read_byte(client); 322 + 323 + client = i2c_acpi_new_device(dev, 1, &board_info); 324 + if (IS_ERR(client)) 325 + return PTR_ERR(client); 443 326 } 444 327 445 328 cm32181 = iio_priv(indio_dev); 446 - i2c_set_clientdata(client, indio_dev); 447 329 cm32181->client = client; 330 + cm32181->dev = dev; 448 331 449 332 mutex_init(&cm32181->lock); 450 - indio_dev->dev.parent = &client->dev; 333 + indio_dev->dev.parent = dev; 451 334 indio_dev->channels = cm32181_channels; 452 335 indio_dev->num_channels = ARRAY_SIZE(cm32181_channels); 453 336 indio_dev->info = &cm32181_info; 454 - indio_dev->name = id->name; 337 + indio_dev->name = dev_name(dev); 455 338 indio_dev->modes = INDIO_DIRECT_MODE; 456 339 457 340 ret = cm32181_reg_init(cm32181); 458 341 if (ret) { 459 - dev_err(&client->dev, 460 - "%s: register init failed\n", 461 - __func__); 342 + dev_err(dev, "%s: register init failed\n", __func__); 462 343 return ret; 463 344 } 464 345 465 - ret = devm_iio_device_register(&client->dev, indio_dev); 346 + ret = devm_iio_device_register(dev, indio_dev); 466 347 if (ret) { 467 - dev_err(&client->dev, 468 - "%s: regist device failed\n", 469 - __func__); 348 + dev_err(dev, "%s: regist device failed\n", __func__); 470 349 return ret; 471 350 } 472 351 473 352 return 0; 474 353 } 475 354 476 - static const struct i2c_device_id cm32181_id[] = { 477 - { "cm32181", 0 }, 478 - { } 479 - }; 480 - 481 - MODULE_DEVICE_TABLE(i2c, cm32181_id); 482 - 483 355 static const struct of_device_id cm32181_of_match[] = { 356 + { .compatible = "capella,cm3218" }, 484 357 { .compatible = "capella,cm32181" }, 485 358 { } 486 359 }; 487 360 MODULE_DEVICE_TABLE(of, cm32181_of_match); 488 361 362 + #ifdef CONFIG_ACPI 363 + static const struct acpi_device_id cm32181_acpi_match[] = { 364 + { "CPLM3218", 0 }, 365 + { } 366 + }; 367 + MODULE_DEVICE_TABLE(acpi, cm32181_acpi_match); 368 + #endif 369 + 489 370 static struct i2c_driver cm32181_driver = { 490 371 .driver = { 491 372 .name = "cm32181", 492 - .of_match_table = of_match_ptr(cm32181_of_match), 373 + .acpi_match_table = ACPI_PTR(cm32181_acpi_match), 374 + .of_match_table = cm32181_of_match, 493 375 }, 494 - .id_table = cm32181_id, 495 - .probe = cm32181_probe, 376 + .probe_new = cm32181_probe, 496 377 }; 497 378 498 379 module_i2c_driver(cm32181_driver);
+2 -1
drivers/iio/light/cm3232.c
··· 10 10 11 11 #include <linux/i2c.h> 12 12 #include <linux/module.h> 13 + #include <linux/mod_devicetable.h> 13 14 #include <linux/iio/iio.h> 14 15 #include <linux/iio/sysfs.h> 15 16 #include <linux/init.h> ··· 419 418 static struct i2c_driver cm3232_driver = { 420 419 .driver = { 421 420 .name = "cm3232", 422 - .of_match_table = of_match_ptr(cm3232_of_match), 421 + .of_match_table = cm3232_of_match, 423 422 #ifdef CONFIG_PM_SLEEP 424 423 .pm = &cm3232_pm_ops, 425 424 #endif
+2 -4
drivers/iio/light/gp2ap020a00f.c
··· 38 38 #include <linux/irq.h> 39 39 #include <linux/irq_work.h> 40 40 #include <linux/module.h> 41 + #include <linux/mod_devicetable.h> 41 42 #include <linux/mutex.h> 42 - #include <linux/of.h> 43 43 #include <linux/regmap.h> 44 44 #include <linux/regulator/consumer.h> 45 45 #include <linux/slab.h> ··· 1617 1617 1618 1618 MODULE_DEVICE_TABLE(i2c, gp2ap020a00f_id); 1619 1619 1620 - #ifdef CONFIG_OF 1621 1620 static const struct of_device_id gp2ap020a00f_of_match[] = { 1622 1621 { .compatible = "sharp,gp2ap020a00f" }, 1623 1622 { } 1624 1623 }; 1625 1624 MODULE_DEVICE_TABLE(of, gp2ap020a00f_of_match); 1626 - #endif 1627 1625 1628 1626 static struct i2c_driver gp2ap020a00f_driver = { 1629 1627 .driver = { 1630 1628 .name = GP2A_I2C_NAME, 1631 - .of_match_table = of_match_ptr(gp2ap020a00f_of_match), 1629 + .of_match_table = gp2ap020a00f_of_match, 1632 1630 }, 1633 1631 .probe = gp2ap020a00f_probe, 1634 1632 .remove = gp2ap020a00f_remove,
+4 -14
drivers/iio/light/hid-sensor-als.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 enum { ··· 306 308 indio_dev->name = name; 307 309 indio_dev->modes = INDIO_DIRECT_MODE; 308 310 309 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 310 - NULL, NULL); 311 - if (ret) { 312 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 313 - goto error_free_dev_mem; 314 - } 315 311 atomic_set(&als_state->common_attributes.data_ready, 0); 312 + 316 313 ret = hid_sensor_setup_trigger(indio_dev, name, 317 314 &als_state->common_attributes); 318 315 if (ret < 0) { 319 316 dev_err(&pdev->dev, "trigger setup failed\n"); 320 - goto error_unreg_buffer_funcs; 317 + goto error_free_dev_mem; 321 318 } 322 319 323 320 ret = iio_device_register(indio_dev); ··· 336 343 error_iio_unreg: 337 344 iio_device_unregister(indio_dev); 338 345 error_remove_trigger: 339 - hid_sensor_remove_trigger(&als_state->common_attributes); 340 - error_unreg_buffer_funcs: 341 - iio_triggered_buffer_cleanup(indio_dev); 346 + hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes); 342 347 error_free_dev_mem: 343 348 kfree(indio_dev->channels); 344 349 return ret; ··· 351 360 352 361 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_ALS); 353 362 iio_device_unregister(indio_dev); 354 - hid_sensor_remove_trigger(&als_state->common_attributes); 355 - iio_triggered_buffer_cleanup(indio_dev); 363 + hid_sensor_remove_trigger(indio_dev, &als_state->common_attributes); 356 364 kfree(indio_dev->channels); 357 365 358 366 return 0;
+4 -14
drivers/iio/light/hid-sensor-prox.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 #define CHANNEL_SCAN_INDEX_PRESENCE 0 ··· 284 286 indio_dev->name = name; 285 287 indio_dev->modes = INDIO_DIRECT_MODE; 286 288 287 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 288 - NULL, NULL); 289 - if (ret) { 290 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 291 - goto error_free_dev_mem; 292 - } 293 289 atomic_set(&prox_state->common_attributes.data_ready, 0); 290 + 294 291 ret = hid_sensor_setup_trigger(indio_dev, name, 295 292 &prox_state->common_attributes); 296 293 if (ret) { 297 294 dev_err(&pdev->dev, "trigger setup failed\n"); 298 - goto error_unreg_buffer_funcs; 295 + goto error_free_dev_mem; 299 296 } 300 297 301 298 ret = iio_device_register(indio_dev); ··· 314 321 error_iio_unreg: 315 322 iio_device_unregister(indio_dev); 316 323 error_remove_trigger: 317 - hid_sensor_remove_trigger(&prox_state->common_attributes); 318 - error_unreg_buffer_funcs: 319 - iio_triggered_buffer_cleanup(indio_dev); 324 + hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes); 320 325 error_free_dev_mem: 321 326 kfree(indio_dev->channels); 322 327 return ret; ··· 329 338 330 339 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PROX); 331 340 iio_device_unregister(indio_dev); 332 - hid_sensor_remove_trigger(&prox_state->common_attributes); 333 - iio_triggered_buffer_cleanup(indio_dev); 341 + hid_sensor_remove_trigger(indio_dev, &prox_state->common_attributes); 334 342 kfree(indio_dev->channels); 335 343 336 344 return 0;
+20 -19
drivers/iio/light/ltr501.c
··· 101 101 int uscale; 102 102 }; 103 103 104 - static struct ltr501_gain ltr501_als_gain_tbl[] = { 104 + static const struct ltr501_gain ltr501_als_gain_tbl[] = { 105 105 {1, 0}, 106 106 {0, 5000}, 107 107 }; 108 108 109 - static struct ltr501_gain ltr559_als_gain_tbl[] = { 109 + static const struct ltr501_gain ltr559_als_gain_tbl[] = { 110 110 {1, 0}, 111 111 {0, 500000}, 112 112 {0, 250000}, ··· 117 117 {0, 10000}, 118 118 }; 119 119 120 - static struct ltr501_gain ltr501_ps_gain_tbl[] = { 120 + static const struct ltr501_gain ltr501_ps_gain_tbl[] = { 121 121 {1, 0}, 122 122 {0, 250000}, 123 123 {0, 125000}, 124 124 {0, 62500}, 125 125 }; 126 126 127 - static struct ltr501_gain ltr559_ps_gain_tbl[] = { 127 + static const struct ltr501_gain ltr559_ps_gain_tbl[] = { 128 128 {0, 62500}, /* x16 gain */ 129 129 {0, 31250}, /* x32 gain */ 130 130 {0, 15625}, /* bits X1 are for x64 gain */ ··· 133 133 134 134 struct ltr501_chip_info { 135 135 u8 partid; 136 - struct ltr501_gain *als_gain; 136 + const struct ltr501_gain *als_gain; 137 137 int als_gain_tbl_size; 138 - struct ltr501_gain *ps_gain; 138 + const struct ltr501_gain *ps_gain; 139 139 int ps_gain_tbl_size; 140 140 u8 als_mode_active; 141 141 u8 als_gain_mask; ··· 192 192 return -EINVAL; 193 193 } 194 194 195 - static int ltr501_als_read_samp_freq(struct ltr501_data *data, 195 + static int ltr501_als_read_samp_freq(const struct ltr501_data *data, 196 196 int *val, int *val2) 197 197 { 198 198 int ret, i; ··· 210 210 return IIO_VAL_INT_PLUS_MICRO; 211 211 } 212 212 213 - static int ltr501_ps_read_samp_freq(struct ltr501_data *data, 213 + static int ltr501_ps_read_samp_freq(const struct ltr501_data *data, 214 214 int *val, int *val2) 215 215 { 216 216 int ret, i; ··· 266 266 return ret; 267 267 } 268 268 269 - static int ltr501_als_read_samp_period(struct ltr501_data *data, int *val) 269 + static int ltr501_als_read_samp_period(const struct ltr501_data *data, int *val) 270 270 { 271 271 int ret, i; 272 272 ··· 282 282 return IIO_VAL_INT; 283 283 } 284 284 285 - static int ltr501_ps_read_samp_period(struct ltr501_data *data, int *val) 285 + static int ltr501_ps_read_samp_period(const struct ltr501_data *data, int *val) 286 286 { 287 287 int ret, i; 288 288 ··· 321 321 return lux / 1000; 322 322 } 323 323 324 - static int ltr501_drdy(struct ltr501_data *data, u8 drdy_mask) 324 + static int ltr501_drdy(const struct ltr501_data *data, u8 drdy_mask) 325 325 { 326 326 int tries = 100; 327 327 int ret, status; ··· 373 373 } 374 374 375 375 /* read int time in micro seconds */ 376 - static int ltr501_read_it_time(struct ltr501_data *data, int *val, int *val2) 376 + static int ltr501_read_it_time(const struct ltr501_data *data, 377 + int *val, int *val2) 377 378 { 378 379 int ret, index; 379 380 ··· 392 391 return IIO_VAL_INT_PLUS_MICRO; 393 392 } 394 393 395 - static int ltr501_read_als(struct ltr501_data *data, __le16 buf[2]) 394 + static int ltr501_read_als(const struct ltr501_data *data, __le16 buf[2]) 396 395 { 397 396 int ret; 398 397 ··· 404 403 buf, 2 * sizeof(__le16)); 405 404 } 406 405 407 - static int ltr501_read_ps(struct ltr501_data *data) 406 + static int ltr501_read_ps(const struct ltr501_data *data) 408 407 { 409 408 int ret, status; 410 409 ··· 420 419 return status; 421 420 } 422 421 423 - static int ltr501_read_intr_prst(struct ltr501_data *data, 422 + static int ltr501_read_intr_prst(const struct ltr501_data *data, 424 423 enum iio_chan_type type, 425 424 int *val2) 426 425 { ··· 717 716 return -EINVAL; 718 717 } 719 718 720 - static int ltr501_get_gain_index(struct ltr501_gain *gain, int size, 719 + static int ltr501_get_gain_index(const struct ltr501_gain *gain, int size, 721 720 int val, int val2) 722 721 { 723 722 int i; ··· 849 848 return ret; 850 849 } 851 850 852 - static int ltr501_read_thresh(struct iio_dev *indio_dev, 851 + static int ltr501_read_thresh(const struct iio_dev *indio_dev, 853 852 const struct iio_chan_spec *chan, 854 853 enum iio_event_type type, 855 854 enum iio_event_direction dir, 856 855 enum iio_event_info info, 857 856 int *val, int *val2) 858 857 { 859 - struct ltr501_data *data = iio_priv(indio_dev); 858 + const struct ltr501_data *data = iio_priv(indio_dev); 860 859 int ret, thresh_data; 861 860 862 861 switch (chan->type) { ··· 1360 1359 } 1361 1360 } 1362 1361 1363 - static struct regmap_config ltr501_regmap_config = { 1362 + static const struct regmap_config ltr501_regmap_config = { 1364 1363 .name = LTR501_REGMAP_NAME, 1365 1364 .reg_bits = 8, 1366 1365 .val_bits = 8,
+2 -1
drivers/iio/light/opt3001.c
··· 16 16 #include <linux/irq.h> 17 17 #include <linux/kernel.h> 18 18 #include <linux/module.h> 19 + #include <linux/mod_devicetable.h> 19 20 #include <linux/mutex.h> 20 21 #include <linux/slab.h> 21 22 #include <linux/types.h> ··· 845 844 846 845 .driver = { 847 846 .name = "opt3001", 848 - .of_match_table = of_match_ptr(opt3001_of_match), 847 + .of_match_table = opt3001_of_match, 849 848 }, 850 849 }; 851 850
+6 -12
drivers/iio/light/si1133.c
··· 17 17 18 18 #include <linux/util_macros.h> 19 19 20 + #include <asm/unaligned.h> 21 + 20 22 #define SI1133_REG_PART_ID 0x00 21 23 #define SI1133_REG_REV_ID 0x01 22 24 #define SI1133_REG_MFR_ID 0x02 ··· 105 103 #define SI1133_LUX_OUTPUT_FRACTION 12 106 104 #define SI1133_LUX_BUFFER_SIZE 9 107 105 #define SI1133_MEASURE_BUFFER_SIZE 3 108 - 109 - #define SI1133_SIGN_BIT_INDEX 23 110 106 111 107 static const int si1133_scale_available[] = { 112 108 1, 2, 4, 8, 16, 32, 64, 128}; ··· 633 633 if (err) 634 634 return err; 635 635 636 - *val = sign_extend32((buffer[0] << 16) | (buffer[1] << 8) | buffer[2], 637 - SI1133_SIGN_BIT_INDEX); 636 + *val = sign_extend32(get_unaligned_be24(&buffer[0]), 23); 638 637 639 638 return err; 640 639 } ··· 722 723 if (err) 723 724 return err; 724 725 725 - high_vis = 726 - sign_extend32((buffer[0] << 16) | (buffer[1] << 8) | buffer[2], 727 - SI1133_SIGN_BIT_INDEX); 726 + high_vis = sign_extend32(get_unaligned_be24(&buffer[0]), 23); 728 727 729 - low_vis = 730 - sign_extend32((buffer[3] << 16) | (buffer[4] << 8) | buffer[5], 731 - SI1133_SIGN_BIT_INDEX); 728 + low_vis = sign_extend32(get_unaligned_be24(&buffer[3]), 23); 732 729 733 - ir = sign_extend32((buffer[6] << 16) | (buffer[7] << 8) | buffer[8], 734 - SI1133_SIGN_BIT_INDEX); 730 + ir = sign_extend32(get_unaligned_be24(&buffer[6]), 23); 735 731 736 732 if (high_vis > SI1133_ADC_THRESHOLD || ir > SI1133_ADC_THRESHOLD) 737 733 lux = si1133_calc_polynomial(high_vis, ir,
+2 -1
drivers/iio/light/st_uvis25_i2c.c
··· 9 9 10 10 #include <linux/kernel.h> 11 11 #include <linux/module.h> 12 + #include <linux/mod_devicetable.h> 12 13 #include <linux/i2c.h> 13 14 #include <linux/slab.h> 14 15 #include <linux/regmap.h> ··· 56 55 .driver = { 57 56 .name = "st_uvis25_i2c", 58 57 .pm = &st_uvis25_pm_ops, 59 - .of_match_table = of_match_ptr(st_uvis25_i2c_of_match), 58 + .of_match_table = st_uvis25_i2c_of_match, 60 59 }, 61 60 .probe = st_uvis25_i2c_probe, 62 61 .id_table = st_uvis25_i2c_id_table,
+2 -1
drivers/iio/light/st_uvis25_spi.c
··· 9 9 10 10 #include <linux/kernel.h> 11 11 #include <linux/module.h> 12 + #include <linux/mod_devicetable.h> 12 13 #include <linux/spi/spi.h> 13 14 #include <linux/slab.h> 14 15 #include <linux/regmap.h> ··· 56 55 .driver = { 57 56 .name = "st_uvis25_spi", 58 57 .pm = &st_uvis25_pm_ops, 59 - .of_match_table = of_match_ptr(st_uvis25_spi_of_match), 58 + .of_match_table = st_uvis25_spi_of_match, 60 59 }, 61 60 .probe = st_uvis25_spi_probe, 62 61 .id_table = st_uvis25_spi_id_table,
+687 -66
drivers/iio/light/vcnl4000.c
··· 5 5 * 6 6 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net> 7 7 * Copyright 2019 Pursim SPC 8 + * Copyright 2020 Mathieu Othacehe <m.othacehe@gmail.com> 8 9 * 9 10 * IIO driver for: 10 11 * VCNL4000/10/20 (7-bit I2C slave address 0x13) ··· 14 13 * 15 14 * TODO: 16 15 * allow to adjust IR current 17 - * proximity threshold and event handling 18 - * periodic ALS/proximity measurement (VCNL4010/20) 19 - * interrupts (VCNL4010/20/40, VCNL4200) 16 + * interrupts (VCNL4040, VCNL4200) 20 17 */ 21 18 22 19 #include <linux/module.h> ··· 22 23 #include <linux/err.h> 23 24 #include <linux/delay.h> 24 25 #include <linux/pm_runtime.h> 26 + #include <linux/interrupt.h> 25 27 28 + #include <linux/iio/buffer.h> 29 + #include <linux/iio/events.h> 26 30 #include <linux/iio/iio.h> 27 31 #include <linux/iio/sysfs.h> 32 + #include <linux/iio/trigger.h> 33 + #include <linux/iio/trigger_consumer.h> 34 + #include <linux/iio/triggered_buffer.h> 28 35 29 36 #define VCNL4000_DRV_NAME "vcnl4000" 30 37 #define VCNL4000_PROD_ID 0x01 ··· 40 35 41 36 #define VCNL4000_COMMAND 0x80 /* Command register */ 42 37 #define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */ 38 + #define VCNL4010_PROX_RATE 0x82 /* Proximity rate */ 43 39 #define VCNL4000_LED_CURRENT 0x83 /* IR LED current for proximity mode */ 44 40 #define VCNL4000_AL_PARAM 0x84 /* Ambient light parameter register */ 41 + #define VCNL4010_ALS_PARAM 0x84 /* ALS rate */ 45 42 #define VCNL4000_AL_RESULT_HI 0x85 /* Ambient light result register, MSB */ 46 43 #define VCNL4000_AL_RESULT_LO 0x86 /* Ambient light result register, LSB */ 47 44 #define VCNL4000_PS_RESULT_HI 0x87 /* Proximity result register, MSB */ 48 45 #define VCNL4000_PS_RESULT_LO 0x88 /* Proximity result register, LSB */ 49 46 #define VCNL4000_PS_MEAS_FREQ 0x89 /* Proximity test signal frequency */ 47 + #define VCNL4010_INT_CTRL 0x89 /* Interrupt control */ 50 48 #define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */ 49 + #define VCNL4010_LOW_THR_HI 0x8a /* Low threshold, MSB */ 50 + #define VCNL4010_LOW_THR_LO 0x8b /* Low threshold, LSB */ 51 + #define VCNL4010_HIGH_THR_HI 0x8c /* High threshold, MSB */ 52 + #define VCNL4010_HIGH_THR_LO 0x8d /* High threshold, LSB */ 53 + #define VCNL4010_ISR 0x8e /* Interrupt status */ 51 54 52 55 #define VCNL4200_AL_CONF 0x00 /* Ambient light configuration */ 53 56 #define VCNL4200_PS_CONF1 0x03 /* Proximity configuration */ ··· 70 57 #define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */ 71 58 #define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */ 72 59 #define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */ 60 + #define VCNL4000_ALS_EN BIT(2) /* start ALS measurement */ 61 + #define VCNL4000_PROX_EN BIT(1) /* start proximity measurement */ 62 + #define VCNL4000_SELF_TIMED_EN BIT(0) /* start self-timed measurement */ 63 + 64 + /* Bit masks for interrupt registers. */ 65 + #define VCNL4010_INT_THR_SEL BIT(0) /* Select threshold interrupt source */ 66 + #define VCNL4010_INT_THR_EN BIT(1) /* Threshold interrupt type */ 67 + #define VCNL4010_INT_ALS_EN BIT(2) /* Enable on ALS data ready */ 68 + #define VCNL4010_INT_PROX_EN BIT(3) /* Enable on proximity data ready */ 69 + 70 + #define VCNL4010_INT_THR_HIGH 0 /* High threshold exceeded */ 71 + #define VCNL4010_INT_THR_LOW 1 /* Low threshold exceeded */ 72 + #define VCNL4010_INT_ALS 2 /* ALS data ready */ 73 + #define VCNL4010_INT_PROXIMITY 3 /* Proximity data ready */ 74 + 75 + #define VCNL4010_INT_THR \ 76 + (BIT(VCNL4010_INT_THR_LOW) | BIT(VCNL4010_INT_THR_HIGH)) 77 + #define VCNL4010_INT_DRDY \ 78 + (BIT(VCNL4010_INT_PROXIMITY) | BIT(VCNL4010_INT_ALS)) 79 + 80 + static const int vcnl4010_prox_sampling_frequency[][2] = { 81 + {1, 950000}, 82 + {3, 906250}, 83 + {7, 812500}, 84 + {16, 625000}, 85 + {31, 250000}, 86 + {62, 500000}, 87 + {125, 0}, 88 + {250, 0}, 89 + }; 73 90 74 91 #define VCNL4000_SLEEP_DELAY_MS 2000 /* before we enter pm_runtime_suspend */ 75 92 ··· 131 88 132 89 struct vcnl4000_chip_spec { 133 90 const char *prod; 91 + struct iio_chan_spec const *channels; 92 + const int num_channels; 93 + const struct iio_info *info; 94 + bool irq_support; 134 95 int (*init)(struct vcnl4000_data *data); 135 96 int (*measure_light)(struct vcnl4000_data *data, int *val); 136 97 int (*measure_proximity)(struct vcnl4000_data *data, int *val); ··· 263 216 return 0; 264 217 }; 265 218 219 + static int vcnl4000_read_data(struct vcnl4000_data *data, u8 data_reg, int *val) 220 + { 221 + s32 ret; 222 + 223 + ret = i2c_smbus_read_word_swapped(data->client, data_reg); 224 + if (ret < 0) 225 + return ret; 226 + 227 + *val = ret; 228 + return 0; 229 + } 230 + 231 + static int vcnl4000_write_data(struct vcnl4000_data *data, u8 data_reg, int val) 232 + { 233 + if (val > U16_MAX) 234 + return -ERANGE; 235 + 236 + return i2c_smbus_write_word_swapped(data->client, data_reg, val); 237 + } 238 + 239 + 266 240 static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask, 267 241 u8 rdy_mask, u8 data_reg, int *val) 268 242 { 269 243 int tries = 20; 270 - __be16 buf; 271 244 int ret; 272 245 273 246 mutex_lock(&data->vcnl4000_lock); ··· 314 247 goto fail; 315 248 } 316 249 317 - ret = i2c_smbus_read_i2c_block_data(data->client, 318 - data_reg, sizeof(buf), (u8 *) &buf); 250 + ret = vcnl4000_read_data(data, data_reg, val); 319 251 if (ret < 0) 320 252 goto fail; 321 253 322 254 mutex_unlock(&data->vcnl4000_lock); 323 - *val = be16_to_cpu(buf); 324 255 325 256 return 0; 326 257 ··· 378 313 return vcnl4200_measure(data, &data->vcnl4200_ps, val); 379 314 } 380 315 381 - static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = { 382 - [VCNL4000] = { 383 - .prod = "VCNL4000", 384 - .init = vcnl4000_init, 385 - .measure_light = vcnl4000_measure_light, 386 - .measure_proximity = vcnl4000_measure_proximity, 387 - .set_power_state = vcnl4000_set_power_state, 388 - }, 389 - [VCNL4010] = { 390 - .prod = "VCNL4010/4020", 391 - .init = vcnl4000_init, 392 - .measure_light = vcnl4000_measure_light, 393 - .measure_proximity = vcnl4000_measure_proximity, 394 - .set_power_state = vcnl4000_set_power_state, 395 - }, 396 - [VCNL4040] = { 397 - .prod = "VCNL4040", 398 - .init = vcnl4200_init, 399 - .measure_light = vcnl4200_measure_light, 400 - .measure_proximity = vcnl4200_measure_proximity, 401 - .set_power_state = vcnl4200_set_power_state, 402 - }, 403 - [VCNL4200] = { 404 - .prod = "VCNL4200", 405 - .init = vcnl4200_init, 406 - .measure_light = vcnl4200_measure_light, 407 - .measure_proximity = vcnl4200_measure_proximity, 408 - .set_power_state = vcnl4200_set_power_state, 409 - }, 410 - }; 411 - 412 - static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev, 413 - uintptr_t priv, 414 - const struct iio_chan_spec *chan, 415 - char *buf) 316 + static int vcnl4010_read_proxy_samp_freq(struct vcnl4000_data *data, int *val, 317 + int *val2) 416 318 { 417 - struct vcnl4000_data *data = iio_priv(indio_dev); 319 + int ret; 418 320 419 - return sprintf(buf, "%u\n", data->near_level); 321 + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_PROX_RATE); 322 + if (ret < 0) 323 + return ret; 324 + 325 + if (ret >= ARRAY_SIZE(vcnl4010_prox_sampling_frequency)) 326 + return -EINVAL; 327 + 328 + *val = vcnl4010_prox_sampling_frequency[ret][0]; 329 + *val2 = vcnl4010_prox_sampling_frequency[ret][1]; 330 + 331 + return 0; 420 332 } 421 333 422 - static const struct iio_chan_spec_ext_info vcnl4000_ext_info[] = { 423 - { 424 - .name = "nearlevel", 425 - .shared = IIO_SEPARATE, 426 - .read = vcnl4000_read_near_level, 427 - }, 428 - { /* sentinel */ } 429 - }; 334 + static bool vcnl4010_is_in_periodic_mode(struct vcnl4000_data *data) 335 + { 336 + int ret; 430 337 431 - static const struct iio_chan_spec vcnl4000_channels[] = { 432 - { 433 - .type = IIO_LIGHT, 434 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 435 - BIT(IIO_CHAN_INFO_SCALE), 436 - }, { 437 - .type = IIO_PROXIMITY, 438 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 439 - .ext_info = vcnl4000_ext_info, 440 - } 441 - }; 338 + ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND); 339 + if (ret < 0) 340 + return false; 341 + 342 + return !!(ret & VCNL4000_SELF_TIMED_EN); 343 + } 442 344 443 345 static int vcnl4000_set_pm_runtime_state(struct vcnl4000_data *data, bool on) 444 346 { ··· 465 433 } 466 434 } 467 435 436 + static int vcnl4010_read_raw(struct iio_dev *indio_dev, 437 + struct iio_chan_spec const *chan, 438 + int *val, int *val2, long mask) 439 + { 440 + int ret; 441 + struct vcnl4000_data *data = iio_priv(indio_dev); 442 + 443 + switch (mask) { 444 + case IIO_CHAN_INFO_RAW: 445 + case IIO_CHAN_INFO_SCALE: 446 + ret = iio_device_claim_direct_mode(indio_dev); 447 + if (ret) 448 + return ret; 449 + 450 + /* Protect against event capture. */ 451 + if (vcnl4010_is_in_periodic_mode(data)) { 452 + ret = -EBUSY; 453 + } else { 454 + ret = vcnl4000_read_raw(indio_dev, chan, val, val2, 455 + mask); 456 + } 457 + 458 + iio_device_release_direct_mode(indio_dev); 459 + return ret; 460 + case IIO_CHAN_INFO_SAMP_FREQ: 461 + switch (chan->type) { 462 + case IIO_PROXIMITY: 463 + ret = vcnl4010_read_proxy_samp_freq(data, val, val2); 464 + if (ret < 0) 465 + return ret; 466 + return IIO_VAL_INT_PLUS_MICRO; 467 + default: 468 + return -EINVAL; 469 + } 470 + default: 471 + return -EINVAL; 472 + } 473 + } 474 + 475 + static int vcnl4010_read_avail(struct iio_dev *indio_dev, 476 + struct iio_chan_spec const *chan, 477 + const int **vals, int *type, int *length, 478 + long mask) 479 + { 480 + switch (mask) { 481 + case IIO_CHAN_INFO_SAMP_FREQ: 482 + *vals = (int *)vcnl4010_prox_sampling_frequency; 483 + *type = IIO_VAL_INT_PLUS_MICRO; 484 + *length = 2 * ARRAY_SIZE(vcnl4010_prox_sampling_frequency); 485 + return IIO_AVAIL_LIST; 486 + default: 487 + return -EINVAL; 488 + } 489 + } 490 + 491 + static int vcnl4010_write_proxy_samp_freq(struct vcnl4000_data *data, int val, 492 + int val2) 493 + { 494 + unsigned int i; 495 + int index = -1; 496 + 497 + for (i = 0; i < ARRAY_SIZE(vcnl4010_prox_sampling_frequency); i++) { 498 + if (val == vcnl4010_prox_sampling_frequency[i][0] && 499 + val2 == vcnl4010_prox_sampling_frequency[i][1]) { 500 + index = i; 501 + break; 502 + } 503 + } 504 + 505 + if (index < 0) 506 + return -EINVAL; 507 + 508 + return i2c_smbus_write_byte_data(data->client, VCNL4010_PROX_RATE, 509 + index); 510 + } 511 + 512 + static int vcnl4010_write_raw(struct iio_dev *indio_dev, 513 + struct iio_chan_spec const *chan, 514 + int val, int val2, long mask) 515 + { 516 + int ret; 517 + struct vcnl4000_data *data = iio_priv(indio_dev); 518 + 519 + ret = iio_device_claim_direct_mode(indio_dev); 520 + if (ret) 521 + return ret; 522 + 523 + /* Protect against event capture. */ 524 + if (vcnl4010_is_in_periodic_mode(data)) { 525 + ret = -EBUSY; 526 + goto end; 527 + } 528 + 529 + switch (mask) { 530 + case IIO_CHAN_INFO_SAMP_FREQ: 531 + switch (chan->type) { 532 + case IIO_PROXIMITY: 533 + ret = vcnl4010_write_proxy_samp_freq(data, val, val2); 534 + goto end; 535 + default: 536 + ret = -EINVAL; 537 + goto end; 538 + } 539 + default: 540 + ret = -EINVAL; 541 + goto end; 542 + } 543 + 544 + end: 545 + iio_device_release_direct_mode(indio_dev); 546 + return ret; 547 + } 548 + 549 + static int vcnl4010_read_event(struct iio_dev *indio_dev, 550 + const struct iio_chan_spec *chan, 551 + enum iio_event_type type, 552 + enum iio_event_direction dir, 553 + enum iio_event_info info, 554 + int *val, int *val2) 555 + { 556 + int ret; 557 + struct vcnl4000_data *data = iio_priv(indio_dev); 558 + 559 + switch (info) { 560 + case IIO_EV_INFO_VALUE: 561 + switch (dir) { 562 + case IIO_EV_DIR_RISING: 563 + ret = vcnl4000_read_data(data, VCNL4010_HIGH_THR_HI, 564 + val); 565 + if (ret < 0) 566 + return ret; 567 + return IIO_VAL_INT; 568 + case IIO_EV_DIR_FALLING: 569 + ret = vcnl4000_read_data(data, VCNL4010_LOW_THR_HI, 570 + val); 571 + if (ret < 0) 572 + return ret; 573 + return IIO_VAL_INT; 574 + default: 575 + return -EINVAL; 576 + } 577 + default: 578 + return -EINVAL; 579 + } 580 + } 581 + 582 + static int vcnl4010_write_event(struct iio_dev *indio_dev, 583 + const struct iio_chan_spec *chan, 584 + enum iio_event_type type, 585 + enum iio_event_direction dir, 586 + enum iio_event_info info, 587 + int val, int val2) 588 + { 589 + int ret; 590 + struct vcnl4000_data *data = iio_priv(indio_dev); 591 + 592 + switch (info) { 593 + case IIO_EV_INFO_VALUE: 594 + switch (dir) { 595 + case IIO_EV_DIR_RISING: 596 + ret = vcnl4000_write_data(data, VCNL4010_HIGH_THR_HI, 597 + val); 598 + if (ret < 0) 599 + return ret; 600 + return IIO_VAL_INT; 601 + case IIO_EV_DIR_FALLING: 602 + ret = vcnl4000_write_data(data, VCNL4010_LOW_THR_HI, 603 + val); 604 + if (ret < 0) 605 + return ret; 606 + return IIO_VAL_INT; 607 + default: 608 + return -EINVAL; 609 + } 610 + default: 611 + return -EINVAL; 612 + } 613 + } 614 + 615 + static bool vcnl4010_is_thr_enabled(struct vcnl4000_data *data) 616 + { 617 + int ret; 618 + 619 + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_INT_CTRL); 620 + if (ret < 0) 621 + return false; 622 + 623 + return !!(ret & VCNL4010_INT_THR_EN); 624 + } 625 + 626 + static int vcnl4010_read_event_config(struct iio_dev *indio_dev, 627 + const struct iio_chan_spec *chan, 628 + enum iio_event_type type, 629 + enum iio_event_direction dir) 630 + { 631 + struct vcnl4000_data *data = iio_priv(indio_dev); 632 + 633 + switch (chan->type) { 634 + case IIO_PROXIMITY: 635 + return vcnl4010_is_thr_enabled(data); 636 + default: 637 + return -EINVAL; 638 + } 639 + } 640 + 641 + static int vcnl4010_config_threshold(struct iio_dev *indio_dev, bool state) 642 + { 643 + struct vcnl4000_data *data = iio_priv(indio_dev); 644 + int ret; 645 + int icr; 646 + int command; 647 + 648 + if (state) { 649 + ret = iio_device_claim_direct_mode(indio_dev); 650 + if (ret) 651 + return ret; 652 + 653 + /* Enable periodic measurement of proximity data. */ 654 + command = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN; 655 + 656 + /* 657 + * Enable interrupts on threshold, for proximity data by 658 + * default. 659 + */ 660 + icr = VCNL4010_INT_THR_EN; 661 + } else { 662 + if (!vcnl4010_is_thr_enabled(data)) 663 + return 0; 664 + 665 + command = 0; 666 + icr = 0; 667 + } 668 + 669 + ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 670 + command); 671 + if (ret < 0) 672 + goto end; 673 + 674 + ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, icr); 675 + 676 + end: 677 + if (state) 678 + iio_device_release_direct_mode(indio_dev); 679 + 680 + return ret; 681 + } 682 + 683 + static int vcnl4010_write_event_config(struct iio_dev *indio_dev, 684 + const struct iio_chan_spec *chan, 685 + enum iio_event_type type, 686 + enum iio_event_direction dir, 687 + int state) 688 + { 689 + switch (chan->type) { 690 + case IIO_PROXIMITY: 691 + return vcnl4010_config_threshold(indio_dev, state); 692 + default: 693 + return -EINVAL; 694 + } 695 + } 696 + 697 + static ssize_t vcnl4000_read_near_level(struct iio_dev *indio_dev, 698 + uintptr_t priv, 699 + const struct iio_chan_spec *chan, 700 + char *buf) 701 + { 702 + struct vcnl4000_data *data = iio_priv(indio_dev); 703 + 704 + return sprintf(buf, "%u\n", data->near_level); 705 + } 706 + 707 + static const struct iio_chan_spec_ext_info vcnl4000_ext_info[] = { 708 + { 709 + .name = "nearlevel", 710 + .shared = IIO_SEPARATE, 711 + .read = vcnl4000_read_near_level, 712 + }, 713 + { /* sentinel */ } 714 + }; 715 + 716 + static const struct iio_event_spec vcnl4000_event_spec[] = { 717 + { 718 + .type = IIO_EV_TYPE_THRESH, 719 + .dir = IIO_EV_DIR_RISING, 720 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 721 + }, { 722 + .type = IIO_EV_TYPE_THRESH, 723 + .dir = IIO_EV_DIR_FALLING, 724 + .mask_separate = BIT(IIO_EV_INFO_VALUE), 725 + }, { 726 + .type = IIO_EV_TYPE_THRESH, 727 + .dir = IIO_EV_DIR_EITHER, 728 + .mask_separate = BIT(IIO_EV_INFO_ENABLE), 729 + } 730 + }; 731 + 732 + static const struct iio_chan_spec vcnl4000_channels[] = { 733 + { 734 + .type = IIO_LIGHT, 735 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 736 + BIT(IIO_CHAN_INFO_SCALE), 737 + }, { 738 + .type = IIO_PROXIMITY, 739 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 740 + .ext_info = vcnl4000_ext_info, 741 + } 742 + }; 743 + 744 + static const struct iio_chan_spec vcnl4010_channels[] = { 745 + { 746 + .type = IIO_LIGHT, 747 + .scan_index = -1, 748 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 749 + BIT(IIO_CHAN_INFO_SCALE), 750 + }, { 751 + .type = IIO_PROXIMITY, 752 + .scan_index = 0, 753 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 754 + BIT(IIO_CHAN_INFO_SAMP_FREQ), 755 + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SAMP_FREQ), 756 + .event_spec = vcnl4000_event_spec, 757 + .num_event_specs = ARRAY_SIZE(vcnl4000_event_spec), 758 + .ext_info = vcnl4000_ext_info, 759 + .scan_type = { 760 + .sign = 'u', 761 + .realbits = 16, 762 + .storagebits = 16, 763 + .endianness = IIO_CPU, 764 + }, 765 + }, 766 + IIO_CHAN_SOFT_TIMESTAMP(1), 767 + }; 768 + 468 769 static const struct iio_info vcnl4000_info = { 469 770 .read_raw = vcnl4000_read_raw, 470 771 }; 772 + 773 + static const struct iio_info vcnl4010_info = { 774 + .read_raw = vcnl4010_read_raw, 775 + .read_avail = vcnl4010_read_avail, 776 + .write_raw = vcnl4010_write_raw, 777 + .read_event_value = vcnl4010_read_event, 778 + .write_event_value = vcnl4010_write_event, 779 + .read_event_config = vcnl4010_read_event_config, 780 + .write_event_config = vcnl4010_write_event_config, 781 + }; 782 + 783 + static const struct vcnl4000_chip_spec vcnl4000_chip_spec_cfg[] = { 784 + [VCNL4000] = { 785 + .prod = "VCNL4000", 786 + .init = vcnl4000_init, 787 + .measure_light = vcnl4000_measure_light, 788 + .measure_proximity = vcnl4000_measure_proximity, 789 + .set_power_state = vcnl4000_set_power_state, 790 + .channels = vcnl4000_channels, 791 + .num_channels = ARRAY_SIZE(vcnl4000_channels), 792 + .info = &vcnl4000_info, 793 + .irq_support = false, 794 + }, 795 + [VCNL4010] = { 796 + .prod = "VCNL4010/4020", 797 + .init = vcnl4000_init, 798 + .measure_light = vcnl4000_measure_light, 799 + .measure_proximity = vcnl4000_measure_proximity, 800 + .set_power_state = vcnl4000_set_power_state, 801 + .channels = vcnl4010_channels, 802 + .num_channels = ARRAY_SIZE(vcnl4010_channels), 803 + .info = &vcnl4010_info, 804 + .irq_support = true, 805 + }, 806 + [VCNL4040] = { 807 + .prod = "VCNL4040", 808 + .init = vcnl4200_init, 809 + .measure_light = vcnl4200_measure_light, 810 + .measure_proximity = vcnl4200_measure_proximity, 811 + .set_power_state = vcnl4200_set_power_state, 812 + .channels = vcnl4000_channels, 813 + .num_channels = ARRAY_SIZE(vcnl4000_channels), 814 + .info = &vcnl4000_info, 815 + .irq_support = false, 816 + }, 817 + [VCNL4200] = { 818 + .prod = "VCNL4200", 819 + .init = vcnl4200_init, 820 + .measure_light = vcnl4200_measure_light, 821 + .measure_proximity = vcnl4200_measure_proximity, 822 + .set_power_state = vcnl4200_set_power_state, 823 + .channels = vcnl4000_channels, 824 + .num_channels = ARRAY_SIZE(vcnl4000_channels), 825 + .info = &vcnl4000_info, 826 + .irq_support = false, 827 + }, 828 + }; 829 + 830 + static irqreturn_t vcnl4010_irq_thread(int irq, void *p) 831 + { 832 + struct iio_dev *indio_dev = p; 833 + struct vcnl4000_data *data = iio_priv(indio_dev); 834 + unsigned long isr; 835 + int ret; 836 + 837 + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR); 838 + if (ret < 0) 839 + goto end; 840 + 841 + isr = ret; 842 + 843 + if (isr & VCNL4010_INT_THR) { 844 + if (test_bit(VCNL4010_INT_THR_LOW, &isr)) { 845 + iio_push_event(indio_dev, 846 + IIO_UNMOD_EVENT_CODE( 847 + IIO_PROXIMITY, 848 + 1, 849 + IIO_EV_TYPE_THRESH, 850 + IIO_EV_DIR_FALLING), 851 + iio_get_time_ns(indio_dev)); 852 + } 853 + 854 + if (test_bit(VCNL4010_INT_THR_HIGH, &isr)) { 855 + iio_push_event(indio_dev, 856 + IIO_UNMOD_EVENT_CODE( 857 + IIO_PROXIMITY, 858 + 1, 859 + IIO_EV_TYPE_THRESH, 860 + IIO_EV_DIR_RISING), 861 + iio_get_time_ns(indio_dev)); 862 + } 863 + 864 + i2c_smbus_write_byte_data(data->client, VCNL4010_ISR, 865 + isr & VCNL4010_INT_THR); 866 + } 867 + 868 + if (isr & VCNL4010_INT_DRDY && iio_buffer_enabled(indio_dev)) 869 + iio_trigger_poll_chained(indio_dev->trig); 870 + 871 + end: 872 + return IRQ_HANDLED; 873 + } 874 + 875 + static irqreturn_t vcnl4010_trigger_handler(int irq, void *p) 876 + { 877 + struct iio_poll_func *pf = p; 878 + struct iio_dev *indio_dev = pf->indio_dev; 879 + struct vcnl4000_data *data = iio_priv(indio_dev); 880 + const unsigned long *active_scan_mask = indio_dev->active_scan_mask; 881 + u16 buffer[8] = {0}; /* 1x16-bit + ts */ 882 + bool data_read = false; 883 + unsigned long isr; 884 + int val = 0; 885 + int ret; 886 + 887 + ret = i2c_smbus_read_byte_data(data->client, VCNL4010_ISR); 888 + if (ret < 0) 889 + goto end; 890 + 891 + isr = ret; 892 + 893 + if (test_bit(0, active_scan_mask)) { 894 + if (test_bit(VCNL4010_INT_PROXIMITY, &isr)) { 895 + ret = vcnl4000_read_data(data, 896 + VCNL4000_PS_RESULT_HI, 897 + &val); 898 + if (ret < 0) 899 + goto end; 900 + 901 + buffer[0] = val; 902 + data_read = true; 903 + } 904 + } 905 + 906 + ret = i2c_smbus_write_byte_data(data->client, VCNL4010_ISR, 907 + isr & VCNL4010_INT_DRDY); 908 + if (ret < 0) 909 + goto end; 910 + 911 + if (!data_read) 912 + goto end; 913 + 914 + iio_push_to_buffers_with_timestamp(indio_dev, buffer, 915 + iio_get_time_ns(indio_dev)); 916 + 917 + end: 918 + iio_trigger_notify_done(indio_dev->trig); 919 + return IRQ_HANDLED; 920 + } 921 + 922 + static int vcnl4010_buffer_postenable(struct iio_dev *indio_dev) 923 + { 924 + struct vcnl4000_data *data = iio_priv(indio_dev); 925 + int ret; 926 + int cmd; 927 + 928 + ret = iio_triggered_buffer_postenable(indio_dev); 929 + if (ret) 930 + return ret; 931 + 932 + /* Do not enable the buffer if we are already capturing events. */ 933 + if (vcnl4010_is_in_periodic_mode(data)) { 934 + ret = -EBUSY; 935 + goto end; 936 + } 937 + 938 + ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 939 + VCNL4010_INT_PROX_EN); 940 + if (ret < 0) 941 + goto end; 942 + 943 + cmd = VCNL4000_SELF_TIMED_EN | VCNL4000_PROX_EN; 944 + ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, cmd); 945 + if (ret < 0) 946 + goto end; 947 + 948 + return 0; 949 + end: 950 + iio_triggered_buffer_predisable(indio_dev); 951 + 952 + return ret; 953 + } 954 + 955 + static int vcnl4010_buffer_predisable(struct iio_dev *indio_dev) 956 + { 957 + struct vcnl4000_data *data = iio_priv(indio_dev); 958 + int ret, ret_disable; 959 + 960 + ret = i2c_smbus_write_byte_data(data->client, VCNL4010_INT_CTRL, 0); 961 + if (ret < 0) 962 + goto end; 963 + 964 + ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 0); 965 + 966 + end: 967 + ret_disable = iio_triggered_buffer_predisable(indio_dev); 968 + if (ret == 0) 969 + ret = ret_disable; 970 + 971 + return ret; 972 + } 973 + 974 + static const struct iio_buffer_setup_ops vcnl4010_buffer_ops = { 975 + .postenable = &vcnl4010_buffer_postenable, 976 + .predisable = &vcnl4010_buffer_predisable, 977 + }; 978 + 979 + static const struct iio_trigger_ops vcnl4010_trigger_ops = { 980 + .validate_device = iio_trigger_validate_own_device, 981 + }; 982 + 983 + static int vcnl4010_probe_trigger(struct iio_dev *indio_dev) 984 + { 985 + struct vcnl4000_data *data = iio_priv(indio_dev); 986 + struct i2c_client *client = data->client; 987 + struct iio_trigger *trigger; 988 + 989 + trigger = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", 990 + indio_dev->name, indio_dev->id); 991 + if (!trigger) 992 + return -ENOMEM; 993 + 994 + trigger->dev.parent = &client->dev; 995 + trigger->ops = &vcnl4010_trigger_ops; 996 + iio_trigger_set_drvdata(trigger, indio_dev); 997 + 998 + return devm_iio_trigger_register(&client->dev, trigger); 999 + } 471 1000 472 1001 static int vcnl4000_probe(struct i2c_client *client, 473 1002 const struct i2c_device_id *id) ··· 1059 466 data->near_level = 0; 1060 467 1061 468 indio_dev->dev.parent = &client->dev; 1062 - indio_dev->info = &vcnl4000_info; 1063 - indio_dev->channels = vcnl4000_channels; 1064 - indio_dev->num_channels = ARRAY_SIZE(vcnl4000_channels); 469 + indio_dev->info = data->chip_spec->info; 470 + indio_dev->channels = data->chip_spec->channels; 471 + indio_dev->num_channels = data->chip_spec->num_channels; 1065 472 indio_dev->name = VCNL4000_DRV_NAME; 1066 473 indio_dev->modes = INDIO_DIRECT_MODE; 474 + 475 + if (client->irq && data->chip_spec->irq_support) { 476 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, 477 + NULL, 478 + vcnl4010_trigger_handler, 479 + &vcnl4010_buffer_ops); 480 + if (ret < 0) { 481 + dev_err(&client->dev, 482 + "unable to setup iio triggered buffer\n"); 483 + return ret; 484 + } 485 + 486 + ret = devm_request_threaded_irq(&client->dev, client->irq, 487 + NULL, vcnl4010_irq_thread, 488 + IRQF_TRIGGER_FALLING | 489 + IRQF_ONESHOT, 490 + "vcnl4010_irq", 491 + indio_dev); 492 + if (ret < 0) { 493 + dev_err(&client->dev, "irq request failed\n"); 494 + return ret; 495 + } 496 + 497 + ret = vcnl4010_probe_trigger(indio_dev); 498 + if (ret < 0) 499 + return ret; 500 + } 1067 501 1068 502 ret = pm_runtime_set_active(&client->dev); 1069 503 if (ret < 0) ··· 1185 565 module_i2c_driver(vcnl4000_driver); 1186 566 1187 567 MODULE_AUTHOR("Peter Meerwald <pmeerw@pmeerw.net>"); 568 + MODULE_AUTHOR("Mathieu Othacehe <m.othacehe@gmail.com>"); 1188 569 MODULE_DESCRIPTION("Vishay VCNL4000 proximity/ambient light sensor driver"); 1189 570 MODULE_LICENSE("GPL");
+2 -1
drivers/iio/light/vl6180.c
··· 16 16 */ 17 17 18 18 #include <linux/module.h> 19 + #include <linux/mod_devicetable.h> 19 20 #include <linux/i2c.h> 20 21 #include <linux/mutex.h> 21 22 #include <linux/err.h> ··· 538 537 static struct i2c_driver vl6180_driver = { 539 538 .driver = { 540 539 .name = VL6180_DRV_NAME, 541 - .of_match_table = of_match_ptr(vl6180_of_match), 540 + .of_match_table = vl6180_of_match, 542 541 }, 543 542 .probe = vl6180_probe, 544 543 .id_table = vl6180_id,
+3 -1
drivers/iio/light/zopt2201.c
··· 19 19 #include <linux/iio/iio.h> 20 20 #include <linux/iio/sysfs.h> 21 21 22 + #include <asm/unaligned.h> 23 + 22 24 #define ZOPT2201_DRV_NAME "zopt2201" 23 25 24 26 /* Registers */ ··· 221 219 goto fail; 222 220 mutex_unlock(&data->lock); 223 221 224 - return (buf[2] << 16) | (buf[1] << 8) | buf[0]; 222 + return get_unaligned_le24(&buf[0]); 225 223 226 224 fail: 227 225 mutex_unlock(&data->lock);
+151 -43
drivers/iio/magnetometer/ak8974.c
··· 49 49 #define AK8974_WHOAMI_VALUE_AMI306 0x46 50 50 #define AK8974_WHOAMI_VALUE_AMI305 0x47 51 51 #define AK8974_WHOAMI_VALUE_AK8974 0x48 52 + #define AK8974_WHOAMI_VALUE_HSCDTD008A 0x49 52 53 53 54 #define AK8974_DATA_X 0x10 54 55 #define AK8974_DATA_Y 0x12 ··· 140 139 #define AK8974_INT_CTRL_POL BIT(3) /* 0 = active low; 1 = active high */ 141 140 #define AK8974_INT_CTRL_PULSE BIT(1) /* 0 = latched; 1 = pulse (50 usec) */ 142 141 #define AK8974_INT_CTRL_RESDEF (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL) 142 + 143 + /* HSCDTD008A-specific control register */ 144 + #define HSCDTD008A_CTRL4 0x1E 145 + #define HSCDTD008A_CTRL4_MMD BIT(7) /* must be set to 1 */ 146 + #define HSCDTD008A_CTRL4_RANGE BIT(4) /* 0 = 14-bit output; 1 = 15-bit output */ 147 + #define HSCDTD008A_CTRL4_RESDEF (HSCDTD008A_CTRL4_MMD | HSCDTD008A_CTRL4_RANGE) 143 148 144 149 /* The AMI305 has elaborate FW version and serial number registers */ 145 150 #define AMI305_VER 0xE8 ··· 248 241 ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF); 249 242 if (ret) 250 243 return ret; 251 - ret = regmap_write(ak8974->map, AK8974_INT_CTRL, 252 - AK8974_INT_CTRL_RESDEF); 253 - if (ret) 254 - return ret; 244 + if (ak8974->variant != AK8974_WHOAMI_VALUE_HSCDTD008A) { 245 + ret = regmap_write(ak8974->map, AK8974_INT_CTRL, 246 + AK8974_INT_CTRL_RESDEF); 247 + if (ret) 248 + return ret; 249 + } else { 250 + ret = regmap_write(ak8974->map, HSCDTD008A_CTRL4, 251 + HSCDTD008A_CTRL4_RESDEF); 252 + if (ret) 253 + return ret; 254 + } 255 255 256 256 /* After reset, power off is default state */ 257 257 return ak8974_set_power(ak8974, AK8974_PWR_OFF); ··· 281 267 if (ret) 282 268 return ret; 283 269 } 270 + if (ak8974->variant == AK8974_WHOAMI_VALUE_HSCDTD008A) 271 + return 0; 284 272 ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL); 285 273 if (ret) 286 274 return ret; ··· 511 495 name = "ak8974"; 512 496 dev_info(&ak8974->i2c->dev, "detected AK8974\n"); 513 497 break; 498 + case AK8974_WHOAMI_VALUE_HSCDTD008A: 499 + name = "hscdtd008a"; 500 + dev_info(&ak8974->i2c->dev, "detected hscdtd008a\n"); 501 + break; 514 502 default: 515 503 dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ", 516 504 whoami); ··· 554 534 return 0; 555 535 } 556 536 537 + static int ak8974_measure_channel(struct ak8974 *ak8974, unsigned long address, 538 + int *val) 539 + { 540 + __le16 hw_values[3]; 541 + int ret; 542 + 543 + pm_runtime_get_sync(&ak8974->i2c->dev); 544 + mutex_lock(&ak8974->lock); 545 + 546 + /* 547 + * We read all axes and discard all but one, for optimized 548 + * reading, use the triggered buffer. 549 + */ 550 + ret = ak8974_trigmeas(ak8974); 551 + if (ret) 552 + goto out_unlock; 553 + ret = ak8974_getresult(ak8974, hw_values); 554 + if (ret) 555 + goto out_unlock; 556 + /* 557 + * This explicit cast to (s16) is necessary as the measurement 558 + * is done in 2's complement with positive and negative values. 559 + * The follwing assignment to *val will then convert the signed 560 + * s16 value to a signed int value. 561 + */ 562 + *val = (s16)le16_to_cpu(hw_values[address]); 563 + out_unlock: 564 + mutex_unlock(&ak8974->lock); 565 + pm_runtime_mark_last_busy(&ak8974->i2c->dev); 566 + pm_runtime_put_autosuspend(&ak8974->i2c->dev); 567 + 568 + return ret; 569 + } 570 + 557 571 static int ak8974_read_raw(struct iio_dev *indio_dev, 558 572 struct iio_chan_spec const *chan, 559 573 int *val, int *val2, 560 574 long mask) 561 575 { 562 576 struct ak8974 *ak8974 = iio_priv(indio_dev); 563 - __le16 hw_values[3]; 564 - int ret = -EINVAL; 565 - 566 - pm_runtime_get_sync(&ak8974->i2c->dev); 567 - mutex_lock(&ak8974->lock); 577 + int ret; 568 578 569 579 switch (mask) { 570 580 case IIO_CHAN_INFO_RAW: 571 581 if (chan->address > 2) { 572 582 dev_err(&ak8974->i2c->dev, "faulty channel address\n"); 573 - ret = -EIO; 574 - goto out_unlock; 583 + return -EIO; 575 584 } 576 - ret = ak8974_trigmeas(ak8974); 585 + ret = ak8974_measure_channel(ak8974, chan->address, val); 577 586 if (ret) 578 - goto out_unlock; 579 - ret = ak8974_getresult(ak8974, hw_values); 580 - if (ret) 581 - goto out_unlock; 582 - 583 - /* 584 - * We read all axes and discard all but one, for optimized 585 - * reading, use the triggered buffer. 586 - */ 587 - *val = (s16)le16_to_cpu(hw_values[chan->address]); 588 - 589 - ret = IIO_VAL_INT; 587 + return ret; 588 + return IIO_VAL_INT; 589 + case IIO_CHAN_INFO_SCALE: 590 + switch (ak8974->variant) { 591 + case AK8974_WHOAMI_VALUE_AMI306: 592 + case AK8974_WHOAMI_VALUE_AMI305: 593 + /* 594 + * The datasheet for AMI305 and AMI306, page 6 595 + * specifies the range of the sensor to be 596 + * +/- 12 Gauss. 597 + */ 598 + *val = 12; 599 + /* 600 + * 12 bits are used, +/- 2^11 601 + * [ -2048 .. 2047 ] (manual page 20) 602 + * [ 0xf800 .. 0x07ff ] 603 + */ 604 + *val2 = 11; 605 + return IIO_VAL_FRACTIONAL_LOG2; 606 + case AK8974_WHOAMI_VALUE_HSCDTD008A: 607 + /* 608 + * The datasheet for HSCDTF008A, page 3 specifies the 609 + * range of the sensor as +/- 2.4 mT per axis, which 610 + * corresponds to +/- 2400 uT = +/- 24 Gauss. 611 + */ 612 + *val = 24; 613 + /* 614 + * 15 bits are used (set up in CTRL4), +/- 2^14 615 + * [ -16384 .. 16383 ] (manual page 24) 616 + * [ 0xc000 .. 0x3fff ] 617 + */ 618 + *val2 = 14; 619 + return IIO_VAL_FRACTIONAL_LOG2; 620 + default: 621 + /* GUESSING +/- 12 Gauss */ 622 + *val = 12; 623 + /* GUESSING 12 bits ADC +/- 2^11 */ 624 + *val2 = 11; 625 + return IIO_VAL_FRACTIONAL_LOG2; 626 + } 627 + break; 628 + default: 629 + /* Unknown request */ 630 + break; 590 631 } 591 632 592 - out_unlock: 593 - mutex_unlock(&ak8974->lock); 594 - pm_runtime_mark_last_busy(&ak8974->i2c->dev); 595 - pm_runtime_put_autosuspend(&ak8974->i2c->dev); 596 - 597 - return ret; 633 + return -EINVAL; 598 634 } 599 635 600 636 static void ak8974_fill_buffer(struct iio_dev *indio_dev) ··· 707 631 { }, 708 632 }; 709 633 710 - #define AK8974_AXIS_CHANNEL(axis, index) \ 634 + #define AK8974_AXIS_CHANNEL(axis, index, bits) \ 711 635 { \ 712 636 .type = IIO_MAGN, \ 713 637 .modified = 1, \ 714 638 .channel2 = IIO_MOD_##axis, \ 715 - .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 639 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 640 + BIT(IIO_CHAN_INFO_SCALE), \ 716 641 .ext_info = ak8974_ext_info, \ 717 642 .address = index, \ 718 643 .scan_index = index, \ 719 644 .scan_type = { \ 720 645 .sign = 's', \ 721 - .realbits = 16, \ 646 + .realbits = bits, \ 722 647 .storagebits = 16, \ 723 648 .endianness = IIO_LE \ 724 649 }, \ 725 650 } 726 651 727 - static const struct iio_chan_spec ak8974_channels[] = { 728 - AK8974_AXIS_CHANNEL(X, 0), 729 - AK8974_AXIS_CHANNEL(Y, 1), 730 - AK8974_AXIS_CHANNEL(Z, 2), 652 + /* 653 + * We have no datasheet for the AK8974 but we guess that its 654 + * ADC is 12 bits. The AMI305 and AMI306 certainly has 12bit 655 + * ADC. 656 + */ 657 + static const struct iio_chan_spec ak8974_12_bits_channels[] = { 658 + AK8974_AXIS_CHANNEL(X, 0, 12), 659 + AK8974_AXIS_CHANNEL(Y, 1, 12), 660 + AK8974_AXIS_CHANNEL(Z, 2, 12), 661 + IIO_CHAN_SOFT_TIMESTAMP(3), 662 + }; 663 + 664 + /* 665 + * The HSCDTD008A has 15 bits resolution the way we set it up 666 + * in CTRL4. 667 + */ 668 + static const struct iio_chan_spec ak8974_15_bits_channels[] = { 669 + AK8974_AXIS_CHANNEL(X, 0, 15), 670 + AK8974_AXIS_CHANNEL(Y, 1, 15), 671 + AK8974_AXIS_CHANNEL(Z, 2, 15), 731 672 IIO_CHAN_SOFT_TIMESTAMP(3), 732 673 }; 733 674 ··· 767 674 case AK8974_INT_CTRL: 768 675 case AK8974_INT_THRES: 769 676 case AK8974_INT_THRES + 1: 677 + return true; 770 678 case AK8974_PRESET: 771 679 case AK8974_PRESET + 1: 772 - return true; 680 + return ak8974->variant != AK8974_WHOAMI_VALUE_HSCDTD008A; 773 681 case AK8974_OFFSET_X: 774 682 case AK8974_OFFSET_X + 1: 775 683 case AK8974_OFFSET_Y: 776 684 case AK8974_OFFSET_Y + 1: 777 685 case AK8974_OFFSET_Z: 778 686 case AK8974_OFFSET_Z + 1: 779 - if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974) 780 - return true; 781 - return false; 687 + return ak8974->variant == AK8974_WHOAMI_VALUE_AK8974 || 688 + ak8974->variant == AK8974_WHOAMI_VALUE_HSCDTD008A; 782 689 case AMI305_OFFSET_X: 783 690 case AMI305_OFFSET_X + 1: 784 691 case AMI305_OFFSET_Y: ··· 893 800 pm_runtime_put(&i2c->dev); 894 801 895 802 indio_dev->dev.parent = &i2c->dev; 896 - indio_dev->channels = ak8974_channels; 897 - indio_dev->num_channels = ARRAY_SIZE(ak8974_channels); 803 + switch (ak8974->variant) { 804 + case AK8974_WHOAMI_VALUE_AMI306: 805 + case AK8974_WHOAMI_VALUE_AMI305: 806 + indio_dev->channels = ak8974_12_bits_channels; 807 + indio_dev->num_channels = ARRAY_SIZE(ak8974_12_bits_channels); 808 + break; 809 + case AK8974_WHOAMI_VALUE_HSCDTD008A: 810 + indio_dev->channels = ak8974_15_bits_channels; 811 + indio_dev->num_channels = ARRAY_SIZE(ak8974_15_bits_channels); 812 + break; 813 + default: 814 + indio_dev->channels = ak8974_12_bits_channels; 815 + indio_dev->num_channels = ARRAY_SIZE(ak8974_12_bits_channels); 816 + break; 817 + } 898 818 indio_dev->info = &ak8974_info; 899 819 indio_dev->available_scan_masks = ak8974_scan_masks; 900 820 indio_dev->modes = INDIO_DIRECT_MODE; ··· 1037 931 {"ami305", 0 }, 1038 932 {"ami306", 0 }, 1039 933 {"ak8974", 0 }, 934 + {"hscdtd008a", 0 }, 1040 935 {} 1041 936 }; 1042 937 MODULE_DEVICE_TABLE(i2c, ak8974_id); 1043 938 1044 939 static const struct of_device_id ak8974_of_match[] = { 1045 940 { .compatible = "asahi-kasei,ak8974", }, 941 + { .compatible = "alps,hscdtd008a", }, 1046 942 {} 1047 943 }; 1048 944 MODULE_DEVICE_TABLE(of, ak8974_of_match);
+4 -14
drivers/iio/magnetometer/hid-sensor-magn-3d.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 enum magn_3d_channel { ··· 517 519 indio_dev->name = name; 518 520 indio_dev->modes = INDIO_DIRECT_MODE; 519 521 520 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 521 - NULL, NULL); 522 - if (ret) { 523 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 524 - return ret; 525 - } 526 522 atomic_set(&magn_state->magn_flux_attributes.data_ready, 0); 523 + 527 524 ret = hid_sensor_setup_trigger(indio_dev, name, 528 525 &magn_state->magn_flux_attributes); 529 526 if (ret < 0) { 530 527 dev_err(&pdev->dev, "trigger setup failed\n"); 531 - goto error_unreg_buffer_funcs; 528 + return ret; 532 529 } 533 530 534 531 ret = iio_device_register(indio_dev); ··· 547 554 error_iio_unreg: 548 555 iio_device_unregister(indio_dev); 549 556 error_remove_trigger: 550 - hid_sensor_remove_trigger(&magn_state->magn_flux_attributes); 551 - error_unreg_buffer_funcs: 552 - iio_triggered_buffer_cleanup(indio_dev); 557 + hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 553 558 return ret; 554 559 } 555 560 ··· 560 569 561 570 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_COMPASS_3D); 562 571 iio_device_unregister(indio_dev); 563 - hid_sensor_remove_trigger(&magn_state->magn_flux_attributes); 564 - iio_triggered_buffer_cleanup(indio_dev); 572 + hid_sensor_remove_trigger(indio_dev, &magn_state->magn_flux_attributes); 565 573 566 574 return 0; 567 575 }
+3 -2
drivers/iio/magnetometer/rm3100-core.c
··· 22 22 #include <linux/iio/triggered_buffer.h> 23 23 #include <linux/iio/trigger_consumer.h> 24 24 25 + #include <asm/unaligned.h> 26 + 25 27 #include "rm3100.h" 26 28 27 29 /* Cycle Count Registers. */ ··· 225 223 goto unlock_return; 226 224 mutex_unlock(&data->lock); 227 225 228 - *val = sign_extend32((buffer[0] << 16) | (buffer[1] << 8) | buffer[2], 229 - 23); 226 + *val = sign_extend32(get_unaligned_be24(&buffer[0]), 23); 230 227 231 228 return IIO_VAL_INT; 232 229
+4 -14
drivers/iio/orientation/hid-sensor-incl-3d.c
··· 15 15 #include <linux/iio/iio.h> 16 16 #include <linux/iio/sysfs.h> 17 17 #include <linux/iio/buffer.h> 18 - #include <linux/iio/trigger_consumer.h> 19 - #include <linux/iio/triggered_buffer.h> 20 18 #include "../common/hid-sensors/hid-sensor-trigger.h" 21 19 22 20 enum incl_3d_channel { ··· 344 346 indio_dev->name = name; 345 347 indio_dev->modes = INDIO_DIRECT_MODE; 346 348 347 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 348 - NULL, NULL); 349 - if (ret) { 350 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 351 - goto error_free_dev_mem; 352 - } 353 349 atomic_set(&incl_state->common_attributes.data_ready, 0); 350 + 354 351 ret = hid_sensor_setup_trigger(indio_dev, name, 355 352 &incl_state->common_attributes); 356 353 if (ret) { 357 354 dev_err(&pdev->dev, "trigger setup failed\n"); 358 - goto error_unreg_buffer_funcs; 355 + goto error_free_dev_mem; 359 356 } 360 357 361 358 ret = iio_device_register(indio_dev); ··· 375 382 error_iio_unreg: 376 383 iio_device_unregister(indio_dev); 377 384 error_remove_trigger: 378 - hid_sensor_remove_trigger(&incl_state->common_attributes); 379 - error_unreg_buffer_funcs: 380 - iio_triggered_buffer_cleanup(indio_dev); 385 + hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes); 381 386 error_free_dev_mem: 382 387 kfree(indio_dev->channels); 383 388 return ret; ··· 390 399 391 400 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_INCLINOMETER_3D); 392 401 iio_device_unregister(indio_dev); 393 - hid_sensor_remove_trigger(&incl_state->common_attributes); 394 - iio_triggered_buffer_cleanup(indio_dev); 402 + hid_sensor_remove_trigger(indio_dev, &incl_state->common_attributes); 395 403 kfree(indio_dev->channels); 396 404 397 405 return 0;
+4 -14
drivers/iio/orientation/hid-sensor-rotation.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 struct dev_rot_state { ··· 286 288 indio_dev->name = name; 287 289 indio_dev->modes = INDIO_DIRECT_MODE; 288 290 289 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 290 - NULL, NULL); 291 - if (ret) { 292 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 293 - return ret; 294 - } 295 291 atomic_set(&rot_state->common_attributes.data_ready, 0); 292 + 296 293 ret = hid_sensor_setup_trigger(indio_dev, name, 297 294 &rot_state->common_attributes); 298 295 if (ret) { 299 296 dev_err(&pdev->dev, "trigger setup failed\n"); 300 - goto error_unreg_buffer_funcs; 297 + return ret; 301 298 } 302 299 303 300 ret = iio_device_register(indio_dev); ··· 316 323 error_iio_unreg: 317 324 iio_device_unregister(indio_dev); 318 325 error_remove_trigger: 319 - hid_sensor_remove_trigger(&rot_state->common_attributes); 320 - error_unreg_buffer_funcs: 321 - iio_triggered_buffer_cleanup(indio_dev); 326 + hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); 322 327 return ret; 323 328 } 324 329 ··· 329 338 330 339 sensor_hub_remove_callback(hsdev, hsdev->usage); 331 340 iio_device_unregister(indio_dev); 332 - hid_sensor_remove_trigger(&rot_state->common_attributes); 333 - iio_triggered_buffer_cleanup(indio_dev); 341 + hid_sensor_remove_trigger(indio_dev, &rot_state->common_attributes); 334 342 335 343 return 0; 336 344 }
+2
drivers/iio/pressure/bmp280-core.c
··· 271 271 + (s32)2097152) * calib->H2 + 8192) >> 14); 272 272 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4; 273 273 274 + var = clamp_val(var, 0, 419430400); 275 + 274 276 return var >> 12; 275 277 }; 276 278
+4 -14
drivers/iio/pressure/hid-sensor-press.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/iio/buffer.h> 17 - #include <linux/iio/trigger_consumer.h> 18 - #include <linux/iio/triggered_buffer.h> 19 17 #include "../common/hid-sensors/hid-sensor-trigger.h" 20 18 21 19 #define CHANNEL_SCAN_INDEX_PRESSURE 0 ··· 288 290 indio_dev->name = name; 289 291 indio_dev->modes = INDIO_DIRECT_MODE; 290 292 291 - ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, 292 - NULL, NULL); 293 - if (ret) { 294 - dev_err(&pdev->dev, "failed to initialize trigger buffer\n"); 295 - goto error_free_dev_mem; 296 - } 297 293 atomic_set(&press_state->common_attributes.data_ready, 0); 294 + 298 295 ret = hid_sensor_setup_trigger(indio_dev, name, 299 296 &press_state->common_attributes); 300 297 if (ret) { 301 298 dev_err(&pdev->dev, "trigger setup failed\n"); 302 - goto error_unreg_buffer_funcs; 299 + goto error_free_dev_mem; 303 300 } 304 301 305 302 ret = iio_device_register(indio_dev); ··· 318 325 error_iio_unreg: 319 326 iio_device_unregister(indio_dev); 320 327 error_remove_trigger: 321 - hid_sensor_remove_trigger(&press_state->common_attributes); 322 - error_unreg_buffer_funcs: 323 - iio_triggered_buffer_cleanup(indio_dev); 328 + hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes); 324 329 error_free_dev_mem: 325 330 kfree(indio_dev->channels); 326 331 return ret; ··· 333 342 334 343 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_PRESSURE); 335 344 iio_device_unregister(indio_dev); 336 - hid_sensor_remove_trigger(&press_state->common_attributes); 337 - iio_triggered_buffer_cleanup(indio_dev); 345 + hid_sensor_remove_trigger(indio_dev, &press_state->common_attributes); 338 346 kfree(indio_dev->channels); 339 347 340 348 return 0;
+5 -3
drivers/iio/pressure/hp206c.c
··· 18 18 #include <linux/util_macros.h> 19 19 #include <linux/acpi.h> 20 20 21 + #include <asm/unaligned.h> 22 + 21 23 /* I2C commands: */ 22 24 #define HP206C_CMD_SOFT_RST 0x06 23 25 ··· 95 93 int ret; 96 94 u8 values[3]; 97 95 98 - ret = i2c_smbus_read_i2c_block_data(client, cmd, 3, values); 96 + ret = i2c_smbus_read_i2c_block_data(client, cmd, sizeof(values), values); 99 97 if (ret < 0) 100 98 return ret; 101 - if (ret != 3) 99 + if (ret != sizeof(values)) 102 100 return -EIO; 103 - return ((values[0] & 0xF) << 16) | (values[1] << 8) | (values[2]); 101 + return get_unaligned_be24(&values[0]) & GENMASK(19, 0); 104 102 } 105 103 106 104 /* Spin for max 160ms until DEV_RDY is 1, or return error. */
+3 -1
drivers/iio/pressure/ms5611_i2c.c
··· 16 16 #include <linux/module.h> 17 17 #include <linux/of_device.h> 18 18 19 + #include <asm/unaligned.h> 20 + 19 21 #include "ms5611.h" 20 22 21 23 static int ms5611_i2c_reset(struct device *dev) ··· 52 50 if (ret < 0) 53 51 return ret; 54 52 55 - *val = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 53 + *val = get_unaligned_be24(&buf[0]); 56 54 57 55 return 0; 58 56 }
+3 -1
drivers/iio/pressure/ms5611_spi.c
··· 11 11 #include <linux/spi/spi.h> 12 12 #include <linux/of_device.h> 13 13 14 + #include <asm/unaligned.h> 15 + 14 16 #include "ms5611.h" 15 17 16 18 static int ms5611_spi_reset(struct device *dev) ··· 47 45 if (ret < 0) 48 46 return ret; 49 47 50 - *val = (buf[0] << 16) | (buf[1] << 8) | buf[2]; 48 + *val = get_unaligned_be24(&buf[0]); 51 49 52 50 return 0; 53 51 }
+4 -5
drivers/iio/pressure/zpa2326.c
··· 64 64 #include <linux/iio/trigger.h> 65 65 #include <linux/iio/trigger_consumer.h> 66 66 #include <linux/iio/triggered_buffer.h> 67 + #include <asm/unaligned.h> 67 68 #include "zpa2326.h" 68 69 69 70 /* 200 ms should be enough for the longest conversion time in one-shot mode. */ ··· 1006 1005 struct regmap *regs = ((struct zpa2326_private *) 1007 1006 iio_priv(indio_dev))->regmap; 1008 1007 int err; 1008 + u8 v[3]; 1009 1009 1010 1010 switch (type) { 1011 1011 case IIO_PRESSURE: 1012 1012 zpa2326_dbg(indio_dev, "fetching raw pressure sample"); 1013 1013 1014 - err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value, 1015 - 3); 1014 + err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, v, sizeof(v)); 1016 1015 if (err) { 1017 1016 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)", 1018 1017 err); 1019 1018 return err; 1020 1019 } 1021 1020 1022 - /* Pressure is a 24 bits wide little-endian unsigned int. */ 1023 - *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) | 1024 - ((u8 *)value)[0]; 1021 + *value = get_unaligned_le24(&v[0]); 1025 1022 1026 1023 return IIO_VAL_INT; 1027 1024
+3 -9
drivers/iio/temperature/hid-sensor-temperature.c
··· 7 7 #include <linux/hid-sensor-hub.h> 8 8 #include <linux/iio/buffer.h> 9 9 #include <linux/iio/iio.h> 10 - #include <linux/iio/triggered_buffer.h> 11 - #include <linux/iio/trigger_consumer.h> 12 10 #include <linux/module.h> 13 11 #include <linux/platform_device.h> 14 12 ··· 228 230 indio_dev->name = name; 229 231 indio_dev->modes = INDIO_DIRECT_MODE; 230 232 231 - ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev, 232 - &iio_pollfunc_store_time, NULL, NULL); 233 - if (ret) 234 - return ret; 235 - 236 233 atomic_set(&temp_st->common_attributes.data_ready, 0); 234 + 237 235 ret = hid_sensor_setup_trigger(indio_dev, name, 238 236 &temp_st->common_attributes); 239 237 if (ret) ··· 252 258 error_remove_callback: 253 259 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE); 254 260 error_remove_trigger: 255 - hid_sensor_remove_trigger(&temp_st->common_attributes); 261 + hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes); 256 262 return ret; 257 263 } 258 264 ··· 264 270 struct temperature_state *temp_st = iio_priv(indio_dev); 265 271 266 272 sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_TEMPERATURE); 267 - hid_sensor_remove_trigger(&temp_st->common_attributes); 273 + hid_sensor_remove_trigger(indio_dev, &temp_st->common_attributes); 268 274 269 275 return 0; 270 276 }
+3 -2
drivers/iio/temperature/max31856.c
··· 14 14 #include <linux/iio/iio.h> 15 15 #include <linux/iio/sysfs.h> 16 16 #include <linux/util_macros.h> 17 + #include <asm/unaligned.h> 17 18 #include <dt-bindings/iio/temperature/thermocouple.h> 18 19 /* 19 20 * The MSB of the register value determines whether the following byte will ··· 169 168 if (ret) 170 169 return ret; 171 170 /* Skip last 5 dead bits of LTCBL */ 172 - *val = (reg_val[0] << 16 | reg_val[1] << 8 | reg_val[2]) >> 5; 171 + *val = get_unaligned_be24(&reg_val[0]) >> 5; 173 172 /* Check 7th bit of LTCBH reg. value for sign*/ 174 173 if (reg_val[0] & 0x80) 175 174 *val -= 0x80000; ··· 186 185 /* Get Cold Junction Temp. offset register value */ 187 186 offset_cjto = reg_val[0]; 188 187 /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */ 189 - *val = (reg_val[1] << 8 | reg_val[2]) >> 2; 188 + *val = get_unaligned_be16(&reg_val[1]) >> 2; 190 189 /* As per datasheet add offset into CJTH and CJTL */ 191 190 *val += offset_cjto; 192 191 /* Check 7th bit of CJTH reg. value for sign */
+37 -40
drivers/staging/iio/impedance-analyzer/ad5933.c
··· 602 602 .postdisable = ad5933_ring_postdisable, 603 603 }; 604 604 605 - static int ad5933_register_ring_funcs_and_init(struct iio_dev *indio_dev) 605 + static int ad5933_register_ring_funcs_and_init(struct device *dev, 606 + struct iio_dev *indio_dev) 606 607 { 607 608 struct iio_buffer *buffer; 608 609 609 - buffer = iio_kfifo_allocate(); 610 + buffer = devm_iio_kfifo_allocate(dev); 610 611 if (!buffer) 611 612 return -ENOMEM; 612 613 ··· 677 676 } 678 677 } 679 678 679 + static void ad5933_reg_disable(void *data) 680 + { 681 + struct ad5933_state *st = data; 682 + 683 + regulator_disable(st->reg); 684 + } 685 + 686 + static void ad5933_clk_disable(void *data) 687 + { 688 + struct ad5933_state *st = data; 689 + 690 + clk_disable_unprepare(st->mclk); 691 + } 692 + 680 693 static int ad5933_probe(struct i2c_client *client, 681 694 const struct i2c_device_id *id) 682 695 { ··· 718 703 dev_err(&client->dev, "Failed to enable specified VDD supply\n"); 719 704 return ret; 720 705 } 721 - ret = regulator_get_voltage(st->reg); 722 706 707 + ret = devm_add_action_or_reset(&client->dev, ad5933_reg_disable, st); 708 + if (ret) 709 + return ret; 710 + 711 + ret = regulator_get_voltage(st->reg); 723 712 if (ret < 0) 724 - goto error_disable_reg; 713 + return ret; 725 714 726 715 st->vref_mv = ret / 1000; 727 716 728 717 st->mclk = devm_clk_get(&client->dev, "mclk"); 729 - if (IS_ERR(st->mclk) && PTR_ERR(st->mclk) != -ENOENT) { 730 - ret = PTR_ERR(st->mclk); 731 - goto error_disable_reg; 732 - } 718 + if (IS_ERR(st->mclk) && PTR_ERR(st->mclk) != -ENOENT) 719 + return PTR_ERR(st->mclk); 733 720 734 721 if (!IS_ERR(st->mclk)) { 735 722 ret = clk_prepare_enable(st->mclk); 736 723 if (ret < 0) 737 - goto error_disable_reg; 724 + return ret; 725 + 726 + ret = devm_add_action_or_reset(&client->dev, 727 + ad5933_clk_disable, 728 + st); 729 + if (ret) 730 + return ret; 731 + 738 732 ext_clk_hz = clk_get_rate(st->mclk); 739 733 } 740 734 ··· 766 742 indio_dev->channels = ad5933_channels; 767 743 indio_dev->num_channels = ARRAY_SIZE(ad5933_channels); 768 744 769 - ret = ad5933_register_ring_funcs_and_init(indio_dev); 745 + ret = ad5933_register_ring_funcs_and_init(&client->dev, indio_dev); 770 746 if (ret) 771 - goto error_disable_mclk; 747 + return ret; 772 748 773 749 ret = ad5933_setup(st); 774 750 if (ret) 775 - goto error_unreg_ring; 751 + return ret; 776 752 777 - ret = iio_device_register(indio_dev); 778 - if (ret) 779 - goto error_unreg_ring; 780 - 781 - return 0; 782 - 783 - error_unreg_ring: 784 - iio_kfifo_free(indio_dev->buffer); 785 - error_disable_mclk: 786 - clk_disable_unprepare(st->mclk); 787 - error_disable_reg: 788 - regulator_disable(st->reg); 789 - 790 - return ret; 791 - } 792 - 793 - static int ad5933_remove(struct i2c_client *client) 794 - { 795 - struct iio_dev *indio_dev = i2c_get_clientdata(client); 796 - struct ad5933_state *st = iio_priv(indio_dev); 797 - 798 - iio_device_unregister(indio_dev); 799 - iio_kfifo_free(indio_dev->buffer); 800 - regulator_disable(st->reg); 801 - clk_disable_unprepare(st->mclk); 802 - 803 - return 0; 753 + return devm_iio_device_register(&client->dev, indio_dev); 804 754 } 805 755 806 756 static const struct i2c_device_id ad5933_id[] = { ··· 799 801 .of_match_table = ad5933_of_match, 800 802 }, 801 803 .probe = ad5933_probe, 802 - .remove = ad5933_remove, 803 804 .id_table = ad5933_id, 804 805 }; 805 806 module_i2c_driver(ad5933_driver);
+85 -2
include/linux/iio/imu/adis.h
··· 83 83 * @trig: IIO trigger object data 84 84 * @data: ADIS chip variant specific data 85 85 * @burst: ADIS burst transfer information 86 + * @burst_extra_len: Burst extra length. Should only be used by devices that can 87 + * dynamically change their burst mode length. 86 88 * @state_lock: Lock used by the device to protect state 87 89 * @msg: SPI message object 88 90 * @xfer: SPI transfer objects to be used for a @msg 89 91 * @current_page: Some ADIS devices have registers, this selects current page 92 + * @irq_flag: IRQ handling flags as passed to request_irq() 90 93 * @buffer: Data buffer for information read from the device 91 94 * @tx: DMA safe TX buffer for SPI transfers 92 95 * @rx: DMA safe RX buffer for SPI transfers ··· 100 97 101 98 const struct adis_data *data; 102 99 struct adis_burst *burst; 103 - 100 + unsigned int burst_extra_len; 104 101 /** 105 102 * The state_lock is meant to be used during operations that require 106 103 * a sequence of SPI R/W in order to protect the SPI transfer ··· 116 113 struct spi_message msg; 117 114 struct spi_transfer *xfer; 118 115 unsigned int current_page; 116 + unsigned long irq_flag; 119 117 void *buffer; 120 118 121 119 uint8_t tx[10] ____cacheline_aligned; ··· 335 331 return ret; 336 332 } 337 333 334 + int __adis_update_bits_base(struct adis *adis, unsigned int reg, const u32 mask, 335 + const u32 val, u8 size); 336 + /** 337 + * adis_update_bits_base() - ADIS Update bits function - Locked version 338 + * @adis: The adis device 339 + * @reg: The address of the lower of the two registers 340 + * @mask: Bitmask to change 341 + * @val: Value to be written 342 + * @size: Size of the register to update 343 + * 344 + * Updates the desired bits of @reg in accordance with @mask and @val. 345 + */ 346 + static inline int adis_update_bits_base(struct adis *adis, unsigned int reg, 347 + const u32 mask, const u32 val, u8 size) 348 + { 349 + int ret; 350 + 351 + mutex_lock(&adis->state_lock); 352 + ret = __adis_update_bits_base(adis, reg, mask, val, size); 353 + mutex_unlock(&adis->state_lock); 354 + return ret; 355 + } 356 + 357 + /** 358 + * adis_update_bits() - Wrapper macro for adis_update_bits_base - Locked version 359 + * @adis: The adis device 360 + * @reg: The address of the lower of the two registers 361 + * @mask: Bitmask to change 362 + * @val: Value to be written 363 + * 364 + * This macro evaluates the sizeof of @val at compile time and calls 365 + * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for 366 + * @val can lead to undesired behavior if the register to update is 16bit. 367 + */ 368 + #define adis_update_bits(adis, reg, mask, val) ({ \ 369 + BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8); \ 370 + __builtin_choose_expr(sizeof(val) == 4, \ 371 + adis_update_bits_base(adis, reg, mask, val, 4), \ 372 + adis_update_bits_base(adis, reg, mask, val, 2)); \ 373 + }) 374 + 375 + /** 376 + * adis_update_bits() - Wrapper macro for adis_update_bits_base 377 + * @adis: The adis device 378 + * @reg: The address of the lower of the two registers 379 + * @mask: Bitmask to change 380 + * @val: Value to be written 381 + * 382 + * This macro evaluates the sizeof of @val at compile time and calls 383 + * adis_update_bits_base() accordingly. Be aware that using MACROS/DEFINES for 384 + * @val can lead to undesired behavior if the register to update is 16bit. 385 + */ 386 + #define __adis_update_bits(adis, reg, mask, val) ({ \ 387 + BUILD_BUG_ON(sizeof(val) == 1 || sizeof(val) == 8); \ 388 + __builtin_choose_expr(sizeof(val) == 4, \ 389 + __adis_update_bits_base(adis, reg, mask, val, 4), \ 390 + __adis_update_bits_base(adis, reg, mask, val, 2)); \ 391 + }) 392 + 338 393 int adis_enable_irq(struct adis *adis, bool enable); 339 394 int __adis_check_status(struct adis *adis); 340 395 int __adis_initial_startup(struct adis *adis); ··· 504 441 * @en burst mode enabled 505 442 * @reg_cmd register command that triggers burst 506 443 * @extra_len extra length to account in the SPI RX buffer 444 + * @burst_max_len holds the maximum burst size when the device supports 445 + * more than one burst mode with different sizes 507 446 */ 508 447 struct adis_burst { 509 448 bool en; 510 449 unsigned int reg_cmd; 511 - unsigned int extra_len; 450 + const u32 extra_len; 451 + const u32 burst_max_len; 512 452 }; 513 453 454 + int 455 + devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev, 456 + irq_handler_t trigger_handler); 514 457 int adis_setup_buffer_and_trigger(struct adis *adis, 515 458 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *)); 516 459 void adis_cleanup_buffer_and_trigger(struct adis *adis, 517 460 struct iio_dev *indio_dev); 518 461 462 + int devm_adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev); 519 463 int adis_probe_trigger(struct adis *adis, struct iio_dev *indio_dev); 520 464 void adis_remove_trigger(struct adis *adis); 521 465 ··· 530 460 const unsigned long *scan_mask); 531 461 532 462 #else /* CONFIG_IIO_BUFFER */ 463 + 464 + static inline int 465 + devm_adis_setup_buffer_and_trigger(struct adis *adis, struct iio_dev *indio_dev, 466 + irq_handler_t trigger_handler) 467 + { 468 + return 0; 469 + } 533 470 534 471 static inline int adis_setup_buffer_and_trigger(struct adis *adis, 535 472 struct iio_dev *indio_dev, irqreturn_t (*trigger_handler)(int, void *)) ··· 547 470 static inline void adis_cleanup_buffer_and_trigger(struct adis *adis, 548 471 struct iio_dev *indio_dev) 549 472 { 473 + } 474 + 475 + static inline int devm_adis_probe_trigger(struct adis *adis, 476 + struct iio_dev *indio_dev) 477 + { 478 + return 0; 550 479 } 551 480 552 481 static inline int adis_probe_trigger(struct adis *adis,