diff options
96 files changed, 6016 insertions, 412 deletions
diff --git a/Documentation/ABI/testing/sysfs-bus-iio b/Documentation/ABI/testing/sysfs-bus-iio index f155eff910f9..df44998e7506 100644 --- a/Documentation/ABI/testing/sysfs-bus-iio +++ b/Documentation/ABI/testing/sysfs-bus-iio | |||
@@ -1512,3 +1512,56 @@ Contact: linux-iio@vger.kernel.org | |||
1512 | Description: | 1512 | Description: |
1513 | Raw (unscaled no offset etc.) pH reading of a substance as a negative | 1513 | Raw (unscaled no offset etc.) pH reading of a substance as a negative |
1514 | base-10 logarithm of hydrodium ions in a litre of water. | 1514 | base-10 logarithm of hydrodium ions in a litre of water. |
1515 | |||
1516 | What: /sys/bus/iio/devices/iio:deviceX/mount_matrix | ||
1517 | What: /sys/bus/iio/devices/iio:deviceX/in_mount_matrix | ||
1518 | What: /sys/bus/iio/devices/iio:deviceX/out_mount_matrix | ||
1519 | What: /sys/bus/iio/devices/iio:deviceX/in_anglvel_mount_matrix | ||
1520 | What: /sys/bus/iio/devices/iio:deviceX/in_accel_mount_matrix | ||
1521 | KernelVersion: 4.6 | ||
1522 | Contact: linux-iio@vger.kernel.org | ||
1523 | Description: | ||
1524 | Mounting matrix for IIO sensors. This is a rotation matrix which | ||
1525 | informs userspace about sensor chip's placement relative to the | ||
1526 | main hardware it is mounted on. | ||
1527 | Main hardware placement is defined according to the local | ||
1528 | reference frame related to the physical quantity the sensor | ||
1529 | measures. | ||
1530 | Given that the rotation matrix is defined in a board specific | ||
1531 | way (platform data and / or device-tree), the main hardware | ||
1532 | reference frame definition is left to the implementor's choice | ||
1533 | (see below for a magnetometer example). | ||
1534 | Applications should apply this rotation matrix to samples so | ||
1535 | that when main hardware reference frame is aligned onto local | ||
1536 | reference frame, then sensor chip reference frame is also | ||
1537 | perfectly aligned with it. | ||
1538 | Matrix is a 3x3 unitary matrix and typically looks like | ||
1539 | [0, 1, 0; 1, 0, 0; 0, 0, -1]. Identity matrix | ||
1540 | [1, 0, 0; 0, 1, 0; 0, 0, 1] means sensor chip and main hardware | ||
1541 | are perfectly aligned with each other. | ||
1542 | |||
1543 | For example, a mounting matrix for a magnetometer sensor informs | ||
1544 | userspace about sensor chip's ORIENTATION relative to the main | ||
1545 | hardware. | ||
1546 | More specifically, main hardware orientation is defined with | ||
1547 | respect to the LOCAL EARTH GEOMAGNETIC REFERENCE FRAME where : | ||
1548 | * Y is in the ground plane and positive towards magnetic North ; | ||
1549 | * X is in the ground plane, perpendicular to the North axis and | ||
1550 | positive towards the East ; | ||
1551 | * Z is perpendicular to the ground plane and positive upwards. | ||
1552 | |||
1553 | An implementor might consider that for a hand-held device, a | ||
1554 | 'natural' orientation would be 'front facing camera at the top'. | ||
1555 | The main hardware reference frame could then be described as : | ||
1556 | * Y is in the plane of the screen and is positive towards the | ||
1557 | top of the screen ; | ||
1558 | * X is in the plane of the screen, perpendicular to Y axis, and | ||
1559 | positive towards the right hand side of the screen ; | ||
1560 | * Z is perpendicular to the screen plane and positive out of the | ||
1561 | screen. | ||
1562 | Another example for a quadrotor UAV might be : | ||
1563 | * Y is in the plane of the propellers and positive towards the | ||
1564 | front-view camera; | ||
1565 | * X is in the plane of the propellers, perpendicular to Y axis, | ||
1566 | and positive towards the starboard side of the UAV ; | ||
1567 | * Z is perpendicular to propellers plane and positive upwards. | ||
diff --git a/Documentation/devicetree/bindings/staging/iio/adc/mxs-lradc.txt b/Documentation/devicetree/bindings/iio/adc/mxs-lradc.txt index 555fb117d4fa..555fb117d4fa 100644 --- a/Documentation/devicetree/bindings/staging/iio/adc/mxs-lradc.txt +++ b/Documentation/devicetree/bindings/iio/adc/mxs-lradc.txt | |||
diff --git a/Documentation/devicetree/bindings/iio/dac/ad5592r.txt b/Documentation/devicetree/bindings/iio/dac/ad5592r.txt new file mode 100644 index 000000000000..989f96f31c66 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/dac/ad5592r.txt | |||
@@ -0,0 +1,155 @@ | |||
1 | Analog Devices AD5592R/AD5593R DAC/ADC device driver | ||
2 | |||
3 | Required properties for the AD5592R: | ||
4 | - compatible: Must be "adi,ad5592r" | ||
5 | - reg: SPI chip select number for the device | ||
6 | - spi-max-frequency: Max SPI frequency to use (< 30000000) | ||
7 | - spi-cpol: The AD5592R requires inverse clock polarity (CPOL) mode | ||
8 | |||
9 | Required properties for the AD5593R: | ||
10 | - compatible: Must be "adi,ad5593r" | ||
11 | - reg: I2C address of the device | ||
12 | |||
13 | Required properties for all supported chips: | ||
14 | - #address-cells: Should be 1. | ||
15 | - #size-cells: Should be 0. | ||
16 | - channel nodes: | ||
17 | Each child node represents one channel and has the following | ||
18 | Required properties: | ||
19 | * reg: Pin on which this channel is connected to. | ||
20 | * adi,mode: Mode or function of this channel. | ||
21 | Macros specifying the valid values | ||
22 | can be found in <dt-bindings/iio/adi,ad5592r.h>. | ||
23 | |||
24 | The following values are currently supported: | ||
25 | * CH_MODE_UNUSED (the pin is unused) | ||
26 | * CH_MODE_ADC (the pin is ADC input) | ||
27 | * CH_MODE_DAC (the pin is DAC output) | ||
28 | * CH_MODE_DAC_AND_ADC (the pin is DAC output | ||
29 | but can be monitored by an ADC, since | ||
30 | there is no disadvantage this | ||
31 | this should be considered as the | ||
32 | preferred DAC mode) | ||
33 | * CH_MODE_GPIO (the pin is registered | ||
34 | with GPIOLIB) | ||
35 | Optional properties: | ||
36 | * adi,off-state: State of this channel when unused or the | ||
37 | device gets removed. Macros specifying the | ||
38 | valid values can be found in | ||
39 | <dt-bindings/iio/adi,ad5592r.h>. | ||
40 | |||
41 | * CH_OFFSTATE_PULLDOWN (the pin is pulled down) | ||
42 | * CH_OFFSTATE_OUT_LOW (the pin is output low) | ||
43 | * CH_OFFSTATE_OUT_HIGH (the pin is output high) | ||
44 | * CH_OFFSTATE_OUT_TRISTATE (the pin is | ||
45 | tristated output) | ||
46 | |||
47 | |||
48 | Optional properties: | ||
49 | - vref-supply: Phandle to the external reference voltage supply. This should | ||
50 | only be set if there is an external reference voltage connected to the VREF | ||
51 | pin. If the property is not set the internal 2.5V reference is used. | ||
52 | - reset-gpios : GPIO spec for the RESET pin. If specified, it will be | ||
53 | asserted during driver probe. | ||
54 | - gpio-controller: Marks the device node as a GPIO controller. | ||
55 | - #gpio-cells: Should be 2. The first cell is the GPIO number and the second | ||
56 | cell specifies GPIO flags, as defined in <dt-bindings/gpio/gpio.h>. | ||
57 | |||
58 | AD5592R Example: | ||
59 | |||
60 | #include <dt-bindings/iio/adi,ad5592r.h> | ||
61 | |||
62 | vref: regulator-vref { | ||
63 | compatible = "regulator-fixed"; | ||
64 | regulator-name = "vref-ad559x"; | ||
65 | regulator-min-microvolt = <3300000>; | ||
66 | regulator-max-microvolt = <3300000>; | ||
67 | regulator-always-on; | ||
68 | }; | ||
69 | |||
70 | ad5592r@0 { | ||
71 | #size-cells = <0>; | ||
72 | #address-cells = <1>; | ||
73 | #gpio-cells = <2>; | ||
74 | compatible = "adi,ad5592r"; | ||
75 | reg = <0>; | ||
76 | |||
77 | spi-max-frequency = <1000000>; | ||
78 | spi-cpol; | ||
79 | |||
80 | vref-supply = <&vref>; /* optional */ | ||
81 | reset-gpios = <&gpio0 86 0>; /* optional */ | ||
82 | gpio-controller; | ||
83 | |||
84 | channel@0 { | ||
85 | reg = <0>; | ||
86 | adi,mode = <CH_MODE_DAC>; | ||
87 | }; | ||
88 | channel@1 { | ||
89 | reg = <1>; | ||
90 | adi,mode = <CH_MODE_ADC>; | ||
91 | }; | ||
92 | channel@2 { | ||
93 | reg = <2>; | ||
94 | adi,mode = <CH_MODE_DAC_AND_ADC>; | ||
95 | }; | ||
96 | channel@3 { | ||
97 | reg = <3>; | ||
98 | adi,mode = <CH_MODE_DAC_AND_ADC>; | ||
99 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
100 | }; | ||
101 | channel@4 { | ||
102 | reg = <4>; | ||
103 | adi,mode = <CH_MODE_UNUSED>; | ||
104 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
105 | }; | ||
106 | channel@5 { | ||
107 | reg = <5>; | ||
108 | adi,mode = <CH_MODE_GPIO>; | ||
109 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
110 | }; | ||
111 | channel@6 { | ||
112 | reg = <6>; | ||
113 | adi,mode = <CH_MODE_GPIO>; | ||
114 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
115 | }; | ||
116 | channel@7 { | ||
117 | reg = <7>; | ||
118 | adi,mode = <CH_MODE_GPIO>; | ||
119 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
120 | }; | ||
121 | }; | ||
122 | |||
123 | AD5593R Example: | ||
124 | |||
125 | #include <dt-bindings/iio/adi,ad5592r.h> | ||
126 | |||
127 | ad5593r@10 { | ||
128 | #size-cells = <0>; | ||
129 | #address-cells = <1>; | ||
130 | #gpio-cells = <2>; | ||
131 | compatible = "adi,ad5593r"; | ||
132 | reg = <0x10>; | ||
133 | gpio-controller; | ||
134 | |||
135 | channel@0 { | ||
136 | reg = <0>; | ||
137 | adi,mode = <CH_MODE_DAC>; | ||
138 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
139 | }; | ||
140 | channel@1 { | ||
141 | reg = <1>; | ||
142 | adi,mode = <CH_MODE_ADC>; | ||
143 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
144 | }; | ||
145 | channel@2 { | ||
146 | reg = <2>; | ||
147 | adi,mode = <CH_MODE_DAC_AND_ADC>; | ||
148 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
149 | }; | ||
150 | channel@6 { | ||
151 | reg = <6>; | ||
152 | adi,mode = <CH_MODE_GPIO>; | ||
153 | adi,off-state = <CH_OFFSTATE_PULLDOWN>; | ||
154 | }; | ||
155 | }; | ||
diff --git a/Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt b/Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt index e4d8f1c52f4a..a9fc11e43b45 100644 --- a/Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt +++ b/Documentation/devicetree/bindings/iio/imu/inv_mpu6050.txt | |||
@@ -8,10 +8,23 @@ Required properties: | |||
8 | - interrupt-parent : should be the phandle for the interrupt controller | 8 | - interrupt-parent : should be the phandle for the interrupt controller |
9 | - interrupts : interrupt mapping for GPIO IRQ | 9 | - interrupts : interrupt mapping for GPIO IRQ |
10 | 10 | ||
11 | Optional properties: | ||
12 | - mount-matrix: an optional 3x3 mounting rotation matrix | ||
13 | |||
14 | |||
11 | Example: | 15 | Example: |
12 | mpu6050@68 { | 16 | mpu6050@68 { |
13 | compatible = "invensense,mpu6050"; | 17 | compatible = "invensense,mpu6050"; |
14 | reg = <0x68>; | 18 | reg = <0x68>; |
15 | interrupt-parent = <&gpio1>; | 19 | interrupt-parent = <&gpio1>; |
16 | interrupts = <18 1>; | 20 | interrupts = <18 1>; |
21 | mount-matrix = "-0.984807753012208", /* x0 */ | ||
22 | "0", /* y0 */ | ||
23 | "-0.173648177666930", /* z0 */ | ||
24 | "0", /* x1 */ | ||
25 | "-1", /* y1 */ | ||
26 | "0", /* z1 */ | ||
27 | "-0.173648177666930", /* x2 */ | ||
28 | "0", /* y2 */ | ||
29 | "0.984807753012208"; /* z2 */ | ||
17 | }; | 30 | }; |
diff --git a/Documentation/devicetree/bindings/iio/magnetometer/ak8975.txt b/Documentation/devicetree/bindings/iio/magnetometer/ak8975.txt index 34a3206eefdf..e1e7dd3259f6 100644 --- a/Documentation/devicetree/bindings/iio/magnetometer/ak8975.txt +++ b/Documentation/devicetree/bindings/iio/magnetometer/ak8975.txt | |||
@@ -9,6 +9,7 @@ Optional properties: | |||
9 | 9 | ||
10 | - gpios : should be device tree identifier of the magnetometer DRDY pin | 10 | - gpios : should be device tree identifier of the magnetometer DRDY pin |
11 | - vdd-supply: an optional regulator that needs to be on to provide VDD | 11 | - vdd-supply: an optional regulator that needs to be on to provide VDD |
12 | - mount-matrix: an optional 3x3 mounting rotation matrix | ||
12 | 13 | ||
13 | Example: | 14 | Example: |
14 | 15 | ||
@@ -17,4 +18,13 @@ ak8975@0c { | |||
17 | reg = <0x0c>; | 18 | reg = <0x0c>; |
18 | gpios = <&gpj0 7 0>; | 19 | gpios = <&gpj0 7 0>; |
19 | vdd-supply = <&ldo_3v3_gnss>; | 20 | vdd-supply = <&ldo_3v3_gnss>; |
21 | mount-matrix = "-0.984807753012208", /* x0 */ | ||
22 | "0", /* y0 */ | ||
23 | "-0.173648177666930", /* z0 */ | ||
24 | "0", /* x1 */ | ||
25 | "-1", /* y1 */ | ||
26 | "0", /* z1 */ | ||
27 | "-0.173648177666930", /* x2 */ | ||
28 | "0", /* y2 */ | ||
29 | "0.984807753012208"; /* z2 */ | ||
20 | }; | 30 | }; |
diff --git a/Documentation/devicetree/bindings/iio/potentiometer/ds1803.txt b/Documentation/devicetree/bindings/iio/potentiometer/ds1803.txt new file mode 100644 index 000000000000..df77bf552656 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/potentiometer/ds1803.txt | |||
@@ -0,0 +1,21 @@ | |||
1 | * Maxim Integrated DS1803 digital potentiometer driver | ||
2 | |||
3 | The node for this driver must be a child node of a I2C controller, hence | ||
4 | all mandatory properties for your controller must be specified. See directory: | ||
5 | |||
6 | Documentation/devicetree/bindings/i2c | ||
7 | |||
8 | for more details. | ||
9 | |||
10 | Required properties: | ||
11 | - compatible: Must be one of the following, depending on the | ||
12 | model: | ||
13 | "maxim,ds1803-010", | ||
14 | "maxim,ds1803-050", | ||
15 | "maxim,ds1803-100" | ||
16 | |||
17 | Example: | ||
18 | ds1803: ds1803@1 { | ||
19 | reg = <0x28>; | ||
20 | compatible = "maxim,ds1803-010"; | ||
21 | }; | ||
diff --git a/Documentation/devicetree/bindings/iio/pressure/hp03.txt b/Documentation/devicetree/bindings/iio/pressure/hp03.txt new file mode 100644 index 000000000000..54e7e70bcea5 --- /dev/null +++ b/Documentation/devicetree/bindings/iio/pressure/hp03.txt | |||
@@ -0,0 +1,17 @@ | |||
1 | HopeRF HP03 digital pressure/temperature sensors | ||
2 | |||
3 | Required properties: | ||
4 | - compatible: must be "hoperf,hp03" | ||
5 | - xclr-gpio: must be device tree identifier of the XCLR pin. | ||
6 | The XCLR pin is a reset of the ADC in the chip, | ||
7 | it must be pulled HI before the conversion and | ||
8 | readout of the value from the ADC registers and | ||
9 | pulled LO afterward. | ||
10 | |||
11 | Example: | ||
12 | |||
13 | hp03@0x77 { | ||
14 | compatible = "hoperf,hp03"; | ||
15 | reg = <0x77>; | ||
16 | xclr-gpio = <&portc 0 0x0>; | ||
17 | }; | ||
diff --git a/Documentation/devicetree/bindings/iio/st-sensors.txt b/Documentation/devicetree/bindings/iio/st-sensors.txt index 71b7bdff21cd..5844cf72862d 100644 --- a/Documentation/devicetree/bindings/iio/st-sensors.txt +++ b/Documentation/devicetree/bindings/iio/st-sensors.txt | |||
@@ -16,6 +16,10 @@ Optional properties: | |||
16 | - st,drdy-int-pin: the pin on the package that will be used to signal | 16 | - st,drdy-int-pin: the pin on the package that will be used to signal |
17 | "data ready" (valid values: 1 or 2). This property is not configurable | 17 | "data ready" (valid values: 1 or 2). This property is not configurable |
18 | on all sensors. | 18 | on all sensors. |
19 | - drive-open-drain: the interrupt/data ready line will be configured | ||
20 | as open drain, which is useful if several sensors share the same | ||
21 | interrupt line. (This binding is taken from pinctrl/pinctrl-bindings.txt) | ||
22 | This is a boolean property. | ||
19 | 23 | ||
20 | Sensors may also have applicable pin control settings, those use the | 24 | Sensors may also have applicable pin control settings, those use the |
21 | standard bindings from pinctrl/pinctrl-bindings.txt. | 25 | standard bindings from pinctrl/pinctrl-bindings.txt. |
@@ -47,6 +51,7 @@ Gyroscopes: | |||
47 | - st,l3gd20-gyro | 51 | - st,l3gd20-gyro |
48 | - st,l3g4is-gyro | 52 | - st,l3g4is-gyro |
49 | - st,lsm330-gyro | 53 | - st,lsm330-gyro |
54 | - st,lsm9ds0-gyro | ||
50 | 55 | ||
51 | Magnetometers: | 56 | Magnetometers: |
52 | - st,lsm303agr-magn | 57 | - st,lsm303agr-magn |
diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 73b98dfbcea4..bdc2dfbe6795 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt | |||
@@ -267,6 +267,10 @@ IIO | |||
267 | devm_iio_kfifo_free() | 267 | devm_iio_kfifo_free() |
268 | devm_iio_trigger_alloc() | 268 | devm_iio_trigger_alloc() |
269 | devm_iio_trigger_free() | 269 | devm_iio_trigger_free() |
270 | devm_iio_channel_get() | ||
271 | devm_iio_channel_release() | ||
272 | devm_iio_channel_get_all() | ||
273 | devm_iio_channel_release_all() | ||
270 | 274 | ||
271 | IO region | 275 | IO region |
272 | devm_release_mem_region() | 276 | devm_release_mem_region() |
diff --git a/drivers/iio/accel/bmc150-accel-core.c b/drivers/iio/accel/bmc150-accel-core.c index 2a0735d6955d..197e693e7e7b 100644 --- a/drivers/iio/accel/bmc150-accel-core.c +++ b/drivers/iio/accel/bmc150-accel-core.c | |||
@@ -188,7 +188,6 @@ enum bmc150_accel_trigger_id { | |||
188 | 188 | ||
189 | struct bmc150_accel_data { | 189 | struct bmc150_accel_data { |
190 | struct regmap *regmap; | 190 | struct regmap *regmap; |
191 | struct device *dev; | ||
192 | int irq; | 191 | int irq; |
193 | struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS]; | 192 | struct bmc150_accel_interrupt interrupts[BMC150_ACCEL_INTERRUPTS]; |
194 | atomic_t active_intr; | 193 | atomic_t active_intr; |
@@ -257,6 +256,7 @@ static int bmc150_accel_set_mode(struct bmc150_accel_data *data, | |||
257 | enum bmc150_power_modes mode, | 256 | enum bmc150_power_modes mode, |
258 | int dur_us) | 257 | int dur_us) |
259 | { | 258 | { |
259 | struct device *dev = regmap_get_device(data->regmap); | ||
260 | int i; | 260 | int i; |
261 | int ret; | 261 | int ret; |
262 | u8 lpw_bits; | 262 | u8 lpw_bits; |
@@ -280,11 +280,11 @@ static int bmc150_accel_set_mode(struct bmc150_accel_data *data, | |||
280 | lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; | 280 | lpw_bits = mode << BMC150_ACCEL_PMU_MODE_SHIFT; |
281 | lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); | 281 | lpw_bits |= (dur_val << BMC150_ACCEL_PMU_BIT_SLEEP_DUR_SHIFT); |
282 | 282 | ||
283 | dev_dbg(data->dev, "Set Mode bits %x\n", lpw_bits); | 283 | dev_dbg(dev, "Set Mode bits %x\n", lpw_bits); |
284 | 284 | ||
285 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); | 285 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_LPW, lpw_bits); |
286 | if (ret < 0) { | 286 | if (ret < 0) { |
287 | dev_err(data->dev, "Error writing reg_pmu_lpw\n"); | 287 | dev_err(dev, "Error writing reg_pmu_lpw\n"); |
288 | return ret; | 288 | return ret; |
289 | } | 289 | } |
290 | 290 | ||
@@ -317,23 +317,24 @@ static int bmc150_accel_set_bw(struct bmc150_accel_data *data, int val, | |||
317 | 317 | ||
318 | static int bmc150_accel_update_slope(struct bmc150_accel_data *data) | 318 | static int bmc150_accel_update_slope(struct bmc150_accel_data *data) |
319 | { | 319 | { |
320 | struct device *dev = regmap_get_device(data->regmap); | ||
320 | int ret; | 321 | int ret; |
321 | 322 | ||
322 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6, | 323 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_6, |
323 | data->slope_thres); | 324 | data->slope_thres); |
324 | if (ret < 0) { | 325 | if (ret < 0) { |
325 | dev_err(data->dev, "Error writing reg_int_6\n"); | 326 | dev_err(dev, "Error writing reg_int_6\n"); |
326 | return ret; | 327 | return ret; |
327 | } | 328 | } |
328 | 329 | ||
329 | ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5, | 330 | ret = regmap_update_bits(data->regmap, BMC150_ACCEL_REG_INT_5, |
330 | BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur); | 331 | BMC150_ACCEL_SLOPE_DUR_MASK, data->slope_dur); |
331 | if (ret < 0) { | 332 | if (ret < 0) { |
332 | dev_err(data->dev, "Error updating reg_int_5\n"); | 333 | dev_err(dev, "Error updating reg_int_5\n"); |
333 | return ret; | 334 | return ret; |
334 | } | 335 | } |
335 | 336 | ||
336 | dev_dbg(data->dev, "%s: %x %x\n", __func__, data->slope_thres, | 337 | dev_dbg(dev, "%s: %x %x\n", __func__, data->slope_thres, |
337 | data->slope_dur); | 338 | data->slope_dur); |
338 | 339 | ||
339 | return ret; | 340 | return ret; |
@@ -379,20 +380,21 @@ static int bmc150_accel_get_startup_times(struct bmc150_accel_data *data) | |||
379 | 380 | ||
380 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) | 381 | static int bmc150_accel_set_power_state(struct bmc150_accel_data *data, bool on) |
381 | { | 382 | { |
383 | struct device *dev = regmap_get_device(data->regmap); | ||
382 | int ret; | 384 | int ret; |
383 | 385 | ||
384 | if (on) { | 386 | if (on) { |
385 | ret = pm_runtime_get_sync(data->dev); | 387 | ret = pm_runtime_get_sync(dev); |
386 | } else { | 388 | } else { |
387 | pm_runtime_mark_last_busy(data->dev); | 389 | pm_runtime_mark_last_busy(dev); |
388 | ret = pm_runtime_put_autosuspend(data->dev); | 390 | ret = pm_runtime_put_autosuspend(dev); |
389 | } | 391 | } |
390 | 392 | ||
391 | if (ret < 0) { | 393 | if (ret < 0) { |
392 | dev_err(data->dev, | 394 | dev_err(dev, |
393 | "Failed: bmc150_accel_set_power_state for %d\n", on); | 395 | "Failed: bmc150_accel_set_power_state for %d\n", on); |
394 | if (on) | 396 | if (on) |
395 | pm_runtime_put_noidle(data->dev); | 397 | pm_runtime_put_noidle(dev); |
396 | 398 | ||
397 | return ret; | 399 | return ret; |
398 | } | 400 | } |
@@ -446,6 +448,7 @@ static void bmc150_accel_interrupts_setup(struct iio_dev *indio_dev, | |||
446 | static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, | 448 | static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, |
447 | bool state) | 449 | bool state) |
448 | { | 450 | { |
451 | struct device *dev = regmap_get_device(data->regmap); | ||
449 | struct bmc150_accel_interrupt *intr = &data->interrupts[i]; | 452 | struct bmc150_accel_interrupt *intr = &data->interrupts[i]; |
450 | const struct bmc150_accel_interrupt_info *info = intr->info; | 453 | const struct bmc150_accel_interrupt_info *info = intr->info; |
451 | int ret; | 454 | int ret; |
@@ -475,7 +478,7 @@ static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, | |||
475 | ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask, | 478 | ret = regmap_update_bits(data->regmap, info->map_reg, info->map_bitmask, |
476 | (state ? info->map_bitmask : 0)); | 479 | (state ? info->map_bitmask : 0)); |
477 | if (ret < 0) { | 480 | if (ret < 0) { |
478 | dev_err(data->dev, "Error updating reg_int_map\n"); | 481 | dev_err(dev, "Error updating reg_int_map\n"); |
479 | goto out_fix_power_state; | 482 | goto out_fix_power_state; |
480 | } | 483 | } |
481 | 484 | ||
@@ -483,7 +486,7 @@ static int bmc150_accel_set_interrupt(struct bmc150_accel_data *data, int i, | |||
483 | ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask, | 486 | ret = regmap_update_bits(data->regmap, info->en_reg, info->en_bitmask, |
484 | (state ? info->en_bitmask : 0)); | 487 | (state ? info->en_bitmask : 0)); |
485 | if (ret < 0) { | 488 | if (ret < 0) { |
486 | dev_err(data->dev, "Error updating reg_int_en\n"); | 489 | dev_err(dev, "Error updating reg_int_en\n"); |
487 | goto out_fix_power_state; | 490 | goto out_fix_power_state; |
488 | } | 491 | } |
489 | 492 | ||
@@ -501,6 +504,7 @@ out_fix_power_state: | |||
501 | 504 | ||
502 | static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) | 505 | static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) |
503 | { | 506 | { |
507 | struct device *dev = regmap_get_device(data->regmap); | ||
504 | int ret, i; | 508 | int ret, i; |
505 | 509 | ||
506 | for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { | 510 | for (i = 0; i < ARRAY_SIZE(data->chip_info->scale_table); ++i) { |
@@ -509,8 +513,7 @@ static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) | |||
509 | BMC150_ACCEL_REG_PMU_RANGE, | 513 | BMC150_ACCEL_REG_PMU_RANGE, |
510 | data->chip_info->scale_table[i].reg_range); | 514 | data->chip_info->scale_table[i].reg_range); |
511 | if (ret < 0) { | 515 | if (ret < 0) { |
512 | dev_err(data->dev, | 516 | dev_err(dev, "Error writing pmu_range\n"); |
513 | "Error writing pmu_range\n"); | ||
514 | return ret; | 517 | return ret; |
515 | } | 518 | } |
516 | 519 | ||
@@ -524,6 +527,7 @@ static int bmc150_accel_set_scale(struct bmc150_accel_data *data, int val) | |||
524 | 527 | ||
525 | static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) | 528 | static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) |
526 | { | 529 | { |
530 | struct device *dev = regmap_get_device(data->regmap); | ||
527 | int ret; | 531 | int ret; |
528 | unsigned int value; | 532 | unsigned int value; |
529 | 533 | ||
@@ -531,7 +535,7 @@ static int bmc150_accel_get_temp(struct bmc150_accel_data *data, int *val) | |||
531 | 535 | ||
532 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); | 536 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_TEMP, &value); |
533 | if (ret < 0) { | 537 | if (ret < 0) { |
534 | dev_err(data->dev, "Error reading reg_temp\n"); | 538 | dev_err(dev, "Error reading reg_temp\n"); |
535 | mutex_unlock(&data->mutex); | 539 | mutex_unlock(&data->mutex); |
536 | return ret; | 540 | return ret; |
537 | } | 541 | } |
@@ -546,6 +550,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data, | |||
546 | struct iio_chan_spec const *chan, | 550 | struct iio_chan_spec const *chan, |
547 | int *val) | 551 | int *val) |
548 | { | 552 | { |
553 | struct device *dev = regmap_get_device(data->regmap); | ||
549 | int ret; | 554 | int ret; |
550 | int axis = chan->scan_index; | 555 | int axis = chan->scan_index; |
551 | __le16 raw_val; | 556 | __le16 raw_val; |
@@ -560,7 +565,7 @@ static int bmc150_accel_get_axis(struct bmc150_accel_data *data, | |||
560 | ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), | 565 | ret = regmap_bulk_read(data->regmap, BMC150_ACCEL_AXIS_TO_REG(axis), |
561 | &raw_val, sizeof(raw_val)); | 566 | &raw_val, sizeof(raw_val)); |
562 | if (ret < 0) { | 567 | if (ret < 0) { |
563 | dev_err(data->dev, "Error reading axis %d\n", axis); | 568 | dev_err(dev, "Error reading axis %d\n", axis); |
564 | bmc150_accel_set_power_state(data, false); | 569 | bmc150_accel_set_power_state(data, false); |
565 | mutex_unlock(&data->mutex); | 570 | mutex_unlock(&data->mutex); |
566 | return ret; | 571 | return ret; |
@@ -832,6 +837,7 @@ static int bmc150_accel_set_watermark(struct iio_dev *indio_dev, unsigned val) | |||
832 | static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, | 837 | static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, |
833 | char *buffer, int samples) | 838 | char *buffer, int samples) |
834 | { | 839 | { |
840 | struct device *dev = regmap_get_device(data->regmap); | ||
835 | int sample_length = 3 * 2; | 841 | int sample_length = 3 * 2; |
836 | int ret; | 842 | int ret; |
837 | int total_length = samples * sample_length; | 843 | int total_length = samples * sample_length; |
@@ -855,7 +861,8 @@ static int bmc150_accel_fifo_transfer(struct bmc150_accel_data *data, | |||
855 | } | 861 | } |
856 | 862 | ||
857 | if (ret) | 863 | if (ret) |
858 | dev_err(data->dev, "Error transferring data from fifo in single steps of %zu\n", | 864 | dev_err(dev, |
865 | "Error transferring data from fifo in single steps of %zu\n", | ||
859 | step); | 866 | step); |
860 | 867 | ||
861 | return ret; | 868 | return ret; |
@@ -865,6 +872,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, | |||
865 | unsigned samples, bool irq) | 872 | unsigned samples, bool irq) |
866 | { | 873 | { |
867 | struct bmc150_accel_data *data = iio_priv(indio_dev); | 874 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
875 | struct device *dev = regmap_get_device(data->regmap); | ||
868 | int ret, i; | 876 | int ret, i; |
869 | u8 count; | 877 | u8 count; |
870 | u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3]; | 878 | u16 buffer[BMC150_ACCEL_FIFO_LENGTH * 3]; |
@@ -874,7 +882,7 @@ static int __bmc150_accel_fifo_flush(struct iio_dev *indio_dev, | |||
874 | 882 | ||
875 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); | 883 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_FIFO_STATUS, &val); |
876 | if (ret < 0) { | 884 | if (ret < 0) { |
877 | dev_err(data->dev, "Error reading reg_fifo_status\n"); | 885 | dev_err(dev, "Error reading reg_fifo_status\n"); |
878 | return ret; | 886 | return ret; |
879 | } | 887 | } |
880 | 888 | ||
@@ -1136,6 +1144,7 @@ static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) | |||
1136 | { | 1144 | { |
1137 | struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig); | 1145 | struct bmc150_accel_trigger *t = iio_trigger_get_drvdata(trig); |
1138 | struct bmc150_accel_data *data = t->data; | 1146 | struct bmc150_accel_data *data = t->data; |
1147 | struct device *dev = regmap_get_device(data->regmap); | ||
1139 | int ret; | 1148 | int ret; |
1140 | 1149 | ||
1141 | /* new data interrupts don't need ack */ | 1150 | /* new data interrupts don't need ack */ |
@@ -1149,8 +1158,7 @@ static int bmc150_accel_trig_try_reen(struct iio_trigger *trig) | |||
1149 | BMC150_ACCEL_INT_MODE_LATCH_RESET); | 1158 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
1150 | mutex_unlock(&data->mutex); | 1159 | mutex_unlock(&data->mutex); |
1151 | if (ret < 0) { | 1160 | if (ret < 0) { |
1152 | dev_err(data->dev, | 1161 | dev_err(dev, "Error writing reg_int_rst_latch\n"); |
1153 | "Error writing reg_int_rst_latch\n"); | ||
1154 | return ret; | 1162 | return ret; |
1155 | } | 1163 | } |
1156 | 1164 | ||
@@ -1201,13 +1209,14 @@ static const struct iio_trigger_ops bmc150_accel_trigger_ops = { | |||
1201 | static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev) | 1209 | static int bmc150_accel_handle_roc_event(struct iio_dev *indio_dev) |
1202 | { | 1210 | { |
1203 | struct bmc150_accel_data *data = iio_priv(indio_dev); | 1211 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
1212 | struct device *dev = regmap_get_device(data->regmap); | ||
1204 | int dir; | 1213 | int dir; |
1205 | int ret; | 1214 | int ret; |
1206 | unsigned int val; | 1215 | unsigned int val; |
1207 | 1216 | ||
1208 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val); | 1217 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_INT_STATUS_2, &val); |
1209 | if (ret < 0) { | 1218 | if (ret < 0) { |
1210 | dev_err(data->dev, "Error reading reg_int_status_2\n"); | 1219 | dev_err(dev, "Error reading reg_int_status_2\n"); |
1211 | return ret; | 1220 | return ret; |
1212 | } | 1221 | } |
1213 | 1222 | ||
@@ -1250,6 +1259,7 @@ static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private) | |||
1250 | { | 1259 | { |
1251 | struct iio_dev *indio_dev = private; | 1260 | struct iio_dev *indio_dev = private; |
1252 | struct bmc150_accel_data *data = iio_priv(indio_dev); | 1261 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
1262 | struct device *dev = regmap_get_device(data->regmap); | ||
1253 | bool ack = false; | 1263 | bool ack = false; |
1254 | int ret; | 1264 | int ret; |
1255 | 1265 | ||
@@ -1273,7 +1283,7 @@ static irqreturn_t bmc150_accel_irq_thread_handler(int irq, void *private) | |||
1273 | BMC150_ACCEL_INT_MODE_LATCH_INT | | 1283 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
1274 | BMC150_ACCEL_INT_MODE_LATCH_RESET); | 1284 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
1275 | if (ret) | 1285 | if (ret) |
1276 | dev_err(data->dev, "Error writing reg_int_rst_latch\n"); | 1286 | dev_err(dev, "Error writing reg_int_rst_latch\n"); |
1277 | 1287 | ||
1278 | ret = IRQ_HANDLED; | 1288 | ret = IRQ_HANDLED; |
1279 | } else { | 1289 | } else { |
@@ -1344,13 +1354,14 @@ static void bmc150_accel_unregister_triggers(struct bmc150_accel_data *data, | |||
1344 | static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, | 1354 | static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, |
1345 | struct bmc150_accel_data *data) | 1355 | struct bmc150_accel_data *data) |
1346 | { | 1356 | { |
1357 | struct device *dev = regmap_get_device(data->regmap); | ||
1347 | int i, ret; | 1358 | int i, ret; |
1348 | 1359 | ||
1349 | for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { | 1360 | for (i = 0; i < BMC150_ACCEL_TRIGGERS; i++) { |
1350 | struct bmc150_accel_trigger *t = &data->triggers[i]; | 1361 | struct bmc150_accel_trigger *t = &data->triggers[i]; |
1351 | 1362 | ||
1352 | t->indio_trig = devm_iio_trigger_alloc(data->dev, | 1363 | t->indio_trig = devm_iio_trigger_alloc(dev, |
1353 | bmc150_accel_triggers[i].name, | 1364 | bmc150_accel_triggers[i].name, |
1354 | indio_dev->name, | 1365 | indio_dev->name, |
1355 | indio_dev->id); | 1366 | indio_dev->id); |
1356 | if (!t->indio_trig) { | 1367 | if (!t->indio_trig) { |
@@ -1358,7 +1369,7 @@ static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, | |||
1358 | break; | 1369 | break; |
1359 | } | 1370 | } |
1360 | 1371 | ||
1361 | t->indio_trig->dev.parent = data->dev; | 1372 | t->indio_trig->dev.parent = dev; |
1362 | t->indio_trig->ops = &bmc150_accel_trigger_ops; | 1373 | t->indio_trig->ops = &bmc150_accel_trigger_ops; |
1363 | t->intr = bmc150_accel_triggers[i].intr; | 1374 | t->intr = bmc150_accel_triggers[i].intr; |
1364 | t->data = data; | 1375 | t->data = data; |
@@ -1382,12 +1393,13 @@ static int bmc150_accel_triggers_setup(struct iio_dev *indio_dev, | |||
1382 | 1393 | ||
1383 | static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data) | 1394 | static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data) |
1384 | { | 1395 | { |
1396 | struct device *dev = regmap_get_device(data->regmap); | ||
1385 | u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1; | 1397 | u8 reg = BMC150_ACCEL_REG_FIFO_CONFIG1; |
1386 | int ret; | 1398 | int ret; |
1387 | 1399 | ||
1388 | ret = regmap_write(data->regmap, reg, data->fifo_mode); | 1400 | ret = regmap_write(data->regmap, reg, data->fifo_mode); |
1389 | if (ret < 0) { | 1401 | if (ret < 0) { |
1390 | dev_err(data->dev, "Error writing reg_fifo_config1\n"); | 1402 | dev_err(dev, "Error writing reg_fifo_config1\n"); |
1391 | return ret; | 1403 | return ret; |
1392 | } | 1404 | } |
1393 | 1405 | ||
@@ -1397,7 +1409,7 @@ static int bmc150_accel_fifo_set_mode(struct bmc150_accel_data *data) | |||
1397 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0, | 1409 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_FIFO_CONFIG0, |
1398 | data->watermark); | 1410 | data->watermark); |
1399 | if (ret < 0) | 1411 | if (ret < 0) |
1400 | dev_err(data->dev, "Error writing reg_fifo_config0\n"); | 1412 | dev_err(dev, "Error writing reg_fifo_config0\n"); |
1401 | 1413 | ||
1402 | return ret; | 1414 | return ret; |
1403 | } | 1415 | } |
@@ -1481,17 +1493,17 @@ static const struct iio_buffer_setup_ops bmc150_accel_buffer_ops = { | |||
1481 | 1493 | ||
1482 | static int bmc150_accel_chip_init(struct bmc150_accel_data *data) | 1494 | static int bmc150_accel_chip_init(struct bmc150_accel_data *data) |
1483 | { | 1495 | { |
1496 | struct device *dev = regmap_get_device(data->regmap); | ||
1484 | int ret, i; | 1497 | int ret, i; |
1485 | unsigned int val; | 1498 | unsigned int val; |
1486 | 1499 | ||
1487 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); | 1500 | ret = regmap_read(data->regmap, BMC150_ACCEL_REG_CHIP_ID, &val); |
1488 | if (ret < 0) { | 1501 | if (ret < 0) { |
1489 | dev_err(data->dev, | 1502 | dev_err(dev, "Error: Reading chip id\n"); |
1490 | "Error: Reading chip id\n"); | ||
1491 | return ret; | 1503 | return ret; |
1492 | } | 1504 | } |
1493 | 1505 | ||
1494 | dev_dbg(data->dev, "Chip Id %x\n", val); | 1506 | dev_dbg(dev, "Chip Id %x\n", val); |
1495 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) { | 1507 | for (i = 0; i < ARRAY_SIZE(bmc150_accel_chip_info_tbl); i++) { |
1496 | if (bmc150_accel_chip_info_tbl[i].chip_id == val) { | 1508 | if (bmc150_accel_chip_info_tbl[i].chip_id == val) { |
1497 | data->chip_info = &bmc150_accel_chip_info_tbl[i]; | 1509 | data->chip_info = &bmc150_accel_chip_info_tbl[i]; |
@@ -1500,7 +1512,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data) | |||
1500 | } | 1512 | } |
1501 | 1513 | ||
1502 | if (!data->chip_info) { | 1514 | if (!data->chip_info) { |
1503 | dev_err(data->dev, "Invalid chip %x\n", val); | 1515 | dev_err(dev, "Invalid chip %x\n", val); |
1504 | return -ENODEV; | 1516 | return -ENODEV; |
1505 | } | 1517 | } |
1506 | 1518 | ||
@@ -1517,8 +1529,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data) | |||
1517 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE, | 1529 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_PMU_RANGE, |
1518 | BMC150_ACCEL_DEF_RANGE_4G); | 1530 | BMC150_ACCEL_DEF_RANGE_4G); |
1519 | if (ret < 0) { | 1531 | if (ret < 0) { |
1520 | dev_err(data->dev, | 1532 | dev_err(dev, "Error writing reg_pmu_range\n"); |
1521 | "Error writing reg_pmu_range\n"); | ||
1522 | return ret; | 1533 | return ret; |
1523 | } | 1534 | } |
1524 | 1535 | ||
@@ -1536,8 +1547,7 @@ static int bmc150_accel_chip_init(struct bmc150_accel_data *data) | |||
1536 | BMC150_ACCEL_INT_MODE_LATCH_INT | | 1547 | BMC150_ACCEL_INT_MODE_LATCH_INT | |
1537 | BMC150_ACCEL_INT_MODE_LATCH_RESET); | 1548 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
1538 | if (ret < 0) { | 1549 | if (ret < 0) { |
1539 | dev_err(data->dev, | 1550 | dev_err(dev, "Error writing reg_int_rst_latch\n"); |
1540 | "Error writing reg_int_rst_latch\n"); | ||
1541 | return ret; | 1551 | return ret; |
1542 | } | 1552 | } |
1543 | 1553 | ||
@@ -1557,7 +1567,6 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, | |||
1557 | 1567 | ||
1558 | data = iio_priv(indio_dev); | 1568 | data = iio_priv(indio_dev); |
1559 | dev_set_drvdata(dev, indio_dev); | 1569 | dev_set_drvdata(dev, indio_dev); |
1560 | data->dev = dev; | ||
1561 | data->irq = irq; | 1570 | data->irq = irq; |
1562 | 1571 | ||
1563 | data->regmap = regmap; | 1572 | data->regmap = regmap; |
@@ -1581,13 +1590,13 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, | |||
1581 | bmc150_accel_trigger_handler, | 1590 | bmc150_accel_trigger_handler, |
1582 | &bmc150_accel_buffer_ops); | 1591 | &bmc150_accel_buffer_ops); |
1583 | if (ret < 0) { | 1592 | if (ret < 0) { |
1584 | dev_err(data->dev, "Failed: iio triggered buffer setup\n"); | 1593 | dev_err(dev, "Failed: iio triggered buffer setup\n"); |
1585 | return ret; | 1594 | return ret; |
1586 | } | 1595 | } |
1587 | 1596 | ||
1588 | if (data->irq > 0) { | 1597 | if (data->irq > 0) { |
1589 | ret = devm_request_threaded_irq( | 1598 | ret = devm_request_threaded_irq( |
1590 | data->dev, data->irq, | 1599 | dev, data->irq, |
1591 | bmc150_accel_irq_handler, | 1600 | bmc150_accel_irq_handler, |
1592 | bmc150_accel_irq_thread_handler, | 1601 | bmc150_accel_irq_thread_handler, |
1593 | IRQF_TRIGGER_RISING, | 1602 | IRQF_TRIGGER_RISING, |
@@ -1605,7 +1614,7 @@ int bmc150_accel_core_probe(struct device *dev, struct regmap *regmap, int irq, | |||
1605 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, | 1614 | ret = regmap_write(data->regmap, BMC150_ACCEL_REG_INT_RST_LATCH, |
1606 | BMC150_ACCEL_INT_MODE_LATCH_RESET); | 1615 | BMC150_ACCEL_INT_MODE_LATCH_RESET); |
1607 | if (ret < 0) { | 1616 | if (ret < 0) { |
1608 | dev_err(data->dev, "Error writing reg_int_rst_latch\n"); | 1617 | dev_err(dev, "Error writing reg_int_rst_latch\n"); |
1609 | goto err_buffer_cleanup; | 1618 | goto err_buffer_cleanup; |
1610 | } | 1619 | } |
1611 | 1620 | ||
@@ -1654,9 +1663,9 @@ int bmc150_accel_core_remove(struct device *dev) | |||
1654 | 1663 | ||
1655 | iio_device_unregister(indio_dev); | 1664 | iio_device_unregister(indio_dev); |
1656 | 1665 | ||
1657 | pm_runtime_disable(data->dev); | 1666 | pm_runtime_disable(dev); |
1658 | pm_runtime_set_suspended(data->dev); | 1667 | pm_runtime_set_suspended(dev); |
1659 | pm_runtime_put_noidle(data->dev); | 1668 | pm_runtime_put_noidle(dev); |
1660 | 1669 | ||
1661 | bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); | 1670 | bmc150_accel_unregister_triggers(data, BMC150_ACCEL_TRIGGERS - 1); |
1662 | 1671 | ||
@@ -1705,7 +1714,7 @@ static int bmc150_accel_runtime_suspend(struct device *dev) | |||
1705 | struct bmc150_accel_data *data = iio_priv(indio_dev); | 1714 | struct bmc150_accel_data *data = iio_priv(indio_dev); |
1706 | int ret; | 1715 | int ret; |
1707 | 1716 | ||
1708 | dev_dbg(data->dev, __func__); | 1717 | dev_dbg(dev, __func__); |
1709 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); | 1718 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_SUSPEND, 0); |
1710 | if (ret < 0) | 1719 | if (ret < 0) |
1711 | return -EAGAIN; | 1720 | return -EAGAIN; |
@@ -1720,7 +1729,7 @@ static int bmc150_accel_runtime_resume(struct device *dev) | |||
1720 | int ret; | 1729 | int ret; |
1721 | int sleep_val; | 1730 | int sleep_val; |
1722 | 1731 | ||
1723 | dev_dbg(data->dev, __func__); | 1732 | dev_dbg(dev, __func__); |
1724 | 1733 | ||
1725 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); | 1734 | ret = bmc150_accel_set_mode(data, BMC150_ACCEL_SLEEP_MODE_NORMAL, 0); |
1726 | if (ret < 0) | 1735 | if (ret < 0) |
diff --git a/drivers/iio/accel/mma7455_core.c b/drivers/iio/accel/mma7455_core.c index c633cc2c0789..c902f54c23f5 100644 --- a/drivers/iio/accel/mma7455_core.c +++ b/drivers/iio/accel/mma7455_core.c | |||
@@ -55,11 +55,11 @@ | |||
55 | 55 | ||
56 | struct mma7455_data { | 56 | struct mma7455_data { |
57 | struct regmap *regmap; | 57 | struct regmap *regmap; |
58 | struct device *dev; | ||
59 | }; | 58 | }; |
60 | 59 | ||
61 | static int mma7455_drdy(struct mma7455_data *mma7455) | 60 | static int mma7455_drdy(struct mma7455_data *mma7455) |
62 | { | 61 | { |
62 | struct device *dev = regmap_get_device(mma7455->regmap); | ||
63 | unsigned int reg; | 63 | unsigned int reg; |
64 | int tries = 3; | 64 | int tries = 3; |
65 | int ret; | 65 | int ret; |
@@ -75,7 +75,7 @@ static int mma7455_drdy(struct mma7455_data *mma7455) | |||
75 | msleep(20); | 75 | msleep(20); |
76 | } | 76 | } |
77 | 77 | ||
78 | dev_warn(mma7455->dev, "data not ready\n"); | 78 | dev_warn(dev, "data not ready\n"); |
79 | 79 | ||
80 | return -EIO; | 80 | return -EIO; |
81 | } | 81 | } |
@@ -260,7 +260,6 @@ int mma7455_core_probe(struct device *dev, struct regmap *regmap, | |||
260 | dev_set_drvdata(dev, indio_dev); | 260 | dev_set_drvdata(dev, indio_dev); |
261 | mma7455 = iio_priv(indio_dev); | 261 | mma7455 = iio_priv(indio_dev); |
262 | mma7455->regmap = regmap; | 262 | mma7455->regmap = regmap; |
263 | mma7455->dev = dev; | ||
264 | 263 | ||
265 | indio_dev->info = &mma7455_info; | 264 | indio_dev->info = &mma7455_info; |
266 | indio_dev->name = name; | 265 | indio_dev->name = name; |
diff --git a/drivers/iio/accel/st_accel_core.c b/drivers/iio/accel/st_accel_core.c index fee32e3d7a05..dc73f2d85e6d 100644 --- a/drivers/iio/accel/st_accel_core.c +++ b/drivers/iio/accel/st_accel_core.c | |||
@@ -99,6 +99,8 @@ | |||
99 | #define ST_ACCEL_2_DRDY_IRQ_INT2_MASK 0x10 | 99 | #define ST_ACCEL_2_DRDY_IRQ_INT2_MASK 0x10 |
100 | #define ST_ACCEL_2_IHL_IRQ_ADDR 0x22 | 100 | #define ST_ACCEL_2_IHL_IRQ_ADDR 0x22 |
101 | #define ST_ACCEL_2_IHL_IRQ_MASK 0x80 | 101 | #define ST_ACCEL_2_IHL_IRQ_MASK 0x80 |
102 | #define ST_ACCEL_2_OD_IRQ_ADDR 0x22 | ||
103 | #define ST_ACCEL_2_OD_IRQ_MASK 0x40 | ||
102 | #define ST_ACCEL_2_MULTIREAD_BIT true | 104 | #define ST_ACCEL_2_MULTIREAD_BIT true |
103 | 105 | ||
104 | /* CUSTOM VALUES FOR SENSOR 3 */ | 106 | /* CUSTOM VALUES FOR SENSOR 3 */ |
@@ -180,6 +182,8 @@ | |||
180 | #define ST_ACCEL_5_DRDY_IRQ_INT2_MASK 0x20 | 182 | #define ST_ACCEL_5_DRDY_IRQ_INT2_MASK 0x20 |
181 | #define ST_ACCEL_5_IHL_IRQ_ADDR 0x22 | 183 | #define ST_ACCEL_5_IHL_IRQ_ADDR 0x22 |
182 | #define ST_ACCEL_5_IHL_IRQ_MASK 0x80 | 184 | #define ST_ACCEL_5_IHL_IRQ_MASK 0x80 |
185 | #define ST_ACCEL_5_OD_IRQ_ADDR 0x22 | ||
186 | #define ST_ACCEL_5_OD_IRQ_MASK 0x40 | ||
183 | #define ST_ACCEL_5_IG1_EN_ADDR 0x21 | 187 | #define ST_ACCEL_5_IG1_EN_ADDR 0x21 |
184 | #define ST_ACCEL_5_IG1_EN_MASK 0x08 | 188 | #define ST_ACCEL_5_IG1_EN_MASK 0x08 |
185 | #define ST_ACCEL_5_MULTIREAD_BIT false | 189 | #define ST_ACCEL_5_MULTIREAD_BIT false |
@@ -332,6 +336,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = { | |||
332 | .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK, | 336 | .mask_int2 = ST_ACCEL_1_DRDY_IRQ_INT2_MASK, |
333 | .addr_ihl = ST_ACCEL_1_IHL_IRQ_ADDR, | 337 | .addr_ihl = ST_ACCEL_1_IHL_IRQ_ADDR, |
334 | .mask_ihl = ST_ACCEL_1_IHL_IRQ_MASK, | 338 | .mask_ihl = ST_ACCEL_1_IHL_IRQ_MASK, |
339 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
335 | }, | 340 | }, |
336 | .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT, | 341 | .multi_read_bit = ST_ACCEL_1_MULTIREAD_BIT, |
337 | .bootime = 2, | 342 | .bootime = 2, |
@@ -397,6 +402,9 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = { | |||
397 | .mask_int2 = ST_ACCEL_2_DRDY_IRQ_INT2_MASK, | 402 | .mask_int2 = ST_ACCEL_2_DRDY_IRQ_INT2_MASK, |
398 | .addr_ihl = ST_ACCEL_2_IHL_IRQ_ADDR, | 403 | .addr_ihl = ST_ACCEL_2_IHL_IRQ_ADDR, |
399 | .mask_ihl = ST_ACCEL_2_IHL_IRQ_MASK, | 404 | .mask_ihl = ST_ACCEL_2_IHL_IRQ_MASK, |
405 | .addr_od = ST_ACCEL_2_OD_IRQ_ADDR, | ||
406 | .mask_od = ST_ACCEL_2_OD_IRQ_MASK, | ||
407 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
400 | }, | 408 | }, |
401 | .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT, | 409 | .multi_read_bit = ST_ACCEL_2_MULTIREAD_BIT, |
402 | .bootime = 2, | 410 | .bootime = 2, |
@@ -474,6 +482,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = { | |||
474 | .mask_int2 = ST_ACCEL_3_DRDY_IRQ_INT2_MASK, | 482 | .mask_int2 = ST_ACCEL_3_DRDY_IRQ_INT2_MASK, |
475 | .addr_ihl = ST_ACCEL_3_IHL_IRQ_ADDR, | 483 | .addr_ihl = ST_ACCEL_3_IHL_IRQ_ADDR, |
476 | .mask_ihl = ST_ACCEL_3_IHL_IRQ_MASK, | 484 | .mask_ihl = ST_ACCEL_3_IHL_IRQ_MASK, |
485 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
477 | .ig1 = { | 486 | .ig1 = { |
478 | .en_addr = ST_ACCEL_3_IG1_EN_ADDR, | 487 | .en_addr = ST_ACCEL_3_IG1_EN_ADDR, |
479 | .en_mask = ST_ACCEL_3_IG1_EN_MASK, | 488 | .en_mask = ST_ACCEL_3_IG1_EN_MASK, |
@@ -532,6 +541,7 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = { | |||
532 | .drdy_irq = { | 541 | .drdy_irq = { |
533 | .addr = ST_ACCEL_4_DRDY_IRQ_ADDR, | 542 | .addr = ST_ACCEL_4_DRDY_IRQ_ADDR, |
534 | .mask_int1 = ST_ACCEL_4_DRDY_IRQ_INT1_MASK, | 543 | .mask_int1 = ST_ACCEL_4_DRDY_IRQ_INT1_MASK, |
544 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
535 | }, | 545 | }, |
536 | .multi_read_bit = ST_ACCEL_4_MULTIREAD_BIT, | 546 | .multi_read_bit = ST_ACCEL_4_MULTIREAD_BIT, |
537 | .bootime = 2, /* guess */ | 547 | .bootime = 2, /* guess */ |
@@ -583,6 +593,9 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = { | |||
583 | .mask_int2 = ST_ACCEL_5_DRDY_IRQ_INT2_MASK, | 593 | .mask_int2 = ST_ACCEL_5_DRDY_IRQ_INT2_MASK, |
584 | .addr_ihl = ST_ACCEL_5_IHL_IRQ_ADDR, | 594 | .addr_ihl = ST_ACCEL_5_IHL_IRQ_ADDR, |
585 | .mask_ihl = ST_ACCEL_5_IHL_IRQ_MASK, | 595 | .mask_ihl = ST_ACCEL_5_IHL_IRQ_MASK, |
596 | .addr_od = ST_ACCEL_5_OD_IRQ_ADDR, | ||
597 | .mask_od = ST_ACCEL_5_OD_IRQ_MASK, | ||
598 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
586 | }, | 599 | }, |
587 | .multi_read_bit = ST_ACCEL_5_MULTIREAD_BIT, | 600 | .multi_read_bit = ST_ACCEL_5_MULTIREAD_BIT, |
588 | .bootime = 2, /* guess */ | 601 | .bootime = 2, /* guess */ |
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 5937030f0444..25378c5882e2 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig | |||
@@ -385,11 +385,11 @@ config ROCKCHIP_SARADC | |||
385 | module will be called rockchip_saradc. | 385 | module will be called rockchip_saradc. |
386 | 386 | ||
387 | config TI_ADC081C | 387 | config TI_ADC081C |
388 | tristate "Texas Instruments ADC081C021/027" | 388 | tristate "Texas Instruments ADC081C/ADC101C/ADC121C family" |
389 | depends on I2C | 389 | depends on I2C |
390 | help | 390 | help |
391 | If you say yes here you get support for Texas Instruments ADC081C021 | 391 | If you say yes here you get support for Texas Instruments ADC081C, |
392 | and ADC081C027 ADC chips. | 392 | ADC101C and ADC121C ADC chips. |
393 | 393 | ||
394 | This driver can also be built as a module. If so, the module will be | 394 | This driver can also be built as a module. If so, the module will be |
395 | called ti-adc081c. | 395 | called ti-adc081c. |
diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 01d71588d752..a3f5254f4e51 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c | |||
@@ -477,7 +477,7 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev, | |||
477 | if (ret < 0) | 477 | if (ret < 0) |
478 | return ret; | 478 | return ret; |
479 | *val = (ret >> chan->scan_type.shift) & | 479 | *val = (ret >> chan->scan_type.shift) & |
480 | GENMASK(chan->scan_type.realbits - 1 , 0); | 480 | GENMASK(chan->scan_type.realbits - 1, 0); |
481 | 481 | ||
482 | return IIO_VAL_INT; | 482 | return IIO_VAL_INT; |
483 | } | 483 | } |
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c index f284cd6a93d6..52430ba171f3 100644 --- a/drivers/iio/adc/at91_adc.c +++ b/drivers/iio/adc/at91_adc.c | |||
@@ -797,8 +797,8 @@ static u32 calc_startup_ticks_9x5(u32 startup_time, u32 adc_clk_khz) | |||
797 | * Startup Time = <lookup_table_value> / ADC Clock | 797 | * Startup Time = <lookup_table_value> / ADC Clock |
798 | */ | 798 | */ |
799 | const int startup_lookup[] = { | 799 | const int startup_lookup[] = { |
800 | 0 , 8 , 16 , 24 , | 800 | 0, 8, 16, 24, |
801 | 64 , 80 , 96 , 112, | 801 | 64, 80, 96, 112, |
802 | 512, 576, 640, 704, | 802 | 512, 576, 640, 704, |
803 | 768, 832, 896, 960 | 803 | 768, 832, 896, 960 |
804 | }; | 804 | }; |
@@ -924,14 +924,14 @@ static int at91_adc_probe_dt(struct at91_adc_state *st, | |||
924 | ret = -EINVAL; | 924 | ret = -EINVAL; |
925 | goto error_ret; | 925 | goto error_ret; |
926 | } | 926 | } |
927 | trig->name = name; | 927 | trig->name = name; |
928 | 928 | ||
929 | if (of_property_read_u32(trig_node, "trigger-value", &prop)) { | 929 | if (of_property_read_u32(trig_node, "trigger-value", &prop)) { |
930 | dev_err(&idev->dev, "Missing trigger-value property in the DT.\n"); | 930 | dev_err(&idev->dev, "Missing trigger-value property in the DT.\n"); |
931 | ret = -EINVAL; | 931 | ret = -EINVAL; |
932 | goto error_ret; | 932 | goto error_ret; |
933 | } | 933 | } |
934 | trig->value = prop; | 934 | trig->value = prop; |
935 | trig->is_external = of_property_read_bool(trig_node, "trigger-external"); | 935 | trig->is_external = of_property_read_bool(trig_node, "trigger-external"); |
936 | i++; | 936 | i++; |
937 | } | 937 | } |
diff --git a/drivers/iio/adc/mcp3422.c b/drivers/iio/adc/mcp3422.c index d7b36efd2f3c..d1172dc1e8e2 100644 --- a/drivers/iio/adc/mcp3422.c +++ b/drivers/iio/adc/mcp3422.c | |||
@@ -61,9 +61,9 @@ | |||
61 | 61 | ||
62 | static const int mcp3422_scales[4][4] = { | 62 | static const int mcp3422_scales[4][4] = { |
63 | { 1000000, 500000, 250000, 125000 }, | 63 | { 1000000, 500000, 250000, 125000 }, |
64 | { 250000 , 125000, 62500 , 31250 }, | 64 | { 250000, 125000, 62500, 31250 }, |
65 | { 62500 , 31250 , 15625 , 7812 }, | 65 | { 62500, 31250, 15625, 7812 }, |
66 | { 15625 , 7812 , 3906 , 1953 } }; | 66 | { 15625, 7812, 3906, 1953 } }; |
67 | 67 | ||
68 | /* Constant msleep times for data acquisitions */ | 68 | /* Constant msleep times for data acquisitions */ |
69 | static const int mcp3422_read_times[4] = { | 69 | static const int mcp3422_read_times[4] = { |
diff --git a/drivers/iio/adc/mxs-lradc.c b/drivers/iio/adc/mxs-lradc.c index 33051b87aac2..ad26da1edbee 100644 --- a/drivers/iio/adc/mxs-lradc.c +++ b/drivers/iio/adc/mxs-lradc.c | |||
@@ -686,6 +686,17 @@ static void mxs_lradc_prepare_pressure(struct mxs_lradc *lradc) | |||
686 | 686 | ||
687 | static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc) | 687 | static void mxs_lradc_enable_touch_detection(struct mxs_lradc *lradc) |
688 | { | 688 | { |
689 | /* Configure the touchscreen type */ | ||
690 | if (lradc->soc == IMX28_LRADC) { | ||
691 | mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, | ||
692 | LRADC_CTRL0); | ||
693 | |||
694 | if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE) | ||
695 | mxs_lradc_reg_set(lradc, | ||
696 | LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, | ||
697 | LRADC_CTRL0); | ||
698 | } | ||
699 | |||
689 | mxs_lradc_setup_touch_detection(lradc); | 700 | mxs_lradc_setup_touch_detection(lradc); |
690 | 701 | ||
691 | lradc->cur_plate = LRADC_TOUCH; | 702 | lradc->cur_plate = LRADC_TOUCH; |
@@ -1127,6 +1138,7 @@ static int mxs_lradc_ts_register(struct mxs_lradc *lradc) | |||
1127 | __set_bit(EV_ABS, input->evbit); | 1138 | __set_bit(EV_ABS, input->evbit); |
1128 | __set_bit(EV_KEY, input->evbit); | 1139 | __set_bit(EV_KEY, input->evbit); |
1129 | __set_bit(BTN_TOUCH, input->keybit); | 1140 | __set_bit(BTN_TOUCH, input->keybit); |
1141 | __set_bit(INPUT_PROP_DIRECT, input->propbit); | ||
1130 | input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); | 1142 | input_set_abs_params(input, ABS_X, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); |
1131 | input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); | 1143 | input_set_abs_params(input, ABS_Y, 0, LRADC_SINGLE_SAMPLE_MASK, 0, 0); |
1132 | input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK, | 1144 | input_set_abs_params(input, ABS_PRESSURE, 0, LRADC_SINGLE_SAMPLE_MASK, |
@@ -1475,18 +1487,13 @@ static const struct iio_chan_spec mx28_lradc_chan_spec[] = { | |||
1475 | MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"), | 1487 | MXS_ADC_CHAN(15, IIO_VOLTAGE, "VDD5V"), |
1476 | }; | 1488 | }; |
1477 | 1489 | ||
1478 | static int mxs_lradc_hw_init(struct mxs_lradc *lradc) | 1490 | static void mxs_lradc_hw_init(struct mxs_lradc *lradc) |
1479 | { | 1491 | { |
1480 | /* The ADC always uses DELAY CHANNEL 0. */ | 1492 | /* The ADC always uses DELAY CHANNEL 0. */ |
1481 | const u32 adc_cfg = | 1493 | const u32 adc_cfg = |
1482 | (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) | | 1494 | (1 << (LRADC_DELAY_TRIGGER_DELAYS_OFFSET + 0)) | |
1483 | (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET); | 1495 | (LRADC_DELAY_TIMER_PER << LRADC_DELAY_DELAY_OFFSET); |
1484 | 1496 | ||
1485 | int ret = stmp_reset_block(lradc->base); | ||
1486 | |||
1487 | if (ret) | ||
1488 | return ret; | ||
1489 | |||
1490 | /* Configure DELAY CHANNEL 0 for generic ADC sampling. */ | 1497 | /* Configure DELAY CHANNEL 0 for generic ADC sampling. */ |
1491 | mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0)); | 1498 | mxs_lradc_reg_wrt(lradc, adc_cfg, LRADC_DELAY(0)); |
1492 | 1499 | ||
@@ -1495,20 +1502,8 @@ static int mxs_lradc_hw_init(struct mxs_lradc *lradc) | |||
1495 | mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); | 1502 | mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(2)); |
1496 | mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); | 1503 | mxs_lradc_reg_wrt(lradc, 0, LRADC_DELAY(3)); |
1497 | 1504 | ||
1498 | /* Configure the touchscreen type */ | ||
1499 | if (lradc->soc == IMX28_LRADC) { | ||
1500 | mxs_lradc_reg_clear(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, | ||
1501 | LRADC_CTRL0); | ||
1502 | |||
1503 | if (lradc->use_touchscreen == MXS_LRADC_TOUCHSCREEN_5WIRE) | ||
1504 | mxs_lradc_reg_set(lradc, LRADC_CTRL0_MX28_TOUCH_SCREEN_TYPE, | ||
1505 | LRADC_CTRL0); | ||
1506 | } | ||
1507 | |||
1508 | /* Start internal temperature sensing. */ | 1505 | /* Start internal temperature sensing. */ |
1509 | mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2); | 1506 | mxs_lradc_reg_wrt(lradc, 0, LRADC_CTRL2); |
1510 | |||
1511 | return 0; | ||
1512 | } | 1507 | } |
1513 | 1508 | ||
1514 | static void mxs_lradc_hw_stop(struct mxs_lradc *lradc) | 1509 | static void mxs_lradc_hw_stop(struct mxs_lradc *lradc) |
@@ -1708,11 +1703,13 @@ static int mxs_lradc_probe(struct platform_device *pdev) | |||
1708 | } | 1703 | } |
1709 | } | 1704 | } |
1710 | 1705 | ||
1711 | /* Configure the hardware. */ | 1706 | ret = stmp_reset_block(lradc->base); |
1712 | ret = mxs_lradc_hw_init(lradc); | ||
1713 | if (ret) | 1707 | if (ret) |
1714 | goto err_dev; | 1708 | goto err_dev; |
1715 | 1709 | ||
1710 | /* Configure the hardware. */ | ||
1711 | mxs_lradc_hw_init(lradc); | ||
1712 | |||
1716 | /* Register the touchscreen input device. */ | 1713 | /* Register the touchscreen input device. */ |
1717 | if (touch_ret == 0) { | 1714 | if (touch_ret == 0) { |
1718 | ret = mxs_lradc_ts_register(lradc); | 1715 | ret = mxs_lradc_ts_register(lradc); |
diff --git a/drivers/iio/adc/ti-adc081c.c b/drivers/iio/adc/ti-adc081c.c index ecbc12138d58..9fd032d9f402 100644 --- a/drivers/iio/adc/ti-adc081c.c +++ b/drivers/iio/adc/ti-adc081c.c | |||
@@ -1,9 +1,21 @@ | |||
1 | /* | 1 | /* |
2 | * TI ADC081C/ADC101C/ADC121C 8/10/12-bit ADC driver | ||
3 | * | ||
2 | * Copyright (C) 2012 Avionic Design GmbH | 4 | * Copyright (C) 2012 Avionic Design GmbH |
5 | * Copyright (C) 2016 Intel | ||
3 | * | 6 | * |
4 | * This program is free software; you can redistribute it and/or modify | 7 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License version 2 as | 8 | * it under the terms of the GNU General Public License version 2 as |
6 | * published by the Free Software Foundation. | 9 | * published by the Free Software Foundation. |
10 | * | ||
11 | * Datasheets: | ||
12 | * http://www.ti.com/lit/ds/symlink/adc081c021.pdf | ||
13 | * http://www.ti.com/lit/ds/symlink/adc101c021.pdf | ||
14 | * http://www.ti.com/lit/ds/symlink/adc121c021.pdf | ||
15 | * | ||
16 | * The devices have a very similar interface and differ mostly in the number of | ||
17 | * bits handled. For the 8-bit and 10-bit models the least-significant 4 or 2 | ||
18 | * bits of value registers are reserved. | ||
7 | */ | 19 | */ |
8 | 20 | ||
9 | #include <linux/err.h> | 21 | #include <linux/err.h> |
@@ -12,11 +24,17 @@ | |||
12 | #include <linux/of.h> | 24 | #include <linux/of.h> |
13 | 25 | ||
14 | #include <linux/iio/iio.h> | 26 | #include <linux/iio/iio.h> |
27 | #include <linux/iio/buffer.h> | ||
28 | #include <linux/iio/trigger_consumer.h> | ||
29 | #include <linux/iio/triggered_buffer.h> | ||
15 | #include <linux/regulator/consumer.h> | 30 | #include <linux/regulator/consumer.h> |
16 | 31 | ||
17 | struct adc081c { | 32 | struct adc081c { |
18 | struct i2c_client *i2c; | 33 | struct i2c_client *i2c; |
19 | struct regulator *ref; | 34 | struct regulator *ref; |
35 | |||
36 | /* 8, 10 or 12 */ | ||
37 | int bits; | ||
20 | }; | 38 | }; |
21 | 39 | ||
22 | #define REG_CONV_RES 0x00 | 40 | #define REG_CONV_RES 0x00 |
@@ -34,7 +52,7 @@ static int adc081c_read_raw(struct iio_dev *iio, | |||
34 | if (err < 0) | 52 | if (err < 0) |
35 | return err; | 53 | return err; |
36 | 54 | ||
37 | *value = (err >> 4) & 0xff; | 55 | *value = (err & 0xFFF) >> (12 - adc->bits); |
38 | return IIO_VAL_INT; | 56 | return IIO_VAL_INT; |
39 | 57 | ||
40 | case IIO_CHAN_INFO_SCALE: | 58 | case IIO_CHAN_INFO_SCALE: |
@@ -43,7 +61,7 @@ static int adc081c_read_raw(struct iio_dev *iio, | |||
43 | return err; | 61 | return err; |
44 | 62 | ||
45 | *value = err / 1000; | 63 | *value = err / 1000; |
46 | *shift = 8; | 64 | *shift = adc->bits; |
47 | 65 | ||
48 | return IIO_VAL_FRACTIONAL_LOG2; | 66 | return IIO_VAL_FRACTIONAL_LOG2; |
49 | 67 | ||
@@ -54,10 +72,53 @@ static int adc081c_read_raw(struct iio_dev *iio, | |||
54 | return -EINVAL; | 72 | return -EINVAL; |
55 | } | 73 | } |
56 | 74 | ||
57 | static const struct iio_chan_spec adc081c_channel = { | 75 | #define ADCxx1C_CHAN(_bits) { \ |
58 | .type = IIO_VOLTAGE, | 76 | .type = IIO_VOLTAGE, \ |
59 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | 77 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ |
60 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | 78 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ |
79 | .scan_type = { \ | ||
80 | .sign = 'u', \ | ||
81 | .realbits = (_bits), \ | ||
82 | .storagebits = 16, \ | ||
83 | .shift = 12 - (_bits), \ | ||
84 | .endianness = IIO_CPU, \ | ||
85 | }, \ | ||
86 | } | ||
87 | |||
88 | #define DEFINE_ADCxx1C_CHANNELS(_name, _bits) \ | ||
89 | static const struct iio_chan_spec _name ## _channels[] = { \ | ||
90 | ADCxx1C_CHAN((_bits)), \ | ||
91 | IIO_CHAN_SOFT_TIMESTAMP(1), \ | ||
92 | }; \ | ||
93 | |||
94 | #define ADC081C_NUM_CHANNELS 2 | ||
95 | |||
96 | struct adcxx1c_model { | ||
97 | const struct iio_chan_spec* channels; | ||
98 | int bits; | ||
99 | }; | ||
100 | |||
101 | #define ADCxx1C_MODEL(_name, _bits) \ | ||
102 | { \ | ||
103 | .channels = _name ## _channels, \ | ||
104 | .bits = (_bits), \ | ||
105 | } | ||
106 | |||
107 | DEFINE_ADCxx1C_CHANNELS(adc081c, 8); | ||
108 | DEFINE_ADCxx1C_CHANNELS(adc101c, 10); | ||
109 | DEFINE_ADCxx1C_CHANNELS(adc121c, 12); | ||
110 | |||
111 | /* Model ids are indexes in _models array */ | ||
112 | enum adcxx1c_model_id { | ||
113 | ADC081C = 0, | ||
114 | ADC101C = 1, | ||
115 | ADC121C = 2, | ||
116 | }; | ||
117 | |||
118 | static struct adcxx1c_model adcxx1c_models[] = { | ||
119 | ADCxx1C_MODEL(adc081c, 8), | ||
120 | ADCxx1C_MODEL(adc101c, 10), | ||
121 | ADCxx1C_MODEL(adc121c, 12), | ||
61 | }; | 122 | }; |
62 | 123 | ||
63 | static const struct iio_info adc081c_info = { | 124 | static const struct iio_info adc081c_info = { |
@@ -65,11 +126,30 @@ static const struct iio_info adc081c_info = { | |||
65 | .driver_module = THIS_MODULE, | 126 | .driver_module = THIS_MODULE, |
66 | }; | 127 | }; |
67 | 128 | ||
129 | static irqreturn_t adc081c_trigger_handler(int irq, void *p) | ||
130 | { | ||
131 | struct iio_poll_func *pf = p; | ||
132 | struct iio_dev *indio_dev = pf->indio_dev; | ||
133 | struct adc081c *data = iio_priv(indio_dev); | ||
134 | u16 buf[8]; /* 2 bytes data + 6 bytes padding + 8 bytes timestamp */ | ||
135 | int ret; | ||
136 | |||
137 | ret = i2c_smbus_read_word_swapped(data->i2c, REG_CONV_RES); | ||
138 | if (ret < 0) | ||
139 | goto out; | ||
140 | buf[0] = ret; | ||
141 | iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); | ||
142 | out: | ||
143 | iio_trigger_notify_done(indio_dev->trig); | ||
144 | return IRQ_HANDLED; | ||
145 | } | ||
146 | |||
68 | static int adc081c_probe(struct i2c_client *client, | 147 | static int adc081c_probe(struct i2c_client *client, |
69 | const struct i2c_device_id *id) | 148 | const struct i2c_device_id *id) |
70 | { | 149 | { |
71 | struct iio_dev *iio; | 150 | struct iio_dev *iio; |
72 | struct adc081c *adc; | 151 | struct adc081c *adc; |
152 | struct adcxx1c_model *model = &adcxx1c_models[id->driver_data]; | ||
73 | int err; | 153 | int err; |
74 | 154 | ||
75 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) | 155 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA)) |
@@ -81,6 +161,7 @@ static int adc081c_probe(struct i2c_client *client, | |||
81 | 161 | ||
82 | adc = iio_priv(iio); | 162 | adc = iio_priv(iio); |
83 | adc->i2c = client; | 163 | adc->i2c = client; |
164 | adc->bits = model->bits; | ||
84 | 165 | ||
85 | adc->ref = devm_regulator_get(&client->dev, "vref"); | 166 | adc->ref = devm_regulator_get(&client->dev, "vref"); |
86 | if (IS_ERR(adc->ref)) | 167 | if (IS_ERR(adc->ref)) |
@@ -95,18 +176,26 @@ static int adc081c_probe(struct i2c_client *client, | |||
95 | iio->modes = INDIO_DIRECT_MODE; | 176 | iio->modes = INDIO_DIRECT_MODE; |
96 | iio->info = &adc081c_info; | 177 | iio->info = &adc081c_info; |
97 | 178 | ||
98 | iio->channels = &adc081c_channel; | 179 | iio->channels = model->channels; |
99 | iio->num_channels = 1; | 180 | iio->num_channels = ADC081C_NUM_CHANNELS; |
181 | |||
182 | err = iio_triggered_buffer_setup(iio, NULL, adc081c_trigger_handler, NULL); | ||
183 | if (err < 0) { | ||
184 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); | ||
185 | goto err_regulator_disable; | ||
186 | } | ||
100 | 187 | ||
101 | err = iio_device_register(iio); | 188 | err = iio_device_register(iio); |
102 | if (err < 0) | 189 | if (err < 0) |
103 | goto regulator_disable; | 190 | goto err_buffer_cleanup; |
104 | 191 | ||
105 | i2c_set_clientdata(client, iio); | 192 | i2c_set_clientdata(client, iio); |
106 | 193 | ||
107 | return 0; | 194 | return 0; |
108 | 195 | ||
109 | regulator_disable: | 196 | err_buffer_cleanup: |
197 | iio_triggered_buffer_cleanup(iio); | ||
198 | err_regulator_disable: | ||
110 | regulator_disable(adc->ref); | 199 | regulator_disable(adc->ref); |
111 | 200 | ||
112 | return err; | 201 | return err; |
@@ -118,13 +207,16 @@ static int adc081c_remove(struct i2c_client *client) | |||
118 | struct adc081c *adc = iio_priv(iio); | 207 | struct adc081c *adc = iio_priv(iio); |
119 | 208 | ||
120 | iio_device_unregister(iio); | 209 | iio_device_unregister(iio); |
210 | iio_triggered_buffer_cleanup(iio); | ||
121 | regulator_disable(adc->ref); | 211 | regulator_disable(adc->ref); |
122 | 212 | ||
123 | return 0; | 213 | return 0; |
124 | } | 214 | } |
125 | 215 | ||
126 | static const struct i2c_device_id adc081c_id[] = { | 216 | static const struct i2c_device_id adc081c_id[] = { |
127 | { "adc081c", 0 }, | 217 | { "adc081c", ADC081C }, |
218 | { "adc101c", ADC101C }, | ||
219 | { "adc121c", ADC121C }, | ||
128 | { } | 220 | { } |
129 | }; | 221 | }; |
130 | MODULE_DEVICE_TABLE(i2c, adc081c_id); | 222 | MODULE_DEVICE_TABLE(i2c, adc081c_id); |
@@ -132,6 +224,8 @@ MODULE_DEVICE_TABLE(i2c, adc081c_id); | |||
132 | #ifdef CONFIG_OF | 224 | #ifdef CONFIG_OF |
133 | static const struct of_device_id adc081c_of_match[] = { | 225 | static const struct of_device_id adc081c_of_match[] = { |
134 | { .compatible = "ti,adc081c" }, | 226 | { .compatible = "ti,adc081c" }, |
227 | { .compatible = "ti,adc101c" }, | ||
228 | { .compatible = "ti,adc121c" }, | ||
135 | { } | 229 | { } |
136 | }; | 230 | }; |
137 | MODULE_DEVICE_TABLE(of, adc081c_of_match); | 231 | MODULE_DEVICE_TABLE(of, adc081c_of_match); |
@@ -149,5 +243,5 @@ static struct i2c_driver adc081c_driver = { | |||
149 | module_i2c_driver(adc081c_driver); | 243 | module_i2c_driver(adc081c_driver); |
150 | 244 | ||
151 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); | 245 | MODULE_AUTHOR("Thierry Reding <thierry.reding@avionic-design.de>"); |
152 | MODULE_DESCRIPTION("Texas Instruments ADC081C021/027 driver"); | 246 | MODULE_DESCRIPTION("Texas Instruments ADC081C/ADC101C/ADC121C driver"); |
153 | MODULE_LICENSE("GPL v2"); | 247 | MODULE_LICENSE("GPL v2"); |
diff --git a/drivers/iio/adc/vf610_adc.c b/drivers/iio/adc/vf610_adc.c index b10f629cc44b..653bf1379d2e 100644 --- a/drivers/iio/adc/vf610_adc.c +++ b/drivers/iio/adc/vf610_adc.c | |||
@@ -714,19 +714,19 @@ static int vf610_write_raw(struct iio_dev *indio_dev, | |||
714 | int i; | 714 | int i; |
715 | 715 | ||
716 | switch (mask) { | 716 | switch (mask) { |
717 | case IIO_CHAN_INFO_SAMP_FREQ: | 717 | case IIO_CHAN_INFO_SAMP_FREQ: |
718 | for (i = 0; | 718 | for (i = 0; |
719 | i < ARRAY_SIZE(info->sample_freq_avail); | 719 | i < ARRAY_SIZE(info->sample_freq_avail); |
720 | i++) | 720 | i++) |
721 | if (val == info->sample_freq_avail[i]) { | 721 | if (val == info->sample_freq_avail[i]) { |
722 | info->adc_feature.sample_rate = i; | 722 | info->adc_feature.sample_rate = i; |
723 | vf610_adc_sample_set(info); | 723 | vf610_adc_sample_set(info); |
724 | return 0; | 724 | return 0; |
725 | } | 725 | } |
726 | break; | 726 | break; |
727 | 727 | ||
728 | default: | 728 | default: |
729 | break; | 729 | break; |
730 | } | 730 | } |
731 | 731 | ||
732 | return -EINVAL; | 732 | return -EINVAL; |
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c index 595511022795..5b41f9d0d4f3 100644 --- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c +++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c | |||
@@ -115,7 +115,7 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state) | |||
115 | return ret; | 115 | return ret; |
116 | } | 116 | } |
117 | 117 | ||
118 | return 0; | 118 | return 0; |
119 | #else | 119 | #else |
120 | atomic_set(&st->user_requested_state, state); | 120 | atomic_set(&st->user_requested_state, state); |
121 | return _hid_sensor_power_state(st, state); | 121 | return _hid_sensor_power_state(st, state); |
diff --git a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c index 669dc7c270f5..ecf7721ecaf4 100644 --- a/drivers/iio/common/ms_sensors/ms_sensors_i2c.c +++ b/drivers/iio/common/ms_sensors/ms_sensors_i2c.c | |||
@@ -106,7 +106,7 @@ int ms_sensors_convert_and_read(void *cli, u8 conv, u8 rd, | |||
106 | unsigned int delay, u32 *adc) | 106 | unsigned int delay, u32 *adc) |
107 | { | 107 | { |
108 | int ret; | 108 | int ret; |
109 | __be32 buf = 0; | 109 | __be32 buf = 0; |
110 | struct i2c_client *client = (struct i2c_client *)cli; | 110 | struct i2c_client *client = (struct i2c_client *)cli; |
111 | 111 | ||
112 | /* Trigger conversion */ | 112 | /* Trigger conversion */ |
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c index 73764961feac..c55898543a47 100644 --- a/drivers/iio/common/st_sensors/st_sensors_buffer.c +++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c | |||
@@ -24,67 +24,30 @@ | |||
24 | 24 | ||
25 | int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf) | 25 | int st_sensors_get_buffer_element(struct iio_dev *indio_dev, u8 *buf) |
26 | { | 26 | { |
27 | u8 addr[3]; /* no ST sensor has more than 3 channels */ | 27 | int i, len; |
28 | int i, n = 0, len; | 28 | int total = 0; |
29 | struct st_sensor_data *sdata = iio_priv(indio_dev); | 29 | struct st_sensor_data *sdata = iio_priv(indio_dev); |
30 | unsigned int num_data_channels = sdata->num_data_channels; | 30 | unsigned int num_data_channels = sdata->num_data_channels; |
31 | unsigned int byte_for_channel = | ||
32 | indio_dev->channels[0].scan_type.storagebits >> 3; | ||
33 | 31 | ||
34 | for (i = 0; i < num_data_channels; i++) { | 32 | for (i = 0; i < num_data_channels; i++) { |
33 | unsigned int bytes_to_read; | ||
34 | |||
35 | if (test_bit(i, indio_dev->active_scan_mask)) { | 35 | if (test_bit(i, indio_dev->active_scan_mask)) { |
36 | addr[n] = indio_dev->channels[i].address; | 36 | bytes_to_read = indio_dev->channels[i].scan_type.storagebits >> 3; |
37 | n++; | ||
38 | } | ||
39 | } | ||
40 | switch (n) { | ||
41 | case 1: | ||
42 | len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev, | ||
43 | addr[0], byte_for_channel, buf, sdata->multiread_bit); | ||
44 | break; | ||
45 | case 2: | ||
46 | if ((addr[1] - addr[0]) == byte_for_channel) { | ||
47 | len = sdata->tf->read_multiple_byte(&sdata->tb, | 37 | len = sdata->tf->read_multiple_byte(&sdata->tb, |
48 | sdata->dev, addr[0], byte_for_channel * n, | 38 | sdata->dev, indio_dev->channels[i].address, |
49 | buf, sdata->multiread_bit); | 39 | bytes_to_read, |
50 | } else { | 40 | buf + total, sdata->multiread_bit); |
51 | u8 *rx_array; | ||
52 | rx_array = kmalloc(byte_for_channel * num_data_channels, | ||
53 | GFP_KERNEL); | ||
54 | if (!rx_array) | ||
55 | return -ENOMEM; | ||
56 | 41 | ||
57 | len = sdata->tf->read_multiple_byte(&sdata->tb, | 42 | if (len < bytes_to_read) |
58 | sdata->dev, addr[0], | 43 | return -EIO; |
59 | byte_for_channel * num_data_channels, | 44 | |
60 | rx_array, sdata->multiread_bit); | 45 | /* Advance the buffer pointer */ |
61 | if (len < 0) { | 46 | total += len; |
62 | kfree(rx_array); | ||
63 | return len; | ||
64 | } | ||
65 | |||
66 | for (i = 0; i < n * byte_for_channel; i++) { | ||
67 | if (i < n) | ||
68 | buf[i] = rx_array[i]; | ||
69 | else | ||
70 | buf[i] = rx_array[n + i]; | ||
71 | } | ||
72 | kfree(rx_array); | ||
73 | len = byte_for_channel * n; | ||
74 | } | 47 | } |
75 | break; | ||
76 | case 3: | ||
77 | len = sdata->tf->read_multiple_byte(&sdata->tb, sdata->dev, | ||
78 | addr[0], byte_for_channel * num_data_channels, | ||
79 | buf, sdata->multiread_bit); | ||
80 | break; | ||
81 | default: | ||
82 | return -EINVAL; | ||
83 | } | 48 | } |
84 | if (len != byte_for_channel * n) | ||
85 | return -EIO; | ||
86 | 49 | ||
87 | return len; | 50 | return total; |
88 | } | 51 | } |
89 | EXPORT_SYMBOL(st_sensors_get_buffer_element); | 52 | EXPORT_SYMBOL(st_sensors_get_buffer_element); |
90 | 53 | ||
@@ -95,6 +58,24 @@ irqreturn_t st_sensors_trigger_handler(int irq, void *p) | |||
95 | struct iio_dev *indio_dev = pf->indio_dev; | 58 | struct iio_dev *indio_dev = pf->indio_dev; |
96 | struct st_sensor_data *sdata = iio_priv(indio_dev); | 59 | struct st_sensor_data *sdata = iio_priv(indio_dev); |
97 | 60 | ||
61 | /* If we have a status register, check if this IRQ came from us */ | ||
62 | if (sdata->sensor_settings->drdy_irq.addr_stat_drdy) { | ||
63 | u8 status; | ||
64 | |||
65 | len = sdata->tf->read_byte(&sdata->tb, sdata->dev, | ||
66 | sdata->sensor_settings->drdy_irq.addr_stat_drdy, | ||
67 | &status); | ||
68 | if (len < 0) | ||
69 | dev_err(sdata->dev, "could not read channel status\n"); | ||
70 | |||
71 | /* | ||
72 | * If this was not caused by any channels on this sensor, | ||
73 | * return IRQ_NONE | ||
74 | */ | ||
75 | if (!(status & (u8)indio_dev->active_scan_mask[0])) | ||
76 | return IRQ_NONE; | ||
77 | } | ||
78 | |||
98 | len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data); | 79 | len = st_sensors_get_buffer_element(indio_dev, sdata->buffer_data); |
99 | if (len < 0) | 80 | if (len < 0) |
100 | goto st_sensors_get_buffer_element_error; | 81 | goto st_sensors_get_buffer_element_error; |
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c index f5a2d445d0c0..dffe00692169 100644 --- a/drivers/iio/common/st_sensors/st_sensors_core.c +++ b/drivers/iio/common/st_sensors/st_sensors_core.c | |||
@@ -301,6 +301,14 @@ static int st_sensors_set_drdy_int_pin(struct iio_dev *indio_dev, | |||
301 | return -EINVAL; | 301 | return -EINVAL; |
302 | } | 302 | } |
303 | 303 | ||
304 | if (pdata->open_drain) { | ||
305 | if (!sdata->sensor_settings->drdy_irq.addr_od) | ||
306 | dev_err(&indio_dev->dev, | ||
307 | "open drain requested but unsupported.\n"); | ||
308 | else | ||
309 | sdata->int_pin_open_drain = true; | ||
310 | } | ||
311 | |||
304 | return 0; | 312 | return 0; |
305 | } | 313 | } |
306 | 314 | ||
@@ -321,6 +329,8 @@ static struct st_sensors_platform_data *st_sensors_of_probe(struct device *dev, | |||
321 | else | 329 | else |
322 | pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0; | 330 | pdata->drdy_int_pin = defdata ? defdata->drdy_int_pin : 0; |
323 | 331 | ||
332 | pdata->open_drain = of_property_read_bool(np, "drive-open-drain"); | ||
333 | |||
324 | return pdata; | 334 | return pdata; |
325 | } | 335 | } |
326 | #else | 336 | #else |
@@ -374,6 +384,16 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev, | |||
374 | return err; | 384 | return err; |
375 | } | 385 | } |
376 | 386 | ||
387 | if (sdata->int_pin_open_drain) { | ||
388 | dev_info(&indio_dev->dev, | ||
389 | "set interrupt line to open drain mode\n"); | ||
390 | err = st_sensors_write_data_with_mask(indio_dev, | ||
391 | sdata->sensor_settings->drdy_irq.addr_od, | ||
392 | sdata->sensor_settings->drdy_irq.mask_od, 1); | ||
393 | if (err < 0) | ||
394 | return err; | ||
395 | } | ||
396 | |||
377 | err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS); | 397 | err = st_sensors_set_axis_enable(indio_dev, ST_SENSORS_ENABLE_ALL_AXIS); |
378 | 398 | ||
379 | return err; | 399 | return err; |
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c index 6a8c98327945..da72279fcf99 100644 --- a/drivers/iio/common/st_sensors/st_sensors_trigger.c +++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c | |||
@@ -64,6 +64,19 @@ int st_sensors_allocate_trigger(struct iio_dev *indio_dev, | |||
64 | "rising edge\n", irq_trig); | 64 | "rising edge\n", irq_trig); |
65 | irq_trig = IRQF_TRIGGER_RISING; | 65 | irq_trig = IRQF_TRIGGER_RISING; |
66 | } | 66 | } |
67 | |||
68 | /* | ||
69 | * If the interrupt pin is Open Drain, by definition this | ||
70 | * means that the interrupt line may be shared with other | ||
71 | * peripherals. But to do this we also need to have a status | ||
72 | * register and mask to figure out if this sensor was firing | ||
73 | * the IRQ or not, so we can tell the interrupt handle that | ||
74 | * it was "our" interrupt. | ||
75 | */ | ||
76 | if (sdata->int_pin_open_drain && | ||
77 | sdata->sensor_settings->drdy_irq.addr_stat_drdy) | ||
78 | irq_trig |= IRQF_SHARED; | ||
79 | |||
67 | err = request_threaded_irq(irq, | 80 | err = request_threaded_irq(irq, |
68 | iio_trigger_generic_data_rdy_poll, | 81 | iio_trigger_generic_data_rdy_poll, |
69 | NULL, | 82 | NULL, |
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig index 210db81ca144..61d5008bff5c 100644 --- a/drivers/iio/dac/Kconfig +++ b/drivers/iio/dac/Kconfig | |||
@@ -74,6 +74,33 @@ config AD5449 | |||
74 | To compile this driver as a module, choose M here: the | 74 | To compile this driver as a module, choose M here: the |
75 | module will be called ad5449. | 75 | module will be called ad5449. |
76 | 76 | ||
77 | config AD5592R_BASE | ||
78 | tristate | ||
79 | |||
80 | config AD5592R | ||
81 | tristate "Analog Devices AD5592R ADC/DAC driver" | ||
82 | depends on SPI_MASTER | ||
83 | select GPIOLIB | ||
84 | select AD5592R_BASE | ||
85 | help | ||
86 | Say yes here to build support for Analog Devices AD5592R | ||
87 | Digital to Analog / Analog to Digital Converter. | ||
88 | |||
89 | To compile this driver as a module, choose M here: the | ||
90 | module will be called ad5592r. | ||
91 | |||
92 | config AD5593R | ||
93 | tristate "Analog Devices AD5593R ADC/DAC driver" | ||
94 | depends on I2C | ||
95 | select GPIOLIB | ||
96 | select AD5592R_BASE | ||
97 | help | ||
98 | Say yes here to build support for Analog Devices AD5593R | ||
99 | Digital to Analog / Analog to Digital Converter. | ||
100 | |||
101 | To compile this driver as a module, choose M here: the | ||
102 | module will be called ad5593r. | ||
103 | |||
77 | config AD5504 | 104 | config AD5504 |
78 | tristate "Analog Devices AD5504/AD5501 DAC SPI driver" | 105 | tristate "Analog Devices AD5504/AD5501 DAC SPI driver" |
79 | depends on SPI | 106 | depends on SPI |
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile index 420a15cdaa53..8b78d5ca9b11 100644 --- a/drivers/iio/dac/Makefile +++ b/drivers/iio/dac/Makefile | |||
@@ -11,6 +11,9 @@ obj-$(CONFIG_AD5064) += ad5064.o | |||
11 | obj-$(CONFIG_AD5504) += ad5504.o | 11 | obj-$(CONFIG_AD5504) += ad5504.o |
12 | obj-$(CONFIG_AD5446) += ad5446.o | 12 | obj-$(CONFIG_AD5446) += ad5446.o |
13 | obj-$(CONFIG_AD5449) += ad5449.o | 13 | obj-$(CONFIG_AD5449) += ad5449.o |
14 | obj-$(CONFIG_AD5592R_BASE) += ad5592r-base.o | ||
15 | obj-$(CONFIG_AD5592R) += ad5592r.o | ||
16 | obj-$(CONFIG_AD5593R) += ad5593r.o | ||
14 | obj-$(CONFIG_AD5755) += ad5755.o | 17 | obj-$(CONFIG_AD5755) += ad5755.o |
15 | obj-$(CONFIG_AD5761) += ad5761.o | 18 | obj-$(CONFIG_AD5761) += ad5761.o |
16 | obj-$(CONFIG_AD5764) += ad5764.o | 19 | obj-$(CONFIG_AD5764) += ad5764.o |
diff --git a/drivers/iio/dac/ad5592r-base.c b/drivers/iio/dac/ad5592r-base.c new file mode 100644 index 000000000000..948f600e7059 --- /dev/null +++ b/drivers/iio/dac/ad5592r-base.c | |||
@@ -0,0 +1,691 @@ | |||
1 | /* | ||
2 | * AD5592R Digital <-> Analog converters driver | ||
3 | * | ||
4 | * Copyright 2014-2016 Analog Devices Inc. | ||
5 | * Author: Paul Cercueil <paul.cercueil@analog.com> | ||
6 | * | ||
7 | * Licensed under the GPL-2. | ||
8 | */ | ||
9 | |||
10 | #include <linux/bitops.h> | ||
11 | #include <linux/delay.h> | ||
12 | #include <linux/iio/iio.h> | ||
13 | #include <linux/module.h> | ||
14 | #include <linux/mutex.h> | ||
15 | #include <linux/of.h> | ||
16 | #include <linux/regulator/consumer.h> | ||
17 | #include <linux/gpio/consumer.h> | ||
18 | #include <linux/gpio/driver.h> | ||
19 | #include <linux/gpio.h> | ||
20 | #include <linux/property.h> | ||
21 | |||
22 | #include <dt-bindings/iio/adi,ad5592r.h> | ||
23 | |||
24 | #include "ad5592r-base.h" | ||
25 | |||
26 | static int ad5592r_gpio_get(struct gpio_chip *chip, unsigned offset) | ||
27 | { | ||
28 | struct ad5592r_state *st = gpiochip_get_data(chip); | ||
29 | int ret = 0; | ||
30 | u8 val; | ||
31 | |||
32 | mutex_lock(&st->gpio_lock); | ||
33 | |||
34 | if (st->gpio_out & BIT(offset)) | ||
35 | val = st->gpio_val; | ||
36 | else | ||
37 | ret = st->ops->gpio_read(st, &val); | ||
38 | |||
39 | mutex_unlock(&st->gpio_lock); | ||
40 | |||
41 | if (ret < 0) | ||
42 | return ret; | ||
43 | |||
44 | return !!(val & BIT(offset)); | ||
45 | } | ||
46 | |||
47 | static void ad5592r_gpio_set(struct gpio_chip *chip, unsigned offset, int value) | ||
48 | { | ||
49 | struct ad5592r_state *st = gpiochip_get_data(chip); | ||
50 | |||
51 | mutex_lock(&st->gpio_lock); | ||
52 | |||
53 | if (value) | ||
54 | st->gpio_val |= BIT(offset); | ||
55 | else | ||
56 | st->gpio_val &= ~BIT(offset); | ||
57 | |||
58 | st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); | ||
59 | |||
60 | mutex_unlock(&st->gpio_lock); | ||
61 | } | ||
62 | |||
63 | static int ad5592r_gpio_direction_input(struct gpio_chip *chip, unsigned offset) | ||
64 | { | ||
65 | struct ad5592r_state *st = gpiochip_get_data(chip); | ||
66 | int ret; | ||
67 | |||
68 | mutex_lock(&st->gpio_lock); | ||
69 | |||
70 | st->gpio_out &= ~BIT(offset); | ||
71 | st->gpio_in |= BIT(offset); | ||
72 | |||
73 | ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); | ||
74 | if (ret < 0) | ||
75 | goto err_unlock; | ||
76 | |||
77 | ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); | ||
78 | |||
79 | err_unlock: | ||
80 | mutex_unlock(&st->gpio_lock); | ||
81 | |||
82 | return ret; | ||
83 | } | ||
84 | |||
85 | static int ad5592r_gpio_direction_output(struct gpio_chip *chip, | ||
86 | unsigned offset, int value) | ||
87 | { | ||
88 | struct ad5592r_state *st = gpiochip_get_data(chip); | ||
89 | int ret; | ||
90 | |||
91 | mutex_lock(&st->gpio_lock); | ||
92 | |||
93 | if (value) | ||
94 | st->gpio_val |= BIT(offset); | ||
95 | else | ||
96 | st->gpio_val &= ~BIT(offset); | ||
97 | |||
98 | st->gpio_in &= ~BIT(offset); | ||
99 | st->gpio_out |= BIT(offset); | ||
100 | |||
101 | ret = st->ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); | ||
102 | if (ret < 0) | ||
103 | goto err_unlock; | ||
104 | |||
105 | ret = st->ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); | ||
106 | if (ret < 0) | ||
107 | goto err_unlock; | ||
108 | |||
109 | ret = st->ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); | ||
110 | |||
111 | err_unlock: | ||
112 | mutex_unlock(&st->gpio_lock); | ||
113 | |||
114 | return ret; | ||
115 | } | ||
116 | |||
117 | static int ad5592r_gpio_request(struct gpio_chip *chip, unsigned offset) | ||
118 | { | ||
119 | struct ad5592r_state *st = gpiochip_get_data(chip); | ||
120 | |||
121 | if (!(st->gpio_map & BIT(offset))) { | ||
122 | dev_err(st->dev, "GPIO %d is reserved by alternate function\n", | ||
123 | offset); | ||
124 | return -ENODEV; | ||
125 | } | ||
126 | |||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | static int ad5592r_gpio_init(struct ad5592r_state *st) | ||
131 | { | ||
132 | if (!st->gpio_map) | ||
133 | return 0; | ||
134 | |||
135 | st->gpiochip.label = dev_name(st->dev); | ||
136 | st->gpiochip.base = -1; | ||
137 | st->gpiochip.ngpio = 8; | ||
138 | st->gpiochip.parent = st->dev; | ||
139 | st->gpiochip.can_sleep = true; | ||
140 | st->gpiochip.direction_input = ad5592r_gpio_direction_input; | ||
141 | st->gpiochip.direction_output = ad5592r_gpio_direction_output; | ||
142 | st->gpiochip.get = ad5592r_gpio_get; | ||
143 | st->gpiochip.set = ad5592r_gpio_set; | ||
144 | st->gpiochip.request = ad5592r_gpio_request; | ||
145 | st->gpiochip.owner = THIS_MODULE; | ||
146 | |||
147 | mutex_init(&st->gpio_lock); | ||
148 | |||
149 | return gpiochip_add_data(&st->gpiochip, st); | ||
150 | } | ||
151 | |||
152 | static void ad5592r_gpio_cleanup(struct ad5592r_state *st) | ||
153 | { | ||
154 | if (st->gpio_map) | ||
155 | gpiochip_remove(&st->gpiochip); | ||
156 | } | ||
157 | |||
158 | static int ad5592r_reset(struct ad5592r_state *st) | ||
159 | { | ||
160 | struct gpio_desc *gpio; | ||
161 | struct iio_dev *iio_dev = iio_priv_to_dev(st); | ||
162 | |||
163 | gpio = devm_gpiod_get_optional(st->dev, "reset", GPIOD_OUT_LOW); | ||
164 | if (IS_ERR(gpio)) | ||
165 | return PTR_ERR(gpio); | ||
166 | |||
167 | if (gpio) { | ||
168 | udelay(1); | ||
169 | gpiod_set_value(gpio, 1); | ||
170 | } else { | ||
171 | mutex_lock(&iio_dev->mlock); | ||
172 | /* Writing this magic value resets the device */ | ||
173 | st->ops->reg_write(st, AD5592R_REG_RESET, 0xdac); | ||
174 | mutex_unlock(&iio_dev->mlock); | ||
175 | } | ||
176 | |||
177 | udelay(250); | ||
178 | |||
179 | return 0; | ||
180 | } | ||
181 | |||
182 | static int ad5592r_get_vref(struct ad5592r_state *st) | ||
183 | { | ||
184 | int ret; | ||
185 | |||
186 | if (st->reg) { | ||
187 | ret = regulator_get_voltage(st->reg); | ||
188 | if (ret < 0) | ||
189 | return ret; | ||
190 | |||
191 | return ret / 1000; | ||
192 | } else { | ||
193 | return 2500; | ||
194 | } | ||
195 | } | ||
196 | |||
197 | static int ad5592r_set_channel_modes(struct ad5592r_state *st) | ||
198 | { | ||
199 | const struct ad5592r_rw_ops *ops = st->ops; | ||
200 | int ret; | ||
201 | unsigned i; | ||
202 | struct iio_dev *iio_dev = iio_priv_to_dev(st); | ||
203 | u8 pulldown = 0, tristate = 0, dac = 0, adc = 0; | ||
204 | u16 read_back; | ||
205 | |||
206 | for (i = 0; i < st->num_channels; i++) { | ||
207 | switch (st->channel_modes[i]) { | ||
208 | case CH_MODE_DAC: | ||
209 | dac |= BIT(i); | ||
210 | break; | ||
211 | |||
212 | case CH_MODE_ADC: | ||
213 | adc |= BIT(i); | ||
214 | break; | ||
215 | |||
216 | case CH_MODE_DAC_AND_ADC: | ||
217 | dac |= BIT(i); | ||
218 | adc |= BIT(i); | ||
219 | break; | ||
220 | |||
221 | case CH_MODE_GPIO: | ||
222 | st->gpio_map |= BIT(i); | ||
223 | st->gpio_in |= BIT(i); /* Default to input */ | ||
224 | break; | ||
225 | |||
226 | case CH_MODE_UNUSED: | ||
227 | /* fall-through */ | ||
228 | default: | ||
229 | switch (st->channel_offstate[i]) { | ||
230 | case CH_OFFSTATE_OUT_TRISTATE: | ||
231 | tristate |= BIT(i); | ||
232 | break; | ||
233 | |||
234 | case CH_OFFSTATE_OUT_LOW: | ||
235 | st->gpio_out |= BIT(i); | ||
236 | break; | ||
237 | |||
238 | case CH_OFFSTATE_OUT_HIGH: | ||
239 | st->gpio_out |= BIT(i); | ||
240 | st->gpio_val |= BIT(i); | ||
241 | break; | ||
242 | |||
243 | case CH_OFFSTATE_PULLDOWN: | ||
244 | /* fall-through */ | ||
245 | default: | ||
246 | pulldown |= BIT(i); | ||
247 | break; | ||
248 | } | ||
249 | } | ||
250 | } | ||
251 | |||
252 | mutex_lock(&iio_dev->mlock); | ||
253 | |||
254 | /* Pull down unused pins to GND */ | ||
255 | ret = ops->reg_write(st, AD5592R_REG_PULLDOWN, pulldown); | ||
256 | if (ret) | ||
257 | goto err_unlock; | ||
258 | |||
259 | ret = ops->reg_write(st, AD5592R_REG_TRISTATE, tristate); | ||
260 | if (ret) | ||
261 | goto err_unlock; | ||
262 | |||
263 | /* Configure pins that we use */ | ||
264 | ret = ops->reg_write(st, AD5592R_REG_DAC_EN, dac); | ||
265 | if (ret) | ||
266 | goto err_unlock; | ||
267 | |||
268 | ret = ops->reg_write(st, AD5592R_REG_ADC_EN, adc); | ||
269 | if (ret) | ||
270 | goto err_unlock; | ||
271 | |||
272 | ret = ops->reg_write(st, AD5592R_REG_GPIO_SET, st->gpio_val); | ||
273 | if (ret) | ||
274 | goto err_unlock; | ||
275 | |||
276 | ret = ops->reg_write(st, AD5592R_REG_GPIO_OUT_EN, st->gpio_out); | ||
277 | if (ret) | ||
278 | goto err_unlock; | ||
279 | |||
280 | ret = ops->reg_write(st, AD5592R_REG_GPIO_IN_EN, st->gpio_in); | ||
281 | if (ret) | ||
282 | goto err_unlock; | ||
283 | |||
284 | /* Verify that we can read back at least one register */ | ||
285 | ret = ops->reg_read(st, AD5592R_REG_ADC_EN, &read_back); | ||
286 | if (!ret && (read_back & 0xff) != adc) | ||
287 | ret = -EIO; | ||
288 | |||
289 | err_unlock: | ||
290 | mutex_unlock(&iio_dev->mlock); | ||
291 | return ret; | ||
292 | } | ||
293 | |||
294 | static int ad5592r_reset_channel_modes(struct ad5592r_state *st) | ||
295 | { | ||
296 | int i; | ||
297 | |||
298 | for (i = 0; i < ARRAY_SIZE(st->channel_modes); i++) | ||
299 | st->channel_modes[i] = CH_MODE_UNUSED; | ||
300 | |||
301 | return ad5592r_set_channel_modes(st); | ||
302 | } | ||
303 | |||
304 | static int ad5592r_write_raw(struct iio_dev *iio_dev, | ||
305 | struct iio_chan_spec const *chan, int val, int val2, long mask) | ||
306 | { | ||
307 | struct ad5592r_state *st = iio_priv(iio_dev); | ||
308 | int ret; | ||
309 | |||
310 | switch (mask) { | ||
311 | case IIO_CHAN_INFO_RAW: | ||
312 | |||
313 | if (val >= (1 << chan->scan_type.realbits) || val < 0) | ||
314 | return -EINVAL; | ||
315 | |||
316 | if (!chan->output) | ||
317 | return -EINVAL; | ||
318 | |||
319 | mutex_lock(&iio_dev->mlock); | ||
320 | ret = st->ops->write_dac(st, chan->channel, val); | ||
321 | if (!ret) | ||
322 | st->cached_dac[chan->channel] = val; | ||
323 | mutex_unlock(&iio_dev->mlock); | ||
324 | return ret; | ||
325 | case IIO_CHAN_INFO_SCALE: | ||
326 | if (chan->type == IIO_VOLTAGE) { | ||
327 | bool gain; | ||
328 | |||
329 | if (val == st->scale_avail[0][0] && | ||
330 | val2 == st->scale_avail[0][1]) | ||
331 | gain = false; | ||
332 | else if (val == st->scale_avail[1][0] && | ||
333 | val2 == st->scale_avail[1][1]) | ||
334 | gain = true; | ||
335 | else | ||
336 | return -EINVAL; | ||
337 | |||
338 | mutex_lock(&iio_dev->mlock); | ||
339 | |||
340 | ret = st->ops->reg_read(st, AD5592R_REG_CTRL, | ||
341 | &st->cached_gp_ctrl); | ||
342 | if (ret < 0) { | ||
343 | mutex_unlock(&iio_dev->mlock); | ||
344 | return ret; | ||
345 | } | ||
346 | |||
347 | if (chan->output) { | ||
348 | if (gain) | ||
349 | st->cached_gp_ctrl |= | ||
350 | AD5592R_REG_CTRL_DAC_RANGE; | ||
351 | else | ||
352 | st->cached_gp_ctrl &= | ||
353 | ~AD5592R_REG_CTRL_DAC_RANGE; | ||
354 | } else { | ||
355 | if (gain) | ||
356 | st->cached_gp_ctrl |= | ||
357 | AD5592R_REG_CTRL_ADC_RANGE; | ||
358 | else | ||
359 | st->cached_gp_ctrl &= | ||
360 | ~AD5592R_REG_CTRL_ADC_RANGE; | ||
361 | } | ||
362 | |||
363 | ret = st->ops->reg_write(st, AD5592R_REG_CTRL, | ||
364 | st->cached_gp_ctrl); | ||
365 | mutex_unlock(&iio_dev->mlock); | ||
366 | |||
367 | return ret; | ||
368 | } | ||
369 | break; | ||
370 | default: | ||
371 | return -EINVAL; | ||
372 | } | ||
373 | |||
374 | return 0; | ||
375 | } | ||
376 | |||
377 | static int ad5592r_read_raw(struct iio_dev *iio_dev, | ||
378 | struct iio_chan_spec const *chan, | ||
379 | int *val, int *val2, long m) | ||
380 | { | ||
381 | struct ad5592r_state *st = iio_priv(iio_dev); | ||
382 | u16 read_val; | ||
383 | int ret; | ||
384 | |||
385 | switch (m) { | ||
386 | case IIO_CHAN_INFO_RAW: | ||
387 | mutex_lock(&iio_dev->mlock); | ||
388 | |||
389 | if (!chan->output) { | ||
390 | ret = st->ops->read_adc(st, chan->channel, &read_val); | ||
391 | if (ret) | ||
392 | goto unlock; | ||
393 | |||
394 | if ((read_val >> 12 & 0x7) != (chan->channel & 0x7)) { | ||
395 | dev_err(st->dev, "Error while reading channel %u\n", | ||
396 | chan->channel); | ||
397 | ret = -EIO; | ||
398 | goto unlock; | ||
399 | } | ||
400 | |||
401 | read_val &= GENMASK(11, 0); | ||
402 | |||
403 | } else { | ||
404 | read_val = st->cached_dac[chan->channel]; | ||
405 | } | ||
406 | |||
407 | dev_dbg(st->dev, "Channel %u read: 0x%04hX\n", | ||
408 | chan->channel, read_val); | ||
409 | |||
410 | *val = (int) read_val; | ||
411 | ret = IIO_VAL_INT; | ||
412 | break; | ||
413 | case IIO_CHAN_INFO_SCALE: | ||
414 | *val = ad5592r_get_vref(st); | ||
415 | |||
416 | if (chan->type == IIO_TEMP) { | ||
417 | s64 tmp = *val * (3767897513LL / 25LL); | ||
418 | *val = div_s64_rem(tmp, 1000000000LL, val2); | ||
419 | |||
420 | ret = IIO_VAL_INT_PLUS_MICRO; | ||
421 | } else { | ||
422 | int mult; | ||
423 | |||
424 | mutex_lock(&iio_dev->mlock); | ||
425 | |||
426 | if (chan->output) | ||
427 | mult = !!(st->cached_gp_ctrl & | ||
428 | AD5592R_REG_CTRL_DAC_RANGE); | ||
429 | else | ||
430 | mult = !!(st->cached_gp_ctrl & | ||
431 | AD5592R_REG_CTRL_ADC_RANGE); | ||
432 | |||
433 | *val *= ++mult; | ||
434 | |||
435 | *val2 = chan->scan_type.realbits; | ||
436 | ret = IIO_VAL_FRACTIONAL_LOG2; | ||
437 | } | ||
438 | break; | ||
439 | case IIO_CHAN_INFO_OFFSET: | ||
440 | ret = ad5592r_get_vref(st); | ||
441 | |||
442 | mutex_lock(&iio_dev->mlock); | ||
443 | |||
444 | if (st->cached_gp_ctrl & AD5592R_REG_CTRL_ADC_RANGE) | ||
445 | *val = (-34365 * 25) / ret; | ||
446 | else | ||
447 | *val = (-75365 * 25) / ret; | ||
448 | ret = IIO_VAL_INT; | ||
449 | break; | ||
450 | default: | ||
451 | ret = -EINVAL; | ||
452 | } | ||
453 | |||
454 | unlock: | ||
455 | mutex_unlock(&iio_dev->mlock); | ||
456 | return ret; | ||
457 | } | ||
458 | |||
459 | static int ad5592r_write_raw_get_fmt(struct iio_dev *indio_dev, | ||
460 | struct iio_chan_spec const *chan, long mask) | ||
461 | { | ||
462 | switch (mask) { | ||
463 | case IIO_CHAN_INFO_SCALE: | ||
464 | return IIO_VAL_INT_PLUS_NANO; | ||
465 | |||
466 | default: | ||
467 | return IIO_VAL_INT_PLUS_MICRO; | ||
468 | } | ||
469 | |||
470 | return -EINVAL; | ||
471 | } | ||
472 | |||
473 | static const struct iio_info ad5592r_info = { | ||
474 | .read_raw = ad5592r_read_raw, | ||
475 | .write_raw = ad5592r_write_raw, | ||
476 | .write_raw_get_fmt = ad5592r_write_raw_get_fmt, | ||
477 | .driver_module = THIS_MODULE, | ||
478 | }; | ||
479 | |||
480 | static ssize_t ad5592r_show_scale_available(struct iio_dev *iio_dev, | ||
481 | uintptr_t private, | ||
482 | const struct iio_chan_spec *chan, | ||
483 | char *buf) | ||
484 | { | ||
485 | struct ad5592r_state *st = iio_priv(iio_dev); | ||
486 | |||
487 | return sprintf(buf, "%d.%09u %d.%09u\n", | ||
488 | st->scale_avail[0][0], st->scale_avail[0][1], | ||
489 | st->scale_avail[1][0], st->scale_avail[1][1]); | ||
490 | } | ||
491 | |||
492 | static struct iio_chan_spec_ext_info ad5592r_ext_info[] = { | ||
493 | { | ||
494 | .name = "scale_available", | ||
495 | .read = ad5592r_show_scale_available, | ||
496 | .shared = true, | ||
497 | }, | ||
498 | {}, | ||
499 | }; | ||
500 | |||
501 | static void ad5592r_setup_channel(struct iio_dev *iio_dev, | ||
502 | struct iio_chan_spec *chan, bool output, unsigned id) | ||
503 | { | ||
504 | chan->type = IIO_VOLTAGE; | ||
505 | chan->indexed = 1; | ||
506 | chan->output = output; | ||
507 | chan->channel = id; | ||
508 | chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW); | ||
509 | chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); | ||
510 | chan->scan_type.sign = 'u'; | ||
511 | chan->scan_type.realbits = 12; | ||
512 | chan->scan_type.storagebits = 16; | ||
513 | chan->ext_info = ad5592r_ext_info; | ||
514 | } | ||
515 | |||
516 | static int ad5592r_alloc_channels(struct ad5592r_state *st) | ||
517 | { | ||
518 | unsigned i, curr_channel = 0, | ||
519 | num_channels = st->num_channels; | ||
520 | struct iio_dev *iio_dev = iio_priv_to_dev(st); | ||
521 | struct iio_chan_spec *channels; | ||
522 | struct fwnode_handle *child; | ||
523 | u32 reg, tmp; | ||
524 | int ret; | ||
525 | |||
526 | device_for_each_child_node(st->dev, child) { | ||
527 | ret = fwnode_property_read_u32(child, "reg", ®); | ||
528 | if (ret || reg > ARRAY_SIZE(st->channel_modes)) | ||
529 | continue; | ||
530 | |||
531 | ret = fwnode_property_read_u32(child, "adi,mode", &tmp); | ||
532 | if (!ret) | ||
533 | st->channel_modes[reg] = tmp; | ||
534 | |||
535 | fwnode_property_read_u32(child, "adi,off-state", &tmp); | ||
536 | if (!ret) | ||
537 | st->channel_offstate[reg] = tmp; | ||
538 | } | ||
539 | |||
540 | channels = devm_kzalloc(st->dev, | ||
541 | (1 + 2 * num_channels) * sizeof(*channels), GFP_KERNEL); | ||
542 | if (!channels) | ||
543 | return -ENOMEM; | ||
544 | |||
545 | for (i = 0; i < num_channels; i++) { | ||
546 | switch (st->channel_modes[i]) { | ||
547 | case CH_MODE_DAC: | ||
548 | ad5592r_setup_channel(iio_dev, &channels[curr_channel], | ||
549 | true, i); | ||
550 | curr_channel++; | ||
551 | break; | ||
552 | |||
553 | case CH_MODE_ADC: | ||
554 | ad5592r_setup_channel(iio_dev, &channels[curr_channel], | ||
555 | false, i); | ||
556 | curr_channel++; | ||
557 | break; | ||
558 | |||
559 | case CH_MODE_DAC_AND_ADC: | ||
560 | ad5592r_setup_channel(iio_dev, &channels[curr_channel], | ||
561 | true, i); | ||
562 | curr_channel++; | ||
563 | ad5592r_setup_channel(iio_dev, &channels[curr_channel], | ||
564 | false, i); | ||
565 | curr_channel++; | ||
566 | break; | ||
567 | |||
568 | default: | ||
569 | continue; | ||
570 | } | ||
571 | } | ||
572 | |||
573 | channels[curr_channel].type = IIO_TEMP; | ||
574 | channels[curr_channel].channel = 8; | ||
575 | channels[curr_channel].info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | ||
576 | BIT(IIO_CHAN_INFO_SCALE) | | ||
577 | BIT(IIO_CHAN_INFO_OFFSET); | ||
578 | curr_channel++; | ||
579 | |||
580 | iio_dev->num_channels = curr_channel; | ||
581 | iio_dev->channels = channels; | ||
582 | |||
583 | return 0; | ||
584 | } | ||
585 | |||
586 | static void ad5592r_init_scales(struct ad5592r_state *st, int vref_mV) | ||
587 | { | ||
588 | s64 tmp = (s64)vref_mV * 1000000000LL >> 12; | ||
589 | |||
590 | st->scale_avail[0][0] = | ||
591 | div_s64_rem(tmp, 1000000000LL, &st->scale_avail[0][1]); | ||
592 | st->scale_avail[1][0] = | ||
593 | div_s64_rem(tmp * 2, 1000000000LL, &st->scale_avail[1][1]); | ||
594 | } | ||
595 | |||
596 | int ad5592r_probe(struct device *dev, const char *name, | ||
597 | const struct ad5592r_rw_ops *ops) | ||
598 | { | ||
599 | struct iio_dev *iio_dev; | ||
600 | struct ad5592r_state *st; | ||
601 | int ret; | ||
602 | |||
603 | iio_dev = devm_iio_device_alloc(dev, sizeof(*st)); | ||
604 | if (!iio_dev) | ||
605 | return -ENOMEM; | ||
606 | |||
607 | st = iio_priv(iio_dev); | ||
608 | st->dev = dev; | ||
609 | st->ops = ops; | ||
610 | st->num_channels = 8; | ||
611 | dev_set_drvdata(dev, iio_dev); | ||
612 | |||
613 | st->reg = devm_regulator_get_optional(dev, "vref"); | ||
614 | if (IS_ERR(st->reg)) { | ||
615 | if ((PTR_ERR(st->reg) != -ENODEV) && dev->of_node) | ||
616 | return PTR_ERR(st->reg); | ||
617 | |||
618 | st->reg = NULL; | ||
619 | } else { | ||
620 | ret = regulator_enable(st->reg); | ||
621 | if (ret) | ||
622 | return ret; | ||
623 | } | ||
624 | |||
625 | iio_dev->dev.parent = dev; | ||
626 | iio_dev->name = name; | ||
627 | iio_dev->info = &ad5592r_info; | ||
628 | iio_dev->modes = INDIO_DIRECT_MODE; | ||
629 | |||
630 | ad5592r_init_scales(st, ad5592r_get_vref(st)); | ||
631 | |||
632 | ret = ad5592r_reset(st); | ||
633 | if (ret) | ||
634 | goto error_disable_reg; | ||
635 | |||
636 | ret = ops->reg_write(st, AD5592R_REG_PD, | ||
637 | (st->reg == NULL) ? AD5592R_REG_PD_EN_REF : 0); | ||
638 | if (ret) | ||
639 | goto error_disable_reg; | ||
640 | |||
641 | ret = ad5592r_alloc_channels(st); | ||
642 | if (ret) | ||
643 | goto error_disable_reg; | ||
644 | |||
645 | ret = ad5592r_set_channel_modes(st); | ||
646 | if (ret) | ||
647 | goto error_reset_ch_modes; | ||
648 | |||
649 | ret = iio_device_register(iio_dev); | ||
650 | if (ret) | ||
651 | goto error_reset_ch_modes; | ||
652 | |||
653 | ret = ad5592r_gpio_init(st); | ||
654 | if (ret) | ||
655 | goto error_dev_unregister; | ||
656 | |||
657 | return 0; | ||
658 | |||
659 | error_dev_unregister: | ||
660 | iio_device_unregister(iio_dev); | ||
661 | |||
662 | error_reset_ch_modes: | ||
663 | ad5592r_reset_channel_modes(st); | ||
664 | |||
665 | error_disable_reg: | ||
666 | if (st->reg) | ||
667 | regulator_disable(st->reg); | ||
668 | |||
669 | return ret; | ||
670 | } | ||
671 | EXPORT_SYMBOL_GPL(ad5592r_probe); | ||
672 | |||
673 | int ad5592r_remove(struct device *dev) | ||
674 | { | ||
675 | struct iio_dev *iio_dev = dev_get_drvdata(dev); | ||
676 | struct ad5592r_state *st = iio_priv(iio_dev); | ||
677 | |||
678 | iio_device_unregister(iio_dev); | ||
679 | ad5592r_reset_channel_modes(st); | ||
680 | ad5592r_gpio_cleanup(st); | ||
681 | |||
682 | if (st->reg) | ||
683 | regulator_disable(st->reg); | ||
684 | |||
685 | return 0; | ||
686 | } | ||
687 | EXPORT_SYMBOL_GPL(ad5592r_remove); | ||
688 | |||
689 | MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); | ||
690 | MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); | ||
691 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/dac/ad5592r-base.h b/drivers/iio/dac/ad5592r-base.h new file mode 100644 index 000000000000..841457e93f85 --- /dev/null +++ b/drivers/iio/dac/ad5592r-base.h | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * AD5592R / AD5593R Digital <-> Analog converters driver | ||
3 | * | ||
4 | * Copyright 2015-2016 Analog Devices Inc. | ||
5 | * Author: Paul Cercueil <paul.cercueil@analog.com> | ||
6 | * | ||
7 | * Licensed under the GPL-2. | ||
8 | */ | ||
9 | |||
10 | #ifndef __DRIVERS_IIO_DAC_AD5592R_BASE_H__ | ||
11 | #define __DRIVERS_IIO_DAC_AD5592R_BASE_H__ | ||
12 | |||
13 | #include <linux/types.h> | ||
14 | #include <linux/cache.h> | ||
15 | #include <linux/mutex.h> | ||
16 | #include <linux/gpio/driver.h> | ||
17 | |||
18 | struct device; | ||
19 | struct ad5592r_state; | ||
20 | |||
21 | enum ad5592r_registers { | ||
22 | AD5592R_REG_NOOP = 0x0, | ||
23 | AD5592R_REG_DAC_READBACK = 0x1, | ||
24 | AD5592R_REG_ADC_SEQ = 0x2, | ||
25 | AD5592R_REG_CTRL = 0x3, | ||
26 | AD5592R_REG_ADC_EN = 0x4, | ||
27 | AD5592R_REG_DAC_EN = 0x5, | ||
28 | AD5592R_REG_PULLDOWN = 0x6, | ||
29 | AD5592R_REG_LDAC = 0x7, | ||
30 | AD5592R_REG_GPIO_OUT_EN = 0x8, | ||
31 | AD5592R_REG_GPIO_SET = 0x9, | ||
32 | AD5592R_REG_GPIO_IN_EN = 0xA, | ||
33 | AD5592R_REG_PD = 0xB, | ||
34 | AD5592R_REG_OPEN_DRAIN = 0xC, | ||
35 | AD5592R_REG_TRISTATE = 0xD, | ||
36 | AD5592R_REG_RESET = 0xF, | ||
37 | }; | ||
38 | |||
39 | #define AD5592R_REG_PD_EN_REF BIT(9) | ||
40 | #define AD5592R_REG_CTRL_ADC_RANGE BIT(5) | ||
41 | #define AD5592R_REG_CTRL_DAC_RANGE BIT(4) | ||
42 | |||
43 | struct ad5592r_rw_ops { | ||
44 | int (*write_dac)(struct ad5592r_state *st, unsigned chan, u16 value); | ||
45 | int (*read_adc)(struct ad5592r_state *st, unsigned chan, u16 *value); | ||
46 | int (*reg_write)(struct ad5592r_state *st, u8 reg, u16 value); | ||
47 | int (*reg_read)(struct ad5592r_state *st, u8 reg, u16 *value); | ||
48 | int (*gpio_read)(struct ad5592r_state *st, u8 *value); | ||
49 | }; | ||
50 | |||
51 | struct ad5592r_state { | ||
52 | struct device *dev; | ||
53 | struct regulator *reg; | ||
54 | struct gpio_chip gpiochip; | ||
55 | struct mutex gpio_lock; /* Protect cached gpio_out, gpio_val, etc. */ | ||
56 | unsigned int num_channels; | ||
57 | const struct ad5592r_rw_ops *ops; | ||
58 | int scale_avail[2][2]; | ||
59 | u16 cached_dac[8]; | ||
60 | u16 cached_gp_ctrl; | ||
61 | u8 channel_modes[8]; | ||
62 | u8 channel_offstate[8]; | ||
63 | u8 gpio_map; | ||
64 | u8 gpio_out; | ||
65 | u8 gpio_in; | ||
66 | u8 gpio_val; | ||
67 | |||
68 | __be16 spi_msg ____cacheline_aligned; | ||
69 | __be16 spi_msg_nop; | ||
70 | }; | ||
71 | |||
72 | int ad5592r_probe(struct device *dev, const char *name, | ||
73 | const struct ad5592r_rw_ops *ops); | ||
74 | int ad5592r_remove(struct device *dev); | ||
75 | |||
76 | #endif /* __DRIVERS_IIO_DAC_AD5592R_BASE_H__ */ | ||
diff --git a/drivers/iio/dac/ad5592r.c b/drivers/iio/dac/ad5592r.c new file mode 100644 index 000000000000..0b235a2c7359 --- /dev/null +++ b/drivers/iio/dac/ad5592r.c | |||
@@ -0,0 +1,164 @@ | |||
1 | /* | ||
2 | * AD5592R Digital <-> Analog converters driver | ||
3 | * | ||
4 | * Copyright 2015-2016 Analog Devices Inc. | ||
5 | * Author: Paul Cercueil <paul.cercueil@analog.com> | ||
6 | * | ||
7 | * Licensed under the GPL-2. | ||
8 | */ | ||
9 | |||
10 | #include "ad5592r-base.h" | ||
11 | |||
12 | #include <linux/bitops.h> | ||
13 | #include <linux/module.h> | ||
14 | #include <linux/of.h> | ||
15 | #include <linux/spi/spi.h> | ||
16 | |||
17 | #define AD5592R_GPIO_READBACK_EN BIT(10) | ||
18 | #define AD5592R_LDAC_READBACK_EN BIT(6) | ||
19 | |||
20 | static int ad5592r_spi_wnop_r16(struct ad5592r_state *st, u16 *buf) | ||
21 | { | ||
22 | struct spi_device *spi = container_of(st->dev, struct spi_device, dev); | ||
23 | struct spi_transfer t = { | ||
24 | .tx_buf = &st->spi_msg_nop, | ||
25 | .rx_buf = buf, | ||
26 | .len = 2 | ||
27 | }; | ||
28 | |||
29 | st->spi_msg_nop = 0; /* NOP */ | ||
30 | |||
31 | return spi_sync_transfer(spi, &t, 1); | ||
32 | } | ||
33 | |||
34 | static int ad5592r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value) | ||
35 | { | ||
36 | struct spi_device *spi = container_of(st->dev, struct spi_device, dev); | ||
37 | |||
38 | st->spi_msg = cpu_to_be16(BIT(15) | (chan << 12) | value); | ||
39 | |||
40 | return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); | ||
41 | } | ||
42 | |||
43 | static int ad5592r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value) | ||
44 | { | ||
45 | struct spi_device *spi = container_of(st->dev, struct spi_device, dev); | ||
46 | int ret; | ||
47 | |||
48 | st->spi_msg = cpu_to_be16((AD5592R_REG_ADC_SEQ << 11) | BIT(chan)); | ||
49 | |||
50 | ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); | ||
51 | if (ret) | ||
52 | return ret; | ||
53 | |||
54 | /* | ||
55 | * Invalid data: | ||
56 | * See Figure 40. Single-Channel ADC Conversion Sequence | ||
57 | */ | ||
58 | ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); | ||
59 | if (ret) | ||
60 | return ret; | ||
61 | |||
62 | ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); | ||
63 | if (ret) | ||
64 | return ret; | ||
65 | |||
66 | *value = be16_to_cpu(st->spi_msg); | ||
67 | |||
68 | return 0; | ||
69 | } | ||
70 | |||
71 | static int ad5592r_reg_write(struct ad5592r_state *st, u8 reg, u16 value) | ||
72 | { | ||
73 | struct spi_device *spi = container_of(st->dev, struct spi_device, dev); | ||
74 | |||
75 | st->spi_msg = cpu_to_be16((reg << 11) | value); | ||
76 | |||
77 | return spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); | ||
78 | } | ||
79 | |||
80 | static int ad5592r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value) | ||
81 | { | ||
82 | struct spi_device *spi = container_of(st->dev, struct spi_device, dev); | ||
83 | int ret; | ||
84 | |||
85 | st->spi_msg = cpu_to_be16((AD5592R_REG_LDAC << 11) | | ||
86 | AD5592R_LDAC_READBACK_EN | (reg << 2)); | ||
87 | |||
88 | ret = spi_write(spi, &st->spi_msg, sizeof(st->spi_msg)); | ||
89 | if (ret) | ||
90 | return ret; | ||
91 | |||
92 | ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); | ||
93 | if (ret) | ||
94 | return ret; | ||
95 | |||
96 | *value = be16_to_cpu(st->spi_msg); | ||
97 | |||
98 | return 0; | ||
99 | } | ||
100 | |||
101 | static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value) | ||
102 | { | ||
103 | int ret; | ||
104 | |||
105 | ret = ad5592r_reg_write(st, AD5592R_REG_GPIO_IN_EN, | ||
106 | AD5592R_GPIO_READBACK_EN | st->gpio_in); | ||
107 | if (ret) | ||
108 | return ret; | ||
109 | |||
110 | ret = ad5592r_spi_wnop_r16(st, &st->spi_msg); | ||
111 | if (ret) | ||
112 | return ret; | ||
113 | |||
114 | *value = (u8) be16_to_cpu(st->spi_msg); | ||
115 | |||
116 | return 0; | ||
117 | } | ||
118 | |||
119 | static const struct ad5592r_rw_ops ad5592r_rw_ops = { | ||
120 | .write_dac = ad5592r_write_dac, | ||
121 | .read_adc = ad5592r_read_adc, | ||
122 | .reg_write = ad5592r_reg_write, | ||
123 | .reg_read = ad5592r_reg_read, | ||
124 | .gpio_read = ad5593r_gpio_read, | ||
125 | }; | ||
126 | |||
127 | static int ad5592r_spi_probe(struct spi_device *spi) | ||
128 | { | ||
129 | const struct spi_device_id *id = spi_get_device_id(spi); | ||
130 | |||
131 | return ad5592r_probe(&spi->dev, id->name, &ad5592r_rw_ops); | ||
132 | } | ||
133 | |||
134 | static int ad5592r_spi_remove(struct spi_device *spi) | ||
135 | { | ||
136 | return ad5592r_remove(&spi->dev); | ||
137 | } | ||
138 | |||
139 | static const struct spi_device_id ad5592r_spi_ids[] = { | ||
140 | { .name = "ad5592r", }, | ||
141 | {} | ||
142 | }; | ||
143 | MODULE_DEVICE_TABLE(spi, ad5592r_spi_ids); | ||
144 | |||
145 | static const struct of_device_id ad5592r_of_match[] = { | ||
146 | { .compatible = "adi,ad5592r", }, | ||
147 | {}, | ||
148 | }; | ||
149 | MODULE_DEVICE_TABLE(of, ad5592r_of_match); | ||
150 | |||
151 | static struct spi_driver ad5592r_spi_driver = { | ||
152 | .driver = { | ||
153 | .name = "ad5592r", | ||
154 | .of_match_table = of_match_ptr(ad5592r_of_match), | ||
155 | }, | ||
156 | .probe = ad5592r_spi_probe, | ||
157 | .remove = ad5592r_spi_remove, | ||
158 | .id_table = ad5592r_spi_ids, | ||
159 | }; | ||
160 | module_spi_driver(ad5592r_spi_driver); | ||
161 | |||
162 | MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); | ||
163 | MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); | ||
164 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/dac/ad5593r.c b/drivers/iio/dac/ad5593r.c new file mode 100644 index 000000000000..dca158a88f47 --- /dev/null +++ b/drivers/iio/dac/ad5593r.c | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | * AD5593R Digital <-> Analog converters driver | ||
3 | * | ||
4 | * Copyright 2015-2016 Analog Devices Inc. | ||
5 | * Author: Paul Cercueil <paul.cercueil@analog.com> | ||
6 | * | ||
7 | * Licensed under the GPL-2. | ||
8 | */ | ||
9 | |||
10 | #include "ad5592r-base.h" | ||
11 | |||
12 | #include <linux/bitops.h> | ||
13 | #include <linux/i2c.h> | ||
14 | #include <linux/module.h> | ||
15 | #include <linux/of.h> | ||
16 | |||
17 | #define AD5593R_MODE_CONF (0 << 4) | ||
18 | #define AD5593R_MODE_DAC_WRITE (1 << 4) | ||
19 | #define AD5593R_MODE_ADC_READBACK (4 << 4) | ||
20 | #define AD5593R_MODE_DAC_READBACK (5 << 4) | ||
21 | #define AD5593R_MODE_GPIO_READBACK (6 << 4) | ||
22 | #define AD5593R_MODE_REG_READBACK (7 << 4) | ||
23 | |||
24 | static int ad5593r_write_dac(struct ad5592r_state *st, unsigned chan, u16 value) | ||
25 | { | ||
26 | struct i2c_client *i2c = to_i2c_client(st->dev); | ||
27 | |||
28 | return i2c_smbus_write_word_swapped(i2c, | ||
29 | AD5593R_MODE_DAC_WRITE | chan, value); | ||
30 | } | ||
31 | |||
32 | static int ad5593r_read_adc(struct ad5592r_state *st, unsigned chan, u16 *value) | ||
33 | { | ||
34 | struct i2c_client *i2c = to_i2c_client(st->dev); | ||
35 | s32 val; | ||
36 | |||
37 | val = i2c_smbus_write_word_swapped(i2c, | ||
38 | AD5593R_MODE_CONF | AD5592R_REG_ADC_SEQ, BIT(chan)); | ||
39 | if (val < 0) | ||
40 | return (int) val; | ||
41 | |||
42 | val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_ADC_READBACK); | ||
43 | if (val < 0) | ||
44 | return (int) val; | ||
45 | |||
46 | *value = (u16) val; | ||
47 | |||
48 | return 0; | ||
49 | } | ||
50 | |||
51 | static int ad5593r_reg_write(struct ad5592r_state *st, u8 reg, u16 value) | ||
52 | { | ||
53 | struct i2c_client *i2c = to_i2c_client(st->dev); | ||
54 | |||
55 | return i2c_smbus_write_word_swapped(i2c, | ||
56 | AD5593R_MODE_CONF | reg, value); | ||
57 | } | ||
58 | |||
59 | static int ad5593r_reg_read(struct ad5592r_state *st, u8 reg, u16 *value) | ||
60 | { | ||
61 | struct i2c_client *i2c = to_i2c_client(st->dev); | ||
62 | s32 val; | ||
63 | |||
64 | val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_REG_READBACK | reg); | ||
65 | if (val < 0) | ||
66 | return (int) val; | ||
67 | |||
68 | *value = (u16) val; | ||
69 | |||
70 | return 0; | ||
71 | } | ||
72 | |||
73 | static int ad5593r_gpio_read(struct ad5592r_state *st, u8 *value) | ||
74 | { | ||
75 | struct i2c_client *i2c = to_i2c_client(st->dev); | ||
76 | s32 val; | ||
77 | |||
78 | val = i2c_smbus_read_word_swapped(i2c, AD5593R_MODE_GPIO_READBACK); | ||
79 | if (val < 0) | ||
80 | return (int) val; | ||
81 | |||
82 | *value = (u8) val; | ||
83 | |||
84 | return 0; | ||
85 | } | ||
86 | |||
87 | static const struct ad5592r_rw_ops ad5593r_rw_ops = { | ||
88 | .write_dac = ad5593r_write_dac, | ||
89 | .read_adc = ad5593r_read_adc, | ||
90 | .reg_write = ad5593r_reg_write, | ||
91 | .reg_read = ad5593r_reg_read, | ||
92 | .gpio_read = ad5593r_gpio_read, | ||
93 | }; | ||
94 | |||
95 | static int ad5593r_i2c_probe(struct i2c_client *i2c, | ||
96 | const struct i2c_device_id *id) | ||
97 | { | ||
98 | return ad5592r_probe(&i2c->dev, id->name, &ad5593r_rw_ops); | ||
99 | } | ||
100 | |||
101 | static int ad5593r_i2c_remove(struct i2c_client *i2c) | ||
102 | { | ||
103 | return ad5592r_remove(&i2c->dev); | ||
104 | } | ||
105 | |||
106 | static const struct i2c_device_id ad5593r_i2c_ids[] = { | ||
107 | { .name = "ad5593r", }, | ||
108 | {}, | ||
109 | }; | ||
110 | MODULE_DEVICE_TABLE(i2c, ad5593r_i2c_ids); | ||
111 | |||
112 | static const struct of_device_id ad5593r_of_match[] = { | ||
113 | { .compatible = "adi,ad5593r", }, | ||
114 | {}, | ||
115 | }; | ||
116 | MODULE_DEVICE_TABLE(of, ad5593r_of_match); | ||
117 | |||
118 | static struct i2c_driver ad5593r_driver = { | ||
119 | .driver = { | ||
120 | .name = "ad5593r", | ||
121 | .of_match_table = of_match_ptr(ad5593r_of_match), | ||
122 | }, | ||
123 | .probe = ad5593r_i2c_probe, | ||
124 | .remove = ad5593r_i2c_remove, | ||
125 | .id_table = ad5593r_i2c_ids, | ||
126 | }; | ||
127 | module_i2c_driver(ad5593r_driver); | ||
128 | |||
129 | MODULE_AUTHOR("Paul Cercueil <paul.cercueil@analog.com>"); | ||
130 | MODULE_DESCRIPTION("Analog Devices AD5592R multi-channel converters"); | ||
131 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c index 44a30f286de1..99eba524f6dd 100644 --- a/drivers/iio/frequency/ad9523.c +++ b/drivers/iio/frequency/ad9523.c | |||
@@ -284,7 +284,7 @@ struct ad9523_state { | |||
284 | } data[2] ____cacheline_aligned; | 284 | } data[2] ____cacheline_aligned; |
285 | }; | 285 | }; |
286 | 286 | ||
287 | static int ad9523_read(struct iio_dev *indio_dev, unsigned addr) | 287 | static int ad9523_read(struct iio_dev *indio_dev, unsigned int addr) |
288 | { | 288 | { |
289 | struct ad9523_state *st = iio_priv(indio_dev); | 289 | struct ad9523_state *st = iio_priv(indio_dev); |
290 | int ret; | 290 | int ret; |
@@ -318,7 +318,8 @@ static int ad9523_read(struct iio_dev *indio_dev, unsigned addr) | |||
318 | return ret; | 318 | return ret; |
319 | }; | 319 | }; |
320 | 320 | ||
321 | static int ad9523_write(struct iio_dev *indio_dev, unsigned addr, unsigned val) | 321 | static int ad9523_write(struct iio_dev *indio_dev, |
322 | unsigned int addr, unsigned int val) | ||
322 | { | 323 | { |
323 | struct ad9523_state *st = iio_priv(indio_dev); | 324 | struct ad9523_state *st = iio_priv(indio_dev); |
324 | int ret; | 325 | int ret; |
@@ -351,11 +352,11 @@ static int ad9523_io_update(struct iio_dev *indio_dev) | |||
351 | } | 352 | } |
352 | 353 | ||
353 | static int ad9523_vco_out_map(struct iio_dev *indio_dev, | 354 | static int ad9523_vco_out_map(struct iio_dev *indio_dev, |
354 | unsigned ch, unsigned out) | 355 | unsigned int ch, unsigned int out) |
355 | { | 356 | { |
356 | struct ad9523_state *st = iio_priv(indio_dev); | 357 | struct ad9523_state *st = iio_priv(indio_dev); |
357 | int ret; | 358 | int ret; |
358 | unsigned mask; | 359 | unsigned int mask; |
359 | 360 | ||
360 | switch (ch) { | 361 | switch (ch) { |
361 | case 0 ... 3: | 362 | case 0 ... 3: |
@@ -405,7 +406,7 @@ static int ad9523_vco_out_map(struct iio_dev *indio_dev, | |||
405 | } | 406 | } |
406 | 407 | ||
407 | static int ad9523_set_clock_provider(struct iio_dev *indio_dev, | 408 | static int ad9523_set_clock_provider(struct iio_dev *indio_dev, |
408 | unsigned ch, unsigned long freq) | 409 | unsigned int ch, unsigned long freq) |
409 | { | 410 | { |
410 | struct ad9523_state *st = iio_priv(indio_dev); | 411 | struct ad9523_state *st = iio_priv(indio_dev); |
411 | long tmp1, tmp2; | 412 | long tmp1, tmp2; |
@@ -619,7 +620,7 @@ static int ad9523_read_raw(struct iio_dev *indio_dev, | |||
619 | long m) | 620 | long m) |
620 | { | 621 | { |
621 | struct ad9523_state *st = iio_priv(indio_dev); | 622 | struct ad9523_state *st = iio_priv(indio_dev); |
622 | unsigned code; | 623 | unsigned int code; |
623 | int ret; | 624 | int ret; |
624 | 625 | ||
625 | mutex_lock(&indio_dev->mlock); | 626 | mutex_lock(&indio_dev->mlock); |
@@ -655,7 +656,7 @@ static int ad9523_write_raw(struct iio_dev *indio_dev, | |||
655 | long mask) | 656 | long mask) |
656 | { | 657 | { |
657 | struct ad9523_state *st = iio_priv(indio_dev); | 658 | struct ad9523_state *st = iio_priv(indio_dev); |
658 | unsigned reg; | 659 | unsigned int reg; |
659 | int ret, tmp, code; | 660 | int ret, tmp, code; |
660 | 661 | ||
661 | mutex_lock(&indio_dev->mlock); | 662 | mutex_lock(&indio_dev->mlock); |
@@ -709,8 +710,8 @@ out: | |||
709 | } | 710 | } |
710 | 711 | ||
711 | static int ad9523_reg_access(struct iio_dev *indio_dev, | 712 | static int ad9523_reg_access(struct iio_dev *indio_dev, |
712 | unsigned reg, unsigned writeval, | 713 | unsigned int reg, unsigned int writeval, |
713 | unsigned *readval) | 714 | unsigned int *readval) |
714 | { | 715 | { |
715 | int ret; | 716 | int ret; |
716 | 717 | ||
diff --git a/drivers/iio/gyro/Kconfig b/drivers/iio/gyro/Kconfig index e816d29d6a62..205a84420ae9 100644 --- a/drivers/iio/gyro/Kconfig +++ b/drivers/iio/gyro/Kconfig | |||
@@ -93,7 +93,7 @@ config IIO_ST_GYRO_3AXIS | |||
93 | select IIO_TRIGGERED_BUFFER if (IIO_BUFFER) | 93 | select IIO_TRIGGERED_BUFFER if (IIO_BUFFER) |
94 | help | 94 | help |
95 | Say yes here to build support for STMicroelectronics gyroscopes: | 95 | Say yes here to build support for STMicroelectronics gyroscopes: |
96 | L3G4200D, LSM330DL, L3GD20, LSM330DLC, L3G4IS, LSM330. | 96 | L3G4200D, LSM330DL, L3GD20, LSM330DLC, L3G4IS, LSM330, LSM9DS0. |
97 | 97 | ||
98 | This driver can also be built as a module. If so, these modules | 98 | This driver can also be built as a module. If so, these modules |
99 | will be created: | 99 | will be created: |
diff --git a/drivers/iio/gyro/bmg160_core.c b/drivers/iio/gyro/bmg160_core.c index dd2f850a2f0b..7ccc044063f6 100644 --- a/drivers/iio/gyro/bmg160_core.c +++ b/drivers/iio/gyro/bmg160_core.c | |||
@@ -95,7 +95,6 @@ | |||
95 | #define BMG160_AUTO_SUSPEND_DELAY_MS 2000 | 95 | #define BMG160_AUTO_SUSPEND_DELAY_MS 2000 |
96 | 96 | ||
97 | struct bmg160_data { | 97 | struct bmg160_data { |
98 | struct device *dev; | ||
99 | struct regmap *regmap; | 98 | struct regmap *regmap; |
100 | struct iio_trigger *dready_trig; | 99 | struct iio_trigger *dready_trig; |
101 | struct iio_trigger *motion_trig; | 100 | struct iio_trigger *motion_trig; |
@@ -137,11 +136,12 @@ static const struct { | |||
137 | 136 | ||
138 | static int bmg160_set_mode(struct bmg160_data *data, u8 mode) | 137 | static int bmg160_set_mode(struct bmg160_data *data, u8 mode) |
139 | { | 138 | { |
139 | struct device *dev = regmap_get_device(data->regmap); | ||
140 | int ret; | 140 | int ret; |
141 | 141 | ||
142 | ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode); | 142 | ret = regmap_write(data->regmap, BMG160_REG_PMU_LPW, mode); |
143 | if (ret < 0) { | 143 | if (ret < 0) { |
144 | dev_err(data->dev, "Error writing reg_pmu_lpw\n"); | 144 | dev_err(dev, "Error writing reg_pmu_lpw\n"); |
145 | return ret; | 145 | return ret; |
146 | } | 146 | } |
147 | 147 | ||
@@ -162,6 +162,7 @@ static int bmg160_convert_freq_to_bit(int val) | |||
162 | 162 | ||
163 | static int bmg160_set_bw(struct bmg160_data *data, int val) | 163 | static int bmg160_set_bw(struct bmg160_data *data, int val) |
164 | { | 164 | { |
165 | struct device *dev = regmap_get_device(data->regmap); | ||
165 | int ret; | 166 | int ret; |
166 | int bw_bits; | 167 | int bw_bits; |
167 | 168 | ||
@@ -171,7 +172,7 @@ static int bmg160_set_bw(struct bmg160_data *data, int val) | |||
171 | 172 | ||
172 | ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits); | 173 | ret = regmap_write(data->regmap, BMG160_REG_PMU_BW, bw_bits); |
173 | if (ret < 0) { | 174 | if (ret < 0) { |
174 | dev_err(data->dev, "Error writing reg_pmu_bw\n"); | 175 | dev_err(dev, "Error writing reg_pmu_bw\n"); |
175 | return ret; | 176 | return ret; |
176 | } | 177 | } |
177 | 178 | ||
@@ -182,18 +183,19 @@ static int bmg160_set_bw(struct bmg160_data *data, int val) | |||
182 | 183 | ||
183 | static int bmg160_chip_init(struct bmg160_data *data) | 184 | static int bmg160_chip_init(struct bmg160_data *data) |
184 | { | 185 | { |
186 | struct device *dev = regmap_get_device(data->regmap); | ||
185 | int ret; | 187 | int ret; |
186 | unsigned int val; | 188 | unsigned int val; |
187 | 189 | ||
188 | ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val); | 190 | ret = regmap_read(data->regmap, BMG160_REG_CHIP_ID, &val); |
189 | if (ret < 0) { | 191 | if (ret < 0) { |
190 | dev_err(data->dev, "Error reading reg_chip_id\n"); | 192 | dev_err(dev, "Error reading reg_chip_id\n"); |
191 | return ret; | 193 | return ret; |
192 | } | 194 | } |
193 | 195 | ||
194 | dev_dbg(data->dev, "Chip Id %x\n", val); | 196 | dev_dbg(dev, "Chip Id %x\n", val); |
195 | if (val != BMG160_CHIP_ID_VAL) { | 197 | if (val != BMG160_CHIP_ID_VAL) { |
196 | dev_err(data->dev, "invalid chip %x\n", val); | 198 | dev_err(dev, "invalid chip %x\n", val); |
197 | return -ENODEV; | 199 | return -ENODEV; |
198 | } | 200 | } |
199 | 201 | ||
@@ -212,14 +214,14 @@ static int bmg160_chip_init(struct bmg160_data *data) | |||
212 | /* Set Default Range */ | 214 | /* Set Default Range */ |
213 | ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS); | 215 | ret = regmap_write(data->regmap, BMG160_REG_RANGE, BMG160_RANGE_500DPS); |
214 | if (ret < 0) { | 216 | if (ret < 0) { |
215 | dev_err(data->dev, "Error writing reg_range\n"); | 217 | dev_err(dev, "Error writing reg_range\n"); |
216 | return ret; | 218 | return ret; |
217 | } | 219 | } |
218 | data->dps_range = BMG160_RANGE_500DPS; | 220 | data->dps_range = BMG160_RANGE_500DPS; |
219 | 221 | ||
220 | ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val); | 222 | ret = regmap_read(data->regmap, BMG160_REG_SLOPE_THRES, &val); |
221 | if (ret < 0) { | 223 | if (ret < 0) { |
222 | dev_err(data->dev, "Error reading reg_slope_thres\n"); | 224 | dev_err(dev, "Error reading reg_slope_thres\n"); |
223 | return ret; | 225 | return ret; |
224 | } | 226 | } |
225 | data->slope_thres = val; | 227 | data->slope_thres = val; |
@@ -228,7 +230,7 @@ static int bmg160_chip_init(struct bmg160_data *data) | |||
228 | ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1, | 230 | ret = regmap_update_bits(data->regmap, BMG160_REG_INT_EN_1, |
229 | BMG160_INT1_BIT_OD, 0); | 231 | BMG160_INT1_BIT_OD, 0); |
230 | if (ret < 0) { | 232 | if (ret < 0) { |
231 | dev_err(data->dev, "Error updating bits in reg_int_en_1\n"); | 233 | dev_err(dev, "Error updating bits in reg_int_en_1\n"); |
232 | return ret; | 234 | return ret; |
233 | } | 235 | } |
234 | 236 | ||
@@ -236,7 +238,7 @@ static int bmg160_chip_init(struct bmg160_data *data) | |||
236 | BMG160_INT_MODE_LATCH_INT | | 238 | BMG160_INT_MODE_LATCH_INT | |
237 | BMG160_INT_MODE_LATCH_RESET); | 239 | BMG160_INT_MODE_LATCH_RESET); |
238 | if (ret < 0) { | 240 | if (ret < 0) { |
239 | dev_err(data->dev, | 241 | dev_err(dev, |
240 | "Error writing reg_motion_intr\n"); | 242 | "Error writing reg_motion_intr\n"); |
241 | return ret; | 243 | return ret; |
242 | } | 244 | } |
@@ -247,20 +249,21 @@ static int bmg160_chip_init(struct bmg160_data *data) | |||
247 | static int bmg160_set_power_state(struct bmg160_data *data, bool on) | 249 | static int bmg160_set_power_state(struct bmg160_data *data, bool on) |
248 | { | 250 | { |
249 | #ifdef CONFIG_PM | 251 | #ifdef CONFIG_PM |
252 | struct device *dev = regmap_get_device(data->regmap); | ||
250 | int ret; | 253 | int ret; |
251 | 254 | ||
252 | if (on) | 255 | if (on) |
253 | ret = pm_runtime_get_sync(data->dev); | 256 | ret = pm_runtime_get_sync(dev); |
254 | else { | 257 | else { |
255 | pm_runtime_mark_last_busy(data->dev); | 258 | pm_runtime_mark_last_busy(dev); |
256 | ret = pm_runtime_put_autosuspend(data->dev); | 259 | ret = pm_runtime_put_autosuspend(dev); |
257 | } | 260 | } |
258 | 261 | ||
259 | if (ret < 0) { | 262 | if (ret < 0) { |
260 | dev_err(data->dev, | 263 | dev_err(dev, "Failed: bmg160_set_power_state for %d\n", on); |
261 | "Failed: bmg160_set_power_state for %d\n", on); | 264 | |
262 | if (on) | 265 | if (on) |
263 | pm_runtime_put_noidle(data->dev); | 266 | pm_runtime_put_noidle(dev); |
264 | 267 | ||
265 | return ret; | 268 | return ret; |
266 | } | 269 | } |
@@ -272,6 +275,7 @@ static int bmg160_set_power_state(struct bmg160_data *data, bool on) | |||
272 | static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | 275 | static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, |
273 | bool status) | 276 | bool status) |
274 | { | 277 | { |
278 | struct device *dev = regmap_get_device(data->regmap); | ||
275 | int ret; | 279 | int ret; |
276 | 280 | ||
277 | /* Enable/Disable INT_MAP0 mapping */ | 281 | /* Enable/Disable INT_MAP0 mapping */ |
@@ -279,7 +283,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
279 | BMG160_INT_MAP_0_BIT_ANY, | 283 | BMG160_INT_MAP_0_BIT_ANY, |
280 | (status ? BMG160_INT_MAP_0_BIT_ANY : 0)); | 284 | (status ? BMG160_INT_MAP_0_BIT_ANY : 0)); |
281 | if (ret < 0) { | 285 | if (ret < 0) { |
282 | dev_err(data->dev, "Error updating bits reg_int_map0\n"); | 286 | dev_err(dev, "Error updating bits reg_int_map0\n"); |
283 | return ret; | 287 | return ret; |
284 | } | 288 | } |
285 | 289 | ||
@@ -289,8 +293,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
289 | ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES, | 293 | ret = regmap_write(data->regmap, BMG160_REG_SLOPE_THRES, |
290 | data->slope_thres); | 294 | data->slope_thres); |
291 | if (ret < 0) { | 295 | if (ret < 0) { |
292 | dev_err(data->dev, | 296 | dev_err(dev, "Error writing reg_slope_thres\n"); |
293 | "Error writing reg_slope_thres\n"); | ||
294 | return ret; | 297 | return ret; |
295 | } | 298 | } |
296 | 299 | ||
@@ -298,8 +301,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
298 | BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y | | 301 | BMG160_INT_MOTION_X | BMG160_INT_MOTION_Y | |
299 | BMG160_INT_MOTION_Z); | 302 | BMG160_INT_MOTION_Z); |
300 | if (ret < 0) { | 303 | if (ret < 0) { |
301 | dev_err(data->dev, | 304 | dev_err(dev, "Error writing reg_motion_intr\n"); |
302 | "Error writing reg_motion_intr\n"); | ||
303 | return ret; | 305 | return ret; |
304 | } | 306 | } |
305 | 307 | ||
@@ -314,8 +316,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
314 | BMG160_INT_MODE_LATCH_INT | | 316 | BMG160_INT_MODE_LATCH_INT | |
315 | BMG160_INT_MODE_LATCH_RESET); | 317 | BMG160_INT_MODE_LATCH_RESET); |
316 | if (ret < 0) { | 318 | if (ret < 0) { |
317 | dev_err(data->dev, | 319 | dev_err(dev, "Error writing reg_rst_latch\n"); |
318 | "Error writing reg_rst_latch\n"); | ||
319 | return ret; | 320 | return ret; |
320 | } | 321 | } |
321 | } | 322 | } |
@@ -328,7 +329,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
328 | } | 329 | } |
329 | 330 | ||
330 | if (ret < 0) { | 331 | if (ret < 0) { |
331 | dev_err(data->dev, "Error writing reg_int_en0\n"); | 332 | dev_err(dev, "Error writing reg_int_en0\n"); |
332 | return ret; | 333 | return ret; |
333 | } | 334 | } |
334 | 335 | ||
@@ -338,6 +339,7 @@ static int bmg160_setup_any_motion_interrupt(struct bmg160_data *data, | |||
338 | static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, | 339 | static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, |
339 | bool status) | 340 | bool status) |
340 | { | 341 | { |
342 | struct device *dev = regmap_get_device(data->regmap); | ||
341 | int ret; | 343 | int ret; |
342 | 344 | ||
343 | /* Enable/Disable INT_MAP1 mapping */ | 345 | /* Enable/Disable INT_MAP1 mapping */ |
@@ -345,7 +347,7 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, | |||
345 | BMG160_INT_MAP_1_BIT_NEW_DATA, | 347 | BMG160_INT_MAP_1_BIT_NEW_DATA, |
346 | (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0)); | 348 | (status ? BMG160_INT_MAP_1_BIT_NEW_DATA : 0)); |
347 | if (ret < 0) { | 349 | if (ret < 0) { |
348 | dev_err(data->dev, "Error updating bits in reg_int_map1\n"); | 350 | dev_err(dev, "Error updating bits in reg_int_map1\n"); |
349 | return ret; | 351 | return ret; |
350 | } | 352 | } |
351 | 353 | ||
@@ -354,9 +356,8 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, | |||
354 | BMG160_INT_MODE_NON_LATCH_INT | | 356 | BMG160_INT_MODE_NON_LATCH_INT | |
355 | BMG160_INT_MODE_LATCH_RESET); | 357 | BMG160_INT_MODE_LATCH_RESET); |
356 | if (ret < 0) { | 358 | if (ret < 0) { |
357 | dev_err(data->dev, | 359 | dev_err(dev, "Error writing reg_rst_latch\n"); |
358 | "Error writing reg_rst_latch\n"); | 360 | return ret; |
359 | return ret; | ||
360 | } | 361 | } |
361 | 362 | ||
362 | ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, | 363 | ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, |
@@ -368,16 +369,15 @@ static int bmg160_setup_new_data_interrupt(struct bmg160_data *data, | |||
368 | BMG160_INT_MODE_LATCH_INT | | 369 | BMG160_INT_MODE_LATCH_INT | |
369 | BMG160_INT_MODE_LATCH_RESET); | 370 | BMG160_INT_MODE_LATCH_RESET); |
370 | if (ret < 0) { | 371 | if (ret < 0) { |
371 | dev_err(data->dev, | 372 | dev_err(dev, "Error writing reg_rst_latch\n"); |
372 | "Error writing reg_rst_latch\n"); | 373 | return ret; |
373 | return ret; | ||
374 | } | 374 | } |
375 | 375 | ||
376 | ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0); | 376 | ret = regmap_write(data->regmap, BMG160_REG_INT_EN_0, 0); |
377 | } | 377 | } |
378 | 378 | ||
379 | if (ret < 0) { | 379 | if (ret < 0) { |
380 | dev_err(data->dev, "Error writing reg_int_en0\n"); | 380 | dev_err(dev, "Error writing reg_int_en0\n"); |
381 | return ret; | 381 | return ret; |
382 | } | 382 | } |
383 | 383 | ||
@@ -400,6 +400,7 @@ static int bmg160_get_bw(struct bmg160_data *data, int *val) | |||
400 | 400 | ||
401 | static int bmg160_set_scale(struct bmg160_data *data, int val) | 401 | static int bmg160_set_scale(struct bmg160_data *data, int val) |
402 | { | 402 | { |
403 | struct device *dev = regmap_get_device(data->regmap); | ||
403 | int ret, i; | 404 | int ret, i; |
404 | 405 | ||
405 | for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) { | 406 | for (i = 0; i < ARRAY_SIZE(bmg160_scale_table); ++i) { |
@@ -407,8 +408,7 @@ static int bmg160_set_scale(struct bmg160_data *data, int val) | |||
407 | ret = regmap_write(data->regmap, BMG160_REG_RANGE, | 408 | ret = regmap_write(data->regmap, BMG160_REG_RANGE, |
408 | bmg160_scale_table[i].dps_range); | 409 | bmg160_scale_table[i].dps_range); |
409 | if (ret < 0) { | 410 | if (ret < 0) { |
410 | dev_err(data->dev, | 411 | dev_err(dev, "Error writing reg_range\n"); |
411 | "Error writing reg_range\n"); | ||
412 | return ret; | 412 | return ret; |
413 | } | 413 | } |
414 | data->dps_range = bmg160_scale_table[i].dps_range; | 414 | data->dps_range = bmg160_scale_table[i].dps_range; |
@@ -421,6 +421,7 @@ static int bmg160_set_scale(struct bmg160_data *data, int val) | |||
421 | 421 | ||
422 | static int bmg160_get_temp(struct bmg160_data *data, int *val) | 422 | static int bmg160_get_temp(struct bmg160_data *data, int *val) |
423 | { | 423 | { |
424 | struct device *dev = regmap_get_device(data->regmap); | ||
424 | int ret; | 425 | int ret; |
425 | unsigned int raw_val; | 426 | unsigned int raw_val; |
426 | 427 | ||
@@ -433,7 +434,7 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val) | |||
433 | 434 | ||
434 | ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val); | 435 | ret = regmap_read(data->regmap, BMG160_REG_TEMP, &raw_val); |
435 | if (ret < 0) { | 436 | if (ret < 0) { |
436 | dev_err(data->dev, "Error reading reg_temp\n"); | 437 | dev_err(dev, "Error reading reg_temp\n"); |
437 | bmg160_set_power_state(data, false); | 438 | bmg160_set_power_state(data, false); |
438 | mutex_unlock(&data->mutex); | 439 | mutex_unlock(&data->mutex); |
439 | return ret; | 440 | return ret; |
@@ -450,6 +451,7 @@ static int bmg160_get_temp(struct bmg160_data *data, int *val) | |||
450 | 451 | ||
451 | static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val) | 452 | static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val) |
452 | { | 453 | { |
454 | struct device *dev = regmap_get_device(data->regmap); | ||
453 | int ret; | 455 | int ret; |
454 | __le16 raw_val; | 456 | __le16 raw_val; |
455 | 457 | ||
@@ -463,7 +465,7 @@ static int bmg160_get_axis(struct bmg160_data *data, int axis, int *val) | |||
463 | ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val, | 465 | ret = regmap_bulk_read(data->regmap, BMG160_AXIS_TO_REG(axis), &raw_val, |
464 | sizeof(raw_val)); | 466 | sizeof(raw_val)); |
465 | if (ret < 0) { | 467 | if (ret < 0) { |
466 | dev_err(data->dev, "Error reading axis %d\n", axis); | 468 | dev_err(dev, "Error reading axis %d\n", axis); |
467 | bmg160_set_power_state(data, false); | 469 | bmg160_set_power_state(data, false); |
468 | mutex_unlock(&data->mutex); | 470 | mutex_unlock(&data->mutex); |
469 | return ret; | 471 | return ret; |
@@ -793,6 +795,7 @@ static int bmg160_trig_try_reen(struct iio_trigger *trig) | |||
793 | { | 795 | { |
794 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); | 796 | struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); |
795 | struct bmg160_data *data = iio_priv(indio_dev); | 797 | struct bmg160_data *data = iio_priv(indio_dev); |
798 | struct device *dev = regmap_get_device(data->regmap); | ||
796 | int ret; | 799 | int ret; |
797 | 800 | ||
798 | /* new data interrupts don't need ack */ | 801 | /* new data interrupts don't need ack */ |
@@ -804,7 +807,7 @@ static int bmg160_trig_try_reen(struct iio_trigger *trig) | |||
804 | BMG160_INT_MODE_LATCH_INT | | 807 | BMG160_INT_MODE_LATCH_INT | |
805 | BMG160_INT_MODE_LATCH_RESET); | 808 | BMG160_INT_MODE_LATCH_RESET); |
806 | if (ret < 0) { | 809 | if (ret < 0) { |
807 | dev_err(data->dev, "Error writing reg_rst_latch\n"); | 810 | dev_err(dev, "Error writing reg_rst_latch\n"); |
808 | return ret; | 811 | return ret; |
809 | } | 812 | } |
810 | 813 | ||
@@ -864,13 +867,14 @@ static irqreturn_t bmg160_event_handler(int irq, void *private) | |||
864 | { | 867 | { |
865 | struct iio_dev *indio_dev = private; | 868 | struct iio_dev *indio_dev = private; |
866 | struct bmg160_data *data = iio_priv(indio_dev); | 869 | struct bmg160_data *data = iio_priv(indio_dev); |
870 | struct device *dev = regmap_get_device(data->regmap); | ||
867 | int ret; | 871 | int ret; |
868 | int dir; | 872 | int dir; |
869 | unsigned int val; | 873 | unsigned int val; |
870 | 874 | ||
871 | ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val); | 875 | ret = regmap_read(data->regmap, BMG160_REG_INT_STATUS_2, &val); |
872 | if (ret < 0) { | 876 | if (ret < 0) { |
873 | dev_err(data->dev, "Error reading reg_int_status2\n"); | 877 | dev_err(dev, "Error reading reg_int_status2\n"); |
874 | goto ack_intr_status; | 878 | goto ack_intr_status; |
875 | } | 879 | } |
876 | 880 | ||
@@ -907,8 +911,7 @@ ack_intr_status: | |||
907 | BMG160_INT_MODE_LATCH_INT | | 911 | BMG160_INT_MODE_LATCH_INT | |
908 | BMG160_INT_MODE_LATCH_RESET); | 912 | BMG160_INT_MODE_LATCH_RESET); |
909 | if (ret < 0) | 913 | if (ret < 0) |
910 | dev_err(data->dev, | 914 | dev_err(dev, "Error writing reg_rst_latch\n"); |
911 | "Error writing reg_rst_latch\n"); | ||
912 | } | 915 | } |
913 | 916 | ||
914 | return IRQ_HANDLED; | 917 | return IRQ_HANDLED; |
@@ -976,7 +979,6 @@ int bmg160_core_probe(struct device *dev, struct regmap *regmap, int irq, | |||
976 | 979 | ||
977 | data = iio_priv(indio_dev); | 980 | data = iio_priv(indio_dev); |
978 | dev_set_drvdata(dev, indio_dev); | 981 | dev_set_drvdata(dev, indio_dev); |
979 | data->dev = dev; | ||
980 | data->irq = irq; | 982 | data->irq = irq; |
981 | data->regmap = regmap; | 983 | data->regmap = regmap; |
982 | 984 | ||
@@ -1139,7 +1141,7 @@ static int bmg160_runtime_suspend(struct device *dev) | |||
1139 | 1141 | ||
1140 | ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND); | 1142 | ret = bmg160_set_mode(data, BMG160_MODE_SUSPEND); |
1141 | if (ret < 0) { | 1143 | if (ret < 0) { |
1142 | dev_err(data->dev, "set mode failed\n"); | 1144 | dev_err(dev, "set mode failed\n"); |
1143 | return -EAGAIN; | 1145 | return -EAGAIN; |
1144 | } | 1146 | } |
1145 | 1147 | ||
diff --git a/drivers/iio/gyro/st_gyro.h b/drivers/iio/gyro/st_gyro.h index 5353d6328c54..a5c5c4e29add 100644 --- a/drivers/iio/gyro/st_gyro.h +++ b/drivers/iio/gyro/st_gyro.h | |||
@@ -21,6 +21,7 @@ | |||
21 | #define L3GD20_GYRO_DEV_NAME "l3gd20" | 21 | #define L3GD20_GYRO_DEV_NAME "l3gd20" |
22 | #define L3G4IS_GYRO_DEV_NAME "l3g4is_ui" | 22 | #define L3G4IS_GYRO_DEV_NAME "l3g4is_ui" |
23 | #define LSM330_GYRO_DEV_NAME "lsm330_gyro" | 23 | #define LSM330_GYRO_DEV_NAME "lsm330_gyro" |
24 | #define LSM9DS0_GYRO_DEV_NAME "lsm9ds0_gyro" | ||
24 | 25 | ||
25 | /** | 26 | /** |
26 | * struct st_sensors_platform_data - gyro platform data | 27 | * struct st_sensors_platform_data - gyro platform data |
diff --git a/drivers/iio/gyro/st_gyro_core.c b/drivers/iio/gyro/st_gyro_core.c index 110f95b6e52f..52a3c87c375c 100644 --- a/drivers/iio/gyro/st_gyro_core.c +++ b/drivers/iio/gyro/st_gyro_core.c | |||
@@ -190,6 +190,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = { | |||
190 | * drain settings, but only for INT1 and not | 190 | * drain settings, but only for INT1 and not |
191 | * for the DRDY line on INT2. | 191 | * for the DRDY line on INT2. |
192 | */ | 192 | */ |
193 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
193 | }, | 194 | }, |
194 | .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT, | 195 | .multi_read_bit = ST_GYRO_1_MULTIREAD_BIT, |
195 | .bootime = 2, | 196 | .bootime = 2, |
@@ -203,6 +204,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = { | |||
203 | [2] = LSM330DLC_GYRO_DEV_NAME, | 204 | [2] = LSM330DLC_GYRO_DEV_NAME, |
204 | [3] = L3G4IS_GYRO_DEV_NAME, | 205 | [3] = L3G4IS_GYRO_DEV_NAME, |
205 | [4] = LSM330_GYRO_DEV_NAME, | 206 | [4] = LSM330_GYRO_DEV_NAME, |
207 | [5] = LSM9DS0_GYRO_DEV_NAME, | ||
206 | }, | 208 | }, |
207 | .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, | 209 | .ch = (struct iio_chan_spec *)st_gyro_16bit_channels, |
208 | .odr = { | 210 | .odr = { |
@@ -258,6 +260,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = { | |||
258 | * drain settings, but only for INT1 and not | 260 | * drain settings, but only for INT1 and not |
259 | * for the DRDY line on INT2. | 261 | * for the DRDY line on INT2. |
260 | */ | 262 | */ |
263 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
261 | }, | 264 | }, |
262 | .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, | 265 | .multi_read_bit = ST_GYRO_2_MULTIREAD_BIT, |
263 | .bootime = 2, | 266 | .bootime = 2, |
@@ -322,6 +325,7 @@ static const struct st_sensor_settings st_gyro_sensors_settings[] = { | |||
322 | * drain settings, but only for INT1 and not | 325 | * drain settings, but only for INT1 and not |
323 | * for the DRDY line on INT2. | 326 | * for the DRDY line on INT2. |
324 | */ | 327 | */ |
328 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
325 | }, | 329 | }, |
326 | .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT, | 330 | .multi_read_bit = ST_GYRO_3_MULTIREAD_BIT, |
327 | .bootime = 2, | 331 | .bootime = 2, |
diff --git a/drivers/iio/gyro/st_gyro_i2c.c b/drivers/iio/gyro/st_gyro_i2c.c index 6848451f817a..40056b821036 100644 --- a/drivers/iio/gyro/st_gyro_i2c.c +++ b/drivers/iio/gyro/st_gyro_i2c.c | |||
@@ -48,6 +48,10 @@ static const struct of_device_id st_gyro_of_match[] = { | |||
48 | .compatible = "st,lsm330-gyro", | 48 | .compatible = "st,lsm330-gyro", |
49 | .data = LSM330_GYRO_DEV_NAME, | 49 | .data = LSM330_GYRO_DEV_NAME, |
50 | }, | 50 | }, |
51 | { | ||
52 | .compatible = "st,lsm9ds0-gyro", | ||
53 | .data = LSM9DS0_GYRO_DEV_NAME, | ||
54 | }, | ||
51 | {}, | 55 | {}, |
52 | }; | 56 | }; |
53 | MODULE_DEVICE_TABLE(of, st_gyro_of_match); | 57 | MODULE_DEVICE_TABLE(of, st_gyro_of_match); |
@@ -93,6 +97,7 @@ static const struct i2c_device_id st_gyro_id_table[] = { | |||
93 | { L3GD20_GYRO_DEV_NAME }, | 97 | { L3GD20_GYRO_DEV_NAME }, |
94 | { L3G4IS_GYRO_DEV_NAME }, | 98 | { L3G4IS_GYRO_DEV_NAME }, |
95 | { LSM330_GYRO_DEV_NAME }, | 99 | { LSM330_GYRO_DEV_NAME }, |
100 | { LSM9DS0_GYRO_DEV_NAME }, | ||
96 | {}, | 101 | {}, |
97 | }; | 102 | }; |
98 | MODULE_DEVICE_TABLE(i2c, st_gyro_id_table); | 103 | MODULE_DEVICE_TABLE(i2c, st_gyro_id_table); |
diff --git a/drivers/iio/gyro/st_gyro_spi.c b/drivers/iio/gyro/st_gyro_spi.c index d2b7a5fa344c..fbf2faed501c 100644 --- a/drivers/iio/gyro/st_gyro_spi.c +++ b/drivers/iio/gyro/st_gyro_spi.c | |||
@@ -54,6 +54,7 @@ static const struct spi_device_id st_gyro_id_table[] = { | |||
54 | { L3GD20_GYRO_DEV_NAME }, | 54 | { L3GD20_GYRO_DEV_NAME }, |
55 | { L3G4IS_GYRO_DEV_NAME }, | 55 | { L3G4IS_GYRO_DEV_NAME }, |
56 | { LSM330_GYRO_DEV_NAME }, | 56 | { LSM330_GYRO_DEV_NAME }, |
57 | { LSM9DS0_GYRO_DEV_NAME }, | ||
57 | {}, | 58 | {}, |
58 | }; | 59 | }; |
59 | MODULE_DEVICE_TABLE(spi, st_gyro_id_table); | 60 | MODULE_DEVICE_TABLE(spi, st_gyro_id_table); |
diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig index 866dda133336..738a86d9e4a9 100644 --- a/drivers/iio/humidity/Kconfig +++ b/drivers/iio/humidity/Kconfig | |||
@@ -3,6 +3,16 @@ | |||
3 | # | 3 | # |
4 | menu "Humidity sensors" | 4 | menu "Humidity sensors" |
5 | 5 | ||
6 | config AM2315 | ||
7 | tristate "Aosong AM2315 relative humidity and temperature sensor" | ||
8 | depends on I2C | ||
9 | help | ||
10 | If you say yes here you get support for the Aosong AM2315 | ||
11 | relative humidity and ambient temperature sensor. | ||
12 | |||
13 | This driver can also be built as a module. If so, the module will | ||
14 | be called am2315. | ||
15 | |||
6 | config DHT11 | 16 | config DHT11 |
7 | tristate "DHT11 (and compatible sensors) driver" | 17 | tristate "DHT11 (and compatible sensors) driver" |
8 | depends on GPIOLIB || COMPILE_TEST | 18 | depends on GPIOLIB || COMPILE_TEST |
diff --git a/drivers/iio/humidity/Makefile b/drivers/iio/humidity/Makefile index c9f089a9a6b8..4a73442fcd9c 100644 --- a/drivers/iio/humidity/Makefile +++ b/drivers/iio/humidity/Makefile | |||
@@ -2,6 +2,7 @@ | |||
2 | # Makefile for IIO humidity sensor drivers | 2 | # Makefile for IIO humidity sensor drivers |
3 | # | 3 | # |
4 | 4 | ||
5 | obj-$(CONFIG_AM2315) += am2315.o | ||
5 | obj-$(CONFIG_DHT11) += dht11.o | 6 | obj-$(CONFIG_DHT11) += dht11.o |
6 | obj-$(CONFIG_HDC100X) += hdc100x.o | 7 | obj-$(CONFIG_HDC100X) += hdc100x.o |
7 | obj-$(CONFIG_HTU21) += htu21.o | 8 | obj-$(CONFIG_HTU21) += htu21.o |
diff --git a/drivers/iio/humidity/am2315.c b/drivers/iio/humidity/am2315.c new file mode 100644 index 000000000000..3be6d209a159 --- /dev/null +++ b/drivers/iio/humidity/am2315.c | |||
@@ -0,0 +1,303 @@ | |||
1 | /** | ||
2 | * Aosong AM2315 relative humidity and temperature | ||
3 | * | ||
4 | * Copyright (c) 2016, Intel Corporation. | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | * | ||
10 | * 7-bit I2C address: 0x5C. | ||
11 | */ | ||
12 | |||
13 | #include <linux/acpi.h> | ||
14 | #include <linux/delay.h> | ||
15 | #include <linux/i2c.h> | ||
16 | #include <linux/kernel.h> | ||
17 | #include <linux/module.h> | ||
18 | #include <linux/iio/buffer.h> | ||
19 | #include <linux/iio/iio.h> | ||
20 | #include <linux/iio/sysfs.h> | ||
21 | #include <linux/iio/trigger_consumer.h> | ||
22 | #include <linux/iio/triggered_buffer.h> | ||
23 | |||
24 | #define AM2315_REG_HUM_MSB 0x00 | ||
25 | #define AM2315_REG_HUM_LSB 0x01 | ||
26 | #define AM2315_REG_TEMP_MSB 0x02 | ||
27 | #define AM2315_REG_TEMP_LSB 0x03 | ||
28 | |||
29 | #define AM2315_FUNCTION_READ 0x03 | ||
30 | #define AM2315_HUM_OFFSET 2 | ||
31 | #define AM2315_TEMP_OFFSET 4 | ||
32 | #define AM2315_ALL_CHANNEL_MASK GENMASK(1, 0) | ||
33 | |||
34 | #define AM2315_DRIVER_NAME "am2315" | ||
35 | |||
36 | struct am2315_data { | ||
37 | struct i2c_client *client; | ||
38 | struct mutex lock; | ||
39 | s16 buffer[8]; /* 2x16-bit channels + 2x16 padding + 4x16 timestamp */ | ||
40 | }; | ||
41 | |||
42 | struct am2315_sensor_data { | ||
43 | s16 hum_data; | ||
44 | s16 temp_data; | ||
45 | }; | ||
46 | |||
47 | static const struct iio_chan_spec am2315_channels[] = { | ||
48 | { | ||
49 | .type = IIO_HUMIDITYRELATIVE, | ||
50 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | ||
51 | BIT(IIO_CHAN_INFO_SCALE), | ||
52 | .scan_index = 0, | ||
53 | .scan_type = { | ||
54 | .sign = 's', | ||
55 | .realbits = 16, | ||
56 | .storagebits = 16, | ||
57 | .endianness = IIO_CPU, | ||
58 | }, | ||
59 | }, | ||
60 | { | ||
61 | .type = IIO_TEMP, | ||
62 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | ||
63 | BIT(IIO_CHAN_INFO_SCALE), | ||
64 | .scan_index = 1, | ||
65 | .scan_type = { | ||
66 | .sign = 's', | ||
67 | .realbits = 16, | ||
68 | .storagebits = 16, | ||
69 | .endianness = IIO_CPU, | ||
70 | }, | ||
71 | }, | ||
72 | IIO_CHAN_SOFT_TIMESTAMP(2), | ||
73 | }; | ||
74 | |||
75 | /* CRC calculation algorithm, as specified in the datasheet (page 13). */ | ||
76 | static u16 am2315_crc(u8 *data, u8 nr_bytes) | ||
77 | { | ||
78 | int i; | ||
79 | u16 crc = 0xffff; | ||
80 | |||
81 | while (nr_bytes--) { | ||
82 | crc ^= *data++; | ||
83 | for (i = 0; i < 8; i++) { | ||
84 | if (crc & 0x01) { | ||
85 | crc >>= 1; | ||
86 | crc ^= 0xA001; | ||
87 | } else { | ||
88 | crc >>= 1; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | return crc; | ||
94 | } | ||
95 | |||
96 | /* Simple function that sends a few bytes to the device to wake it up. */ | ||
97 | static void am2315_ping(struct i2c_client *client) | ||
98 | { | ||
99 | i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB); | ||
100 | } | ||
101 | |||
102 | static int am2315_read_data(struct am2315_data *data, | ||
103 | struct am2315_sensor_data *sensor_data) | ||
104 | { | ||
105 | int ret; | ||
106 | /* tx_buf format: <function code> <start addr> <nr of regs to read> */ | ||
107 | u8 tx_buf[3] = { AM2315_FUNCTION_READ, AM2315_REG_HUM_MSB, 4 }; | ||
108 | /* | ||
109 | * rx_buf format: | ||
110 | * <function code> <number of registers read> | ||
111 | * <humidity MSB> <humidity LSB> <temp MSB> <temp LSB> | ||
112 | * <CRC LSB> <CRC MSB> | ||
113 | */ | ||
114 | u8 rx_buf[8]; | ||
115 | u16 crc; | ||
116 | |||
117 | /* First wake up the device. */ | ||
118 | am2315_ping(data->client); | ||
119 | |||
120 | mutex_lock(&data->lock); | ||
121 | ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf)); | ||
122 | if (ret < 0) { | ||
123 | dev_err(&data->client->dev, "failed to send read request\n"); | ||
124 | goto exit_unlock; | ||
125 | } | ||
126 | /* Wait 2-3 ms, then read back the data sent by the device. */ | ||
127 | usleep_range(2000, 3000); | ||
128 | /* Do a bulk data read, then pick out what we need. */ | ||
129 | ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf)); | ||
130 | if (ret < 0) { | ||
131 | dev_err(&data->client->dev, "failed to read sensor data\n"); | ||
132 | goto exit_unlock; | ||
133 | } | ||
134 | mutex_unlock(&data->lock); | ||
135 | /* | ||
136 | * Do a CRC check on the data and compare it to the value | ||
137 | * calculated by the device. | ||
138 | */ | ||
139 | crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2); | ||
140 | if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) { | ||
141 | dev_err(&data->client->dev, "failed to verify sensor data\n"); | ||
142 | return -EIO; | ||
143 | } | ||
144 | |||
145 | sensor_data->hum_data = (rx_buf[AM2315_HUM_OFFSET] << 8) | | ||
146 | rx_buf[AM2315_HUM_OFFSET + 1]; | ||
147 | sensor_data->temp_data = (rx_buf[AM2315_TEMP_OFFSET] << 8) | | ||
148 | rx_buf[AM2315_TEMP_OFFSET + 1]; | ||
149 | |||
150 | return ret; | ||
151 | |||
152 | exit_unlock: | ||
153 | mutex_unlock(&data->lock); | ||
154 | return ret; | ||
155 | } | ||
156 | |||
157 | static irqreturn_t am2315_trigger_handler(int irq, void *p) | ||
158 | { | ||
159 | int i; | ||
160 | int ret; | ||
161 | int bit; | ||
162 | struct iio_poll_func *pf = p; | ||
163 | struct iio_dev *indio_dev = pf->indio_dev; | ||
164 | struct am2315_data *data = iio_priv(indio_dev); | ||
165 | struct am2315_sensor_data sensor_data; | ||
166 | |||
167 | ret = am2315_read_data(data, &sensor_data); | ||
168 | if (ret < 0) { | ||
169 | mutex_unlock(&data->lock); | ||
170 | goto err; | ||
171 | } | ||
172 | |||
173 | mutex_lock(&data->lock); | ||
174 | if (*(indio_dev->active_scan_mask) == AM2315_ALL_CHANNEL_MASK) { | ||
175 | data->buffer[0] = sensor_data.hum_data; | ||
176 | data->buffer[1] = sensor_data.temp_data; | ||
177 | } else { | ||
178 | i = 0; | ||
179 | for_each_set_bit(bit, indio_dev->active_scan_mask, | ||
180 | indio_dev->masklength) { | ||
181 | data->buffer[i] = (bit ? sensor_data.temp_data : | ||
182 | sensor_data.hum_data); | ||
183 | i++; | ||
184 | } | ||
185 | } | ||
186 | mutex_unlock(&data->lock); | ||
187 | |||
188 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, | ||
189 | pf->timestamp); | ||
190 | err: | ||
191 | iio_trigger_notify_done(indio_dev->trig); | ||
192 | return IRQ_HANDLED; | ||
193 | } | ||
194 | |||
195 | static int am2315_read_raw(struct iio_dev *indio_dev, | ||
196 | struct iio_chan_spec const *chan, | ||
197 | int *val, int *val2, long mask) | ||
198 | { | ||
199 | int ret; | ||
200 | struct am2315_sensor_data sensor_data; | ||
201 | struct am2315_data *data = iio_priv(indio_dev); | ||
202 | |||
203 | switch (mask) { | ||
204 | case IIO_CHAN_INFO_RAW: | ||
205 | ret = am2315_read_data(data, &sensor_data); | ||
206 | if (ret < 0) | ||
207 | return ret; | ||
208 | *val = (chan->type == IIO_HUMIDITYRELATIVE) ? | ||
209 | sensor_data.hum_data : sensor_data.temp_data; | ||
210 | return IIO_VAL_INT; | ||
211 | case IIO_CHAN_INFO_SCALE: | ||
212 | *val = 100; | ||
213 | return IIO_VAL_INT; | ||
214 | } | ||
215 | |||
216 | return -EINVAL; | ||
217 | } | ||
218 | |||
219 | static const struct iio_info am2315_info = { | ||
220 | .driver_module = THIS_MODULE, | ||
221 | .read_raw = am2315_read_raw, | ||
222 | }; | ||
223 | |||
224 | static int am2315_probe(struct i2c_client *client, | ||
225 | const struct i2c_device_id *id) | ||
226 | { | ||
227 | int ret; | ||
228 | struct iio_dev *indio_dev; | ||
229 | struct am2315_data *data; | ||
230 | |||
231 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | ||
232 | if (!indio_dev) { | ||
233 | dev_err(&client->dev, "iio allocation failed!\n"); | ||
234 | return -ENOMEM; | ||
235 | } | ||
236 | |||
237 | data = iio_priv(indio_dev); | ||
238 | data->client = client; | ||
239 | i2c_set_clientdata(client, indio_dev); | ||
240 | mutex_init(&data->lock); | ||
241 | |||
242 | indio_dev->dev.parent = &client->dev; | ||
243 | indio_dev->info = &am2315_info; | ||
244 | indio_dev->name = AM2315_DRIVER_NAME; | ||
245 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
246 | indio_dev->channels = am2315_channels; | ||
247 | indio_dev->num_channels = ARRAY_SIZE(am2315_channels); | ||
248 | |||
249 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | ||
250 | am2315_trigger_handler, NULL); | ||
251 | if (ret < 0) { | ||
252 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); | ||
253 | return ret; | ||
254 | } | ||
255 | |||
256 | ret = iio_device_register(indio_dev); | ||
257 | if (ret < 0) | ||
258 | goto err_buffer_cleanup; | ||
259 | |||
260 | return 0; | ||
261 | |||
262 | err_buffer_cleanup: | ||
263 | iio_triggered_buffer_cleanup(indio_dev); | ||
264 | return ret; | ||
265 | } | ||
266 | |||
267 | static int am2315_remove(struct i2c_client *client) | ||
268 | { | ||
269 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
270 | |||
271 | iio_device_unregister(indio_dev); | ||
272 | iio_triggered_buffer_cleanup(indio_dev); | ||
273 | |||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | static const struct i2c_device_id am2315_i2c_id[] = { | ||
278 | {"am2315", 0}, | ||
279 | {} | ||
280 | }; | ||
281 | |||
282 | static const struct acpi_device_id am2315_acpi_id[] = { | ||
283 | {"AOS2315", 0}, | ||
284 | {} | ||
285 | }; | ||
286 | |||
287 | MODULE_DEVICE_TABLE(acpi, am2315_acpi_id); | ||
288 | |||
289 | static struct i2c_driver am2315_driver = { | ||
290 | .driver = { | ||
291 | .name = "am2315", | ||
292 | .acpi_match_table = ACPI_PTR(am2315_acpi_id), | ||
293 | }, | ||
294 | .probe = am2315_probe, | ||
295 | .remove = am2315_remove, | ||
296 | .id_table = am2315_i2c_id, | ||
297 | }; | ||
298 | |||
299 | module_i2c_driver(am2315_driver); | ||
300 | |||
301 | MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>"); | ||
302 | MODULE_DESCRIPTION("Aosong AM2315 relative humidity and temperature"); | ||
303 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/humidity/dht11.c b/drivers/iio/humidity/dht11.c index 20b500da94db..9c47bc98f3ac 100644 --- a/drivers/iio/humidity/dht11.c +++ b/drivers/iio/humidity/dht11.c | |||
@@ -96,6 +96,24 @@ struct dht11 { | |||
96 | struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ]; | 96 | struct {s64 ts; int value; } edges[DHT11_EDGES_PER_READ]; |
97 | }; | 97 | }; |
98 | 98 | ||
99 | #ifdef CONFIG_DYNAMIC_DEBUG | ||
100 | /* | ||
101 | * dht11_edges_print: show the data as actually received by the | ||
102 | * driver. | ||
103 | */ | ||
104 | static void dht11_edges_print(struct dht11 *dht11) | ||
105 | { | ||
106 | int i; | ||
107 | |||
108 | dev_dbg(dht11->dev, "%d edges detected:\n", dht11->num_edges); | ||
109 | for (i = 1; i < dht11->num_edges; ++i) { | ||
110 | dev_dbg(dht11->dev, "%d: %lld ns %s\n", i, | ||
111 | dht11->edges[i].ts - dht11->edges[i - 1].ts, | ||
112 | dht11->edges[i - 1].value ? "high" : "low"); | ||
113 | } | ||
114 | } | ||
115 | #endif /* CONFIG_DYNAMIC_DEBUG */ | ||
116 | |||
99 | static unsigned char dht11_decode_byte(char *bits) | 117 | static unsigned char dht11_decode_byte(char *bits) |
100 | { | 118 | { |
101 | unsigned char ret = 0; | 119 | unsigned char ret = 0; |
@@ -119,8 +137,12 @@ static int dht11_decode(struct dht11 *dht11, int offset) | |||
119 | for (i = 0; i < DHT11_BITS_PER_READ; ++i) { | 137 | for (i = 0; i < DHT11_BITS_PER_READ; ++i) { |
120 | t = dht11->edges[offset + 2 * i + 2].ts - | 138 | t = dht11->edges[offset + 2 * i + 2].ts - |
121 | dht11->edges[offset + 2 * i + 1].ts; | 139 | dht11->edges[offset + 2 * i + 1].ts; |
122 | if (!dht11->edges[offset + 2 * i + 1].value) | 140 | if (!dht11->edges[offset + 2 * i + 1].value) { |
123 | return -EIO; /* lost synchronisation */ | 141 | dev_dbg(dht11->dev, |
142 | "lost synchronisation at edge %d\n", | ||
143 | offset + 2 * i + 1); | ||
144 | return -EIO; | ||
145 | } | ||
124 | bits[i] = t > DHT11_THRESHOLD; | 146 | bits[i] = t > DHT11_THRESHOLD; |
125 | } | 147 | } |
126 | 148 | ||
@@ -130,8 +152,10 @@ static int dht11_decode(struct dht11 *dht11, int offset) | |||
130 | temp_dec = dht11_decode_byte(&bits[24]); | 152 | temp_dec = dht11_decode_byte(&bits[24]); |
131 | checksum = dht11_decode_byte(&bits[32]); | 153 | checksum = dht11_decode_byte(&bits[32]); |
132 | 154 | ||
133 | if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) | 155 | if (((hum_int + hum_dec + temp_int + temp_dec) & 0xff) != checksum) { |
156 | dev_dbg(dht11->dev, "invalid checksum\n"); | ||
134 | return -EIO; | 157 | return -EIO; |
158 | } | ||
135 | 159 | ||
136 | dht11->timestamp = ktime_get_boot_ns(); | 160 | dht11->timestamp = ktime_get_boot_ns(); |
137 | if (hum_int < 20) { /* DHT22 */ | 161 | if (hum_int < 20) { /* DHT22 */ |
@@ -182,6 +206,7 @@ static int dht11_read_raw(struct iio_dev *iio_dev, | |||
182 | mutex_lock(&dht11->lock); | 206 | mutex_lock(&dht11->lock); |
183 | if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) { | 207 | if (dht11->timestamp + DHT11_DATA_VALID_TIME < ktime_get_boot_ns()) { |
184 | timeres = ktime_get_resolution_ns(); | 208 | timeres = ktime_get_resolution_ns(); |
209 | dev_dbg(dht11->dev, "current timeresolution: %dns\n", timeres); | ||
185 | if (timeres > DHT11_MIN_TIMERES) { | 210 | if (timeres > DHT11_MIN_TIMERES) { |
186 | dev_err(dht11->dev, "timeresolution %dns too low\n", | 211 | dev_err(dht11->dev, "timeresolution %dns too low\n", |
187 | timeres); | 212 | timeres); |
@@ -219,10 +244,13 @@ static int dht11_read_raw(struct iio_dev *iio_dev, | |||
219 | 244 | ||
220 | free_irq(dht11->irq, iio_dev); | 245 | free_irq(dht11->irq, iio_dev); |
221 | 246 | ||
247 | #ifdef CONFIG_DYNAMIC_DEBUG | ||
248 | dht11_edges_print(dht11); | ||
249 | #endif | ||
250 | |||
222 | if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) { | 251 | if (ret == 0 && dht11->num_edges < DHT11_EDGES_PER_READ - 1) { |
223 | dev_err(&iio_dev->dev, | 252 | dev_err(dht11->dev, "Only %d signal edges detected\n", |
224 | "Only %d signal edges detected\n", | 253 | dht11->num_edges); |
225 | dht11->num_edges); | ||
226 | ret = -ETIMEDOUT; | 254 | ret = -ETIMEDOUT; |
227 | } | 255 | } |
228 | if (ret < 0) | 256 | if (ret < 0) |
diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig index 5e610f7de5aa..1f1ad41ef881 100644 --- a/drivers/iio/imu/Kconfig +++ b/drivers/iio/imu/Kconfig | |||
@@ -25,6 +25,8 @@ config ADIS16480 | |||
25 | Say yes here to build support for Analog Devices ADIS16375, ADIS16480, | 25 | Say yes here to build support for Analog Devices ADIS16375, ADIS16480, |
26 | ADIS16485, ADIS16488 inertial sensors. | 26 | ADIS16485, ADIS16488 inertial sensors. |
27 | 27 | ||
28 | source "drivers/iio/imu/bmi160/Kconfig" | ||
29 | |||
28 | config KMX61 | 30 | config KMX61 |
29 | tristate "Kionix KMX61 6-axis accelerometer and magnetometer" | 31 | tristate "Kionix KMX61 6-axis accelerometer and magnetometer" |
30 | depends on I2C | 32 | depends on I2C |
diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile index e1e6e3d70e26..c71bcd30dc38 100644 --- a/drivers/iio/imu/Makefile +++ b/drivers/iio/imu/Makefile | |||
@@ -13,6 +13,7 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_trigger.o | |||
13 | adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o | 13 | adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o |
14 | obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o | 14 | obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o |
15 | 15 | ||
16 | obj-y += bmi160/ | ||
16 | obj-y += inv_mpu6050/ | 17 | obj-y += inv_mpu6050/ |
17 | 18 | ||
18 | obj-$(CONFIG_KMX61) += kmx61.o | 19 | obj-$(CONFIG_KMX61) += kmx61.o |
diff --git a/drivers/iio/imu/adis.c b/drivers/iio/imu/adis.c index 911255d41c1a..ad6f91d06185 100644 --- a/drivers/iio/imu/adis.c +++ b/drivers/iio/imu/adis.c | |||
@@ -324,7 +324,12 @@ static int adis_self_test(struct adis *adis) | |||
324 | 324 | ||
325 | msleep(adis->data->startup_delay); | 325 | msleep(adis->data->startup_delay); |
326 | 326 | ||
327 | return adis_check_status(adis); | 327 | ret = adis_check_status(adis); |
328 | |||
329 | if (adis->data->self_test_no_autoclear) | ||
330 | adis_write_reg_16(adis, adis->data->msc_ctrl_reg, 0x00); | ||
331 | |||
332 | return ret; | ||
328 | } | 333 | } |
329 | 334 | ||
330 | /** | 335 | /** |
diff --git a/drivers/iio/imu/bmi160/Kconfig b/drivers/iio/imu/bmi160/Kconfig new file mode 100644 index 000000000000..005c17ccc2b0 --- /dev/null +++ b/drivers/iio/imu/bmi160/Kconfig | |||
@@ -0,0 +1,32 @@ | |||
1 | # | ||
2 | # BMI160 IMU driver | ||
3 | # | ||
4 | |||
5 | config BMI160 | ||
6 | tristate | ||
7 | select IIO_BUFFER | ||
8 | select IIO_TRIGGERED_BUFFER | ||
9 | |||
10 | config BMI160_I2C | ||
11 | tristate "Bosch BMI160 I2C driver" | ||
12 | depends on I2C | ||
13 | select BMI160 | ||
14 | select REGMAP_I2C | ||
15 | help | ||
16 | If you say yes here you get support for BMI160 IMU on I2C with | ||
17 | accelerometer, gyroscope and external BMG160 magnetometer. | ||
18 | |||
19 | This driver can also be built as a module. If so, the module will be | ||
20 | called bmi160_i2c. | ||
21 | |||
22 | config BMI160_SPI | ||
23 | tristate "Bosch BMI160 SPI driver" | ||
24 | depends on SPI | ||
25 | select BMI160 | ||
26 | select REGMAP_SPI | ||
27 | help | ||
28 | If you say yes here you get support for BMI160 IMU on SPI with | ||
29 | accelerometer, gyroscope and external BMG160 magnetometer. | ||
30 | |||
31 | This driver can also be built as a module. If so, the module will be | ||
32 | called bmi160_spi. | ||
diff --git a/drivers/iio/imu/bmi160/Makefile b/drivers/iio/imu/bmi160/Makefile new file mode 100644 index 000000000000..10365e493ae2 --- /dev/null +++ b/drivers/iio/imu/bmi160/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # | ||
2 | # Makefile for Bosch BMI160 IMU | ||
3 | # | ||
4 | obj-$(CONFIG_BMI160) += bmi160_core.o | ||
5 | obj-$(CONFIG_BMI160_I2C) += bmi160_i2c.o | ||
6 | obj-$(CONFIG_BMI160_SPI) += bmi160_spi.o | ||
diff --git a/drivers/iio/imu/bmi160/bmi160.h b/drivers/iio/imu/bmi160/bmi160.h new file mode 100644 index 000000000000..d2ae6ed70271 --- /dev/null +++ b/drivers/iio/imu/bmi160/bmi160.h | |||
@@ -0,0 +1,10 @@ | |||
1 | #ifndef BMI160_H_ | ||
2 | #define BMI160_H_ | ||
3 | |||
4 | extern const struct regmap_config bmi160_regmap_config; | ||
5 | |||
6 | int bmi160_core_probe(struct device *dev, struct regmap *regmap, | ||
7 | const char *name, bool use_spi); | ||
8 | void bmi160_core_remove(struct device *dev); | ||
9 | |||
10 | #endif /* BMI160_H_ */ | ||
diff --git a/drivers/iio/imu/bmi160/bmi160_core.c b/drivers/iio/imu/bmi160/bmi160_core.c new file mode 100644 index 000000000000..0bf92b06d7d8 --- /dev/null +++ b/drivers/iio/imu/bmi160/bmi160_core.c | |||
@@ -0,0 +1,596 @@ | |||
1 | /* | ||
2 | * BMI160 - Bosch IMU (accel, gyro plus external magnetometer) | ||
3 | * | ||
4 | * Copyright (c) 2016, Intel Corporation. | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | * | ||
10 | * IIO core driver for BMI160, with support for I2C/SPI busses | ||
11 | * | ||
12 | * TODO: magnetometer, interrupts, hardware FIFO | ||
13 | */ | ||
14 | #include <linux/module.h> | ||
15 | #include <linux/regmap.h> | ||
16 | #include <linux/acpi.h> | ||
17 | #include <linux/delay.h> | ||
18 | |||
19 | #include <linux/iio/iio.h> | ||
20 | #include <linux/iio/triggered_buffer.h> | ||
21 | #include <linux/iio/trigger_consumer.h> | ||
22 | #include <linux/iio/buffer.h> | ||
23 | |||
24 | #include "bmi160.h" | ||
25 | |||
26 | #define BMI160_REG_CHIP_ID 0x00 | ||
27 | #define BMI160_CHIP_ID_VAL 0xD1 | ||
28 | |||
29 | #define BMI160_REG_PMU_STATUS 0x03 | ||
30 | |||
31 | /* X axis data low byte address, the rest can be obtained using axis offset */ | ||
32 | #define BMI160_REG_DATA_MAGN_XOUT_L 0x04 | ||
33 | #define BMI160_REG_DATA_GYRO_XOUT_L 0x0C | ||
34 | #define BMI160_REG_DATA_ACCEL_XOUT_L 0x12 | ||
35 | |||
36 | #define BMI160_REG_ACCEL_CONFIG 0x40 | ||
37 | #define BMI160_ACCEL_CONFIG_ODR_MASK GENMASK(3, 0) | ||
38 | #define BMI160_ACCEL_CONFIG_BWP_MASK GENMASK(6, 4) | ||
39 | |||
40 | #define BMI160_REG_ACCEL_RANGE 0x41 | ||
41 | #define BMI160_ACCEL_RANGE_2G 0x03 | ||
42 | #define BMI160_ACCEL_RANGE_4G 0x05 | ||
43 | #define BMI160_ACCEL_RANGE_8G 0x08 | ||
44 | #define BMI160_ACCEL_RANGE_16G 0x0C | ||
45 | |||
46 | #define BMI160_REG_GYRO_CONFIG 0x42 | ||
47 | #define BMI160_GYRO_CONFIG_ODR_MASK GENMASK(3, 0) | ||
48 | #define BMI160_GYRO_CONFIG_BWP_MASK GENMASK(5, 4) | ||
49 | |||
50 | #define BMI160_REG_GYRO_RANGE 0x43 | ||
51 | #define BMI160_GYRO_RANGE_2000DPS 0x00 | ||
52 | #define BMI160_GYRO_RANGE_1000DPS 0x01 | ||
53 | #define BMI160_GYRO_RANGE_500DPS 0x02 | ||
54 | #define BMI160_GYRO_RANGE_250DPS 0x03 | ||
55 | #define BMI160_GYRO_RANGE_125DPS 0x04 | ||
56 | |||
57 | #define BMI160_REG_CMD 0x7E | ||
58 | #define BMI160_CMD_ACCEL_PM_SUSPEND 0x10 | ||
59 | #define BMI160_CMD_ACCEL_PM_NORMAL 0x11 | ||
60 | #define BMI160_CMD_ACCEL_PM_LOW_POWER 0x12 | ||
61 | #define BMI160_CMD_GYRO_PM_SUSPEND 0x14 | ||
62 | #define BMI160_CMD_GYRO_PM_NORMAL 0x15 | ||
63 | #define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17 | ||
64 | #define BMI160_CMD_SOFTRESET 0xB6 | ||
65 | |||
66 | #define BMI160_REG_DUMMY 0x7F | ||
67 | |||
68 | #define BMI160_ACCEL_PMU_MIN_USLEEP 3200 | ||
69 | #define BMI160_ACCEL_PMU_MAX_USLEEP 3800 | ||
70 | #define BMI160_GYRO_PMU_MIN_USLEEP 55000 | ||
71 | #define BMI160_GYRO_PMU_MAX_USLEEP 80000 | ||
72 | #define BMI160_SOFTRESET_USLEEP 1000 | ||
73 | |||
74 | #define BMI160_CHANNEL(_type, _axis, _index) { \ | ||
75 | .type = _type, \ | ||
76 | .modified = 1, \ | ||
77 | .channel2 = IIO_MOD_##_axis, \ | ||
78 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ | ||
79 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ | ||
80 | BIT(IIO_CHAN_INFO_SAMP_FREQ), \ | ||
81 | .scan_index = _index, \ | ||
82 | .scan_type = { \ | ||
83 | .sign = 's', \ | ||
84 | .realbits = 16, \ | ||
85 | .storagebits = 16, \ | ||
86 | .endianness = IIO_LE, \ | ||
87 | }, \ | ||
88 | } | ||
89 | |||
90 | /* scan indexes follow DATA register order */ | ||
91 | enum bmi160_scan_axis { | ||
92 | BMI160_SCAN_EXT_MAGN_X = 0, | ||
93 | BMI160_SCAN_EXT_MAGN_Y, | ||
94 | BMI160_SCAN_EXT_MAGN_Z, | ||
95 | BMI160_SCAN_RHALL, | ||
96 | BMI160_SCAN_GYRO_X, | ||
97 | BMI160_SCAN_GYRO_Y, | ||
98 | BMI160_SCAN_GYRO_Z, | ||
99 | BMI160_SCAN_ACCEL_X, | ||
100 | BMI160_SCAN_ACCEL_Y, | ||
101 | BMI160_SCAN_ACCEL_Z, | ||
102 | BMI160_SCAN_TIMESTAMP, | ||
103 | }; | ||
104 | |||
105 | enum bmi160_sensor_type { | ||
106 | BMI160_ACCEL = 0, | ||
107 | BMI160_GYRO, | ||
108 | BMI160_EXT_MAGN, | ||
109 | BMI160_NUM_SENSORS /* must be last */ | ||
110 | }; | ||
111 | |||
112 | struct bmi160_data { | ||
113 | struct regmap *regmap; | ||
114 | }; | ||
115 | |||
116 | const struct regmap_config bmi160_regmap_config = { | ||
117 | .reg_bits = 8, | ||
118 | .val_bits = 8, | ||
119 | }; | ||
120 | EXPORT_SYMBOL(bmi160_regmap_config); | ||
121 | |||
122 | struct bmi160_regs { | ||
123 | u8 data; /* LSB byte register for X-axis */ | ||
124 | u8 config; | ||
125 | u8 config_odr_mask; | ||
126 | u8 config_bwp_mask; | ||
127 | u8 range; | ||
128 | u8 pmu_cmd_normal; | ||
129 | u8 pmu_cmd_suspend; | ||
130 | }; | ||
131 | |||
132 | static struct bmi160_regs bmi160_regs[] = { | ||
133 | [BMI160_ACCEL] = { | ||
134 | .data = BMI160_REG_DATA_ACCEL_XOUT_L, | ||
135 | .config = BMI160_REG_ACCEL_CONFIG, | ||
136 | .config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK, | ||
137 | .config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK, | ||
138 | .range = BMI160_REG_ACCEL_RANGE, | ||
139 | .pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL, | ||
140 | .pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND, | ||
141 | }, | ||
142 | [BMI160_GYRO] = { | ||
143 | .data = BMI160_REG_DATA_GYRO_XOUT_L, | ||
144 | .config = BMI160_REG_GYRO_CONFIG, | ||
145 | .config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK, | ||
146 | .config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK, | ||
147 | .range = BMI160_REG_GYRO_RANGE, | ||
148 | .pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL, | ||
149 | .pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND, | ||
150 | }, | ||
151 | }; | ||
152 | |||
153 | struct bmi160_pmu_time { | ||
154 | unsigned long min; | ||
155 | unsigned long max; | ||
156 | }; | ||
157 | |||
158 | static struct bmi160_pmu_time bmi160_pmu_time[] = { | ||
159 | [BMI160_ACCEL] = { | ||
160 | .min = BMI160_ACCEL_PMU_MIN_USLEEP, | ||
161 | .max = BMI160_ACCEL_PMU_MAX_USLEEP | ||
162 | }, | ||
163 | [BMI160_GYRO] = { | ||
164 | .min = BMI160_GYRO_PMU_MIN_USLEEP, | ||
165 | .max = BMI160_GYRO_PMU_MIN_USLEEP, | ||
166 | }, | ||
167 | }; | ||
168 | |||
169 | struct bmi160_scale { | ||
170 | u8 bits; | ||
171 | int uscale; | ||
172 | }; | ||
173 | |||
174 | struct bmi160_odr { | ||
175 | u8 bits; | ||
176 | int odr; | ||
177 | int uodr; | ||
178 | }; | ||
179 | |||
180 | static const struct bmi160_scale bmi160_accel_scale[] = { | ||
181 | { BMI160_ACCEL_RANGE_2G, 598}, | ||
182 | { BMI160_ACCEL_RANGE_4G, 1197}, | ||
183 | { BMI160_ACCEL_RANGE_8G, 2394}, | ||
184 | { BMI160_ACCEL_RANGE_16G, 4788}, | ||
185 | }; | ||
186 | |||
187 | static const struct bmi160_scale bmi160_gyro_scale[] = { | ||
188 | { BMI160_GYRO_RANGE_2000DPS, 1065}, | ||
189 | { BMI160_GYRO_RANGE_1000DPS, 532}, | ||
190 | { BMI160_GYRO_RANGE_500DPS, 266}, | ||
191 | { BMI160_GYRO_RANGE_250DPS, 133}, | ||
192 | { BMI160_GYRO_RANGE_125DPS, 66}, | ||
193 | }; | ||
194 | |||
195 | struct bmi160_scale_item { | ||
196 | const struct bmi160_scale *tbl; | ||
197 | int num; | ||
198 | }; | ||
199 | |||
200 | static const struct bmi160_scale_item bmi160_scale_table[] = { | ||
201 | [BMI160_ACCEL] = { | ||
202 | .tbl = bmi160_accel_scale, | ||
203 | .num = ARRAY_SIZE(bmi160_accel_scale), | ||
204 | }, | ||
205 | [BMI160_GYRO] = { | ||
206 | .tbl = bmi160_gyro_scale, | ||
207 | .num = ARRAY_SIZE(bmi160_gyro_scale), | ||
208 | }, | ||
209 | }; | ||
210 | |||
211 | static const struct bmi160_odr bmi160_accel_odr[] = { | ||
212 | {0x01, 0, 78125}, | ||
213 | {0x02, 1, 5625}, | ||
214 | {0x03, 3, 125}, | ||
215 | {0x04, 6, 25}, | ||
216 | {0x05, 12, 5}, | ||
217 | {0x06, 25, 0}, | ||
218 | {0x07, 50, 0}, | ||
219 | {0x08, 100, 0}, | ||
220 | {0x09, 200, 0}, | ||
221 | {0x0A, 400, 0}, | ||
222 | {0x0B, 800, 0}, | ||
223 | {0x0C, 1600, 0}, | ||
224 | }; | ||
225 | |||
226 | static const struct bmi160_odr bmi160_gyro_odr[] = { | ||
227 | {0x06, 25, 0}, | ||
228 | {0x07, 50, 0}, | ||
229 | {0x08, 100, 0}, | ||
230 | {0x09, 200, 0}, | ||
231 | {0x0A, 400, 0}, | ||
232 | {0x0B, 8000, 0}, | ||
233 | {0x0C, 1600, 0}, | ||
234 | {0x0D, 3200, 0}, | ||
235 | }; | ||
236 | |||
237 | struct bmi160_odr_item { | ||
238 | const struct bmi160_odr *tbl; | ||
239 | int num; | ||
240 | }; | ||
241 | |||
242 | static const struct bmi160_odr_item bmi160_odr_table[] = { | ||
243 | [BMI160_ACCEL] = { | ||
244 | .tbl = bmi160_accel_odr, | ||
245 | .num = ARRAY_SIZE(bmi160_accel_odr), | ||
246 | }, | ||
247 | [BMI160_GYRO] = { | ||
248 | .tbl = bmi160_gyro_odr, | ||
249 | .num = ARRAY_SIZE(bmi160_gyro_odr), | ||
250 | }, | ||
251 | }; | ||
252 | |||
253 | static const struct iio_chan_spec bmi160_channels[] = { | ||
254 | BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X), | ||
255 | BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y), | ||
256 | BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z), | ||
257 | BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X), | ||
258 | BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y), | ||
259 | BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z), | ||
260 | IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP), | ||
261 | }; | ||
262 | |||
263 | static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type) | ||
264 | { | ||
265 | switch (iio_type) { | ||
266 | case IIO_ACCEL: | ||
267 | return BMI160_ACCEL; | ||
268 | case IIO_ANGL_VEL: | ||
269 | return BMI160_GYRO; | ||
270 | default: | ||
271 | return -EINVAL; | ||
272 | } | ||
273 | } | ||
274 | |||
275 | static | ||
276 | int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t, | ||
277 | bool mode) | ||
278 | { | ||
279 | int ret; | ||
280 | u8 cmd; | ||
281 | |||
282 | if (mode) | ||
283 | cmd = bmi160_regs[t].pmu_cmd_normal; | ||
284 | else | ||
285 | cmd = bmi160_regs[t].pmu_cmd_suspend; | ||
286 | |||
287 | ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd); | ||
288 | if (ret < 0) | ||
289 | return ret; | ||
290 | |||
291 | usleep_range(bmi160_pmu_time[t].min, bmi160_pmu_time[t].max); | ||
292 | |||
293 | return 0; | ||
294 | } | ||
295 | |||
296 | static | ||
297 | int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t, | ||
298 | int uscale) | ||
299 | { | ||
300 | int i; | ||
301 | |||
302 | for (i = 0; i < bmi160_scale_table[t].num; i++) | ||
303 | if (bmi160_scale_table[t].tbl[i].uscale == uscale) | ||
304 | break; | ||
305 | |||
306 | if (i == bmi160_scale_table[t].num) | ||
307 | return -EINVAL; | ||
308 | |||
309 | return regmap_write(data->regmap, bmi160_regs[t].range, | ||
310 | bmi160_scale_table[t].tbl[i].bits); | ||
311 | } | ||
312 | |||
313 | static | ||
314 | int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t, | ||
315 | int *uscale) | ||
316 | { | ||
317 | int i, ret, val; | ||
318 | |||
319 | ret = regmap_read(data->regmap, bmi160_regs[t].range, &val); | ||
320 | if (ret < 0) | ||
321 | return ret; | ||
322 | |||
323 | for (i = 0; i < bmi160_scale_table[t].num; i++) | ||
324 | if (bmi160_scale_table[t].tbl[i].bits == val) { | ||
325 | *uscale = bmi160_scale_table[t].tbl[i].uscale; | ||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | return -EINVAL; | ||
330 | } | ||
331 | |||
332 | static int bmi160_get_data(struct bmi160_data *data, int chan_type, | ||
333 | int axis, int *val) | ||
334 | { | ||
335 | u8 reg; | ||
336 | int ret; | ||
337 | __le16 sample; | ||
338 | enum bmi160_sensor_type t = bmi160_to_sensor(chan_type); | ||
339 | |||
340 | reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(__le16); | ||
341 | |||
342 | ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(__le16)); | ||
343 | if (ret < 0) | ||
344 | return ret; | ||
345 | |||
346 | *val = sign_extend32(le16_to_cpu(sample), 15); | ||
347 | |||
348 | return 0; | ||
349 | } | ||
350 | |||
351 | static | ||
352 | int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t, | ||
353 | int odr, int uodr) | ||
354 | { | ||
355 | int i; | ||
356 | |||
357 | for (i = 0; i < bmi160_odr_table[t].num; i++) | ||
358 | if (bmi160_odr_table[t].tbl[i].odr == odr && | ||
359 | bmi160_odr_table[t].tbl[i].uodr == uodr) | ||
360 | break; | ||
361 | |||
362 | if (i >= bmi160_odr_table[t].num) | ||
363 | return -EINVAL; | ||
364 | |||
365 | return regmap_update_bits(data->regmap, | ||
366 | bmi160_regs[t].config, | ||
367 | bmi160_odr_table[t].tbl[i].bits, | ||
368 | bmi160_regs[t].config_odr_mask); | ||
369 | } | ||
370 | |||
371 | static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t, | ||
372 | int *odr, int *uodr) | ||
373 | { | ||
374 | int i, val, ret; | ||
375 | |||
376 | ret = regmap_read(data->regmap, bmi160_regs[t].config, &val); | ||
377 | if (ret < 0) | ||
378 | return ret; | ||
379 | |||
380 | val &= bmi160_regs[t].config_odr_mask; | ||
381 | |||
382 | for (i = 0; i < bmi160_odr_table[t].num; i++) | ||
383 | if (val == bmi160_odr_table[t].tbl[i].bits) | ||
384 | break; | ||
385 | |||
386 | if (i >= bmi160_odr_table[t].num) | ||
387 | return -EINVAL; | ||
388 | |||
389 | *odr = bmi160_odr_table[t].tbl[i].odr; | ||
390 | *uodr = bmi160_odr_table[t].tbl[i].uodr; | ||
391 | |||
392 | return 0; | ||
393 | } | ||
394 | |||
395 | static irqreturn_t bmi160_trigger_handler(int irq, void *p) | ||
396 | { | ||
397 | struct iio_poll_func *pf = p; | ||
398 | struct iio_dev *indio_dev = pf->indio_dev; | ||
399 | struct bmi160_data *data = iio_priv(indio_dev); | ||
400 | s16 buf[16]; /* 3 sens x 3 axis x s16 + 3 x s16 pad + 4 x s16 tstamp */ | ||
401 | int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L; | ||
402 | __le16 sample; | ||
403 | |||
404 | for_each_set_bit(i, indio_dev->active_scan_mask, | ||
405 | indio_dev->masklength) { | ||
406 | ret = regmap_bulk_read(data->regmap, base + i * sizeof(__le16), | ||
407 | &sample, sizeof(__le16)); | ||
408 | if (ret < 0) | ||
409 | goto done; | ||
410 | buf[j++] = sample; | ||
411 | } | ||
412 | |||
413 | iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); | ||
414 | done: | ||
415 | iio_trigger_notify_done(indio_dev->trig); | ||
416 | return IRQ_HANDLED; | ||
417 | } | ||
418 | |||
419 | static int bmi160_read_raw(struct iio_dev *indio_dev, | ||
420 | struct iio_chan_spec const *chan, | ||
421 | int *val, int *val2, long mask) | ||
422 | { | ||
423 | int ret; | ||
424 | struct bmi160_data *data = iio_priv(indio_dev); | ||
425 | |||
426 | switch (mask) { | ||
427 | case IIO_CHAN_INFO_RAW: | ||
428 | ret = bmi160_get_data(data, chan->type, chan->channel2, val); | ||
429 | if (ret < 0) | ||
430 | return ret; | ||
431 | return IIO_VAL_INT; | ||
432 | case IIO_CHAN_INFO_SCALE: | ||
433 | *val = 0; | ||
434 | ret = bmi160_get_scale(data, | ||
435 | bmi160_to_sensor(chan->type), val2); | ||
436 | return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO; | ||
437 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
438 | ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type), | ||
439 | val, val2); | ||
440 | return ret < 0 ? ret : IIO_VAL_INT_PLUS_MICRO; | ||
441 | default: | ||
442 | return -EINVAL; | ||
443 | } | ||
444 | |||
445 | return 0; | ||
446 | } | ||
447 | |||
448 | static int bmi160_write_raw(struct iio_dev *indio_dev, | ||
449 | struct iio_chan_spec const *chan, | ||
450 | int val, int val2, long mask) | ||
451 | { | ||
452 | struct bmi160_data *data = iio_priv(indio_dev); | ||
453 | |||
454 | switch (mask) { | ||
455 | case IIO_CHAN_INFO_SCALE: | ||
456 | return bmi160_set_scale(data, | ||
457 | bmi160_to_sensor(chan->type), val2); | ||
458 | break; | ||
459 | case IIO_CHAN_INFO_SAMP_FREQ: | ||
460 | return bmi160_set_odr(data, bmi160_to_sensor(chan->type), | ||
461 | val, val2); | ||
462 | default: | ||
463 | return -EINVAL; | ||
464 | } | ||
465 | |||
466 | return 0; | ||
467 | } | ||
468 | |||
469 | static const struct iio_info bmi160_info = { | ||
470 | .driver_module = THIS_MODULE, | ||
471 | .read_raw = bmi160_read_raw, | ||
472 | .write_raw = bmi160_write_raw, | ||
473 | }; | ||
474 | |||
475 | static const char *bmi160_match_acpi_device(struct device *dev) | ||
476 | { | ||
477 | const struct acpi_device_id *id; | ||
478 | |||
479 | id = acpi_match_device(dev->driver->acpi_match_table, dev); | ||
480 | if (!id) | ||
481 | return NULL; | ||
482 | |||
483 | return dev_name(dev); | ||
484 | } | ||
485 | |||
486 | static int bmi160_chip_init(struct bmi160_data *data, bool use_spi) | ||
487 | { | ||
488 | int ret; | ||
489 | unsigned int val; | ||
490 | struct device *dev = regmap_get_device(data->regmap); | ||
491 | |||
492 | ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET); | ||
493 | if (ret < 0) | ||
494 | return ret; | ||
495 | |||
496 | usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1); | ||
497 | |||
498 | /* | ||
499 | * CS rising edge is needed before starting SPI, so do a dummy read | ||
500 | * See Section 3.2.1, page 86 of the datasheet | ||
501 | */ | ||
502 | if (use_spi) { | ||
503 | ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val); | ||
504 | if (ret < 0) | ||
505 | return ret; | ||
506 | } | ||
507 | |||
508 | ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val); | ||
509 | if (ret < 0) { | ||
510 | dev_err(dev, "Error reading chip id\n"); | ||
511 | return ret; | ||
512 | } | ||
513 | if (val != BMI160_CHIP_ID_VAL) { | ||
514 | dev_err(dev, "Wrong chip id, got %x expected %x\n", | ||
515 | val, BMI160_CHIP_ID_VAL); | ||
516 | return -ENODEV; | ||
517 | } | ||
518 | |||
519 | ret = bmi160_set_mode(data, BMI160_ACCEL, true); | ||
520 | if (ret < 0) | ||
521 | return ret; | ||
522 | |||
523 | ret = bmi160_set_mode(data, BMI160_GYRO, true); | ||
524 | if (ret < 0) | ||
525 | return ret; | ||
526 | |||
527 | return 0; | ||
528 | } | ||
529 | |||
530 | static void bmi160_chip_uninit(struct bmi160_data *data) | ||
531 | { | ||
532 | bmi160_set_mode(data, BMI160_GYRO, false); | ||
533 | bmi160_set_mode(data, BMI160_ACCEL, false); | ||
534 | } | ||
535 | |||
536 | int bmi160_core_probe(struct device *dev, struct regmap *regmap, | ||
537 | const char *name, bool use_spi) | ||
538 | { | ||
539 | struct iio_dev *indio_dev; | ||
540 | struct bmi160_data *data; | ||
541 | int ret; | ||
542 | |||
543 | indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); | ||
544 | if (!indio_dev) | ||
545 | return -ENOMEM; | ||
546 | |||
547 | data = iio_priv(indio_dev); | ||
548 | dev_set_drvdata(dev, indio_dev); | ||
549 | data->regmap = regmap; | ||
550 | |||
551 | ret = bmi160_chip_init(data, use_spi); | ||
552 | if (ret < 0) | ||
553 | return ret; | ||
554 | |||
555 | if (!name && ACPI_HANDLE(dev)) | ||
556 | name = bmi160_match_acpi_device(dev); | ||
557 | |||
558 | indio_dev->dev.parent = dev; | ||
559 | indio_dev->channels = bmi160_channels; | ||
560 | indio_dev->num_channels = ARRAY_SIZE(bmi160_channels); | ||
561 | indio_dev->name = name; | ||
562 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
563 | indio_dev->info = &bmi160_info; | ||
564 | |||
565 | ret = iio_triggered_buffer_setup(indio_dev, NULL, | ||
566 | bmi160_trigger_handler, NULL); | ||
567 | if (ret < 0) | ||
568 | goto uninit; | ||
569 | |||
570 | ret = iio_device_register(indio_dev); | ||
571 | if (ret < 0) | ||
572 | goto buffer_cleanup; | ||
573 | |||
574 | return 0; | ||
575 | buffer_cleanup: | ||
576 | iio_triggered_buffer_cleanup(indio_dev); | ||
577 | uninit: | ||
578 | bmi160_chip_uninit(data); | ||
579 | return ret; | ||
580 | } | ||
581 | EXPORT_SYMBOL_GPL(bmi160_core_probe); | ||
582 | |||
583 | void bmi160_core_remove(struct device *dev) | ||
584 | { | ||
585 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | ||
586 | struct bmi160_data *data = iio_priv(indio_dev); | ||
587 | |||
588 | iio_device_unregister(indio_dev); | ||
589 | iio_triggered_buffer_cleanup(indio_dev); | ||
590 | bmi160_chip_uninit(data); | ||
591 | } | ||
592 | EXPORT_SYMBOL_GPL(bmi160_core_remove); | ||
593 | |||
594 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); | ||
595 | MODULE_DESCRIPTION("Bosch BMI160 driver"); | ||
596 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/imu/bmi160/bmi160_i2c.c b/drivers/iio/imu/bmi160/bmi160_i2c.c new file mode 100644 index 000000000000..07a179d8fb48 --- /dev/null +++ b/drivers/iio/imu/bmi160/bmi160_i2c.c | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * BMI160 - Bosch IMU, I2C bits | ||
3 | * | ||
4 | * Copyright (c) 2016, Intel Corporation. | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | * | ||
10 | * 7-bit I2C slave address is: | ||
11 | * - 0x68 if SDO is pulled to GND | ||
12 | * - 0x69 if SDO is pulled to VDDIO | ||
13 | */ | ||
14 | #include <linux/module.h> | ||
15 | #include <linux/i2c.h> | ||
16 | #include <linux/regmap.h> | ||
17 | #include <linux/acpi.h> | ||
18 | |||
19 | #include "bmi160.h" | ||
20 | |||
21 | static int bmi160_i2c_probe(struct i2c_client *client, | ||
22 | const struct i2c_device_id *id) | ||
23 | { | ||
24 | struct regmap *regmap; | ||
25 | const char *name = NULL; | ||
26 | |||
27 | regmap = devm_regmap_init_i2c(client, &bmi160_regmap_config); | ||
28 | if (IS_ERR(regmap)) { | ||
29 | dev_err(&client->dev, "Failed to register i2c regmap %d\n", | ||
30 | (int)PTR_ERR(regmap)); | ||
31 | return PTR_ERR(regmap); | ||
32 | } | ||
33 | |||
34 | if (id) | ||
35 | name = id->name; | ||
36 | |||
37 | return bmi160_core_probe(&client->dev, regmap, name, false); | ||
38 | } | ||
39 | |||
40 | static int bmi160_i2c_remove(struct i2c_client *client) | ||
41 | { | ||
42 | bmi160_core_remove(&client->dev); | ||
43 | |||
44 | return 0; | ||
45 | } | ||
46 | |||
47 | static const struct i2c_device_id bmi160_i2c_id[] = { | ||
48 | {"bmi160", 0}, | ||
49 | {} | ||
50 | }; | ||
51 | MODULE_DEVICE_TABLE(i2c, bmi160_i2c_id); | ||
52 | |||
53 | static const struct acpi_device_id bmi160_acpi_match[] = { | ||
54 | {"BMI0160", 0}, | ||
55 | { }, | ||
56 | }; | ||
57 | MODULE_DEVICE_TABLE(acpi, bmi160_acpi_match); | ||
58 | |||
59 | static struct i2c_driver bmi160_i2c_driver = { | ||
60 | .driver = { | ||
61 | .name = "bmi160_i2c", | ||
62 | .acpi_match_table = ACPI_PTR(bmi160_acpi_match), | ||
63 | }, | ||
64 | .probe = bmi160_i2c_probe, | ||
65 | .remove = bmi160_i2c_remove, | ||
66 | .id_table = bmi160_i2c_id, | ||
67 | }; | ||
68 | module_i2c_driver(bmi160_i2c_driver); | ||
69 | |||
70 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); | ||
71 | MODULE_DESCRIPTION("BMI160 I2C driver"); | ||
72 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/imu/bmi160/bmi160_spi.c b/drivers/iio/imu/bmi160/bmi160_spi.c new file mode 100644 index 000000000000..1ec8b12bd984 --- /dev/null +++ b/drivers/iio/imu/bmi160/bmi160_spi.c | |||
@@ -0,0 +1,63 @@ | |||
1 | /* | ||
2 | * BMI160 - Bosch IMU, SPI bits | ||
3 | * | ||
4 | * Copyright (c) 2016, Intel Corporation. | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | */ | ||
10 | #include <linux/module.h> | ||
11 | #include <linux/spi/spi.h> | ||
12 | #include <linux/regmap.h> | ||
13 | #include <linux/acpi.h> | ||
14 | |||
15 | #include "bmi160.h" | ||
16 | |||
17 | static int bmi160_spi_probe(struct spi_device *spi) | ||
18 | { | ||
19 | struct regmap *regmap; | ||
20 | const struct spi_device_id *id = spi_get_device_id(spi); | ||
21 | |||
22 | regmap = devm_regmap_init_spi(spi, &bmi160_regmap_config); | ||
23 | if (IS_ERR(regmap)) { | ||
24 | dev_err(&spi->dev, "Failed to register spi regmap %d\n", | ||
25 | (int)PTR_ERR(regmap)); | ||
26 | return PTR_ERR(regmap); | ||
27 | } | ||
28 | return bmi160_core_probe(&spi->dev, regmap, id->name, true); | ||
29 | } | ||
30 | |||
31 | static int bmi160_spi_remove(struct spi_device *spi) | ||
32 | { | ||
33 | bmi160_core_remove(&spi->dev); | ||
34 | |||
35 | return 0; | ||
36 | } | ||
37 | |||
38 | static const struct spi_device_id bmi160_spi_id[] = { | ||
39 | {"bmi160", 0}, | ||
40 | {} | ||
41 | }; | ||
42 | MODULE_DEVICE_TABLE(spi, bmi160_spi_id); | ||
43 | |||
44 | static const struct acpi_device_id bmi160_acpi_match[] = { | ||
45 | {"BMI0160", 0}, | ||
46 | { }, | ||
47 | }; | ||
48 | MODULE_DEVICE_TABLE(acpi, bmi160_acpi_match); | ||
49 | |||
50 | static struct spi_driver bmi160_spi_driver = { | ||
51 | .probe = bmi160_spi_probe, | ||
52 | .remove = bmi160_spi_remove, | ||
53 | .id_table = bmi160_spi_id, | ||
54 | .driver = { | ||
55 | .acpi_match_table = ACPI_PTR(bmi160_acpi_match), | ||
56 | .name = "bmi160_spi", | ||
57 | }, | ||
58 | }; | ||
59 | module_spi_driver(bmi160_spi_driver); | ||
60 | |||
61 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); | ||
62 | MODULE_DESCRIPTION("Bosch BMI160 SPI driver"); | ||
63 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig index 847455a2d6bb..f756feecfa4c 100644 --- a/drivers/iio/imu/inv_mpu6050/Kconfig +++ b/drivers/iio/imu/inv_mpu6050/Kconfig | |||
@@ -13,10 +13,8 @@ config INV_MPU6050_I2C | |||
13 | select INV_MPU6050_IIO | 13 | select INV_MPU6050_IIO |
14 | select REGMAP_I2C | 14 | select REGMAP_I2C |
15 | help | 15 | help |
16 | This driver supports the Invensense MPU6050 devices. | 16 | This driver supports the Invensense MPU6050/6500/9150 motion tracking |
17 | This driver can also support MPU6500 in MPU6050 compatibility mode | 17 | devices over I2C. |
18 | and also in MPU6500 mode with some limitations. | ||
19 | It is a gyroscope/accelerometer combo device. | ||
20 | This driver can be built as a module. The module will be called | 18 | This driver can be built as a module. The module will be called |
21 | inv-mpu6050-i2c. | 19 | inv-mpu6050-i2c. |
22 | 20 | ||
@@ -26,7 +24,7 @@ config INV_MPU6050_SPI | |||
26 | select INV_MPU6050_IIO | 24 | select INV_MPU6050_IIO |
27 | select REGMAP_SPI | 25 | select REGMAP_SPI |
28 | help | 26 | help |
29 | This driver supports the Invensense MPU6050 devices. | 27 | This driver supports the Invensense MPU6000/6500/9150 motion tracking |
30 | It is a gyroscope/accelerometer combo device. | 28 | devices over SPI. |
31 | This driver can be built as a module. The module will be called | 29 | This driver can be built as a module. The module will be called |
32 | inv-mpu6050-spi. | 30 | inv-mpu6050-spi. |
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c index d192953e9a38..b269b375ca34 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c | |||
@@ -88,16 +88,29 @@ static const struct inv_mpu6050_chip_config chip_config_6050 = { | |||
88 | .accl_fs = INV_MPU6050_FS_02G, | 88 | .accl_fs = INV_MPU6050_FS_02G, |
89 | }; | 89 | }; |
90 | 90 | ||
91 | /* Indexed by enum inv_devices */ | ||
91 | static const struct inv_mpu6050_hw hw_info[] = { | 92 | static const struct inv_mpu6050_hw hw_info[] = { |
92 | { | 93 | { |
93 | .num_reg = 117, | 94 | .whoami = INV_MPU6050_WHOAMI_VALUE, |
95 | .name = "MPU6050", | ||
96 | .reg = ®_set_6050, | ||
97 | .config = &chip_config_6050, | ||
98 | }, | ||
99 | { | ||
100 | .whoami = INV_MPU6500_WHOAMI_VALUE, | ||
94 | .name = "MPU6500", | 101 | .name = "MPU6500", |
95 | .reg = ®_set_6500, | 102 | .reg = ®_set_6500, |
96 | .config = &chip_config_6050, | 103 | .config = &chip_config_6050, |
97 | }, | 104 | }, |
98 | { | 105 | { |
99 | .num_reg = 117, | 106 | .whoami = INV_MPU6000_WHOAMI_VALUE, |
100 | .name = "MPU6050", | 107 | .name = "MPU6000", |
108 | .reg = ®_set_6050, | ||
109 | .config = &chip_config_6050, | ||
110 | }, | ||
111 | { | ||
112 | .whoami = INV_MPU9150_WHOAMI_VALUE, | ||
113 | .name = "MPU9150", | ||
101 | .reg = ®_set_6050, | 114 | .reg = ®_set_6050, |
102 | .config = &chip_config_6050, | 115 | .config = &chip_config_6050, |
103 | }, | 116 | }, |
@@ -600,6 +613,10 @@ inv_fifo_rate_show(struct device *dev, struct device_attribute *attr, | |||
600 | /** | 613 | /** |
601 | * inv_attr_show() - calling this function will show current | 614 | * inv_attr_show() - calling this function will show current |
602 | * parameters. | 615 | * parameters. |
616 | * | ||
617 | * Deprecated in favor of IIO mounting matrix API. | ||
618 | * | ||
619 | * See inv_get_mount_matrix() | ||
603 | */ | 620 | */ |
604 | static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr, | 621 | static ssize_t inv_attr_show(struct device *dev, struct device_attribute *attr, |
605 | char *buf) | 622 | char *buf) |
@@ -644,6 +661,18 @@ static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev, | |||
644 | return 0; | 661 | return 0; |
645 | } | 662 | } |
646 | 663 | ||
664 | static const struct iio_mount_matrix * | ||
665 | inv_get_mount_matrix(const struct iio_dev *indio_dev, | ||
666 | const struct iio_chan_spec *chan) | ||
667 | { | ||
668 | return &((struct inv_mpu6050_state *)iio_priv(indio_dev))->orientation; | ||
669 | } | ||
670 | |||
671 | static const struct iio_chan_spec_ext_info inv_ext_info[] = { | ||
672 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, inv_get_mount_matrix), | ||
673 | { }, | ||
674 | }; | ||
675 | |||
647 | #define INV_MPU6050_CHAN(_type, _channel2, _index) \ | 676 | #define INV_MPU6050_CHAN(_type, _channel2, _index) \ |
648 | { \ | 677 | { \ |
649 | .type = _type, \ | 678 | .type = _type, \ |
@@ -660,6 +689,7 @@ static int inv_mpu6050_validate_trigger(struct iio_dev *indio_dev, | |||
660 | .shift = 0, \ | 689 | .shift = 0, \ |
661 | .endianness = IIO_BE, \ | 690 | .endianness = IIO_BE, \ |
662 | }, \ | 691 | }, \ |
692 | .ext_info = inv_ext_info, \ | ||
663 | } | 693 | } |
664 | 694 | ||
665 | static const struct iio_chan_spec inv_mpu_channels[] = { | 695 | static const struct iio_chan_spec inv_mpu_channels[] = { |
@@ -692,14 +722,16 @@ static IIO_CONST_ATTR(in_accel_scale_available, | |||
692 | "0.000598 0.001196 0.002392 0.004785"); | 722 | "0.000598 0.001196 0.002392 0.004785"); |
693 | static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show, | 723 | static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR, inv_fifo_rate_show, |
694 | inv_mpu6050_fifo_rate_store); | 724 | inv_mpu6050_fifo_rate_store); |
725 | |||
726 | /* Deprecated: kept for userspace backward compatibility. */ | ||
695 | static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL, | 727 | static IIO_DEVICE_ATTR(in_gyro_matrix, S_IRUGO, inv_attr_show, NULL, |
696 | ATTR_GYRO_MATRIX); | 728 | ATTR_GYRO_MATRIX); |
697 | static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL, | 729 | static IIO_DEVICE_ATTR(in_accel_matrix, S_IRUGO, inv_attr_show, NULL, |
698 | ATTR_ACCL_MATRIX); | 730 | ATTR_ACCL_MATRIX); |
699 | 731 | ||
700 | static struct attribute *inv_attributes[] = { | 732 | static struct attribute *inv_attributes[] = { |
701 | &iio_dev_attr_in_gyro_matrix.dev_attr.attr, | 733 | &iio_dev_attr_in_gyro_matrix.dev_attr.attr, /* deprecated */ |
702 | &iio_dev_attr_in_accel_matrix.dev_attr.attr, | 734 | &iio_dev_attr_in_accel_matrix.dev_attr.attr, /* deprecated */ |
703 | &iio_dev_attr_sampling_frequency.dev_attr.attr, | 735 | &iio_dev_attr_sampling_frequency.dev_attr.attr, |
704 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | 736 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, |
705 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, | 737 | &iio_const_attr_in_accel_scale_available.dev_attr.attr, |
@@ -726,6 +758,7 @@ static const struct iio_info mpu_info = { | |||
726 | static int inv_check_and_setup_chip(struct inv_mpu6050_state *st) | 758 | static int inv_check_and_setup_chip(struct inv_mpu6050_state *st) |
727 | { | 759 | { |
728 | int result; | 760 | int result; |
761 | unsigned int regval; | ||
729 | 762 | ||
730 | st->hw = &hw_info[st->chip_type]; | 763 | st->hw = &hw_info[st->chip_type]; |
731 | st->reg = hw_info[st->chip_type].reg; | 764 | st->reg = hw_info[st->chip_type].reg; |
@@ -736,6 +769,17 @@ static int inv_check_and_setup_chip(struct inv_mpu6050_state *st) | |||
736 | if (result) | 769 | if (result) |
737 | return result; | 770 | return result; |
738 | msleep(INV_MPU6050_POWER_UP_TIME); | 771 | msleep(INV_MPU6050_POWER_UP_TIME); |
772 | |||
773 | /* check chip self-identification */ | ||
774 | result = regmap_read(st->map, INV_MPU6050_REG_WHOAMI, ®val); | ||
775 | if (result) | ||
776 | return result; | ||
777 | if (regval != st->hw->whoami) { | ||
778 | dev_warn(regmap_get_device(st->map), | ||
779 | "whoami mismatch got %#02x expected %#02hhx for %s\n", | ||
780 | regval, st->hw->whoami, st->hw->name); | ||
781 | } | ||
782 | |||
739 | /* | 783 | /* |
740 | * toggle power state. After reset, the sleep bit could be on | 784 | * toggle power state. After reset, the sleep bit could be on |
741 | * or off depending on the OTP settings. Toggling power would | 785 | * or off depending on the OTP settings. Toggling power would |
@@ -774,14 +818,31 @@ int inv_mpu_core_probe(struct regmap *regmap, int irq, const char *name, | |||
774 | if (!indio_dev) | 818 | if (!indio_dev) |
775 | return -ENOMEM; | 819 | return -ENOMEM; |
776 | 820 | ||
821 | BUILD_BUG_ON(ARRAY_SIZE(hw_info) != INV_NUM_PARTS); | ||
822 | if (chip_type < 0 || chip_type >= INV_NUM_PARTS) { | ||
823 | dev_err(dev, "Bad invensense chip_type=%d name=%s\n", | ||
824 | chip_type, name); | ||
825 | return -ENODEV; | ||
826 | } | ||
777 | st = iio_priv(indio_dev); | 827 | st = iio_priv(indio_dev); |
778 | st->chip_type = chip_type; | 828 | st->chip_type = chip_type; |
779 | st->powerup_count = 0; | 829 | st->powerup_count = 0; |
780 | st->irq = irq; | 830 | st->irq = irq; |
781 | st->map = regmap; | 831 | st->map = regmap; |
832 | |||
782 | pdata = dev_get_platdata(dev); | 833 | pdata = dev_get_platdata(dev); |
783 | if (pdata) | 834 | if (!pdata) { |
835 | result = of_iio_read_mount_matrix(dev, "mount-matrix", | ||
836 | &st->orientation); | ||
837 | if (result) { | ||
838 | dev_err(dev, "Failed to retrieve mounting matrix %d\n", | ||
839 | result); | ||
840 | return result; | ||
841 | } | ||
842 | } else { | ||
784 | st->plat_data = *pdata; | 843 | st->plat_data = *pdata; |
844 | } | ||
845 | |||
785 | /* power is turned on inside check chip type*/ | 846 | /* power is turned on inside check chip type*/ |
786 | result = inv_check_and_setup_chip(st); | 847 | result = inv_check_and_setup_chip(st); |
787 | if (result) | 848 | if (result) |
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c index 5ee4e0dc093e..1a424a6561de 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_i2c.c | |||
@@ -202,13 +202,14 @@ static int inv_mpu_remove(struct i2c_client *client) | |||
202 | static const struct i2c_device_id inv_mpu_id[] = { | 202 | static const struct i2c_device_id inv_mpu_id[] = { |
203 | {"mpu6050", INV_MPU6050}, | 203 | {"mpu6050", INV_MPU6050}, |
204 | {"mpu6500", INV_MPU6500}, | 204 | {"mpu6500", INV_MPU6500}, |
205 | {"mpu9150", INV_MPU9150}, | ||
205 | {} | 206 | {} |
206 | }; | 207 | }; |
207 | 208 | ||
208 | MODULE_DEVICE_TABLE(i2c, inv_mpu_id); | 209 | MODULE_DEVICE_TABLE(i2c, inv_mpu_id); |
209 | 210 | ||
210 | static const struct acpi_device_id inv_acpi_match[] = { | 211 | static const struct acpi_device_id inv_acpi_match[] = { |
211 | {"INVN6500", 0}, | 212 | {"INVN6500", INV_MPU6500}, |
212 | { }, | 213 | { }, |
213 | }; | 214 | }; |
214 | 215 | ||
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h index e302a49703bf..47ca25b94a73 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h | |||
@@ -68,6 +68,7 @@ enum inv_devices { | |||
68 | INV_MPU6050, | 68 | INV_MPU6050, |
69 | INV_MPU6500, | 69 | INV_MPU6500, |
70 | INV_MPU6000, | 70 | INV_MPU6000, |
71 | INV_MPU9150, | ||
71 | INV_NUM_PARTS | 72 | INV_NUM_PARTS |
72 | }; | 73 | }; |
73 | 74 | ||
@@ -93,13 +94,13 @@ struct inv_mpu6050_chip_config { | |||
93 | 94 | ||
94 | /** | 95 | /** |
95 | * struct inv_mpu6050_hw - Other important hardware information. | 96 | * struct inv_mpu6050_hw - Other important hardware information. |
96 | * @num_reg: Number of registers on device. | 97 | * @whoami: Self identification byte from WHO_AM_I register |
97 | * @name: name of the chip. | 98 | * @name: name of the chip. |
98 | * @reg: register map of the chip. | 99 | * @reg: register map of the chip. |
99 | * @config: configuration of the chip. | 100 | * @config: configuration of the chip. |
100 | */ | 101 | */ |
101 | struct inv_mpu6050_hw { | 102 | struct inv_mpu6050_hw { |
102 | u8 num_reg; | 103 | u8 whoami; |
103 | u8 *name; | 104 | u8 *name; |
104 | const struct inv_mpu6050_reg_map *reg; | 105 | const struct inv_mpu6050_reg_map *reg; |
105 | const struct inv_mpu6050_chip_config *config; | 106 | const struct inv_mpu6050_chip_config *config; |
@@ -114,7 +115,8 @@ struct inv_mpu6050_hw { | |||
114 | * @hw: Other hardware-specific information. | 115 | * @hw: Other hardware-specific information. |
115 | * @chip_type: chip type. | 116 | * @chip_type: chip type. |
116 | * @time_stamp_lock: spin lock to time stamp. | 117 | * @time_stamp_lock: spin lock to time stamp. |
117 | * @plat_data: platform data. | 118 | * @plat_data: platform data (deprecated in favor of @orientation). |
119 | * @orientation: sensor chip orientation relative to main hardware. | ||
118 | * @timestamps: kfifo queue to store time stamp. | 120 | * @timestamps: kfifo queue to store time stamp. |
119 | * @map regmap pointer. | 121 | * @map regmap pointer. |
120 | * @irq interrupt number. | 122 | * @irq interrupt number. |
@@ -131,6 +133,7 @@ struct inv_mpu6050_state { | |||
131 | struct i2c_client *mux_client; | 133 | struct i2c_client *mux_client; |
132 | unsigned int powerup_count; | 134 | unsigned int powerup_count; |
133 | struct inv_mpu6050_platform_data plat_data; | 135 | struct inv_mpu6050_platform_data plat_data; |
136 | struct iio_mount_matrix orientation; | ||
134 | DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE); | 137 | DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE); |
135 | struct regmap *map; | 138 | struct regmap *map; |
136 | int irq; | 139 | int irq; |
@@ -215,6 +218,13 @@ struct inv_mpu6050_state { | |||
215 | #define INV_MPU6050_MIN_FIFO_RATE 4 | 218 | #define INV_MPU6050_MIN_FIFO_RATE 4 |
216 | #define INV_MPU6050_ONE_K_HZ 1000 | 219 | #define INV_MPU6050_ONE_K_HZ 1000 |
217 | 220 | ||
221 | #define INV_MPU6050_REG_WHOAMI 117 | ||
222 | |||
223 | #define INV_MPU6000_WHOAMI_VALUE 0x68 | ||
224 | #define INV_MPU6050_WHOAMI_VALUE 0x68 | ||
225 | #define INV_MPU6500_WHOAMI_VALUE 0x70 | ||
226 | #define INV_MPU9150_WHOAMI_VALUE 0x68 | ||
227 | |||
218 | /* scan element definition */ | 228 | /* scan element definition */ |
219 | enum inv_mpu6050_scan { | 229 | enum inv_mpu6050_scan { |
220 | INV_MPU6050_SCAN_ACCL_X, | 230 | INV_MPU6050_SCAN_ACCL_X, |
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c index 7bcb8d839f05..190a4a51c830 100644 --- a/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c +++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_spi.c | |||
@@ -44,9 +44,19 @@ static int inv_mpu_i2c_disable(struct iio_dev *indio_dev) | |||
44 | static int inv_mpu_probe(struct spi_device *spi) | 44 | static int inv_mpu_probe(struct spi_device *spi) |
45 | { | 45 | { |
46 | struct regmap *regmap; | 46 | struct regmap *regmap; |
47 | const struct spi_device_id *id = spi_get_device_id(spi); | 47 | const struct spi_device_id *spi_id; |
48 | const char *name = id ? id->name : NULL; | 48 | const struct acpi_device_id *acpi_id; |
49 | const int chip_type = id ? id->driver_data : 0; | 49 | const char *name = NULL; |
50 | enum inv_devices chip_type; | ||
51 | |||
52 | if ((spi_id = spi_get_device_id(spi))) { | ||
53 | chip_type = (enum inv_devices)spi_id->driver_data; | ||
54 | name = spi_id->name; | ||
55 | } else if ((acpi_id = acpi_match_device(spi->dev.driver->acpi_match_table, &spi->dev))) { | ||
56 | chip_type = (enum inv_devices)acpi_id->driver_data; | ||
57 | } else { | ||
58 | return -ENODEV; | ||
59 | } | ||
50 | 60 | ||
51 | regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config); | 61 | regmap = devm_regmap_init_spi(spi, &inv_mpu_regmap_config); |
52 | if (IS_ERR(regmap)) { | 62 | if (IS_ERR(regmap)) { |
@@ -70,13 +80,15 @@ static int inv_mpu_remove(struct spi_device *spi) | |||
70 | */ | 80 | */ |
71 | static const struct spi_device_id inv_mpu_id[] = { | 81 | static const struct spi_device_id inv_mpu_id[] = { |
72 | {"mpu6000", INV_MPU6000}, | 82 | {"mpu6000", INV_MPU6000}, |
83 | {"mpu6500", INV_MPU6500}, | ||
84 | {"mpu9150", INV_MPU9150}, | ||
73 | {} | 85 | {} |
74 | }; | 86 | }; |
75 | 87 | ||
76 | MODULE_DEVICE_TABLE(spi, inv_mpu_id); | 88 | MODULE_DEVICE_TABLE(spi, inv_mpu_id); |
77 | 89 | ||
78 | static const struct acpi_device_id inv_acpi_match[] = { | 90 | static const struct acpi_device_id inv_acpi_match[] = { |
79 | {"INVN6000", 0}, | 91 | {"INVN6000", INV_MPU6000}, |
80 | { }, | 92 | { }, |
81 | }; | 93 | }; |
82 | MODULE_DEVICE_TABLE(acpi, inv_acpi_match); | 94 | MODULE_DEVICE_TABLE(acpi, inv_acpi_match); |
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 190a5939fd8c..e6319a9346b2 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c | |||
@@ -412,6 +412,88 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev, | |||
412 | } | 412 | } |
413 | EXPORT_SYMBOL_GPL(iio_enum_write); | 413 | EXPORT_SYMBOL_GPL(iio_enum_write); |
414 | 414 | ||
415 | static const struct iio_mount_matrix iio_mount_idmatrix = { | ||
416 | .rotation = { | ||
417 | "1", "0", "0", | ||
418 | "0", "1", "0", | ||
419 | "0", "0", "1" | ||
420 | } | ||
421 | }; | ||
422 | |||
423 | static int iio_setup_mount_idmatrix(const struct device *dev, | ||
424 | struct iio_mount_matrix *matrix) | ||
425 | { | ||
426 | *matrix = iio_mount_idmatrix; | ||
427 | dev_info(dev, "mounting matrix not found: using identity...\n"); | ||
428 | return 0; | ||
429 | } | ||
430 | |||
431 | ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, | ||
432 | const struct iio_chan_spec *chan, char *buf) | ||
433 | { | ||
434 | const struct iio_mount_matrix *mtx = ((iio_get_mount_matrix_t *) | ||
435 | priv)(indio_dev, chan); | ||
436 | |||
437 | if (IS_ERR(mtx)) | ||
438 | return PTR_ERR(mtx); | ||
439 | |||
440 | if (!mtx) | ||
441 | mtx = &iio_mount_idmatrix; | ||
442 | |||
443 | return snprintf(buf, PAGE_SIZE, "%s, %s, %s; %s, %s, %s; %s, %s, %s\n", | ||
444 | mtx->rotation[0], mtx->rotation[1], mtx->rotation[2], | ||
445 | mtx->rotation[3], mtx->rotation[4], mtx->rotation[5], | ||
446 | mtx->rotation[6], mtx->rotation[7], mtx->rotation[8]); | ||
447 | } | ||
448 | EXPORT_SYMBOL_GPL(iio_show_mount_matrix); | ||
449 | |||
450 | /** | ||
451 | * of_iio_read_mount_matrix() - retrieve iio device mounting matrix from | ||
452 | * device-tree "mount-matrix" property | ||
453 | * @dev: device the mounting matrix property is assigned to | ||
454 | * @propname: device specific mounting matrix property name | ||
455 | * @matrix: where to store retrieved matrix | ||
456 | * | ||
457 | * If device is assigned no mounting matrix property, a default 3x3 identity | ||
458 | * matrix will be filled in. | ||
459 | * | ||
460 | * Return: 0 if success, or a negative error code on failure. | ||
461 | */ | ||
462 | #ifdef CONFIG_OF | ||
463 | int of_iio_read_mount_matrix(const struct device *dev, | ||
464 | const char *propname, | ||
465 | struct iio_mount_matrix *matrix) | ||
466 | { | ||
467 | if (dev->of_node) { | ||
468 | int err = of_property_read_string_array(dev->of_node, | ||
469 | propname, matrix->rotation, | ||
470 | ARRAY_SIZE(iio_mount_idmatrix.rotation)); | ||
471 | |||
472 | if (err == ARRAY_SIZE(iio_mount_idmatrix.rotation)) | ||
473 | return 0; | ||
474 | |||
475 | if (err >= 0) | ||
476 | /* Invalid number of matrix entries. */ | ||
477 | return -EINVAL; | ||
478 | |||
479 | if (err != -EINVAL) | ||
480 | /* Invalid matrix declaration format. */ | ||
481 | return err; | ||
482 | } | ||
483 | |||
484 | /* Matrix was not declared at all: fallback to identity. */ | ||
485 | return iio_setup_mount_idmatrix(dev, matrix); | ||
486 | } | ||
487 | #else | ||
488 | int of_iio_read_mount_matrix(const struct device *dev, | ||
489 | const char *propname, | ||
490 | struct iio_mount_matrix *matrix) | ||
491 | { | ||
492 | return iio_setup_mount_idmatrix(dev, matrix); | ||
493 | } | ||
494 | #endif | ||
495 | EXPORT_SYMBOL(of_iio_read_mount_matrix); | ||
496 | |||
415 | /** | 497 | /** |
416 | * iio_format_value() - Formats a IIO value into its string representation | 498 | * iio_format_value() - Formats a IIO value into its string representation |
417 | * @buf: The buffer to which the formatted value gets written | 499 | * @buf: The buffer to which the formatted value gets written |
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c index 734a0042de0c..c4757e6367e7 100644 --- a/drivers/iio/inkern.c +++ b/drivers/iio/inkern.c | |||
@@ -356,6 +356,54 @@ void iio_channel_release(struct iio_channel *channel) | |||
356 | } | 356 | } |
357 | EXPORT_SYMBOL_GPL(iio_channel_release); | 357 | EXPORT_SYMBOL_GPL(iio_channel_release); |
358 | 358 | ||
359 | static void devm_iio_channel_free(struct device *dev, void *res) | ||
360 | { | ||
361 | struct iio_channel *channel = *(struct iio_channel **)res; | ||
362 | |||
363 | iio_channel_release(channel); | ||
364 | } | ||
365 | |||
366 | static int devm_iio_channel_match(struct device *dev, void *res, void *data) | ||
367 | { | ||
368 | struct iio_channel **r = res; | ||
369 | |||
370 | if (!r || !*r) { | ||
371 | WARN_ON(!r || !*r); | ||
372 | return 0; | ||
373 | } | ||
374 | |||
375 | return *r == data; | ||
376 | } | ||
377 | |||
378 | struct iio_channel *devm_iio_channel_get(struct device *dev, | ||
379 | const char *channel_name) | ||
380 | { | ||
381 | struct iio_channel **ptr, *channel; | ||
382 | |||
383 | ptr = devres_alloc(devm_iio_channel_free, sizeof(*ptr), GFP_KERNEL); | ||
384 | if (!ptr) | ||
385 | return ERR_PTR(-ENOMEM); | ||
386 | |||
387 | channel = iio_channel_get(dev, channel_name); | ||
388 | if (IS_ERR(channel)) { | ||
389 | devres_free(ptr); | ||
390 | return channel; | ||
391 | } | ||
392 | |||
393 | *ptr = channel; | ||
394 | devres_add(dev, ptr); | ||
395 | |||
396 | return channel; | ||
397 | } | ||
398 | EXPORT_SYMBOL_GPL(devm_iio_channel_get); | ||
399 | |||
400 | void devm_iio_channel_release(struct device *dev, struct iio_channel *channel) | ||
401 | { | ||
402 | WARN_ON(devres_release(dev, devm_iio_channel_free, | ||
403 | devm_iio_channel_match, channel)); | ||
404 | } | ||
405 | EXPORT_SYMBOL_GPL(devm_iio_channel_release); | ||
406 | |||
359 | struct iio_channel *iio_channel_get_all(struct device *dev) | 407 | struct iio_channel *iio_channel_get_all(struct device *dev) |
360 | { | 408 | { |
361 | const char *name; | 409 | const char *name; |
@@ -441,6 +489,42 @@ void iio_channel_release_all(struct iio_channel *channels) | |||
441 | } | 489 | } |
442 | EXPORT_SYMBOL_GPL(iio_channel_release_all); | 490 | EXPORT_SYMBOL_GPL(iio_channel_release_all); |
443 | 491 | ||
492 | static void devm_iio_channel_free_all(struct device *dev, void *res) | ||
493 | { | ||
494 | struct iio_channel *channels = *(struct iio_channel **)res; | ||
495 | |||
496 | iio_channel_release_all(channels); | ||
497 | } | ||
498 | |||
499 | struct iio_channel *devm_iio_channel_get_all(struct device *dev) | ||
500 | { | ||
501 | struct iio_channel **ptr, *channels; | ||
502 | |||
503 | ptr = devres_alloc(devm_iio_channel_free_all, sizeof(*ptr), GFP_KERNEL); | ||
504 | if (!ptr) | ||
505 | return ERR_PTR(-ENOMEM); | ||
506 | |||
507 | channels = iio_channel_get_all(dev); | ||
508 | if (IS_ERR(channels)) { | ||
509 | devres_free(ptr); | ||
510 | return channels; | ||
511 | } | ||
512 | |||
513 | *ptr = channels; | ||
514 | devres_add(dev, ptr); | ||
515 | |||
516 | return channels; | ||
517 | } | ||
518 | EXPORT_SYMBOL_GPL(devm_iio_channel_get_all); | ||
519 | |||
520 | void devm_iio_channel_release_all(struct device *dev, | ||
521 | struct iio_channel *channels) | ||
522 | { | ||
523 | WARN_ON(devres_release(dev, devm_iio_channel_free_all, | ||
524 | devm_iio_channel_match, channels)); | ||
525 | } | ||
526 | EXPORT_SYMBOL_GPL(devm_iio_channel_release_all); | ||
527 | |||
444 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, | 528 | static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, |
445 | enum iio_chan_info_enum info) | 529 | enum iio_chan_info_enum info) |
446 | { | 530 | { |
@@ -452,7 +536,7 @@ static int iio_channel_read(struct iio_channel *chan, int *val, int *val2, | |||
452 | if (val2 == NULL) | 536 | if (val2 == NULL) |
453 | val2 = &unused; | 537 | val2 = &unused; |
454 | 538 | ||
455 | if(!iio_channel_has_info(chan->channel, info)) | 539 | if (!iio_channel_has_info(chan->channel, info)) |
456 | return -EINVAL; | 540 | return -EINVAL; |
457 | 541 | ||
458 | if (chan->indio_dev->info->read_raw_multi) { | 542 | if (chan->indio_dev->info->read_raw_multi) { |
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig index cfd3df8416bb..7c566f516572 100644 --- a/drivers/iio/light/Kconfig +++ b/drivers/iio/light/Kconfig | |||
@@ -73,6 +73,17 @@ config BH1750 | |||
73 | To compile this driver as a module, choose M here: the module will | 73 | To compile this driver as a module, choose M here: the module will |
74 | be called bh1750. | 74 | be called bh1750. |
75 | 75 | ||
76 | config BH1780 | ||
77 | tristate "ROHM BH1780 ambient light sensor" | ||
78 | depends on I2C | ||
79 | depends on !SENSORS_BH1780 | ||
80 | help | ||
81 | Say Y here to build support for the ROHM BH1780GLI ambient | ||
82 | light sensor. | ||
83 | |||
84 | To compile this driver as a module, choose M here: the module will | ||
85 | be called bh1780. | ||
86 | |||
76 | config CM32181 | 87 | config CM32181 |
77 | depends on I2C | 88 | depends on I2C |
78 | tristate "CM32181 driver" | 89 | tristate "CM32181 driver" |
@@ -223,6 +234,17 @@ config LTR501 | |||
223 | This driver can also be built as a module. If so, the module | 234 | This driver can also be built as a module. If so, the module |
224 | will be called ltr501. | 235 | will be called ltr501. |
225 | 236 | ||
237 | config MAX44000 | ||
238 | tristate "MAX44000 Ambient and Infrared Proximity Sensor" | ||
239 | depends on I2C | ||
240 | select REGMAP_I2C | ||
241 | help | ||
242 | Say Y here if you want to build support for Maxim Integrated's | ||
243 | MAX44000 ambient and infrared proximity sensor device. | ||
244 | |||
245 | To compile this driver as a module, choose M here: | ||
246 | the module will be called max44000. | ||
247 | |||
226 | config OPT3001 | 248 | config OPT3001 |
227 | tristate "Texas Instruments OPT3001 Light Sensor" | 249 | tristate "Texas Instruments OPT3001 Light Sensor" |
228 | depends on I2C | 250 | depends on I2C |
@@ -320,4 +342,14 @@ config VCNL4000 | |||
320 | To compile this driver as a module, choose M here: the | 342 | To compile this driver as a module, choose M here: the |
321 | module will be called vcnl4000. | 343 | module will be called vcnl4000. |
322 | 344 | ||
345 | config VEML6070 | ||
346 | tristate "VEML6070 UV A light sensor" | ||
347 | depends on I2C | ||
348 | help | ||
349 | Say Y here if you want to build a driver for the Vishay VEML6070 UV A | ||
350 | light sensor. | ||
351 | |||
352 | To compile this driver as a module, choose M here: the | ||
353 | module will be called veml6070. | ||
354 | |||
323 | endmenu | 355 | endmenu |
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile index b2c31053db0c..6f2a3c62de27 100644 --- a/drivers/iio/light/Makefile +++ b/drivers/iio/light/Makefile | |||
@@ -9,6 +9,7 @@ obj-$(CONFIG_AL3320A) += al3320a.o | |||
9 | obj-$(CONFIG_APDS9300) += apds9300.o | 9 | obj-$(CONFIG_APDS9300) += apds9300.o |
10 | obj-$(CONFIG_APDS9960) += apds9960.o | 10 | obj-$(CONFIG_APDS9960) += apds9960.o |
11 | obj-$(CONFIG_BH1750) += bh1750.o | 11 | obj-$(CONFIG_BH1750) += bh1750.o |
12 | obj-$(CONFIG_BH1780) += bh1780.o | ||
12 | obj-$(CONFIG_CM32181) += cm32181.o | 13 | obj-$(CONFIG_CM32181) += cm32181.o |
13 | obj-$(CONFIG_CM3232) += cm3232.o | 14 | obj-$(CONFIG_CM3232) += cm3232.o |
14 | obj-$(CONFIG_CM3323) += cm3323.o | 15 | obj-$(CONFIG_CM3323) += cm3323.o |
@@ -20,6 +21,7 @@ obj-$(CONFIG_ISL29125) += isl29125.o | |||
20 | obj-$(CONFIG_JSA1212) += jsa1212.o | 21 | obj-$(CONFIG_JSA1212) += jsa1212.o |
21 | obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o | 22 | obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o |
22 | obj-$(CONFIG_LTR501) += ltr501.o | 23 | obj-$(CONFIG_LTR501) += ltr501.o |
24 | obj-$(CONFIG_MAX44000) += max44000.o | ||
23 | obj-$(CONFIG_OPT3001) += opt3001.o | 25 | obj-$(CONFIG_OPT3001) += opt3001.o |
24 | obj-$(CONFIG_PA12203001) += pa12203001.o | 26 | obj-$(CONFIG_PA12203001) += pa12203001.o |
25 | obj-$(CONFIG_RPR0521) += rpr0521.o | 27 | obj-$(CONFIG_RPR0521) += rpr0521.o |
@@ -30,3 +32,4 @@ obj-$(CONFIG_TCS3472) += tcs3472.o | |||
30 | obj-$(CONFIG_TSL4531) += tsl4531.o | 32 | obj-$(CONFIG_TSL4531) += tsl4531.o |
31 | obj-$(CONFIG_US5182D) += us5182d.o | 33 | obj-$(CONFIG_US5182D) += us5182d.o |
32 | obj-$(CONFIG_VCNL4000) += vcnl4000.o | 34 | obj-$(CONFIG_VCNL4000) += vcnl4000.o |
35 | obj-$(CONFIG_VEML6070) += veml6070.o | ||
diff --git a/drivers/iio/light/apds9960.c b/drivers/iio/light/apds9960.c index a6af56ad10e1..b4dbb3912977 100644 --- a/drivers/iio/light/apds9960.c +++ b/drivers/iio/light/apds9960.c | |||
@@ -321,8 +321,12 @@ static const struct iio_chan_spec apds9960_channels[] = { | |||
321 | }; | 321 | }; |
322 | 322 | ||
323 | /* integration time in us */ | 323 | /* integration time in us */ |
324 | static const int apds9960_int_time[][2] = | 324 | static const int apds9960_int_time[][2] = { |
325 | { {28000, 246}, {100000, 219}, {200000, 182}, {700000, 0} }; | 325 | { 28000, 246}, |
326 | {100000, 219}, | ||
327 | {200000, 182}, | ||
328 | {700000, 0} | ||
329 | }; | ||
326 | 330 | ||
327 | /* gain mapping */ | 331 | /* gain mapping */ |
328 | static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8}; | 332 | static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8}; |
@@ -491,9 +495,10 @@ static int apds9960_read_raw(struct iio_dev *indio_dev, | |||
491 | case IIO_INTENSITY: | 495 | case IIO_INTENSITY: |
492 | ret = regmap_bulk_read(data->regmap, chan->address, | 496 | ret = regmap_bulk_read(data->regmap, chan->address, |
493 | &buf, 2); | 497 | &buf, 2); |
494 | if (!ret) | 498 | if (!ret) { |
495 | ret = IIO_VAL_INT; | 499 | ret = IIO_VAL_INT; |
496 | *val = le16_to_cpu(buf); | 500 | *val = le16_to_cpu(buf); |
501 | } | ||
497 | break; | 502 | break; |
498 | default: | 503 | default: |
499 | ret = -EINVAL; | 504 | ret = -EINVAL; |
diff --git a/drivers/iio/light/bh1780.c b/drivers/iio/light/bh1780.c new file mode 100644 index 000000000000..72b364e4aa72 --- /dev/null +++ b/drivers/iio/light/bh1780.c | |||
@@ -0,0 +1,297 @@ | |||
1 | /* | ||
2 | * ROHM 1780GLI Ambient Light Sensor Driver | ||
3 | * | ||
4 | * Copyright (C) 2016 Linaro Ltd. | ||
5 | * Author: Linus Walleij <linus.walleij@linaro.org> | ||
6 | * Loosely based on the previous BH1780 ALS misc driver | ||
7 | * Copyright (C) 2010 Texas Instruments | ||
8 | * Author: Hemanth V <hemanthv@ti.com> | ||
9 | */ | ||
10 | #include <linux/i2c.h> | ||
11 | #include <linux/slab.h> | ||
12 | #include <linux/platform_device.h> | ||
13 | #include <linux/delay.h> | ||
14 | #include <linux/module.h> | ||
15 | #include <linux/of.h> | ||
16 | #include <linux/pm_runtime.h> | ||
17 | #include <linux/iio/iio.h> | ||
18 | #include <linux/iio/sysfs.h> | ||
19 | #include <linux/bitops.h> | ||
20 | |||
21 | #define BH1780_CMD_BIT BIT(7) | ||
22 | #define BH1780_REG_CONTROL 0x00 | ||
23 | #define BH1780_REG_PARTID 0x0A | ||
24 | #define BH1780_REG_MANFID 0x0B | ||
25 | #define BH1780_REG_DLOW 0x0C | ||
26 | #define BH1780_REG_DHIGH 0x0D | ||
27 | |||
28 | #define BH1780_REVMASK GENMASK(3,0) | ||
29 | #define BH1780_POWMASK GENMASK(1,0) | ||
30 | #define BH1780_POFF (0x0) | ||
31 | #define BH1780_PON (0x3) | ||
32 | |||
33 | /* power on settling time in ms */ | ||
34 | #define BH1780_PON_DELAY 2 | ||
35 | /* max time before value available in ms */ | ||
36 | #define BH1780_INTERVAL 250 | ||
37 | |||
38 | struct bh1780_data { | ||
39 | struct i2c_client *client; | ||
40 | }; | ||
41 | |||
42 | static int bh1780_write(struct bh1780_data *bh1780, u8 reg, u8 val) | ||
43 | { | ||
44 | int ret = i2c_smbus_write_byte_data(bh1780->client, | ||
45 | BH1780_CMD_BIT | reg, | ||
46 | val); | ||
47 | if (ret < 0) | ||
48 | dev_err(&bh1780->client->dev, | ||
49 | "i2c_smbus_write_byte_data failed error " | ||
50 | "%d, register %01x\n", | ||
51 | ret, reg); | ||
52 | return ret; | ||
53 | } | ||
54 | |||
55 | static int bh1780_read(struct bh1780_data *bh1780, u8 reg) | ||
56 | { | ||
57 | int ret = i2c_smbus_read_byte_data(bh1780->client, | ||
58 | BH1780_CMD_BIT | reg); | ||
59 | if (ret < 0) | ||
60 | dev_err(&bh1780->client->dev, | ||
61 | "i2c_smbus_read_byte_data failed error " | ||
62 | "%d, register %01x\n", | ||
63 | ret, reg); | ||
64 | return ret; | ||
65 | } | ||
66 | |||
67 | static int bh1780_read_word(struct bh1780_data *bh1780, u8 reg) | ||
68 | { | ||
69 | int ret = i2c_smbus_read_word_data(bh1780->client, | ||
70 | BH1780_CMD_BIT | reg); | ||
71 | if (ret < 0) | ||
72 | dev_err(&bh1780->client->dev, | ||
73 | "i2c_smbus_read_word_data failed error " | ||
74 | "%d, register %01x\n", | ||
75 | ret, reg); | ||
76 | return ret; | ||
77 | } | ||
78 | |||
79 | static int bh1780_debugfs_reg_access(struct iio_dev *indio_dev, | ||
80 | unsigned int reg, unsigned int writeval, | ||
81 | unsigned int *readval) | ||
82 | { | ||
83 | struct bh1780_data *bh1780 = iio_priv(indio_dev); | ||
84 | int ret; | ||
85 | |||
86 | if (!readval) | ||
87 | bh1780_write(bh1780, (u8)reg, (u8)writeval); | ||
88 | |||
89 | ret = bh1780_read(bh1780, (u8)reg); | ||
90 | if (ret < 0) | ||
91 | return ret; | ||
92 | |||
93 | *readval = ret; | ||
94 | |||
95 | return 0; | ||
96 | } | ||
97 | |||
98 | static int bh1780_read_raw(struct iio_dev *indio_dev, | ||
99 | struct iio_chan_spec const *chan, | ||
100 | int *val, int *val2, long mask) | ||
101 | { | ||
102 | struct bh1780_data *bh1780 = iio_priv(indio_dev); | ||
103 | int value; | ||
104 | |||
105 | switch (mask) { | ||
106 | case IIO_CHAN_INFO_RAW: | ||
107 | switch (chan->type) { | ||
108 | case IIO_LIGHT: | ||
109 | pm_runtime_get_sync(&bh1780->client->dev); | ||
110 | value = bh1780_read_word(bh1780, BH1780_REG_DLOW); | ||
111 | if (value < 0) | ||
112 | return value; | ||
113 | pm_runtime_mark_last_busy(&bh1780->client->dev); | ||
114 | pm_runtime_put_autosuspend(&bh1780->client->dev); | ||
115 | *val = value; | ||
116 | |||
117 | return IIO_VAL_INT; | ||
118 | default: | ||
119 | return -EINVAL; | ||
120 | } | ||
121 | case IIO_CHAN_INFO_INT_TIME: | ||
122 | *val = 0; | ||
123 | *val2 = BH1780_INTERVAL * 1000; | ||
124 | return IIO_VAL_INT_PLUS_MICRO; | ||
125 | default: | ||
126 | return -EINVAL; | ||
127 | } | ||
128 | } | ||
129 | |||
130 | static const struct iio_info bh1780_info = { | ||
131 | .driver_module = THIS_MODULE, | ||
132 | .read_raw = bh1780_read_raw, | ||
133 | .debugfs_reg_access = bh1780_debugfs_reg_access, | ||
134 | }; | ||
135 | |||
136 | static const struct iio_chan_spec bh1780_channels[] = { | ||
137 | { | ||
138 | .type = IIO_LIGHT, | ||
139 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | ||
140 | BIT(IIO_CHAN_INFO_INT_TIME) | ||
141 | } | ||
142 | }; | ||
143 | |||
144 | static int bh1780_probe(struct i2c_client *client, | ||
145 | const struct i2c_device_id *id) | ||
146 | { | ||
147 | int ret; | ||
148 | struct bh1780_data *bh1780; | ||
149 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | ||
150 | struct iio_dev *indio_dev; | ||
151 | |||
152 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) | ||
153 | return -EIO; | ||
154 | |||
155 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*bh1780)); | ||
156 | if (!indio_dev) | ||
157 | return -ENOMEM; | ||
158 | |||
159 | bh1780 = iio_priv(indio_dev); | ||
160 | bh1780->client = client; | ||
161 | i2c_set_clientdata(client, indio_dev); | ||
162 | |||
163 | /* Power up the device */ | ||
164 | ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON); | ||
165 | if (ret < 0) | ||
166 | return ret; | ||
167 | msleep(BH1780_PON_DELAY); | ||
168 | pm_runtime_get_noresume(&client->dev); | ||
169 | pm_runtime_set_active(&client->dev); | ||
170 | pm_runtime_enable(&client->dev); | ||
171 | |||
172 | ret = bh1780_read(bh1780, BH1780_REG_PARTID); | ||
173 | if (ret < 0) | ||
174 | goto out_disable_pm; | ||
175 | dev_info(&client->dev, | ||
176 | "Ambient Light Sensor, Rev : %lu\n", | ||
177 | (ret & BH1780_REVMASK)); | ||
178 | |||
179 | /* | ||
180 | * As the device takes 250 ms to even come up with a fresh | ||
181 | * measurement after power-on, do not shut it down unnecessarily. | ||
182 | * Set autosuspend to a five seconds. | ||
183 | */ | ||
184 | pm_runtime_set_autosuspend_delay(&client->dev, 5000); | ||
185 | pm_runtime_use_autosuspend(&client->dev); | ||
186 | pm_runtime_put(&client->dev); | ||
187 | |||
188 | indio_dev->dev.parent = &client->dev; | ||
189 | indio_dev->info = &bh1780_info; | ||
190 | indio_dev->name = id->name; | ||
191 | indio_dev->channels = bh1780_channels; | ||
192 | indio_dev->num_channels = ARRAY_SIZE(bh1780_channels); | ||
193 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
194 | |||
195 | ret = iio_device_register(indio_dev); | ||
196 | if (ret) | ||
197 | goto out_disable_pm; | ||
198 | return 0; | ||
199 | |||
200 | out_disable_pm: | ||
201 | pm_runtime_put_noidle(&client->dev); | ||
202 | pm_runtime_disable(&client->dev); | ||
203 | return ret; | ||
204 | } | ||
205 | |||
206 | static int bh1780_remove(struct i2c_client *client) | ||
207 | { | ||
208 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
209 | struct bh1780_data *bh1780 = iio_priv(indio_dev); | ||
210 | int ret; | ||
211 | |||
212 | iio_device_unregister(indio_dev); | ||
213 | pm_runtime_get_sync(&client->dev); | ||
214 | pm_runtime_put_noidle(&client->dev); | ||
215 | pm_runtime_disable(&client->dev); | ||
216 | ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF); | ||
217 | if (ret < 0) { | ||
218 | dev_err(&client->dev, "failed to power off\n"); | ||
219 | return ret; | ||
220 | } | ||
221 | |||
222 | return 0; | ||
223 | } | ||
224 | |||
225 | #ifdef CONFIG_PM | ||
226 | static int bh1780_runtime_suspend(struct device *dev) | ||
227 | { | ||
228 | struct i2c_client *client = to_i2c_client(dev); | ||
229 | struct bh1780_data *bh1780 = i2c_get_clientdata(client); | ||
230 | int ret; | ||
231 | |||
232 | ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_POFF); | ||
233 | if (ret < 0) { | ||
234 | dev_err(dev, "failed to runtime suspend\n"); | ||
235 | return ret; | ||
236 | } | ||
237 | |||
238 | return 0; | ||
239 | } | ||
240 | |||
241 | static int bh1780_runtime_resume(struct device *dev) | ||
242 | { | ||
243 | struct i2c_client *client = to_i2c_client(dev); | ||
244 | struct bh1780_data *bh1780 = i2c_get_clientdata(client); | ||
245 | int ret; | ||
246 | |||
247 | ret = bh1780_write(bh1780, BH1780_REG_CONTROL, BH1780_PON); | ||
248 | if (ret < 0) { | ||
249 | dev_err(dev, "failed to runtime resume\n"); | ||
250 | return ret; | ||
251 | } | ||
252 | |||
253 | /* Wait for power on, then for a value to be available */ | ||
254 | msleep(BH1780_PON_DELAY + BH1780_INTERVAL); | ||
255 | |||
256 | return 0; | ||
257 | } | ||
258 | #endif /* CONFIG_PM */ | ||
259 | |||
260 | static const struct dev_pm_ops bh1780_dev_pm_ops = { | ||
261 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, | ||
262 | pm_runtime_force_resume) | ||
263 | SET_RUNTIME_PM_OPS(bh1780_runtime_suspend, | ||
264 | bh1780_runtime_resume, NULL) | ||
265 | }; | ||
266 | |||
267 | static const struct i2c_device_id bh1780_id[] = { | ||
268 | { "bh1780", 0 }, | ||
269 | { }, | ||
270 | }; | ||
271 | |||
272 | MODULE_DEVICE_TABLE(i2c, bh1780_id); | ||
273 | |||
274 | #ifdef CONFIG_OF | ||
275 | static const struct of_device_id of_bh1780_match[] = { | ||
276 | { .compatible = "rohm,bh1780gli", }, | ||
277 | {}, | ||
278 | }; | ||
279 | MODULE_DEVICE_TABLE(of, of_bh1780_match); | ||
280 | #endif | ||
281 | |||
282 | static struct i2c_driver bh1780_driver = { | ||
283 | .probe = bh1780_probe, | ||
284 | .remove = bh1780_remove, | ||
285 | .id_table = bh1780_id, | ||
286 | .driver = { | ||
287 | .name = "bh1780", | ||
288 | .pm = &bh1780_dev_pm_ops, | ||
289 | .of_match_table = of_match_ptr(of_bh1780_match), | ||
290 | }, | ||
291 | }; | ||
292 | |||
293 | module_i2c_driver(bh1780_driver); | ||
294 | |||
295 | MODULE_DESCRIPTION("ROHM BH1780GLI Ambient Light Sensor Driver"); | ||
296 | MODULE_LICENSE("GPL"); | ||
297 | MODULE_AUTHOR("Linus Walleij <linus.walleij@linaro.org>"); | ||
diff --git a/drivers/iio/light/max44000.c b/drivers/iio/light/max44000.c new file mode 100644 index 000000000000..e01e58a9bd14 --- /dev/null +++ b/drivers/iio/light/max44000.c | |||
@@ -0,0 +1,639 @@ | |||
1 | /* | ||
2 | * MAX44000 Ambient and Infrared Proximity Sensor | ||
3 | * | ||
4 | * Copyright (c) 2016, Intel Corporation. | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | * | ||
10 | * Data sheet: https://datasheets.maximintegrated.com/en/ds/MAX44000.pdf | ||
11 | * | ||
12 | * 7-bit I2C slave address 0x4a | ||
13 | */ | ||
14 | |||
15 | #include <linux/module.h> | ||
16 | #include <linux/init.h> | ||
17 | #include <linux/i2c.h> | ||
18 | #include <linux/regmap.h> | ||
19 | #include <linux/util_macros.h> | ||
20 | #include <linux/iio/iio.h> | ||
21 | #include <linux/iio/sysfs.h> | ||
22 | #include <linux/iio/buffer.h> | ||
23 | #include <linux/iio/trigger_consumer.h> | ||
24 | #include <linux/iio/triggered_buffer.h> | ||
25 | #include <linux/acpi.h> | ||
26 | |||
27 | #define MAX44000_DRV_NAME "max44000" | ||
28 | |||
29 | /* Registers in datasheet order */ | ||
30 | #define MAX44000_REG_STATUS 0x00 | ||
31 | #define MAX44000_REG_CFG_MAIN 0x01 | ||
32 | #define MAX44000_REG_CFG_RX 0x02 | ||
33 | #define MAX44000_REG_CFG_TX 0x03 | ||
34 | #define MAX44000_REG_ALS_DATA_HI 0x04 | ||
35 | #define MAX44000_REG_ALS_DATA_LO 0x05 | ||
36 | #define MAX44000_REG_PRX_DATA 0x16 | ||
37 | #define MAX44000_REG_ALS_UPTHR_HI 0x06 | ||
38 | #define MAX44000_REG_ALS_UPTHR_LO 0x07 | ||
39 | #define MAX44000_REG_ALS_LOTHR_HI 0x08 | ||
40 | #define MAX44000_REG_ALS_LOTHR_LO 0x09 | ||
41 | #define MAX44000_REG_PST 0x0a | ||
42 | #define MAX44000_REG_PRX_IND 0x0b | ||
43 | #define MAX44000_REG_PRX_THR 0x0c | ||
44 | #define MAX44000_REG_TRIM_GAIN_GREEN 0x0f | ||
45 | #define MAX44000_REG_TRIM_GAIN_IR 0x10 | ||
46 | |||
47 | /* REG_CFG bits */ | ||
48 | #define MAX44000_CFG_ALSINTE 0x01 | ||
49 | #define MAX44000_CFG_PRXINTE 0x02 | ||
50 | #define MAX44000_CFG_MASK 0x1c | ||
51 | #define MAX44000_CFG_MODE_SHUTDOWN 0x00 | ||
52 | #define MAX44000_CFG_MODE_ALS_GIR 0x04 | ||
53 | #define MAX44000_CFG_MODE_ALS_G 0x08 | ||
54 | #define MAX44000_CFG_MODE_ALS_IR 0x0c | ||
55 | #define MAX44000_CFG_MODE_ALS_PRX 0x10 | ||
56 | #define MAX44000_CFG_MODE_PRX 0x14 | ||
57 | #define MAX44000_CFG_TRIM 0x20 | ||
58 | |||
59 | /* | ||
60 | * Upper 4 bits are not documented but start as 1 on powerup | ||
61 | * Setting them to 0 causes proximity to misbehave so set them to 1 | ||
62 | */ | ||
63 | #define MAX44000_REG_CFG_RX_DEFAULT 0xf0 | ||
64 | |||
65 | /* REG_RX bits */ | ||
66 | #define MAX44000_CFG_RX_ALSTIM_MASK 0x0c | ||
67 | #define MAX44000_CFG_RX_ALSTIM_SHIFT 2 | ||
68 | #define MAX44000_CFG_RX_ALSPGA_MASK 0x03 | ||
69 | #define MAX44000_CFG_RX_ALSPGA_SHIFT 0 | ||
70 | |||
71 | /* REG_TX bits */ | ||
72 | #define MAX44000_LED_CURRENT_MASK 0xf | ||
73 | #define MAX44000_LED_CURRENT_MAX 11 | ||
74 | #define MAX44000_LED_CURRENT_DEFAULT 6 | ||
75 | |||
76 | #define MAX44000_ALSDATA_OVERFLOW 0x4000 | ||
77 | |||
78 | struct max44000_data { | ||
79 | struct mutex lock; | ||
80 | struct regmap *regmap; | ||
81 | }; | ||
82 | |||
83 | /* Default scale is set to the minimum of 0.03125 or 1 / (1 << 5) lux */ | ||
84 | #define MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 5 | ||
85 | |||
86 | /* Scale can be multiplied by up to 128x via ALSPGA for measurement gain */ | ||
87 | static const int max44000_alspga_shift[] = {0, 2, 4, 7}; | ||
88 | #define MAX44000_ALSPGA_MAX_SHIFT 7 | ||
89 | |||
90 | /* | ||
91 | * Scale can be multiplied by up to 64x via ALSTIM because of lost resolution | ||
92 | * | ||
93 | * This scaling factor is hidden from userspace and instead accounted for when | ||
94 | * reading raw values from the device. | ||
95 | * | ||
96 | * This makes it possible to cleanly expose ALSPGA as IIO_CHAN_INFO_SCALE and | ||
97 | * ALSTIM as IIO_CHAN_INFO_INT_TIME without the values affecting each other. | ||
98 | * | ||
99 | * Handling this internally is also required for buffer support because the | ||
100 | * channel's scan_type can't be modified dynamically. | ||
101 | */ | ||
102 | static const int max44000_alstim_shift[] = {0, 2, 4, 6}; | ||
103 | #define MAX44000_ALSTIM_SHIFT(alstim) (2 * (alstim)) | ||
104 | |||
105 | /* Available integration times with pretty manual alignment: */ | ||
106 | static const int max44000_int_time_avail_ns_array[] = { | ||
107 | 100000000, | ||
108 | 25000000, | ||
109 | 6250000, | ||
110 | 1562500, | ||
111 | }; | ||
112 | static const char max44000_int_time_avail_str[] = | ||
113 | "0.100 " | ||
114 | "0.025 " | ||
115 | "0.00625 " | ||
116 | "0.001625"; | ||
117 | |||
118 | /* Available scales (internal to ulux) with pretty manual alignment: */ | ||
119 | static const int max44000_scale_avail_ulux_array[] = { | ||
120 | 31250, | ||
121 | 125000, | ||
122 | 500000, | ||
123 | 4000000, | ||
124 | }; | ||
125 | static const char max44000_scale_avail_str[] = | ||
126 | "0.03125 " | ||
127 | "0.125 " | ||
128 | "0.5 " | ||
129 | "4"; | ||
130 | |||
131 | #define MAX44000_SCAN_INDEX_ALS 0 | ||
132 | #define MAX44000_SCAN_INDEX_PRX 1 | ||
133 | |||
134 | static const struct iio_chan_spec max44000_channels[] = { | ||
135 | { | ||
136 | .type = IIO_LIGHT, | ||
137 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
138 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | | ||
139 | BIT(IIO_CHAN_INFO_INT_TIME), | ||
140 | .scan_index = MAX44000_SCAN_INDEX_ALS, | ||
141 | .scan_type = { | ||
142 | .sign = 'u', | ||
143 | .realbits = 14, | ||
144 | .storagebits = 16, | ||
145 | } | ||
146 | }, | ||
147 | { | ||
148 | .type = IIO_PROXIMITY, | ||
149 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
150 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | ||
151 | .scan_index = MAX44000_SCAN_INDEX_PRX, | ||
152 | .scan_type = { | ||
153 | .sign = 'u', | ||
154 | .realbits = 8, | ||
155 | .storagebits = 16, | ||
156 | } | ||
157 | }, | ||
158 | IIO_CHAN_SOFT_TIMESTAMP(2), | ||
159 | { | ||
160 | .type = IIO_CURRENT, | ||
161 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | | ||
162 | BIT(IIO_CHAN_INFO_SCALE), | ||
163 | .extend_name = "led", | ||
164 | .output = 1, | ||
165 | .scan_index = -1, | ||
166 | }, | ||
167 | }; | ||
168 | |||
169 | static int max44000_read_alstim(struct max44000_data *data) | ||
170 | { | ||
171 | unsigned int val; | ||
172 | int ret; | ||
173 | |||
174 | ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); | ||
175 | if (ret < 0) | ||
176 | return ret; | ||
177 | return (val & MAX44000_CFG_RX_ALSTIM_MASK) >> MAX44000_CFG_RX_ALSTIM_SHIFT; | ||
178 | } | ||
179 | |||
180 | static int max44000_write_alstim(struct max44000_data *data, int val) | ||
181 | { | ||
182 | return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, | ||
183 | MAX44000_CFG_RX_ALSTIM_MASK, | ||
184 | val << MAX44000_CFG_RX_ALSTIM_SHIFT); | ||
185 | } | ||
186 | |||
187 | static int max44000_read_alspga(struct max44000_data *data) | ||
188 | { | ||
189 | unsigned int val; | ||
190 | int ret; | ||
191 | |||
192 | ret = regmap_read(data->regmap, MAX44000_REG_CFG_RX, &val); | ||
193 | if (ret < 0) | ||
194 | return ret; | ||
195 | return (val & MAX44000_CFG_RX_ALSPGA_MASK) >> MAX44000_CFG_RX_ALSPGA_SHIFT; | ||
196 | } | ||
197 | |||
198 | static int max44000_write_alspga(struct max44000_data *data, int val) | ||
199 | { | ||
200 | return regmap_write_bits(data->regmap, MAX44000_REG_CFG_RX, | ||
201 | MAX44000_CFG_RX_ALSPGA_MASK, | ||
202 | val << MAX44000_CFG_RX_ALSPGA_SHIFT); | ||
203 | } | ||
204 | |||
205 | static int max44000_read_alsval(struct max44000_data *data) | ||
206 | { | ||
207 | u16 regval; | ||
208 | int alstim, ret; | ||
209 | |||
210 | ret = regmap_bulk_read(data->regmap, MAX44000_REG_ALS_DATA_HI, | ||
211 | ®val, sizeof(regval)); | ||
212 | if (ret < 0) | ||
213 | return ret; | ||
214 | alstim = ret = max44000_read_alstim(data); | ||
215 | if (ret < 0) | ||
216 | return ret; | ||
217 | |||
218 | regval = be16_to_cpu(regval); | ||
219 | |||
220 | /* | ||
221 | * Overflow is explained on datasheet page 17. | ||
222 | * | ||
223 | * It's a warning that either the G or IR channel has become saturated | ||
224 | * and that the value in the register is likely incorrect. | ||
225 | * | ||
226 | * The recommendation is to change the scale (ALSPGA). | ||
227 | * The driver just returns the max representable value. | ||
228 | */ | ||
229 | if (regval & MAX44000_ALSDATA_OVERFLOW) | ||
230 | return 0x3FFF; | ||
231 | |||
232 | return regval << MAX44000_ALSTIM_SHIFT(alstim); | ||
233 | } | ||
234 | |||
235 | static int max44000_write_led_current_raw(struct max44000_data *data, int val) | ||
236 | { | ||
237 | /* Maybe we should clamp the value instead? */ | ||
238 | if (val < 0 || val > MAX44000_LED_CURRENT_MAX) | ||
239 | return -ERANGE; | ||
240 | if (val >= 8) | ||
241 | val += 4; | ||
242 | return regmap_write_bits(data->regmap, MAX44000_REG_CFG_TX, | ||
243 | MAX44000_LED_CURRENT_MASK, val); | ||
244 | } | ||
245 | |||
246 | static int max44000_read_led_current_raw(struct max44000_data *data) | ||
247 | { | ||
248 | unsigned int regval; | ||
249 | int ret; | ||
250 | |||
251 | ret = regmap_read(data->regmap, MAX44000_REG_CFG_TX, ®val); | ||
252 | if (ret < 0) | ||
253 | return ret; | ||
254 | regval &= MAX44000_LED_CURRENT_MASK; | ||
255 | if (regval >= 8) | ||
256 | regval -= 4; | ||
257 | return regval; | ||
258 | } | ||
259 | |||
260 | static int max44000_read_raw(struct iio_dev *indio_dev, | ||
261 | struct iio_chan_spec const *chan, | ||
262 | int *val, int *val2, long mask) | ||
263 | { | ||
264 | struct max44000_data *data = iio_priv(indio_dev); | ||
265 | int alstim, alspga; | ||
266 | unsigned int regval; | ||
267 | int ret; | ||
268 | |||
269 | switch (mask) { | ||
270 | case IIO_CHAN_INFO_RAW: | ||
271 | switch (chan->type) { | ||
272 | case IIO_LIGHT: | ||
273 | mutex_lock(&data->lock); | ||
274 | ret = max44000_read_alsval(data); | ||
275 | mutex_unlock(&data->lock); | ||
276 | if (ret < 0) | ||
277 | return ret; | ||
278 | *val = ret; | ||
279 | return IIO_VAL_INT; | ||
280 | |||
281 | case IIO_PROXIMITY: | ||
282 | mutex_lock(&data->lock); | ||
283 | ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); | ||
284 | mutex_unlock(&data->lock); | ||
285 | if (ret < 0) | ||
286 | return ret; | ||
287 | *val = regval; | ||
288 | return IIO_VAL_INT; | ||
289 | |||
290 | case IIO_CURRENT: | ||
291 | mutex_lock(&data->lock); | ||
292 | ret = max44000_read_led_current_raw(data); | ||
293 | mutex_unlock(&data->lock); | ||
294 | if (ret < 0) | ||
295 | return ret; | ||
296 | *val = ret; | ||
297 | return IIO_VAL_INT; | ||
298 | |||
299 | default: | ||
300 | return -EINVAL; | ||
301 | } | ||
302 | |||
303 | case IIO_CHAN_INFO_SCALE: | ||
304 | switch (chan->type) { | ||
305 | case IIO_CURRENT: | ||
306 | /* Output register is in 10s of miliamps */ | ||
307 | *val = 10; | ||
308 | return IIO_VAL_INT; | ||
309 | |||
310 | case IIO_LIGHT: | ||
311 | mutex_lock(&data->lock); | ||
312 | alspga = ret = max44000_read_alspga(data); | ||
313 | mutex_unlock(&data->lock); | ||
314 | if (ret < 0) | ||
315 | return ret; | ||
316 | |||
317 | /* Avoid negative shifts */ | ||
318 | *val = (1 << MAX44000_ALSPGA_MAX_SHIFT); | ||
319 | *val2 = MAX44000_ALS_TO_LUX_DEFAULT_FRACTION_LOG2 | ||
320 | + MAX44000_ALSPGA_MAX_SHIFT | ||
321 | - max44000_alspga_shift[alspga]; | ||
322 | return IIO_VAL_FRACTIONAL_LOG2; | ||
323 | |||
324 | default: | ||
325 | return -EINVAL; | ||
326 | } | ||
327 | |||
328 | case IIO_CHAN_INFO_INT_TIME: | ||
329 | mutex_lock(&data->lock); | ||
330 | alstim = ret = max44000_read_alstim(data); | ||
331 | mutex_unlock(&data->lock); | ||
332 | |||
333 | if (ret < 0) | ||
334 | return ret; | ||
335 | *val = 0; | ||
336 | *val2 = max44000_int_time_avail_ns_array[alstim]; | ||
337 | return IIO_VAL_INT_PLUS_NANO; | ||
338 | |||
339 | default: | ||
340 | return -EINVAL; | ||
341 | } | ||
342 | } | ||
343 | |||
344 | static int max44000_write_raw(struct iio_dev *indio_dev, | ||
345 | struct iio_chan_spec const *chan, | ||
346 | int val, int val2, long mask) | ||
347 | { | ||
348 | struct max44000_data *data = iio_priv(indio_dev); | ||
349 | int ret; | ||
350 | |||
351 | if (mask == IIO_CHAN_INFO_RAW && chan->type == IIO_CURRENT) { | ||
352 | mutex_lock(&data->lock); | ||
353 | ret = max44000_write_led_current_raw(data, val); | ||
354 | mutex_unlock(&data->lock); | ||
355 | return ret; | ||
356 | } else if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) { | ||
357 | s64 valns = val * NSEC_PER_SEC + val2; | ||
358 | int alstim = find_closest_descending(valns, | ||
359 | max44000_int_time_avail_ns_array, | ||
360 | ARRAY_SIZE(max44000_int_time_avail_ns_array)); | ||
361 | mutex_lock(&data->lock); | ||
362 | ret = max44000_write_alstim(data, alstim); | ||
363 | mutex_unlock(&data->lock); | ||
364 | return ret; | ||
365 | } else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) { | ||
366 | s64 valus = val * USEC_PER_SEC + val2; | ||
367 | int alspga = find_closest(valus, | ||
368 | max44000_scale_avail_ulux_array, | ||
369 | ARRAY_SIZE(max44000_scale_avail_ulux_array)); | ||
370 | mutex_lock(&data->lock); | ||
371 | ret = max44000_write_alspga(data, alspga); | ||
372 | mutex_unlock(&data->lock); | ||
373 | return ret; | ||
374 | } | ||
375 | |||
376 | return -EINVAL; | ||
377 | } | ||
378 | |||
379 | static int max44000_write_raw_get_fmt(struct iio_dev *indio_dev, | ||
380 | struct iio_chan_spec const *chan, | ||
381 | long mask) | ||
382 | { | ||
383 | if (mask == IIO_CHAN_INFO_INT_TIME && chan->type == IIO_LIGHT) | ||
384 | return IIO_VAL_INT_PLUS_NANO; | ||
385 | else if (mask == IIO_CHAN_INFO_SCALE && chan->type == IIO_LIGHT) | ||
386 | return IIO_VAL_INT_PLUS_MICRO; | ||
387 | else | ||
388 | return IIO_VAL_INT; | ||
389 | } | ||
390 | |||
391 | static IIO_CONST_ATTR(illuminance_integration_time_available, max44000_int_time_avail_str); | ||
392 | static IIO_CONST_ATTR(illuminance_scale_available, max44000_scale_avail_str); | ||
393 | |||
394 | static struct attribute *max44000_attributes[] = { | ||
395 | &iio_const_attr_illuminance_integration_time_available.dev_attr.attr, | ||
396 | &iio_const_attr_illuminance_scale_available.dev_attr.attr, | ||
397 | NULL | ||
398 | }; | ||
399 | |||
400 | static const struct attribute_group max44000_attribute_group = { | ||
401 | .attrs = max44000_attributes, | ||
402 | }; | ||
403 | |||
404 | static const struct iio_info max44000_info = { | ||
405 | .driver_module = THIS_MODULE, | ||
406 | .read_raw = max44000_read_raw, | ||
407 | .write_raw = max44000_write_raw, | ||
408 | .write_raw_get_fmt = max44000_write_raw_get_fmt, | ||
409 | .attrs = &max44000_attribute_group, | ||
410 | }; | ||
411 | |||
412 | static bool max44000_readable_reg(struct device *dev, unsigned int reg) | ||
413 | { | ||
414 | switch (reg) { | ||
415 | case MAX44000_REG_STATUS: | ||
416 | case MAX44000_REG_CFG_MAIN: | ||
417 | case MAX44000_REG_CFG_RX: | ||
418 | case MAX44000_REG_CFG_TX: | ||
419 | case MAX44000_REG_ALS_DATA_HI: | ||
420 | case MAX44000_REG_ALS_DATA_LO: | ||
421 | case MAX44000_REG_PRX_DATA: | ||
422 | case MAX44000_REG_ALS_UPTHR_HI: | ||
423 | case MAX44000_REG_ALS_UPTHR_LO: | ||
424 | case MAX44000_REG_ALS_LOTHR_HI: | ||
425 | case MAX44000_REG_ALS_LOTHR_LO: | ||
426 | case MAX44000_REG_PST: | ||
427 | case MAX44000_REG_PRX_IND: | ||
428 | case MAX44000_REG_PRX_THR: | ||
429 | case MAX44000_REG_TRIM_GAIN_GREEN: | ||
430 | case MAX44000_REG_TRIM_GAIN_IR: | ||
431 | return true; | ||
432 | default: | ||
433 | return false; | ||
434 | } | ||
435 | } | ||
436 | |||
437 | static bool max44000_writeable_reg(struct device *dev, unsigned int reg) | ||
438 | { | ||
439 | switch (reg) { | ||
440 | case MAX44000_REG_CFG_MAIN: | ||
441 | case MAX44000_REG_CFG_RX: | ||
442 | case MAX44000_REG_CFG_TX: | ||
443 | case MAX44000_REG_ALS_UPTHR_HI: | ||
444 | case MAX44000_REG_ALS_UPTHR_LO: | ||
445 | case MAX44000_REG_ALS_LOTHR_HI: | ||
446 | case MAX44000_REG_ALS_LOTHR_LO: | ||
447 | case MAX44000_REG_PST: | ||
448 | case MAX44000_REG_PRX_IND: | ||
449 | case MAX44000_REG_PRX_THR: | ||
450 | case MAX44000_REG_TRIM_GAIN_GREEN: | ||
451 | case MAX44000_REG_TRIM_GAIN_IR: | ||
452 | return true; | ||
453 | default: | ||
454 | return false; | ||
455 | } | ||
456 | } | ||
457 | |||
458 | static bool max44000_volatile_reg(struct device *dev, unsigned int reg) | ||
459 | { | ||
460 | switch (reg) { | ||
461 | case MAX44000_REG_STATUS: | ||
462 | case MAX44000_REG_ALS_DATA_HI: | ||
463 | case MAX44000_REG_ALS_DATA_LO: | ||
464 | case MAX44000_REG_PRX_DATA: | ||
465 | return true; | ||
466 | default: | ||
467 | return false; | ||
468 | } | ||
469 | } | ||
470 | |||
471 | static bool max44000_precious_reg(struct device *dev, unsigned int reg) | ||
472 | { | ||
473 | return reg == MAX44000_REG_STATUS; | ||
474 | } | ||
475 | |||
476 | static const struct regmap_config max44000_regmap_config = { | ||
477 | .reg_bits = 8, | ||
478 | .val_bits = 8, | ||
479 | |||
480 | .max_register = MAX44000_REG_PRX_DATA, | ||
481 | .readable_reg = max44000_readable_reg, | ||
482 | .writeable_reg = max44000_writeable_reg, | ||
483 | .volatile_reg = max44000_volatile_reg, | ||
484 | .precious_reg = max44000_precious_reg, | ||
485 | |||
486 | .use_single_rw = 1, | ||
487 | .cache_type = REGCACHE_RBTREE, | ||
488 | }; | ||
489 | |||
490 | static irqreturn_t max44000_trigger_handler(int irq, void *p) | ||
491 | { | ||
492 | struct iio_poll_func *pf = p; | ||
493 | struct iio_dev *indio_dev = pf->indio_dev; | ||
494 | struct max44000_data *data = iio_priv(indio_dev); | ||
495 | u16 buf[8]; /* 2x u16 + padding + 8 bytes timestamp */ | ||
496 | int index = 0; | ||
497 | unsigned int regval; | ||
498 | int ret; | ||
499 | |||
500 | mutex_lock(&data->lock); | ||
501 | if (test_bit(MAX44000_SCAN_INDEX_ALS, indio_dev->active_scan_mask)) { | ||
502 | ret = max44000_read_alsval(data); | ||
503 | if (ret < 0) | ||
504 | goto out_unlock; | ||
505 | buf[index++] = ret; | ||
506 | } | ||
507 | if (test_bit(MAX44000_SCAN_INDEX_PRX, indio_dev->active_scan_mask)) { | ||
508 | ret = regmap_read(data->regmap, MAX44000_REG_PRX_DATA, ®val); | ||
509 | if (ret < 0) | ||
510 | goto out_unlock; | ||
511 | buf[index] = regval; | ||
512 | } | ||
513 | mutex_unlock(&data->lock); | ||
514 | |||
515 | iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns()); | ||
516 | iio_trigger_notify_done(indio_dev->trig); | ||
517 | return IRQ_HANDLED; | ||
518 | |||
519 | out_unlock: | ||
520 | mutex_unlock(&data->lock); | ||
521 | iio_trigger_notify_done(indio_dev->trig); | ||
522 | return IRQ_HANDLED; | ||
523 | } | ||
524 | |||
525 | static int max44000_probe(struct i2c_client *client, | ||
526 | const struct i2c_device_id *id) | ||
527 | { | ||
528 | struct max44000_data *data; | ||
529 | struct iio_dev *indio_dev; | ||
530 | int ret, reg; | ||
531 | |||
532 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | ||
533 | if (!indio_dev) | ||
534 | return -ENOMEM; | ||
535 | data = iio_priv(indio_dev); | ||
536 | data->regmap = devm_regmap_init_i2c(client, &max44000_regmap_config); | ||
537 | if (IS_ERR(data->regmap)) { | ||
538 | dev_err(&client->dev, "regmap_init failed!\n"); | ||
539 | return PTR_ERR(data->regmap); | ||
540 | } | ||
541 | |||
542 | i2c_set_clientdata(client, indio_dev); | ||
543 | mutex_init(&data->lock); | ||
544 | indio_dev->dev.parent = &client->dev; | ||
545 | indio_dev->info = &max44000_info; | ||
546 | indio_dev->name = MAX44000_DRV_NAME; | ||
547 | indio_dev->channels = max44000_channels; | ||
548 | indio_dev->num_channels = ARRAY_SIZE(max44000_channels); | ||
549 | |||
550 | /* | ||
551 | * The device doesn't have a reset function so we just clear some | ||
552 | * important bits at probe time to ensure sane operation. | ||
553 | * | ||
554 | * Since we don't support interrupts/events the threshold values are | ||
555 | * not important. We also don't touch trim values. | ||
556 | */ | ||
557 | |||
558 | /* Reset ALS scaling bits */ | ||
559 | ret = regmap_write(data->regmap, MAX44000_REG_CFG_RX, | ||
560 | MAX44000_REG_CFG_RX_DEFAULT); | ||
561 | if (ret < 0) { | ||
562 | dev_err(&client->dev, "failed to write default CFG_RX: %d\n", | ||
563 | ret); | ||
564 | return ret; | ||
565 | } | ||
566 | |||
567 | /* | ||
568 | * By default the LED pulse used for the proximity sensor is disabled. | ||
569 | * Set a middle value so that we get some sort of valid data by default. | ||
570 | */ | ||
571 | ret = max44000_write_led_current_raw(data, MAX44000_LED_CURRENT_DEFAULT); | ||
572 | if (ret < 0) { | ||
573 | dev_err(&client->dev, "failed to write init config: %d\n", ret); | ||
574 | return ret; | ||
575 | } | ||
576 | |||
577 | /* Reset CFG bits to ALS_PRX mode which allows easy reading of both values. */ | ||
578 | reg = MAX44000_CFG_TRIM | MAX44000_CFG_MODE_ALS_PRX; | ||
579 | ret = regmap_write(data->regmap, MAX44000_REG_CFG_MAIN, reg); | ||
580 | if (ret < 0) { | ||
581 | dev_err(&client->dev, "failed to write init config: %d\n", ret); | ||
582 | return ret; | ||
583 | } | ||
584 | |||
585 | /* Read status at least once to clear any stale interrupt bits. */ | ||
586 | ret = regmap_read(data->regmap, MAX44000_REG_STATUS, ®); | ||
587 | if (ret < 0) { | ||
588 | dev_err(&client->dev, "failed to read init status: %d\n", ret); | ||
589 | return ret; | ||
590 | } | ||
591 | |||
592 | ret = iio_triggered_buffer_setup(indio_dev, NULL, max44000_trigger_handler, NULL); | ||
593 | if (ret < 0) { | ||
594 | dev_err(&client->dev, "iio triggered buffer setup failed\n"); | ||
595 | return ret; | ||
596 | } | ||
597 | |||
598 | return iio_device_register(indio_dev); | ||
599 | } | ||
600 | |||
601 | static int max44000_remove(struct i2c_client *client) | ||
602 | { | ||
603 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
604 | |||
605 | iio_device_unregister(indio_dev); | ||
606 | iio_triggered_buffer_cleanup(indio_dev); | ||
607 | |||
608 | return 0; | ||
609 | } | ||
610 | |||
611 | static const struct i2c_device_id max44000_id[] = { | ||
612 | {"max44000", 0}, | ||
613 | { } | ||
614 | }; | ||
615 | MODULE_DEVICE_TABLE(i2c, max44000_id); | ||
616 | |||
617 | #ifdef CONFIG_ACPI | ||
618 | static const struct acpi_device_id max44000_acpi_match[] = { | ||
619 | {"MAX44000", 0}, | ||
620 | { } | ||
621 | }; | ||
622 | MODULE_DEVICE_TABLE(acpi, max44000_acpi_match); | ||
623 | #endif | ||
624 | |||
625 | static struct i2c_driver max44000_driver = { | ||
626 | .driver = { | ||
627 | .name = MAX44000_DRV_NAME, | ||
628 | .acpi_match_table = ACPI_PTR(max44000_acpi_match), | ||
629 | }, | ||
630 | .probe = max44000_probe, | ||
631 | .remove = max44000_remove, | ||
632 | .id_table = max44000_id, | ||
633 | }; | ||
634 | |||
635 | module_i2c_driver(max44000_driver); | ||
636 | |||
637 | MODULE_AUTHOR("Crestez Dan Leonard <leonard.crestez@intel.com>"); | ||
638 | MODULE_DESCRIPTION("MAX44000 Ambient and Infrared Proximity Sensor"); | ||
639 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/light/veml6070.c b/drivers/iio/light/veml6070.c new file mode 100644 index 000000000000..bc1c4cb782cd --- /dev/null +++ b/drivers/iio/light/veml6070.c | |||
@@ -0,0 +1,218 @@ | |||
1 | /* | ||
2 | * veml6070.c - Support for Vishay VEML6070 UV A light sensor | ||
3 | * | ||
4 | * Copyright 2016 Peter Meerwald-Stadler <pmeerw@pmeerw.net> | ||
5 | * | ||
6 | * This file is subject to the terms and conditions of version 2 of | ||
7 | * the GNU General Public License. See the file COPYING in the main | ||
8 | * directory of this archive for more details. | ||
9 | * | ||
10 | * IIO driver for VEML6070 (7-bit I2C slave addresses 0x38 and 0x39) | ||
11 | * | ||
12 | * TODO: integration time, ACK signal | ||
13 | */ | ||
14 | |||
15 | #include <linux/module.h> | ||
16 | #include <linux/i2c.h> | ||
17 | #include <linux/mutex.h> | ||
18 | #include <linux/err.h> | ||
19 | #include <linux/delay.h> | ||
20 | |||
21 | #include <linux/iio/iio.h> | ||
22 | #include <linux/iio/sysfs.h> | ||
23 | |||
24 | #define VEML6070_DRV_NAME "veml6070" | ||
25 | |||
26 | #define VEML6070_ADDR_CONFIG_DATA_MSB 0x38 /* read: MSB data, write: config */ | ||
27 | #define VEML6070_ADDR_DATA_LSB 0x39 /* LSB data */ | ||
28 | |||
29 | #define VEML6070_COMMAND_ACK BIT(5) /* raise interrupt when over threshold */ | ||
30 | #define VEML6070_COMMAND_IT GENMASK(3, 2) /* bit mask integration time */ | ||
31 | #define VEML6070_COMMAND_RSRVD BIT(1) /* reserved, set to 1 */ | ||
32 | #define VEML6070_COMMAND_SD BIT(0) /* shutdown mode when set */ | ||
33 | |||
34 | #define VEML6070_IT_10 0x04 /* integration time 1x */ | ||
35 | |||
36 | struct veml6070_data { | ||
37 | struct i2c_client *client1; | ||
38 | struct i2c_client *client2; | ||
39 | u8 config; | ||
40 | struct mutex lock; | ||
41 | }; | ||
42 | |||
43 | static int veml6070_read(struct veml6070_data *data) | ||
44 | { | ||
45 | int ret; | ||
46 | u8 msb, lsb; | ||
47 | |||
48 | mutex_lock(&data->lock); | ||
49 | |||
50 | /* disable shutdown */ | ||
51 | ret = i2c_smbus_write_byte(data->client1, | ||
52 | data->config & ~VEML6070_COMMAND_SD); | ||
53 | if (ret < 0) | ||
54 | goto out; | ||
55 | |||
56 | msleep(125 + 10); /* measurement takes up to 125 ms for IT 1x */ | ||
57 | |||
58 | ret = i2c_smbus_read_byte(data->client2); /* read MSB, address 0x39 */ | ||
59 | if (ret < 0) | ||
60 | goto out; | ||
61 | msb = ret; | ||
62 | |||
63 | ret = i2c_smbus_read_byte(data->client1); /* read LSB, address 0x38 */ | ||
64 | if (ret < 0) | ||
65 | goto out; | ||
66 | lsb = ret; | ||
67 | |||
68 | /* shutdown again */ | ||
69 | ret = i2c_smbus_write_byte(data->client1, data->config); | ||
70 | if (ret < 0) | ||
71 | goto out; | ||
72 | |||
73 | ret = (msb << 8) | lsb; | ||
74 | |||
75 | out: | ||
76 | mutex_unlock(&data->lock); | ||
77 | return ret; | ||
78 | } | ||
79 | |||
80 | static const struct iio_chan_spec veml6070_channels[] = { | ||
81 | { | ||
82 | .type = IIO_INTENSITY, | ||
83 | .modified = 1, | ||
84 | .channel2 = IIO_MOD_LIGHT_UV, | ||
85 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
86 | }, | ||
87 | { | ||
88 | .type = IIO_UVINDEX, | ||
89 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | ||
90 | } | ||
91 | }; | ||
92 | |||
93 | static int veml6070_to_uv_index(unsigned val) | ||
94 | { | ||
95 | /* | ||
96 | * conversion of raw UV intensity values to UV index depends on | ||
97 | * integration time (IT) and value of the resistor connected to | ||
98 | * the RSET pin (default: 270 KOhm) | ||
99 | */ | ||
100 | unsigned uvi[11] = { | ||
101 | 187, 373, 560, /* low */ | ||
102 | 746, 933, 1120, /* moderate */ | ||
103 | 1308, 1494, /* high */ | ||
104 | 1681, 1868, 2054}; /* very high */ | ||
105 | int i; | ||
106 | |||
107 | for (i = 0; i < ARRAY_SIZE(uvi); i++) | ||
108 | if (val <= uvi[i]) | ||
109 | return i; | ||
110 | |||
111 | return 11; /* extreme */ | ||
112 | } | ||
113 | |||
114 | static int veml6070_read_raw(struct iio_dev *indio_dev, | ||
115 | struct iio_chan_spec const *chan, | ||
116 | int *val, int *val2, long mask) | ||
117 | { | ||
118 | struct veml6070_data *data = iio_priv(indio_dev); | ||
119 | int ret; | ||
120 | |||
121 | switch (mask) { | ||
122 | case IIO_CHAN_INFO_RAW: | ||
123 | case IIO_CHAN_INFO_PROCESSED: | ||
124 | ret = veml6070_read(data); | ||
125 | if (ret < 0) | ||
126 | return ret; | ||
127 | if (mask == IIO_CHAN_INFO_PROCESSED) | ||
128 | *val = veml6070_to_uv_index(ret); | ||
129 | else | ||
130 | *val = ret; | ||
131 | return IIO_VAL_INT; | ||
132 | default: | ||
133 | return -EINVAL; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | static const struct iio_info veml6070_info = { | ||
138 | .read_raw = veml6070_read_raw, | ||
139 | .driver_module = THIS_MODULE, | ||
140 | }; | ||
141 | |||
142 | static int veml6070_probe(struct i2c_client *client, | ||
143 | const struct i2c_device_id *id) | ||
144 | { | ||
145 | struct veml6070_data *data; | ||
146 | struct iio_dev *indio_dev; | ||
147 | int ret; | ||
148 | |||
149 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | ||
150 | if (!indio_dev) | ||
151 | return -ENOMEM; | ||
152 | |||
153 | data = iio_priv(indio_dev); | ||
154 | i2c_set_clientdata(client, indio_dev); | ||
155 | data->client1 = client; | ||
156 | mutex_init(&data->lock); | ||
157 | |||
158 | indio_dev->dev.parent = &client->dev; | ||
159 | indio_dev->info = &veml6070_info; | ||
160 | indio_dev->channels = veml6070_channels; | ||
161 | indio_dev->num_channels = ARRAY_SIZE(veml6070_channels); | ||
162 | indio_dev->name = VEML6070_DRV_NAME; | ||
163 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
164 | |||
165 | data->client2 = i2c_new_dummy(client->adapter, VEML6070_ADDR_DATA_LSB); | ||
166 | if (!data->client2) { | ||
167 | dev_err(&client->dev, "i2c device for second chip address failed\n"); | ||
168 | return -ENODEV; | ||
169 | } | ||
170 | |||
171 | data->config = VEML6070_IT_10 | VEML6070_COMMAND_RSRVD | | ||
172 | VEML6070_COMMAND_SD; | ||
173 | ret = i2c_smbus_write_byte(data->client1, data->config); | ||
174 | if (ret < 0) | ||
175 | goto fail; | ||
176 | |||
177 | ret = iio_device_register(indio_dev); | ||
178 | if (ret < 0) | ||
179 | goto fail; | ||
180 | |||
181 | return ret; | ||
182 | |||
183 | fail: | ||
184 | i2c_unregister_device(data->client2); | ||
185 | return ret; | ||
186 | } | ||
187 | |||
188 | static int veml6070_remove(struct i2c_client *client) | ||
189 | { | ||
190 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
191 | struct veml6070_data *data = iio_priv(indio_dev); | ||
192 | |||
193 | iio_device_unregister(indio_dev); | ||
194 | i2c_unregister_device(data->client2); | ||
195 | |||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | static const struct i2c_device_id veml6070_id[] = { | ||
200 | { "veml6070", 0 }, | ||
201 | { } | ||
202 | }; | ||
203 | MODULE_DEVICE_TABLE(i2c, veml6070_id); | ||
204 | |||
205 | static struct i2c_driver veml6070_driver = { | ||
206 | .driver = { | ||
207 | .name = VEML6070_DRV_NAME, | ||
208 | }, | ||
209 | .probe = veml6070_probe, | ||
210 | .remove = veml6070_remove, | ||
211 | .id_table = veml6070_id, | ||
212 | }; | ||
213 | |||
214 | module_i2c_driver(veml6070_driver); | ||
215 | |||
216 | MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>"); | ||
217 | MODULE_DESCRIPTION("Vishay VEML6070 UV A light sensor driver"); | ||
218 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig index 021dc5361f53..84e6559ccc65 100644 --- a/drivers/iio/magnetometer/Kconfig +++ b/drivers/iio/magnetometer/Kconfig | |||
@@ -9,6 +9,8 @@ config AK8975 | |||
9 | tristate "Asahi Kasei AK 3-Axis Magnetometer" | 9 | tristate "Asahi Kasei AK 3-Axis Magnetometer" |
10 | depends on I2C | 10 | depends on I2C |
11 | depends on GPIOLIB || COMPILE_TEST | 11 | depends on GPIOLIB || COMPILE_TEST |
12 | select IIO_BUFFER | ||
13 | select IIO_TRIGGERED_BUFFER | ||
12 | help | 14 | help |
13 | Say yes here to build support for Asahi Kasei AK8975, AK8963, | 15 | Say yes here to build support for Asahi Kasei AK8975, AK8963, |
14 | AK09911 or AK09912 3-Axis Magnetometer. | 16 | AK09911 or AK09912 3-Axis Magnetometer. |
@@ -25,22 +27,41 @@ config AK09911 | |||
25 | Deprecated: AK09911 is now supported by AK8975 driver. | 27 | Deprecated: AK09911 is now supported by AK8975 driver. |
26 | 28 | ||
27 | config BMC150_MAGN | 29 | config BMC150_MAGN |
28 | tristate "Bosch BMC150 Magnetometer Driver" | 30 | tristate |
29 | depends on I2C | ||
30 | select REGMAP_I2C | ||
31 | select IIO_BUFFER | 31 | select IIO_BUFFER |
32 | select IIO_TRIGGERED_BUFFER | 32 | select IIO_TRIGGERED_BUFFER |
33 | |||
34 | config BMC150_MAGN_I2C | ||
35 | tristate "Bosch BMC150 I2C Magnetometer Driver" | ||
36 | depends on I2C | ||
37 | select BMC150_MAGN | ||
38 | select REGMAP_I2C | ||
33 | help | 39 | help |
34 | Say yes here to build support for the BMC150 magnetometer. | 40 | Say yes here to build support for the BMC150 magnetometer with |
41 | I2C interface. | ||
35 | 42 | ||
36 | Currently this only supports the device via an i2c interface. | 43 | This is a combo module with both accelerometer and magnetometer. |
44 | This driver is only implementing magnetometer part, which has | ||
45 | its own address and register map. | ||
46 | |||
47 | To compile this driver as a module, choose M here: the module will be | ||
48 | called bmc150_magn_i2c. | ||
49 | |||
50 | config BMC150_MAGN_SPI | ||
51 | tristate "Bosch BMC150 SPI Magnetometer Driver" | ||
52 | depends on SPI | ||
53 | select BMC150_MAGN | ||
54 | select REGMAP_SPI | ||
55 | help | ||
56 | Say yes here to build support for the BMC150 magnetometer with | ||
57 | SPI interface. | ||
37 | 58 | ||
38 | This is a combo module with both accelerometer and magnetometer. | 59 | This is a combo module with both accelerometer and magnetometer. |
39 | This driver is only implementing magnetometer part, which has | 60 | This driver is only implementing magnetometer part, which has |
40 | its own address and register map. | 61 | its own address and register map. |
41 | 62 | ||
42 | To compile this driver as a module, choose M here: the module will be | 63 | To compile this driver as a module, choose M here: the module will be |
43 | called bmc150_magn. | 64 | called bmc150_magn_spi. |
44 | 65 | ||
45 | config MAG3110 | 66 | config MAG3110 |
46 | tristate "Freescale MAG3110 3-Axis Magnetometer" | 67 | tristate "Freescale MAG3110 3-Axis Magnetometer" |
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile index dd03fe524481..92a745c9a6e8 100644 --- a/drivers/iio/magnetometer/Makefile +++ b/drivers/iio/magnetometer/Makefile | |||
@@ -5,6 +5,9 @@ | |||
5 | # When adding new entries keep the list in alphabetical order | 5 | # When adding new entries keep the list in alphabetical order |
6 | obj-$(CONFIG_AK8975) += ak8975.o | 6 | obj-$(CONFIG_AK8975) += ak8975.o |
7 | obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o | 7 | obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o |
8 | obj-$(CONFIG_BMC150_MAGN_I2C) += bmc150_magn_i2c.o | ||
9 | obj-$(CONFIG_BMC150_MAGN_SPI) += bmc150_magn_spi.o | ||
10 | |||
8 | obj-$(CONFIG_MAG3110) += mag3110.o | 11 | obj-$(CONFIG_MAG3110) += mag3110.o |
9 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o | 12 | obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o |
10 | obj-$(CONFIG_MMC35240) += mmc35240.o | 13 | obj-$(CONFIG_MMC35240) += mmc35240.o |
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c index 48d127a45d90..dbf066129a04 100644 --- a/drivers/iio/magnetometer/ak8975.c +++ b/drivers/iio/magnetometer/ak8975.c | |||
@@ -36,6 +36,13 @@ | |||
36 | 36 | ||
37 | #include <linux/iio/iio.h> | 37 | #include <linux/iio/iio.h> |
38 | #include <linux/iio/sysfs.h> | 38 | #include <linux/iio/sysfs.h> |
39 | #include <linux/iio/buffer.h> | ||
40 | #include <linux/iio/trigger.h> | ||
41 | #include <linux/iio/trigger_consumer.h> | ||
42 | #include <linux/iio/triggered_buffer.h> | ||
43 | |||
44 | #include <linux/iio/magnetometer/ak8975.h> | ||
45 | |||
39 | /* | 46 | /* |
40 | * Register definitions, as well as various shifts and masks to get at the | 47 | * Register definitions, as well as various shifts and masks to get at the |
41 | * individual fields of the registers. | 48 | * individual fields of the registers. |
@@ -370,6 +377,7 @@ struct ak8975_data { | |||
370 | wait_queue_head_t data_ready_queue; | 377 | wait_queue_head_t data_ready_queue; |
371 | unsigned long flags; | 378 | unsigned long flags; |
372 | u8 cntl_cache; | 379 | u8 cntl_cache; |
380 | struct iio_mount_matrix orientation; | ||
373 | struct regulator *vdd; | 381 | struct regulator *vdd; |
374 | }; | 382 | }; |
375 | 383 | ||
@@ -633,22 +641,15 @@ static int wait_conversion_complete_interrupt(struct ak8975_data *data) | |||
633 | return ret > 0 ? 0 : -ETIME; | 641 | return ret > 0 ? 0 : -ETIME; |
634 | } | 642 | } |
635 | 643 | ||
636 | /* | 644 | static int ak8975_start_read_axis(struct ak8975_data *data, |
637 | * Emits the raw flux value for the x, y, or z axis. | 645 | const struct i2c_client *client) |
638 | */ | ||
639 | static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) | ||
640 | { | 646 | { |
641 | struct ak8975_data *data = iio_priv(indio_dev); | ||
642 | struct i2c_client *client = data->client; | ||
643 | int ret; | ||
644 | |||
645 | mutex_lock(&data->lock); | ||
646 | |||
647 | /* Set up the device for taking a sample. */ | 647 | /* Set up the device for taking a sample. */ |
648 | ret = ak8975_set_mode(data, MODE_ONCE); | 648 | int ret = ak8975_set_mode(data, MODE_ONCE); |
649 | |||
649 | if (ret < 0) { | 650 | if (ret < 0) { |
650 | dev_err(&client->dev, "Error in setting operating mode\n"); | 651 | dev_err(&client->dev, "Error in setting operating mode\n"); |
651 | goto exit; | 652 | return ret; |
652 | } | 653 | } |
653 | 654 | ||
654 | /* Wait for the conversion to complete. */ | 655 | /* Wait for the conversion to complete. */ |
@@ -659,7 +660,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) | |||
659 | else | 660 | else |
660 | ret = wait_conversion_complete_polled(data); | 661 | ret = wait_conversion_complete_polled(data); |
661 | if (ret < 0) | 662 | if (ret < 0) |
662 | goto exit; | 663 | return ret; |
663 | 664 | ||
664 | /* This will be executed only for non-interrupt based waiting case */ | 665 | /* This will be executed only for non-interrupt based waiting case */ |
665 | if (ret & data->def->ctrl_masks[ST1_DRDY]) { | 666 | if (ret & data->def->ctrl_masks[ST1_DRDY]) { |
@@ -667,32 +668,45 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) | |||
667 | data->def->ctrl_regs[ST2]); | 668 | data->def->ctrl_regs[ST2]); |
668 | if (ret < 0) { | 669 | if (ret < 0) { |
669 | dev_err(&client->dev, "Error in reading ST2\n"); | 670 | dev_err(&client->dev, "Error in reading ST2\n"); |
670 | goto exit; | 671 | return ret; |
671 | } | 672 | } |
672 | if (ret & (data->def->ctrl_masks[ST2_DERR] | | 673 | if (ret & (data->def->ctrl_masks[ST2_DERR] | |
673 | data->def->ctrl_masks[ST2_HOFL])) { | 674 | data->def->ctrl_masks[ST2_HOFL])) { |
674 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); | 675 | dev_err(&client->dev, "ST2 status error 0x%x\n", ret); |
675 | ret = -EINVAL; | 676 | return -EINVAL; |
676 | goto exit; | ||
677 | } | 677 | } |
678 | } | 678 | } |
679 | 679 | ||
680 | /* Read the flux value from the appropriate register | 680 | return 0; |
681 | (the register is specified in the iio device attributes). */ | 681 | } |
682 | ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]); | 682 | |
683 | if (ret < 0) { | 683 | /* Retrieve raw flux value for one of the x, y, or z axis. */ |
684 | dev_err(&client->dev, "Read axis data fails\n"); | 684 | static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val) |
685 | { | ||
686 | struct ak8975_data *data = iio_priv(indio_dev); | ||
687 | const struct i2c_client *client = data->client; | ||
688 | const struct ak_def *def = data->def; | ||
689 | int ret; | ||
690 | |||
691 | mutex_lock(&data->lock); | ||
692 | |||
693 | ret = ak8975_start_read_axis(data, client); | ||
694 | if (ret) | ||
695 | goto exit; | ||
696 | |||
697 | ret = i2c_smbus_read_word_data(client, def->data_regs[index]); | ||
698 | if (ret < 0) | ||
685 | goto exit; | 699 | goto exit; |
686 | } | ||
687 | 700 | ||
688 | mutex_unlock(&data->lock); | 701 | mutex_unlock(&data->lock); |
689 | 702 | ||
690 | /* Clamp to valid range. */ | 703 | /* Clamp to valid range. */ |
691 | *val = clamp_t(s16, ret, -data->def->range, data->def->range); | 704 | *val = clamp_t(s16, ret, -def->range, def->range); |
692 | return IIO_VAL_INT; | 705 | return IIO_VAL_INT; |
693 | 706 | ||
694 | exit: | 707 | exit: |
695 | mutex_unlock(&data->lock); | 708 | mutex_unlock(&data->lock); |
709 | dev_err(&client->dev, "Error in reading axis\n"); | ||
696 | return ret; | 710 | return ret; |
697 | } | 711 | } |
698 | 712 | ||
@@ -714,6 +728,18 @@ static int ak8975_read_raw(struct iio_dev *indio_dev, | |||
714 | return -EINVAL; | 728 | return -EINVAL; |
715 | } | 729 | } |
716 | 730 | ||
731 | static const struct iio_mount_matrix * | ||
732 | ak8975_get_mount_matrix(const struct iio_dev *indio_dev, | ||
733 | const struct iio_chan_spec *chan) | ||
734 | { | ||
735 | return &((struct ak8975_data *)iio_priv(indio_dev))->orientation; | ||
736 | } | ||
737 | |||
738 | static const struct iio_chan_spec_ext_info ak8975_ext_info[] = { | ||
739 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix), | ||
740 | { }, | ||
741 | }; | ||
742 | |||
717 | #define AK8975_CHANNEL(axis, index) \ | 743 | #define AK8975_CHANNEL(axis, index) \ |
718 | { \ | 744 | { \ |
719 | .type = IIO_MAGN, \ | 745 | .type = IIO_MAGN, \ |
@@ -722,12 +748,23 @@ static int ak8975_read_raw(struct iio_dev *indio_dev, | |||
722 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ | 748 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \ |
723 | BIT(IIO_CHAN_INFO_SCALE), \ | 749 | BIT(IIO_CHAN_INFO_SCALE), \ |
724 | .address = index, \ | 750 | .address = index, \ |
751 | .scan_index = index, \ | ||
752 | .scan_type = { \ | ||
753 | .sign = 's', \ | ||
754 | .realbits = 16, \ | ||
755 | .storagebits = 16, \ | ||
756 | .endianness = IIO_CPU \ | ||
757 | }, \ | ||
758 | .ext_info = ak8975_ext_info, \ | ||
725 | } | 759 | } |
726 | 760 | ||
727 | static const struct iio_chan_spec ak8975_channels[] = { | 761 | static const struct iio_chan_spec ak8975_channels[] = { |
728 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), | 762 | AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2), |
763 | IIO_CHAN_SOFT_TIMESTAMP(3), | ||
729 | }; | 764 | }; |
730 | 765 | ||
766 | static const unsigned long ak8975_scan_masks[] = { 0x7, 0 }; | ||
767 | |||
731 | static const struct iio_info ak8975_info = { | 768 | static const struct iio_info ak8975_info = { |
732 | .read_raw = &ak8975_read_raw, | 769 | .read_raw = &ak8975_read_raw, |
733 | .driver_module = THIS_MODULE, | 770 | .driver_module = THIS_MODULE, |
@@ -756,6 +793,56 @@ static const char *ak8975_match_acpi_device(struct device *dev, | |||
756 | return dev_name(dev); | 793 | return dev_name(dev); |
757 | } | 794 | } |
758 | 795 | ||
796 | static void ak8975_fill_buffer(struct iio_dev *indio_dev) | ||
797 | { | ||
798 | struct ak8975_data *data = iio_priv(indio_dev); | ||
799 | const struct i2c_client *client = data->client; | ||
800 | const struct ak_def *def = data->def; | ||
801 | int ret; | ||
802 | s16 buff[8]; /* 3 x 16 bits axis values + 1 aligned 64 bits timestamp */ | ||
803 | |||
804 | mutex_lock(&data->lock); | ||
805 | |||
806 | ret = ak8975_start_read_axis(data, client); | ||
807 | if (ret) | ||
808 | goto unlock; | ||
809 | |||
810 | /* | ||
811 | * For each axis, read the flux value from the appropriate register | ||
812 | * (the register is specified in the iio device attributes). | ||
813 | */ | ||
814 | ret = i2c_smbus_read_i2c_block_data_or_emulated(client, | ||
815 | def->data_regs[0], | ||
816 | 3 * sizeof(buff[0]), | ||
817 | (u8 *)buff); | ||
818 | if (ret < 0) | ||
819 | goto unlock; | ||
820 | |||
821 | mutex_unlock(&data->lock); | ||
822 | |||
823 | /* Clamp to valid range. */ | ||
824 | buff[0] = clamp_t(s16, le16_to_cpu(buff[0]), -def->range, def->range); | ||
825 | buff[1] = clamp_t(s16, le16_to_cpu(buff[1]), -def->range, def->range); | ||
826 | buff[2] = clamp_t(s16, le16_to_cpu(buff[2]), -def->range, def->range); | ||
827 | |||
828 | iio_push_to_buffers_with_timestamp(indio_dev, buff, iio_get_time_ns()); | ||
829 | return; | ||
830 | |||
831 | unlock: | ||
832 | mutex_unlock(&data->lock); | ||
833 | dev_err(&client->dev, "Error in reading axes block\n"); | ||
834 | } | ||
835 | |||
836 | static irqreturn_t ak8975_handle_trigger(int irq, void *p) | ||
837 | { | ||
838 | const struct iio_poll_func *pf = p; | ||
839 | struct iio_dev *indio_dev = pf->indio_dev; | ||
840 | |||
841 | ak8975_fill_buffer(indio_dev); | ||
842 | iio_trigger_notify_done(indio_dev->trig); | ||
843 | return IRQ_HANDLED; | ||
844 | } | ||
845 | |||
759 | static int ak8975_probe(struct i2c_client *client, | 846 | static int ak8975_probe(struct i2c_client *client, |
760 | const struct i2c_device_id *id) | 847 | const struct i2c_device_id *id) |
761 | { | 848 | { |
@@ -765,10 +852,12 @@ static int ak8975_probe(struct i2c_client *client, | |||
765 | int err; | 852 | int err; |
766 | const char *name = NULL; | 853 | const char *name = NULL; |
767 | enum asahi_compass_chipset chipset; | 854 | enum asahi_compass_chipset chipset; |
855 | const struct ak8975_platform_data *pdata = | ||
856 | dev_get_platdata(&client->dev); | ||
768 | 857 | ||
769 | /* Grab and set up the supplied GPIO. */ | 858 | /* Grab and set up the supplied GPIO. */ |
770 | if (client->dev.platform_data) | 859 | if (pdata) |
771 | eoc_gpio = *(int *)(client->dev.platform_data); | 860 | eoc_gpio = pdata->eoc_gpio; |
772 | else if (client->dev.of_node) | 861 | else if (client->dev.of_node) |
773 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); | 862 | eoc_gpio = of_get_gpio(client->dev.of_node, 0); |
774 | else | 863 | else |
@@ -802,6 +891,15 @@ static int ak8975_probe(struct i2c_client *client, | |||
802 | data->eoc_gpio = eoc_gpio; | 891 | data->eoc_gpio = eoc_gpio; |
803 | data->eoc_irq = 0; | 892 | data->eoc_irq = 0; |
804 | 893 | ||
894 | if (!pdata) { | ||
895 | err = of_iio_read_mount_matrix(&client->dev, | ||
896 | "mount-matrix", | ||
897 | &data->orientation); | ||
898 | if (err) | ||
899 | return err; | ||
900 | } else | ||
901 | data->orientation = pdata->orientation; | ||
902 | |||
805 | /* id will be NULL when enumerated via ACPI */ | 903 | /* id will be NULL when enumerated via ACPI */ |
806 | if (id) { | 904 | if (id) { |
807 | chipset = (enum asahi_compass_chipset)(id->driver_data); | 905 | chipset = (enum asahi_compass_chipset)(id->driver_data); |
@@ -810,8 +908,7 @@ static int ak8975_probe(struct i2c_client *client, | |||
810 | name = ak8975_match_acpi_device(&client->dev, &chipset); | 908 | name = ak8975_match_acpi_device(&client->dev, &chipset); |
811 | if (!name) | 909 | if (!name) |
812 | return -ENODEV; | 910 | return -ENODEV; |
813 | } | 911 | } else |
814 | else | ||
815 | return -ENOSYS; | 912 | return -ENOSYS; |
816 | 913 | ||
817 | if (chipset >= AK_MAX_TYPE) { | 914 | if (chipset >= AK_MAX_TYPE) { |
@@ -845,15 +942,27 @@ static int ak8975_probe(struct i2c_client *client, | |||
845 | indio_dev->channels = ak8975_channels; | 942 | indio_dev->channels = ak8975_channels; |
846 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); | 943 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); |
847 | indio_dev->info = &ak8975_info; | 944 | indio_dev->info = &ak8975_info; |
945 | indio_dev->available_scan_masks = ak8975_scan_masks; | ||
848 | indio_dev->modes = INDIO_DIRECT_MODE; | 946 | indio_dev->modes = INDIO_DIRECT_MODE; |
849 | indio_dev->name = name; | 947 | indio_dev->name = name; |
850 | 948 | ||
851 | err = iio_device_register(indio_dev); | 949 | err = iio_triggered_buffer_setup(indio_dev, NULL, ak8975_handle_trigger, |
852 | if (err) | 950 | NULL); |
951 | if (err) { | ||
952 | dev_err(&client->dev, "triggered buffer setup failed\n"); | ||
853 | goto power_off; | 953 | goto power_off; |
954 | } | ||
955 | |||
956 | err = iio_device_register(indio_dev); | ||
957 | if (err) { | ||
958 | dev_err(&client->dev, "device register failed\n"); | ||
959 | goto cleanup_buffer; | ||
960 | } | ||
854 | 961 | ||
855 | return 0; | 962 | return 0; |
856 | 963 | ||
964 | cleanup_buffer: | ||
965 | iio_triggered_buffer_cleanup(indio_dev); | ||
857 | power_off: | 966 | power_off: |
858 | ak8975_power_off(client); | 967 | ak8975_power_off(client); |
859 | return err; | 968 | return err; |
@@ -864,6 +973,7 @@ static int ak8975_remove(struct i2c_client *client) | |||
864 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | 973 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
865 | 974 | ||
866 | iio_device_unregister(indio_dev); | 975 | iio_device_unregister(indio_dev); |
976 | iio_triggered_buffer_cleanup(indio_dev); | ||
867 | ak8975_power_off(client); | 977 | ak8975_power_off(client); |
868 | 978 | ||
869 | return 0; | 979 | return 0; |
diff --git a/drivers/iio/magnetometer/bmc150_magn.c b/drivers/iio/magnetometer/bmc150_magn.c index 0e9da189dc4c..d104fb8d9379 100644 --- a/drivers/iio/magnetometer/bmc150_magn.c +++ b/drivers/iio/magnetometer/bmc150_magn.c | |||
@@ -34,6 +34,8 @@ | |||
34 | #include <linux/iio/triggered_buffer.h> | 34 | #include <linux/iio/triggered_buffer.h> |
35 | #include <linux/regmap.h> | 35 | #include <linux/regmap.h> |
36 | 36 | ||
37 | #include "bmc150_magn.h" | ||
38 | |||
37 | #define BMC150_MAGN_DRV_NAME "bmc150_magn" | 39 | #define BMC150_MAGN_DRV_NAME "bmc150_magn" |
38 | #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event" | 40 | #define BMC150_MAGN_IRQ_NAME "bmc150_magn_event" |
39 | 41 | ||
@@ -134,7 +136,7 @@ struct bmc150_magn_trim_regs { | |||
134 | } __packed; | 136 | } __packed; |
135 | 137 | ||
136 | struct bmc150_magn_data { | 138 | struct bmc150_magn_data { |
137 | struct i2c_client *client; | 139 | struct device *dev; |
138 | /* | 140 | /* |
139 | * 1. Protect this structure. | 141 | * 1. Protect this structure. |
140 | * 2. Serialize sequences that power on/off the device and access HW. | 142 | * 2. Serialize sequences that power on/off the device and access HW. |
@@ -146,6 +148,7 @@ struct bmc150_magn_data { | |||
146 | struct iio_trigger *dready_trig; | 148 | struct iio_trigger *dready_trig; |
147 | bool dready_trigger_on; | 149 | bool dready_trigger_on; |
148 | int max_odr; | 150 | int max_odr; |
151 | int irq; | ||
149 | }; | 152 | }; |
150 | 153 | ||
151 | static const struct { | 154 | static const struct { |
@@ -215,7 +218,7 @@ static bool bmc150_magn_is_volatile_reg(struct device *dev, unsigned int reg) | |||
215 | } | 218 | } |
216 | } | 219 | } |
217 | 220 | ||
218 | static const struct regmap_config bmc150_magn_regmap_config = { | 221 | const struct regmap_config bmc150_magn_regmap_config = { |
219 | .reg_bits = 8, | 222 | .reg_bits = 8, |
220 | .val_bits = 8, | 223 | .val_bits = 8, |
221 | 224 | ||
@@ -225,6 +228,7 @@ static const struct regmap_config bmc150_magn_regmap_config = { | |||
225 | .writeable_reg = bmc150_magn_is_writeable_reg, | 228 | .writeable_reg = bmc150_magn_is_writeable_reg, |
226 | .volatile_reg = bmc150_magn_is_volatile_reg, | 229 | .volatile_reg = bmc150_magn_is_volatile_reg, |
227 | }; | 230 | }; |
231 | EXPORT_SYMBOL(bmc150_magn_regmap_config); | ||
228 | 232 | ||
229 | static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, | 233 | static int bmc150_magn_set_power_mode(struct bmc150_magn_data *data, |
230 | enum bmc150_magn_power_modes mode, | 234 | enum bmc150_magn_power_modes mode, |
@@ -263,17 +267,17 @@ static int bmc150_magn_set_power_state(struct bmc150_magn_data *data, bool on) | |||
263 | int ret; | 267 | int ret; |
264 | 268 | ||
265 | if (on) { | 269 | if (on) { |
266 | ret = pm_runtime_get_sync(&data->client->dev); | 270 | ret = pm_runtime_get_sync(data->dev); |
267 | } else { | 271 | } else { |
268 | pm_runtime_mark_last_busy(&data->client->dev); | 272 | pm_runtime_mark_last_busy(data->dev); |
269 | ret = pm_runtime_put_autosuspend(&data->client->dev); | 273 | ret = pm_runtime_put_autosuspend(data->dev); |
270 | } | 274 | } |
271 | 275 | ||
272 | if (ret < 0) { | 276 | if (ret < 0) { |
273 | dev_err(&data->client->dev, | 277 | dev_err(data->dev, |
274 | "failed to change power state to %d\n", on); | 278 | "failed to change power state to %d\n", on); |
275 | if (on) | 279 | if (on) |
276 | pm_runtime_put_noidle(&data->client->dev); | 280 | pm_runtime_put_noidle(data->dev); |
277 | 281 | ||
278 | return ret; | 282 | return ret; |
279 | } | 283 | } |
@@ -350,7 +354,7 @@ static int bmc150_magn_set_max_odr(struct bmc150_magn_data *data, int rep_xy, | |||
350 | /* the maximum selectable read-out frequency from datasheet */ | 354 | /* the maximum selectable read-out frequency from datasheet */ |
351 | max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980); | 355 | max_odr = 1000000 / (145 * rep_xy + 500 * rep_z + 980); |
352 | if (odr > max_odr) { | 356 | if (odr > max_odr) { |
353 | dev_err(&data->client->dev, | 357 | dev_err(data->dev, |
354 | "Can't set oversampling with sampling freq %d\n", | 358 | "Can't set oversampling with sampling freq %d\n", |
355 | odr); | 359 | odr); |
356 | return -EINVAL; | 360 | return -EINVAL; |
@@ -684,27 +688,27 @@ static int bmc150_magn_init(struct bmc150_magn_data *data) | |||
684 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, | 688 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, |
685 | false); | 689 | false); |
686 | if (ret < 0) { | 690 | if (ret < 0) { |
687 | dev_err(&data->client->dev, | 691 | dev_err(data->dev, |
688 | "Failed to bring up device from suspend mode\n"); | 692 | "Failed to bring up device from suspend mode\n"); |
689 | return ret; | 693 | return ret; |
690 | } | 694 | } |
691 | 695 | ||
692 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); | 696 | ret = regmap_read(data->regmap, BMC150_MAGN_REG_CHIP_ID, &chip_id); |
693 | if (ret < 0) { | 697 | if (ret < 0) { |
694 | dev_err(&data->client->dev, "Failed reading chip id\n"); | 698 | dev_err(data->dev, "Failed reading chip id\n"); |
695 | goto err_poweroff; | 699 | goto err_poweroff; |
696 | } | 700 | } |
697 | if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { | 701 | if (chip_id != BMC150_MAGN_CHIP_ID_VAL) { |
698 | dev_err(&data->client->dev, "Invalid chip id 0x%x\n", chip_id); | 702 | dev_err(data->dev, "Invalid chip id 0x%x\n", chip_id); |
699 | ret = -ENODEV; | 703 | ret = -ENODEV; |
700 | goto err_poweroff; | 704 | goto err_poweroff; |
701 | } | 705 | } |
702 | dev_dbg(&data->client->dev, "Chip id %x\n", chip_id); | 706 | dev_dbg(data->dev, "Chip id %x\n", chip_id); |
703 | 707 | ||
704 | preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; | 708 | preset = bmc150_magn_presets_table[BMC150_MAGN_DEFAULT_PRESET]; |
705 | ret = bmc150_magn_set_odr(data, preset.odr); | 709 | ret = bmc150_magn_set_odr(data, preset.odr); |
706 | if (ret < 0) { | 710 | if (ret < 0) { |
707 | dev_err(&data->client->dev, "Failed to set ODR to %d\n", | 711 | dev_err(data->dev, "Failed to set ODR to %d\n", |
708 | preset.odr); | 712 | preset.odr); |
709 | goto err_poweroff; | 713 | goto err_poweroff; |
710 | } | 714 | } |
@@ -712,7 +716,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data) | |||
712 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, | 716 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_XY, |
713 | BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); | 717 | BMC150_MAGN_REPXY_TO_REGVAL(preset.rep_xy)); |
714 | if (ret < 0) { | 718 | if (ret < 0) { |
715 | dev_err(&data->client->dev, "Failed to set REP XY to %d\n", | 719 | dev_err(data->dev, "Failed to set REP XY to %d\n", |
716 | preset.rep_xy); | 720 | preset.rep_xy); |
717 | goto err_poweroff; | 721 | goto err_poweroff; |
718 | } | 722 | } |
@@ -720,7 +724,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data) | |||
720 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, | 724 | ret = regmap_write(data->regmap, BMC150_MAGN_REG_REP_Z, |
721 | BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); | 725 | BMC150_MAGN_REPZ_TO_REGVAL(preset.rep_z)); |
722 | if (ret < 0) { | 726 | if (ret < 0) { |
723 | dev_err(&data->client->dev, "Failed to set REP Z to %d\n", | 727 | dev_err(data->dev, "Failed to set REP Z to %d\n", |
724 | preset.rep_z); | 728 | preset.rep_z); |
725 | goto err_poweroff; | 729 | goto err_poweroff; |
726 | } | 730 | } |
@@ -733,7 +737,7 @@ static int bmc150_magn_init(struct bmc150_magn_data *data) | |||
733 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, | 737 | ret = bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, |
734 | true); | 738 | true); |
735 | if (ret < 0) { | 739 | if (ret < 0) { |
736 | dev_err(&data->client->dev, "Failed to power on device\n"); | 740 | dev_err(data->dev, "Failed to power on device\n"); |
737 | goto err_poweroff; | 741 | goto err_poweroff; |
738 | } | 742 | } |
739 | 743 | ||
@@ -842,41 +846,33 @@ static const char *bmc150_magn_match_acpi_device(struct device *dev) | |||
842 | return dev_name(dev); | 846 | return dev_name(dev); |
843 | } | 847 | } |
844 | 848 | ||
845 | static int bmc150_magn_probe(struct i2c_client *client, | 849 | int bmc150_magn_probe(struct device *dev, struct regmap *regmap, |
846 | const struct i2c_device_id *id) | 850 | int irq, const char *name) |
847 | { | 851 | { |
848 | struct bmc150_magn_data *data; | 852 | struct bmc150_magn_data *data; |
849 | struct iio_dev *indio_dev; | 853 | struct iio_dev *indio_dev; |
850 | const char *name = NULL; | ||
851 | int ret; | 854 | int ret; |
852 | 855 | ||
853 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | 856 | indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); |
854 | if (!indio_dev) | 857 | if (!indio_dev) |
855 | return -ENOMEM; | 858 | return -ENOMEM; |
856 | 859 | ||
857 | data = iio_priv(indio_dev); | 860 | data = iio_priv(indio_dev); |
858 | i2c_set_clientdata(client, indio_dev); | 861 | dev_set_drvdata(dev, indio_dev); |
859 | data->client = client; | 862 | data->regmap = regmap; |
863 | data->irq = irq; | ||
864 | data->dev = dev; | ||
860 | 865 | ||
861 | if (id) | 866 | if (!name && ACPI_HANDLE(dev)) |
862 | name = id->name; | 867 | name = bmc150_magn_match_acpi_device(dev); |
863 | else if (ACPI_HANDLE(&client->dev)) | ||
864 | name = bmc150_magn_match_acpi_device(&client->dev); | ||
865 | else | ||
866 | return -ENOSYS; | ||
867 | 868 | ||
868 | mutex_init(&data->mutex); | 869 | mutex_init(&data->mutex); |
869 | data->regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config); | ||
870 | if (IS_ERR(data->regmap)) { | ||
871 | dev_err(&client->dev, "Failed to allocate register map\n"); | ||
872 | return PTR_ERR(data->regmap); | ||
873 | } | ||
874 | 870 | ||
875 | ret = bmc150_magn_init(data); | 871 | ret = bmc150_magn_init(data); |
876 | if (ret < 0) | 872 | if (ret < 0) |
877 | return ret; | 873 | return ret; |
878 | 874 | ||
879 | indio_dev->dev.parent = &client->dev; | 875 | indio_dev->dev.parent = dev; |
880 | indio_dev->channels = bmc150_magn_channels; | 876 | indio_dev->channels = bmc150_magn_channels; |
881 | indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); | 877 | indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels); |
882 | indio_dev->available_scan_masks = bmc150_magn_scan_masks; | 878 | indio_dev->available_scan_masks = bmc150_magn_scan_masks; |
@@ -884,35 +880,34 @@ static int bmc150_magn_probe(struct i2c_client *client, | |||
884 | indio_dev->modes = INDIO_DIRECT_MODE; | 880 | indio_dev->modes = INDIO_DIRECT_MODE; |
885 | indio_dev->info = &bmc150_magn_info; | 881 | indio_dev->info = &bmc150_magn_info; |
886 | 882 | ||
887 | if (client->irq > 0) { | 883 | if (irq > 0) { |
888 | data->dready_trig = devm_iio_trigger_alloc(&client->dev, | 884 | data->dready_trig = devm_iio_trigger_alloc(dev, |
889 | "%s-dev%d", | 885 | "%s-dev%d", |
890 | indio_dev->name, | 886 | indio_dev->name, |
891 | indio_dev->id); | 887 | indio_dev->id); |
892 | if (!data->dready_trig) { | 888 | if (!data->dready_trig) { |
893 | ret = -ENOMEM; | 889 | ret = -ENOMEM; |
894 | dev_err(&client->dev, "iio trigger alloc failed\n"); | 890 | dev_err(dev, "iio trigger alloc failed\n"); |
895 | goto err_poweroff; | 891 | goto err_poweroff; |
896 | } | 892 | } |
897 | 893 | ||
898 | data->dready_trig->dev.parent = &client->dev; | 894 | data->dready_trig->dev.parent = dev; |
899 | data->dready_trig->ops = &bmc150_magn_trigger_ops; | 895 | data->dready_trig->ops = &bmc150_magn_trigger_ops; |
900 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); | 896 | iio_trigger_set_drvdata(data->dready_trig, indio_dev); |
901 | ret = iio_trigger_register(data->dready_trig); | 897 | ret = iio_trigger_register(data->dready_trig); |
902 | if (ret) { | 898 | if (ret) { |
903 | dev_err(&client->dev, "iio trigger register failed\n"); | 899 | dev_err(dev, "iio trigger register failed\n"); |
904 | goto err_poweroff; | 900 | goto err_poweroff; |
905 | } | 901 | } |
906 | 902 | ||
907 | ret = request_threaded_irq(client->irq, | 903 | ret = request_threaded_irq(irq, |
908 | iio_trigger_generic_data_rdy_poll, | 904 | iio_trigger_generic_data_rdy_poll, |
909 | NULL, | 905 | NULL, |
910 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, | 906 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
911 | BMC150_MAGN_IRQ_NAME, | 907 | BMC150_MAGN_IRQ_NAME, |
912 | data->dready_trig); | 908 | data->dready_trig); |
913 | if (ret < 0) { | 909 | if (ret < 0) { |
914 | dev_err(&client->dev, "request irq %d failed\n", | 910 | dev_err(dev, "request irq %d failed\n", irq); |
915 | client->irq); | ||
916 | goto err_trigger_unregister; | 911 | goto err_trigger_unregister; |
917 | } | 912 | } |
918 | } | 913 | } |
@@ -922,34 +917,33 @@ static int bmc150_magn_probe(struct i2c_client *client, | |||
922 | bmc150_magn_trigger_handler, | 917 | bmc150_magn_trigger_handler, |
923 | &bmc150_magn_buffer_setup_ops); | 918 | &bmc150_magn_buffer_setup_ops); |
924 | if (ret < 0) { | 919 | if (ret < 0) { |
925 | dev_err(&client->dev, | 920 | dev_err(dev, "iio triggered buffer setup failed\n"); |
926 | "iio triggered buffer setup failed\n"); | ||
927 | goto err_free_irq; | 921 | goto err_free_irq; |
928 | } | 922 | } |
929 | 923 | ||
930 | ret = pm_runtime_set_active(&client->dev); | 924 | ret = pm_runtime_set_active(dev); |
931 | if (ret) | 925 | if (ret) |
932 | goto err_buffer_cleanup; | 926 | goto err_buffer_cleanup; |
933 | 927 | ||
934 | pm_runtime_enable(&client->dev); | 928 | pm_runtime_enable(dev); |
935 | pm_runtime_set_autosuspend_delay(&client->dev, | 929 | pm_runtime_set_autosuspend_delay(dev, |
936 | BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); | 930 | BMC150_MAGN_AUTO_SUSPEND_DELAY_MS); |
937 | pm_runtime_use_autosuspend(&client->dev); | 931 | pm_runtime_use_autosuspend(dev); |
938 | 932 | ||
939 | ret = iio_device_register(indio_dev); | 933 | ret = iio_device_register(indio_dev); |
940 | if (ret < 0) { | 934 | if (ret < 0) { |
941 | dev_err(&client->dev, "unable to register iio device\n"); | 935 | dev_err(dev, "unable to register iio device\n"); |
942 | goto err_buffer_cleanup; | 936 | goto err_buffer_cleanup; |
943 | } | 937 | } |
944 | 938 | ||
945 | dev_dbg(&indio_dev->dev, "Registered device %s\n", name); | 939 | dev_dbg(dev, "Registered device %s\n", name); |
946 | return 0; | 940 | return 0; |
947 | 941 | ||
948 | err_buffer_cleanup: | 942 | err_buffer_cleanup: |
949 | iio_triggered_buffer_cleanup(indio_dev); | 943 | iio_triggered_buffer_cleanup(indio_dev); |
950 | err_free_irq: | 944 | err_free_irq: |
951 | if (client->irq > 0) | 945 | if (irq > 0) |
952 | free_irq(client->irq, data->dready_trig); | 946 | free_irq(irq, data->dready_trig); |
953 | err_trigger_unregister: | 947 | err_trigger_unregister: |
954 | if (data->dready_trig) | 948 | if (data->dready_trig) |
955 | iio_trigger_unregister(data->dready_trig); | 949 | iio_trigger_unregister(data->dready_trig); |
@@ -957,22 +951,23 @@ err_poweroff: | |||
957 | bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); | 951 | bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_SUSPEND, true); |
958 | return ret; | 952 | return ret; |
959 | } | 953 | } |
954 | EXPORT_SYMBOL(bmc150_magn_probe); | ||
960 | 955 | ||
961 | static int bmc150_magn_remove(struct i2c_client *client) | 956 | int bmc150_magn_remove(struct device *dev) |
962 | { | 957 | { |
963 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | 958 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
964 | struct bmc150_magn_data *data = iio_priv(indio_dev); | 959 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
965 | 960 | ||
966 | iio_device_unregister(indio_dev); | 961 | iio_device_unregister(indio_dev); |
967 | 962 | ||
968 | pm_runtime_disable(&client->dev); | 963 | pm_runtime_disable(dev); |
969 | pm_runtime_set_suspended(&client->dev); | 964 | pm_runtime_set_suspended(dev); |
970 | pm_runtime_put_noidle(&client->dev); | 965 | pm_runtime_put_noidle(dev); |
971 | 966 | ||
972 | iio_triggered_buffer_cleanup(indio_dev); | 967 | iio_triggered_buffer_cleanup(indio_dev); |
973 | 968 | ||
974 | if (client->irq > 0) | 969 | if (data->irq > 0) |
975 | free_irq(data->client->irq, data->dready_trig); | 970 | free_irq(data->irq, data->dready_trig); |
976 | 971 | ||
977 | if (data->dready_trig) | 972 | if (data->dready_trig) |
978 | iio_trigger_unregister(data->dready_trig); | 973 | iio_trigger_unregister(data->dready_trig); |
@@ -983,11 +978,12 @@ static int bmc150_magn_remove(struct i2c_client *client) | |||
983 | 978 | ||
984 | return 0; | 979 | return 0; |
985 | } | 980 | } |
981 | EXPORT_SYMBOL(bmc150_magn_remove); | ||
986 | 982 | ||
987 | #ifdef CONFIG_PM | 983 | #ifdef CONFIG_PM |
988 | static int bmc150_magn_runtime_suspend(struct device *dev) | 984 | static int bmc150_magn_runtime_suspend(struct device *dev) |
989 | { | 985 | { |
990 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | 986 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
991 | struct bmc150_magn_data *data = iio_priv(indio_dev); | 987 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
992 | int ret; | 988 | int ret; |
993 | 989 | ||
@@ -996,7 +992,7 @@ static int bmc150_magn_runtime_suspend(struct device *dev) | |||
996 | true); | 992 | true); |
997 | mutex_unlock(&data->mutex); | 993 | mutex_unlock(&data->mutex); |
998 | if (ret < 0) { | 994 | if (ret < 0) { |
999 | dev_err(&data->client->dev, "powering off device failed\n"); | 995 | dev_err(dev, "powering off device failed\n"); |
1000 | return ret; | 996 | return ret; |
1001 | } | 997 | } |
1002 | return 0; | 998 | return 0; |
@@ -1007,7 +1003,7 @@ static int bmc150_magn_runtime_suspend(struct device *dev) | |||
1007 | */ | 1003 | */ |
1008 | static int bmc150_magn_runtime_resume(struct device *dev) | 1004 | static int bmc150_magn_runtime_resume(struct device *dev) |
1009 | { | 1005 | { |
1010 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | 1006 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
1011 | struct bmc150_magn_data *data = iio_priv(indio_dev); | 1007 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
1012 | 1008 | ||
1013 | return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, | 1009 | return bmc150_magn_set_power_mode(data, BMC150_MAGN_POWER_MODE_NORMAL, |
@@ -1018,7 +1014,7 @@ static int bmc150_magn_runtime_resume(struct device *dev) | |||
1018 | #ifdef CONFIG_PM_SLEEP | 1014 | #ifdef CONFIG_PM_SLEEP |
1019 | static int bmc150_magn_suspend(struct device *dev) | 1015 | static int bmc150_magn_suspend(struct device *dev) |
1020 | { | 1016 | { |
1021 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | 1017 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
1022 | struct bmc150_magn_data *data = iio_priv(indio_dev); | 1018 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
1023 | int ret; | 1019 | int ret; |
1024 | 1020 | ||
@@ -1032,7 +1028,7 @@ static int bmc150_magn_suspend(struct device *dev) | |||
1032 | 1028 | ||
1033 | static int bmc150_magn_resume(struct device *dev) | 1029 | static int bmc150_magn_resume(struct device *dev) |
1034 | { | 1030 | { |
1035 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); | 1031 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
1036 | struct bmc150_magn_data *data = iio_priv(indio_dev); | 1032 | struct bmc150_magn_data *data = iio_priv(indio_dev); |
1037 | int ret; | 1033 | int ret; |
1038 | 1034 | ||
@@ -1045,38 +1041,13 @@ static int bmc150_magn_resume(struct device *dev) | |||
1045 | } | 1041 | } |
1046 | #endif | 1042 | #endif |
1047 | 1043 | ||
1048 | static const struct dev_pm_ops bmc150_magn_pm_ops = { | 1044 | const struct dev_pm_ops bmc150_magn_pm_ops = { |
1049 | SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) | 1045 | SET_SYSTEM_SLEEP_PM_OPS(bmc150_magn_suspend, bmc150_magn_resume) |
1050 | SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, | 1046 | SET_RUNTIME_PM_OPS(bmc150_magn_runtime_suspend, |
1051 | bmc150_magn_runtime_resume, NULL) | 1047 | bmc150_magn_runtime_resume, NULL) |
1052 | }; | 1048 | }; |
1053 | 1049 | EXPORT_SYMBOL(bmc150_magn_pm_ops); | |
1054 | static const struct acpi_device_id bmc150_magn_acpi_match[] = { | ||
1055 | {"BMC150B", 0}, | ||
1056 | {"BMC156B", 0}, | ||
1057 | {}, | ||
1058 | }; | ||
1059 | MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); | ||
1060 | |||
1061 | static const struct i2c_device_id bmc150_magn_id[] = { | ||
1062 | {"bmc150_magn", 0}, | ||
1063 | {"bmc156_magn", 0}, | ||
1064 | {}, | ||
1065 | }; | ||
1066 | MODULE_DEVICE_TABLE(i2c, bmc150_magn_id); | ||
1067 | |||
1068 | static struct i2c_driver bmc150_magn_driver = { | ||
1069 | .driver = { | ||
1070 | .name = BMC150_MAGN_DRV_NAME, | ||
1071 | .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), | ||
1072 | .pm = &bmc150_magn_pm_ops, | ||
1073 | }, | ||
1074 | .probe = bmc150_magn_probe, | ||
1075 | .remove = bmc150_magn_remove, | ||
1076 | .id_table = bmc150_magn_id, | ||
1077 | }; | ||
1078 | module_i2c_driver(bmc150_magn_driver); | ||
1079 | 1050 | ||
1080 | MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); | 1051 | MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>"); |
1081 | MODULE_LICENSE("GPL v2"); | 1052 | MODULE_LICENSE("GPL v2"); |
1082 | MODULE_DESCRIPTION("BMC150 magnetometer driver"); | 1053 | MODULE_DESCRIPTION("BMC150 magnetometer core driver"); |
diff --git a/drivers/iio/magnetometer/bmc150_magn.h b/drivers/iio/magnetometer/bmc150_magn.h new file mode 100644 index 000000000000..9a8e26812ca8 --- /dev/null +++ b/drivers/iio/magnetometer/bmc150_magn.h | |||
@@ -0,0 +1,11 @@ | |||
1 | #ifndef _BMC150_MAGN_H_ | ||
2 | #define _BMC150_MAGN_H_ | ||
3 | |||
4 | extern const struct regmap_config bmc150_magn_regmap_config; | ||
5 | extern const struct dev_pm_ops bmc150_magn_pm_ops; | ||
6 | |||
7 | int bmc150_magn_probe(struct device *dev, struct regmap *regmap, int irq, | ||
8 | const char *name); | ||
9 | int bmc150_magn_remove(struct device *dev); | ||
10 | |||
11 | #endif /* _BMC150_MAGN_H_ */ | ||
diff --git a/drivers/iio/magnetometer/bmc150_magn_i2c.c b/drivers/iio/magnetometer/bmc150_magn_i2c.c new file mode 100644 index 000000000000..eddc7f0d0096 --- /dev/null +++ b/drivers/iio/magnetometer/bmc150_magn_i2c.c | |||
@@ -0,0 +1,77 @@ | |||
1 | /* | ||
2 | * 3-axis magnetometer driver supporting following I2C Bosch-Sensortec chips: | ||
3 | * - BMC150 | ||
4 | * - BMC156 | ||
5 | * | ||
6 | * Copyright (c) 2016, Intel Corporation. | ||
7 | * | ||
8 | * This program is free software; you can redistribute it and/or modify it | ||
9 | * under the terms and conditions of the GNU General Public License, | ||
10 | * version 2, as published by the Free Software Foundation. | ||
11 | * | ||
12 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
15 | * more details. | ||
16 | */ | ||
17 | #include <linux/device.h> | ||
18 | #include <linux/mod_devicetable.h> | ||
19 | #include <linux/i2c.h> | ||
20 | #include <linux/module.h> | ||
21 | #include <linux/acpi.h> | ||
22 | #include <linux/regmap.h> | ||
23 | |||
24 | #include "bmc150_magn.h" | ||
25 | |||
26 | static int bmc150_magn_i2c_probe(struct i2c_client *client, | ||
27 | const struct i2c_device_id *id) | ||
28 | { | ||
29 | struct regmap *regmap; | ||
30 | const char *name = NULL; | ||
31 | |||
32 | regmap = devm_regmap_init_i2c(client, &bmc150_magn_regmap_config); | ||
33 | if (IS_ERR(regmap)) { | ||
34 | dev_err(&client->dev, "Failed to initialize i2c regmap\n"); | ||
35 | return PTR_ERR(regmap); | ||
36 | } | ||
37 | |||
38 | if (id) | ||
39 | name = id->name; | ||
40 | |||
41 | return bmc150_magn_probe(&client->dev, regmap, client->irq, name); | ||
42 | } | ||
43 | |||
44 | static int bmc150_magn_i2c_remove(struct i2c_client *client) | ||
45 | { | ||
46 | return bmc150_magn_remove(&client->dev); | ||
47 | } | ||
48 | |||
49 | static const struct acpi_device_id bmc150_magn_acpi_match[] = { | ||
50 | {"BMC150B", 0}, | ||
51 | {"BMC156B", 0}, | ||
52 | {}, | ||
53 | }; | ||
54 | MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); | ||
55 | |||
56 | static const struct i2c_device_id bmc150_magn_i2c_id[] = { | ||
57 | {"bmc150_magn", 0}, | ||
58 | {"bmc156_magn", 0}, | ||
59 | {} | ||
60 | }; | ||
61 | MODULE_DEVICE_TABLE(i2c, bmc150_magn_i2c_id); | ||
62 | |||
63 | static struct i2c_driver bmc150_magn_driver = { | ||
64 | .driver = { | ||
65 | .name = "bmc150_magn_i2c", | ||
66 | .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), | ||
67 | .pm = &bmc150_magn_pm_ops, | ||
68 | }, | ||
69 | .probe = bmc150_magn_i2c_probe, | ||
70 | .remove = bmc150_magn_i2c_remove, | ||
71 | .id_table = bmc150_magn_i2c_id, | ||
72 | }; | ||
73 | module_i2c_driver(bmc150_magn_driver); | ||
74 | |||
75 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); | ||
76 | MODULE_LICENSE("GPL v2"); | ||
77 | MODULE_DESCRIPTION("BMC150 I2C magnetometer driver"); | ||
diff --git a/drivers/iio/magnetometer/bmc150_magn_spi.c b/drivers/iio/magnetometer/bmc150_magn_spi.c new file mode 100644 index 000000000000..c4c738a07695 --- /dev/null +++ b/drivers/iio/magnetometer/bmc150_magn_spi.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | * 3-axis magnetometer driver support following SPI Bosch-Sensortec chips: | ||
3 | * - BMC150 | ||
4 | * - BMC156 | ||
5 | * | ||
6 | * Copyright (c) 2016, Intel Corporation. | ||
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 | #include <linux/module.h> | ||
13 | #include <linux/mod_devicetable.h> | ||
14 | #include <linux/spi/spi.h> | ||
15 | #include <linux/acpi.h> | ||
16 | #include <linux/regmap.h> | ||
17 | |||
18 | #include "bmc150_magn.h" | ||
19 | |||
20 | static int bmc150_magn_spi_probe(struct spi_device *spi) | ||
21 | { | ||
22 | struct regmap *regmap; | ||
23 | const struct spi_device_id *id = spi_get_device_id(spi); | ||
24 | |||
25 | regmap = devm_regmap_init_spi(spi, &bmc150_magn_regmap_config); | ||
26 | if (IS_ERR(regmap)) { | ||
27 | dev_err(&spi->dev, "Failed to register spi regmap %d\n", | ||
28 | (int)PTR_ERR(regmap)); | ||
29 | return PTR_ERR(regmap); | ||
30 | } | ||
31 | return bmc150_magn_probe(&spi->dev, regmap, spi->irq, id->name); | ||
32 | } | ||
33 | |||
34 | static int bmc150_magn_spi_remove(struct spi_device *spi) | ||
35 | { | ||
36 | bmc150_magn_remove(&spi->dev); | ||
37 | |||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | static const struct spi_device_id bmc150_magn_spi_id[] = { | ||
42 | {"bmc150_magn", 0}, | ||
43 | {"bmc156_magn", 0}, | ||
44 | {} | ||
45 | }; | ||
46 | MODULE_DEVICE_TABLE(spi, bmc150_magn_spi_id); | ||
47 | |||
48 | static const struct acpi_device_id bmc150_magn_acpi_match[] = { | ||
49 | {"BMC150B", 0}, | ||
50 | {"BMC156B", 0}, | ||
51 | {}, | ||
52 | }; | ||
53 | MODULE_DEVICE_TABLE(acpi, bmc150_magn_acpi_match); | ||
54 | |||
55 | static struct spi_driver bmc150_magn_spi_driver = { | ||
56 | .probe = bmc150_magn_spi_probe, | ||
57 | .remove = bmc150_magn_spi_remove, | ||
58 | .id_table = bmc150_magn_spi_id, | ||
59 | .driver = { | ||
60 | .acpi_match_table = ACPI_PTR(bmc150_magn_acpi_match), | ||
61 | .name = "bmc150_magn_spi", | ||
62 | }, | ||
63 | }; | ||
64 | module_spi_driver(bmc150_magn_spi_driver); | ||
65 | |||
66 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com"); | ||
67 | MODULE_DESCRIPTION("BMC150 magnetometer SPI driver"); | ||
68 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/magnetometer/st_magn_core.c b/drivers/iio/magnetometer/st_magn_core.c index 501f858df413..62036d2a9956 100644 --- a/drivers/iio/magnetometer/st_magn_core.c +++ b/drivers/iio/magnetometer/st_magn_core.c | |||
@@ -484,6 +484,7 @@ static const struct st_sensor_settings st_magn_sensors_settings[] = { | |||
484 | .mask_int1 = ST_MAGN_3_DRDY_INT_MASK, | 484 | .mask_int1 = ST_MAGN_3_DRDY_INT_MASK, |
485 | .addr_ihl = ST_MAGN_3_IHL_IRQ_ADDR, | 485 | .addr_ihl = ST_MAGN_3_IHL_IRQ_ADDR, |
486 | .mask_ihl = ST_MAGN_3_IHL_IRQ_MASK, | 486 | .mask_ihl = ST_MAGN_3_IHL_IRQ_MASK, |
487 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
487 | }, | 488 | }, |
488 | .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT, | 489 | .multi_read_bit = ST_MAGN_3_MULTIREAD_BIT, |
489 | .bootime = 2, | 490 | .bootime = 2, |
diff --git a/drivers/iio/potentiometer/Kconfig b/drivers/iio/potentiometer/Kconfig index 7ea069bbca2d..6acb23810bb4 100644 --- a/drivers/iio/potentiometer/Kconfig +++ b/drivers/iio/potentiometer/Kconfig | |||
@@ -5,6 +5,16 @@ | |||
5 | 5 | ||
6 | menu "Digital potentiometers" | 6 | menu "Digital potentiometers" |
7 | 7 | ||
8 | config DS1803 | ||
9 | tristate "Maxim Integrated DS1803 Digital Potentiometer driver" | ||
10 | depends on I2C | ||
11 | help | ||
12 | Say yes here to build support for the Maxim Integrated DS1803 | ||
13 | digital potentiomenter chip. | ||
14 | |||
15 | To compile this driver as a module, choose M here: the | ||
16 | module will be called ds1803. | ||
17 | |||
8 | config MCP4131 | 18 | config MCP4131 |
9 | tristate "Microchip MCP413X/414X/415X/416X/423X/424X/425X/426X Digital Potentiometer driver" | 19 | tristate "Microchip MCP413X/414X/415X/416X/423X/424X/425X/426X Digital Potentiometer driver" |
10 | depends on SPI | 20 | depends on SPI |
diff --git a/drivers/iio/potentiometer/Makefile b/drivers/iio/potentiometer/Makefile index 91a80f8db24d..6007faa2fb02 100644 --- a/drivers/iio/potentiometer/Makefile +++ b/drivers/iio/potentiometer/Makefile | |||
@@ -3,6 +3,7 @@ | |||
3 | # | 3 | # |
4 | 4 | ||
5 | # When adding new entries keep the list in alphabetical order | 5 | # When adding new entries keep the list in alphabetical order |
6 | obj-$(CONFIG_DS1803) += ds1803.o | ||
6 | obj-$(CONFIG_MCP4131) += mcp4131.o | 7 | obj-$(CONFIG_MCP4131) += mcp4131.o |
7 | obj-$(CONFIG_MCP4531) += mcp4531.o | 8 | obj-$(CONFIG_MCP4531) += mcp4531.o |
8 | obj-$(CONFIG_TPL0102) += tpl0102.o | 9 | obj-$(CONFIG_TPL0102) += tpl0102.o |
diff --git a/drivers/iio/potentiometer/ds1803.c b/drivers/iio/potentiometer/ds1803.c new file mode 100644 index 000000000000..fb9e2a337dc2 --- /dev/null +++ b/drivers/iio/potentiometer/ds1803.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* | ||
2 | * Maxim Integrated DS1803 digital potentiometer driver | ||
3 | * Copyright (c) 2016 Slawomir Stepien | ||
4 | * | ||
5 | * Datasheet: https://datasheets.maximintegrated.com/en/ds/DS1803.pdf | ||
6 | * | ||
7 | * DEVID #Wipers #Positions Resistor Opts (kOhm) i2c address | ||
8 | * ds1803 2 256 10, 50, 100 0101xxx | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or modify it | ||
11 | * under the terms of the GNU General Public License version 2 as published by | ||
12 | * the Free Software Foundation. | ||
13 | */ | ||
14 | |||
15 | #include <linux/err.h> | ||
16 | #include <linux/export.h> | ||
17 | #include <linux/i2c.h> | ||
18 | #include <linux/iio/iio.h> | ||
19 | #include <linux/module.h> | ||
20 | #include <linux/of.h> | ||
21 | |||
22 | #define DS1803_MAX_POS 255 | ||
23 | #define DS1803_WRITE(chan) (0xa8 | ((chan) + 1)) | ||
24 | |||
25 | enum ds1803_type { | ||
26 | DS1803_010, | ||
27 | DS1803_050, | ||
28 | DS1803_100, | ||
29 | }; | ||
30 | |||
31 | struct ds1803_cfg { | ||
32 | int kohms; | ||
33 | }; | ||
34 | |||
35 | static const struct ds1803_cfg ds1803_cfg[] = { | ||
36 | [DS1803_010] = { .kohms = 10, }, | ||
37 | [DS1803_050] = { .kohms = 50, }, | ||
38 | [DS1803_100] = { .kohms = 100, }, | ||
39 | }; | ||
40 | |||
41 | struct ds1803_data { | ||
42 | struct i2c_client *client; | ||
43 | const struct ds1803_cfg *cfg; | ||
44 | }; | ||
45 | |||
46 | #define DS1803_CHANNEL(ch) { \ | ||
47 | .type = IIO_RESISTANCE, \ | ||
48 | .indexed = 1, \ | ||
49 | .output = 1, \ | ||
50 | .channel = (ch), \ | ||
51 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ | ||
52 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ | ||
53 | } | ||
54 | |||
55 | static const struct iio_chan_spec ds1803_channels[] = { | ||
56 | DS1803_CHANNEL(0), | ||
57 | DS1803_CHANNEL(1), | ||
58 | }; | ||
59 | |||
60 | static int ds1803_read_raw(struct iio_dev *indio_dev, | ||
61 | struct iio_chan_spec const *chan, | ||
62 | int *val, int *val2, long mask) | ||
63 | { | ||
64 | struct ds1803_data *data = iio_priv(indio_dev); | ||
65 | int pot = chan->channel; | ||
66 | int ret; | ||
67 | u8 result[indio_dev->num_channels]; | ||
68 | |||
69 | switch (mask) { | ||
70 | case IIO_CHAN_INFO_RAW: | ||
71 | ret = i2c_master_recv(data->client, result, | ||
72 | indio_dev->num_channels); | ||
73 | if (ret < 0) | ||
74 | return ret; | ||
75 | |||
76 | *val = result[pot]; | ||
77 | return IIO_VAL_INT; | ||
78 | |||
79 | case IIO_CHAN_INFO_SCALE: | ||
80 | *val = 1000 * data->cfg->kohms; | ||
81 | *val2 = DS1803_MAX_POS; | ||
82 | return IIO_VAL_FRACTIONAL; | ||
83 | } | ||
84 | |||
85 | return -EINVAL; | ||
86 | } | ||
87 | |||
88 | static int ds1803_write_raw(struct iio_dev *indio_dev, | ||
89 | struct iio_chan_spec const *chan, | ||
90 | int val, int val2, long mask) | ||
91 | { | ||
92 | struct ds1803_data *data = iio_priv(indio_dev); | ||
93 | int pot = chan->channel; | ||
94 | |||
95 | if (val2 != 0) | ||
96 | return -EINVAL; | ||
97 | |||
98 | switch (mask) { | ||
99 | case IIO_CHAN_INFO_RAW: | ||
100 | if (val > DS1803_MAX_POS || val < 0) | ||
101 | return -EINVAL; | ||
102 | break; | ||
103 | default: | ||
104 | return -EINVAL; | ||
105 | } | ||
106 | |||
107 | return i2c_smbus_write_byte_data(data->client, DS1803_WRITE(pot), val); | ||
108 | } | ||
109 | |||
110 | static const struct iio_info ds1803_info = { | ||
111 | .read_raw = ds1803_read_raw, | ||
112 | .write_raw = ds1803_write_raw, | ||
113 | .driver_module = THIS_MODULE, | ||
114 | }; | ||
115 | |||
116 | static int ds1803_probe(struct i2c_client *client, | ||
117 | const struct i2c_device_id *id) | ||
118 | { | ||
119 | struct device *dev = &client->dev; | ||
120 | struct ds1803_data *data; | ||
121 | struct iio_dev *indio_dev; | ||
122 | |||
123 | indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); | ||
124 | if (!indio_dev) | ||
125 | return -ENOMEM; | ||
126 | |||
127 | i2c_set_clientdata(client, indio_dev); | ||
128 | |||
129 | data = iio_priv(indio_dev); | ||
130 | data->client = client; | ||
131 | data->cfg = &ds1803_cfg[id->driver_data]; | ||
132 | |||
133 | indio_dev->dev.parent = dev; | ||
134 | indio_dev->info = &ds1803_info; | ||
135 | indio_dev->channels = ds1803_channels; | ||
136 | indio_dev->num_channels = ARRAY_SIZE(ds1803_channels); | ||
137 | indio_dev->name = client->name; | ||
138 | |||
139 | return devm_iio_device_register(dev, indio_dev); | ||
140 | } | ||
141 | |||
142 | #if defined(CONFIG_OF) | ||
143 | static const struct of_device_id ds1803_dt_ids[] = { | ||
144 | { .compatible = "maxim,ds1803-010", .data = &ds1803_cfg[DS1803_010] }, | ||
145 | { .compatible = "maxim,ds1803-050", .data = &ds1803_cfg[DS1803_050] }, | ||
146 | { .compatible = "maxim,ds1803-100", .data = &ds1803_cfg[DS1803_100] }, | ||
147 | {} | ||
148 | }; | ||
149 | MODULE_DEVICE_TABLE(of, ds1803_dt_ids); | ||
150 | #endif /* CONFIG_OF */ | ||
151 | |||
152 | static const struct i2c_device_id ds1803_id[] = { | ||
153 | { "ds1803-010", DS1803_010 }, | ||
154 | { "ds1803-050", DS1803_050 }, | ||
155 | { "ds1803-100", DS1803_100 }, | ||
156 | {} | ||
157 | }; | ||
158 | MODULE_DEVICE_TABLE(i2c, ds1803_id); | ||
159 | |||
160 | static struct i2c_driver ds1803_driver = { | ||
161 | .driver = { | ||
162 | .name = "ds1803", | ||
163 | .of_match_table = of_match_ptr(ds1803_dt_ids), | ||
164 | }, | ||
165 | .probe = ds1803_probe, | ||
166 | .id_table = ds1803_id, | ||
167 | }; | ||
168 | |||
169 | module_i2c_driver(ds1803_driver); | ||
170 | |||
171 | MODULE_AUTHOR("Slawomir Stepien <sst@poczta.fm>"); | ||
172 | MODULE_DESCRIPTION("DS1803 digital potentiometer"); | ||
173 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig index 8de0192adea6..cda9f128f3a4 100644 --- a/drivers/iio/pressure/Kconfig +++ b/drivers/iio/pressure/Kconfig | |||
@@ -6,12 +6,13 @@ | |||
6 | menu "Pressure sensors" | 6 | menu "Pressure sensors" |
7 | 7 | ||
8 | config BMP280 | 8 | config BMP280 |
9 | tristate "Bosch Sensortec BMP280 pressure sensor driver" | 9 | tristate "Bosch Sensortec BMP180 and BMP280 pressure sensor driver" |
10 | depends on I2C | 10 | depends on I2C |
11 | depends on !(BMP085_I2C=y || BMP085_I2C=m) | ||
11 | select REGMAP_I2C | 12 | select REGMAP_I2C |
12 | help | 13 | help |
13 | Say yes here to build support for Bosch Sensortec BMP280 | 14 | Say yes here to build support for Bosch Sensortec BMP180 and BMP280 |
14 | pressure and temperature sensor. | 15 | pressure and temperature sensors. |
15 | 16 | ||
16 | To compile this driver as a module, choose M here: the module | 17 | To compile this driver as a module, choose M here: the module |
17 | will be called bmp280. | 18 | will be called bmp280. |
@@ -30,6 +31,17 @@ config HID_SENSOR_PRESS | |||
30 | To compile this driver as a module, choose M here: the module | 31 | To compile this driver as a module, choose M here: the module |
31 | will be called hid-sensor-press. | 32 | will be called hid-sensor-press. |
32 | 33 | ||
34 | config HP03 | ||
35 | tristate "Hope RF HP03 temperature and pressure sensor driver" | ||
36 | depends on I2C | ||
37 | select REGMAP_I2C | ||
38 | help | ||
39 | Say yes here to build support for Hope RF HP03 pressure and | ||
40 | temperature sensor. | ||
41 | |||
42 | To compile this driver as a module, choose M here: the module | ||
43 | will be called hp03. | ||
44 | |||
33 | config MPL115 | 45 | config MPL115 |
34 | tristate | 46 | tristate |
35 | 47 | ||
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile index 6e60863c1086..17d6e7afa1ff 100644 --- a/drivers/iio/pressure/Makefile +++ b/drivers/iio/pressure/Makefile | |||
@@ -5,6 +5,7 @@ | |||
5 | # When adding new entries keep the list in alphabetical order | 5 | # When adding new entries keep the list in alphabetical order |
6 | obj-$(CONFIG_BMP280) += bmp280.o | 6 | obj-$(CONFIG_BMP280) += bmp280.o |
7 | obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o | 7 | obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o |
8 | obj-$(CONFIG_HP03) += hp03.o | ||
8 | obj-$(CONFIG_MPL115) += mpl115.o | 9 | obj-$(CONFIG_MPL115) += mpl115.o |
9 | obj-$(CONFIG_MPL115_I2C) += mpl115_i2c.o | 10 | obj-$(CONFIG_MPL115_I2C) += mpl115_i2c.o |
10 | obj-$(CONFIG_MPL115_SPI) += mpl115_spi.o | 11 | obj-$(CONFIG_MPL115_SPI) += mpl115_spi.o |
diff --git a/drivers/iio/pressure/bmp280.c b/drivers/iio/pressure/bmp280.c index a2602d8dd6d5..2f1498e12bb2 100644 --- a/drivers/iio/pressure/bmp280.c +++ b/drivers/iio/pressure/bmp280.c | |||
@@ -1,12 +1,15 @@ | |||
1 | /* | 1 | /* |
2 | * Copyright (c) 2014 Intel Corporation | 2 | * Copyright (c) 2014 Intel Corporation |
3 | * | 3 | * |
4 | * Driver for Bosch Sensortec BMP280 digital pressure sensor. | 4 | * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. |
5 | * | 5 | * |
6 | * This program is free software; you can redistribute it and/or modify | 6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License version 2 as | 7 | * it under the terms of the GNU General Public License version 2 as |
8 | * published by the Free Software Foundation. | 8 | * published by the Free Software Foundation. |
9 | * | 9 | * |
10 | * Datasheet: | ||
11 | * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf | ||
12 | * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf | ||
10 | */ | 13 | */ |
11 | 14 | ||
12 | #define pr_fmt(fmt) "bmp280: " fmt | 15 | #define pr_fmt(fmt) "bmp280: " fmt |
@@ -15,9 +18,11 @@ | |||
15 | #include <linux/i2c.h> | 18 | #include <linux/i2c.h> |
16 | #include <linux/acpi.h> | 19 | #include <linux/acpi.h> |
17 | #include <linux/regmap.h> | 20 | #include <linux/regmap.h> |
21 | #include <linux/delay.h> | ||
18 | #include <linux/iio/iio.h> | 22 | #include <linux/iio/iio.h> |
19 | #include <linux/iio/sysfs.h> | 23 | #include <linux/iio/sysfs.h> |
20 | 24 | ||
25 | /* BMP280 specific registers */ | ||
21 | #define BMP280_REG_TEMP_XLSB 0xFC | 26 | #define BMP280_REG_TEMP_XLSB 0xFC |
22 | #define BMP280_REG_TEMP_LSB 0xFB | 27 | #define BMP280_REG_TEMP_LSB 0xFB |
23 | #define BMP280_REG_TEMP_MSB 0xFA | 28 | #define BMP280_REG_TEMP_MSB 0xFA |
@@ -26,10 +31,7 @@ | |||
26 | #define BMP280_REG_PRESS_MSB 0xF7 | 31 | #define BMP280_REG_PRESS_MSB 0xF7 |
27 | 32 | ||
28 | #define BMP280_REG_CONFIG 0xF5 | 33 | #define BMP280_REG_CONFIG 0xF5 |
29 | #define BMP280_REG_CTRL_MEAS 0xF4 | ||
30 | #define BMP280_REG_STATUS 0xF3 | 34 | #define BMP280_REG_STATUS 0xF3 |
31 | #define BMP280_REG_RESET 0xE0 | ||
32 | #define BMP280_REG_ID 0xD0 | ||
33 | 35 | ||
34 | #define BMP280_REG_COMP_TEMP_START 0x88 | 36 | #define BMP280_REG_COMP_TEMP_START 0x88 |
35 | #define BMP280_COMP_TEMP_REG_COUNT 6 | 37 | #define BMP280_COMP_TEMP_REG_COUNT 6 |
@@ -46,25 +48,49 @@ | |||
46 | 48 | ||
47 | #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5)) | 49 | #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5)) |
48 | #define BMP280_OSRS_TEMP_SKIP 0 | 50 | #define BMP280_OSRS_TEMP_SKIP 0 |
49 | #define BMP280_OSRS_TEMP_1X BIT(5) | 51 | #define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5) |
50 | #define BMP280_OSRS_TEMP_2X BIT(6) | 52 | #define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1) |
51 | #define BMP280_OSRS_TEMP_4X (BIT(6) | BIT(5)) | 53 | #define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2) |
52 | #define BMP280_OSRS_TEMP_8X BIT(7) | 54 | #define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3) |
53 | #define BMP280_OSRS_TEMP_16X (BIT(7) | BIT(5)) | 55 | #define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4) |
56 | #define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5) | ||
54 | 57 | ||
55 | #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2)) | 58 | #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2)) |
56 | #define BMP280_OSRS_PRESS_SKIP 0 | 59 | #define BMP280_OSRS_PRESS_SKIP 0 |
57 | #define BMP280_OSRS_PRESS_1X BIT(2) | 60 | #define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2) |
58 | #define BMP280_OSRS_PRESS_2X BIT(3) | 61 | #define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1) |
59 | #define BMP280_OSRS_PRESS_4X (BIT(3) | BIT(2)) | 62 | #define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2) |
60 | #define BMP280_OSRS_PRESS_8X BIT(4) | 63 | #define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3) |
61 | #define BMP280_OSRS_PRESS_16X (BIT(4) | BIT(2)) | 64 | #define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4) |
65 | #define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5) | ||
62 | 66 | ||
63 | #define BMP280_MODE_MASK (BIT(1) | BIT(0)) | 67 | #define BMP280_MODE_MASK (BIT(1) | BIT(0)) |
64 | #define BMP280_MODE_SLEEP 0 | 68 | #define BMP280_MODE_SLEEP 0 |
65 | #define BMP280_MODE_FORCED BIT(0) | 69 | #define BMP280_MODE_FORCED BIT(0) |
66 | #define BMP280_MODE_NORMAL (BIT(1) | BIT(0)) | 70 | #define BMP280_MODE_NORMAL (BIT(1) | BIT(0)) |
67 | 71 | ||
72 | /* BMP180 specific registers */ | ||
73 | #define BMP180_REG_OUT_XLSB 0xF8 | ||
74 | #define BMP180_REG_OUT_LSB 0xF7 | ||
75 | #define BMP180_REG_OUT_MSB 0xF6 | ||
76 | |||
77 | #define BMP180_REG_CALIB_START 0xAA | ||
78 | #define BMP180_REG_CALIB_COUNT 22 | ||
79 | |||
80 | #define BMP180_MEAS_SCO BIT(5) | ||
81 | #define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO) | ||
82 | #define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO) | ||
83 | #define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0) | ||
84 | #define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1) | ||
85 | #define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2) | ||
86 | #define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3) | ||
87 | |||
88 | /* BMP180 and BMP280 common registers */ | ||
89 | #define BMP280_REG_CTRL_MEAS 0xF4 | ||
90 | #define BMP280_REG_RESET 0xE0 | ||
91 | #define BMP280_REG_ID 0xD0 | ||
92 | |||
93 | #define BMP180_CHIP_ID 0x55 | ||
68 | #define BMP280_CHIP_ID 0x58 | 94 | #define BMP280_CHIP_ID 0x58 |
69 | #define BMP280_SOFT_RESET_VAL 0xB6 | 95 | #define BMP280_SOFT_RESET_VAL 0xB6 |
70 | 96 | ||
@@ -72,6 +98,11 @@ struct bmp280_data { | |||
72 | struct i2c_client *client; | 98 | struct i2c_client *client; |
73 | struct mutex lock; | 99 | struct mutex lock; |
74 | struct regmap *regmap; | 100 | struct regmap *regmap; |
101 | const struct bmp280_chip_info *chip_info; | ||
102 | |||
103 | /* log of base 2 of oversampling rate */ | ||
104 | u8 oversampling_press; | ||
105 | u8 oversampling_temp; | ||
75 | 106 | ||
76 | /* | 107 | /* |
77 | * Carryover value from temperature conversion, used in pressure | 108 | * Carryover value from temperature conversion, used in pressure |
@@ -80,9 +111,23 @@ struct bmp280_data { | |||
80 | s32 t_fine; | 111 | s32 t_fine; |
81 | }; | 112 | }; |
82 | 113 | ||
114 | struct bmp280_chip_info { | ||
115 | const struct regmap_config *regmap_config; | ||
116 | |||
117 | const int *oversampling_temp_avail; | ||
118 | int num_oversampling_temp_avail; | ||
119 | |||
120 | const int *oversampling_press_avail; | ||
121 | int num_oversampling_press_avail; | ||
122 | |||
123 | int (*chip_config)(struct bmp280_data *); | ||
124 | int (*read_temp)(struct bmp280_data *, int *); | ||
125 | int (*read_press)(struct bmp280_data *, int *, int *); | ||
126 | }; | ||
127 | |||
83 | /* | 128 | /* |
84 | * These enums are used for indexing into the array of compensation | 129 | * These enums are used for indexing into the array of compensation |
85 | * parameters. | 130 | * parameters for BMP280. |
86 | */ | 131 | */ |
87 | enum { T1, T2, T3 }; | 132 | enum { T1, T2, T3 }; |
88 | enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; | 133 | enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; |
@@ -90,11 +135,13 @@ enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; | |||
90 | static const struct iio_chan_spec bmp280_channels[] = { | 135 | static const struct iio_chan_spec bmp280_channels[] = { |
91 | { | 136 | { |
92 | .type = IIO_PRESSURE, | 137 | .type = IIO_PRESSURE, |
93 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | 138 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
139 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), | ||
94 | }, | 140 | }, |
95 | { | 141 | { |
96 | .type = IIO_TEMP, | 142 | .type = IIO_TEMP, |
97 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | 143 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) | |
144 | BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), | ||
98 | }, | 145 | }, |
99 | }; | 146 | }; |
100 | 147 | ||
@@ -290,10 +337,25 @@ static int bmp280_read_raw(struct iio_dev *indio_dev, | |||
290 | case IIO_CHAN_INFO_PROCESSED: | 337 | case IIO_CHAN_INFO_PROCESSED: |
291 | switch (chan->type) { | 338 | switch (chan->type) { |
292 | case IIO_PRESSURE: | 339 | case IIO_PRESSURE: |
293 | ret = bmp280_read_press(data, val, val2); | 340 | ret = data->chip_info->read_press(data, val, val2); |
294 | break; | 341 | break; |
295 | case IIO_TEMP: | 342 | case IIO_TEMP: |
296 | ret = bmp280_read_temp(data, val); | 343 | ret = data->chip_info->read_temp(data, val); |
344 | break; | ||
345 | default: | ||
346 | ret = -EINVAL; | ||
347 | break; | ||
348 | } | ||
349 | break; | ||
350 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: | ||
351 | switch (chan->type) { | ||
352 | case IIO_PRESSURE: | ||
353 | *val = 1 << data->oversampling_press; | ||
354 | ret = IIO_VAL_INT; | ||
355 | break; | ||
356 | case IIO_TEMP: | ||
357 | *val = 1 << data->oversampling_temp; | ||
358 | ret = IIO_VAL_INT; | ||
297 | break; | 359 | break; |
298 | default: | 360 | default: |
299 | ret = -EINVAL; | 361 | ret = -EINVAL; |
@@ -310,22 +372,135 @@ static int bmp280_read_raw(struct iio_dev *indio_dev, | |||
310 | return ret; | 372 | return ret; |
311 | } | 373 | } |
312 | 374 | ||
375 | static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data, | ||
376 | int val) | ||
377 | { | ||
378 | int i; | ||
379 | const int *avail = data->chip_info->oversampling_temp_avail; | ||
380 | const int n = data->chip_info->num_oversampling_temp_avail; | ||
381 | |||
382 | for (i = 0; i < n; i++) { | ||
383 | if (avail[i] == val) { | ||
384 | data->oversampling_temp = ilog2(val); | ||
385 | |||
386 | return data->chip_info->chip_config(data); | ||
387 | } | ||
388 | } | ||
389 | return -EINVAL; | ||
390 | } | ||
391 | |||
392 | static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data, | ||
393 | int val) | ||
394 | { | ||
395 | int i; | ||
396 | const int *avail = data->chip_info->oversampling_press_avail; | ||
397 | const int n = data->chip_info->num_oversampling_press_avail; | ||
398 | |||
399 | for (i = 0; i < n; i++) { | ||
400 | if (avail[i] == val) { | ||
401 | data->oversampling_press = ilog2(val); | ||
402 | |||
403 | return data->chip_info->chip_config(data); | ||
404 | } | ||
405 | } | ||
406 | return -EINVAL; | ||
407 | } | ||
408 | |||
409 | static int bmp280_write_raw(struct iio_dev *indio_dev, | ||
410 | struct iio_chan_spec const *chan, | ||
411 | int val, int val2, long mask) | ||
412 | { | ||
413 | int ret = 0; | ||
414 | struct bmp280_data *data = iio_priv(indio_dev); | ||
415 | |||
416 | switch (mask) { | ||
417 | case IIO_CHAN_INFO_OVERSAMPLING_RATIO: | ||
418 | mutex_lock(&data->lock); | ||
419 | switch (chan->type) { | ||
420 | case IIO_PRESSURE: | ||
421 | ret = bmp280_write_oversampling_ratio_press(data, val); | ||
422 | break; | ||
423 | case IIO_TEMP: | ||
424 | ret = bmp280_write_oversampling_ratio_temp(data, val); | ||
425 | break; | ||
426 | default: | ||
427 | ret = -EINVAL; | ||
428 | break; | ||
429 | } | ||
430 | mutex_unlock(&data->lock); | ||
431 | break; | ||
432 | default: | ||
433 | return -EINVAL; | ||
434 | } | ||
435 | |||
436 | return ret; | ||
437 | } | ||
438 | |||
439 | static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n) | ||
440 | { | ||
441 | size_t len = 0; | ||
442 | int i; | ||
443 | |||
444 | for (i = 0; i < n; i++) | ||
445 | len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]); | ||
446 | |||
447 | buf[len - 1] = '\n'; | ||
448 | |||
449 | return len; | ||
450 | } | ||
451 | |||
452 | static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev, | ||
453 | struct device_attribute *attr, char *buf) | ||
454 | { | ||
455 | struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev)); | ||
456 | |||
457 | return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail, | ||
458 | data->chip_info->num_oversampling_temp_avail); | ||
459 | } | ||
460 | |||
461 | static ssize_t bmp280_show_press_oversampling_avail(struct device *dev, | ||
462 | struct device_attribute *attr, char *buf) | ||
463 | { | ||
464 | struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev)); | ||
465 | |||
466 | return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail, | ||
467 | data->chip_info->num_oversampling_press_avail); | ||
468 | } | ||
469 | |||
470 | static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available, | ||
471 | S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0); | ||
472 | |||
473 | static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available, | ||
474 | S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0); | ||
475 | |||
476 | static struct attribute *bmp280_attributes[] = { | ||
477 | &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr, | ||
478 | &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr, | ||
479 | NULL, | ||
480 | }; | ||
481 | |||
482 | static const struct attribute_group bmp280_attrs_group = { | ||
483 | .attrs = bmp280_attributes, | ||
484 | }; | ||
485 | |||
313 | static const struct iio_info bmp280_info = { | 486 | static const struct iio_info bmp280_info = { |
314 | .driver_module = THIS_MODULE, | 487 | .driver_module = THIS_MODULE, |
315 | .read_raw = &bmp280_read_raw, | 488 | .read_raw = &bmp280_read_raw, |
489 | .write_raw = &bmp280_write_raw, | ||
490 | .attrs = &bmp280_attrs_group, | ||
316 | }; | 491 | }; |
317 | 492 | ||
318 | static int bmp280_chip_init(struct bmp280_data *data) | 493 | static int bmp280_chip_config(struct bmp280_data *data) |
319 | { | 494 | { |
320 | int ret; | 495 | int ret; |
496 | u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) | | ||
497 | BMP280_OSRS_PRESS_X(data->oversampling_press + 1); | ||
321 | 498 | ||
322 | ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, | 499 | ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, |
323 | BMP280_OSRS_TEMP_MASK | | 500 | BMP280_OSRS_TEMP_MASK | |
324 | BMP280_OSRS_PRESS_MASK | | 501 | BMP280_OSRS_PRESS_MASK | |
325 | BMP280_MODE_MASK, | 502 | BMP280_MODE_MASK, |
326 | BMP280_OSRS_TEMP_2X | | 503 | osrs | BMP280_MODE_NORMAL); |
327 | BMP280_OSRS_PRESS_16X | | ||
328 | BMP280_MODE_NORMAL); | ||
329 | if (ret < 0) { | 504 | if (ret < 0) { |
330 | dev_err(&data->client->dev, | 505 | dev_err(&data->client->dev, |
331 | "failed to write ctrl_meas register\n"); | 506 | "failed to write ctrl_meas register\n"); |
@@ -344,6 +519,317 @@ static int bmp280_chip_init(struct bmp280_data *data) | |||
344 | return ret; | 519 | return ret; |
345 | } | 520 | } |
346 | 521 | ||
522 | static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 }; | ||
523 | |||
524 | static const struct bmp280_chip_info bmp280_chip_info = { | ||
525 | .regmap_config = &bmp280_regmap_config, | ||
526 | |||
527 | .oversampling_temp_avail = bmp280_oversampling_avail, | ||
528 | .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail), | ||
529 | |||
530 | .oversampling_press_avail = bmp280_oversampling_avail, | ||
531 | .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail), | ||
532 | |||
533 | .chip_config = bmp280_chip_config, | ||
534 | .read_temp = bmp280_read_temp, | ||
535 | .read_press = bmp280_read_press, | ||
536 | }; | ||
537 | |||
538 | static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg) | ||
539 | { | ||
540 | switch (reg) { | ||
541 | case BMP280_REG_CTRL_MEAS: | ||
542 | case BMP280_REG_RESET: | ||
543 | return true; | ||
544 | default: | ||
545 | return false; | ||
546 | }; | ||
547 | } | ||
548 | |||
549 | static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg) | ||
550 | { | ||
551 | switch (reg) { | ||
552 | case BMP180_REG_OUT_XLSB: | ||
553 | case BMP180_REG_OUT_LSB: | ||
554 | case BMP180_REG_OUT_MSB: | ||
555 | case BMP280_REG_CTRL_MEAS: | ||
556 | return true; | ||
557 | default: | ||
558 | return false; | ||
559 | } | ||
560 | } | ||
561 | |||
562 | static const struct regmap_config bmp180_regmap_config = { | ||
563 | .reg_bits = 8, | ||
564 | .val_bits = 8, | ||
565 | |||
566 | .max_register = BMP180_REG_OUT_XLSB, | ||
567 | .cache_type = REGCACHE_RBTREE, | ||
568 | |||
569 | .writeable_reg = bmp180_is_writeable_reg, | ||
570 | .volatile_reg = bmp180_is_volatile_reg, | ||
571 | }; | ||
572 | |||
573 | static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas) | ||
574 | { | ||
575 | int ret; | ||
576 | const int conversion_time_max[] = { 4500, 7500, 13500, 25500 }; | ||
577 | unsigned int delay_us; | ||
578 | unsigned int ctrl; | ||
579 | |||
580 | ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas); | ||
581 | if (ret) | ||
582 | return ret; | ||
583 | |||
584 | if (ctrl_meas == BMP180_MEAS_TEMP) | ||
585 | delay_us = 4500; | ||
586 | else | ||
587 | delay_us = conversion_time_max[data->oversampling_press]; | ||
588 | |||
589 | usleep_range(delay_us, delay_us + 1000); | ||
590 | |||
591 | ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl); | ||
592 | if (ret) | ||
593 | return ret; | ||
594 | |||
595 | /* The value of this bit reset to "0" after conversion is complete */ | ||
596 | if (ctrl & BMP180_MEAS_SCO) | ||
597 | return -EIO; | ||
598 | |||
599 | return 0; | ||
600 | } | ||
601 | |||
602 | static int bmp180_read_adc_temp(struct bmp280_data *data, int *val) | ||
603 | { | ||
604 | int ret; | ||
605 | __be16 tmp = 0; | ||
606 | |||
607 | ret = bmp180_measure(data, BMP180_MEAS_TEMP); | ||
608 | if (ret) | ||
609 | return ret; | ||
610 | |||
611 | ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2); | ||
612 | if (ret) | ||
613 | return ret; | ||
614 | |||
615 | *val = be16_to_cpu(tmp); | ||
616 | |||
617 | return 0; | ||
618 | } | ||
619 | |||
620 | /* | ||
621 | * These enums are used for indexing into the array of calibration | ||
622 | * coefficients for BMP180. | ||
623 | */ | ||
624 | enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD }; | ||
625 | |||
626 | struct bmp180_calib { | ||
627 | s16 AC1; | ||
628 | s16 AC2; | ||
629 | s16 AC3; | ||
630 | u16 AC4; | ||
631 | u16 AC5; | ||
632 | u16 AC6; | ||
633 | s16 B1; | ||
634 | s16 B2; | ||
635 | s16 MB; | ||
636 | s16 MC; | ||
637 | s16 MD; | ||
638 | }; | ||
639 | |||
640 | static int bmp180_read_calib(struct bmp280_data *data, | ||
641 | struct bmp180_calib *calib) | ||
642 | { | ||
643 | int ret; | ||
644 | int i; | ||
645 | __be16 buf[BMP180_REG_CALIB_COUNT / 2]; | ||
646 | |||
647 | ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf, | ||
648 | sizeof(buf)); | ||
649 | |||
650 | if (ret < 0) | ||
651 | return ret; | ||
652 | |||
653 | /* None of the words has the value 0 or 0xFFFF */ | ||
654 | for (i = 0; i < ARRAY_SIZE(buf); i++) { | ||
655 | if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff)) | ||
656 | return -EIO; | ||
657 | } | ||
658 | |||
659 | calib->AC1 = be16_to_cpu(buf[AC1]); | ||
660 | calib->AC2 = be16_to_cpu(buf[AC2]); | ||
661 | calib->AC3 = be16_to_cpu(buf[AC3]); | ||
662 | calib->AC4 = be16_to_cpu(buf[AC4]); | ||
663 | calib->AC5 = be16_to_cpu(buf[AC5]); | ||
664 | calib->AC6 = be16_to_cpu(buf[AC6]); | ||
665 | calib->B1 = be16_to_cpu(buf[B1]); | ||
666 | calib->B2 = be16_to_cpu(buf[B2]); | ||
667 | calib->MB = be16_to_cpu(buf[MB]); | ||
668 | calib->MC = be16_to_cpu(buf[MC]); | ||
669 | calib->MD = be16_to_cpu(buf[MD]); | ||
670 | |||
671 | return 0; | ||
672 | } | ||
673 | |||
674 | /* | ||
675 | * Returns temperature in DegC, resolution is 0.1 DegC. | ||
676 | * t_fine carries fine temperature as global value. | ||
677 | * | ||
678 | * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". | ||
679 | */ | ||
680 | static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp) | ||
681 | { | ||
682 | int ret; | ||
683 | s32 x1, x2; | ||
684 | struct bmp180_calib calib; | ||
685 | |||
686 | ret = bmp180_read_calib(data, &calib); | ||
687 | if (ret < 0) { | ||
688 | dev_err(&data->client->dev, | ||
689 | "failed to read calibration coefficients\n"); | ||
690 | return ret; | ||
691 | } | ||
692 | |||
693 | x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15; | ||
694 | x2 = (calib.MC << 11) / (x1 + calib.MD); | ||
695 | data->t_fine = x1 + x2; | ||
696 | |||
697 | return (data->t_fine + 8) >> 4; | ||
698 | } | ||
699 | |||
700 | static int bmp180_read_temp(struct bmp280_data *data, int *val) | ||
701 | { | ||
702 | int ret; | ||
703 | s32 adc_temp, comp_temp; | ||
704 | |||
705 | ret = bmp180_read_adc_temp(data, &adc_temp); | ||
706 | if (ret) | ||
707 | return ret; | ||
708 | |||
709 | comp_temp = bmp180_compensate_temp(data, adc_temp); | ||
710 | |||
711 | /* | ||
712 | * val might be NULL if we're called by the read_press routine, | ||
713 | * who only cares about the carry over t_fine value. | ||
714 | */ | ||
715 | if (val) { | ||
716 | *val = comp_temp * 100; | ||
717 | return IIO_VAL_INT; | ||
718 | } | ||
719 | |||
720 | return 0; | ||
721 | } | ||
722 | |||
723 | static int bmp180_read_adc_press(struct bmp280_data *data, int *val) | ||
724 | { | ||
725 | int ret; | ||
726 | __be32 tmp = 0; | ||
727 | u8 oss = data->oversampling_press; | ||
728 | |||
729 | ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss)); | ||
730 | if (ret) | ||
731 | return ret; | ||
732 | |||
733 | ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3); | ||
734 | if (ret) | ||
735 | return ret; | ||
736 | |||
737 | *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss); | ||
738 | |||
739 | return 0; | ||
740 | } | ||
741 | |||
742 | /* | ||
743 | * Returns pressure in Pa, resolution is 1 Pa. | ||
744 | * | ||
745 | * Taken from datasheet, Section 3.5, "Calculating pressure and temperature". | ||
746 | */ | ||
747 | static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press) | ||
748 | { | ||
749 | int ret; | ||
750 | s32 x1, x2, x3, p; | ||
751 | s32 b3, b6; | ||
752 | u32 b4, b7; | ||
753 | s32 oss = data->oversampling_press; | ||
754 | struct bmp180_calib calib; | ||
755 | |||
756 | ret = bmp180_read_calib(data, &calib); | ||
757 | if (ret < 0) { | ||
758 | dev_err(&data->client->dev, | ||
759 | "failed to read calibration coefficients\n"); | ||
760 | return ret; | ||
761 | } | ||
762 | |||
763 | b6 = data->t_fine - 4000; | ||
764 | x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11; | ||
765 | x2 = calib.AC2 * b6 >> 11; | ||
766 | x3 = x1 + x2; | ||
767 | b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4; | ||
768 | x1 = calib.AC3 * b6 >> 13; | ||
769 | x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16; | ||
770 | x3 = (x1 + x2 + 2) >> 2; | ||
771 | b4 = calib.AC4 * (u32)(x3 + 32768) >> 15; | ||
772 | b7 = ((u32)adc_press - b3) * (50000 >> oss); | ||
773 | if (b7 < 0x80000000) | ||
774 | p = (b7 * 2) / b4; | ||
775 | else | ||
776 | p = (b7 / b4) * 2; | ||
777 | |||
778 | x1 = (p >> 8) * (p >> 8); | ||
779 | x1 = (x1 * 3038) >> 16; | ||
780 | x2 = (-7357 * p) >> 16; | ||
781 | |||
782 | return p + ((x1 + x2 + 3791) >> 4); | ||
783 | } | ||
784 | |||
785 | static int bmp180_read_press(struct bmp280_data *data, | ||
786 | int *val, int *val2) | ||
787 | { | ||
788 | int ret; | ||
789 | s32 adc_press; | ||
790 | u32 comp_press; | ||
791 | |||
792 | /* Read and compensate temperature so we get a reading of t_fine. */ | ||
793 | ret = bmp180_read_temp(data, NULL); | ||
794 | if (ret) | ||
795 | return ret; | ||
796 | |||
797 | ret = bmp180_read_adc_press(data, &adc_press); | ||
798 | if (ret) | ||
799 | return ret; | ||
800 | |||
801 | comp_press = bmp180_compensate_press(data, adc_press); | ||
802 | |||
803 | *val = comp_press; | ||
804 | *val2 = 1000; | ||
805 | |||
806 | return IIO_VAL_FRACTIONAL; | ||
807 | } | ||
808 | |||
809 | static int bmp180_chip_config(struct bmp280_data *data) | ||
810 | { | ||
811 | return 0; | ||
812 | } | ||
813 | |||
814 | static const int bmp180_oversampling_temp_avail[] = { 1 }; | ||
815 | static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 }; | ||
816 | |||
817 | static const struct bmp280_chip_info bmp180_chip_info = { | ||
818 | .regmap_config = &bmp180_regmap_config, | ||
819 | |||
820 | .oversampling_temp_avail = bmp180_oversampling_temp_avail, | ||
821 | .num_oversampling_temp_avail = | ||
822 | ARRAY_SIZE(bmp180_oversampling_temp_avail), | ||
823 | |||
824 | .oversampling_press_avail = bmp180_oversampling_press_avail, | ||
825 | .num_oversampling_press_avail = | ||
826 | ARRAY_SIZE(bmp180_oversampling_press_avail), | ||
827 | |||
828 | .chip_config = bmp180_chip_config, | ||
829 | .read_temp = bmp180_read_temp, | ||
830 | .read_press = bmp180_read_press, | ||
831 | }; | ||
832 | |||
347 | static int bmp280_probe(struct i2c_client *client, | 833 | static int bmp280_probe(struct i2c_client *client, |
348 | const struct i2c_device_id *id) | 834 | const struct i2c_device_id *id) |
349 | { | 835 | { |
@@ -367,7 +853,23 @@ static int bmp280_probe(struct i2c_client *client, | |||
367 | indio_dev->info = &bmp280_info; | 853 | indio_dev->info = &bmp280_info; |
368 | indio_dev->modes = INDIO_DIRECT_MODE; | 854 | indio_dev->modes = INDIO_DIRECT_MODE; |
369 | 855 | ||
370 | data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config); | 856 | switch (id->driver_data) { |
857 | case BMP180_CHIP_ID: | ||
858 | data->chip_info = &bmp180_chip_info; | ||
859 | data->oversampling_press = ilog2(8); | ||
860 | data->oversampling_temp = ilog2(1); | ||
861 | break; | ||
862 | case BMP280_CHIP_ID: | ||
863 | data->chip_info = &bmp280_chip_info; | ||
864 | data->oversampling_press = ilog2(16); | ||
865 | data->oversampling_temp = ilog2(2); | ||
866 | break; | ||
867 | default: | ||
868 | return -EINVAL; | ||
869 | } | ||
870 | |||
871 | data->regmap = devm_regmap_init_i2c(client, | ||
872 | data->chip_info->regmap_config); | ||
371 | if (IS_ERR(data->regmap)) { | 873 | if (IS_ERR(data->regmap)) { |
372 | dev_err(&client->dev, "failed to allocate register map\n"); | 874 | dev_err(&client->dev, "failed to allocate register map\n"); |
373 | return PTR_ERR(data->regmap); | 875 | return PTR_ERR(data->regmap); |
@@ -376,13 +878,13 @@ static int bmp280_probe(struct i2c_client *client, | |||
376 | ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); | 878 | ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); |
377 | if (ret < 0) | 879 | if (ret < 0) |
378 | return ret; | 880 | return ret; |
379 | if (chip_id != BMP280_CHIP_ID) { | 881 | if (chip_id != id->driver_data) { |
380 | dev_err(&client->dev, "bad chip id. expected %x got %x\n", | 882 | dev_err(&client->dev, "bad chip id. expected %x got %x\n", |
381 | BMP280_CHIP_ID, chip_id); | 883 | BMP280_CHIP_ID, chip_id); |
382 | return -EINVAL; | 884 | return -EINVAL; |
383 | } | 885 | } |
384 | 886 | ||
385 | ret = bmp280_chip_init(data); | 887 | ret = data->chip_info->chip_config(data); |
386 | if (ret < 0) | 888 | if (ret < 0) |
387 | return ret; | 889 | return ret; |
388 | 890 | ||
@@ -390,13 +892,17 @@ static int bmp280_probe(struct i2c_client *client, | |||
390 | } | 892 | } |
391 | 893 | ||
392 | static const struct acpi_device_id bmp280_acpi_match[] = { | 894 | static const struct acpi_device_id bmp280_acpi_match[] = { |
393 | {"BMP0280", 0}, | 895 | {"BMP0280", BMP280_CHIP_ID }, |
896 | {"BMP0180", BMP180_CHIP_ID }, | ||
897 | {"BMP0085", BMP180_CHIP_ID }, | ||
394 | { }, | 898 | { }, |
395 | }; | 899 | }; |
396 | MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); | 900 | MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); |
397 | 901 | ||
398 | static const struct i2c_device_id bmp280_id[] = { | 902 | static const struct i2c_device_id bmp280_id[] = { |
399 | {"bmp280", 0}, | 903 | {"bmp280", BMP280_CHIP_ID }, |
904 | {"bmp180", BMP180_CHIP_ID }, | ||
905 | {"bmp085", BMP180_CHIP_ID }, | ||
400 | { }, | 906 | { }, |
401 | }; | 907 | }; |
402 | MODULE_DEVICE_TABLE(i2c, bmp280_id); | 908 | MODULE_DEVICE_TABLE(i2c, bmp280_id); |
@@ -412,5 +918,5 @@ static struct i2c_driver bmp280_driver = { | |||
412 | module_i2c_driver(bmp280_driver); | 918 | module_i2c_driver(bmp280_driver); |
413 | 919 | ||
414 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); | 920 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); |
415 | MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor"); | 921 | MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor"); |
416 | MODULE_LICENSE("GPL v2"); | 922 | MODULE_LICENSE("GPL v2"); |
diff --git a/drivers/iio/pressure/hp03.c b/drivers/iio/pressure/hp03.c new file mode 100644 index 000000000000..ac76515d5d49 --- /dev/null +++ b/drivers/iio/pressure/hp03.c | |||
@@ -0,0 +1,312 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2016 Marek Vasut <marex@denx.de> | ||
3 | * | ||
4 | * Driver for Hope RF HP03 digital temperature and pressure sensor. | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License version 2 as | ||
8 | * published by the Free Software Foundation. | ||
9 | */ | ||
10 | |||
11 | #define pr_fmt(fmt) "hp03: " fmt | ||
12 | |||
13 | #include <linux/module.h> | ||
14 | #include <linux/delay.h> | ||
15 | #include <linux/gpio/consumer.h> | ||
16 | #include <linux/i2c.h> | ||
17 | #include <linux/regmap.h> | ||
18 | #include <linux/iio/iio.h> | ||
19 | #include <linux/iio/sysfs.h> | ||
20 | |||
21 | /* | ||
22 | * The HP03 sensor occupies two fixed I2C addresses: | ||
23 | * 0x50 ... read-only EEPROM with calibration data | ||
24 | * 0x77 ... read-write ADC for pressure and temperature | ||
25 | */ | ||
26 | #define HP03_EEPROM_ADDR 0x50 | ||
27 | #define HP03_ADC_ADDR 0x77 | ||
28 | |||
29 | #define HP03_EEPROM_CX_OFFSET 0x10 | ||
30 | #define HP03_EEPROM_AB_OFFSET 0x1e | ||
31 | #define HP03_EEPROM_CD_OFFSET 0x20 | ||
32 | |||
33 | #define HP03_ADC_WRITE_REG 0xff | ||
34 | #define HP03_ADC_READ_REG 0xfd | ||
35 | #define HP03_ADC_READ_PRESSURE 0xf0 /* D1 in datasheet */ | ||
36 | #define HP03_ADC_READ_TEMP 0xe8 /* D2 in datasheet */ | ||
37 | |||
38 | struct hp03_priv { | ||
39 | struct i2c_client *client; | ||
40 | struct mutex lock; | ||
41 | struct gpio_desc *xclr_gpio; | ||
42 | |||
43 | struct i2c_client *eeprom_client; | ||
44 | struct regmap *eeprom_regmap; | ||
45 | |||
46 | s32 pressure; /* kPa */ | ||
47 | s32 temp; /* Deg. C */ | ||
48 | }; | ||
49 | |||
50 | static const struct iio_chan_spec hp03_channels[] = { | ||
51 | { | ||
52 | .type = IIO_PRESSURE, | ||
53 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
54 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | ||
55 | }, | ||
56 | { | ||
57 | .type = IIO_TEMP, | ||
58 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), | ||
59 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), | ||
60 | }, | ||
61 | }; | ||
62 | |||
63 | static bool hp03_is_writeable_reg(struct device *dev, unsigned int reg) | ||
64 | { | ||
65 | return false; | ||
66 | } | ||
67 | |||
68 | static bool hp03_is_volatile_reg(struct device *dev, unsigned int reg) | ||
69 | { | ||
70 | return false; | ||
71 | } | ||
72 | |||
73 | static const struct regmap_config hp03_regmap_config = { | ||
74 | .reg_bits = 8, | ||
75 | .val_bits = 8, | ||
76 | |||
77 | .max_register = HP03_EEPROM_CD_OFFSET + 1, | ||
78 | .cache_type = REGCACHE_RBTREE, | ||
79 | |||
80 | .writeable_reg = hp03_is_writeable_reg, | ||
81 | .volatile_reg = hp03_is_volatile_reg, | ||
82 | }; | ||
83 | |||
84 | static int hp03_get_temp_pressure(struct hp03_priv *priv, const u8 reg) | ||
85 | { | ||
86 | int ret; | ||
87 | |||
88 | ret = i2c_smbus_write_byte_data(priv->client, HP03_ADC_WRITE_REG, reg); | ||
89 | if (ret < 0) | ||
90 | return ret; | ||
91 | |||
92 | msleep(50); /* Wait for conversion to finish */ | ||
93 | |||
94 | return i2c_smbus_read_word_data(priv->client, HP03_ADC_READ_REG); | ||
95 | } | ||
96 | |||
97 | static int hp03_update_temp_pressure(struct hp03_priv *priv) | ||
98 | { | ||
99 | struct device *dev = &priv->client->dev; | ||
100 | u8 coefs[18]; | ||
101 | u16 cx_val[7]; | ||
102 | int ab_val, d1_val, d2_val, diff_val, dut, off, sens, x; | ||
103 | int i, ret; | ||
104 | |||
105 | /* Sample coefficients from EEPROM */ | ||
106 | ret = regmap_bulk_read(priv->eeprom_regmap, HP03_EEPROM_CX_OFFSET, | ||
107 | coefs, sizeof(coefs)); | ||
108 | if (ret < 0) { | ||
109 | dev_err(dev, "Failed to read EEPROM (reg=%02x)\n", | ||
110 | HP03_EEPROM_CX_OFFSET); | ||
111 | return ret; | ||
112 | } | ||
113 | |||
114 | /* Sample Temperature and Pressure */ | ||
115 | gpiod_set_value_cansleep(priv->xclr_gpio, 1); | ||
116 | |||
117 | ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_PRESSURE); | ||
118 | if (ret < 0) { | ||
119 | dev_err(dev, "Failed to read pressure\n"); | ||
120 | goto err_adc; | ||
121 | } | ||
122 | d1_val = ret; | ||
123 | |||
124 | ret = hp03_get_temp_pressure(priv, HP03_ADC_READ_TEMP); | ||
125 | if (ret < 0) { | ||
126 | dev_err(dev, "Failed to read temperature\n"); | ||
127 | goto err_adc; | ||
128 | } | ||
129 | d2_val = ret; | ||
130 | |||
131 | gpiod_set_value_cansleep(priv->xclr_gpio, 0); | ||
132 | |||
133 | /* The Cx coefficients and Temp/Pressure values are MSB first. */ | ||
134 | for (i = 0; i < 7; i++) | ||
135 | cx_val[i] = (coefs[2 * i] << 8) | (coefs[(2 * i) + 1] << 0); | ||
136 | d1_val = ((d1_val >> 8) & 0xff) | ((d1_val & 0xff) << 8); | ||
137 | d2_val = ((d2_val >> 8) & 0xff) | ((d2_val & 0xff) << 8); | ||
138 | |||
139 | /* Coefficient voodoo from the HP03 datasheet. */ | ||
140 | if (d2_val >= cx_val[4]) | ||
141 | ab_val = coefs[14]; /* A-value */ | ||
142 | else | ||
143 | ab_val = coefs[15]; /* B-value */ | ||
144 | |||
145 | diff_val = d2_val - cx_val[4]; | ||
146 | dut = (ab_val * (diff_val >> 7) * (diff_val >> 7)) >> coefs[16]; | ||
147 | dut = diff_val - dut; | ||
148 | |||
149 | off = (cx_val[1] + (((cx_val[3] - 1024) * dut) >> 14)) * 4; | ||
150 | sens = cx_val[0] + ((cx_val[2] * dut) >> 10); | ||
151 | x = ((sens * (d1_val - 7168)) >> 14) - off; | ||
152 | |||
153 | priv->pressure = ((x * 100) >> 5) + (cx_val[6] * 10); | ||
154 | priv->temp = 250 + ((dut * cx_val[5]) >> 16) - (dut >> coefs[17]); | ||
155 | |||
156 | return 0; | ||
157 | |||
158 | err_adc: | ||
159 | gpiod_set_value_cansleep(priv->xclr_gpio, 0); | ||
160 | return ret; | ||
161 | } | ||
162 | |||
163 | static int hp03_read_raw(struct iio_dev *indio_dev, | ||
164 | struct iio_chan_spec const *chan, | ||
165 | int *val, int *val2, long mask) | ||
166 | { | ||
167 | struct hp03_priv *priv = iio_priv(indio_dev); | ||
168 | int ret; | ||
169 | |||
170 | mutex_lock(&priv->lock); | ||
171 | ret = hp03_update_temp_pressure(priv); | ||
172 | mutex_unlock(&priv->lock); | ||
173 | |||
174 | if (ret) | ||
175 | return ret; | ||
176 | |||
177 | switch (mask) { | ||
178 | case IIO_CHAN_INFO_RAW: | ||
179 | switch (chan->type) { | ||
180 | case IIO_PRESSURE: | ||
181 | *val = priv->pressure; | ||
182 | return IIO_VAL_INT; | ||
183 | case IIO_TEMP: | ||
184 | *val = priv->temp; | ||
185 | return IIO_VAL_INT; | ||
186 | default: | ||
187 | return -EINVAL; | ||
188 | } | ||
189 | break; | ||
190 | case IIO_CHAN_INFO_SCALE: | ||
191 | switch (chan->type) { | ||
192 | case IIO_PRESSURE: | ||
193 | *val = 0; | ||
194 | *val2 = 1000; | ||
195 | return IIO_VAL_INT_PLUS_MICRO; | ||
196 | case IIO_TEMP: | ||
197 | *val = 10; | ||
198 | return IIO_VAL_INT; | ||
199 | default: | ||
200 | return -EINVAL; | ||
201 | } | ||
202 | break; | ||
203 | default: | ||
204 | return -EINVAL; | ||
205 | } | ||
206 | |||
207 | return -EINVAL; | ||
208 | } | ||
209 | |||
210 | static const struct iio_info hp03_info = { | ||
211 | .driver_module = THIS_MODULE, | ||
212 | .read_raw = &hp03_read_raw, | ||
213 | }; | ||
214 | |||
215 | static int hp03_probe(struct i2c_client *client, | ||
216 | const struct i2c_device_id *id) | ||
217 | { | ||
218 | struct device *dev = &client->dev; | ||
219 | struct iio_dev *indio_dev; | ||
220 | struct hp03_priv *priv; | ||
221 | int ret; | ||
222 | |||
223 | indio_dev = devm_iio_device_alloc(dev, sizeof(*priv)); | ||
224 | if (!indio_dev) | ||
225 | return -ENOMEM; | ||
226 | |||
227 | priv = iio_priv(indio_dev); | ||
228 | priv->client = client; | ||
229 | mutex_init(&priv->lock); | ||
230 | |||
231 | indio_dev->dev.parent = dev; | ||
232 | indio_dev->name = id->name; | ||
233 | indio_dev->channels = hp03_channels; | ||
234 | indio_dev->num_channels = ARRAY_SIZE(hp03_channels); | ||
235 | indio_dev->info = &hp03_info; | ||
236 | indio_dev->modes = INDIO_DIRECT_MODE; | ||
237 | |||
238 | priv->xclr_gpio = devm_gpiod_get_index(dev, "xclr", 0, GPIOD_OUT_HIGH); | ||
239 | if (IS_ERR(priv->xclr_gpio)) { | ||
240 | dev_err(dev, "Failed to claim XCLR GPIO\n"); | ||
241 | ret = PTR_ERR(priv->xclr_gpio); | ||
242 | return ret; | ||
243 | } | ||
244 | |||
245 | /* | ||
246 | * Allocate another device for the on-sensor EEPROM, | ||
247 | * which has it's dedicated I2C address and contains | ||
248 | * the calibration constants for the sensor. | ||
249 | */ | ||
250 | priv->eeprom_client = i2c_new_dummy(client->adapter, HP03_EEPROM_ADDR); | ||
251 | if (!priv->eeprom_client) { | ||
252 | dev_err(dev, "New EEPROM I2C device failed\n"); | ||
253 | return -ENODEV; | ||
254 | } | ||
255 | |||
256 | priv->eeprom_regmap = regmap_init_i2c(priv->eeprom_client, | ||
257 | &hp03_regmap_config); | ||
258 | if (IS_ERR(priv->eeprom_regmap)) { | ||
259 | dev_err(dev, "Failed to allocate EEPROM regmap\n"); | ||
260 | ret = PTR_ERR(priv->eeprom_regmap); | ||
261 | goto err_cleanup_eeprom_client; | ||
262 | } | ||
263 | |||
264 | ret = iio_device_register(indio_dev); | ||
265 | if (ret) { | ||
266 | dev_err(dev, "Failed to register IIO device\n"); | ||
267 | goto err_cleanup_eeprom_regmap; | ||
268 | } | ||
269 | |||
270 | i2c_set_clientdata(client, indio_dev); | ||
271 | |||
272 | return 0; | ||
273 | |||
274 | err_cleanup_eeprom_regmap: | ||
275 | regmap_exit(priv->eeprom_regmap); | ||
276 | |||
277 | err_cleanup_eeprom_client: | ||
278 | i2c_unregister_device(priv->eeprom_client); | ||
279 | return ret; | ||
280 | } | ||
281 | |||
282 | static int hp03_remove(struct i2c_client *client) | ||
283 | { | ||
284 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | ||
285 | struct hp03_priv *priv = iio_priv(indio_dev); | ||
286 | |||
287 | iio_device_unregister(indio_dev); | ||
288 | regmap_exit(priv->eeprom_regmap); | ||
289 | i2c_unregister_device(priv->eeprom_client); | ||
290 | |||
291 | return 0; | ||
292 | } | ||
293 | |||
294 | static const struct i2c_device_id hp03_id[] = { | ||
295 | { "hp03", 0 }, | ||
296 | { }, | ||
297 | }; | ||
298 | MODULE_DEVICE_TABLE(i2c, hp03_id); | ||
299 | |||
300 | static struct i2c_driver hp03_driver = { | ||
301 | .driver = { | ||
302 | .name = "hp03", | ||
303 | }, | ||
304 | .probe = hp03_probe, | ||
305 | .remove = hp03_remove, | ||
306 | .id_table = hp03_id, | ||
307 | }; | ||
308 | module_i2c_driver(hp03_driver); | ||
309 | |||
310 | MODULE_AUTHOR("Marek Vasut <marex@denx.de>"); | ||
311 | MODULE_DESCRIPTION("Driver for Hope RF HP03 pressure and temperature sensor"); | ||
312 | MODULE_LICENSE("GPL v2"); | ||
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c index c4e65868bc28..76578b07bb6e 100644 --- a/drivers/iio/pressure/ms5611_core.c +++ b/drivers/iio/pressure/ms5611_core.c | |||
@@ -429,7 +429,7 @@ static void ms5611_fini(const struct iio_dev *indio_dev) | |||
429 | } | 429 | } |
430 | 430 | ||
431 | int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, | 431 | int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, |
432 | const char *name, int type) | 432 | const char *name, int type) |
433 | { | 433 | { |
434 | int ret; | 434 | int ret; |
435 | struct ms5611_state *st = iio_priv(indio_dev); | 435 | struct ms5611_state *st = iio_priv(indio_dev); |
diff --git a/drivers/iio/pressure/ms5611_spi.c b/drivers/iio/pressure/ms5611_spi.c index 7600483e8cb4..932e05001e1a 100644 --- a/drivers/iio/pressure/ms5611_spi.c +++ b/drivers/iio/pressure/ms5611_spi.c | |||
@@ -108,7 +108,7 @@ static int ms5611_spi_probe(struct spi_device *spi) | |||
108 | st->client = spi; | 108 | st->client = spi; |
109 | 109 | ||
110 | return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name, | 110 | return ms5611_probe(indio_dev, &spi->dev, spi_get_device_id(spi)->name, |
111 | spi_get_device_id(spi)->driver_data); | 111 | spi_get_device_id(spi)->driver_data); |
112 | } | 112 | } |
113 | 113 | ||
114 | static int ms5611_spi_remove(struct spi_device *spi) | 114 | static int ms5611_spi_remove(struct spi_device *spi) |
diff --git a/drivers/iio/pressure/st_pressure_core.c b/drivers/iio/pressure/st_pressure_core.c index 172393ad34af..9e9b72a8f18f 100644 --- a/drivers/iio/pressure/st_pressure_core.c +++ b/drivers/iio/pressure/st_pressure_core.c | |||
@@ -64,6 +64,8 @@ | |||
64 | #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20 | 64 | #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20 |
65 | #define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22 | 65 | #define ST_PRESS_LPS331AP_IHL_IRQ_ADDR 0x22 |
66 | #define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80 | 66 | #define ST_PRESS_LPS331AP_IHL_IRQ_MASK 0x80 |
67 | #define ST_PRESS_LPS331AP_OD_IRQ_ADDR 0x22 | ||
68 | #define ST_PRESS_LPS331AP_OD_IRQ_MASK 0x40 | ||
67 | #define ST_PRESS_LPS331AP_MULTIREAD_BIT true | 69 | #define ST_PRESS_LPS331AP_MULTIREAD_BIT true |
68 | #define ST_PRESS_LPS331AP_TEMP_OFFSET 42500 | 70 | #define ST_PRESS_LPS331AP_TEMP_OFFSET 42500 |
69 | 71 | ||
@@ -104,6 +106,8 @@ | |||
104 | #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10 | 106 | #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10 |
105 | #define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22 | 107 | #define ST_PRESS_LPS25H_IHL_IRQ_ADDR 0x22 |
106 | #define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80 | 108 | #define ST_PRESS_LPS25H_IHL_IRQ_MASK 0x80 |
109 | #define ST_PRESS_LPS25H_OD_IRQ_ADDR 0x22 | ||
110 | #define ST_PRESS_LPS25H_OD_IRQ_MASK 0x40 | ||
107 | #define ST_PRESS_LPS25H_MULTIREAD_BIT true | 111 | #define ST_PRESS_LPS25H_MULTIREAD_BIT true |
108 | #define ST_PRESS_LPS25H_TEMP_OFFSET 42500 | 112 | #define ST_PRESS_LPS25H_TEMP_OFFSET 42500 |
109 | #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 | 113 | #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 |
@@ -226,6 +230,9 @@ static const struct st_sensor_settings st_press_sensors_settings[] = { | |||
226 | .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, | 230 | .mask_int2 = ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK, |
227 | .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR, | 231 | .addr_ihl = ST_PRESS_LPS331AP_IHL_IRQ_ADDR, |
228 | .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK, | 232 | .mask_ihl = ST_PRESS_LPS331AP_IHL_IRQ_MASK, |
233 | .addr_od = ST_PRESS_LPS331AP_OD_IRQ_ADDR, | ||
234 | .mask_od = ST_PRESS_LPS331AP_OD_IRQ_MASK, | ||
235 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
229 | }, | 236 | }, |
230 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, | 237 | .multi_read_bit = ST_PRESS_LPS331AP_MULTIREAD_BIT, |
231 | .bootime = 2, | 238 | .bootime = 2, |
@@ -312,6 +319,9 @@ static const struct st_sensor_settings st_press_sensors_settings[] = { | |||
312 | .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK, | 319 | .mask_int2 = ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK, |
313 | .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR, | 320 | .addr_ihl = ST_PRESS_LPS25H_IHL_IRQ_ADDR, |
314 | .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK, | 321 | .mask_ihl = ST_PRESS_LPS25H_IHL_IRQ_MASK, |
322 | .addr_od = ST_PRESS_LPS25H_OD_IRQ_ADDR, | ||
323 | .mask_od = ST_PRESS_LPS25H_OD_IRQ_MASK, | ||
324 | .addr_stat_drdy = ST_SENSORS_DEFAULT_STAT_ADDR, | ||
315 | }, | 325 | }, |
316 | .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT, | 326 | .multi_read_bit = ST_PRESS_LPS25H_MULTIREAD_BIT, |
317 | .bootime = 2, | 327 | .bootime = 2, |
diff --git a/drivers/staging/iio/accel/adis16201_core.c b/drivers/staging/iio/accel/adis16201_core.c index 06c0b75ed26a..6f3f8ff2a066 100644 --- a/drivers/staging/iio/accel/adis16201_core.c +++ b/drivers/staging/iio/accel/adis16201_core.c | |||
@@ -167,6 +167,7 @@ static const struct adis_data adis16201_data = { | |||
167 | .diag_stat_reg = ADIS16201_DIAG_STAT, | 167 | .diag_stat_reg = ADIS16201_DIAG_STAT, |
168 | 168 | ||
169 | .self_test_mask = ADIS16201_MSC_CTRL_SELF_TEST_EN, | 169 | .self_test_mask = ADIS16201_MSC_CTRL_SELF_TEST_EN, |
170 | .self_test_no_autoclear = true, | ||
170 | .startup_delay = ADIS16201_STARTUP_DELAY, | 171 | .startup_delay = ADIS16201_STARTUP_DELAY, |
171 | 172 | ||
172 | .status_error_msgs = adis16201_status_error_msgs, | 173 | .status_error_msgs = adis16201_status_error_msgs, |
diff --git a/drivers/staging/iio/accel/adis16203_core.c b/drivers/staging/iio/accel/adis16203_core.c index de5b84ac842b..c70671778bae 100644 --- a/drivers/staging/iio/accel/adis16203_core.c +++ b/drivers/staging/iio/accel/adis16203_core.c | |||
@@ -134,6 +134,7 @@ static const struct adis_data adis16203_data = { | |||
134 | .diag_stat_reg = ADIS16203_DIAG_STAT, | 134 | .diag_stat_reg = ADIS16203_DIAG_STAT, |
135 | 135 | ||
136 | .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN, | 136 | .self_test_mask = ADIS16203_MSC_CTRL_SELF_TEST_EN, |
137 | .self_test_no_autoclear = true, | ||
137 | .startup_delay = ADIS16203_STARTUP_DELAY, | 138 | .startup_delay = ADIS16203_STARTUP_DELAY, |
138 | 139 | ||
139 | .status_error_msgs = adis16203_status_error_msgs, | 140 | .status_error_msgs = adis16203_status_error_msgs, |
diff --git a/drivers/staging/iio/accel/adis16209_core.c b/drivers/staging/iio/accel/adis16209_core.c index 8b42bf8c3f60..8dbad58628a1 100644 --- a/drivers/staging/iio/accel/adis16209_core.c +++ b/drivers/staging/iio/accel/adis16209_core.c | |||
@@ -168,6 +168,7 @@ static const struct adis_data adis16209_data = { | |||
168 | .diag_stat_reg = ADIS16209_DIAG_STAT, | 168 | .diag_stat_reg = ADIS16209_DIAG_STAT, |
169 | 169 | ||
170 | .self_test_mask = ADIS16209_MSC_CTRL_SELF_TEST_EN, | 170 | .self_test_mask = ADIS16209_MSC_CTRL_SELF_TEST_EN, |
171 | .self_test_no_autoclear = true, | ||
171 | .startup_delay = ADIS16209_STARTUP_DELAY, | 172 | .startup_delay = ADIS16209_STARTUP_DELAY, |
172 | 173 | ||
173 | .status_error_msgs = adis16209_status_error_msgs, | 174 | .status_error_msgs = adis16209_status_error_msgs, |
diff --git a/drivers/staging/iio/accel/adis16240_core.c b/drivers/staging/iio/accel/adis16240_core.c index a425e1894863..d5b99e610d08 100644 --- a/drivers/staging/iio/accel/adis16240_core.c +++ b/drivers/staging/iio/accel/adis16240_core.c | |||
@@ -222,6 +222,7 @@ static const struct adis_data adis16240_data = { | |||
222 | .diag_stat_reg = ADIS16240_DIAG_STAT, | 222 | .diag_stat_reg = ADIS16240_DIAG_STAT, |
223 | 223 | ||
224 | .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN, | 224 | .self_test_mask = ADIS16240_MSC_CTRL_SELF_TEST_EN, |
225 | .self_test_no_autoclear = true, | ||
225 | .startup_delay = ADIS16240_STARTUP_DELAY, | 226 | .startup_delay = ADIS16240_STARTUP_DELAY, |
226 | 227 | ||
227 | .status_error_msgs = adis16240_status_error_msgs, | 228 | .status_error_msgs = adis16240_status_error_msgs, |
diff --git a/drivers/staging/iio/adc/ad7606_core.c b/drivers/staging/iio/adc/ad7606_core.c index 6dbc811730ae..f79ee61851f6 100644 --- a/drivers/staging/iio/adc/ad7606_core.c +++ b/drivers/staging/iio/adc/ad7606_core.c | |||
@@ -88,12 +88,12 @@ static int ad7606_read_raw(struct iio_dev *indio_dev, | |||
88 | 88 | ||
89 | switch (m) { | 89 | switch (m) { |
90 | case IIO_CHAN_INFO_RAW: | 90 | case IIO_CHAN_INFO_RAW: |
91 | mutex_lock(&indio_dev->mlock); | 91 | ret = iio_device_claim_direct_mode(indio_dev); |
92 | if (iio_buffer_enabled(indio_dev)) | 92 | if (ret) |
93 | ret = -EBUSY; | 93 | return ret; |
94 | else | 94 | |
95 | ret = ad7606_scan_direct(indio_dev, chan->address); | 95 | ret = ad7606_scan_direct(indio_dev, chan->address); |
96 | mutex_unlock(&indio_dev->mlock); | 96 | iio_device_release_direct_mode(indio_dev); |
97 | 97 | ||
98 | if (ret < 0) | 98 | if (ret < 0) |
99 | return ret; | 99 | return ret; |
diff --git a/include/dt-bindings/iio/adi,ad5592r.h b/include/dt-bindings/iio/adi,ad5592r.h new file mode 100644 index 000000000000..c48aca1dcade --- /dev/null +++ b/include/dt-bindings/iio/adi,ad5592r.h | |||
@@ -0,0 +1,16 @@ | |||
1 | |||
2 | #ifndef _DT_BINDINGS_ADI_AD5592R_H | ||
3 | #define _DT_BINDINGS_ADI_AD5592R_H | ||
4 | |||
5 | #define CH_MODE_UNUSED 0 | ||
6 | #define CH_MODE_ADC 1 | ||
7 | #define CH_MODE_DAC 2 | ||
8 | #define CH_MODE_DAC_AND_ADC 3 | ||
9 | #define CH_MODE_GPIO 8 | ||
10 | |||
11 | #define CH_OFFSTATE_PULLDOWN 0 | ||
12 | #define CH_OFFSTATE_OUT_LOW 1 | ||
13 | #define CH_OFFSTATE_OUT_HIGH 2 | ||
14 | #define CH_OFFSTATE_OUT_TRISTATE 3 | ||
15 | |||
16 | #endif /* _DT_BINDINGS_ADI_AD5592R_H */ | ||
diff --git a/include/linux/iio/common/st_sensors.h b/include/linux/iio/common/st_sensors.h index 6670c3d25c58..d029ffac0d69 100644 --- a/include/linux/iio/common/st_sensors.h +++ b/include/linux/iio/common/st_sensors.h | |||
@@ -37,6 +37,7 @@ | |||
37 | #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20 | 37 | #define ST_SENSORS_DEFAULT_AXIS_ADDR 0x20 |
38 | #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07 | 38 | #define ST_SENSORS_DEFAULT_AXIS_MASK 0x07 |
39 | #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3 | 39 | #define ST_SENSORS_DEFAULT_AXIS_N_BIT 3 |
40 | #define ST_SENSORS_DEFAULT_STAT_ADDR 0x27 | ||
40 | 41 | ||
41 | #define ST_SENSORS_MAX_NAME 17 | 42 | #define ST_SENSORS_MAX_NAME 17 |
42 | #define ST_SENSORS_MAX_4WAI 7 | 43 | #define ST_SENSORS_MAX_4WAI 7 |
@@ -121,6 +122,9 @@ struct st_sensor_bdu { | |||
121 | * @mask_int2: mask to enable/disable IRQ on INT2 pin. | 122 | * @mask_int2: mask to enable/disable IRQ on INT2 pin. |
122 | * @addr_ihl: address to enable/disable active low on the INT lines. | 123 | * @addr_ihl: address to enable/disable active low on the INT lines. |
123 | * @mask_ihl: mask to enable/disable active low on the INT lines. | 124 | * @mask_ihl: mask to enable/disable active low on the INT lines. |
125 | * @addr_od: address to enable/disable Open Drain on the INT lines. | ||
126 | * @mask_od: mask to enable/disable Open Drain on the INT lines. | ||
127 | * @addr_stat_drdy: address to read status of DRDY (data ready) interrupt | ||
124 | * struct ig1 - represents the Interrupt Generator 1 of sensors. | 128 | * struct ig1 - represents the Interrupt Generator 1 of sensors. |
125 | * @en_addr: address of the enable ig1 register. | 129 | * @en_addr: address of the enable ig1 register. |
126 | * @en_mask: mask to write the on/off value for enable. | 130 | * @en_mask: mask to write the on/off value for enable. |
@@ -131,6 +135,9 @@ struct st_sensor_data_ready_irq { | |||
131 | u8 mask_int2; | 135 | u8 mask_int2; |
132 | u8 addr_ihl; | 136 | u8 addr_ihl; |
133 | u8 mask_ihl; | 137 | u8 mask_ihl; |
138 | u8 addr_od; | ||
139 | u8 mask_od; | ||
140 | u8 addr_stat_drdy; | ||
134 | struct { | 141 | struct { |
135 | u8 en_addr; | 142 | u8 en_addr; |
136 | u8 en_mask; | 143 | u8 en_mask; |
@@ -212,6 +219,7 @@ struct st_sensor_settings { | |||
212 | * @odr: Output data rate of the sensor [Hz]. | 219 | * @odr: Output data rate of the sensor [Hz]. |
213 | * num_data_channels: Number of data channels used in buffer. | 220 | * num_data_channels: Number of data channels used in buffer. |
214 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). | 221 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). |
222 | * @int_pin_open_drain: Set the interrupt/DRDY to open drain. | ||
215 | * @get_irq_data_ready: Function to get the IRQ used for data ready signal. | 223 | * @get_irq_data_ready: Function to get the IRQ used for data ready signal. |
216 | * @tf: Transfer function structure used by I/O operations. | 224 | * @tf: Transfer function structure used by I/O operations. |
217 | * @tb: Transfer buffers and mutex used by I/O operations. | 225 | * @tb: Transfer buffers and mutex used by I/O operations. |
@@ -233,6 +241,7 @@ struct st_sensor_data { | |||
233 | unsigned int num_data_channels; | 241 | unsigned int num_data_channels; |
234 | 242 | ||
235 | u8 drdy_int_pin; | 243 | u8 drdy_int_pin; |
244 | bool int_pin_open_drain; | ||
236 | 245 | ||
237 | unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); | 246 | unsigned int (*get_irq_data_ready) (struct iio_dev *indio_dev); |
238 | 247 | ||
diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index fad58671c49e..3d672f72e7ec 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h | |||
@@ -49,6 +49,33 @@ struct iio_channel *iio_channel_get(struct device *dev, | |||
49 | void iio_channel_release(struct iio_channel *chan); | 49 | void iio_channel_release(struct iio_channel *chan); |
50 | 50 | ||
51 | /** | 51 | /** |
52 | * devm_iio_channel_get() - Resource managed version of iio_channel_get(). | ||
53 | * @dev: Pointer to consumer device. Device name must match | ||
54 | * the name of the device as provided in the iio_map | ||
55 | * with which the desired provider to consumer mapping | ||
56 | * was registered. | ||
57 | * @consumer_channel: Unique name to identify the channel on the consumer | ||
58 | * side. This typically describes the channels use within | ||
59 | * the consumer. E.g. 'battery_voltage' | ||
60 | * | ||
61 | * Returns a pointer to negative errno if it is not able to get the iio channel | ||
62 | * otherwise returns valid pointer for iio channel. | ||
63 | * | ||
64 | * The allocated iio channel is automatically released when the device is | ||
65 | * unbound. | ||
66 | */ | ||
67 | struct iio_channel *devm_iio_channel_get(struct device *dev, | ||
68 | const char *consumer_channel); | ||
69 | /** | ||
70 | * devm_iio_channel_release() - Resource managed version of | ||
71 | * iio_channel_release(). | ||
72 | * @dev: Pointer to consumer device for which resource | ||
73 | * is allocared. | ||
74 | * @chan: The channel to be released. | ||
75 | */ | ||
76 | void devm_iio_channel_release(struct device *dev, struct iio_channel *chan); | ||
77 | |||
78 | /** | ||
52 | * iio_channel_get_all() - get all channels associated with a client | 79 | * iio_channel_get_all() - get all channels associated with a client |
53 | * @dev: Pointer to consumer device. | 80 | * @dev: Pointer to consumer device. |
54 | * | 81 | * |
@@ -65,6 +92,32 @@ struct iio_channel *iio_channel_get_all(struct device *dev); | |||
65 | */ | 92 | */ |
66 | void iio_channel_release_all(struct iio_channel *chan); | 93 | void iio_channel_release_all(struct iio_channel *chan); |
67 | 94 | ||
95 | /** | ||
96 | * devm_iio_channel_get_all() - Resource managed version of | ||
97 | * iio_channel_get_all(). | ||
98 | * @dev: Pointer to consumer device. | ||
99 | * | ||
100 | * Returns a pointer to negative errno if it is not able to get the iio channel | ||
101 | * otherwise returns an array of iio_channel structures terminated with one with | ||
102 | * null iio_dev pointer. | ||
103 | * | ||
104 | * This function is used by fairly generic consumers to get all the | ||
105 | * channels registered as having this consumer. | ||
106 | * | ||
107 | * The allocated iio channels are automatically released when the device is | ||
108 | * unbounded. | ||
109 | */ | ||
110 | struct iio_channel *devm_iio_channel_get_all(struct device *dev); | ||
111 | |||
112 | /** | ||
113 | * devm_iio_channel_release_all() - Resource managed version of | ||
114 | * iio_channel_release_all(). | ||
115 | * @dev: Pointer to consumer device for which resource | ||
116 | * is allocared. | ||
117 | * @chan: Array channel to be released. | ||
118 | */ | ||
119 | void devm_iio_channel_release_all(struct device *dev, struct iio_channel *chan); | ||
120 | |||
68 | struct iio_cb_buffer; | 121 | struct iio_cb_buffer; |
69 | /** | 122 | /** |
70 | * iio_channel_get_all_cb() - register callback for triggered capture | 123 | * iio_channel_get_all_cb() - register callback for triggered capture |
diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 0b2773ada0ba..7c29cb0124ae 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h | |||
@@ -148,6 +148,37 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev, | |||
148 | } | 148 | } |
149 | 149 | ||
150 | /** | 150 | /** |
151 | * struct iio_mount_matrix - iio mounting matrix | ||
152 | * @rotation: 3 dimensional space rotation matrix defining sensor alignment with | ||
153 | * main hardware | ||
154 | */ | ||
155 | struct iio_mount_matrix { | ||
156 | const char *rotation[9]; | ||
157 | }; | ||
158 | |||
159 | ssize_t iio_show_mount_matrix(struct iio_dev *indio_dev, uintptr_t priv, | ||
160 | const struct iio_chan_spec *chan, char *buf); | ||
161 | int of_iio_read_mount_matrix(const struct device *dev, const char *propname, | ||
162 | struct iio_mount_matrix *matrix); | ||
163 | |||
164 | typedef const struct iio_mount_matrix * | ||
165 | (iio_get_mount_matrix_t)(const struct iio_dev *indio_dev, | ||
166 | const struct iio_chan_spec *chan); | ||
167 | |||
168 | /** | ||
169 | * IIO_MOUNT_MATRIX() - Initialize mount matrix extended channel attribute | ||
170 | * @_shared: Whether the attribute is shared between all channels | ||
171 | * @_get: Pointer to an iio_get_mount_matrix_t accessor | ||
172 | */ | ||
173 | #define IIO_MOUNT_MATRIX(_shared, _get) \ | ||
174 | { \ | ||
175 | .name = "mount_matrix", \ | ||
176 | .shared = (_shared), \ | ||
177 | .read = iio_show_mount_matrix, \ | ||
178 | .private = (uintptr_t)(_get), \ | ||
179 | } | ||
180 | |||
181 | /** | ||
151 | * struct iio_event_spec - specification for a channel event | 182 | * struct iio_event_spec - specification for a channel event |
152 | * @type: Type of the event | 183 | * @type: Type of the event |
153 | * @dir: Direction of the event | 184 | * @dir: Direction of the event |
diff --git a/include/linux/iio/imu/adis.h b/include/linux/iio/imu/adis.h index fa2d01ef8f55..360da7d18a3d 100644 --- a/include/linux/iio/imu/adis.h +++ b/include/linux/iio/imu/adis.h | |||
@@ -41,6 +41,7 @@ struct adis_data { | |||
41 | unsigned int diag_stat_reg; | 41 | unsigned int diag_stat_reg; |
42 | 42 | ||
43 | unsigned int self_test_mask; | 43 | unsigned int self_test_mask; |
44 | bool self_test_no_autoclear; | ||
44 | unsigned int startup_delay; | 45 | unsigned int startup_delay; |
45 | 46 | ||
46 | const char * const *status_error_msgs; | 47 | const char * const *status_error_msgs; |
diff --git a/include/linux/iio/magnetometer/ak8975.h b/include/linux/iio/magnetometer/ak8975.h new file mode 100644 index 000000000000..c8400959d197 --- /dev/null +++ b/include/linux/iio/magnetometer/ak8975.h | |||
@@ -0,0 +1,16 @@ | |||
1 | #ifndef __IIO_MAGNETOMETER_AK8975_H__ | ||
2 | #define __IIO_MAGNETOMETER_AK8975_H__ | ||
3 | |||
4 | #include <linux/iio/iio.h> | ||
5 | |||
6 | /** | ||
7 | * struct ak8975_platform_data - AK8975 magnetometer driver platform data | ||
8 | * @eoc_gpio: data ready event gpio | ||
9 | * @orientation: mounting matrix relative to main hardware | ||
10 | */ | ||
11 | struct ak8975_platform_data { | ||
12 | int eoc_gpio; | ||
13 | struct iio_mount_matrix orientation; | ||
14 | }; | ||
15 | |||
16 | #endif | ||
diff --git a/include/linux/platform_data/invensense_mpu6050.h b/include/linux/platform_data/invensense_mpu6050.h index ad3aa7b95f35..554b59801aa8 100644 --- a/include/linux/platform_data/invensense_mpu6050.h +++ b/include/linux/platform_data/invensense_mpu6050.h | |||
@@ -16,13 +16,16 @@ | |||
16 | 16 | ||
17 | /** | 17 | /** |
18 | * struct inv_mpu6050_platform_data - Platform data for the mpu driver | 18 | * struct inv_mpu6050_platform_data - Platform data for the mpu driver |
19 | * @orientation: Orientation matrix of the chip | 19 | * @orientation: Orientation matrix of the chip (deprecated in favor of |
20 | * mounting matrix retrieved from device-tree) | ||
20 | * | 21 | * |
21 | * Contains platform specific information on how to configure the MPU6050 to | 22 | * Contains platform specific information on how to configure the MPU6050 to |
22 | * work on this platform. The orientation matricies are 3x3 rotation matricies | 23 | * work on this platform. The orientation matricies are 3x3 rotation matricies |
23 | * that are applied to the data to rotate from the mounting orientation to the | 24 | * that are applied to the data to rotate from the mounting orientation to the |
24 | * platform orientation. The values must be one of 0, 1, or -1 and each row and | 25 | * platform orientation. The values must be one of 0, 1, or -1 and each row and |
25 | * column should have exactly 1 non-zero value. | 26 | * column should have exactly 1 non-zero value. |
27 | * | ||
28 | * Deprecated in favor of mounting matrix retrieved from device-tree. | ||
26 | */ | 29 | */ |
27 | struct inv_mpu6050_platform_data { | 30 | struct inv_mpu6050_platform_data { |
28 | __s8 orientation[9]; | 31 | __s8 orientation[9]; |
diff --git a/include/linux/platform_data/st_sensors_pdata.h b/include/linux/platform_data/st_sensors_pdata.h index 753839187ba0..79b0e4cdb814 100644 --- a/include/linux/platform_data/st_sensors_pdata.h +++ b/include/linux/platform_data/st_sensors_pdata.h | |||
@@ -16,9 +16,11 @@ | |||
16 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). | 16 | * @drdy_int_pin: Redirect DRDY on pin 1 (1) or pin 2 (2). |
17 | * Available only for accelerometer and pressure sensors. | 17 | * Available only for accelerometer and pressure sensors. |
18 | * Accelerometer DRDY on LSM330 available only on pin 1 (see datasheet). | 18 | * Accelerometer DRDY on LSM330 available only on pin 1 (see datasheet). |
19 | * @open_drain: set the interrupt line to be open drain if possible. | ||
19 | */ | 20 | */ |
20 | struct st_sensors_platform_data { | 21 | struct st_sensors_platform_data { |
21 | u8 drdy_int_pin; | 22 | u8 drdy_int_pin; |
23 | bool open_drain; | ||
22 | }; | 24 | }; |
23 | 25 | ||
24 | #endif /* ST_SENSORS_PDATA_H */ | 26 | #endif /* ST_SENSORS_PDATA_H */ |
diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index c42b7f836b48..2429c78de940 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c | |||
@@ -35,6 +35,15 @@ | |||
35 | #include "iio_utils.h" | 35 | #include "iio_utils.h" |
36 | 36 | ||
37 | /** | 37 | /** |
38 | * enum autochan - state for the automatic channel enabling mechanism | ||
39 | */ | ||
40 | enum autochan { | ||
41 | AUTOCHANNELS_DISABLED, | ||
42 | AUTOCHANNELS_ENABLED, | ||
43 | AUTOCHANNELS_ACTIVE, | ||
44 | }; | ||
45 | |||
46 | /** | ||
38 | * size_from_channelarray() - calculate the storage size of a scan | 47 | * size_from_channelarray() - calculate the storage size of a scan |
39 | * @channels: the channel info array | 48 | * @channels: the channel info array |
40 | * @num_channels: number of channels | 49 | * @num_channels: number of channels |
@@ -191,10 +200,51 @@ void process_scan(char *data, | |||
191 | printf("\n"); | 200 | printf("\n"); |
192 | } | 201 | } |
193 | 202 | ||
203 | static int enable_disable_all_channels(char *dev_dir_name, int enable) | ||
204 | { | ||
205 | const struct dirent *ent; | ||
206 | char scanelemdir[256]; | ||
207 | DIR *dp; | ||
208 | int ret; | ||
209 | |||
210 | snprintf(scanelemdir, sizeof(scanelemdir), | ||
211 | FORMAT_SCAN_ELEMENTS_DIR, dev_dir_name); | ||
212 | scanelemdir[sizeof(scanelemdir)-1] = '\0'; | ||
213 | |||
214 | dp = opendir(scanelemdir); | ||
215 | if (!dp) { | ||
216 | fprintf(stderr, "Enabling/disabling channels: can't open %s\n", | ||
217 | scanelemdir); | ||
218 | return -EIO; | ||
219 | } | ||
220 | |||
221 | ret = -ENOENT; | ||
222 | while (ent = readdir(dp), ent) { | ||
223 | if (iioutils_check_suffix(ent->d_name, "_en")) { | ||
224 | printf("%sabling: %s\n", | ||
225 | enable ? "En" : "Dis", | ||
226 | ent->d_name); | ||
227 | ret = write_sysfs_int(ent->d_name, scanelemdir, | ||
228 | enable); | ||
229 | if (ret < 0) | ||
230 | fprintf(stderr, "Failed to enable/disable %s\n", | ||
231 | ent->d_name); | ||
232 | } | ||
233 | } | ||
234 | |||
235 | if (closedir(dp) == -1) { | ||
236 | perror("Enabling/disabling channels: " | ||
237 | "Failed to close directory"); | ||
238 | return -errno; | ||
239 | } | ||
240 | return 0; | ||
241 | } | ||
242 | |||
194 | void print_usage(void) | 243 | void print_usage(void) |
195 | { | 244 | { |
196 | fprintf(stderr, "Usage: generic_buffer [options]...\n" | 245 | fprintf(stderr, "Usage: generic_buffer [options]...\n" |
197 | "Capture, convert and output data from IIO device buffer\n" | 246 | "Capture, convert and output data from IIO device buffer\n" |
247 | " -a Auto-activate all available channels\n" | ||
198 | " -c <n> Do n conversions\n" | 248 | " -c <n> Do n conversions\n" |
199 | " -e Disable wait for event (new data)\n" | 249 | " -e Disable wait for event (new data)\n" |
200 | " -g Use trigger-less mode\n" | 250 | " -g Use trigger-less mode\n" |
@@ -225,12 +275,16 @@ int main(int argc, char **argv) | |||
225 | int scan_size; | 275 | int scan_size; |
226 | int noevents = 0; | 276 | int noevents = 0; |
227 | int notrigger = 0; | 277 | int notrigger = 0; |
278 | enum autochan autochannels = AUTOCHANNELS_DISABLED; | ||
228 | char *dummy; | 279 | char *dummy; |
229 | 280 | ||
230 | struct iio_channel_info *channels; | 281 | struct iio_channel_info *channels; |
231 | 282 | ||
232 | while ((c = getopt(argc, argv, "c:egl:n:t:w:")) != -1) { | 283 | while ((c = getopt(argc, argv, "ac:egl:n:t:w:")) != -1) { |
233 | switch (c) { | 284 | switch (c) { |
285 | case 'a': | ||
286 | autochannels = AUTOCHANNELS_ENABLED; | ||
287 | break; | ||
234 | case 'c': | 288 | case 'c': |
235 | errno = 0; | 289 | errno = 0; |
236 | num_loops = strtoul(optarg, &dummy, 10); | 290 | num_loops = strtoul(optarg, &dummy, 10); |
@@ -340,12 +394,47 @@ int main(int argc, char **argv) | |||
340 | "diag %s\n", dev_dir_name); | 394 | "diag %s\n", dev_dir_name); |
341 | goto error_free_triggername; | 395 | goto error_free_triggername; |
342 | } | 396 | } |
343 | if (!num_channels) { | 397 | if (num_channels && autochannels == AUTOCHANNELS_ENABLED) { |
398 | fprintf(stderr, "Auto-channels selected but some channels " | ||
399 | "are already activated in sysfs\n"); | ||
400 | fprintf(stderr, "Proceeding without activating any channels\n"); | ||
401 | } | ||
402 | |||
403 | if (!num_channels && autochannels == AUTOCHANNELS_ENABLED) { | ||
404 | fprintf(stderr, | ||
405 | "No channels are enabled, enabling all channels\n"); | ||
406 | |||
407 | ret = enable_disable_all_channels(dev_dir_name, 1); | ||
408 | if (ret) { | ||
409 | fprintf(stderr, "Failed to enable all channels\n"); | ||
410 | goto error_free_triggername; | ||
411 | } | ||
412 | |||
413 | /* This flags that we need to disable the channels again */ | ||
414 | autochannels = AUTOCHANNELS_ACTIVE; | ||
415 | |||
416 | ret = build_channel_array(dev_dir_name, &channels, | ||
417 | &num_channels); | ||
418 | if (ret) { | ||
419 | fprintf(stderr, "Problem reading scan element " | ||
420 | "information\n" | ||
421 | "diag %s\n", dev_dir_name); | ||
422 | goto error_disable_channels; | ||
423 | } | ||
424 | if (!num_channels) { | ||
425 | fprintf(stderr, "Still no channels after " | ||
426 | "auto-enabling, giving up\n"); | ||
427 | goto error_disable_channels; | ||
428 | } | ||
429 | } | ||
430 | |||
431 | if (!num_channels && autochannels == AUTOCHANNELS_DISABLED) { | ||
344 | fprintf(stderr, | 432 | fprintf(stderr, |
345 | "No channels are enabled, we have nothing to scan.\n"); | 433 | "No channels are enabled, we have nothing to scan.\n"); |
346 | fprintf(stderr, "Enable channels manually in " | 434 | fprintf(stderr, "Enable channels manually in " |
347 | FORMAT_SCAN_ELEMENTS_DIR | 435 | FORMAT_SCAN_ELEMENTS_DIR |
348 | "/*_en and try again.\n", dev_dir_name); | 436 | "/*_en or pass -a to autoenable channels and " |
437 | "try again.\n", dev_dir_name); | ||
349 | ret = -ENOENT; | 438 | ret = -ENOENT; |
350 | goto error_free_triggername; | 439 | goto error_free_triggername; |
351 | } | 440 | } |
@@ -479,7 +568,12 @@ error_free_channels: | |||
479 | error_free_triggername: | 568 | error_free_triggername: |
480 | if (datardytrigger) | 569 | if (datardytrigger) |
481 | free(trigger_name); | 570 | free(trigger_name); |
482 | 571 | error_disable_channels: | |
572 | if (autochannels == AUTOCHANNELS_ACTIVE) { | ||
573 | ret = enable_disable_all_channels(dev_dir_name, 0); | ||
574 | if (ret) | ||
575 | fprintf(stderr, "Failed to disable all channels\n"); | ||
576 | } | ||
483 | error_free_dev_dir_name: | 577 | error_free_dev_dir_name: |
484 | free(dev_dir_name); | 578 | free(dev_dir_name); |
485 | 579 | ||
diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h index e3503bfe538b..780f2014f8fa 100644 --- a/tools/iio/iio_utils.h +++ b/tools/iio/iio_utils.h | |||
@@ -52,6 +52,13 @@ struct iio_channel_info { | |||
52 | unsigned location; | 52 | unsigned location; |
53 | }; | 53 | }; |
54 | 54 | ||
55 | static inline int iioutils_check_suffix(const char *str, const char *suffix) | ||
56 | { | ||
57 | return strlen(str) >= strlen(suffix) && | ||
58 | strncmp(str+strlen(str)-strlen(suffix), | ||
59 | suffix, strlen(suffix)) == 0; | ||
60 | } | ||
61 | |||
55 | int iioutils_break_up_name(const char *full_name, char **generic_name); | 62 | int iioutils_break_up_name(const char *full_name, char **generic_name); |
56 | int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, | 63 | int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, |
57 | unsigned *shift, uint64_t *mask, unsigned *be, | 64 | unsigned *shift, uint64_t *mask, unsigned *be, |