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

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

Third set of new device support, cleanups and features for IIO in the 4.12 cycle

Somewhat dominated in patch numbers of last of the outreachy application
window related patches (they are still coming, despite window being closed
which is good to see!)

Good set of new drivers as well.

New device support
* ASPEED ADC
- new driver
* cpcap PMIC ADC
- new driver
* hid-humidity
- driver for HID compatible humidity sensors.
* ltc2497 ADC
- new driver
* mpu6050
- bring bindings up to date and add trivial support for 9250
* rockchip-saradc
- update bindings to cover rk3328
* vl6180 light, proximity and time of flight sensor.
- new driver

Features
* meson-saradc
- add calibration

Cleanup and minor fixes
* ad5504
- constify attribute_group structure
- drop casting of void *
* ad7150
- replace some shifts of 1 by BIT macro usage
* ad7152
- blank lines between function definitions
* ad7280a
- octal permissions.
* ad7606
- replace use of core mlock mutex with a local lock
* ad7746
- replace some shifts of 1 by BIT macro usage
- function parameter alignment
- drop some excessive brackets (introduced in last pull request)
* ad7753
- white space cleanup
* ad7754
- includes in alphabetical order and groupped appropriately.
- change from missuse of internal mlock mutex to using the buffer lock to
also protect values during frequency update.
* ad779x
- constify attribute_group structures
* ad9832
- octal permissions
* adis16060
- remove use of core mlock mutex in favour of adding a local
_spi_write_then_read which can use the local buffer protection lock.
- fix naming of above function.
* adis16203
- remove locking during reads of calibbias that doesn't protect anything
not protected elsewhere.
* adis16209
- remove unnecessary braces in single statement if
* adis16240
- remove unnecessary braces in single statement if
* adt7136
- drop excess blank lines and put some in between functions.
* ams-iaq
- replace comma with semi colon. Not actual bug, just unusual syntax.
* apds9960
- constify attribute group structure
* as3935
- constify attribute group structure
* bm1750
- constify attribute group structure
* cros_ec
- devm version of triggered buffer setup to simplify code.
* exynos
- drop casting of void *
* hdc100x
- constify attribute_group structure
* hid-accel
- fix wrong scale for newly introduced gravity sensor.
* hts221
- drop casting of void *
* hx711
- constify attribute_group structure
* imx7d_adc
- drop casting of void *
* lm35333
- constify attribute_group structure
* lsm6dsx
- drop casting of void *
- hold ODR configuration until enabling to avoid a race condition.
* max1027
- drop casting of void *
* max11100
- fix a comma where semicolon was intended (no actual bug, just odd)
* max1363
- constify attribute_group structure
* ms sensors
- drop casting of void *
* rockchip_saradc
- drop casting of void *
* sun4i-gpadc
- fix missing dependency on THERMAL or presence of stubs (issue only
introduced in pervious set)
- drop casting of void *
* tsl2x7x
- fix wrong standard deviation calc. Note these aren't actually used for
anything at the moment so bug didn't really matter.
- constify attribute group structure.
* vf610adc
- drop casting of void *
* vz89x
- replace comma with semicolon. Not actual bug, just odd syntax.
* zpa2326
- drop casting of void *

+2830 -158
+20
Documentation/devicetree/bindings/iio/adc/aspeed_adc.txt
··· 1 + Aspeed ADC 2 + 3 + This device is a 10-bit converter for 16 voltage channels. All inputs are 4 + single ended. 5 + 6 + Required properties: 7 + - compatible: Should be "aspeed,ast2400-adc" or "aspeed,ast2500-adc" 8 + - reg: memory window mapping address and length 9 + - clocks: Input clock used to derive the sample clock. Expected to be the 10 + SoC's APB clock. 11 + - #io-channel-cells: Must be set to <1> to indicate channels are selected 12 + by index. 13 + 14 + Example: 15 + adc@1e6e9000 { 16 + compatible = "aspeed,ast2400-adc"; 17 + reg = <0x1e6e9000 0xb0>; 18 + clocks = <&clk_apb>; 19 + #io-channel-cells = <1>; 20 + };
+18
Documentation/devicetree/bindings/iio/adc/cpcap-adc.txt
··· 1 + Motorola CPCAP PMIC ADC binding 2 + 3 + Required properties: 4 + - compatible: Should be "motorola,cpcap-adc" or "motorola,mapphone-cpcap-adc" 5 + - interrupt-parent: The interrupt controller 6 + - interrupts: The interrupt number for the ADC device 7 + - interrupt-names: Should be "adcdone" 8 + - #io-channel-cells: Number of cells in an IIO specifier 9 + 10 + Example: 11 + 12 + cpcap_adc: adc { 13 + compatible = "motorola,mapphone-cpcap-adc"; 14 + interrupt-parent = <&cpcap>; 15 + interrupts = <8 IRQ_TYPE_NONE>; 16 + interrupt-names = "adcdone"; 17 + #io-channel-cells = <1>; 18 + };
+13
Documentation/devicetree/bindings/iio/adc/ltc2497.txt
··· 1 + * Linear Technology / Analog Devices LTC2497 ADC 2 + 3 + Required properties: 4 + - compatible: Must be "lltc,ltc2497" 5 + - reg: Must contain the ADC I2C address 6 + - vref-supply: The regulator supply for ADC reference voltage 7 + 8 + Example: 9 + ltc2497: adc@76 { 10 + compatible = "lltc,ltc2497"; 11 + reg = <0x76>; 12 + vref-supply = <&ltc2497_reg>; 13 + };
+1
Documentation/devicetree/bindings/iio/adc/rockchip-saradc.txt
··· 4 4 - compatible: should be "rockchip,<name>-saradc" or "rockchip,rk3066-tsadc" 5 5 - "rockchip,saradc": for rk3188, rk3288 6 6 - "rockchip,rk3066-tsadc": for rk3036 7 + - "rockchip,rk3328-saradc", "rockchip,rk3399-saradc": for rk3328 7 8 - "rockchip,rk3399-saradc": for rk3399 8 9 9 10 - reg: physical base address of the controller and length of memory mapped
+25 -2
Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt
··· 3 3 http://www.invensense.com/mems/gyro/mpu6050.html 4 4 5 5 Required properties: 6 - - compatible : should be "invensense,mpu6050" 6 + - compatible : should be one of 7 + "invensense,mpu6050" 8 + "invensense,mpu6500" 9 + "invensense,mpu9150" 10 + "invensense,mpu9250" 11 + "invensense,icm20608" 7 12 - reg : the I2C address of the sensor 8 13 - interrupt-parent : should be the phandle for the interrupt controller 9 14 - interrupts : interrupt mapping for GPIO IRQ 10 15 11 16 Optional properties: 12 17 - mount-matrix: an optional 3x3 mounting rotation matrix 13 - 18 + - i2c-gate node. These devices also support an auxiliary i2c bus. This is 19 + simple enough to be described using the i2c-gate binding. See 20 + i2c/i2c-gate.txt for more details. 14 21 15 22 Example: 16 23 mpu6050@68 { ··· 34 27 "-0.173648177666930", /* x2 */ 35 28 "0", /* y2 */ 36 29 "0.984807753012208"; /* z2 */ 30 + }; 31 + 32 + 33 + mpu9250@68 { 34 + compatible = "invensense,mpu9250"; 35 + reg = <0x68>; 36 + interrupt-parent = <&gpio3>; 37 + interrupts = <21 1>; 38 + i2c-gate { 39 + #address-cells = <1>; 40 + #size-cells = <0>; 41 + ax8975@c { 42 + compatible = "ak,ak8975"; 43 + reg = <0x0c>; 44 + }; 45 + }; 37 46 };
+15
Documentation/devicetree/bindings/iio/light/vl6180.txt
··· 1 + STMicro VL6180 - ALS, range and proximity sensor 2 + 3 + Link to datasheet: http://www.st.com/resource/en/datasheet/vl6180x.pdf 4 + 5 + Required properties: 6 + 7 + -compatible: should be "st,vl6180" 8 + -reg: the I2C address of the sensor 9 + 10 + Example: 11 + 12 + vl6180@29 { 13 + compatible = "st,vl6180"; 14 + reg = <0x29>; 15 + };
+1
MAINTAINERS
··· 813 813 W: http://ez.analog.com/community/linux-device-drivers 814 814 S: Supported 815 815 F: drivers/iio/*/ad* 816 + F: drivers/iio/adc/ltc2497* 816 817 X: drivers/iio/*/adjd* 817 818 F: drivers/staging/iio/*/ad* 818 819 F: drivers/staging/iio/trigger/iio-trig-bfin-timer.c
+33
drivers/iio/adc/Kconfig
··· 130 130 To compile this driver as a module, choose M here: the module will be 131 131 called ad799x. 132 132 133 + config ASPEED_ADC 134 + tristate "Aspeed ADC" 135 + depends on ARCH_ASPEED || COMPILE_TEST 136 + depends on COMMON_CLK 137 + help 138 + If you say yes here you get support for the ADC included in Aspeed 139 + BMC SoCs. 140 + 141 + To compile this driver as a module, choose M here: the module will be 142 + called aspeed_adc. 143 + 133 144 config AT91_ADC 134 145 tristate "Atmel AT91 ADC" 135 146 depends on ARCH_AT91 ··· 205 194 206 195 This driver can also be built as a module. If so, the module will be 207 196 called cc10001_adc. 197 + 198 + config CPCAP_ADC 199 + tristate "Motorola CPCAP PMIC ADC driver" 200 + depends on MFD_CPCAP 201 + select IIO_BUFFER 202 + select IIO_TRIGGERED_BUFFER 203 + help 204 + Say yes here to build support for Motorola CPCAP PMIC ADC. 205 + 206 + This driver can also be built as a module. If so, the module will be 207 + called cpcap-adc. 208 208 209 209 config DA9150_GPADC 210 210 tristate "Dialog DA9150 GPADC driver support" ··· 347 325 348 326 To compile this driver as a module, choose M here: the module will be 349 327 called ltc2485. 328 + 329 + config LTC2497 330 + tristate "Linear Technology LTC2497 ADC driver" 331 + depends on I2C 332 + help 333 + Say yes here to build support for Linear Technology LTC2497 334 + 16-Bit 8-/16-Channel Delta Sigma ADC. 335 + 336 + To compile this driver as a module, choose M here: the module will be 337 + called ltc2497. 350 338 351 339 config MAX1027 352 340 tristate "Maxim max1027 ADC driver" ··· 595 563 tristate "Support for the Allwinner SoCs GPADC" 596 564 depends on IIO 597 565 depends on MFD_SUN4I_GPADC 566 + depends on THERMAL || !THERMAL_OF 598 567 help 599 568 Say yes here to build support for Allwinner (A10, A13 and A31) SoCs 600 569 GPADC. This ADC provides 4 channels which can be used as an ADC or as
+3
drivers/iio/adc/Makefile
··· 14 14 obj-$(CONFIG_AD7793) += ad7793.o 15 15 obj-$(CONFIG_AD7887) += ad7887.o 16 16 obj-$(CONFIG_AD799X) += ad799x.o 17 + obj-$(CONFIG_ASPEED_ADC) += aspeed_adc.o 17 18 obj-$(CONFIG_AT91_ADC) += at91_adc.o 18 19 obj-$(CONFIG_AT91_SAMA5D2_ADC) += at91-sama5d2_adc.o 19 20 obj-$(CONFIG_AXP288_ADC) += axp288_adc.o 20 21 obj-$(CONFIG_BCM_IPROC_ADC) += bcm_iproc_adc.o 21 22 obj-$(CONFIG_BERLIN2_ADC) += berlin2-adc.o 22 23 obj-$(CONFIG_CC10001_ADC) += cc10001_adc.o 24 + obj-$(CONFIG_CPCAP_ADC) += cpcap-adc.o 23 25 obj-$(CONFIG_DA9150_GPADC) += da9150-gpadc.o 24 26 obj-$(CONFIG_ENVELOPE_DETECTOR) += envelope-detector.o 25 27 obj-$(CONFIG_EXYNOS_ADC) += exynos_adc.o ··· 34 32 obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o 35 33 obj-$(CONFIG_LPC32XX_ADC) += lpc32xx_adc.o 36 34 obj-$(CONFIG_LTC2485) += ltc2485.o 35 + obj-$(CONFIG_LTC2497) += ltc2497.o 37 36 obj-$(CONFIG_MAX1027) += max1027.o 38 37 obj-$(CONFIG_MAX11100) += max11100.o 39 38 obj-$(CONFIG_MAX1363) += max1363.o
+1 -1
drivers/iio/adc/ad799x.c
··· 520 520 NULL, 521 521 }; 522 522 523 - static struct attribute_group ad799x_event_attrs_group = { 523 + static const struct attribute_group ad799x_event_attrs_group = { 524 524 .attrs = ad799x_event_attributes, 525 525 }; 526 526
+295
drivers/iio/adc/aspeed_adc.c
··· 1 + /* 2 + * Aspeed AST2400/2500 ADC 3 + * 4 + * Copyright (C) 2017 Google, Inc. 5 + * 6 + * This program is free software; you can redistribute it and/or modify it 7 + * under the terms and conditions of the GNU General Public License, 8 + * version 2, as published by the Free Software Foundation. 9 + * 10 + */ 11 + 12 + #include <linux/clk.h> 13 + #include <linux/clk-provider.h> 14 + #include <linux/err.h> 15 + #include <linux/errno.h> 16 + #include <linux/io.h> 17 + #include <linux/module.h> 18 + #include <linux/of_platform.h> 19 + #include <linux/platform_device.h> 20 + #include <linux/spinlock.h> 21 + #include <linux/types.h> 22 + 23 + #include <linux/iio/iio.h> 24 + #include <linux/iio/driver.h> 25 + 26 + #define ASPEED_RESOLUTION_BITS 10 27 + #define ASPEED_CLOCKS_PER_SAMPLE 12 28 + 29 + #define ASPEED_REG_ENGINE_CONTROL 0x00 30 + #define ASPEED_REG_INTERRUPT_CONTROL 0x04 31 + #define ASPEED_REG_VGA_DETECT_CONTROL 0x08 32 + #define ASPEED_REG_CLOCK_CONTROL 0x0C 33 + #define ASPEED_REG_MAX 0xC0 34 + 35 + #define ASPEED_OPERATION_MODE_POWER_DOWN (0x0 << 1) 36 + #define ASPEED_OPERATION_MODE_STANDBY (0x1 << 1) 37 + #define ASPEED_OPERATION_MODE_NORMAL (0x7 << 1) 38 + 39 + #define ASPEED_ENGINE_ENABLE BIT(0) 40 + 41 + struct aspeed_adc_model_data { 42 + const char *model_name; 43 + unsigned int min_sampling_rate; // Hz 44 + unsigned int max_sampling_rate; // Hz 45 + unsigned int vref_voltage; // mV 46 + }; 47 + 48 + struct aspeed_adc_data { 49 + struct device *dev; 50 + void __iomem *base; 51 + spinlock_t clk_lock; 52 + struct clk_hw *clk_prescaler; 53 + struct clk_hw *clk_scaler; 54 + }; 55 + 56 + #define ASPEED_CHAN(_idx, _data_reg_addr) { \ 57 + .type = IIO_VOLTAGE, \ 58 + .indexed = 1, \ 59 + .channel = (_idx), \ 60 + .address = (_data_reg_addr), \ 61 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 62 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 63 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 64 + } 65 + 66 + static const struct iio_chan_spec aspeed_adc_iio_channels[] = { 67 + ASPEED_CHAN(0, 0x10), 68 + ASPEED_CHAN(1, 0x12), 69 + ASPEED_CHAN(2, 0x14), 70 + ASPEED_CHAN(3, 0x16), 71 + ASPEED_CHAN(4, 0x18), 72 + ASPEED_CHAN(5, 0x1A), 73 + ASPEED_CHAN(6, 0x1C), 74 + ASPEED_CHAN(7, 0x1E), 75 + ASPEED_CHAN(8, 0x20), 76 + ASPEED_CHAN(9, 0x22), 77 + ASPEED_CHAN(10, 0x24), 78 + ASPEED_CHAN(11, 0x26), 79 + ASPEED_CHAN(12, 0x28), 80 + ASPEED_CHAN(13, 0x2A), 81 + ASPEED_CHAN(14, 0x2C), 82 + ASPEED_CHAN(15, 0x2E), 83 + }; 84 + 85 + static int aspeed_adc_read_raw(struct iio_dev *indio_dev, 86 + struct iio_chan_spec const *chan, 87 + int *val, int *val2, long mask) 88 + { 89 + struct aspeed_adc_data *data = iio_priv(indio_dev); 90 + const struct aspeed_adc_model_data *model_data = 91 + of_device_get_match_data(data->dev); 92 + 93 + switch (mask) { 94 + case IIO_CHAN_INFO_RAW: 95 + *val = readw(data->base + chan->address); 96 + return IIO_VAL_INT; 97 + 98 + case IIO_CHAN_INFO_SCALE: 99 + *val = model_data->vref_voltage; 100 + *val2 = ASPEED_RESOLUTION_BITS; 101 + return IIO_VAL_FRACTIONAL_LOG2; 102 + 103 + case IIO_CHAN_INFO_SAMP_FREQ: 104 + *val = clk_get_rate(data->clk_scaler->clk) / 105 + ASPEED_CLOCKS_PER_SAMPLE; 106 + return IIO_VAL_INT; 107 + 108 + default: 109 + return -EINVAL; 110 + } 111 + } 112 + 113 + static int aspeed_adc_write_raw(struct iio_dev *indio_dev, 114 + struct iio_chan_spec const *chan, 115 + int val, int val2, long mask) 116 + { 117 + struct aspeed_adc_data *data = iio_priv(indio_dev); 118 + const struct aspeed_adc_model_data *model_data = 119 + of_device_get_match_data(data->dev); 120 + 121 + switch (mask) { 122 + case IIO_CHAN_INFO_SAMP_FREQ: 123 + if (val < model_data->min_sampling_rate || 124 + val > model_data->max_sampling_rate) 125 + return -EINVAL; 126 + 127 + clk_set_rate(data->clk_scaler->clk, 128 + val * ASPEED_CLOCKS_PER_SAMPLE); 129 + return 0; 130 + 131 + case IIO_CHAN_INFO_SCALE: 132 + case IIO_CHAN_INFO_RAW: 133 + /* 134 + * Technically, these could be written but the only reasons 135 + * for doing so seem better handled in userspace. EPERM is 136 + * returned to signal this is a policy choice rather than a 137 + * hardware limitation. 138 + */ 139 + return -EPERM; 140 + 141 + default: 142 + return -EINVAL; 143 + } 144 + } 145 + 146 + static int aspeed_adc_reg_access(struct iio_dev *indio_dev, 147 + unsigned int reg, unsigned int writeval, 148 + unsigned int *readval) 149 + { 150 + struct aspeed_adc_data *data = iio_priv(indio_dev); 151 + 152 + if (!readval || reg % 4 || reg > ASPEED_REG_MAX) 153 + return -EINVAL; 154 + 155 + *readval = readl(data->base + reg); 156 + 157 + return 0; 158 + } 159 + 160 + static const struct iio_info aspeed_adc_iio_info = { 161 + .driver_module = THIS_MODULE, 162 + .read_raw = aspeed_adc_read_raw, 163 + .write_raw = aspeed_adc_write_raw, 164 + .debugfs_reg_access = aspeed_adc_reg_access, 165 + }; 166 + 167 + static int aspeed_adc_probe(struct platform_device *pdev) 168 + { 169 + struct iio_dev *indio_dev; 170 + struct aspeed_adc_data *data; 171 + const struct aspeed_adc_model_data *model_data; 172 + struct resource *res; 173 + const char *clk_parent_name; 174 + int ret; 175 + u32 adc_engine_control_reg_val; 176 + 177 + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*data)); 178 + if (!indio_dev) 179 + return -ENOMEM; 180 + 181 + data = iio_priv(indio_dev); 182 + data->dev = &pdev->dev; 183 + 184 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 185 + data->base = devm_ioremap_resource(&pdev->dev, res); 186 + if (IS_ERR(data->base)) 187 + return PTR_ERR(data->base); 188 + 189 + /* Register ADC clock prescaler with source specified by device tree. */ 190 + spin_lock_init(&data->clk_lock); 191 + clk_parent_name = of_clk_get_parent_name(pdev->dev.of_node, 0); 192 + 193 + data->clk_prescaler = clk_hw_register_divider( 194 + &pdev->dev, "prescaler", clk_parent_name, 0, 195 + data->base + ASPEED_REG_CLOCK_CONTROL, 196 + 17, 15, 0, &data->clk_lock); 197 + if (IS_ERR(data->clk_prescaler)) 198 + return PTR_ERR(data->clk_prescaler); 199 + 200 + /* 201 + * Register ADC clock scaler downstream from the prescaler. Allow rate 202 + * setting to adjust the prescaler as well. 203 + */ 204 + data->clk_scaler = clk_hw_register_divider( 205 + &pdev->dev, "scaler", "prescaler", 206 + CLK_SET_RATE_PARENT, 207 + data->base + ASPEED_REG_CLOCK_CONTROL, 208 + 0, 10, 0, &data->clk_lock); 209 + if (IS_ERR(data->clk_scaler)) { 210 + ret = PTR_ERR(data->clk_scaler); 211 + goto scaler_error; 212 + } 213 + 214 + /* Start all channels in normal mode. */ 215 + clk_prepare_enable(data->clk_scaler->clk); 216 + adc_engine_control_reg_val = GENMASK(31, 16) | 217 + ASPEED_OPERATION_MODE_NORMAL | ASPEED_ENGINE_ENABLE; 218 + writel(adc_engine_control_reg_val, 219 + data->base + ASPEED_REG_ENGINE_CONTROL); 220 + 221 + model_data = of_device_get_match_data(&pdev->dev); 222 + indio_dev->name = model_data->model_name; 223 + indio_dev->dev.parent = &pdev->dev; 224 + indio_dev->info = &aspeed_adc_iio_info; 225 + indio_dev->modes = INDIO_DIRECT_MODE; 226 + indio_dev->channels = aspeed_adc_iio_channels; 227 + indio_dev->num_channels = ARRAY_SIZE(aspeed_adc_iio_channels); 228 + 229 + ret = iio_device_register(indio_dev); 230 + if (ret) 231 + goto iio_register_error; 232 + 233 + return 0; 234 + 235 + iio_register_error: 236 + writel(ASPEED_OPERATION_MODE_POWER_DOWN, 237 + data->base + ASPEED_REG_ENGINE_CONTROL); 238 + clk_disable_unprepare(data->clk_scaler->clk); 239 + clk_hw_unregister_divider(data->clk_scaler); 240 + 241 + scaler_error: 242 + clk_hw_unregister_divider(data->clk_prescaler); 243 + return ret; 244 + } 245 + 246 + static int aspeed_adc_remove(struct platform_device *pdev) 247 + { 248 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 249 + struct aspeed_adc_data *data = iio_priv(indio_dev); 250 + 251 + iio_device_unregister(indio_dev); 252 + writel(ASPEED_OPERATION_MODE_POWER_DOWN, 253 + data->base + ASPEED_REG_ENGINE_CONTROL); 254 + clk_disable_unprepare(data->clk_scaler->clk); 255 + clk_hw_unregister_divider(data->clk_scaler); 256 + clk_hw_unregister_divider(data->clk_prescaler); 257 + 258 + return 0; 259 + } 260 + 261 + static const struct aspeed_adc_model_data ast2400_model_data = { 262 + .model_name = "ast2400-adc", 263 + .vref_voltage = 2500, // mV 264 + .min_sampling_rate = 10000, 265 + .max_sampling_rate = 500000, 266 + }; 267 + 268 + static const struct aspeed_adc_model_data ast2500_model_data = { 269 + .model_name = "ast2500-adc", 270 + .vref_voltage = 1800, // mV 271 + .min_sampling_rate = 1, 272 + .max_sampling_rate = 1000000, 273 + }; 274 + 275 + static const struct of_device_id aspeed_adc_matches[] = { 276 + { .compatible = "aspeed,ast2400-adc", .data = &ast2400_model_data }, 277 + { .compatible = "aspeed,ast2500-adc", .data = &ast2500_model_data }, 278 + {}, 279 + }; 280 + MODULE_DEVICE_TABLE(of, aspeed_adc_matches); 281 + 282 + static struct platform_driver aspeed_adc_driver = { 283 + .probe = aspeed_adc_probe, 284 + .remove = aspeed_adc_remove, 285 + .driver = { 286 + .name = KBUILD_MODNAME, 287 + .of_match_table = aspeed_adc_matches, 288 + } 289 + }; 290 + 291 + module_platform_driver(aspeed_adc_driver); 292 + 293 + MODULE_AUTHOR("Rick Altherr <raltherr@google.com>"); 294 + MODULE_DESCRIPTION("Aspeed AST2400/2500 ADC Driver"); 295 + MODULE_LICENSE("GPL");
+1007
drivers/iio/adc/cpcap-adc.c
··· 1 + /* 2 + * Copyright (C) 2017 Tony Lindgren <tony@atomide.com> 3 + * 4 + * Rewritten for Linux IIO framework with some code based on 5 + * earlier driver found in the Motorola Linux kernel: 6 + * 7 + * Copyright (C) 2009-2010 Motorola, Inc. 8 + * 9 + * This program is free software; you can redistribute it and/or modify 10 + * it under the terms of the GNU General Public License version 2 as 11 + * published by the Free Software Foundation. 12 + * 13 + * This program is distributed in the hope that it will be useful, 14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 + * GNU General Public License for more details. 17 + */ 18 + 19 + #include <linux/delay.h> 20 + #include <linux/device.h> 21 + #include <linux/err.h> 22 + #include <linux/init.h> 23 + #include <linux/interrupt.h> 24 + #include <linux/kernel.h> 25 + #include <linux/module.h> 26 + #include <linux/of.h> 27 + #include <linux/of_platform.h> 28 + #include <linux/platform_device.h> 29 + #include <linux/regmap.h> 30 + 31 + #include <linux/iio/buffer.h> 32 + #include <linux/iio/driver.h> 33 + #include <linux/iio/iio.h> 34 + #include <linux/iio/kfifo_buf.h> 35 + #include <linux/mfd/motorola-cpcap.h> 36 + 37 + /* Register CPCAP_REG_ADCC1 bits */ 38 + #define CPCAP_BIT_ADEN_AUTO_CLR BIT(15) /* Currently unused */ 39 + #define CPCAP_BIT_CAL_MODE BIT(14) /* Set with BIT_RAND0 */ 40 + #define CPCAP_BIT_ADC_CLK_SEL1 BIT(13) /* Currently unused */ 41 + #define CPCAP_BIT_ADC_CLK_SEL0 BIT(12) /* Currently unused */ 42 + #define CPCAP_BIT_ATOX BIT(11) 43 + #define CPCAP_BIT_ATO3 BIT(10) 44 + #define CPCAP_BIT_ATO2 BIT(9) 45 + #define CPCAP_BIT_ATO1 BIT(8) 46 + #define CPCAP_BIT_ATO0 BIT(7) 47 + #define CPCAP_BIT_ADA2 BIT(6) 48 + #define CPCAP_BIT_ADA1 BIT(5) 49 + #define CPCAP_BIT_ADA0 BIT(4) 50 + #define CPCAP_BIT_AD_SEL1 BIT(3) /* Set for bank1 */ 51 + #define CPCAP_BIT_RAND1 BIT(2) /* Set for channel 16 & 17 */ 52 + #define CPCAP_BIT_RAND0 BIT(1) /* Set with CAL_MODE */ 53 + #define CPCAP_BIT_ADEN BIT(0) /* Currently unused */ 54 + 55 + /* Register CPCAP_REG_ADCC2 bits */ 56 + #define CPCAP_BIT_CAL_FACTOR_ENABLE BIT(15) /* Currently unused */ 57 + #define CPCAP_BIT_BATDETB_EN BIT(14) /* Currently unused */ 58 + #define CPCAP_BIT_ADTRIG_ONESHOT BIT(13) /* Set for !TIMING_IMM */ 59 + #define CPCAP_BIT_ASC BIT(12) /* Set for TIMING_IMM */ 60 + #define CPCAP_BIT_ATOX_PS_FACTOR BIT(11) 61 + #define CPCAP_BIT_ADC_PS_FACTOR1 BIT(10) 62 + #define CPCAP_BIT_ADC_PS_FACTOR0 BIT(9) 63 + #define CPCAP_BIT_AD4_SELECT BIT(8) /* Currently unused */ 64 + #define CPCAP_BIT_ADC_BUSY BIT(7) /* Currently unused */ 65 + #define CPCAP_BIT_THERMBIAS_EN BIT(6) /* Currently unused */ 66 + #define CPCAP_BIT_ADTRIG_DIS BIT(5) /* Disable interrupt */ 67 + #define CPCAP_BIT_LIADC BIT(4) /* Currently unused */ 68 + #define CPCAP_BIT_TS_REFEN BIT(3) /* Currently unused */ 69 + #define CPCAP_BIT_TS_M2 BIT(2) /* Currently unused */ 70 + #define CPCAP_BIT_TS_M1 BIT(1) /* Currently unused */ 71 + #define CPCAP_BIT_TS_M0 BIT(0) /* Currently unused */ 72 + 73 + #define CPCAP_MAX_TEMP_LVL 27 74 + #define CPCAP_FOUR_POINT_TWO_ADC 801 75 + #define ST_ADC_CAL_CHRGI_HIGH_THRESHOLD 530 76 + #define ST_ADC_CAL_CHRGI_LOW_THRESHOLD 494 77 + #define ST_ADC_CAL_BATTI_HIGH_THRESHOLD 530 78 + #define ST_ADC_CAL_BATTI_LOW_THRESHOLD 494 79 + #define ST_ADC_CALIBRATE_DIFF_THRESHOLD 3 80 + 81 + #define CPCAP_ADC_MAX_RETRIES 5 /* Calibration and quirk */ 82 + 83 + /** 84 + * struct cpcap_adc_ato - timing settings for cpcap adc 85 + * 86 + * Unfortunately no cpcap documentation available, please document when 87 + * using these. 88 + */ 89 + struct cpcap_adc_ato { 90 + unsigned short ato_in; 91 + unsigned short atox_in; 92 + unsigned short adc_ps_factor_in; 93 + unsigned short atox_ps_factor_in; 94 + unsigned short ato_out; 95 + unsigned short atox_out; 96 + unsigned short adc_ps_factor_out; 97 + unsigned short atox_ps_factor_out; 98 + }; 99 + 100 + /** 101 + * struct cpcap-adc - cpcap adc device driver data 102 + * @reg: cpcap regmap 103 + * @dev: struct device 104 + * @vendor: cpcap vendor 105 + * @irq: interrupt 106 + * @lock: mutex 107 + * @ato: request timings 108 + * @wq_data_avail: work queue 109 + * @done: work done 110 + */ 111 + struct cpcap_adc { 112 + struct regmap *reg; 113 + struct device *dev; 114 + u16 vendor; 115 + int irq; 116 + struct mutex lock; /* ADC register access lock */ 117 + const struct cpcap_adc_ato *ato; 118 + wait_queue_head_t wq_data_avail; 119 + bool done; 120 + }; 121 + 122 + /** 123 + * enum cpcap_adc_channel - cpcap adc channels 124 + */ 125 + enum cpcap_adc_channel { 126 + /* Bank0 channels */ 127 + CPCAP_ADC_AD0_BATTDETB, /* Battery detection */ 128 + CPCAP_ADC_BATTP, /* Battery voltage */ 129 + CPCAP_ADC_VBUS, /* USB VBUS voltage */ 130 + CPCAP_ADC_AD3, /* Battery temperature when charging */ 131 + CPCAP_ADC_BPLUS_AD4, /* Another battery or system voltage */ 132 + CPCAP_ADC_CHG_ISENSE, /* Calibrated charge current */ 133 + CPCAP_ADC_BATTI, /* Calibrated system current */ 134 + CPCAP_ADC_USB_ID, /* USB OTG ID, unused on droid 4? */ 135 + 136 + /* Bank1 channels */ 137 + CPCAP_ADC_AD8, /* Seems unused */ 138 + CPCAP_ADC_AD9, /* Seems unused */ 139 + CPCAP_ADC_LICELL, /* Maybe system voltage? Always 3V */ 140 + CPCAP_ADC_HV_BATTP, /* Another battery detection? */ 141 + CPCAP_ADC_TSX1_AD12, /* Seems unused, for touchscreen? */ 142 + CPCAP_ADC_TSX2_AD13, /* Seems unused, for touchscreen? */ 143 + CPCAP_ADC_TSY1_AD14, /* Seems unused, for touchscreen? */ 144 + CPCAP_ADC_TSY2_AD15, /* Seems unused, for touchscreen? */ 145 + 146 + /* Remuxed channels using bank0 entries */ 147 + CPCAP_ADC_BATTP_PI16, /* Alternative mux mode for BATTP */ 148 + CPCAP_ADC_BATTI_PI17, /* Alternative mux mode for BATTI */ 149 + 150 + CPCAP_ADC_CHANNEL_NUM, 151 + }; 152 + 153 + /** 154 + * enum cpcap_adc_timing - cpcap adc timing options 155 + * 156 + * CPCAP_ADC_TIMING_IMM seems to be immediate with no timings. 157 + * Please document when using. 158 + */ 159 + enum cpcap_adc_timing { 160 + CPCAP_ADC_TIMING_IMM, 161 + CPCAP_ADC_TIMING_IN, 162 + CPCAP_ADC_TIMING_OUT, 163 + }; 164 + 165 + /** 166 + * struct cpcap_adc_phasing_tbl - cpcap phasing table 167 + * @offset: offset in the phasing table 168 + * @multiplier: multiplier in the phasing table 169 + * @divider: divider in the phasing table 170 + * @min: minimum value 171 + * @max: maximum value 172 + */ 173 + struct cpcap_adc_phasing_tbl { 174 + short offset; 175 + unsigned short multiplier; 176 + unsigned short divider; 177 + short min; 178 + short max; 179 + }; 180 + 181 + /** 182 + * struct cpcap_adc_conversion_tbl - cpcap conversion table 183 + * @conv_type: conversion type 184 + * @align_offset: align offset 185 + * @conv_offset: conversion offset 186 + * @cal_offset: calibration offset 187 + * @multiplier: conversion multiplier 188 + * @divider: conversion divider 189 + */ 190 + struct cpcap_adc_conversion_tbl { 191 + enum iio_chan_info_enum conv_type; 192 + int align_offset; 193 + int conv_offset; 194 + int cal_offset; 195 + int multiplier; 196 + int divider; 197 + }; 198 + 199 + /** 200 + * struct cpcap_adc_request - cpcap adc request 201 + * @channel: request channel 202 + * @phase_tbl: channel phasing table 203 + * @conv_tbl: channel conversion table 204 + * @bank_index: channel index within the bank 205 + * @timing: timing settings 206 + * @result: result 207 + */ 208 + struct cpcap_adc_request { 209 + int channel; 210 + const struct cpcap_adc_phasing_tbl *phase_tbl; 211 + const struct cpcap_adc_conversion_tbl *conv_tbl; 212 + int bank_index; 213 + enum cpcap_adc_timing timing; 214 + int result; 215 + }; 216 + 217 + /* Phasing table for channels. Note that channels 16 & 17 use BATTP and BATTI */ 218 + static const struct cpcap_adc_phasing_tbl bank_phasing[] = { 219 + /* Bank0 */ 220 + [CPCAP_ADC_AD0_BATTDETB] = {0, 0x80, 0x80, 0, 1023}, 221 + [CPCAP_ADC_BATTP] = {0, 0x80, 0x80, 0, 1023}, 222 + [CPCAP_ADC_VBUS] = {0, 0x80, 0x80, 0, 1023}, 223 + [CPCAP_ADC_AD3] = {0, 0x80, 0x80, 0, 1023}, 224 + [CPCAP_ADC_BPLUS_AD4] = {0, 0x80, 0x80, 0, 1023}, 225 + [CPCAP_ADC_CHG_ISENSE] = {0, 0x80, 0x80, -512, 511}, 226 + [CPCAP_ADC_BATTI] = {0, 0x80, 0x80, -512, 511}, 227 + [CPCAP_ADC_USB_ID] = {0, 0x80, 0x80, 0, 1023}, 228 + 229 + /* Bank1 */ 230 + [CPCAP_ADC_AD8] = {0, 0x80, 0x80, 0, 1023}, 231 + [CPCAP_ADC_AD9] = {0, 0x80, 0x80, 0, 1023}, 232 + [CPCAP_ADC_LICELL] = {0, 0x80, 0x80, 0, 1023}, 233 + [CPCAP_ADC_HV_BATTP] = {0, 0x80, 0x80, 0, 1023}, 234 + [CPCAP_ADC_TSX1_AD12] = {0, 0x80, 0x80, 0, 1023}, 235 + [CPCAP_ADC_TSX2_AD13] = {0, 0x80, 0x80, 0, 1023}, 236 + [CPCAP_ADC_TSY1_AD14] = {0, 0x80, 0x80, 0, 1023}, 237 + [CPCAP_ADC_TSY2_AD15] = {0, 0x80, 0x80, 0, 1023}, 238 + }; 239 + 240 + /* 241 + * Conversion table for channels. Updated during init based on calibration. 242 + * Here too channels 16 & 17 use BATTP and BATTI. 243 + */ 244 + static struct cpcap_adc_conversion_tbl bank_conversion[] = { 245 + /* Bank0 */ 246 + [CPCAP_ADC_AD0_BATTDETB] = { 247 + IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1, 248 + }, 249 + [CPCAP_ADC_BATTP] = { 250 + IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023, 251 + }, 252 + [CPCAP_ADC_VBUS] = { 253 + IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 10000, 1023, 254 + }, 255 + [CPCAP_ADC_AD3] = { 256 + IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 1, 1, 257 + }, 258 + [CPCAP_ADC_BPLUS_AD4] = { 259 + IIO_CHAN_INFO_PROCESSED, 0, 2400, 0, 2300, 1023, 260 + }, 261 + [CPCAP_ADC_CHG_ISENSE] = { 262 + IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023, 263 + }, 264 + [CPCAP_ADC_BATTI] = { 265 + IIO_CHAN_INFO_PROCESSED, -512, 2, 0, 5000, 1023, 266 + }, 267 + [CPCAP_ADC_USB_ID] = { 268 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 269 + }, 270 + 271 + /* Bank1 */ 272 + [CPCAP_ADC_AD8] = { 273 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 274 + }, 275 + [CPCAP_ADC_AD9] = { 276 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 277 + }, 278 + [CPCAP_ADC_LICELL] = { 279 + IIO_CHAN_INFO_PROCESSED, 0, 0, 0, 3400, 1023, 280 + }, 281 + [CPCAP_ADC_HV_BATTP] = { 282 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 283 + }, 284 + [CPCAP_ADC_TSX1_AD12] = { 285 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 286 + }, 287 + [CPCAP_ADC_TSX2_AD13] = { 288 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 289 + }, 290 + [CPCAP_ADC_TSY1_AD14] = { 291 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 292 + }, 293 + [CPCAP_ADC_TSY2_AD15] = { 294 + IIO_CHAN_INFO_RAW, 0, 0, 0, 1, 1, 295 + }, 296 + }; 297 + 298 + /* 299 + * Temperature lookup table of register values to milliCelcius. 300 + * REVISIT: Check the duplicate 0x3ff entry in a freezer 301 + */ 302 + static const int temp_map[CPCAP_MAX_TEMP_LVL][2] = { 303 + { 0x03ff, -40000 }, 304 + { 0x03ff, -35000 }, 305 + { 0x03ef, -30000 }, 306 + { 0x03b2, -25000 }, 307 + { 0x036c, -20000 }, 308 + { 0x0320, -15000 }, 309 + { 0x02d0, -10000 }, 310 + { 0x027f, -5000 }, 311 + { 0x022f, 0 }, 312 + { 0x01e4, 5000 }, 313 + { 0x019f, 10000 }, 314 + { 0x0161, 15000 }, 315 + { 0x012b, 20000 }, 316 + { 0x00fc, 25000 }, 317 + { 0x00d4, 30000 }, 318 + { 0x00b2, 35000 }, 319 + { 0x0095, 40000 }, 320 + { 0x007d, 45000 }, 321 + { 0x0069, 50000 }, 322 + { 0x0059, 55000 }, 323 + { 0x004b, 60000 }, 324 + { 0x003f, 65000 }, 325 + { 0x0036, 70000 }, 326 + { 0x002e, 75000 }, 327 + { 0x0027, 80000 }, 328 + { 0x0022, 85000 }, 329 + { 0x001d, 90000 }, 330 + }; 331 + 332 + #define CPCAP_CHAN(_type, _index, _address, _datasheet_name) { \ 333 + .type = (_type), \ 334 + .address = (_address), \ 335 + .indexed = 1, \ 336 + .channel = (_index), \ 337 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 338 + BIT(IIO_CHAN_INFO_PROCESSED), \ 339 + .scan_index = (_index), \ 340 + .scan_type = { \ 341 + .sign = 'u', \ 342 + .realbits = 10, \ 343 + .storagebits = 16, \ 344 + .endianness = IIO_CPU, \ 345 + }, \ 346 + .datasheet_name = (_datasheet_name), \ 347 + } 348 + 349 + /* 350 + * The datasheet names are from Motorola mapphone Linux kernel except 351 + * for the last two which might be uncalibrated charge voltage and 352 + * current. 353 + */ 354 + static const struct iio_chan_spec cpcap_adc_channels[] = { 355 + /* Bank0 */ 356 + CPCAP_CHAN(IIO_TEMP, 0, CPCAP_REG_ADCD0, "battdetb"), 357 + CPCAP_CHAN(IIO_VOLTAGE, 1, CPCAP_REG_ADCD1, "battp"), 358 + CPCAP_CHAN(IIO_VOLTAGE, 2, CPCAP_REG_ADCD2, "vbus"), 359 + CPCAP_CHAN(IIO_TEMP, 3, CPCAP_REG_ADCD3, "ad3"), 360 + CPCAP_CHAN(IIO_VOLTAGE, 4, CPCAP_REG_ADCD4, "ad4"), 361 + CPCAP_CHAN(IIO_CURRENT, 5, CPCAP_REG_ADCD5, "chg_isense"), 362 + CPCAP_CHAN(IIO_CURRENT, 6, CPCAP_REG_ADCD6, "batti"), 363 + CPCAP_CHAN(IIO_VOLTAGE, 7, CPCAP_REG_ADCD7, "usb_id"), 364 + 365 + /* Bank1 */ 366 + CPCAP_CHAN(IIO_CURRENT, 8, CPCAP_REG_ADCD0, "ad8"), 367 + CPCAP_CHAN(IIO_VOLTAGE, 9, CPCAP_REG_ADCD1, "ad9"), 368 + CPCAP_CHAN(IIO_VOLTAGE, 10, CPCAP_REG_ADCD2, "licell"), 369 + CPCAP_CHAN(IIO_VOLTAGE, 11, CPCAP_REG_ADCD3, "hv_battp"), 370 + CPCAP_CHAN(IIO_VOLTAGE, 12, CPCAP_REG_ADCD4, "tsx1_ad12"), 371 + CPCAP_CHAN(IIO_VOLTAGE, 13, CPCAP_REG_ADCD5, "tsx2_ad13"), 372 + CPCAP_CHAN(IIO_VOLTAGE, 14, CPCAP_REG_ADCD6, "tsy1_ad14"), 373 + CPCAP_CHAN(IIO_VOLTAGE, 15, CPCAP_REG_ADCD7, "tsy2_ad15"), 374 + 375 + /* There are two registers with multiplexed functionality */ 376 + CPCAP_CHAN(IIO_VOLTAGE, 16, CPCAP_REG_ADCD0, "chg_vsense"), 377 + CPCAP_CHAN(IIO_CURRENT, 17, CPCAP_REG_ADCD1, "batti2"), 378 + }; 379 + 380 + static irqreturn_t cpcap_adc_irq_thread(int irq, void *data) 381 + { 382 + struct iio_dev *indio_dev = data; 383 + struct cpcap_adc *ddata = iio_priv(indio_dev); 384 + int error; 385 + 386 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 387 + CPCAP_BIT_ADTRIG_DIS, 388 + CPCAP_BIT_ADTRIG_DIS); 389 + if (error) 390 + return IRQ_NONE; 391 + 392 + ddata->done = true; 393 + wake_up_interruptible(&ddata->wq_data_avail); 394 + 395 + return IRQ_HANDLED; 396 + } 397 + 398 + /* ADC calibration functions */ 399 + static void cpcap_adc_setup_calibrate(struct cpcap_adc *ddata, 400 + enum cpcap_adc_channel chan) 401 + { 402 + unsigned int value = 0; 403 + unsigned long timeout = jiffies + msecs_to_jiffies(3000); 404 + int error; 405 + 406 + if ((chan != CPCAP_ADC_CHG_ISENSE) && 407 + (chan != CPCAP_ADC_BATTI)) 408 + return; 409 + 410 + value |= CPCAP_BIT_CAL_MODE | CPCAP_BIT_RAND0; 411 + value |= ((chan << 4) & 412 + (CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | CPCAP_BIT_ADA0)); 413 + 414 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1, 415 + CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX | 416 + CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 | 417 + CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 | 418 + CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | 419 + CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 | 420 + CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0, 421 + value); 422 + if (error) 423 + return; 424 + 425 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 426 + CPCAP_BIT_ATOX_PS_FACTOR | 427 + CPCAP_BIT_ADC_PS_FACTOR1 | 428 + CPCAP_BIT_ADC_PS_FACTOR0, 429 + 0); 430 + if (error) 431 + return; 432 + 433 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 434 + CPCAP_BIT_ADTRIG_DIS, 435 + CPCAP_BIT_ADTRIG_DIS); 436 + if (error) 437 + return; 438 + 439 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 440 + CPCAP_BIT_ASC, 441 + CPCAP_BIT_ASC); 442 + if (error) 443 + return; 444 + 445 + do { 446 + schedule_timeout_uninterruptible(1); 447 + error = regmap_read(ddata->reg, CPCAP_REG_ADCC2, &value); 448 + if (error) 449 + return; 450 + } while ((value & CPCAP_BIT_ASC) && time_before(jiffies, timeout)); 451 + 452 + if (value & CPCAP_BIT_ASC) 453 + dev_err(ddata->dev, 454 + "Timeout waiting for calibration to complete\n"); 455 + 456 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1, 457 + CPCAP_BIT_CAL_MODE, 0); 458 + if (error) 459 + return; 460 + } 461 + 462 + static int cpcap_adc_calibrate_one(struct cpcap_adc *ddata, 463 + int channel, 464 + u16 calibration_register, 465 + int lower_threshold, 466 + int upper_threshold) 467 + { 468 + unsigned int calibration_data[2]; 469 + unsigned short cal_data_diff; 470 + int i, error; 471 + 472 + for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) { 473 + calibration_data[0] = 0; 474 + calibration_data[1] = 0; 475 + cal_data_diff = 0; 476 + cpcap_adc_setup_calibrate(ddata, channel); 477 + error = regmap_read(ddata->reg, calibration_register, 478 + &calibration_data[0]); 479 + if (error) 480 + return error; 481 + cpcap_adc_setup_calibrate(ddata, channel); 482 + error = regmap_read(ddata->reg, calibration_register, 483 + &calibration_data[1]); 484 + if (error) 485 + return error; 486 + 487 + if (calibration_data[0] > calibration_data[1]) 488 + cal_data_diff = 489 + calibration_data[0] - calibration_data[1]; 490 + else 491 + cal_data_diff = 492 + calibration_data[1] - calibration_data[0]; 493 + 494 + if (((calibration_data[1] >= lower_threshold) && 495 + (calibration_data[1] <= upper_threshold) && 496 + (cal_data_diff <= ST_ADC_CALIBRATE_DIFF_THRESHOLD)) || 497 + (ddata->vendor == CPCAP_VENDOR_TI)) { 498 + bank_conversion[channel].cal_offset = 499 + ((short)calibration_data[1] * -1) + 512; 500 + dev_dbg(ddata->dev, "ch%i calibration complete: %i\n", 501 + channel, bank_conversion[channel].cal_offset); 502 + break; 503 + } 504 + usleep_range(5000, 10000); 505 + } 506 + 507 + return 0; 508 + } 509 + 510 + static int cpcap_adc_calibrate(struct cpcap_adc *ddata) 511 + { 512 + int error; 513 + 514 + error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_CHG_ISENSE, 515 + CPCAP_REG_ADCAL1, 516 + ST_ADC_CAL_CHRGI_LOW_THRESHOLD, 517 + ST_ADC_CAL_CHRGI_HIGH_THRESHOLD); 518 + if (error) 519 + return error; 520 + 521 + error = cpcap_adc_calibrate_one(ddata, CPCAP_ADC_BATTI, 522 + CPCAP_REG_ADCAL2, 523 + ST_ADC_CAL_BATTI_LOW_THRESHOLD, 524 + ST_ADC_CAL_BATTI_HIGH_THRESHOLD); 525 + if (error) 526 + return error; 527 + 528 + return 0; 529 + } 530 + 531 + /* ADC setup, read and scale functions */ 532 + static void cpcap_adc_setup_bank(struct cpcap_adc *ddata, 533 + struct cpcap_adc_request *req) 534 + { 535 + const struct cpcap_adc_ato *ato = ddata->ato; 536 + unsigned short value1 = 0; 537 + unsigned short value2 = 0; 538 + int error; 539 + 540 + if (!ato) 541 + return; 542 + 543 + switch (req->channel) { 544 + case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15: 545 + value1 |= CPCAP_BIT_AD_SEL1; 546 + break; 547 + case CPCAP_ADC_BATTP_PI16 ... CPCAP_ADC_BATTI_PI17: 548 + value1 |= CPCAP_BIT_RAND1; 549 + default: 550 + break; 551 + } 552 + 553 + switch (req->timing) { 554 + case CPCAP_ADC_TIMING_IN: 555 + value1 |= ato->ato_in; 556 + value1 |= ato->atox_in; 557 + value2 |= ato->adc_ps_factor_in; 558 + value2 |= ato->atox_ps_factor_in; 559 + break; 560 + case CPCAP_ADC_TIMING_OUT: 561 + value1 |= ato->ato_out; 562 + value1 |= ato->atox_out; 563 + value2 |= ato->adc_ps_factor_out; 564 + value2 |= ato->atox_ps_factor_out; 565 + break; 566 + 567 + case CPCAP_ADC_TIMING_IMM: 568 + default: 569 + break; 570 + } 571 + 572 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC1, 573 + CPCAP_BIT_CAL_MODE | CPCAP_BIT_ATOX | 574 + CPCAP_BIT_ATO3 | CPCAP_BIT_ATO2 | 575 + CPCAP_BIT_ATO1 | CPCAP_BIT_ATO0 | 576 + CPCAP_BIT_ADA2 | CPCAP_BIT_ADA1 | 577 + CPCAP_BIT_ADA0 | CPCAP_BIT_AD_SEL1 | 578 + CPCAP_BIT_RAND1 | CPCAP_BIT_RAND0, 579 + value1); 580 + if (error) 581 + return; 582 + 583 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 584 + CPCAP_BIT_ATOX_PS_FACTOR | 585 + CPCAP_BIT_ADC_PS_FACTOR1 | 586 + CPCAP_BIT_ADC_PS_FACTOR0, 587 + value2); 588 + if (error) 589 + return; 590 + 591 + if (req->timing == CPCAP_ADC_TIMING_IMM) { 592 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 593 + CPCAP_BIT_ADTRIG_DIS, 594 + CPCAP_BIT_ADTRIG_DIS); 595 + if (error) 596 + return; 597 + 598 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 599 + CPCAP_BIT_ASC, 600 + CPCAP_BIT_ASC); 601 + if (error) 602 + return; 603 + } else { 604 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 605 + CPCAP_BIT_ADTRIG_ONESHOT, 606 + CPCAP_BIT_ADTRIG_ONESHOT); 607 + if (error) 608 + return; 609 + 610 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 611 + CPCAP_BIT_ADTRIG_DIS, 0); 612 + if (error) 613 + return; 614 + } 615 + } 616 + 617 + /* 618 + * Occasionally the ADC does not seem to start and there will be no 619 + * interrupt. Let's re-init interrupt to prevent the ADC from hanging 620 + * for the next request. It is unclear why this happens, but the next 621 + * request will usually work after doing this. 622 + */ 623 + static void cpcap_adc_quirk_reset_lost_irq(struct cpcap_adc *ddata) 624 + { 625 + int error; 626 + 627 + dev_info(ddata->dev, "lost ADC irq, attempting to reinit\n"); 628 + disable_irq(ddata->irq); 629 + error = regmap_update_bits(ddata->reg, CPCAP_REG_ADCC2, 630 + CPCAP_BIT_ADTRIG_DIS, 631 + CPCAP_BIT_ADTRIG_DIS); 632 + if (error) 633 + dev_warn(ddata->dev, "%s reset failed: %i\n", 634 + __func__, error); 635 + enable_irq(ddata->irq); 636 + } 637 + 638 + static int cpcap_adc_start_bank(struct cpcap_adc *ddata, 639 + struct cpcap_adc_request *req) 640 + { 641 + int i, error; 642 + 643 + req->timing = CPCAP_ADC_TIMING_IMM; 644 + ddata->done = false; 645 + 646 + for (i = 0; i < CPCAP_ADC_MAX_RETRIES; i++) { 647 + cpcap_adc_setup_bank(ddata, req); 648 + error = wait_event_interruptible_timeout(ddata->wq_data_avail, 649 + ddata->done, 650 + msecs_to_jiffies(50)); 651 + if (error > 0) 652 + return 0; 653 + 654 + if (error == 0) { 655 + cpcap_adc_quirk_reset_lost_irq(ddata); 656 + error = -ETIMEDOUT; 657 + continue; 658 + } 659 + 660 + if (error < 0) 661 + return error; 662 + } 663 + 664 + return error; 665 + } 666 + 667 + static void cpcap_adc_phase(struct cpcap_adc_request *req) 668 + { 669 + const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl; 670 + const struct cpcap_adc_phasing_tbl *phase_tbl = req->phase_tbl; 671 + int index = req->channel; 672 + 673 + /* Remuxed channels 16 and 17 use BATTP and BATTI entries */ 674 + switch (req->channel) { 675 + case CPCAP_ADC_BATTP: 676 + case CPCAP_ADC_BATTP_PI16: 677 + index = req->bank_index; 678 + req->result -= phase_tbl[index].offset; 679 + req->result -= CPCAP_FOUR_POINT_TWO_ADC; 680 + req->result *= phase_tbl[index].multiplier; 681 + if (phase_tbl[index].divider == 0) 682 + return; 683 + req->result /= phase_tbl[index].divider; 684 + req->result += CPCAP_FOUR_POINT_TWO_ADC; 685 + break; 686 + case CPCAP_ADC_BATTI_PI17: 687 + index = req->bank_index; 688 + /* fallthrough */ 689 + default: 690 + req->result += conv_tbl[index].cal_offset; 691 + req->result += conv_tbl[index].align_offset; 692 + req->result *= phase_tbl[index].multiplier; 693 + if (phase_tbl[index].divider == 0) 694 + return; 695 + req->result /= phase_tbl[index].divider; 696 + req->result += phase_tbl[index].offset; 697 + break; 698 + } 699 + 700 + if (req->result < phase_tbl[index].min) 701 + req->result = phase_tbl[index].min; 702 + else if (req->result > phase_tbl[index].max) 703 + req->result = phase_tbl[index].max; 704 + } 705 + 706 + /* Looks up temperatures in a table and calculates averages if needed */ 707 + static int cpcap_adc_table_to_millicelcius(unsigned short value) 708 + { 709 + int i, result = 0, alpha; 710 + 711 + if (value <= temp_map[CPCAP_MAX_TEMP_LVL - 1][0]) 712 + return temp_map[CPCAP_MAX_TEMP_LVL - 1][1]; 713 + 714 + if (value >= temp_map[0][0]) 715 + return temp_map[0][1]; 716 + 717 + for (i = 0; i < CPCAP_MAX_TEMP_LVL - 1; i++) { 718 + if ((value <= temp_map[i][0]) && 719 + (value >= temp_map[i + 1][0])) { 720 + if (value == temp_map[i][0]) { 721 + result = temp_map[i][1]; 722 + } else if (value == temp_map[i + 1][0]) { 723 + result = temp_map[i + 1][1]; 724 + } else { 725 + alpha = ((value - temp_map[i][0]) * 1000) / 726 + (temp_map[i + 1][0] - temp_map[i][0]); 727 + 728 + result = temp_map[i][1] + 729 + ((alpha * (temp_map[i + 1][1] - 730 + temp_map[i][1])) / 1000); 731 + } 732 + break; 733 + } 734 + } 735 + 736 + return result; 737 + } 738 + 739 + static void cpcap_adc_convert(struct cpcap_adc_request *req) 740 + { 741 + const struct cpcap_adc_conversion_tbl *conv_tbl = req->conv_tbl; 742 + int index = req->channel; 743 + 744 + /* Remuxed channels 16 and 17 use BATTP and BATTI entries */ 745 + switch (req->channel) { 746 + case CPCAP_ADC_BATTP_PI16: 747 + index = CPCAP_ADC_BATTP; 748 + break; 749 + case CPCAP_ADC_BATTI_PI17: 750 + index = CPCAP_ADC_BATTI; 751 + break; 752 + default: 753 + break; 754 + } 755 + 756 + /* No conversion for raw channels */ 757 + if (conv_tbl[index].conv_type == IIO_CHAN_INFO_RAW) 758 + return; 759 + 760 + /* Temperatures use a lookup table instead of conversion table */ 761 + if ((req->channel == CPCAP_ADC_AD0_BATTDETB) || 762 + (req->channel == CPCAP_ADC_AD3)) { 763 + req->result = 764 + cpcap_adc_table_to_millicelcius(req->result); 765 + 766 + return; 767 + } 768 + 769 + /* All processed channels use a conversion table */ 770 + req->result *= conv_tbl[index].multiplier; 771 + if (conv_tbl[index].divider == 0) 772 + return; 773 + req->result /= conv_tbl[index].divider; 774 + req->result += conv_tbl[index].conv_offset; 775 + } 776 + 777 + /* 778 + * REVISIT: Check if timed sampling can use multiple channels at the 779 + * same time. If not, replace channel_mask with just channel. 780 + */ 781 + static int cpcap_adc_read_bank_scaled(struct cpcap_adc *ddata, 782 + struct cpcap_adc_request *req) 783 + { 784 + int calibration_data, error, addr; 785 + 786 + if (ddata->vendor == CPCAP_VENDOR_TI) { 787 + error = regmap_read(ddata->reg, CPCAP_REG_ADCAL1, 788 + &calibration_data); 789 + if (error) 790 + return error; 791 + bank_conversion[CPCAP_ADC_CHG_ISENSE].cal_offset = 792 + ((short)calibration_data * -1) + 512; 793 + 794 + error = regmap_read(ddata->reg, CPCAP_REG_ADCAL2, 795 + &calibration_data); 796 + if (error) 797 + return error; 798 + bank_conversion[CPCAP_ADC_BATTI].cal_offset = 799 + ((short)calibration_data * -1) + 512; 800 + } 801 + 802 + addr = CPCAP_REG_ADCD0 + req->bank_index * 4; 803 + 804 + error = regmap_read(ddata->reg, addr, &req->result); 805 + if (error) 806 + return error; 807 + 808 + req->result &= 0x3ff; 809 + cpcap_adc_phase(req); 810 + cpcap_adc_convert(req); 811 + 812 + return 0; 813 + } 814 + 815 + static int cpcap_adc_init_request(struct cpcap_adc_request *req, 816 + int channel) 817 + { 818 + req->channel = channel; 819 + req->phase_tbl = bank_phasing; 820 + req->conv_tbl = bank_conversion; 821 + 822 + switch (channel) { 823 + case CPCAP_ADC_AD0_BATTDETB ... CPCAP_ADC_USB_ID: 824 + req->bank_index = channel; 825 + break; 826 + case CPCAP_ADC_AD8 ... CPCAP_ADC_TSY2_AD15: 827 + req->bank_index = channel - 8; 828 + break; 829 + case CPCAP_ADC_BATTP_PI16: 830 + req->bank_index = CPCAP_ADC_BATTP; 831 + break; 832 + case CPCAP_ADC_BATTI_PI17: 833 + req->bank_index = CPCAP_ADC_BATTI; 834 + break; 835 + default: 836 + return -EINVAL; 837 + } 838 + 839 + return 0; 840 + } 841 + 842 + static int cpcap_adc_read(struct iio_dev *indio_dev, 843 + struct iio_chan_spec const *chan, 844 + int *val, int *val2, long mask) 845 + { 846 + struct cpcap_adc *ddata = iio_priv(indio_dev); 847 + struct cpcap_adc_request req; 848 + int error; 849 + 850 + error = cpcap_adc_init_request(&req, chan->channel); 851 + if (error) 852 + return error; 853 + 854 + switch (mask) { 855 + case IIO_CHAN_INFO_RAW: 856 + mutex_lock(&ddata->lock); 857 + error = cpcap_adc_start_bank(ddata, &req); 858 + if (error) 859 + goto err_unlock; 860 + error = regmap_read(ddata->reg, chan->address, val); 861 + if (error) 862 + goto err_unlock; 863 + mutex_unlock(&ddata->lock); 864 + break; 865 + case IIO_CHAN_INFO_PROCESSED: 866 + mutex_lock(&ddata->lock); 867 + error = cpcap_adc_start_bank(ddata, &req); 868 + if (error) 869 + goto err_unlock; 870 + error = cpcap_adc_read_bank_scaled(ddata, &req); 871 + if (error) 872 + goto err_unlock; 873 + mutex_unlock(&ddata->lock); 874 + *val = req.result; 875 + break; 876 + default: 877 + return -EINVAL; 878 + } 879 + 880 + return IIO_VAL_INT; 881 + 882 + err_unlock: 883 + mutex_unlock(&ddata->lock); 884 + dev_err(ddata->dev, "error reading ADC: %i\n", error); 885 + 886 + return error; 887 + } 888 + 889 + static const struct iio_info cpcap_adc_info = { 890 + .read_raw = &cpcap_adc_read, 891 + .driver_module = THIS_MODULE, 892 + }; 893 + 894 + /* 895 + * Configuration for Motorola mapphone series such as droid 4. 896 + * Copied from the Motorola mapphone kernel tree. 897 + */ 898 + static const struct cpcap_adc_ato mapphone_adc = { 899 + .ato_in = 0x0480, 900 + .atox_in = 0, 901 + .adc_ps_factor_in = 0x0200, 902 + .atox_ps_factor_in = 0, 903 + .ato_out = 0, 904 + .atox_out = 0, 905 + .adc_ps_factor_out = 0, 906 + .atox_ps_factor_out = 0, 907 + }; 908 + 909 + static const struct of_device_id cpcap_adc_id_table[] = { 910 + { 911 + .compatible = "motorola,cpcap-adc", 912 + }, 913 + { 914 + .compatible = "motorola,mapphone-cpcap-adc", 915 + .data = &mapphone_adc, 916 + }, 917 + { /* sentinel */ }, 918 + }; 919 + MODULE_DEVICE_TABLE(of, cpcap_adc_id_table); 920 + 921 + static int cpcap_adc_probe(struct platform_device *pdev) 922 + { 923 + const struct of_device_id *match; 924 + struct cpcap_adc *ddata; 925 + struct iio_dev *indio_dev; 926 + int error; 927 + 928 + match = of_match_device(of_match_ptr(cpcap_adc_id_table), 929 + &pdev->dev); 930 + if (!match) 931 + return -EINVAL; 932 + 933 + if (!match->data) { 934 + dev_err(&pdev->dev, "no configuration data found\n"); 935 + 936 + return -ENODEV; 937 + } 938 + 939 + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*ddata)); 940 + if (!indio_dev) { 941 + dev_err(&pdev->dev, "failed to allocate iio device\n"); 942 + 943 + return -ENOMEM; 944 + } 945 + ddata = iio_priv(indio_dev); 946 + ddata->ato = match->data; 947 + ddata->dev = &pdev->dev; 948 + 949 + mutex_init(&ddata->lock); 950 + init_waitqueue_head(&ddata->wq_data_avail); 951 + 952 + indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 953 + indio_dev->dev.parent = &pdev->dev; 954 + indio_dev->dev.of_node = pdev->dev.of_node; 955 + indio_dev->channels = cpcap_adc_channels; 956 + indio_dev->num_channels = ARRAY_SIZE(cpcap_adc_channels); 957 + indio_dev->name = dev_name(&pdev->dev); 958 + indio_dev->info = &cpcap_adc_info; 959 + 960 + ddata->reg = dev_get_regmap(pdev->dev.parent, NULL); 961 + if (!ddata->reg) 962 + return -ENODEV; 963 + 964 + error = cpcap_get_vendor(ddata->dev, ddata->reg, &ddata->vendor); 965 + if (error) 966 + return error; 967 + 968 + platform_set_drvdata(pdev, indio_dev); 969 + 970 + ddata->irq = platform_get_irq_byname(pdev, "adcdone"); 971 + if (!ddata->irq) 972 + return -ENODEV; 973 + 974 + error = devm_request_threaded_irq(&pdev->dev, ddata->irq, NULL, 975 + cpcap_adc_irq_thread, 976 + IRQF_TRIGGER_NONE, 977 + "cpcap-adc", indio_dev); 978 + if (error) { 979 + dev_err(&pdev->dev, "could not get irq: %i\n", 980 + error); 981 + 982 + return error; 983 + } 984 + 985 + error = cpcap_adc_calibrate(ddata); 986 + if (error) 987 + return error; 988 + 989 + dev_info(&pdev->dev, "CPCAP ADC device probed\n"); 990 + 991 + return devm_iio_device_register(&pdev->dev, indio_dev); 992 + } 993 + 994 + static struct platform_driver cpcap_adc_driver = { 995 + .driver = { 996 + .name = "cpcap_adc", 997 + .of_match_table = of_match_ptr(cpcap_adc_id_table), 998 + }, 999 + .probe = cpcap_adc_probe, 1000 + }; 1001 + 1002 + module_platform_driver(cpcap_adc_driver); 1003 + 1004 + MODULE_ALIAS("platform:cpcap_adc"); 1005 + MODULE_DESCRIPTION("CPCAP ADC driver"); 1006 + MODULE_AUTHOR("Tony Lindgren <tony@atomide.com"); 1007 + MODULE_LICENSE("GPL v2");
+1 -1
drivers/iio/adc/exynos_adc.c
··· 579 579 580 580 static irqreturn_t exynos_adc_isr(int irq, void *dev_id) 581 581 { 582 - struct exynos_adc *info = (struct exynos_adc *)dev_id; 582 + struct exynos_adc *info = dev_id; 583 583 u32 mask = info->data->mask; 584 584 585 585 /* Read value */
+1 -1
drivers/iio/adc/hx711.c
··· 369 369 NULL, 370 370 }; 371 371 372 - static struct attribute_group hx711_attribute_group = { 372 + static const struct attribute_group hx711_attribute_group = { 373 373 .attrs = hx711_attributes, 374 374 }; 375 375
+1 -1
drivers/iio/adc/imx7d_adc.c
··· 365 365 366 366 static irqreturn_t imx7d_adc_isr(int irq, void *dev_id) 367 367 { 368 - struct imx7d_adc *info = (struct imx7d_adc *)dev_id; 368 + struct imx7d_adc *info = dev_id; 369 369 int status; 370 370 371 371 status = readl(info->regs + IMX7D_REG_ADC_INT_STATUS);
+279
drivers/iio/adc/ltc2497.c
··· 1 + /* 2 + * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC 3 + * 4 + * Copyright (C) 2017 Analog Devices Inc. 5 + * 6 + * Licensed under the GPL-2. 7 + * 8 + * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf 9 + */ 10 + 11 + #include <linux/delay.h> 12 + #include <linux/i2c.h> 13 + #include <linux/iio/iio.h> 14 + #include <linux/iio/sysfs.h> 15 + #include <linux/module.h> 16 + #include <linux/of.h> 17 + #include <linux/regulator/consumer.h> 18 + 19 + #define LTC2497_ENABLE 0xA0 20 + #define LTC2497_SGL BIT(4) 21 + #define LTC2497_DIFF 0 22 + #define LTC2497_SIGN BIT(3) 23 + #define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE 24 + #define LTC2497_CONVERSION_TIME_MS 150ULL 25 + 26 + struct ltc2497_st { 27 + struct i2c_client *client; 28 + struct regulator *ref; 29 + ktime_t time_prev; 30 + u8 addr_prev; 31 + /* 32 + * DMA (thus cache coherency maintenance) requires the 33 + * transfer buffers to live in their own cache lines. 34 + */ 35 + __be32 buf ____cacheline_aligned; 36 + }; 37 + 38 + static int ltc2497_wait_conv(struct ltc2497_st *st) 39 + { 40 + s64 time_elapsed; 41 + 42 + time_elapsed = ktime_ms_delta(ktime_get(), st->time_prev); 43 + 44 + if (time_elapsed < LTC2497_CONVERSION_TIME_MS) { 45 + /* delay if conversion time not passed 46 + * since last read or write 47 + */ 48 + if (msleep_interruptible( 49 + LTC2497_CONVERSION_TIME_MS - time_elapsed)) 50 + return -ERESTARTSYS; 51 + 52 + return 0; 53 + } 54 + 55 + if (time_elapsed - LTC2497_CONVERSION_TIME_MS <= 0) { 56 + /* We're in automatic mode - 57 + * so the last reading is stil not outdated 58 + */ 59 + return 0; 60 + } 61 + 62 + return 1; 63 + } 64 + 65 + static int ltc2497_read(struct ltc2497_st *st, u8 address, int *val) 66 + { 67 + struct i2c_client *client = st->client; 68 + int ret; 69 + 70 + ret = ltc2497_wait_conv(st); 71 + if (ret < 0) 72 + return ret; 73 + 74 + if (ret || st->addr_prev != address) { 75 + ret = i2c_smbus_write_byte(st->client, 76 + LTC2497_ENABLE | address); 77 + if (ret < 0) 78 + return ret; 79 + st->addr_prev = address; 80 + if (msleep_interruptible(LTC2497_CONVERSION_TIME_MS)) 81 + return -ERESTARTSYS; 82 + } 83 + ret = i2c_master_recv(client, (char *)&st->buf, 3); 84 + if (ret < 0) { 85 + dev_err(&client->dev, "i2c_master_recv failed\n"); 86 + return ret; 87 + } 88 + st->time_prev = ktime_get(); 89 + 90 + /* convert and shift the result, 91 + * and finally convert from offset binary to signed integer 92 + */ 93 + *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17); 94 + 95 + return ret; 96 + } 97 + 98 + static int ltc2497_read_raw(struct iio_dev *indio_dev, 99 + struct iio_chan_spec const *chan, 100 + int *val, int *val2, long mask) 101 + { 102 + struct ltc2497_st *st = iio_priv(indio_dev); 103 + int ret; 104 + 105 + switch (mask) { 106 + case IIO_CHAN_INFO_RAW: 107 + mutex_lock(&indio_dev->mlock); 108 + ret = ltc2497_read(st, chan->address, val); 109 + mutex_unlock(&indio_dev->mlock); 110 + if (ret < 0) 111 + return ret; 112 + 113 + return IIO_VAL_INT; 114 + 115 + case IIO_CHAN_INFO_SCALE: 116 + ret = regulator_get_voltage(st->ref); 117 + if (ret < 0) 118 + return ret; 119 + 120 + *val = ret / 1000; 121 + *val2 = 17; 122 + 123 + return IIO_VAL_FRACTIONAL_LOG2; 124 + 125 + default: 126 + return -EINVAL; 127 + } 128 + } 129 + 130 + #define LTC2497_CHAN(_chan, _addr) { \ 131 + .type = IIO_VOLTAGE, \ 132 + .indexed = 1, \ 133 + .channel = (_chan), \ 134 + .address = (_addr | (_chan / 2) | ((_chan & 1) ? LTC2497_SIGN : 0)), \ 135 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 136 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 137 + } 138 + 139 + #define LTC2497_CHAN_DIFF(_chan, _addr) { \ 140 + .type = IIO_VOLTAGE, \ 141 + .indexed = 1, \ 142 + .channel = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 1 : 0), \ 143 + .channel2 = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 0 : 1),\ 144 + .address = (_addr | _chan), \ 145 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 146 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 147 + .differential = 1, \ 148 + } 149 + 150 + static const struct iio_chan_spec ltc2497_channel[] = { 151 + LTC2497_CHAN(0, LTC2497_SGL), 152 + LTC2497_CHAN(1, LTC2497_SGL), 153 + LTC2497_CHAN(2, LTC2497_SGL), 154 + LTC2497_CHAN(3, LTC2497_SGL), 155 + LTC2497_CHAN(4, LTC2497_SGL), 156 + LTC2497_CHAN(5, LTC2497_SGL), 157 + LTC2497_CHAN(6, LTC2497_SGL), 158 + LTC2497_CHAN(7, LTC2497_SGL), 159 + LTC2497_CHAN(8, LTC2497_SGL), 160 + LTC2497_CHAN(9, LTC2497_SGL), 161 + LTC2497_CHAN(10, LTC2497_SGL), 162 + LTC2497_CHAN(11, LTC2497_SGL), 163 + LTC2497_CHAN(12, LTC2497_SGL), 164 + LTC2497_CHAN(13, LTC2497_SGL), 165 + LTC2497_CHAN(14, LTC2497_SGL), 166 + LTC2497_CHAN(15, LTC2497_SGL), 167 + LTC2497_CHAN_DIFF(0, LTC2497_DIFF), 168 + LTC2497_CHAN_DIFF(1, LTC2497_DIFF), 169 + LTC2497_CHAN_DIFF(2, LTC2497_DIFF), 170 + LTC2497_CHAN_DIFF(3, LTC2497_DIFF), 171 + LTC2497_CHAN_DIFF(4, LTC2497_DIFF), 172 + LTC2497_CHAN_DIFF(5, LTC2497_DIFF), 173 + LTC2497_CHAN_DIFF(6, LTC2497_DIFF), 174 + LTC2497_CHAN_DIFF(7, LTC2497_DIFF), 175 + LTC2497_CHAN_DIFF(0, LTC2497_DIFF | LTC2497_SIGN), 176 + LTC2497_CHAN_DIFF(1, LTC2497_DIFF | LTC2497_SIGN), 177 + LTC2497_CHAN_DIFF(2, LTC2497_DIFF | LTC2497_SIGN), 178 + LTC2497_CHAN_DIFF(3, LTC2497_DIFF | LTC2497_SIGN), 179 + LTC2497_CHAN_DIFF(4, LTC2497_DIFF | LTC2497_SIGN), 180 + LTC2497_CHAN_DIFF(5, LTC2497_DIFF | LTC2497_SIGN), 181 + LTC2497_CHAN_DIFF(6, LTC2497_DIFF | LTC2497_SIGN), 182 + LTC2497_CHAN_DIFF(7, LTC2497_DIFF | LTC2497_SIGN), 183 + }; 184 + 185 + static const struct iio_info ltc2497_info = { 186 + .read_raw = ltc2497_read_raw, 187 + .driver_module = THIS_MODULE, 188 + }; 189 + 190 + static int ltc2497_probe(struct i2c_client *client, 191 + const struct i2c_device_id *id) 192 + { 193 + struct iio_dev *indio_dev; 194 + struct ltc2497_st *st; 195 + int ret; 196 + 197 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C | 198 + I2C_FUNC_SMBUS_WRITE_BYTE)) 199 + return -EOPNOTSUPP; 200 + 201 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); 202 + if (!indio_dev) 203 + return -ENOMEM; 204 + 205 + st = iio_priv(indio_dev); 206 + i2c_set_clientdata(client, indio_dev); 207 + st->client = client; 208 + 209 + indio_dev->dev.parent = &client->dev; 210 + indio_dev->name = id->name; 211 + indio_dev->info = &ltc2497_info; 212 + indio_dev->modes = INDIO_DIRECT_MODE; 213 + indio_dev->channels = ltc2497_channel; 214 + indio_dev->num_channels = ARRAY_SIZE(ltc2497_channel); 215 + 216 + st->ref = devm_regulator_get(&client->dev, "vref"); 217 + if (IS_ERR(st->ref)) 218 + return PTR_ERR(st->ref); 219 + 220 + ret = regulator_enable(st->ref); 221 + if (ret < 0) 222 + return ret; 223 + 224 + ret = i2c_smbus_write_byte(st->client, LTC2497_CONFIG_DEFAULT); 225 + if (ret < 0) 226 + goto err_regulator_disable; 227 + 228 + st->addr_prev = LTC2497_CONFIG_DEFAULT; 229 + st->time_prev = ktime_get(); 230 + 231 + ret = iio_device_register(indio_dev); 232 + if (ret < 0) 233 + goto err_regulator_disable; 234 + 235 + return 0; 236 + 237 + err_regulator_disable: 238 + regulator_disable(st->ref); 239 + 240 + return ret; 241 + } 242 + 243 + static int ltc2497_remove(struct i2c_client *client) 244 + { 245 + struct iio_dev *indio_dev = i2c_get_clientdata(client); 246 + struct ltc2497_st *st = iio_priv(indio_dev); 247 + 248 + iio_device_unregister(indio_dev); 249 + regulator_disable(st->ref); 250 + 251 + return 0; 252 + } 253 + 254 + static const struct i2c_device_id ltc2497_id[] = { 255 + { "ltc2497", 0 }, 256 + { } 257 + }; 258 + MODULE_DEVICE_TABLE(i2c, ltc2497_id); 259 + 260 + static const struct of_device_id ltc2497_of_match[] = { 261 + { .compatible = "lltc,ltc2497", }, 262 + {}, 263 + }; 264 + MODULE_DEVICE_TABLE(of, ltc2497_of_match); 265 + 266 + static struct i2c_driver ltc2497_driver = { 267 + .driver = { 268 + .name = "ltc2497", 269 + .of_match_table = of_match_ptr(ltc2497_of_match), 270 + }, 271 + .probe = ltc2497_probe, 272 + .remove = ltc2497_remove, 273 + .id_table = ltc2497_id, 274 + }; 275 + module_i2c_driver(ltc2497_driver); 276 + 277 + MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 278 + MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver"); 279 + MODULE_LICENSE("GPL v2");
+1 -1
drivers/iio/adc/max1027.c
··· 364 364 365 365 static irqreturn_t max1027_trigger_handler(int irq, void *private) 366 366 { 367 - struct iio_poll_func *pf = (struct iio_poll_func *)private; 367 + struct iio_poll_func *pf = private; 368 368 struct iio_dev *indio_dev = pf->indio_dev; 369 369 struct max1027_state *st = iio_priv(indio_dev); 370 370
+2 -2
drivers/iio/adc/max11100.c
··· 124 124 indio_dev->name = "max11100"; 125 125 indio_dev->info = &max11100_info; 126 126 indio_dev->modes = INDIO_DIRECT_MODE; 127 - indio_dev->channels = max11100_channels, 128 - indio_dev->num_channels = ARRAY_SIZE(max11100_channels), 127 + indio_dev->channels = max11100_channels; 128 + indio_dev->num_channels = ARRAY_SIZE(max11100_channels); 129 129 130 130 state->vref_reg = devm_regulator_get(&spi->dev, "vref"); 131 131 if (IS_ERR(state->vref_reg))
+1 -1
drivers/iio/adc/max1363.c
··· 1007 1007 NULL, 1008 1008 }; 1009 1009 1010 - static struct attribute_group max1363_event_attribute_group = { 1010 + static const struct attribute_group max1363_event_attribute_group = { 1011 1011 .attrs = max1363_event_attributes, 1012 1012 }; 1013 1013
+75 -2
drivers/iio/adc/meson_saradc.c
··· 166 166 167 167 #define MESON_SAR_ADC_MAX_FIFO_SIZE 32 168 168 #define MESON_SAR_ADC_TIMEOUT 100 /* ms */ 169 + /* for use with IIO_VAL_INT_PLUS_MICRO */ 170 + #define MILLION 1000000 169 171 170 172 #define MESON_SAR_ADC_CHAN(_chan) { \ 171 173 .type = IIO_VOLTAGE, \ ··· 175 173 .channel = _chan, \ 176 174 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ 177 175 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \ 178 - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 176 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 177 + BIT(IIO_CHAN_INFO_CALIBBIAS) | \ 178 + BIT(IIO_CHAN_INFO_CALIBSCALE), \ 179 179 .datasheet_name = "SAR_ADC_CH"#_chan, \ 180 180 } 181 181 ··· 237 233 struct clk *adc_div_clk; 238 234 struct clk_divider clk_div; 239 235 struct completion done; 236 + int calibbias; 237 + int calibscale; 240 238 }; 241 239 242 240 static const struct regmap_config meson_sar_adc_regmap_config = { ··· 256 250 regmap_read(priv->regmap, MESON_SAR_ADC_REG0, &regval); 257 251 258 252 return FIELD_GET(MESON_SAR_ADC_REG0_FIFO_COUNT_MASK, regval); 253 + } 254 + 255 + static int meson_sar_adc_calib_val(struct iio_dev *indio_dev, int val) 256 + { 257 + struct meson_sar_adc_priv *priv = iio_priv(indio_dev); 258 + int tmp; 259 + 260 + /* use val_calib = scale * val_raw + offset calibration function */ 261 + tmp = div_s64((s64)val * priv->calibscale, MILLION) + priv->calibbias; 262 + 263 + return clamp(tmp, 0, (1 << priv->data->resolution) - 1); 259 264 } 260 265 261 266 static int meson_sar_adc_wait_busy_clear(struct iio_dev *indio_dev) ··· 319 302 320 303 fifo_val = FIELD_GET(MESON_SAR_ADC_FIFO_RD_SAMPLE_VALUE_MASK, regval); 321 304 fifo_val &= GENMASK(priv->data->resolution - 1, 0); 322 - *val = fifo_val; 305 + *val = meson_sar_adc_calib_val(indio_dev, fifo_val); 323 306 324 307 return 0; 325 308 } ··· 543 526 *val = ret / 1000; 544 527 *val2 = priv->data->resolution; 545 528 return IIO_VAL_FRACTIONAL_LOG2; 529 + 530 + case IIO_CHAN_INFO_CALIBBIAS: 531 + *val = priv->calibbias; 532 + return IIO_VAL_INT; 533 + 534 + case IIO_CHAN_INFO_CALIBSCALE: 535 + *val = priv->calibscale / MILLION; 536 + *val2 = priv->calibscale % MILLION; 537 + return IIO_VAL_INT_PLUS_MICRO; 546 538 547 539 default: 548 540 return -EINVAL; ··· 788 762 return IRQ_HANDLED; 789 763 } 790 764 765 + static int meson_sar_adc_calib(struct iio_dev *indio_dev) 766 + { 767 + struct meson_sar_adc_priv *priv = iio_priv(indio_dev); 768 + int ret, nominal0, nominal1, value0, value1; 769 + 770 + /* use points 25% and 75% for calibration */ 771 + nominal0 = (1 << priv->data->resolution) / 4; 772 + nominal1 = (1 << priv->data->resolution) * 3 / 4; 773 + 774 + meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_DIV4); 775 + usleep_range(10, 20); 776 + ret = meson_sar_adc_get_sample(indio_dev, 777 + &meson_sar_adc_iio_channels[7], 778 + MEAN_AVERAGING, EIGHT_SAMPLES, &value0); 779 + if (ret < 0) 780 + goto out; 781 + 782 + meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_VDD_MUL3_DIV4); 783 + usleep_range(10, 20); 784 + ret = meson_sar_adc_get_sample(indio_dev, 785 + &meson_sar_adc_iio_channels[7], 786 + MEAN_AVERAGING, EIGHT_SAMPLES, &value1); 787 + if (ret < 0) 788 + goto out; 789 + 790 + if (value1 <= value0) { 791 + ret = -EINVAL; 792 + goto out; 793 + } 794 + 795 + priv->calibscale = div_s64((nominal1 - nominal0) * (s64)MILLION, 796 + value1 - value0); 797 + priv->calibbias = nominal0 - div_s64((s64)value0 * priv->calibscale, 798 + MILLION); 799 + ret = 0; 800 + out: 801 + meson_sar_adc_set_chan7_mux(indio_dev, CHAN7_MUX_CH7_INPUT); 802 + 803 + return ret; 804 + } 805 + 791 806 static const struct iio_info meson_sar_adc_iio_info = { 792 807 .read_raw = meson_sar_adc_iio_info_read_raw, 793 808 .driver_module = THIS_MODULE, ··· 968 901 return PTR_ERR(priv->vref); 969 902 } 970 903 904 + priv->calibscale = MILLION; 905 + 971 906 ret = meson_sar_adc_init(indio_dev); 972 907 if (ret) 973 908 goto err; ··· 977 908 ret = meson_sar_adc_hw_enable(indio_dev); 978 909 if (ret) 979 910 goto err; 911 + 912 + ret = meson_sar_adc_calib(indio_dev); 913 + if (ret) 914 + dev_warn(&pdev->dev, "calibration failed\n"); 980 915 981 916 platform_set_drvdata(pdev, indio_dev); 982 917
+1 -1
drivers/iio/adc/rockchip_saradc.c
··· 109 109 110 110 static irqreturn_t rockchip_saradc_isr(int irq, void *dev_id) 111 111 { 112 - struct rockchip_saradc *info = (struct rockchip_saradc *)dev_id; 112 + struct rockchip_saradc *info = dev_id; 113 113 114 114 /* Read value */ 115 115 info->last_val = readl_relaxed(info->regs + SARADC_DATA);
+1 -1
drivers/iio/adc/sun4i-gpadc-iio.c
··· 382 382 383 383 static int sun4i_gpadc_get_temp(void *data, int *temp) 384 384 { 385 - struct sun4i_gpadc_iio *info = (struct sun4i_gpadc_iio *)data; 385 + struct sun4i_gpadc_iio *info = data; 386 386 int val, scale, offset; 387 387 388 388 if (sun4i_gpadc_temp_read(info->indio_dev, &val))
+1 -1
drivers/iio/adc/vf610_adc.c
··· 584 584 585 585 static irqreturn_t vf610_adc_isr(int irq, void *dev_id) 586 586 { 587 - struct iio_dev *indio_dev = (struct iio_dev *)dev_id; 587 + struct iio_dev *indio_dev = dev_id; 588 588 struct vf610_adc *info = iio_priv(indio_dev); 589 589 int coco; 590 590
+1 -1
drivers/iio/chemical/ams-iaq-core.c
··· 163 163 mutex_init(&data->lock); 164 164 165 165 indio_dev->dev.parent = &client->dev; 166 - indio_dev->info = &ams_iaqcore_info, 166 + indio_dev->info = &ams_iaqcore_info; 167 167 indio_dev->name = dev_name(&client->dev); 168 168 indio_dev->modes = INDIO_DIRECT_MODE; 169 169
+1 -1
drivers/iio/chemical/vz89x.c
··· 393 393 mutex_init(&data->lock); 394 394 395 395 indio_dev->dev.parent = &client->dev; 396 - indio_dev->info = &vz89x_info, 396 + indio_dev->info = &vz89x_info; 397 397 indio_dev->name = dev_name(&client->dev); 398 398 indio_dev->modes = INDIO_DIRECT_MODE; 399 399
+3 -23
drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
··· 267 267 else 268 268 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd; 269 269 270 - ret = iio_triggered_buffer_setup(indio_dev, NULL, 271 - cros_ec_sensors_capture, NULL); 270 + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, 271 + cros_ec_sensors_capture, NULL); 272 272 if (ret) 273 273 return ret; 274 274 275 - ret = iio_device_register(indio_dev); 276 - if (ret) 277 - goto error_uninit_buffer; 278 - 279 - return 0; 280 - 281 - error_uninit_buffer: 282 - iio_triggered_buffer_cleanup(indio_dev); 283 - 284 - return ret; 285 - } 286 - 287 - static int cros_ec_sensors_remove(struct platform_device *pdev) 288 - { 289 - struct iio_dev *indio_dev = platform_get_drvdata(pdev); 290 - 291 - iio_device_unregister(indio_dev); 292 - iio_triggered_buffer_cleanup(indio_dev); 293 - 294 - return 0; 275 + return devm_iio_device_register(dev, indio_dev); 295 276 } 296 277 297 278 static const struct platform_device_id cros_ec_sensors_ids[] = { ··· 294 313 .name = "cros-ec-sensors", 295 314 }, 296 315 .probe = cros_ec_sensors_probe, 297 - .remove = cros_ec_sensors_remove, 298 316 .id_table = cros_ec_sensors_ids, 299 317 }; 300 318 module_platform_driver(cros_ec_sensors_platform_driver);
+8
drivers/iio/common/hid-sensors/hid-sensor-attributes.c
··· 38 38 {HID_USAGE_SENSOR_ACCEL_3D, 39 39 HID_USAGE_SENSOR_UNITS_G, 9, 806650000}, 40 40 41 + {HID_USAGE_SENSOR_GRAVITY_VECTOR, 0, 9, 806650000}, 42 + {HID_USAGE_SENSOR_GRAVITY_VECTOR, 43 + HID_USAGE_SENSOR_UNITS_METERS_PER_SEC_SQRD, 1, 0}, 44 + {HID_USAGE_SENSOR_GRAVITY_VECTOR, 45 + HID_USAGE_SENSOR_UNITS_G, 9, 806650000}, 46 + 41 47 {HID_USAGE_SENSOR_GYRO_3D, 0, 0, 17453293}, 42 48 {HID_USAGE_SENSOR_GYRO_3D, 43 49 HID_USAGE_SENSOR_UNITS_RADIANS_PER_SECOND, 1, 0}, ··· 71 65 72 66 {HID_USAGE_SENSOR_TEMPERATURE, 0, 1000, 0}, 73 67 {HID_USAGE_SENSOR_TEMPERATURE, HID_USAGE_SENSOR_UNITS_DEGREES, 1000, 0}, 68 + 69 + {HID_USAGE_SENSOR_HUMIDITY, 0, 1000, 0}, 74 70 }; 75 71 76 72 static int pow_10(unsigned power)
+2 -2
drivers/iio/common/ms_sensors/ms_sensors_i2c.c
··· 74 74 int ms_sensors_read_prom_word(void *cli, int cmd, u16 *word) 75 75 { 76 76 int ret; 77 - struct i2c_client *client = (struct i2c_client *)cli; 77 + struct i2c_client *client = cli; 78 78 79 79 ret = i2c_smbus_read_word_swapped(client, cmd); 80 80 if (ret < 0) { ··· 107 107 { 108 108 int ret; 109 109 __be32 buf = 0; 110 - struct i2c_client *client = (struct i2c_client *)cli; 110 + struct i2c_client *client = cli; 111 111 112 112 /* Trigger conversion */ 113 113 ret = i2c_smbus_write_byte(client, conv);
+2 -2
drivers/iio/dac/ad5504.c
··· 212 212 NULL, 213 213 }; 214 214 215 - static struct attribute_group ad5504_ev_attribute_group = { 215 + static const struct attribute_group ad5504_ev_attribute_group = { 216 216 .attrs = ad5504_ev_attributes, 217 217 }; 218 218 ··· 223 223 0, 224 224 IIO_EV_TYPE_THRESH, 225 225 IIO_EV_DIR_RISING), 226 - iio_get_time_ns((struct iio_dev *)private)); 226 + iio_get_time_ns(private)); 227 227 228 228 return IRQ_HANDLED; 229 229 }
+14
drivers/iio/humidity/Kconfig
··· 36 36 To compile this driver as a module, choose M here: the module 37 37 will be called hdc100x. 38 38 39 + config HID_SENSOR_HUMIDITY 40 + tristate "HID Environmental humidity sensor" 41 + depends on HID_SENSOR_HUB 42 + select IIO_BUFFER 43 + select IIO_TRIGGERED_BUFFER 44 + select HID_SENSOR_IIO_COMMON 45 + select HID_SENSOR_IIO_TRIGGER 46 + help 47 + Say yes here to build support for the HID SENSOR 48 + humidity driver 49 + 50 + To compile this driver as a module, choose M here: the module 51 + will be called hid-sensor-humidity. 52 + 39 53 config HTS221 40 54 tristate "STMicroelectronics HTS221 sensor Driver" 41 55 depends on (I2C || SPI)
+3
drivers/iio/humidity/Makefile
··· 5 5 obj-$(CONFIG_AM2315) += am2315.o 6 6 obj-$(CONFIG_DHT11) += dht11.o 7 7 obj-$(CONFIG_HDC100X) += hdc100x.o 8 + obj-$(CONFIG_HID_SENSOR_HUMIDITY) += hid-sensor-humidity.o 8 9 9 10 hts221-y := hts221_core.o \ 10 11 hts221_buffer.o ··· 16 15 obj-$(CONFIG_HTU21) += htu21.o 17 16 obj-$(CONFIG_SI7005) += si7005.o 18 17 obj-$(CONFIG_SI7020) += si7020.o 18 + 19 + ccflags-y += -I$(srctree)/drivers/iio/common/hid-sensors
+1 -1
drivers/iio/humidity/hdc100x.c
··· 79 79 NULL 80 80 }; 81 81 82 - static struct attribute_group hdc100x_attribute_group = { 82 + static const struct attribute_group hdc100x_attribute_group = { 83 83 .attrs = hdc100x_attributes, 84 84 }; 85 85
+315
drivers/iio/humidity/hid-sensor-humidity.c
··· 1 + /* 2 + * HID Sensors Driver 3 + * Copyright (c) 2017, Intel Corporation. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms and conditions of the GNU General Public License, 7 + * version 2, as published by the Free Software Foundation. 8 + * 9 + * This program is distributed in the hope it will be useful, but WITHOUT 10 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12 + * more details. 13 + * 14 + * You should have received a copy of the GNU General Public License along with 15 + * this program. 16 + */ 17 + #include <linux/device.h> 18 + #include <linux/hid-sensor-hub.h> 19 + #include <linux/iio/buffer.h> 20 + #include <linux/iio/iio.h> 21 + #include <linux/iio/triggered_buffer.h> 22 + #include <linux/iio/trigger_consumer.h> 23 + #include <linux/module.h> 24 + #include <linux/platform_device.h> 25 + 26 + #include "hid-sensor-trigger.h" 27 + 28 + struct hid_humidity_state { 29 + struct hid_sensor_common common_attributes; 30 + struct hid_sensor_hub_attribute_info humidity_attr; 31 + s32 humidity_data; 32 + int scale_pre_decml; 33 + int scale_post_decml; 34 + int scale_precision; 35 + int value_offset; 36 + }; 37 + 38 + /* Channel definitions */ 39 + static const struct iio_chan_spec humidity_channels[] = { 40 + { 41 + .type = IIO_HUMIDITYRELATIVE, 42 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 43 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OFFSET) | 44 + BIT(IIO_CHAN_INFO_SCALE) | 45 + BIT(IIO_CHAN_INFO_SAMP_FREQ) | 46 + BIT(IIO_CHAN_INFO_HYSTERESIS), 47 + }, 48 + IIO_CHAN_SOFT_TIMESTAMP(1) 49 + }; 50 + 51 + /* Adjust channel real bits based on report descriptor */ 52 + static void humidity_adjust_channel_bit_mask(struct iio_chan_spec *channels, 53 + int channel, int size) 54 + { 55 + channels[channel].scan_type.sign = 's'; 56 + /* Real storage bits will change based on the report desc. */ 57 + channels[channel].scan_type.realbits = size * 8; 58 + /* Maximum size of a sample to capture is s32 */ 59 + channels[channel].scan_type.storagebits = sizeof(s32) * 8; 60 + } 61 + 62 + static int humidity_read_raw(struct iio_dev *indio_dev, 63 + struct iio_chan_spec const *chan, 64 + int *val, int *val2, long mask) 65 + { 66 + struct hid_humidity_state *humid_st = iio_priv(indio_dev); 67 + 68 + switch (mask) { 69 + case IIO_CHAN_INFO_RAW: 70 + if (chan->type != IIO_HUMIDITYRELATIVE) 71 + return -EINVAL; 72 + hid_sensor_power_state(&humid_st->common_attributes, true); 73 + *val = sensor_hub_input_attr_get_raw_value( 74 + humid_st->common_attributes.hsdev, 75 + HID_USAGE_SENSOR_HUMIDITY, 76 + HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 77 + humid_st->humidity_attr.report_id, 78 + SENSOR_HUB_SYNC); 79 + hid_sensor_power_state(&humid_st->common_attributes, false); 80 + 81 + return IIO_VAL_INT; 82 + 83 + case IIO_CHAN_INFO_SCALE: 84 + *val = humid_st->scale_pre_decml; 85 + *val2 = humid_st->scale_post_decml; 86 + 87 + return humid_st->scale_precision; 88 + 89 + case IIO_CHAN_INFO_OFFSET: 90 + *val = humid_st->value_offset; 91 + 92 + return IIO_VAL_INT; 93 + 94 + case IIO_CHAN_INFO_SAMP_FREQ: 95 + return hid_sensor_read_samp_freq_value( 96 + &humid_st->common_attributes, val, val2); 97 + 98 + case IIO_CHAN_INFO_HYSTERESIS: 99 + return hid_sensor_read_raw_hyst_value( 100 + &humid_st->common_attributes, val, val2); 101 + 102 + default: 103 + return -EINVAL; 104 + } 105 + } 106 + 107 + static int humidity_write_raw(struct iio_dev *indio_dev, 108 + struct iio_chan_spec const *chan, 109 + int val, int val2, long mask) 110 + { 111 + struct hid_humidity_state *humid_st = iio_priv(indio_dev); 112 + 113 + switch (mask) { 114 + case IIO_CHAN_INFO_SAMP_FREQ: 115 + return hid_sensor_write_samp_freq_value( 116 + &humid_st->common_attributes, val, val2); 117 + 118 + case IIO_CHAN_INFO_HYSTERESIS: 119 + return hid_sensor_write_raw_hyst_value( 120 + &humid_st->common_attributes, val, val2); 121 + 122 + default: 123 + return -EINVAL; 124 + } 125 + } 126 + 127 + static const struct iio_info humidity_info = { 128 + .driver_module = THIS_MODULE, 129 + .read_raw = &humidity_read_raw, 130 + .write_raw = &humidity_write_raw, 131 + }; 132 + 133 + /* Callback handler to send event after all samples are received and captured */ 134 + static int humidity_proc_event(struct hid_sensor_hub_device *hsdev, 135 + unsigned int usage_id, void *pdev) 136 + { 137 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 138 + struct hid_humidity_state *humid_st = iio_priv(indio_dev); 139 + 140 + if (atomic_read(&humid_st->common_attributes.data_ready)) 141 + iio_push_to_buffers_with_timestamp(indio_dev, 142 + &humid_st->humidity_data, 143 + iio_get_time_ns(indio_dev)); 144 + 145 + return 0; 146 + } 147 + 148 + /* Capture samples in local storage */ 149 + static int humidity_capture_sample(struct hid_sensor_hub_device *hsdev, 150 + unsigned int usage_id, size_t raw_len, 151 + char *raw_data, void *pdev) 152 + { 153 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 154 + struct hid_humidity_state *humid_st = iio_priv(indio_dev); 155 + 156 + switch (usage_id) { 157 + case HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY: 158 + humid_st->humidity_data = *(s32 *)raw_data; 159 + 160 + return 0; 161 + default: 162 + return -EINVAL; 163 + } 164 + } 165 + 166 + /* Parse report which is specific to an usage id */ 167 + static int humidity_parse_report(struct platform_device *pdev, 168 + struct hid_sensor_hub_device *hsdev, 169 + struct iio_chan_spec *channels, 170 + unsigned int usage_id, 171 + struct hid_humidity_state *st) 172 + { 173 + int ret; 174 + 175 + ret = sensor_hub_input_get_attribute_info(hsdev, HID_INPUT_REPORT, 176 + usage_id, 177 + HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 178 + &st->humidity_attr); 179 + if (ret < 0) 180 + return ret; 181 + 182 + humidity_adjust_channel_bit_mask(channels, 0, st->humidity_attr.size); 183 + 184 + st->scale_precision = hid_sensor_format_scale( 185 + HID_USAGE_SENSOR_HUMIDITY, 186 + &st->humidity_attr, 187 + &st->scale_pre_decml, 188 + &st->scale_post_decml); 189 + 190 + /* Set Sensitivity field ids, when there is no individual modifier */ 191 + if (st->common_attributes.sensitivity.index < 0) 192 + sensor_hub_input_get_attribute_info(hsdev, 193 + HID_FEATURE_REPORT, usage_id, 194 + HID_USAGE_SENSOR_DATA_MOD_CHANGE_SENSITIVITY_ABS | 195 + HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY, 196 + &st->common_attributes.sensitivity); 197 + 198 + return ret; 199 + } 200 + 201 + static struct hid_sensor_hub_callbacks humidity_callbacks = { 202 + .send_event = &humidity_proc_event, 203 + .capture_sample = &humidity_capture_sample, 204 + }; 205 + 206 + /* Function to initialize the processing for usage id */ 207 + static int hid_humidity_probe(struct platform_device *pdev) 208 + { 209 + static const char *name = "humidity"; 210 + struct iio_dev *indio_dev; 211 + struct hid_humidity_state *humid_st; 212 + struct iio_chan_spec *humid_chans; 213 + struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 214 + int ret; 215 + 216 + indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*humid_st)); 217 + if (!indio_dev) 218 + return -ENOMEM; 219 + 220 + humid_st = iio_priv(indio_dev); 221 + humid_st->common_attributes.hsdev = hsdev; 222 + humid_st->common_attributes.pdev = pdev; 223 + 224 + ret = hid_sensor_parse_common_attributes(hsdev, 225 + HID_USAGE_SENSOR_HUMIDITY, 226 + &humid_st->common_attributes); 227 + if (ret) 228 + return ret; 229 + 230 + humid_chans = devm_kmemdup(&indio_dev->dev, humidity_channels, 231 + sizeof(humidity_channels), GFP_KERNEL); 232 + if (!humid_chans) 233 + return -ENOMEM; 234 + 235 + ret = humidity_parse_report(pdev, hsdev, humid_chans, 236 + HID_USAGE_SENSOR_HUMIDITY, humid_st); 237 + if (ret) 238 + return ret; 239 + 240 + indio_dev->channels = humid_chans; 241 + indio_dev->num_channels = ARRAY_SIZE(humidity_channels); 242 + indio_dev->dev.parent = &pdev->dev; 243 + indio_dev->info = &humidity_info; 244 + indio_dev->name = name; 245 + indio_dev->modes = INDIO_DIRECT_MODE; 246 + 247 + ret = devm_iio_triggered_buffer_setup(&pdev->dev, indio_dev, 248 + &iio_pollfunc_store_time, NULL, NULL); 249 + if (ret) 250 + return ret; 251 + 252 + atomic_set(&humid_st->common_attributes.data_ready, 0); 253 + ret = hid_sensor_setup_trigger(indio_dev, name, 254 + &humid_st->common_attributes); 255 + if (ret) 256 + return ret; 257 + 258 + platform_set_drvdata(pdev, indio_dev); 259 + 260 + humidity_callbacks.pdev = pdev; 261 + ret = sensor_hub_register_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY, 262 + &humidity_callbacks); 263 + if (ret) 264 + goto error_remove_trigger; 265 + 266 + ret = iio_device_register(indio_dev); 267 + if (ret) 268 + goto error_remove_callback; 269 + 270 + return ret; 271 + 272 + error_remove_callback: 273 + sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 274 + error_remove_trigger: 275 + hid_sensor_remove_trigger(&humid_st->common_attributes); 276 + return ret; 277 + } 278 + 279 + /* Function to deinitialize the processing for usage id */ 280 + static int hid_humidity_remove(struct platform_device *pdev) 281 + { 282 + struct hid_sensor_hub_device *hsdev = dev_get_platdata(&pdev->dev); 283 + struct iio_dev *indio_dev = platform_get_drvdata(pdev); 284 + struct hid_humidity_state *humid_st = iio_priv(indio_dev); 285 + 286 + iio_device_unregister(indio_dev); 287 + sensor_hub_remove_callback(hsdev, HID_USAGE_SENSOR_HUMIDITY); 288 + hid_sensor_remove_trigger(&humid_st->common_attributes); 289 + 290 + return 0; 291 + } 292 + 293 + static const struct platform_device_id hid_humidity_ids[] = { 294 + { 295 + /* Format: HID-SENSOR-usage_id_in_hex_lowercase */ 296 + .name = "HID-SENSOR-200032", 297 + }, 298 + { /* sentinel */ } 299 + }; 300 + MODULE_DEVICE_TABLE(platform, hid_humidity_ids); 301 + 302 + static struct platform_driver hid_humidity_platform_driver = { 303 + .id_table = hid_humidity_ids, 304 + .driver = { 305 + .name = KBUILD_MODNAME, 306 + .pm = &hid_sensor_pm_ops, 307 + }, 308 + .probe = hid_humidity_probe, 309 + .remove = hid_humidity_remove, 310 + }; 311 + module_platform_driver(hid_humidity_platform_driver); 312 + 313 + MODULE_DESCRIPTION("HID Environmental humidity sensor"); 314 + MODULE_AUTHOR("Song Hongyan <hongyan.song@intel.com>"); 315 + MODULE_LICENSE("GPL v2");
+1 -1
drivers/iio/humidity/hts221_buffer.c
··· 41 41 42 42 static irqreturn_t hts221_trigger_handler_thread(int irq, void *private) 43 43 { 44 - struct hts221_hw *hw = (struct hts221_hw *)private; 44 + struct hts221_hw *hw = private; 45 45 u8 status; 46 46 int err; 47 47
+6
drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
··· 114 114 .config = &chip_config_6050, 115 115 }, 116 116 { 117 + .whoami = INV_MPU9250_WHOAMI_VALUE, 118 + .name = "MPU9250", 119 + .reg = &reg_set_6500, 120 + .config = &chip_config_6050, 121 + }, 122 + { 117 123 .whoami = INV_ICM20608_WHOAMI_VALUE, 118 124 .name = "ICM20608", 119 125 .reg = &reg_set_6500,
+5
drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c
··· 178 178 {"mpu6050", INV_MPU6050}, 179 179 {"mpu6500", INV_MPU6500}, 180 180 {"mpu9150", INV_MPU9150}, 181 + {"mpu9250", INV_MPU9250}, 181 182 {"icm20608", INV_ICM20608}, 182 183 {} 183 184 }; ··· 197 196 { 198 197 .compatible = "invensense,mpu9150", 199 198 .data = (void *)INV_MPU9150 199 + }, 200 + { 201 + .compatible = "invensense,mpu9250", 202 + .data = (void *)INV_MPU9250 200 203 }, 201 204 { 202 205 .compatible = "invensense,icm20608",
+2
drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
··· 70 70 INV_MPU6500, 71 71 INV_MPU6000, 72 72 INV_MPU9150, 73 + INV_MPU9250, 73 74 INV_ICM20608, 74 75 INV_NUM_PARTS 75 76 }; ··· 227 226 #define INV_MPU6050_WHOAMI_VALUE 0x68 228 227 #define INV_MPU6500_WHOAMI_VALUE 0x70 229 228 #define INV_MPU9150_WHOAMI_VALUE 0x68 229 + #define INV_MPU9250_WHOAMI_VALUE 0x71 230 230 #define INV_ICM20608_WHOAMI_VALUE 0xAF 231 231 232 232 /* scan element definition */
+1
drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c
··· 82 82 {"mpu6000", INV_MPU6000}, 83 83 {"mpu6500", INV_MPU6500}, 84 84 {"mpu9150", INV_MPU9150}, 85 + {"mpu9250", INV_MPU9250}, 85 86 {"icm20608", INV_ICM20608}, 86 87 {} 87 88 };
+2 -2
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
··· 364 364 365 365 static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private) 366 366 { 367 - struct st_lsm6dsx_hw *hw = (struct st_lsm6dsx_hw *)private; 367 + struct st_lsm6dsx_hw *hw = private; 368 368 struct st_lsm6dsx_sensor *sensor; 369 369 int i; 370 370 ··· 388 388 389 389 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private) 390 390 { 391 - struct st_lsm6dsx_hw *hw = (struct st_lsm6dsx_hw *)private; 391 + struct st_lsm6dsx_hw *hw = private; 392 392 int count; 393 393 394 394 mutex_lock(&hw->fifo_lock);
+26 -15
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c
··· 308 308 return 0; 309 309 } 310 310 311 - static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr) 311 + static int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, 312 + u8 *val) 312 313 { 313 - enum st_lsm6dsx_sensor_id id = sensor->id; 314 - int i, err; 315 - u8 val; 314 + int i; 316 315 317 316 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++) 318 - if (st_lsm6dsx_odr_table[id].odr_avl[i].hz == odr) 317 + if (st_lsm6dsx_odr_table[sensor->id].odr_avl[i].hz == odr) 319 318 break; 320 319 321 320 if (i == ST_LSM6DSX_ODR_LIST_SIZE) 322 321 return -EINVAL; 323 322 324 - val = st_lsm6dsx_odr_table[id].odr_avl[i].val; 325 - err = st_lsm6dsx_write_with_mask(sensor->hw, 326 - st_lsm6dsx_odr_table[id].reg.addr, 327 - st_lsm6dsx_odr_table[id].reg.mask, 328 - val); 329 - if (err < 0) 330 - return err; 331 - 323 + *val = st_lsm6dsx_odr_table[sensor->id].odr_avl[i].val; 332 324 sensor->odr = odr; 333 325 334 326 return 0; 327 + } 328 + 329 + static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 odr) 330 + { 331 + enum st_lsm6dsx_sensor_id id = sensor->id; 332 + int err; 333 + u8 val; 334 + 335 + err = st_lsm6dsx_check_odr(sensor, odr, &val); 336 + if (err < 0) 337 + return err; 338 + 339 + return st_lsm6dsx_write_with_mask(sensor->hw, 340 + st_lsm6dsx_odr_table[id].reg.addr, 341 + st_lsm6dsx_odr_table[id].reg.mask, 342 + val); 335 343 } 336 344 337 345 int st_lsm6dsx_sensor_enable(struct st_lsm6dsx_sensor *sensor) ··· 444 436 case IIO_CHAN_INFO_SCALE: 445 437 err = st_lsm6dsx_set_full_scale(sensor, val2); 446 438 break; 447 - case IIO_CHAN_INFO_SAMP_FREQ: 448 - err = st_lsm6dsx_set_odr(sensor, val); 439 + case IIO_CHAN_INFO_SAMP_FREQ: { 440 + u8 data; 441 + 442 + err = st_lsm6dsx_check_odr(sensor, val, &data); 449 443 break; 444 + } 450 445 default: 451 446 err = -EINVAL; 452 447 break;
+10
drivers/iio/light/Kconfig
··· 405 405 To compile this driver as a module, choose M here: the 406 406 module will be called veml6070. 407 407 408 + config VL6180 409 + tristate "VL6180 ALS, range and proximity sensor" 410 + depends on I2C 411 + help 412 + Say Y here if you want to build a driver for the STMicroelectronics 413 + VL6180 combined ambient light, range and proximity sensor. 414 + 415 + To compile this driver as a module, choose M here: the 416 + module will be called vl6180. 417 + 408 418 endmenu
+1
drivers/iio/light/Makefile
··· 38 38 obj-$(CONFIG_US5182D) += us5182d.o 39 39 obj-$(CONFIG_VCNL4000) += vcnl4000.o 40 40 obj-$(CONFIG_VEML6070) += veml6070.o 41 + obj-$(CONFIG_VL6180) += vl6180.o
+1 -1
drivers/iio/light/apds9960.c
··· 343 343 NULL, 344 344 }; 345 345 346 - static struct attribute_group apds9960_attribute_group = { 346 + static const struct attribute_group apds9960_attribute_group = { 347 347 .attrs = apds9960_attributes, 348 348 }; 349 349
+1 -1
drivers/iio/light/bh1750.c
··· 212 212 NULL, 213 213 }; 214 214 215 - static struct attribute_group bh1750_attribute_group = { 215 + static const struct attribute_group bh1750_attribute_group = { 216 216 .attrs = bh1750_attributes, 217 217 }; 218 218
+2 -2
drivers/iio/light/lm3533-als.c
··· 690 690 NULL 691 691 }; 692 692 693 - static struct attribute_group lm3533_als_event_attribute_group = { 693 + static const struct attribute_group lm3533_als_event_attribute_group = { 694 694 .attrs = lm3533_als_event_attributes 695 695 }; 696 696 ··· 714 714 NULL 715 715 }; 716 716 717 - static struct attribute_group lm3533_als_attribute_group = { 717 + static const struct attribute_group lm3533_als_attribute_group = { 718 718 .attrs = lm3533_als_attributes 719 719 }; 720 720
+543
drivers/iio/light/vl6180.c
··· 1 + /* 2 + * vl6180.c - Support for STMicroelectronics VL6180 ALS, range and proximity 3 + * sensor 4 + * 5 + * Copyright 2017 Peter Meerwald-Stadler <pmeerw@pmeerw.net> 6 + * Copyright 2017 Manivannan Sadhasivam <manivannanece23@gmail.com> 7 + * 8 + * This file is subject to the terms and conditions of version 2 of 9 + * the GNU General Public License. See the file COPYING in the main 10 + * directory of this archive for more details. 11 + * 12 + * IIO driver for VL6180 (7-bit I2C slave address 0x29) 13 + * 14 + * Range: 0 to 100mm 15 + * ALS: < 1 Lux up to 100 kLux 16 + * IR: 850nm 17 + * 18 + * TODO: irq, threshold events, continuous mode, hardware buffer 19 + */ 20 + 21 + #include <linux/module.h> 22 + #include <linux/i2c.h> 23 + #include <linux/mutex.h> 24 + #include <linux/err.h> 25 + #include <linux/of.h> 26 + #include <linux/delay.h> 27 + 28 + #include <linux/iio/iio.h> 29 + #include <linux/iio/sysfs.h> 30 + 31 + #define VL6180_DRV_NAME "vl6180" 32 + 33 + /* Device identification register and value */ 34 + #define VL6180_MODEL_ID 0x000 35 + #define VL6180_MODEL_ID_VAL 0xb4 36 + 37 + /* Configuration registers */ 38 + #define VL6180_INTR_CONFIG 0x014 39 + #define VL6180_INTR_CLEAR 0x015 40 + #define VL6180_OUT_OF_RESET 0x016 41 + #define VL6180_HOLD 0x017 42 + #define VL6180_RANGE_START 0x018 43 + #define VL6180_ALS_START 0x038 44 + #define VL6180_ALS_GAIN 0x03f 45 + #define VL6180_ALS_IT 0x040 46 + 47 + /* Status registers */ 48 + #define VL6180_RANGE_STATUS 0x04d 49 + #define VL6180_ALS_STATUS 0x04e 50 + #define VL6180_INTR_STATUS 0x04f 51 + 52 + /* Result value registers */ 53 + #define VL6180_ALS_VALUE 0x050 54 + #define VL6180_RANGE_VALUE 0x062 55 + #define VL6180_RANGE_RATE 0x066 56 + 57 + /* bits of the RANGE_START and ALS_START register */ 58 + #define VL6180_MODE_CONT BIT(1) /* continuous mode */ 59 + #define VL6180_STARTSTOP BIT(0) /* start measurement, auto-reset */ 60 + 61 + /* bits of the INTR_STATUS and INTR_CONFIG register */ 62 + #define VL6180_ALS_READY BIT(5) 63 + #define VL6180_RANGE_READY BIT(2) 64 + 65 + /* bits of the INTR_CLEAR register */ 66 + #define VL6180_CLEAR_ERROR BIT(2) 67 + #define VL6180_CLEAR_ALS BIT(1) 68 + #define VL6180_CLEAR_RANGE BIT(0) 69 + 70 + /* bits of the HOLD register */ 71 + #define VL6180_HOLD_ON BIT(0) 72 + 73 + /* default value for the ALS_IT register */ 74 + #define VL6180_ALS_IT_100 0x63 /* 100 ms */ 75 + 76 + /* values for the ALS_GAIN register */ 77 + #define VL6180_ALS_GAIN_1 0x46 78 + #define VL6180_ALS_GAIN_1_25 0x45 79 + #define VL6180_ALS_GAIN_1_67 0x44 80 + #define VL6180_ALS_GAIN_2_5 0x43 81 + #define VL6180_ALS_GAIN_5 0x42 82 + #define VL6180_ALS_GAIN_10 0x41 83 + #define VL6180_ALS_GAIN_20 0x40 84 + #define VL6180_ALS_GAIN_40 0x47 85 + 86 + struct vl6180_data { 87 + struct i2c_client *client; 88 + struct mutex lock; 89 + }; 90 + 91 + enum { VL6180_ALS, VL6180_RANGE, VL6180_PROX }; 92 + 93 + /** 94 + * struct vl6180_chan_regs - Registers for accessing channels 95 + * @drdy_mask: Data ready bit in status register 96 + * @start_reg: Conversion start register 97 + * @value_reg: Result value register 98 + * @word: Register word length 99 + */ 100 + struct vl6180_chan_regs { 101 + u8 drdy_mask; 102 + u16 start_reg, value_reg; 103 + bool word; 104 + }; 105 + 106 + static const struct vl6180_chan_regs vl6180_chan_regs_table[] = { 107 + [VL6180_ALS] = { 108 + .drdy_mask = VL6180_ALS_READY, 109 + .start_reg = VL6180_ALS_START, 110 + .value_reg = VL6180_ALS_VALUE, 111 + .word = true, 112 + }, 113 + [VL6180_RANGE] = { 114 + .drdy_mask = VL6180_RANGE_READY, 115 + .start_reg = VL6180_RANGE_START, 116 + .value_reg = VL6180_RANGE_VALUE, 117 + .word = false, 118 + }, 119 + [VL6180_PROX] = { 120 + .drdy_mask = VL6180_RANGE_READY, 121 + .start_reg = VL6180_RANGE_START, 122 + .value_reg = VL6180_RANGE_RATE, 123 + .word = true, 124 + }, 125 + }; 126 + 127 + static int vl6180_read(struct i2c_client *client, u16 cmd, void *databuf, 128 + u8 len) 129 + { 130 + __be16 cmdbuf = cpu_to_be16(cmd); 131 + struct i2c_msg msgs[2] = { 132 + { .addr = client->addr, .len = sizeof(cmdbuf), .buf = (u8 *) &cmdbuf }, 133 + { .addr = client->addr, .len = len, .buf = databuf, 134 + .flags = I2C_M_RD } }; 135 + int ret; 136 + 137 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 138 + if (ret < 0) 139 + dev_err(&client->dev, "failed reading register 0x%04x\n", cmd); 140 + 141 + return ret; 142 + } 143 + 144 + static int vl6180_read_byte(struct i2c_client *client, u16 cmd) 145 + { 146 + u8 data; 147 + int ret; 148 + 149 + ret = vl6180_read(client, cmd, &data, sizeof(data)); 150 + if (ret < 0) 151 + return ret; 152 + 153 + return data; 154 + } 155 + 156 + static int vl6180_read_word(struct i2c_client *client, u16 cmd) 157 + { 158 + __be16 data; 159 + int ret; 160 + 161 + ret = vl6180_read(client, cmd, &data, sizeof(data)); 162 + if (ret < 0) 163 + return ret; 164 + 165 + return be16_to_cpu(data); 166 + } 167 + 168 + static int vl6180_write_byte(struct i2c_client *client, u16 cmd, u8 val) 169 + { 170 + u8 buf[3]; 171 + struct i2c_msg msgs[1] = { 172 + { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 173 + int ret; 174 + 175 + buf[0] = cmd >> 8; 176 + buf[1] = cmd & 0xff; 177 + buf[2] = val; 178 + 179 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 180 + if (ret < 0) { 181 + dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 182 + return ret; 183 + } 184 + 185 + return 0; 186 + } 187 + 188 + static int vl6180_write_word(struct i2c_client *client, u16 cmd, u16 val) 189 + { 190 + __be16 buf[2]; 191 + struct i2c_msg msgs[1] = { 192 + { .addr = client->addr, .len = sizeof(buf), .buf = (u8 *) &buf } }; 193 + int ret; 194 + 195 + buf[0] = cpu_to_be16(cmd); 196 + buf[1] = cpu_to_be16(val); 197 + 198 + ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); 199 + if (ret < 0) { 200 + dev_err(&client->dev, "failed writing register 0x%04x\n", cmd); 201 + return ret; 202 + } 203 + 204 + return 0; 205 + } 206 + 207 + static int vl6180_measure(struct vl6180_data *data, int addr) 208 + { 209 + struct i2c_client *client = data->client; 210 + int tries = 20, ret; 211 + u16 value; 212 + 213 + mutex_lock(&data->lock); 214 + /* Start single shot measurement */ 215 + ret = vl6180_write_byte(client, 216 + vl6180_chan_regs_table[addr].start_reg, VL6180_STARTSTOP); 217 + if (ret < 0) 218 + goto fail; 219 + 220 + while (tries--) { 221 + ret = vl6180_read_byte(client, VL6180_INTR_STATUS); 222 + if (ret < 0) 223 + goto fail; 224 + 225 + if (ret & vl6180_chan_regs_table[addr].drdy_mask) 226 + break; 227 + msleep(20); 228 + } 229 + 230 + if (tries < 0) { 231 + ret = -EIO; 232 + goto fail; 233 + } 234 + 235 + /* Read result value from appropriate registers */ 236 + ret = vl6180_chan_regs_table[addr].word ? 237 + vl6180_read_word(client, vl6180_chan_regs_table[addr].value_reg) : 238 + vl6180_read_byte(client, vl6180_chan_regs_table[addr].value_reg); 239 + if (ret < 0) 240 + goto fail; 241 + value = ret; 242 + 243 + /* Clear the interrupt flag after data read */ 244 + ret = vl6180_write_byte(client, VL6180_INTR_CLEAR, 245 + VL6180_CLEAR_ERROR | VL6180_CLEAR_ALS | VL6180_CLEAR_RANGE); 246 + if (ret < 0) 247 + goto fail; 248 + 249 + ret = value; 250 + 251 + fail: 252 + mutex_unlock(&data->lock); 253 + 254 + return ret; 255 + } 256 + 257 + static const struct iio_chan_spec vl6180_channels[] = { 258 + { 259 + .type = IIO_LIGHT, 260 + .address = VL6180_ALS, 261 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 262 + BIT(IIO_CHAN_INFO_INT_TIME) | 263 + BIT(IIO_CHAN_INFO_SCALE) | 264 + BIT(IIO_CHAN_INFO_HARDWAREGAIN), 265 + }, { 266 + .type = IIO_DISTANCE, 267 + .address = VL6180_RANGE, 268 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 269 + BIT(IIO_CHAN_INFO_SCALE), 270 + }, { 271 + .type = IIO_PROXIMITY, 272 + .address = VL6180_PROX, 273 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 274 + } 275 + }; 276 + 277 + /* 278 + * Columns 3 & 4 represent the same value in decimal and hex notations. 279 + * Kept in order to avoid the datatype conversion while reading the 280 + * hardware_gain. 281 + */ 282 + static const int vl6180_als_gain[8][4] = { 283 + { 1, 0, 70, VL6180_ALS_GAIN_1 }, 284 + { 1, 250000, 69, VL6180_ALS_GAIN_1_25 }, 285 + { 1, 670000, 68, VL6180_ALS_GAIN_1_67 }, 286 + { 2, 500000, 67, VL6180_ALS_GAIN_2_5 }, 287 + { 5, 0, 66, VL6180_ALS_GAIN_5 }, 288 + { 10, 0, 65, VL6180_ALS_GAIN_10 }, 289 + { 20, 0, 64, VL6180_ALS_GAIN_20 }, 290 + { 40, 0, 71, VL6180_ALS_GAIN_40 } 291 + }; 292 + 293 + static int vl6180_read_raw(struct iio_dev *indio_dev, 294 + struct iio_chan_spec const *chan, 295 + int *val, int *val2, long mask) 296 + { 297 + struct vl6180_data *data = iio_priv(indio_dev); 298 + int ret, i; 299 + 300 + switch (mask) { 301 + case IIO_CHAN_INFO_RAW: 302 + ret = vl6180_measure(data, chan->address); 303 + if (ret < 0) 304 + return ret; 305 + *val = ret; 306 + 307 + return IIO_VAL_INT; 308 + case IIO_CHAN_INFO_INT_TIME: 309 + ret = vl6180_read_word(data->client, VL6180_ALS_IT); 310 + if (ret < 0) 311 + return ret; 312 + *val = 0; /* 1 count = 1ms (0 = 1ms) */ 313 + *val2 = (ret + 1) * 1000; /* convert to seconds */ 314 + 315 + return IIO_VAL_INT_PLUS_MICRO; 316 + case IIO_CHAN_INFO_SCALE: 317 + switch (chan->type) { 318 + case IIO_LIGHT: 319 + *val = 0; /* one ALS count is 0.32 Lux */ 320 + *val2 = 320000; 321 + break; 322 + case IIO_DISTANCE: 323 + *val = 0; /* sensor reports mm, scale to meter */ 324 + *val2 = 1000; 325 + break; 326 + default: 327 + return -EINVAL; 328 + } 329 + 330 + return IIO_VAL_INT_PLUS_MICRO; 331 + case IIO_CHAN_INFO_HARDWAREGAIN: 332 + ret = vl6180_read_byte(data->client, VL6180_ALS_GAIN); 333 + if (ret < 0) 334 + return -EINVAL; 335 + for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) { 336 + if (ret == vl6180_als_gain[i][2]) { 337 + *val = vl6180_als_gain[i][0]; 338 + *val2 = vl6180_als_gain[i][1]; 339 + } 340 + } 341 + 342 + return IIO_VAL_INT_PLUS_MICRO; 343 + default: 344 + return -EINVAL; 345 + } 346 + } 347 + 348 + static IIO_CONST_ATTR(als_gain_available, "1 1.25 1.67 2.5 5 10 20 40"); 349 + 350 + static struct attribute *vl6180_attributes[] = { 351 + &iio_const_attr_als_gain_available.dev_attr.attr, 352 + NULL 353 + }; 354 + 355 + static const struct attribute_group vl6180_attribute_group = { 356 + .attrs = vl6180_attributes, 357 + }; 358 + 359 + /* HOLD is needed before updating any config registers */ 360 + static int vl6180_hold(struct vl6180_data *data, bool hold) 361 + { 362 + return vl6180_write_byte(data->client, VL6180_HOLD, 363 + hold ? VL6180_HOLD_ON : 0); 364 + } 365 + 366 + static int vl6180_set_als_gain(struct vl6180_data *data, int val, int val2) 367 + { 368 + int i, ret; 369 + 370 + for (i = 0; i < ARRAY_SIZE(vl6180_als_gain); i++) { 371 + if (val == vl6180_als_gain[i][0] && 372 + val2 == vl6180_als_gain[i][1]) { 373 + mutex_lock(&data->lock); 374 + ret = vl6180_hold(data, true); 375 + if (ret < 0) 376 + goto fail; 377 + ret = vl6180_write_byte(data->client, VL6180_ALS_GAIN, 378 + vl6180_als_gain[i][3]); 379 + fail: 380 + vl6180_hold(data, false); 381 + mutex_unlock(&data->lock); 382 + return ret; 383 + } 384 + } 385 + 386 + return -EINVAL; 387 + } 388 + 389 + static int vl6180_set_it(struct vl6180_data *data, int val2) 390 + { 391 + int ret; 392 + 393 + mutex_lock(&data->lock); 394 + ret = vl6180_hold(data, true); 395 + if (ret < 0) 396 + goto fail; 397 + ret = vl6180_write_word(data->client, VL6180_ALS_IT, 398 + (val2 - 500) / 1000); /* write value in ms */ 399 + fail: 400 + vl6180_hold(data, false); 401 + mutex_unlock(&data->lock); 402 + 403 + return ret; 404 + } 405 + 406 + static int vl6180_write_raw(struct iio_dev *indio_dev, 407 + struct iio_chan_spec const *chan, 408 + int val, int val2, long mask) 409 + { 410 + struct vl6180_data *data = iio_priv(indio_dev); 411 + 412 + switch (mask) { 413 + case IIO_CHAN_INFO_INT_TIME: 414 + if (val != 0 || val2 < 500 || val2 >= 512500) 415 + return -EINVAL; 416 + 417 + return vl6180_set_it(data, val2); 418 + case IIO_CHAN_INFO_HARDWAREGAIN: 419 + if (chan->type != IIO_LIGHT) 420 + return -EINVAL; 421 + 422 + return vl6180_set_als_gain(data, val, val2); 423 + default: 424 + return -EINVAL; 425 + } 426 + } 427 + 428 + static const struct iio_info vl6180_info = { 429 + .read_raw = vl6180_read_raw, 430 + .write_raw = vl6180_write_raw, 431 + .attrs = &vl6180_attribute_group, 432 + .driver_module = THIS_MODULE, 433 + }; 434 + 435 + static int vl6180_init(struct vl6180_data *data) 436 + { 437 + struct i2c_client *client = data->client; 438 + int ret; 439 + 440 + ret = vl6180_read_byte(client, VL6180_MODEL_ID); 441 + if (ret < 0) 442 + return ret; 443 + 444 + if (ret != VL6180_MODEL_ID_VAL) { 445 + dev_err(&client->dev, "invalid model ID %02x\n", ret); 446 + return -ENODEV; 447 + } 448 + 449 + ret = vl6180_hold(data, true); 450 + if (ret < 0) 451 + return ret; 452 + 453 + ret = vl6180_read_byte(client, VL6180_OUT_OF_RESET); 454 + if (ret < 0) 455 + return ret; 456 + 457 + /* 458 + * Detect false reset condition here. This bit is always set when the 459 + * system comes out of reset. 460 + */ 461 + if (ret != 0x01) 462 + dev_info(&client->dev, "device is not fresh out of reset\n"); 463 + 464 + /* Enable ALS and Range ready interrupts */ 465 + ret = vl6180_write_byte(client, VL6180_INTR_CONFIG, 466 + VL6180_ALS_READY | VL6180_RANGE_READY); 467 + if (ret < 0) 468 + return ret; 469 + 470 + /* ALS integration time: 100ms */ 471 + ret = vl6180_write_word(client, VL6180_ALS_IT, VL6180_ALS_IT_100); 472 + if (ret < 0) 473 + return ret; 474 + 475 + /* ALS gain: 1 */ 476 + ret = vl6180_write_byte(client, VL6180_ALS_GAIN, VL6180_ALS_GAIN_1); 477 + if (ret < 0) 478 + return ret; 479 + 480 + ret = vl6180_write_byte(client, VL6180_OUT_OF_RESET, 0x00); 481 + if (ret < 0) 482 + return ret; 483 + 484 + return vl6180_hold(data, false); 485 + } 486 + 487 + static int vl6180_probe(struct i2c_client *client, 488 + const struct i2c_device_id *id) 489 + { 490 + struct vl6180_data *data; 491 + struct iio_dev *indio_dev; 492 + int ret; 493 + 494 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 495 + if (!indio_dev) 496 + return -ENOMEM; 497 + 498 + data = iio_priv(indio_dev); 499 + i2c_set_clientdata(client, indio_dev); 500 + data->client = client; 501 + mutex_init(&data->lock); 502 + 503 + indio_dev->dev.parent = &client->dev; 504 + indio_dev->info = &vl6180_info; 505 + indio_dev->channels = vl6180_channels; 506 + indio_dev->num_channels = ARRAY_SIZE(vl6180_channels); 507 + indio_dev->name = VL6180_DRV_NAME; 508 + indio_dev->modes = INDIO_DIRECT_MODE; 509 + 510 + ret = vl6180_init(data); 511 + if (ret < 0) 512 + return ret; 513 + 514 + return devm_iio_device_register(&client->dev, indio_dev); 515 + } 516 + 517 + static const struct of_device_id vl6180_of_match[] = { 518 + { .compatible = "st,vl6180", }, 519 + { }, 520 + }; 521 + MODULE_DEVICE_TABLE(of, vl6180_of_match); 522 + 523 + static const struct i2c_device_id vl6180_id[] = { 524 + { "vl6180", 0 }, 525 + { } 526 + }; 527 + MODULE_DEVICE_TABLE(i2c, vl6180_id); 528 + 529 + static struct i2c_driver vl6180_driver = { 530 + .driver = { 531 + .name = VL6180_DRV_NAME, 532 + .of_match_table = of_match_ptr(vl6180_of_match), 533 + }, 534 + .probe = vl6180_probe, 535 + .id_table = vl6180_id, 536 + }; 537 + 538 + module_i2c_driver(vl6180_driver); 539 + 540 + MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); 541 + MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>"); 542 + MODULE_DESCRIPTION("STMicro VL6180 ALS, range and proximity sensor driver"); 543 + MODULE_LICENSE("GPL");
+2 -2
drivers/iio/pressure/zpa2326.c
··· 751 751 */ 752 752 static irqreturn_t zpa2326_handle_irq(int irq, void *data) 753 753 { 754 - struct iio_dev *indio_dev = (struct iio_dev *)data; 754 + struct iio_dev *indio_dev = data; 755 755 756 756 if (iio_buffer_enabled(indio_dev)) { 757 757 /* Timestamping needed for buffered sampling only. */ ··· 790 790 */ 791 791 static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data) 792 792 { 793 - struct iio_dev *indio_dev = (struct iio_dev *)data; 793 + struct iio_dev *indio_dev = data; 794 794 struct zpa2326_private *priv = iio_priv(indio_dev); 795 795 unsigned int val; 796 796 bool cont;
+1 -1
drivers/iio/proximity/as3935.c
··· 155 155 NULL, 156 156 }; 157 157 158 - static struct attribute_group as3935_attribute_group = { 158 + static const struct attribute_group as3935_attribute_group = { 159 159 .attrs = as3935_attributes, 160 160 }; 161 161
-3
drivers/staging/iio/accel/adis16203.c
··· 203 203 return IIO_VAL_INT; 204 204 case IIO_CHAN_INFO_CALIBBIAS: 205 205 bits = 14; 206 - mutex_lock(&indio_dev->mlock); 207 206 addr = adis16203_addresses[chan->scan_index]; 208 207 ret = adis_read_reg_16(st, addr, &val16); 209 208 if (ret) { 210 - mutex_unlock(&indio_dev->mlock); 211 209 return ret; 212 210 } 213 211 val16 &= (1 << bits) - 1; 214 212 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); 215 213 *val = val16; 216 - mutex_unlock(&indio_dev->mlock); 217 214 return IIO_VAL_INT; 218 215 default: 219 216 return -EINVAL;
+1 -2
drivers/staging/iio/accel/adis16209.c
··· 255 255 } 256 256 addr = adis16209_addresses[chan->scan_index][0]; 257 257 ret = adis_read_reg_16(st, addr, &val16); 258 - if (ret) { 258 + if (ret) 259 259 return ret; 260 - } 261 260 val16 &= (1 << bits) - 1; 262 261 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); 263 262 *val = val16;
+2 -4
drivers/staging/iio/accel/adis16240.c
··· 290 290 bits = 10; 291 291 addr = adis16240_addresses[chan->scan_index][0]; 292 292 ret = adis_read_reg_16(st, addr, &val16); 293 - if (ret) { 293 + if (ret) 294 294 return ret; 295 - } 296 295 val16 &= (1 << bits) - 1; 297 296 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); 298 297 *val = val16; ··· 300 301 bits = 10; 301 302 addr = adis16240_addresses[chan->scan_index][1]; 302 303 ret = adis_read_reg_16(st, addr, &val16); 303 - if (ret) { 304 + if (ret) 304 305 return ret; 305 - } 306 306 val16 &= (1 << bits) - 1; 307 307 val16 = (s16)(val16 << (16 - bits)) >> (16 - bits); 308 308 *val = val16;
+6 -6
drivers/staging/iio/adc/ad7280a.c
··· 560 560 st->iio_attr[cnt].address = 561 561 AD7280A_DEVADDR(dev) << 8 | ch; 562 562 st->iio_attr[cnt].dev_attr.attr.mode = 563 - S_IWUSR | S_IRUGO; 563 + 0644; 564 564 st->iio_attr[cnt].dev_attr.show = 565 565 ad7280_show_balance_sw; 566 566 st->iio_attr[cnt].dev_attr.store = ··· 577 577 AD7280A_DEVADDR(dev) << 8 | 578 578 (AD7280A_CB1_TIMER + ch); 579 579 st->iio_attr[cnt].dev_attr.attr.mode = 580 - S_IWUSR | S_IRUGO; 580 + 0644; 581 581 st->iio_attr[cnt].dev_attr.show = 582 582 ad7280_show_balance_timer; 583 583 st->iio_attr[cnt].dev_attr.store = ··· 746 746 747 747 static IIO_DEVICE_ATTR_NAMED(in_thresh_low_value, 748 748 in_voltage-voltage_thresh_low_value, 749 - S_IRUGO | S_IWUSR, 749 + 0644, 750 750 ad7280_read_channel_config, 751 751 ad7280_write_channel_config, 752 752 AD7280A_CELL_UNDERVOLTAGE); 753 753 754 754 static IIO_DEVICE_ATTR_NAMED(in_thresh_high_value, 755 755 in_voltage-voltage_thresh_high_value, 756 - S_IRUGO | S_IWUSR, 756 + 0644, 757 757 ad7280_read_channel_config, 758 758 ad7280_write_channel_config, 759 759 AD7280A_CELL_OVERVOLTAGE); 760 760 761 761 static IIO_DEVICE_ATTR(in_temp_thresh_low_value, 762 - S_IRUGO | S_IWUSR, 762 + 0644, 763 763 ad7280_read_channel_config, 764 764 ad7280_write_channel_config, 765 765 AD7280A_AUX_ADC_UNDERVOLTAGE); 766 766 767 767 static IIO_DEVICE_ATTR(in_temp_thresh_high_value, 768 - S_IRUGO | S_IWUSR, 768 + 0644, 769 769 ad7280_read_channel_config, 770 770 ad7280_write_channel_config, 771 771 AD7280A_AUX_ADC_OVERVOLTAGE);
+5 -4
drivers/staging/iio/adc/ad7606.c
··· 208 208 switch (mask) { 209 209 case IIO_CHAN_INFO_SCALE: 210 210 ret = -EINVAL; 211 - mutex_lock(&indio_dev->mlock); 211 + mutex_lock(&st->lock); 212 212 for (i = 0; i < ARRAY_SIZE(scale_avail); i++) 213 213 if (val2 == scale_avail[i][1]) { 214 214 gpiod_set_value(st->gpio_range, i); ··· 217 217 ret = 0; 218 218 break; 219 219 } 220 - mutex_unlock(&indio_dev->mlock); 220 + mutex_unlock(&st->lock); 221 221 222 222 return ret; 223 223 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: ··· 231 231 values[1] = (ret >> 1) & 1; 232 232 values[2] = (ret >> 2) & 1; 233 233 234 - mutex_lock(&indio_dev->mlock); 234 + mutex_lock(&st->lock); 235 235 gpiod_set_array_value(ARRAY_SIZE(values), st->gpio_os->desc, 236 236 values); 237 237 st->oversampling = val; 238 - mutex_unlock(&indio_dev->mlock); 238 + mutex_unlock(&st->lock); 239 239 240 240 return 0; 241 241 default: ··· 413 413 st = iio_priv(indio_dev); 414 414 415 415 st->dev = dev; 416 + mutex_init(&st->lock); 416 417 st->bops = bops; 417 418 st->base_address = base_address; 418 419 /* tied to logic low, analog input range is +/- 5V */
+3
drivers/staging/iio/adc/ad7606.h
··· 14 14 * @name: identification string for chip 15 15 * @channels: channel specification 16 16 * @num_channels: number of channels 17 + * @lock protect sensor state 17 18 */ 18 19 19 20 struct ad7606_chip_info { ··· 24 23 25 24 /** 26 25 * struct ad7606_state - driver instance specific data 26 + * @lock protect sensor state 27 27 */ 28 28 29 29 struct ad7606_state { ··· 39 37 bool done; 40 38 void __iomem *base_address; 41 39 40 + struct mutex lock; /* protect sensor state */ 42 41 struct gpio_desc *gpio_convst; 43 42 struct gpio_desc *gpio_reset; 44 43 struct gpio_desc *gpio_range;
+1 -4
drivers/staging/iio/addac/adt7316.c
··· 244 244 chip->config1 = config1; 245 245 246 246 return ret; 247 - 248 247 } 249 248 250 249 static ssize_t adt7316_store_enabled(struct device *dev, ··· 432 433 433 434 config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MASK); 434 435 } 435 - 436 436 437 437 config2 |= data; 438 438 ··· 723 725 adt7316_store_AIN_internal_Vref, 724 726 0); 725 727 726 - 727 728 static ssize_t adt7316_show_enable_prop_DACA(struct device *dev, 728 729 struct device_attribute *attr, 729 730 char *buf) ··· 921 924 922 925 static IIO_DEVICE_ATTR(all_DAC_update_modes, 0444, 923 926 adt7316_show_all_DAC_update_modes, NULL, 0); 924 - 925 927 926 928 static ssize_t adt7316_store_update_DAC(struct device *dev, 927 929 struct device_attribute *attr, ··· 1863 1867 1864 1868 return len; 1865 1869 } 1870 + 1866 1871 static inline ssize_t adt7316_show_ad_bound(struct device *dev, 1867 1872 struct device_attribute *attr, 1868 1873 char *buf)
+1 -1
drivers/staging/iio/cdc/ad7150.c
··· 232 232 if (ret < 0) 233 233 goto error_ret; 234 234 235 - cfg = ret & ~((0x03 << 5) | (0x1 << 7)); 235 + cfg = ret & ~((0x03 << 5) | BIT(7)); 236 236 237 237 switch (type) { 238 238 case IIO_EV_TYPE_MAG_ADAPTIVE:
+1
drivers/staging/iio/cdc/ad7152.c
··· 244 244 245 245 return ret; 246 246 } 247 + 247 248 static int ad7152_write_raw(struct iio_dev *indio_dev, 248 249 struct iio_chan_spec const *chan, 249 250 int val,
+9 -9
drivers/staging/iio/cdc/ad7746.c
··· 45 45 #define AD7746_STATUS_RDYCAP BIT(0) 46 46 47 47 /* Capacitive Channel Setup Register Bit Designations (AD7746_REG_CAP_SETUP) */ 48 - #define AD7746_CAPSETUP_CAPEN (1 << 7) 49 - #define AD7746_CAPSETUP_CIN2 (1 << 6) /* AD7746 only */ 50 - #define AD7746_CAPSETUP_CAPDIFF (1 << 5) 51 - #define AD7746_CAPSETUP_CACHOP (1 << 0) 48 + #define AD7746_CAPSETUP_CAPEN BIT(7) 49 + #define AD7746_CAPSETUP_CIN2 BIT(6) /* AD7746 only */ 50 + #define AD7746_CAPSETUP_CAPDIFF BIT(5) 51 + #define AD7746_CAPSETUP_CACHOP BIT(0) 52 52 53 53 /* Voltage/Temperature Setup Register Bit Designations (AD7746_REG_VT_SETUP) */ 54 54 #define AD7746_VTSETUP_VTEN (1 << 7) ··· 56 56 #define AD7746_VTSETUP_VTMD_EXT_TEMP (1 << 5) 57 57 #define AD7746_VTSETUP_VTMD_VDD_MON (2 << 5) 58 58 #define AD7746_VTSETUP_VTMD_EXT_VIN (3 << 5) 59 - #define AD7746_VTSETUP_EXTREF (1 << 4) 60 - #define AD7746_VTSETUP_VTSHORT (1 << 1) 61 - #define AD7746_VTSETUP_VTCHOP (1 << 0) 59 + #define AD7746_VTSETUP_EXTREF BIT(4) 60 + #define AD7746_VTSETUP_VTSHORT BIT(1) 61 + #define AD7746_VTSETUP_VTCHOP BIT(0) 62 62 63 63 /* Excitation Setup Register Bit Designations (AD7746_REG_EXC_SETUP) */ 64 64 #define AD7746_EXCSETUP_CLKCTRL BIT(7) ··· 82 82 #define AD7746_CONF_MODE_GAIN_CAL (6 << 0) 83 83 84 84 /* CAPDAC Register Bit Designations (AD7746_REG_CAPDACx) */ 85 - #define AD7746_CAPDAC_DACEN (1 << 7) 85 + #define AD7746_CAPDAC_DACEN BIT(7) 86 86 #define AD7746_CAPDAC_DACP(x) ((x) & 0x7F) 87 87 88 88 /* ··· 547 547 548 548 regval = chip->config | AD7746_CONF_MODE_SINGLE_CONV; 549 549 ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG, 550 - regval); 550 + regval); 551 551 if (ret < 0) 552 552 goto out; 553 553
+10 -10
drivers/staging/iio/frequency/ad9832.c
··· 250 250 * see dds.h for further information 251 251 */ 252 252 253 - static IIO_DEV_ATTR_FREQ(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ0HM); 254 - static IIO_DEV_ATTR_FREQ(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_FREQ1HM); 255 - static IIO_DEV_ATTR_FREQSYMBOL(0, S_IWUSR, NULL, ad9832_write, AD9832_FREQ_SYM); 253 + static IIO_DEV_ATTR_FREQ(0, 0, 0200, NULL, ad9832_write, AD9832_FREQ0HM); 254 + static IIO_DEV_ATTR_FREQ(0, 1, 0200, NULL, ad9832_write, AD9832_FREQ1HM); 255 + static IIO_DEV_ATTR_FREQSYMBOL(0, 0200, NULL, ad9832_write, AD9832_FREQ_SYM); 256 256 static IIO_CONST_ATTR_FREQ_SCALE(0, "1"); /* 1Hz */ 257 257 258 - static IIO_DEV_ATTR_PHASE(0, 0, S_IWUSR, NULL, ad9832_write, AD9832_PHASE0H); 259 - static IIO_DEV_ATTR_PHASE(0, 1, S_IWUSR, NULL, ad9832_write, AD9832_PHASE1H); 260 - static IIO_DEV_ATTR_PHASE(0, 2, S_IWUSR, NULL, ad9832_write, AD9832_PHASE2H); 261 - static IIO_DEV_ATTR_PHASE(0, 3, S_IWUSR, NULL, ad9832_write, AD9832_PHASE3H); 262 - static IIO_DEV_ATTR_PHASESYMBOL(0, S_IWUSR, NULL, 258 + static IIO_DEV_ATTR_PHASE(0, 0, 0200, NULL, ad9832_write, AD9832_PHASE0H); 259 + static IIO_DEV_ATTR_PHASE(0, 1, 0200, NULL, ad9832_write, AD9832_PHASE1H); 260 + static IIO_DEV_ATTR_PHASE(0, 2, 0200, NULL, ad9832_write, AD9832_PHASE2H); 261 + static IIO_DEV_ATTR_PHASE(0, 3, 0200, NULL, ad9832_write, AD9832_PHASE3H); 262 + static IIO_DEV_ATTR_PHASESYMBOL(0, 0200, NULL, 263 263 ad9832_write, AD9832_PHASE_SYM); 264 264 static IIO_CONST_ATTR_PHASE_SCALE(0, "0.0015339808"); /* 2PI/2^12 rad*/ 265 265 266 - static IIO_DEV_ATTR_PINCONTROL_EN(0, S_IWUSR, NULL, 266 + static IIO_DEV_ATTR_PINCONTROL_EN(0, 0200, NULL, 267 267 ad9832_write, AD9832_PINCTRL_EN); 268 - static IIO_DEV_ATTR_OUT_ENABLE(0, S_IWUSR, NULL, 268 + static IIO_DEV_ATTR_OUT_ENABLE(0, 0200, NULL, 269 269 ad9832_write, AD9832_OUTPUT_EN); 270 270 271 271 static struct attribute *ad9832_attributes[] = {
+10 -25
drivers/staging/iio/gyro/adis16060_core.c
··· 40 40 41 41 static struct iio_dev *adis16060_iio_dev; 42 42 43 - static int adis16060_spi_write(struct iio_dev *indio_dev, u8 val) 43 + static int adis16060_spi_write_then_read(struct iio_dev *indio_dev, 44 + u8 conf, u16 *val) 44 45 { 45 46 int ret; 46 47 struct adis16060_state *st = iio_priv(indio_dev); 47 48 48 49 mutex_lock(&st->buf_lock); 49 - st->buf[2] = val; /* The last 8 bits clocked in are latched */ 50 + st->buf[2] = conf; /* The last 8 bits clocked in are latched */ 50 51 ret = spi_write(st->us_w, st->buf, 3); 51 - mutex_unlock(&st->buf_lock); 52 52 53 - return ret; 54 - } 55 - 56 - static int adis16060_spi_read(struct iio_dev *indio_dev, u16 *val) 57 - { 58 - int ret; 59 - struct adis16060_state *st = iio_priv(indio_dev); 60 - 61 - mutex_lock(&st->buf_lock); 53 + if (ret < 0) { 54 + mutex_unlock(&st->buf_lock); 55 + return ret; 56 + } 62 57 63 58 ret = spi_read(st->us_r, st->buf, 3); 64 59 ··· 81 86 82 87 switch (mask) { 83 88 case IIO_CHAN_INFO_RAW: 84 - /* Take the iio_dev status lock */ 85 - mutex_lock(&indio_dev->mlock); 86 - ret = adis16060_spi_write(indio_dev, chan->address); 89 + ret = adis16060_spi_write_then_read(indio_dev, 90 + chan->address, &tval); 87 91 if (ret < 0) 88 - goto out_unlock; 92 + return ret; 89 93 90 - ret = adis16060_spi_read(indio_dev, &tval); 91 - if (ret < 0) 92 - goto out_unlock; 93 - 94 - mutex_unlock(&indio_dev->mlock); 95 94 *val = tval; 96 95 return IIO_VAL_INT; 97 96 case IIO_CHAN_INFO_OFFSET: ··· 99 110 } 100 111 101 112 return -EINVAL; 102 - 103 - out_unlock: 104 - mutex_unlock(&indio_dev->mlock); 105 - return ret; 106 113 } 107 114 108 115 static const struct iio_info adis16060_info = {
+2 -2
drivers/staging/iio/light/tsl2x7x_core.c
··· 854 854 tmp = data[i] - statP->mean; 855 855 sample_sum += tmp * tmp; 856 856 } 857 - statP->stddev = int_sqrt((long)sample_sum) / length; 857 + statP->stddev = int_sqrt((long)sample_sum / length); 858 858 } 859 859 860 860 /** ··· 1676 1676 }, 1677 1677 }; 1678 1678 1679 - static struct attribute_group tsl2X7X_event_attr_group_tbl[] = { 1679 + static const struct attribute_group tsl2X7X_event_attr_group_tbl[] = { 1680 1680 [ALS] = { 1681 1681 .attrs = tsl2X7X_ALS_event_attrs, 1682 1682 .name = "events",
+4 -4
drivers/staging/iio/meter/ade7753.c
··· 83 83 * @buf_lock: mutex to protect tx and rx 84 84 **/ 85 85 struct ade7753_state { 86 - struct spi_device *us; 87 - struct mutex buf_lock; 88 - u8 tx[ADE7753_MAX_TX] ____cacheline_aligned; 89 - u8 rx[ADE7753_MAX_RX]; 86 + struct spi_device *us; 87 + struct mutex buf_lock; 88 + u8 tx[ADE7753_MAX_TX] ____cacheline_aligned; 89 + u8 rx[ADE7753_MAX_RX]; 90 90 }; 91 91 92 92 static int ade7753_spi_write_reg_8(struct device *dev,
+21 -14
drivers/staging/iio/meter/ade7754.c
··· 6 6 * Licensed under the GPL-2 or later. 7 7 */ 8 8 9 + #include <linux/delay.h> 10 + #include <linux/device.h> 9 11 #include <linux/interrupt.h> 10 12 #include <linux/irq.h> 11 - #include <linux/delay.h> 12 - #include <linux/mutex.h> 13 - #include <linux/device.h> 14 13 #include <linux/kernel.h> 14 + #include <linux/list.h> 15 + #include <linux/module.h> 16 + #include <linux/mutex.h> 15 17 #include <linux/spi/spi.h> 16 18 #include <linux/slab.h> 17 19 #include <linux/sysfs.h> 18 - #include <linux/list.h> 19 - #include <linux/module.h> 20 - 21 20 #include <linux/iio/iio.h> 22 21 #include <linux/iio/sysfs.h> 23 22 #include "meter.h" ··· 96 97 /** 97 98 * struct ade7754_state - device instance specific data 98 99 * @us: actual spi_device 99 - * @buf_lock: mutex to protect tx and rx 100 + * @buf_lock: mutex to protect tx, rx and write frequency 100 101 * @tx: transmit buffer 101 102 * @rx: receive buffer 102 103 **/ ··· 107 108 u8 rx[ADE7754_MAX_RX]; 108 109 }; 109 110 111 + /* Unlocked version of ade7754_spi_write_reg_8 function */ 112 + static int __ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) 113 + { 114 + struct iio_dev *indio_dev = dev_to_iio_dev(dev); 115 + struct ade7754_state *st = iio_priv(indio_dev); 116 + 117 + st->tx[0] = ADE7754_WRITE_REG(reg_address); 118 + st->tx[1] = val; 119 + return spi_write(st->us, st->tx, 2); 120 + } 121 + 110 122 static int ade7754_spi_write_reg_8(struct device *dev, u8 reg_address, u8 val) 111 123 { 112 124 int ret; ··· 125 115 struct ade7754_state *st = iio_priv(indio_dev); 126 116 127 117 mutex_lock(&st->buf_lock); 128 - st->tx[0] = ADE7754_WRITE_REG(reg_address); 129 - st->tx[1] = val; 130 - 131 - ret = spi_write(st->us, st->tx, 2); 118 + ret = __ade7754_spi_write_reg_8(dev, reg_address, val); 132 119 mutex_unlock(&st->buf_lock); 133 120 134 121 return ret; ··· 520 513 if (!val) 521 514 return -EINVAL; 522 515 523 - mutex_lock(&indio_dev->mlock); 516 + mutex_lock(&st->buf_lock); 524 517 525 518 t = 26000 / val; 526 519 if (t > 0) ··· 538 531 reg &= ~(3 << 3); 539 532 reg |= t << 3; 540 533 541 - ret = ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg); 534 + ret = __ade7754_spi_write_reg_8(dev, ADE7754_WAVMODE, reg); 542 535 543 536 out: 544 - mutex_unlock(&indio_dev->mlock); 537 + mutex_unlock(&st->buf_lock); 545 538 546 539 return ret ? ret : len; 547 540 }
+4
include/linux/hid-sensor-ids.h
··· 49 49 #define HID_USAGE_SENSOR_TEMPERATURE 0x200033 50 50 #define HID_USAGE_SENSOR_DATA_ENVIRONMENTAL_TEMPERATURE 0x200434 51 51 52 + /* humidity */ 53 + #define HID_USAGE_SENSOR_HUMIDITY 0x200032 54 + #define HID_USAGE_SENSOR_ATMOSPHERIC_HUMIDITY 0x200433 55 + 52 56 /* Gyro 3D: (200076) */ 53 57 #define HID_USAGE_SENSOR_GYRO_3D 0x200076 54 58 #define HID_USAGE_SENSOR_DATA_ANGL_VELOCITY 0x200456