aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2015-01-20 21:13:37 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2015-01-20 21:13:37 -0500
commit6e49557b316e8db983a6d40d18e88ab0ba8571ef (patch)
treeded6ad6cdaace89769d7a3464fd0170f4d4471b8 /drivers/iio
parente60088575bcbaf8d71a0e9f1d44fbf136f95e2cd (diff)
parentdf1fad80dfa0a7644b525c4a0dd8f2be21f57358 (diff)
Merge tag 'iio-for-3.20a_take2' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio into staging-testing
Jonathan writes: First round of IIO new drivers, cleanups and functionality for the 3.20 cycle take 2 Updated pull request with Daniel's fix on top for the power management Kconfig changes that had snuck in since last update of the IIO tree worked it's way through from mainline. Original pull message New device support * jsa1212 proxmity / ambient light sensor * SM08500 supported added to the kxcjk-1013 accelerometer driver * KMX61 Accelerometer/Magnetometer. This took a somewhat rocky path being first merged, then reverted for a rewrite after a discussion of how to support additional functionality and finally being merged prior to some last reviews coming in, with resultant follow up patches. * Freescale mma9551l driver (minor follow up warning supression patch). * Semtech SX9500 proximity device driver. * ak8975 gains support for ak09911 and ak09912 and drop the standalone driver for the ak09911. New functionality * Dummy driver gains some virtual registers making it more flexible. * IIO_ACTIVITY channel types, with modifiers running, walking etc. This is to support on chip motion clasifiers. As such it is in the form of a confidence percentage. The only devices so far only do binary decisions but this gives us room when other devices give more nuanced clasification. * IIO_EV_DIR_NONE type for events where there is no obvious direction. First case is step detection. * IIO_STEPS channel type for pedometers. * ENABLE mask element used to control turning on counting types such as the pedometer that need a 'start point'. * INSTANCE event type to support things that happen once. * info element for height calibration (used in various motion estimation algorithms). Note heigh tof use * dummy driver demonstration of the use of all the new bits above. * event monitor support for the new events. * inv_mpu6050 gains an i2c mux to allow bypassing the device to access additional devices connected on the other side of it. Note that in Windows these are handled by firmware on the device and not exposed directly. * inv_mpu6050 gains ACPI enumeration. * inkern interface gains iio_write_channel_raw to allow in kernel users of DAC functionality via a simple wrapper. * Document input current readings in the ABI docs. * Add an error message when we get an out of range error in device tree processing for the in kernel interfaces. Basically a device tree debugging aid. * Add a sanity check that a scan index for a channel is unique during registration. There to help catch bugs as this should never happen in a bug free driver. Cleanups and fixlets A rework of buffer registration from Lars - a precursor to some other upcoming new stuff (a few patches from others rolled in here as well). * Ensure all drivers register the same channels for the device and buffer. * Move buffer registration into the core rather than using the old two step approach. Now we have simple ways of using a unified set channels for both without requiring channels be exposed by both interface, this removes a fair bit of boilerplate. * Stop sca3000 and ad5933 (both in staging) enabling buffer channels by default. It has long be convention in IIO to startup with no channels enabled and leave it up to userspace to say what goes in the buffer. Getting rid of these allows us to drop export of iio_scan_mask_set. * Drop get_bytes_per_datum from iio_buffer_access_funcs as not been used for a while. * Allocate standard buffer attributes in the core rather than in every driver with a buffer. * Make the length attribute read only when a driver is not able to set the length. * Drop the get_length callback for buffers as it is already available in struct iio_buffer. * Drop an unused arguement form iio_kfifo_allocate and add devm allocator for it. * some kconfig entries gain anotation with the resulting module name. * Fix a resulting compile issue in dummy driver due to a stub taking wrong parameters as a result of the above rework. * Fix an off by 2 error in copying the core assigned buffer attributes. Other cleanups, * Trivial space before comma fixups. * ak8975 fixlets - none critical. Rework to allow more device support. * Drop unnecessary sizeof(u8) calls. * bmp280 - refactor the compensation code to reduce copy operations and code length. A second patch futher optimized this and performed some other minor cleanups. * kxcjk-1013 - various power control cleanups to avoid unnecessary enable / disable of device. Make sure it is only controlled at all if CONFIG_PM is enabled. Also som cleanups of error paths. * Small cleanups in adf4530 driver - pointless message and unnecessary braces. * Clarifiy the proximity ABI docs to make it clear it should get bigger as we move futher away. * Drop a misleading comment form industrialio-core.c * Trivial white space cleanups. * sca3000 looses an unused debug function. * Fix char unsigned ordering in ad8366 * Increase the sleep time in ad9523 to make it predictable (value didn't really matter so make it more than 20 msecs) * mxs-lradc touchscreen property cleanups in device tree are fixed to ensure the meet all the 'interesting' documentation. * A couple of cleanups for the staging ad5933 driver to avoid unnecessary conversion to a processed temperature vlaue in kernel and remove platform data form the state structure as not needed after probe. * Fix a wrong scale factor in the docs. Misc * Add IIO include files to the maintainers entry.
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/accel/Kconfig16
-rw-r--r--drivers/iio/accel/Makefile1
-rw-r--r--drivers/iio/accel/kxcjk-1013.c52
-rw-r--r--drivers/iio/accel/mma8452.c2
-rw-r--r--drivers/iio/accel/mma9551.c956
-rw-r--r--drivers/iio/adc/ti_am335x_adc.c11
-rw-r--r--drivers/iio/amplifiers/ad8366.c4
-rw-r--r--drivers/iio/common/st_sensors/st_sensors_spi.c2
-rw-r--r--drivers/iio/frequency/ad9523.c2
-rw-r--r--drivers/iio/frequency/adf4350.c7
-rw-r--r--drivers/iio/iio_core.h9
-rw-r--r--drivers/iio/imu/Kconfig11
-rw-r--r--drivers/iio/imu/Makefile2
-rw-r--r--drivers/iio/imu/inv_mpu6050/Kconfig1
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_core.c124
-rw-r--r--drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h6
-rw-r--r--drivers/iio/imu/kmx61.c1595
-rw-r--r--drivers/iio/industrialio-buffer.c408
-rw-r--r--drivers/iio/industrialio-core.c50
-rw-r--r--drivers/iio/industrialio-event.c13
-rw-r--r--drivers/iio/industrialio-triggered-buffer.c13
-rw-r--r--drivers/iio/inkern.c30
-rw-r--r--drivers/iio/kfifo_buf.c87
-rw-r--r--drivers/iio/light/Kconfig13
-rw-r--r--drivers/iio/light/Makefile1
-rw-r--r--drivers/iio/light/jsa1212.c471
-rw-r--r--drivers/iio/light/lm3533-als.c2
-rw-r--r--drivers/iio/magnetometer/Kconfig15
-rw-r--r--drivers/iio/magnetometer/Makefile1
-rw-r--r--drivers/iio/magnetometer/ak09911.c326
-rw-r--r--drivers/iio/magnetometer/ak8975.c504
-rw-r--r--drivers/iio/pressure/bmp280.c150
-rw-r--r--drivers/iio/proximity/Kconfig17
-rw-r--r--drivers/iio/proximity/Makefile1
-rw-r--r--drivers/iio/proximity/sx9500.c752
-rw-r--r--drivers/iio/trigger/iio-trig-sysfs.c2
36 files changed, 4818 insertions, 839 deletions
diff --git a/drivers/iio/accel/Kconfig b/drivers/iio/accel/Kconfig
index 9b9be8725e9d..9f67c10291bd 100644
--- a/drivers/iio/accel/Kconfig
+++ b/drivers/iio/accel/Kconfig
@@ -43,6 +43,9 @@ config HID_SENSOR_ACCEL_3D
43 Say yes here to build support for the HID SENSOR 43 Say yes here to build support for the HID SENSOR
44 accelerometers 3D. 44 accelerometers 3D.
45 45
46 To compile this driver as a module, choose M here: the
47 module will be called hid-sensor-accel-3d.
48
46config IIO_ST_ACCEL_3AXIS 49config IIO_ST_ACCEL_3AXIS
47 tristate "STMicroelectronics accelerometers 3-Axis Driver" 50 tristate "STMicroelectronics accelerometers 3-Axis Driver"
48 depends on (I2C || SPI_MASTER) && SYSFS 51 depends on (I2C || SPI_MASTER) && SYSFS
@@ -80,6 +83,9 @@ config KXSD9
80 Say yes here to build support for the Kionix KXSD9 accelerometer. 83 Say yes here to build support for the Kionix KXSD9 accelerometer.
81 Currently this only supports the device via an SPI interface. 84 Currently this only supports the device via an SPI interface.
82 85
86 To compile this driver as a module, choose M here: the module
87 will be called kxsd9.
88
83config MMA8452 89config MMA8452
84 tristate "Freescale MMA8452Q Accelerometer Driver" 90 tristate "Freescale MMA8452Q Accelerometer Driver"
85 depends on I2C 91 depends on I2C
@@ -105,4 +111,14 @@ config KXCJK1013
105 To compile this driver as a module, choose M here: the module will 111 To compile this driver as a module, choose M here: the module will
106 be called kxcjk-1013. 112 be called kxcjk-1013.
107 113
114config MMA9551
115 tristate "Freescale MMA9551L Intelligent Motion-Sensing Platform Driver"
116 depends on I2C
117 help
118 Say yes here to build support for the Freescale MMA9551L
119 Intelligent Motion-Sensing Platform Driver.
120
121 To compile this driver as a module, choose M here: the module
122 will be called mma9551.
123
108endmenu 124endmenu
diff --git a/drivers/iio/accel/Makefile b/drivers/iio/accel/Makefile
index a593996c6539..de5b9cb9670f 100644
--- a/drivers/iio/accel/Makefile
+++ b/drivers/iio/accel/Makefile
@@ -9,6 +9,7 @@ obj-$(CONFIG_HID_SENSOR_ACCEL_3D) += hid-sensor-accel-3d.o
9obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o 9obj-$(CONFIG_KXCJK1013) += kxcjk-1013.o
10obj-$(CONFIG_KXSD9) += kxsd9.o 10obj-$(CONFIG_KXSD9) += kxsd9.o
11obj-$(CONFIG_MMA8452) += mma8452.o 11obj-$(CONFIG_MMA8452) += mma8452.o
12obj-$(CONFIG_MMA9551) += mma9551.o
12 13
13obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o 14obj-$(CONFIG_IIO_ST_ACCEL_3AXIS) += st_accel.o
14st_accel-y := st_accel_core.o 15st_accel-y := st_accel_core.o
diff --git a/drivers/iio/accel/kxcjk-1013.c b/drivers/iio/accel/kxcjk-1013.c
index da2fe93739a2..567de269cc00 100644
--- a/drivers/iio/accel/kxcjk-1013.c
+++ b/drivers/iio/accel/kxcjk-1013.c
@@ -108,6 +108,7 @@ struct kxcjk1013_data {
108 bool motion_trigger_on; 108 bool motion_trigger_on;
109 int64_t timestamp; 109 int64_t timestamp;
110 enum kx_chipset chipset; 110 enum kx_chipset chipset;
111 bool is_smo8500_device;
111}; 112};
112 113
113enum kxcjk1013_axis { 114enum kxcjk1013_axis {
@@ -377,6 +378,7 @@ static int kxcjk1013_get_startup_times(struct kxcjk1013_data *data)
377 378
378static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on) 379static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
379{ 380{
381#ifdef CONFIG_PM
380 int ret; 382 int ret;
381 383
382 if (on) 384 if (on)
@@ -388,8 +390,11 @@ static int kxcjk1013_set_power_state(struct kxcjk1013_data *data, bool on)
388 if (ret < 0) { 390 if (ret < 0) {
389 dev_err(&data->client->dev, 391 dev_err(&data->client->dev,
390 "Failed: kxcjk1013_set_power_state for %d\n", on); 392 "Failed: kxcjk1013_set_power_state for %d\n", on);
393 if (on)
394 pm_runtime_put_noidle(&data->client->dev);
391 return ret; 395 return ret;
392 } 396 }
397#endif
393 398
394 return 0; 399 return 0;
395} 400}
@@ -858,6 +863,8 @@ static int kxcjk1013_write_event_config(struct iio_dev *indio_dev,
858 863
859 ret = kxcjk1013_setup_any_motion_interrupt(data, state); 864 ret = kxcjk1013_setup_any_motion_interrupt(data, state);
860 if (ret < 0) { 865 if (ret < 0) {
866 kxcjk1013_set_power_state(data, false);
867 data->ev_enable_state = 0;
861 mutex_unlock(&data->mutex); 868 mutex_unlock(&data->mutex);
862 return ret; 869 return ret;
863 } 870 }
@@ -1008,6 +1015,7 @@ static int kxcjk1013_data_rdy_trigger_set_state(struct iio_trigger *trig,
1008 else 1015 else
1009 ret = kxcjk1013_setup_new_data_interrupt(data, state); 1016 ret = kxcjk1013_setup_new_data_interrupt(data, state);
1010 if (ret < 0) { 1017 if (ret < 0) {
1018 kxcjk1013_set_power_state(data, false);
1011 mutex_unlock(&data->mutex); 1019 mutex_unlock(&data->mutex);
1012 return ret; 1020 return ret;
1013 } 1021 }
@@ -1131,12 +1139,16 @@ static irqreturn_t kxcjk1013_data_rdy_trig_poll(int irq, void *private)
1131} 1139}
1132 1140
1133static const char *kxcjk1013_match_acpi_device(struct device *dev, 1141static const char *kxcjk1013_match_acpi_device(struct device *dev,
1134 enum kx_chipset *chipset) 1142 enum kx_chipset *chipset,
1143 bool *is_smo8500_device)
1135{ 1144{
1136 const struct acpi_device_id *id; 1145 const struct acpi_device_id *id;
1146
1137 id = acpi_match_device(dev->driver->acpi_match_table, dev); 1147 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1138 if (!id) 1148 if (!id)
1139 return NULL; 1149 return NULL;
1150 if (strcmp(id->id, "SMO8500") == 0)
1151 *is_smo8500_device = true;
1140 *chipset = (enum kx_chipset)id->driver_data; 1152 *chipset = (enum kx_chipset)id->driver_data;
1141 1153
1142 return dev_name(dev); 1154 return dev_name(dev);
@@ -1151,6 +1163,8 @@ static int kxcjk1013_gpio_probe(struct i2c_client *client,
1151 1163
1152 if (!client) 1164 if (!client)
1153 return -EINVAL; 1165 return -EINVAL;
1166 if (data->is_smo8500_device)
1167 return -ENOTSUPP;
1154 1168
1155 dev = &client->dev; 1169 dev = &client->dev;
1156 1170
@@ -1200,7 +1214,8 @@ static int kxcjk1013_probe(struct i2c_client *client,
1200 name = id->name; 1214 name = id->name;
1201 } else if (ACPI_HANDLE(&client->dev)) { 1215 } else if (ACPI_HANDLE(&client->dev)) {
1202 name = kxcjk1013_match_acpi_device(&client->dev, 1216 name = kxcjk1013_match_acpi_device(&client->dev,
1203 &data->chipset); 1217 &data->chipset,
1218 &data->is_smo8500_device);
1204 } else 1219 } else
1205 return -ENODEV; 1220 return -ENODEV;
1206 1221
@@ -1228,21 +1243,25 @@ static int kxcjk1013_probe(struct i2c_client *client,
1228 KXCJK1013_IRQ_NAME, 1243 KXCJK1013_IRQ_NAME,
1229 indio_dev); 1244 indio_dev);
1230 if (ret) 1245 if (ret)
1231 return ret; 1246 goto err_poweroff;
1232 1247
1233 data->dready_trig = devm_iio_trigger_alloc(&client->dev, 1248 data->dready_trig = devm_iio_trigger_alloc(&client->dev,
1234 "%s-dev%d", 1249 "%s-dev%d",
1235 indio_dev->name, 1250 indio_dev->name,
1236 indio_dev->id); 1251 indio_dev->id);
1237 if (!data->dready_trig) 1252 if (!data->dready_trig) {
1238 return -ENOMEM; 1253 ret = -ENOMEM;
1254 goto err_poweroff;
1255 }
1239 1256
1240 data->motion_trig = devm_iio_trigger_alloc(&client->dev, 1257 data->motion_trig = devm_iio_trigger_alloc(&client->dev,
1241 "%s-any-motion-dev%d", 1258 "%s-any-motion-dev%d",
1242 indio_dev->name, 1259 indio_dev->name,
1243 indio_dev->id); 1260 indio_dev->id);
1244 if (!data->motion_trig) 1261 if (!data->motion_trig) {
1245 return -ENOMEM; 1262 ret = -ENOMEM;
1263 goto err_poweroff;
1264 }
1246 1265
1247 data->dready_trig->dev.parent = &client->dev; 1266 data->dready_trig->dev.parent = &client->dev;
1248 data->dready_trig->ops = &kxcjk1013_trigger_ops; 1267 data->dready_trig->ops = &kxcjk1013_trigger_ops;
@@ -1251,7 +1270,7 @@ static int kxcjk1013_probe(struct i2c_client *client,
1251 iio_trigger_get(indio_dev->trig); 1270 iio_trigger_get(indio_dev->trig);
1252 ret = iio_trigger_register(data->dready_trig); 1271 ret = iio_trigger_register(data->dready_trig);
1253 if (ret) 1272 if (ret)
1254 return ret; 1273 goto err_poweroff;
1255 1274
1256 data->motion_trig->dev.parent = &client->dev; 1275 data->motion_trig->dev.parent = &client->dev;
1257 data->motion_trig->ops = &kxcjk1013_trigger_ops; 1276 data->motion_trig->ops = &kxcjk1013_trigger_ops;
@@ -1300,6 +1319,8 @@ err_trigger_unregister:
1300 iio_trigger_unregister(data->dready_trig); 1319 iio_trigger_unregister(data->dready_trig);
1301 if (data->motion_trig) 1320 if (data->motion_trig)
1302 iio_trigger_unregister(data->motion_trig); 1321 iio_trigger_unregister(data->motion_trig);
1322err_poweroff:
1323 kxcjk1013_set_mode(data, STANDBY);
1303 1324
1304 return ret; 1325 return ret;
1305} 1326}
@@ -1349,10 +1370,7 @@ static int kxcjk1013_resume(struct device *dev)
1349 int ret = 0; 1370 int ret = 0;
1350 1371
1351 mutex_lock(&data->mutex); 1372 mutex_lock(&data->mutex);
1352 /* Check, if the suspend occured while active */ 1373 ret = kxcjk1013_set_mode(data, OPERATION);
1353 if (data->dready_trigger_on || data->motion_trigger_on ||
1354 data->ev_enable_state)
1355 ret = kxcjk1013_set_mode(data, OPERATION);
1356 mutex_unlock(&data->mutex); 1374 mutex_unlock(&data->mutex);
1357 1375
1358 return ret; 1376 return ret;
@@ -1364,8 +1382,14 @@ static int kxcjk1013_runtime_suspend(struct device *dev)
1364{ 1382{
1365 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); 1383 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1366 struct kxcjk1013_data *data = iio_priv(indio_dev); 1384 struct kxcjk1013_data *data = iio_priv(indio_dev);
1385 int ret;
1367 1386
1368 return kxcjk1013_set_mode(data, STANDBY); 1387 ret = kxcjk1013_set_mode(data, STANDBY);
1388 if (ret < 0) {
1389 dev_err(&data->client->dev, "powering off device failed\n");
1390 return -EAGAIN;
1391 }
1392 return 0;
1369} 1393}
1370 1394
1371static int kxcjk1013_runtime_resume(struct device *dev) 1395static int kxcjk1013_runtime_resume(struct device *dev)
@@ -1399,6 +1423,7 @@ static const struct acpi_device_id kx_acpi_match[] = {
1399 {"KXCJ1013", KXCJK1013}, 1423 {"KXCJ1013", KXCJK1013},
1400 {"KXCJ1008", KXCJ91008}, 1424 {"KXCJ1008", KXCJ91008},
1401 {"KXTJ1009", KXTJ21009}, 1425 {"KXTJ1009", KXTJ21009},
1426 {"SMO8500", KXCJ91008},
1402 { }, 1427 { },
1403}; 1428};
1404MODULE_DEVICE_TABLE(acpi, kx_acpi_match); 1429MODULE_DEVICE_TABLE(acpi, kx_acpi_match);
@@ -1407,6 +1432,7 @@ static const struct i2c_device_id kxcjk1013_id[] = {
1407 {"kxcjk1013", KXCJK1013}, 1432 {"kxcjk1013", KXCJK1013},
1408 {"kxcj91008", KXCJ91008}, 1433 {"kxcj91008", KXCJ91008},
1409 {"kxtj21009", KXTJ21009}, 1434 {"kxtj21009", KXTJ21009},
1435 {"SMO8500", KXCJ91008},
1410 {} 1436 {}
1411}; 1437};
1412 1438
diff --git a/drivers/iio/accel/mma8452.c b/drivers/iio/accel/mma8452.c
index 3c12d4966376..5b80657883bb 100644
--- a/drivers/iio/accel/mma8452.c
+++ b/drivers/iio/accel/mma8452.c
@@ -111,7 +111,7 @@ static const int mma8452_samp_freq[8][2] = {
111 {6, 250000}, {1, 560000} 111 {6, 250000}, {1, 560000}
112}; 112};
113 113
114/* 114/*
115 * Hardware has fullscale of -2G, -4G, -8G corresponding to raw value -2048 115 * Hardware has fullscale of -2G, -4G, -8G corresponding to raw value -2048
116 * The userspace interface uses m/s^2 and we declare micro units 116 * The userspace interface uses m/s^2 and we declare micro units
117 * So scale factor is given by: 117 * So scale factor is given by:
diff --git a/drivers/iio/accel/mma9551.c b/drivers/iio/accel/mma9551.c
new file mode 100644
index 000000000000..6563e267b8ae
--- /dev/null
+++ b/drivers/iio/accel/mma9551.c
@@ -0,0 +1,956 @@
1/*
2 * Freescale MMA9551L Intelligent Motion-Sensing Platform driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/interrupt.h>
18#include <linux/slab.h>
19#include <linux/acpi.h>
20#include <linux/delay.h>
21#include <linux/gpio/consumer.h>
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/events.h>
25
26#define MMA9551_DRV_NAME "mma9551"
27#define MMA9551_IRQ_NAME "mma9551_event"
28#define MMA9551_GPIO_NAME "mma9551_int"
29#define MMA9551_GPIO_COUNT 4
30
31/* Applications IDs */
32#define MMA9551_APPID_VERSION 0x00
33#define MMA9551_APPID_GPIO 0x03
34#define MMA9551_APPID_AFE 0x06
35#define MMA9551_APPID_TILT 0x0B
36#define MMA9551_APPID_SLEEP_WAKE 0x12
37#define MMA9551_APPID_RESET 0x17
38#define MMA9551_APPID_NONE 0xff
39
40/* Command masks for mailbox write command */
41#define MMA9551_CMD_READ_VERSION_INFO 0x00
42#define MMA9551_CMD_READ_CONFIG 0x10
43#define MMA9551_CMD_WRITE_CONFIG 0x20
44#define MMA9551_CMD_READ_STATUS 0x30
45
46enum mma9551_gpio_pin {
47 mma9551_gpio6 = 0,
48 mma9551_gpio7,
49 mma9551_gpio8,
50 mma9551_gpio9,
51 mma9551_gpio_max = mma9551_gpio9,
52};
53
54/* Mailbox read command */
55#define MMA9551_RESPONSE_COCO BIT(7)
56
57/* Error-Status codes returned in mailbox read command */
58#define MMA9551_MCI_ERROR_NONE 0x00
59#define MMA9551_MCI_ERROR_PARAM 0x04
60#define MMA9551_MCI_INVALID_COUNT 0x19
61#define MMA9551_MCI_ERROR_COMMAND 0x1C
62#define MMA9551_MCI_ERROR_INVALID_LENGTH 0x21
63#define MMA9551_MCI_ERROR_FIFO_BUSY 0x22
64#define MMA9551_MCI_ERROR_FIFO_ALLOCATED 0x23
65#define MMA9551_MCI_ERROR_FIFO_OVERSIZE 0x24
66
67/* GPIO Application */
68#define MMA9551_GPIO_POL_MSB 0x08
69#define MMA9551_GPIO_POL_LSB 0x09
70
71/* Sleep/Wake application */
72#define MMA9551_SLEEP_CFG 0x06
73#define MMA9551_SLEEP_CFG_SNCEN BIT(0)
74#define MMA9551_SLEEP_CFG_SCHEN BIT(2)
75
76/* AFE application */
77#define MMA9551_AFE_X_ACCEL_REG 0x00
78#define MMA9551_AFE_Y_ACCEL_REG 0x02
79#define MMA9551_AFE_Z_ACCEL_REG 0x04
80
81/* Tilt application (inclination in IIO terms). */
82#define MMA9551_TILT_XZ_ANG_REG 0x00
83#define MMA9551_TILT_YZ_ANG_REG 0x01
84#define MMA9551_TILT_XY_ANG_REG 0x02
85#define MMA9551_TILT_ANGFLG BIT(7)
86#define MMA9551_TILT_QUAD_REG 0x03
87#define MMA9551_TILT_XY_QUAD_SHIFT 0
88#define MMA9551_TILT_YZ_QUAD_SHIFT 2
89#define MMA9551_TILT_XZ_QUAD_SHIFT 4
90#define MMA9551_TILT_CFG_REG 0x01
91#define MMA9551_TILT_ANG_THRESH_MASK GENMASK(3, 0)
92
93/* Tilt events are mapped to the first three GPIO pins. */
94enum mma9551_tilt_axis {
95 mma9551_x = 0,
96 mma9551_y,
97 mma9551_z,
98};
99
100/*
101 * A response is composed of:
102 * - control registers: MB0-3
103 * - data registers: MB4-31
104 *
105 * A request is composed of:
106 * - mbox to write to (always 0)
107 * - control registers: MB1-4
108 * - data registers: MB5-31
109 */
110#define MMA9551_MAILBOX_CTRL_REGS 4
111#define MMA9551_MAX_MAILBOX_DATA_REGS 28
112#define MMA9551_MAILBOX_REGS 32
113
114#define MMA9551_I2C_READ_RETRIES 5
115#define MMA9551_I2C_READ_DELAY 50 /* us */
116
117struct mma9551_mbox_request {
118 u8 start_mbox; /* Always 0. */
119 u8 app_id;
120 /*
121 * See Section 5.3.1 of the MMA955xL Software Reference Manual.
122 *
123 * Bit 7: reserved, always 0
124 * Bits 6-4: command
125 * Bits 3-0: upper bits of register offset
126 */
127 u8 cmd_off;
128 u8 lower_off;
129 u8 nbytes;
130 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS - 1];
131} __packed;
132
133struct mma9551_mbox_response {
134 u8 app_id;
135 /*
136 * See Section 5.3.3 of the MMA955xL Software Reference Manual.
137 *
138 * Bit 7: COCO
139 * Bits 6-0: Error code.
140 */
141 u8 coco_err;
142 u8 nbytes;
143 u8 req_bytes;
144 u8 buf[MMA9551_MAX_MAILBOX_DATA_REGS];
145} __packed;
146
147struct mma9551_version_info {
148 __be32 device_id;
149 u8 rom_version[2];
150 u8 fw_version[2];
151 u8 hw_version[2];
152 u8 fw_build[2];
153};
154
155struct mma9551_data {
156 struct i2c_client *client;
157 struct mutex mutex;
158 int event_enabled[3];
159 int irqs[MMA9551_GPIO_COUNT];
160};
161
162static int mma9551_transfer(struct i2c_client *client,
163 u8 app_id, u8 command, u16 offset,
164 u8 *inbytes, int num_inbytes,
165 u8 *outbytes, int num_outbytes)
166{
167 struct mma9551_mbox_request req;
168 struct mma9551_mbox_response rsp;
169 struct i2c_msg in, out;
170 u8 req_len, err_code;
171 int ret, retries;
172
173 if (offset >= 1 << 12) {
174 dev_err(&client->dev, "register offset too large\n");
175 return -EINVAL;
176 }
177
178 req_len = 1 + MMA9551_MAILBOX_CTRL_REGS + num_inbytes;
179 req.start_mbox = 0;
180 req.app_id = app_id;
181 req.cmd_off = command | (offset >> 8);
182 req.lower_off = offset;
183
184 if (command == MMA9551_CMD_WRITE_CONFIG)
185 req.nbytes = num_inbytes;
186 else
187 req.nbytes = num_outbytes;
188 if (num_inbytes)
189 memcpy(req.buf, inbytes, num_inbytes);
190
191 out.addr = client->addr;
192 out.flags = 0;
193 out.len = req_len;
194 out.buf = (u8 *)&req;
195
196 ret = i2c_transfer(client->adapter, &out, 1);
197 if (ret < 0) {
198 dev_err(&client->dev, "i2c write failed\n");
199 return ret;
200 }
201
202 retries = MMA9551_I2C_READ_RETRIES;
203 do {
204 udelay(MMA9551_I2C_READ_DELAY);
205
206 in.addr = client->addr;
207 in.flags = I2C_M_RD;
208 in.len = sizeof(rsp);
209 in.buf = (u8 *)&rsp;
210
211 ret = i2c_transfer(client->adapter, &in, 1);
212 if (ret < 0) {
213 dev_err(&client->dev, "i2c read failed\n");
214 return ret;
215 }
216
217 if (rsp.coco_err & MMA9551_RESPONSE_COCO)
218 break;
219 } while (--retries > 0);
220
221 if (retries == 0) {
222 dev_err(&client->dev,
223 "timed out while waiting for command response\n");
224 return -ETIMEDOUT;
225 }
226
227 if (rsp.app_id != app_id) {
228 dev_err(&client->dev,
229 "app_id mismatch in response got %02x expected %02x\n",
230 rsp.app_id, app_id);
231 return -EINVAL;
232 }
233
234 err_code = rsp.coco_err & ~MMA9551_RESPONSE_COCO;
235 if (err_code != MMA9551_MCI_ERROR_NONE) {
236 dev_err(&client->dev, "read returned error %x\n", err_code);
237 return -EINVAL;
238 }
239
240 if (rsp.nbytes != rsp.req_bytes) {
241 dev_err(&client->dev,
242 "output length mismatch got %d expected %d\n",
243 rsp.nbytes, rsp.req_bytes);
244 return -EINVAL;
245 }
246
247 if (num_outbytes)
248 memcpy(outbytes, rsp.buf, num_outbytes);
249
250 return 0;
251}
252
253static int mma9551_read_config_byte(struct i2c_client *client, u8 app_id,
254 u16 reg, u8 *val)
255{
256 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG,
257 reg, NULL, 0, val, 1);
258}
259
260static int mma9551_write_config_byte(struct i2c_client *client, u8 app_id,
261 u16 reg, u8 val)
262{
263 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, reg,
264 &val, 1, NULL, 0);
265}
266
267static int mma9551_read_status_byte(struct i2c_client *client, u8 app_id,
268 u16 reg, u8 *val)
269{
270 return mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
271 reg, NULL, 0, val, 1);
272}
273
274static int mma9551_read_status_word(struct i2c_client *client, u8 app_id,
275 u16 reg, u16 *val)
276{
277 int ret;
278 __be16 v;
279
280 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS,
281 reg, NULL, 0, (u8 *)&v, 2);
282 *val = be16_to_cpu(v);
283
284 return ret;
285}
286
287static int mma9551_update_config_bits(struct i2c_client *client, u8 app_id,
288 u16 reg, u8 mask, u8 val)
289{
290 int ret;
291 u8 tmp, orig;
292
293 ret = mma9551_read_config_byte(client, app_id, reg, &orig);
294 if (ret < 0)
295 return ret;
296
297 tmp = orig & ~mask;
298 tmp |= val & mask;
299
300 if (tmp == orig)
301 return 0;
302
303 return mma9551_write_config_byte(client, app_id, reg, tmp);
304}
305
306/*
307 * The polarity parameter is described in section 6.2.2, page 66, of the
308 * Software Reference Manual. Basically, polarity=0 means the interrupt
309 * line has the same value as the selected bit, while polarity=1 means
310 * the line is inverted.
311 */
312static int mma9551_gpio_config(struct i2c_client *client,
313 enum mma9551_gpio_pin pin,
314 u8 app_id, u8 bitnum, int polarity)
315{
316 u8 reg, pol_mask, pol_val;
317 int ret;
318
319 if (pin > mma9551_gpio_max) {
320 dev_err(&client->dev, "bad GPIO pin\n");
321 return -EINVAL;
322 }
323
324 /*
325 * Pin 6 is configured by regs 0x00 and 0x01, pin 7 by 0x02 and
326 * 0x03, and so on.
327 */
328 reg = pin * 2;
329
330 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
331 reg, app_id);
332 if (ret < 0) {
333 dev_err(&client->dev, "error setting GPIO app_id\n");
334 return ret;
335 }
336
337 ret = mma9551_write_config_byte(client, MMA9551_APPID_GPIO,
338 reg + 1, bitnum);
339 if (ret < 0) {
340 dev_err(&client->dev, "error setting GPIO bit number\n");
341 return ret;
342 }
343
344 switch (pin) {
345 case mma9551_gpio6:
346 reg = MMA9551_GPIO_POL_LSB;
347 pol_mask = 1 << 6;
348 break;
349 case mma9551_gpio7:
350 reg = MMA9551_GPIO_POL_LSB;
351 pol_mask = 1 << 7;
352 break;
353 case mma9551_gpio8:
354 reg = MMA9551_GPIO_POL_MSB;
355 pol_mask = 1 << 0;
356 break;
357 case mma9551_gpio9:
358 reg = MMA9551_GPIO_POL_MSB;
359 pol_mask = 1 << 1;
360 break;
361 }
362 pol_val = polarity ? pol_mask : 0;
363
364 ret = mma9551_update_config_bits(client, MMA9551_APPID_GPIO, reg,
365 pol_mask, pol_val);
366 if (ret < 0)
367 dev_err(&client->dev, "error setting GPIO polarity\n");
368
369 return ret;
370}
371
372static int mma9551_read_version(struct i2c_client *client)
373{
374 struct mma9551_version_info info;
375 int ret;
376
377 ret = mma9551_transfer(client, MMA9551_APPID_VERSION, 0x00, 0x00,
378 NULL, 0, (u8 *)&info, sizeof(info));
379 if (ret < 0)
380 return ret;
381
382 dev_info(&client->dev, "Device ID 0x%x, firmware version %02x.%02x\n",
383 be32_to_cpu(info.device_id), info.fw_version[0],
384 info.fw_version[1]);
385
386 return 0;
387}
388
389/*
390 * Use 'false' as the second parameter to cause the device to enter
391 * sleep.
392 */
393static int mma9551_set_device_state(struct i2c_client *client,
394 bool enable)
395{
396 return mma9551_update_config_bits(client, MMA9551_APPID_SLEEP_WAKE,
397 MMA9551_SLEEP_CFG,
398 MMA9551_SLEEP_CFG_SNCEN,
399 enable ? 0 : MMA9551_SLEEP_CFG_SNCEN);
400}
401
402static int mma9551_read_incli_chan(struct i2c_client *client,
403 const struct iio_chan_spec *chan,
404 int *val)
405{
406 u8 quad_shift, angle, quadrant;
407 u16 reg_addr;
408 int ret;
409
410 switch (chan->channel2) {
411 case IIO_MOD_X:
412 reg_addr = MMA9551_TILT_YZ_ANG_REG;
413 quad_shift = MMA9551_TILT_YZ_QUAD_SHIFT;
414 break;
415 case IIO_MOD_Y:
416 reg_addr = MMA9551_TILT_XZ_ANG_REG;
417 quad_shift = MMA9551_TILT_XZ_QUAD_SHIFT;
418 break;
419 case IIO_MOD_Z:
420 reg_addr = MMA9551_TILT_XY_ANG_REG;
421 quad_shift = MMA9551_TILT_XY_QUAD_SHIFT;
422 break;
423 default:
424 return -EINVAL;
425 }
426
427 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
428 reg_addr, &angle);
429 if (ret < 0)
430 return ret;
431
432 ret = mma9551_read_status_byte(client, MMA9551_APPID_TILT,
433 MMA9551_TILT_QUAD_REG, &quadrant);
434 if (ret < 0)
435 return ret;
436
437 angle &= ~MMA9551_TILT_ANGFLG;
438 quadrant = (quadrant >> quad_shift) & 0x03;
439
440 if (quadrant == 1 || quadrant == 3)
441 *val = 90 * (quadrant + 1) - angle;
442 else
443 *val = angle + 90 * quadrant;
444
445 return IIO_VAL_INT;
446}
447
448static int mma9551_read_accel_chan(struct i2c_client *client,
449 const struct iio_chan_spec *chan,
450 int *val, int *val2)
451{
452 u16 reg_addr;
453 s16 raw_accel;
454 int ret;
455
456 switch (chan->channel2) {
457 case IIO_MOD_X:
458 reg_addr = MMA9551_AFE_X_ACCEL_REG;
459 break;
460 case IIO_MOD_Y:
461 reg_addr = MMA9551_AFE_Y_ACCEL_REG;
462 break;
463 case IIO_MOD_Z:
464 reg_addr = MMA9551_AFE_Z_ACCEL_REG;
465 break;
466 default:
467 return -EINVAL;
468 }
469
470 ret = mma9551_read_status_word(client, MMA9551_APPID_AFE,
471 reg_addr, &raw_accel);
472 if (ret < 0)
473 return ret;
474
475 *val = raw_accel;
476
477 return IIO_VAL_INT;
478}
479
480static int mma9551_read_raw(struct iio_dev *indio_dev,
481 struct iio_chan_spec const *chan,
482 int *val, int *val2, long mask)
483{
484 struct mma9551_data *data = iio_priv(indio_dev);
485 int ret;
486
487 switch (mask) {
488 case IIO_CHAN_INFO_PROCESSED:
489 switch (chan->type) {
490 case IIO_INCLI:
491 mutex_lock(&data->mutex);
492 ret = mma9551_read_incli_chan(data->client, chan, val);
493 mutex_unlock(&data->mutex);
494 return ret;
495 default:
496 return -EINVAL;
497 }
498 case IIO_CHAN_INFO_RAW:
499 switch (chan->type) {
500 case IIO_ACCEL:
501 mutex_lock(&data->mutex);
502 ret = mma9551_read_accel_chan(data->client,
503 chan, val, val2);
504 mutex_unlock(&data->mutex);
505 return ret;
506 default:
507 return -EINVAL;
508 }
509 case IIO_CHAN_INFO_SCALE:
510 switch (chan->type) {
511 case IIO_ACCEL:
512 *val = 0;
513 *val2 = 2440;
514 return IIO_VAL_INT_PLUS_MICRO;
515 default:
516 return -EINVAL;
517 }
518 default:
519 return -EINVAL;
520 }
521}
522
523static int mma9551_read_event_config(struct iio_dev *indio_dev,
524 const struct iio_chan_spec *chan,
525 enum iio_event_type type,
526 enum iio_event_direction dir)
527{
528 struct mma9551_data *data = iio_priv(indio_dev);
529
530 switch (chan->type) {
531 case IIO_INCLI:
532 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
533 return data->event_enabled[chan->channel2 - 1];
534 default:
535 return -EINVAL;
536 }
537}
538
539static int mma9551_config_incli_event(struct iio_dev *indio_dev,
540 enum iio_modifier axis,
541 int state)
542{
543 struct mma9551_data *data = iio_priv(indio_dev);
544 enum mma9551_tilt_axis mma_axis;
545 int ret;
546
547 /* IIO counts axes from 1, because IIO_NO_MOD is 0. */
548 mma_axis = axis - 1;
549
550 if (data->event_enabled[mma_axis] == state)
551 return 0;
552
553 if (state == 0) {
554 ret = mma9551_gpio_config(data->client,
555 (enum mma9551_gpio_pin)mma_axis,
556 MMA9551_APPID_NONE, 0, 0);
557 if (ret < 0)
558 return ret;
559 } else {
560 int bitnum;
561
562 /* Bit 7 of each angle register holds the angle flag. */
563 switch (axis) {
564 case IIO_MOD_X:
565 bitnum = 7 + 8 * MMA9551_TILT_YZ_ANG_REG;
566 break;
567 case IIO_MOD_Y:
568 bitnum = 7 + 8 * MMA9551_TILT_XZ_ANG_REG;
569 break;
570 case IIO_MOD_Z:
571 bitnum = 7 + 8 * MMA9551_TILT_XY_ANG_REG;
572 break;
573 default:
574 return -EINVAL;
575 }
576
577 ret = mma9551_gpio_config(data->client,
578 (enum mma9551_gpio_pin)mma_axis,
579 MMA9551_APPID_TILT, bitnum, 0);
580 if (ret < 0)
581 return ret;
582 }
583
584 data->event_enabled[mma_axis] = state;
585
586 return ret;
587}
588
589static int mma9551_write_event_config(struct iio_dev *indio_dev,
590 const struct iio_chan_spec *chan,
591 enum iio_event_type type,
592 enum iio_event_direction dir,
593 int state)
594{
595 struct mma9551_data *data = iio_priv(indio_dev);
596 int ret;
597
598 switch (chan->type) {
599 case IIO_INCLI:
600 mutex_lock(&data->mutex);
601 ret = mma9551_config_incli_event(indio_dev,
602 chan->channel2, state);
603 mutex_unlock(&data->mutex);
604 return ret;
605 default:
606 return -EINVAL;
607 }
608}
609
610static int mma9551_write_event_value(struct iio_dev *indio_dev,
611 const struct iio_chan_spec *chan,
612 enum iio_event_type type,
613 enum iio_event_direction dir,
614 enum iio_event_info info,
615 int val, int val2)
616{
617 struct mma9551_data *data = iio_priv(indio_dev);
618 int ret;
619
620 switch (chan->type) {
621 case IIO_INCLI:
622 if (val2 != 0 || val < 1 || val > 10)
623 return -EINVAL;
624 mutex_lock(&data->mutex);
625 ret = mma9551_update_config_bits(data->client,
626 MMA9551_APPID_TILT,
627 MMA9551_TILT_CFG_REG,
628 MMA9551_TILT_ANG_THRESH_MASK,
629 val);
630 mutex_unlock(&data->mutex);
631 return ret;
632 default:
633 return -EINVAL;
634 }
635}
636
637static int mma9551_read_event_value(struct iio_dev *indio_dev,
638 const struct iio_chan_spec *chan,
639 enum iio_event_type type,
640 enum iio_event_direction dir,
641 enum iio_event_info info,
642 int *val, int *val2)
643{
644 struct mma9551_data *data = iio_priv(indio_dev);
645 int ret;
646 u8 tmp;
647
648 switch (chan->type) {
649 case IIO_INCLI:
650 mutex_lock(&data->mutex);
651 ret = mma9551_read_config_byte(data->client,
652 MMA9551_APPID_TILT,
653 MMA9551_TILT_CFG_REG, &tmp);
654 mutex_unlock(&data->mutex);
655 if (ret < 0)
656 return ret;
657 *val = tmp & MMA9551_TILT_ANG_THRESH_MASK;
658 *val2 = 0;
659 return IIO_VAL_INT;
660 default:
661 return -EINVAL;
662 }
663}
664
665static const struct iio_event_spec mma9551_incli_event = {
666 .type = IIO_EV_TYPE_ROC,
667 .dir = IIO_EV_DIR_RISING,
668 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
669 .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE),
670};
671
672#define MMA9551_ACCEL_CHANNEL(axis) { \
673 .type = IIO_ACCEL, \
674 .modified = 1, \
675 .channel2 = axis, \
676 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
677 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
678}
679
680#define MMA9551_INCLI_CHANNEL(axis) { \
681 .type = IIO_INCLI, \
682 .modified = 1, \
683 .channel2 = axis, \
684 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
685 .event_spec = &mma9551_incli_event, \
686 .num_event_specs = 1, \
687}
688
689static const struct iio_chan_spec mma9551_channels[] = {
690 MMA9551_ACCEL_CHANNEL(IIO_MOD_X),
691 MMA9551_ACCEL_CHANNEL(IIO_MOD_Y),
692 MMA9551_ACCEL_CHANNEL(IIO_MOD_Z),
693
694 MMA9551_INCLI_CHANNEL(IIO_MOD_X),
695 MMA9551_INCLI_CHANNEL(IIO_MOD_Y),
696 MMA9551_INCLI_CHANNEL(IIO_MOD_Z),
697};
698
699static const struct iio_info mma9551_info = {
700 .driver_module = THIS_MODULE,
701 .read_raw = mma9551_read_raw,
702 .read_event_config = mma9551_read_event_config,
703 .write_event_config = mma9551_write_event_config,
704 .read_event_value = mma9551_read_event_value,
705 .write_event_value = mma9551_write_event_value,
706};
707
708static irqreturn_t mma9551_event_handler(int irq, void *private)
709{
710 struct iio_dev *indio_dev = private;
711 struct mma9551_data *data = iio_priv(indio_dev);
712 int i, ret, mma_axis = -1;
713 u16 reg;
714 u8 val;
715
716 mutex_lock(&data->mutex);
717
718 for (i = 0; i < 3; i++)
719 if (irq == data->irqs[i]) {
720 mma_axis = i;
721 break;
722 }
723
724 if (mma_axis == -1) {
725 /* IRQ was triggered on 4th line, which we don't use. */
726 dev_warn(&data->client->dev,
727 "irq triggered on unused line %d\n", data->irqs[3]);
728 goto out;
729 }
730
731 switch (mma_axis) {
732 case mma9551_x:
733 reg = MMA9551_TILT_YZ_ANG_REG;
734 break;
735 case mma9551_y:
736 reg = MMA9551_TILT_XZ_ANG_REG;
737 break;
738 case mma9551_z:
739 reg = MMA9551_TILT_XY_ANG_REG;
740 break;
741 }
742
743 /*
744 * Read the angle even though we don't use it, otherwise we
745 * won't get any further interrupts.
746 */
747 ret = mma9551_read_status_byte(data->client, MMA9551_APPID_TILT,
748 reg, &val);
749 if (ret < 0) {
750 dev_err(&data->client->dev,
751 "error %d reading tilt register in IRQ\n", ret);
752 goto out;
753 }
754
755 iio_push_event(indio_dev,
756 IIO_MOD_EVENT_CODE(IIO_INCLI, 0, (mma_axis + 1),
757 IIO_EV_TYPE_ROC, IIO_EV_DIR_RISING),
758 iio_get_time_ns());
759
760out:
761 mutex_unlock(&data->mutex);
762
763 return IRQ_HANDLED;
764}
765
766static int mma9551_init(struct mma9551_data *data)
767{
768 int ret;
769
770 ret = mma9551_read_version(data->client);
771 if (ret)
772 return ret;
773
774 /* Power on chip and enable doze mode. */
775 return mma9551_update_config_bits(data->client,
776 MMA9551_APPID_SLEEP_WAKE,
777 MMA9551_SLEEP_CFG,
778 MMA9551_SLEEP_CFG_SCHEN | MMA9551_SLEEP_CFG_SNCEN,
779 MMA9551_SLEEP_CFG_SCHEN);
780}
781
782static int mma9551_gpio_probe(struct iio_dev *indio_dev)
783{
784 struct gpio_desc *gpio;
785 int i, ret;
786 struct mma9551_data *data = iio_priv(indio_dev);
787 struct device *dev = &data->client->dev;
788
789 for (i = 0; i < MMA9551_GPIO_COUNT; i++) {
790 gpio = devm_gpiod_get_index(dev, MMA9551_GPIO_NAME, i);
791 if (IS_ERR(gpio)) {
792 dev_err(dev, "acpi gpio get index failed\n");
793 return PTR_ERR(gpio);
794 }
795
796 ret = gpiod_direction_input(gpio);
797 if (ret)
798 return ret;
799
800 data->irqs[i] = gpiod_to_irq(gpio);
801 ret = devm_request_threaded_irq(dev, data->irqs[i],
802 NULL, mma9551_event_handler,
803 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
804 MMA9551_IRQ_NAME, indio_dev);
805 if (ret < 0) {
806 dev_err(dev, "request irq %d failed\n", data->irqs[i]);
807 return ret;
808 }
809
810 dev_dbg(dev, "gpio resource, no:%d irq:%d\n",
811 desc_to_gpio(gpio), data->irqs[i]);
812 }
813
814 return 0;
815}
816
817static const char *mma9551_match_acpi_device(struct device *dev)
818{
819 const struct acpi_device_id *id;
820
821 id = acpi_match_device(dev->driver->acpi_match_table, dev);
822 if (!id)
823 return NULL;
824
825 return dev_name(dev);
826}
827
828static int mma9551_probe(struct i2c_client *client,
829 const struct i2c_device_id *id)
830{
831 struct mma9551_data *data;
832 struct iio_dev *indio_dev;
833 const char *name = NULL;
834 int ret;
835
836 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
837 if (!indio_dev)
838 return -ENOMEM;
839
840 data = iio_priv(indio_dev);
841 i2c_set_clientdata(client, indio_dev);
842 data->client = client;
843
844 if (id)
845 name = id->name;
846 else if (ACPI_HANDLE(&client->dev))
847 name = mma9551_match_acpi_device(&client->dev);
848
849 ret = mma9551_init(data);
850 if (ret < 0)
851 return ret;
852
853 mutex_init(&data->mutex);
854
855 indio_dev->dev.parent = &client->dev;
856 indio_dev->channels = mma9551_channels;
857 indio_dev->num_channels = ARRAY_SIZE(mma9551_channels);
858 indio_dev->name = name;
859 indio_dev->modes = INDIO_DIRECT_MODE;
860 indio_dev->info = &mma9551_info;
861
862 ret = mma9551_gpio_probe(indio_dev);
863 if (ret < 0)
864 goto out_poweroff;
865
866 ret = iio_device_register(indio_dev);
867 if (ret < 0) {
868 dev_err(&client->dev, "unable to register iio device\n");
869 goto out_poweroff;
870 }
871
872 return 0;
873
874out_poweroff:
875 mma9551_set_device_state(client, false);
876
877 return ret;
878}
879
880static int mma9551_remove(struct i2c_client *client)
881{
882 struct iio_dev *indio_dev = i2c_get_clientdata(client);
883 struct mma9551_data *data = iio_priv(indio_dev);
884
885 iio_device_unregister(indio_dev);
886 mutex_lock(&data->mutex);
887 mma9551_set_device_state(data->client, false);
888 mutex_unlock(&data->mutex);
889
890 return 0;
891}
892
893#ifdef CONFIG_PM_SLEEP
894static int mma9551_suspend(struct device *dev)
895{
896 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
897 struct mma9551_data *data = iio_priv(indio_dev);
898
899 mutex_lock(&data->mutex);
900 mma9551_set_device_state(data->client, false);
901 mutex_unlock(&data->mutex);
902
903 return 0;
904}
905
906static int mma9551_resume(struct device *dev)
907{
908 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
909 struct mma9551_data *data = iio_priv(indio_dev);
910
911 mutex_lock(&data->mutex);
912 mma9551_set_device_state(data->client, true);
913 mutex_unlock(&data->mutex);
914
915 return 0;
916}
917#else
918#define mma9551_suspend NULL
919#define mma9551_resume NULL
920#endif
921
922static const struct dev_pm_ops mma9551_pm_ops = {
923 SET_SYSTEM_SLEEP_PM_OPS(mma9551_suspend, mma9551_resume)
924};
925
926static const struct acpi_device_id mma9551_acpi_match[] = {
927 {"MMA9551", 0},
928 {},
929};
930
931MODULE_DEVICE_TABLE(acpi, mma9551_acpi_match);
932
933static const struct i2c_device_id mma9551_id[] = {
934 {"mma9551", 0},
935 {}
936};
937
938MODULE_DEVICE_TABLE(i2c, mma9551_id);
939
940static struct i2c_driver mma9551_driver = {
941 .driver = {
942 .name = MMA9551_DRV_NAME,
943 .acpi_match_table = ACPI_PTR(mma9551_acpi_match),
944 .pm = &mma9551_pm_ops,
945 },
946 .probe = mma9551_probe,
947 .remove = mma9551_remove,
948 .id_table = mma9551_id,
949};
950
951module_i2c_driver(mma9551_driver);
952
953MODULE_AUTHOR("Irina Tirdea <irina.tirdea@intel.com>");
954MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
955MODULE_LICENSE("GPL v2");
956MODULE_DESCRIPTION("MMA9551L motion-sensing platform driver");
diff --git a/drivers/iio/adc/ti_am335x_adc.c b/drivers/iio/adc/ti_am335x_adc.c
index b730864731e8..5eea299518a3 100644
--- a/drivers/iio/adc/ti_am335x_adc.c
+++ b/drivers/iio/adc/ti_am335x_adc.c
@@ -250,7 +250,7 @@ static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
250 struct iio_buffer *buffer; 250 struct iio_buffer *buffer;
251 int ret; 251 int ret;
252 252
253 buffer = iio_kfifo_allocate(indio_dev); 253 buffer = iio_kfifo_allocate();
254 if (!buffer) 254 if (!buffer)
255 return -ENOMEM; 255 return -ENOMEM;
256 256
@@ -264,16 +264,8 @@ static int tiadc_iio_buffered_hardware_setup(struct iio_dev *indio_dev,
264 indio_dev->setup_ops = setup_ops; 264 indio_dev->setup_ops = setup_ops;
265 indio_dev->modes |= INDIO_BUFFER_HARDWARE; 265 indio_dev->modes |= INDIO_BUFFER_HARDWARE;
266 266
267 ret = iio_buffer_register(indio_dev,
268 indio_dev->channels,
269 indio_dev->num_channels);
270 if (ret)
271 goto error_free_irq;
272
273 return 0; 267 return 0;
274 268
275error_free_irq:
276 free_irq(irq, indio_dev);
277error_kfifo_free: 269error_kfifo_free:
278 iio_kfifo_free(indio_dev->buffer); 270 iio_kfifo_free(indio_dev->buffer);
279 return ret; 271 return ret;
@@ -285,7 +277,6 @@ static void tiadc_iio_buffered_hardware_remove(struct iio_dev *indio_dev)
285 277
286 free_irq(adc_dev->mfd_tscadc->irq, indio_dev); 278 free_irq(adc_dev->mfd_tscadc->irq, indio_dev);
287 iio_kfifo_free(indio_dev->buffer); 279 iio_kfifo_free(indio_dev->buffer);
288 iio_buffer_unregister(indio_dev);
289} 280}
290 281
291 282
diff --git a/drivers/iio/amplifiers/ad8366.c b/drivers/iio/amplifiers/ad8366.c
index ba6f6a91dfff..c0d364ebaea8 100644
--- a/drivers/iio/amplifiers/ad8366.c
+++ b/drivers/iio/amplifiers/ad8366.c
@@ -31,7 +31,7 @@ struct ad8366_state {
31}; 31};
32 32
33static int ad8366_write(struct iio_dev *indio_dev, 33static int ad8366_write(struct iio_dev *indio_dev,
34 unsigned char ch_a, char unsigned ch_b) 34 unsigned char ch_a, unsigned char ch_b)
35{ 35{
36 struct ad8366_state *st = iio_priv(indio_dev); 36 struct ad8366_state *st = iio_priv(indio_dev);
37 int ret; 37 int ret;
@@ -166,7 +166,7 @@ static int ad8366_probe(struct spi_device *spi)
166 if (ret) 166 if (ret)
167 goto error_disable_reg; 167 goto error_disable_reg;
168 168
169 ad8366_write(indio_dev, 0 , 0); 169 ad8366_write(indio_dev, 0, 0);
170 170
171 return 0; 171 return 0;
172 172
diff --git a/drivers/iio/common/st_sensors/st_sensors_spi.c b/drivers/iio/common/st_sensors/st_sensors_spi.c
index 78a6a1ab3ece..5b377373f48d 100644
--- a/drivers/iio/common/st_sensors/st_sensors_spi.c
+++ b/drivers/iio/common/st_sensors/st_sensors_spi.c
@@ -54,7 +54,7 @@ static int st_sensors_spi_read(struct st_sensor_transfer_buffer *tb,
54 if (err) 54 if (err)
55 goto acc_spi_read_error; 55 goto acc_spi_read_error;
56 56
57 memcpy(data, tb->rx_buf, len*sizeof(u8)); 57 memcpy(data, tb->rx_buf, len);
58 mutex_unlock(&tb->buf_lock); 58 mutex_unlock(&tb->buf_lock);
59 return len; 59 return len;
60 60
diff --git a/drivers/iio/frequency/ad9523.c b/drivers/iio/frequency/ad9523.c
index 7c5245d9f99c..50ed8d1ca45a 100644
--- a/drivers/iio/frequency/ad9523.c
+++ b/drivers/iio/frequency/ad9523.c
@@ -445,7 +445,7 @@ static int ad9523_store_eeprom(struct iio_dev *indio_dev)
445 445
446 tmp = 4; 446 tmp = 4;
447 do { 447 do {
448 msleep(16); 448 msleep(20);
449 ret = ad9523_read(indio_dev, 449 ret = ad9523_read(indio_dev,
450 AD9523_EEPROM_DATA_XFER_STATUS); 450 AD9523_EEPROM_DATA_XFER_STATUS);
451 if (ret < 0) 451 if (ret < 0)
diff --git a/drivers/iio/frequency/adf4350.c b/drivers/iio/frequency/adf4350.c
index 63a25d9e1204..10a0dfc3b01f 100644
--- a/drivers/iio/frequency/adf4350.c
+++ b/drivers/iio/frequency/adf4350.c
@@ -387,10 +387,8 @@ static struct adf4350_platform_data *adf4350_parse_dt(struct device *dev)
387 int ret; 387 int ret;
388 388
389 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 389 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
390 if (!pdata) { 390 if (!pdata)
391 dev_err(dev, "could not allocate memory for platform data\n");
392 return NULL; 391 return NULL;
393 }
394 392
395 strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1); 393 strncpy(&pdata->name[0], np->name, SPI_NAME_SIZE - 1);
396 394
@@ -613,9 +611,8 @@ static int adf4350_remove(struct spi_device *spi)
613 if (st->clk) 611 if (st->clk)
614 clk_disable_unprepare(st->clk); 612 clk_disable_unprepare(st->clk);
615 613
616 if (!IS_ERR(reg)) { 614 if (!IS_ERR(reg))
617 regulator_disable(reg); 615 regulator_disable(reg);
618 }
619 616
620 return 0; 617 return 0;
621} 618}
diff --git a/drivers/iio/iio_core.h b/drivers/iio/iio_core.h
index 5f0ea77fe717..359883525ab7 100644
--- a/drivers/iio/iio_core.h
+++ b/drivers/iio/iio_core.h
@@ -48,6 +48,8 @@ unsigned int iio_buffer_poll(struct file *filp,
48ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf, 48ssize_t iio_buffer_read_first_n_outer(struct file *filp, char __user *buf,
49 size_t n, loff_t *f_ps); 49 size_t n, loff_t *f_ps);
50 50
51int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev);
52void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev);
51 53
52#define iio_buffer_poll_addr (&iio_buffer_poll) 54#define iio_buffer_poll_addr (&iio_buffer_poll)
53#define iio_buffer_read_first_n_outer_addr (&iio_buffer_read_first_n_outer) 55#define iio_buffer_read_first_n_outer_addr (&iio_buffer_read_first_n_outer)
@@ -60,6 +62,13 @@ void iio_buffer_wakeup_poll(struct iio_dev *indio_dev);
60#define iio_buffer_poll_addr NULL 62#define iio_buffer_poll_addr NULL
61#define iio_buffer_read_first_n_outer_addr NULL 63#define iio_buffer_read_first_n_outer_addr NULL
62 64
65static inline int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
66{
67 return 0;
68}
69
70static inline void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) {}
71
63static inline void iio_disable_all_buffers(struct iio_dev *indio_dev) {} 72static inline void iio_disable_all_buffers(struct iio_dev *indio_dev) {}
64static inline void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) {} 73static inline void iio_buffer_wakeup_poll(struct iio_dev *indio_dev) {}
65 74
diff --git a/drivers/iio/imu/Kconfig b/drivers/iio/imu/Kconfig
index 2b0e45133e9d..5e610f7de5aa 100644
--- a/drivers/iio/imu/Kconfig
+++ b/drivers/iio/imu/Kconfig
@@ -25,6 +25,17 @@ config ADIS16480
25 Say yes here to build support for Analog Devices ADIS16375, ADIS16480, 25 Say yes here to build support for Analog Devices ADIS16375, ADIS16480,
26 ADIS16485, ADIS16488 inertial sensors. 26 ADIS16485, ADIS16488 inertial sensors.
27 27
28config KMX61
29 tristate "Kionix KMX61 6-axis accelerometer and magnetometer"
30 depends on I2C
31 select IIO_BUFFER
32 select IIO_TRIGGERED_BUFFER
33 help
34 Say Y here if you want to build a driver for Kionix KMX61 6-axis
35 accelerometer and magnetometer.
36 To compile this driver as module, choose M here: the module will
37 be called kmx61.
38
28source "drivers/iio/imu/inv_mpu6050/Kconfig" 39source "drivers/iio/imu/inv_mpu6050/Kconfig"
29 40
30endmenu 41endmenu
diff --git a/drivers/iio/imu/Makefile b/drivers/iio/imu/Makefile
index 114d2c17cbe2..e1e6e3d70e26 100644
--- a/drivers/iio/imu/Makefile
+++ b/drivers/iio/imu/Makefile
@@ -14,3 +14,5 @@ adis_lib-$(CONFIG_IIO_ADIS_LIB_BUFFER) += adis_buffer.o
14obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o 14obj-$(CONFIG_IIO_ADIS_LIB) += adis_lib.o
15 15
16obj-y += inv_mpu6050/ 16obj-y += inv_mpu6050/
17
18obj-$(CONFIG_KMX61) += kmx61.o
diff --git a/drivers/iio/imu/inv_mpu6050/Kconfig b/drivers/iio/imu/inv_mpu6050/Kconfig
index 2d0608ba88d7..48fbc0bc7e2a 100644
--- a/drivers/iio/imu/inv_mpu6050/Kconfig
+++ b/drivers/iio/imu/inv_mpu6050/Kconfig
@@ -7,6 +7,7 @@ config INV_MPU6050_IIO
7 depends on I2C && SYSFS 7 depends on I2C && SYSFS
8 select IIO_BUFFER 8 select IIO_BUFFER
9 select IIO_TRIGGERED_BUFFER 9 select IIO_TRIGGERED_BUFFER
10 select I2C_MUX
10 help 11 help
11 This driver supports the Invensense MPU6050 devices. 12 This driver supports the Invensense MPU6050 devices.
12 This driver can also support MPU6500 in MPU6050 compatibility mode 13 This driver can also support MPU6500 in MPU6050 compatibility mode
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
index b75519deac1a..f73e60b7a796 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_core.c
@@ -23,6 +23,8 @@
23#include <linux/kfifo.h> 23#include <linux/kfifo.h>
24#include <linux/spinlock.h> 24#include <linux/spinlock.h>
25#include <linux/iio/iio.h> 25#include <linux/iio/iio.h>
26#include <linux/i2c-mux.h>
27#include <linux/acpi.h>
26#include "inv_mpu_iio.h" 28#include "inv_mpu_iio.h"
27 29
28/* 30/*
@@ -52,6 +54,7 @@ static const struct inv_mpu6050_reg_map reg_set_6050 = {
52 .int_enable = INV_MPU6050_REG_INT_ENABLE, 54 .int_enable = INV_MPU6050_REG_INT_ENABLE,
53 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1, 55 .pwr_mgmt_1 = INV_MPU6050_REG_PWR_MGMT_1,
54 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2, 56 .pwr_mgmt_2 = INV_MPU6050_REG_PWR_MGMT_2,
57 .int_pin_cfg = INV_MPU6050_REG_INT_PIN_CFG,
55}; 58};
56 59
57static const struct inv_mpu6050_chip_config chip_config_6050 = { 60static const struct inv_mpu6050_chip_config chip_config_6050 = {
@@ -77,6 +80,83 @@ int inv_mpu6050_write_reg(struct inv_mpu6050_state *st, int reg, u8 d)
77 return i2c_smbus_write_i2c_block_data(st->client, reg, 1, &d); 80 return i2c_smbus_write_i2c_block_data(st->client, reg, 1, &d);
78} 81}
79 82
83/*
84 * The i2c read/write needs to happen in unlocked mode. As the parent
85 * adapter is common. If we use locked versions, it will fail as
86 * the mux adapter will lock the parent i2c adapter, while calling
87 * select/deselect functions.
88 */
89static int inv_mpu6050_write_reg_unlocked(struct inv_mpu6050_state *st,
90 u8 reg, u8 d)
91{
92 int ret;
93 u8 buf[2];
94 struct i2c_msg msg[1] = {
95 {
96 .addr = st->client->addr,
97 .flags = 0,
98 .len = sizeof(buf),
99 .buf = buf,
100 }
101 };
102
103 buf[0] = reg;
104 buf[1] = d;
105 ret = __i2c_transfer(st->client->adapter, msg, 1);
106 if (ret != 1)
107 return ret;
108
109 return 0;
110}
111
112static int inv_mpu6050_select_bypass(struct i2c_adapter *adap, void *mux_priv,
113 u32 chan_id)
114{
115 struct iio_dev *indio_dev = mux_priv;
116 struct inv_mpu6050_state *st = iio_priv(indio_dev);
117 int ret = 0;
118
119 /* Use the same mutex which was used everywhere to protect power-op */
120 mutex_lock(&indio_dev->mlock);
121 if (!st->powerup_count) {
122 ret = inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
123 0);
124 if (ret)
125 goto write_error;
126
127 msleep(INV_MPU6050_REG_UP_TIME);
128 }
129 if (!ret) {
130 st->powerup_count++;
131 ret = inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
132 st->client->irq |
133 INV_MPU6050_BIT_BYPASS_EN);
134 }
135write_error:
136 mutex_unlock(&indio_dev->mlock);
137
138 return ret;
139}
140
141static int inv_mpu6050_deselect_bypass(struct i2c_adapter *adap,
142 void *mux_priv, u32 chan_id)
143{
144 struct iio_dev *indio_dev = mux_priv;
145 struct inv_mpu6050_state *st = iio_priv(indio_dev);
146
147 mutex_lock(&indio_dev->mlock);
148 /* It doesn't really mattter, if any of the calls fails */
149 inv_mpu6050_write_reg_unlocked(st, st->reg->int_pin_cfg,
150 st->client->irq);
151 st->powerup_count--;
152 if (!st->powerup_count)
153 inv_mpu6050_write_reg_unlocked(st, st->reg->pwr_mgmt_1,
154 INV_MPU6050_BIT_SLEEP);
155 mutex_unlock(&indio_dev->mlock);
156
157 return 0;
158}
159
80int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask) 160int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
81{ 161{
82 u8 d, mgmt_1; 162 u8 d, mgmt_1;
@@ -133,13 +213,22 @@ int inv_mpu6050_switch_engine(struct inv_mpu6050_state *st, bool en, u32 mask)
133 213
134int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on) 214int inv_mpu6050_set_power_itg(struct inv_mpu6050_state *st, bool power_on)
135{ 215{
136 int result; 216 int result = 0;
217
218 if (power_on) {
219 /* Already under indio-dev->mlock mutex */
220 if (!st->powerup_count)
221 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
222 0);
223 if (!result)
224 st->powerup_count++;
225 } else {
226 st->powerup_count--;
227 if (!st->powerup_count)
228 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
229 INV_MPU6050_BIT_SLEEP);
230 }
137 231
138 if (power_on)
139 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1, 0);
140 else
141 result = inv_mpu6050_write_reg(st, st->reg->pwr_mgmt_1,
142 INV_MPU6050_BIT_SLEEP);
143 if (result) 232 if (result)
144 return result; 233 return result;
145 234
@@ -673,6 +762,7 @@ static int inv_mpu_probe(struct i2c_client *client,
673 762
674 st = iio_priv(indio_dev); 763 st = iio_priv(indio_dev);
675 st->client = client; 764 st->client = client;
765 st->powerup_count = 0;
676 pdata = dev_get_platdata(&client->dev); 766 pdata = dev_get_platdata(&client->dev);
677 if (pdata) 767 if (pdata)
678 st->plat_data = *pdata; 768 st->plat_data = *pdata;
@@ -720,8 +810,21 @@ static int inv_mpu_probe(struct i2c_client *client,
720 goto out_remove_trigger; 810 goto out_remove_trigger;
721 } 811 }
722 812
813 st->mux_adapter = i2c_add_mux_adapter(client->adapter,
814 &client->dev,
815 indio_dev,
816 0, 0, 0,
817 inv_mpu6050_select_bypass,
818 inv_mpu6050_deselect_bypass);
819 if (!st->mux_adapter) {
820 result = -ENODEV;
821 goto out_unreg_device;
822 }
823
723 return 0; 824 return 0;
724 825
826out_unreg_device:
827 iio_device_unregister(indio_dev);
725out_remove_trigger: 828out_remove_trigger:
726 inv_mpu6050_remove_trigger(st); 829 inv_mpu6050_remove_trigger(st);
727out_unreg_ring: 830out_unreg_ring:
@@ -734,6 +837,7 @@ static int inv_mpu_remove(struct i2c_client *client)
734 struct iio_dev *indio_dev = i2c_get_clientdata(client); 837 struct iio_dev *indio_dev = i2c_get_clientdata(client);
735 struct inv_mpu6050_state *st = iio_priv(indio_dev); 838 struct inv_mpu6050_state *st = iio_priv(indio_dev);
736 839
840 i2c_del_mux_adapter(st->mux_adapter);
737 iio_device_unregister(indio_dev); 841 iio_device_unregister(indio_dev);
738 inv_mpu6050_remove_trigger(st); 842 inv_mpu6050_remove_trigger(st);
739 iio_triggered_buffer_cleanup(indio_dev); 843 iio_triggered_buffer_cleanup(indio_dev);
@@ -772,6 +876,13 @@ static const struct i2c_device_id inv_mpu_id[] = {
772 876
773MODULE_DEVICE_TABLE(i2c, inv_mpu_id); 877MODULE_DEVICE_TABLE(i2c, inv_mpu_id);
774 878
879static const struct acpi_device_id inv_acpi_match[] = {
880 {"INVN6500", 0},
881 { },
882};
883
884MODULE_DEVICE_TABLE(acpi, inv_acpi_match);
885
775static struct i2c_driver inv_mpu_driver = { 886static struct i2c_driver inv_mpu_driver = {
776 .probe = inv_mpu_probe, 887 .probe = inv_mpu_probe,
777 .remove = inv_mpu_remove, 888 .remove = inv_mpu_remove,
@@ -780,6 +891,7 @@ static struct i2c_driver inv_mpu_driver = {
780 .owner = THIS_MODULE, 891 .owner = THIS_MODULE,
781 .name = "inv-mpu6050", 892 .name = "inv-mpu6050",
782 .pm = INV_MPU6050_PMOPS, 893 .pm = INV_MPU6050_PMOPS,
894 .acpi_match_table = ACPI_PTR(inv_acpi_match),
783 }, 895 },
784}; 896};
785 897
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
index e7799315d4dc..aa837de57079 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_iio.h
@@ -54,6 +54,7 @@ struct inv_mpu6050_reg_map {
54 u8 int_enable; 54 u8 int_enable;
55 u8 pwr_mgmt_1; 55 u8 pwr_mgmt_1;
56 u8 pwr_mgmt_2; 56 u8 pwr_mgmt_2;
57 u8 int_pin_cfg;
57}; 58};
58 59
59/*device enum */ 60/*device enum */
@@ -119,6 +120,8 @@ struct inv_mpu6050_state {
119 enum inv_devices chip_type; 120 enum inv_devices chip_type;
120 spinlock_t time_stamp_lock; 121 spinlock_t time_stamp_lock;
121 struct i2c_client *client; 122 struct i2c_client *client;
123 struct i2c_adapter *mux_adapter;
124 unsigned int powerup_count;
122 struct inv_mpu6050_platform_data plat_data; 125 struct inv_mpu6050_platform_data plat_data;
123 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE); 126 DECLARE_KFIFO(timestamps, long long, TIMESTAMP_FIFO_SIZE);
124}; 127};
@@ -179,6 +182,9 @@ struct inv_mpu6050_state {
179/* 6 + 6 round up and plus 8 */ 182/* 6 + 6 round up and plus 8 */
180#define INV_MPU6050_OUTPUT_DATA_SIZE 24 183#define INV_MPU6050_OUTPUT_DATA_SIZE 24
181 184
185#define INV_MPU6050_REG_INT_PIN_CFG 0x37
186#define INV_MPU6050_BIT_BYPASS_EN 0x2
187
182/* init parameters */ 188/* init parameters */
183#define INV_MPU6050_INIT_FIFO_RATE 50 189#define INV_MPU6050_INIT_FIFO_RATE 50
184#define INV_MPU6050_TIME_STAMP_TOR 5 190#define INV_MPU6050_TIME_STAMP_TOR 5
diff --git a/drivers/iio/imu/kmx61.c b/drivers/iio/imu/kmx61.c
new file mode 100644
index 000000000000..5cc3692acf37
--- /dev/null
+++ b/drivers/iio/imu/kmx61.c
@@ -0,0 +1,1595 @@
1/*
2 * KMX61 - Kionix 6-axis Accelerometer/Magnetometer
3 *
4 * Copyright (c) 2014, Intel Corporation.
5 *
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
9 *
10 * IIO driver for KMX61 (7-bit I2C slave address 0x0E or 0x0F).
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/i2c.h>
16#include <linux/acpi.h>
17#include <linux/gpio/consumer.h>
18#include <linux/interrupt.h>
19#include <linux/pm.h>
20#include <linux/pm_runtime.h>
21#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
23#include <linux/iio/events.h>
24#include <linux/iio/trigger.h>
25#include <linux/iio/buffer.h>
26#include <linux/iio/triggered_buffer.h>
27#include <linux/iio/trigger_consumer.h>
28
29#define KMX61_DRV_NAME "kmx61"
30#define KMX61_GPIO_NAME "kmx61_int"
31#define KMX61_IRQ_NAME "kmx61_event"
32
33#define KMX61_REG_WHO_AM_I 0x00
34#define KMX61_REG_INS1 0x01
35#define KMX61_REG_INS2 0x02
36
37/*
38 * three 16-bit accelerometer output registers for X/Y/Z axis
39 * we use only XOUT_L as a base register, all other addresses
40 * can be obtained by applying an offset and are provided here
41 * only for clarity.
42 */
43#define KMX61_ACC_XOUT_L 0x0A
44#define KMX61_ACC_XOUT_H 0x0B
45#define KMX61_ACC_YOUT_L 0x0C
46#define KMX61_ACC_YOUT_H 0x0D
47#define KMX61_ACC_ZOUT_L 0x0E
48#define KMX61_ACC_ZOUT_H 0x0F
49
50/*
51 * one 16-bit temperature output register
52 */
53#define KMX61_TEMP_L 0x10
54#define KMX61_TEMP_H 0x11
55
56/*
57 * three 16-bit magnetometer output registers for X/Y/Z axis
58 */
59#define KMX61_MAG_XOUT_L 0x12
60#define KMX61_MAG_XOUT_H 0x13
61#define KMX61_MAG_YOUT_L 0x14
62#define KMX61_MAG_YOUT_H 0x15
63#define KMX61_MAG_ZOUT_L 0x16
64#define KMX61_MAG_ZOUT_H 0x17
65
66#define KMX61_REG_INL 0x28
67#define KMX61_REG_STBY 0x29
68#define KMX61_REG_CTRL1 0x2A
69#define KMX61_REG_CTRL2 0x2B
70#define KMX61_REG_ODCNTL 0x2C
71#define KMX61_REG_INC1 0x2D
72
73#define KMX61_REG_WUF_THRESH 0x3D
74#define KMX61_REG_WUF_TIMER 0x3E
75
76#define KMX61_ACC_STBY_BIT BIT(0)
77#define KMX61_MAG_STBY_BIT BIT(1)
78#define KMX61_ACT_STBY_BIT BIT(7)
79
80#define KMX61_ALL_STBY (KMX61_ACC_STBY_BIT | KMX61_MAG_STBY_BIT)
81
82#define KMX61_REG_INS1_BIT_WUFS BIT(1)
83
84#define KMX61_REG_INS2_BIT_ZP BIT(0)
85#define KMX61_REG_INS2_BIT_ZN BIT(1)
86#define KMX61_REG_INS2_BIT_YP BIT(2)
87#define KMX61_REG_INS2_BIT_YN BIT(3)
88#define KMX61_REG_INS2_BIT_XP BIT(4)
89#define KMX61_REG_INS2_BIT_XN BIT(5)
90
91#define KMX61_REG_CTRL1_GSEL_MASK 0x03
92
93#define KMX61_REG_CTRL1_BIT_RES BIT(4)
94#define KMX61_REG_CTRL1_BIT_DRDYE BIT(5)
95#define KMX61_REG_CTRL1_BIT_WUFE BIT(6)
96#define KMX61_REG_CTRL1_BIT_BTSE BIT(7)
97
98#define KMX61_REG_INC1_BIT_WUFS BIT(0)
99#define KMX61_REG_INC1_BIT_DRDYM BIT(1)
100#define KMX61_REG_INC1_BIT_DRDYA BIT(2)
101#define KMX61_REG_INC1_BIT_IEN BIT(5)
102
103#define KMX61_ACC_ODR_SHIFT 0
104#define KMX61_MAG_ODR_SHIFT 4
105#define KMX61_ACC_ODR_MASK 0x0F
106#define KMX61_MAG_ODR_MASK 0xF0
107
108#define KMX61_OWUF_MASK 0x7
109
110#define KMX61_DEFAULT_WAKE_THRESH 1
111#define KMX61_DEFAULT_WAKE_DURATION 1
112
113#define KMX61_SLEEP_DELAY_MS 2000
114
115#define KMX61_CHIP_ID 0x12
116
117/* KMX61 devices */
118#define KMX61_ACC 0x01
119#define KMX61_MAG 0x02
120
121struct kmx61_data {
122 struct i2c_client *client;
123
124 /* serialize access to non-atomic ops, e.g set_mode */
125 struct mutex lock;
126
127 /* standby state */
128 bool acc_stby;
129 bool mag_stby;
130
131 /* power state */
132 bool acc_ps;
133 bool mag_ps;
134
135 /* config bits */
136 u8 range;
137 u8 odr_bits;
138 u8 wake_thresh;
139 u8 wake_duration;
140
141 /* accelerometer specific data */
142 struct iio_dev *acc_indio_dev;
143 struct iio_trigger *acc_dready_trig;
144 struct iio_trigger *motion_trig;
145 bool acc_dready_trig_on;
146 bool motion_trig_on;
147 bool ev_enable_state;
148
149 /* magnetometer specific data */
150 struct iio_dev *mag_indio_dev;
151 struct iio_trigger *mag_dready_trig;
152 bool mag_dready_trig_on;
153};
154
155enum kmx61_range {
156 KMX61_RANGE_2G,
157 KMX61_RANGE_4G,
158 KMX61_RANGE_8G,
159};
160
161enum kmx61_axis {
162 KMX61_AXIS_X,
163 KMX61_AXIS_Y,
164 KMX61_AXIS_Z,
165};
166
167static const u16 kmx61_uscale_table[] = {9582, 19163, 38326};
168
169static const struct {
170 int val;
171 int val2;
172 u8 odr_bits;
173} kmx61_samp_freq_table[] = { {12, 500000, 0x00},
174 {25, 0, 0x01},
175 {50, 0, 0x02},
176 {100, 0, 0x03},
177 {200, 0, 0x04},
178 {400, 0, 0x05},
179 {800, 0, 0x06},
180 {1600, 0, 0x07},
181 {0, 781000, 0x08},
182 {1, 563000, 0x09},
183 {3, 125000, 0x0A},
184 {6, 250000, 0x0B} };
185
186static const struct {
187 int val;
188 int val2;
189 int odr_bits;
190} kmx61_wake_up_odr_table[] = { {0, 781000, 0x00},
191 {1, 563000, 0x01},
192 {3, 125000, 0x02},
193 {6, 250000, 0x03},
194 {12, 500000, 0x04},
195 {25, 0, 0x05},
196 {50, 0, 0x06},
197 {100, 0, 0x06},
198 {200, 0, 0x06},
199 {400, 0, 0x06},
200 {800, 0, 0x06},
201 {1600, 0, 0x06} };
202
203static IIO_CONST_ATTR(accel_scale_available, "0.009582 0.019163 0.038326");
204static IIO_CONST_ATTR(magn_scale_available, "0.001465");
205static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
206 "0.781000 1.563000 3.125000 6.250000 12.500000 25 50 100 200 400 800");
207
208static struct attribute *kmx61_acc_attributes[] = {
209 &iio_const_attr_accel_scale_available.dev_attr.attr,
210 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
211 NULL,
212};
213
214static struct attribute *kmx61_mag_attributes[] = {
215 &iio_const_attr_magn_scale_available.dev_attr.attr,
216 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
217 NULL,
218};
219
220static const struct attribute_group kmx61_acc_attribute_group = {
221 .attrs = kmx61_acc_attributes,
222};
223
224static const struct attribute_group kmx61_mag_attribute_group = {
225 .attrs = kmx61_mag_attributes,
226};
227
228static const struct iio_event_spec kmx61_event = {
229 .type = IIO_EV_TYPE_THRESH,
230 .dir = IIO_EV_DIR_EITHER,
231 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
232 BIT(IIO_EV_INFO_ENABLE) |
233 BIT(IIO_EV_INFO_PERIOD),
234};
235
236#define KMX61_ACC_CHAN(_axis) { \
237 .type = IIO_ACCEL, \
238 .modified = 1, \
239 .channel2 = IIO_MOD_ ## _axis, \
240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
241 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
242 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
243 .address = KMX61_ACC, \
244 .scan_index = KMX61_AXIS_ ## _axis, \
245 .scan_type = { \
246 .sign = 's', \
247 .realbits = 12, \
248 .storagebits = 16, \
249 .shift = 4, \
250 .endianness = IIO_LE, \
251 }, \
252 .event_spec = &kmx61_event, \
253 .num_event_specs = 1 \
254}
255
256#define KMX61_MAG_CHAN(_axis) { \
257 .type = IIO_MAGN, \
258 .modified = 1, \
259 .channel2 = IIO_MOD_ ## _axis, \
260 .address = KMX61_MAG, \
261 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
262 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
263 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
264 .scan_index = KMX61_AXIS_ ## _axis, \
265 .scan_type = { \
266 .sign = 's', \
267 .realbits = 14, \
268 .storagebits = 16, \
269 .shift = 2, \
270 .endianness = IIO_LE, \
271 }, \
272}
273
274static const struct iio_chan_spec kmx61_acc_channels[] = {
275 KMX61_ACC_CHAN(X),
276 KMX61_ACC_CHAN(Y),
277 KMX61_ACC_CHAN(Z),
278};
279
280static const struct iio_chan_spec kmx61_mag_channels[] = {
281 KMX61_MAG_CHAN(X),
282 KMX61_MAG_CHAN(Y),
283 KMX61_MAG_CHAN(Z),
284};
285
286static void kmx61_set_data(struct iio_dev *indio_dev, struct kmx61_data *data)
287{
288 struct kmx61_data **priv = iio_priv(indio_dev);
289
290 *priv = data;
291}
292
293static struct kmx61_data *kmx61_get_data(struct iio_dev *indio_dev)
294{
295 return *(struct kmx61_data **)iio_priv(indio_dev);
296}
297
298static int kmx61_convert_freq_to_bit(int val, int val2)
299{
300 int i;
301
302 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
303 if (val == kmx61_samp_freq_table[i].val &&
304 val2 == kmx61_samp_freq_table[i].val2)
305 return kmx61_samp_freq_table[i].odr_bits;
306 return -EINVAL;
307}
308
309static int kmx61_convert_bit_to_freq(u8 odr_bits, int *val, int *val2)
310{
311 int i;
312
313 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
314 if (odr_bits == kmx61_samp_freq_table[i].odr_bits) {
315 *val = kmx61_samp_freq_table[i].val;
316 *val2 = kmx61_samp_freq_table[i].val2;
317 return 0;
318 }
319 return -EINVAL;
320}
321
322
323static int kmx61_convert_wake_up_odr_to_bit(int val, int val2)
324{
325 int i;
326
327 for (i = 0; i < ARRAY_SIZE(kmx61_wake_up_odr_table); ++i)
328 if (kmx61_wake_up_odr_table[i].val == val &&
329 kmx61_wake_up_odr_table[i].val2 == val2)
330 return kmx61_wake_up_odr_table[i].odr_bits;
331 return -EINVAL;
332}
333
334/**
335 * kmx61_set_mode() - set KMX61 device operating mode
336 * @data - kmx61 device private data pointer
337 * @mode - bitmask, indicating operating mode for @device
338 * @device - bitmask, indicating device for which @mode needs to be set
339 * @update - update stby bits stored in device's private @data
340 *
341 * For each sensor (accelerometer/magnetometer) there are two operating modes
342 * STANDBY and OPERATION. Neither accel nor magn can be disabled independently
343 * if they are both enabled. Internal sensors state is saved in acc_stby and
344 * mag_stby members of driver's private @data.
345 */
346static int kmx61_set_mode(struct kmx61_data *data, u8 mode, u8 device,
347 bool update)
348{
349 int ret;
350 int acc_stby = -1, mag_stby = -1;
351
352 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
353 if (ret < 0) {
354 dev_err(&data->client->dev, "Error reading reg_stby\n");
355 return ret;
356 }
357 if (device & KMX61_ACC) {
358 if (mode & KMX61_ACC_STBY_BIT) {
359 ret |= KMX61_ACC_STBY_BIT;
360 acc_stby = 1;
361 } else {
362 ret &= ~KMX61_ACC_STBY_BIT;
363 acc_stby = 0;
364 }
365 }
366
367 if (device & KMX61_MAG) {
368 if (mode & KMX61_MAG_STBY_BIT) {
369 ret |= KMX61_MAG_STBY_BIT;
370 mag_stby = 1;
371 } else {
372 ret &= ~KMX61_MAG_STBY_BIT;
373 mag_stby = 0;
374 }
375 }
376
377 if (mode & KMX61_ACT_STBY_BIT)
378 ret |= KMX61_ACT_STBY_BIT;
379
380 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_STBY, ret);
381 if (ret < 0) {
382 dev_err(&data->client->dev, "Error writing reg_stby\n");
383 return ret;
384 }
385
386 if (acc_stby != -1 && update)
387 data->acc_stby = acc_stby;
388 if (mag_stby != -1 && update)
389 data->mag_stby = mag_stby;
390
391 return 0;
392}
393
394static int kmx61_get_mode(struct kmx61_data *data, u8 *mode, u8 device)
395{
396 int ret;
397
398 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_STBY);
399 if (ret < 0) {
400 dev_err(&data->client->dev, "Error reading reg_stby\n");
401 return ret;
402 }
403 *mode = 0;
404
405 if (device & KMX61_ACC) {
406 if (ret & KMX61_ACC_STBY_BIT)
407 *mode |= KMX61_ACC_STBY_BIT;
408 else
409 *mode &= ~KMX61_ACC_STBY_BIT;
410 }
411
412 if (device & KMX61_MAG) {
413 if (ret & KMX61_MAG_STBY_BIT)
414 *mode |= KMX61_MAG_STBY_BIT;
415 else
416 *mode &= ~KMX61_MAG_STBY_BIT;
417 }
418
419 return 0;
420}
421
422static int kmx61_set_wake_up_odr(struct kmx61_data *data, int val, int val2)
423{
424 int ret, odr_bits;
425
426 odr_bits = kmx61_convert_wake_up_odr_to_bit(val, val2);
427 if (odr_bits < 0)
428 return odr_bits;
429
430 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL2,
431 odr_bits);
432 if (ret < 0)
433 dev_err(&data->client->dev, "Error writing reg_ctrl2\n");
434 return ret;
435}
436
437static int kmx61_set_odr(struct kmx61_data *data, int val, int val2, u8 device)
438{
439 int ret;
440 u8 mode;
441 int lodr_bits, odr_bits;
442
443 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
444 if (ret < 0)
445 return ret;
446
447 lodr_bits = kmx61_convert_freq_to_bit(val, val2);
448 if (lodr_bits < 0)
449 return lodr_bits;
450
451 /* To change ODR, accel and magn must be in STDBY */
452 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
453 true);
454 if (ret < 0)
455 return ret;
456
457 odr_bits = 0;
458 if (device & KMX61_ACC)
459 odr_bits |= lodr_bits << KMX61_ACC_ODR_SHIFT;
460 if (device & KMX61_MAG)
461 odr_bits |= lodr_bits << KMX61_MAG_ODR_SHIFT;
462
463 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_ODCNTL,
464 odr_bits);
465 if (ret < 0)
466 return ret;
467
468 data->odr_bits = odr_bits;
469
470 if (device & KMX61_ACC) {
471 ret = kmx61_set_wake_up_odr(data, val, val2);
472 if (ret)
473 return ret;
474 }
475
476 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
477}
478
479static int kmx61_get_odr(struct kmx61_data *data, int *val, int *val2,
480 u8 device)
481{ int i;
482 u8 lodr_bits;
483
484 if (device & KMX61_ACC)
485 lodr_bits = (data->odr_bits >> KMX61_ACC_ODR_SHIFT) &
486 KMX61_ACC_ODR_MASK;
487 else if (device & KMX61_MAG)
488 lodr_bits = (data->odr_bits >> KMX61_MAG_ODR_SHIFT) &
489 KMX61_MAG_ODR_MASK;
490 else
491 return -EINVAL;
492
493 for (i = 0; i < ARRAY_SIZE(kmx61_samp_freq_table); i++)
494 if (lodr_bits == kmx61_samp_freq_table[i].odr_bits) {
495 *val = kmx61_samp_freq_table[i].val;
496 *val2 = kmx61_samp_freq_table[i].val2;
497 return 0;
498 }
499 return -EINVAL;
500}
501
502static int kmx61_set_range(struct kmx61_data *data, u8 range)
503{
504 int ret;
505
506 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
507 if (ret < 0) {
508 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
509 return ret;
510 }
511
512 ret &= ~KMX61_REG_CTRL1_GSEL_MASK;
513 ret |= range & KMX61_REG_CTRL1_GSEL_MASK;
514
515 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
516 if (ret < 0) {
517 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
518 return ret;
519 }
520
521 data->range = range;
522
523 return 0;
524}
525
526static int kmx61_set_scale(struct kmx61_data *data, u16 uscale)
527{
528 int ret, i;
529 u8 mode;
530
531 for (i = 0; i < ARRAY_SIZE(kmx61_uscale_table); i++) {
532 if (kmx61_uscale_table[i] == uscale) {
533 ret = kmx61_get_mode(data, &mode,
534 KMX61_ACC | KMX61_MAG);
535 if (ret < 0)
536 return ret;
537
538 ret = kmx61_set_mode(data, KMX61_ALL_STBY,
539 KMX61_ACC | KMX61_MAG, true);
540 if (ret < 0)
541 return ret;
542
543 ret = kmx61_set_range(data, i);
544 if (ret < 0)
545 return ret;
546
547 return kmx61_set_mode(data, mode,
548 KMX61_ACC | KMX61_MAG, true);
549 }
550 }
551 return -EINVAL;
552}
553
554static int kmx61_chip_init(struct kmx61_data *data)
555{
556 int ret, val, val2;
557
558 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_WHO_AM_I);
559 if (ret < 0) {
560 dev_err(&data->client->dev, "Error reading who_am_i\n");
561 return ret;
562 }
563
564 if (ret != KMX61_CHIP_ID) {
565 dev_err(&data->client->dev,
566 "Wrong chip id, got %x expected %x\n",
567 ret, KMX61_CHIP_ID);
568 return -EINVAL;
569 }
570
571 /* set accel 12bit, 4g range */
572 ret = kmx61_set_range(data, KMX61_RANGE_4G);
573 if (ret < 0)
574 return ret;
575
576 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_ODCNTL);
577 if (ret < 0) {
578 dev_err(&data->client->dev, "Error reading reg_odcntl\n");
579 return ret;
580 }
581 data->odr_bits = ret;
582
583 /* set output data rate for wake up (motion detection) function */
584 ret = kmx61_convert_bit_to_freq(data->odr_bits, &val, &val2);
585 if (ret < 0)
586 return ret;
587
588 ret = kmx61_set_wake_up_odr(data, val, val2);
589 if (ret < 0)
590 return ret;
591
592 /* set acc/magn to OPERATION mode */
593 ret = kmx61_set_mode(data, 0, KMX61_ACC | KMX61_MAG, true);
594 if (ret < 0)
595 return ret;
596
597 data->wake_thresh = KMX61_DEFAULT_WAKE_THRESH;
598 data->wake_duration = KMX61_DEFAULT_WAKE_DURATION;
599
600 return 0;
601}
602
603static int kmx61_setup_new_data_interrupt(struct kmx61_data *data,
604 bool status, u8 device)
605{
606 u8 mode;
607 int ret;
608
609 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
610 if (ret < 0)
611 return ret;
612
613 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
614 if (ret < 0)
615 return ret;
616
617 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
618 if (ret < 0) {
619 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
620 return ret;
621 }
622
623 if (status) {
624 ret |= KMX61_REG_INC1_BIT_IEN;
625 if (device & KMX61_ACC)
626 ret |= KMX61_REG_INC1_BIT_DRDYA;
627 if (device & KMX61_MAG)
628 ret |= KMX61_REG_INC1_BIT_DRDYM;
629 } else {
630 ret &= ~KMX61_REG_INC1_BIT_IEN;
631 if (device & KMX61_ACC)
632 ret &= ~KMX61_REG_INC1_BIT_DRDYA;
633 if (device & KMX61_MAG)
634 ret &= ~KMX61_REG_INC1_BIT_DRDYM;
635 }
636 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
637 if (ret < 0) {
638 dev_err(&data->client->dev, "Error writing reg_int_ctrl1\n");
639 return ret;
640 }
641
642 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
643 if (ret < 0) {
644 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
645 return ret;
646 }
647
648 if (status)
649 ret |= KMX61_REG_CTRL1_BIT_DRDYE;
650 else
651 ret &= ~KMX61_REG_CTRL1_BIT_DRDYE;
652
653 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
654 if (ret < 0) {
655 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
656 return ret;
657 }
658
659 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
660}
661
662static int kmx61_chip_update_thresholds(struct kmx61_data *data)
663{
664 int ret;
665
666 ret = i2c_smbus_write_byte_data(data->client,
667 KMX61_REG_WUF_TIMER,
668 data->wake_duration);
669 if (ret < 0) {
670 dev_err(&data->client->dev, "Errow writing reg_wuf_timer\n");
671 return ret;
672 }
673
674 ret = i2c_smbus_write_byte_data(data->client,
675 KMX61_REG_WUF_THRESH,
676 data->wake_thresh);
677 if (ret < 0)
678 dev_err(&data->client->dev, "Error writing reg_wuf_thresh\n");
679
680 return ret;
681}
682
683static int kmx61_setup_any_motion_interrupt(struct kmx61_data *data,
684 bool status)
685{
686 u8 mode;
687 int ret;
688
689 ret = kmx61_get_mode(data, &mode, KMX61_ACC | KMX61_MAG);
690 if (ret < 0)
691 return ret;
692
693 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
694 if (ret < 0)
695 return ret;
696
697 ret = kmx61_chip_update_thresholds(data);
698 if (ret < 0)
699 return ret;
700
701 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INC1);
702 if (ret < 0) {
703 dev_err(&data->client->dev, "Error reading reg_inc1\n");
704 return ret;
705 }
706 if (status)
707 ret |= (KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
708 else
709 ret &= ~(KMX61_REG_INC1_BIT_IEN | KMX61_REG_INC1_BIT_WUFS);
710
711 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_INC1, ret);
712 if (ret < 0) {
713 dev_err(&data->client->dev, "Error writing reg_inc1\n");
714 return ret;
715 }
716
717 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
718 if (ret < 0) {
719 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
720 return ret;
721 }
722
723 if (status)
724 ret |= KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE;
725 else
726 ret &= ~(KMX61_REG_CTRL1_BIT_WUFE | KMX61_REG_CTRL1_BIT_BTSE);
727
728 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
729 if (ret < 0) {
730 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
731 return ret;
732 }
733 mode |= KMX61_ACT_STBY_BIT;
734 return kmx61_set_mode(data, mode, KMX61_ACC | KMX61_MAG, true);
735}
736
737/**
738 * kmx61_set_power_state() - set power state for kmx61 @device
739 * @data - kmx61 device private pointer
740 * @on - power state to be set for @device
741 * @device - bitmask indicating device for which @on state needs to be set
742 *
743 * Notice that when ACC power state needs to be set to ON and MAG is in
744 * OPERATION then we know that kmx61_runtime_resume was already called
745 * so we must set ACC OPERATION mode here. The same happens when MAG power
746 * state needs to be set to ON and ACC is in OPERATION.
747 */
748static int kmx61_set_power_state(struct kmx61_data *data, bool on, u8 device)
749{
750#ifdef CONFIG_PM
751 int ret;
752
753 if (device & KMX61_ACC) {
754 if (on && !data->acc_ps && !data->mag_stby) {
755 ret = kmx61_set_mode(data, 0, KMX61_ACC, true);
756 if (ret < 0)
757 return ret;
758 }
759 data->acc_ps = on;
760 }
761 if (device & KMX61_MAG) {
762 if (on && !data->mag_ps && !data->acc_stby) {
763 ret = kmx61_set_mode(data, 0, KMX61_MAG, true);
764 if (ret < 0)
765 return ret;
766 }
767 data->mag_ps = on;
768 }
769
770 if (on) {
771 ret = pm_runtime_get_sync(&data->client->dev);
772 } else {
773 pm_runtime_mark_last_busy(&data->client->dev);
774 ret = pm_runtime_put_autosuspend(&data->client->dev);
775 }
776 if (ret < 0) {
777 dev_err(&data->client->dev,
778 "Failed: kmx61_set_power_state for %d, ret %d\n",
779 on, ret);
780 if (on)
781 pm_runtime_put_noidle(&data->client->dev);
782
783 return ret;
784 }
785#endif
786 return 0;
787}
788
789static int kmx61_read_measurement(struct kmx61_data *data, u8 base, u8 offset)
790{
791 int ret;
792 u8 reg = base + offset * 2;
793
794 ret = i2c_smbus_read_word_data(data->client, reg);
795 if (ret < 0)
796 dev_err(&data->client->dev, "failed to read reg at %x\n", reg);
797
798 return ret;
799}
800
801static int kmx61_read_raw(struct iio_dev *indio_dev,
802 struct iio_chan_spec const *chan, int *val,
803 int *val2, long mask)
804{
805 int ret;
806 u8 base_reg;
807 struct kmx61_data *data = kmx61_get_data(indio_dev);
808
809 switch (mask) {
810 case IIO_CHAN_INFO_RAW:
811 switch (chan->type) {
812 case IIO_ACCEL:
813 base_reg = KMX61_ACC_XOUT_L;
814 break;
815 case IIO_MAGN:
816 base_reg = KMX61_MAG_XOUT_L;
817 break;
818 default:
819 return -EINVAL;
820 }
821 mutex_lock(&data->lock);
822
823 ret = kmx61_set_power_state(data, true, chan->address);
824 if (ret) {
825 mutex_unlock(&data->lock);
826 return ret;
827 }
828
829 ret = kmx61_read_measurement(data, base_reg, chan->scan_index);
830 if (ret < 0) {
831 kmx61_set_power_state(data, false, chan->address);
832 mutex_unlock(&data->lock);
833 return ret;
834 }
835 *val = sign_extend32(ret >> chan->scan_type.shift,
836 chan->scan_type.realbits - 1);
837 ret = kmx61_set_power_state(data, false, chan->address);
838
839 mutex_unlock(&data->lock);
840 if (ret)
841 return ret;
842 return IIO_VAL_INT;
843 case IIO_CHAN_INFO_SCALE:
844 switch (chan->type) {
845 case IIO_ACCEL:
846 *val = 0;
847 *val2 = kmx61_uscale_table[data->range];
848 return IIO_VAL_INT_PLUS_MICRO;
849 case IIO_MAGN:
850 /* 14 bits res, 1465 microGauss per magn count */
851 *val = 0;
852 *val2 = 1465;
853 return IIO_VAL_INT_PLUS_MICRO;
854 default:
855 return -EINVAL;
856 }
857 case IIO_CHAN_INFO_SAMP_FREQ:
858 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
859 return -EINVAL;
860
861 mutex_lock(&data->lock);
862 ret = kmx61_get_odr(data, val, val2, chan->address);
863 mutex_unlock(&data->lock);
864 if (ret)
865 return -EINVAL;
866 return IIO_VAL_INT_PLUS_MICRO;
867 }
868 return -EINVAL;
869}
870
871static int kmx61_write_raw(struct iio_dev *indio_dev,
872 struct iio_chan_spec const *chan, int val,
873 int val2, long mask)
874{
875 int ret;
876 struct kmx61_data *data = kmx61_get_data(indio_dev);
877
878 switch (mask) {
879 case IIO_CHAN_INFO_SAMP_FREQ:
880 if (chan->type != IIO_ACCEL && chan->type != IIO_MAGN)
881 return -EINVAL;
882
883 mutex_lock(&data->lock);
884 ret = kmx61_set_odr(data, val, val2, chan->address);
885 mutex_unlock(&data->lock);
886 return ret;
887 case IIO_CHAN_INFO_SCALE:
888 switch (chan->type) {
889 case IIO_ACCEL:
890 if (val != 0)
891 return -EINVAL;
892 mutex_lock(&data->lock);
893 ret = kmx61_set_scale(data, val2);
894 mutex_unlock(&data->lock);
895 return ret;
896 default:
897 return -EINVAL;
898 }
899 default:
900 return -EINVAL;
901 }
902}
903
904static int kmx61_read_event(struct iio_dev *indio_dev,
905 const struct iio_chan_spec *chan,
906 enum iio_event_type type,
907 enum iio_event_direction dir,
908 enum iio_event_info info,
909 int *val, int *val2)
910{
911 struct kmx61_data *data = kmx61_get_data(indio_dev);
912
913 *val2 = 0;
914 switch (info) {
915 case IIO_EV_INFO_VALUE:
916 *val = data->wake_thresh;
917 return IIO_VAL_INT;
918 case IIO_EV_INFO_PERIOD:
919 *val = data->wake_duration;
920 return IIO_VAL_INT;
921 default:
922 return -EINVAL;
923 }
924}
925
926static int kmx61_write_event(struct iio_dev *indio_dev,
927 const struct iio_chan_spec *chan,
928 enum iio_event_type type,
929 enum iio_event_direction dir,
930 enum iio_event_info info,
931 int val, int val2)
932{
933 struct kmx61_data *data = kmx61_get_data(indio_dev);
934
935 if (data->ev_enable_state)
936 return -EBUSY;
937
938 switch (info) {
939 case IIO_EV_INFO_VALUE:
940 data->wake_thresh = val;
941 return IIO_VAL_INT;
942 case IIO_EV_INFO_PERIOD:
943 data->wake_duration = val;
944 return IIO_VAL_INT;
945 default:
946 return -EINVAL;
947 }
948}
949
950static int kmx61_read_event_config(struct iio_dev *indio_dev,
951 const struct iio_chan_spec *chan,
952 enum iio_event_type type,
953 enum iio_event_direction dir)
954{
955 struct kmx61_data *data = kmx61_get_data(indio_dev);
956
957 return data->ev_enable_state;
958}
959
960static int kmx61_write_event_config(struct iio_dev *indio_dev,
961 const struct iio_chan_spec *chan,
962 enum iio_event_type type,
963 enum iio_event_direction dir,
964 int state)
965{
966 struct kmx61_data *data = kmx61_get_data(indio_dev);
967 int ret = 0;
968
969 if (state && data->ev_enable_state)
970 return 0;
971
972 mutex_lock(&data->lock);
973
974 if (!state && data->motion_trig_on) {
975 data->ev_enable_state = false;
976 goto err_unlock;
977 }
978
979 ret = kmx61_set_power_state(data, state, KMX61_ACC);
980 if (ret < 0)
981 goto err_unlock;
982
983 ret = kmx61_setup_any_motion_interrupt(data, state);
984 if (ret < 0) {
985 kmx61_set_power_state(data, false, KMX61_ACC);
986 goto err_unlock;
987 }
988
989 data->ev_enable_state = state;
990
991err_unlock:
992 mutex_unlock(&data->lock);
993
994 return ret;
995}
996
997static int kmx61_acc_validate_trigger(struct iio_dev *indio_dev,
998 struct iio_trigger *trig)
999{
1000 struct kmx61_data *data = kmx61_get_data(indio_dev);
1001
1002 if (data->acc_dready_trig != trig && data->motion_trig != trig)
1003 return -EINVAL;
1004
1005 return 0;
1006}
1007
1008static int kmx61_mag_validate_trigger(struct iio_dev *indio_dev,
1009 struct iio_trigger *trig)
1010{
1011 struct kmx61_data *data = kmx61_get_data(indio_dev);
1012
1013 if (data->mag_dready_trig != trig)
1014 return -EINVAL;
1015
1016 return 0;
1017}
1018
1019static const struct iio_info kmx61_acc_info = {
1020 .driver_module = THIS_MODULE,
1021 .read_raw = kmx61_read_raw,
1022 .write_raw = kmx61_write_raw,
1023 .attrs = &kmx61_acc_attribute_group,
1024 .read_event_value = kmx61_read_event,
1025 .write_event_value = kmx61_write_event,
1026 .read_event_config = kmx61_read_event_config,
1027 .write_event_config = kmx61_write_event_config,
1028 .validate_trigger = kmx61_acc_validate_trigger,
1029};
1030
1031static const struct iio_info kmx61_mag_info = {
1032 .driver_module = THIS_MODULE,
1033 .read_raw = kmx61_read_raw,
1034 .write_raw = kmx61_write_raw,
1035 .attrs = &kmx61_mag_attribute_group,
1036 .validate_trigger = kmx61_mag_validate_trigger,
1037};
1038
1039
1040static int kmx61_data_rdy_trigger_set_state(struct iio_trigger *trig,
1041 bool state)
1042{
1043 int ret = 0;
1044 u8 device;
1045
1046 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1047 struct kmx61_data *data = kmx61_get_data(indio_dev);
1048
1049 mutex_lock(&data->lock);
1050
1051 if (!state && data->ev_enable_state && data->motion_trig_on) {
1052 data->motion_trig_on = false;
1053 goto err_unlock;
1054 }
1055
1056 if (data->acc_dready_trig == trig || data->motion_trig == trig)
1057 device = KMX61_ACC;
1058 else
1059 device = KMX61_MAG;
1060
1061 ret = kmx61_set_power_state(data, state, device);
1062 if (ret < 0)
1063 goto err_unlock;
1064
1065 if (data->acc_dready_trig == trig || data->mag_dready_trig == trig)
1066 ret = kmx61_setup_new_data_interrupt(data, state, device);
1067 else
1068 ret = kmx61_setup_any_motion_interrupt(data, state);
1069 if (ret < 0) {
1070 kmx61_set_power_state(data, false, device);
1071 goto err_unlock;
1072 }
1073
1074 if (data->acc_dready_trig == trig)
1075 data->acc_dready_trig_on = state;
1076 else if (data->mag_dready_trig == trig)
1077 data->mag_dready_trig_on = state;
1078 else
1079 data->motion_trig_on = state;
1080err_unlock:
1081 mutex_unlock(&data->lock);
1082
1083 return ret;
1084}
1085
1086static int kmx61_trig_try_reenable(struct iio_trigger *trig)
1087{
1088 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1089 struct kmx61_data *data = kmx61_get_data(indio_dev);
1090 int ret;
1091
1092 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1093 if (ret < 0) {
1094 dev_err(&data->client->dev, "Error reading reg_inl\n");
1095 return ret;
1096 }
1097
1098 return 0;
1099}
1100
1101static const struct iio_trigger_ops kmx61_trigger_ops = {
1102 .set_trigger_state = kmx61_data_rdy_trigger_set_state,
1103 .try_reenable = kmx61_trig_try_reenable,
1104 .owner = THIS_MODULE,
1105};
1106
1107static irqreturn_t kmx61_event_handler(int irq, void *private)
1108{
1109 struct kmx61_data *data = private;
1110 struct iio_dev *indio_dev = data->acc_indio_dev;
1111 int ret;
1112
1113 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS1);
1114 if (ret < 0) {
1115 dev_err(&data->client->dev, "Error reading reg_ins1\n");
1116 goto ack_intr;
1117 }
1118
1119 if (ret & KMX61_REG_INS1_BIT_WUFS) {
1120 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INS2);
1121 if (ret < 0) {
1122 dev_err(&data->client->dev, "Error reading reg_ins2\n");
1123 goto ack_intr;
1124 }
1125
1126 if (ret & KMX61_REG_INS2_BIT_XN)
1127 iio_push_event(indio_dev,
1128 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1129 0,
1130 IIO_MOD_X,
1131 IIO_EV_TYPE_THRESH,
1132 IIO_EV_DIR_FALLING),
1133 0);
1134
1135 if (ret & KMX61_REG_INS2_BIT_XP)
1136 iio_push_event(indio_dev,
1137 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1138 0,
1139 IIO_MOD_X,
1140 IIO_EV_TYPE_THRESH,
1141 IIO_EV_DIR_RISING),
1142 0);
1143
1144 if (ret & KMX61_REG_INS2_BIT_YN)
1145 iio_push_event(indio_dev,
1146 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1147 0,
1148 IIO_MOD_Y,
1149 IIO_EV_TYPE_THRESH,
1150 IIO_EV_DIR_FALLING),
1151 0);
1152
1153 if (ret & KMX61_REG_INS2_BIT_YP)
1154 iio_push_event(indio_dev,
1155 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1156 0,
1157 IIO_MOD_Y,
1158 IIO_EV_TYPE_THRESH,
1159 IIO_EV_DIR_RISING),
1160 0);
1161
1162 if (ret & KMX61_REG_INS2_BIT_ZN)
1163 iio_push_event(indio_dev,
1164 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1165 0,
1166 IIO_MOD_Z,
1167 IIO_EV_TYPE_THRESH,
1168 IIO_EV_DIR_FALLING),
1169 0);
1170
1171 if (ret & KMX61_REG_INS2_BIT_ZP)
1172 iio_push_event(indio_dev,
1173 IIO_MOD_EVENT_CODE(IIO_ACCEL,
1174 0,
1175 IIO_MOD_Z,
1176 IIO_EV_TYPE_THRESH,
1177 IIO_EV_DIR_RISING),
1178 0);
1179 }
1180
1181ack_intr:
1182 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_CTRL1);
1183 if (ret < 0)
1184 dev_err(&data->client->dev, "Error reading reg_ctrl1\n");
1185
1186 ret |= KMX61_REG_CTRL1_BIT_RES;
1187 ret = i2c_smbus_write_byte_data(data->client, KMX61_REG_CTRL1, ret);
1188 if (ret < 0)
1189 dev_err(&data->client->dev, "Error writing reg_ctrl1\n");
1190
1191 ret = i2c_smbus_read_byte_data(data->client, KMX61_REG_INL);
1192 if (ret < 0)
1193 dev_err(&data->client->dev, "Error reading reg_inl\n");
1194
1195 return IRQ_HANDLED;
1196}
1197
1198static irqreturn_t kmx61_data_rdy_trig_poll(int irq, void *private)
1199{
1200 struct kmx61_data *data = private;
1201
1202 if (data->acc_dready_trig_on)
1203 iio_trigger_poll(data->acc_dready_trig);
1204 if (data->mag_dready_trig_on)
1205 iio_trigger_poll(data->mag_dready_trig);
1206
1207 if (data->motion_trig_on)
1208 iio_trigger_poll(data->motion_trig);
1209
1210 if (data->ev_enable_state)
1211 return IRQ_WAKE_THREAD;
1212 return IRQ_HANDLED;
1213}
1214
1215static irqreturn_t kmx61_trigger_handler(int irq, void *p)
1216{
1217 struct iio_poll_func *pf = p;
1218 struct iio_dev *indio_dev = pf->indio_dev;
1219 struct kmx61_data *data = kmx61_get_data(indio_dev);
1220 int bit, ret, i = 0;
1221 u8 base;
1222 s16 buffer[8];
1223
1224 if (indio_dev == data->acc_indio_dev)
1225 base = KMX61_ACC_XOUT_L;
1226 else
1227 base = KMX61_MAG_XOUT_L;
1228
1229 mutex_lock(&data->lock);
1230 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
1231 indio_dev->masklength) {
1232 ret = kmx61_read_measurement(data, base, bit);
1233 if (ret < 0) {
1234 mutex_unlock(&data->lock);
1235 goto err;
1236 }
1237 buffer[i++] = ret;
1238 }
1239 mutex_unlock(&data->lock);
1240
1241 iio_push_to_buffers(indio_dev, buffer);
1242err:
1243 iio_trigger_notify_done(indio_dev->trig);
1244
1245 return IRQ_HANDLED;
1246}
1247
1248static const char *kmx61_match_acpi_device(struct device *dev)
1249{
1250 const struct acpi_device_id *id;
1251
1252 id = acpi_match_device(dev->driver->acpi_match_table, dev);
1253 if (!id)
1254 return NULL;
1255 return dev_name(dev);
1256}
1257
1258static int kmx61_gpio_probe(struct i2c_client *client, struct kmx61_data *data)
1259{
1260 struct device *dev;
1261 struct gpio_desc *gpio;
1262 int ret;
1263
1264 if (!client)
1265 return -EINVAL;
1266
1267 dev = &client->dev;
1268
1269 /* data ready gpio interrupt pin */
1270 gpio = devm_gpiod_get_index(dev, KMX61_GPIO_NAME, 0);
1271 if (IS_ERR(gpio)) {
1272 dev_err(dev, "acpi gpio get index failed\n");
1273 return PTR_ERR(gpio);
1274 }
1275
1276 ret = gpiod_direction_input(gpio);
1277 if (ret)
1278 return ret;
1279
1280 ret = gpiod_to_irq(gpio);
1281
1282 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
1283 return ret;
1284}
1285
1286static struct iio_dev *kmx61_indiodev_setup(struct kmx61_data *data,
1287 const struct iio_info *info,
1288 const struct iio_chan_spec *chan,
1289 int num_channels,
1290 const char *name)
1291{
1292 struct iio_dev *indio_dev;
1293
1294 indio_dev = devm_iio_device_alloc(&data->client->dev, sizeof(data));
1295 if (!indio_dev)
1296 return ERR_PTR(-ENOMEM);
1297
1298 kmx61_set_data(indio_dev, data);
1299
1300 indio_dev->dev.parent = &data->client->dev;
1301 indio_dev->channels = chan;
1302 indio_dev->num_channels = num_channels;
1303 indio_dev->name = name;
1304 indio_dev->modes = INDIO_DIRECT_MODE;
1305 indio_dev->info = info;
1306
1307 return indio_dev;
1308}
1309
1310static struct iio_trigger *kmx61_trigger_setup(struct kmx61_data *data,
1311 struct iio_dev *indio_dev,
1312 const char *tag)
1313{
1314 struct iio_trigger *trig;
1315 int ret;
1316
1317 trig = devm_iio_trigger_alloc(&data->client->dev,
1318 "%s-%s-dev%d",
1319 indio_dev->name,
1320 tag,
1321 indio_dev->id);
1322 if (!trig)
1323 return ERR_PTR(-ENOMEM);
1324
1325 trig->dev.parent = &data->client->dev;
1326 trig->ops = &kmx61_trigger_ops;
1327 iio_trigger_set_drvdata(trig, indio_dev);
1328
1329 ret = iio_trigger_register(trig);
1330 if (ret)
1331 return ERR_PTR(ret);
1332
1333 return trig;
1334}
1335
1336static int kmx61_probe(struct i2c_client *client,
1337 const struct i2c_device_id *id)
1338{
1339 int ret;
1340 struct kmx61_data *data;
1341 const char *name = NULL;
1342
1343 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
1344 if (!data)
1345 return -ENOMEM;
1346
1347 i2c_set_clientdata(client, data);
1348 data->client = client;
1349
1350 mutex_init(&data->lock);
1351
1352 if (id)
1353 name = id->name;
1354 else if (ACPI_HANDLE(&client->dev))
1355 name = kmx61_match_acpi_device(&client->dev);
1356 else
1357 return -ENODEV;
1358
1359 data->acc_indio_dev =
1360 kmx61_indiodev_setup(data, &kmx61_acc_info,
1361 kmx61_acc_channels,
1362 ARRAY_SIZE(kmx61_acc_channels),
1363 name);
1364 if (IS_ERR(data->acc_indio_dev))
1365 return PTR_ERR(data->acc_indio_dev);
1366
1367 data->mag_indio_dev =
1368 kmx61_indiodev_setup(data, &kmx61_mag_info,
1369 kmx61_mag_channels,
1370 ARRAY_SIZE(kmx61_mag_channels),
1371 name);
1372 if (IS_ERR(data->mag_indio_dev))
1373 return PTR_ERR(data->mag_indio_dev);
1374
1375 ret = kmx61_chip_init(data);
1376 if (ret < 0)
1377 return ret;
1378
1379 if (client->irq < 0)
1380 client->irq = kmx61_gpio_probe(client, data);
1381
1382 if (client->irq >= 0) {
1383 ret = devm_request_threaded_irq(&client->dev, client->irq,
1384 kmx61_data_rdy_trig_poll,
1385 kmx61_event_handler,
1386 IRQF_TRIGGER_RISING,
1387 KMX61_IRQ_NAME,
1388 data);
1389 if (ret)
1390 goto err_chip_uninit;
1391
1392 data->acc_dready_trig =
1393 kmx61_trigger_setup(data, data->acc_indio_dev,
1394 "dready");
1395 if (IS_ERR(data->acc_dready_trig)) {
1396 ret = PTR_ERR(data->acc_dready_trig);
1397 goto err_chip_uninit;
1398 }
1399
1400 data->mag_dready_trig =
1401 kmx61_trigger_setup(data, data->mag_indio_dev,
1402 "dready");
1403 if (IS_ERR(data->mag_dready_trig)) {
1404 ret = PTR_ERR(data->mag_dready_trig);
1405 goto err_trigger_unregister_acc_dready;
1406 }
1407
1408 data->motion_trig =
1409 kmx61_trigger_setup(data, data->acc_indio_dev,
1410 "any-motion");
1411 if (IS_ERR(data->motion_trig)) {
1412 ret = PTR_ERR(data->motion_trig);
1413 goto err_trigger_unregister_mag_dready;
1414 }
1415
1416 ret = iio_triggered_buffer_setup(data->acc_indio_dev,
1417 &iio_pollfunc_store_time,
1418 kmx61_trigger_handler,
1419 NULL);
1420 if (ret < 0) {
1421 dev_err(&data->client->dev,
1422 "Failed to setup acc triggered buffer\n");
1423 goto err_trigger_unregister_motion;
1424 }
1425
1426 ret = iio_triggered_buffer_setup(data->mag_indio_dev,
1427 &iio_pollfunc_store_time,
1428 kmx61_trigger_handler,
1429 NULL);
1430 if (ret < 0) {
1431 dev_err(&data->client->dev,
1432 "Failed to setup mag triggered buffer\n");
1433 goto err_buffer_cleanup_acc;
1434 }
1435 }
1436
1437 ret = iio_device_register(data->acc_indio_dev);
1438 if (ret < 0) {
1439 dev_err(&client->dev, "Failed to register acc iio device\n");
1440 goto err_buffer_cleanup_mag;
1441 }
1442
1443 ret = iio_device_register(data->mag_indio_dev);
1444 if (ret < 0) {
1445 dev_err(&client->dev, "Failed to register mag iio device\n");
1446 goto err_iio_unregister_acc;
1447 }
1448
1449 ret = pm_runtime_set_active(&client->dev);
1450 if (ret < 0)
1451 goto err_iio_unregister_mag;
1452
1453 pm_runtime_enable(&client->dev);
1454 pm_runtime_set_autosuspend_delay(&client->dev, KMX61_SLEEP_DELAY_MS);
1455 pm_runtime_use_autosuspend(&client->dev);
1456
1457 return 0;
1458
1459err_iio_unregister_mag:
1460 iio_device_unregister(data->mag_indio_dev);
1461err_iio_unregister_acc:
1462 iio_device_unregister(data->acc_indio_dev);
1463err_buffer_cleanup_mag:
1464 if (client->irq >= 0)
1465 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1466err_buffer_cleanup_acc:
1467 if (client->irq >= 0)
1468 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1469err_trigger_unregister_motion:
1470 iio_trigger_unregister(data->motion_trig);
1471err_trigger_unregister_mag_dready:
1472 iio_trigger_unregister(data->mag_dready_trig);
1473err_trigger_unregister_acc_dready:
1474 iio_trigger_unregister(data->acc_dready_trig);
1475err_chip_uninit:
1476 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1477 return ret;
1478}
1479
1480static int kmx61_remove(struct i2c_client *client)
1481{
1482 struct kmx61_data *data = i2c_get_clientdata(client);
1483
1484 pm_runtime_disable(&client->dev);
1485 pm_runtime_set_suspended(&client->dev);
1486 pm_runtime_put_noidle(&client->dev);
1487
1488 iio_device_unregister(data->acc_indio_dev);
1489 iio_device_unregister(data->mag_indio_dev);
1490
1491 if (client->irq >= 0) {
1492 iio_triggered_buffer_cleanup(data->acc_indio_dev);
1493 iio_triggered_buffer_cleanup(data->mag_indio_dev);
1494 iio_trigger_unregister(data->acc_dready_trig);
1495 iio_trigger_unregister(data->mag_dready_trig);
1496 iio_trigger_unregister(data->motion_trig);
1497 }
1498
1499 mutex_lock(&data->lock);
1500 kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1501 mutex_unlock(&data->lock);
1502
1503 return 0;
1504}
1505
1506#ifdef CONFIG_PM_SLEEP
1507static int kmx61_suspend(struct device *dev)
1508{
1509 int ret;
1510 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1511
1512 mutex_lock(&data->lock);
1513 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG,
1514 false);
1515 mutex_unlock(&data->lock);
1516
1517 return ret;
1518}
1519
1520static int kmx61_resume(struct device *dev)
1521{
1522 u8 stby = 0;
1523 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1524
1525 if (data->acc_stby)
1526 stby |= KMX61_ACC_STBY_BIT;
1527 if (data->mag_stby)
1528 stby |= KMX61_MAG_STBY_BIT;
1529
1530 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1531}
1532#endif
1533
1534#ifdef CONFIG_PM
1535static int kmx61_runtime_suspend(struct device *dev)
1536{
1537 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1538 int ret;
1539
1540 mutex_lock(&data->lock);
1541 ret = kmx61_set_mode(data, KMX61_ALL_STBY, KMX61_ACC | KMX61_MAG, true);
1542 mutex_unlock(&data->lock);
1543
1544 return ret;
1545}
1546
1547static int kmx61_runtime_resume(struct device *dev)
1548{
1549 struct kmx61_data *data = i2c_get_clientdata(to_i2c_client(dev));
1550 u8 stby = 0;
1551
1552 if (!data->acc_ps)
1553 stby |= KMX61_ACC_STBY_BIT;
1554 if (!data->mag_ps)
1555 stby |= KMX61_MAG_STBY_BIT;
1556
1557 return kmx61_set_mode(data, stby, KMX61_ACC | KMX61_MAG, true);
1558}
1559#endif
1560
1561static const struct dev_pm_ops kmx61_pm_ops = {
1562 SET_SYSTEM_SLEEP_PM_OPS(kmx61_suspend, kmx61_resume)
1563 SET_RUNTIME_PM_OPS(kmx61_runtime_suspend, kmx61_runtime_resume, NULL)
1564};
1565
1566static const struct acpi_device_id kmx61_acpi_match[] = {
1567 {"KMX61021", 0},
1568 {}
1569};
1570
1571MODULE_DEVICE_TABLE(acpi, kmx61_acpi_match);
1572
1573static const struct i2c_device_id kmx61_id[] = {
1574 {"kmx611021", 0},
1575 {}
1576};
1577
1578MODULE_DEVICE_TABLE(i2c, kmx61_id);
1579
1580static struct i2c_driver kmx61_driver = {
1581 .driver = {
1582 .name = KMX61_DRV_NAME,
1583 .acpi_match_table = ACPI_PTR(kmx61_acpi_match),
1584 .pm = &kmx61_pm_ops,
1585 },
1586 .probe = kmx61_probe,
1587 .remove = kmx61_remove,
1588 .id_table = kmx61_id,
1589};
1590
1591module_i2c_driver(kmx61_driver);
1592
1593MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
1594MODULE_DESCRIPTION("KMX61 accelerometer/magnetometer driver");
1595MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index f971f79103ec..403b72878b0b 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -178,6 +178,80 @@ static ssize_t iio_scan_el_show(struct device *dev,
178 return sprintf(buf, "%d\n", ret); 178 return sprintf(buf, "%d\n", ret);
179} 179}
180 180
181/* Note NULL used as error indicator as it doesn't make sense. */
182static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
183 unsigned int masklength,
184 const unsigned long *mask)
185{
186 if (bitmap_empty(mask, masklength))
187 return NULL;
188 while (*av_masks) {
189 if (bitmap_subset(mask, av_masks, masklength))
190 return av_masks;
191 av_masks += BITS_TO_LONGS(masklength);
192 }
193 return NULL;
194}
195
196static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
197 const unsigned long *mask)
198{
199 if (!indio_dev->setup_ops->validate_scan_mask)
200 return true;
201
202 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask);
203}
204
205/**
206 * iio_scan_mask_set() - set particular bit in the scan mask
207 * @indio_dev: the iio device
208 * @buffer: the buffer whose scan mask we are interested in
209 * @bit: the bit to be set.
210 *
211 * Note that at this point we have no way of knowing what other
212 * buffers might request, hence this code only verifies that the
213 * individual buffers request is plausible.
214 */
215static int iio_scan_mask_set(struct iio_dev *indio_dev,
216 struct iio_buffer *buffer, int bit)
217{
218 const unsigned long *mask;
219 unsigned long *trialmask;
220
221 trialmask = kmalloc(sizeof(*trialmask)*
222 BITS_TO_LONGS(indio_dev->masklength),
223 GFP_KERNEL);
224
225 if (trialmask == NULL)
226 return -ENOMEM;
227 if (!indio_dev->masklength) {
228 WARN_ON("Trying to set scanmask prior to registering buffer\n");
229 goto err_invalid_mask;
230 }
231 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength);
232 set_bit(bit, trialmask);
233
234 if (!iio_validate_scan_mask(indio_dev, trialmask))
235 goto err_invalid_mask;
236
237 if (indio_dev->available_scan_masks) {
238 mask = iio_scan_mask_match(indio_dev->available_scan_masks,
239 indio_dev->masklength,
240 trialmask);
241 if (!mask)
242 goto err_invalid_mask;
243 }
244 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
245
246 kfree(trialmask);
247
248 return 0;
249
250err_invalid_mask:
251 kfree(trialmask);
252 return -EINVAL;
253}
254
181static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit) 255static int iio_scan_mask_clear(struct iio_buffer *buffer, int bit)
182{ 256{
183 clear_bit(bit, buffer->scan_mask); 257 clear_bit(bit, buffer->scan_mask);
@@ -309,115 +383,19 @@ static int iio_buffer_add_channel_sysfs(struct iio_dev *indio_dev,
309 return ret; 383 return ret;
310} 384}
311 385
312static const char * const iio_scan_elements_group_name = "scan_elements"; 386static ssize_t iio_buffer_read_length(struct device *dev,
313 387 struct device_attribute *attr,
314int iio_buffer_register(struct iio_dev *indio_dev, 388 char *buf)
315 const struct iio_chan_spec *channels,
316 int num_channels)
317{
318 struct iio_dev_attr *p;
319 struct attribute **attr;
320 struct iio_buffer *buffer = indio_dev->buffer;
321 int ret, i, attrn, attrcount, attrcount_orig = 0;
322
323 if (buffer->attrs)
324 indio_dev->groups[indio_dev->groupcounter++] = buffer->attrs;
325
326 if (buffer->scan_el_attrs != NULL) {
327 attr = buffer->scan_el_attrs->attrs;
328 while (*attr++ != NULL)
329 attrcount_orig++;
330 }
331 attrcount = attrcount_orig;
332 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
333 if (channels) {
334 /* new magic */
335 for (i = 0; i < num_channels; i++) {
336 if (channels[i].scan_index < 0)
337 continue;
338
339 /* Establish necessary mask length */
340 if (channels[i].scan_index >
341 (int)indio_dev->masklength - 1)
342 indio_dev->masklength
343 = channels[i].scan_index + 1;
344
345 ret = iio_buffer_add_channel_sysfs(indio_dev,
346 &channels[i]);
347 if (ret < 0)
348 goto error_cleanup_dynamic;
349 attrcount += ret;
350 if (channels[i].type == IIO_TIMESTAMP)
351 indio_dev->scan_index_timestamp =
352 channels[i].scan_index;
353 }
354 if (indio_dev->masklength && buffer->scan_mask == NULL) {
355 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
356 sizeof(*buffer->scan_mask),
357 GFP_KERNEL);
358 if (buffer->scan_mask == NULL) {
359 ret = -ENOMEM;
360 goto error_cleanup_dynamic;
361 }
362 }
363 }
364
365 buffer->scan_el_group.name = iio_scan_elements_group_name;
366
367 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
368 sizeof(buffer->scan_el_group.attrs[0]),
369 GFP_KERNEL);
370 if (buffer->scan_el_group.attrs == NULL) {
371 ret = -ENOMEM;
372 goto error_free_scan_mask;
373 }
374 if (buffer->scan_el_attrs)
375 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
376 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
377 attrn = attrcount_orig;
378
379 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
380 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
381 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
382
383 return 0;
384
385error_free_scan_mask:
386 kfree(buffer->scan_mask);
387error_cleanup_dynamic:
388 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
389
390 return ret;
391}
392EXPORT_SYMBOL(iio_buffer_register);
393
394void iio_buffer_unregister(struct iio_dev *indio_dev)
395{
396 kfree(indio_dev->buffer->scan_mask);
397 kfree(indio_dev->buffer->scan_el_group.attrs);
398 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
399}
400EXPORT_SYMBOL(iio_buffer_unregister);
401
402ssize_t iio_buffer_read_length(struct device *dev,
403 struct device_attribute *attr,
404 char *buf)
405{ 389{
406 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 390 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
407 struct iio_buffer *buffer = indio_dev->buffer; 391 struct iio_buffer *buffer = indio_dev->buffer;
408 392
409 if (buffer->access->get_length) 393 return sprintf(buf, "%d\n", buffer->length);
410 return sprintf(buf, "%d\n",
411 buffer->access->get_length(buffer));
412
413 return 0;
414} 394}
415EXPORT_SYMBOL(iio_buffer_read_length);
416 395
417ssize_t iio_buffer_write_length(struct device *dev, 396static ssize_t iio_buffer_write_length(struct device *dev,
418 struct device_attribute *attr, 397 struct device_attribute *attr,
419 const char *buf, 398 const char *buf, size_t len)
420 size_t len)
421{ 399{
422 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 400 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
423 struct iio_buffer *buffer = indio_dev->buffer; 401 struct iio_buffer *buffer = indio_dev->buffer;
@@ -428,47 +406,28 @@ ssize_t iio_buffer_write_length(struct device *dev,
428 if (ret) 406 if (ret)
429 return ret; 407 return ret;
430 408
431 if (buffer->access->get_length) 409 if (val == buffer->length)
432 if (val == buffer->access->get_length(buffer)) 410 return len;
433 return len;
434 411
435 mutex_lock(&indio_dev->mlock); 412 mutex_lock(&indio_dev->mlock);
436 if (iio_buffer_is_active(indio_dev->buffer)) { 413 if (iio_buffer_is_active(indio_dev->buffer)) {
437 ret = -EBUSY; 414 ret = -EBUSY;
438 } else { 415 } else {
439 if (buffer->access->set_length) 416 buffer->access->set_length(buffer, val);
440 buffer->access->set_length(buffer, val);
441 ret = 0; 417 ret = 0;
442 } 418 }
443 mutex_unlock(&indio_dev->mlock); 419 mutex_unlock(&indio_dev->mlock);
444 420
445 return ret ? ret : len; 421 return ret ? ret : len;
446} 422}
447EXPORT_SYMBOL(iio_buffer_write_length);
448 423
449ssize_t iio_buffer_show_enable(struct device *dev, 424static ssize_t iio_buffer_show_enable(struct device *dev,
450 struct device_attribute *attr, 425 struct device_attribute *attr,
451 char *buf) 426 char *buf)
452{ 427{
453 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 428 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
454 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer)); 429 return sprintf(buf, "%d\n", iio_buffer_is_active(indio_dev->buffer));
455} 430}
456EXPORT_SYMBOL(iio_buffer_show_enable);
457
458/* Note NULL used as error indicator as it doesn't make sense. */
459static const unsigned long *iio_scan_mask_match(const unsigned long *av_masks,
460 unsigned int masklength,
461 const unsigned long *mask)
462{
463 if (bitmap_empty(mask, masklength))
464 return NULL;
465 while (*av_masks) {
466 if (bitmap_subset(mask, av_masks, masklength))
467 return av_masks;
468 av_masks += BITS_TO_LONGS(masklength);
469 }
470 return NULL;
471}
472 431
473static int iio_compute_scan_bytes(struct iio_dev *indio_dev, 432static int iio_compute_scan_bytes(struct iio_dev *indio_dev,
474 const unsigned long *mask, bool timestamp) 433 const unsigned long *mask, bool timestamp)
@@ -755,10 +714,10 @@ out_unlock:
755} 714}
756EXPORT_SYMBOL_GPL(iio_update_buffers); 715EXPORT_SYMBOL_GPL(iio_update_buffers);
757 716
758ssize_t iio_buffer_store_enable(struct device *dev, 717static ssize_t iio_buffer_store_enable(struct device *dev,
759 struct device_attribute *attr, 718 struct device_attribute *attr,
760 const char *buf, 719 const char *buf,
761 size_t len) 720 size_t len)
762{ 721{
763 int ret; 722 int ret;
764 bool requested_state; 723 bool requested_state;
@@ -790,83 +749,146 @@ done:
790 mutex_unlock(&indio_dev->mlock); 749 mutex_unlock(&indio_dev->mlock);
791 return (ret < 0) ? ret : len; 750 return (ret < 0) ? ret : len;
792} 751}
793EXPORT_SYMBOL(iio_buffer_store_enable);
794 752
795/** 753static const char * const iio_scan_elements_group_name = "scan_elements";
796 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
797 * @indio_dev: the iio device
798 * @mask: scan mask to be checked
799 *
800 * Return true if exactly one bit is set in the scan mask, false otherwise. It
801 * can be used for devices where only one channel can be active for sampling at
802 * a time.
803 */
804bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
805 const unsigned long *mask)
806{
807 return bitmap_weight(mask, indio_dev->masklength) == 1;
808}
809EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
810
811static bool iio_validate_scan_mask(struct iio_dev *indio_dev,
812 const unsigned long *mask)
813{
814 if (!indio_dev->setup_ops->validate_scan_mask)
815 return true;
816 754
817 return indio_dev->setup_ops->validate_scan_mask(indio_dev, mask); 755static DEVICE_ATTR(length, S_IRUGO | S_IWUSR, iio_buffer_read_length,
818} 756 iio_buffer_write_length);
757static struct device_attribute dev_attr_length_ro = __ATTR(length,
758 S_IRUGO, iio_buffer_read_length, NULL);
759static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,
760 iio_buffer_show_enable, iio_buffer_store_enable);
819 761
820/** 762int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
821 * iio_scan_mask_set() - set particular bit in the scan mask
822 * @indio_dev: the iio device
823 * @buffer: the buffer whose scan mask we are interested in
824 * @bit: the bit to be set.
825 *
826 * Note that at this point we have no way of knowing what other
827 * buffers might request, hence this code only verifies that the
828 * individual buffers request is plausible.
829 */
830int iio_scan_mask_set(struct iio_dev *indio_dev,
831 struct iio_buffer *buffer, int bit)
832{ 763{
833 const unsigned long *mask; 764 struct iio_dev_attr *p;
834 unsigned long *trialmask; 765 struct attribute **attr;
766 struct iio_buffer *buffer = indio_dev->buffer;
767 int ret, i, attrn, attrcount, attrcount_orig = 0;
768 const struct iio_chan_spec *channels;
835 769
836 trialmask = kmalloc(sizeof(*trialmask)* 770 if (!buffer)
837 BITS_TO_LONGS(indio_dev->masklength), 771 return 0;
838 GFP_KERNEL);
839 772
840 if (trialmask == NULL) 773 attrcount = 0;
774 if (buffer->attrs) {
775 while (buffer->attrs[attrcount] != NULL)
776 attrcount++;
777 }
778
779 buffer->buffer_group.name = "buffer";
780 buffer->buffer_group.attrs = kcalloc(attrcount + 3,
781 sizeof(*buffer->buffer_group.attrs), GFP_KERNEL);
782 if (!buffer->buffer_group.attrs)
841 return -ENOMEM; 783 return -ENOMEM;
842 if (!indio_dev->masklength) { 784
843 WARN_ON("Trying to set scanmask prior to registering buffer\n"); 785 if (buffer->access->set_length)
844 goto err_invalid_mask; 786 buffer->buffer_group.attrs[0] = &dev_attr_length.attr;
787 else
788 buffer->buffer_group.attrs[0] = &dev_attr_length_ro.attr;
789 buffer->buffer_group.attrs[1] = &dev_attr_enable.attr;
790 if (buffer->attrs)
791 memcpy(&buffer->buffer_group.attrs[2], buffer->attrs,
792 sizeof(*&buffer->buffer_group.attrs) * attrcount);
793 buffer->buffer_group.attrs[attrcount+2] = NULL;
794
795 indio_dev->groups[indio_dev->groupcounter++] = &buffer->buffer_group;
796
797 if (buffer->scan_el_attrs != NULL) {
798 attr = buffer->scan_el_attrs->attrs;
799 while (*attr++ != NULL)
800 attrcount_orig++;
845 } 801 }
846 bitmap_copy(trialmask, buffer->scan_mask, indio_dev->masklength); 802 attrcount = attrcount_orig;
847 set_bit(bit, trialmask); 803 INIT_LIST_HEAD(&buffer->scan_el_dev_attr_list);
804 channels = indio_dev->channels;
805 if (channels) {
806 /* new magic */
807 for (i = 0; i < indio_dev->num_channels; i++) {
808 if (channels[i].scan_index < 0)
809 continue;
848 810
849 if (!iio_validate_scan_mask(indio_dev, trialmask)) 811 /* Establish necessary mask length */
850 goto err_invalid_mask; 812 if (channels[i].scan_index >
813 (int)indio_dev->masklength - 1)
814 indio_dev->masklength
815 = channels[i].scan_index + 1;
851 816
852 if (indio_dev->available_scan_masks) { 817 ret = iio_buffer_add_channel_sysfs(indio_dev,
853 mask = iio_scan_mask_match(indio_dev->available_scan_masks, 818 &channels[i]);
854 indio_dev->masklength, 819 if (ret < 0)
855 trialmask); 820 goto error_cleanup_dynamic;
856 if (!mask) 821 attrcount += ret;
857 goto err_invalid_mask; 822 if (channels[i].type == IIO_TIMESTAMP)
823 indio_dev->scan_index_timestamp =
824 channels[i].scan_index;
825 }
826 if (indio_dev->masklength && buffer->scan_mask == NULL) {
827 buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
828 sizeof(*buffer->scan_mask),
829 GFP_KERNEL);
830 if (buffer->scan_mask == NULL) {
831 ret = -ENOMEM;
832 goto error_cleanup_dynamic;
833 }
834 }
858 } 835 }
859 bitmap_copy(buffer->scan_mask, trialmask, indio_dev->masklength);
860 836
861 kfree(trialmask); 837 buffer->scan_el_group.name = iio_scan_elements_group_name;
838
839 buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
840 sizeof(buffer->scan_el_group.attrs[0]),
841 GFP_KERNEL);
842 if (buffer->scan_el_group.attrs == NULL) {
843 ret = -ENOMEM;
844 goto error_free_scan_mask;
845 }
846 if (buffer->scan_el_attrs)
847 memcpy(buffer->scan_el_group.attrs, buffer->scan_el_attrs,
848 sizeof(buffer->scan_el_group.attrs[0])*attrcount_orig);
849 attrn = attrcount_orig;
850
851 list_for_each_entry(p, &buffer->scan_el_dev_attr_list, l)
852 buffer->scan_el_group.attrs[attrn++] = &p->dev_attr.attr;
853 indio_dev->groups[indio_dev->groupcounter++] = &buffer->scan_el_group;
862 854
863 return 0; 855 return 0;
864 856
865err_invalid_mask: 857error_free_scan_mask:
866 kfree(trialmask); 858 kfree(buffer->scan_mask);
867 return -EINVAL; 859error_cleanup_dynamic:
860 iio_free_chan_devattr_list(&buffer->scan_el_dev_attr_list);
861 kfree(indio_dev->buffer->buffer_group.attrs);
862
863 return ret;
864}
865
866void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
867{
868 if (!indio_dev->buffer)
869 return;
870
871 kfree(indio_dev->buffer->scan_mask);
872 kfree(indio_dev->buffer->buffer_group.attrs);
873 kfree(indio_dev->buffer->scan_el_group.attrs);
874 iio_free_chan_devattr_list(&indio_dev->buffer->scan_el_dev_attr_list);
875}
876
877/**
878 * iio_validate_scan_mask_onehot() - Validates that exactly one channel is selected
879 * @indio_dev: the iio device
880 * @mask: scan mask to be checked
881 *
882 * Return true if exactly one bit is set in the scan mask, false otherwise. It
883 * can be used for devices where only one channel can be active for sampling at
884 * a time.
885 */
886bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
887 const unsigned long *mask)
888{
889 return bitmap_weight(mask, indio_dev->masklength) == 1;
868} 890}
869EXPORT_SYMBOL_GPL(iio_scan_mask_set); 891EXPORT_SYMBOL_GPL(iio_validate_scan_mask_onehot);
870 892
871int iio_scan_mask_query(struct iio_dev *indio_dev, 893int iio_scan_mask_query(struct iio_dev *indio_dev,
872 struct iio_buffer *buffer, int bit) 894 struct iio_buffer *buffer, int bit)
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index af3e76d652ba..69feb912515a 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -70,6 +70,8 @@ static const char * const iio_chan_type_name_spec[] = {
70 [IIO_CCT] = "cct", 70 [IIO_CCT] = "cct",
71 [IIO_PRESSURE] = "pressure", 71 [IIO_PRESSURE] = "pressure",
72 [IIO_HUMIDITYRELATIVE] = "humidityrelative", 72 [IIO_HUMIDITYRELATIVE] = "humidityrelative",
73 [IIO_ACTIVITY] = "activity",
74 [IIO_STEPS] = "steps",
73}; 75};
74 76
75static const char * const iio_modifier_names[] = { 77static const char * const iio_modifier_names[] = {
@@ -91,6 +93,10 @@ static const char * const iio_modifier_names[] = {
91 [IIO_MOD_NORTH_TRUE] = "from_north_true", 93 [IIO_MOD_NORTH_TRUE] = "from_north_true",
92 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp", 94 [IIO_MOD_NORTH_MAGN_TILT_COMP] = "from_north_magnetic_tilt_comp",
93 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp", 95 [IIO_MOD_NORTH_TRUE_TILT_COMP] = "from_north_true_tilt_comp",
96 [IIO_MOD_RUNNING] = "running",
97 [IIO_MOD_JOGGING] = "jogging",
98 [IIO_MOD_WALKING] = "walking",
99 [IIO_MOD_STILL] = "still",
94}; 100};
95 101
96/* relies on pairs of these shared then separate */ 102/* relies on pairs of these shared then separate */
@@ -113,6 +119,8 @@ static const char * const iio_chan_info_postfix[] = {
113 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain", 119 [IIO_CHAN_INFO_HARDWAREGAIN] = "hardwaregain",
114 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis", 120 [IIO_CHAN_INFO_HYSTERESIS] = "hysteresis",
115 [IIO_CHAN_INFO_INT_TIME] = "integration_time", 121 [IIO_CHAN_INFO_INT_TIME] = "integration_time",
122 [IIO_CHAN_INFO_ENABLE] = "en",
123 [IIO_CHAN_INFO_CALIBHEIGHT] = "calibheight",
116}; 124};
117 125
118/** 126/**
@@ -1035,7 +1043,6 @@ struct iio_dev *devm_iio_device_alloc(struct device *dev, int sizeof_priv)
1035 if (!ptr) 1043 if (!ptr)
1036 return NULL; 1044 return NULL;
1037 1045
1038 /* use raw alloc_dr for kmalloc caller tracing */
1039 iio_dev = iio_device_alloc(sizeof_priv); 1046 iio_dev = iio_device_alloc(sizeof_priv);
1040 if (iio_dev) { 1047 if (iio_dev) {
1041 *ptr = iio_dev; 1048 *ptr = iio_dev;
@@ -1127,6 +1134,29 @@ static const struct file_operations iio_buffer_fileops = {
1127 .compat_ioctl = iio_ioctl, 1134 .compat_ioctl = iio_ioctl,
1128}; 1135};
1129 1136
1137static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
1138{
1139 int i, j;
1140 const struct iio_chan_spec *channels = indio_dev->channels;
1141
1142 if (!(indio_dev->modes & INDIO_ALL_BUFFER_MODES))
1143 return 0;
1144
1145 for (i = 0; i < indio_dev->num_channels - 1; i++) {
1146 if (channels[i].scan_index < 0)
1147 continue;
1148 for (j = i + 1; j < indio_dev->num_channels; j++)
1149 if (channels[i].scan_index == channels[j].scan_index) {
1150 dev_err(&indio_dev->dev,
1151 "Duplicate scan index %d\n",
1152 channels[i].scan_index);
1153 return -EINVAL;
1154 }
1155 }
1156
1157 return 0;
1158}
1159
1130static const struct iio_buffer_setup_ops noop_ring_setup_ops; 1160static const struct iio_buffer_setup_ops noop_ring_setup_ops;
1131 1161
1132/** 1162/**
@@ -1141,6 +1171,10 @@ int iio_device_register(struct iio_dev *indio_dev)
1141 if (!indio_dev->dev.of_node && indio_dev->dev.parent) 1171 if (!indio_dev->dev.of_node && indio_dev->dev.parent)
1142 indio_dev->dev.of_node = indio_dev->dev.parent->of_node; 1172 indio_dev->dev.of_node = indio_dev->dev.parent->of_node;
1143 1173
1174 ret = iio_check_unique_scan_index(indio_dev);
1175 if (ret < 0)
1176 return ret;
1177
1144 /* configure elements for the chrdev */ 1178 /* configure elements for the chrdev */
1145 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id); 1179 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
1146 1180
@@ -1150,11 +1184,19 @@ int iio_device_register(struct iio_dev *indio_dev)
1150 "Failed to register debugfs interfaces\n"); 1184 "Failed to register debugfs interfaces\n");
1151 return ret; 1185 return ret;
1152 } 1186 }
1187
1188 ret = iio_buffer_alloc_sysfs_and_mask(indio_dev);
1189 if (ret) {
1190 dev_err(indio_dev->dev.parent,
1191 "Failed to create buffer sysfs interfaces\n");
1192 goto error_unreg_debugfs;
1193 }
1194
1153 ret = iio_device_register_sysfs(indio_dev); 1195 ret = iio_device_register_sysfs(indio_dev);
1154 if (ret) { 1196 if (ret) {
1155 dev_err(indio_dev->dev.parent, 1197 dev_err(indio_dev->dev.parent,
1156 "Failed to register sysfs interfaces\n"); 1198 "Failed to register sysfs interfaces\n");
1157 goto error_unreg_debugfs; 1199 goto error_buffer_free_sysfs;
1158 } 1200 }
1159 ret = iio_device_register_eventset(indio_dev); 1201 ret = iio_device_register_eventset(indio_dev);
1160 if (ret) { 1202 if (ret) {
@@ -1187,6 +1229,8 @@ error_unreg_eventset:
1187 iio_device_unregister_eventset(indio_dev); 1229 iio_device_unregister_eventset(indio_dev);
1188error_free_sysfs: 1230error_free_sysfs:
1189 iio_device_unregister_sysfs(indio_dev); 1231 iio_device_unregister_sysfs(indio_dev);
1232error_buffer_free_sysfs:
1233 iio_buffer_free_sysfs_and_mask(indio_dev);
1190error_unreg_debugfs: 1234error_unreg_debugfs:
1191 iio_device_unregister_debugfs(indio_dev); 1235 iio_device_unregister_debugfs(indio_dev);
1192 return ret; 1236 return ret;
@@ -1215,6 +1259,8 @@ void iio_device_unregister(struct iio_dev *indio_dev)
1215 iio_buffer_wakeup_poll(indio_dev); 1259 iio_buffer_wakeup_poll(indio_dev);
1216 1260
1217 mutex_unlock(&indio_dev->info_exist_lock); 1261 mutex_unlock(&indio_dev->info_exist_lock);
1262
1263 iio_buffer_free_sysfs_and_mask(indio_dev);
1218} 1264}
1219EXPORT_SYMBOL(iio_device_unregister); 1265EXPORT_SYMBOL(iio_device_unregister);
1220 1266
diff --git a/drivers/iio/industrialio-event.c b/drivers/iio/industrialio-event.c
index 0c1e37e3120a..3f5cee0295c5 100644
--- a/drivers/iio/industrialio-event.c
+++ b/drivers/iio/industrialio-event.c
@@ -197,6 +197,7 @@ static const char * const iio_ev_type_text[] = {
197 [IIO_EV_TYPE_ROC] = "roc", 197 [IIO_EV_TYPE_ROC] = "roc",
198 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive", 198 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
199 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive", 199 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
200 [IIO_EV_TYPE_INSTANCE] = "instance",
200}; 201};
201 202
202static const char * const iio_ev_dir_text[] = { 203static const char * const iio_ev_dir_text[] = {
@@ -327,9 +328,15 @@ static int iio_device_add_event(struct iio_dev *indio_dev,
327 for_each_set_bit(i, mask, sizeof(*mask)*8) { 328 for_each_set_bit(i, mask, sizeof(*mask)*8) {
328 if (i >= ARRAY_SIZE(iio_ev_info_text)) 329 if (i >= ARRAY_SIZE(iio_ev_info_text))
329 return -EINVAL; 330 return -EINVAL;
330 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s", 331 if (dir != IIO_EV_DIR_NONE)
331 iio_ev_type_text[type], iio_ev_dir_text[dir], 332 postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
332 iio_ev_info_text[i]); 333 iio_ev_type_text[type],
334 iio_ev_dir_text[dir],
335 iio_ev_info_text[i]);
336 else
337 postfix = kasprintf(GFP_KERNEL, "%s_%s",
338 iio_ev_type_text[type],
339 iio_ev_info_text[i]);
333 if (postfix == NULL) 340 if (postfix == NULL)
334 return -ENOMEM; 341 return -ENOMEM;
335 342
diff --git a/drivers/iio/industrialio-triggered-buffer.c b/drivers/iio/industrialio-triggered-buffer.c
index d6f54930b34a..15a5341b5e7b 100644
--- a/drivers/iio/industrialio-triggered-buffer.c
+++ b/drivers/iio/industrialio-triggered-buffer.c
@@ -32,7 +32,7 @@ static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
32 * 32 *
33 * This function combines some common tasks which will normally be performed 33 * This function combines some common tasks which will normally be performed
34 * when setting up a triggered buffer. It will allocate the buffer and the 34 * when setting up a triggered buffer. It will allocate the buffer and the
35 * pollfunc, as well as register the buffer with the IIO core. 35 * pollfunc.
36 * 36 *
37 * Before calling this function the indio_dev structure should already be 37 * Before calling this function the indio_dev structure should already be
38 * completely initialized, but not yet registered. In practice this means that 38 * completely initialized, but not yet registered. In practice this means that
@@ -49,7 +49,7 @@ int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
49 struct iio_buffer *buffer; 49 struct iio_buffer *buffer;
50 int ret; 50 int ret;
51 51
52 buffer = iio_kfifo_allocate(indio_dev); 52 buffer = iio_kfifo_allocate();
53 if (!buffer) { 53 if (!buffer) {
54 ret = -ENOMEM; 54 ret = -ENOMEM;
55 goto error_ret; 55 goto error_ret;
@@ -78,16 +78,8 @@ int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
78 /* Flag that polled ring buffering is possible */ 78 /* Flag that polled ring buffering is possible */
79 indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 79 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
80 80
81 ret = iio_buffer_register(indio_dev,
82 indio_dev->channels,
83 indio_dev->num_channels);
84 if (ret)
85 goto error_dealloc_pollfunc;
86
87 return 0; 81 return 0;
88 82
89error_dealloc_pollfunc:
90 iio_dealloc_pollfunc(indio_dev->pollfunc);
91error_kfifo_free: 83error_kfifo_free:
92 iio_kfifo_free(indio_dev->buffer); 84 iio_kfifo_free(indio_dev->buffer);
93error_ret: 85error_ret:
@@ -101,7 +93,6 @@ EXPORT_SYMBOL(iio_triggered_buffer_setup);
101 */ 93 */
102void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev) 94void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
103{ 95{
104 iio_buffer_unregister(indio_dev);
105 iio_dealloc_pollfunc(indio_dev->pollfunc); 96 iio_dealloc_pollfunc(indio_dev->pollfunc);
106 iio_kfifo_free(indio_dev->buffer); 97 iio_kfifo_free(indio_dev->buffer);
107} 98}
diff --git a/drivers/iio/inkern.c b/drivers/iio/inkern.c
index 90c8cb727cc7..c8bad3cf891d 100644
--- a/drivers/iio/inkern.c
+++ b/drivers/iio/inkern.c
@@ -116,8 +116,11 @@ static int __of_iio_simple_xlate(struct iio_dev *indio_dev,
116 if (!iiospec->args_count) 116 if (!iiospec->args_count)
117 return 0; 117 return 0;
118 118
119 if (iiospec->args[0] >= indio_dev->num_channels) 119 if (iiospec->args[0] >= indio_dev->num_channels) {
120 dev_err(&indio_dev->dev, "invalid channel index %u\n",
121 iiospec->args[0]);
120 return -EINVAL; 122 return -EINVAL;
123 }
121 124
122 return iiospec->args[0]; 125 return iiospec->args[0];
123} 126}
@@ -634,3 +637,28 @@ err_unlock:
634 return ret; 637 return ret;
635} 638}
636EXPORT_SYMBOL_GPL(iio_get_channel_type); 639EXPORT_SYMBOL_GPL(iio_get_channel_type);
640
641static int iio_channel_write(struct iio_channel *chan, int val, int val2,
642 enum iio_chan_info_enum info)
643{
644 return chan->indio_dev->info->write_raw(chan->indio_dev,
645 chan->channel, val, val2, info);
646}
647
648int iio_write_channel_raw(struct iio_channel *chan, int val)
649{
650 int ret;
651
652 mutex_lock(&chan->indio_dev->info_exist_lock);
653 if (chan->indio_dev->info == NULL) {
654 ret = -ENODEV;
655 goto err_unlock;
656 }
657
658 ret = iio_channel_write(chan, val, 0, IIO_CHAN_INFO_RAW);
659err_unlock:
660 mutex_unlock(&chan->indio_dev->info_exist_lock);
661
662 return ret;
663}
664EXPORT_SYMBOL_GPL(iio_write_channel_raw);
diff --git a/drivers/iio/kfifo_buf.c b/drivers/iio/kfifo_buf.c
index 7134e8ada09a..b2beea01c49b 100644
--- a/drivers/iio/kfifo_buf.c
+++ b/drivers/iio/kfifo_buf.c
@@ -47,30 +47,6 @@ static int iio_request_update_kfifo(struct iio_buffer *r)
47 return ret; 47 return ret;
48} 48}
49 49
50static int iio_get_length_kfifo(struct iio_buffer *r)
51{
52 return r->length;
53}
54
55static IIO_BUFFER_ENABLE_ATTR;
56static IIO_BUFFER_LENGTH_ATTR;
57
58static struct attribute *iio_kfifo_attributes[] = {
59 &dev_attr_length.attr,
60 &dev_attr_enable.attr,
61 NULL,
62};
63
64static struct attribute_group iio_kfifo_attribute_group = {
65 .attrs = iio_kfifo_attributes,
66 .name = "buffer",
67};
68
69static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
70{
71 return r->bytes_per_datum;
72}
73
74static int iio_mark_update_needed_kfifo(struct iio_buffer *r) 50static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
75{ 51{
76 struct iio_kfifo *kf = iio_to_kfifo(r); 52 struct iio_kfifo *kf = iio_to_kfifo(r);
@@ -159,26 +135,25 @@ static const struct iio_buffer_access_funcs kfifo_access_funcs = {
159 .read_first_n = &iio_read_first_n_kfifo, 135 .read_first_n = &iio_read_first_n_kfifo,
160 .data_available = iio_kfifo_buf_data_available, 136 .data_available = iio_kfifo_buf_data_available,
161 .request_update = &iio_request_update_kfifo, 137 .request_update = &iio_request_update_kfifo,
162 .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
163 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, 138 .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
164 .get_length = &iio_get_length_kfifo,
165 .set_length = &iio_set_length_kfifo, 139 .set_length = &iio_set_length_kfifo,
166 .release = &iio_kfifo_buffer_release, 140 .release = &iio_kfifo_buffer_release,
167}; 141};
168 142
169struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev) 143struct iio_buffer *iio_kfifo_allocate(void)
170{ 144{
171 struct iio_kfifo *kf; 145 struct iio_kfifo *kf;
172 146
173 kf = kzalloc(sizeof *kf, GFP_KERNEL); 147 kf = kzalloc(sizeof(*kf), GFP_KERNEL);
174 if (!kf) 148 if (!kf)
175 return NULL; 149 return NULL;
150
176 kf->update_needed = true; 151 kf->update_needed = true;
177 iio_buffer_init(&kf->buffer); 152 iio_buffer_init(&kf->buffer);
178 kf->buffer.attrs = &iio_kfifo_attribute_group;
179 kf->buffer.access = &kfifo_access_funcs; 153 kf->buffer.access = &kfifo_access_funcs;
180 kf->buffer.length = 2; 154 kf->buffer.length = 2;
181 mutex_init(&kf->user_lock); 155 mutex_init(&kf->user_lock);
156
182 return &kf->buffer; 157 return &kf->buffer;
183} 158}
184EXPORT_SYMBOL(iio_kfifo_allocate); 159EXPORT_SYMBOL(iio_kfifo_allocate);
@@ -189,4 +164,58 @@ void iio_kfifo_free(struct iio_buffer *r)
189} 164}
190EXPORT_SYMBOL(iio_kfifo_free); 165EXPORT_SYMBOL(iio_kfifo_free);
191 166
167static void devm_iio_kfifo_release(struct device *dev, void *res)
168{
169 iio_kfifo_free(*(struct iio_buffer **)res);
170}
171
172static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
173{
174 struct iio_buffer **r = res;
175
176 if (WARN_ON(!r || !*r))
177 return 0;
178
179 return *r == data;
180}
181
182/**
183 * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
184 * @dev: Device to allocate kfifo buffer for
185 *
186 * RETURNS:
187 * Pointer to allocated iio_buffer on success, NULL on failure.
188 */
189struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
190{
191 struct iio_buffer **ptr, *r;
192
193 ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
194 if (!ptr)
195 return NULL;
196
197 r = iio_kfifo_allocate();
198 if (r) {
199 *ptr = r;
200 devres_add(dev, ptr);
201 } else {
202 devres_free(ptr);
203 }
204
205 return r;
206}
207EXPORT_SYMBOL(devm_iio_kfifo_allocate);
208
209/**
210 * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
211 * @dev: Device the buffer belongs to
212 * @r: The buffer associated with the device
213 */
214void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
215{
216 WARN_ON(devres_release(dev, devm_iio_kfifo_release,
217 devm_iio_kfifo_match, r));
218}
219EXPORT_SYMBOL(devm_iio_kfifo_free);
220
192MODULE_LICENSE("GPL"); 221MODULE_LICENSE("GPL");
diff --git a/drivers/iio/light/Kconfig b/drivers/iio/light/Kconfig
index 5bea821adcae..5a3237b2aaa5 100644
--- a/drivers/iio/light/Kconfig
+++ b/drivers/iio/light/Kconfig
@@ -95,6 +95,9 @@ config HID_SENSOR_ALS
95 Say yes here to build support for the HID SENSOR 95 Say yes here to build support for the HID SENSOR
96 Ambient light sensor. 96 Ambient light sensor.
97 97
98 To compile this driver as a module, choose M here: the
99 module will be called hid-sensor-als.
100
98config HID_SENSOR_PROX 101config HID_SENSOR_PROX
99 depends on HID_SENSOR_HUB 102 depends on HID_SENSOR_HUB
100 select IIO_BUFFER 103 select IIO_BUFFER
@@ -109,6 +112,16 @@ config HID_SENSOR_PROX
109 To compile this driver as a module, choose M here: the 112 To compile this driver as a module, choose M here: the
110 module will be called hid-sensor-prox. 113 module will be called hid-sensor-prox.
111 114
115config JSA1212
116 tristate "JSA1212 ALS and proximity sensor driver"
117 depends on I2C
118 help
119 Say Y here if you want to build a IIO driver for JSA1212
120 proximity & ALS sensor device.
121
122 To compile this driver as a module, choose M here:
123 the module will be called jsa1212.
124
112config SENSORS_LM3533 125config SENSORS_LM3533
113 tristate "LM3533 ambient light sensor" 126 tristate "LM3533 ambient light sensor"
114 depends on MFD_LM3533 127 depends on MFD_LM3533
diff --git a/drivers/iio/light/Makefile b/drivers/iio/light/Makefile
index 47877a36cc12..74656c19a899 100644
--- a/drivers/iio/light/Makefile
+++ b/drivers/iio/light/Makefile
@@ -12,6 +12,7 @@ obj-$(CONFIG_GP2AP020A00F) += gp2ap020a00f.o
12obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o 12obj-$(CONFIG_HID_SENSOR_ALS) += hid-sensor-als.o
13obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o 13obj-$(CONFIG_HID_SENSOR_PROX) += hid-sensor-prox.o
14obj-$(CONFIG_ISL29125) += isl29125.o 14obj-$(CONFIG_ISL29125) += isl29125.o
15obj-$(CONFIG_JSA1212) += jsa1212.o
15obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o 16obj-$(CONFIG_SENSORS_LM3533) += lm3533-als.o
16obj-$(CONFIG_LTR501) += ltr501.o 17obj-$(CONFIG_LTR501) += ltr501.o
17obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o 18obj-$(CONFIG_SENSORS_TSL2563) += tsl2563.o
diff --git a/drivers/iio/light/jsa1212.c b/drivers/iio/light/jsa1212.c
new file mode 100644
index 000000000000..29de7e7d9562
--- /dev/null
+++ b/drivers/iio/light/jsa1212.c
@@ -0,0 +1,471 @@
1/*
2 * JSA1212 Ambient Light & Proximity Sensor Driver
3 *
4 * Copyright (c) 2014, Intel Corporation.
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 * JSA1212 I2C slave address: 0x44(ADDR tied to GND), 0x45(ADDR tied to VDD)
16 *
17 * TODO: Interrupt support, thresholds, range support.
18 */
19
20#include <linux/kernel.h>
21#include <linux/slab.h>
22#include <linux/module.h>
23#include <linux/delay.h>
24#include <linux/i2c.h>
25#include <linux/mutex.h>
26#include <linux/acpi.h>
27#include <linux/regmap.h>
28#include <linux/iio/iio.h>
29#include <linux/iio/sysfs.h>
30
31/* JSA1212 reg address */
32#define JSA1212_CONF_REG 0x01
33#define JSA1212_INT_REG 0x02
34#define JSA1212_PXS_LT_REG 0x03
35#define JSA1212_PXS_HT_REG 0x04
36#define JSA1212_ALS_TH1_REG 0x05
37#define JSA1212_ALS_TH2_REG 0x06
38#define JSA1212_ALS_TH3_REG 0x07
39#define JSA1212_PXS_DATA_REG 0x08
40#define JSA1212_ALS_DT1_REG 0x09
41#define JSA1212_ALS_DT2_REG 0x0A
42#define JSA1212_ALS_RNG_REG 0x0B
43#define JSA1212_MAX_REG 0x0C
44
45/* JSA1212 reg masks */
46#define JSA1212_CONF_MASK 0xFF
47#define JSA1212_INT_MASK 0xFF
48#define JSA1212_PXS_LT_MASK 0xFF
49#define JSA1212_PXS_HT_MASK 0xFF
50#define JSA1212_ALS_TH1_MASK 0xFF
51#define JSA1212_ALS_TH2_LT_MASK 0x0F
52#define JSA1212_ALS_TH2_HT_MASK 0xF0
53#define JSA1212_ALS_TH3_MASK 0xFF
54#define JSA1212_PXS_DATA_MASK 0xFF
55#define JSA1212_ALS_DATA_MASK 0x0FFF
56#define JSA1212_ALS_DT1_MASK 0xFF
57#define JSA1212_ALS_DT2_MASK 0x0F
58#define JSA1212_ALS_RNG_MASK 0x07
59
60/* JSA1212 CONF REG bits */
61#define JSA1212_CONF_PXS_MASK 0x80
62#define JSA1212_CONF_PXS_ENABLE 0x80
63#define JSA1212_CONF_PXS_DISABLE 0x00
64#define JSA1212_CONF_ALS_MASK 0x04
65#define JSA1212_CONF_ALS_ENABLE 0x04
66#define JSA1212_CONF_ALS_DISABLE 0x00
67#define JSA1212_CONF_IRDR_MASK 0x08
68/* Proxmity sensing IRDR current sink settings */
69#define JSA1212_CONF_IRDR_200MA 0x08
70#define JSA1212_CONF_IRDR_100MA 0x00
71#define JSA1212_CONF_PXS_SLP_MASK 0x70
72#define JSA1212_CONF_PXS_SLP_0MS 0x70
73#define JSA1212_CONF_PXS_SLP_12MS 0x60
74#define JSA1212_CONF_PXS_SLP_50MS 0x50
75#define JSA1212_CONF_PXS_SLP_75MS 0x40
76#define JSA1212_CONF_PXS_SLP_100MS 0x30
77#define JSA1212_CONF_PXS_SLP_200MS 0x20
78#define JSA1212_CONF_PXS_SLP_400MS 0x10
79#define JSA1212_CONF_PXS_SLP_800MS 0x00
80
81/* JSA1212 INT REG bits */
82#define JSA1212_INT_CTRL_MASK 0x01
83#define JSA1212_INT_CTRL_EITHER 0x00
84#define JSA1212_INT_CTRL_BOTH 0x01
85#define JSA1212_INT_ALS_PRST_MASK 0x06
86#define JSA1212_INT_ALS_PRST_1CONV 0x00
87#define JSA1212_INT_ALS_PRST_4CONV 0x02
88#define JSA1212_INT_ALS_PRST_8CONV 0x04
89#define JSA1212_INT_ALS_PRST_16CONV 0x06
90#define JSA1212_INT_ALS_FLAG_MASK 0x08
91#define JSA1212_INT_ALS_FLAG_CLR 0x00
92#define JSA1212_INT_PXS_PRST_MASK 0x60
93#define JSA1212_INT_PXS_PRST_1CONV 0x00
94#define JSA1212_INT_PXS_PRST_4CONV 0x20
95#define JSA1212_INT_PXS_PRST_8CONV 0x40
96#define JSA1212_INT_PXS_PRST_16CONV 0x60
97#define JSA1212_INT_PXS_FLAG_MASK 0x80
98#define JSA1212_INT_PXS_FLAG_CLR 0x00
99
100/* JSA1212 ALS RNG REG bits */
101#define JSA1212_ALS_RNG_0_2048 0x00
102#define JSA1212_ALS_RNG_0_1024 0x01
103#define JSA1212_ALS_RNG_0_512 0x02
104#define JSA1212_ALS_RNG_0_256 0x03
105#define JSA1212_ALS_RNG_0_128 0x04
106
107/* JSA1212 INT threshold range */
108#define JSA1212_ALS_TH_MIN 0x0000
109#define JSA1212_ALS_TH_MAX 0x0FFF
110#define JSA1212_PXS_TH_MIN 0x00
111#define JSA1212_PXS_TH_MAX 0xFF
112
113#define JSA1212_ALS_DELAY_MS 200
114#define JSA1212_PXS_DELAY_MS 100
115
116#define JSA1212_DRIVER_NAME "jsa1212"
117#define JSA1212_REGMAP_NAME "jsa1212_regmap"
118
119enum jsa1212_op_mode {
120 JSA1212_OPMODE_ALS_EN,
121 JSA1212_OPMODE_PXS_EN,
122};
123
124struct jsa1212_data {
125 struct i2c_client *client;
126 struct mutex lock;
127 u8 als_rng_idx;
128 bool als_en; /* ALS enable status */
129 bool pxs_en; /* proximity enable status */
130 struct regmap *regmap;
131};
132
133/* ALS range idx to val mapping */
134static const int jsa1212_als_range_val[] = {2048, 1024, 512, 256, 128,
135 128, 128, 128};
136
137/* Enables or disables ALS function based on status */
138static int jsa1212_als_enable(struct jsa1212_data *data, u8 status)
139{
140 int ret;
141
142 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
143 JSA1212_CONF_ALS_MASK,
144 status);
145 if (ret < 0)
146 return ret;
147
148 data->als_en = !!status;
149
150 return 0;
151}
152
153/* Enables or disables PXS function based on status */
154static int jsa1212_pxs_enable(struct jsa1212_data *data, u8 status)
155{
156 int ret;
157
158 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
159 JSA1212_CONF_PXS_MASK,
160 status);
161 if (ret < 0)
162 return ret;
163
164 data->pxs_en = !!status;
165
166 return 0;
167}
168
169static int jsa1212_read_als_data(struct jsa1212_data *data,
170 unsigned int *val)
171{
172 int ret;
173 __le16 als_data;
174
175 ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
176 if (ret < 0)
177 return ret;
178
179 /* Delay for data output */
180 msleep(JSA1212_ALS_DELAY_MS);
181
182 /* Read 12 bit data */
183 ret = regmap_bulk_read(data->regmap, JSA1212_ALS_DT1_REG, &als_data, 2);
184 if (ret < 0) {
185 dev_err(&data->client->dev, "als data read err\n");
186 goto als_data_read_err;
187 }
188
189 *val = le16_to_cpu(als_data);
190
191als_data_read_err:
192 return jsa1212_als_enable(data, JSA1212_CONF_ALS_DISABLE);
193}
194
195static int jsa1212_read_pxs_data(struct jsa1212_data *data,
196 unsigned int *val)
197{
198 int ret;
199 unsigned int pxs_data;
200
201 ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
202 if (ret < 0)
203 return ret;
204
205 /* Delay for data output */
206 msleep(JSA1212_PXS_DELAY_MS);
207
208 /* Read out all data */
209 ret = regmap_read(data->regmap, JSA1212_PXS_DATA_REG, &pxs_data);
210 if (ret < 0) {
211 dev_err(&data->client->dev, "pxs data read err\n");
212 goto pxs_data_read_err;
213 }
214
215 *val = pxs_data & JSA1212_PXS_DATA_MASK;
216
217pxs_data_read_err:
218 return jsa1212_pxs_enable(data, JSA1212_CONF_PXS_DISABLE);
219}
220
221static int jsa1212_read_raw(struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan,
223 int *val, int *val2, long mask)
224{
225 int ret;
226 struct jsa1212_data *data = iio_priv(indio_dev);
227
228 switch (mask) {
229 case IIO_CHAN_INFO_RAW:
230 mutex_lock(&data->lock);
231 switch (chan->type) {
232 case IIO_LIGHT:
233 ret = jsa1212_read_als_data(data, val);
234 break;
235 case IIO_PROXIMITY:
236 ret = jsa1212_read_pxs_data(data, val);
237 break;
238 default:
239 ret = -EINVAL;
240 break;
241 }
242 mutex_unlock(&data->lock);
243 return ret < 0 ? ret : IIO_VAL_INT;
244 case IIO_CHAN_INFO_SCALE:
245 switch (chan->type) {
246 case IIO_LIGHT:
247 *val = jsa1212_als_range_val[data->als_rng_idx];
248 *val2 = BIT(12); /* Max 12 bit value */
249 return IIO_VAL_FRACTIONAL;
250 default:
251 break;
252 }
253 break;
254 default:
255 break;
256 }
257
258 return -EINVAL;
259}
260
261static const struct iio_chan_spec jsa1212_channels[] = {
262 {
263 .type = IIO_LIGHT,
264 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
265 BIT(IIO_CHAN_INFO_SCALE),
266 },
267 {
268 .type = IIO_PROXIMITY,
269 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
270 }
271};
272
273static const struct iio_info jsa1212_info = {
274 .driver_module = THIS_MODULE,
275 .read_raw = &jsa1212_read_raw,
276};
277
278static int jsa1212_chip_init(struct jsa1212_data *data)
279{
280 int ret;
281
282 ret = regmap_write(data->regmap, JSA1212_CONF_REG,
283 (JSA1212_CONF_PXS_SLP_50MS |
284 JSA1212_CONF_IRDR_200MA));
285 if (ret < 0)
286 return ret;
287
288 ret = regmap_write(data->regmap, JSA1212_INT_REG,
289 JSA1212_INT_ALS_PRST_4CONV);
290 if (ret < 0)
291 return ret;
292
293 data->als_rng_idx = JSA1212_ALS_RNG_0_2048;
294
295 return 0;
296}
297
298static bool jsa1212_is_volatile_reg(struct device *dev, unsigned int reg)
299{
300 switch (reg) {
301 case JSA1212_PXS_DATA_REG:
302 case JSA1212_ALS_DT1_REG:
303 case JSA1212_ALS_DT2_REG:
304 case JSA1212_INT_REG:
305 return true;
306 default:
307 return false;
308 }
309}
310
311static struct regmap_config jsa1212_regmap_config = {
312 .name = JSA1212_REGMAP_NAME,
313 .reg_bits = 8,
314 .val_bits = 8,
315 .max_register = JSA1212_MAX_REG,
316 .cache_type = REGCACHE_RBTREE,
317 .volatile_reg = jsa1212_is_volatile_reg,
318};
319
320static int jsa1212_probe(struct i2c_client *client,
321 const struct i2c_device_id *id)
322{
323 struct jsa1212_data *data;
324 struct iio_dev *indio_dev;
325 struct regmap *regmap;
326 int ret;
327
328 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
329 return -ENODEV;
330
331 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
332 if (!indio_dev)
333 return -ENOMEM;
334
335 regmap = devm_regmap_init_i2c(client, &jsa1212_regmap_config);
336 if (IS_ERR(regmap)) {
337 dev_err(&client->dev, "Regmap initialization failed.\n");
338 return PTR_ERR(regmap);
339 }
340
341 data = iio_priv(indio_dev);
342
343 i2c_set_clientdata(client, indio_dev);
344 data->client = client;
345 data->regmap = regmap;
346
347 mutex_init(&data->lock);
348
349 ret = jsa1212_chip_init(data);
350 if (ret < 0)
351 return ret;
352
353 indio_dev->dev.parent = &client->dev;
354 indio_dev->channels = jsa1212_channels;
355 indio_dev->num_channels = ARRAY_SIZE(jsa1212_channels);
356 indio_dev->name = JSA1212_DRIVER_NAME;
357 indio_dev->modes = INDIO_DIRECT_MODE;
358
359 indio_dev->info = &jsa1212_info;
360
361 ret = iio_device_register(indio_dev);
362 if (ret < 0)
363 dev_err(&client->dev, "%s: register device failed\n", __func__);
364
365 return ret;
366}
367
368 /* power off the device */
369static int jsa1212_power_off(struct jsa1212_data *data)
370{
371 int ret;
372
373 mutex_lock(&data->lock);
374
375 ret = regmap_update_bits(data->regmap, JSA1212_CONF_REG,
376 JSA1212_CONF_ALS_MASK |
377 JSA1212_CONF_PXS_MASK,
378 JSA1212_CONF_ALS_DISABLE |
379 JSA1212_CONF_PXS_DISABLE);
380
381 if (ret < 0)
382 dev_err(&data->client->dev, "power off cmd failed\n");
383
384 mutex_unlock(&data->lock);
385
386 return ret;
387}
388
389static int jsa1212_remove(struct i2c_client *client)
390{
391 struct iio_dev *indio_dev = i2c_get_clientdata(client);
392 struct jsa1212_data *data = iio_priv(indio_dev);
393
394 iio_device_unregister(indio_dev);
395
396 return jsa1212_power_off(data);
397}
398
399#ifdef CONFIG_PM_SLEEP
400static int jsa1212_suspend(struct device *dev)
401{
402 struct jsa1212_data *data;
403
404 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
405
406 return jsa1212_power_off(data);
407}
408
409static int jsa1212_resume(struct device *dev)
410{
411 int ret = 0;
412 struct jsa1212_data *data;
413
414 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
415
416 mutex_lock(&data->lock);
417
418 if (data->als_en) {
419 ret = jsa1212_als_enable(data, JSA1212_CONF_ALS_ENABLE);
420 if (ret < 0) {
421 dev_err(dev, "als resume failed\n");
422 goto unlock_and_ret;
423 }
424 }
425
426 if (data->pxs_en) {
427 ret = jsa1212_pxs_enable(data, JSA1212_CONF_PXS_ENABLE);
428 if (ret < 0)
429 dev_err(dev, "pxs resume failed\n");
430 }
431
432unlock_and_ret:
433 mutex_unlock(&data->lock);
434 return ret;
435}
436
437static SIMPLE_DEV_PM_OPS(jsa1212_pm_ops, jsa1212_suspend, jsa1212_resume);
438
439#define JSA1212_PM_OPS (&jsa1212_pm_ops)
440#else
441#define JSA1212_PM_OPS NULL
442#endif
443
444static const struct acpi_device_id jsa1212_acpi_match[] = {
445 {"JSA1212", 0},
446 { },
447};
448MODULE_DEVICE_TABLE(acpi, jsa1212_acpi_match);
449
450static const struct i2c_device_id jsa1212_id[] = {
451 { JSA1212_DRIVER_NAME, 0 },
452 { }
453};
454MODULE_DEVICE_TABLE(i2c, jsa1212_id);
455
456static struct i2c_driver jsa1212_driver = {
457 .driver = {
458 .name = JSA1212_DRIVER_NAME,
459 .pm = JSA1212_PM_OPS,
460 .owner = THIS_MODULE,
461 .acpi_match_table = ACPI_PTR(jsa1212_acpi_match),
462 },
463 .probe = jsa1212_probe,
464 .remove = jsa1212_remove,
465 .id_table = jsa1212_id,
466};
467module_i2c_driver(jsa1212_driver);
468
469MODULE_AUTHOR("Sathya Kuppuswamy <sathyanarayanan.kuppuswamy@linux.intel.com>");
470MODULE_DESCRIPTION("JSA1212 proximity/ambient light sensor driver");
471MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/light/lm3533-als.c b/drivers/iio/light/lm3533-als.c
index ae3c71bdd6c6..076bc46fad03 100644
--- a/drivers/iio/light/lm3533-als.c
+++ b/drivers/iio/light/lm3533-als.c
@@ -657,7 +657,7 @@ static ALS_HYSTERESIS_ATTR_RO(3);
657#define ILLUMINANCE_ATTR_RO(_name) \ 657#define ILLUMINANCE_ATTR_RO(_name) \
658 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO, show_##_name, NULL) 658 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO, show_##_name, NULL)
659#define ILLUMINANCE_ATTR_RW(_name) \ 659#define ILLUMINANCE_ATTR_RW(_name) \
660 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO | S_IWUSR , \ 660 DEVICE_ATTR(in_illuminance0_##_name, S_IRUGO | S_IWUSR, \
661 show_##_name, store_##_name) 661 show_##_name, store_##_name)
662/* 662/*
663 * ALS Zone threshold-event enable 663 * ALS Zone threshold-event enable
diff --git a/drivers/iio/magnetometer/Kconfig b/drivers/iio/magnetometer/Kconfig
index b2dba9e506ab..4c7a4c52dd06 100644
--- a/drivers/iio/magnetometer/Kconfig
+++ b/drivers/iio/magnetometer/Kconfig
@@ -6,26 +6,21 @@
6menu "Magnetometer sensors" 6menu "Magnetometer sensors"
7 7
8config AK8975 8config AK8975
9 tristate "Asahi Kasei AK8975 3-Axis Magnetometer" 9 tristate "Asahi Kasei AK 3-Axis Magnetometer"
10 depends on I2C 10 depends on I2C
11 depends on GPIOLIB 11 depends on GPIOLIB
12 help 12 help
13 Say yes here to build support for Asahi Kasei AK8975 3-Axis 13 Say yes here to build support for Asahi Kasei AK8975, AK8963,
14 Magnetometer. This driver can also support AK8963, if i2c 14 AK09911 or AK09912 3-Axis Magnetometer.
15 device name is identified as ak8963.
16 15
17 To compile this driver as a module, choose M here: the module 16 To compile this driver as a module, choose M here: the module
18 will be called ak8975. 17 will be called ak8975.
19 18
20config AK09911 19config AK09911
21 tristate "Asahi Kasei AK09911 3-axis Compass" 20 tristate "Asahi Kasei AK09911 3-axis Compass"
22 depends on I2C 21 select AK8975
23 help 22 help
24 Say yes here to build support for Asahi Kasei AK09911 3-Axis 23 Deprecated: AK09911 is now supported by AK8975 driver.
25 Magnetometer.
26
27 To compile this driver as a module, choose M here: the module
28 will be called ak09911.
29 24
30config MAG3110 25config MAG3110
31 tristate "Freescale MAG3110 3-Axis Magnetometer" 26 tristate "Freescale MAG3110 3-Axis Magnetometer"
diff --git a/drivers/iio/magnetometer/Makefile b/drivers/iio/magnetometer/Makefile
index b91315e0b826..0f5d3c985799 100644
--- a/drivers/iio/magnetometer/Makefile
+++ b/drivers/iio/magnetometer/Makefile
@@ -3,7 +3,6 @@
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_AK09911) += ak09911.o
7obj-$(CONFIG_AK8975) += ak8975.o 6obj-$(CONFIG_AK8975) += ak8975.o
8obj-$(CONFIG_MAG3110) += mag3110.o 7obj-$(CONFIG_MAG3110) += mag3110.o
9obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o 8obj-$(CONFIG_HID_SENSOR_MAGNETOMETER_3D) += hid-sensor-magn-3d.o
diff --git a/drivers/iio/magnetometer/ak09911.c b/drivers/iio/magnetometer/ak09911.c
deleted file mode 100644
index b2bc942ff6b8..000000000000
--- a/drivers/iio/magnetometer/ak09911.c
+++ /dev/null
@@ -1,326 +0,0 @@
1/*
2 * AK09911 3-axis compass driver
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/types.h>
19#include <linux/slab.h>
20#include <linux/delay.h>
21#include <linux/i2c.h>
22#include <linux/acpi.h>
23#include <linux/iio/iio.h>
24
25#define AK09911_REG_WIA1 0x00
26#define AK09911_REG_WIA2 0x01
27#define AK09911_WIA1_VALUE 0x48
28#define AK09911_WIA2_VALUE 0x05
29
30#define AK09911_REG_ST1 0x10
31#define AK09911_REG_HXL 0x11
32#define AK09911_REG_HXH 0x12
33#define AK09911_REG_HYL 0x13
34#define AK09911_REG_HYH 0x14
35#define AK09911_REG_HZL 0x15
36#define AK09911_REG_HZH 0x16
37
38#define AK09911_REG_ASAX 0x60
39#define AK09911_REG_ASAY 0x61
40#define AK09911_REG_ASAZ 0x62
41
42#define AK09911_REG_CNTL1 0x30
43#define AK09911_REG_CNTL2 0x31
44#define AK09911_REG_CNTL3 0x32
45
46#define AK09911_MODE_SNG_MEASURE 0x01
47#define AK09911_MODE_SELF_TEST 0x10
48#define AK09911_MODE_FUSE_ACCESS 0x1F
49#define AK09911_MODE_POWERDOWN 0x00
50#define AK09911_RESET_DATA 0x01
51
52#define AK09911_REG_CNTL1 0x30
53#define AK09911_REG_CNTL2 0x31
54#define AK09911_REG_CNTL3 0x32
55
56#define AK09911_RAW_TO_GAUSS(asa) ((((asa) + 128) * 6000) / 256)
57
58#define AK09911_MAX_CONVERSION_TIMEOUT_MS 500
59#define AK09911_CONVERSION_DONE_POLL_TIME_MS 10
60
61struct ak09911_data {
62 struct i2c_client *client;
63 struct mutex lock;
64 u8 asa[3];
65 long raw_to_gauss[3];
66};
67
68static const int ak09911_index_to_reg[] = {
69 AK09911_REG_HXL, AK09911_REG_HYL, AK09911_REG_HZL,
70};
71
72static int ak09911_set_mode(struct i2c_client *client, u8 mode)
73{
74 int ret;
75
76 switch (mode) {
77 case AK09911_MODE_SNG_MEASURE:
78 case AK09911_MODE_SELF_TEST:
79 case AK09911_MODE_FUSE_ACCESS:
80 case AK09911_MODE_POWERDOWN:
81 ret = i2c_smbus_write_byte_data(client,
82 AK09911_REG_CNTL2, mode);
83 if (ret < 0) {
84 dev_err(&client->dev, "set_mode error\n");
85 return ret;
86 }
87 /* After mode change wait atleast 100us */
88 usleep_range(100, 500);
89 break;
90 default:
91 dev_err(&client->dev,
92 "%s: Unknown mode(%d).", __func__, mode);
93 return -EINVAL;
94 }
95
96 return ret;
97}
98
99/* Get Sensitivity Adjustment value */
100static int ak09911_get_asa(struct i2c_client *client)
101{
102 struct iio_dev *indio_dev = i2c_get_clientdata(client);
103 struct ak09911_data *data = iio_priv(indio_dev);
104 int ret;
105
106 ret = ak09911_set_mode(client, AK09911_MODE_FUSE_ACCESS);
107 if (ret < 0)
108 return ret;
109
110 /* Get asa data and store in the device data. */
111 ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_ASAX,
112 3, data->asa);
113 if (ret < 0) {
114 dev_err(&client->dev, "Not able to read asa data\n");
115 return ret;
116 }
117
118 ret = ak09911_set_mode(client, AK09911_MODE_POWERDOWN);
119 if (ret < 0)
120 return ret;
121
122 data->raw_to_gauss[0] = AK09911_RAW_TO_GAUSS(data->asa[0]);
123 data->raw_to_gauss[1] = AK09911_RAW_TO_GAUSS(data->asa[1]);
124 data->raw_to_gauss[2] = AK09911_RAW_TO_GAUSS(data->asa[2]);
125
126 return 0;
127}
128
129static int ak09911_verify_chip_id(struct i2c_client *client)
130{
131 u8 wia_val[2];
132 int ret;
133
134 ret = i2c_smbus_read_i2c_block_data(client, AK09911_REG_WIA1,
135 2, wia_val);
136 if (ret < 0) {
137 dev_err(&client->dev, "Error reading WIA\n");
138 return ret;
139 }
140
141 dev_dbg(&client->dev, "WIA %02x %02x\n", wia_val[0], wia_val[1]);
142
143 if (wia_val[0] != AK09911_WIA1_VALUE ||
144 wia_val[1] != AK09911_WIA2_VALUE) {
145 dev_err(&client->dev, "Device ak09911 not found\n");
146 return -ENODEV;
147 }
148
149 return 0;
150}
151
152static int wait_conversion_complete_polled(struct ak09911_data *data)
153{
154 struct i2c_client *client = data->client;
155 u8 read_status;
156 u32 timeout_ms = AK09911_MAX_CONVERSION_TIMEOUT_MS;
157 int ret;
158
159 /* Wait for the conversion to complete. */
160 while (timeout_ms) {
161 msleep_interruptible(AK09911_CONVERSION_DONE_POLL_TIME_MS);
162 ret = i2c_smbus_read_byte_data(client, AK09911_REG_ST1);
163 if (ret < 0) {
164 dev_err(&client->dev, "Error in reading ST1\n");
165 return ret;
166 }
167 read_status = ret & 0x01;
168 if (read_status)
169 break;
170 timeout_ms -= AK09911_CONVERSION_DONE_POLL_TIME_MS;
171 }
172 if (!timeout_ms) {
173 dev_err(&client->dev, "Conversion timeout happened\n");
174 return -EIO;
175 }
176
177 return read_status;
178}
179
180static int ak09911_read_axis(struct iio_dev *indio_dev, int index, int *val)
181{
182 struct ak09911_data *data = iio_priv(indio_dev);
183 struct i2c_client *client = data->client;
184 int ret;
185
186 mutex_lock(&data->lock);
187
188 ret = ak09911_set_mode(client, AK09911_MODE_SNG_MEASURE);
189 if (ret < 0)
190 goto fn_exit;
191
192 ret = wait_conversion_complete_polled(data);
193 if (ret < 0)
194 goto fn_exit;
195
196 /* Read data */
197 ret = i2c_smbus_read_word_data(client, ak09911_index_to_reg[index]);
198 if (ret < 0) {
199 dev_err(&client->dev, "Read axis data fails\n");
200 goto fn_exit;
201 }
202
203 mutex_unlock(&data->lock);
204
205 /* Clamp to valid range. */
206 *val = sign_extend32(clamp_t(s16, ret, -8192, 8191), 13);
207
208 return IIO_VAL_INT;
209
210fn_exit:
211 mutex_unlock(&data->lock);
212
213 return ret;
214}
215
216static int ak09911_read_raw(struct iio_dev *indio_dev,
217 struct iio_chan_spec const *chan,
218 int *val, int *val2,
219 long mask)
220{
221 struct ak09911_data *data = iio_priv(indio_dev);
222
223 switch (mask) {
224 case IIO_CHAN_INFO_RAW:
225 return ak09911_read_axis(indio_dev, chan->address, val);
226 case IIO_CHAN_INFO_SCALE:
227 *val = 0;
228 *val2 = data->raw_to_gauss[chan->address];
229 return IIO_VAL_INT_PLUS_MICRO;
230 }
231
232 return -EINVAL;
233}
234
235#define AK09911_CHANNEL(axis, index) \
236 { \
237 .type = IIO_MAGN, \
238 .modified = 1, \
239 .channel2 = IIO_MOD_##axis, \
240 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
241 BIT(IIO_CHAN_INFO_SCALE), \
242 .address = index, \
243 }
244
245static const struct iio_chan_spec ak09911_channels[] = {
246 AK09911_CHANNEL(X, 0), AK09911_CHANNEL(Y, 1), AK09911_CHANNEL(Z, 2),
247};
248
249static const struct iio_info ak09911_info = {
250 .read_raw = &ak09911_read_raw,
251 .driver_module = THIS_MODULE,
252};
253
254static const struct acpi_device_id ak_acpi_match[] = {
255 {"AK009911", 0},
256 { },
257};
258MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
259
260static int ak09911_probe(struct i2c_client *client,
261 const struct i2c_device_id *id)
262{
263 struct iio_dev *indio_dev;
264 struct ak09911_data *data;
265 const char *name;
266 int ret;
267
268 ret = ak09911_verify_chip_id(client);
269 if (ret) {
270 dev_err(&client->dev, "AK00911 not detected\n");
271 return -ENODEV;
272 }
273
274 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
275 if (indio_dev == NULL)
276 return -ENOMEM;
277
278 data = iio_priv(indio_dev);
279 i2c_set_clientdata(client, indio_dev);
280
281 data->client = client;
282 mutex_init(&data->lock);
283
284 ret = ak09911_get_asa(client);
285 if (ret)
286 return ret;
287
288 if (id)
289 name = id->name;
290 else if (ACPI_HANDLE(&client->dev))
291 name = dev_name(&client->dev);
292 else
293 return -ENODEV;
294
295 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
296
297 indio_dev->dev.parent = &client->dev;
298 indio_dev->channels = ak09911_channels;
299 indio_dev->num_channels = ARRAY_SIZE(ak09911_channels);
300 indio_dev->info = &ak09911_info;
301 indio_dev->modes = INDIO_DIRECT_MODE;
302 indio_dev->name = name;
303
304 return devm_iio_device_register(&client->dev, indio_dev);
305}
306
307static const struct i2c_device_id ak09911_id[] = {
308 {"ak09911", 0},
309 {}
310};
311
312MODULE_DEVICE_TABLE(i2c, ak09911_id);
313
314static struct i2c_driver ak09911_driver = {
315 .driver = {
316 .name = "ak09911",
317 .acpi_match_table = ACPI_PTR(ak_acpi_match),
318 },
319 .probe = ak09911_probe,
320 .id_table = ak09911_id,
321};
322module_i2c_driver(ak09911_driver);
323
324MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>");
325MODULE_LICENSE("GPL v2");
326MODULE_DESCRIPTION("AK09911 Compass driver");
diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
index bf5ef077e791..0d10a4baceb6 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -64,10 +64,10 @@
64#define AK8975_REG_CNTL 0x0A 64#define AK8975_REG_CNTL 0x0A
65#define AK8975_REG_CNTL_MODE_SHIFT 0 65#define AK8975_REG_CNTL_MODE_SHIFT 0
66#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT) 66#define AK8975_REG_CNTL_MODE_MASK (0xF << AK8975_REG_CNTL_MODE_SHIFT)
67#define AK8975_REG_CNTL_MODE_POWER_DOWN 0 67#define AK8975_REG_CNTL_MODE_POWER_DOWN 0x00
68#define AK8975_REG_CNTL_MODE_ONCE 1 68#define AK8975_REG_CNTL_MODE_ONCE 0x01
69#define AK8975_REG_CNTL_MODE_SELF_TEST 8 69#define AK8975_REG_CNTL_MODE_SELF_TEST 0x08
70#define AK8975_REG_CNTL_MODE_FUSE_ROM 0xF 70#define AK8975_REG_CNTL_MODE_FUSE_ROM 0x0F
71 71
72#define AK8975_REG_RSVC 0x0B 72#define AK8975_REG_RSVC 0x0B
73#define AK8975_REG_ASTC 0x0C 73#define AK8975_REG_ASTC 0x0C
@@ -81,18 +81,278 @@
81#define AK8975_MAX_REGS AK8975_REG_ASAZ 81#define AK8975_MAX_REGS AK8975_REG_ASAZ
82 82
83/* 83/*
84 * AK09912 Register definitions
85 */
86#define AK09912_REG_WIA1 0x00
87#define AK09912_REG_WIA2 0x01
88#define AK09912_DEVICE_ID 0x04
89#define AK09911_DEVICE_ID 0x05
90
91#define AK09911_REG_INFO1 0x02
92#define AK09911_REG_INFO2 0x03
93
94#define AK09912_REG_ST1 0x10
95
96#define AK09912_REG_ST1_DRDY_SHIFT 0
97#define AK09912_REG_ST1_DRDY_MASK (1 << AK09912_REG_ST1_DRDY_SHIFT)
98
99#define AK09912_REG_HXL 0x11
100#define AK09912_REG_HXH 0x12
101#define AK09912_REG_HYL 0x13
102#define AK09912_REG_HYH 0x14
103#define AK09912_REG_HZL 0x15
104#define AK09912_REG_HZH 0x16
105#define AK09912_REG_TMPS 0x17
106
107#define AK09912_REG_ST2 0x18
108#define AK09912_REG_ST2_HOFL_SHIFT 3
109#define AK09912_REG_ST2_HOFL_MASK (1 << AK09912_REG_ST2_HOFL_SHIFT)
110
111#define AK09912_REG_CNTL1 0x30
112
113#define AK09912_REG_CNTL2 0x31
114#define AK09912_REG_CNTL_MODE_POWER_DOWN 0x00
115#define AK09912_REG_CNTL_MODE_ONCE 0x01
116#define AK09912_REG_CNTL_MODE_SELF_TEST 0x10
117#define AK09912_REG_CNTL_MODE_FUSE_ROM 0x1F
118#define AK09912_REG_CNTL2_MODE_SHIFT 0
119#define AK09912_REG_CNTL2_MODE_MASK (0x1F << AK09912_REG_CNTL2_MODE_SHIFT)
120
121#define AK09912_REG_CNTL3 0x32
122
123#define AK09912_REG_TS1 0x33
124#define AK09912_REG_TS2 0x34
125#define AK09912_REG_TS3 0x35
126#define AK09912_REG_I2CDIS 0x36
127#define AK09912_REG_TS4 0x37
128
129#define AK09912_REG_ASAX 0x60
130#define AK09912_REG_ASAY 0x61
131#define AK09912_REG_ASAZ 0x62
132
133#define AK09912_MAX_REGS AK09912_REG_ASAZ
134
135/*
84 * Miscellaneous values. 136 * Miscellaneous values.
85 */ 137 */
86#define AK8975_MAX_CONVERSION_TIMEOUT 500 138#define AK8975_MAX_CONVERSION_TIMEOUT 500
87#define AK8975_CONVERSION_DONE_POLL_TIME 10 139#define AK8975_CONVERSION_DONE_POLL_TIME 10
88#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000) 140#define AK8975_DATA_READY_TIMEOUT ((100*HZ)/1000)
89#define RAW_TO_GAUSS_8975(asa) ((((asa) + 128) * 3000) / 256) 141
90#define RAW_TO_GAUSS_8963(asa) ((((asa) + 128) * 6000) / 256) 142/*
143 * Precalculate scale factor (in Gauss units) for each axis and
144 * store in the device data.
145 *
146 * This scale factor is axis-dependent, and is derived from 3 calibration
147 * factors ASA(x), ASA(y), and ASA(z).
148 *
149 * These ASA values are read from the sensor device at start of day, and
150 * cached in the device context struct.
151 *
152 * Adjusting the flux value with the sensitivity adjustment value should be
153 * done via the following formula:
154 *
155 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
156 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
157 * is the resultant adjusted value.
158 *
159 * We reduce the formula to:
160 *
161 * Hadj = H * (ASA + 128) / 256
162 *
163 * H is in the range of -4096 to 4095. The magnetometer has a range of
164 * +-1229uT. To go from the raw value to uT is:
165 *
166 * HuT = H * 1229/4096, or roughly, 3/10.
167 *
168 * Since 1uT = 0.01 gauss, our final scale factor becomes:
169 *
170 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
171 * Hadj = H * ((ASA + 128) * 0.003) / 256
172 *
173 * Since ASA doesn't change, we cache the resultant scale factor into the
174 * device context in ak8975_setup().
175 *
176 * Given we use IIO_VAL_INT_PLUS_MICRO bit when displaying the scale, we
177 * multiply the stored scale value by 1e6.
178 */
179static long ak8975_raw_to_gauss(u16 data)
180{
181 return (((long)data + 128) * 3000) / 256;
182}
183
184/*
185 * For AK8963 and AK09911, same calculation, but the device is less sensitive:
186 *
187 * H is in the range of +-8190. The magnetometer has a range of
188 * +-4912uT. To go from the raw value to uT is:
189 *
190 * HuT = H * 4912/8190, or roughly, 6/10, instead of 3/10.
191 */
192
193static long ak8963_09911_raw_to_gauss(u16 data)
194{
195 return (((long)data + 128) * 6000) / 256;
196}
197
198/*
199 * For AK09912, same calculation, except the device is more sensitive:
200 *
201 * H is in the range of -32752 to 32752. The magnetometer has a range of
202 * +-4912uT. To go from the raw value to uT is:
203 *
204 * HuT = H * 4912/32752, or roughly, 3/20, instead of 3/10.
205 */
206static long ak09912_raw_to_gauss(u16 data)
207{
208 return (((long)data + 128) * 1500) / 256;
209}
91 210
92/* Compatible Asahi Kasei Compass parts */ 211/* Compatible Asahi Kasei Compass parts */
93enum asahi_compass_chipset { 212enum asahi_compass_chipset {
94 AK8975, 213 AK8975,
95 AK8963, 214 AK8963,
215 AK09911,
216 AK09912,
217 AK_MAX_TYPE
218};
219
220enum ak_ctrl_reg_addr {
221 ST1,
222 ST2,
223 CNTL,
224 ASA_BASE,
225 MAX_REGS,
226 REGS_END,
227};
228
229enum ak_ctrl_reg_mask {
230 ST1_DRDY,
231 ST2_HOFL,
232 ST2_DERR,
233 CNTL_MODE,
234 MASK_END,
235};
236
237enum ak_ctrl_mode {
238 POWER_DOWN,
239 MODE_ONCE,
240 SELF_TEST,
241 FUSE_ROM,
242 MODE_END,
243};
244
245struct ak_def {
246 enum asahi_compass_chipset type;
247 long (*raw_to_gauss)(u16 data);
248 u16 range;
249 u8 ctrl_regs[REGS_END];
250 u8 ctrl_masks[MASK_END];
251 u8 ctrl_modes[MODE_END];
252 u8 data_regs[3];
253};
254
255static struct ak_def ak_def_array[AK_MAX_TYPE] = {
256 {
257 .type = AK8975,
258 .raw_to_gauss = ak8975_raw_to_gauss,
259 .range = 4096,
260 .ctrl_regs = {
261 AK8975_REG_ST1,
262 AK8975_REG_ST2,
263 AK8975_REG_CNTL,
264 AK8975_REG_ASAX,
265 AK8975_MAX_REGS},
266 .ctrl_masks = {
267 AK8975_REG_ST1_DRDY_MASK,
268 AK8975_REG_ST2_HOFL_MASK,
269 AK8975_REG_ST2_DERR_MASK,
270 AK8975_REG_CNTL_MODE_MASK},
271 .ctrl_modes = {
272 AK8975_REG_CNTL_MODE_POWER_DOWN,
273 AK8975_REG_CNTL_MODE_ONCE,
274 AK8975_REG_CNTL_MODE_SELF_TEST,
275 AK8975_REG_CNTL_MODE_FUSE_ROM},
276 .data_regs = {
277 AK8975_REG_HXL,
278 AK8975_REG_HYL,
279 AK8975_REG_HZL},
280 },
281 {
282 .type = AK8963,
283 .raw_to_gauss = ak8963_09911_raw_to_gauss,
284 .range = 8190,
285 .ctrl_regs = {
286 AK8975_REG_ST1,
287 AK8975_REG_ST2,
288 AK8975_REG_CNTL,
289 AK8975_REG_ASAX,
290 AK8975_MAX_REGS},
291 .ctrl_masks = {
292 AK8975_REG_ST1_DRDY_MASK,
293 AK8975_REG_ST2_HOFL_MASK,
294 0,
295 AK8975_REG_CNTL_MODE_MASK},
296 .ctrl_modes = {
297 AK8975_REG_CNTL_MODE_POWER_DOWN,
298 AK8975_REG_CNTL_MODE_ONCE,
299 AK8975_REG_CNTL_MODE_SELF_TEST,
300 AK8975_REG_CNTL_MODE_FUSE_ROM},
301 .data_regs = {
302 AK8975_REG_HXL,
303 AK8975_REG_HYL,
304 AK8975_REG_HZL},
305 },
306 {
307 .type = AK09911,
308 .raw_to_gauss = ak8963_09911_raw_to_gauss,
309 .range = 8192,
310 .ctrl_regs = {
311 AK09912_REG_ST1,
312 AK09912_REG_ST2,
313 AK09912_REG_CNTL2,
314 AK09912_REG_ASAX,
315 AK09912_MAX_REGS},
316 .ctrl_masks = {
317 AK09912_REG_ST1_DRDY_MASK,
318 AK09912_REG_ST2_HOFL_MASK,
319 0,
320 AK09912_REG_CNTL2_MODE_MASK},
321 .ctrl_modes = {
322 AK09912_REG_CNTL_MODE_POWER_DOWN,
323 AK09912_REG_CNTL_MODE_ONCE,
324 AK09912_REG_CNTL_MODE_SELF_TEST,
325 AK09912_REG_CNTL_MODE_FUSE_ROM},
326 .data_regs = {
327 AK09912_REG_HXL,
328 AK09912_REG_HYL,
329 AK09912_REG_HZL},
330 },
331 {
332 .type = AK09912,
333 .raw_to_gauss = ak09912_raw_to_gauss,
334 .range = 32752,
335 .ctrl_regs = {
336 AK09912_REG_ST1,
337 AK09912_REG_ST2,
338 AK09912_REG_CNTL2,
339 AK09912_REG_ASAX,
340 AK09912_MAX_REGS},
341 .ctrl_masks = {
342 AK09912_REG_ST1_DRDY_MASK,
343 AK09912_REG_ST2_HOFL_MASK,
344 0,
345 AK09912_REG_CNTL2_MODE_MASK},
346 .ctrl_modes = {
347 AK09912_REG_CNTL_MODE_POWER_DOWN,
348 AK09912_REG_CNTL_MODE_ONCE,
349 AK09912_REG_CNTL_MODE_SELF_TEST,
350 AK09912_REG_CNTL_MODE_FUSE_ROM},
351 .data_regs = {
352 AK09912_REG_HXL,
353 AK09912_REG_HYL,
354 AK09912_REG_HZL},
355 }
96}; 356};
97 357
98/* 358/*
@@ -100,40 +360,82 @@ enum asahi_compass_chipset {
100 */ 360 */
101struct ak8975_data { 361struct ak8975_data {
102 struct i2c_client *client; 362 struct i2c_client *client;
363 struct ak_def *def;
103 struct attribute_group attrs; 364 struct attribute_group attrs;
104 struct mutex lock; 365 struct mutex lock;
105 u8 asa[3]; 366 u8 asa[3];
106 long raw_to_gauss[3]; 367 long raw_to_gauss[3];
107 u8 reg_cache[AK8975_MAX_REGS];
108 int eoc_gpio; 368 int eoc_gpio;
109 int eoc_irq; 369 int eoc_irq;
110 wait_queue_head_t data_ready_queue; 370 wait_queue_head_t data_ready_queue;
111 unsigned long flags; 371 unsigned long flags;
112 enum asahi_compass_chipset chipset; 372 u8 cntl_cache;
113}; 373};
114 374
115static const int ak8975_index_to_reg[] = { 375/*
116 AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL, 376 * Return 0 if the i2c device is the one we expect.
117}; 377 * return a negative error number otherwise
378 */
379static int ak8975_who_i_am(struct i2c_client *client,
380 enum asahi_compass_chipset type)
381{
382 u8 wia_val[2];
383 int ret;
384
385 /*
386 * Signature for each device:
387 * Device | WIA1 | WIA2
388 * AK09912 | DEVICE_ID | AK09912_DEVICE_ID
389 * AK09911 | DEVICE_ID | AK09911_DEVICE_ID
390 * AK8975 | DEVICE_ID | NA
391 * AK8963 | DEVICE_ID | NA
392 */
393 ret = i2c_smbus_read_i2c_block_data(client, AK09912_REG_WIA1,
394 2, wia_val);
395 if (ret < 0) {
396 dev_err(&client->dev, "Error reading WIA\n");
397 return ret;
398 }
399
400 if (wia_val[0] != AK8975_DEVICE_ID)
401 return -ENODEV;
402
403 switch (type) {
404 case AK8975:
405 case AK8963:
406 return 0;
407 case AK09911:
408 if (wia_val[1] == AK09911_DEVICE_ID)
409 return 0;
410 break;
411 case AK09912:
412 if (wia_val[1] == AK09912_DEVICE_ID)
413 return 0;
414 break;
415 default:
416 dev_err(&client->dev, "Type %d unknown\n", type);
417 }
418 return -ENODEV;
419}
118 420
119/* 421/*
120 * Helper function to write to the I2C device's registers. 422 * Helper function to write to CNTL register.
121 */ 423 */
122static int ak8975_write_data(struct i2c_client *client, 424static int ak8975_set_mode(struct ak8975_data *data, enum ak_ctrl_mode mode)
123 u8 reg, u8 val, u8 mask, u8 shift)
124{ 425{
125 struct iio_dev *indio_dev = i2c_get_clientdata(client);
126 struct ak8975_data *data = iio_priv(indio_dev);
127 u8 regval; 426 u8 regval;
128 int ret; 427 int ret;
129 428
130 regval = (data->reg_cache[reg] & ~mask) | (val << shift); 429 regval = (data->cntl_cache & ~data->def->ctrl_masks[CNTL_MODE]) |
131 ret = i2c_smbus_write_byte_data(client, reg, regval); 430 data->def->ctrl_modes[mode];
431 ret = i2c_smbus_write_byte_data(data->client,
432 data->def->ctrl_regs[CNTL], regval);
132 if (ret < 0) { 433 if (ret < 0) {
133 dev_err(&client->dev, "Write to device fails status %x\n", ret);
134 return ret; 434 return ret;
135 } 435 }
136 data->reg_cache[reg] = regval; 436 data->cntl_cache = regval;
437 /* After mode change wait atleast 100us */
438 usleep_range(100, 500);
137 439
138 return 0; 440 return 0;
139} 441}
@@ -166,8 +468,8 @@ static int ak8975_setup_irq(struct ak8975_data *data)
166 irq = gpio_to_irq(data->eoc_gpio); 468 irq = gpio_to_irq(data->eoc_gpio);
167 469
168 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, 470 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
169 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 471 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
170 dev_name(&client->dev), data); 472 dev_name(&client->dev), data);
171 if (rc < 0) { 473 if (rc < 0) {
172 dev_err(&client->dev, 474 dev_err(&client->dev,
173 "irq %d request failed, (gpio %d): %d\n", 475 "irq %d request failed, (gpio %d): %d\n",
@@ -191,34 +493,18 @@ static int ak8975_setup(struct i2c_client *client)
191{ 493{
192 struct iio_dev *indio_dev = i2c_get_clientdata(client); 494 struct iio_dev *indio_dev = i2c_get_clientdata(client);
193 struct ak8975_data *data = iio_priv(indio_dev); 495 struct ak8975_data *data = iio_priv(indio_dev);
194 u8 device_id;
195 int ret; 496 int ret;
196 497
197 /* Confirm that the device we're talking to is really an AK8975. */
198 ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
199 if (ret < 0) {
200 dev_err(&client->dev, "Error reading WIA\n");
201 return ret;
202 }
203 device_id = ret;
204 if (device_id != AK8975_DEVICE_ID) {
205 dev_err(&client->dev, "Device ak8975 not found\n");
206 return -ENODEV;
207 }
208
209 /* Write the fused rom access mode. */ 498 /* Write the fused rom access mode. */
210 ret = ak8975_write_data(client, 499 ret = ak8975_set_mode(data, FUSE_ROM);
211 AK8975_REG_CNTL,
212 AK8975_REG_CNTL_MODE_FUSE_ROM,
213 AK8975_REG_CNTL_MODE_MASK,
214 AK8975_REG_CNTL_MODE_SHIFT);
215 if (ret < 0) { 500 if (ret < 0) {
216 dev_err(&client->dev, "Error in setting fuse access mode\n"); 501 dev_err(&client->dev, "Error in setting fuse access mode\n");
217 return ret; 502 return ret;
218 } 503 }
219 504
220 /* Get asa data and store in the device data. */ 505 /* Get asa data and store in the device data. */
221 ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX, 506 ret = i2c_smbus_read_i2c_block_data(client,
507 data->def->ctrl_regs[ASA_BASE],
222 3, data->asa); 508 3, data->asa);
223 if (ret < 0) { 509 if (ret < 0) {
224 dev_err(&client->dev, "Not able to read asa data\n"); 510 dev_err(&client->dev, "Not able to read asa data\n");
@@ -226,13 +512,13 @@ static int ak8975_setup(struct i2c_client *client)
226 } 512 }
227 513
228 /* After reading fuse ROM data set power-down mode */ 514 /* After reading fuse ROM data set power-down mode */
229 ret = ak8975_write_data(client, 515 ret = ak8975_set_mode(data, POWER_DOWN);
230 AK8975_REG_CNTL, 516 if (ret < 0) {
231 AK8975_REG_CNTL_MODE_POWER_DOWN, 517 dev_err(&client->dev, "Error in setting power-down mode\n");
232 AK8975_REG_CNTL_MODE_MASK, 518 return ret;
233 AK8975_REG_CNTL_MODE_SHIFT); 519 }
234 520
235 if (data->eoc_gpio > 0 || client->irq) { 521 if (data->eoc_gpio > 0 || client->irq > 0) {
236 ret = ak8975_setup_irq(data); 522 ret = ak8975_setup_irq(data);
237 if (ret < 0) { 523 if (ret < 0) {
238 dev_err(&client->dev, 524 dev_err(&client->dev,
@@ -241,61 +527,9 @@ static int ak8975_setup(struct i2c_client *client)
241 } 527 }
242 } 528 }
243 529
244 if (ret < 0) { 530 data->raw_to_gauss[0] = data->def->raw_to_gauss(data->asa[0]);
245 dev_err(&client->dev, "Error in setting power-down mode\n"); 531 data->raw_to_gauss[1] = data->def->raw_to_gauss(data->asa[1]);
246 return ret; 532 data->raw_to_gauss[2] = data->def->raw_to_gauss(data->asa[2]);
247 }
248
249/*
250 * Precalculate scale factor (in Gauss units) for each axis and
251 * store in the device data.
252 *
253 * This scale factor is axis-dependent, and is derived from 3 calibration
254 * factors ASA(x), ASA(y), and ASA(z).
255 *
256 * These ASA values are read from the sensor device at start of day, and
257 * cached in the device context struct.
258 *
259 * Adjusting the flux value with the sensitivity adjustment value should be
260 * done via the following formula:
261 *
262 * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
263 *
264 * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
265 * is the resultant adjusted value.
266 *
267 * We reduce the formula to:
268 *
269 * Hadj = H * (ASA + 128) / 256
270 *
271 * H is in the range of -4096 to 4095. The magnetometer has a range of
272 * +-1229uT. To go from the raw value to uT is:
273 *
274 * HuT = H * 1229/4096, or roughly, 3/10.
275 *
276 * Since 1uT = 0.01 gauss, our final scale factor becomes:
277 *
278 * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
279 * Hadj = H * ((ASA + 128) * 0.003) / 256
280 *
281 * Since ASA doesn't change, we cache the resultant scale factor into the
282 * device context in ak8975_setup().
283 */
284 if (data->chipset == AK8963) {
285 /*
286 * H range is +-8190 and magnetometer range is +-4912.
287 * So HuT using the above explanation for 8975,
288 * 4912/8190 = ~ 6/10.
289 * So the Hadj should use 6/10 instead of 3/10.
290 */
291 data->raw_to_gauss[0] = RAW_TO_GAUSS_8963(data->asa[0]);
292 data->raw_to_gauss[1] = RAW_TO_GAUSS_8963(data->asa[1]);
293 data->raw_to_gauss[2] = RAW_TO_GAUSS_8963(data->asa[2]);
294 } else {
295 data->raw_to_gauss[0] = RAW_TO_GAUSS_8975(data->asa[0]);
296 data->raw_to_gauss[1] = RAW_TO_GAUSS_8975(data->asa[1]);
297 data->raw_to_gauss[2] = RAW_TO_GAUSS_8975(data->asa[2]);
298 }
299 533
300 return 0; 534 return 0;
301} 535}
@@ -318,7 +552,7 @@ static int wait_conversion_complete_gpio(struct ak8975_data *data)
318 return -EINVAL; 552 return -EINVAL;
319 } 553 }
320 554
321 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); 555 ret = i2c_smbus_read_byte_data(client, data->def->ctrl_regs[ST1]);
322 if (ret < 0) 556 if (ret < 0)
323 dev_err(&client->dev, "Error in reading ST1\n"); 557 dev_err(&client->dev, "Error in reading ST1\n");
324 558
@@ -335,7 +569,8 @@ static int wait_conversion_complete_polled(struct ak8975_data *data)
335 /* Wait for the conversion to complete. */ 569 /* Wait for the conversion to complete. */
336 while (timeout_ms) { 570 while (timeout_ms) {
337 msleep(AK8975_CONVERSION_DONE_POLL_TIME); 571 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
338 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1); 572 ret = i2c_smbus_read_byte_data(client,
573 data->def->ctrl_regs[ST1]);
339 if (ret < 0) { 574 if (ret < 0) {
340 dev_err(&client->dev, "Error in reading ST1\n"); 575 dev_err(&client->dev, "Error in reading ST1\n");
341 return ret; 576 return ret;
@@ -378,11 +613,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
378 mutex_lock(&data->lock); 613 mutex_lock(&data->lock);
379 614
380 /* Set up the device for taking a sample. */ 615 /* Set up the device for taking a sample. */
381 ret = ak8975_write_data(client, 616 ret = ak8975_set_mode(data, MODE_ONCE);
382 AK8975_REG_CNTL,
383 AK8975_REG_CNTL_MODE_ONCE,
384 AK8975_REG_CNTL_MODE_MASK,
385 AK8975_REG_CNTL_MODE_SHIFT);
386 if (ret < 0) { 617 if (ret < 0) {
387 dev_err(&client->dev, "Error in setting operating mode\n"); 618 dev_err(&client->dev, "Error in setting operating mode\n");
388 goto exit; 619 goto exit;
@@ -399,14 +630,15 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
399 goto exit; 630 goto exit;
400 631
401 /* This will be executed only for non-interrupt based waiting case */ 632 /* This will be executed only for non-interrupt based waiting case */
402 if (ret & AK8975_REG_ST1_DRDY_MASK) { 633 if (ret & data->def->ctrl_masks[ST1_DRDY]) {
403 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2); 634 ret = i2c_smbus_read_byte_data(client,
635 data->def->ctrl_regs[ST2]);
404 if (ret < 0) { 636 if (ret < 0) {
405 dev_err(&client->dev, "Error in reading ST2\n"); 637 dev_err(&client->dev, "Error in reading ST2\n");
406 goto exit; 638 goto exit;
407 } 639 }
408 if (ret & (AK8975_REG_ST2_DERR_MASK | 640 if (ret & (data->def->ctrl_masks[ST2_DERR] |
409 AK8975_REG_ST2_HOFL_MASK)) { 641 data->def->ctrl_masks[ST2_HOFL])) {
410 dev_err(&client->dev, "ST2 status error 0x%x\n", ret); 642 dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
411 ret = -EINVAL; 643 ret = -EINVAL;
412 goto exit; 644 goto exit;
@@ -415,7 +647,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
415 647
416 /* Read the flux value from the appropriate register 648 /* Read the flux value from the appropriate register
417 (the register is specified in the iio device attributes). */ 649 (the register is specified in the iio device attributes). */
418 ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]); 650 ret = i2c_smbus_read_word_data(client, data->def->data_regs[index]);
419 if (ret < 0) { 651 if (ret < 0) {
420 dev_err(&client->dev, "Read axis data fails\n"); 652 dev_err(&client->dev, "Read axis data fails\n");
421 goto exit; 653 goto exit;
@@ -424,7 +656,7 @@ static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
424 mutex_unlock(&data->lock); 656 mutex_unlock(&data->lock);
425 657
426 /* Clamp to valid range. */ 658 /* Clamp to valid range. */
427 *val = clamp_t(s16, ret, -4096, 4095); 659 *val = clamp_t(s16, ret, -data->def->range, data->def->range);
428 return IIO_VAL_INT; 660 return IIO_VAL_INT;
429 661
430exit: 662exit:
@@ -473,6 +705,8 @@ static const struct acpi_device_id ak_acpi_match[] = {
473 {"AK8975", AK8975}, 705 {"AK8975", AK8975},
474 {"AK8963", AK8963}, 706 {"AK8963", AK8963},
475 {"INVN6500", AK8963}, 707 {"INVN6500", AK8963},
708 {"AK09911", AK09911},
709 {"AK09912", AK09912},
476 { }, 710 { },
477}; 711};
478MODULE_DEVICE_TABLE(acpi, ak_acpi_match); 712MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
@@ -498,6 +732,7 @@ static int ak8975_probe(struct i2c_client *client,
498 int eoc_gpio; 732 int eoc_gpio;
499 int err; 733 int err;
500 const char *name = NULL; 734 const char *name = NULL;
735 enum asahi_compass_chipset chipset;
501 736
502 /* Grab and set up the supplied GPIO. */ 737 /* Grab and set up the supplied GPIO. */
503 if (client->dev.platform_data) 738 if (client->dev.platform_data)
@@ -537,42 +772,49 @@ static int ak8975_probe(struct i2c_client *client,
537 772
538 /* id will be NULL when enumerated via ACPI */ 773 /* id will be NULL when enumerated via ACPI */
539 if (id) { 774 if (id) {
540 data->chipset = 775 chipset = (enum asahi_compass_chipset)(id->driver_data);
541 (enum asahi_compass_chipset)(id->driver_data);
542 name = id->name; 776 name = id->name;
543 } else if (ACPI_HANDLE(&client->dev)) 777 } else if (ACPI_HANDLE(&client->dev))
544 name = ak8975_match_acpi_device(&client->dev, &data->chipset); 778 name = ak8975_match_acpi_device(&client->dev, &chipset);
545 else 779 else
546 return -ENOSYS; 780 return -ENOSYS;
547 781
782 if (chipset >= AK_MAX_TYPE) {
783 dev_err(&client->dev, "AKM device type unsupported: %d\n",
784 chipset);
785 return -ENODEV;
786 }
787
788 data->def = &ak_def_array[chipset];
789 err = ak8975_who_i_am(client, data->def->type);
790 if (err < 0) {
791 dev_err(&client->dev, "Unexpected device\n");
792 return err;
793 }
548 dev_dbg(&client->dev, "Asahi compass chip %s\n", name); 794 dev_dbg(&client->dev, "Asahi compass chip %s\n", name);
549 795
550 /* Perform some basic start-of-day setup of the device. */ 796 /* Perform some basic start-of-day setup of the device. */
551 err = ak8975_setup(client); 797 err = ak8975_setup(client);
552 if (err < 0) { 798 if (err < 0) {
553 dev_err(&client->dev, "AK8975 initialization fails\n"); 799 dev_err(&client->dev, "%s initialization fails\n", name);
554 return err; 800 return err;
555 } 801 }
556 802
557 data->client = client;
558 mutex_init(&data->lock); 803 mutex_init(&data->lock);
559 data->eoc_gpio = eoc_gpio;
560 indio_dev->dev.parent = &client->dev; 804 indio_dev->dev.parent = &client->dev;
561 indio_dev->channels = ak8975_channels; 805 indio_dev->channels = ak8975_channels;
562 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); 806 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
563 indio_dev->info = &ak8975_info; 807 indio_dev->info = &ak8975_info;
564 indio_dev->modes = INDIO_DIRECT_MODE; 808 indio_dev->modes = INDIO_DIRECT_MODE;
565 indio_dev->name = name; 809 indio_dev->name = name;
566 err = devm_iio_device_register(&client->dev, indio_dev); 810 return devm_iio_device_register(&client->dev, indio_dev);
567 if (err < 0)
568 return err;
569
570 return 0;
571} 811}
572 812
573static const struct i2c_device_id ak8975_id[] = { 813static const struct i2c_device_id ak8975_id[] = {
574 {"ak8975", AK8975}, 814 {"ak8975", AK8975},
575 {"ak8963", AK8963}, 815 {"ak8963", AK8963},
816 {"ak09911", AK09911},
817 {"ak09912", AK09912},
576 {} 818 {}
577}; 819};
578 820
@@ -581,14 +823,20 @@ MODULE_DEVICE_TABLE(i2c, ak8975_id);
581static const struct of_device_id ak8975_of_match[] = { 823static const struct of_device_id ak8975_of_match[] = {
582 { .compatible = "asahi-kasei,ak8975", }, 824 { .compatible = "asahi-kasei,ak8975", },
583 { .compatible = "ak8975", }, 825 { .compatible = "ak8975", },
584 { } 826 { .compatible = "asahi-kasei,ak8963", },
827 { .compatible = "ak8963", },
828 { .compatible = "asahi-kasei,ak09911", },
829 { .compatible = "ak09911", },
830 { .compatible = "asahi-kasei,ak09912", },
831 { .compatible = "ak09912", },
832 {}
585}; 833};
586MODULE_DEVICE_TABLE(of, ak8975_of_match); 834MODULE_DEVICE_TABLE(of, ak8975_of_match);
587 835
588static struct i2c_driver ak8975_driver = { 836static struct i2c_driver ak8975_driver = {
589 .driver = { 837 .driver = {
590 .name = "ak8975", 838 .name = "ak8975",
591 .of_match_table = ak8975_of_match, 839 .of_match_table = of_match_ptr(ak8975_of_match),
592 .acpi_match_table = ACPI_PTR(ak_acpi_match), 840 .acpi_match_table = ACPI_PTR(ak_acpi_match),
593 }, 841 },
594 .probe = ak8975_probe, 842 .probe = ak8975_probe,
diff --git a/drivers/iio/pressure/bmp280.c b/drivers/iio/pressure/bmp280.c
index 75038dacfff1..7c623e2bd633 100644
--- a/drivers/iio/pressure/bmp280.c
+++ b/drivers/iio/pressure/bmp280.c
@@ -80,16 +80,12 @@ struct bmp280_data {
80 s32 t_fine; 80 s32 t_fine;
81}; 81};
82 82
83/* Compensation parameters. */ 83/*
84struct bmp280_comp_temp { 84 * These enums are used for indexing into the array of compensation
85 u16 dig_t1; 85 * parameters.
86 s16 dig_t2, dig_t3; 86 */
87}; 87enum { T1, T2, T3 };
88 88enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
89struct bmp280_comp_press {
90 u16 dig_p1;
91 s16 dig_p2, dig_p3, dig_p4, dig_p5, dig_p6, dig_p7, dig_p8, dig_p9;
92};
93 89
94static const struct iio_chan_spec bmp280_channels[] = { 90static const struct iio_chan_spec bmp280_channels[] = {
95 { 91 {
@@ -141,54 +137,6 @@ static const struct regmap_config bmp280_regmap_config = {
141 .volatile_reg = bmp280_is_volatile_reg, 137 .volatile_reg = bmp280_is_volatile_reg,
142}; 138};
143 139
144static int bmp280_read_compensation_temp(struct bmp280_data *data,
145 struct bmp280_comp_temp *comp)
146{
147 int ret;
148 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
149
150 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
151 buf, BMP280_COMP_TEMP_REG_COUNT);
152 if (ret < 0) {
153 dev_err(&data->client->dev,
154 "failed to read temperature calibration parameters\n");
155 return ret;
156 }
157
158 comp->dig_t1 = (u16) le16_to_cpu(buf[0]);
159 comp->dig_t2 = (s16) le16_to_cpu(buf[1]);
160 comp->dig_t3 = (s16) le16_to_cpu(buf[2]);
161
162 return 0;
163}
164
165static int bmp280_read_compensation_press(struct bmp280_data *data,
166 struct bmp280_comp_press *comp)
167{
168 int ret;
169 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
170
171 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
172 buf, BMP280_COMP_PRESS_REG_COUNT);
173 if (ret < 0) {
174 dev_err(&data->client->dev,
175 "failed to read pressure calibration parameters\n");
176 return ret;
177 }
178
179 comp->dig_p1 = (u16) le16_to_cpu(buf[0]);
180 comp->dig_p2 = (s16) le16_to_cpu(buf[1]);
181 comp->dig_p3 = (s16) le16_to_cpu(buf[2]);
182 comp->dig_p4 = (s16) le16_to_cpu(buf[3]);
183 comp->dig_p5 = (s16) le16_to_cpu(buf[4]);
184 comp->dig_p6 = (s16) le16_to_cpu(buf[5]);
185 comp->dig_p7 = (s16) le16_to_cpu(buf[6]);
186 comp->dig_p8 = (s16) le16_to_cpu(buf[7]);
187 comp->dig_p9 = (s16) le16_to_cpu(buf[8]);
188
189 return 0;
190}
191
192/* 140/*
193 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of 141 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
194 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global 142 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
@@ -197,21 +145,35 @@ static int bmp280_read_compensation_press(struct bmp280_data *data,
197 * Taken from datasheet, Section 3.11.3, "Compensation formula". 145 * Taken from datasheet, Section 3.11.3, "Compensation formula".
198 */ 146 */
199static s32 bmp280_compensate_temp(struct bmp280_data *data, 147static s32 bmp280_compensate_temp(struct bmp280_data *data,
200 struct bmp280_comp_temp *comp,
201 s32 adc_temp) 148 s32 adc_temp)
202{ 149{
203 s32 var1, var2, t; 150 int ret;
151 s32 var1, var2;
152 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
204 153
205 var1 = (((adc_temp >> 3) - ((s32) comp->dig_t1 << 1)) * 154 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
206 ((s32) comp->dig_t2)) >> 11; 155 buf, BMP280_COMP_TEMP_REG_COUNT);
207 var2 = (((((adc_temp >> 4) - ((s32) comp->dig_t1)) * 156 if (ret < 0) {
208 ((adc_temp >> 4) - ((s32) comp->dig_t1))) >> 12) * 157 dev_err(&data->client->dev,
209 ((s32) comp->dig_t3)) >> 14; 158 "failed to read temperature calibration parameters\n");
159 return ret;
160 }
210 161
211 data->t_fine = var1 + var2; 162 /*
212 t = (data->t_fine * 5 + 128) >> 8; 163 * The double casts are necessary because le16_to_cpu returns an
164 * unsigned 16-bit value. Casting that value directly to a
165 * signed 32-bit will not do proper sign extension.
166 *
167 * Conversely, T1 and P1 are unsigned values, so they can be
168 * cast straight to the larger type.
169 */
170 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
171 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
172 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
173 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
174 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
213 175
214 return t; 176 return (data->t_fine * 5 + 128) >> 8;
215} 177}
216 178
217/* 179/*
@@ -222,29 +184,38 @@ static s32 bmp280_compensate_temp(struct bmp280_data *data,
222 * Taken from datasheet, Section 3.11.3, "Compensation formula". 184 * Taken from datasheet, Section 3.11.3, "Compensation formula".
223 */ 185 */
224static u32 bmp280_compensate_press(struct bmp280_data *data, 186static u32 bmp280_compensate_press(struct bmp280_data *data,
225 struct bmp280_comp_press *comp,
226 s32 adc_press) 187 s32 adc_press)
227{ 188{
189 int ret;
228 s64 var1, var2, p; 190 s64 var1, var2, p;
191 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
192
193 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
194 buf, BMP280_COMP_PRESS_REG_COUNT);
195 if (ret < 0) {
196 dev_err(&data->client->dev,
197 "failed to read pressure calibration parameters\n");
198 return ret;
199 }
229 200
230 var1 = ((s64) data->t_fine) - 128000; 201 var1 = ((s64)data->t_fine) - 128000;
231 var2 = var1 * var1 * (s64) comp->dig_p6; 202 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
232 var2 = var2 + ((var1 * (s64) comp->dig_p5) << 17); 203 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
233 var2 = var2 + (((s64) comp->dig_p4) << 35); 204 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
234 var1 = ((var1 * var1 * (s64) comp->dig_p3) >> 8) + 205 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
235 ((var1 * (s64) comp->dig_p2) << 12); 206 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
236 var1 = (((((s64) 1) << 47) + var1)) * ((s64) comp->dig_p1) >> 33; 207 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
237 208
238 if (var1 == 0) 209 if (var1 == 0)
239 return 0; 210 return 0;
240 211
241 p = ((((s64) 1048576 - adc_press) << 31) - var2) * 3125; 212 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
242 p = div64_s64(p, var1); 213 p = div64_s64(p, var1);
243 var1 = (((s64) comp->dig_p9) * (p >> 13) * (p >> 13)) >> 25; 214 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
244 var2 = (((s64) comp->dig_p8) * p) >> 19; 215 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
245 p = ((p + var1 + var2) >> 8) + (((s64) comp->dig_p7) << 4); 216 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
246 217
247 return (u32) p; 218 return (u32)p;
248} 219}
249 220
250static int bmp280_read_temp(struct bmp280_data *data, 221static int bmp280_read_temp(struct bmp280_data *data,
@@ -253,11 +224,6 @@ static int bmp280_read_temp(struct bmp280_data *data,
253 int ret; 224 int ret;
254 __be32 tmp = 0; 225 __be32 tmp = 0;
255 s32 adc_temp, comp_temp; 226 s32 adc_temp, comp_temp;
256 struct bmp280_comp_temp comp;
257
258 ret = bmp280_read_compensation_temp(data, &comp);
259 if (ret < 0)
260 return ret;
261 227
262 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, 228 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
263 (u8 *) &tmp, 3); 229 (u8 *) &tmp, 3);
@@ -267,7 +233,7 @@ static int bmp280_read_temp(struct bmp280_data *data,
267 } 233 }
268 234
269 adc_temp = be32_to_cpu(tmp) >> 12; 235 adc_temp = be32_to_cpu(tmp) >> 12;
270 comp_temp = bmp280_compensate_temp(data, &comp, adc_temp); 236 comp_temp = bmp280_compensate_temp(data, adc_temp);
271 237
272 /* 238 /*
273 * val might be NULL if we're called by the read_press routine, 239 * val might be NULL if we're called by the read_press routine,
@@ -288,11 +254,6 @@ static int bmp280_read_press(struct bmp280_data *data,
288 __be32 tmp = 0; 254 __be32 tmp = 0;
289 s32 adc_press; 255 s32 adc_press;
290 u32 comp_press; 256 u32 comp_press;
291 struct bmp280_comp_press comp;
292
293 ret = bmp280_read_compensation_press(data, &comp);
294 if (ret < 0)
295 return ret;
296 257
297 /* Read and compensate temperature so we get a reading of t_fine. */ 258 /* Read and compensate temperature so we get a reading of t_fine. */
298 ret = bmp280_read_temp(data, NULL); 259 ret = bmp280_read_temp(data, NULL);
@@ -307,7 +268,7 @@ static int bmp280_read_press(struct bmp280_data *data,
307 } 268 }
308 269
309 adc_press = be32_to_cpu(tmp) >> 12; 270 adc_press = be32_to_cpu(tmp) >> 12;
310 comp_press = bmp280_compensate_press(data, &comp, adc_press); 271 comp_press = bmp280_compensate_press(data, adc_press);
311 272
312 *val = comp_press; 273 *val = comp_press;
313 *val2 = 256000; 274 *val2 = 256000;
@@ -366,7 +327,7 @@ static int bmp280_chip_init(struct bmp280_data *data)
366 BMP280_MODE_NORMAL); 327 BMP280_MODE_NORMAL);
367 if (ret < 0) { 328 if (ret < 0) {
368 dev_err(&data->client->dev, 329 dev_err(&data->client->dev,
369 "failed to write config register\n"); 330 "failed to write ctrl_meas register\n");
370 return ret; 331 return ret;
371 } 332 }
372 333
@@ -394,7 +355,6 @@ static int bmp280_probe(struct i2c_client *client,
394 if (!indio_dev) 355 if (!indio_dev)
395 return -ENOMEM; 356 return -ENOMEM;
396 357
397 i2c_set_clientdata(client, indio_dev);
398 data = iio_priv(indio_dev); 358 data = iio_priv(indio_dev);
399 mutex_init(&data->lock); 359 mutex_init(&data->lock);
400 data->client = client; 360 data->client = client;
diff --git a/drivers/iio/proximity/Kconfig b/drivers/iio/proximity/Kconfig
index 0c8cdf58f6a1..41a8d8ffa0de 100644
--- a/drivers/iio/proximity/Kconfig
+++ b/drivers/iio/proximity/Kconfig
@@ -17,3 +17,20 @@ config AS3935
17 module will be called as3935 17 module will be called as3935
18 18
19endmenu 19endmenu
20
21menu "Proximity sensors"
22
23config SX9500
24 tristate "SX9500 Semtech proximity sensor"
25 select IIO_BUFFER
26 select IIO_TRIGGERED_BUFFER
27 select REGMAP_I2C
28 depends on I2C
29 help
30 Say Y here to build a driver for Semtech's SX9500 capacitive
31 proximity/button sensor.
32
33 To compile this driver as a module, choose M here: the
34 module will be called sx9500.
35
36endmenu
diff --git a/drivers/iio/proximity/Makefile b/drivers/iio/proximity/Makefile
index 743adee1c8bf..9818dc562abd 100644
--- a/drivers/iio/proximity/Makefile
+++ b/drivers/iio/proximity/Makefile
@@ -4,3 +4,4 @@
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_AS3935) += as3935.o 6obj-$(CONFIG_AS3935) += as3935.o
7obj-$(CONFIG_SX9500) += sx9500.o
diff --git a/drivers/iio/proximity/sx9500.c b/drivers/iio/proximity/sx9500.c
new file mode 100644
index 000000000000..74dff4e4a11a
--- /dev/null
+++ b/drivers/iio/proximity/sx9500.c
@@ -0,0 +1,752 @@
1/*
2 * Copyright (c) 2014 Intel Corporation
3 *
4 * Driver for Semtech's SX9500 capacitive proximity/button solution.
5 * Datasheet available at
6 * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
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
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/module.h>
16#include <linux/i2c.h>
17#include <linux/irq.h>
18#include <linux/acpi.h>
19#include <linux/gpio/consumer.h>
20#include <linux/regmap.h>
21
22#include <linux/iio/iio.h>
23#include <linux/iio/buffer.h>
24#include <linux/iio/sysfs.h>
25#include <linux/iio/events.h>
26#include <linux/iio/trigger.h>
27#include <linux/iio/triggered_buffer.h>
28#include <linux/iio/trigger_consumer.h>
29
30#define SX9500_DRIVER_NAME "sx9500"
31#define SX9500_IRQ_NAME "sx9500_event"
32#define SX9500_GPIO_NAME "sx9500_gpio"
33
34/* Register definitions. */
35#define SX9500_REG_IRQ_SRC 0x00
36#define SX9500_REG_STAT 0x01
37#define SX9500_REG_IRQ_MSK 0x03
38
39#define SX9500_REG_PROX_CTRL0 0x06
40#define SX9500_REG_PROX_CTRL1 0x07
41#define SX9500_REG_PROX_CTRL2 0x08
42#define SX9500_REG_PROX_CTRL3 0x09
43#define SX9500_REG_PROX_CTRL4 0x0a
44#define SX9500_REG_PROX_CTRL5 0x0b
45#define SX9500_REG_PROX_CTRL6 0x0c
46#define SX9500_REG_PROX_CTRL7 0x0d
47#define SX9500_REG_PROX_CTRL8 0x0e
48
49#define SX9500_REG_SENSOR_SEL 0x20
50#define SX9500_REG_USE_MSB 0x21
51#define SX9500_REG_USE_LSB 0x22
52#define SX9500_REG_AVG_MSB 0x23
53#define SX9500_REG_AVG_LSB 0x24
54#define SX9500_REG_DIFF_MSB 0x25
55#define SX9500_REG_DIFF_LSB 0x26
56#define SX9500_REG_OFFSET_MSB 0x27
57#define SX9500_REG_OFFSET_LSB 0x28
58
59#define SX9500_REG_RESET 0x7f
60
61/* Write this to REG_RESET to do a soft reset. */
62#define SX9500_SOFT_RESET 0xde
63
64#define SX9500_SCAN_PERIOD_MASK GENMASK(6, 4)
65#define SX9500_SCAN_PERIOD_SHIFT 4
66
67/*
68 * These serve for identifying IRQ source in the IRQ_SRC register, and
69 * also for masking the IRQs in the IRQ_MSK register.
70 */
71#define SX9500_CLOSE_IRQ BIT(6)
72#define SX9500_FAR_IRQ BIT(5)
73#define SX9500_CONVDONE_IRQ BIT(3)
74
75#define SX9500_PROXSTAT_SHIFT 4
76
77#define SX9500_NUM_CHANNELS 4
78
79struct sx9500_data {
80 struct mutex mutex;
81 struct i2c_client *client;
82 struct iio_trigger *trig;
83 struct regmap *regmap;
84 /*
85 * Last reading of the proximity status for each channel. We
86 * only send an event to user space when this changes.
87 */
88 bool prox_stat[SX9500_NUM_CHANNELS];
89 bool event_enabled[SX9500_NUM_CHANNELS];
90 bool trigger_enabled;
91 u16 *buffer;
92};
93
94static const struct iio_event_spec sx9500_events[] = {
95 {
96 .type = IIO_EV_TYPE_THRESH,
97 .dir = IIO_EV_DIR_EITHER,
98 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
99 },
100};
101
102#define SX9500_CHANNEL(idx) \
103 { \
104 .type = IIO_PROXIMITY, \
105 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
106 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
107 .indexed = 1, \
108 .channel = idx, \
109 .event_spec = sx9500_events, \
110 .num_event_specs = ARRAY_SIZE(sx9500_events), \
111 .scan_index = idx, \
112 .scan_type = { \
113 .sign = 'u', \
114 .realbits = 16, \
115 .storagebits = 16, \
116 .shift = 0, \
117 }, \
118 }
119
120static const struct iio_chan_spec sx9500_channels[] = {
121 SX9500_CHANNEL(0),
122 SX9500_CHANNEL(1),
123 SX9500_CHANNEL(2),
124 SX9500_CHANNEL(3),
125 IIO_CHAN_SOFT_TIMESTAMP(4),
126};
127
128static const struct {
129 int val;
130 int val2;
131} sx9500_samp_freq_table[] = {
132 {33, 333333},
133 {16, 666666},
134 {11, 111111},
135 {8, 333333},
136 {6, 666666},
137 {5, 0},
138 {3, 333333},
139 {2, 500000},
140};
141
142static const struct regmap_range sx9500_writable_reg_ranges[] = {
143 regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK),
144 regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8),
145 regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL),
146 regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB),
147 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
148};
149
150static const struct regmap_access_table sx9500_writeable_regs = {
151 .yes_ranges = sx9500_writable_reg_ranges,
152 .n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges),
153};
154
155/*
156 * All allocated registers are readable, so we just list unallocated
157 * ones.
158 */
159static const struct regmap_range sx9500_non_readable_reg_ranges[] = {
160 regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1),
161 regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
162 regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
163 regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
164};
165
166static const struct regmap_access_table sx9500_readable_regs = {
167 .no_ranges = sx9500_non_readable_reg_ranges,
168 .n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges),
169};
170
171static const struct regmap_range sx9500_volatile_reg_ranges[] = {
172 regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT),
173 regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB),
174 regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
175};
176
177static const struct regmap_access_table sx9500_volatile_regs = {
178 .yes_ranges = sx9500_volatile_reg_ranges,
179 .n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges),
180};
181
182static const struct regmap_config sx9500_regmap_config = {
183 .reg_bits = 8,
184 .val_bits = 8,
185
186 .max_register = SX9500_REG_RESET,
187 .cache_type = REGCACHE_RBTREE,
188
189 .wr_table = &sx9500_writeable_regs,
190 .rd_table = &sx9500_readable_regs,
191 .volatile_table = &sx9500_volatile_regs,
192};
193
194static int sx9500_read_proximity(struct sx9500_data *data,
195 const struct iio_chan_spec *chan,
196 int *val)
197{
198 int ret;
199 __be16 regval;
200
201 ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel);
202 if (ret < 0)
203 return ret;
204
205 ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, &regval, 2);
206 if (ret < 0)
207 return ret;
208
209 *val = 32767 - (s16)be16_to_cpu(regval);
210
211 return IIO_VAL_INT;
212}
213
214static int sx9500_read_samp_freq(struct sx9500_data *data,
215 int *val, int *val2)
216{
217 int ret;
218 unsigned int regval;
219
220 mutex_lock(&data->mutex);
221 ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &regval);
222 mutex_unlock(&data->mutex);
223
224 if (ret < 0)
225 return ret;
226
227 regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
228 *val = sx9500_samp_freq_table[regval].val;
229 *val2 = sx9500_samp_freq_table[regval].val2;
230
231 return IIO_VAL_INT_PLUS_MICRO;
232}
233
234static int sx9500_read_raw(struct iio_dev *indio_dev,
235 const struct iio_chan_spec *chan,
236 int *val, int *val2, long mask)
237{
238 struct sx9500_data *data = iio_priv(indio_dev);
239 int ret;
240
241 switch (chan->type) {
242 case IIO_PROXIMITY:
243 switch (mask) {
244 case IIO_CHAN_INFO_RAW:
245 if (iio_buffer_enabled(indio_dev))
246 return -EBUSY;
247 mutex_lock(&data->mutex);
248 ret = sx9500_read_proximity(data, chan, val);
249 mutex_unlock(&data->mutex);
250 return ret;
251 case IIO_CHAN_INFO_SAMP_FREQ:
252 return sx9500_read_samp_freq(data, val, val2);
253 default:
254 return -EINVAL;
255 }
256 default:
257 return -EINVAL;
258 }
259}
260
261static int sx9500_set_samp_freq(struct sx9500_data *data,
262 int val, int val2)
263{
264 int i, ret;
265
266 for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
267 if (val == sx9500_samp_freq_table[i].val &&
268 val2 == sx9500_samp_freq_table[i].val2)
269 break;
270
271 if (i == ARRAY_SIZE(sx9500_samp_freq_table))
272 return -EINVAL;
273
274 mutex_lock(&data->mutex);
275
276 ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
277 SX9500_SCAN_PERIOD_MASK,
278 i << SX9500_SCAN_PERIOD_SHIFT);
279
280 mutex_unlock(&data->mutex);
281
282 return ret;
283}
284
285static int sx9500_write_raw(struct iio_dev *indio_dev,
286 const struct iio_chan_spec *chan,
287 int val, int val2, long mask)
288{
289 struct sx9500_data *data = iio_priv(indio_dev);
290
291 switch (chan->type) {
292 case IIO_PROXIMITY:
293 switch (mask) {
294 case IIO_CHAN_INFO_SAMP_FREQ:
295 return sx9500_set_samp_freq(data, val, val2);
296 default:
297 return -EINVAL;
298 }
299 default:
300 return -EINVAL;
301 }
302}
303
304static irqreturn_t sx9500_irq_handler(int irq, void *private)
305{
306 struct iio_dev *indio_dev = private;
307 struct sx9500_data *data = iio_priv(indio_dev);
308
309 if (data->trigger_enabled)
310 iio_trigger_poll(data->trig);
311
312 /*
313 * Even if no event is enabled, we need to wake the thread to
314 * clear the interrupt state by reading SX9500_REG_IRQ_SRC. It
315 * is not possible to do that here because regmap_read takes a
316 * mutex.
317 */
318 return IRQ_WAKE_THREAD;
319}
320
321static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
322{
323 struct iio_dev *indio_dev = private;
324 struct sx9500_data *data = iio_priv(indio_dev);
325 int ret;
326 unsigned int val, chan;
327
328 mutex_lock(&data->mutex);
329
330 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
331 if (ret < 0) {
332 dev_err(&data->client->dev, "i2c transfer error in irq\n");
333 goto out;
334 }
335
336 if (!(val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ)))
337 goto out;
338
339 ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
340 if (ret < 0) {
341 dev_err(&data->client->dev, "i2c transfer error in irq\n");
342 goto out;
343 }
344
345 val >>= SX9500_PROXSTAT_SHIFT;
346 for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
347 int dir;
348 u64 ev;
349 bool new_prox = val & BIT(chan);
350
351 if (!data->event_enabled[chan])
352 continue;
353 if (new_prox == data->prox_stat[chan])
354 /* No change on this channel. */
355 continue;
356
357 dir = new_prox ? IIO_EV_DIR_FALLING :
358 IIO_EV_DIR_RISING;
359 ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY,
360 chan,
361 IIO_EV_TYPE_THRESH,
362 dir);
363 iio_push_event(indio_dev, ev, iio_get_time_ns());
364 data->prox_stat[chan] = new_prox;
365 }
366
367out:
368 mutex_unlock(&data->mutex);
369
370 return IRQ_HANDLED;
371}
372
373static int sx9500_read_event_config(struct iio_dev *indio_dev,
374 const struct iio_chan_spec *chan,
375 enum iio_event_type type,
376 enum iio_event_direction dir)
377{
378 struct sx9500_data *data = iio_priv(indio_dev);
379
380 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
381 dir != IIO_EV_DIR_EITHER)
382 return -EINVAL;
383
384 return data->event_enabled[chan->channel];
385}
386
387static int sx9500_write_event_config(struct iio_dev *indio_dev,
388 const struct iio_chan_spec *chan,
389 enum iio_event_type type,
390 enum iio_event_direction dir,
391 int state)
392{
393 struct sx9500_data *data = iio_priv(indio_dev);
394 int ret, i;
395 bool any_active = false;
396 unsigned int irqmask;
397
398 if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
399 dir != IIO_EV_DIR_EITHER)
400 return -EINVAL;
401
402 mutex_lock(&data->mutex);
403
404 data->event_enabled[chan->channel] = state;
405
406 for (i = 0; i < SX9500_NUM_CHANNELS; i++)
407 if (data->event_enabled[i]) {
408 any_active = true;
409 break;
410 }
411
412 irqmask = SX9500_CLOSE_IRQ | SX9500_FAR_IRQ;
413 if (any_active)
414 ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK,
415 irqmask, irqmask);
416 else
417 ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK,
418 irqmask, 0);
419
420 mutex_unlock(&data->mutex);
421
422 return ret;
423}
424
425static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
426 const unsigned long *scan_mask)
427{
428 struct sx9500_data *data = iio_priv(indio_dev);
429
430 mutex_lock(&data->mutex);
431 kfree(data->buffer);
432 data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
433 mutex_unlock(&data->mutex);
434
435 if (data->buffer == NULL)
436 return -ENOMEM;
437
438 return 0;
439}
440
441static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
442 "2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
443
444static struct attribute *sx9500_attributes[] = {
445 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
446 NULL,
447};
448
449static const struct attribute_group sx9500_attribute_group = {
450 .attrs = sx9500_attributes,
451};
452
453static const struct iio_info sx9500_info = {
454 .driver_module = THIS_MODULE,
455 .attrs = &sx9500_attribute_group,
456 .read_raw = &sx9500_read_raw,
457 .write_raw = &sx9500_write_raw,
458 .read_event_config = &sx9500_read_event_config,
459 .write_event_config = &sx9500_write_event_config,
460 .update_scan_mode = &sx9500_update_scan_mode,
461};
462
463static int sx9500_set_trigger_state(struct iio_trigger *trig,
464 bool state)
465{
466 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
467 struct sx9500_data *data = iio_priv(indio_dev);
468 int ret;
469
470 mutex_lock(&data->mutex);
471
472 ret = regmap_update_bits(data->regmap, SX9500_REG_IRQ_MSK,
473 SX9500_CONVDONE_IRQ,
474 state ? SX9500_CONVDONE_IRQ : 0);
475 if (ret == 0)
476 data->trigger_enabled = state;
477
478 mutex_unlock(&data->mutex);
479
480 return ret;
481}
482
483static const struct iio_trigger_ops sx9500_trigger_ops = {
484 .set_trigger_state = sx9500_set_trigger_state,
485 .owner = THIS_MODULE,
486};
487
488static irqreturn_t sx9500_trigger_handler(int irq, void *private)
489{
490 struct iio_poll_func *pf = private;
491 struct iio_dev *indio_dev = pf->indio_dev;
492 struct sx9500_data *data = iio_priv(indio_dev);
493 int val, bit, ret, i = 0;
494
495 mutex_lock(&data->mutex);
496
497 for_each_set_bit(bit, indio_dev->buffer->scan_mask,
498 indio_dev->masklength) {
499 ret = sx9500_read_proximity(data, &indio_dev->channels[bit],
500 &val);
501 if (ret < 0)
502 goto out;
503
504 data->buffer[i++] = val;
505 }
506
507 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
508 iio_get_time_ns());
509
510out:
511 mutex_unlock(&data->mutex);
512
513 iio_trigger_notify_done(indio_dev->trig);
514
515 return IRQ_HANDLED;
516}
517
518struct sx9500_reg_default {
519 u8 reg;
520 u8 def;
521};
522
523static const struct sx9500_reg_default sx9500_default_regs[] = {
524 {
525 .reg = SX9500_REG_PROX_CTRL1,
526 /* Shield enabled, small range. */
527 .def = 0x43,
528 },
529 {
530 .reg = SX9500_REG_PROX_CTRL2,
531 /* x8 gain, 167kHz frequency, finest resolution. */
532 .def = 0x77,
533 },
534 {
535 .reg = SX9500_REG_PROX_CTRL3,
536 /* Doze enabled, 2x scan period doze, no raw filter. */
537 .def = 0x40,
538 },
539 {
540 .reg = SX9500_REG_PROX_CTRL4,
541 /* Average threshold. */
542 .def = 0x30,
543 },
544 {
545 .reg = SX9500_REG_PROX_CTRL5,
546 /*
547 * Debouncer off, lowest average negative filter,
548 * highest average postive filter.
549 */
550 .def = 0x0f,
551 },
552 {
553 .reg = SX9500_REG_PROX_CTRL6,
554 /* Proximity detection threshold: 280 */
555 .def = 0x0e,
556 },
557 {
558 .reg = SX9500_REG_PROX_CTRL7,
559 /*
560 * No automatic compensation, compensate each pin
561 * independently, proximity hysteresis: 32, close
562 * debouncer off, far debouncer off.
563 */
564 .def = 0x00,
565 },
566 {
567 .reg = SX9500_REG_PROX_CTRL8,
568 /* No stuck timeout, no periodic compensation. */
569 .def = 0x00,
570 },
571 {
572 .reg = SX9500_REG_PROX_CTRL0,
573 /* Scan period: 30ms, all sensors enabled. */
574 .def = 0x0f,
575 },
576};
577
578static int sx9500_init_device(struct iio_dev *indio_dev)
579{
580 struct sx9500_data *data = iio_priv(indio_dev);
581 int ret, i;
582 unsigned int val;
583
584 ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
585 if (ret < 0)
586 return ret;
587
588 ret = regmap_write(data->regmap, SX9500_REG_RESET,
589 SX9500_SOFT_RESET);
590 if (ret < 0)
591 return ret;
592
593 ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
594 if (ret < 0)
595 return ret;
596
597 for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
598 ret = regmap_write(data->regmap,
599 sx9500_default_regs[i].reg,
600 sx9500_default_regs[i].def);
601 if (ret < 0)
602 return ret;
603 }
604
605 return 0;
606}
607
608static int sx9500_gpio_probe(struct i2c_client *client,
609 struct sx9500_data *data)
610{
611 struct device *dev;
612 struct gpio_desc *gpio;
613 int ret;
614
615 if (!client)
616 return -EINVAL;
617
618 dev = &client->dev;
619
620 /* data ready gpio interrupt pin */
621 gpio = devm_gpiod_get_index(dev, SX9500_GPIO_NAME, 0);
622 if (IS_ERR(gpio)) {
623 dev_err(dev, "acpi gpio get index failed\n");
624 return PTR_ERR(gpio);
625 }
626
627 ret = gpiod_direction_input(gpio);
628 if (ret)
629 return ret;
630
631 ret = gpiod_to_irq(gpio);
632
633 dev_dbg(dev, "GPIO resource, no:%d irq:%d\n", desc_to_gpio(gpio), ret);
634
635 return ret;
636}
637
638static int sx9500_probe(struct i2c_client *client,
639 const struct i2c_device_id *id)
640{
641 int ret;
642 struct iio_dev *indio_dev;
643 struct sx9500_data *data;
644
645 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
646 if (indio_dev == NULL)
647 return -ENOMEM;
648
649 data = iio_priv(indio_dev);
650 data->client = client;
651 mutex_init(&data->mutex);
652 data->trigger_enabled = false;
653
654 data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
655 if (IS_ERR(data->regmap))
656 return PTR_ERR(data->regmap);
657
658 sx9500_init_device(indio_dev);
659
660 indio_dev->dev.parent = &client->dev;
661 indio_dev->name = SX9500_DRIVER_NAME;
662 indio_dev->channels = sx9500_channels;
663 indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
664 indio_dev->info = &sx9500_info;
665 indio_dev->modes = INDIO_DIRECT_MODE;
666 i2c_set_clientdata(client, indio_dev);
667
668 if (client->irq <= 0)
669 client->irq = sx9500_gpio_probe(client, data);
670
671 if (client->irq > 0) {
672 ret = devm_request_threaded_irq(&client->dev, client->irq,
673 sx9500_irq_handler, sx9500_irq_thread_handler,
674 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
675 SX9500_IRQ_NAME, indio_dev);
676 if (ret < 0)
677 return ret;
678
679 data->trig = devm_iio_trigger_alloc(&client->dev,
680 "%s-dev%d", indio_dev->name, indio_dev->id);
681 if (!data->trig)
682 return -ENOMEM;
683
684 data->trig->dev.parent = &client->dev;
685 data->trig->ops = &sx9500_trigger_ops;
686 iio_trigger_set_drvdata(data->trig, indio_dev);
687
688 ret = iio_trigger_register(data->trig);
689 if (ret)
690 return ret;
691 }
692
693 ret = iio_triggered_buffer_setup(indio_dev, NULL,
694 sx9500_trigger_handler, NULL);
695 if (ret < 0)
696 goto out_trigger_unregister;
697
698 ret = iio_device_register(indio_dev);
699 if (ret < 0)
700 goto out_buffer_cleanup;
701
702 return 0;
703
704out_buffer_cleanup:
705 iio_triggered_buffer_cleanup(indio_dev);
706out_trigger_unregister:
707 if (client->irq > 0)
708 iio_trigger_unregister(data->trig);
709
710 return ret;
711}
712
713static int sx9500_remove(struct i2c_client *client)
714{
715 struct iio_dev *indio_dev = i2c_get_clientdata(client);
716 struct sx9500_data *data = iio_priv(indio_dev);
717
718 iio_device_unregister(indio_dev);
719 iio_triggered_buffer_cleanup(indio_dev);
720 if (client->irq > 0)
721 iio_trigger_unregister(data->trig);
722 kfree(data->buffer);
723
724 return 0;
725}
726
727static const struct acpi_device_id sx9500_acpi_match[] = {
728 {"SSX9500", 0},
729 { },
730};
731MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
732
733static const struct i2c_device_id sx9500_id[] = {
734 {"sx9500", 0},
735 {}
736};
737MODULE_DEVICE_TABLE(i2c, sx9500_id);
738
739static struct i2c_driver sx9500_driver = {
740 .driver = {
741 .name = SX9500_DRIVER_NAME,
742 .acpi_match_table = ACPI_PTR(sx9500_acpi_match),
743 },
744 .probe = sx9500_probe,
745 .remove = sx9500_remove,
746 .id_table = sx9500_id,
747};
748module_i2c_driver(sx9500_driver);
749
750MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
751MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
752MODULE_LICENSE("GPL v2");
diff --git a/drivers/iio/trigger/iio-trig-sysfs.c b/drivers/iio/trigger/iio-trig-sysfs.c
index 254c7e906127..3dfab2bc6d69 100644
--- a/drivers/iio/trigger/iio-trig-sysfs.c
+++ b/drivers/iio/trigger/iio-trig-sysfs.c
@@ -135,6 +135,7 @@ static int iio_sysfs_trigger_probe(int id)
135 struct iio_sysfs_trig *t; 135 struct iio_sysfs_trig *t;
136 int ret; 136 int ret;
137 bool foundit = false; 137 bool foundit = false;
138
138 mutex_lock(&iio_sysfs_trig_list_mut); 139 mutex_lock(&iio_sysfs_trig_list_mut);
139 list_for_each_entry(t, &iio_sysfs_trig_list, l) 140 list_for_each_entry(t, &iio_sysfs_trig_list, l)
140 if (id == t->id) { 141 if (id == t->id) {
@@ -185,6 +186,7 @@ static int iio_sysfs_trigger_remove(int id)
185{ 186{
186 bool foundit = false; 187 bool foundit = false;
187 struct iio_sysfs_trig *t; 188 struct iio_sysfs_trig *t;
189
188 mutex_lock(&iio_sysfs_trig_list_mut); 190 mutex_lock(&iio_sysfs_trig_list_mut);
189 list_for_each_entry(t, &iio_sysfs_trig_list, l) 191 list_for_each_entry(t, &iio_sysfs_trig_list, l)
190 if (id == t->id) { 192 if (id == t->id) {