aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-10-05 17:50:51 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2016-10-05 17:50:51 -0400
commit41844e36206be90cd4d962ea49b0abc3612a99d0 (patch)
treece0b3a3403bc6abdb28f52779d0d7b57a51a5c86 /drivers/iio
parent5691f0e9a3e7855832d5fd094801bf600347c2d0 (diff)
parentfc1e2c8ea85e109acf09e74789e9b852f6eed251 (diff)
Merge tag 'staging-4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging
Pull staging and IIO updates from Greg KH: "Here is the big staging and IIO driver pull request for 4.9-rc1. There are a lot of patches in here, the majority due to the drivers/staging/greybus/ subsystem being merged in with full development history that went back a few years, in order to preserve the work that those developers did over time. Lots and lots of tiny cleanups happened in the tree as well, due to the Outreachy application process and lots of other developers showing up for the first time to clean code up. Along with those changes, we deleted a wireless driver, and added a raspberrypi driver (currently marked broken), and lots of new iio drivers. Overall the tree still shrunk with more lines removed than added, about 10 thousand lines removed in total. Full details are in the very long shortlog below. All of this has been in the linux-next tree with no issues. There will be some merge problems with other subsystem trees, but those are all minor problems and shouldn't be hard to work out when they happen (MAINTAINERS and some lustre build problems with the IB tree)" And furter from me asking for clarification about greybus: "Right now there is a phone from Motorola shipping with this code (a slightly older version, but the same tree), so even though Ara is not alive in the same form, the functionality is happening. We are working with the developers of that phone to merge the newer stuff in with their fork so they can use the upstream version in future versions of their phone product line. Toshiba has at least one chip shipping in their catalog that needs/uses this protocol over a Unipro link, and rumor has it that there might be more in the future. There are also other users of the greybus protocols, there is a talk next week at ELC that shows how it is being used across a network connection to control a device, and previous ELC talks have showed the protocol stack being used over USB to drive embedded Linux boards. I've also talked to some people who are starting to work to add a host controller driver to control arduinos as the greybus PHY protocols are very useful to control a serial/i2c/spio/whatever device across a random physical link, as it is a way to have a self-describing device be attached to a host without needing manual configuration. So yes, people are using it, and there is still the chance that it will show up in a phone/laptop/tablet/whatever from Google in the future as well, the tech isn't dead, even if the original large phone project happens to be" * tag 'staging-4.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/staging: (3703 commits) Staging: fbtft: Fix bug in fbtft-core staging: rtl8188eu: fix double unlock error in rtw_resume_process() staging:r8188eu: remove GEN_MLME_EXT_HANDLER macro staging:r8188eu: remove GEN_DRV_CMD_HANDLER macro staging:r8188eu: remove GEN_EVT_CODE macro staging:r8188eu: remove GEN_CMD_CODE macro staging:r8188eu: remove pkt_newalloc member of the recv_buf structure staging:r8188eu: remove rtw_handle_dualmac declaration staging:r8188eu: remove (RGTRY|BSSID)_(OFT|SZ) macros staging:r8188eu: change rtl8188e_process_phy_info function argument type Staging: fsl-mc: Remove blank lines Staging: fsl-mc: Fix unaligned * in block comments Staging: comedi: Align the * in block comments Staging : ks7010 : Fix block comments warninig Staging: vt6655: Remove explicit NULL comparison using Coccinelle staging: rtl8188eu: core: rtw_xmit: Use macros instead of constants staging: rtl8188eu: core: rtw_xmit: Move constant of the right side staging: dgnc: Fix lines longer than 80 characters Staging: dgnc: constify attribute_group structures Staging: most: hdm-dim2: constify attribute_group structures ...
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig56
-rw-r--r--drivers/iio/accel/Makefile5
-rw-r--r--drivers/iio/accel/bma180.c9
-rw-r--r--drivers/iio/accel/dmard06.c241
-rw-r--r--drivers/iio/accel/dmard09.c157
-rw-r--r--drivers/iio/accel/kxcjk-1013.c1
-rw-r--r--drivers/iio/accel/kxsd9-i2c.c64
-rw-r--r--drivers/iio/accel/kxsd9-spi.c56
-rw-r--r--drivers/iio/accel/kxsd9.c435
-rw-r--r--drivers/iio/accel/kxsd9.h12
-rw-r--r--drivers/iio/accel/mc3230.c211
-rw-r--r--drivers/iio/accel/mma7660.c1
-rw-r--r--drivers/iio/accel/mxc6255.c4
-rw-r--r--drivers/iio/accel/ssp_accel_sensor.c2
-rw-r--r--drivers/iio/adc/Kconfig61
-rw-r--r--drivers/iio/adc/Makefile5
-rw-r--r--drivers/iio/adc/ad7266.c4
-rw-r--r--drivers/iio/adc/ad7298.c20
-rw-r--r--drivers/iio/adc/ad7793.c10
-rw-r--r--drivers/iio/adc/at91_adc.c13
-rw-r--r--drivers/iio/adc/ina2xx-adc.c5
-rw-r--r--drivers/iio/adc/ltc2485.c148
-rw-r--r--drivers/iio/adc/men_z188_adc.c2
-rw-r--r--drivers/iio/adc/mt6577_auxadc.c291
-rw-r--r--drivers/iio/adc/nau7802.c2
-rw-r--r--drivers/iio/adc/stx104.c (renamed from drivers/iio/dac/stx104.c)153
-rw-r--r--drivers/iio/adc/ti-adc12138.c552
-rw-r--r--drivers/iio/adc/ti-adc161s626.c248
-rw-r--r--drivers/iio/adc/ti-ads1015.c2
-rw-r--r--drivers/iio/adc/ti-ads8688.c4
-rw-r--r--drivers/iio/buffer/industrialio-buffer-cb.c24
-rw-r--r--drivers/iio/buffer/industrialio-triggered-buffer.c42
-rw-r--r--drivers/iio/chemical/Kconfig1
-rw-r--r--drivers/iio/chemical/atlas-ph-sensor.c81
-rw-r--r--drivers/iio/chemical/vz89x.c201
-rw-r--r--drivers/iio/common/hid-sensors/hid-sensor-trigger.c29
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_buffer.c2
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_core.c53
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_trigger.c4
-rw-r--r--drivers/iio/dac/Kconfig29
-rw-r--r--drivers/iio/dac/Makefile3
-rw-r--r--drivers/iio/dac/ad5755.c2
-rw-r--r--drivers/iio/dac/ad8801.c239
-rw-r--r--drivers/iio/dac/cio-dac.c144
-rw-r--r--drivers/iio/gyro/ssp_gyro_sensor.c2
-rw-r--r--drivers/iio/humidity/Kconfig8
-rw-r--r--drivers/iio/industrialio-core.c3
-rw-r--r--drivers/iio/industrialio-event.c8
-rw-r--r--drivers/iio/industrialio-trigger.c95
-rw-r--r--drivers/iio/light/Kconfig19
-rw-r--r--drivers/iio/light/Makefile1
-rw-r--r--drivers/iio/light/si1145.c1404
-rw-r--r--drivers/iio/light/us5182d.c2
-rw-r--r--drivers/iio/light/vcnl4000.c72
-rw-r--r--drivers/iio/magnetometer/Kconfig16
-rw-r--r--drivers/iio/magnetometer/Makefile1
-rw-r--r--drivers/iio/magnetometer/ak8974.c860
-rw-r--r--drivers/iio/magnetometer/mag3110.c23
-rw-r--r--drivers/iio/pressure/Kconfig22
-rw-r--r--drivers/iio/pressure/Makefile3
-rw-r--r--drivers/iio/pressure/ms5611_core.c6
-rw-r--r--drivers/iio/pressure/zpa2326.c1735
-rw-r--r--drivers/iio/pressure/zpa2326.h89
-rw-r--r--drivers/iio/pressure/zpa2326_i2c.c99
-rw-r--r--drivers/iio/pressure/zpa2326_spi.c103
-rw-r--r--drivers/iio/proximity/sx9500.c9
-rw-r--r--drivers/iio/temperature/Kconfig16
-rw-r--r--drivers/iio/temperature/Makefile1
-rw-r--r--drivers/iio/temperature/maxim_thermocouple.c281
69 files changed, 8182 insertions, 324 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 78f148ea9d9f..2b791fe1e2bc 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -52,6 +52,27 @@ config BMC150_ACCEL_SPI
52 tristate 52 tristate
53 select REGMAP_SPI 53 select REGMAP_SPI
54 54
55config DMARD06
56 tristate "Domintech DMARD06 Digital Accelerometer Driver"
57 depends on OF || COMPILE_TEST
58 depends on I2C
59 help
60 Say yes here to build support for the Domintech low-g tri-axial
61 digital accelerometers: DMARD05, DMARD06, DMARD07.
62
63 To compile this driver as a module, choose M here: the
64 module will be called dmard06.
65
66config DMARD09
67 tristate "Domintech DMARD09 3-axis Accelerometer Driver"
68 depends on I2C
69 help
70 Say yes here to get support for the Domintech DMARD09 3-axis
71 accelerometer.
72
73 Choosing M will build the driver as a module. If so, the module
74 will be called dmard09.
75
55config HID_SENSOR_ACCEL_3D 76config HID_SENSOR_ACCEL_3D
56 depends on HID_SENSOR_HUB 77 depends on HID_SENSOR_HUB
57 select IIO_BUFFER 78 select IIO_BUFFER
@@ -98,14 +119,35 @@ config IIO_ST_ACCEL_SPI_3AXIS
98 119
99config KXSD9 120config KXSD9
100 tristate "Kionix KXSD9 Accelerometer Driver" 121 tristate "Kionix KXSD9 Accelerometer Driver"
101 depends on SPI 122 select IIO_BUFFER
123 select IIO_TRIGGERED_BUFFER
102 help 124 help
103 Say yes here to build support for the Kionix KXSD9 accelerometer. 125 Say yes here to build support for the Kionix KXSD9 accelerometer.
104 Currently this only supports the device via an SPI interface. 126 It can be accessed using an (optional) SPI or I2C interface.
105 127
106 To compile this driver as a module, choose M here: the module 128 To compile this driver as a module, choose M here: the module
107 will be called kxsd9. 129 will be called kxsd9.
108 130
131config KXSD9_SPI
132 tristate "Kionix KXSD9 SPI transport"
133 depends on KXSD9
134 depends on SPI
135 default KXSD9
136 select REGMAP_SPI
137 help
138 Say yes here to enable the Kionix KXSD9 accelerometer
139 SPI transport channel.
140
141config KXSD9_I2C
142 tristate "Kionix KXSD9 I2C transport"
143 depends on KXSD9
144 depends on I2C
145 default KXSD9
146 select REGMAP_I2C
147 help
148 Say yes here to enable the Kionix KXSD9 accelerometer
149 I2C transport channel.
150
109config KXCJK1013 151config KXCJK1013
110 tristate "Kionix 3-Axis Accelerometer Driver" 152 tristate "Kionix 3-Axis Accelerometer Driver"
111 depends on I2C 153 depends on I2C
@@ -119,6 +161,16 @@ config KXCJK1013
119 To compile this driver as a module, choose M here: the module will 161 To compile this driver as a module, choose M here: the module will
120 be called kxcjk-1013. 162 be called kxcjk-1013.
121 163
164config MC3230
165 tristate "mCube MC3230 Digital Accelerometer Driver"
166 depends on I2C
167 help
168 Say yes here to build support for the mCube MC3230 low-g tri-axial
169 digital accelerometer.
170
171 To compile this driver as a module, choose M here: the
172 module will be called mc3230.
173
122config MMA7455 174config MMA7455
123 tristate 175 tristate
124 select IIO_BUFFER 176 select IIO_BUFFER
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index 6cedbecca2ee..f5d3ddee619e 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -8,9 +8,14 @@ obj-$(CONFIG_BMA220) += bma220_spi.o
8obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o 8obj-$(CONFIG_BMC150_ACCEL) += bmc150-accel-core.o
9obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o 9obj-$(CONFIG_BMC150_ACCEL_I2C) += bmc150-accel-i2c.o
10obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o 10obj-$(CONFIG_BMC150_ACCEL_SPI) += bmc150-accel-spi.o
11obj-$(CONFIG_DMARD06) += dmard06.o
12obj-$(CONFIG_DMARD09) += dmard09.o
11obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o 13obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
12obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o 14obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
13obj-$(CONFIG_KXSD9) += kxsd9.o 15obj-$(CONFIG_KXSD9) += kxsd9.o
16obj-$(CONFIG_KXSD9_SPI) += kxsd9-spi.o
17obj-$(CONFIG_KXSD9_I2C) += kxsd9-i2c.o
18obj-$(CONFIG_MC3230) += mc3230.o
14 19
15obj-$(CONFIG_MMA7455) += mma7455_core.o 20obj-$(CONFIG_MMA7455) += mma7455_core.o
16obj-$(CONFIG_MMA7455_I2C) += mma7455_i2c.o 21obj-$(CONFIG_MMA7455_I2C) += mma7455_i2c.o
diff --git a/drivers/iio/accel/bma180.c b/drivers/iio/accel/bma180.c
index e3f88ba5faf3..0890934ef66f 100644
--- a/drivers/iio/accel/bma180.c
+++ b/drivers/iio/accel/bma180.c
@@ -469,13 +469,14 @@ static int bma180_read_raw(struct iio_dev *indio_dev,
469 469
470 switch (mask) { 470 switch (mask) {
471 case IIO_CHAN_INFO_RAW: 471 case IIO_CHAN_INFO_RAW:
472 ret = iio_device_claim_direct_mode(indio_dev);
473 if (ret)
474 return ret;
475
472 mutex_lock(&data->mutex); 476 mutex_lock(&data->mutex);
473 if (iio_buffer_enabled(indio_dev)) {
474 mutex_unlock(&data->mutex);
475 return -EBUSY;
476 }
477 ret = bma180_get_data_reg(data, chan->scan_index); 477 ret = bma180_get_data_reg(data, chan->scan_index);
478 mutex_unlock(&data->mutex); 478 mutex_unlock(&data->mutex);
479 iio_device_release_direct_mode(indio_dev);
479 if (ret < 0) 480 if (ret < 0)
480 return ret; 481 return ret;
481 *val = sign_extend32(ret >> chan->scan_type.shift, 482 *val = sign_extend32(ret >> chan->scan_type.shift,
diff --git a/drivers/iio/accel/dmard06.c b/drivers/iio/accel/dmard06.c
new file mode 100644
index 000000000000..656ca8e1927f
--- /dev/null
+++ b/drivers/iio/accel/dmard06.c
@@ -0,0 +1,241 @@
1/*
2 * IIO driver for Domintech DMARD06 accelerometer
3 *
4 * Copyright (C) 2016 Aleksei Mamlin <mamlinav@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/i2c.h>
13#include <linux/iio/iio.h>
14
15#define DMARD06_DRV_NAME "dmard06"
16
17/* Device data registers */
18#define DMARD06_CHIP_ID_REG 0x0f
19#define DMARD06_TOUT_REG 0x40
20#define DMARD06_XOUT_REG 0x41
21#define DMARD06_YOUT_REG 0x42
22#define DMARD06_ZOUT_REG 0x43
23#define DMARD06_CTRL1_REG 0x44
24
25/* Device ID value */
26#define DMARD05_CHIP_ID 0x05
27#define DMARD06_CHIP_ID 0x06
28#define DMARD07_CHIP_ID 0x07
29
30/* Device values */
31#define DMARD05_AXIS_SCALE_VAL 15625
32#define DMARD06_AXIS_SCALE_VAL 31250
33#define DMARD06_TEMP_CENTER_VAL 25
34#define DMARD06_SIGN_BIT 7
35
36/* Device power modes */
37#define DMARD06_MODE_NORMAL 0x27
38#define DMARD06_MODE_POWERDOWN 0x00
39
40/* Device channels */
41#define DMARD06_ACCEL_CHANNEL(_axis, _reg) { \
42 .type = IIO_ACCEL, \
43 .address = _reg, \
44 .channel2 = IIO_MOD_##_axis, \
45 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
46 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
47 .modified = 1, \
48}
49
50#define DMARD06_TEMP_CHANNEL(_reg) { \
51 .type = IIO_TEMP, \
52 .address = _reg, \
53 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
54 BIT(IIO_CHAN_INFO_OFFSET), \
55}
56
57struct dmard06_data {
58 struct i2c_client *client;
59 u8 chip_id;
60};
61
62static const struct iio_chan_spec dmard06_channels[] = {
63 DMARD06_ACCEL_CHANNEL(X, DMARD06_XOUT_REG),
64 DMARD06_ACCEL_CHANNEL(Y, DMARD06_YOUT_REG),
65 DMARD06_ACCEL_CHANNEL(Z, DMARD06_ZOUT_REG),
66 DMARD06_TEMP_CHANNEL(DMARD06_TOUT_REG),
67};
68
69static int dmard06_read_raw(struct iio_dev *indio_dev,
70 struct iio_chan_spec const *chan,
71 int *val, int *val2, long mask)
72{
73 struct dmard06_data *dmard06 = iio_priv(indio_dev);
74 int ret;
75
76 switch (mask) {
77 case IIO_CHAN_INFO_RAW:
78 ret = i2c_smbus_read_byte_data(dmard06->client,
79 chan->address);
80 if (ret < 0) {
81 dev_err(&dmard06->client->dev,
82 "Error reading data: %d\n", ret);
83 return ret;
84 }
85
86 *val = sign_extend32(ret, DMARD06_SIGN_BIT);
87
88 if (dmard06->chip_id == DMARD06_CHIP_ID)
89 *val = *val >> 1;
90
91 switch (chan->type) {
92 case IIO_ACCEL:
93 return IIO_VAL_INT;
94 case IIO_TEMP:
95 if (dmard06->chip_id != DMARD06_CHIP_ID)
96 *val = *val / 2;
97 return IIO_VAL_INT;
98 default:
99 return -EINVAL;
100 }
101 case IIO_CHAN_INFO_OFFSET:
102 switch (chan->type) {
103 case IIO_TEMP:
104 *val = DMARD06_TEMP_CENTER_VAL;
105 return IIO_VAL_INT;
106 default:
107 return -EINVAL;
108 }
109 case IIO_CHAN_INFO_SCALE:
110 switch (chan->type) {
111 case IIO_ACCEL:
112 *val = 0;
113 if (dmard06->chip_id == DMARD06_CHIP_ID)
114 *val2 = DMARD06_AXIS_SCALE_VAL;
115 else
116 *val2 = DMARD05_AXIS_SCALE_VAL;
117 return IIO_VAL_INT_PLUS_MICRO;
118 default:
119 return -EINVAL;
120 }
121 default:
122 return -EINVAL;
123 }
124}
125
126static const struct iio_info dmard06_info = {
127 .driver_module = THIS_MODULE,
128 .read_raw = dmard06_read_raw,
129};
130
131static int dmard06_probe(struct i2c_client *client,
132 const struct i2c_device_id *id)
133{
134 int ret;
135 struct iio_dev *indio_dev;
136 struct dmard06_data *dmard06;
137
138 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
139 dev_err(&client->dev, "I2C check functionality failed\n");
140 return -ENXIO;
141 }
142
143 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*dmard06));
144 if (!indio_dev) {
145 dev_err(&client->dev, "Failed to allocate iio device\n");
146 return -ENOMEM;
147 }
148
149 dmard06 = iio_priv(indio_dev);
150 dmard06->client = client;
151
152 ret = i2c_smbus_read_byte_data(dmard06->client, DMARD06_CHIP_ID_REG);
153 if (ret < 0) {
154 dev_err(&client->dev, "Error reading chip id: %d\n", ret);
155 return ret;
156 }
157
158 if (ret != DMARD05_CHIP_ID && ret != DMARD06_CHIP_ID &&
159 ret != DMARD07_CHIP_ID) {
160 dev_err(&client->dev, "Invalid chip id: %02d\n", ret);
161 return -ENODEV;
162 }
163
164 dmard06->chip_id = ret;
165
166 i2c_set_clientdata(client, indio_dev);
167 indio_dev->dev.parent = &client->dev;
168 indio_dev->name = DMARD06_DRV_NAME;
169 indio_dev->modes = INDIO_DIRECT_MODE;
170 indio_dev->channels = dmard06_channels;
171 indio_dev->num_channels = ARRAY_SIZE(dmard06_channels);
172 indio_dev->info = &dmard06_info;
173
174 return devm_iio_device_register(&client->dev, indio_dev);
175}
176
177#ifdef CONFIG_PM_SLEEP
178static int dmard06_suspend(struct device *dev)
179{
180 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
181 struct dmard06_data *dmard06 = iio_priv(indio_dev);
182 int ret;
183
184 ret = i2c_smbus_write_byte_data(dmard06->client, DMARD06_CTRL1_REG,
185 DMARD06_MODE_POWERDOWN);
186 if (ret < 0)
187 return ret;
188
189 return 0;
190}
191
192static int dmard06_resume(struct device *dev)
193{
194 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
195 struct dmard06_data *dmard06 = iio_priv(indio_dev);
196 int ret;
197
198 ret = i2c_smbus_write_byte_data(dmard06->client, DMARD06_CTRL1_REG,
199 DMARD06_MODE_NORMAL);
200 if (ret < 0)
201 return ret;
202
203 return 0;
204}
205
206static SIMPLE_DEV_PM_OPS(dmard06_pm_ops, dmard06_suspend, dmard06_resume);
207#define DMARD06_PM_OPS (&dmard06_pm_ops)
208#else
209#define DMARD06_PM_OPS NULL
210#endif
211
212static const struct i2c_device_id dmard06_id[] = {
213 { "dmard05", 0 },
214 { "dmard06", 0 },
215 { "dmard07", 0 },
216 { }
217};
218MODULE_DEVICE_TABLE(i2c, dmard06_id);
219
220static const struct of_device_id dmard06_of_match[] = {
221 { .compatible = "domintech,dmard05" },
222 { .compatible = "domintech,dmard06" },
223 { .compatible = "domintech,dmard07" },
224 { }
225};
226MODULE_DEVICE_TABLE(of, dmard06_of_match);
227
228static struct i2c_driver dmard06_driver = {
229 .probe = dmard06_probe,
230 .id_table = dmard06_id,
231 .driver = {
232 .name = DMARD06_DRV_NAME,
233 .of_match_table = of_match_ptr(dmard06_of_match),
234 .pm = DMARD06_PM_OPS,
235 },
236};
237module_i2c_driver(dmard06_driver);
238
239MODULE_AUTHOR("Aleksei Mamlin <mamlinav@gmail.com>");
240MODULE_DESCRIPTION("Domintech DMARD06 accelerometer driver");
241MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/dmard09.c b/drivers/iio/accel/dmard09.c
new file mode 100644
index 000000000000..d3a28f96565c
--- /dev/null
+++ b/drivers/iio/accel/dmard09.c
@@ -0,0 +1,157 @@
1/*
2 * IIO driver for the 3-axis accelerometer Domintech DMARD09.
3 *
4 * Copyright (c) 2016, Jelle van der Waa <jelle@vdwaa.nl>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 */
15
16#include <asm/unaligned.h>
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/iio/iio.h>
20
21#define DMARD09_DRV_NAME "dmard09"
22
23#define DMARD09_REG_CHIPID 0x18
24#define DMARD09_REG_STAT 0x0A
25#define DMARD09_REG_X 0x0C
26#define DMARD09_REG_Y 0x0E
27#define DMARD09_REG_Z 0x10
28#define DMARD09_CHIPID 0x95
29
30#define DMARD09_BUF_LEN 8
31#define DMARD09_AXIS_X 0
32#define DMARD09_AXIS_Y 1
33#define DMARD09_AXIS_Z 2
34#define DMARD09_AXIS_X_OFFSET ((DMARD09_AXIS_X + 1) * 2)
35#define DMARD09_AXIS_Y_OFFSET ((DMARD09_AXIS_Y + 1 )* 2)
36#define DMARD09_AXIS_Z_OFFSET ((DMARD09_AXIS_Z + 1) * 2)
37
38struct dmard09_data {
39 struct i2c_client *client;
40};
41
42#define DMARD09_CHANNEL(_axis, offset) { \
43 .type = IIO_ACCEL, \
44 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
45 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
46 .modified = 1, \
47 .address = offset, \
48 .channel2 = IIO_MOD_##_axis, \
49}
50
51static const struct iio_chan_spec dmard09_channels[] = {
52 DMARD09_CHANNEL(X, DMARD09_AXIS_X_OFFSET),
53 DMARD09_CHANNEL(Y, DMARD09_AXIS_Y_OFFSET),
54 DMARD09_CHANNEL(Z, DMARD09_AXIS_Z_OFFSET),
55};
56
57static int dmard09_read_raw(struct iio_dev *indio_dev,
58 struct iio_chan_spec const *chan,
59 int *val, int *val2, long mask)
60{
61 struct dmard09_data *data = iio_priv(indio_dev);
62 u8 buf[DMARD09_BUF_LEN];
63 int ret;
64 s16 accel;
65
66 switch (mask) {
67 case IIO_CHAN_INFO_RAW:
68 /*
69 * Read from the DMAR09_REG_STAT register, since the chip
70 * caches reads from the individual X, Y, Z registers.
71 */
72 ret = i2c_smbus_read_i2c_block_data(data->client,
73 DMARD09_REG_STAT,
74 DMARD09_BUF_LEN, buf);
75 if (ret < 0) {
76 dev_err(&data->client->dev, "Error reading reg %d\n",
77 DMARD09_REG_STAT);
78 return ret;
79 }
80
81 accel = get_unaligned_le16(&buf[chan->address]);
82
83 /* Remove lower 3 bits and sign extend */
84 accel <<= 4;
85 accel >>= 7;
86
87 *val = accel;
88
89 return IIO_VAL_INT;
90 default:
91 return -EINVAL;
92 }
93}
94
95static const struct iio_info dmard09_info = {
96 .driver_module = THIS_MODULE,
97 .read_raw = dmard09_read_raw,
98};
99
100static int dmard09_probe(struct i2c_client *client,
101 const struct i2c_device_id *id)
102{
103 int ret;
104 struct iio_dev *indio_dev;
105 struct dmard09_data *data;
106
107 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
108 if (!indio_dev) {
109 dev_err(&client->dev, "iio allocation failed\n");
110 return -ENOMEM;
111 }
112
113 data = iio_priv(indio_dev);
114 data->client = client;
115
116 ret = i2c_smbus_read_byte_data(data->client, DMARD09_REG_CHIPID);
117 if (ret < 0) {
118 dev_err(&client->dev, "Error reading chip id %d\n", ret);
119 return ret;
120 }
121
122 if (ret != DMARD09_CHIPID) {
123 dev_err(&client->dev, "Invalid chip id %d\n", ret);
124 return -ENODEV;
125 }
126
127 i2c_set_clientdata(client, indio_dev);
128 indio_dev->dev.parent = &client->dev;
129 indio_dev->name = DMARD09_DRV_NAME;
130 indio_dev->modes = INDIO_DIRECT_MODE;
131 indio_dev->channels = dmard09_channels;
132 indio_dev->num_channels = ARRAY_SIZE(dmard09_channels);
133 indio_dev->info = &dmard09_info;
134
135 return devm_iio_device_register(&client->dev, indio_dev);
136}
137
138static const struct i2c_device_id dmard09_id[] = {
139 { "dmard09", 0},
140 { },
141};
142
143MODULE_DEVICE_TABLE(i2c, dmard09_id);
144
145static struct i2c_driver dmard09_driver = {
146 .driver = {
147 .name = DMARD09_DRV_NAME
148 },
149 .probe = dmard09_probe,
150 .id_table = dmard09_id,
151};
152
153module_i2c_driver(dmard09_driver);
154
155MODULE_AUTHOR("Jelle van der Waa <jelle@vdwaa.nl>");
156MODULE_DESCRIPTION("DMARD09 3-axis accelerometer driver");
157MODULE_LICENSE("GPL");
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index 765a72362dc6..3f968c46e667 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -1392,6 +1392,7 @@ static const struct acpi_device_id kx_acpi_match[] = {
1392 {"KXCJ1013", KXCJK1013}, 1392 {"KXCJ1013", KXCJK1013},
1393 {"KXCJ1008", KXCJ91008}, 1393 {"KXCJ1008", KXCJ91008},
1394 {"KXCJ9000", KXCJ91008}, 1394 {"KXCJ9000", KXCJ91008},
1395 {"KIOX000A", KXCJ91008},
1395 {"KXTJ1009", KXTJ21009}, 1396 {"KXTJ1009", KXTJ21009},
1396 {"SMO8500", KXCJ91008}, 1397 {"SMO8500", KXCJ91008},
1397 { }, 1398 { },
diff --git a/drivers/iio/accel/kxsd9-i2c.c b/drivers/iio/accel/kxsd9-i2c.c
new file mode 100644
index 000000000000..95e20855d2ef
--- /dev/null
+++ b/drivers/iio/accel/kxsd9-i2c.c
@@ -0,0 +1,64 @@
1#include <linux/device.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/slab.h>
5#include <linux/i2c.h>
6#include <linux/delay.h>
7#include <linux/regmap.h>
8
9#include "kxsd9.h"
10
11static int kxsd9_i2c_probe(struct i2c_client *i2c,
12 const struct i2c_device_id *id)
13{
14 static const struct regmap_config config = {
15 .reg_bits = 8,
16 .val_bits = 8,
17 .max_register = 0x0e,
18 };
19 struct regmap *regmap;
20
21 regmap = devm_regmap_init_i2c(i2c, &config);
22 if (IS_ERR(regmap)) {
23 dev_err(&i2c->dev, "Failed to register i2c regmap %d\n",
24 (int)PTR_ERR(regmap));
25 return PTR_ERR(regmap);
26 }
27
28 return kxsd9_common_probe(&i2c->dev,
29 regmap,
30 i2c->name);
31}
32
33static int kxsd9_i2c_remove(struct i2c_client *client)
34{
35 return kxsd9_common_remove(&client->dev);
36}
37
38#ifdef CONFIG_OF
39static const struct of_device_id kxsd9_of_match[] = {
40 { .compatible = "kionix,kxsd9", },
41 { },
42};
43MODULE_DEVICE_TABLE(of, kxsd9_of_match);
44#else
45#define kxsd9_of_match NULL
46#endif
47
48static const struct i2c_device_id kxsd9_i2c_id[] = {
49 {"kxsd9", 0},
50 { },
51};
52MODULE_DEVICE_TABLE(i2c, kxsd9_i2c_id);
53
54static struct i2c_driver kxsd9_i2c_driver = {
55 .driver = {
56 .name = "kxsd9",
57 .of_match_table = of_match_ptr(kxsd9_of_match),
58 .pm = &kxsd9_dev_pm_ops,
59 },
60 .probe = kxsd9_i2c_probe,
61 .remove = kxsd9_i2c_remove,
62 .id_table = kxsd9_i2c_id,
63};
64module_i2c_driver(kxsd9_i2c_driver);
diff --git a/drivers/iio/accel/kxsd9-spi.c b/drivers/iio/accel/kxsd9-spi.c
new file mode 100644
index 000000000000..b7d0078fd00e
--- /dev/null
+++ b/drivers/iio/accel/kxsd9-spi.c
@@ -0,0 +1,56 @@
1#include <linux/device.h>
2#include <linux/kernel.h>
3#include <linux/spi/spi.h>
4#include <linux/module.h>
5#include <linux/slab.h>
6#include <linux/regmap.h>
7
8#include "kxsd9.h"
9
10static int kxsd9_spi_probe(struct spi_device *spi)
11{
12 static const struct regmap_config config = {
13 .reg_bits = 8,
14 .val_bits = 8,
15 .max_register = 0x0e,
16 };
17 struct regmap *regmap;
18
19 spi->mode = SPI_MODE_0;
20 regmap = devm_regmap_init_spi(spi, &config);
21 if (IS_ERR(regmap)) {
22 dev_err(&spi->dev, "%s: regmap allocation failed: %ld\n",
23 __func__, PTR_ERR(regmap));
24 return PTR_ERR(regmap);
25 }
26
27 return kxsd9_common_probe(&spi->dev,
28 regmap,
29 spi_get_device_id(spi)->name);
30}
31
32static int kxsd9_spi_remove(struct spi_device *spi)
33{
34 return kxsd9_common_remove(&spi->dev);
35}
36
37static const struct spi_device_id kxsd9_spi_id[] = {
38 {"kxsd9", 0},
39 { },
40};
41MODULE_DEVICE_TABLE(spi, kxsd9_spi_id);
42
43static struct spi_driver kxsd9_spi_driver = {
44 .driver = {
45 .name = "kxsd9",
46 .pm = &kxsd9_dev_pm_ops,
47 },
48 .probe = kxsd9_spi_probe,
49 .remove = kxsd9_spi_remove,
50 .id_table = kxsd9_spi_id,
51};
52module_spi_driver(kxsd9_spi_driver);
53
54MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
55MODULE_DESCRIPTION("Kionix KXSD9 SPI driver");
56MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/kxsd9.c b/drivers/iio/accel/kxsd9.c
index 9d72d4bcf5e9..9af60ac70738 100644
--- a/drivers/iio/accel/kxsd9.c
+++ b/drivers/iio/accel/kxsd9.c
@@ -12,19 +12,25 @@
12 * I have a suitable wire made up. 12 * I have a suitable wire made up.
13 * 13 *
14 * TODO: Support the motion detector 14 * TODO: Support the motion detector
15 * Uses register address incrementing so could have a
16 * heavily optimized ring buffer access function.
17 */ 15 */
18 16
19#include <linux/device.h> 17#include <linux/device.h>
20#include <linux/kernel.h> 18#include <linux/kernel.h>
21#include <linux/spi/spi.h>
22#include <linux/sysfs.h> 19#include <linux/sysfs.h>
23#include <linux/slab.h> 20#include <linux/slab.h>
24#include <linux/module.h> 21#include <linux/module.h>
25 22#include <linux/regmap.h>
23#include <linux/bitops.h>
24#include <linux/delay.h>
25#include <linux/regulator/consumer.h>
26#include <linux/pm_runtime.h>
26#include <linux/iio/iio.h> 27#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h> 28#include <linux/iio/sysfs.h>
29#include <linux/iio/buffer.h>
30#include <linux/iio/triggered_buffer.h>
31#include <linux/iio/trigger_consumer.h>
32
33#include "kxsd9.h"
28 34
29#define KXSD9_REG_X 0x00 35#define KXSD9_REG_X 0x00
30#define KXSD9_REG_Y 0x02 36#define KXSD9_REG_Y 0x02
@@ -33,28 +39,45 @@
33#define KXSD9_REG_RESET 0x0a 39#define KXSD9_REG_RESET 0x0a
34#define KXSD9_REG_CTRL_C 0x0c 40#define KXSD9_REG_CTRL_C 0x0c
35 41
36#define KXSD9_FS_MASK 0x03 42#define KXSD9_CTRL_C_FS_MASK 0x03
43#define KXSD9_CTRL_C_FS_8G 0x00
44#define KXSD9_CTRL_C_FS_6G 0x01
45#define KXSD9_CTRL_C_FS_4G 0x02
46#define KXSD9_CTRL_C_FS_2G 0x03
47#define KXSD9_CTRL_C_MOT_LAT BIT(3)
48#define KXSD9_CTRL_C_MOT_LEV BIT(4)
49#define KXSD9_CTRL_C_LP_MASK 0xe0
50#define KXSD9_CTRL_C_LP_NONE 0x00
51#define KXSD9_CTRL_C_LP_2000HZC BIT(5)
52#define KXSD9_CTRL_C_LP_2000HZB BIT(6)
53#define KXSD9_CTRL_C_LP_2000HZA (BIT(5)|BIT(6))
54#define KXSD9_CTRL_C_LP_1000HZ BIT(7)
55#define KXSD9_CTRL_C_LP_500HZ (BIT(7)|BIT(5))
56#define KXSD9_CTRL_C_LP_100HZ (BIT(7)|BIT(6))
57#define KXSD9_CTRL_C_LP_50HZ (BIT(7)|BIT(6)|BIT(5))
37 58
38#define KXSD9_REG_CTRL_B 0x0d 59#define KXSD9_REG_CTRL_B 0x0d
39#define KXSD9_REG_CTRL_A 0x0e
40 60
41#define KXSD9_READ(a) (0x80 | (a)) 61#define KXSD9_CTRL_B_CLK_HLD BIT(7)
42#define KXSD9_WRITE(a) (a) 62#define KXSD9_CTRL_B_ENABLE BIT(6)
63#define KXSD9_CTRL_B_ST BIT(5) /* Self-test */
64
65#define KXSD9_REG_CTRL_A 0x0e
43 66
44#define KXSD9_STATE_RX_SIZE 2
45#define KXSD9_STATE_TX_SIZE 2
46/** 67/**
47 * struct kxsd9_state - device related storage 68 * struct kxsd9_state - device related storage
48 * @buf_lock: protect the rx and tx buffers. 69 * @dev: pointer to the parent device
49 * @us: spi device 70 * @map: regmap to the device
50 * @rx: single rx buffer storage 71 * @orientation: mounting matrix, flipped axis etc
51 * @tx: single tx buffer storage 72 * @regs: regulators for this device, VDD and IOVDD
52 **/ 73 * @scale: the current scaling setting
74 */
53struct kxsd9_state { 75struct kxsd9_state {
54 struct mutex buf_lock; 76 struct device *dev;
55 struct spi_device *us; 77 struct regmap *map;
56 u8 rx[KXSD9_STATE_RX_SIZE] ____cacheline_aligned; 78 struct iio_mount_matrix orientation;
57 u8 tx[KXSD9_STATE_TX_SIZE]; 79 struct regulator_bulk_data regs[2];
80 u8 scale;
58}; 81};
59 82
60#define KXSD9_SCALE_2G "0.011978" 83#define KXSD9_SCALE_2G "0.011978"
@@ -65,6 +88,14 @@ struct kxsd9_state {
65/* reverse order */ 88/* reverse order */
66static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 }; 89static const int kxsd9_micro_scales[4] = { 47853, 35934, 23927, 11978 };
67 90
91#define KXSD9_ZERO_G_OFFSET -2048
92
93/*
94 * Regulator names
95 */
96static const char kxsd9_reg_vdd[] = "vdd";
97static const char kxsd9_reg_iovdd[] = "iovdd";
98
68static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro) 99static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
69{ 100{
70 int ret, i; 101 int ret, i;
@@ -79,42 +110,17 @@ static int kxsd9_write_scale(struct iio_dev *indio_dev, int micro)
79 if (!foundit) 110 if (!foundit)
80 return -EINVAL; 111 return -EINVAL;
81 112
82 mutex_lock(&st->buf_lock); 113 ret = regmap_update_bits(st->map,
83 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C)); 114 KXSD9_REG_CTRL_C,
115 KXSD9_CTRL_C_FS_MASK,
116 i);
84 if (ret < 0) 117 if (ret < 0)
85 goto error_ret; 118 goto error_ret;
86 st->tx[0] = KXSD9_WRITE(KXSD9_REG_CTRL_C);
87 st->tx[1] = (ret & ~KXSD9_FS_MASK) | i;
88 119
89 ret = spi_write(st->us, st->tx, 2); 120 /* Cached scale when the sensor is powered down */
90error_ret: 121 st->scale = i;
91 mutex_unlock(&st->buf_lock);
92 return ret;
93}
94 122
95static int kxsd9_read(struct iio_dev *indio_dev, u8 address) 123error_ret:
96{
97 int ret;
98 struct kxsd9_state *st = iio_priv(indio_dev);
99 struct spi_transfer xfers[] = {
100 {
101 .bits_per_word = 8,
102 .len = 1,
103 .delay_usecs = 200,
104 .tx_buf = st->tx,
105 }, {
106 .bits_per_word = 8,
107 .len = 2,
108 .rx_buf = st->rx,
109 },
110 };
111
112 mutex_lock(&st->buf_lock);
113 st->tx[0] = KXSD9_READ(address);
114 ret = spi_sync_transfer(st->us, xfers, ARRAY_SIZE(xfers));
115 if (!ret)
116 ret = (((u16)(st->rx[0])) << 8) | (st->rx[1] & 0xF0);
117 mutex_unlock(&st->buf_lock);
118 return ret; 124 return ret;
119} 125}
120 126
@@ -136,6 +142,9 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev,
136 long mask) 142 long mask)
137{ 143{
138 int ret = -EINVAL; 144 int ret = -EINVAL;
145 struct kxsd9_state *st = iio_priv(indio_dev);
146
147 pm_runtime_get_sync(st->dev);
139 148
140 if (mask == IIO_CHAN_INFO_SCALE) { 149 if (mask == IIO_CHAN_INFO_SCALE) {
141 /* Check no integer component */ 150 /* Check no integer component */
@@ -144,6 +153,9 @@ static int kxsd9_write_raw(struct iio_dev *indio_dev,
144 ret = kxsd9_write_scale(indio_dev, val2); 153 ret = kxsd9_write_scale(indio_dev, val2);
145 } 154 }
146 155
156 pm_runtime_mark_last_busy(st->dev);
157 pm_runtime_put_autosuspend(st->dev);
158
147 return ret; 159 return ret;
148} 160}
149 161
@@ -153,46 +165,154 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,
153{ 165{
154 int ret = -EINVAL; 166 int ret = -EINVAL;
155 struct kxsd9_state *st = iio_priv(indio_dev); 167 struct kxsd9_state *st = iio_priv(indio_dev);
168 unsigned int regval;
169 __be16 raw_val;
170 u16 nval;
171
172 pm_runtime_get_sync(st->dev);
156 173
157 switch (mask) { 174 switch (mask) {
158 case IIO_CHAN_INFO_RAW: 175 case IIO_CHAN_INFO_RAW:
159 ret = kxsd9_read(indio_dev, chan->address); 176 ret = regmap_bulk_read(st->map, chan->address, &raw_val,
160 if (ret < 0) 177 sizeof(raw_val));
178 if (ret)
161 goto error_ret; 179 goto error_ret;
162 *val = ret; 180 nval = be16_to_cpu(raw_val);
181 /* Only 12 bits are valid */
182 nval >>= 4;
183 *val = nval;
184 ret = IIO_VAL_INT;
185 break;
186 case IIO_CHAN_INFO_OFFSET:
187 /* This has a bias of -2048 */
188 *val = KXSD9_ZERO_G_OFFSET;
163 ret = IIO_VAL_INT; 189 ret = IIO_VAL_INT;
164 break; 190 break;
165 case IIO_CHAN_INFO_SCALE: 191 case IIO_CHAN_INFO_SCALE:
166 ret = spi_w8r8(st->us, KXSD9_READ(KXSD9_REG_CTRL_C)); 192 ret = regmap_read(st->map,
193 KXSD9_REG_CTRL_C,
194 &regval);
167 if (ret < 0) 195 if (ret < 0)
168 goto error_ret; 196 goto error_ret;
169 *val = 0; 197 *val = 0;
170 *val2 = kxsd9_micro_scales[ret & KXSD9_FS_MASK]; 198 *val2 = kxsd9_micro_scales[regval & KXSD9_CTRL_C_FS_MASK];
171 ret = IIO_VAL_INT_PLUS_MICRO; 199 ret = IIO_VAL_INT_PLUS_MICRO;
172 break; 200 break;
173 } 201 }
174 202
175error_ret: 203error_ret:
204 pm_runtime_mark_last_busy(st->dev);
205 pm_runtime_put_autosuspend(st->dev);
206
176 return ret; 207 return ret;
177}; 208};
178#define KXSD9_ACCEL_CHAN(axis) \ 209
210static irqreturn_t kxsd9_trigger_handler(int irq, void *p)
211{
212 const struct iio_poll_func *pf = p;
213 struct iio_dev *indio_dev = pf->indio_dev;
214 struct kxsd9_state *st = iio_priv(indio_dev);
215 int ret;
216 /* 4 * 16bit values AND timestamp */
217 __be16 hw_values[8];
218
219 ret = regmap_bulk_read(st->map,
220 KXSD9_REG_X,
221 &hw_values,
222 8);
223 if (ret) {
224 dev_err(st->dev,
225 "error reading data\n");
226 return ret;
227 }
228
229 iio_push_to_buffers_with_timestamp(indio_dev,
230 hw_values,
231 iio_get_time_ns(indio_dev));
232 iio_trigger_notify_done(indio_dev->trig);
233
234 return IRQ_HANDLED;
235}
236
237static int kxsd9_buffer_preenable(struct iio_dev *indio_dev)
238{
239 struct kxsd9_state *st = iio_priv(indio_dev);
240
241 pm_runtime_get_sync(st->dev);
242
243 return 0;
244}
245
246static int kxsd9_buffer_postdisable(struct iio_dev *indio_dev)
247{
248 struct kxsd9_state *st = iio_priv(indio_dev);
249
250 pm_runtime_mark_last_busy(st->dev);
251 pm_runtime_put_autosuspend(st->dev);
252
253 return 0;
254}
255
256static const struct iio_buffer_setup_ops kxsd9_buffer_setup_ops = {
257 .preenable = kxsd9_buffer_preenable,
258 .postenable = iio_triggered_buffer_postenable,
259 .predisable = iio_triggered_buffer_predisable,
260 .postdisable = kxsd9_buffer_postdisable,
261};
262
263static const struct iio_mount_matrix *
264kxsd9_get_mount_matrix(const struct iio_dev *indio_dev,
265 const struct iio_chan_spec *chan)
266{
267 struct kxsd9_state *st = iio_priv(indio_dev);
268
269 return &st->orientation;
270}
271
272static const struct iio_chan_spec_ext_info kxsd9_ext_info[] = {
273 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, kxsd9_get_mount_matrix),
274 { },
275};
276
277#define KXSD9_ACCEL_CHAN(axis, index) \
179 { \ 278 { \
180 .type = IIO_ACCEL, \ 279 .type = IIO_ACCEL, \
181 .modified = 1, \ 280 .modified = 1, \
182 .channel2 = IIO_MOD_##axis, \ 281 .channel2 = IIO_MOD_##axis, \
183 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 282 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
184 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 283 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
284 BIT(IIO_CHAN_INFO_OFFSET), \
285 .ext_info = kxsd9_ext_info, \
185 .address = KXSD9_REG_##axis, \ 286 .address = KXSD9_REG_##axis, \
287 .scan_index = index, \
288 .scan_type = { \
289 .sign = 'u', \
290 .realbits = 12, \
291 .storagebits = 16, \
292 .shift = 4, \
293 .endianness = IIO_BE, \
294 }, \
186 } 295 }
187 296
188static const struct iio_chan_spec kxsd9_channels[] = { 297static const struct iio_chan_spec kxsd9_channels[] = {
189 KXSD9_ACCEL_CHAN(X), KXSD9_ACCEL_CHAN(Y), KXSD9_ACCEL_CHAN(Z), 298 KXSD9_ACCEL_CHAN(X, 0),
299 KXSD9_ACCEL_CHAN(Y, 1),
300 KXSD9_ACCEL_CHAN(Z, 2),
190 { 301 {
191 .type = IIO_VOLTAGE, 302 .type = IIO_VOLTAGE,
192 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 303 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
193 .indexed = 1, 304 .indexed = 1,
194 .address = KXSD9_REG_AUX, 305 .address = KXSD9_REG_AUX,
195 } 306 .scan_index = 3,
307 .scan_type = {
308 .sign = 'u',
309 .realbits = 12,
310 .storagebits = 16,
311 .shift = 4,
312 .endianness = IIO_BE,
313 },
314 },
315 IIO_CHAN_SOFT_TIMESTAMP(4),
196}; 316};
197 317
198static const struct attribute_group kxsd9_attribute_group = { 318static const struct attribute_group kxsd9_attribute_group = {
@@ -203,17 +323,69 @@ static int kxsd9_power_up(struct kxsd9_state *st)
203{ 323{
204 int ret; 324 int ret;
205 325
206 st->tx[0] = 0x0d; 326 /* Enable the regulators */
207 st->tx[1] = 0x40; 327 ret = regulator_bulk_enable(ARRAY_SIZE(st->regs), st->regs);
208 ret = spi_write(st->us, st->tx, 2); 328 if (ret) {
329 dev_err(st->dev, "Cannot enable regulators\n");
330 return ret;
331 }
332
333 /* Power up */
334 ret = regmap_write(st->map,
335 KXSD9_REG_CTRL_B,
336 KXSD9_CTRL_B_ENABLE);
209 if (ret) 337 if (ret)
210 return ret; 338 return ret;
211 339
212 st->tx[0] = 0x0c; 340 /*
213 st->tx[1] = 0x9b; 341 * Set 1000Hz LPF, 2g fullscale, motion wakeup threshold 1g,
214 return spi_write(st->us, st->tx, 2); 342 * latched wakeup
343 */
344 ret = regmap_write(st->map,
345 KXSD9_REG_CTRL_C,
346 KXSD9_CTRL_C_LP_1000HZ |
347 KXSD9_CTRL_C_MOT_LEV |
348 KXSD9_CTRL_C_MOT_LAT |
349 st->scale);
350 if (ret)
351 return ret;
352
353 /*
354 * Power-up time depends on the LPF setting, but typ 15.9 ms, let's
355 * set 20 ms to allow for some slack.
356 */
357 msleep(20);
358
359 return 0;
215}; 360};
216 361
362static int kxsd9_power_down(struct kxsd9_state *st)
363{
364 int ret;
365
366 /*
367 * Set into low power mode - since there may be more users of the
368 * regulators this is the first step of the power saving: it will
369 * make sure we conserve power even if there are others users on the
370 * regulators.
371 */
372 ret = regmap_update_bits(st->map,
373 KXSD9_REG_CTRL_B,
374 KXSD9_CTRL_B_ENABLE,
375 0);
376 if (ret)
377 return ret;
378
379 /* Disable the regulators */
380 ret = regulator_bulk_disable(ARRAY_SIZE(st->regs), st->regs);
381 if (ret) {
382 dev_err(st->dev, "Cannot disable regulators\n");
383 return ret;
384 }
385
386 return 0;
387}
388
217static const struct iio_info kxsd9_info = { 389static const struct iio_info kxsd9_info = {
218 .read_raw = &kxsd9_read_raw, 390 .read_raw = &kxsd9_read_raw,
219 .write_raw = &kxsd9_write_raw, 391 .write_raw = &kxsd9_write_raw,
@@ -221,57 +393,136 @@ static const struct iio_info kxsd9_info = {
221 .driver_module = THIS_MODULE, 393 .driver_module = THIS_MODULE,
222}; 394};
223 395
224static int kxsd9_probe(struct spi_device *spi) 396/* Four channels apart from timestamp, scan mask = 0x0f */
397static const unsigned long kxsd9_scan_masks[] = { 0xf, 0 };
398
399int kxsd9_common_probe(struct device *dev,
400 struct regmap *map,
401 const char *name)
225{ 402{
226 struct iio_dev *indio_dev; 403 struct iio_dev *indio_dev;
227 struct kxsd9_state *st; 404 struct kxsd9_state *st;
405 int ret;
228 406
229 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 407 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
230 if (!indio_dev) 408 if (!indio_dev)
231 return -ENOMEM; 409 return -ENOMEM;
232 410
233 st = iio_priv(indio_dev); 411 st = iio_priv(indio_dev);
234 spi_set_drvdata(spi, indio_dev); 412 st->dev = dev;
413 st->map = map;
235 414
236 st->us = spi;
237 mutex_init(&st->buf_lock);
238 indio_dev->channels = kxsd9_channels; 415 indio_dev->channels = kxsd9_channels;
239 indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels); 416 indio_dev->num_channels = ARRAY_SIZE(kxsd9_channels);
240 indio_dev->name = spi_get_device_id(spi)->name; 417 indio_dev->name = name;
241 indio_dev->dev.parent = &spi->dev; 418 indio_dev->dev.parent = dev;
242 indio_dev->info = &kxsd9_info; 419 indio_dev->info = &kxsd9_info;
243 indio_dev->modes = INDIO_DIRECT_MODE; 420 indio_dev->modes = INDIO_DIRECT_MODE;
421 indio_dev->available_scan_masks = kxsd9_scan_masks;
422
423 /* Read the mounting matrix, if present */
424 ret = of_iio_read_mount_matrix(dev,
425 "mount-matrix",
426 &st->orientation);
427 if (ret)
428 return ret;
429
430 /* Fetch and turn on regulators */
431 st->regs[0].supply = kxsd9_reg_vdd;
432 st->regs[1].supply = kxsd9_reg_iovdd;
433 ret = devm_regulator_bulk_get(dev,
434 ARRAY_SIZE(st->regs),
435 st->regs);
436 if (ret) {
437 dev_err(dev, "Cannot get regulators\n");
438 return ret;
439 }
440 /* Default scaling */
441 st->scale = KXSD9_CTRL_C_FS_2G;
244 442
245 spi->mode = SPI_MODE_0;
246 spi_setup(spi);
247 kxsd9_power_up(st); 443 kxsd9_power_up(st);
248 444
249 return iio_device_register(indio_dev); 445 ret = iio_triggered_buffer_setup(indio_dev,
446 iio_pollfunc_store_time,
447 kxsd9_trigger_handler,
448 &kxsd9_buffer_setup_ops);
449 if (ret) {
450 dev_err(dev, "triggered buffer setup failed\n");
451 goto err_power_down;
452 }
453
454 ret = iio_device_register(indio_dev);
455 if (ret)
456 goto err_cleanup_buffer;
457
458 dev_set_drvdata(dev, indio_dev);
459
460 /* Enable runtime PM */
461 pm_runtime_get_noresume(dev);
462 pm_runtime_set_active(dev);
463 pm_runtime_enable(dev);
464 /*
465 * Set autosuspend to two orders of magnitude larger than the
466 * start-up time. 20ms start-up time means 2000ms autosuspend,
467 * i.e. 2 seconds.
468 */
469 pm_runtime_set_autosuspend_delay(dev, 2000);
470 pm_runtime_use_autosuspend(dev);
471 pm_runtime_put(dev);
472
473 return 0;
474
475err_cleanup_buffer:
476 iio_triggered_buffer_cleanup(indio_dev);
477err_power_down:
478 kxsd9_power_down(st);
479
480 return ret;
250} 481}
482EXPORT_SYMBOL(kxsd9_common_probe);
251 483
252static int kxsd9_remove(struct spi_device *spi) 484int kxsd9_common_remove(struct device *dev)
253{ 485{
254 iio_device_unregister(spi_get_drvdata(spi)); 486 struct iio_dev *indio_dev = dev_get_drvdata(dev);
487 struct kxsd9_state *st = iio_priv(indio_dev);
488
489 iio_triggered_buffer_cleanup(indio_dev);
490 iio_device_unregister(indio_dev);
491 pm_runtime_get_sync(dev);
492 pm_runtime_put_noidle(dev);
493 pm_runtime_disable(dev);
494 kxsd9_power_down(st);
255 495
256 return 0; 496 return 0;
257} 497}
498EXPORT_SYMBOL(kxsd9_common_remove);
258 499
259static const struct spi_device_id kxsd9_id[] = { 500#ifdef CONFIG_PM
260 {"kxsd9", 0}, 501static int kxsd9_runtime_suspend(struct device *dev)
261 { }, 502{
262}; 503 struct iio_dev *indio_dev = dev_get_drvdata(dev);
263MODULE_DEVICE_TABLE(spi, kxsd9_id); 504 struct kxsd9_state *st = iio_priv(indio_dev);
264 505
265static struct spi_driver kxsd9_driver = { 506 return kxsd9_power_down(st);
266 .driver = { 507}
267 .name = "kxsd9", 508
268 }, 509static int kxsd9_runtime_resume(struct device *dev)
269 .probe = kxsd9_probe, 510{
270 .remove = kxsd9_remove, 511 struct iio_dev *indio_dev = dev_get_drvdata(dev);
271 .id_table = kxsd9_id, 512 struct kxsd9_state *st = iio_priv(indio_dev);
513
514 return kxsd9_power_up(st);
515}
516#endif /* CONFIG_PM */
517
518const struct dev_pm_ops kxsd9_dev_pm_ops = {
519 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
520 pm_runtime_force_resume)
521 SET_RUNTIME_PM_OPS(kxsd9_runtime_suspend,
522 kxsd9_runtime_resume, NULL)
272}; 523};
273module_spi_driver(kxsd9_driver); 524EXPORT_SYMBOL(kxsd9_dev_pm_ops);
274 525
275MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 526MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
276MODULE_DESCRIPTION("Kionix KXSD9 SPI driver"); 527MODULE_DESCRIPTION("Kionix KXSD9 driver");
277MODULE_LICENSE("GPL v2"); 528MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/kxsd9.h b/drivers/iio/accel/kxsd9.h
new file mode 100644
index 000000000000..7e8a28168310
--- /dev/null
+++ b/drivers/iio/accel/kxsd9.h
@@ -0,0 +1,12 @@
1#include <linux/device.h>
2#include <linux/kernel.h>
3
4#define KXSD9_STATE_RX_SIZE 2
5#define KXSD9_STATE_TX_SIZE 2
6
7int kxsd9_common_probe(struct device *dev,
8 struct regmap *map,
9 const char *name);
10int kxsd9_common_remove(struct device *dev);
11
12extern const struct dev_pm_ops kxsd9_dev_pm_ops;
diff --git a/drivers/iio/accel/mc3230.c b/drivers/iio/accel/mc3230.c
new file mode 100644
index 000000000000..4ea2ff623a6d
--- /dev/null
+++ b/drivers/iio/accel/mc3230.c
@@ -0,0 +1,211 @@
1/**
2 * mCube MC3230 3-Axis Accelerometer
3 *
4 * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * IIO driver for mCube MC3230; 7-bit I2C address: 0x4c.
12 */
13
14#include <linux/i2c.h>
15#include <linux/module.h>
16#include <linux/iio/iio.h>
17#include <linux/iio/sysfs.h>
18
19#define MC3230_REG_XOUT 0x00
20#define MC3230_REG_YOUT 0x01
21#define MC3230_REG_ZOUT 0x02
22
23#define MC3230_REG_MODE 0x07
24#define MC3230_MODE_OPCON_MASK 0x03
25#define MC3230_MODE_OPCON_WAKE 0x01
26#define MC3230_MODE_OPCON_STANDBY 0x03
27
28#define MC3230_REG_CHIP_ID 0x18
29#define MC3230_CHIP_ID 0x01
30
31#define MC3230_REG_PRODUCT_CODE 0x3b
32#define MC3230_PRODUCT_CODE 0x19
33
34/*
35 * The accelerometer has one measurement range:
36 *
37 * -1.5g - +1.5g (8-bit, signed)
38 *
39 * scale = (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765
40 */
41
42static const int mc3230_nscale = 115411765;
43
44#define MC3230_CHANNEL(reg, axis) { \
45 .type = IIO_ACCEL, \
46 .address = reg, \
47 .modified = 1, \
48 .channel2 = IIO_MOD_##axis, \
49 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
50 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
51}
52
53static const struct iio_chan_spec mc3230_channels[] = {
54 MC3230_CHANNEL(MC3230_REG_XOUT, X),
55 MC3230_CHANNEL(MC3230_REG_YOUT, Y),
56 MC3230_CHANNEL(MC3230_REG_ZOUT, Z),
57};
58
59struct mc3230_data {
60 struct i2c_client *client;
61};
62
63static int mc3230_set_opcon(struct mc3230_data *data, int opcon)
64{
65 int ret;
66 struct i2c_client *client = data->client;
67
68 ret = i2c_smbus_read_byte_data(client, MC3230_REG_MODE);
69 if (ret < 0) {
70 dev_err(&client->dev, "failed to read mode reg: %d\n", ret);
71 return ret;
72 }
73
74 ret &= ~MC3230_MODE_OPCON_MASK;
75 ret |= opcon;
76
77 ret = i2c_smbus_write_byte_data(client, MC3230_REG_MODE, ret);
78 if (ret < 0) {
79 dev_err(&client->dev, "failed to write mode reg: %d\n", ret);
80 return ret;
81 }
82
83 return 0;
84}
85
86static int mc3230_read_raw(struct iio_dev *indio_dev,
87 struct iio_chan_spec const *chan,
88 int *val, int *val2, long mask)
89{
90 struct mc3230_data *data = iio_priv(indio_dev);
91 int ret;
92
93 switch (mask) {
94 case IIO_CHAN_INFO_RAW:
95 ret = i2c_smbus_read_byte_data(data->client, chan->address);
96 if (ret < 0)
97 return ret;
98 *val = sign_extend32(ret, 7);
99 return IIO_VAL_INT;
100 case IIO_CHAN_INFO_SCALE:
101 *val = 0;
102 *val2 = mc3230_nscale;
103 return IIO_VAL_INT_PLUS_NANO;
104 default:
105 return -EINVAL;
106 }
107}
108
109static const struct iio_info mc3230_info = {
110 .driver_module = THIS_MODULE,
111 .read_raw = mc3230_read_raw,
112};
113
114static int mc3230_probe(struct i2c_client *client,
115 const struct i2c_device_id *id)
116{
117 int ret;
118 struct iio_dev *indio_dev;
119 struct mc3230_data *data;
120
121 /* First check chip-id and product-id */
122 ret = i2c_smbus_read_byte_data(client, MC3230_REG_CHIP_ID);
123 if (ret != MC3230_CHIP_ID)
124 return (ret < 0) ? ret : -ENODEV;
125
126 ret = i2c_smbus_read_byte_data(client, MC3230_REG_PRODUCT_CODE);
127 if (ret != MC3230_PRODUCT_CODE)
128 return (ret < 0) ? ret : -ENODEV;
129
130 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
131 if (!indio_dev) {
132 dev_err(&client->dev, "iio allocation failed!\n");
133 return -ENOMEM;
134 }
135
136 data = iio_priv(indio_dev);
137 data->client = client;
138 i2c_set_clientdata(client, indio_dev);
139
140 indio_dev->dev.parent = &client->dev;
141 indio_dev->info = &mc3230_info;
142 indio_dev->name = "mc3230";
143 indio_dev->modes = INDIO_DIRECT_MODE;
144 indio_dev->channels = mc3230_channels;
145 indio_dev->num_channels = ARRAY_SIZE(mc3230_channels);
146
147 ret = mc3230_set_opcon(data, MC3230_MODE_OPCON_WAKE);
148 if (ret < 0)
149 return ret;
150
151 ret = iio_device_register(indio_dev);
152 if (ret < 0) {
153 dev_err(&client->dev, "device_register failed\n");
154 mc3230_set_opcon(data, MC3230_MODE_OPCON_STANDBY);
155 }
156
157 return ret;
158}
159
160static int mc3230_remove(struct i2c_client *client)
161{
162 struct iio_dev *indio_dev = i2c_get_clientdata(client);
163
164 iio_device_unregister(indio_dev);
165
166 return mc3230_set_opcon(iio_priv(indio_dev), MC3230_MODE_OPCON_STANDBY);
167}
168
169#ifdef CONFIG_PM_SLEEP
170static int mc3230_suspend(struct device *dev)
171{
172 struct mc3230_data *data;
173
174 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
175
176 return mc3230_set_opcon(data, MC3230_MODE_OPCON_STANDBY);
177}
178
179static int mc3230_resume(struct device *dev)
180{
181 struct mc3230_data *data;
182
183 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
184
185 return mc3230_set_opcon(data, MC3230_MODE_OPCON_WAKE);
186}
187#endif
188
189static SIMPLE_DEV_PM_OPS(mc3230_pm_ops, mc3230_suspend, mc3230_resume);
190
191static const struct i2c_device_id mc3230_i2c_id[] = {
192 {"mc3230", 0},
193 {}
194};
195MODULE_DEVICE_TABLE(i2c, mc3230_i2c_id);
196
197static struct i2c_driver mc3230_driver = {
198 .driver = {
199 .name = "mc3230",
200 .pm = &mc3230_pm_ops,
201 },
202 .probe = mc3230_probe,
203 .remove = mc3230_remove,
204 .id_table = mc3230_i2c_id,
205};
206
207module_i2c_driver(mc3230_driver);
208
209MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
210MODULE_DESCRIPTION("mCube MC3230 3-Axis Accelerometer driver");
211MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/accel/mma7660.c b/drivers/iio/accel/mma7660.c
index 0acdee516973..03beadf14ad3 100644
--- a/drivers/iio/accel/mma7660.c
+++ b/drivers/iio/accel/mma7660.c
@@ -251,6 +251,7 @@ static const struct i2c_device_id mma7660_i2c_id[] = {
251 {"mma7660", 0}, 251 {"mma7660", 0},
252 {} 252 {}
253}; 253};
254MODULE_DEVICE_TABLE(i2c, mma7660_i2c_id);
254 255
255static const struct acpi_device_id mma7660_acpi_id[] = { 256static const struct acpi_device_id mma7660_acpi_id[] = {
256 {"MMA7660", 0}, 257 {"MMA7660", 0},
diff --git a/drivers/iio/accel/mxc6255.c b/drivers/iio/accel/mxc6255.c
index 97ccde722e7b..0abad6948201 100644
--- a/drivers/iio/accel/mxc6255.c
+++ b/drivers/iio/accel/mxc6255.c
@@ -154,7 +154,7 @@ static int mxc6255_probe(struct i2c_client *client,
154 return ret; 154 return ret;
155 } 155 }
156 156
157 if (chip_id != MXC6255_CHIP_ID) { 157 if ((chip_id & 0x1f) != MXC6255_CHIP_ID) {
158 dev_err(&client->dev, "Invalid chip id %x\n", chip_id); 158 dev_err(&client->dev, "Invalid chip id %x\n", chip_id);
159 return -ENODEV; 159 return -ENODEV;
160 } 160 }
@@ -171,12 +171,14 @@ static int mxc6255_probe(struct i2c_client *client,
171} 171}
172 172
173static const struct acpi_device_id mxc6255_acpi_match[] = { 173static const struct acpi_device_id mxc6255_acpi_match[] = {
174 {"MXC6225", 0},
174 {"MXC6255", 0}, 175 {"MXC6255", 0},
175 { } 176 { }
176}; 177};
177MODULE_DEVICE_TABLE(acpi, mxc6255_acpi_match); 178MODULE_DEVICE_TABLE(acpi, mxc6255_acpi_match);
178 179
179static const struct i2c_device_id mxc6255_id[] = { 180static const struct i2c_device_id mxc6255_id[] = {
181 {"mxc6225", 0},
180 {"mxc6255", 0}, 182 {"mxc6255", 0},
181 { } 183 { }
182}; 184};
diff --git a/drivers/iio/accel/ssp_accel_sensor.c b/drivers/iio/accel/ssp_accel_sensor.c
index 4ae05fce9f24..31db00970fa0 100644
--- a/drivers/iio/accel/ssp_accel_sensor.c
+++ b/drivers/iio/accel/ssp_accel_sensor.c
@@ -74,7 +74,7 @@ static int ssp_accel_write_raw(struct iio_dev *indio_dev,
74 return -EINVAL; 74 return -EINVAL;
75} 75}
76 76
77static struct iio_info ssp_accel_iio_info = { 77static const struct iio_info ssp_accel_iio_info = {
78 .read_raw = &ssp_accel_read_raw, 78 .read_raw = &ssp_accel_read_raw,
79 .write_raw = &ssp_accel_write_raw, 79 .write_raw = &ssp_accel_write_raw,
80}; 80};
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 767577298ee3..7edcf3238620 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -264,6 +264,15 @@ config LPC18XX_ADC
264 To compile this driver as a module, choose M here: the module will be 264 To compile this driver as a module, choose M here: the module will be
265 called lpc18xx_adc. 265 called lpc18xx_adc.
266 266
267config LTC2485
268 tristate "Linear Technology LTC2485 ADC driver"
269 depends on I2C
270 help
271 Say yes here to build support for Linear Technology LTC2485 ADC.
272
273 To compile this driver as a module, choose M here: the module will be
274 called ltc2485.
275
267config MAX1027 276config MAX1027
268 tristate "Maxim max1027 ADC driver" 277 tristate "Maxim max1027 ADC driver"
269 depends on SPI 278 depends on SPI
@@ -317,6 +326,19 @@ config MCP3422
317 This driver can also be built as a module. If so, the module will be 326 This driver can also be built as a module. If so, the module will be
318 called mcp3422. 327 called mcp3422.
319 328
329config MEDIATEK_MT6577_AUXADC
330 tristate "MediaTek AUXADC driver"
331 depends on ARCH_MEDIATEK || COMPILE_TEST
332 depends on HAS_IOMEM
333 help
334 Say yes here to enable support for MediaTek mt65xx AUXADC.
335
336 The driver supports immediate mode operation to read from one of sixteen
337 channels (external or internal).
338
339 This driver can also be built as a module. If so, the module will be
340 called mt6577_auxadc.
341
320config MEN_Z188_ADC 342config MEN_Z188_ADC
321 tristate "MEN 16z188 ADC IP Core support" 343 tristate "MEN 16z188 ADC IP Core support"
322 depends on MCB 344 depends on MCB
@@ -397,6 +419,21 @@ config ROCKCHIP_SARADC
397 To compile this driver as a module, choose M here: the 419 To compile this driver as a module, choose M here: the
398 module will be called rockchip_saradc. 420 module will be called rockchip_saradc.
399 421
422config STX104
423 tristate "Apex Embedded Systems STX104 driver"
424 depends on X86 && ISA_BUS_API
425 select GPIOLIB
426 help
427 Say yes here to build support for the Apex Embedded Systems STX104
428 integrated analog PC/104 card.
429
430 This driver supports the 16 channels of single-ended (8 channels of
431 differential) analog inputs, 2 channels of analog output, 4 digital
432 inputs, and 4 digital outputs provided by the STX104.
433
434 The base port addresses for the devices may be configured via the base
435 array module parameter.
436
400config TI_ADC081C 437config TI_ADC081C
401 tristate "Texas Instruments ADC081C/ADC101C/ADC121C family" 438 tristate "Texas Instruments ADC081C/ADC101C/ADC121C family"
402 depends on I2C 439 depends on I2C
@@ -417,6 +454,18 @@ config TI_ADC0832
417 This driver can also be built as a module. If so, the module will be 454 This driver can also be built as a module. If so, the module will be
418 called ti-adc0832. 455 called ti-adc0832.
419 456
457config TI_ADC12138
458 tristate "Texas Instruments ADC12130/ADC12132/ADC12138"
459 depends on SPI
460 select IIO_BUFFER
461 select IIO_TRIGGERED_BUFFER
462 help
463 If you say yes here you get support for Texas Instruments ADC12130,
464 ADC12132 and ADC12138 chips.
465
466 This driver can also be built as a module. If so, the module will be
467 called ti-adc12138.
468
420config TI_ADC128S052 469config TI_ADC128S052
421 tristate "Texas Instruments ADC128S052/ADC122S021/ADC124S021" 470 tristate "Texas Instruments ADC128S052/ADC122S021/ADC124S021"
422 depends on SPI 471 depends on SPI
@@ -427,6 +476,18 @@ config TI_ADC128S052
427 This driver can also be built as a module. If so, the module will be 476 This driver can also be built as a module. If so, the module will be
428 called ti-adc128s052. 477 called ti-adc128s052.
429 478
479config TI_ADC161S626
480 tristate "Texas Instruments ADC161S626 1-channel differential ADC"
481 depends on SPI
482 select IIO_BUFFER
483 select IIO_TRIGGERED_BUFFER
484 help
485 If you say yes here you get support for Texas Instruments ADC141S626,
486 and ADC161S626 chips.
487
488 This driver can also be built as a module. If so, the module will be
489 called ti-adc161s626.
490
430config TI_ADS1015 491config TI_ADS1015
431 tristate "Texas Instruments ADS1015 ADC" 492 tristate "Texas Instruments ADS1015 ADC"
432 depends on I2C && !SENSORS_ADS1015 493 depends on I2C && !SENSORS_ADS1015
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 0ba0d500eedb..7a40c04c311f 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -27,10 +27,12 @@ obj-$(CONFIG_IMX7D_ADC) += imx7d_adc.o
27obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o 27obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
28obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o 28obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
29obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o 29obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
30obj-$(CONFIG_LTC2485) += ltc2485.o
30obj-$(CONFIG_MAX1027) += max1027.o 31obj-$(CONFIG_MAX1027) += max1027.o
31obj-$(CONFIG_MAX1363) += max1363.o 32obj-$(CONFIG_MAX1363) += max1363.o
32obj-$(CONFIG_MCP320X) += mcp320x.o 33obj-$(CONFIG_MCP320X) += mcp320x.o
33obj-$(CONFIG_MCP3422) += mcp3422.o 34obj-$(CONFIG_MCP3422) += mcp3422.o
35obj-$(CONFIG_MEDIATEK_MT6577_AUXADC) += mt6577_auxadc.o
34obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o 36obj-$(CONFIG_MEN_Z188_ADC) += men_z188_adc.o
35obj-$(CONFIG_MXS_LRADC) += mxs-lradc.o 37obj-$(CONFIG_MXS_LRADC) += mxs-lradc.o
36obj-$(CONFIG_NAU7802) += nau7802.o 38obj-$(CONFIG_NAU7802) += nau7802.o
@@ -38,9 +40,12 @@ obj-$(CONFIG_PALMAS_GPADC) += palmas_gpadc.o
38obj-$(CONFIG_QCOM_SPMI_IADC) += qcom-spmi-iadc.o 40obj-$(CONFIG_QCOM_SPMI_IADC) += qcom-spmi-iadc.o
39obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o 41obj-$(CONFIG_QCOM_SPMI_VADC) += qcom-spmi-vadc.o
40obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o 42obj-$(CONFIG_ROCKCHIP_SARADC) += rockchip_saradc.o
43obj-$(CONFIG_STX104) += stx104.o
41obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o 44obj-$(CONFIG_TI_ADC081C) += ti-adc081c.o
42obj-$(CONFIG_TI_ADC0832) += ti-adc0832.o 45obj-$(CONFIG_TI_ADC0832) += ti-adc0832.o
46obj-$(CONFIG_TI_ADC12138) += ti-adc12138.o
43obj-$(CONFIG_TI_ADC128S052) += ti-adc128s052.o 47obj-$(CONFIG_TI_ADC128S052) += ti-adc128s052.o
48obj-$(CONFIG_TI_ADC161S626) += ti-adc161s626.o
44obj-$(CONFIG_TI_ADS1015) += ti-ads1015.o 49obj-$(CONFIG_TI_ADS1015) += ti-ads1015.o
45obj-$(CONFIG_TI_ADS8688) += ti-ads8688.o 50obj-$(CONFIG_TI_ADS8688) += ti-ads8688.o
46obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o 51obj-$(CONFIG_TI_AM335X_ADC) += ti_am335x_adc.o
diff --git a/drivers/iio/adc/ad7266.c b/drivers/iio/adc/ad7266.c
index c0f6a98fd9bd..b8d5cfd57ec4 100644
--- a/drivers/iio/adc/ad7266.c
+++ b/drivers/iio/adc/ad7266.c
@@ -481,7 +481,7 @@ error_free_gpios:
481 if (!st->fixed_addr) 481 if (!st->fixed_addr)
482 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios)); 482 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios));
483error_disable_reg: 483error_disable_reg:
484 if (!IS_ERR_OR_NULL(st->reg)) 484 if (!IS_ERR(st->reg))
485 regulator_disable(st->reg); 485 regulator_disable(st->reg);
486 486
487 return ret; 487 return ret;
@@ -496,7 +496,7 @@ static int ad7266_remove(struct spi_device *spi)
496 iio_triggered_buffer_cleanup(indio_dev); 496 iio_triggered_buffer_cleanup(indio_dev);
497 if (!st->fixed_addr) 497 if (!st->fixed_addr)
498 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios)); 498 gpio_free_array(st->gpios, ARRAY_SIZE(st->gpios));
499 if (!IS_ERR_OR_NULL(st->reg)) 499 if (!IS_ERR(st->reg))
500 regulator_disable(st->reg); 500 regulator_disable(st->reg);
501 501
502 return 0; 502 return 0;
diff --git a/drivers/iio/adc/ad7298.c b/drivers/iio/adc/ad7298.c
index 10ec8fce395f..e399bf04c73a 100644
--- a/drivers/iio/adc/ad7298.c
+++ b/drivers/iio/adc/ad7298.c
@@ -239,16 +239,16 @@ static int ad7298_read_raw(struct iio_dev *indio_dev,
239 239
240 switch (m) { 240 switch (m) {
241 case IIO_CHAN_INFO_RAW: 241 case IIO_CHAN_INFO_RAW:
242 mutex_lock(&indio_dev->mlock); 242 ret = iio_device_claim_direct_mode(indio_dev);
243 if (indio_dev->currentmode == INDIO_BUFFER_TRIGGERED) { 243 if (ret)
244 ret = -EBUSY; 244 return ret;
245 } else { 245
246 if (chan->address == AD7298_CH_TEMP) 246 if (chan->address == AD7298_CH_TEMP)
247 ret = ad7298_scan_temp(st, val); 247 ret = ad7298_scan_temp(st, val);
248 else 248 else
249 ret = ad7298_scan_direct(st, chan->address); 249 ret = ad7298_scan_direct(st, chan->address);
250 } 250
251 mutex_unlock(&indio_dev->mlock); 251 iio_device_release_direct_mode(indio_dev);
252 252
253 if (ret < 0) 253 if (ret < 0)
254 return ret; 254 return ret;
diff --git a/drivers/iio/adc/ad7793.c b/drivers/iio/adc/ad7793.c
index 847789bae821..e6706a09e100 100644
--- a/drivers/iio/adc/ad7793.c
+++ b/drivers/iio/adc/ad7793.c
@@ -519,11 +519,9 @@ static int ad7793_write_raw(struct iio_dev *indio_dev,
519 int ret, i; 519 int ret, i;
520 unsigned int tmp; 520 unsigned int tmp;
521 521
522 mutex_lock(&indio_dev->mlock); 522 ret = iio_device_claim_direct_mode(indio_dev);
523 if (iio_buffer_enabled(indio_dev)) { 523 if (ret)
524 mutex_unlock(&indio_dev->mlock); 524 return ret;
525 return -EBUSY;
526 }
527 525
528 switch (mask) { 526 switch (mask) {
529 case IIO_CHAN_INFO_SCALE: 527 case IIO_CHAN_INFO_SCALE:
@@ -548,7 +546,7 @@ static int ad7793_write_raw(struct iio_dev *indio_dev,
548 ret = -EINVAL; 546 ret = -EINVAL;
549 } 547 }
550 548
551 mutex_unlock(&indio_dev->mlock); 549 iio_device_release_direct_mode(indio_dev);
552 return ret; 550 return ret;
553} 551}
554 552
diff --git a/drivers/iio/adc/at91_adc.c b/drivers/iio/adc/at91_adc.c
index 0438c68015e8..bbdac07f4aaa 100644
--- a/drivers/iio/adc/at91_adc.c
+++ b/drivers/iio/adc/at91_adc.c
@@ -113,6 +113,7 @@
113#define AT91_ADC_TSMR_TSAV (3 << 4) /* Averages samples */ 113#define AT91_ADC_TSMR_TSAV (3 << 4) /* Averages samples */
114#define AT91_ADC_TSMR_TSAV_(x) ((x) << 4) 114#define AT91_ADC_TSMR_TSAV_(x) ((x) << 4)
115#define AT91_ADC_TSMR_SCTIM (0x0f << 16) /* Switch closure time */ 115#define AT91_ADC_TSMR_SCTIM (0x0f << 16) /* Switch closure time */
116#define AT91_ADC_TSMR_SCTIM_(x) ((x) << 16)
116#define AT91_ADC_TSMR_PENDBC (0x0f << 28) /* Pen Debounce time */ 117#define AT91_ADC_TSMR_PENDBC (0x0f << 28) /* Pen Debounce time */
117#define AT91_ADC_TSMR_PENDBC_(x) ((x) << 28) 118#define AT91_ADC_TSMR_PENDBC_(x) ((x) << 28)
118#define AT91_ADC_TSMR_NOTSDMA (1 << 22) /* No Touchscreen DMA */ 119#define AT91_ADC_TSMR_NOTSDMA (1 << 22) /* No Touchscreen DMA */
@@ -150,6 +151,7 @@
150#define MAX_RLPOS_BITS 10 151#define MAX_RLPOS_BITS 10
151#define TOUCH_SAMPLE_PERIOD_US_RL 10000 /* 10ms, the SoC can't keep up with 2ms */ 152#define TOUCH_SAMPLE_PERIOD_US_RL 10000 /* 10ms, the SoC can't keep up with 2ms */
152#define TOUCH_SHTIM 0xa 153#define TOUCH_SHTIM 0xa
154#define TOUCH_SCTIM_US 10 /* 10us for the Touchscreen Switches Closure Time */
153 155
154/** 156/**
155 * struct at91_adc_reg_desc - Various informations relative to registers 157 * struct at91_adc_reg_desc - Various informations relative to registers
@@ -1001,7 +1003,9 @@ static void atmel_ts_close(struct input_dev *dev)
1001 1003
1002static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz) 1004static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
1003{ 1005{
1006 struct iio_dev *idev = iio_priv_to_dev(st);
1004 u32 reg = 0; 1007 u32 reg = 0;
1008 u32 tssctim = 0;
1005 int i = 0; 1009 int i = 0;
1006 1010
1007 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid 1011 /* a Pen Detect Debounce Time is necessary for the ADC Touch to avoid
@@ -1034,11 +1038,20 @@ static int at91_ts_hw_init(struct at91_adc_state *st, u32 adc_clk_khz)
1034 return 0; 1038 return 0;
1035 } 1039 }
1036 1040
1041 /* Touchscreen Switches Closure time needed for allowing the value to
1042 * stabilize.
1043 * Switch Closure Time = (TSSCTIM * 4) ADCClock periods
1044 */
1045 tssctim = DIV_ROUND_UP(TOUCH_SCTIM_US * adc_clk_khz / 1000, 4);
1046 dev_dbg(&idev->dev, "adc_clk at: %d KHz, tssctim at: %d\n",
1047 adc_clk_khz, tssctim);
1048
1037 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE) 1049 if (st->touchscreen_type == ATMEL_ADC_TOUCHSCREEN_4WIRE)
1038 reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS; 1050 reg = AT91_ADC_TSMR_TSMODE_4WIRE_PRESS;
1039 else 1051 else
1040 reg = AT91_ADC_TSMR_TSMODE_5WIRE; 1052 reg = AT91_ADC_TSMR_TSMODE_5WIRE;
1041 1053
1054 reg |= AT91_ADC_TSMR_SCTIM_(tssctim) & AT91_ADC_TSMR_SCTIM;
1042 reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average) 1055 reg |= AT91_ADC_TSMR_TSAV_(st->caps->ts_filter_average)
1043 & AT91_ADC_TSMR_TSAV; 1056 & AT91_ADC_TSMR_TSAV;
1044 reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC; 1057 reg |= AT91_ADC_TSMR_PENDBC_(st->ts_pendbc) & AT91_ADC_TSMR_PENDBC;
diff --git a/drivers/iio/adc/ina2xx-adc.c b/drivers/iio/adc/ina2xx-adc.c
index 955f3fdaf519..59b7d76e1ad2 100644
--- a/drivers/iio/adc/ina2xx-adc.c
+++ b/drivers/iio/adc/ina2xx-adc.c
@@ -114,7 +114,6 @@ struct ina2xx_chip_info {
114 struct mutex state_lock; 114 struct mutex state_lock;
115 unsigned int shunt_resistor; 115 unsigned int shunt_resistor;
116 int avg; 116 int avg;
117 s64 prev_ns; /* track buffer capture time, check for underruns */
118 int int_time_vbus; /* Bus voltage integration time uS */ 117 int int_time_vbus; /* Bus voltage integration time uS */
119 int int_time_vshunt; /* Shunt voltage integration time uS */ 118 int int_time_vshunt; /* Shunt voltage integration time uS */
120 bool allow_async_readout; 119 bool allow_async_readout;
@@ -509,8 +508,6 @@ static int ina2xx_work_buffer(struct iio_dev *indio_dev)
509 iio_push_to_buffers_with_timestamp(indio_dev, 508 iio_push_to_buffers_with_timestamp(indio_dev,
510 (unsigned int *)data, time_a); 509 (unsigned int *)data, time_a);
511 510
512 chip->prev_ns = time_a;
513
514 return (unsigned long)(time_b - time_a) / 1000; 511 return (unsigned long)(time_b - time_a) / 1000;
515}; 512};
516 513
@@ -554,8 +551,6 @@ static int ina2xx_buffer_enable(struct iio_dev *indio_dev)
554 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n", 551 dev_dbg(&indio_dev->dev, "Async readout mode: %d\n",
555 chip->allow_async_readout); 552 chip->allow_async_readout);
556 553
557 chip->prev_ns = iio_get_time_ns(indio_dev);
558
559 chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev, 554 chip->task = kthread_run(ina2xx_capture_thread, (void *)indio_dev,
560 "%s:%d-%uus", indio_dev->name, indio_dev->id, 555 "%s:%d-%uus", indio_dev->name, indio_dev->id,
561 sampling_us); 556 sampling_us);
diff --git a/drivers/iio/adc/ltc2485.c b/drivers/iio/adc/ltc2485.c
new file mode 100644
index 000000000000..eab91f12454a
--- /dev/null
+++ b/drivers/iio/adc/ltc2485.c
@@ -0,0 +1,148 @@
1/*
2 * ltc2485.c - Driver for Linear Technology LTC2485 ADC
3 *
4 * Copyright (C) 2016 Alison Schofield <amsfield22@gmail.com>
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 * Datasheet: http://cds.linear.com/docs/en/datasheet/2485fd.pdf
11 */
12
13#include <linux/delay.h>
14#include <linux/i2c.h>
15#include <linux/module.h>
16
17#include <linux/iio/iio.h>
18#include <linux/iio/sysfs.h>
19
20/* Power-on configuration: rejects both 50/60Hz, operates at 1x speed */
21#define LTC2485_CONFIG_DEFAULT 0
22
23struct ltc2485_data {
24 struct i2c_client *client;
25 ktime_t time_prev; /* last conversion */
26};
27
28static void ltc2485_wait_conv(struct ltc2485_data *data)
29{
30 const unsigned int conv_time = 147; /* conversion time ms */
31 unsigned int time_elapsed;
32
33 /* delay if conversion time not passed since last read or write */
34 time_elapsed = ktime_ms_delta(ktime_get(), data->time_prev);
35
36 if (time_elapsed < conv_time)
37 msleep(conv_time - time_elapsed);
38}
39
40static int ltc2485_read(struct ltc2485_data *data, int *val)
41{
42 struct i2c_client *client = data->client;
43 __be32 buf = 0;
44 int ret;
45
46 ltc2485_wait_conv(data);
47
48 ret = i2c_master_recv(client, (char *)&buf, 4);
49 if (ret < 0) {
50 dev_err(&client->dev, "i2c_master_recv failed\n");
51 return ret;
52 }
53 data->time_prev = ktime_get();
54 *val = sign_extend32(be32_to_cpu(buf) >> 6, 24);
55
56 return ret;
57}
58
59static int ltc2485_read_raw(struct iio_dev *indio_dev,
60 struct iio_chan_spec const *chan,
61 int *val, int *val2, long mask)
62{
63 struct ltc2485_data *data = iio_priv(indio_dev);
64 int ret;
65
66 if (mask == IIO_CHAN_INFO_RAW) {
67 ret = ltc2485_read(data, val);
68 if (ret < 0)
69 return ret;
70
71 return IIO_VAL_INT;
72
73 } else if (mask == IIO_CHAN_INFO_SCALE) {
74 *val = 5000; /* on board vref millivolts */
75 *val2 = 25; /* 25 (24 + sign) data bits */
76 return IIO_VAL_FRACTIONAL_LOG2;
77
78 } else {
79 return -EINVAL;
80 }
81}
82
83static const struct iio_chan_spec ltc2485_channel[] = {
84 {
85 .type = IIO_VOLTAGE,
86 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
87 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE)
88 },
89};
90
91static const struct iio_info ltc2485_info = {
92 .read_raw = ltc2485_read_raw,
93 .driver_module = THIS_MODULE,
94};
95
96static int ltc2485_probe(struct i2c_client *client,
97 const struct i2c_device_id *id)
98{
99 struct iio_dev *indio_dev;
100 struct ltc2485_data *data;
101 int ret;
102
103 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
104 I2C_FUNC_SMBUS_WRITE_BYTE))
105 return -EOPNOTSUPP;
106
107 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
108 if (!indio_dev)
109 return -ENOMEM;
110
111 data = iio_priv(indio_dev);
112 i2c_set_clientdata(client, indio_dev);
113 data->client = client;
114
115 indio_dev->dev.parent = &client->dev;
116 indio_dev->name = id->name;
117 indio_dev->info = &ltc2485_info;
118 indio_dev->modes = INDIO_DIRECT_MODE;
119 indio_dev->channels = ltc2485_channel;
120 indio_dev->num_channels = ARRAY_SIZE(ltc2485_channel);
121
122 ret = i2c_smbus_write_byte(data->client, LTC2485_CONFIG_DEFAULT);
123 if (ret < 0)
124 return ret;
125
126 data->time_prev = ktime_get();
127
128 return devm_iio_device_register(&client->dev, indio_dev);
129}
130
131static const struct i2c_device_id ltc2485_id[] = {
132 { "ltc2485", 0 },
133 { }
134};
135MODULE_DEVICE_TABLE(i2c, ltc2485_id);
136
137static struct i2c_driver ltc2485_driver = {
138 .driver = {
139 .name = "ltc2485",
140 },
141 .probe = ltc2485_probe,
142 .id_table = ltc2485_id,
143};
144module_i2c_driver(ltc2485_driver);
145
146MODULE_AUTHOR("Alison Schofield <amsfield22@gmail.com>");
147MODULE_DESCRIPTION("Linear Technology LTC2485 ADC driver");
148MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/men_z188_adc.c b/drivers/iio/adc/men_z188_adc.c
index d095efe1ba14..8f3606de4eaf 100644
--- a/drivers/iio/adc/men_z188_adc.c
+++ b/drivers/iio/adc/men_z188_adc.c
@@ -78,7 +78,7 @@ static int z188_iio_read_raw(struct iio_dev *iio_dev,
78 return ret; 78 return ret;
79} 79}
80 80
81static struct iio_info z188_adc_info = { 81static const struct iio_info z188_adc_info = {
82 .read_raw = &z188_iio_read_raw, 82 .read_raw = &z188_iio_read_raw,
83 .driver_module = THIS_MODULE, 83 .driver_module = THIS_MODULE,
84}; 84};
diff --git a/drivers/iio/adc/mt6577_auxadc.c b/drivers/iio/adc/mt6577_auxadc.c
new file mode 100644
index 000000000000..2d104c828041
--- /dev/null
+++ b/drivers/iio/adc/mt6577_auxadc.c
@@ -0,0 +1,291 @@
1/*
2 * Copyright (c) 2016 MediaTek Inc.
3 * Author: Zhiyong Tao <zhiyong.tao@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/clk.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/of_device.h>
22#include <linux/platform_device.h>
23#include <linux/iopoll.h>
24#include <linux/io.h>
25#include <linux/iio/iio.h>
26
27/* Register definitions */
28#define MT6577_AUXADC_CON0 0x00
29#define MT6577_AUXADC_CON1 0x04
30#define MT6577_AUXADC_CON2 0x10
31#define MT6577_AUXADC_STA BIT(0)
32
33#define MT6577_AUXADC_DAT0 0x14
34#define MT6577_AUXADC_RDY0 BIT(12)
35
36#define MT6577_AUXADC_MISC 0x94
37#define MT6577_AUXADC_PDN_EN BIT(14)
38
39#define MT6577_AUXADC_DAT_MASK 0xfff
40#define MT6577_AUXADC_SLEEP_US 1000
41#define MT6577_AUXADC_TIMEOUT_US 10000
42#define MT6577_AUXADC_POWER_READY_MS 1
43#define MT6577_AUXADC_SAMPLE_READY_US 25
44
45struct mt6577_auxadc_device {
46 void __iomem *reg_base;
47 struct clk *adc_clk;
48 struct mutex lock;
49};
50
51#define MT6577_AUXADC_CHANNEL(idx) { \
52 .type = IIO_VOLTAGE, \
53 .indexed = 1, \
54 .channel = (idx), \
55 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
56}
57
58static const struct iio_chan_spec mt6577_auxadc_iio_channels[] = {
59 MT6577_AUXADC_CHANNEL(0),
60 MT6577_AUXADC_CHANNEL(1),
61 MT6577_AUXADC_CHANNEL(2),
62 MT6577_AUXADC_CHANNEL(3),
63 MT6577_AUXADC_CHANNEL(4),
64 MT6577_AUXADC_CHANNEL(5),
65 MT6577_AUXADC_CHANNEL(6),
66 MT6577_AUXADC_CHANNEL(7),
67 MT6577_AUXADC_CHANNEL(8),
68 MT6577_AUXADC_CHANNEL(9),
69 MT6577_AUXADC_CHANNEL(10),
70 MT6577_AUXADC_CHANNEL(11),
71 MT6577_AUXADC_CHANNEL(12),
72 MT6577_AUXADC_CHANNEL(13),
73 MT6577_AUXADC_CHANNEL(14),
74 MT6577_AUXADC_CHANNEL(15),
75};
76
77static inline void mt6577_auxadc_mod_reg(void __iomem *reg,
78 u32 or_mask, u32 and_mask)
79{
80 u32 val;
81
82 val = readl(reg);
83 val |= or_mask;
84 val &= ~and_mask;
85 writel(val, reg);
86}
87
88static int mt6577_auxadc_read(struct iio_dev *indio_dev,
89 struct iio_chan_spec const *chan)
90{
91 u32 val;
92 void __iomem *reg_channel;
93 int ret;
94 struct mt6577_auxadc_device *adc_dev = iio_priv(indio_dev);
95
96 reg_channel = adc_dev->reg_base + MT6577_AUXADC_DAT0 +
97 chan->channel * 0x04;
98
99 mutex_lock(&adc_dev->lock);
100
101 mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_CON1,
102 0, 1 << chan->channel);
103
104 /* read channel and make sure old ready bit == 0 */
105 ret = readl_poll_timeout(reg_channel, val,
106 ((val & MT6577_AUXADC_RDY0) == 0),
107 MT6577_AUXADC_SLEEP_US,
108 MT6577_AUXADC_TIMEOUT_US);
109 if (ret < 0) {
110 dev_err(indio_dev->dev.parent,
111 "wait for channel[%d] ready bit clear time out\n",
112 chan->channel);
113 goto err_timeout;
114 }
115
116 /* set bit to trigger sample */
117 mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_CON1,
118 1 << chan->channel, 0);
119
120 /* we must delay here for hardware sample channel data */
121 udelay(MT6577_AUXADC_SAMPLE_READY_US);
122
123 /* check MTK_AUXADC_CON2 if auxadc is idle */
124 ret = readl_poll_timeout(adc_dev->reg_base + MT6577_AUXADC_CON2, val,
125 ((val & MT6577_AUXADC_STA) == 0),
126 MT6577_AUXADC_SLEEP_US,
127 MT6577_AUXADC_TIMEOUT_US);
128 if (ret < 0) {
129 dev_err(indio_dev->dev.parent,
130 "wait for auxadc idle time out\n");
131 goto err_timeout;
132 }
133
134 /* read channel and make sure ready bit == 1 */
135 ret = readl_poll_timeout(reg_channel, val,
136 ((val & MT6577_AUXADC_RDY0) != 0),
137 MT6577_AUXADC_SLEEP_US,
138 MT6577_AUXADC_TIMEOUT_US);
139 if (ret < 0) {
140 dev_err(indio_dev->dev.parent,
141 "wait for channel[%d] data ready time out\n",
142 chan->channel);
143 goto err_timeout;
144 }
145
146 /* read data */
147 val = readl(reg_channel) & MT6577_AUXADC_DAT_MASK;
148
149 mutex_unlock(&adc_dev->lock);
150
151 return val;
152
153err_timeout:
154
155 mutex_unlock(&adc_dev->lock);
156
157 return -ETIMEDOUT;
158}
159
160static int mt6577_auxadc_read_raw(struct iio_dev *indio_dev,
161 struct iio_chan_spec const *chan,
162 int *val,
163 int *val2,
164 long info)
165{
166 switch (info) {
167 case IIO_CHAN_INFO_PROCESSED:
168 *val = mt6577_auxadc_read(indio_dev, chan);
169 if (*val < 0) {
170 dev_err(indio_dev->dev.parent,
171 "failed to sample data on channel[%d]\n",
172 chan->channel);
173 return *val;
174 }
175 return IIO_VAL_INT;
176
177 default:
178 return -EINVAL;
179 }
180}
181
182static const struct iio_info mt6577_auxadc_info = {
183 .driver_module = THIS_MODULE,
184 .read_raw = &mt6577_auxadc_read_raw,
185};
186
187static int mt6577_auxadc_probe(struct platform_device *pdev)
188{
189 struct mt6577_auxadc_device *adc_dev;
190 unsigned long adc_clk_rate;
191 struct resource *res;
192 struct iio_dev *indio_dev;
193 int ret;
194
195 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
196 if (!indio_dev)
197 return -ENOMEM;
198
199 adc_dev = iio_priv(indio_dev);
200 indio_dev->dev.parent = &pdev->dev;
201 indio_dev->name = dev_name(&pdev->dev);
202 indio_dev->info = &mt6577_auxadc_info;
203 indio_dev->modes = INDIO_DIRECT_MODE;
204 indio_dev->channels = mt6577_auxadc_iio_channels;
205 indio_dev->num_channels = ARRAY_SIZE(mt6577_auxadc_iio_channels);
206
207 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 adc_dev->reg_base = devm_ioremap_resource(&pdev->dev, res);
209 if (IS_ERR(adc_dev->reg_base)) {
210 dev_err(&pdev->dev, "failed to get auxadc base address\n");
211 return PTR_ERR(adc_dev->reg_base);
212 }
213
214 adc_dev->adc_clk = devm_clk_get(&pdev->dev, "main");
215 if (IS_ERR(adc_dev->adc_clk)) {
216 dev_err(&pdev->dev, "failed to get auxadc clock\n");
217 return PTR_ERR(adc_dev->adc_clk);
218 }
219
220 ret = clk_prepare_enable(adc_dev->adc_clk);
221 if (ret) {
222 dev_err(&pdev->dev, "failed to enable auxadc clock\n");
223 return ret;
224 }
225
226 adc_clk_rate = clk_get_rate(adc_dev->adc_clk);
227 if (!adc_clk_rate) {
228 ret = -EINVAL;
229 dev_err(&pdev->dev, "null clock rate\n");
230 goto err_disable_clk;
231 }
232
233 mutex_init(&adc_dev->lock);
234
235 mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
236 MT6577_AUXADC_PDN_EN, 0);
237 mdelay(MT6577_AUXADC_POWER_READY_MS);
238
239 platform_set_drvdata(pdev, indio_dev);
240
241 ret = iio_device_register(indio_dev);
242 if (ret < 0) {
243 dev_err(&pdev->dev, "failed to register iio device\n");
244 goto err_power_off;
245 }
246
247 return 0;
248
249err_power_off:
250 mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
251 0, MT6577_AUXADC_PDN_EN);
252err_disable_clk:
253 clk_disable_unprepare(adc_dev->adc_clk);
254 return ret;
255}
256
257static int mt6577_auxadc_remove(struct platform_device *pdev)
258{
259 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
260 struct mt6577_auxadc_device *adc_dev = iio_priv(indio_dev);
261
262 iio_device_unregister(indio_dev);
263
264 mt6577_auxadc_mod_reg(adc_dev->reg_base + MT6577_AUXADC_MISC,
265 0, MT6577_AUXADC_PDN_EN);
266
267 clk_disable_unprepare(adc_dev->adc_clk);
268
269 return 0;
270}
271
272static const struct of_device_id mt6577_auxadc_of_match[] = {
273 { .compatible = "mediatek,mt2701-auxadc", },
274 { .compatible = "mediatek,mt8173-auxadc", },
275 { }
276};
277MODULE_DEVICE_TABLE(of, mt6577_auxadc_of_match);
278
279static struct platform_driver mt6577_auxadc_driver = {
280 .driver = {
281 .name = "mt6577-auxadc",
282 .of_match_table = mt6577_auxadc_of_match,
283 },
284 .probe = mt6577_auxadc_probe,
285 .remove = mt6577_auxadc_remove,
286};
287module_platform_driver(mt6577_auxadc_driver);
288
289MODULE_AUTHOR("Zhiyong Tao <zhiyong.tao@mediatek.com>");
290MODULE_DESCRIPTION("MTK AUXADC Device Driver");
291MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/nau7802.c b/drivers/iio/adc/nau7802.c
index db9b829ccf0d..08f446695f97 100644
--- a/drivers/iio/adc/nau7802.c
+++ b/drivers/iio/adc/nau7802.c
@@ -197,7 +197,7 @@ static irqreturn_t nau7802_eoc_trigger(int irq, void *private)
197 if (st->conversion_count < NAU7802_MIN_CONVERSIONS) 197 if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
198 st->conversion_count++; 198 st->conversion_count++;
199 if (st->conversion_count >= NAU7802_MIN_CONVERSIONS) 199 if (st->conversion_count >= NAU7802_MIN_CONVERSIONS)
200 complete_all(&st->value_ok); 200 complete(&st->value_ok);
201 201
202 return IRQ_HANDLED; 202 return IRQ_HANDLED;
203} 203}
diff --git a/drivers/iio/dac/stx104.c b/drivers/iio/adc/stx104.c
index bebbd00304ce..7e3645749eaf 100644
--- a/drivers/iio/dac/stx104.c
+++ b/drivers/iio/adc/stx104.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * DAC driver for the Apex Embedded Systems STX104 2 * IIO driver for the Apex Embedded Systems STX104
3 * Copyright (C) 2016 William Breathitt Gray 3 * Copyright (C) 2016 William Breathitt Gray
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
@@ -20,19 +20,30 @@
20#include <linux/io.h> 20#include <linux/io.h>
21#include <linux/ioport.h> 21#include <linux/ioport.h>
22#include <linux/isa.h> 22#include <linux/isa.h>
23#include <linux/kernel.h>
23#include <linux/module.h> 24#include <linux/module.h>
24#include <linux/moduleparam.h> 25#include <linux/moduleparam.h>
25#include <linux/spinlock.h> 26#include <linux/spinlock.h>
26 27
27#define STX104_NUM_CHAN 2 28#define STX104_OUT_CHAN(chan) { \
28
29#define STX104_CHAN(chan) { \
30 .type = IIO_VOLTAGE, \ 29 .type = IIO_VOLTAGE, \
31 .channel = chan, \ 30 .channel = chan, \
32 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 31 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
33 .indexed = 1, \ 32 .indexed = 1, \
34 .output = 1 \ 33 .output = 1 \
35} 34}
35#define STX104_IN_CHAN(chan, diff) { \
36 .type = IIO_VOLTAGE, \
37 .channel = chan, \
38 .channel2 = chan, \
39 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_HARDWAREGAIN) | \
40 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), \
41 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
42 .indexed = 1, \
43 .differential = diff \
44}
45
46#define STX104_NUM_OUT_CHAN 2
36 47
37#define STX104_EXTENT 16 48#define STX104_EXTENT 16
38 49
@@ -47,8 +58,8 @@ MODULE_PARM_DESC(base, "Apex Embedded Systems STX104 base addresses");
47 * @base: base port address of the IIO device 58 * @base: base port address of the IIO device
48 */ 59 */
49struct stx104_iio { 60struct stx104_iio {
50 unsigned chan_out_states[STX104_NUM_CHAN]; 61 unsigned int chan_out_states[STX104_NUM_OUT_CHAN];
51 unsigned base; 62 unsigned int base;
52}; 63};
53 64
54/** 65/**
@@ -79,28 +90,95 @@ static int stx104_read_raw(struct iio_dev *indio_dev,
79 struct iio_chan_spec const *chan, int *val, int *val2, long mask) 90 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
80{ 91{
81 struct stx104_iio *const priv = iio_priv(indio_dev); 92 struct stx104_iio *const priv = iio_priv(indio_dev);
93 unsigned int adc_config;
94 int adbu;
95 int gain;
96
97 switch (mask) {
98 case IIO_CHAN_INFO_HARDWAREGAIN:
99 /* get gain configuration */
100 adc_config = inb(priv->base + 11);
101 gain = adc_config & 0x3;
102
103 *val = 1 << gain;
104 return IIO_VAL_INT;
105 case IIO_CHAN_INFO_RAW:
106 if (chan->output) {
107 *val = priv->chan_out_states[chan->channel];
108 return IIO_VAL_INT;
109 }
110
111 /* select ADC channel */
112 outb(chan->channel | (chan->channel << 4), priv->base + 2);
113
114 /* trigger ADC sample capture and wait for completion */
115 outb(0, priv->base);
116 while (inb(priv->base + 8) & BIT(7));
117
118 *val = inw(priv->base);
119 return IIO_VAL_INT;
120 case IIO_CHAN_INFO_OFFSET:
121 /* get ADC bipolar/unipolar configuration */
122 adc_config = inb(priv->base + 11);
123 adbu = !(adc_config & BIT(2));
124
125 *val = -32768 * adbu;
126 return IIO_VAL_INT;
127 case IIO_CHAN_INFO_SCALE:
128 /* get ADC bipolar/unipolar and gain configuration */
129 adc_config = inb(priv->base + 11);
130 adbu = !(adc_config & BIT(2));
131 gain = adc_config & 0x3;
132
133 *val = 5;
134 *val2 = 15 - adbu + gain;
135 return IIO_VAL_FRACTIONAL_LOG2;
136 }
82 137
83 if (mask != IIO_CHAN_INFO_RAW) 138 return -EINVAL;
84 return -EINVAL;
85
86 *val = priv->chan_out_states[chan->channel];
87
88 return IIO_VAL_INT;
89} 139}
90 140
91static int stx104_write_raw(struct iio_dev *indio_dev, 141static int stx104_write_raw(struct iio_dev *indio_dev,
92 struct iio_chan_spec const *chan, int val, int val2, long mask) 142 struct iio_chan_spec const *chan, int val, int val2, long mask)
93{ 143{
94 struct stx104_iio *const priv = iio_priv(indio_dev); 144 struct stx104_iio *const priv = iio_priv(indio_dev);
95 const unsigned chan_addr_offset = 2 * chan->channel;
96 145
97 if (mask != IIO_CHAN_INFO_RAW) 146 switch (mask) {
147 case IIO_CHAN_INFO_HARDWAREGAIN:
148 /* Only four gain states (x1, x2, x4, x8) */
149 switch (val) {
150 case 1:
151 outb(0, priv->base + 11);
152 break;
153 case 2:
154 outb(1, priv->base + 11);
155 break;
156 case 4:
157 outb(2, priv->base + 11);
158 break;
159 case 8:
160 outb(3, priv->base + 11);
161 break;
162 default:
163 return -EINVAL;
164 }
165
166 return 0;
167 case IIO_CHAN_INFO_RAW:
168 if (chan->output) {
169 /* DAC can only accept up to a 16-bit value */
170 if ((unsigned int)val > 65535)
171 return -EINVAL;
172
173 priv->chan_out_states[chan->channel] = val;
174 outw(val, priv->base + 4 + 2 * chan->channel);
175
176 return 0;
177 }
98 return -EINVAL; 178 return -EINVAL;
179 }
99 180
100 priv->chan_out_states[chan->channel] = val; 181 return -EINVAL;
101 outw(val, priv->base + 4 + chan_addr_offset);
102
103 return 0;
104} 182}
105 183
106static const struct iio_info stx104_info = { 184static const struct iio_info stx104_info = {
@@ -109,9 +187,22 @@ static const struct iio_info stx104_info = {
109 .write_raw = stx104_write_raw 187 .write_raw = stx104_write_raw
110}; 188};
111 189
112static const struct iio_chan_spec stx104_channels[STX104_NUM_CHAN] = { 190/* single-ended input channels configuration */
113 STX104_CHAN(0), 191static const struct iio_chan_spec stx104_channels_sing[] = {
114 STX104_CHAN(1) 192 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
193 STX104_IN_CHAN(0, 0), STX104_IN_CHAN(1, 0), STX104_IN_CHAN(2, 0),
194 STX104_IN_CHAN(3, 0), STX104_IN_CHAN(4, 0), STX104_IN_CHAN(5, 0),
195 STX104_IN_CHAN(6, 0), STX104_IN_CHAN(7, 0), STX104_IN_CHAN(8, 0),
196 STX104_IN_CHAN(9, 0), STX104_IN_CHAN(10, 0), STX104_IN_CHAN(11, 0),
197 STX104_IN_CHAN(12, 0), STX104_IN_CHAN(13, 0), STX104_IN_CHAN(14, 0),
198 STX104_IN_CHAN(15, 0)
199};
200/* differential input channels configuration */
201static const struct iio_chan_spec stx104_channels_diff[] = {
202 STX104_OUT_CHAN(0), STX104_OUT_CHAN(1),
203 STX104_IN_CHAN(0, 1), STX104_IN_CHAN(1, 1), STX104_IN_CHAN(2, 1),
204 STX104_IN_CHAN(3, 1), STX104_IN_CHAN(4, 1), STX104_IN_CHAN(5, 1),
205 STX104_IN_CHAN(6, 1), STX104_IN_CHAN(7, 1)
115}; 206};
116 207
117static int stx104_gpio_get_direction(struct gpio_chip *chip, 208static int stx104_gpio_get_direction(struct gpio_chip *chip,
@@ -204,13 +295,27 @@ static int stx104_probe(struct device *dev, unsigned int id)
204 295
205 indio_dev->info = &stx104_info; 296 indio_dev->info = &stx104_info;
206 indio_dev->modes = INDIO_DIRECT_MODE; 297 indio_dev->modes = INDIO_DIRECT_MODE;
207 indio_dev->channels = stx104_channels; 298
208 indio_dev->num_channels = STX104_NUM_CHAN; 299 /* determine if differential inputs */
300 if (inb(base[id] + 8) & BIT(5)) {
301 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_diff);
302 indio_dev->channels = stx104_channels_diff;
303 } else {
304 indio_dev->num_channels = ARRAY_SIZE(stx104_channels_sing);
305 indio_dev->channels = stx104_channels_sing;
306 }
307
209 indio_dev->name = dev_name(dev); 308 indio_dev->name = dev_name(dev);
210 309
211 priv = iio_priv(indio_dev); 310 priv = iio_priv(indio_dev);
212 priv->base = base[id]; 311 priv->base = base[id];
213 312
313 /* configure device for software trigger operation */
314 outb(0, base[id] + 9);
315
316 /* initialize gain setting to x1 */
317 outb(0, base[id] + 11);
318
214 /* initialize DAC output to 0V */ 319 /* initialize DAC output to 0V */
215 outw(0, base[id] + 4); 320 outw(0, base[id] + 4);
216 outw(0, base[id] + 6); 321 outw(0, base[id] + 6);
@@ -271,5 +376,5 @@ static struct isa_driver stx104_driver = {
271module_isa_driver(stx104_driver, num_stx104); 376module_isa_driver(stx104_driver, num_stx104);
272 377
273MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>"); 378MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
274MODULE_DESCRIPTION("Apex Embedded Systems STX104 DAC driver"); 379MODULE_DESCRIPTION("Apex Embedded Systems STX104 IIO driver");
275MODULE_LICENSE("GPL v2"); 380MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/ti-adc12138.c b/drivers/iio/adc/ti-adc12138.c
new file mode 100644
index 000000000000..072f03bfe6a0
--- /dev/null
+++ b/drivers/iio/adc/ti-adc12138.c
@@ -0,0 +1,552 @@
1/*
2 * ADC12130/ADC12132/ADC12138 12-bit plus sign ADC driver
3 *
4 * Copyright (c) 2016 Akinobu Mita <akinobu.mita@gmail.com>
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 * Datasheet: http://www.ti.com/lit/ds/symlink/adc12138.pdf
11 */
12
13#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/completion.h>
16#include <linux/clk.h>
17#include <linux/spi/spi.h>
18#include <linux/iio/iio.h>
19#include <linux/iio/buffer.h>
20#include <linux/iio/trigger.h>
21#include <linux/iio/triggered_buffer.h>
22#include <linux/iio/trigger_consumer.h>
23#include <linux/regulator/consumer.h>
24
25#define ADC12138_MODE_AUTO_CAL 0x08
26#define ADC12138_MODE_READ_STATUS 0x0c
27#define ADC12138_MODE_ACQUISITION_TIME_6 0x0e
28#define ADC12138_MODE_ACQUISITION_TIME_10 0x4e
29#define ADC12138_MODE_ACQUISITION_TIME_18 0x8e
30#define ADC12138_MODE_ACQUISITION_TIME_34 0xce
31
32#define ADC12138_STATUS_CAL BIT(6)
33
34enum {
35 adc12130,
36 adc12132,
37 adc12138,
38};
39
40struct adc12138 {
41 struct spi_device *spi;
42 unsigned int id;
43 /* conversion clock */
44 struct clk *cclk;
45 /* positive analog voltage reference */
46 struct regulator *vref_p;
47 /* negative analog voltage reference */
48 struct regulator *vref_n;
49 struct mutex lock;
50 struct completion complete;
51 /* The number of cclk periods for the S/H's acquisition time */
52 unsigned int acquisition_time;
53
54 u8 tx_buf[2] ____cacheline_aligned;
55 u8 rx_buf[2];
56};
57
58#define ADC12138_VOLTAGE_CHANNEL(chan) \
59 { \
60 .type = IIO_VOLTAGE, \
61 .indexed = 1, \
62 .channel = chan, \
63 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
64 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
65 | BIT(IIO_CHAN_INFO_OFFSET), \
66 .scan_index = chan, \
67 .scan_type = { \
68 .sign = 's', \
69 .realbits = 13, \
70 .storagebits = 16, \
71 .shift = 3, \
72 .endianness = IIO_BE, \
73 }, \
74 }
75
76#define ADC12138_VOLTAGE_CHANNEL_DIFF(chan1, chan2, si) \
77 { \
78 .type = IIO_VOLTAGE, \
79 .indexed = 1, \
80 .channel = (chan1), \
81 .channel2 = (chan2), \
82 .differential = 1, \
83 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
84 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) \
85 | BIT(IIO_CHAN_INFO_OFFSET), \
86 .scan_index = si, \
87 .scan_type = { \
88 .sign = 's', \
89 .realbits = 13, \
90 .storagebits = 16, \
91 .shift = 3, \
92 .endianness = IIO_BE, \
93 }, \
94 }
95
96static const struct iio_chan_spec adc12132_channels[] = {
97 ADC12138_VOLTAGE_CHANNEL(0),
98 ADC12138_VOLTAGE_CHANNEL(1),
99 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 2),
100 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 3),
101 IIO_CHAN_SOFT_TIMESTAMP(4),
102};
103
104static const struct iio_chan_spec adc12138_channels[] = {
105 ADC12138_VOLTAGE_CHANNEL(0),
106 ADC12138_VOLTAGE_CHANNEL(1),
107 ADC12138_VOLTAGE_CHANNEL(2),
108 ADC12138_VOLTAGE_CHANNEL(3),
109 ADC12138_VOLTAGE_CHANNEL(4),
110 ADC12138_VOLTAGE_CHANNEL(5),
111 ADC12138_VOLTAGE_CHANNEL(6),
112 ADC12138_VOLTAGE_CHANNEL(7),
113 ADC12138_VOLTAGE_CHANNEL_DIFF(0, 1, 8),
114 ADC12138_VOLTAGE_CHANNEL_DIFF(1, 0, 9),
115 ADC12138_VOLTAGE_CHANNEL_DIFF(2, 3, 10),
116 ADC12138_VOLTAGE_CHANNEL_DIFF(3, 2, 11),
117 ADC12138_VOLTAGE_CHANNEL_DIFF(4, 5, 12),
118 ADC12138_VOLTAGE_CHANNEL_DIFF(5, 4, 13),
119 ADC12138_VOLTAGE_CHANNEL_DIFF(6, 7, 14),
120 ADC12138_VOLTAGE_CHANNEL_DIFF(7, 6, 15),
121 IIO_CHAN_SOFT_TIMESTAMP(16),
122};
123
124static int adc12138_mode_programming(struct adc12138 *adc, u8 mode,
125 void *rx_buf, int len)
126{
127 struct spi_transfer xfer = {
128 .tx_buf = adc->tx_buf,
129 .rx_buf = adc->rx_buf,
130 .len = len,
131 };
132 int ret;
133
134 /* Skip unused bits for ADC12130 and ADC12132 */
135 if (adc->id != adc12138)
136 mode = (mode & 0xc0) | ((mode & 0x0f) << 2);
137
138 adc->tx_buf[0] = mode;
139
140 ret = spi_sync_transfer(adc->spi, &xfer, 1);
141 if (ret)
142 return ret;
143
144 memcpy(rx_buf, adc->rx_buf, len);
145
146 return 0;
147}
148
149static int adc12138_read_status(struct adc12138 *adc)
150{
151 u8 rx_buf[2];
152 int ret;
153
154 ret = adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
155 rx_buf, 2);
156 if (ret)
157 return ret;
158
159 return (rx_buf[0] << 1) | (rx_buf[1] >> 7);
160}
161
162static int __adc12138_start_conv(struct adc12138 *adc,
163 struct iio_chan_spec const *channel,
164 void *data, int len)
165
166{
167 const u8 ch_to_mux[] = { 0, 4, 1, 5, 2, 6, 3, 7 };
168 u8 mode = (ch_to_mux[channel->channel] << 4) |
169 (channel->differential ? 0 : 0x80);
170
171 return adc12138_mode_programming(adc, mode, data, len);
172}
173
174static int adc12138_start_conv(struct adc12138 *adc,
175 struct iio_chan_spec const *channel)
176{
177 u8 trash;
178
179 return __adc12138_start_conv(adc, channel, &trash, 1);
180}
181
182static int adc12138_start_and_read_conv(struct adc12138 *adc,
183 struct iio_chan_spec const *channel,
184 __be16 *data)
185{
186 return __adc12138_start_conv(adc, channel, data, 2);
187}
188
189static int adc12138_read_conv_data(struct adc12138 *adc, __be16 *value)
190{
191 /* Issue a read status instruction and read previous conversion data */
192 return adc12138_mode_programming(adc, ADC12138_MODE_READ_STATUS,
193 value, sizeof(*value));
194}
195
196static int adc12138_wait_eoc(struct adc12138 *adc, unsigned long timeout)
197{
198 if (!wait_for_completion_timeout(&adc->complete, timeout))
199 return -ETIMEDOUT;
200
201 return 0;
202}
203
204static int adc12138_adc_conversion(struct adc12138 *adc,
205 struct iio_chan_spec const *channel,
206 __be16 *value)
207{
208 int ret;
209
210 reinit_completion(&adc->complete);
211
212 ret = adc12138_start_conv(adc, channel);
213 if (ret)
214 return ret;
215
216 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
217 if (ret)
218 return ret;
219
220 return adc12138_read_conv_data(adc, value);
221}
222
223static int adc12138_read_raw(struct iio_dev *iio,
224 struct iio_chan_spec const *channel, int *value,
225 int *shift, long mask)
226{
227 struct adc12138 *adc = iio_priv(iio);
228 int ret;
229 __be16 data;
230
231 switch (mask) {
232 case IIO_CHAN_INFO_RAW:
233 mutex_lock(&adc->lock);
234 ret = adc12138_adc_conversion(adc, channel, &data);
235 mutex_unlock(&adc->lock);
236 if (ret)
237 return ret;
238
239 *value = sign_extend32(be16_to_cpu(data) >> 3, 12);
240
241 return IIO_VAL_INT;
242 case IIO_CHAN_INFO_SCALE:
243 ret = regulator_get_voltage(adc->vref_p);
244 if (ret < 0)
245 return ret;
246 *value = ret;
247
248 if (!IS_ERR(adc->vref_n)) {
249 ret = regulator_get_voltage(adc->vref_n);
250 if (ret < 0)
251 return ret;
252 *value -= ret;
253 }
254
255 /* convert regulator output voltage to mV */
256 *value /= 1000;
257 *shift = channel->scan_type.realbits - 1;
258
259 return IIO_VAL_FRACTIONAL_LOG2;
260 case IIO_CHAN_INFO_OFFSET:
261 if (!IS_ERR(adc->vref_n)) {
262 *value = regulator_get_voltage(adc->vref_n);
263 if (*value < 0)
264 return *value;
265 } else {
266 *value = 0;
267 }
268
269 /* convert regulator output voltage to mV */
270 *value /= 1000;
271
272 return IIO_VAL_INT;
273 }
274
275 return -EINVAL;
276}
277
278static const struct iio_info adc12138_info = {
279 .read_raw = adc12138_read_raw,
280 .driver_module = THIS_MODULE,
281};
282
283static int adc12138_init(struct adc12138 *adc)
284{
285 int ret;
286 int status;
287 u8 mode;
288 u8 trash;
289
290 reinit_completion(&adc->complete);
291
292 ret = adc12138_mode_programming(adc, ADC12138_MODE_AUTO_CAL, &trash, 1);
293 if (ret)
294 return ret;
295
296 /* data output at this time has no significance */
297 status = adc12138_read_status(adc);
298 if (status < 0)
299 return status;
300
301 adc12138_wait_eoc(adc, msecs_to_jiffies(100));
302
303 status = adc12138_read_status(adc);
304 if (status & ADC12138_STATUS_CAL) {
305 dev_warn(&adc->spi->dev,
306 "Auto Cal sequence is still in progress: %#x\n",
307 status);
308 return -EIO;
309 }
310
311 switch (adc->acquisition_time) {
312 case 6:
313 mode = ADC12138_MODE_ACQUISITION_TIME_6;
314 break;
315 case 10:
316 mode = ADC12138_MODE_ACQUISITION_TIME_10;
317 break;
318 case 18:
319 mode = ADC12138_MODE_ACQUISITION_TIME_18;
320 break;
321 case 34:
322 mode = ADC12138_MODE_ACQUISITION_TIME_34;
323 break;
324 default:
325 return -EINVAL;
326 }
327
328 return adc12138_mode_programming(adc, mode, &trash, 1);
329}
330
331static irqreturn_t adc12138_trigger_handler(int irq, void *p)
332{
333 struct iio_poll_func *pf = p;
334 struct iio_dev *indio_dev = pf->indio_dev;
335 struct adc12138 *adc = iio_priv(indio_dev);
336 __be16 data[20] = { }; /* 16x 2 bytes ADC data + 8 bytes timestamp */
337 __be16 trash;
338 int ret;
339 int scan_index;
340 int i = 0;
341
342 mutex_lock(&adc->lock);
343
344 for_each_set_bit(scan_index, indio_dev->active_scan_mask,
345 indio_dev->masklength) {
346 const struct iio_chan_spec *scan_chan =
347 &indio_dev->channels[scan_index];
348
349 reinit_completion(&adc->complete);
350
351 ret = adc12138_start_and_read_conv(adc, scan_chan,
352 i ? &data[i - 1] : &trash);
353 if (ret) {
354 dev_warn(&adc->spi->dev,
355 "failed to start conversion\n");
356 goto out;
357 }
358
359 ret = adc12138_wait_eoc(adc, msecs_to_jiffies(100));
360 if (ret) {
361 dev_warn(&adc->spi->dev, "wait eoc timeout\n");
362 goto out;
363 }
364
365 i++;
366 }
367
368 if (i) {
369 ret = adc12138_read_conv_data(adc, &data[i - 1]);
370 if (ret) {
371 dev_warn(&adc->spi->dev,
372 "failed to get conversion data\n");
373 goto out;
374 }
375 }
376
377 iio_push_to_buffers_with_timestamp(indio_dev, data,
378 iio_get_time_ns(indio_dev));
379out:
380 mutex_unlock(&adc->lock);
381
382 iio_trigger_notify_done(indio_dev->trig);
383
384 return IRQ_HANDLED;
385}
386
387static irqreturn_t adc12138_eoc_handler(int irq, void *p)
388{
389 struct iio_dev *indio_dev = p;
390 struct adc12138 *adc = iio_priv(indio_dev);
391
392 complete(&adc->complete);
393
394 return IRQ_HANDLED;
395}
396
397static int adc12138_probe(struct spi_device *spi)
398{
399 struct iio_dev *indio_dev;
400 struct adc12138 *adc;
401 int ret;
402
403 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adc));
404 if (!indio_dev)
405 return -ENOMEM;
406
407 adc = iio_priv(indio_dev);
408 adc->spi = spi;
409 adc->id = spi_get_device_id(spi)->driver_data;
410 mutex_init(&adc->lock);
411 init_completion(&adc->complete);
412
413 indio_dev->name = spi_get_device_id(spi)->name;
414 indio_dev->dev.parent = &spi->dev;
415 indio_dev->info = &adc12138_info;
416 indio_dev->modes = INDIO_DIRECT_MODE;
417
418 switch (adc->id) {
419 case adc12130:
420 case adc12132:
421 indio_dev->channels = adc12132_channels;
422 indio_dev->num_channels = ARRAY_SIZE(adc12132_channels);
423 break;
424 case adc12138:
425 indio_dev->channels = adc12138_channels;
426 indio_dev->num_channels = ARRAY_SIZE(adc12138_channels);
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 ret = of_property_read_u32(spi->dev.of_node, "ti,acquisition-time",
433 &adc->acquisition_time);
434 if (ret)
435 adc->acquisition_time = 10;
436
437 adc->cclk = devm_clk_get(&spi->dev, NULL);
438 if (IS_ERR(adc->cclk))
439 return PTR_ERR(adc->cclk);
440
441 adc->vref_p = devm_regulator_get(&spi->dev, "vref-p");
442 if (IS_ERR(adc->vref_p))
443 return PTR_ERR(adc->vref_p);
444
445 adc->vref_n = devm_regulator_get_optional(&spi->dev, "vref-n");
446 if (IS_ERR(adc->vref_n)) {
447 /*
448 * Assume vref_n is 0V if an optional regulator is not
449 * specified, otherwise return the error code.
450 */
451 ret = PTR_ERR(adc->vref_n);
452 if (ret != -ENODEV)
453 return ret;
454 }
455
456 ret = devm_request_irq(&spi->dev, spi->irq, adc12138_eoc_handler,
457 IRQF_TRIGGER_RISING, indio_dev->name, indio_dev);
458 if (ret)
459 return ret;
460
461 ret = clk_prepare_enable(adc->cclk);
462 if (ret)
463 return ret;
464
465 ret = regulator_enable(adc->vref_p);
466 if (ret)
467 goto err_clk_disable;
468
469 if (!IS_ERR(adc->vref_n)) {
470 ret = regulator_enable(adc->vref_n);
471 if (ret)
472 goto err_vref_p_disable;
473 }
474
475 ret = adc12138_init(adc);
476 if (ret)
477 goto err_vref_n_disable;
478
479 spi_set_drvdata(spi, indio_dev);
480
481 ret = iio_triggered_buffer_setup(indio_dev, NULL,
482 adc12138_trigger_handler, NULL);
483 if (ret)
484 goto err_vref_n_disable;
485
486 ret = iio_device_register(indio_dev);
487 if (ret)
488 goto err_buffer_cleanup;
489
490 return 0;
491err_buffer_cleanup:
492 iio_triggered_buffer_cleanup(indio_dev);
493err_vref_n_disable:
494 if (!IS_ERR(adc->vref_n))
495 regulator_disable(adc->vref_n);
496err_vref_p_disable:
497 regulator_disable(adc->vref_p);
498err_clk_disable:
499 clk_disable_unprepare(adc->cclk);
500
501 return ret;
502}
503
504static int adc12138_remove(struct spi_device *spi)
505{
506 struct iio_dev *indio_dev = spi_get_drvdata(spi);
507 struct adc12138 *adc = iio_priv(indio_dev);
508
509 iio_device_unregister(indio_dev);
510 iio_triggered_buffer_cleanup(indio_dev);
511 if (!IS_ERR(adc->vref_n))
512 regulator_disable(adc->vref_n);
513 regulator_disable(adc->vref_p);
514 clk_disable_unprepare(adc->cclk);
515
516 return 0;
517}
518
519#ifdef CONFIG_OF
520
521static const struct of_device_id adc12138_dt_ids[] = {
522 { .compatible = "ti,adc12130", },
523 { .compatible = "ti,adc12132", },
524 { .compatible = "ti,adc12138", },
525 {}
526};
527MODULE_DEVICE_TABLE(of, adc12138_dt_ids);
528
529#endif
530
531static const struct spi_device_id adc12138_id[] = {
532 { "adc12130", adc12130 },
533 { "adc12132", adc12132 },
534 { "adc12138", adc12138 },
535 {}
536};
537MODULE_DEVICE_TABLE(spi, adc12138_id);
538
539static struct spi_driver adc12138_driver = {
540 .driver = {
541 .name = "adc12138",
542 .of_match_table = of_match_ptr(adc12138_dt_ids),
543 },
544 .probe = adc12138_probe,
545 .remove = adc12138_remove,
546 .id_table = adc12138_id,
547};
548module_spi_driver(adc12138_driver);
549
550MODULE_AUTHOR("Akinobu Mita <akinobu.mita@gmail.com>");
551MODULE_DESCRIPTION("ADC12130/ADC12132/ADC12138 driver");
552MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/adc/ti-adc161s626.c b/drivers/iio/adc/ti-adc161s626.c
new file mode 100644
index 000000000000..f94b69f9c288
--- /dev/null
+++ b/drivers/iio/adc/ti-adc161s626.c
@@ -0,0 +1,248 @@
1/*
2 * ti-adc161s626.c - Texas Instruments ADC161S626 1-channel differential ADC
3 *
4 * ADC Devices Supported:
5 * adc141s626 - 14-bit ADC
6 * adc161s626 - 16-bit ADC
7 *
8 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/err.h>
24#include <linux/spi/spi.h>
25#include <linux/iio/iio.h>
26#include <linux/iio/trigger.h>
27#include <linux/iio/buffer.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/iio/triggered_buffer.h>
30
31#define TI_ADC_DRV_NAME "ti-adc161s626"
32
33enum {
34 TI_ADC141S626,
35 TI_ADC161S626,
36};
37
38static const struct iio_chan_spec ti_adc141s626_channels[] = {
39 {
40 .type = IIO_VOLTAGE,
41 .channel = 0,
42 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
43 .scan_index = 0,
44 .scan_type = {
45 .sign = 's',
46 .realbits = 14,
47 .storagebits = 16,
48 },
49 },
50 IIO_CHAN_SOFT_TIMESTAMP(1),
51};
52
53static const struct iio_chan_spec ti_adc161s626_channels[] = {
54 {
55 .type = IIO_VOLTAGE,
56 .channel = 0,
57 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
58 .scan_index = 0,
59 .scan_type = {
60 .sign = 's',
61 .realbits = 16,
62 .storagebits = 16,
63 },
64 },
65 IIO_CHAN_SOFT_TIMESTAMP(1),
66};
67
68struct ti_adc_data {
69 struct iio_dev *indio_dev;
70 struct spi_device *spi;
71 u8 read_size;
72 u8 shift;
73
74 u8 buffer[16] ____cacheline_aligned;
75};
76
77static int ti_adc_read_measurement(struct ti_adc_data *data,
78 struct iio_chan_spec const *chan, int *val)
79{
80 int ret;
81
82 switch (data->read_size) {
83 case 2: {
84 __be16 buf;
85
86 ret = spi_read(data->spi, (void *) &buf, 2);
87 if (ret)
88 return ret;
89
90 *val = be16_to_cpu(buf);
91 break;
92 }
93 case 3: {
94 __be32 buf;
95
96 ret = spi_read(data->spi, (void *) &buf, 3);
97 if (ret)
98 return ret;
99
100 *val = be32_to_cpu(buf) >> 8;
101 break;
102 }
103 default:
104 return -EINVAL;
105 }
106
107 *val = sign_extend32(*val >> data->shift, chan->scan_type.realbits - 1);
108
109 return 0;
110}
111
112static irqreturn_t ti_adc_trigger_handler(int irq, void *private)
113{
114 struct iio_poll_func *pf = private;
115 struct iio_dev *indio_dev = pf->indio_dev;
116 struct ti_adc_data *data = iio_priv(indio_dev);
117 int ret;
118
119 ret = ti_adc_read_measurement(data, &indio_dev->channels[0],
120 (int *) &data->buffer);
121 if (!ret)
122 iio_push_to_buffers_with_timestamp(indio_dev,
123 data->buffer,
124 iio_get_time_ns(indio_dev));
125
126 iio_trigger_notify_done(indio_dev->trig);
127
128 return IRQ_HANDLED;
129}
130
131static int ti_adc_read_raw(struct iio_dev *indio_dev,
132 struct iio_chan_spec const *chan,
133 int *val, int *val2, long mask)
134{
135 struct ti_adc_data *data = iio_priv(indio_dev);
136 int ret;
137
138 if (mask != IIO_CHAN_INFO_RAW)
139 return -EINVAL;
140
141 ret = iio_device_claim_direct_mode(indio_dev);
142 if (ret)
143 return ret;
144
145 ret = ti_adc_read_measurement(data, chan, val);
146 iio_device_release_direct_mode(indio_dev);
147
148 if (!ret)
149 return IIO_VAL_INT;
150
151 return 0;
152}
153
154static const struct iio_info ti_adc_info = {
155 .driver_module = THIS_MODULE,
156 .read_raw = ti_adc_read_raw,
157};
158
159static int ti_adc_probe(struct spi_device *spi)
160{
161 struct iio_dev *indio_dev;
162 struct ti_adc_data *data;
163 int ret;
164
165 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
166 if (!indio_dev)
167 return -ENOMEM;
168
169 indio_dev->info = &ti_adc_info;
170 indio_dev->dev.parent = &spi->dev;
171 indio_dev->dev.of_node = spi->dev.of_node;
172 indio_dev->name = TI_ADC_DRV_NAME;
173 indio_dev->modes = INDIO_DIRECT_MODE;
174 spi_set_drvdata(spi, indio_dev);
175
176 data = iio_priv(indio_dev);
177 data->spi = spi;
178
179 switch (spi_get_device_id(spi)->driver_data) {
180 case TI_ADC141S626:
181 indio_dev->channels = ti_adc141s626_channels;
182 indio_dev->num_channels = ARRAY_SIZE(ti_adc141s626_channels);
183 data->shift = 0;
184 data->read_size = 2;
185 break;
186 case TI_ADC161S626:
187 indio_dev->channels = ti_adc161s626_channels;
188 indio_dev->num_channels = ARRAY_SIZE(ti_adc161s626_channels);
189 data->shift = 6;
190 data->read_size = 3;
191 break;
192 }
193
194 ret = iio_triggered_buffer_setup(indio_dev, NULL,
195 ti_adc_trigger_handler, NULL);
196 if (ret)
197 return ret;
198
199 ret = iio_device_register(indio_dev);
200 if (ret)
201 goto error_unreg_buffer;
202
203 return 0;
204
205error_unreg_buffer:
206 iio_triggered_buffer_cleanup(indio_dev);
207
208 return ret;
209}
210
211static int ti_adc_remove(struct spi_device *spi)
212{
213 struct iio_dev *indio_dev = spi_get_drvdata(spi);
214
215 iio_device_unregister(indio_dev);
216 iio_triggered_buffer_cleanup(indio_dev);
217
218 return 0;
219}
220
221static const struct of_device_id ti_adc_dt_ids[] = {
222 { .compatible = "ti,adc141s626", },
223 { .compatible = "ti,adc161s626", },
224 {}
225};
226MODULE_DEVICE_TABLE(of, ti_adc_dt_ids);
227
228static const struct spi_device_id ti_adc_id[] = {
229 {"adc141s626", TI_ADC141S626},
230 {"adc161s626", TI_ADC161S626},
231 {},
232};
233MODULE_DEVICE_TABLE(spi, ti_adc_id);
234
235static struct spi_driver ti_adc_driver = {
236 .driver = {
237 .name = TI_ADC_DRV_NAME,
238 .of_match_table = of_match_ptr(ti_adc_dt_ids),
239 },
240 .probe = ti_adc_probe,
241 .remove = ti_adc_remove,
242 .id_table = ti_adc_id,
243};
244module_spi_driver(ti_adc_driver);
245
246MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
247MODULE_DESCRIPTION("Texas Instruments ADC1x1S 1-channel differential ADC");
248MODULE_LICENSE("GPL");
diff --git a/drivers/iio/adc/ti-ads1015.c b/drivers/iio/adc/ti-ads1015.c
index 066abaf80201..cde6f130a99a 100644
--- a/drivers/iio/adc/ti-ads1015.c
+++ b/drivers/iio/adc/ti-ads1015.c
@@ -522,6 +522,7 @@ static int ads1015_get_channels_config_of(struct i2c_client *client)
522 if (pga > 6) { 522 if (pga > 6) {
523 dev_err(&client->dev, "invalid gain on %s\n", 523 dev_err(&client->dev, "invalid gain on %s\n",
524 node->full_name); 524 node->full_name);
525 of_node_put(node);
525 return -EINVAL; 526 return -EINVAL;
526 } 527 }
527 } 528 }
@@ -532,6 +533,7 @@ static int ads1015_get_channels_config_of(struct i2c_client *client)
532 dev_err(&client->dev, 533 dev_err(&client->dev,
533 "invalid data_rate on %s\n", 534 "invalid data_rate on %s\n",
534 node->full_name); 535 node->full_name);
536 of_node_put(node);
535 return -EINVAL; 537 return -EINVAL;
536 } 538 }
537 } 539 }
diff --git a/drivers/iio/adc/ti-ads8688.c b/drivers/iio/adc/ti-ads8688.c
index c400439900af..4a163496d9e4 100644
--- a/drivers/iio/adc/ti-ads8688.c
+++ b/drivers/iio/adc/ti-ads8688.c
@@ -438,7 +438,7 @@ static int ads8688_probe(struct spi_device *spi)
438 return 0; 438 return 0;
439 439
440error_out: 440error_out:
441 if (!IS_ERR_OR_NULL(st->reg)) 441 if (!IS_ERR(st->reg))
442 regulator_disable(st->reg); 442 regulator_disable(st->reg);
443 443
444 return ret; 444 return ret;
@@ -451,7 +451,7 @@ static int ads8688_remove(struct spi_device *spi)
451 451
452 iio_device_unregister(indio_dev); 452 iio_device_unregister(indio_dev);
453 453
454 if (!IS_ERR_OR_NULL(st->reg)) 454 if (!IS_ERR(st->reg))
455 regulator_disable(st->reg); 455 regulator_disable(st->reg);
456 456
457 return 0; 457 return 0;
diff --git a/drivers/iio/buffer/industrialio-buffer-cb.c b/drivers/iio/buffer/industrialio-buffer-cb.c
index 323079c3ccce..b8f550e47d3d 100644
--- a/drivers/iio/buffer/industrialio-buffer-cb.c
+++ b/drivers/iio/buffer/industrialio-buffer-cb.c
@@ -18,6 +18,7 @@ struct iio_cb_buffer {
18 int (*cb)(const void *data, void *private); 18 int (*cb)(const void *data, void *private);
19 void *private; 19 void *private;
20 struct iio_channel *channels; 20 struct iio_channel *channels;
21 struct iio_dev *indio_dev;
21}; 22};
22 23
23static struct iio_cb_buffer *buffer_to_cb_buffer(struct iio_buffer *buffer) 24static struct iio_cb_buffer *buffer_to_cb_buffer(struct iio_buffer *buffer)
@@ -52,7 +53,6 @@ struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
52{ 53{
53 int ret; 54 int ret;
54 struct iio_cb_buffer *cb_buff; 55 struct iio_cb_buffer *cb_buff;
55 struct iio_dev *indio_dev;
56 struct iio_channel *chan; 56 struct iio_channel *chan;
57 57
58 cb_buff = kzalloc(sizeof(*cb_buff), GFP_KERNEL); 58 cb_buff = kzalloc(sizeof(*cb_buff), GFP_KERNEL);
@@ -72,17 +72,17 @@ struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
72 goto error_free_cb_buff; 72 goto error_free_cb_buff;
73 } 73 }
74 74
75 indio_dev = cb_buff->channels[0].indio_dev; 75 cb_buff->indio_dev = cb_buff->channels[0].indio_dev;
76 cb_buff->buffer.scan_mask 76 cb_buff->buffer.scan_mask
77 = kcalloc(BITS_TO_LONGS(indio_dev->masklength), sizeof(long), 77 = kcalloc(BITS_TO_LONGS(cb_buff->indio_dev->masklength),
78 GFP_KERNEL); 78 sizeof(long), GFP_KERNEL);
79 if (cb_buff->buffer.scan_mask == NULL) { 79 if (cb_buff->buffer.scan_mask == NULL) {
80 ret = -ENOMEM; 80 ret = -ENOMEM;
81 goto error_release_channels; 81 goto error_release_channels;
82 } 82 }
83 chan = &cb_buff->channels[0]; 83 chan = &cb_buff->channels[0];
84 while (chan->indio_dev) { 84 while (chan->indio_dev) {
85 if (chan->indio_dev != indio_dev) { 85 if (chan->indio_dev != cb_buff->indio_dev) {
86 ret = -EINVAL; 86 ret = -EINVAL;
87 goto error_free_scan_mask; 87 goto error_free_scan_mask;
88 } 88 }
@@ -105,17 +105,14 @@ EXPORT_SYMBOL_GPL(iio_channel_get_all_cb);
105 105
106int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff) 106int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff)
107{ 107{
108 return iio_update_buffers(cb_buff->channels[0].indio_dev, 108 return iio_update_buffers(cb_buff->indio_dev, &cb_buff->buffer,
109 &cb_buff->buffer,
110 NULL); 109 NULL);
111} 110}
112EXPORT_SYMBOL_GPL(iio_channel_start_all_cb); 111EXPORT_SYMBOL_GPL(iio_channel_start_all_cb);
113 112
114void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff) 113void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff)
115{ 114{
116 iio_update_buffers(cb_buff->channels[0].indio_dev, 115 iio_update_buffers(cb_buff->indio_dev, NULL, &cb_buff->buffer);
117 NULL,
118 &cb_buff->buffer);
119} 116}
120EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb); 117EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb);
121 118
@@ -133,6 +130,13 @@ struct iio_channel
133} 130}
134EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels); 131EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels);
135 132
133struct iio_dev
134*iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer)
135{
136 return cb_buffer->indio_dev;
137}
138EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev);
139
136MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>"); 140MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
137MODULE_DESCRIPTION("Industrial I/O callback buffer"); 141MODULE_DESCRIPTION("Industrial I/O callback buffer");
138MODULE_LICENSE("GPL"); 142MODULE_LICENSE("GPL");
diff --git a/drivers/iio/buffer/industrialio-triggered-buffer.c b/drivers/iio/buffer/industrialio-triggered-buffer.c
index 4b2858ba1fd6..d3db1fce54d2 100644
--- a/drivers/iio/buffer/industrialio-triggered-buffer.c
+++ b/drivers/iio/buffer/industrialio-triggered-buffer.c
@@ -98,6 +98,48 @@ void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
98} 98}
99EXPORT_SYMBOL(iio_triggered_buffer_cleanup); 99EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
100 100
101static void devm_iio_triggered_buffer_clean(struct device *dev, void *res)
102{
103 iio_triggered_buffer_cleanup(*(struct iio_dev **)res);
104}
105
106int devm_iio_triggered_buffer_setup(struct device *dev,
107 struct iio_dev *indio_dev,
108 irqreturn_t (*h)(int irq, void *p),
109 irqreturn_t (*thread)(int irq, void *p),
110 const struct iio_buffer_setup_ops *ops)
111{
112 struct iio_dev **ptr;
113 int ret;
114
115 ptr = devres_alloc(devm_iio_triggered_buffer_clean, sizeof(*ptr),
116 GFP_KERNEL);
117 if (!ptr)
118 return -ENOMEM;
119
120 *ptr = indio_dev;
121
122 ret = iio_triggered_buffer_setup(indio_dev, h, thread, ops);
123 if (!ret)
124 devres_add(dev, ptr);
125 else
126 devres_free(ptr);
127
128 return ret;
129}
130EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup);
131
132void devm_iio_triggered_buffer_cleanup(struct device *dev,
133 struct iio_dev *indio_dev)
134{
135 int rc;
136
137 rc = devres_release(dev, devm_iio_triggered_buffer_clean,
138 devm_iio_device_match, indio_dev);
139 WARN_ON(rc);
140}
141EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_cleanup);
142
101MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 143MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
102MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers"); 144MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
103MODULE_LICENSE("GPL"); 145MODULE_LICENSE("GPL");
diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
index 4bcc025e8c8a..cea7f9857a1f 100644
--- a/drivers/iio/chemical/Kconfig
+++ b/drivers/iio/chemical/Kconfig
@@ -16,6 +16,7 @@ config ATLAS_PH_SENSOR
16 Atlas Scientific OEM SM sensors: 16 Atlas Scientific OEM SM sensors:
17 * pH SM sensor 17 * pH SM sensor
18 * EC SM sensor 18 * EC SM sensor
19 * ORP SM sensor
19 20
20 To compile this driver as module, choose M here: the 21 To compile this driver as module, choose M here: the
21 module will be called atlas-ph-sensor. 22 module will be called atlas-ph-sensor.
diff --git a/drivers/iio/chemical/atlas-ph-sensor.c b/drivers/iio/chemical/atlas-ph-sensor.c
index 407f141a1eee..bd321b305a0a 100644
--- a/drivers/iio/chemical/atlas-ph-sensor.c
+++ b/drivers/iio/chemical/atlas-ph-sensor.c
@@ -66,12 +66,17 @@
66#define ATLAS_REG_TDS_DATA 0x1c 66#define ATLAS_REG_TDS_DATA 0x1c
67#define ATLAS_REG_PSS_DATA 0x20 67#define ATLAS_REG_PSS_DATA 0x20
68 68
69#define ATLAS_REG_ORP_CALIB_STATUS 0x0d
70#define ATLAS_REG_ORP_DATA 0x0e
71
69#define ATLAS_PH_INT_TIME_IN_US 450000 72#define ATLAS_PH_INT_TIME_IN_US 450000
70#define ATLAS_EC_INT_TIME_IN_US 650000 73#define ATLAS_EC_INT_TIME_IN_US 650000
74#define ATLAS_ORP_INT_TIME_IN_US 450000
71 75
72enum { 76enum {
73 ATLAS_PH_SM, 77 ATLAS_PH_SM,
74 ATLAS_EC_SM, 78 ATLAS_EC_SM,
79 ATLAS_ORP_SM,
75}; 80};
76 81
77struct atlas_data { 82struct atlas_data {
@@ -84,26 +89,10 @@ struct atlas_data {
84 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */ 89 __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */
85}; 90};
86 91
87static const struct regmap_range atlas_volatile_ranges[] = {
88 regmap_reg_range(ATLAS_REG_INT_CONTROL, ATLAS_REG_INT_CONTROL),
89 regmap_reg_range(ATLAS_REG_PH_DATA, ATLAS_REG_PH_DATA + 4),
90 regmap_reg_range(ATLAS_REG_EC_DATA, ATLAS_REG_PSS_DATA + 4),
91};
92
93static const struct regmap_access_table atlas_volatile_table = {
94 .yes_ranges = atlas_volatile_ranges,
95 .n_yes_ranges = ARRAY_SIZE(atlas_volatile_ranges),
96};
97
98static const struct regmap_config atlas_regmap_config = { 92static const struct regmap_config atlas_regmap_config = {
99 .name = ATLAS_REGMAP_NAME, 93 .name = ATLAS_REGMAP_NAME,
100
101 .reg_bits = 8, 94 .reg_bits = 8,
102 .val_bits = 8, 95 .val_bits = 8,
103
104 .volatile_table = &atlas_volatile_table,
105 .max_register = ATLAS_REG_PSS_DATA + 4,
106 .cache_type = REGCACHE_RBTREE,
107}; 96};
108 97
109static const struct iio_chan_spec atlas_ph_channels[] = { 98static const struct iio_chan_spec atlas_ph_channels[] = {
@@ -175,6 +164,23 @@ static const struct iio_chan_spec atlas_ec_channels[] = {
175 }, 164 },
176}; 165};
177 166
167static const struct iio_chan_spec atlas_orp_channels[] = {
168 {
169 .type = IIO_VOLTAGE,
170 .address = ATLAS_REG_ORP_DATA,
171 .info_mask_separate =
172 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
173 .scan_index = 0,
174 .scan_type = {
175 .sign = 's',
176 .realbits = 32,
177 .storagebits = 32,
178 .endianness = IIO_BE,
179 },
180 },
181 IIO_CHAN_SOFT_TIMESTAMP(1),
182};
183
178static int atlas_check_ph_calibration(struct atlas_data *data) 184static int atlas_check_ph_calibration(struct atlas_data *data)
179{ 185{
180 struct device *dev = &data->client->dev; 186 struct device *dev = &data->client->dev;
@@ -240,6 +246,22 @@ static int atlas_check_ec_calibration(struct atlas_data *data)
240 return 0; 246 return 0;
241} 247}
242 248
249static int atlas_check_orp_calibration(struct atlas_data *data)
250{
251 struct device *dev = &data->client->dev;
252 int ret;
253 unsigned int val;
254
255 ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val);
256 if (ret)
257 return ret;
258
259 if (!val)
260 dev_warn(dev, "device has not been calibrated\n");
261
262 return 0;
263};
264
243struct atlas_device { 265struct atlas_device {
244 const struct iio_chan_spec *channels; 266 const struct iio_chan_spec *channels;
245 int num_channels; 267 int num_channels;
@@ -264,7 +286,13 @@ static struct atlas_device atlas_devices[] = {
264 .calibration = &atlas_check_ec_calibration, 286 .calibration = &atlas_check_ec_calibration,
265 .delay = ATLAS_EC_INT_TIME_IN_US, 287 .delay = ATLAS_EC_INT_TIME_IN_US,
266 }, 288 },
267 289 [ATLAS_ORP_SM] = {
290 .channels = atlas_orp_channels,
291 .num_channels = 2,
292 .data_reg = ATLAS_REG_ORP_DATA,
293 .calibration = &atlas_check_orp_calibration,
294 .delay = ATLAS_ORP_INT_TIME_IN_US,
295 },
268}; 296};
269 297
270static int atlas_set_powermode(struct atlas_data *data, int on) 298static int atlas_set_powermode(struct atlas_data *data, int on)
@@ -402,15 +430,14 @@ static int atlas_read_raw(struct iio_dev *indio_dev,
402 case IIO_PH: 430 case IIO_PH:
403 case IIO_CONCENTRATION: 431 case IIO_CONCENTRATION:
404 case IIO_ELECTRICALCONDUCTIVITY: 432 case IIO_ELECTRICALCONDUCTIVITY:
405 mutex_lock(&indio_dev->mlock); 433 case IIO_VOLTAGE:
434 ret = iio_device_claim_direct_mode(indio_dev);
435 if (ret)
436 return ret;
406 437
407 if (iio_buffer_enabled(indio_dev)) 438 ret = atlas_read_measurement(data, chan->address, &reg);
408 ret = -EBUSY;
409 else
410 ret = atlas_read_measurement(data,
411 chan->address, &reg);
412 439
413 mutex_unlock(&indio_dev->mlock); 440 iio_device_release_direct_mode(indio_dev);
414 break; 441 break;
415 default: 442 default:
416 ret = -EINVAL; 443 ret = -EINVAL;
@@ -440,6 +467,10 @@ static int atlas_read_raw(struct iio_dev *indio_dev,
440 *val = 0; /* 0.000000001 */ 467 *val = 0; /* 0.000000001 */
441 *val2 = 1000; 468 *val2 = 1000;
442 return IIO_VAL_INT_PLUS_NANO; 469 return IIO_VAL_INT_PLUS_NANO;
470 case IIO_VOLTAGE:
471 *val = 1; /* 0.1 */
472 *val2 = 10;
473 break;
443 default: 474 default:
444 return -EINVAL; 475 return -EINVAL;
445 } 476 }
@@ -475,6 +506,7 @@ static const struct iio_info atlas_info = {
475static const struct i2c_device_id atlas_id[] = { 506static const struct i2c_device_id atlas_id[] = {
476 { "atlas-ph-sm", ATLAS_PH_SM}, 507 { "atlas-ph-sm", ATLAS_PH_SM},
477 { "atlas-ec-sm", ATLAS_EC_SM}, 508 { "atlas-ec-sm", ATLAS_EC_SM},
509 { "atlas-orp-sm", ATLAS_ORP_SM},
478 {} 510 {}
479}; 511};
480MODULE_DEVICE_TABLE(i2c, atlas_id); 512MODULE_DEVICE_TABLE(i2c, atlas_id);
@@ -482,6 +514,7 @@ MODULE_DEVICE_TABLE(i2c, atlas_id);
482static const struct of_device_id atlas_dt_ids[] = { 514static const struct of_device_id atlas_dt_ids[] = {
483 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, }, 515 { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, },
484 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, }, 516 { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, },
517 { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, },
485 { } 518 { }
486}; 519};
487MODULE_DEVICE_TABLE(of, atlas_dt_ids); 520MODULE_DEVICE_TABLE(of, atlas_dt_ids);
diff --git a/drivers/iio/chemical/vz89x.c b/drivers/iio/chemical/vz89x.c
index 652649da500f..8e0e4415c161 100644
--- a/drivers/iio/chemical/vz89x.c
+++ b/drivers/iio/chemical/vz89x.c
@@ -19,25 +19,55 @@
19#include <linux/mutex.h> 19#include <linux/mutex.h>
20#include <linux/init.h> 20#include <linux/init.h>
21#include <linux/i2c.h> 21#include <linux/i2c.h>
22#include <linux/of.h>
23#include <linux/of_device.h>
22 24
23#include <linux/iio/iio.h> 25#include <linux/iio/iio.h>
24#include <linux/iio/sysfs.h> 26#include <linux/iio/sysfs.h>
25 27
26#define VZ89X_REG_MEASUREMENT 0x09 28#define VZ89X_REG_MEASUREMENT 0x09
27#define VZ89X_REG_MEASUREMENT_SIZE 6 29#define VZ89X_REG_MEASUREMENT_RD_SIZE 6
30#define VZ89X_REG_MEASUREMENT_WR_SIZE 3
28 31
29#define VZ89X_VOC_CO2_IDX 0 32#define VZ89X_VOC_CO2_IDX 0
30#define VZ89X_VOC_SHORT_IDX 1 33#define VZ89X_VOC_SHORT_IDX 1
31#define VZ89X_VOC_TVOC_IDX 2 34#define VZ89X_VOC_TVOC_IDX 2
32#define VZ89X_VOC_RESISTANCE_IDX 3 35#define VZ89X_VOC_RESISTANCE_IDX 3
33 36
37#define VZ89TE_REG_MEASUREMENT 0x0c
38#define VZ89TE_REG_MEASUREMENT_RD_SIZE 7
39#define VZ89TE_REG_MEASUREMENT_WR_SIZE 6
40
41#define VZ89TE_VOC_TVOC_IDX 0
42#define VZ89TE_VOC_CO2_IDX 1
43#define VZ89TE_VOC_RESISTANCE_IDX 2
44
45enum {
46 VZ89X,
47 VZ89TE,
48};
49
50struct vz89x_chip_data;
51
34struct vz89x_data { 52struct vz89x_data {
35 struct i2c_client *client; 53 struct i2c_client *client;
54 const struct vz89x_chip_data *chip;
36 struct mutex lock; 55 struct mutex lock;
37 int (*xfer)(struct vz89x_data *data, u8 cmd); 56 int (*xfer)(struct vz89x_data *data, u8 cmd);
38 57
58 bool is_valid;
39 unsigned long last_update; 59 unsigned long last_update;
40 u8 buffer[VZ89X_REG_MEASUREMENT_SIZE]; 60 u8 buffer[VZ89TE_REG_MEASUREMENT_RD_SIZE];
61};
62
63struct vz89x_chip_data {
64 bool (*valid)(struct vz89x_data *data);
65 const struct iio_chan_spec *channels;
66 u8 num_channels;
67
68 u8 cmd;
69 u8 read_size;
70 u8 write_size;
41}; 71};
42 72
43static const struct iio_chan_spec vz89x_channels[] = { 73static const struct iio_chan_spec vz89x_channels[] = {
@@ -70,6 +100,40 @@ static const struct iio_chan_spec vz89x_channels[] = {
70 .info_mask_separate = 100 .info_mask_separate =
71 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 101 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
72 .address = VZ89X_VOC_RESISTANCE_IDX, 102 .address = VZ89X_VOC_RESISTANCE_IDX,
103 .scan_index = -1,
104 .scan_type = {
105 .endianness = IIO_LE,
106 },
107 },
108};
109
110static const struct iio_chan_spec vz89te_channels[] = {
111 {
112 .type = IIO_CONCENTRATION,
113 .channel2 = IIO_MOD_VOC,
114 .modified = 1,
115 .info_mask_separate =
116 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
117 .address = VZ89TE_VOC_TVOC_IDX,
118 },
119
120 {
121 .type = IIO_CONCENTRATION,
122 .channel2 = IIO_MOD_CO2,
123 .modified = 1,
124 .info_mask_separate =
125 BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_RAW),
126 .address = VZ89TE_VOC_CO2_IDX,
127 },
128 {
129 .type = IIO_RESISTANCE,
130 .info_mask_separate =
131 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
132 .address = VZ89TE_VOC_RESISTANCE_IDX,
133 .scan_index = -1,
134 .scan_type = {
135 .endianness = IIO_BE,
136 },
73 }, 137 },
74}; 138};
75 139
@@ -93,29 +157,45 @@ static const struct attribute_group vz89x_attrs_group = {
93 * always zero, and by also confirming the VOC_short isn't zero. 157 * always zero, and by also confirming the VOC_short isn't zero.
94 */ 158 */
95 159
96static int vz89x_measurement_is_valid(struct vz89x_data *data) 160static bool vz89x_measurement_is_valid(struct vz89x_data *data)
97{ 161{
98 if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0) 162 if (data->buffer[VZ89X_VOC_SHORT_IDX] == 0)
99 return 1; 163 return true;
100 164
101 return !!(data->buffer[VZ89X_REG_MEASUREMENT_SIZE - 1] > 0); 165 return !!(data->buffer[data->chip->read_size - 1] > 0);
166}
167
168/* VZ89TE device has a modified CRC-8 two complement check */
169static bool vz89te_measurement_is_valid(struct vz89x_data *data)
170{
171 u8 crc = 0;
172 int i, sum = 0;
173
174 for (i = 0; i < (data->chip->read_size - 1); i++) {
175 sum = crc + data->buffer[i];
176 crc = sum;
177 crc += sum / 256;
178 }
179
180 return !((0xff - crc) == data->buffer[data->chip->read_size - 1]);
102} 181}
103 182
104static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd) 183static int vz89x_i2c_xfer(struct vz89x_data *data, u8 cmd)
105{ 184{
185 const struct vz89x_chip_data *chip = data->chip;
106 struct i2c_client *client = data->client; 186 struct i2c_client *client = data->client;
107 struct i2c_msg msg[2]; 187 struct i2c_msg msg[2];
108 int ret; 188 int ret;
109 u8 buf[3] = { cmd, 0, 0}; 189 u8 buf[6] = { cmd, 0, 0, 0, 0, 0xf3 };
110 190
111 msg[0].addr = client->addr; 191 msg[0].addr = client->addr;
112 msg[0].flags = client->flags; 192 msg[0].flags = client->flags;
113 msg[0].len = 3; 193 msg[0].len = chip->write_size;
114 msg[0].buf = (char *) &buf; 194 msg[0].buf = (char *) &buf;
115 195
116 msg[1].addr = client->addr; 196 msg[1].addr = client->addr;
117 msg[1].flags = client->flags | I2C_M_RD; 197 msg[1].flags = client->flags | I2C_M_RD;
118 msg[1].len = VZ89X_REG_MEASUREMENT_SIZE; 198 msg[1].len = chip->read_size;
119 msg[1].buf = (char *) &data->buffer; 199 msg[1].buf = (char *) &data->buffer;
120 200
121 ret = i2c_transfer(client->adapter, msg, 2); 201 ret = i2c_transfer(client->adapter, msg, 2);
@@ -133,7 +213,7 @@ static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
133 if (ret < 0) 213 if (ret < 0)
134 return ret; 214 return ret;
135 215
136 for (i = 0; i < VZ89X_REG_MEASUREMENT_SIZE; i++) { 216 for (i = 0; i < data->chip->read_size; i++) {
137 ret = i2c_smbus_read_byte(client); 217 ret = i2c_smbus_read_byte(client);
138 if (ret < 0) 218 if (ret < 0)
139 return ret; 219 return ret;
@@ -145,30 +225,47 @@ static int vz89x_smbus_xfer(struct vz89x_data *data, u8 cmd)
145 225
146static int vz89x_get_measurement(struct vz89x_data *data) 226static int vz89x_get_measurement(struct vz89x_data *data)
147{ 227{
228 const struct vz89x_chip_data *chip = data->chip;
148 int ret; 229 int ret;
149 230
150 /* sensor can only be polled once a second max per datasheet */ 231 /* sensor can only be polled once a second max per datasheet */
151 if (!time_after(jiffies, data->last_update + HZ)) 232 if (!time_after(jiffies, data->last_update + HZ))
152 return 0; 233 return data->is_valid ? 0 : -EAGAIN;
234
235 data->is_valid = false;
236 data->last_update = jiffies;
153 237
154 ret = data->xfer(data, VZ89X_REG_MEASUREMENT); 238 ret = data->xfer(data, chip->cmd);
155 if (ret < 0) 239 if (ret < 0)
156 return ret; 240 return ret;
157 241
158 ret = vz89x_measurement_is_valid(data); 242 ret = chip->valid(data);
159 if (ret) 243 if (ret)
160 return -EAGAIN; 244 return -EAGAIN;
161 245
162 data->last_update = jiffies; 246 data->is_valid = true;
163 247
164 return 0; 248 return 0;
165} 249}
166 250
167static int vz89x_get_resistance_reading(struct vz89x_data *data) 251static int vz89x_get_resistance_reading(struct vz89x_data *data,
252 struct iio_chan_spec const *chan,
253 int *val)
168{ 254{
169 u8 *buf = &data->buffer[VZ89X_VOC_RESISTANCE_IDX]; 255 u8 *tmp = (u8 *) &data->buffer[chan->address];
170 256
171 return buf[0] | (buf[1] << 8); 257 switch (chan->scan_type.endianness) {
258 case IIO_LE:
259 *val = le32_to_cpup((__le32 *) tmp) & GENMASK(23, 0);
260 break;
261 case IIO_BE:
262 *val = be32_to_cpup((__be32 *) tmp) >> 8;
263 break;
264 default:
265 return -EINVAL;
266 }
267
268 return 0;
172} 269}
173 270
174static int vz89x_read_raw(struct iio_dev *indio_dev, 271static int vz89x_read_raw(struct iio_dev *indio_dev,
@@ -187,15 +284,15 @@ static int vz89x_read_raw(struct iio_dev *indio_dev,
187 if (ret) 284 if (ret)
188 return ret; 285 return ret;
189 286
190 switch (chan->address) { 287 switch (chan->type) {
191 case VZ89X_VOC_CO2_IDX: 288 case IIO_CONCENTRATION:
192 case VZ89X_VOC_SHORT_IDX:
193 case VZ89X_VOC_TVOC_IDX:
194 *val = data->buffer[chan->address]; 289 *val = data->buffer[chan->address];
195 return IIO_VAL_INT; 290 return IIO_VAL_INT;
196 case VZ89X_VOC_RESISTANCE_IDX: 291 case IIO_RESISTANCE:
197 *val = vz89x_get_resistance_reading(data); 292 ret = vz89x_get_resistance_reading(data, chan, val);
198 return IIO_VAL_INT; 293 if (!ret)
294 return IIO_VAL_INT;
295 break;
199 default: 296 default:
200 return -EINVAL; 297 return -EINVAL;
201 } 298 }
@@ -210,12 +307,12 @@ static int vz89x_read_raw(struct iio_dev *indio_dev,
210 } 307 }
211 break; 308 break;
212 case IIO_CHAN_INFO_OFFSET: 309 case IIO_CHAN_INFO_OFFSET:
213 switch (chan->address) { 310 switch (chan->channel2) {
214 case VZ89X_VOC_CO2_IDX: 311 case IIO_MOD_CO2:
215 *val = 44; 312 *val = 44;
216 *val2 = 250000; 313 *val2 = 250000;
217 return IIO_VAL_INT_PLUS_MICRO; 314 return IIO_VAL_INT_PLUS_MICRO;
218 case VZ89X_VOC_TVOC_IDX: 315 case IIO_MOD_VOC:
219 *val = -13; 316 *val = -13;
220 return IIO_VAL_INT; 317 return IIO_VAL_INT;
221 default: 318 default:
@@ -232,11 +329,43 @@ static const struct iio_info vz89x_info = {
232 .driver_module = THIS_MODULE, 329 .driver_module = THIS_MODULE,
233}; 330};
234 331
332static const struct vz89x_chip_data vz89x_chips[] = {
333 {
334 .valid = vz89x_measurement_is_valid,
335
336 .cmd = VZ89X_REG_MEASUREMENT,
337 .read_size = VZ89X_REG_MEASUREMENT_RD_SIZE,
338 .write_size = VZ89X_REG_MEASUREMENT_WR_SIZE,
339
340 .channels = vz89x_channels,
341 .num_channels = ARRAY_SIZE(vz89x_channels),
342 },
343 {
344 .valid = vz89te_measurement_is_valid,
345
346 .cmd = VZ89TE_REG_MEASUREMENT,
347 .read_size = VZ89TE_REG_MEASUREMENT_RD_SIZE,
348 .write_size = VZ89TE_REG_MEASUREMENT_WR_SIZE,
349
350 .channels = vz89te_channels,
351 .num_channels = ARRAY_SIZE(vz89te_channels),
352 },
353};
354
355static const struct of_device_id vz89x_dt_ids[] = {
356 { .compatible = "sgx,vz89x", .data = (void *) VZ89X },
357 { .compatible = "sgx,vz89te", .data = (void *) VZ89TE },
358 { }
359};
360MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
361
235static int vz89x_probe(struct i2c_client *client, 362static int vz89x_probe(struct i2c_client *client,
236 const struct i2c_device_id *id) 363 const struct i2c_device_id *id)
237{ 364{
238 struct iio_dev *indio_dev; 365 struct iio_dev *indio_dev;
239 struct vz89x_data *data; 366 struct vz89x_data *data;
367 const struct of_device_id *of_id;
368 int chip_id;
240 369
241 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 370 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
242 if (!indio_dev) 371 if (!indio_dev)
@@ -251,8 +380,15 @@ static int vz89x_probe(struct i2c_client *client,
251 else 380 else
252 return -EOPNOTSUPP; 381 return -EOPNOTSUPP;
253 382
383 of_id = of_match_device(vz89x_dt_ids, &client->dev);
384 if (!of_id)
385 chip_id = id->driver_data;
386 else
387 chip_id = (unsigned long)of_id->data;
388
254 i2c_set_clientdata(client, indio_dev); 389 i2c_set_clientdata(client, indio_dev);
255 data->client = client; 390 data->client = client;
391 data->chip = &vz89x_chips[chip_id];
256 data->last_update = jiffies - HZ; 392 data->last_update = jiffies - HZ;
257 mutex_init(&data->lock); 393 mutex_init(&data->lock);
258 394
@@ -261,24 +397,19 @@ static int vz89x_probe(struct i2c_client *client,
261 indio_dev->name = dev_name(&client->dev); 397 indio_dev->name = dev_name(&client->dev);
262 indio_dev->modes = INDIO_DIRECT_MODE; 398 indio_dev->modes = INDIO_DIRECT_MODE;
263 399
264 indio_dev->channels = vz89x_channels; 400 indio_dev->channels = data->chip->channels;
265 indio_dev->num_channels = ARRAY_SIZE(vz89x_channels); 401 indio_dev->num_channels = data->chip->num_channels;
266 402
267 return devm_iio_device_register(&client->dev, indio_dev); 403 return devm_iio_device_register(&client->dev, indio_dev);
268} 404}
269 405
270static const struct i2c_device_id vz89x_id[] = { 406static const struct i2c_device_id vz89x_id[] = {
271 { "vz89x", 0 }, 407 { "vz89x", VZ89X },
408 { "vz89te", VZ89TE },
272 { } 409 { }
273}; 410};
274MODULE_DEVICE_TABLE(i2c, vz89x_id); 411MODULE_DEVICE_TABLE(i2c, vz89x_id);
275 412
276static const struct of_device_id vz89x_dt_ids[] = {
277 { .compatible = "sgx,vz89x" },
278 { }
279};
280MODULE_DEVICE_TABLE(of, vz89x_dt_ids);
281
282static struct i2c_driver vz89x_driver = { 413static struct i2c_driver vz89x_driver = {
283 .driver = { 414 .driver = {
284 .name = "vz89x", 415 .name = "vz89x",
diff --git a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
index 5b41f9d0d4f3..a3cce3a38300 100644
--- a/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
+++ b/drivers/iio/common/hid-sensors/hid-sensor-trigger.c
@@ -122,6 +122,14 @@ int hid_sensor_power_state(struct hid_sensor_common *st, bool state)
122#endif 122#endif
123} 123}
124 124
125static void hid_sensor_set_power_work(struct work_struct *work)
126{
127 struct hid_sensor_common *attrb = container_of(work,
128 struct hid_sensor_common,
129 work);
130 _hid_sensor_power_state(attrb, true);
131}
132
125static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig, 133static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
126 bool state) 134 bool state)
127{ 135{
@@ -130,6 +138,7 @@ static int hid_sensor_data_rdy_trigger_set_state(struct iio_trigger *trig,
130 138
131void hid_sensor_remove_trigger(struct hid_sensor_common *attrb) 139void hid_sensor_remove_trigger(struct hid_sensor_common *attrb)
132{ 140{
141 cancel_work_sync(&attrb->work);
133 iio_trigger_unregister(attrb->trigger); 142 iio_trigger_unregister(attrb->trigger);
134 iio_trigger_free(attrb->trigger); 143 iio_trigger_free(attrb->trigger);
135} 144}
@@ -170,6 +179,9 @@ int hid_sensor_setup_trigger(struct iio_dev *indio_dev, const char *name,
170 goto error_unreg_trigger; 179 goto error_unreg_trigger;
171 180
172 iio_device_set_drvdata(indio_dev, attrb); 181 iio_device_set_drvdata(indio_dev, attrb);
182
183 INIT_WORK(&attrb->work, hid_sensor_set_power_work);
184
173 pm_suspend_ignore_children(&attrb->pdev->dev, true); 185 pm_suspend_ignore_children(&attrb->pdev->dev, true);
174 pm_runtime_enable(&attrb->pdev->dev); 186 pm_runtime_enable(&attrb->pdev->dev);
175 /* Default to 3 seconds, but can be changed from sysfs */ 187 /* Default to 3 seconds, but can be changed from sysfs */
@@ -187,8 +199,7 @@ error_ret:
187} 199}
188EXPORT_SYMBOL(hid_sensor_setup_trigger); 200EXPORT_SYMBOL(hid_sensor_setup_trigger);
189 201
190#ifdef CONFIG_PM 202static int __maybe_unused hid_sensor_suspend(struct device *dev)
191static int hid_sensor_suspend(struct device *dev)
192{ 203{
193 struct platform_device *pdev = to_platform_device(dev); 204 struct platform_device *pdev = to_platform_device(dev);
194 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 205 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
@@ -197,21 +208,27 @@ static int hid_sensor_suspend(struct device *dev)
197 return _hid_sensor_power_state(attrb, false); 208 return _hid_sensor_power_state(attrb, false);
198} 209}
199 210
200static int hid_sensor_resume(struct device *dev) 211static int __maybe_unused hid_sensor_resume(struct device *dev)
201{ 212{
202 struct platform_device *pdev = to_platform_device(dev); 213 struct platform_device *pdev = to_platform_device(dev);
203 struct iio_dev *indio_dev = platform_get_drvdata(pdev); 214 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
204 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev); 215 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
216 schedule_work(&attrb->work);
217 return 0;
218}
205 219
220static int __maybe_unused hid_sensor_runtime_resume(struct device *dev)
221{
222 struct platform_device *pdev = to_platform_device(dev);
223 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
224 struct hid_sensor_common *attrb = iio_device_get_drvdata(indio_dev);
206 return _hid_sensor_power_state(attrb, true); 225 return _hid_sensor_power_state(attrb, true);
207} 226}
208 227
209#endif
210
211const struct dev_pm_ops hid_sensor_pm_ops = { 228const struct dev_pm_ops hid_sensor_pm_ops = {
212 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume) 229 SET_SYSTEM_SLEEP_PM_OPS(hid_sensor_suspend, hid_sensor_resume)
213 SET_RUNTIME_PM_OPS(hid_sensor_suspend, 230 SET_RUNTIME_PM_OPS(hid_sensor_suspend,
214 hid_sensor_resume, NULL) 231 hid_sensor_runtime_resume, NULL)
215}; 232};
216EXPORT_SYMBOL(hid_sensor_pm_ops); 233EXPORT_SYMBOL(hid_sensor_pm_ops);
217 234
diff --git a/drivers/iio/common/st_sensors/st_sensors_buffer.c b/drivers/iio/common/st_sensors/st_sensors_buffer.c
index d06e728cea37..fe7775bb3740 100644
--- a/drivers/iio/common/st_sensors/st_sensors_buffer.c
+++ b/drivers/iio/common/st_sensors/st_sensors_buffer.c
@@ -63,7 +63,7 @@ irqreturn_t st_sensors_trigger_handler(int irq, void *p)
63 * the hardware trigger) and the hw_timestamp may get updated. 63 * the hardware trigger) and the hw_timestamp may get updated.
64 * By storing it in a local variable first, we are safe. 64 * By storing it in a local variable first, we are safe.
65 */ 65 */
66 if (sdata->hw_irq_trigger) 66 if (iio_trigger_using_own(indio_dev))
67 timestamp = sdata->hw_timestamp; 67 timestamp = sdata->hw_timestamp;
68 else 68 else
69 timestamp = iio_get_time_ns(indio_dev); 69 timestamp = iio_get_time_ns(indio_dev);
diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index 2d5282e05482..285a64a589d7 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -234,39 +234,35 @@ int st_sensors_power_enable(struct iio_dev *indio_dev)
234 int err; 234 int err;
235 235
236 /* Regulators not mandatory, but if requested we should enable them. */ 236 /* Regulators not mandatory, but if requested we should enable them. */
237 pdata->vdd = devm_regulator_get_optional(indio_dev->dev.parent, "vdd"); 237 pdata->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
238 if (!IS_ERR(pdata->vdd)) { 238 if (IS_ERR(pdata->vdd)) {
239 err = regulator_enable(pdata->vdd); 239 dev_err(&indio_dev->dev, "unable to get Vdd supply\n");
240 if (err != 0) { 240 return PTR_ERR(pdata->vdd);
241 dev_warn(&indio_dev->dev, 241 }
242 "Failed to enable specified Vdd supply\n"); 242 err = regulator_enable(pdata->vdd);
243 return err; 243 if (err != 0) {
244 } 244 dev_warn(&indio_dev->dev,
245 } else { 245 "Failed to enable specified Vdd supply\n");
246 err = PTR_ERR(pdata->vdd); 246 return err;
247 if (err != -ENODEV)
248 return err;
249 } 247 }
250 248
251 pdata->vdd_io = devm_regulator_get_optional(indio_dev->dev.parent, "vddio"); 249 pdata->vdd_io = devm_regulator_get(indio_dev->dev.parent, "vddio");
252 if (!IS_ERR(pdata->vdd_io)) { 250 if (IS_ERR(pdata->vdd_io)) {
253 err = regulator_enable(pdata->vdd_io); 251 dev_err(&indio_dev->dev, "unable to get Vdd_IO supply\n");
254 if (err != 0) {
255 dev_warn(&indio_dev->dev,
256 "Failed to enable specified Vdd_IO supply\n");
257 goto st_sensors_disable_vdd;
258 }
259 } else {
260 err = PTR_ERR(pdata->vdd_io); 252 err = PTR_ERR(pdata->vdd_io);
261 if (err != -ENODEV) 253 goto st_sensors_disable_vdd;
262 goto st_sensors_disable_vdd; 254 }
255 err = regulator_enable(pdata->vdd_io);
256 if (err != 0) {
257 dev_warn(&indio_dev->dev,
258 "Failed to enable specified Vdd_IO supply\n");
259 goto st_sensors_disable_vdd;
263 } 260 }
264 261
265 return 0; 262 return 0;
266 263
267st_sensors_disable_vdd: 264st_sensors_disable_vdd:
268 if (!IS_ERR_OR_NULL(pdata->vdd)) 265 regulator_disable(pdata->vdd);
269 regulator_disable(pdata->vdd);
270 return err; 266 return err;
271} 267}
272EXPORT_SYMBOL(st_sensors_power_enable); 268EXPORT_SYMBOL(st_sensors_power_enable);
@@ -275,11 +271,8 @@ void st_sensors_power_disable(struct iio_dev *indio_dev)
275{ 271{
276 struct st_sensor_data *pdata = iio_priv(indio_dev); 272 struct st_sensor_data *pdata = iio_priv(indio_dev);
277 273
278 if (!IS_ERR_OR_NULL(pdata->vdd)) 274 regulator_disable(pdata->vdd);
279 regulator_disable(pdata->vdd); 275 regulator_disable(pdata->vdd_io);
280
281 if (!IS_ERR_OR_NULL(pdata->vdd_io))
282 regulator_disable(pdata->vdd_io);
283} 276}
284EXPORT_SYMBOL(st_sensors_power_disable); 277EXPORT_SYMBOL(st_sensors_power_disable);
285 278
diff --git a/drivers/iio/common/st_sensors/st_sensors_trigger.c b/drivers/iio/common/st_sensors/st_sensors_trigger.c
index e66f12ee8a55..fa73e6795359 100644
--- a/drivers/iio/common/st_sensors/st_sensors_trigger.c
+++ b/drivers/iio/common/st_sensors/st_sensors_trigger.c
@@ -66,7 +66,7 @@ static int st_sensors_new_samples_available(struct iio_dev *indio_dev,
66 * @irq: irq number 66 * @irq: irq number
67 * @p: private handler data 67 * @p: private handler data
68 */ 68 */
69irqreturn_t st_sensors_irq_handler(int irq, void *p) 69static irqreturn_t st_sensors_irq_handler(int irq, void *p)
70{ 70{
71 struct iio_trigger *trig = p; 71 struct iio_trigger *trig = p;
72 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 72 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
@@ -82,7 +82,7 @@ irqreturn_t st_sensors_irq_handler(int irq, void *p)
82 * @irq: irq number 82 * @irq: irq number
83 * @p: private handler data 83 * @p: private handler data
84 */ 84 */
85irqreturn_t st_sensors_irq_thread(int irq, void *p) 85static irqreturn_t st_sensors_irq_thread(int irq, void *p)
86{ 86{
87 struct iio_trigger *trig = p; 87 struct iio_trigger *trig = p;
88 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig); 88 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
diff --git a/drivers/iio/dac/Kconfig b/drivers/iio/dac/Kconfig
index ca814479fadf..120b24478469 100644
--- a/drivers/iio/dac/Kconfig
+++ b/drivers/iio/dac/Kconfig
@@ -181,6 +181,25 @@ config AD7303
181 To compile this driver as module choose M here: the module will be called 181 To compile this driver as module choose M here: the module will be called
182 ad7303. 182 ad7303.
183 183
184config CIO_DAC
185 tristate "Measurement Computing CIO-DAC IIO driver"
186 depends on X86 && ISA_BUS_API
187 help
188 Say yes here to build support for the Measurement Computing CIO-DAC
189 analog output device family (CIO-DAC16, CIO-DAC08, PC104-DAC06). The
190 base port addresses for the devices may be configured via the base
191 array module parameter.
192
193config AD8801
194 tristate "Analog Devices AD8801/AD8803 DAC driver"
195 depends on SPI_MASTER
196 help
197 Say yes here to build support for Analog Devices AD8801, AD8803 Digital to
198 Analog Converters (DAC).
199
200 To compile this driver as a module choose M here: the module will be called
201 ad8801.
202
184config LPC18XX_DAC 203config LPC18XX_DAC
185 tristate "NXP LPC18xx DAC driver" 204 tristate "NXP LPC18xx DAC driver"
186 depends on ARCH_LPC18XX || COMPILE_TEST 205 depends on ARCH_LPC18XX || COMPILE_TEST
@@ -245,16 +264,6 @@ config MCP4922
245 To compile this driver as a module, choose M here: the module 264 To compile this driver as a module, choose M here: the module
246 will be called mcp4922. 265 will be called mcp4922.
247 266
248config STX104
249 tristate "Apex Embedded Systems STX104 DAC driver"
250 depends on X86 && ISA_BUS_API
251 select GPIOLIB
252 help
253 Say yes here to build support for the 2-channel DAC and GPIO on the
254 Apex Embedded Systems STX104 integrated analog PC/104 card. The base
255 port addresses for the devices may be configured via the base array
256 module parameter.
257
258config VF610_DAC 267config VF610_DAC
259 tristate "Vybrid vf610 DAC driver" 268 tristate "Vybrid vf610 DAC driver"
260 depends on OF 269 depends on OF
diff --git a/drivers/iio/dac/Makefile b/drivers/iio/dac/Makefile
index 8b78d5ca9b11..27642bbf75f2 100644
--- a/drivers/iio/dac/Makefile
+++ b/drivers/iio/dac/Makefile
@@ -20,11 +20,12 @@ obj-$(CONFIG_AD5764) += ad5764.o
20obj-$(CONFIG_AD5791) += ad5791.o 20obj-$(CONFIG_AD5791) += ad5791.o
21obj-$(CONFIG_AD5686) += ad5686.o 21obj-$(CONFIG_AD5686) += ad5686.o
22obj-$(CONFIG_AD7303) += ad7303.o 22obj-$(CONFIG_AD7303) += ad7303.o
23obj-$(CONFIG_AD8801) += ad8801.o
24obj-$(CONFIG_CIO_DAC) += cio-dac.o
23obj-$(CONFIG_LPC18XX_DAC) += lpc18xx_dac.o 25obj-$(CONFIG_LPC18XX_DAC) += lpc18xx_dac.o
24obj-$(CONFIG_M62332) += m62332.o 26obj-$(CONFIG_M62332) += m62332.o
25obj-$(CONFIG_MAX517) += max517.o 27obj-$(CONFIG_MAX517) += max517.o
26obj-$(CONFIG_MAX5821) += max5821.o 28obj-$(CONFIG_MAX5821) += max5821.o
27obj-$(CONFIG_MCP4725) += mcp4725.o 29obj-$(CONFIG_MCP4725) += mcp4725.o
28obj-$(CONFIG_MCP4922) += mcp4922.o 30obj-$(CONFIG_MCP4922) += mcp4922.o
29obj-$(CONFIG_STX104) += stx104.o
30obj-$(CONFIG_VF610_DAC) += vf610_dac.o 31obj-$(CONFIG_VF610_DAC) += vf610_dac.o
diff --git a/drivers/iio/dac/ad5755.c b/drivers/iio/dac/ad5755.c
index 0fde593ec0d9..5f7968232564 100644
--- a/drivers/iio/dac/ad5755.c
+++ b/drivers/iio/dac/ad5755.c
@@ -655,7 +655,7 @@ static struct ad5755_platform_data *ad5755_parse_dt(struct device *dev)
655 655
656 devnr = 0; 656 devnr = 0;
657 for_each_child_of_node(np, pp) { 657 for_each_child_of_node(np, pp) {
658 if (devnr > AD5755_NUM_CHANNELS) { 658 if (devnr >= AD5755_NUM_CHANNELS) {
659 dev_err(dev, 659 dev_err(dev,
660 "There is to many channels defined in DT\n"); 660 "There is to many channels defined in DT\n");
661 goto error_out; 661 goto error_out;
diff --git a/drivers/iio/dac/ad8801.c b/drivers/iio/dac/ad8801.c
new file mode 100644
index 000000000000..f06faa1aec09
--- /dev/null
+++ b/drivers/iio/dac/ad8801.c
@@ -0,0 +1,239 @@
1/*
2 * IIO DAC driver for Analog Devices AD8801 DAC
3 *
4 * Copyright (C) 2016 Gwenhael Goavec-Merou
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 */
15
16#include <linux/iio/iio.h>
17#include <linux/module.h>
18#include <linux/regulator/consumer.h>
19#include <linux/spi/spi.h>
20#include <linux/sysfs.h>
21
22#define AD8801_CFG_ADDR_OFFSET 8
23
24enum ad8801_device_ids {
25 ID_AD8801,
26 ID_AD8803,
27};
28
29struct ad8801_state {
30 struct spi_device *spi;
31 unsigned char dac_cache[8]; /* Value write on each channel */
32 unsigned int vrefh_mv;
33 unsigned int vrefl_mv;
34 struct regulator *vrefh_reg;
35 struct regulator *vrefl_reg;
36
37 __be16 data ____cacheline_aligned;
38};
39
40static int ad8801_spi_write(struct ad8801_state *state,
41 u8 channel, unsigned char value)
42{
43 state->data = cpu_to_be16((channel << AD8801_CFG_ADDR_OFFSET) | value);
44 return spi_write(state->spi, &state->data, sizeof(state->data));
45}
46
47static int ad8801_write_raw(struct iio_dev *indio_dev,
48 struct iio_chan_spec const *chan, int val, int val2, long mask)
49{
50 struct ad8801_state *state = iio_priv(indio_dev);
51 int ret;
52
53 switch (mask) {
54 case IIO_CHAN_INFO_RAW:
55 if (val >= 256 || val < 0)
56 return -EINVAL;
57
58 ret = ad8801_spi_write(state, chan->channel, val);
59 if (ret == 0)
60 state->dac_cache[chan->channel] = val;
61 break;
62 default:
63 ret = -EINVAL;
64 }
65
66 return ret;
67}
68
69static int ad8801_read_raw(struct iio_dev *indio_dev,
70 struct iio_chan_spec const *chan, int *val, int *val2, long info)
71{
72 struct ad8801_state *state = iio_priv(indio_dev);
73
74 switch (info) {
75 case IIO_CHAN_INFO_RAW:
76 *val = state->dac_cache[chan->channel];
77 return IIO_VAL_INT;
78 case IIO_CHAN_INFO_SCALE:
79 *val = state->vrefh_mv - state->vrefl_mv;
80 *val2 = 8;
81 return IIO_VAL_FRACTIONAL_LOG2;
82 case IIO_CHAN_INFO_OFFSET:
83 *val = state->vrefl_mv;
84 return IIO_VAL_INT;
85 default:
86 return -EINVAL;
87 }
88
89 return -EINVAL;
90}
91
92static const struct iio_info ad8801_info = {
93 .read_raw = ad8801_read_raw,
94 .write_raw = ad8801_write_raw,
95 .driver_module = THIS_MODULE,
96};
97
98#define AD8801_CHANNEL(chan) { \
99 .type = IIO_VOLTAGE, \
100 .indexed = 1, \
101 .output = 1, \
102 .channel = chan, \
103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
104 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
105 BIT(IIO_CHAN_INFO_OFFSET), \
106}
107
108static const struct iio_chan_spec ad8801_channels[] = {
109 AD8801_CHANNEL(0),
110 AD8801_CHANNEL(1),
111 AD8801_CHANNEL(2),
112 AD8801_CHANNEL(3),
113 AD8801_CHANNEL(4),
114 AD8801_CHANNEL(5),
115 AD8801_CHANNEL(6),
116 AD8801_CHANNEL(7),
117};
118
119static int ad8801_probe(struct spi_device *spi)
120{
121 struct iio_dev *indio_dev;
122 struct ad8801_state *state;
123 const struct spi_device_id *id;
124 int ret;
125
126 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
127 if (indio_dev == NULL)
128 return -ENOMEM;
129
130 state = iio_priv(indio_dev);
131 state->spi = spi;
132 id = spi_get_device_id(spi);
133
134 state->vrefh_reg = devm_regulator_get(&spi->dev, "vrefh");
135 if (IS_ERR(state->vrefh_reg)) {
136 dev_err(&spi->dev, "Vrefh regulator not specified\n");
137 return PTR_ERR(state->vrefh_reg);
138 }
139
140 ret = regulator_enable(state->vrefh_reg);
141 if (ret) {
142 dev_err(&spi->dev, "Failed to enable vrefh regulator: %d\n",
143 ret);
144 return ret;
145 }
146
147 ret = regulator_get_voltage(state->vrefh_reg);
148 if (ret < 0) {
149 dev_err(&spi->dev, "Failed to read vrefh regulator: %d\n",
150 ret);
151 goto error_disable_vrefh_reg;
152 }
153 state->vrefh_mv = ret / 1000;
154
155 if (id->driver_data == ID_AD8803) {
156 state->vrefl_reg = devm_regulator_get(&spi->dev, "vrefl");
157 if (IS_ERR(state->vrefl_reg)) {
158 dev_err(&spi->dev, "Vrefl regulator not specified\n");
159 ret = PTR_ERR(state->vrefl_reg);
160 goto error_disable_vrefh_reg;
161 }
162
163 ret = regulator_enable(state->vrefl_reg);
164 if (ret) {
165 dev_err(&spi->dev, "Failed to enable vrefl regulator: %d\n",
166 ret);
167 goto error_disable_vrefh_reg;
168 }
169
170 ret = regulator_get_voltage(state->vrefl_reg);
171 if (ret < 0) {
172 dev_err(&spi->dev, "Failed to read vrefl regulator: %d\n",
173 ret);
174 goto error_disable_vrefl_reg;
175 }
176 state->vrefl_mv = ret / 1000;
177 } else {
178 state->vrefl_mv = 0;
179 state->vrefl_reg = NULL;
180 }
181
182 spi_set_drvdata(spi, indio_dev);
183 indio_dev->dev.parent = &spi->dev;
184 indio_dev->info = &ad8801_info;
185 indio_dev->modes = INDIO_DIRECT_MODE;
186 indio_dev->channels = ad8801_channels;
187 indio_dev->num_channels = ARRAY_SIZE(ad8801_channels);
188 indio_dev->name = id->name;
189
190 ret = iio_device_register(indio_dev);
191 if (ret) {
192 dev_err(&spi->dev, "Failed to register iio device: %d\n",
193 ret);
194 goto error_disable_vrefl_reg;
195 }
196
197 return 0;
198
199error_disable_vrefl_reg:
200 if (state->vrefl_reg)
201 regulator_disable(state->vrefl_reg);
202error_disable_vrefh_reg:
203 regulator_disable(state->vrefh_reg);
204 return ret;
205}
206
207static int ad8801_remove(struct spi_device *spi)
208{
209 struct iio_dev *indio_dev = spi_get_drvdata(spi);
210 struct ad8801_state *state = iio_priv(indio_dev);
211
212 iio_device_unregister(indio_dev);
213 if (state->vrefl_reg)
214 regulator_disable(state->vrefl_reg);
215 regulator_disable(state->vrefh_reg);
216
217 return 0;
218}
219
220static const struct spi_device_id ad8801_ids[] = {
221 {"ad8801", ID_AD8801},
222 {"ad8803", ID_AD8803},
223 {}
224};
225MODULE_DEVICE_TABLE(spi, ad8801_ids);
226
227static struct spi_driver ad8801_driver = {
228 .driver = {
229 .name = "ad8801",
230 },
231 .probe = ad8801_probe,
232 .remove = ad8801_remove,
233 .id_table = ad8801_ids,
234};
235module_spi_driver(ad8801_driver);
236
237MODULE_AUTHOR("Gwenhael Goavec-Merou <gwenhael.goavec-merou@trabucayre.com>");
238MODULE_DESCRIPTION("Analog Devices AD8801/AD8803 DAC");
239MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/dac/cio-dac.c b/drivers/iio/dac/cio-dac.c
new file mode 100644
index 000000000000..5a743e2a779d
--- /dev/null
+++ b/drivers/iio/dac/cio-dac.c
@@ -0,0 +1,144 @@
1/*
2 * IIO driver for the Measurement Computing CIO-DAC
3 * Copyright (C) 2016 William Breathitt Gray
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * This driver supports the following Measurement Computing devices: CIO-DAC16,
15 * CIO-DAC06, and PC104-DAC06.
16 */
17#include <linux/bitops.h>
18#include <linux/device.h>
19#include <linux/errno.h>
20#include <linux/iio/iio.h>
21#include <linux/iio/types.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/isa.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27
28#define CIO_DAC_NUM_CHAN 16
29
30#define CIO_DAC_CHAN(chan) { \
31 .type = IIO_VOLTAGE, \
32 .channel = chan, \
33 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
34 .indexed = 1, \
35 .output = 1 \
36}
37
38#define CIO_DAC_EXTENT 32
39
40static unsigned int base[max_num_isa_dev(CIO_DAC_EXTENT)];
41static unsigned int num_cio_dac;
42module_param_array(base, uint, &num_cio_dac, 0);
43MODULE_PARM_DESC(base, "Measurement Computing CIO-DAC base addresses");
44
45/**
46 * struct cio_dac_iio - IIO device private data structure
47 * @chan_out_states: channels' output states
48 * @base: base port address of the IIO device
49 */
50struct cio_dac_iio {
51 int chan_out_states[CIO_DAC_NUM_CHAN];
52 unsigned int base;
53};
54
55static int cio_dac_read_raw(struct iio_dev *indio_dev,
56 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
57{
58 struct cio_dac_iio *const priv = iio_priv(indio_dev);
59
60 if (mask != IIO_CHAN_INFO_RAW)
61 return -EINVAL;
62
63 *val = priv->chan_out_states[chan->channel];
64
65 return IIO_VAL_INT;
66}
67
68static int cio_dac_write_raw(struct iio_dev *indio_dev,
69 struct iio_chan_spec const *chan, int val, int val2, long mask)
70{
71 struct cio_dac_iio *const priv = iio_priv(indio_dev);
72 const unsigned int chan_addr_offset = 2 * chan->channel;
73
74 if (mask != IIO_CHAN_INFO_RAW)
75 return -EINVAL;
76
77 /* DAC can only accept up to a 16-bit value */
78 if ((unsigned int)val > 65535)
79 return -EINVAL;
80
81 priv->chan_out_states[chan->channel] = val;
82 outw(val, priv->base + chan_addr_offset);
83
84 return 0;
85}
86
87static const struct iio_info cio_dac_info = {
88 .driver_module = THIS_MODULE,
89 .read_raw = cio_dac_read_raw,
90 .write_raw = cio_dac_write_raw
91};
92
93static const struct iio_chan_spec cio_dac_channels[CIO_DAC_NUM_CHAN] = {
94 CIO_DAC_CHAN(0), CIO_DAC_CHAN(1), CIO_DAC_CHAN(2), CIO_DAC_CHAN(3),
95 CIO_DAC_CHAN(4), CIO_DAC_CHAN(5), CIO_DAC_CHAN(6), CIO_DAC_CHAN(7),
96 CIO_DAC_CHAN(8), CIO_DAC_CHAN(9), CIO_DAC_CHAN(10), CIO_DAC_CHAN(11),
97 CIO_DAC_CHAN(12), CIO_DAC_CHAN(13), CIO_DAC_CHAN(14), CIO_DAC_CHAN(15)
98};
99
100static int cio_dac_probe(struct device *dev, unsigned int id)
101{
102 struct iio_dev *indio_dev;
103 struct cio_dac_iio *priv;
104 unsigned int i;
105
106 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
107 if (!indio_dev)
108 return -ENOMEM;
109
110 if (!devm_request_region(dev, base[id], CIO_DAC_EXTENT,
111 dev_name(dev))) {
112 dev_err(dev, "Unable to request port addresses (0x%X-0x%X)\n",
113 base[id], base[id] + CIO_DAC_EXTENT);
114 return -EBUSY;
115 }
116
117 indio_dev->info = &cio_dac_info;
118 indio_dev->modes = INDIO_DIRECT_MODE;
119 indio_dev->channels = cio_dac_channels;
120 indio_dev->num_channels = CIO_DAC_NUM_CHAN;
121 indio_dev->name = dev_name(dev);
122
123 priv = iio_priv(indio_dev);
124 priv->base = base[id];
125
126 /* initialize DAC outputs to 0V */
127 for (i = 0; i < 32; i += 2)
128 outw(0, base[id] + i);
129
130 return devm_iio_device_register(dev, indio_dev);
131}
132
133static struct isa_driver cio_dac_driver = {
134 .probe = cio_dac_probe,
135 .driver = {
136 .name = "cio-dac"
137 }
138};
139
140module_isa_driver(cio_dac_driver, num_cio_dac);
141
142MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
143MODULE_DESCRIPTION("Measurement Computing CIO-DAC IIO driver");
144MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/gyro/ssp_gyro_sensor.c b/drivers/iio/gyro/ssp_gyro_sensor.c
index 0a8afdd21728..1f25f406c545 100644
--- a/drivers/iio/gyro/ssp_gyro_sensor.c
+++ b/drivers/iio/gyro/ssp_gyro_sensor.c
@@ -74,7 +74,7 @@ static int ssp_gyro_write_raw(struct iio_dev *indio_dev,
74 return -EINVAL; 74 return -EINVAL;
75} 75}
76 76
77static struct iio_info ssp_gyro_iio_info = { 77static const struct iio_info ssp_gyro_iio_info = {
78 .read_raw = &ssp_gyro_read_raw, 78 .read_raw = &ssp_gyro_read_raw,
79 .write_raw = &ssp_gyro_write_raw, 79 .write_raw = &ssp_gyro_write_raw,
80}; 80};
diff --git a/drivers/iio/humidity/Kconfig b/drivers/iio/humidity/Kconfig
index d04124345992..b17e2e2bd4f5 100644
--- a/drivers/iio/humidity/Kconfig
+++ b/drivers/iio/humidity/Kconfig
@@ -28,11 +28,11 @@ config HDC100X
28 tristate "TI HDC100x relative humidity and temperature sensor" 28 tristate "TI HDC100x relative humidity and temperature sensor"
29 depends on I2C 29 depends on I2C
30 help 30 help
31 Say yes here to build support for the TI HDC100x series of 31 Say yes here to build support for the Texas Instruments
32 relative humidity and temperature sensors. 32 HDC1000 and HDC1008 relative humidity and temperature sensors.
33 33
34 To compile this driver as a module, choose M here: the module 34 To compile this driver as a module, choose M here: the module
35 will be called hdc100x. 35 will be called hdc100x.
36 36
37config HTU21 37config HTU21
38 tristate "Measurement Specialties HTU21 humidity & temperature sensor" 38 tristate "Measurement Specialties HTU21 humidity & temperature sensor"
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index d2b889918c3e..fc340ed3dca1 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1308,7 +1308,7 @@ static void devm_iio_device_release(struct device *dev, void *res)
1308 iio_device_free(*(struct iio_dev **)res); 1308 iio_device_free(*(struct iio_dev **)res);
1309} 1309}
1310 1310
1311static int devm_iio_device_match(struct device *dev, void *res, void *data) 1311int devm_iio_device_match(struct device *dev, void *res, void *data)
1312{ 1312{
1313 struct iio_dev **r = res; 1313 struct iio_dev **r = res;
1314 if (!r || !*r) { 1314 if (!r || !*r) {
@@ -1317,6 +1317,7 @@ static int devm_iio_device_match(struct device *dev, void *res, void *data)
1317 } 1317 }
1318 return *r == data; 1318 return *r == data;
1319} 1319}
1320EXPORT_SYMBOL_GPL(devm_iio_device_match);
1320 1321
1321/** 1322/**
1322 * devm_iio_device_alloc - Resource-managed iio_device_alloc() 1323 * devm_iio_device_alloc - Resource-managed iio_device_alloc()
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index 0ebfc923a997..90fac8ec63c9 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -57,6 +57,11 @@ bool iio_event_enabled(const struct iio_event_interface *ev_int)
57 * 57 *
58 * Note: The caller must make sure that this function is not running 58 * Note: The caller must make sure that this function is not running
59 * concurrently for the same indio_dev more than once. 59 * concurrently for the same indio_dev more than once.
60 *
61 * This function may be safely used as soon as a valid reference to iio_dev has
62 * been obtained via iio_device_alloc(), but any events that are submitted
63 * before iio_device_register() has successfully completed will be silently
64 * discarded.
60 **/ 65 **/
61int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp) 66int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
62{ 67{
@@ -64,6 +69,9 @@ int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
64 struct iio_event_data ev; 69 struct iio_event_data ev;
65 int copied; 70 int copied;
66 71
72 if (!ev_int)
73 return 0;
74
67 /* Does anyone care? */ 75 /* Does anyone care? */
68 if (iio_event_enabled(ev_int)) { 76 if (iio_event_enabled(ev_int)) {
69 77
diff --git a/drivers/iio/industrialio-trigger.c b/drivers/iio/industrialio-trigger.c
index 7ad82fdd3e5b..e1e104845e38 100644
--- a/drivers/iio/industrialio-trigger.c
+++ b/drivers/iio/industrialio-trigger.c
@@ -119,6 +119,22 @@ void iio_trigger_unregister(struct iio_trigger *trig_info)
119} 119}
120EXPORT_SYMBOL(iio_trigger_unregister); 120EXPORT_SYMBOL(iio_trigger_unregister);
121 121
122int iio_trigger_set_immutable(struct iio_dev *indio_dev, struct iio_trigger *trig)
123{
124 if (!indio_dev || !trig)
125 return -EINVAL;
126
127 mutex_lock(&indio_dev->mlock);
128 WARN_ON(indio_dev->trig_readonly);
129
130 indio_dev->trig = iio_trigger_get(trig);
131 indio_dev->trig_readonly = true;
132 mutex_unlock(&indio_dev->mlock);
133
134 return 0;
135}
136EXPORT_SYMBOL(iio_trigger_set_immutable);
137
122/* Search for trigger by name, assuming iio_trigger_list_lock held */ 138/* Search for trigger by name, assuming iio_trigger_list_lock held */
123static struct iio_trigger *__iio_trigger_find_by_name(const char *name) 139static struct iio_trigger *__iio_trigger_find_by_name(const char *name)
124{ 140{
@@ -255,6 +271,14 @@ static int iio_trigger_attach_poll_func(struct iio_trigger *trig,
255 goto out_free_irq; 271 goto out_free_irq;
256 } 272 }
257 273
274 /*
275 * Check if we just registered to our own trigger: we determine that
276 * this is the case if the IIO device and the trigger device share the
277 * same parent device.
278 */
279 if (pf->indio_dev->dev.parent == trig->dev.parent)
280 trig->attached_own_device = true;
281
258 return ret; 282 return ret;
259 283
260out_free_irq: 284out_free_irq:
@@ -279,6 +303,8 @@ static int iio_trigger_detach_poll_func(struct iio_trigger *trig,
279 if (ret) 303 if (ret)
280 return ret; 304 return ret;
281 } 305 }
306 if (pf->indio_dev->dev.parent == trig->dev.parent)
307 trig->attached_own_device = false;
282 iio_trigger_put_irq(trig, pf->irq); 308 iio_trigger_put_irq(trig, pf->irq);
283 free_irq(pf->irq, pf); 309 free_irq(pf->irq, pf);
284 module_put(pf->indio_dev->info->driver_module); 310 module_put(pf->indio_dev->info->driver_module);
@@ -384,6 +410,10 @@ static ssize_t iio_trigger_write_current(struct device *dev,
384 mutex_unlock(&indio_dev->mlock); 410 mutex_unlock(&indio_dev->mlock);
385 return -EBUSY; 411 return -EBUSY;
386 } 412 }
413 if (indio_dev->trig_readonly) {
414 mutex_unlock(&indio_dev->mlock);
415 return -EPERM;
416 }
387 mutex_unlock(&indio_dev->mlock); 417 mutex_unlock(&indio_dev->mlock);
388 418
389 trig = iio_trigger_find_by_name(buf, len); 419 trig = iio_trigger_find_by_name(buf, len);
@@ -622,6 +652,71 @@ void devm_iio_trigger_free(struct device *dev, struct iio_trigger *iio_trig)
622} 652}
623EXPORT_SYMBOL_GPL(devm_iio_trigger_free); 653EXPORT_SYMBOL_GPL(devm_iio_trigger_free);
624 654
655static void devm_iio_trigger_unreg(struct device *dev, void *res)
656{
657 iio_trigger_unregister(*(struct iio_trigger **)res);
658}
659
660/**
661 * devm_iio_trigger_register - Resource-managed iio_trigger_register()
662 * @dev: device this trigger was allocated for
663 * @trig_info: trigger to register
664 *
665 * Managed iio_trigger_register(). The IIO trigger registered with this
666 * function is automatically unregistered on driver detach. This function
667 * calls iio_trigger_register() internally. Refer to that function for more
668 * information.
669 *
670 * If an iio_trigger registered with this function needs to be unregistered
671 * separately, devm_iio_trigger_unregister() must be used.
672 *
673 * RETURNS:
674 * 0 on success, negative error number on failure.
675 */
676int devm_iio_trigger_register(struct device *dev, struct iio_trigger *trig_info)
677{
678 struct iio_trigger **ptr;
679 int ret;
680
681 ptr = devres_alloc(devm_iio_trigger_unreg, sizeof(*ptr), GFP_KERNEL);
682 if (!ptr)
683 return -ENOMEM;
684
685 *ptr = trig_info;
686 ret = iio_trigger_register(trig_info);
687 if (!ret)
688 devres_add(dev, ptr);
689 else
690 devres_free(ptr);
691
692 return ret;
693}
694EXPORT_SYMBOL_GPL(devm_iio_trigger_register);
695
696/**
697 * devm_iio_trigger_unregister - Resource-managed iio_trigger_unregister()
698 * @dev: device this iio_trigger belongs to
699 * @trig_info: the trigger associated with the device
700 *
701 * Unregister trigger registered with devm_iio_trigger_register().
702 */
703void devm_iio_trigger_unregister(struct device *dev,
704 struct iio_trigger *trig_info)
705{
706 int rc;
707
708 rc = devres_release(dev, devm_iio_trigger_unreg, devm_iio_trigger_match,
709 trig_info);
710 WARN_ON(rc);
711}
712EXPORT_SYMBOL_GPL(devm_iio_trigger_unregister);
713
714bool iio_trigger_using_own(struct iio_dev *indio_dev)
715{
716 return indio_dev->trig->attached_own_device;
717}
718EXPORT_SYMBOL(iio_trigger_using_own);
719
625void iio_device_register_trigger_consumer(struct iio_dev *indio_dev) 720void iio_device_register_trigger_consumer(struct iio_dev *indio_dev)
626{ 721{
627 indio_dev->groups[indio_dev->groupcounter++] = 722 indio_dev->groups[indio_dev->groupcounter++] =
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 3574945183fe..ba2e64d7ee58 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -267,6 +267,19 @@ config PA12203001
267 This driver can also be built as a module. If so, the module 267 This driver can also be built as a module. If so, the module
268 will be called pa12203001. 268 will be called pa12203001.
269 269
270config SI1145
271 tristate "SI1132 and SI1141/2/3/5/6/7 combined ALS, UV index and proximity sensor"
272 depends on I2C
273 select IIO_BUFFER
274 select IIO_TRIGGERED_BUFFER
275 help
276 Say Y here if you want to build a driver for the Silicon Labs SI1132 or
277 SI1141/2/3/5/6/7 combined ambient light, UV index and proximity sensor
278 chips.
279
280 To compile this driver as a module, choose M here: the module will be
281 called si1145.
282
270config STK3310 283config STK3310
271 tristate "STK3310 ALS and proximity sensor" 284 tristate "STK3310 ALS and proximity sensor"
272 depends on I2C 285 depends on I2C
@@ -334,11 +347,11 @@ config US5182D
334 will be called us5182d. 347 will be called us5182d.
335 348
336config VCNL4000 349config VCNL4000
337 tristate "VCNL4000 combined ALS and proximity sensor" 350 tristate "VCNL4000/4010/4020 combined ALS and proximity sensor"
338 depends on I2C 351 depends on I2C
339 help 352 help
340 Say Y here if you want to build a driver for the Vishay VCNL4000 353 Say Y here if you want to build a driver for the Vishay VCNL4000,
341 combined ambient light and proximity sensor. 354 VCNL4010, VCNL4020 combined ambient light and proximity sensor.
342 355
343 To compile this driver as a module, choose M here: the 356 To compile this driver as a module, choose M here: the
344 module will be called vcnl4000. 357 module will be called vcnl4000.
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 6f2a3c62de27..c5768df87a17 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -26,6 +26,7 @@ obj-$(CONFIG_OPT3001) += opt3001.o
26obj-$(CONFIG_PA12203001) += pa12203001.o 26obj-$(CONFIG_PA12203001) += pa12203001.o
27obj-$(CONFIG_RPR0521) += rpr0521.o 27obj-$(CONFIG_RPR0521) += rpr0521.o
28obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o 28obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o
29obj-$(CONFIG_SI1145) += si1145.o
29obj-$(CONFIG_STK3310) += stk3310.o 30obj-$(CONFIG_STK3310) += stk3310.o
30obj-$(CONFIG_TCS3414) += tcs3414.o 31obj-$(CONFIG_TCS3414) += tcs3414.o
31obj-$(CONFIG_TCS3472) += tcs3472.o 32obj-$(CONFIG_TCS3472) += tcs3472.o
diff --git a/drivers/iio/light/si1145.c b/drivers/iio/light/si1145.c
new file mode 100644
index 000000000000..096034c126a4
--- /dev/null
+++ b/drivers/iio/light/si1145.c
@@ -0,0 +1,1404 @@
1/*
2 * si1145.c - Support for Silabs SI1132 and SI1141/2/3/5/6/7 combined ambient
3 * light, UV index and proximity sensors
4 *
5 * Copyright 2014-16 Peter Meerwald-Stadler <pmeerw@pmeerw.net>
6 * Copyright 2016 Crestez Dan Leonard <leonard.crestez@intel.com>
7 *
8 * This file is subject to the terms and conditions of version 2 of
9 * the GNU General Public License. See the file COPYING in the main
10 * directory of this archive for more details.
11 *
12 * SI1132 (7-bit I2C slave address 0x60)
13 * SI1141/2/3 (7-bit I2C slave address 0x5a)
14 * SI1145/6/6 (7-bit I2C slave address 0x60)
15 */
16
17#include <linux/module.h>
18#include <linux/i2c.h>
19#include <linux/err.h>
20#include <linux/slab.h>
21#include <linux/delay.h>
22#include <linux/irq.h>
23#include <linux/gpio.h>
24
25#include <linux/iio/iio.h>
26#include <linux/iio/sysfs.h>
27#include <linux/iio/trigger.h>
28#include <linux/iio/trigger_consumer.h>
29#include <linux/iio/triggered_buffer.h>
30#include <linux/iio/buffer.h>
31#include <linux/util_macros.h>
32
33#define SI1145_REG_PART_ID 0x00
34#define SI1145_REG_REV_ID 0x01
35#define SI1145_REG_SEQ_ID 0x02
36#define SI1145_REG_INT_CFG 0x03
37#define SI1145_REG_IRQ_ENABLE 0x04
38#define SI1145_REG_IRQ_MODE 0x05
39#define SI1145_REG_HW_KEY 0x07
40#define SI1145_REG_MEAS_RATE 0x08
41#define SI1145_REG_PS_LED21 0x0f
42#define SI1145_REG_PS_LED3 0x10
43#define SI1145_REG_UCOEF1 0x13
44#define SI1145_REG_UCOEF2 0x14
45#define SI1145_REG_UCOEF3 0x15
46#define SI1145_REG_UCOEF4 0x16
47#define SI1145_REG_PARAM_WR 0x17
48#define SI1145_REG_COMMAND 0x18
49#define SI1145_REG_RESPONSE 0x20
50#define SI1145_REG_IRQ_STATUS 0x21
51#define SI1145_REG_ALSVIS_DATA 0x22
52#define SI1145_REG_ALSIR_DATA 0x24
53#define SI1145_REG_PS1_DATA 0x26
54#define SI1145_REG_PS2_DATA 0x28
55#define SI1145_REG_PS3_DATA 0x2a
56#define SI1145_REG_AUX_DATA 0x2c
57#define SI1145_REG_PARAM_RD 0x2e
58#define SI1145_REG_CHIP_STAT 0x30
59
60#define SI1145_UCOEF1_DEFAULT 0x7b
61#define SI1145_UCOEF2_DEFAULT 0x6b
62#define SI1145_UCOEF3_DEFAULT 0x01
63#define SI1145_UCOEF4_DEFAULT 0x00
64
65/* Helper to figure out PS_LED register / shift per channel */
66#define SI1145_PS_LED_REG(ch) \
67 (((ch) == 2) ? SI1145_REG_PS_LED3 : SI1145_REG_PS_LED21)
68#define SI1145_PS_LED_SHIFT(ch) \
69 (((ch) == 1) ? 4 : 0)
70
71/* Parameter offsets */
72#define SI1145_PARAM_CHLIST 0x01
73#define SI1145_PARAM_PSLED12_SELECT 0x02
74#define SI1145_PARAM_PSLED3_SELECT 0x03
75#define SI1145_PARAM_PS_ENCODING 0x05
76#define SI1145_PARAM_ALS_ENCODING 0x06
77#define SI1145_PARAM_PS1_ADC_MUX 0x07
78#define SI1145_PARAM_PS2_ADC_MUX 0x08
79#define SI1145_PARAM_PS3_ADC_MUX 0x09
80#define SI1145_PARAM_PS_ADC_COUNTER 0x0a
81#define SI1145_PARAM_PS_ADC_GAIN 0x0b
82#define SI1145_PARAM_PS_ADC_MISC 0x0c
83#define SI1145_PARAM_ALS_ADC_MUX 0x0d
84#define SI1145_PARAM_ALSIR_ADC_MUX 0x0e
85#define SI1145_PARAM_AUX_ADC_MUX 0x0f
86#define SI1145_PARAM_ALSVIS_ADC_COUNTER 0x10
87#define SI1145_PARAM_ALSVIS_ADC_GAIN 0x11
88#define SI1145_PARAM_ALSVIS_ADC_MISC 0x12
89#define SI1145_PARAM_LED_RECOVERY 0x1c
90#define SI1145_PARAM_ALSIR_ADC_COUNTER 0x1d
91#define SI1145_PARAM_ALSIR_ADC_GAIN 0x1e
92#define SI1145_PARAM_ALSIR_ADC_MISC 0x1f
93#define SI1145_PARAM_ADC_OFFSET 0x1a
94
95/* Channel enable masks for CHLIST parameter */
96#define SI1145_CHLIST_EN_PS1 BIT(0)
97#define SI1145_CHLIST_EN_PS2 BIT(1)
98#define SI1145_CHLIST_EN_PS3 BIT(2)
99#define SI1145_CHLIST_EN_ALSVIS BIT(4)
100#define SI1145_CHLIST_EN_ALSIR BIT(5)
101#define SI1145_CHLIST_EN_AUX BIT(6)
102#define SI1145_CHLIST_EN_UV BIT(7)
103
104/* Proximity measurement mode for ADC_MISC parameter */
105#define SI1145_PS_ADC_MODE_NORMAL BIT(2)
106/* Signal range mask for ADC_MISC parameter */
107#define SI1145_ADC_MISC_RANGE BIT(5)
108
109/* Commands for REG_COMMAND */
110#define SI1145_CMD_NOP 0x00
111#define SI1145_CMD_RESET 0x01
112#define SI1145_CMD_PS_FORCE 0x05
113#define SI1145_CMD_ALS_FORCE 0x06
114#define SI1145_CMD_PSALS_FORCE 0x07
115#define SI1145_CMD_PS_PAUSE 0x09
116#define SI1145_CMD_ALS_PAUSE 0x0a
117#define SI1145_CMD_PSALS_PAUSE 0x0b
118#define SI1145_CMD_PS_AUTO 0x0d
119#define SI1145_CMD_ALS_AUTO 0x0e
120#define SI1145_CMD_PSALS_AUTO 0x0f
121#define SI1145_CMD_PARAM_QUERY 0x80
122#define SI1145_CMD_PARAM_SET 0xa0
123
124#define SI1145_RSP_INVALID_SETTING 0x80
125#define SI1145_RSP_COUNTER_MASK 0x0F
126
127/* Minimum sleep after each command to ensure it's received */
128#define SI1145_COMMAND_MINSLEEP_MS 5
129/* Return -ETIMEDOUT after this long */
130#define SI1145_COMMAND_TIMEOUT_MS 25
131
132/* Interrupt configuration masks for INT_CFG register */
133#define SI1145_INT_CFG_OE BIT(0) /* enable interrupt */
134#define SI1145_INT_CFG_MODE BIT(1) /* auto reset interrupt pin */
135
136/* Interrupt enable masks for IRQ_ENABLE register */
137#define SI1145_MASK_ALL_IE (BIT(4) | BIT(3) | BIT(2) | BIT(0))
138
139#define SI1145_MUX_TEMP 0x65
140#define SI1145_MUX_VDD 0x75
141
142/* Proximity LED current; see Table 2 in datasheet */
143#define SI1145_LED_CURRENT_45mA 0x04
144
145enum {
146 SI1132,
147 SI1141,
148 SI1142,
149 SI1143,
150 SI1145,
151 SI1146,
152 SI1147,
153};
154
155struct si1145_part_info {
156 u8 part;
157 const struct iio_info *iio_info;
158 const struct iio_chan_spec *channels;
159 unsigned int num_channels;
160 unsigned int num_leds;
161 bool uncompressed_meas_rate;
162};
163
164/**
165 * struct si1145_data - si1145 chip state data
166 * @client: I2C client
167 * @lock: mutex to protect shared state.
168 * @cmdlock: Low-level mutex to protect command execution only
169 * @rsp_seq: Next expected response number or -1 if counter reset required
170 * @scan_mask: Saved scan mask to avoid duplicate set_chlist
171 * @autonomous: If automatic measurements are active (for buffer support)
172 * @part_info: Part information
173 * @trig: Pointer to iio trigger
174 * @meas_rate: Value of MEAS_RATE register. Only set in HW in auto mode
175 */
176struct si1145_data {
177 struct i2c_client *client;
178 struct mutex lock;
179 struct mutex cmdlock;
180 int rsp_seq;
181 const struct si1145_part_info *part_info;
182 unsigned long scan_mask;
183 bool autonomous;
184 struct iio_trigger *trig;
185 int meas_rate;
186};
187
188/**
189 * __si1145_command_reset() - Send CMD_NOP and wait for response 0
190 *
191 * Does not modify data->rsp_seq
192 *
193 * Return: 0 on success and -errno on error.
194 */
195static int __si1145_command_reset(struct si1145_data *data)
196{
197 struct device *dev = &data->client->dev;
198 unsigned long stop_jiffies;
199 int ret;
200
201 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND,
202 SI1145_CMD_NOP);
203 if (ret < 0)
204 return ret;
205 msleep(SI1145_COMMAND_MINSLEEP_MS);
206
207 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
208 while (true) {
209 ret = i2c_smbus_read_byte_data(data->client,
210 SI1145_REG_RESPONSE);
211 if (ret <= 0)
212 return ret;
213 if (time_after(jiffies, stop_jiffies)) {
214 dev_warn(dev, "timeout on reset\n");
215 return -ETIMEDOUT;
216 }
217 msleep(SI1145_COMMAND_MINSLEEP_MS);
218 continue;
219 }
220}
221
222/**
223 * si1145_command() - Execute a command and poll the response register
224 *
225 * All conversion overflows are reported as -EOVERFLOW
226 * INVALID_SETTING is reported as -EINVAL
227 * Timeouts are reported as -ETIMEDOUT
228 *
229 * Return: 0 on success or -errno on failure
230 */
231static int si1145_command(struct si1145_data *data, u8 cmd)
232{
233 struct device *dev = &data->client->dev;
234 unsigned long stop_jiffies;
235 int ret;
236
237 mutex_lock(&data->cmdlock);
238
239 if (data->rsp_seq < 0) {
240 ret = __si1145_command_reset(data);
241 if (ret < 0) {
242 dev_err(dev, "failed to reset command counter, ret=%d\n",
243 ret);
244 goto out;
245 }
246 data->rsp_seq = 0;
247 }
248
249 ret = i2c_smbus_write_byte_data(data->client, SI1145_REG_COMMAND, cmd);
250 if (ret) {
251 dev_warn(dev, "failed to write command, ret=%d\n", ret);
252 goto out;
253 }
254 /* Sleep a little to ensure the command is received */
255 msleep(SI1145_COMMAND_MINSLEEP_MS);
256
257 stop_jiffies = jiffies + SI1145_COMMAND_TIMEOUT_MS * HZ / 1000;
258 while (true) {
259 ret = i2c_smbus_read_byte_data(data->client,
260 SI1145_REG_RESPONSE);
261 if (ret < 0) {
262 dev_warn(dev, "failed to read response, ret=%d\n", ret);
263 break;
264 }
265
266 if ((ret & ~SI1145_RSP_COUNTER_MASK) == 0) {
267 if (ret == data->rsp_seq) {
268 if (time_after(jiffies, stop_jiffies)) {
269 dev_warn(dev, "timeout on command %#02hhx\n",
270 cmd);
271 ret = -ETIMEDOUT;
272 break;
273 }
274 msleep(SI1145_COMMAND_MINSLEEP_MS);
275 continue;
276 }
277 if (ret == ((data->rsp_seq + 1) &
278 SI1145_RSP_COUNTER_MASK)) {
279 data->rsp_seq = ret;
280 ret = 0;
281 break;
282 }
283 dev_warn(dev, "unexpected response counter %d instead of %d\n",
284 ret, (data->rsp_seq + 1) &
285 SI1145_RSP_COUNTER_MASK);
286 ret = -EIO;
287 } else {
288 if (ret == SI1145_RSP_INVALID_SETTING) {
289 dev_warn(dev, "INVALID_SETTING error on command %#02hhx\n",
290 cmd);
291 ret = -EINVAL;
292 } else {
293 /* All overflows are treated identically */
294 dev_dbg(dev, "overflow, ret=%d, cmd=%#02hhx\n",
295 ret, cmd);
296 ret = -EOVERFLOW;
297 }
298 }
299
300 /* Force a counter reset next time */
301 data->rsp_seq = -1;
302 break;
303 }
304
305out:
306 mutex_unlock(&data->cmdlock);
307
308 return ret;
309}
310
311static int si1145_param_update(struct si1145_data *data, u8 op, u8 param,
312 u8 value)
313{
314 int ret;
315
316 ret = i2c_smbus_write_byte_data(data->client,
317 SI1145_REG_PARAM_WR, value);
318 if (ret < 0)
319 return ret;
320
321 return si1145_command(data, op | (param & 0x1F));
322}
323
324static int si1145_param_set(struct si1145_data *data, u8 param, u8 value)
325{
326 return si1145_param_update(data, SI1145_CMD_PARAM_SET, param, value);
327}
328
329/* Set param. Returns negative errno or current value */
330static int si1145_param_query(struct si1145_data *data, u8 param)
331{
332 int ret;
333
334 ret = si1145_command(data, SI1145_CMD_PARAM_QUERY | (param & 0x1F));
335 if (ret < 0)
336 return ret;
337
338 return i2c_smbus_read_byte_data(data->client, SI1145_REG_PARAM_RD);
339}
340
341/* Expand 8 bit compressed value to 16 bit, see Silabs AN498 */
342static u16 si1145_uncompress(u8 x)
343{
344 u16 result = 0;
345 u8 exponent = 0;
346
347 if (x < 8)
348 return 0;
349
350 exponent = (x & 0xf0) >> 4;
351 result = 0x10 | (x & 0x0f);
352
353 if (exponent >= 4)
354 return result << (exponent - 4);
355 return result >> (4 - exponent);
356}
357
358/* Compress 16 bit value to 8 bit, see Silabs AN498 */
359static u8 si1145_compress(u16 x)
360{
361 u32 exponent = 0;
362 u32 significand = 0;
363 u32 tmp = x;
364
365 if (x == 0x0000)
366 return 0x00;
367 if (x == 0x0001)
368 return 0x08;
369
370 while (1) {
371 tmp >>= 1;
372 exponent += 1;
373 if (tmp == 1)
374 break;
375 }
376
377 if (exponent < 5) {
378 significand = x << (4 - exponent);
379 return (exponent << 4) | (significand & 0xF);
380 }
381
382 significand = x >> (exponent - 5);
383 if (significand & 1) {
384 significand += 2;
385 if (significand & 0x0040) {
386 exponent += 1;
387 significand >>= 1;
388 }
389 }
390
391 return (exponent << 4) | ((significand >> 1) & 0xF);
392}
393
394/* Write meas_rate in hardware */
395static int si1145_set_meas_rate(struct si1145_data *data, int interval)
396{
397 if (data->part_info->uncompressed_meas_rate)
398 return i2c_smbus_write_word_data(data->client,
399 SI1145_REG_MEAS_RATE, interval);
400 else
401 return i2c_smbus_write_byte_data(data->client,
402 SI1145_REG_MEAS_RATE, interval);
403}
404
405static int si1145_read_samp_freq(struct si1145_data *data, int *val, int *val2)
406{
407 *val = 32000;
408 if (data->part_info->uncompressed_meas_rate)
409 *val2 = data->meas_rate;
410 else
411 *val2 = si1145_uncompress(data->meas_rate);
412 return IIO_VAL_FRACTIONAL;
413}
414
415/* Set the samp freq in driver private data */
416static int si1145_store_samp_freq(struct si1145_data *data, int val)
417{
418 int ret = 0;
419 int meas_rate;
420
421 if (val <= 0 || val > 32000)
422 return -ERANGE;
423 meas_rate = 32000 / val;
424
425 mutex_lock(&data->lock);
426 if (data->autonomous) {
427 ret = si1145_set_meas_rate(data, meas_rate);
428 if (ret)
429 goto out;
430 }
431 if (data->part_info->uncompressed_meas_rate)
432 data->meas_rate = meas_rate;
433 else
434 data->meas_rate = si1145_compress(meas_rate);
435
436out:
437 mutex_unlock(&data->lock);
438
439 return ret;
440}
441
442static irqreturn_t si1145_trigger_handler(int irq, void *private)
443{
444 struct iio_poll_func *pf = private;
445 struct iio_dev *indio_dev = pf->indio_dev;
446 struct si1145_data *data = iio_priv(indio_dev);
447 /*
448 * Maximum buffer size:
449 * 6*2 bytes channels data + 4 bytes alignment +
450 * 8 bytes timestamp
451 */
452 u8 buffer[24];
453 int i, j = 0;
454 int ret;
455 u8 irq_status = 0;
456
457 if (!data->autonomous) {
458 ret = si1145_command(data, SI1145_CMD_PSALS_FORCE);
459 if (ret < 0 && ret != -EOVERFLOW)
460 goto done;
461 } else {
462 irq_status = ret = i2c_smbus_read_byte_data(data->client,
463 SI1145_REG_IRQ_STATUS);
464 if (ret < 0)
465 goto done;
466 if (!(irq_status & SI1145_MASK_ALL_IE))
467 goto done;
468 }
469
470 for_each_set_bit(i, indio_dev->active_scan_mask,
471 indio_dev->masklength) {
472 int run = 1;
473
474 while (i + run < indio_dev->masklength) {
475 if (!test_bit(i + run, indio_dev->active_scan_mask))
476 break;
477 if (indio_dev->channels[i + run].address !=
478 indio_dev->channels[i].address + 2 * run)
479 break;
480 run++;
481 }
482
483 ret = i2c_smbus_read_i2c_block_data_or_emulated(
484 data->client, indio_dev->channels[i].address,
485 sizeof(u16) * run, &buffer[j]);
486 if (ret < 0)
487 goto done;
488 j += run * sizeof(u16);
489 i += run - 1;
490 }
491
492 if (data->autonomous) {
493 ret = i2c_smbus_write_byte_data(data->client,
494 SI1145_REG_IRQ_STATUS,
495 irq_status & SI1145_MASK_ALL_IE);
496 if (ret < 0)
497 goto done;
498 }
499
500 iio_push_to_buffers_with_timestamp(indio_dev, buffer,
501 iio_get_time_ns(indio_dev));
502
503done:
504 iio_trigger_notify_done(indio_dev->trig);
505 return IRQ_HANDLED;
506}
507
508static int si1145_set_chlist(struct iio_dev *indio_dev, unsigned long scan_mask)
509{
510 struct si1145_data *data = iio_priv(indio_dev);
511 u8 reg = 0, mux;
512 int ret;
513 int i;
514
515 /* channel list already set, no need to reprogram */
516 if (data->scan_mask == scan_mask)
517 return 0;
518
519 for_each_set_bit(i, &scan_mask, indio_dev->masklength) {
520 switch (indio_dev->channels[i].address) {
521 case SI1145_REG_ALSVIS_DATA:
522 reg |= SI1145_CHLIST_EN_ALSVIS;
523 break;
524 case SI1145_REG_ALSIR_DATA:
525 reg |= SI1145_CHLIST_EN_ALSIR;
526 break;
527 case SI1145_REG_PS1_DATA:
528 reg |= SI1145_CHLIST_EN_PS1;
529 break;
530 case SI1145_REG_PS2_DATA:
531 reg |= SI1145_CHLIST_EN_PS2;
532 break;
533 case SI1145_REG_PS3_DATA:
534 reg |= SI1145_CHLIST_EN_PS3;
535 break;
536 case SI1145_REG_AUX_DATA:
537 switch (indio_dev->channels[i].type) {
538 case IIO_UVINDEX:
539 reg |= SI1145_CHLIST_EN_UV;
540 break;
541 default:
542 reg |= SI1145_CHLIST_EN_AUX;
543 if (indio_dev->channels[i].type == IIO_TEMP)
544 mux = SI1145_MUX_TEMP;
545 else
546 mux = SI1145_MUX_VDD;
547 ret = si1145_param_set(data,
548 SI1145_PARAM_AUX_ADC_MUX, mux);
549 if (ret < 0)
550 return ret;
551
552 break;
553 }
554 }
555 }
556
557 data->scan_mask = scan_mask;
558 ret = si1145_param_set(data, SI1145_PARAM_CHLIST, reg);
559
560 return ret < 0 ? ret : 0;
561}
562
563static int si1145_measure(struct iio_dev *indio_dev,
564 struct iio_chan_spec const *chan)
565{
566 struct si1145_data *data = iio_priv(indio_dev);
567 u8 cmd;
568 int ret;
569
570 ret = si1145_set_chlist(indio_dev, BIT(chan->scan_index));
571 if (ret < 0)
572 return ret;
573
574 cmd = (chan->type == IIO_PROXIMITY) ? SI1145_CMD_PS_FORCE :
575 SI1145_CMD_ALS_FORCE;
576 ret = si1145_command(data, cmd);
577 if (ret < 0 && ret != -EOVERFLOW)
578 return ret;
579
580 return i2c_smbus_read_word_data(data->client, chan->address);
581}
582
583/*
584 * Conversion between iio scale and ADC_GAIN values
585 * These could be further adjusted but proximity/intensity are dimensionless
586 */
587static const int si1145_proximity_scale_available[] = {
588 128, 64, 32, 16, 8, 4};
589static const int si1145_intensity_scale_available[] = {
590 128, 64, 32, 16, 8, 4, 2, 1};
591static IIO_CONST_ATTR(in_proximity_scale_available,
592 "128 64 32 16 8 4");
593static IIO_CONST_ATTR(in_intensity_scale_available,
594 "128 64 32 16 8 4 2 1");
595static IIO_CONST_ATTR(in_intensity_ir_scale_available,
596 "128 64 32 16 8 4 2 1");
597
598static int si1145_scale_from_adcgain(int regval)
599{
600 return 128 >> regval;
601}
602
603static int si1145_proximity_adcgain_from_scale(int val, int val2)
604{
605 val = find_closest_descending(val, si1145_proximity_scale_available,
606 ARRAY_SIZE(si1145_proximity_scale_available));
607 if (val < 0 || val > 5 || val2 != 0)
608 return -EINVAL;
609
610 return val;
611}
612
613static int si1145_intensity_adcgain_from_scale(int val, int val2)
614{
615 val = find_closest_descending(val, si1145_intensity_scale_available,
616 ARRAY_SIZE(si1145_intensity_scale_available));
617 if (val < 0 || val > 7 || val2 != 0)
618 return -EINVAL;
619
620 return val;
621}
622
623static int si1145_read_raw(struct iio_dev *indio_dev,
624 struct iio_chan_spec const *chan,
625 int *val, int *val2, long mask)
626{
627 struct si1145_data *data = iio_priv(indio_dev);
628 int ret;
629 u8 reg;
630
631 switch (mask) {
632 case IIO_CHAN_INFO_RAW:
633 switch (chan->type) {
634 case IIO_INTENSITY:
635 case IIO_PROXIMITY:
636 case IIO_VOLTAGE:
637 case IIO_TEMP:
638 case IIO_UVINDEX:
639 ret = iio_device_claim_direct_mode(indio_dev);
640 if (ret)
641 return ret;
642 ret = si1145_measure(indio_dev, chan);
643 iio_device_release_direct_mode(indio_dev);
644
645 if (ret < 0)
646 return ret;
647
648 *val = ret;
649
650 return IIO_VAL_INT;
651 case IIO_CURRENT:
652 ret = i2c_smbus_read_byte_data(data->client,
653 SI1145_PS_LED_REG(chan->channel));
654 if (ret < 0)
655 return ret;
656
657 *val = (ret >> SI1145_PS_LED_SHIFT(chan->channel))
658 & 0x0f;
659
660 return IIO_VAL_INT;
661 default:
662 return -EINVAL;
663 }
664 case IIO_CHAN_INFO_SCALE:
665 switch (chan->type) {
666 case IIO_PROXIMITY:
667 reg = SI1145_PARAM_PS_ADC_GAIN;
668 break;
669 case IIO_INTENSITY:
670 if (chan->channel2 == IIO_MOD_LIGHT_IR)
671 reg = SI1145_PARAM_ALSIR_ADC_GAIN;
672 else
673 reg = SI1145_PARAM_ALSVIS_ADC_GAIN;
674 break;
675 case IIO_TEMP:
676 *val = 28;
677 *val2 = 571429;
678 return IIO_VAL_INT_PLUS_MICRO;
679 case IIO_UVINDEX:
680 *val = 0;
681 *val2 = 10000;
682 return IIO_VAL_INT_PLUS_MICRO;
683 default:
684 return -EINVAL;
685 }
686
687 ret = si1145_param_query(data, reg);
688 if (ret < 0)
689 return ret;
690
691 *val = si1145_scale_from_adcgain(ret & 0x07);
692
693 return IIO_VAL_INT;
694 case IIO_CHAN_INFO_OFFSET:
695 switch (chan->type) {
696 case IIO_TEMP:
697 /*
698 * -ADC offset - ADC counts @ 25°C -
699 * 35 * ADC counts / °C
700 */
701 *val = -256 - 11136 + 25 * 35;
702 return IIO_VAL_INT;
703 default:
704 /*
705 * All ADC measurements have are by default offset
706 * by -256
707 * See AN498 5.6.3
708 */
709 ret = si1145_param_query(data, SI1145_PARAM_ADC_OFFSET);
710 if (ret < 0)
711 return ret;
712 *val = -si1145_uncompress(ret);
713 return IIO_VAL_INT;
714 }
715 case IIO_CHAN_INFO_SAMP_FREQ:
716 return si1145_read_samp_freq(data, val, val2);
717 default:
718 return -EINVAL;
719 }
720}
721
722static int si1145_write_raw(struct iio_dev *indio_dev,
723 struct iio_chan_spec const *chan,
724 int val, int val2, long mask)
725{
726 struct si1145_data *data = iio_priv(indio_dev);
727 u8 reg1, reg2, shift;
728 int ret;
729
730 switch (mask) {
731 case IIO_CHAN_INFO_SCALE:
732 switch (chan->type) {
733 case IIO_PROXIMITY:
734 val = si1145_proximity_adcgain_from_scale(val, val2);
735 if (val < 0)
736 return val;
737 reg1 = SI1145_PARAM_PS_ADC_GAIN;
738 reg2 = SI1145_PARAM_PS_ADC_COUNTER;
739 break;
740 case IIO_INTENSITY:
741 val = si1145_intensity_adcgain_from_scale(val, val2);
742 if (val < 0)
743 return val;
744 if (chan->channel2 == IIO_MOD_LIGHT_IR) {
745 reg1 = SI1145_PARAM_ALSIR_ADC_GAIN;
746 reg2 = SI1145_PARAM_ALSIR_ADC_COUNTER;
747 } else {
748 reg1 = SI1145_PARAM_ALSVIS_ADC_GAIN;
749 reg2 = SI1145_PARAM_ALSVIS_ADC_COUNTER;
750 }
751 break;
752 default:
753 return -EINVAL;
754 }
755
756 ret = iio_device_claim_direct_mode(indio_dev);
757 if (ret)
758 return ret;
759
760 ret = si1145_param_set(data, reg1, val);
761 if (ret < 0) {
762 iio_device_release_direct_mode(indio_dev);
763 return ret;
764 }
765 /* Set recovery period to one's complement of gain */
766 ret = si1145_param_set(data, reg2, (~val & 0x07) << 4);
767 iio_device_release_direct_mode(indio_dev);
768 return ret;
769 case IIO_CHAN_INFO_RAW:
770 if (chan->type != IIO_CURRENT)
771 return -EINVAL;
772
773 if (val < 0 || val > 15 || val2 != 0)
774 return -EINVAL;
775
776 reg1 = SI1145_PS_LED_REG(chan->channel);
777 shift = SI1145_PS_LED_SHIFT(chan->channel);
778
779 ret = iio_device_claim_direct_mode(indio_dev);
780 if (ret)
781 return ret;
782
783 ret = i2c_smbus_read_byte_data(data->client, reg1);
784 if (ret < 0) {
785 iio_device_release_direct_mode(indio_dev);
786 return ret;
787 }
788 ret = i2c_smbus_write_byte_data(data->client, reg1,
789 (ret & ~(0x0f << shift)) |
790 ((val & 0x0f) << shift));
791 iio_device_release_direct_mode(indio_dev);
792 return ret;
793 case IIO_CHAN_INFO_SAMP_FREQ:
794 return si1145_store_samp_freq(data, val);
795 default:
796 return -EINVAL;
797 }
798}
799
800#define SI1145_ST { \
801 .sign = 'u', \
802 .realbits = 16, \
803 .storagebits = 16, \
804 .endianness = IIO_LE, \
805}
806
807#define SI1145_INTENSITY_CHANNEL(_si) { \
808 .type = IIO_INTENSITY, \
809 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
810 BIT(IIO_CHAN_INFO_OFFSET) | \
811 BIT(IIO_CHAN_INFO_SCALE), \
812 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
813 .scan_type = SI1145_ST, \
814 .scan_index = _si, \
815 .address = SI1145_REG_ALSVIS_DATA, \
816}
817
818#define SI1145_INTENSITY_IR_CHANNEL(_si) { \
819 .type = IIO_INTENSITY, \
820 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
821 BIT(IIO_CHAN_INFO_OFFSET) | \
822 BIT(IIO_CHAN_INFO_SCALE), \
823 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
824 .modified = 1, \
825 .channel2 = IIO_MOD_LIGHT_IR, \
826 .scan_type = SI1145_ST, \
827 .scan_index = _si, \
828 .address = SI1145_REG_ALSIR_DATA, \
829}
830
831#define SI1145_TEMP_CHANNEL(_si) { \
832 .type = IIO_TEMP, \
833 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
834 BIT(IIO_CHAN_INFO_OFFSET) | \
835 BIT(IIO_CHAN_INFO_SCALE), \
836 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
837 .scan_type = SI1145_ST, \
838 .scan_index = _si, \
839 .address = SI1145_REG_AUX_DATA, \
840}
841
842#define SI1145_UV_CHANNEL(_si) { \
843 .type = IIO_UVINDEX, \
844 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
845 BIT(IIO_CHAN_INFO_SCALE), \
846 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
847 .scan_type = SI1145_ST, \
848 .scan_index = _si, \
849 .address = SI1145_REG_AUX_DATA, \
850}
851
852#define SI1145_PROXIMITY_CHANNEL(_si, _ch) { \
853 .type = IIO_PROXIMITY, \
854 .indexed = 1, \
855 .channel = _ch, \
856 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
857 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
858 BIT(IIO_CHAN_INFO_OFFSET), \
859 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
860 .scan_type = SI1145_ST, \
861 .scan_index = _si, \
862 .address = SI1145_REG_PS1_DATA + _ch * 2, \
863}
864
865#define SI1145_VOLTAGE_CHANNEL(_si) { \
866 .type = IIO_VOLTAGE, \
867 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
868 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
869 .scan_type = SI1145_ST, \
870 .scan_index = _si, \
871 .address = SI1145_REG_AUX_DATA, \
872}
873
874#define SI1145_CURRENT_CHANNEL(_ch) { \
875 .type = IIO_CURRENT, \
876 .indexed = 1, \
877 .channel = _ch, \
878 .output = 1, \
879 .scan_index = -1, \
880 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
881}
882
883static const struct iio_chan_spec si1132_channels[] = {
884 SI1145_INTENSITY_CHANNEL(0),
885 SI1145_INTENSITY_IR_CHANNEL(1),
886 SI1145_TEMP_CHANNEL(2),
887 SI1145_VOLTAGE_CHANNEL(3),
888 SI1145_UV_CHANNEL(4),
889 IIO_CHAN_SOFT_TIMESTAMP(6),
890};
891
892static const struct iio_chan_spec si1141_channels[] = {
893 SI1145_INTENSITY_CHANNEL(0),
894 SI1145_INTENSITY_IR_CHANNEL(1),
895 SI1145_PROXIMITY_CHANNEL(2, 0),
896 SI1145_TEMP_CHANNEL(3),
897 SI1145_VOLTAGE_CHANNEL(4),
898 IIO_CHAN_SOFT_TIMESTAMP(5),
899 SI1145_CURRENT_CHANNEL(0),
900};
901
902static const struct iio_chan_spec si1142_channels[] = {
903 SI1145_INTENSITY_CHANNEL(0),
904 SI1145_INTENSITY_IR_CHANNEL(1),
905 SI1145_PROXIMITY_CHANNEL(2, 0),
906 SI1145_PROXIMITY_CHANNEL(3, 1),
907 SI1145_TEMP_CHANNEL(4),
908 SI1145_VOLTAGE_CHANNEL(5),
909 IIO_CHAN_SOFT_TIMESTAMP(6),
910 SI1145_CURRENT_CHANNEL(0),
911 SI1145_CURRENT_CHANNEL(1),
912};
913
914static const struct iio_chan_spec si1143_channels[] = {
915 SI1145_INTENSITY_CHANNEL(0),
916 SI1145_INTENSITY_IR_CHANNEL(1),
917 SI1145_PROXIMITY_CHANNEL(2, 0),
918 SI1145_PROXIMITY_CHANNEL(3, 1),
919 SI1145_PROXIMITY_CHANNEL(4, 2),
920 SI1145_TEMP_CHANNEL(5),
921 SI1145_VOLTAGE_CHANNEL(6),
922 IIO_CHAN_SOFT_TIMESTAMP(7),
923 SI1145_CURRENT_CHANNEL(0),
924 SI1145_CURRENT_CHANNEL(1),
925 SI1145_CURRENT_CHANNEL(2),
926};
927
928static const struct iio_chan_spec si1145_channels[] = {
929 SI1145_INTENSITY_CHANNEL(0),
930 SI1145_INTENSITY_IR_CHANNEL(1),
931 SI1145_PROXIMITY_CHANNEL(2, 0),
932 SI1145_TEMP_CHANNEL(3),
933 SI1145_VOLTAGE_CHANNEL(4),
934 SI1145_UV_CHANNEL(5),
935 IIO_CHAN_SOFT_TIMESTAMP(6),
936 SI1145_CURRENT_CHANNEL(0),
937};
938
939static const struct iio_chan_spec si1146_channels[] = {
940 SI1145_INTENSITY_CHANNEL(0),
941 SI1145_INTENSITY_IR_CHANNEL(1),
942 SI1145_TEMP_CHANNEL(2),
943 SI1145_VOLTAGE_CHANNEL(3),
944 SI1145_UV_CHANNEL(4),
945 SI1145_PROXIMITY_CHANNEL(5, 0),
946 SI1145_PROXIMITY_CHANNEL(6, 1),
947 IIO_CHAN_SOFT_TIMESTAMP(7),
948 SI1145_CURRENT_CHANNEL(0),
949 SI1145_CURRENT_CHANNEL(1),
950};
951
952static const struct iio_chan_spec si1147_channels[] = {
953 SI1145_INTENSITY_CHANNEL(0),
954 SI1145_INTENSITY_IR_CHANNEL(1),
955 SI1145_PROXIMITY_CHANNEL(2, 0),
956 SI1145_PROXIMITY_CHANNEL(3, 1),
957 SI1145_PROXIMITY_CHANNEL(4, 2),
958 SI1145_TEMP_CHANNEL(5),
959 SI1145_VOLTAGE_CHANNEL(6),
960 SI1145_UV_CHANNEL(7),
961 IIO_CHAN_SOFT_TIMESTAMP(8),
962 SI1145_CURRENT_CHANNEL(0),
963 SI1145_CURRENT_CHANNEL(1),
964 SI1145_CURRENT_CHANNEL(2),
965};
966
967static struct attribute *si1132_attributes[] = {
968 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
969 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
970 NULL,
971};
972
973static struct attribute *si114x_attributes[] = {
974 &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
975 &iio_const_attr_in_intensity_ir_scale_available.dev_attr.attr,
976 &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
977 NULL,
978};
979
980static const struct attribute_group si1132_attribute_group = {
981 .attrs = si1132_attributes,
982};
983
984static const struct attribute_group si114x_attribute_group = {
985 .attrs = si114x_attributes,
986};
987
988
989static const struct iio_info si1132_info = {
990 .read_raw = si1145_read_raw,
991 .write_raw = si1145_write_raw,
992 .driver_module = THIS_MODULE,
993 .attrs = &si1132_attribute_group,
994};
995
996static const struct iio_info si114x_info = {
997 .read_raw = si1145_read_raw,
998 .write_raw = si1145_write_raw,
999 .driver_module = THIS_MODULE,
1000 .attrs = &si114x_attribute_group,
1001};
1002
1003#define SI1145_PART(id, iio_info, chans, leds, uncompressed_meas_rate) \
1004 {id, iio_info, chans, ARRAY_SIZE(chans), leds, uncompressed_meas_rate}
1005
1006static const struct si1145_part_info si1145_part_info[] = {
1007 [SI1132] = SI1145_PART(0x32, &si1132_info, si1132_channels, 0, true),
1008 [SI1141] = SI1145_PART(0x41, &si114x_info, si1141_channels, 1, false),
1009 [SI1142] = SI1145_PART(0x42, &si114x_info, si1142_channels, 2, false),
1010 [SI1143] = SI1145_PART(0x43, &si114x_info, si1143_channels, 3, false),
1011 [SI1145] = SI1145_PART(0x45, &si114x_info, si1145_channels, 1, true),
1012 [SI1146] = SI1145_PART(0x46, &si114x_info, si1146_channels, 2, true),
1013 [SI1147] = SI1145_PART(0x47, &si114x_info, si1147_channels, 3, true),
1014};
1015
1016static int si1145_initialize(struct si1145_data *data)
1017{
1018 struct i2c_client *client = data->client;
1019 int ret;
1020
1021 ret = i2c_smbus_write_byte_data(client, SI1145_REG_COMMAND,
1022 SI1145_CMD_RESET);
1023 if (ret < 0)
1024 return ret;
1025 msleep(SI1145_COMMAND_TIMEOUT_MS);
1026
1027 /* Hardware key, magic value */
1028 ret = i2c_smbus_write_byte_data(client, SI1145_REG_HW_KEY, 0x17);
1029 if (ret < 0)
1030 return ret;
1031 msleep(SI1145_COMMAND_TIMEOUT_MS);
1032
1033 /* Turn off autonomous mode */
1034 ret = si1145_set_meas_rate(data, 0);
1035 if (ret < 0)
1036 return ret;
1037
1038 /* Initialize sampling freq to 10 Hz */
1039 ret = si1145_store_samp_freq(data, 10);
1040 if (ret < 0)
1041 return ret;
1042
1043 /* Set LED currents to 45 mA; have 4 bits, see Table 2 in datasheet */
1044 switch (data->part_info->num_leds) {
1045 case 3:
1046 ret = i2c_smbus_write_byte_data(client,
1047 SI1145_REG_PS_LED3,
1048 SI1145_LED_CURRENT_45mA);
1049 if (ret < 0)
1050 return ret;
1051 /* fallthrough */
1052 case 2:
1053 ret = i2c_smbus_write_byte_data(client,
1054 SI1145_REG_PS_LED21,
1055 (SI1145_LED_CURRENT_45mA << 4) |
1056 SI1145_LED_CURRENT_45mA);
1057 break;
1058 case 1:
1059 ret = i2c_smbus_write_byte_data(client,
1060 SI1145_REG_PS_LED21,
1061 SI1145_LED_CURRENT_45mA);
1062 break;
1063 default:
1064 ret = 0;
1065 break;
1066 }
1067 if (ret < 0)
1068 return ret;
1069
1070 /* Set normal proximity measurement mode */
1071 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_MISC,
1072 SI1145_PS_ADC_MODE_NORMAL);
1073 if (ret < 0)
1074 return ret;
1075
1076 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_GAIN, 0x01);
1077 if (ret < 0)
1078 return ret;
1079
1080 /* ADC_COUNTER should be one complement of ADC_GAIN */
1081 ret = si1145_param_set(data, SI1145_PARAM_PS_ADC_COUNTER, 0x06 << 4);
1082 if (ret < 0)
1083 return ret;
1084
1085 /* Set ALS visible measurement mode */
1086 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_MISC,
1087 SI1145_ADC_MISC_RANGE);
1088 if (ret < 0)
1089 return ret;
1090
1091 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_GAIN, 0x03);
1092 if (ret < 0)
1093 return ret;
1094
1095 ret = si1145_param_set(data, SI1145_PARAM_ALSVIS_ADC_COUNTER,
1096 0x04 << 4);
1097 if (ret < 0)
1098 return ret;
1099
1100 /* Set ALS IR measurement mode */
1101 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_MISC,
1102 SI1145_ADC_MISC_RANGE);
1103 if (ret < 0)
1104 return ret;
1105
1106 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_GAIN, 0x01);
1107 if (ret < 0)
1108 return ret;
1109
1110 ret = si1145_param_set(data, SI1145_PARAM_ALSIR_ADC_COUNTER,
1111 0x06 << 4);
1112 if (ret < 0)
1113 return ret;
1114
1115 /*
1116 * Initialize UCOEF to default values in datasheet
1117 * These registers are normally zero on reset
1118 */
1119 if (data->part_info == &si1145_part_info[SI1132] ||
1120 data->part_info == &si1145_part_info[SI1145] ||
1121 data->part_info == &si1145_part_info[SI1146] ||
1122 data->part_info == &si1145_part_info[SI1147]) {
1123 ret = i2c_smbus_write_byte_data(data->client,
1124 SI1145_REG_UCOEF1,
1125 SI1145_UCOEF1_DEFAULT);
1126 if (ret < 0)
1127 return ret;
1128 ret = i2c_smbus_write_byte_data(data->client,
1129 SI1145_REG_UCOEF2, SI1145_UCOEF2_DEFAULT);
1130 if (ret < 0)
1131 return ret;
1132 ret = i2c_smbus_write_byte_data(data->client,
1133 SI1145_REG_UCOEF3, SI1145_UCOEF3_DEFAULT);
1134 if (ret < 0)
1135 return ret;
1136 ret = i2c_smbus_write_byte_data(data->client,
1137 SI1145_REG_UCOEF4, SI1145_UCOEF4_DEFAULT);
1138 if (ret < 0)
1139 return ret;
1140 }
1141
1142 return 0;
1143}
1144
1145/*
1146 * Program the channels we want to measure with CMD_PSALS_AUTO. No need for
1147 * _postdisable as we stop with CMD_PSALS_PAUSE; single measurement (direct)
1148 * mode reprograms the channels list anyway...
1149 */
1150static int si1145_buffer_preenable(struct iio_dev *indio_dev)
1151{
1152 struct si1145_data *data = iio_priv(indio_dev);
1153 int ret;
1154
1155 mutex_lock(&data->lock);
1156 ret = si1145_set_chlist(indio_dev, *indio_dev->active_scan_mask);
1157 mutex_unlock(&data->lock);
1158
1159 return ret;
1160}
1161
1162static bool si1145_validate_scan_mask(struct iio_dev *indio_dev,
1163 const unsigned long *scan_mask)
1164{
1165 struct si1145_data *data = iio_priv(indio_dev);
1166 unsigned int count = 0;
1167 int i;
1168
1169 /* Check that at most one AUX channel is enabled */
1170 for_each_set_bit(i, scan_mask, data->part_info->num_channels) {
1171 if (indio_dev->channels[i].address == SI1145_REG_AUX_DATA)
1172 count++;
1173 }
1174
1175 return count <= 1;
1176}
1177
1178static const struct iio_buffer_setup_ops si1145_buffer_setup_ops = {
1179 .preenable = si1145_buffer_preenable,
1180 .postenable = iio_triggered_buffer_postenable,
1181 .predisable = iio_triggered_buffer_predisable,
1182 .validate_scan_mask = si1145_validate_scan_mask,
1183};
1184
1185/**
1186 * si1145_trigger_set_state() - Set trigger state
1187 *
1188 * When not using triggers interrupts are disabled and measurement rate is
1189 * set to zero in order to minimize power consumption.
1190 */
1191static int si1145_trigger_set_state(struct iio_trigger *trig, bool state)
1192{
1193 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1194 struct si1145_data *data = iio_priv(indio_dev);
1195 int err = 0, ret;
1196
1197 mutex_lock(&data->lock);
1198
1199 if (state) {
1200 data->autonomous = true;
1201 err = i2c_smbus_write_byte_data(data->client,
1202 SI1145_REG_INT_CFG, SI1145_INT_CFG_OE);
1203 if (err < 0)
1204 goto disable;
1205 err = i2c_smbus_write_byte_data(data->client,
1206 SI1145_REG_IRQ_ENABLE, SI1145_MASK_ALL_IE);
1207 if (err < 0)
1208 goto disable;
1209 err = si1145_set_meas_rate(data, data->meas_rate);
1210 if (err < 0)
1211 goto disable;
1212 err = si1145_command(data, SI1145_CMD_PSALS_AUTO);
1213 if (err < 0)
1214 goto disable;
1215 } else {
1216disable:
1217 /* Disable as much as possible skipping errors */
1218 ret = si1145_command(data, SI1145_CMD_PSALS_PAUSE);
1219 if (ret < 0 && !err)
1220 err = ret;
1221 ret = si1145_set_meas_rate(data, 0);
1222 if (ret < 0 && !err)
1223 err = ret;
1224 ret = i2c_smbus_write_byte_data(data->client,
1225 SI1145_REG_IRQ_ENABLE, 0);
1226 if (ret < 0 && !err)
1227 err = ret;
1228 ret = i2c_smbus_write_byte_data(data->client,
1229 SI1145_REG_INT_CFG, 0);
1230 if (ret < 0 && !err)
1231 err = ret;
1232 data->autonomous = false;
1233 }
1234
1235 mutex_unlock(&data->lock);
1236 return err;
1237}
1238
1239static const struct iio_trigger_ops si1145_trigger_ops = {
1240 .owner = THIS_MODULE,
1241 .set_trigger_state = si1145_trigger_set_state,
1242};
1243
1244static int si1145_probe_trigger(struct iio_dev *indio_dev)
1245{
1246 struct si1145_data *data = iio_priv(indio_dev);
1247 struct i2c_client *client = data->client;
1248 struct iio_trigger *trig;
1249 int ret;
1250
1251 trig = devm_iio_trigger_alloc(&client->dev,
1252 "%s-dev%d", indio_dev->name, indio_dev->id);
1253 if (!trig)
1254 return -ENOMEM;
1255
1256 trig->dev.parent = &client->dev;
1257 trig->ops = &si1145_trigger_ops;
1258 iio_trigger_set_drvdata(trig, indio_dev);
1259
1260 ret = devm_request_irq(&client->dev, client->irq,
1261 iio_trigger_generic_data_rdy_poll,
1262 IRQF_TRIGGER_FALLING,
1263 "si1145_irq",
1264 trig);
1265 if (ret < 0) {
1266 dev_err(&client->dev, "irq request failed\n");
1267 return ret;
1268 }
1269
1270 ret = iio_trigger_register(trig);
1271 if (ret)
1272 return ret;
1273
1274 data->trig = trig;
1275 indio_dev->trig = iio_trigger_get(data->trig);
1276
1277 return 0;
1278}
1279
1280static void si1145_remove_trigger(struct iio_dev *indio_dev)
1281{
1282 struct si1145_data *data = iio_priv(indio_dev);
1283
1284 if (data->trig) {
1285 iio_trigger_unregister(data->trig);
1286 data->trig = NULL;
1287 }
1288}
1289
1290static int si1145_probe(struct i2c_client *client,
1291 const struct i2c_device_id *id)
1292{
1293 struct si1145_data *data;
1294 struct iio_dev *indio_dev;
1295 u8 part_id, rev_id, seq_id;
1296 int ret;
1297
1298 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1299 if (!indio_dev)
1300 return -ENOMEM;
1301
1302 data = iio_priv(indio_dev);
1303 i2c_set_clientdata(client, indio_dev);
1304 data->client = client;
1305 data->part_info = &si1145_part_info[id->driver_data];
1306
1307 part_id = ret = i2c_smbus_read_byte_data(data->client,
1308 SI1145_REG_PART_ID);
1309 if (ret < 0)
1310 return ret;
1311 rev_id = ret = i2c_smbus_read_byte_data(data->client,
1312 SI1145_REG_REV_ID);
1313 if (ret < 0)
1314 return ret;
1315 seq_id = ret = i2c_smbus_read_byte_data(data->client,
1316 SI1145_REG_SEQ_ID);
1317 if (ret < 0)
1318 return ret;
1319 dev_info(&client->dev, "device ID part %#02hhx rev %#02hhx seq %#02hhx\n",
1320 part_id, rev_id, seq_id);
1321 if (part_id != data->part_info->part) {
1322 dev_err(&client->dev, "part ID mismatch got %#02hhx, expected %#02x\n",
1323 part_id, data->part_info->part);
1324 return -ENODEV;
1325 }
1326
1327 indio_dev->dev.parent = &client->dev;
1328 indio_dev->name = id->name;
1329 indio_dev->channels = data->part_info->channels;
1330 indio_dev->num_channels = data->part_info->num_channels;
1331 indio_dev->info = data->part_info->iio_info;
1332 indio_dev->modes = INDIO_DIRECT_MODE;
1333
1334 mutex_init(&data->lock);
1335 mutex_init(&data->cmdlock);
1336
1337 ret = si1145_initialize(data);
1338 if (ret < 0)
1339 return ret;
1340
1341 ret = iio_triggered_buffer_setup(indio_dev, NULL,
1342 si1145_trigger_handler, &si1145_buffer_setup_ops);
1343 if (ret < 0)
1344 return ret;
1345
1346 if (client->irq) {
1347 ret = si1145_probe_trigger(indio_dev);
1348 if (ret < 0)
1349 goto error_free_buffer;
1350 } else {
1351 dev_info(&client->dev, "no irq, using polling\n");
1352 }
1353
1354 ret = iio_device_register(indio_dev);
1355 if (ret < 0)
1356 goto error_free_trigger;
1357
1358 return 0;
1359
1360error_free_trigger:
1361 si1145_remove_trigger(indio_dev);
1362error_free_buffer:
1363 iio_triggered_buffer_cleanup(indio_dev);
1364
1365 return ret;
1366}
1367
1368static const struct i2c_device_id si1145_ids[] = {
1369 { "si1132", SI1132 },
1370 { "si1141", SI1141 },
1371 { "si1142", SI1142 },
1372 { "si1143", SI1143 },
1373 { "si1145", SI1145 },
1374 { "si1146", SI1146 },
1375 { "si1147", SI1147 },
1376 { }
1377};
1378MODULE_DEVICE_TABLE(i2c, si1145_ids);
1379
1380static int si1145_remove(struct i2c_client *client)
1381{
1382 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1383
1384 iio_device_unregister(indio_dev);
1385 si1145_remove_trigger(indio_dev);
1386 iio_triggered_buffer_cleanup(indio_dev);
1387
1388 return 0;
1389}
1390
1391static struct i2c_driver si1145_driver = {
1392 .driver = {
1393 .name = "si1145",
1394 },
1395 .probe = si1145_probe,
1396 .remove = si1145_remove,
1397 .id_table = si1145_ids,
1398};
1399
1400module_i2c_driver(si1145_driver);
1401
1402MODULE_AUTHOR("Peter Meerwald-Stadler <pmeerw@pmeerw.net>");
1403MODULE_DESCRIPTION("Silabs SI1132 and SI1141/2/3/5/6/7 proximity, ambient light and UV index sensor driver");
1404MODULE_LICENSE("GPL");
diff --git a/drivers/iio/light/us5182d.c b/drivers/iio/light/us5182d.c
index 20c40f780964..18cf2e29e4d5 100644
--- a/drivers/iio/light/us5182d.c
+++ b/drivers/iio/light/us5182d.c
@@ -894,7 +894,7 @@ static int us5182d_probe(struct i2c_client *client,
894 goto out_err; 894 goto out_err;
895 895
896 if (data->default_continuous) { 896 if (data->default_continuous) {
897 pm_runtime_set_active(&client->dev); 897 ret = pm_runtime_set_active(&client->dev);
898 if (ret < 0) 898 if (ret < 0)
899 goto out_err; 899 goto out_err;
900 } 900 }
diff --git a/drivers/iio/light/vcnl4000.c b/drivers/iio/light/vcnl4000.c
index c9d85bbc9230..360b6e98137a 100644
--- a/drivers/iio/light/vcnl4000.c
+++ b/drivers/iio/light/vcnl4000.c
@@ -1,6 +1,6 @@
1/* 1/*
2 * vcnl4000.c - Support for Vishay VCNL4000 combined ambient light and 2 * vcnl4000.c - Support for Vishay VCNL4000/4010/4020 combined ambient
3 * proximity sensor 3 * light and proximity sensor
4 * 4 *
5 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net> 5 * Copyright 2012 Peter Meerwald <pmeerw@pmeerw.net>
6 * 6 *
@@ -13,6 +13,8 @@
13 * TODO: 13 * TODO:
14 * allow to adjust IR current 14 * allow to adjust IR current
15 * proximity threshold and event handling 15 * proximity threshold and event handling
16 * periodic ALS/proximity measurement (VCNL4010/20)
17 * interrupts (VCNL4010/20)
16 */ 18 */
17 19
18#include <linux/module.h> 20#include <linux/module.h>
@@ -24,6 +26,8 @@
24#include <linux/iio/sysfs.h> 26#include <linux/iio/sysfs.h>
25 27
26#define VCNL4000_DRV_NAME "vcnl4000" 28#define VCNL4000_DRV_NAME "vcnl4000"
29#define VCNL4000_ID 0x01
30#define VCNL4010_ID 0x02 /* for VCNL4020, VCNL4010 */
27 31
28#define VCNL4000_COMMAND 0x80 /* Command register */ 32#define VCNL4000_COMMAND 0x80 /* Command register */
29#define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */ 33#define VCNL4000_PROD_REV 0x81 /* Product ID and Revision ID */
@@ -37,13 +41,14 @@
37#define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */ 41#define VCNL4000_PS_MOD_ADJ 0x8a /* Proximity modulator timing adjustment */
38 42
39/* Bit masks for COMMAND register */ 43/* Bit masks for COMMAND register */
40#define VCNL4000_AL_RDY 0x40 /* ALS data ready? */ 44#define VCNL4000_AL_RDY BIT(6) /* ALS data ready? */
41#define VCNL4000_PS_RDY 0x20 /* proximity data ready? */ 45#define VCNL4000_PS_RDY BIT(5) /* proximity data ready? */
42#define VCNL4000_AL_OD 0x10 /* start on-demand ALS measurement */ 46#define VCNL4000_AL_OD BIT(4) /* start on-demand ALS measurement */
43#define VCNL4000_PS_OD 0x08 /* start on-demand proximity measurement */ 47#define VCNL4000_PS_OD BIT(3) /* start on-demand proximity measurement */
44 48
45struct vcnl4000_data { 49struct vcnl4000_data {
46 struct i2c_client *client; 50 struct i2c_client *client;
51 struct mutex lock;
47}; 52};
48 53
49static const struct i2c_device_id vcnl4000_id[] = { 54static const struct i2c_device_id vcnl4000_id[] = {
@@ -59,16 +64,18 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
59 __be16 buf; 64 __be16 buf;
60 int ret; 65 int ret;
61 66
67 mutex_lock(&data->lock);
68
62 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND, 69 ret = i2c_smbus_write_byte_data(data->client, VCNL4000_COMMAND,
63 req_mask); 70 req_mask);
64 if (ret < 0) 71 if (ret < 0)
65 return ret; 72 goto fail;
66 73
67 /* wait for data to become ready */ 74 /* wait for data to become ready */
68 while (tries--) { 75 while (tries--) {
69 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND); 76 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_COMMAND);
70 if (ret < 0) 77 if (ret < 0)
71 return ret; 78 goto fail;
72 if (ret & rdy_mask) 79 if (ret & rdy_mask)
73 break; 80 break;
74 msleep(20); /* measurement takes up to 100 ms */ 81 msleep(20); /* measurement takes up to 100 ms */
@@ -77,17 +84,23 @@ static int vcnl4000_measure(struct vcnl4000_data *data, u8 req_mask,
77 if (tries < 0) { 84 if (tries < 0) {
78 dev_err(&data->client->dev, 85 dev_err(&data->client->dev,
79 "vcnl4000_measure() failed, data not ready\n"); 86 "vcnl4000_measure() failed, data not ready\n");
80 return -EIO; 87 ret = -EIO;
88 goto fail;
81 } 89 }
82 90
83 ret = i2c_smbus_read_i2c_block_data(data->client, 91 ret = i2c_smbus_read_i2c_block_data(data->client,
84 data_reg, sizeof(buf), (u8 *) &buf); 92 data_reg, sizeof(buf), (u8 *) &buf);
85 if (ret < 0) 93 if (ret < 0)
86 return ret; 94 goto fail;
87 95
96 mutex_unlock(&data->lock);
88 *val = be16_to_cpu(buf); 97 *val = be16_to_cpu(buf);
89 98
90 return 0; 99 return 0;
100
101fail:
102 mutex_unlock(&data->lock);
103 return ret;
91} 104}
92 105
93static const struct iio_chan_spec vcnl4000_channels[] = { 106static const struct iio_chan_spec vcnl4000_channels[] = {
@@ -105,7 +118,7 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
105 struct iio_chan_spec const *chan, 118 struct iio_chan_spec const *chan,
106 int *val, int *val2, long mask) 119 int *val, int *val2, long mask)
107{ 120{
108 int ret = -EINVAL; 121 int ret;
109 struct vcnl4000_data *data = iio_priv(indio_dev); 122 struct vcnl4000_data *data = iio_priv(indio_dev);
110 123
111 switch (mask) { 124 switch (mask) {
@@ -117,32 +130,27 @@ static int vcnl4000_read_raw(struct iio_dev *indio_dev,
117 VCNL4000_AL_RESULT_HI, val); 130 VCNL4000_AL_RESULT_HI, val);
118 if (ret < 0) 131 if (ret < 0)
119 return ret; 132 return ret;
120 ret = IIO_VAL_INT; 133 return IIO_VAL_INT;
121 break;
122 case IIO_PROXIMITY: 134 case IIO_PROXIMITY:
123 ret = vcnl4000_measure(data, 135 ret = vcnl4000_measure(data,
124 VCNL4000_PS_OD, VCNL4000_PS_RDY, 136 VCNL4000_PS_OD, VCNL4000_PS_RDY,
125 VCNL4000_PS_RESULT_HI, val); 137 VCNL4000_PS_RESULT_HI, val);
126 if (ret < 0) 138 if (ret < 0)
127 return ret; 139 return ret;
128 ret = IIO_VAL_INT; 140 return IIO_VAL_INT;
129 break;
130 default: 141 default:
131 break; 142 return -EINVAL;
132 } 143 }
133 break;
134 case IIO_CHAN_INFO_SCALE: 144 case IIO_CHAN_INFO_SCALE:
135 if (chan->type == IIO_LIGHT) { 145 if (chan->type != IIO_LIGHT)
136 *val = 0; 146 return -EINVAL;
137 *val2 = 250000; 147
138 ret = IIO_VAL_INT_PLUS_MICRO; 148 *val = 0;
139 } 149 *val2 = 250000;
140 break; 150 return IIO_VAL_INT_PLUS_MICRO;
141 default: 151 default:
142 break; 152 return -EINVAL;
143 } 153 }
144
145 return ret;
146} 154}
147 155
148static const struct iio_info vcnl4000_info = { 156static const struct iio_info vcnl4000_info = {
@@ -155,7 +163,7 @@ static int vcnl4000_probe(struct i2c_client *client,
155{ 163{
156 struct vcnl4000_data *data; 164 struct vcnl4000_data *data;
157 struct iio_dev *indio_dev; 165 struct iio_dev *indio_dev;
158 int ret; 166 int ret, prod_id;
159 167
160 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); 168 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
161 if (!indio_dev) 169 if (!indio_dev)
@@ -164,13 +172,19 @@ static int vcnl4000_probe(struct i2c_client *client,
164 data = iio_priv(indio_dev); 172 data = iio_priv(indio_dev);
165 i2c_set_clientdata(client, indio_dev); 173 i2c_set_clientdata(client, indio_dev);
166 data->client = client; 174 data->client = client;
175 mutex_init(&data->lock);
167 176
168 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV); 177 ret = i2c_smbus_read_byte_data(data->client, VCNL4000_PROD_REV);
169 if (ret < 0) 178 if (ret < 0)
170 return ret; 179 return ret;
171 180
172 dev_info(&client->dev, "VCNL4000 Ambient light/proximity sensor, Prod %02x, Rev: %02x\n", 181 prod_id = ret >> 4;
173 ret >> 4, ret & 0xf); 182 if (prod_id != VCNL4010_ID && prod_id != VCNL4000_ID)
183 return -ENODEV;
184
185 dev_dbg(&client->dev, "%s Ambient light/proximity sensor, Rev: %02x\n",
186 (prod_id == VCNL4010_ID) ? "VCNL4010/4020" : "VCNL4000",
187 ret & 0xf);
174 188
175 indio_dev->dev.parent = &client->dev; 189 indio_dev->dev.parent = &client->dev;
176 indio_dev->info = &vcnl4000_info; 190 indio_dev->info = &vcnl4000_info;
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index 1f842abcb4a4..421ad90a5fbe 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -5,8 +5,22 @@
5 5
6menu "Magnetometer sensors" 6menu "Magnetometer sensors"
7 7
8config AK8974
9 tristate "Asahi Kasei AK8974 3-Axis Magnetometer"
10 depends on I2C
11 depends on OF
12 select REGMAP_I2C
13 select IIO_BUFFER
14 select IIO_TRIGGERED_BUFFER
15 help
16 Say yes here to build support for Asahi Kasei AK8974 or
17 AMI305 I2C-based 3-axis magnetometer chips.
18
19 To compile this driver as a module, choose M here: the module
20 will be called ak8974.
21
8config AK8975 22config AK8975
9 tristate "Asahi Kasei AK 3-Axis Magnetometer" 23 tristate "Asahi Kasei AK8975 3-Axis Magnetometer"
10 depends on I2C 24 depends on I2C
11 depends on GPIOLIB || COMPILE_TEST 25 depends on GPIOLIB || COMPILE_TEST
12 select IIO_BUFFER 26 select IIO_BUFFER
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index 92a745c9a6e8..b86d6cb7f285 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/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
6obj-$(CONFIG_AK8974) += ak8974.o
6obj-$(CONFIG_AK8975) += ak8975.o 7obj-$(CONFIG_AK8975) += ak8975.o
7obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o 8obj-$(CONFIG_BMC150_MAGN) += bmc150_magn.o
8obj-$(CONFIG_BMC150_MAGN_I2C) += bmc150_magn_i2c.o 9obj-$(CONFIG_BMC150_MAGN_I2C) += bmc150_magn_i2c.o
diff --git a/drivers/iio/magnetometer/ak8974.c b/drivers/iio/magnetometer/ak8974.c
new file mode 100644
index 000000000000..217353145676
--- /dev/null
+++ b/drivers/iio/magnetometer/ak8974.c
@@ -0,0 +1,860 @@
1/*
2 * Driver for the Asahi Kasei EMD Corporation AK8974
3 * and Aichi Steel AMI305 magnetometer chips.
4 * Based on a patch from Samu Onkalo and the AK8975 IIO driver.
5 *
6 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 * Copyright (c) 2010 NVIDIA Corporation.
8 * Copyright (C) 2016 Linaro Ltd.
9 *
10 * Author: Samu Onkalo <samu.p.onkalo@nokia.com>
11 * Author: Linus Walleij <linus.walleij@linaro.org>
12 */
13#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/i2c.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h> /* For irq_get_irq_data() */
18#include <linux/completion.h>
19#include <linux/err.h>
20#include <linux/mutex.h>
21#include <linux/delay.h>
22#include <linux/bitops.h>
23#include <linux/regmap.h>
24#include <linux/regulator/consumer.h>
25#include <linux/pm_runtime.h>
26
27#include <linux/iio/iio.h>
28#include <linux/iio/sysfs.h>
29#include <linux/iio/buffer.h>
30#include <linux/iio/trigger.h>
31#include <linux/iio/trigger_consumer.h>
32#include <linux/iio/triggered_buffer.h>
33
34/*
35 * 16-bit registers are little-endian. LSB is at the address defined below
36 * and MSB is at the next higher address.
37 */
38
39/* These registers are common for AK8974 and AMI305 */
40#define AK8974_SELFTEST 0x0C
41#define AK8974_SELFTEST_IDLE 0x55
42#define AK8974_SELFTEST_OK 0xAA
43
44#define AK8974_INFO 0x0D
45
46#define AK8974_WHOAMI 0x0F
47#define AK8974_WHOAMI_VALUE_AMI305 0x47
48#define AK8974_WHOAMI_VALUE_AK8974 0x48
49
50#define AK8974_DATA_X 0x10
51#define AK8974_DATA_Y 0x12
52#define AK8974_DATA_Z 0x14
53#define AK8974_INT_SRC 0x16
54#define AK8974_STATUS 0x18
55#define AK8974_INT_CLEAR 0x1A
56#define AK8974_CTRL1 0x1B
57#define AK8974_CTRL2 0x1C
58#define AK8974_CTRL3 0x1D
59#define AK8974_INT_CTRL 0x1E
60#define AK8974_INT_THRES 0x26 /* Absolute any axis value threshold */
61#define AK8974_PRESET 0x30
62
63/* AK8974-specific offsets */
64#define AK8974_OFFSET_X 0x20
65#define AK8974_OFFSET_Y 0x22
66#define AK8974_OFFSET_Z 0x24
67/* AMI305-specific offsets */
68#define AMI305_OFFSET_X 0x6C
69#define AMI305_OFFSET_Y 0x72
70#define AMI305_OFFSET_Z 0x78
71
72/* Different temperature registers */
73#define AK8974_TEMP 0x31
74#define AMI305_TEMP 0x60
75
76#define AK8974_INT_X_HIGH BIT(7) /* Axis over +threshold */
77#define AK8974_INT_Y_HIGH BIT(6)
78#define AK8974_INT_Z_HIGH BIT(5)
79#define AK8974_INT_X_LOW BIT(4) /* Axis below -threshold */
80#define AK8974_INT_Y_LOW BIT(3)
81#define AK8974_INT_Z_LOW BIT(2)
82#define AK8974_INT_RANGE BIT(1) /* Range overflow (any axis) */
83
84#define AK8974_STATUS_DRDY BIT(6) /* Data ready */
85#define AK8974_STATUS_OVERRUN BIT(5) /* Data overrun */
86#define AK8974_STATUS_INT BIT(4) /* Interrupt occurred */
87
88#define AK8974_CTRL1_POWER BIT(7) /* 0 = standby; 1 = active */
89#define AK8974_CTRL1_RATE BIT(4) /* 0 = 10 Hz; 1 = 20 Hz */
90#define AK8974_CTRL1_FORCE_EN BIT(1) /* 0 = normal; 1 = force */
91#define AK8974_CTRL1_MODE2 BIT(0) /* 0 */
92
93#define AK8974_CTRL2_INT_EN BIT(4) /* 1 = enable interrupts */
94#define AK8974_CTRL2_DRDY_EN BIT(3) /* 1 = enable data ready signal */
95#define AK8974_CTRL2_DRDY_POL BIT(2) /* 1 = data ready active high */
96#define AK8974_CTRL2_RESDEF (AK8974_CTRL2_DRDY_POL)
97
98#define AK8974_CTRL3_RESET BIT(7) /* Software reset */
99#define AK8974_CTRL3_FORCE BIT(6) /* Start forced measurement */
100#define AK8974_CTRL3_SELFTEST BIT(4) /* Set selftest register */
101#define AK8974_CTRL3_RESDEF 0x00
102
103#define AK8974_INT_CTRL_XEN BIT(7) /* Enable interrupt for this axis */
104#define AK8974_INT_CTRL_YEN BIT(6)
105#define AK8974_INT_CTRL_ZEN BIT(5)
106#define AK8974_INT_CTRL_XYZEN (BIT(7)|BIT(6)|BIT(5))
107#define AK8974_INT_CTRL_POL BIT(3) /* 0 = active low; 1 = active high */
108#define AK8974_INT_CTRL_PULSE BIT(1) /* 0 = latched; 1 = pulse (50 usec) */
109#define AK8974_INT_CTRL_RESDEF (AK8974_INT_CTRL_XYZEN | AK8974_INT_CTRL_POL)
110
111/* The AMI305 has elaborate FW version and serial number registers */
112#define AMI305_VER 0xE8
113#define AMI305_SN 0xEA
114
115#define AK8974_MAX_RANGE 2048
116
117#define AK8974_POWERON_DELAY 50
118#define AK8974_ACTIVATE_DELAY 1
119#define AK8974_SELFTEST_DELAY 1
120/*
121 * Set the autosuspend to two orders of magnitude larger than the poweron
122 * delay to make sane reasonable power tradeoff savings (5 seconds in
123 * this case).
124 */
125#define AK8974_AUTOSUSPEND_DELAY 5000
126
127#define AK8974_MEASTIME 3
128
129#define AK8974_PWR_ON 1
130#define AK8974_PWR_OFF 0
131
132/**
133 * struct ak8974 - state container for the AK8974 driver
134 * @i2c: parent I2C client
135 * @orientation: mounting matrix, flipped axis etc
136 * @map: regmap to access the AK8974 registers over I2C
137 * @regs: the avdd and dvdd power regulators
138 * @name: the name of the part
139 * @variant: the whoami ID value (for selecting code paths)
140 * @lock: locks the magnetometer for exclusive use during a measurement
141 * @drdy_irq: uses the DRDY IRQ line
142 * @drdy_complete: completion for DRDY
143 * @drdy_active_low: the DRDY IRQ is active low
144 */
145struct ak8974 {
146 struct i2c_client *i2c;
147 struct iio_mount_matrix orientation;
148 struct regmap *map;
149 struct regulator_bulk_data regs[2];
150 const char *name;
151 u8 variant;
152 struct mutex lock;
153 bool drdy_irq;
154 struct completion drdy_complete;
155 bool drdy_active_low;
156};
157
158static const char ak8974_reg_avdd[] = "avdd";
159static const char ak8974_reg_dvdd[] = "dvdd";
160
161static int ak8974_set_power(struct ak8974 *ak8974, bool mode)
162{
163 int ret;
164 u8 val;
165
166 val = mode ? AK8974_CTRL1_POWER : 0;
167 val |= AK8974_CTRL1_FORCE_EN;
168 ret = regmap_write(ak8974->map, AK8974_CTRL1, val);
169 if (ret < 0)
170 return ret;
171
172 if (mode)
173 msleep(AK8974_ACTIVATE_DELAY);
174
175 return 0;
176}
177
178static int ak8974_reset(struct ak8974 *ak8974)
179{
180 int ret;
181
182 /* Power on to get register access. Sets CTRL1 reg to reset state */
183 ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
184 if (ret)
185 return ret;
186 ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_RESDEF);
187 if (ret)
188 return ret;
189 ret = regmap_write(ak8974->map, AK8974_CTRL3, AK8974_CTRL3_RESDEF);
190 if (ret)
191 return ret;
192 ret = regmap_write(ak8974->map, AK8974_INT_CTRL,
193 AK8974_INT_CTRL_RESDEF);
194 if (ret)
195 return ret;
196
197 /* After reset, power off is default state */
198 return ak8974_set_power(ak8974, AK8974_PWR_OFF);
199}
200
201static int ak8974_configure(struct ak8974 *ak8974)
202{
203 int ret;
204
205 ret = regmap_write(ak8974->map, AK8974_CTRL2, AK8974_CTRL2_DRDY_EN |
206 AK8974_CTRL2_INT_EN);
207 if (ret)
208 return ret;
209 ret = regmap_write(ak8974->map, AK8974_CTRL3, 0);
210 if (ret)
211 return ret;
212 ret = regmap_write(ak8974->map, AK8974_INT_CTRL, AK8974_INT_CTRL_POL);
213 if (ret)
214 return ret;
215
216 return regmap_write(ak8974->map, AK8974_PRESET, 0);
217}
218
219static int ak8974_trigmeas(struct ak8974 *ak8974)
220{
221 unsigned int clear;
222 u8 mask;
223 u8 val;
224 int ret;
225
226 /* Clear any previous measurement overflow status */
227 ret = regmap_read(ak8974->map, AK8974_INT_CLEAR, &clear);
228 if (ret)
229 return ret;
230
231 /* If we have a DRDY IRQ line, use it */
232 if (ak8974->drdy_irq) {
233 mask = AK8974_CTRL2_INT_EN |
234 AK8974_CTRL2_DRDY_EN |
235 AK8974_CTRL2_DRDY_POL;
236 val = AK8974_CTRL2_DRDY_EN;
237
238 if (!ak8974->drdy_active_low)
239 val |= AK8974_CTRL2_DRDY_POL;
240
241 init_completion(&ak8974->drdy_complete);
242 ret = regmap_update_bits(ak8974->map, AK8974_CTRL2,
243 mask, val);
244 if (ret)
245 return ret;
246 }
247
248 /* Force a measurement */
249 return regmap_update_bits(ak8974->map,
250 AK8974_CTRL3,
251 AK8974_CTRL3_FORCE,
252 AK8974_CTRL3_FORCE);
253}
254
255static int ak8974_await_drdy(struct ak8974 *ak8974)
256{
257 int timeout = 2;
258 unsigned int val;
259 int ret;
260
261 if (ak8974->drdy_irq) {
262 ret = wait_for_completion_timeout(&ak8974->drdy_complete,
263 1 + msecs_to_jiffies(1000));
264 if (!ret) {
265 dev_err(&ak8974->i2c->dev,
266 "timeout waiting for DRDY IRQ\n");
267 return -ETIMEDOUT;
268 }
269 return 0;
270 }
271
272 /* Default delay-based poll loop */
273 do {
274 msleep(AK8974_MEASTIME);
275 ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
276 if (ret < 0)
277 return ret;
278 if (val & AK8974_STATUS_DRDY)
279 return 0;
280 } while (--timeout);
281 if (!timeout) {
282 dev_err(&ak8974->i2c->dev,
283 "timeout waiting for DRDY\n");
284 return -ETIMEDOUT;
285 }
286
287 return 0;
288}
289
290static int ak8974_getresult(struct ak8974 *ak8974, s16 *result)
291{
292 unsigned int src;
293 int ret;
294
295 ret = ak8974_await_drdy(ak8974);
296 if (ret)
297 return ret;
298 ret = regmap_read(ak8974->map, AK8974_INT_SRC, &src);
299 if (ret < 0)
300 return ret;
301
302 /* Out of range overflow! Strong magnet close? */
303 if (src & AK8974_INT_RANGE) {
304 dev_err(&ak8974->i2c->dev,
305 "range overflow in sensor\n");
306 return -ERANGE;
307 }
308
309 ret = regmap_bulk_read(ak8974->map, AK8974_DATA_X, result, 6);
310 if (ret)
311 return ret;
312
313 return ret;
314}
315
316static irqreturn_t ak8974_drdy_irq(int irq, void *d)
317{
318 struct ak8974 *ak8974 = d;
319
320 if (!ak8974->drdy_irq)
321 return IRQ_NONE;
322
323 /* TODO: timestamp here to get good measurement stamps */
324 return IRQ_WAKE_THREAD;
325}
326
327static irqreturn_t ak8974_drdy_irq_thread(int irq, void *d)
328{
329 struct ak8974 *ak8974 = d;
330 unsigned int val;
331 int ret;
332
333 /* Check if this was a DRDY from us */
334 ret = regmap_read(ak8974->map, AK8974_STATUS, &val);
335 if (ret < 0) {
336 dev_err(&ak8974->i2c->dev, "error reading DRDY status\n");
337 return IRQ_HANDLED;
338 }
339 if (val & AK8974_STATUS_DRDY) {
340 /* Yes this was our IRQ */
341 complete(&ak8974->drdy_complete);
342 return IRQ_HANDLED;
343 }
344
345 /* We may be on a shared IRQ, let the next client check */
346 return IRQ_NONE;
347}
348
349static int ak8974_selftest(struct ak8974 *ak8974)
350{
351 struct device *dev = &ak8974->i2c->dev;
352 unsigned int val;
353 int ret;
354
355 ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
356 if (ret)
357 return ret;
358 if (val != AK8974_SELFTEST_IDLE) {
359 dev_err(dev, "selftest not idle before test\n");
360 return -EIO;
361 }
362
363 /* Trigger self-test */
364 ret = regmap_update_bits(ak8974->map,
365 AK8974_CTRL3,
366 AK8974_CTRL3_SELFTEST,
367 AK8974_CTRL3_SELFTEST);
368 if (ret) {
369 dev_err(dev, "could not write CTRL3\n");
370 return ret;
371 }
372
373 msleep(AK8974_SELFTEST_DELAY);
374
375 ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
376 if (ret)
377 return ret;
378 if (val != AK8974_SELFTEST_OK) {
379 dev_err(dev, "selftest result NOT OK (%02x)\n", val);
380 return -EIO;
381 }
382
383 ret = regmap_read(ak8974->map, AK8974_SELFTEST, &val);
384 if (ret)
385 return ret;
386 if (val != AK8974_SELFTEST_IDLE) {
387 dev_err(dev, "selftest not idle after test (%02x)\n", val);
388 return -EIO;
389 }
390 dev_dbg(dev, "passed self-test\n");
391
392 return 0;
393}
394
395static int ak8974_get_u16_val(struct ak8974 *ak8974, u8 reg, u16 *val)
396{
397 int ret;
398 u16 bulk;
399
400 ret = regmap_bulk_read(ak8974->map, reg, &bulk, 2);
401 if (ret)
402 return ret;
403 *val = le16_to_cpu(bulk);
404
405 return 0;
406}
407
408static int ak8974_detect(struct ak8974 *ak8974)
409{
410 unsigned int whoami;
411 const char *name;
412 int ret;
413 unsigned int fw;
414 u16 sn;
415
416 ret = regmap_read(ak8974->map, AK8974_WHOAMI, &whoami);
417 if (ret)
418 return ret;
419
420 switch (whoami) {
421 case AK8974_WHOAMI_VALUE_AMI305:
422 name = "ami305";
423 ret = regmap_read(ak8974->map, AMI305_VER, &fw);
424 if (ret)
425 return ret;
426 fw &= 0x7f; /* only bits 0 thru 6 valid */
427 ret = ak8974_get_u16_val(ak8974, AMI305_SN, &sn);
428 if (ret)
429 return ret;
430 dev_info(&ak8974->i2c->dev,
431 "detected %s, FW ver %02x, S/N: %04x\n",
432 name, fw, sn);
433 break;
434 case AK8974_WHOAMI_VALUE_AK8974:
435 name = "ak8974";
436 dev_info(&ak8974->i2c->dev, "detected AK8974\n");
437 break;
438 default:
439 dev_err(&ak8974->i2c->dev, "unsupported device (%02x) ",
440 whoami);
441 return -ENODEV;
442 }
443
444 ak8974->name = name;
445 ak8974->variant = whoami;
446
447 return 0;
448}
449
450static int ak8974_read_raw(struct iio_dev *indio_dev,
451 struct iio_chan_spec const *chan,
452 int *val, int *val2,
453 long mask)
454{
455 struct ak8974 *ak8974 = iio_priv(indio_dev);
456 s16 hw_values[3];
457 int ret = -EINVAL;
458
459 pm_runtime_get_sync(&ak8974->i2c->dev);
460 mutex_lock(&ak8974->lock);
461
462 switch (mask) {
463 case IIO_CHAN_INFO_RAW:
464 if (chan->address > 2) {
465 dev_err(&ak8974->i2c->dev, "faulty channel address\n");
466 ret = -EIO;
467 goto out_unlock;
468 }
469 ret = ak8974_trigmeas(ak8974);
470 if (ret)
471 goto out_unlock;
472 ret = ak8974_getresult(ak8974, hw_values);
473 if (ret)
474 goto out_unlock;
475
476 /*
477 * We read all axes and discard all but one, for optimized
478 * reading, use the triggered buffer.
479 */
480 *val = le16_to_cpu(hw_values[chan->address]);
481
482 ret = IIO_VAL_INT;
483 }
484
485 out_unlock:
486 mutex_unlock(&ak8974->lock);
487 pm_runtime_mark_last_busy(&ak8974->i2c->dev);
488 pm_runtime_put_autosuspend(&ak8974->i2c->dev);
489
490 return ret;
491}
492
493static void ak8974_fill_buffer(struct iio_dev *indio_dev)
494{
495 struct ak8974 *ak8974 = iio_priv(indio_dev);
496 int ret;
497 s16 hw_values[8]; /* Three axes + 64bit padding */
498
499 pm_runtime_get_sync(&ak8974->i2c->dev);
500 mutex_lock(&ak8974->lock);
501
502 ret = ak8974_trigmeas(ak8974);
503 if (ret) {
504 dev_err(&ak8974->i2c->dev, "error triggering measure\n");
505 goto out_unlock;
506 }
507 ret = ak8974_getresult(ak8974, hw_values);
508 if (ret) {
509 dev_err(&ak8974->i2c->dev, "error getting measures\n");
510 goto out_unlock;
511 }
512
513 iio_push_to_buffers_with_timestamp(indio_dev, hw_values,
514 iio_get_time_ns(indio_dev));
515
516 out_unlock:
517 mutex_unlock(&ak8974->lock);
518 pm_runtime_mark_last_busy(&ak8974->i2c->dev);
519 pm_runtime_put_autosuspend(&ak8974->i2c->dev);
520}
521
522static irqreturn_t ak8974_handle_trigger(int irq, void *p)
523{
524 const struct iio_poll_func *pf = p;
525 struct iio_dev *indio_dev = pf->indio_dev;
526
527 ak8974_fill_buffer(indio_dev);
528 iio_trigger_notify_done(indio_dev->trig);
529
530 return IRQ_HANDLED;
531}
532
533static const struct iio_mount_matrix *
534ak8974_get_mount_matrix(const struct iio_dev *indio_dev,
535 const struct iio_chan_spec *chan)
536{
537 struct ak8974 *ak8974 = iio_priv(indio_dev);
538
539 return &ak8974->orientation;
540}
541
542static const struct iio_chan_spec_ext_info ak8974_ext_info[] = {
543 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8974_get_mount_matrix),
544 { },
545};
546
547#define AK8974_AXIS_CHANNEL(axis, index) \
548 { \
549 .type = IIO_MAGN, \
550 .modified = 1, \
551 .channel2 = IIO_MOD_##axis, \
552 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
553 .ext_info = ak8974_ext_info, \
554 .address = index, \
555 .scan_index = index, \
556 .scan_type = { \
557 .sign = 's', \
558 .realbits = 16, \
559 .storagebits = 16, \
560 .endianness = IIO_LE \
561 }, \
562 }
563
564static const struct iio_chan_spec ak8974_channels[] = {
565 AK8974_AXIS_CHANNEL(X, 0),
566 AK8974_AXIS_CHANNEL(Y, 1),
567 AK8974_AXIS_CHANNEL(Z, 2),
568 IIO_CHAN_SOFT_TIMESTAMP(3),
569};
570
571static const unsigned long ak8974_scan_masks[] = { 0x7, 0 };
572
573static const struct iio_info ak8974_info = {
574 .read_raw = &ak8974_read_raw,
575 .driver_module = THIS_MODULE,
576};
577
578static bool ak8974_writeable_reg(struct device *dev, unsigned int reg)
579{
580 struct i2c_client *i2c = to_i2c_client(dev);
581 struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
582 struct ak8974 *ak8974 = iio_priv(indio_dev);
583
584 switch (reg) {
585 case AK8974_CTRL1:
586 case AK8974_CTRL2:
587 case AK8974_CTRL3:
588 case AK8974_INT_CTRL:
589 case AK8974_INT_THRES:
590 case AK8974_INT_THRES + 1:
591 case AK8974_PRESET:
592 case AK8974_PRESET + 1:
593 return true;
594 case AK8974_OFFSET_X:
595 case AK8974_OFFSET_X + 1:
596 case AK8974_OFFSET_Y:
597 case AK8974_OFFSET_Y + 1:
598 case AK8974_OFFSET_Z:
599 case AK8974_OFFSET_Z + 1:
600 if (ak8974->variant == AK8974_WHOAMI_VALUE_AK8974)
601 return true;
602 return false;
603 case AMI305_OFFSET_X:
604 case AMI305_OFFSET_X + 1:
605 case AMI305_OFFSET_Y:
606 case AMI305_OFFSET_Y + 1:
607 case AMI305_OFFSET_Z:
608 case AMI305_OFFSET_Z + 1:
609 if (ak8974->variant == AK8974_WHOAMI_VALUE_AMI305)
610 return true;
611 return false;
612 default:
613 return false;
614 }
615}
616
617static const struct regmap_config ak8974_regmap_config = {
618 .reg_bits = 8,
619 .val_bits = 8,
620 .max_register = 0xff,
621 .writeable_reg = ak8974_writeable_reg,
622};
623
624static int ak8974_probe(struct i2c_client *i2c,
625 const struct i2c_device_id *id)
626{
627 struct iio_dev *indio_dev;
628 struct ak8974 *ak8974;
629 unsigned long irq_trig;
630 int irq = i2c->irq;
631 int ret;
632
633 /* Register with IIO */
634 indio_dev = devm_iio_device_alloc(&i2c->dev, sizeof(*ak8974));
635 if (indio_dev == NULL)
636 return -ENOMEM;
637
638 ak8974 = iio_priv(indio_dev);
639 i2c_set_clientdata(i2c, indio_dev);
640 ak8974->i2c = i2c;
641 mutex_init(&ak8974->lock);
642
643 ret = of_iio_read_mount_matrix(&i2c->dev,
644 "mount-matrix",
645 &ak8974->orientation);
646 if (ret)
647 return ret;
648
649 ak8974->regs[0].supply = ak8974_reg_avdd;
650 ak8974->regs[1].supply = ak8974_reg_dvdd;
651
652 ret = devm_regulator_bulk_get(&i2c->dev,
653 ARRAY_SIZE(ak8974->regs),
654 ak8974->regs);
655 if (ret < 0) {
656 dev_err(&i2c->dev, "cannot get regulators\n");
657 return ret;
658 }
659
660 ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
661 if (ret < 0) {
662 dev_err(&i2c->dev, "cannot enable regulators\n");
663 return ret;
664 }
665
666 /* Take runtime PM online */
667 pm_runtime_get_noresume(&i2c->dev);
668 pm_runtime_set_active(&i2c->dev);
669 pm_runtime_enable(&i2c->dev);
670
671 ak8974->map = devm_regmap_init_i2c(i2c, &ak8974_regmap_config);
672 if (IS_ERR(ak8974->map)) {
673 dev_err(&i2c->dev, "failed to allocate register map\n");
674 return PTR_ERR(ak8974->map);
675 }
676
677 ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
678 if (ret) {
679 dev_err(&i2c->dev, "could not power on\n");
680 goto power_off;
681 }
682
683 ret = ak8974_detect(ak8974);
684 if (ret) {
685 dev_err(&i2c->dev, "neither AK8974 nor AMI305 found\n");
686 goto power_off;
687 }
688
689 ret = ak8974_selftest(ak8974);
690 if (ret)
691 dev_err(&i2c->dev, "selftest failed (continuing anyway)\n");
692
693 ret = ak8974_reset(ak8974);
694 if (ret) {
695 dev_err(&i2c->dev, "AK8974 reset failed\n");
696 goto power_off;
697 }
698
699 pm_runtime_set_autosuspend_delay(&i2c->dev,
700 AK8974_AUTOSUSPEND_DELAY);
701 pm_runtime_use_autosuspend(&i2c->dev);
702 pm_runtime_put(&i2c->dev);
703
704 indio_dev->dev.parent = &i2c->dev;
705 indio_dev->channels = ak8974_channels;
706 indio_dev->num_channels = ARRAY_SIZE(ak8974_channels);
707 indio_dev->info = &ak8974_info;
708 indio_dev->available_scan_masks = ak8974_scan_masks;
709 indio_dev->modes = INDIO_DIRECT_MODE;
710 indio_dev->name = ak8974->name;
711
712 ret = iio_triggered_buffer_setup(indio_dev, NULL,
713 ak8974_handle_trigger,
714 NULL);
715 if (ret) {
716 dev_err(&i2c->dev, "triggered buffer setup failed\n");
717 goto disable_pm;
718 }
719
720 /* If we have a valid DRDY IRQ, make use of it */
721 if (irq > 0) {
722 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
723 if (irq_trig == IRQF_TRIGGER_RISING) {
724 dev_info(&i2c->dev, "enable rising edge DRDY IRQ\n");
725 } else if (irq_trig == IRQF_TRIGGER_FALLING) {
726 ak8974->drdy_active_low = true;
727 dev_info(&i2c->dev, "enable falling edge DRDY IRQ\n");
728 } else {
729 irq_trig = IRQF_TRIGGER_RISING;
730 }
731 irq_trig |= IRQF_ONESHOT;
732 irq_trig |= IRQF_SHARED;
733
734 ret = devm_request_threaded_irq(&i2c->dev,
735 irq,
736 ak8974_drdy_irq,
737 ak8974_drdy_irq_thread,
738 irq_trig,
739 ak8974->name,
740 ak8974);
741 if (ret) {
742 dev_err(&i2c->dev, "unable to request DRDY IRQ "
743 "- proceeding without IRQ\n");
744 goto no_irq;
745 }
746 ak8974->drdy_irq = true;
747 }
748
749no_irq:
750 ret = iio_device_register(indio_dev);
751 if (ret) {
752 dev_err(&i2c->dev, "device register failed\n");
753 goto cleanup_buffer;
754 }
755
756 return 0;
757
758cleanup_buffer:
759 iio_triggered_buffer_cleanup(indio_dev);
760disable_pm:
761 pm_runtime_put_noidle(&i2c->dev);
762 pm_runtime_disable(&i2c->dev);
763 ak8974_set_power(ak8974, AK8974_PWR_OFF);
764power_off:
765 regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
766
767 return ret;
768}
769
770static int __exit ak8974_remove(struct i2c_client *i2c)
771{
772 struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
773 struct ak8974 *ak8974 = iio_priv(indio_dev);
774
775 iio_device_unregister(indio_dev);
776 iio_triggered_buffer_cleanup(indio_dev);
777 pm_runtime_get_sync(&i2c->dev);
778 pm_runtime_put_noidle(&i2c->dev);
779 pm_runtime_disable(&i2c->dev);
780 ak8974_set_power(ak8974, AK8974_PWR_OFF);
781 regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
782
783 return 0;
784}
785
786static int __maybe_unused ak8974_runtime_suspend(struct device *dev)
787{
788 struct ak8974 *ak8974 =
789 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
790
791 ak8974_set_power(ak8974, AK8974_PWR_OFF);
792 regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
793
794 return 0;
795}
796
797static int __maybe_unused ak8974_runtime_resume(struct device *dev)
798{
799 struct ak8974 *ak8974 =
800 iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
801 int ret;
802
803 ret = regulator_bulk_enable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
804 if (ret)
805 return ret;
806 msleep(AK8974_POWERON_DELAY);
807 ret = ak8974_set_power(ak8974, AK8974_PWR_ON);
808 if (ret)
809 goto out_regulator_disable;
810
811 ret = ak8974_configure(ak8974);
812 if (ret)
813 goto out_disable_power;
814
815 return 0;
816
817out_disable_power:
818 ak8974_set_power(ak8974, AK8974_PWR_OFF);
819out_regulator_disable:
820 regulator_bulk_disable(ARRAY_SIZE(ak8974->regs), ak8974->regs);
821
822 return ret;
823}
824
825static const struct dev_pm_ops ak8974_dev_pm_ops = {
826 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
827 pm_runtime_force_resume)
828 SET_RUNTIME_PM_OPS(ak8974_runtime_suspend,
829 ak8974_runtime_resume, NULL)
830};
831
832static const struct i2c_device_id ak8974_id[] = {
833 {"ami305", 0 },
834 {"ak8974", 0 },
835 {}
836};
837MODULE_DEVICE_TABLE(i2c, ak8974_id);
838
839static const struct of_device_id ak8974_of_match[] = {
840 { .compatible = "asahi-kasei,ak8974", },
841 {}
842};
843MODULE_DEVICE_TABLE(of, ak8974_of_match);
844
845static struct i2c_driver ak8974_driver = {
846 .driver = {
847 .name = "ak8974",
848 .pm = &ak8974_dev_pm_ops,
849 .of_match_table = of_match_ptr(ak8974_of_match),
850 },
851 .probe = ak8974_probe,
852 .remove = __exit_p(ak8974_remove),
853 .id_table = ak8974_id,
854};
855module_i2c_driver(ak8974_driver);
856
857MODULE_DESCRIPTION("AK8974 and AMI305 3-axis magnetometer driver");
858MODULE_AUTHOR("Samu Onkalo");
859MODULE_AUTHOR("Linus Walleij");
860MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/magnetometer/mag3110.c b/drivers/iio/magnetometer/mag3110.c
index f2be4a049056..f2b3bd7bf862 100644
--- a/drivers/iio/magnetometer/mag3110.c
+++ b/drivers/iio/magnetometer/mag3110.c
@@ -154,34 +154,41 @@ static int mag3110_read_raw(struct iio_dev *indio_dev,
154 154
155 switch (mask) { 155 switch (mask) {
156 case IIO_CHAN_INFO_RAW: 156 case IIO_CHAN_INFO_RAW:
157 if (iio_buffer_enabled(indio_dev)) 157 ret = iio_device_claim_direct_mode(indio_dev);
158 return -EBUSY; 158 if (ret)
159 return ret;
159 160
160 switch (chan->type) { 161 switch (chan->type) {
161 case IIO_MAGN: /* in 0.1 uT / LSB */ 162 case IIO_MAGN: /* in 0.1 uT / LSB */
162 ret = mag3110_read(data, buffer); 163 ret = mag3110_read(data, buffer);
163 if (ret < 0) 164 if (ret < 0)
164 return ret; 165 goto release;
165 *val = sign_extend32( 166 *val = sign_extend32(
166 be16_to_cpu(buffer[chan->scan_index]), 15); 167 be16_to_cpu(buffer[chan->scan_index]), 15);
167 return IIO_VAL_INT; 168 ret = IIO_VAL_INT;
169 break;
168 case IIO_TEMP: /* in 1 C / LSB */ 170 case IIO_TEMP: /* in 1 C / LSB */
169 mutex_lock(&data->lock); 171 mutex_lock(&data->lock);
170 ret = mag3110_request(data); 172 ret = mag3110_request(data);
171 if (ret < 0) { 173 if (ret < 0) {
172 mutex_unlock(&data->lock); 174 mutex_unlock(&data->lock);
173 return ret; 175 goto release;
174 } 176 }
175 ret = i2c_smbus_read_byte_data(data->client, 177 ret = i2c_smbus_read_byte_data(data->client,
176 MAG3110_DIE_TEMP); 178 MAG3110_DIE_TEMP);
177 mutex_unlock(&data->lock); 179 mutex_unlock(&data->lock);
178 if (ret < 0) 180 if (ret < 0)
179 return ret; 181 goto release;
180 *val = sign_extend32(ret, 7); 182 *val = sign_extend32(ret, 7);
181 return IIO_VAL_INT; 183 ret = IIO_VAL_INT;
184 break;
182 default: 185 default:
183 return -EINVAL; 186 ret = -EINVAL;
184 } 187 }
188release:
189 iio_device_release_direct_mode(indio_dev);
190 return ret;
191
185 case IIO_CHAN_INFO_SCALE: 192 case IIO_CHAN_INFO_SCALE:
186 switch (chan->type) { 193 switch (chan->type) {
187 case IIO_MAGN: 194 case IIO_MAGN:
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 7fa65ab664fb..15cd416365c1 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -185,4 +185,26 @@ config HP206C
185 This driver can also be built as a module. If so, the module will 185 This driver can also be built as a module. If so, the module will
186 be called hp206c. 186 be called hp206c.
187 187
188config ZPA2326
189 tristate "Murata ZPA2326 pressure sensor driver"
190 select IIO_BUFFER
191 select IIO_TRIGGERED_BUFFER
192 select REGMAP
193 select ZPA2326_I2C if I2C
194 select ZPA2326_SPI if SPI_MASTER
195 help
196 Say Y here to build support for the Murata ZPA2326 pressure and
197 temperature sensor.
198
199 To compile this driver as a module, choose M here: the module will
200 be called zpa2326.
201
202config ZPA2326_I2C
203 tristate
204 select REGMAP_I2C
205
206config ZPA2326_SPI
207 tristate
208 select REGMAP_SPI
209
188endmenu 210endmenu
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index 7f395bed5e88..fff77185a5cc 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -22,6 +22,9 @@ st_pressure-y := st_pressure_core.o
22st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o 22st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
23obj-$(CONFIG_T5403) += t5403.o 23obj-$(CONFIG_T5403) += t5403.o
24obj-$(CONFIG_HP206C) += hp206c.o 24obj-$(CONFIG_HP206C) += hp206c.o
25obj-$(CONFIG_ZPA2326) += zpa2326.o
26obj-$(CONFIG_ZPA2326_I2C) += zpa2326_i2c.o
27obj-$(CONFIG_ZPA2326_SPI) += zpa2326_spi.o
25 28
26obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o 29obj-$(CONFIG_IIO_ST_PRESS_I2C) += st_pressure_i2c.o
27obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o 30obj-$(CONFIG_IIO_ST_PRESS_SPI) += st_pressure_spi.o
diff --git a/drivers/iio/pressure/ms5611_core.c b/drivers/iio/pressure/ms5611_core.c
index feb41f82c64a..a74ed1f0c880 100644
--- a/drivers/iio/pressure/ms5611_core.c
+++ b/drivers/iio/pressure/ms5611_core.c
@@ -416,8 +416,7 @@ static int ms5611_init(struct iio_dev *indio_dev)
416 return 0; 416 return 0;
417 417
418err_regulator_disable: 418err_regulator_disable:
419 if (!IS_ERR_OR_NULL(st->vdd)) 419 regulator_disable(st->vdd);
420 regulator_disable(st->vdd);
421 return ret; 420 return ret;
422} 421}
423 422
@@ -425,8 +424,7 @@ static void ms5611_fini(const struct iio_dev *indio_dev)
425{ 424{
426 const struct ms5611_state *st = iio_priv(indio_dev); 425 const struct ms5611_state *st = iio_priv(indio_dev);
427 426
428 if (!IS_ERR_OR_NULL(st->vdd)) 427 regulator_disable(st->vdd);
429 regulator_disable(st->vdd);
430} 428}
431 429
432int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, 430int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
diff --git a/drivers/iio/pressure/zpa2326.c b/drivers/iio/pressure/zpa2326.c
new file mode 100644
index 000000000000..19d2eb46fda6
--- /dev/null
+++ b/drivers/iio/pressure/zpa2326.c
@@ -0,0 +1,1735 @@
1/*
2 * Murata ZPA2326 pressure and temperature sensor IIO driver
3 *
4 * Copyright (c) 2016 Parrot S.A.
5 *
6 * Author: Gregor Boirie <gregor.boirie@parrot.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 *
12 * This program is distributed in the hope that 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
18/**
19 * DOC: ZPA2326 theory of operations
20 *
21 * This driver supports %INDIO_DIRECT_MODE and %INDIO_BUFFER_TRIGGERED IIO
22 * modes.
23 * A internal hardware trigger is also implemented to dispatch registered IIO
24 * trigger consumers upon "sample ready" interrupts.
25 *
26 * ZPA2326 hardware supports 2 sampling mode: one shot and continuous.
27 *
28 * A complete one shot sampling cycle gets device out of low power mode,
29 * performs pressure and temperature measurements, then automatically switches
30 * back to low power mode. It is meant for on demand sampling with optimal power
31 * saving at the cost of lower sampling rate and higher software overhead.
32 * This is a natural candidate for IIO read_raw hook implementation
33 * (%INDIO_DIRECT_MODE). It is also used for triggered buffering support to
34 * ensure explicit synchronization with external trigger events
35 * (%INDIO_BUFFER_TRIGGERED).
36 *
37 * The continuous mode works according to a periodic hardware measurement
38 * process continuously pushing samples into an internal hardware FIFO (for
39 * pressure samples only). Measurement cycle completion may be signaled by a
40 * "sample ready" interrupt.
41 * Typical software sequence of operations :
42 * - get device out of low power mode,
43 * - setup hardware sampling period,
44 * - at end of period, upon data ready interrupt: pop pressure samples out of
45 * hardware FIFO and fetch temperature sample
46 * - when no longer needed, stop sampling process by putting device into
47 * low power mode.
48 * This mode is used to implement %INDIO_BUFFER_TRIGGERED mode if device tree
49 * declares a valid interrupt line. In this case, the internal hardware trigger
50 * drives acquisition.
51 *
52 * Note that hardware sampling frequency is taken into account only when
53 * internal hardware trigger is attached as the highest sampling rate seems to
54 * be the most energy efficient.
55 *
56 * TODO:
57 * preset pressure threshold crossing / IIO events ;
58 * differential pressure sampling ;
59 * hardware samples averaging.
60 */
61
62#include <linux/module.h>
63#include <linux/kernel.h>
64#include <linux/delay.h>
65#include <linux/interrupt.h>
66#include <linux/regulator/consumer.h>
67#include <linux/pm_runtime.h>
68#include <linux/regmap.h>
69#include <linux/iio/iio.h>
70#include <linux/iio/sysfs.h>
71#include <linux/iio/buffer.h>
72#include <linux/iio/trigger.h>
73#include <linux/iio/trigger_consumer.h>
74#include <linux/iio/triggered_buffer.h>
75#include "zpa2326.h"
76
77/* 200 ms should be enough for the longest conversion time in one-shot mode. */
78#define ZPA2326_CONVERSION_JIFFIES (HZ / 5)
79
80/* There should be a 1 ms delay (Tpup) after getting out of reset. */
81#define ZPA2326_TPUP_USEC_MIN (1000)
82#define ZPA2326_TPUP_USEC_MAX (2000)
83
84/**
85 * struct zpa2326_frequency - Hardware sampling frequency descriptor
86 * @hz : Frequency in Hertz.
87 * @odr: Output Data Rate word as expected by %ZPA2326_CTRL_REG3_REG.
88 */
89struct zpa2326_frequency {
90 int hz;
91 u16 odr;
92};
93
94/*
95 * Keep these in strict ascending order: last array entry is expected to
96 * correspond to the highest sampling frequency.
97 */
98static const struct zpa2326_frequency zpa2326_sampling_frequencies[] = {
99 { .hz = 1, .odr = 1 << ZPA2326_CTRL_REG3_ODR_SHIFT },
100 { .hz = 5, .odr = 5 << ZPA2326_CTRL_REG3_ODR_SHIFT },
101 { .hz = 11, .odr = 6 << ZPA2326_CTRL_REG3_ODR_SHIFT },
102 { .hz = 23, .odr = 7 << ZPA2326_CTRL_REG3_ODR_SHIFT },
103};
104
105/* Return the highest hardware sampling frequency available. */
106static const struct zpa2326_frequency *zpa2326_highest_frequency(void)
107{
108 return &zpa2326_sampling_frequencies[
109 ARRAY_SIZE(zpa2326_sampling_frequencies) - 1];
110}
111
112/**
113 * struct zpa_private - Per-device internal private state
114 * @timestamp: Buffered samples ready datum.
115 * @regmap: Underlying I2C / SPI bus adapter used to abstract slave register
116 * accesses.
117 * @result: Allows sampling logic to get completion status of operations
118 * that interrupt handlers perform asynchronously.
119 * @data_ready: Interrupt handler uses this to wake user context up at sampling
120 * operation completion.
121 * @trigger: Optional hardware / interrupt driven trigger used to notify
122 * external devices a new sample is ready.
123 * @waken: Flag indicating whether or not device has just been powered on.
124 * @irq: Optional interrupt line: negative or zero if not declared into
125 * DT, in which case sampling logic keeps polling status register
126 * to detect completion.
127 * @frequency: Current hardware sampling frequency.
128 * @vref: Power / voltage reference.
129 * @vdd: Power supply.
130 */
131struct zpa2326_private {
132 s64 timestamp;
133 struct regmap *regmap;
134 int result;
135 struct completion data_ready;
136 struct iio_trigger *trigger;
137 bool waken;
138 int irq;
139 const struct zpa2326_frequency *frequency;
140 struct regulator *vref;
141 struct regulator *vdd;
142};
143
144#define zpa2326_err(_idev, _format, _arg...) \
145 dev_err(_idev->dev.parent, _format, ##_arg)
146
147#define zpa2326_warn(_idev, _format, _arg...) \
148 dev_warn(_idev->dev.parent, _format, ##_arg)
149
150#ifdef DEBUG
151#define zpa2326_dbg(_idev, _format, _arg...) \
152 dev_dbg(_idev->dev.parent, _format, ##_arg)
153#else
154#define zpa2326_dbg(_idev, _format, _arg...)
155#endif
156
157bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg)
158{
159 switch (reg) {
160 case ZPA2326_REF_P_XL_REG:
161 case ZPA2326_REF_P_L_REG:
162 case ZPA2326_REF_P_H_REG:
163 case ZPA2326_RES_CONF_REG:
164 case ZPA2326_CTRL_REG0_REG:
165 case ZPA2326_CTRL_REG1_REG:
166 case ZPA2326_CTRL_REG2_REG:
167 case ZPA2326_CTRL_REG3_REG:
168 case ZPA2326_THS_P_LOW_REG:
169 case ZPA2326_THS_P_HIGH_REG:
170 return true;
171
172 default:
173 return false;
174 }
175}
176EXPORT_SYMBOL_GPL(zpa2326_isreg_writeable);
177
178bool zpa2326_isreg_readable(struct device *dev, unsigned int reg)
179{
180 switch (reg) {
181 case ZPA2326_REF_P_XL_REG:
182 case ZPA2326_REF_P_L_REG:
183 case ZPA2326_REF_P_H_REG:
184 case ZPA2326_DEVICE_ID_REG:
185 case ZPA2326_RES_CONF_REG:
186 case ZPA2326_CTRL_REG0_REG:
187 case ZPA2326_CTRL_REG1_REG:
188 case ZPA2326_CTRL_REG2_REG:
189 case ZPA2326_CTRL_REG3_REG:
190 case ZPA2326_INT_SOURCE_REG:
191 case ZPA2326_THS_P_LOW_REG:
192 case ZPA2326_THS_P_HIGH_REG:
193 case ZPA2326_STATUS_REG:
194 case ZPA2326_PRESS_OUT_XL_REG:
195 case ZPA2326_PRESS_OUT_L_REG:
196 case ZPA2326_PRESS_OUT_H_REG:
197 case ZPA2326_TEMP_OUT_L_REG:
198 case ZPA2326_TEMP_OUT_H_REG:
199 return true;
200
201 default:
202 return false;
203 }
204}
205EXPORT_SYMBOL_GPL(zpa2326_isreg_readable);
206
207bool zpa2326_isreg_precious(struct device *dev, unsigned int reg)
208{
209 switch (reg) {
210 case ZPA2326_INT_SOURCE_REG:
211 case ZPA2326_PRESS_OUT_H_REG:
212 return true;
213
214 default:
215 return false;
216 }
217}
218EXPORT_SYMBOL_GPL(zpa2326_isreg_precious);
219
220/**
221 * zpa2326_enable_device() - Enable device, i.e. get out of low power mode.
222 * @indio_dev: The IIO device associated with the hardware to enable.
223 *
224 * Required to access complete register space and to perform any sampling
225 * or control operations.
226 *
227 * Return: Zero when successful, a negative error code otherwise.
228 */
229static int zpa2326_enable_device(const struct iio_dev *indio_dev)
230{
231 int err;
232
233 err = regmap_write(((struct zpa2326_private *)
234 iio_priv(indio_dev))->regmap,
235 ZPA2326_CTRL_REG0_REG, ZPA2326_CTRL_REG0_ENABLE);
236 if (err) {
237 zpa2326_err(indio_dev, "failed to enable device (%d)", err);
238 return err;
239 }
240
241 zpa2326_dbg(indio_dev, "enabled");
242
243 return 0;
244}
245
246/**
247 * zpa2326_sleep() - Disable device, i.e. switch to low power mode.
248 * @indio_dev: The IIO device associated with the hardware to disable.
249 *
250 * Only %ZPA2326_DEVICE_ID_REG and %ZPA2326_CTRL_REG0_REG registers may be
251 * accessed once device is in the disabled state.
252 *
253 * Return: Zero when successful, a negative error code otherwise.
254 */
255static int zpa2326_sleep(const struct iio_dev *indio_dev)
256{
257 int err;
258
259 err = regmap_write(((struct zpa2326_private *)
260 iio_priv(indio_dev))->regmap,
261 ZPA2326_CTRL_REG0_REG, 0);
262 if (err) {
263 zpa2326_err(indio_dev, "failed to sleep (%d)", err);
264 return err;
265 }
266
267 zpa2326_dbg(indio_dev, "sleeping");
268
269 return 0;
270}
271
272/**
273 * zpa2326_reset_device() - Reset device to default hardware state.
274 * @indio_dev: The IIO device associated with the hardware to reset.
275 *
276 * Disable sampling and empty hardware FIFO.
277 * Device must be enabled before reset, i.e. not in low power mode.
278 *
279 * Return: Zero when successful, a negative error code otherwise.
280 */
281static int zpa2326_reset_device(const struct iio_dev *indio_dev)
282{
283 int err;
284
285 err = regmap_write(((struct zpa2326_private *)
286 iio_priv(indio_dev))->regmap,
287 ZPA2326_CTRL_REG2_REG, ZPA2326_CTRL_REG2_SWRESET);
288 if (err) {
289 zpa2326_err(indio_dev, "failed to reset device (%d)", err);
290 return err;
291 }
292
293 usleep_range(ZPA2326_TPUP_USEC_MIN, ZPA2326_TPUP_USEC_MAX);
294
295 zpa2326_dbg(indio_dev, "reset");
296
297 return 0;
298}
299
300/**
301 * zpa2326_start_oneshot() - Start a single sampling cycle, i.e. in one shot
302 * mode.
303 * @indio_dev: The IIO device associated with the sampling hardware.
304 *
305 * Device must have been previously enabled and configured for one shot mode.
306 * Device will be switched back to low power mode at end of cycle.
307 *
308 * Return: Zero when successful, a negative error code otherwise.
309 */
310static int zpa2326_start_oneshot(const struct iio_dev *indio_dev)
311{
312 int err;
313
314 err = regmap_write(((struct zpa2326_private *)
315 iio_priv(indio_dev))->regmap,
316 ZPA2326_CTRL_REG0_REG,
317 ZPA2326_CTRL_REG0_ENABLE |
318 ZPA2326_CTRL_REG0_ONE_SHOT);
319 if (err) {
320 zpa2326_err(indio_dev, "failed to start one shot cycle (%d)",
321 err);
322 return err;
323 }
324
325 zpa2326_dbg(indio_dev, "one shot cycle started");
326
327 return 0;
328}
329
330/**
331 * zpa2326_power_on() - Power on device to allow subsequent configuration.
332 * @indio_dev: The IIO device associated with the sampling hardware.
333 * @private: Internal private state related to @indio_dev.
334 *
335 * Sampling will be disabled, preventing strange things from happening in our
336 * back. Hardware FIFO content will be cleared.
337 * When successful, device will be left in the enabled state to allow further
338 * configuration.
339 *
340 * Return: Zero when successful, a negative error code otherwise.
341 */
342static int zpa2326_power_on(const struct iio_dev *indio_dev,
343 const struct zpa2326_private *private)
344{
345 int err;
346
347 err = regulator_enable(private->vref);
348 if (err)
349 return err;
350
351 err = regulator_enable(private->vdd);
352 if (err)
353 goto vref;
354
355 zpa2326_dbg(indio_dev, "powered on");
356
357 err = zpa2326_enable_device(indio_dev);
358 if (err)
359 goto vdd;
360
361 err = zpa2326_reset_device(indio_dev);
362 if (err)
363 goto sleep;
364
365 return 0;
366
367sleep:
368 zpa2326_sleep(indio_dev);
369vdd:
370 regulator_disable(private->vdd);
371vref:
372 regulator_disable(private->vref);
373
374 zpa2326_dbg(indio_dev, "powered off");
375
376 return err;
377}
378
379/**
380 * zpa2326_power_off() - Power off device, i.e. disable attached power
381 * regulators.
382 * @indio_dev: The IIO device associated with the sampling hardware.
383 * @private: Internal private state related to @indio_dev.
384 *
385 * Return: Zero when successful, a negative error code otherwise.
386 */
387static void zpa2326_power_off(const struct iio_dev *indio_dev,
388 const struct zpa2326_private *private)
389{
390 regulator_disable(private->vdd);
391 regulator_disable(private->vref);
392
393 zpa2326_dbg(indio_dev, "powered off");
394}
395
396/**
397 * zpa2326_config_oneshot() - Setup device for one shot / on demand mode.
398 * @indio_dev: The IIO device associated with the sampling hardware.
399 * @irq: Optional interrupt line the hardware uses to notify new data
400 * samples are ready. Negative or zero values indicate no interrupts
401 * are available, meaning polling is required.
402 *
403 * Output Data Rate is configured for the highest possible rate so that
404 * conversion time and power consumption are reduced to a minimum.
405 * Note that hardware internal averaging machinery (not implemented in this
406 * driver) is not applicable in this mode.
407 *
408 * Device must have been previously enabled before calling
409 * zpa2326_config_oneshot().
410 *
411 * Return: Zero when successful, a negative error code otherwise.
412 */
413static int zpa2326_config_oneshot(const struct iio_dev *indio_dev,
414 int irq)
415{
416 struct regmap *regs = ((struct zpa2326_private *)
417 iio_priv(indio_dev))->regmap;
418 const struct zpa2326_frequency *freq = zpa2326_highest_frequency();
419 int err;
420
421 /* Setup highest available Output Data Rate for one shot mode. */
422 err = regmap_write(regs, ZPA2326_CTRL_REG3_REG, freq->odr);
423 if (err)
424 return err;
425
426 if (irq > 0) {
427 /* Request interrupt when new sample is available. */
428 err = regmap_write(regs, ZPA2326_CTRL_REG1_REG,
429 (u8)~ZPA2326_CTRL_REG1_MASK_DATA_READY);
430
431 if (err) {
432 dev_err(indio_dev->dev.parent,
433 "failed to setup one shot mode (%d)", err);
434 return err;
435 }
436 }
437
438 zpa2326_dbg(indio_dev, "one shot mode setup @%dHz", freq->hz);
439
440 return 0;
441}
442
443/**
444 * zpa2326_clear_fifo() - Clear remaining entries in hardware FIFO.
445 * @indio_dev: The IIO device associated with the sampling hardware.
446 * @min_count: Number of samples present within hardware FIFO.
447 *
448 * @min_count argument is a hint corresponding to the known minimum number of
449 * samples currently living in the FIFO. This allows to reduce the number of bus
450 * accesses by skipping status register read operation as long as we know for
451 * sure there are still entries left.
452 *
453 * Return: Zero when successful, a negative error code otherwise.
454 */
455static int zpa2326_clear_fifo(const struct iio_dev *indio_dev,
456 unsigned int min_count)
457{
458 struct regmap *regs = ((struct zpa2326_private *)
459 iio_priv(indio_dev))->regmap;
460 int err;
461 unsigned int val;
462
463 if (!min_count) {
464 /*
465 * No hint: read status register to determine whether FIFO is
466 * empty or not.
467 */
468 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
469
470 if (err < 0)
471 goto err;
472
473 if (val & ZPA2326_STATUS_FIFO_E)
474 /* Fifo is empty: nothing to trash. */
475 return 0;
476 }
477
478 /* Clear FIFO. */
479 do {
480 /*
481 * A single fetch from pressure MSB register is enough to pop
482 * values out of FIFO.
483 */
484 err = regmap_read(regs, ZPA2326_PRESS_OUT_H_REG, &val);
485 if (err < 0)
486 goto err;
487
488 if (min_count) {
489 /*
490 * We know for sure there are at least min_count entries
491 * left in FIFO. Skip status register read.
492 */
493 min_count--;
494 continue;
495 }
496
497 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
498 if (err < 0)
499 goto err;
500
501 } while (!(val & ZPA2326_STATUS_FIFO_E));
502
503 zpa2326_dbg(indio_dev, "FIFO cleared");
504
505 return 0;
506
507err:
508 zpa2326_err(indio_dev, "failed to clear FIFO (%d)", err);
509
510 return err;
511}
512
513/**
514 * zpa2326_dequeue_pressure() - Retrieve the most recent pressure sample from
515 * hardware FIFO.
516 * @indio_dev: The IIO device associated with the sampling hardware.
517 * @pressure: Sampled pressure output.
518 *
519 * Note that ZPA2326 hardware FIFO stores pressure samples only.
520 *
521 * Return: Zero when successful, a negative error code otherwise.
522 */
523static int zpa2326_dequeue_pressure(const struct iio_dev *indio_dev,
524 u32 *pressure)
525{
526 struct regmap *regs = ((struct zpa2326_private *)
527 iio_priv(indio_dev))->regmap;
528 unsigned int val;
529 int err;
530 int cleared = -1;
531
532 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
533 if (err < 0)
534 return err;
535
536 *pressure = 0;
537
538 if (val & ZPA2326_STATUS_P_OR) {
539 /*
540 * Fifo overrun : first sample dequeued from FIFO is the
541 * newest.
542 */
543 zpa2326_warn(indio_dev, "FIFO overflow");
544
545 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
546 3);
547 if (err)
548 return err;
549
550#define ZPA2326_FIFO_DEPTH (16U)
551 /* Hardware FIFO may hold no more than 16 pressure samples. */
552 return zpa2326_clear_fifo(indio_dev, ZPA2326_FIFO_DEPTH - 1);
553 }
554
555 /*
556 * Fifo has not overflown : retrieve newest sample. We need to pop
557 * values out until FIFO is empty : last fetched pressure is the newest.
558 * In nominal cases, we should find a single queued sample only.
559 */
560 do {
561 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, pressure,
562 3);
563 if (err)
564 return err;
565
566 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
567 if (err < 0)
568 return err;
569
570 cleared++;
571 } while (!(val & ZPA2326_STATUS_FIFO_E));
572
573 if (cleared)
574 /*
575 * Samples were pushed by hardware during previous rounds but we
576 * didn't consume them fast enough: inform user.
577 */
578 zpa2326_dbg(indio_dev, "cleared %d FIFO entries", cleared);
579
580 return 0;
581}
582
583/**
584 * zpa2326_fill_sample_buffer() - Enqueue new channel samples to IIO buffer.
585 * @indio_dev: The IIO device associated with the sampling hardware.
586 * @private: Internal private state related to @indio_dev.
587 *
588 * Return: Zero when successful, a negative error code otherwise.
589 */
590static int zpa2326_fill_sample_buffer(struct iio_dev *indio_dev,
591 const struct zpa2326_private *private)
592{
593 struct {
594 u32 pressure;
595 u16 temperature;
596 u64 timestamp;
597 } sample;
598 int err;
599
600 if (test_bit(0, indio_dev->active_scan_mask)) {
601 /* Get current pressure from hardware FIFO. */
602 err = zpa2326_dequeue_pressure(indio_dev, &sample.pressure);
603 if (err) {
604 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
605 err);
606 return err;
607 }
608 }
609
610 if (test_bit(1, indio_dev->active_scan_mask)) {
611 /* Get current temperature. */
612 err = regmap_bulk_read(private->regmap, ZPA2326_TEMP_OUT_L_REG,
613 &sample.temperature, 2);
614 if (err) {
615 zpa2326_warn(indio_dev,
616 "failed to fetch temperature (%d)", err);
617 return err;
618 }
619 }
620
621 /*
622 * Now push samples using timestamp stored either :
623 * - by hardware interrupt handler if interrupt is available: see
624 * zpa2326_handle_irq(),
625 * - or oneshot completion polling machinery : see
626 * zpa2326_trigger_handler().
627 */
628 zpa2326_dbg(indio_dev, "filling raw samples buffer");
629
630 iio_push_to_buffers_with_timestamp(indio_dev, &sample,
631 private->timestamp);
632
633 return 0;
634}
635
636#ifdef CONFIG_PM
637static int zpa2326_runtime_suspend(struct device *parent)
638{
639 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
640
641 if (pm_runtime_autosuspend_expiration(parent))
642 /* Userspace changed autosuspend delay. */
643 return -EAGAIN;
644
645 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
646
647 return 0;
648}
649
650static int zpa2326_runtime_resume(struct device *parent)
651{
652 const struct iio_dev *indio_dev = dev_get_drvdata(parent);
653
654 return zpa2326_power_on(indio_dev, iio_priv(indio_dev));
655}
656
657const struct dev_pm_ops zpa2326_pm_ops = {
658 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
659 pm_runtime_force_resume)
660 SET_RUNTIME_PM_OPS(zpa2326_runtime_suspend, zpa2326_runtime_resume,
661 NULL)
662};
663EXPORT_SYMBOL_GPL(zpa2326_pm_ops);
664
665/**
666 * zpa2326_resume() - Request the PM layer to power supply the device.
667 * @indio_dev: The IIO device associated with the sampling hardware.
668 *
669 * Return:
670 * < 0 - a negative error code meaning failure ;
671 * 0 - success, device has just been powered up ;
672 * 1 - success, device was already powered.
673 */
674static int zpa2326_resume(const struct iio_dev *indio_dev)
675{
676 int err;
677
678 err = pm_runtime_get_sync(indio_dev->dev.parent);
679 if (err < 0)
680 return err;
681
682 if (err > 0) {
683 /*
684 * Device was already power supplied: get it out of low power
685 * mode and inform caller.
686 */
687 zpa2326_enable_device(indio_dev);
688 return 1;
689 }
690
691 /* Inform caller device has just been brought back to life. */
692 return 0;
693}
694
695/**
696 * zpa2326_suspend() - Schedule a power down using autosuspend feature of PM
697 * layer.
698 * @indio_dev: The IIO device associated with the sampling hardware.
699 *
700 * Device is switched to low power mode at first to save power even when
701 * attached regulator is a "dummy" one.
702 */
703static void zpa2326_suspend(struct iio_dev *indio_dev)
704{
705 struct device *parent = indio_dev->dev.parent;
706
707 zpa2326_sleep(indio_dev);
708
709 pm_runtime_mark_last_busy(parent);
710 pm_runtime_put_autosuspend(parent);
711}
712
713static void zpa2326_init_runtime(struct device *parent)
714{
715 pm_runtime_get_noresume(parent);
716 pm_runtime_set_active(parent);
717 pm_runtime_enable(parent);
718 pm_runtime_set_autosuspend_delay(parent, 1000);
719 pm_runtime_use_autosuspend(parent);
720 pm_runtime_mark_last_busy(parent);
721 pm_runtime_put_autosuspend(parent);
722}
723
724static void zpa2326_fini_runtime(struct device *parent)
725{
726 pm_runtime_disable(parent);
727 pm_runtime_set_suspended(parent);
728}
729#else /* !CONFIG_PM */
730static int zpa2326_resume(const struct iio_dev *indio_dev)
731{
732 zpa2326_enable_device(indio_dev);
733
734 return 0;
735}
736
737static void zpa2326_suspend(struct iio_dev *indio_dev)
738{
739 zpa2326_sleep(indio_dev);
740}
741
742#define zpa2326_init_runtime(_parent)
743#define zpa2326_fini_runtime(_parent)
744#endif /* !CONFIG_PM */
745
746/**
747 * zpa2326_handle_irq() - Process hardware interrupts.
748 * @irq: Interrupt line the hardware uses to notify new data has arrived.
749 * @data: The IIO device associated with the sampling hardware.
750 *
751 * Timestamp buffered samples as soon as possible then schedule threaded bottom
752 * half.
753 *
754 * Return: Always successful.
755 */
756static irqreturn_t zpa2326_handle_irq(int irq, void *data)
757{
758 struct iio_dev *indio_dev = (struct iio_dev *)data;
759
760 if (iio_buffer_enabled(indio_dev)) {
761 /* Timestamping needed for buffered sampling only. */
762 ((struct zpa2326_private *)
763 iio_priv(indio_dev))->timestamp = iio_get_time_ns(indio_dev);
764 }
765
766 return IRQ_WAKE_THREAD;
767}
768
769/**
770 * zpa2326_handle_threaded_irq() - Interrupt bottom-half handler.
771 * @irq: Interrupt line the hardware uses to notify new data has arrived.
772 * @data: The IIO device associated with the sampling hardware.
773 *
774 * Mainly ensures interrupt is caused by a real "new sample available"
775 * condition. This relies upon the ability to perform blocking / sleeping bus
776 * accesses to slave's registers. This is why zpa2326_handle_threaded_irq() is
777 * called from within a thread, i.e. not called from hard interrupt context.
778 *
779 * When device is using its own internal hardware trigger in continuous sampling
780 * mode, data are available into hardware FIFO once interrupt has occurred. All
781 * we have to do is to dispatch the trigger, which in turn will fetch data and
782 * fill IIO buffer.
783 *
784 * When not using its own internal hardware trigger, the device has been
785 * configured in one-shot mode either by an external trigger or the IIO read_raw
786 * hook. This means one of the latter is currently waiting for sampling
787 * completion, in which case we must simply wake it up.
788 *
789 * See zpa2326_trigger_handler().
790 *
791 * Return:
792 * %IRQ_NONE - no consistent interrupt happened ;
793 * %IRQ_HANDLED - there was new samples available.
794 */
795static irqreturn_t zpa2326_handle_threaded_irq(int irq, void *data)
796{
797 struct iio_dev *indio_dev = (struct iio_dev *)data;
798 struct zpa2326_private *priv = iio_priv(indio_dev);
799 unsigned int val;
800 bool cont;
801 irqreturn_t ret = IRQ_NONE;
802
803 /*
804 * Are we using our own internal trigger in triggered buffer mode, i.e.,
805 * currently working in continuous sampling mode ?
806 */
807 cont = (iio_buffer_enabled(indio_dev) &&
808 iio_trigger_using_own(indio_dev));
809
810 /*
811 * Device works according to a level interrupt scheme: reading interrupt
812 * status de-asserts interrupt line.
813 */
814 priv->result = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
815 if (priv->result < 0) {
816 if (cont)
817 return IRQ_NONE;
818
819 goto complete;
820 }
821
822 /* Data ready is the only interrupt source we requested. */
823 if (!(val & ZPA2326_INT_SOURCE_DATA_READY)) {
824 /*
825 * Interrupt happened but no new sample available: likely caused
826 * by spurious interrupts, in which case, returning IRQ_NONE
827 * allows to benefit from the generic spurious interrupts
828 * handling.
829 */
830 zpa2326_warn(indio_dev, "unexpected interrupt status %02x",
831 val);
832
833 if (cont)
834 return IRQ_NONE;
835
836 priv->result = -ENODATA;
837 goto complete;
838 }
839
840 /* New sample available: dispatch internal trigger consumers. */
841 iio_trigger_poll_chained(priv->trigger);
842
843 if (cont)
844 /*
845 * Internal hardware trigger has been scheduled above : it will
846 * fetch data on its own.
847 */
848 return IRQ_HANDLED;
849
850 ret = IRQ_HANDLED;
851
852complete:
853 /*
854 * Wake up direct or externaly triggered buffer mode waiters: see
855 * zpa2326_sample_oneshot() and zpa2326_trigger_handler().
856 */
857 complete(&priv->data_ready);
858
859 return ret;
860}
861
862/**
863 * zpa2326_wait_oneshot_completion() - Wait for oneshot data ready interrupt.
864 * @indio_dev: The IIO device associated with the sampling hardware.
865 * @private: Internal private state related to @indio_dev.
866 *
867 * Return: Zero when successful, a negative error code otherwise.
868 */
869static int zpa2326_wait_oneshot_completion(const struct iio_dev *indio_dev,
870 struct zpa2326_private *private)
871{
872 int ret;
873 unsigned int val;
874
875 zpa2326_dbg(indio_dev, "waiting for one shot completion interrupt");
876
877 ret = wait_for_completion_interruptible_timeout(
878 &private->data_ready, ZPA2326_CONVERSION_JIFFIES);
879 if (ret > 0)
880 /*
881 * Interrupt handler completed before timeout: return operation
882 * status.
883 */
884 return private->result;
885
886 /* Clear all interrupts just to be sure. */
887 regmap_read(private->regmap, ZPA2326_INT_SOURCE_REG, &val);
888
889 if (!ret)
890 /* Timed out. */
891 ret = -ETIME;
892
893 if (ret != -ERESTARTSYS)
894 zpa2326_warn(indio_dev, "no one shot interrupt occurred (%d)",
895 ret);
896
897 return ret;
898}
899
900static int zpa2326_init_managed_irq(struct device *parent,
901 struct iio_dev *indio_dev,
902 struct zpa2326_private *private,
903 int irq)
904{
905 int err;
906
907 private->irq = irq;
908
909 if (irq <= 0) {
910 /*
911 * Platform declared no interrupt line: device will be polled
912 * for data availability.
913 */
914 dev_info(parent, "no interrupt found, running in polling mode");
915 return 0;
916 }
917
918 init_completion(&private->data_ready);
919
920 /* Request handler to be scheduled into threaded interrupt context. */
921 err = devm_request_threaded_irq(parent, irq, zpa2326_handle_irq,
922 zpa2326_handle_threaded_irq,
923 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
924 dev_name(parent), indio_dev);
925 if (err) {
926 dev_err(parent, "failed to request interrupt %d (%d)", irq,
927 err);
928 return err;
929 }
930
931 dev_info(parent, "using interrupt %d", irq);
932
933 return 0;
934}
935
936/**
937 * zpa2326_poll_oneshot_completion() - Actively poll for one shot data ready.
938 * @indio_dev: The IIO device associated with the sampling hardware.
939 *
940 * Loop over registers content to detect end of sampling cycle. Used when DT
941 * declared no valid interrupt lines.
942 *
943 * Return: Zero when successful, a negative error code otherwise.
944 */
945static int zpa2326_poll_oneshot_completion(const struct iio_dev *indio_dev)
946{
947 unsigned long tmout = jiffies + ZPA2326_CONVERSION_JIFFIES;
948 struct regmap *regs = ((struct zpa2326_private *)
949 iio_priv(indio_dev))->regmap;
950 unsigned int val;
951 int err;
952
953 zpa2326_dbg(indio_dev, "polling for one shot completion");
954
955 /*
956 * At least, 100 ms is needed for the device to complete its one-shot
957 * cycle.
958 */
959 if (msleep_interruptible(100))
960 return -ERESTARTSYS;
961
962 /* Poll for conversion completion in hardware. */
963 while (true) {
964 err = regmap_read(regs, ZPA2326_CTRL_REG0_REG, &val);
965 if (err < 0)
966 goto err;
967
968 if (!(val & ZPA2326_CTRL_REG0_ONE_SHOT))
969 /* One-shot bit self clears at conversion end. */
970 break;
971
972 if (time_after(jiffies, tmout)) {
973 /* Prevent from waiting forever : let's time out. */
974 err = -ETIME;
975 goto err;
976 }
977
978 usleep_range(10000, 20000);
979 }
980
981 /*
982 * In oneshot mode, pressure sample availability guarantees that
983 * temperature conversion has also completed : just check pressure
984 * status bit to keep things simple.
985 */
986 err = regmap_read(regs, ZPA2326_STATUS_REG, &val);
987 if (err < 0)
988 goto err;
989
990 if (!(val & ZPA2326_STATUS_P_DA)) {
991 /* No sample available. */
992 err = -ENODATA;
993 goto err;
994 }
995
996 return 0;
997
998err:
999 zpa2326_warn(indio_dev, "failed to poll one shot completion (%d)", err);
1000
1001 return err;
1002}
1003
1004/**
1005 * zpa2326_fetch_raw_sample() - Retrieve a raw sample and convert it to CPU
1006 * endianness.
1007 * @indio_dev: The IIO device associated with the sampling hardware.
1008 * @type: Type of measurement / channel to fetch from.
1009 * @value: Sample output.
1010 *
1011 * Return: Zero when successful, a negative error code otherwise.
1012 */
1013static int zpa2326_fetch_raw_sample(const struct iio_dev *indio_dev,
1014 enum iio_chan_type type,
1015 int *value)
1016{
1017 struct regmap *regs = ((struct zpa2326_private *)
1018 iio_priv(indio_dev))->regmap;
1019 int err;
1020
1021 switch (type) {
1022 case IIO_PRESSURE:
1023 zpa2326_dbg(indio_dev, "fetching raw pressure sample");
1024
1025 err = regmap_bulk_read(regs, ZPA2326_PRESS_OUT_XL_REG, value,
1026 3);
1027 if (err) {
1028 zpa2326_warn(indio_dev, "failed to fetch pressure (%d)",
1029 err);
1030 return err;
1031 }
1032
1033 /* Pressure is a 24 bits wide little-endian unsigned int. */
1034 *value = (((u8 *)value)[2] << 16) | (((u8 *)value)[1] << 8) |
1035 ((u8 *)value)[0];
1036
1037 return IIO_VAL_INT;
1038
1039 case IIO_TEMP:
1040 zpa2326_dbg(indio_dev, "fetching raw temperature sample");
1041
1042 err = regmap_bulk_read(regs, ZPA2326_TEMP_OUT_L_REG, value, 2);
1043 if (err) {
1044 zpa2326_warn(indio_dev,
1045 "failed to fetch temperature (%d)", err);
1046 return err;
1047 }
1048
1049 /* Temperature is a 16 bits wide little-endian signed int. */
1050 *value = (int)le16_to_cpup((__le16 *)value);
1051
1052 return IIO_VAL_INT;
1053
1054 default:
1055 return -EINVAL;
1056 }
1057}
1058
1059/**
1060 * zpa2326_sample_oneshot() - Perform a complete one shot sampling cycle.
1061 * @indio_dev: The IIO device associated with the sampling hardware.
1062 * @type: Type of measurement / channel to fetch from.
1063 * @value: Sample output.
1064 *
1065 * Return: Zero when successful, a negative error code otherwise.
1066 */
1067static int zpa2326_sample_oneshot(struct iio_dev *indio_dev,
1068 enum iio_chan_type type,
1069 int *value)
1070{
1071 int ret;
1072 struct zpa2326_private *priv;
1073
1074 ret = iio_device_claim_direct_mode(indio_dev);
1075 if (ret)
1076 return ret;
1077
1078 ret = zpa2326_resume(indio_dev);
1079 if (ret < 0)
1080 goto release;
1081
1082 priv = iio_priv(indio_dev);
1083
1084 if (ret > 0) {
1085 /*
1086 * We were already power supplied. Just clear hardware FIFO to
1087 * get rid of samples acquired during previous rounds (if any).
1088 * Sampling operation always generates both temperature and
1089 * pressure samples. The latter are always enqueued into
1090 * hardware FIFO. This may lead to situations were pressure
1091 * samples still sit into FIFO when previous cycle(s) fetched
1092 * temperature data only.
1093 * Hence, we need to clear hardware FIFO content to prevent from
1094 * getting outdated values at the end of current cycle.
1095 */
1096 if (type == IIO_PRESSURE) {
1097 ret = zpa2326_clear_fifo(indio_dev, 0);
1098 if (ret)
1099 goto suspend;
1100 }
1101 } else {
1102 /*
1103 * We have just been power supplied, i.e. device is in default
1104 * "out of reset" state, meaning we need to reconfigure it
1105 * entirely.
1106 */
1107 ret = zpa2326_config_oneshot(indio_dev, priv->irq);
1108 if (ret)
1109 goto suspend;
1110 }
1111
1112 /* Start a sampling cycle in oneshot mode. */
1113 ret = zpa2326_start_oneshot(indio_dev);
1114 if (ret)
1115 goto suspend;
1116
1117 /* Wait for sampling cycle to complete. */
1118 if (priv->irq > 0)
1119 ret = zpa2326_wait_oneshot_completion(indio_dev, priv);
1120 else
1121 ret = zpa2326_poll_oneshot_completion(indio_dev);
1122
1123 if (ret)
1124 goto suspend;
1125
1126 /* Retrieve raw sample value and convert it to CPU endianness. */
1127 ret = zpa2326_fetch_raw_sample(indio_dev, type, value);
1128
1129suspend:
1130 zpa2326_suspend(indio_dev);
1131release:
1132 iio_device_release_direct_mode(indio_dev);
1133
1134 return ret;
1135}
1136
1137/**
1138 * zpa2326_trigger_handler() - Perform an IIO buffered sampling round in one
1139 * shot mode.
1140 * @irq: The software interrupt assigned to @data
1141 * @data: The IIO poll function dispatched by external trigger our device is
1142 * attached to.
1143 *
1144 * Bottom-half handler called by the IIO trigger to which our device is
1145 * currently attached. Allows us to synchronize this device buffered sampling
1146 * either with external events (such as timer expiration, external device sample
1147 * ready, etc...) or with its own interrupt (internal hardware trigger).
1148 *
1149 * When using an external trigger, basically run the same sequence of operations
1150 * as for zpa2326_sample_oneshot() with the following hereafter. Hardware FIFO
1151 * is not cleared since already done at buffering enable time and samples
1152 * dequeueing always retrieves the most recent value.
1153 *
1154 * Otherwise, when internal hardware trigger has dispatched us, just fetch data
1155 * from hardware FIFO.
1156 *
1157 * Fetched data will pushed unprocessed to IIO buffer since samples conversion
1158 * is delegated to userspace in buffered mode (endianness, etc...).
1159 *
1160 * Return:
1161 * %IRQ_NONE - no consistent interrupt happened ;
1162 * %IRQ_HANDLED - there was new samples available.
1163 */
1164static irqreturn_t zpa2326_trigger_handler(int irq, void *data)
1165{
1166 struct iio_dev *indio_dev = ((struct iio_poll_func *)
1167 data)->indio_dev;
1168 struct zpa2326_private *priv = iio_priv(indio_dev);
1169 bool cont;
1170
1171 /*
1172 * We have been dispatched, meaning we are in triggered buffer mode.
1173 * Using our own internal trigger implies we are currently in continuous
1174 * hardware sampling mode.
1175 */
1176 cont = iio_trigger_using_own(indio_dev);
1177
1178 if (!cont) {
1179 /* On demand sampling : start a one shot cycle. */
1180 if (zpa2326_start_oneshot(indio_dev))
1181 goto out;
1182
1183 /* Wait for sampling cycle to complete. */
1184 if (priv->irq <= 0) {
1185 /* No interrupt available: poll for completion. */
1186 if (zpa2326_poll_oneshot_completion(indio_dev))
1187 goto out;
1188
1189 /* Only timestamp sample once it is ready. */
1190 priv->timestamp = iio_get_time_ns(indio_dev);
1191 } else {
1192 /* Interrupt handlers will timestamp for us. */
1193 if (zpa2326_wait_oneshot_completion(indio_dev, priv))
1194 goto out;
1195 }
1196 }
1197
1198 /* Enqueue to IIO buffer / userspace. */
1199 zpa2326_fill_sample_buffer(indio_dev, priv);
1200
1201out:
1202 if (!cont)
1203 /* Don't switch to low power if sampling continuously. */
1204 zpa2326_sleep(indio_dev);
1205
1206 /* Inform attached trigger we are done. */
1207 iio_trigger_notify_done(indio_dev->trig);
1208
1209 return IRQ_HANDLED;
1210}
1211
1212/**
1213 * zpa2326_preenable_buffer() - Prepare device for configuring triggered
1214 * sampling
1215 * modes.
1216 * @indio_dev: The IIO device associated with the sampling hardware.
1217 *
1218 * Basically power up device.
1219 * Called with IIO device's lock held.
1220 *
1221 * Return: Zero when successful, a negative error code otherwise.
1222 */
1223static int zpa2326_preenable_buffer(struct iio_dev *indio_dev)
1224{
1225 int ret = zpa2326_resume(indio_dev);
1226
1227 if (ret < 0)
1228 return ret;
1229
1230 /* Tell zpa2326_postenable_buffer() if we have just been powered on. */
1231 ((struct zpa2326_private *)
1232 iio_priv(indio_dev))->waken = iio_priv(indio_dev);
1233
1234 return 0;
1235}
1236
1237/**
1238 * zpa2326_postenable_buffer() - Configure device for triggered sampling.
1239 * @indio_dev: The IIO device associated with the sampling hardware.
1240 *
1241 * Basically setup one-shot mode if plugging external trigger.
1242 * Otherwise, let internal trigger configure continuous sampling :
1243 * see zpa2326_set_trigger_state().
1244 *
1245 * If an error is returned, IIO layer will call our postdisable hook for us,
1246 * i.e. no need to explicitly power device off here.
1247 * Called with IIO device's lock held.
1248 *
1249 * Called with IIO device's lock held.
1250 *
1251 * Return: Zero when successful, a negative error code otherwise.
1252 */
1253static int zpa2326_postenable_buffer(struct iio_dev *indio_dev)
1254{
1255 const struct zpa2326_private *priv = iio_priv(indio_dev);
1256 int err;
1257
1258 if (!priv->waken) {
1259 /*
1260 * We were already power supplied. Just clear hardware FIFO to
1261 * get rid of samples acquired during previous rounds (if any).
1262 */
1263 err = zpa2326_clear_fifo(indio_dev, 0);
1264 if (err)
1265 goto err;
1266 }
1267
1268 if (!iio_trigger_using_own(indio_dev) && priv->waken) {
1269 /*
1270 * We are using an external trigger and we have just been
1271 * powered up: reconfigure one-shot mode.
1272 */
1273 err = zpa2326_config_oneshot(indio_dev, priv->irq);
1274 if (err)
1275 goto err;
1276 }
1277
1278 /* Plug our own trigger event handler. */
1279 err = iio_triggered_buffer_postenable(indio_dev);
1280 if (err)
1281 goto err;
1282
1283 return 0;
1284
1285err:
1286 zpa2326_err(indio_dev, "failed to enable buffering (%d)", err);
1287
1288 return err;
1289}
1290
1291static int zpa2326_postdisable_buffer(struct iio_dev *indio_dev)
1292{
1293 zpa2326_suspend(indio_dev);
1294
1295 return 0;
1296}
1297
1298static const struct iio_buffer_setup_ops zpa2326_buffer_setup_ops = {
1299 .preenable = zpa2326_preenable_buffer,
1300 .postenable = zpa2326_postenable_buffer,
1301 .predisable = iio_triggered_buffer_predisable,
1302 .postdisable = zpa2326_postdisable_buffer
1303};
1304
1305/**
1306 * zpa2326_set_trigger_state() - Start / stop continuous sampling.
1307 * @trig: The trigger being attached to IIO device associated with the sampling
1308 * hardware.
1309 * @state: Tell whether to start (true) or stop (false)
1310 *
1311 * Basically enable / disable hardware continuous sampling mode.
1312 *
1313 * Called with IIO device's lock held at postenable() or predisable() time.
1314 *
1315 * Return: Zero when successful, a negative error code otherwise.
1316 */
1317static int zpa2326_set_trigger_state(struct iio_trigger *trig, bool state)
1318{
1319 const struct iio_dev *indio_dev = dev_get_drvdata(
1320 trig->dev.parent);
1321 const struct zpa2326_private *priv = iio_priv(indio_dev);
1322 int err;
1323
1324 if (!state) {
1325 /*
1326 * Switch trigger off : in case of failure, interrupt is left
1327 * disabled in order to prevent handler from accessing released
1328 * resources.
1329 */
1330 unsigned int val;
1331
1332 /*
1333 * As device is working in continuous mode, handlers may be
1334 * accessing resources we are currently freeing...
1335 * Prevent this by disabling interrupt handlers and ensure
1336 * the device will generate no more interrupts unless explicitly
1337 * required to, i.e. by restoring back to default one shot mode.
1338 */
1339 disable_irq(priv->irq);
1340
1341 /*
1342 * Disable continuous sampling mode to restore settings for
1343 * one shot / direct sampling operations.
1344 */
1345 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1346 zpa2326_highest_frequency()->odr);
1347 if (err)
1348 return err;
1349
1350 /*
1351 * Now that device won't generate interrupts on its own,
1352 * acknowledge any currently active interrupts (may happen on
1353 * rare occasions while stopping continuous mode).
1354 */
1355 err = regmap_read(priv->regmap, ZPA2326_INT_SOURCE_REG, &val);
1356 if (err < 0)
1357 return err;
1358
1359 /*
1360 * Re-enable interrupts only if we can guarantee the device will
1361 * generate no more interrupts to prevent handlers from
1362 * accessing released resources.
1363 */
1364 enable_irq(priv->irq);
1365
1366 zpa2326_dbg(indio_dev, "continuous mode stopped");
1367 } else {
1368 /*
1369 * Switch trigger on : start continuous sampling at required
1370 * frequency.
1371 */
1372
1373 if (priv->waken) {
1374 /* Enable interrupt if getting out of reset. */
1375 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG1_REG,
1376 (u8)
1377 ~ZPA2326_CTRL_REG1_MASK_DATA_READY);
1378 if (err)
1379 return err;
1380 }
1381
1382 /* Enable continuous sampling at specified frequency. */
1383 err = regmap_write(priv->regmap, ZPA2326_CTRL_REG3_REG,
1384 ZPA2326_CTRL_REG3_ENABLE_MEAS |
1385 priv->frequency->odr);
1386 if (err)
1387 return err;
1388
1389 zpa2326_dbg(indio_dev, "continuous mode setup @%dHz",
1390 priv->frequency->hz);
1391 }
1392
1393 return 0;
1394}
1395
1396static const struct iio_trigger_ops zpa2326_trigger_ops = {
1397 .owner = THIS_MODULE,
1398 .set_trigger_state = zpa2326_set_trigger_state,
1399};
1400
1401/**
1402 * zpa2326_init_trigger() - Create an interrupt driven / hardware trigger
1403 * allowing to notify external devices a new sample is
1404 * ready.
1405 * @parent: Hardware sampling device @indio_dev is a child of.
1406 * @indio_dev: The IIO device associated with the sampling hardware.
1407 * @private: Internal private state related to @indio_dev.
1408 * @irq: Optional interrupt line the hardware uses to notify new data
1409 * samples are ready. Negative or zero values indicate no interrupts
1410 * are available, meaning polling is required.
1411 *
1412 * Only relevant when DT declares a valid interrupt line.
1413 *
1414 * Return: Zero when successful, a negative error code otherwise.
1415 */
1416static int zpa2326_init_managed_trigger(struct device *parent,
1417 struct iio_dev *indio_dev,
1418 struct zpa2326_private *private,
1419 int irq)
1420{
1421 struct iio_trigger *trigger;
1422 int ret;
1423
1424 if (irq <= 0)
1425 return 0;
1426
1427 trigger = devm_iio_trigger_alloc(parent, "%s-dev%d",
1428 indio_dev->name, indio_dev->id);
1429 if (!trigger)
1430 return -ENOMEM;
1431
1432 /* Basic setup. */
1433 trigger->dev.parent = parent;
1434 trigger->ops = &zpa2326_trigger_ops;
1435
1436 private->trigger = trigger;
1437
1438 /* Register to triggers space. */
1439 ret = devm_iio_trigger_register(parent, trigger);
1440 if (ret)
1441 dev_err(parent, "failed to register hardware trigger (%d)",
1442 ret);
1443
1444 return ret;
1445}
1446
1447static int zpa2326_get_frequency(const struct iio_dev *indio_dev)
1448{
1449 return ((struct zpa2326_private *)iio_priv(indio_dev))->frequency->hz;
1450}
1451
1452static int zpa2326_set_frequency(struct iio_dev *indio_dev, int hz)
1453{
1454 struct zpa2326_private *priv = iio_priv(indio_dev);
1455 int freq;
1456 int err;
1457
1458 /* Check if requested frequency is supported. */
1459 for (freq = 0; freq < ARRAY_SIZE(zpa2326_sampling_frequencies); freq++)
1460 if (zpa2326_sampling_frequencies[freq].hz == hz)
1461 break;
1462 if (freq == ARRAY_SIZE(zpa2326_sampling_frequencies))
1463 return -EINVAL;
1464
1465 /* Don't allow changing frequency if buffered sampling is ongoing. */
1466 err = iio_device_claim_direct_mode(indio_dev);
1467 if (err)
1468 return err;
1469
1470 priv->frequency = &zpa2326_sampling_frequencies[freq];
1471
1472 iio_device_release_direct_mode(indio_dev);
1473
1474 return 0;
1475}
1476
1477/* Expose supported hardware sampling frequencies (Hz) through sysfs. */
1478static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1 5 11 23");
1479
1480static struct attribute *zpa2326_attributes[] = {
1481 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
1482 NULL
1483};
1484
1485static const struct attribute_group zpa2326_attribute_group = {
1486 .attrs = zpa2326_attributes,
1487};
1488
1489static int zpa2326_read_raw(struct iio_dev *indio_dev,
1490 struct iio_chan_spec const *chan,
1491 int *val,
1492 int *val2,
1493 long mask)
1494{
1495 switch (mask) {
1496 case IIO_CHAN_INFO_RAW:
1497 return zpa2326_sample_oneshot(indio_dev, chan->type, val);
1498
1499 case IIO_CHAN_INFO_SCALE:
1500 switch (chan->type) {
1501 case IIO_PRESSURE:
1502 /*
1503 * Pressure resolution is 1/64 Pascal. Scale to kPascal
1504 * as required by IIO ABI.
1505 */
1506 *val = 1;
1507 *val2 = 64000;
1508 return IIO_VAL_FRACTIONAL;
1509
1510 case IIO_TEMP:
1511 /*
1512 * Temperature follows the equation:
1513 * Temp[degC] = Tempcode * 0.00649 - 176.83
1514 * where:
1515 * Tempcode is composed the raw sampled 16 bits.
1516 *
1517 * Hence, to produce a temperature in milli-degrees
1518 * Celsius according to IIO ABI, we need to apply the
1519 * following equation to raw samples:
1520 * Temp[milli degC] = (Tempcode + Offset) * Scale
1521 * where:
1522 * Offset = -176.83 / 0.00649
1523 * Scale = 0.00649 * 1000
1524 */
1525 *val = 6;
1526 *val2 = 490000;
1527 return IIO_VAL_INT_PLUS_MICRO;
1528
1529 default:
1530 return -EINVAL;
1531 }
1532
1533 case IIO_CHAN_INFO_OFFSET:
1534 switch (chan->type) {
1535 case IIO_TEMP:
1536 *val = -17683000;
1537 *val2 = 649;
1538 return IIO_VAL_FRACTIONAL;
1539
1540 default:
1541 return -EINVAL;
1542 }
1543
1544 case IIO_CHAN_INFO_SAMP_FREQ:
1545 *val = zpa2326_get_frequency(indio_dev);
1546 return IIO_VAL_INT;
1547
1548 default:
1549 return -EINVAL;
1550 }
1551}
1552
1553static int zpa2326_write_raw(struct iio_dev *indio_dev,
1554 const struct iio_chan_spec *chan,
1555 int val,
1556 int val2,
1557 long mask)
1558{
1559 if ((mask != IIO_CHAN_INFO_SAMP_FREQ) || val2)
1560 return -EINVAL;
1561
1562 return zpa2326_set_frequency(indio_dev, val);
1563}
1564
1565static const struct iio_chan_spec zpa2326_channels[] = {
1566 [0] = {
1567 .type = IIO_PRESSURE,
1568 .scan_index = 0,
1569 .scan_type = {
1570 .sign = 'u',
1571 .realbits = 24,
1572 .storagebits = 32,
1573 .endianness = IIO_LE,
1574 },
1575 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1576 BIT(IIO_CHAN_INFO_SCALE),
1577 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1578 },
1579 [1] = {
1580 .type = IIO_TEMP,
1581 .scan_index = 1,
1582 .scan_type = {
1583 .sign = 's',
1584 .realbits = 16,
1585 .storagebits = 16,
1586 .endianness = IIO_LE,
1587 },
1588 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
1589 BIT(IIO_CHAN_INFO_SCALE) |
1590 BIT(IIO_CHAN_INFO_OFFSET),
1591 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
1592 },
1593 [2] = IIO_CHAN_SOFT_TIMESTAMP(2),
1594};
1595
1596static const struct iio_info zpa2326_info = {
1597 .driver_module = THIS_MODULE,
1598 .attrs = &zpa2326_attribute_group,
1599 .read_raw = zpa2326_read_raw,
1600 .write_raw = zpa2326_write_raw,
1601};
1602
1603static struct iio_dev *zpa2326_create_managed_iiodev(struct device *device,
1604 const char *name,
1605 struct regmap *regmap)
1606{
1607 struct iio_dev *indio_dev;
1608
1609 /* Allocate space to hold IIO device internal state. */
1610 indio_dev = devm_iio_device_alloc(device,
1611 sizeof(struct zpa2326_private));
1612 if (!indio_dev)
1613 return NULL;
1614
1615 /* Setup for userspace synchronous on demand sampling. */
1616 indio_dev->modes = INDIO_DIRECT_MODE;
1617 indio_dev->dev.parent = device;
1618 indio_dev->channels = zpa2326_channels;
1619 indio_dev->num_channels = ARRAY_SIZE(zpa2326_channels);
1620 indio_dev->name = name;
1621 indio_dev->info = &zpa2326_info;
1622
1623 return indio_dev;
1624}
1625
1626int zpa2326_probe(struct device *parent,
1627 const char *name,
1628 int irq,
1629 unsigned int hwid,
1630 struct regmap *regmap)
1631{
1632 struct iio_dev *indio_dev;
1633 struct zpa2326_private *priv;
1634 int err;
1635 unsigned int id;
1636
1637 indio_dev = zpa2326_create_managed_iiodev(parent, name, regmap);
1638 if (!indio_dev)
1639 return -ENOMEM;
1640
1641 priv = iio_priv(indio_dev);
1642
1643 priv->vref = devm_regulator_get(parent, "vref");
1644 if (IS_ERR(priv->vref))
1645 return PTR_ERR(priv->vref);
1646
1647 priv->vdd = devm_regulator_get(parent, "vdd");
1648 if (IS_ERR(priv->vdd))
1649 return PTR_ERR(priv->vdd);
1650
1651 /* Set default hardware sampling frequency to highest rate supported. */
1652 priv->frequency = zpa2326_highest_frequency();
1653
1654 /*
1655 * Plug device's underlying bus abstraction : this MUST be set before
1656 * registering interrupt handlers since an interrupt might happen if
1657 * power up sequence is not properly applied.
1658 */
1659 priv->regmap = regmap;
1660
1661 err = devm_iio_triggered_buffer_setup(parent, indio_dev, NULL,
1662 zpa2326_trigger_handler,
1663 &zpa2326_buffer_setup_ops);
1664 if (err)
1665 return err;
1666
1667 err = zpa2326_init_managed_trigger(parent, indio_dev, priv, irq);
1668 if (err)
1669 return err;
1670
1671 err = zpa2326_init_managed_irq(parent, indio_dev, priv, irq);
1672 if (err)
1673 return err;
1674
1675 /* Power up to check device ID and perform initial hardware setup. */
1676 err = zpa2326_power_on(indio_dev, priv);
1677 if (err)
1678 return err;
1679
1680 /* Read id register to check we are talking to the right slave. */
1681 err = regmap_read(regmap, ZPA2326_DEVICE_ID_REG, &id);
1682 if (err)
1683 goto sleep;
1684
1685 if (id != hwid) {
1686 dev_err(parent, "found device with unexpected id %02x", id);
1687 err = -ENODEV;
1688 goto sleep;
1689 }
1690
1691 err = zpa2326_config_oneshot(indio_dev, irq);
1692 if (err)
1693 goto sleep;
1694
1695 /* Setup done : go sleeping. Device will be awaken upon user request. */
1696 err = zpa2326_sleep(indio_dev);
1697 if (err)
1698 goto poweroff;
1699
1700 dev_set_drvdata(parent, indio_dev);
1701
1702 zpa2326_init_runtime(parent);
1703
1704 err = iio_device_register(indio_dev);
1705 if (err) {
1706 zpa2326_fini_runtime(parent);
1707 goto poweroff;
1708 }
1709
1710 return 0;
1711
1712sleep:
1713 /* Put to sleep just in case power regulators are "dummy" ones. */
1714 zpa2326_sleep(indio_dev);
1715poweroff:
1716 zpa2326_power_off(indio_dev, priv);
1717
1718 return err;
1719}
1720EXPORT_SYMBOL_GPL(zpa2326_probe);
1721
1722void zpa2326_remove(const struct device *parent)
1723{
1724 struct iio_dev *indio_dev = dev_get_drvdata(parent);
1725
1726 iio_device_unregister(indio_dev);
1727 zpa2326_fini_runtime(indio_dev->dev.parent);
1728 zpa2326_sleep(indio_dev);
1729 zpa2326_power_off(indio_dev, iio_priv(indio_dev));
1730}
1731EXPORT_SYMBOL_GPL(zpa2326_remove);
1732
1733MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
1734MODULE_DESCRIPTION("Core driver for Murata ZPA2326 pressure sensor");
1735MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/zpa2326.h b/drivers/iio/pressure/zpa2326.h
new file mode 100644
index 000000000000..05d3e1e3a449
--- /dev/null
+++ b/drivers/iio/pressure/zpa2326.h
@@ -0,0 +1,89 @@
1/*
2 * Murata ZPA2326 pressure and temperature sensor IIO driver
3 *
4 * Copyright (c) 2016 Parrot S.A.
5 *
6 * Author: Gregor Boirie <gregor.boirie@parrot.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 *
12 * This program is distributed in the hope that 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
18#ifndef _ZPA2326_H
19#define _ZPA2326_H
20
21/* Register map. */
22#define ZPA2326_REF_P_XL_REG (0x8)
23#define ZPA2326_REF_P_L_REG (0x9)
24#define ZPA2326_REF_P_H_REG (0xa)
25#define ZPA2326_DEVICE_ID_REG (0xf)
26#define ZPA2326_DEVICE_ID (0xb9)
27#define ZPA2326_RES_CONF_REG (0x10)
28#define ZPA2326_CTRL_REG0_REG (0x20)
29#define ZPA2326_CTRL_REG0_ONE_SHOT BIT(0)
30#define ZPA2326_CTRL_REG0_ENABLE BIT(1)
31#define ZPA2326_CTRL_REG1_REG (0x21)
32#define ZPA2326_CTRL_REG1_MASK_DATA_READY BIT(2)
33#define ZPA2326_CTRL_REG2_REG (0x22)
34#define ZPA2326_CTRL_REG2_SWRESET BIT(2)
35#define ZPA2326_CTRL_REG3_REG (0x23)
36#define ZPA2326_CTRL_REG3_ODR_SHIFT (4)
37#define ZPA2326_CTRL_REG3_ENABLE_MEAS BIT(7)
38#define ZPA2326_INT_SOURCE_REG (0x24)
39#define ZPA2326_INT_SOURCE_DATA_READY BIT(2)
40#define ZPA2326_THS_P_LOW_REG (0x25)
41#define ZPA2326_THS_P_HIGH_REG (0x26)
42#define ZPA2326_STATUS_REG (0x27)
43#define ZPA2326_STATUS_P_DA BIT(1)
44#define ZPA2326_STATUS_FIFO_E BIT(2)
45#define ZPA2326_STATUS_P_OR BIT(5)
46#define ZPA2326_PRESS_OUT_XL_REG (0x28)
47#define ZPA2326_PRESS_OUT_L_REG (0x29)
48#define ZPA2326_PRESS_OUT_H_REG (0x2a)
49#define ZPA2326_TEMP_OUT_L_REG (0x2b)
50#define ZPA2326_TEMP_OUT_H_REG (0x2c)
51
52struct device;
53struct regmap;
54
55bool zpa2326_isreg_writeable(struct device *dev, unsigned int reg);
56bool zpa2326_isreg_readable(struct device *dev, unsigned int reg);
57bool zpa2326_isreg_precious(struct device *dev, unsigned int reg);
58
59/**
60 * zpa2326_probe() - Instantiate and register core ZPA2326 IIO device
61 * @parent: Hardware sampling device the created IIO device will be a child of.
62 * @name: Arbitrary name to identify the device.
63 * @irq: Interrupt line, negative if none.
64 * @hwid: Expected device hardware id.
65 * @regmap: Registers map used to abstract underlying bus accesses.
66 *
67 * Return: Zero when successful, a negative error code otherwise.
68 */
69int zpa2326_probe(struct device *parent,
70 const char *name,
71 int irq,
72 unsigned int hwid,
73 struct regmap *regmap);
74
75/**
76 * zpa2326_remove() - Unregister and destroy core ZPA2326 IIO device.
77 * @parent: Hardware sampling device the IIO device to remove is a child of.
78 */
79void zpa2326_remove(const struct device *parent);
80
81#ifdef CONFIG_PM
82#include <linux/pm.h>
83extern const struct dev_pm_ops zpa2326_pm_ops;
84#define ZPA2326_PM_OPS (&zpa2326_pm_ops)
85#else
86#define ZPA2326_PM_OPS (NULL)
87#endif
88
89#endif
diff --git a/drivers/iio/pressure/zpa2326_i2c.c b/drivers/iio/pressure/zpa2326_i2c.c
new file mode 100644
index 000000000000..e4d27dd4493a
--- /dev/null
+++ b/drivers/iio/pressure/zpa2326_i2c.c
@@ -0,0 +1,99 @@
1/*
2 * Murata ZPA2326 I2C pressure and temperature sensor driver
3 *
4 * Copyright (c) 2016 Parrot S.A.
5 *
6 * Author: Gregor Boirie <gregor.boirie@parrot.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 *
12 * This program is distributed in the hope that 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
18#include <linux/module.h>
19#include <linux/regmap.h>
20#include <linux/i2c.h>
21#include <linux/of_device.h>
22#include "zpa2326.h"
23
24/*
25 * read_flag_mask:
26 * - address bit 7 must be set to request a register read operation
27 */
28static const struct regmap_config zpa2326_regmap_i2c_config = {
29 .reg_bits = 8,
30 .val_bits = 8,
31 .writeable_reg = zpa2326_isreg_writeable,
32 .readable_reg = zpa2326_isreg_readable,
33 .precious_reg = zpa2326_isreg_precious,
34 .max_register = ZPA2326_TEMP_OUT_H_REG,
35 .read_flag_mask = BIT(7),
36 .cache_type = REGCACHE_NONE,
37};
38
39static unsigned int zpa2326_i2c_hwid(const struct i2c_client *client)
40{
41#define ZPA2326_SA0(_addr) (_addr & BIT(0))
42#define ZPA2326_DEVICE_ID_SA0_SHIFT (1)
43
44 /* Identification register bit 1 mirrors device address bit 0. */
45 return (ZPA2326_DEVICE_ID |
46 (ZPA2326_SA0(client->addr) << ZPA2326_DEVICE_ID_SA0_SHIFT));
47}
48
49static int zpa2326_probe_i2c(struct i2c_client *client,
50 const struct i2c_device_id *i2c_id)
51{
52 struct regmap *regmap;
53
54 regmap = devm_regmap_init_i2c(client, &zpa2326_regmap_i2c_config);
55 if (IS_ERR(regmap)) {
56 dev_err(&client->dev, "failed to init registers map");
57 return PTR_ERR(regmap);
58 }
59
60 return zpa2326_probe(&client->dev, i2c_id->name, client->irq,
61 zpa2326_i2c_hwid(client), regmap);
62}
63
64static int zpa2326_remove_i2c(struct i2c_client *client)
65{
66 zpa2326_remove(&client->dev);
67
68 return 0;
69}
70
71static const struct i2c_device_id zpa2326_i2c_ids[] = {
72 { "zpa2326", 0 },
73 { },
74};
75MODULE_DEVICE_TABLE(i2c, zpa2326_i2c_ids);
76
77#if defined(CONFIG_OF)
78static const struct of_device_id zpa2326_i2c_matches[] = {
79 { .compatible = "murata,zpa2326" },
80 { }
81};
82MODULE_DEVICE_TABLE(of, zpa2326_i2c_matches);
83#endif
84
85static struct i2c_driver zpa2326_i2c_driver = {
86 .driver = {
87 .name = "zpa2326-i2c",
88 .of_match_table = of_match_ptr(zpa2326_i2c_matches),
89 .pm = ZPA2326_PM_OPS,
90 },
91 .probe = zpa2326_probe_i2c,
92 .remove = zpa2326_remove_i2c,
93 .id_table = zpa2326_i2c_ids,
94};
95module_i2c_driver(zpa2326_i2c_driver);
96
97MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
98MODULE_DESCRIPTION("I2C driver for Murata ZPA2326 pressure sensor");
99MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/pressure/zpa2326_spi.c b/drivers/iio/pressure/zpa2326_spi.c
new file mode 100644
index 000000000000..bd2c1c319fca
--- /dev/null
+++ b/drivers/iio/pressure/zpa2326_spi.c
@@ -0,0 +1,103 @@
1/*
2 * Murata ZPA2326 SPI pressure and temperature sensor driver
3 *
4 * Copyright (c) 2016 Parrot S.A.
5 *
6 * Author: Gregor Boirie <gregor.boirie@parrot.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 *
12 * This program is distributed in the hope that 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
18#include <linux/module.h>
19#include <linux/regmap.h>
20#include <linux/spi/spi.h>
21#include <linux/of_device.h>
22#include "zpa2326.h"
23
24/*
25 * read_flag_mask:
26 * - address bit 7 must be set to request a register read operation
27 * - address bit 6 must be set to request register address auto increment
28 */
29static const struct regmap_config zpa2326_regmap_spi_config = {
30 .reg_bits = 8,
31 .val_bits = 8,
32 .writeable_reg = zpa2326_isreg_writeable,
33 .readable_reg = zpa2326_isreg_readable,
34 .precious_reg = zpa2326_isreg_precious,
35 .max_register = ZPA2326_TEMP_OUT_H_REG,
36 .read_flag_mask = BIT(7) | BIT(6),
37 .cache_type = REGCACHE_NONE,
38};
39
40static int zpa2326_probe_spi(struct spi_device *spi)
41{
42 struct regmap *regmap;
43 int err;
44
45 regmap = devm_regmap_init_spi(spi, &zpa2326_regmap_spi_config);
46 if (IS_ERR(regmap)) {
47 dev_err(&spi->dev, "failed to init registers map");
48 return PTR_ERR(regmap);
49 }
50
51 /*
52 * Enforce SPI slave settings to prevent from DT misconfiguration.
53 *
54 * Clock is idle high. Sampling happens on trailing edge, i.e., rising
55 * edge. Maximum bus frequency is 1 MHz. Registers are 8 bits wide.
56 */
57 spi->mode = SPI_MODE_3;
58 spi->max_speed_hz = min(spi->max_speed_hz, 1000000U);
59 spi->bits_per_word = 8;
60 err = spi_setup(spi);
61 if (err < 0)
62 return err;
63
64 return zpa2326_probe(&spi->dev, spi_get_device_id(spi)->name,
65 spi->irq, ZPA2326_DEVICE_ID, regmap);
66}
67
68static int zpa2326_remove_spi(struct spi_device *spi)
69{
70 zpa2326_remove(&spi->dev);
71
72 return 0;
73}
74
75static const struct spi_device_id zpa2326_spi_ids[] = {
76 { "zpa2326", 0 },
77 { },
78};
79MODULE_DEVICE_TABLE(spi, zpa2326_spi_ids);
80
81#if defined(CONFIG_OF)
82static const struct of_device_id zpa2326_spi_matches[] = {
83 { .compatible = "murata,zpa2326" },
84 { }
85};
86MODULE_DEVICE_TABLE(of, zpa2326_spi_matches);
87#endif
88
89static struct spi_driver zpa2326_spi_driver = {
90 .driver = {
91 .name = "zpa2326-spi",
92 .of_match_table = of_match_ptr(zpa2326_spi_matches),
93 .pm = ZPA2326_PM_OPS,
94 },
95 .probe = zpa2326_probe_spi,
96 .remove = zpa2326_remove_spi,
97 .id_table = zpa2326_spi_ids,
98};
99module_spi_driver(zpa2326_spi_driver);
100
101MODULE_AUTHOR("Gregor Boirie <gregor.boirie@parrot.com>");
102MODULE_DESCRIPTION("SPI driver for Murata ZPA2326 pressure sensor");
103MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/proximity/sx9500.c b/drivers/iio/proximity/sx9500.c
index 1d74b3aafeed..1f06282ec793 100644
--- a/drivers/iio/proximity/sx9500.c
+++ b/drivers/iio/proximity/sx9500.c
@@ -516,7 +516,7 @@ static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
516 sx9500_push_events(indio_dev); 516 sx9500_push_events(indio_dev);
517 517
518 if (val & SX9500_CONVDONE_IRQ) 518 if (val & SX9500_CONVDONE_IRQ)
519 complete_all(&data->completion); 519 complete(&data->completion);
520 520
521out: 521out:
522 mutex_unlock(&data->mutex); 522 mutex_unlock(&data->mutex);
@@ -1025,6 +1025,12 @@ static const struct acpi_device_id sx9500_acpi_match[] = {
1025}; 1025};
1026MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match); 1026MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
1027 1027
1028static const struct of_device_id sx9500_of_match[] = {
1029 { .compatible = "semtech,sx9500", },
1030 { }
1031};
1032MODULE_DEVICE_TABLE(of, sx9500_of_match);
1033
1028static const struct i2c_device_id sx9500_id[] = { 1034static const struct i2c_device_id sx9500_id[] = {
1029 {"sx9500", 0}, 1035 {"sx9500", 0},
1030 { }, 1036 { },
@@ -1035,6 +1041,7 @@ static struct i2c_driver sx9500_driver = {
1035 .driver = { 1041 .driver = {
1036 .name = SX9500_DRIVER_NAME, 1042 .name = SX9500_DRIVER_NAME,
1037 .acpi_match_table = ACPI_PTR(sx9500_acpi_match), 1043 .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
1044 .of_match_table = of_match_ptr(sx9500_of_match),
1038 .pm = &sx9500_pm_ops, 1045 .pm = &sx9500_pm_ops,
1039 }, 1046 },
1040 .probe = sx9500_probe, 1047 .probe = sx9500_probe,
diff --git a/drivers/iio/temperature/Kconfig b/drivers/iio/temperature/Kconfig
index c4664e5de791..5ea77a7e261d 100644
--- a/drivers/iio/temperature/Kconfig
+++ b/drivers/iio/temperature/Kconfig
@@ -3,6 +3,22 @@
3# 3#
4menu "Temperature sensors" 4menu "Temperature sensors"
5 5
6config MAXIM_THERMOCOUPLE
7 tristate "Maxim thermocouple sensors"
8 depends on SPI
9 select IIO_BUFFER
10 select IIO_TRIGGERED_BUFFER
11 help
12 If you say yes here you get support for the Maxim series of
13 thermocouple sensors connected via SPI.
14
15 Supported sensors:
16 * MAX6675
17 * MAX31855
18
19 This driver can also be built as a module. If so, the module will
20 be called maxim_thermocouple.
21
6config MLX90614 22config MLX90614
7 tristate "MLX90614 contact-less infrared sensor" 23 tristate "MLX90614 contact-less infrared sensor"
8 depends on I2C 24 depends on I2C
diff --git a/drivers/iio/temperature/Makefile b/drivers/iio/temperature/Makefile
index 02bc79d49b24..78c3de0dc3f0 100644
--- a/drivers/iio/temperature/Makefile
+++ b/drivers/iio/temperature/Makefile
@@ -2,6 +2,7 @@
2# Makefile for industrial I/O temperature drivers 2# Makefile for industrial I/O temperature drivers
3# 3#
4 4
5obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.o
5obj-$(CONFIG_MLX90614) += mlx90614.o 6obj-$(CONFIG_MLX90614) += mlx90614.o
6obj-$(CONFIG_TMP006) += tmp006.o 7obj-$(CONFIG_TMP006) += tmp006.o
7obj-$(CONFIG_TSYS01) += tsys01.o 8obj-$(CONFIG_TSYS01) += tsys01.o
diff --git a/drivers/iio/temperature/maxim_thermocouple.c b/drivers/iio/temperature/maxim_thermocouple.c
new file mode 100644
index 000000000000..39dd2026ccc9
--- /dev/null
+++ b/drivers/iio/temperature/maxim_thermocouple.c
@@ -0,0 +1,281 @@
1/*
2 * maxim_thermocouple.c - Support for Maxim thermocouple chips
3 *
4 * Copyright (C) 2016 Matt Ranostay <mranostay@gmail.com>
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 as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/spi/spi.h>
22#include <linux/iio/iio.h>
23#include <linux/iio/trigger.h>
24#include <linux/iio/buffer.h>
25#include <linux/iio/triggered_buffer.h>
26#include <linux/iio/trigger_consumer.h>
27
28#define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
29
30enum {
31 MAX6675,
32 MAX31855,
33};
34
35static const struct iio_chan_spec max6675_channels[] = {
36 { /* thermocouple temperature */
37 .type = IIO_TEMP,
38 .info_mask_separate =
39 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
40 .scan_index = 0,
41 .scan_type = {
42 .sign = 's',
43 .realbits = 13,
44 .storagebits = 16,
45 .shift = 3,
46 .endianness = IIO_BE,
47 },
48 },
49 IIO_CHAN_SOFT_TIMESTAMP(1),
50};
51
52static const struct iio_chan_spec max31855_channels[] = {
53 { /* thermocouple temperature */
54 .type = IIO_TEMP,
55 .address = 2,
56 .info_mask_separate =
57 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
58 .scan_index = 0,
59 .scan_type = {
60 .sign = 's',
61 .realbits = 14,
62 .storagebits = 16,
63 .shift = 2,
64 .endianness = IIO_BE,
65 },
66 },
67 { /* cold junction temperature */
68 .type = IIO_TEMP,
69 .address = 0,
70 .channel2 = IIO_MOD_TEMP_AMBIENT,
71 .modified = 1,
72 .info_mask_separate =
73 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
74 .scan_index = 1,
75 .scan_type = {
76 .sign = 's',
77 .realbits = 12,
78 .storagebits = 16,
79 .shift = 4,
80 .endianness = IIO_BE,
81 },
82 },
83 IIO_CHAN_SOFT_TIMESTAMP(2),
84};
85
86static const unsigned long max31855_scan_masks[] = {0x3, 0};
87
88struct maxim_thermocouple_chip {
89 const struct iio_chan_spec *channels;
90 const unsigned long *scan_masks;
91 u8 num_channels;
92 u8 read_size;
93
94 /* bit-check for valid input */
95 u32 status_bit;
96};
97
98static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
99 [MAX6675] = {
100 .channels = max6675_channels,
101 .num_channels = ARRAY_SIZE(max6675_channels),
102 .read_size = 2,
103 .status_bit = BIT(2),
104 },
105 [MAX31855] = {
106 .channels = max31855_channels,
107 .num_channels = ARRAY_SIZE(max31855_channels),
108 .read_size = 4,
109 .scan_masks = max31855_scan_masks,
110 .status_bit = BIT(16),
111 },
112};
113
114struct maxim_thermocouple_data {
115 struct spi_device *spi;
116 const struct maxim_thermocouple_chip *chip;
117
118 u8 buffer[16] ____cacheline_aligned;
119};
120
121static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
122 struct iio_chan_spec const *chan, int *val)
123{
124 unsigned int storage_bytes = data->chip->read_size;
125 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
126 unsigned int buf;
127 int ret;
128
129 ret = spi_read(data->spi, (void *) &buf, storage_bytes);
130 if (ret)
131 return ret;
132
133 switch (storage_bytes) {
134 case 2:
135 *val = be16_to_cpu(buf);
136 break;
137 case 4:
138 *val = be32_to_cpu(buf);
139 break;
140 }
141
142 /* check to be sure this is a valid reading */
143 if (*val & data->chip->status_bit)
144 return -EINVAL;
145
146 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
147
148 return 0;
149}
150
151static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
152{
153 struct iio_poll_func *pf = private;
154 struct iio_dev *indio_dev = pf->indio_dev;
155 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
156 int ret;
157
158 ret = spi_read(data->spi, data->buffer, data->chip->read_size);
159 if (!ret) {
160 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
161 iio_get_time_ns(indio_dev));
162 }
163
164 iio_trigger_notify_done(indio_dev->trig);
165
166 return IRQ_HANDLED;
167}
168
169static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
170 struct iio_chan_spec const *chan,
171 int *val, int *val2, long mask)
172{
173 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
174 int ret = -EINVAL;
175
176 switch (mask) {
177 case IIO_CHAN_INFO_RAW:
178 ret = iio_device_claim_direct_mode(indio_dev);
179 if (ret)
180 return ret;
181
182 ret = maxim_thermocouple_read(data, chan, val);
183 iio_device_release_direct_mode(indio_dev);
184
185 if (!ret)
186 return IIO_VAL_INT;
187
188 break;
189 case IIO_CHAN_INFO_SCALE:
190 switch (chan->channel2) {
191 case IIO_MOD_TEMP_AMBIENT:
192 *val = 62;
193 *val2 = 500000; /* 1000 * 0.0625 */
194 ret = IIO_VAL_INT_PLUS_MICRO;
195 break;
196 default:
197 *val = 250; /* 1000 * 0.25 */
198 ret = IIO_VAL_INT;
199 };
200 break;
201 }
202
203 return ret;
204}
205
206static const struct iio_info maxim_thermocouple_info = {
207 .driver_module = THIS_MODULE,
208 .read_raw = maxim_thermocouple_read_raw,
209};
210
211static int maxim_thermocouple_probe(struct spi_device *spi)
212{
213 const struct spi_device_id *id = spi_get_device_id(spi);
214 struct iio_dev *indio_dev;
215 struct maxim_thermocouple_data *data;
216 const struct maxim_thermocouple_chip *chip =
217 &maxim_thermocouple_chips[id->driver_data];
218 int ret;
219
220 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
221 if (!indio_dev)
222 return -ENOMEM;
223
224 indio_dev->info = &maxim_thermocouple_info;
225 indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
226 indio_dev->channels = chip->channels;
227 indio_dev->available_scan_masks = chip->scan_masks;
228 indio_dev->num_channels = chip->num_channels;
229 indio_dev->modes = INDIO_DIRECT_MODE;
230
231 data = iio_priv(indio_dev);
232 data->spi = spi;
233 data->chip = chip;
234
235 ret = iio_triggered_buffer_setup(indio_dev, NULL,
236 maxim_thermocouple_trigger_handler, NULL);
237 if (ret)
238 return ret;
239
240 ret = iio_device_register(indio_dev);
241 if (ret)
242 goto error_unreg_buffer;
243
244 return 0;
245
246error_unreg_buffer:
247 iio_triggered_buffer_cleanup(indio_dev);
248
249 return ret;
250}
251
252static int maxim_thermocouple_remove(struct spi_device *spi)
253{
254 struct iio_dev *indio_dev = spi_get_drvdata(spi);
255
256 iio_device_unregister(indio_dev);
257 iio_triggered_buffer_cleanup(indio_dev);
258
259 return 0;
260}
261
262static const struct spi_device_id maxim_thermocouple_id[] = {
263 {"max6675", MAX6675},
264 {"max31855", MAX31855},
265 {},
266};
267MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
268
269static struct spi_driver maxim_thermocouple_driver = {
270 .driver = {
271 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
272 },
273 .probe = maxim_thermocouple_probe,
274 .remove = maxim_thermocouple_remove,
275 .id_table = maxim_thermocouple_id,
276};
277module_spi_driver(maxim_thermocouple_driver);
278
279MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
280MODULE_DESCRIPTION("Maxim thermocouple sensors");
281MODULE_LICENSE("GPL");